perf_cpum_sf.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance event support for the System z CPU-measurement Sampling Facility
  4. *
  5. * Copyright IBM Corp. 2013, 2018
  6. * Author(s): Hendrik Brueckner <[email protected]>
  7. */
  8. #define KMSG_COMPONENT "cpum_sf"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/percpu.h>
  14. #include <linux/pid.h>
  15. #include <linux/notifier.h>
  16. #include <linux/export.h>
  17. #include <linux/slab.h>
  18. #include <linux/mm.h>
  19. #include <linux/moduleparam.h>
  20. #include <asm/cpu_mf.h>
  21. #include <asm/irq.h>
  22. #include <asm/debug.h>
  23. #include <asm/timex.h>
  24. /* Minimum number of sample-data-block-tables:
  25. * At least one table is required for the sampling buffer structure.
  26. * A single table contains up to 511 pointers to sample-data-blocks.
  27. */
  28. #define CPUM_SF_MIN_SDBT 1
  29. /* Number of sample-data-blocks per sample-data-block-table (SDBT):
  30. * A table contains SDB pointers (8 bytes) and one table-link entry
  31. * that points to the origin of the next SDBT.
  32. */
  33. #define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8)
  34. /* Maximum page offset for an SDBT table-link entry:
  35. * If this page offset is reached, a table-link entry to the next SDBT
  36. * must be added.
  37. */
  38. #define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8)
  39. static inline int require_table_link(const void *sdbt)
  40. {
  41. return ((unsigned long) sdbt & ~PAGE_MASK) == CPUM_SF_SDBT_TL_OFFSET;
  42. }
  43. /* Minimum and maximum sampling buffer sizes:
  44. *
  45. * This number represents the maximum size of the sampling buffer taking
  46. * the number of sample-data-block-tables into account. Note that these
  47. * numbers apply to the basic-sampling function only.
  48. * The maximum number of SDBs is increased by CPUM_SF_SDB_DIAG_FACTOR if
  49. * the diagnostic-sampling function is active.
  50. *
  51. * Sampling buffer size Buffer characteristics
  52. * ---------------------------------------------------
  53. * 64KB == 16 pages (4KB per page)
  54. * 1 page for SDB-tables
  55. * 15 pages for SDBs
  56. *
  57. * 32MB == 8192 pages (4KB per page)
  58. * 16 pages for SDB-tables
  59. * 8176 pages for SDBs
  60. */
  61. static unsigned long __read_mostly CPUM_SF_MIN_SDB = 15;
  62. static unsigned long __read_mostly CPUM_SF_MAX_SDB = 8176;
  63. static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR = 1;
  64. struct sf_buffer {
  65. unsigned long *sdbt; /* Sample-data-block-table origin */
  66. /* buffer characteristics (required for buffer increments) */
  67. unsigned long num_sdb; /* Number of sample-data-blocks */
  68. unsigned long num_sdbt; /* Number of sample-data-block-tables */
  69. unsigned long *tail; /* last sample-data-block-table */
  70. };
  71. struct aux_buffer {
  72. struct sf_buffer sfb;
  73. unsigned long head; /* index of SDB of buffer head */
  74. unsigned long alert_mark; /* index of SDB of alert request position */
  75. unsigned long empty_mark; /* mark of SDB not marked full */
  76. unsigned long *sdb_index; /* SDB address for fast lookup */
  77. unsigned long *sdbt_index; /* SDBT address for fast lookup */
  78. };
  79. struct cpu_hw_sf {
  80. /* CPU-measurement sampling information block */
  81. struct hws_qsi_info_block qsi;
  82. /* CPU-measurement sampling control block */
  83. struct hws_lsctl_request_block lsctl;
  84. struct sf_buffer sfb; /* Sampling buffer */
  85. unsigned int flags; /* Status flags */
  86. struct perf_event *event; /* Scheduled perf event */
  87. struct perf_output_handle handle; /* AUX buffer output handle */
  88. };
  89. static DEFINE_PER_CPU(struct cpu_hw_sf, cpu_hw_sf);
  90. /* Debug feature */
  91. static debug_info_t *sfdbg;
  92. /*
  93. * sf_disable() - Switch off sampling facility
  94. */
  95. static int sf_disable(void)
  96. {
  97. struct hws_lsctl_request_block sreq;
  98. memset(&sreq, 0, sizeof(sreq));
  99. return lsctl(&sreq);
  100. }
  101. /*
  102. * sf_buffer_available() - Check for an allocated sampling buffer
  103. */
  104. static int sf_buffer_available(struct cpu_hw_sf *cpuhw)
  105. {
  106. return !!cpuhw->sfb.sdbt;
  107. }
  108. /*
  109. * deallocate sampling facility buffer
  110. */
  111. static void free_sampling_buffer(struct sf_buffer *sfb)
  112. {
  113. unsigned long *sdbt, *curr;
  114. if (!sfb->sdbt)
  115. return;
  116. sdbt = sfb->sdbt;
  117. curr = sdbt;
  118. /* Free the SDBT after all SDBs are processed... */
  119. while (1) {
  120. if (!*curr || !sdbt)
  121. break;
  122. /* Process table-link entries */
  123. if (is_link_entry(curr)) {
  124. curr = get_next_sdbt(curr);
  125. if (sdbt)
  126. free_page((unsigned long) sdbt);
  127. /* If the origin is reached, sampling buffer is freed */
  128. if (curr == sfb->sdbt)
  129. break;
  130. else
  131. sdbt = curr;
  132. } else {
  133. /* Process SDB pointer */
  134. if (*curr) {
  135. free_page(*curr);
  136. curr++;
  137. }
  138. }
  139. }
  140. debug_sprintf_event(sfdbg, 5, "%s: freed sdbt %#lx\n", __func__,
  141. (unsigned long)sfb->sdbt);
  142. memset(sfb, 0, sizeof(*sfb));
  143. }
  144. static int alloc_sample_data_block(unsigned long *sdbt, gfp_t gfp_flags)
  145. {
  146. struct hws_trailer_entry *te;
  147. unsigned long sdb;
  148. /* Allocate and initialize sample-data-block */
  149. sdb = get_zeroed_page(gfp_flags);
  150. if (!sdb)
  151. return -ENOMEM;
  152. te = (struct hws_trailer_entry *)trailer_entry_ptr(sdb);
  153. te->header.a = 1;
  154. /* Link SDB into the sample-data-block-table */
  155. *sdbt = sdb;
  156. return 0;
  157. }
  158. /*
  159. * realloc_sampling_buffer() - extend sampler memory
  160. *
  161. * Allocates new sample-data-blocks and adds them to the specified sampling
  162. * buffer memory.
  163. *
  164. * Important: This modifies the sampling buffer and must be called when the
  165. * sampling facility is disabled.
  166. *
  167. * Returns zero on success, non-zero otherwise.
  168. */
  169. static int realloc_sampling_buffer(struct sf_buffer *sfb,
  170. unsigned long num_sdb, gfp_t gfp_flags)
  171. {
  172. int i, rc;
  173. unsigned long *new, *tail, *tail_prev = NULL;
  174. if (!sfb->sdbt || !sfb->tail)
  175. return -EINVAL;
  176. if (!is_link_entry(sfb->tail))
  177. return -EINVAL;
  178. /* Append to the existing sampling buffer, overwriting the table-link
  179. * register.
  180. * The tail variables always points to the "tail" (last and table-link)
  181. * entry in an SDB-table.
  182. */
  183. tail = sfb->tail;
  184. /* Do a sanity check whether the table-link entry points to
  185. * the sampling buffer origin.
  186. */
  187. if (sfb->sdbt != get_next_sdbt(tail)) {
  188. debug_sprintf_event(sfdbg, 3, "%s: "
  189. "sampling buffer is not linked: origin %#lx"
  190. " tail %#lx\n", __func__,
  191. (unsigned long)sfb->sdbt,
  192. (unsigned long)tail);
  193. return -EINVAL;
  194. }
  195. /* Allocate remaining SDBs */
  196. rc = 0;
  197. for (i = 0; i < num_sdb; i++) {
  198. /* Allocate a new SDB-table if it is full. */
  199. if (require_table_link(tail)) {
  200. new = (unsigned long *) get_zeroed_page(gfp_flags);
  201. if (!new) {
  202. rc = -ENOMEM;
  203. break;
  204. }
  205. sfb->num_sdbt++;
  206. /* Link current page to tail of chain */
  207. *tail = (unsigned long)(void *) new + 1;
  208. tail_prev = tail;
  209. tail = new;
  210. }
  211. /* Allocate a new sample-data-block.
  212. * If there is not enough memory, stop the realloc process
  213. * and simply use what was allocated. If this is a temporary
  214. * issue, a new realloc call (if required) might succeed.
  215. */
  216. rc = alloc_sample_data_block(tail, gfp_flags);
  217. if (rc) {
  218. /* Undo last SDBT. An SDBT with no SDB at its first
  219. * entry but with an SDBT entry instead can not be
  220. * handled by the interrupt handler code.
  221. * Avoid this situation.
  222. */
  223. if (tail_prev) {
  224. sfb->num_sdbt--;
  225. free_page((unsigned long) new);
  226. tail = tail_prev;
  227. }
  228. break;
  229. }
  230. sfb->num_sdb++;
  231. tail++;
  232. tail_prev = new = NULL; /* Allocated at least one SBD */
  233. }
  234. /* Link sampling buffer to its origin */
  235. *tail = (unsigned long) sfb->sdbt + 1;
  236. sfb->tail = tail;
  237. debug_sprintf_event(sfdbg, 4, "%s: new buffer"
  238. " settings: sdbt %lu sdb %lu\n", __func__,
  239. sfb->num_sdbt, sfb->num_sdb);
  240. return rc;
  241. }
  242. /*
  243. * allocate_sampling_buffer() - allocate sampler memory
  244. *
  245. * Allocates and initializes a sampling buffer structure using the
  246. * specified number of sample-data-blocks (SDB). For each allocation,
  247. * a 4K page is used. The number of sample-data-block-tables (SDBT)
  248. * are calculated from SDBs.
  249. * Also set the ALERT_REQ mask in each SDBs trailer.
  250. *
  251. * Returns zero on success, non-zero otherwise.
  252. */
  253. static int alloc_sampling_buffer(struct sf_buffer *sfb, unsigned long num_sdb)
  254. {
  255. int rc;
  256. if (sfb->sdbt)
  257. return -EINVAL;
  258. /* Allocate the sample-data-block-table origin */
  259. sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  260. if (!sfb->sdbt)
  261. return -ENOMEM;
  262. sfb->num_sdb = 0;
  263. sfb->num_sdbt = 1;
  264. /* Link the table origin to point to itself to prepare for
  265. * realloc_sampling_buffer() invocation.
  266. */
  267. sfb->tail = sfb->sdbt;
  268. *sfb->tail = (unsigned long)(void *) sfb->sdbt + 1;
  269. /* Allocate requested number of sample-data-blocks */
  270. rc = realloc_sampling_buffer(sfb, num_sdb, GFP_KERNEL);
  271. if (rc) {
  272. free_sampling_buffer(sfb);
  273. debug_sprintf_event(sfdbg, 4, "%s: "
  274. "realloc_sampling_buffer failed with rc %i\n",
  275. __func__, rc);
  276. } else
  277. debug_sprintf_event(sfdbg, 4,
  278. "%s: tear %#lx dear %#lx\n", __func__,
  279. (unsigned long)sfb->sdbt, (unsigned long)*sfb->sdbt);
  280. return rc;
  281. }
  282. static void sfb_set_limits(unsigned long min, unsigned long max)
  283. {
  284. struct hws_qsi_info_block si;
  285. CPUM_SF_MIN_SDB = min;
  286. CPUM_SF_MAX_SDB = max;
  287. memset(&si, 0, sizeof(si));
  288. if (!qsi(&si))
  289. CPUM_SF_SDB_DIAG_FACTOR = DIV_ROUND_UP(si.dsdes, si.bsdes);
  290. }
  291. static unsigned long sfb_max_limit(struct hw_perf_event *hwc)
  292. {
  293. return SAMPL_DIAG_MODE(hwc) ? CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR
  294. : CPUM_SF_MAX_SDB;
  295. }
  296. static unsigned long sfb_pending_allocs(struct sf_buffer *sfb,
  297. struct hw_perf_event *hwc)
  298. {
  299. if (!sfb->sdbt)
  300. return SFB_ALLOC_REG(hwc);
  301. if (SFB_ALLOC_REG(hwc) > sfb->num_sdb)
  302. return SFB_ALLOC_REG(hwc) - sfb->num_sdb;
  303. return 0;
  304. }
  305. static int sfb_has_pending_allocs(struct sf_buffer *sfb,
  306. struct hw_perf_event *hwc)
  307. {
  308. return sfb_pending_allocs(sfb, hwc) > 0;
  309. }
  310. static void sfb_account_allocs(unsigned long num, struct hw_perf_event *hwc)
  311. {
  312. /* Limit the number of SDBs to not exceed the maximum */
  313. num = min_t(unsigned long, num, sfb_max_limit(hwc) - SFB_ALLOC_REG(hwc));
  314. if (num)
  315. SFB_ALLOC_REG(hwc) += num;
  316. }
  317. static void sfb_init_allocs(unsigned long num, struct hw_perf_event *hwc)
  318. {
  319. SFB_ALLOC_REG(hwc) = 0;
  320. sfb_account_allocs(num, hwc);
  321. }
  322. static void deallocate_buffers(struct cpu_hw_sf *cpuhw)
  323. {
  324. if (cpuhw->sfb.sdbt)
  325. free_sampling_buffer(&cpuhw->sfb);
  326. }
  327. static int allocate_buffers(struct cpu_hw_sf *cpuhw, struct hw_perf_event *hwc)
  328. {
  329. unsigned long n_sdb, freq;
  330. size_t sample_size;
  331. /* Calculate sampling buffers using 4K pages
  332. *
  333. * 1. The sampling size is 32 bytes for basic sampling. This size
  334. * is the same for all machine types. Diagnostic
  335. * sampling uses auxlilary data buffer setup which provides the
  336. * memory for SDBs using linux common code auxiliary trace
  337. * setup.
  338. *
  339. * 2. Function alloc_sampling_buffer() sets the Alert Request
  340. * Control indicator to trigger a measurement-alert to harvest
  341. * sample-data-blocks (SDB). This is done per SDB. This
  342. * measurement alert interrupt fires quick enough to handle
  343. * one SDB, on very high frequency and work loads there might
  344. * be 2 to 3 SBDs available for sample processing.
  345. * Currently there is no need for setup alert request on every
  346. * n-th page. This is counterproductive as one IRQ triggers
  347. * a very high number of samples to be processed at one IRQ.
  348. *
  349. * 3. Use the sampling frequency as input.
  350. * Compute the number of SDBs and ensure a minimum
  351. * of CPUM_SF_MIN_SDB. Depending on frequency add some more
  352. * SDBs to handle a higher sampling rate.
  353. * Use a minimum of CPUM_SF_MIN_SDB and allow for 100 samples
  354. * (one SDB) for every 10000 HZ frequency increment.
  355. *
  356. * 4. Compute the number of sample-data-block-tables (SDBT) and
  357. * ensure a minimum of CPUM_SF_MIN_SDBT (one table can manage up
  358. * to 511 SDBs).
  359. */
  360. sample_size = sizeof(struct hws_basic_entry);
  361. freq = sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc));
  362. n_sdb = CPUM_SF_MIN_SDB + DIV_ROUND_UP(freq, 10000);
  363. /* If there is already a sampling buffer allocated, it is very likely
  364. * that the sampling facility is enabled too. If the event to be
  365. * initialized requires a greater sampling buffer, the allocation must
  366. * be postponed. Changing the sampling buffer requires the sampling
  367. * facility to be in the disabled state. So, account the number of
  368. * required SDBs and let cpumsf_pmu_enable() resize the buffer just
  369. * before the event is started.
  370. */
  371. sfb_init_allocs(n_sdb, hwc);
  372. if (sf_buffer_available(cpuhw))
  373. return 0;
  374. debug_sprintf_event(sfdbg, 3,
  375. "%s: rate %lu f %lu sdb %lu/%lu"
  376. " sample_size %lu cpuhw %p\n", __func__,
  377. SAMPL_RATE(hwc), freq, n_sdb, sfb_max_limit(hwc),
  378. sample_size, cpuhw);
  379. return alloc_sampling_buffer(&cpuhw->sfb,
  380. sfb_pending_allocs(&cpuhw->sfb, hwc));
  381. }
  382. static unsigned long min_percent(unsigned int percent, unsigned long base,
  383. unsigned long min)
  384. {
  385. return min_t(unsigned long, min, DIV_ROUND_UP(percent * base, 100));
  386. }
  387. static unsigned long compute_sfb_extent(unsigned long ratio, unsigned long base)
  388. {
  389. /* Use a percentage-based approach to extend the sampling facility
  390. * buffer. Accept up to 5% sample data loss.
  391. * Vary the extents between 1% to 5% of the current number of
  392. * sample-data-blocks.
  393. */
  394. if (ratio <= 5)
  395. return 0;
  396. if (ratio <= 25)
  397. return min_percent(1, base, 1);
  398. if (ratio <= 50)
  399. return min_percent(1, base, 1);
  400. if (ratio <= 75)
  401. return min_percent(2, base, 2);
  402. if (ratio <= 100)
  403. return min_percent(3, base, 3);
  404. if (ratio <= 250)
  405. return min_percent(4, base, 4);
  406. return min_percent(5, base, 8);
  407. }
  408. static void sfb_account_overflows(struct cpu_hw_sf *cpuhw,
  409. struct hw_perf_event *hwc)
  410. {
  411. unsigned long ratio, num;
  412. if (!OVERFLOW_REG(hwc))
  413. return;
  414. /* The sample_overflow contains the average number of sample data
  415. * that has been lost because sample-data-blocks were full.
  416. *
  417. * Calculate the total number of sample data entries that has been
  418. * discarded. Then calculate the ratio of lost samples to total samples
  419. * per second in percent.
  420. */
  421. ratio = DIV_ROUND_UP(100 * OVERFLOW_REG(hwc) * cpuhw->sfb.num_sdb,
  422. sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc)));
  423. /* Compute number of sample-data-blocks */
  424. num = compute_sfb_extent(ratio, cpuhw->sfb.num_sdb);
  425. if (num)
  426. sfb_account_allocs(num, hwc);
  427. debug_sprintf_event(sfdbg, 5, "%s: overflow %llu ratio %lu num %lu\n",
  428. __func__, OVERFLOW_REG(hwc), ratio, num);
  429. OVERFLOW_REG(hwc) = 0;
  430. }
  431. /* extend_sampling_buffer() - Extend sampling buffer
  432. * @sfb: Sampling buffer structure (for local CPU)
  433. * @hwc: Perf event hardware structure
  434. *
  435. * Use this function to extend the sampling buffer based on the overflow counter
  436. * and postponed allocation extents stored in the specified Perf event hardware.
  437. *
  438. * Important: This function disables the sampling facility in order to safely
  439. * change the sampling buffer structure. Do not call this function
  440. * when the PMU is active.
  441. */
  442. static void extend_sampling_buffer(struct sf_buffer *sfb,
  443. struct hw_perf_event *hwc)
  444. {
  445. unsigned long num, num_old;
  446. int rc;
  447. num = sfb_pending_allocs(sfb, hwc);
  448. if (!num)
  449. return;
  450. num_old = sfb->num_sdb;
  451. /* Disable the sampling facility to reset any states and also
  452. * clear pending measurement alerts.
  453. */
  454. sf_disable();
  455. /* Extend the sampling buffer.
  456. * This memory allocation typically happens in an atomic context when
  457. * called by perf. Because this is a reallocation, it is fine if the
  458. * new SDB-request cannot be satisfied immediately.
  459. */
  460. rc = realloc_sampling_buffer(sfb, num, GFP_ATOMIC);
  461. if (rc)
  462. debug_sprintf_event(sfdbg, 5, "%s: realloc failed with rc %i\n",
  463. __func__, rc);
  464. if (sfb_has_pending_allocs(sfb, hwc))
  465. debug_sprintf_event(sfdbg, 5, "%s: "
  466. "req %lu alloc %lu remaining %lu\n",
  467. __func__, num, sfb->num_sdb - num_old,
  468. sfb_pending_allocs(sfb, hwc));
  469. }
  470. /* Number of perf events counting hardware events */
  471. static atomic_t num_events;
  472. /* Used to avoid races in calling reserve/release_cpumf_hardware */
  473. static DEFINE_MUTEX(pmc_reserve_mutex);
  474. #define PMC_INIT 0
  475. #define PMC_RELEASE 1
  476. #define PMC_FAILURE 2
  477. static void setup_pmc_cpu(void *flags)
  478. {
  479. int err;
  480. struct cpu_hw_sf *cpusf = this_cpu_ptr(&cpu_hw_sf);
  481. err = 0;
  482. switch (*((int *) flags)) {
  483. case PMC_INIT:
  484. memset(cpusf, 0, sizeof(*cpusf));
  485. err = qsi(&cpusf->qsi);
  486. if (err)
  487. break;
  488. cpusf->flags |= PMU_F_RESERVED;
  489. err = sf_disable();
  490. if (err)
  491. pr_err("Switching off the sampling facility failed "
  492. "with rc %i\n", err);
  493. debug_sprintf_event(sfdbg, 5,
  494. "%s: initialized: cpuhw %p\n", __func__,
  495. cpusf);
  496. break;
  497. case PMC_RELEASE:
  498. cpusf->flags &= ~PMU_F_RESERVED;
  499. err = sf_disable();
  500. if (err) {
  501. pr_err("Switching off the sampling facility failed "
  502. "with rc %i\n", err);
  503. } else
  504. deallocate_buffers(cpusf);
  505. debug_sprintf_event(sfdbg, 5,
  506. "%s: released: cpuhw %p\n", __func__,
  507. cpusf);
  508. break;
  509. }
  510. if (err)
  511. *((int *) flags) |= PMC_FAILURE;
  512. }
  513. static void release_pmc_hardware(void)
  514. {
  515. int flags = PMC_RELEASE;
  516. irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  517. on_each_cpu(setup_pmc_cpu, &flags, 1);
  518. }
  519. static int reserve_pmc_hardware(void)
  520. {
  521. int flags = PMC_INIT;
  522. on_each_cpu(setup_pmc_cpu, &flags, 1);
  523. if (flags & PMC_FAILURE) {
  524. release_pmc_hardware();
  525. return -ENODEV;
  526. }
  527. irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  528. return 0;
  529. }
  530. static void hw_perf_event_destroy(struct perf_event *event)
  531. {
  532. /* Release PMC if this is the last perf event */
  533. if (!atomic_add_unless(&num_events, -1, 1)) {
  534. mutex_lock(&pmc_reserve_mutex);
  535. if (atomic_dec_return(&num_events) == 0)
  536. release_pmc_hardware();
  537. mutex_unlock(&pmc_reserve_mutex);
  538. }
  539. }
  540. static void hw_init_period(struct hw_perf_event *hwc, u64 period)
  541. {
  542. hwc->sample_period = period;
  543. hwc->last_period = hwc->sample_period;
  544. local64_set(&hwc->period_left, hwc->sample_period);
  545. }
  546. static unsigned long hw_limit_rate(const struct hws_qsi_info_block *si,
  547. unsigned long rate)
  548. {
  549. return clamp_t(unsigned long, rate,
  550. si->min_sampl_rate, si->max_sampl_rate);
  551. }
  552. static u32 cpumsf_pid_type(struct perf_event *event,
  553. u32 pid, enum pid_type type)
  554. {
  555. struct task_struct *tsk;
  556. /* Idle process */
  557. if (!pid)
  558. goto out;
  559. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  560. pid = -1;
  561. if (tsk) {
  562. /*
  563. * Only top level events contain the pid namespace in which
  564. * they are created.
  565. */
  566. if (event->parent)
  567. event = event->parent;
  568. pid = __task_pid_nr_ns(tsk, type, event->ns);
  569. /*
  570. * See also 1d953111b648
  571. * "perf/core: Don't report zero PIDs for exiting tasks".
  572. */
  573. if (!pid && !pid_alive(tsk))
  574. pid = -1;
  575. }
  576. out:
  577. return pid;
  578. }
  579. static void cpumsf_output_event_pid(struct perf_event *event,
  580. struct perf_sample_data *data,
  581. struct pt_regs *regs)
  582. {
  583. u32 pid;
  584. struct perf_event_header header;
  585. struct perf_output_handle handle;
  586. /*
  587. * Obtain the PID from the basic-sampling data entry and
  588. * correct the data->tid_entry.pid value.
  589. */
  590. pid = data->tid_entry.pid;
  591. /* Protect callchain buffers, tasks */
  592. rcu_read_lock();
  593. perf_prepare_sample(&header, data, event, regs);
  594. if (perf_output_begin(&handle, data, event, header.size))
  595. goto out;
  596. /* Update the process ID (see also kernel/events/core.c) */
  597. data->tid_entry.pid = cpumsf_pid_type(event, pid, PIDTYPE_TGID);
  598. data->tid_entry.tid = cpumsf_pid_type(event, pid, PIDTYPE_PID);
  599. perf_output_sample(&handle, &header, data, event);
  600. perf_output_end(&handle);
  601. out:
  602. rcu_read_unlock();
  603. }
  604. static unsigned long getrate(bool freq, unsigned long sample,
  605. struct hws_qsi_info_block *si)
  606. {
  607. unsigned long rate;
  608. if (freq) {
  609. rate = freq_to_sample_rate(si, sample);
  610. rate = hw_limit_rate(si, rate);
  611. } else {
  612. /* The min/max sampling rates specifies the valid range
  613. * of sample periods. If the specified sample period is
  614. * out of range, limit the period to the range boundary.
  615. */
  616. rate = hw_limit_rate(si, sample);
  617. /* The perf core maintains a maximum sample rate that is
  618. * configurable through the sysctl interface. Ensure the
  619. * sampling rate does not exceed this value. This also helps
  620. * to avoid throttling when pushing samples with
  621. * perf_event_overflow().
  622. */
  623. if (sample_rate_to_freq(si, rate) >
  624. sysctl_perf_event_sample_rate) {
  625. debug_sprintf_event(sfdbg, 1, "%s: "
  626. "Sampling rate exceeds maximum "
  627. "perf sample rate\n", __func__);
  628. rate = 0;
  629. }
  630. }
  631. return rate;
  632. }
  633. /* The sampling information (si) contains information about the
  634. * min/max sampling intervals and the CPU speed. So calculate the
  635. * correct sampling interval and avoid the whole period adjust
  636. * feedback loop.
  637. *
  638. * Since the CPU Measurement sampling facility can not handle frequency
  639. * calculate the sampling interval when frequency is specified using
  640. * this formula:
  641. * interval := cpu_speed * 1000000 / sample_freq
  642. *
  643. * Returns errno on bad input and zero on success with parameter interval
  644. * set to the correct sampling rate.
  645. *
  646. * Note: This function turns off freq bit to avoid calling function
  647. * perf_adjust_period(). This causes frequency adjustment in the common
  648. * code part which causes tremendous variations in the counter values.
  649. */
  650. static int __hw_perf_event_init_rate(struct perf_event *event,
  651. struct hws_qsi_info_block *si)
  652. {
  653. struct perf_event_attr *attr = &event->attr;
  654. struct hw_perf_event *hwc = &event->hw;
  655. unsigned long rate;
  656. if (attr->freq) {
  657. if (!attr->sample_freq)
  658. return -EINVAL;
  659. rate = getrate(attr->freq, attr->sample_freq, si);
  660. attr->freq = 0; /* Don't call perf_adjust_period() */
  661. SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FREQ_MODE;
  662. } else {
  663. rate = getrate(attr->freq, attr->sample_period, si);
  664. if (!rate)
  665. return -EINVAL;
  666. }
  667. attr->sample_period = rate;
  668. SAMPL_RATE(hwc) = rate;
  669. hw_init_period(hwc, SAMPL_RATE(hwc));
  670. debug_sprintf_event(sfdbg, 4, "%s: cpu %d period %#llx freq %d,%#lx\n",
  671. __func__, event->cpu, event->attr.sample_period,
  672. event->attr.freq, SAMPLE_FREQ_MODE(hwc));
  673. return 0;
  674. }
  675. static int __hw_perf_event_init(struct perf_event *event)
  676. {
  677. struct cpu_hw_sf *cpuhw;
  678. struct hws_qsi_info_block si;
  679. struct perf_event_attr *attr = &event->attr;
  680. struct hw_perf_event *hwc = &event->hw;
  681. int cpu, err;
  682. /* Reserve CPU-measurement sampling facility */
  683. err = 0;
  684. if (!atomic_inc_not_zero(&num_events)) {
  685. mutex_lock(&pmc_reserve_mutex);
  686. if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
  687. err = -EBUSY;
  688. else
  689. atomic_inc(&num_events);
  690. mutex_unlock(&pmc_reserve_mutex);
  691. }
  692. event->destroy = hw_perf_event_destroy;
  693. if (err)
  694. goto out;
  695. /* Access per-CPU sampling information (query sampling info) */
  696. /*
  697. * The event->cpu value can be -1 to count on every CPU, for example,
  698. * when attaching to a task. If this is specified, use the query
  699. * sampling info from the current CPU, otherwise use event->cpu to
  700. * retrieve the per-CPU information.
  701. * Later, cpuhw indicates whether to allocate sampling buffers for a
  702. * particular CPU (cpuhw!=NULL) or each online CPU (cpuw==NULL).
  703. */
  704. memset(&si, 0, sizeof(si));
  705. cpuhw = NULL;
  706. if (event->cpu == -1)
  707. qsi(&si);
  708. else {
  709. /* Event is pinned to a particular CPU, retrieve the per-CPU
  710. * sampling structure for accessing the CPU-specific QSI.
  711. */
  712. cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
  713. si = cpuhw->qsi;
  714. }
  715. /* Check sampling facility authorization and, if not authorized,
  716. * fall back to other PMUs. It is safe to check any CPU because
  717. * the authorization is identical for all configured CPUs.
  718. */
  719. if (!si.as) {
  720. err = -ENOENT;
  721. goto out;
  722. }
  723. if (si.ribm & CPU_MF_SF_RIBM_NOTAV) {
  724. pr_warn("CPU Measurement Facility sampling is temporarily not available\n");
  725. err = -EBUSY;
  726. goto out;
  727. }
  728. /* Always enable basic sampling */
  729. SAMPL_FLAGS(hwc) = PERF_CPUM_SF_BASIC_MODE;
  730. /* Check if diagnostic sampling is requested. Deny if the required
  731. * sampling authorization is missing.
  732. */
  733. if (attr->config == PERF_EVENT_CPUM_SF_DIAG) {
  734. if (!si.ad) {
  735. err = -EPERM;
  736. goto out;
  737. }
  738. SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_DIAG_MODE;
  739. }
  740. /* Check and set other sampling flags */
  741. if (attr->config1 & PERF_CPUM_SF_FULL_BLOCKS)
  742. SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FULL_BLOCKS;
  743. err = __hw_perf_event_init_rate(event, &si);
  744. if (err)
  745. goto out;
  746. /* Initialize sample data overflow accounting */
  747. hwc->extra_reg.reg = REG_OVERFLOW;
  748. OVERFLOW_REG(hwc) = 0;
  749. /* Use AUX buffer. No need to allocate it by ourself */
  750. if (attr->config == PERF_EVENT_CPUM_SF_DIAG)
  751. return 0;
  752. /* Allocate the per-CPU sampling buffer using the CPU information
  753. * from the event. If the event is not pinned to a particular
  754. * CPU (event->cpu == -1; or cpuhw == NULL), allocate sampling
  755. * buffers for each online CPU.
  756. */
  757. if (cpuhw)
  758. /* Event is pinned to a particular CPU */
  759. err = allocate_buffers(cpuhw, hwc);
  760. else {
  761. /* Event is not pinned, allocate sampling buffer on
  762. * each online CPU
  763. */
  764. for_each_online_cpu(cpu) {
  765. cpuhw = &per_cpu(cpu_hw_sf, cpu);
  766. err = allocate_buffers(cpuhw, hwc);
  767. if (err)
  768. break;
  769. }
  770. }
  771. /* If PID/TID sampling is active, replace the default overflow
  772. * handler to extract and resolve the PIDs from the basic-sampling
  773. * data entries.
  774. */
  775. if (event->attr.sample_type & PERF_SAMPLE_TID)
  776. if (is_default_overflow_handler(event))
  777. event->overflow_handler = cpumsf_output_event_pid;
  778. out:
  779. return err;
  780. }
  781. static bool is_callchain_event(struct perf_event *event)
  782. {
  783. u64 sample_type = event->attr.sample_type;
  784. return sample_type & (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_REGS_USER |
  785. PERF_SAMPLE_STACK_USER);
  786. }
  787. static int cpumsf_pmu_event_init(struct perf_event *event)
  788. {
  789. int err;
  790. /* No support for taken branch sampling */
  791. /* No support for callchain, stacks and registers */
  792. if (has_branch_stack(event) || is_callchain_event(event))
  793. return -EOPNOTSUPP;
  794. switch (event->attr.type) {
  795. case PERF_TYPE_RAW:
  796. if ((event->attr.config != PERF_EVENT_CPUM_SF) &&
  797. (event->attr.config != PERF_EVENT_CPUM_SF_DIAG))
  798. return -ENOENT;
  799. break;
  800. case PERF_TYPE_HARDWARE:
  801. /* Support sampling of CPU cycles in addition to the
  802. * counter facility. However, the counter facility
  803. * is more precise and, hence, restrict this PMU to
  804. * sampling events only.
  805. */
  806. if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES)
  807. return -ENOENT;
  808. if (!is_sampling_event(event))
  809. return -ENOENT;
  810. break;
  811. default:
  812. return -ENOENT;
  813. }
  814. /* Check online status of the CPU to which the event is pinned */
  815. if (event->cpu >= 0 && !cpu_online(event->cpu))
  816. return -ENODEV;
  817. /* Force reset of idle/hv excludes regardless of what the
  818. * user requested.
  819. */
  820. if (event->attr.exclude_hv)
  821. event->attr.exclude_hv = 0;
  822. if (event->attr.exclude_idle)
  823. event->attr.exclude_idle = 0;
  824. err = __hw_perf_event_init(event);
  825. if (unlikely(err))
  826. if (event->destroy)
  827. event->destroy(event);
  828. return err;
  829. }
  830. static void cpumsf_pmu_enable(struct pmu *pmu)
  831. {
  832. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  833. struct hw_perf_event *hwc;
  834. int err;
  835. if (cpuhw->flags & PMU_F_ENABLED)
  836. return;
  837. if (cpuhw->flags & PMU_F_ERR_MASK)
  838. return;
  839. /* Check whether to extent the sampling buffer.
  840. *
  841. * Two conditions trigger an increase of the sampling buffer for a
  842. * perf event:
  843. * 1. Postponed buffer allocations from the event initialization.
  844. * 2. Sampling overflows that contribute to pending allocations.
  845. *
  846. * Note that the extend_sampling_buffer() function disables the sampling
  847. * facility, but it can be fully re-enabled using sampling controls that
  848. * have been saved in cpumsf_pmu_disable().
  849. */
  850. if (cpuhw->event) {
  851. hwc = &cpuhw->event->hw;
  852. if (!(SAMPL_DIAG_MODE(hwc))) {
  853. /*
  854. * Account number of overflow-designated
  855. * buffer extents
  856. */
  857. sfb_account_overflows(cpuhw, hwc);
  858. extend_sampling_buffer(&cpuhw->sfb, hwc);
  859. }
  860. /* Rate may be adjusted with ioctl() */
  861. cpuhw->lsctl.interval = SAMPL_RATE(&cpuhw->event->hw);
  862. }
  863. /* (Re)enable the PMU and sampling facility */
  864. cpuhw->flags |= PMU_F_ENABLED;
  865. barrier();
  866. err = lsctl(&cpuhw->lsctl);
  867. if (err) {
  868. cpuhw->flags &= ~PMU_F_ENABLED;
  869. pr_err("Loading sampling controls failed: op %i err %i\n",
  870. 1, err);
  871. return;
  872. }
  873. /* Load current program parameter */
  874. lpp(&S390_lowcore.lpp);
  875. debug_sprintf_event(sfdbg, 6, "%s: es %i cs %i ed %i cd %i "
  876. "interval %#lx tear %#lx dear %#lx\n", __func__,
  877. cpuhw->lsctl.es, cpuhw->lsctl.cs, cpuhw->lsctl.ed,
  878. cpuhw->lsctl.cd, cpuhw->lsctl.interval,
  879. cpuhw->lsctl.tear, cpuhw->lsctl.dear);
  880. }
  881. static void cpumsf_pmu_disable(struct pmu *pmu)
  882. {
  883. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  884. struct hws_lsctl_request_block inactive;
  885. struct hws_qsi_info_block si;
  886. int err;
  887. if (!(cpuhw->flags & PMU_F_ENABLED))
  888. return;
  889. if (cpuhw->flags & PMU_F_ERR_MASK)
  890. return;
  891. /* Switch off sampling activation control */
  892. inactive = cpuhw->lsctl;
  893. inactive.cs = 0;
  894. inactive.cd = 0;
  895. err = lsctl(&inactive);
  896. if (err) {
  897. pr_err("Loading sampling controls failed: op %i err %i\n",
  898. 2, err);
  899. return;
  900. }
  901. /* Save state of TEAR and DEAR register contents */
  902. err = qsi(&si);
  903. if (!err) {
  904. /* TEAR/DEAR values are valid only if the sampling facility is
  905. * enabled. Note that cpumsf_pmu_disable() might be called even
  906. * for a disabled sampling facility because cpumsf_pmu_enable()
  907. * controls the enable/disable state.
  908. */
  909. if (si.es) {
  910. cpuhw->lsctl.tear = si.tear;
  911. cpuhw->lsctl.dear = si.dear;
  912. }
  913. } else
  914. debug_sprintf_event(sfdbg, 3, "%s: qsi() failed with err %i\n",
  915. __func__, err);
  916. cpuhw->flags &= ~PMU_F_ENABLED;
  917. }
  918. /* perf_exclude_event() - Filter event
  919. * @event: The perf event
  920. * @regs: pt_regs structure
  921. * @sde_regs: Sample-data-entry (sde) regs structure
  922. *
  923. * Filter perf events according to their exclude specification.
  924. *
  925. * Return non-zero if the event shall be excluded.
  926. */
  927. static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
  928. struct perf_sf_sde_regs *sde_regs)
  929. {
  930. if (event->attr.exclude_user && user_mode(regs))
  931. return 1;
  932. if (event->attr.exclude_kernel && !user_mode(regs))
  933. return 1;
  934. if (event->attr.exclude_guest && sde_regs->in_guest)
  935. return 1;
  936. if (event->attr.exclude_host && !sde_regs->in_guest)
  937. return 1;
  938. return 0;
  939. }
  940. /* perf_push_sample() - Push samples to perf
  941. * @event: The perf event
  942. * @sample: Hardware sample data
  943. *
  944. * Use the hardware sample data to create perf event sample. The sample
  945. * is the pushed to the event subsystem and the function checks for
  946. * possible event overflows. If an event overflow occurs, the PMU is
  947. * stopped.
  948. *
  949. * Return non-zero if an event overflow occurred.
  950. */
  951. static int perf_push_sample(struct perf_event *event,
  952. struct hws_basic_entry *basic)
  953. {
  954. int overflow;
  955. struct pt_regs regs;
  956. struct perf_sf_sde_regs *sde_regs;
  957. struct perf_sample_data data;
  958. /* Setup perf sample */
  959. perf_sample_data_init(&data, 0, event->hw.last_period);
  960. /* Setup pt_regs to look like an CPU-measurement external interrupt
  961. * using the Program Request Alert code. The regs.int_parm_long
  962. * field which is unused contains additional sample-data-entry related
  963. * indicators.
  964. */
  965. memset(&regs, 0, sizeof(regs));
  966. regs.int_code = 0x1407;
  967. regs.int_parm = CPU_MF_INT_SF_PRA;
  968. sde_regs = (struct perf_sf_sde_regs *) &regs.int_parm_long;
  969. psw_bits(regs.psw).ia = basic->ia;
  970. psw_bits(regs.psw).dat = basic->T;
  971. psw_bits(regs.psw).wait = basic->W;
  972. psw_bits(regs.psw).pstate = basic->P;
  973. psw_bits(regs.psw).as = basic->AS;
  974. /*
  975. * Use the hardware provided configuration level to decide if the
  976. * sample belongs to a guest or host. If that is not available,
  977. * fall back to the following heuristics:
  978. * A non-zero guest program parameter always indicates a guest
  979. * sample. Some early samples or samples from guests without
  980. * lpp usage would be misaccounted to the host. We use the asn
  981. * value as an addon heuristic to detect most of these guest samples.
  982. * If the value differs from 0xffff (the host value), we assume to
  983. * be a KVM guest.
  984. */
  985. switch (basic->CL) {
  986. case 1: /* logical partition */
  987. sde_regs->in_guest = 0;
  988. break;
  989. case 2: /* virtual machine */
  990. sde_regs->in_guest = 1;
  991. break;
  992. default: /* old machine, use heuristics */
  993. if (basic->gpp || basic->prim_asn != 0xffff)
  994. sde_regs->in_guest = 1;
  995. break;
  996. }
  997. /*
  998. * Store the PID value from the sample-data-entry to be
  999. * processed and resolved by cpumsf_output_event_pid().
  1000. */
  1001. data.tid_entry.pid = basic->hpp & LPP_PID_MASK;
  1002. overflow = 0;
  1003. if (perf_exclude_event(event, &regs, sde_regs))
  1004. goto out;
  1005. if (perf_event_overflow(event, &data, &regs)) {
  1006. overflow = 1;
  1007. event->pmu->stop(event, 0);
  1008. }
  1009. perf_event_update_userpage(event);
  1010. out:
  1011. return overflow;
  1012. }
  1013. static void perf_event_count_update(struct perf_event *event, u64 count)
  1014. {
  1015. local64_add(count, &event->count);
  1016. }
  1017. /* hw_collect_samples() - Walk through a sample-data-block and collect samples
  1018. * @event: The perf event
  1019. * @sdbt: Sample-data-block table
  1020. * @overflow: Event overflow counter
  1021. *
  1022. * Walks through a sample-data-block and collects sampling data entries that are
  1023. * then pushed to the perf event subsystem. Depending on the sampling function,
  1024. * there can be either basic-sampling or combined-sampling data entries. A
  1025. * combined-sampling data entry consists of a basic- and a diagnostic-sampling
  1026. * data entry. The sampling function is determined by the flags in the perf
  1027. * event hardware structure. The function always works with a combined-sampling
  1028. * data entry but ignores the the diagnostic portion if it is not available.
  1029. *
  1030. * Note that the implementation focuses on basic-sampling data entries and, if
  1031. * such an entry is not valid, the entire combined-sampling data entry is
  1032. * ignored.
  1033. *
  1034. * The overflow variables counts the number of samples that has been discarded
  1035. * due to a perf event overflow.
  1036. */
  1037. static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt,
  1038. unsigned long long *overflow)
  1039. {
  1040. struct hws_trailer_entry *te;
  1041. struct hws_basic_entry *sample;
  1042. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1043. sample = (struct hws_basic_entry *) *sdbt;
  1044. while ((unsigned long *) sample < (unsigned long *) te) {
  1045. /* Check for an empty sample */
  1046. if (!sample->def || sample->LS)
  1047. break;
  1048. /* Update perf event period */
  1049. perf_event_count_update(event, SAMPL_RATE(&event->hw));
  1050. /* Check whether sample is valid */
  1051. if (sample->def == 0x0001) {
  1052. /* If an event overflow occurred, the PMU is stopped to
  1053. * throttle event delivery. Remaining sample data is
  1054. * discarded.
  1055. */
  1056. if (!*overflow) {
  1057. /* Check whether sample is consistent */
  1058. if (sample->I == 0 && sample->W == 0) {
  1059. /* Deliver sample data to perf */
  1060. *overflow = perf_push_sample(event,
  1061. sample);
  1062. }
  1063. } else
  1064. /* Count discarded samples */
  1065. *overflow += 1;
  1066. } else {
  1067. debug_sprintf_event(sfdbg, 4,
  1068. "%s: Found unknown"
  1069. " sampling data entry: te->f %i"
  1070. " basic.def %#4x (%p)\n", __func__,
  1071. te->header.f, sample->def, sample);
  1072. /* Sample slot is not yet written or other record.
  1073. *
  1074. * This condition can occur if the buffer was reused
  1075. * from a combined basic- and diagnostic-sampling.
  1076. * If only basic-sampling is then active, entries are
  1077. * written into the larger diagnostic entries.
  1078. * This is typically the case for sample-data-blocks
  1079. * that are not full. Stop processing if the first
  1080. * invalid format was detected.
  1081. */
  1082. if (!te->header.f)
  1083. break;
  1084. }
  1085. /* Reset sample slot and advance to next sample */
  1086. sample->def = 0;
  1087. sample++;
  1088. }
  1089. }
  1090. static inline __uint128_t __cdsg(__uint128_t *ptr, __uint128_t old, __uint128_t new)
  1091. {
  1092. asm volatile(
  1093. " cdsg %[old],%[new],%[ptr]\n"
  1094. : [old] "+d" (old), [ptr] "+QS" (*ptr)
  1095. : [new] "d" (new)
  1096. : "memory", "cc");
  1097. return old;
  1098. }
  1099. /* hw_perf_event_update() - Process sampling buffer
  1100. * @event: The perf event
  1101. * @flush_all: Flag to also flush partially filled sample-data-blocks
  1102. *
  1103. * Processes the sampling buffer and create perf event samples.
  1104. * The sampling buffer position are retrieved and saved in the TEAR_REG
  1105. * register of the specified perf event.
  1106. *
  1107. * Only full sample-data-blocks are processed. Specify the flash_all flag
  1108. * to also walk through partially filled sample-data-blocks. It is ignored
  1109. * if PERF_CPUM_SF_FULL_BLOCKS is set. The PERF_CPUM_SF_FULL_BLOCKS flag
  1110. * enforces the processing of full sample-data-blocks only (trailer entries
  1111. * with the block-full-indicator bit set).
  1112. */
  1113. static void hw_perf_event_update(struct perf_event *event, int flush_all)
  1114. {
  1115. unsigned long long event_overflow, sampl_overflow, num_sdb;
  1116. union hws_trailer_header old, prev, new;
  1117. struct hw_perf_event *hwc = &event->hw;
  1118. struct hws_trailer_entry *te;
  1119. unsigned long *sdbt;
  1120. int done;
  1121. /*
  1122. * AUX buffer is used when in diagnostic sampling mode.
  1123. * No perf events/samples are created.
  1124. */
  1125. if (SAMPL_DIAG_MODE(&event->hw))
  1126. return;
  1127. if (flush_all && SDB_FULL_BLOCKS(hwc))
  1128. flush_all = 0;
  1129. sdbt = (unsigned long *) TEAR_REG(hwc);
  1130. done = event_overflow = sampl_overflow = num_sdb = 0;
  1131. while (!done) {
  1132. /* Get the trailer entry of the sample-data-block */
  1133. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1134. /* Leave loop if no more work to do (block full indicator) */
  1135. if (!te->header.f) {
  1136. done = 1;
  1137. if (!flush_all)
  1138. break;
  1139. }
  1140. /* Check the sample overflow count */
  1141. if (te->header.overflow)
  1142. /* Account sample overflows and, if a particular limit
  1143. * is reached, extend the sampling buffer.
  1144. * For details, see sfb_account_overflows().
  1145. */
  1146. sampl_overflow += te->header.overflow;
  1147. /* Timestamps are valid for full sample-data-blocks only */
  1148. debug_sprintf_event(sfdbg, 6, "%s: sdbt %#lx "
  1149. "overflow %llu timestamp %#llx\n",
  1150. __func__, (unsigned long)sdbt, te->header.overflow,
  1151. (te->header.f) ? trailer_timestamp(te) : 0ULL);
  1152. /* Collect all samples from a single sample-data-block and
  1153. * flag if an (perf) event overflow happened. If so, the PMU
  1154. * is stopped and remaining samples will be discarded.
  1155. */
  1156. hw_collect_samples(event, sdbt, &event_overflow);
  1157. num_sdb++;
  1158. /* Reset trailer (using compare-double-and-swap) */
  1159. /* READ_ONCE() 16 byte header */
  1160. prev.val = __cdsg(&te->header.val, 0, 0);
  1161. do {
  1162. old.val = prev.val;
  1163. new.val = prev.val;
  1164. new.f = 0;
  1165. new.a = 1;
  1166. new.overflow = 0;
  1167. prev.val = __cdsg(&te->header.val, old.val, new.val);
  1168. } while (prev.val != old.val);
  1169. /* Advance to next sample-data-block */
  1170. sdbt++;
  1171. if (is_link_entry(sdbt))
  1172. sdbt = get_next_sdbt(sdbt);
  1173. /* Update event hardware registers */
  1174. TEAR_REG(hwc) = (unsigned long) sdbt;
  1175. /* Stop processing sample-data if all samples of the current
  1176. * sample-data-block were flushed even if it was not full.
  1177. */
  1178. if (flush_all && done)
  1179. break;
  1180. }
  1181. /* Account sample overflows in the event hardware structure */
  1182. if (sampl_overflow)
  1183. OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) +
  1184. sampl_overflow, 1 + num_sdb);
  1185. /* Perf_event_overflow() and perf_event_account_interrupt() limit
  1186. * the interrupt rate to an upper limit. Roughly 1000 samples per
  1187. * task tick.
  1188. * Hitting this limit results in a large number
  1189. * of throttled REF_REPORT_THROTTLE entries and the samples
  1190. * are dropped.
  1191. * Slightly increase the interval to avoid hitting this limit.
  1192. */
  1193. if (event_overflow) {
  1194. SAMPL_RATE(hwc) += DIV_ROUND_UP(SAMPL_RATE(hwc), 10);
  1195. debug_sprintf_event(sfdbg, 1, "%s: rate adjustment %ld\n",
  1196. __func__,
  1197. DIV_ROUND_UP(SAMPL_RATE(hwc), 10));
  1198. }
  1199. if (sampl_overflow || event_overflow)
  1200. debug_sprintf_event(sfdbg, 4, "%s: "
  1201. "overflows: sample %llu event %llu"
  1202. " total %llu num_sdb %llu\n",
  1203. __func__, sampl_overflow, event_overflow,
  1204. OVERFLOW_REG(hwc), num_sdb);
  1205. }
  1206. #define AUX_SDB_INDEX(aux, i) ((i) % aux->sfb.num_sdb)
  1207. #define AUX_SDB_NUM(aux, start, end) (end >= start ? end - start + 1 : 0)
  1208. #define AUX_SDB_NUM_ALERT(aux) AUX_SDB_NUM(aux, aux->head, aux->alert_mark)
  1209. #define AUX_SDB_NUM_EMPTY(aux) AUX_SDB_NUM(aux, aux->head, aux->empty_mark)
  1210. /*
  1211. * Get trailer entry by index of SDB.
  1212. */
  1213. static struct hws_trailer_entry *aux_sdb_trailer(struct aux_buffer *aux,
  1214. unsigned long index)
  1215. {
  1216. unsigned long sdb;
  1217. index = AUX_SDB_INDEX(aux, index);
  1218. sdb = aux->sdb_index[index];
  1219. return (struct hws_trailer_entry *)trailer_entry_ptr(sdb);
  1220. }
  1221. /*
  1222. * Finish sampling on the cpu. Called by cpumsf_pmu_del() with pmu
  1223. * disabled. Collect the full SDBs in AUX buffer which have not reached
  1224. * the point of alert indicator. And ignore the SDBs which are not
  1225. * full.
  1226. *
  1227. * 1. Scan SDBs to see how much data is there and consume them.
  1228. * 2. Remove alert indicator in the buffer.
  1229. */
  1230. static void aux_output_end(struct perf_output_handle *handle)
  1231. {
  1232. unsigned long i, range_scan, idx;
  1233. struct aux_buffer *aux;
  1234. struct hws_trailer_entry *te;
  1235. aux = perf_get_aux(handle);
  1236. if (!aux)
  1237. return;
  1238. range_scan = AUX_SDB_NUM_ALERT(aux);
  1239. for (i = 0, idx = aux->head; i < range_scan; i++, idx++) {
  1240. te = aux_sdb_trailer(aux, idx);
  1241. if (!te->header.f)
  1242. break;
  1243. }
  1244. /* i is num of SDBs which are full */
  1245. perf_aux_output_end(handle, i << PAGE_SHIFT);
  1246. /* Remove alert indicators in the buffer */
  1247. te = aux_sdb_trailer(aux, aux->alert_mark);
  1248. te->header.a = 0;
  1249. debug_sprintf_event(sfdbg, 6, "%s: SDBs %ld range %ld head %ld\n",
  1250. __func__, i, range_scan, aux->head);
  1251. }
  1252. /*
  1253. * Start sampling on the CPU. Called by cpumsf_pmu_add() when an event
  1254. * is first added to the CPU or rescheduled again to the CPU. It is called
  1255. * with pmu disabled.
  1256. *
  1257. * 1. Reset the trailer of SDBs to get ready for new data.
  1258. * 2. Tell the hardware where to put the data by reset the SDBs buffer
  1259. * head(tear/dear).
  1260. */
  1261. static int aux_output_begin(struct perf_output_handle *handle,
  1262. struct aux_buffer *aux,
  1263. struct cpu_hw_sf *cpuhw)
  1264. {
  1265. unsigned long range;
  1266. unsigned long i, range_scan, idx;
  1267. unsigned long head, base, offset;
  1268. struct hws_trailer_entry *te;
  1269. if (WARN_ON_ONCE(handle->head & ~PAGE_MASK))
  1270. return -EINVAL;
  1271. aux->head = handle->head >> PAGE_SHIFT;
  1272. range = (handle->size + 1) >> PAGE_SHIFT;
  1273. if (range <= 1)
  1274. return -ENOMEM;
  1275. /*
  1276. * SDBs between aux->head and aux->empty_mark are already ready
  1277. * for new data. range_scan is num of SDBs not within them.
  1278. */
  1279. debug_sprintf_event(sfdbg, 6,
  1280. "%s: range %ld head %ld alert %ld empty %ld\n",
  1281. __func__, range, aux->head, aux->alert_mark,
  1282. aux->empty_mark);
  1283. if (range > AUX_SDB_NUM_EMPTY(aux)) {
  1284. range_scan = range - AUX_SDB_NUM_EMPTY(aux);
  1285. idx = aux->empty_mark + 1;
  1286. for (i = 0; i < range_scan; i++, idx++) {
  1287. te = aux_sdb_trailer(aux, idx);
  1288. te->header.f = 0;
  1289. te->header.a = 0;
  1290. te->header.overflow = 0;
  1291. }
  1292. /* Save the position of empty SDBs */
  1293. aux->empty_mark = aux->head + range - 1;
  1294. }
  1295. /* Set alert indicator */
  1296. aux->alert_mark = aux->head + range/2 - 1;
  1297. te = aux_sdb_trailer(aux, aux->alert_mark);
  1298. te->header.a = 1;
  1299. /* Reset hardware buffer head */
  1300. head = AUX_SDB_INDEX(aux, aux->head);
  1301. base = aux->sdbt_index[head / CPUM_SF_SDB_PER_TABLE];
  1302. offset = head % CPUM_SF_SDB_PER_TABLE;
  1303. cpuhw->lsctl.tear = base + offset * sizeof(unsigned long);
  1304. cpuhw->lsctl.dear = aux->sdb_index[head];
  1305. debug_sprintf_event(sfdbg, 6, "%s: head %ld alert %ld empty %ld "
  1306. "index %ld tear %#lx dear %#lx\n", __func__,
  1307. aux->head, aux->alert_mark, aux->empty_mark,
  1308. head / CPUM_SF_SDB_PER_TABLE,
  1309. cpuhw->lsctl.tear, cpuhw->lsctl.dear);
  1310. return 0;
  1311. }
  1312. /*
  1313. * Set alert indicator on SDB at index @alert_index while sampler is running.
  1314. *
  1315. * Return true if successfully.
  1316. * Return false if full indicator is already set by hardware sampler.
  1317. */
  1318. static bool aux_set_alert(struct aux_buffer *aux, unsigned long alert_index,
  1319. unsigned long long *overflow)
  1320. {
  1321. union hws_trailer_header old, prev, new;
  1322. struct hws_trailer_entry *te;
  1323. te = aux_sdb_trailer(aux, alert_index);
  1324. /* READ_ONCE() 16 byte header */
  1325. prev.val = __cdsg(&te->header.val, 0, 0);
  1326. do {
  1327. old.val = prev.val;
  1328. new.val = prev.val;
  1329. *overflow = old.overflow;
  1330. if (old.f) {
  1331. /*
  1332. * SDB is already set by hardware.
  1333. * Abort and try to set somewhere
  1334. * behind.
  1335. */
  1336. return false;
  1337. }
  1338. new.a = 1;
  1339. new.overflow = 0;
  1340. prev.val = __cdsg(&te->header.val, old.val, new.val);
  1341. } while (prev.val != old.val);
  1342. return true;
  1343. }
  1344. /*
  1345. * aux_reset_buffer() - Scan and setup SDBs for new samples
  1346. * @aux: The AUX buffer to set
  1347. * @range: The range of SDBs to scan started from aux->head
  1348. * @overflow: Set to overflow count
  1349. *
  1350. * Set alert indicator on the SDB at index of aux->alert_mark. If this SDB is
  1351. * marked as empty, check if it is already set full by the hardware sampler.
  1352. * If yes, that means new data is already there before we can set an alert
  1353. * indicator. Caller should try to set alert indicator to some position behind.
  1354. *
  1355. * Scan the SDBs in AUX buffer from behind aux->empty_mark. They are used
  1356. * previously and have already been consumed by user space. Reset these SDBs
  1357. * (clear full indicator and alert indicator) for new data.
  1358. * If aux->alert_mark fall in this area, just set it. Overflow count is
  1359. * recorded while scanning.
  1360. *
  1361. * SDBs between aux->head and aux->empty_mark are already reset at last time.
  1362. * and ready for new samples. So scanning on this area could be skipped.
  1363. *
  1364. * Return true if alert indicator is set successfully and false if not.
  1365. */
  1366. static bool aux_reset_buffer(struct aux_buffer *aux, unsigned long range,
  1367. unsigned long long *overflow)
  1368. {
  1369. unsigned long i, range_scan, idx, idx_old;
  1370. union hws_trailer_header old, prev, new;
  1371. unsigned long long orig_overflow;
  1372. struct hws_trailer_entry *te;
  1373. debug_sprintf_event(sfdbg, 6, "%s: range %ld head %ld alert %ld "
  1374. "empty %ld\n", __func__, range, aux->head,
  1375. aux->alert_mark, aux->empty_mark);
  1376. if (range <= AUX_SDB_NUM_EMPTY(aux))
  1377. /*
  1378. * No need to scan. All SDBs in range are marked as empty.
  1379. * Just set alert indicator. Should check race with hardware
  1380. * sampler.
  1381. */
  1382. return aux_set_alert(aux, aux->alert_mark, overflow);
  1383. if (aux->alert_mark <= aux->empty_mark)
  1384. /*
  1385. * Set alert indicator on empty SDB. Should check race
  1386. * with hardware sampler.
  1387. */
  1388. if (!aux_set_alert(aux, aux->alert_mark, overflow))
  1389. return false;
  1390. /*
  1391. * Scan the SDBs to clear full and alert indicator used previously.
  1392. * Start scanning from one SDB behind empty_mark. If the new alert
  1393. * indicator fall into this range, set it.
  1394. */
  1395. range_scan = range - AUX_SDB_NUM_EMPTY(aux);
  1396. idx_old = idx = aux->empty_mark + 1;
  1397. for (i = 0; i < range_scan; i++, idx++) {
  1398. te = aux_sdb_trailer(aux, idx);
  1399. /* READ_ONCE() 16 byte header */
  1400. prev.val = __cdsg(&te->header.val, 0, 0);
  1401. do {
  1402. old.val = prev.val;
  1403. new.val = prev.val;
  1404. orig_overflow = old.overflow;
  1405. new.f = 0;
  1406. new.overflow = 0;
  1407. if (idx == aux->alert_mark)
  1408. new.a = 1;
  1409. else
  1410. new.a = 0;
  1411. prev.val = __cdsg(&te->header.val, old.val, new.val);
  1412. } while (prev.val != old.val);
  1413. *overflow += orig_overflow;
  1414. }
  1415. /* Update empty_mark to new position */
  1416. aux->empty_mark = aux->head + range - 1;
  1417. debug_sprintf_event(sfdbg, 6, "%s: range_scan %ld idx %ld..%ld "
  1418. "empty %ld\n", __func__, range_scan, idx_old,
  1419. idx - 1, aux->empty_mark);
  1420. return true;
  1421. }
  1422. /*
  1423. * Measurement alert handler for diagnostic mode sampling.
  1424. */
  1425. static void hw_collect_aux(struct cpu_hw_sf *cpuhw)
  1426. {
  1427. struct aux_buffer *aux;
  1428. int done = 0;
  1429. unsigned long range = 0, size;
  1430. unsigned long long overflow = 0;
  1431. struct perf_output_handle *handle = &cpuhw->handle;
  1432. unsigned long num_sdb;
  1433. aux = perf_get_aux(handle);
  1434. if (WARN_ON_ONCE(!aux))
  1435. return;
  1436. /* Inform user space new data arrived */
  1437. size = AUX_SDB_NUM_ALERT(aux) << PAGE_SHIFT;
  1438. debug_sprintf_event(sfdbg, 6, "%s: #alert %ld\n", __func__,
  1439. size >> PAGE_SHIFT);
  1440. perf_aux_output_end(handle, size);
  1441. num_sdb = aux->sfb.num_sdb;
  1442. while (!done) {
  1443. /* Get an output handle */
  1444. aux = perf_aux_output_begin(handle, cpuhw->event);
  1445. if (handle->size == 0) {
  1446. pr_err("The AUX buffer with %lu pages for the "
  1447. "diagnostic-sampling mode is full\n",
  1448. num_sdb);
  1449. debug_sprintf_event(sfdbg, 1,
  1450. "%s: AUX buffer used up\n",
  1451. __func__);
  1452. break;
  1453. }
  1454. if (WARN_ON_ONCE(!aux))
  1455. return;
  1456. /* Update head and alert_mark to new position */
  1457. aux->head = handle->head >> PAGE_SHIFT;
  1458. range = (handle->size + 1) >> PAGE_SHIFT;
  1459. if (range == 1)
  1460. aux->alert_mark = aux->head;
  1461. else
  1462. aux->alert_mark = aux->head + range/2 - 1;
  1463. if (aux_reset_buffer(aux, range, &overflow)) {
  1464. if (!overflow) {
  1465. done = 1;
  1466. break;
  1467. }
  1468. size = range << PAGE_SHIFT;
  1469. perf_aux_output_end(&cpuhw->handle, size);
  1470. pr_err("Sample data caused the AUX buffer with %lu "
  1471. "pages to overflow\n", aux->sfb.num_sdb);
  1472. debug_sprintf_event(sfdbg, 1, "%s: head %ld range %ld "
  1473. "overflow %lld\n", __func__,
  1474. aux->head, range, overflow);
  1475. } else {
  1476. size = AUX_SDB_NUM_ALERT(aux) << PAGE_SHIFT;
  1477. perf_aux_output_end(&cpuhw->handle, size);
  1478. debug_sprintf_event(sfdbg, 6, "%s: head %ld alert %ld "
  1479. "already full, try another\n",
  1480. __func__,
  1481. aux->head, aux->alert_mark);
  1482. }
  1483. }
  1484. if (done)
  1485. debug_sprintf_event(sfdbg, 6, "%s: head %ld alert %ld "
  1486. "empty %ld\n", __func__, aux->head,
  1487. aux->alert_mark, aux->empty_mark);
  1488. }
  1489. /*
  1490. * Callback when freeing AUX buffers.
  1491. */
  1492. static void aux_buffer_free(void *data)
  1493. {
  1494. struct aux_buffer *aux = data;
  1495. unsigned long i, num_sdbt;
  1496. if (!aux)
  1497. return;
  1498. /* Free SDBT. SDB is freed by the caller */
  1499. num_sdbt = aux->sfb.num_sdbt;
  1500. for (i = 0; i < num_sdbt; i++)
  1501. free_page(aux->sdbt_index[i]);
  1502. kfree(aux->sdbt_index);
  1503. kfree(aux->sdb_index);
  1504. kfree(aux);
  1505. debug_sprintf_event(sfdbg, 4, "%s: SDBTs %lu\n", __func__, num_sdbt);
  1506. }
  1507. static void aux_sdb_init(unsigned long sdb)
  1508. {
  1509. struct hws_trailer_entry *te;
  1510. te = (struct hws_trailer_entry *)trailer_entry_ptr(sdb);
  1511. /* Save clock base */
  1512. te->clock_base = 1;
  1513. te->progusage2 = tod_clock_base.tod;
  1514. }
  1515. /*
  1516. * aux_buffer_setup() - Setup AUX buffer for diagnostic mode sampling
  1517. * @event: Event the buffer is setup for, event->cpu == -1 means current
  1518. * @pages: Array of pointers to buffer pages passed from perf core
  1519. * @nr_pages: Total pages
  1520. * @snapshot: Flag for snapshot mode
  1521. *
  1522. * This is the callback when setup an event using AUX buffer. Perf tool can
  1523. * trigger this by an additional mmap() call on the event. Unlike the buffer
  1524. * for basic samples, AUX buffer belongs to the event. It is scheduled with
  1525. * the task among online cpus when it is a per-thread event.
  1526. *
  1527. * Return the private AUX buffer structure if success or NULL if fails.
  1528. */
  1529. static void *aux_buffer_setup(struct perf_event *event, void **pages,
  1530. int nr_pages, bool snapshot)
  1531. {
  1532. struct sf_buffer *sfb;
  1533. struct aux_buffer *aux;
  1534. unsigned long *new, *tail;
  1535. int i, n_sdbt;
  1536. if (!nr_pages || !pages)
  1537. return NULL;
  1538. if (nr_pages > CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR) {
  1539. pr_err("AUX buffer size (%i pages) is larger than the "
  1540. "maximum sampling buffer limit\n",
  1541. nr_pages);
  1542. return NULL;
  1543. } else if (nr_pages < CPUM_SF_MIN_SDB * CPUM_SF_SDB_DIAG_FACTOR) {
  1544. pr_err("AUX buffer size (%i pages) is less than the "
  1545. "minimum sampling buffer limit\n",
  1546. nr_pages);
  1547. return NULL;
  1548. }
  1549. /* Allocate aux_buffer struct for the event */
  1550. aux = kzalloc(sizeof(struct aux_buffer), GFP_KERNEL);
  1551. if (!aux)
  1552. goto no_aux;
  1553. sfb = &aux->sfb;
  1554. /* Allocate sdbt_index for fast reference */
  1555. n_sdbt = DIV_ROUND_UP(nr_pages, CPUM_SF_SDB_PER_TABLE);
  1556. aux->sdbt_index = kmalloc_array(n_sdbt, sizeof(void *), GFP_KERNEL);
  1557. if (!aux->sdbt_index)
  1558. goto no_sdbt_index;
  1559. /* Allocate sdb_index for fast reference */
  1560. aux->sdb_index = kmalloc_array(nr_pages, sizeof(void *), GFP_KERNEL);
  1561. if (!aux->sdb_index)
  1562. goto no_sdb_index;
  1563. /* Allocate the first SDBT */
  1564. sfb->num_sdbt = 0;
  1565. sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  1566. if (!sfb->sdbt)
  1567. goto no_sdbt;
  1568. aux->sdbt_index[sfb->num_sdbt++] = (unsigned long)sfb->sdbt;
  1569. tail = sfb->tail = sfb->sdbt;
  1570. /*
  1571. * Link the provided pages of AUX buffer to SDBT.
  1572. * Allocate SDBT if needed.
  1573. */
  1574. for (i = 0; i < nr_pages; i++, tail++) {
  1575. if (require_table_link(tail)) {
  1576. new = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  1577. if (!new)
  1578. goto no_sdbt;
  1579. aux->sdbt_index[sfb->num_sdbt++] = (unsigned long)new;
  1580. /* Link current page to tail of chain */
  1581. *tail = (unsigned long)(void *) new + 1;
  1582. tail = new;
  1583. }
  1584. /* Tail is the entry in a SDBT */
  1585. *tail = (unsigned long)pages[i];
  1586. aux->sdb_index[i] = (unsigned long)pages[i];
  1587. aux_sdb_init((unsigned long)pages[i]);
  1588. }
  1589. sfb->num_sdb = nr_pages;
  1590. /* Link the last entry in the SDBT to the first SDBT */
  1591. *tail = (unsigned long) sfb->sdbt + 1;
  1592. sfb->tail = tail;
  1593. /*
  1594. * Initial all SDBs are zeroed. Mark it as empty.
  1595. * So there is no need to clear the full indicator
  1596. * when this event is first added.
  1597. */
  1598. aux->empty_mark = sfb->num_sdb - 1;
  1599. debug_sprintf_event(sfdbg, 4, "%s: SDBTs %lu SDBs %lu\n", __func__,
  1600. sfb->num_sdbt, sfb->num_sdb);
  1601. return aux;
  1602. no_sdbt:
  1603. /* SDBs (AUX buffer pages) are freed by caller */
  1604. for (i = 0; i < sfb->num_sdbt; i++)
  1605. free_page(aux->sdbt_index[i]);
  1606. kfree(aux->sdb_index);
  1607. no_sdb_index:
  1608. kfree(aux->sdbt_index);
  1609. no_sdbt_index:
  1610. kfree(aux);
  1611. no_aux:
  1612. return NULL;
  1613. }
  1614. static void cpumsf_pmu_read(struct perf_event *event)
  1615. {
  1616. /* Nothing to do ... updates are interrupt-driven */
  1617. }
  1618. /* Check if the new sampling period/freqeuncy is appropriate.
  1619. *
  1620. * Return non-zero on error and zero on passed checks.
  1621. */
  1622. static int cpumsf_pmu_check_period(struct perf_event *event, u64 value)
  1623. {
  1624. struct hws_qsi_info_block si;
  1625. unsigned long rate;
  1626. bool do_freq;
  1627. memset(&si, 0, sizeof(si));
  1628. if (event->cpu == -1) {
  1629. if (qsi(&si))
  1630. return -ENODEV;
  1631. } else {
  1632. /* Event is pinned to a particular CPU, retrieve the per-CPU
  1633. * sampling structure for accessing the CPU-specific QSI.
  1634. */
  1635. struct cpu_hw_sf *cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
  1636. si = cpuhw->qsi;
  1637. }
  1638. do_freq = !!SAMPLE_FREQ_MODE(&event->hw);
  1639. rate = getrate(do_freq, value, &si);
  1640. if (!rate)
  1641. return -EINVAL;
  1642. event->attr.sample_period = rate;
  1643. SAMPL_RATE(&event->hw) = rate;
  1644. hw_init_period(&event->hw, SAMPL_RATE(&event->hw));
  1645. debug_sprintf_event(sfdbg, 4, "%s:"
  1646. " cpu %d value %#llx period %#llx freq %d\n",
  1647. __func__, event->cpu, value,
  1648. event->attr.sample_period, do_freq);
  1649. return 0;
  1650. }
  1651. /* Activate sampling control.
  1652. * Next call of pmu_enable() starts sampling.
  1653. */
  1654. static void cpumsf_pmu_start(struct perf_event *event, int flags)
  1655. {
  1656. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1657. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  1658. return;
  1659. if (flags & PERF_EF_RELOAD)
  1660. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1661. perf_pmu_disable(event->pmu);
  1662. event->hw.state = 0;
  1663. cpuhw->lsctl.cs = 1;
  1664. if (SAMPL_DIAG_MODE(&event->hw))
  1665. cpuhw->lsctl.cd = 1;
  1666. perf_pmu_enable(event->pmu);
  1667. }
  1668. /* Deactivate sampling control.
  1669. * Next call of pmu_enable() stops sampling.
  1670. */
  1671. static void cpumsf_pmu_stop(struct perf_event *event, int flags)
  1672. {
  1673. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1674. if (event->hw.state & PERF_HES_STOPPED)
  1675. return;
  1676. perf_pmu_disable(event->pmu);
  1677. cpuhw->lsctl.cs = 0;
  1678. cpuhw->lsctl.cd = 0;
  1679. event->hw.state |= PERF_HES_STOPPED;
  1680. if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
  1681. hw_perf_event_update(event, 1);
  1682. event->hw.state |= PERF_HES_UPTODATE;
  1683. }
  1684. perf_pmu_enable(event->pmu);
  1685. }
  1686. static int cpumsf_pmu_add(struct perf_event *event, int flags)
  1687. {
  1688. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1689. struct aux_buffer *aux;
  1690. int err;
  1691. if (cpuhw->flags & PMU_F_IN_USE)
  1692. return -EAGAIN;
  1693. if (!SAMPL_DIAG_MODE(&event->hw) && !cpuhw->sfb.sdbt)
  1694. return -EINVAL;
  1695. err = 0;
  1696. perf_pmu_disable(event->pmu);
  1697. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  1698. /* Set up sampling controls. Always program the sampling register
  1699. * using the SDB-table start. Reset TEAR_REG event hardware register
  1700. * that is used by hw_perf_event_update() to store the sampling buffer
  1701. * position after samples have been flushed.
  1702. */
  1703. cpuhw->lsctl.s = 0;
  1704. cpuhw->lsctl.h = 1;
  1705. cpuhw->lsctl.interval = SAMPL_RATE(&event->hw);
  1706. if (!SAMPL_DIAG_MODE(&event->hw)) {
  1707. cpuhw->lsctl.tear = (unsigned long) cpuhw->sfb.sdbt;
  1708. cpuhw->lsctl.dear = *(unsigned long *) cpuhw->sfb.sdbt;
  1709. TEAR_REG(&event->hw) = (unsigned long) cpuhw->sfb.sdbt;
  1710. }
  1711. /* Ensure sampling functions are in the disabled state. If disabled,
  1712. * switch on sampling enable control. */
  1713. if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) {
  1714. err = -EAGAIN;
  1715. goto out;
  1716. }
  1717. if (SAMPL_DIAG_MODE(&event->hw)) {
  1718. aux = perf_aux_output_begin(&cpuhw->handle, event);
  1719. if (!aux) {
  1720. err = -EINVAL;
  1721. goto out;
  1722. }
  1723. err = aux_output_begin(&cpuhw->handle, aux, cpuhw);
  1724. if (err)
  1725. goto out;
  1726. cpuhw->lsctl.ed = 1;
  1727. }
  1728. cpuhw->lsctl.es = 1;
  1729. /* Set in_use flag and store event */
  1730. cpuhw->event = event;
  1731. cpuhw->flags |= PMU_F_IN_USE;
  1732. if (flags & PERF_EF_START)
  1733. cpumsf_pmu_start(event, PERF_EF_RELOAD);
  1734. out:
  1735. perf_event_update_userpage(event);
  1736. perf_pmu_enable(event->pmu);
  1737. return err;
  1738. }
  1739. static void cpumsf_pmu_del(struct perf_event *event, int flags)
  1740. {
  1741. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1742. perf_pmu_disable(event->pmu);
  1743. cpumsf_pmu_stop(event, PERF_EF_UPDATE);
  1744. cpuhw->lsctl.es = 0;
  1745. cpuhw->lsctl.ed = 0;
  1746. cpuhw->flags &= ~PMU_F_IN_USE;
  1747. cpuhw->event = NULL;
  1748. if (SAMPL_DIAG_MODE(&event->hw))
  1749. aux_output_end(&cpuhw->handle);
  1750. perf_event_update_userpage(event);
  1751. perf_pmu_enable(event->pmu);
  1752. }
  1753. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF);
  1754. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG);
  1755. /* Attribute list for CPU_SF.
  1756. *
  1757. * The availablitiy depends on the CPU_MF sampling facility authorization
  1758. * for basic + diagnositic samples. This is determined at initialization
  1759. * time by the sampling facility device driver.
  1760. * If the authorization for basic samples is turned off, it should be
  1761. * also turned off for diagnostic sampling.
  1762. *
  1763. * During initialization of the device driver, check the authorization
  1764. * level for diagnostic sampling and installs the attribute
  1765. * file for diagnostic sampling if necessary.
  1766. *
  1767. * For now install a placeholder to reference all possible attributes:
  1768. * SF_CYCLES_BASIC and SF_CYCLES_BASIC_DIAG.
  1769. * Add another entry for the final NULL pointer.
  1770. */
  1771. enum {
  1772. SF_CYCLES_BASIC_ATTR_IDX = 0,
  1773. SF_CYCLES_BASIC_DIAG_ATTR_IDX,
  1774. SF_CYCLES_ATTR_MAX
  1775. };
  1776. static struct attribute *cpumsf_pmu_events_attr[SF_CYCLES_ATTR_MAX + 1] = {
  1777. [SF_CYCLES_BASIC_ATTR_IDX] = CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC)
  1778. };
  1779. PMU_FORMAT_ATTR(event, "config:0-63");
  1780. static struct attribute *cpumsf_pmu_format_attr[] = {
  1781. &format_attr_event.attr,
  1782. NULL,
  1783. };
  1784. static struct attribute_group cpumsf_pmu_events_group = {
  1785. .name = "events",
  1786. .attrs = cpumsf_pmu_events_attr,
  1787. };
  1788. static struct attribute_group cpumsf_pmu_format_group = {
  1789. .name = "format",
  1790. .attrs = cpumsf_pmu_format_attr,
  1791. };
  1792. static const struct attribute_group *cpumsf_pmu_attr_groups[] = {
  1793. &cpumsf_pmu_events_group,
  1794. &cpumsf_pmu_format_group,
  1795. NULL,
  1796. };
  1797. static struct pmu cpumf_sampling = {
  1798. .pmu_enable = cpumsf_pmu_enable,
  1799. .pmu_disable = cpumsf_pmu_disable,
  1800. .event_init = cpumsf_pmu_event_init,
  1801. .add = cpumsf_pmu_add,
  1802. .del = cpumsf_pmu_del,
  1803. .start = cpumsf_pmu_start,
  1804. .stop = cpumsf_pmu_stop,
  1805. .read = cpumsf_pmu_read,
  1806. .attr_groups = cpumsf_pmu_attr_groups,
  1807. .setup_aux = aux_buffer_setup,
  1808. .free_aux = aux_buffer_free,
  1809. .check_period = cpumsf_pmu_check_period,
  1810. };
  1811. static void cpumf_measurement_alert(struct ext_code ext_code,
  1812. unsigned int alert, unsigned long unused)
  1813. {
  1814. struct cpu_hw_sf *cpuhw;
  1815. if (!(alert & CPU_MF_INT_SF_MASK))
  1816. return;
  1817. inc_irq_stat(IRQEXT_CMS);
  1818. cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1819. /* Measurement alerts are shared and might happen when the PMU
  1820. * is not reserved. Ignore these alerts in this case. */
  1821. if (!(cpuhw->flags & PMU_F_RESERVED))
  1822. return;
  1823. /* The processing below must take care of multiple alert events that
  1824. * might be indicated concurrently. */
  1825. /* Program alert request */
  1826. if (alert & CPU_MF_INT_SF_PRA) {
  1827. if (cpuhw->flags & PMU_F_IN_USE)
  1828. if (SAMPL_DIAG_MODE(&cpuhw->event->hw))
  1829. hw_collect_aux(cpuhw);
  1830. else
  1831. hw_perf_event_update(cpuhw->event, 0);
  1832. else
  1833. WARN_ON_ONCE(!(cpuhw->flags & PMU_F_IN_USE));
  1834. }
  1835. /* Report measurement alerts only for non-PRA codes */
  1836. if (alert != CPU_MF_INT_SF_PRA)
  1837. debug_sprintf_event(sfdbg, 6, "%s: alert %#x\n", __func__,
  1838. alert);
  1839. /* Sampling authorization change request */
  1840. if (alert & CPU_MF_INT_SF_SACA)
  1841. qsi(&cpuhw->qsi);
  1842. /* Loss of sample data due to high-priority machine activities */
  1843. if (alert & CPU_MF_INT_SF_LSDA) {
  1844. pr_err("Sample data was lost\n");
  1845. cpuhw->flags |= PMU_F_ERR_LSDA;
  1846. sf_disable();
  1847. }
  1848. /* Invalid sampling buffer entry */
  1849. if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) {
  1850. pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
  1851. alert);
  1852. cpuhw->flags |= PMU_F_ERR_IBE;
  1853. sf_disable();
  1854. }
  1855. }
  1856. static int cpusf_pmu_setup(unsigned int cpu, int flags)
  1857. {
  1858. /* Ignore the notification if no events are scheduled on the PMU.
  1859. * This might be racy...
  1860. */
  1861. if (!atomic_read(&num_events))
  1862. return 0;
  1863. local_irq_disable();
  1864. setup_pmc_cpu(&flags);
  1865. local_irq_enable();
  1866. return 0;
  1867. }
  1868. static int s390_pmu_sf_online_cpu(unsigned int cpu)
  1869. {
  1870. return cpusf_pmu_setup(cpu, PMC_INIT);
  1871. }
  1872. static int s390_pmu_sf_offline_cpu(unsigned int cpu)
  1873. {
  1874. return cpusf_pmu_setup(cpu, PMC_RELEASE);
  1875. }
  1876. static int param_get_sfb_size(char *buffer, const struct kernel_param *kp)
  1877. {
  1878. if (!cpum_sf_avail())
  1879. return -ENODEV;
  1880. return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1881. }
  1882. static int param_set_sfb_size(const char *val, const struct kernel_param *kp)
  1883. {
  1884. int rc;
  1885. unsigned long min, max;
  1886. if (!cpum_sf_avail())
  1887. return -ENODEV;
  1888. if (!val || !strlen(val))
  1889. return -EINVAL;
  1890. /* Valid parameter values: "min,max" or "max" */
  1891. min = CPUM_SF_MIN_SDB;
  1892. max = CPUM_SF_MAX_SDB;
  1893. if (strchr(val, ','))
  1894. rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL;
  1895. else
  1896. rc = kstrtoul(val, 10, &max);
  1897. if (min < 2 || min >= max || max > get_num_physpages())
  1898. rc = -EINVAL;
  1899. if (rc)
  1900. return rc;
  1901. sfb_set_limits(min, max);
  1902. pr_info("The sampling buffer limits have changed to: "
  1903. "min %lu max %lu (diag %lu)\n",
  1904. CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR);
  1905. return 0;
  1906. }
  1907. #define param_check_sfb_size(name, p) __param_check(name, p, void)
  1908. static const struct kernel_param_ops param_ops_sfb_size = {
  1909. .set = param_set_sfb_size,
  1910. .get = param_get_sfb_size,
  1911. };
  1912. #define RS_INIT_FAILURE_QSI 0x0001
  1913. #define RS_INIT_FAILURE_BSDES 0x0002
  1914. #define RS_INIT_FAILURE_ALRT 0x0003
  1915. #define RS_INIT_FAILURE_PERF 0x0004
  1916. static void __init pr_cpumsf_err(unsigned int reason)
  1917. {
  1918. pr_err("Sampling facility support for perf is not available: "
  1919. "reason %#x\n", reason);
  1920. }
  1921. static int __init init_cpum_sampling_pmu(void)
  1922. {
  1923. struct hws_qsi_info_block si;
  1924. int err;
  1925. if (!cpum_sf_avail())
  1926. return -ENODEV;
  1927. memset(&si, 0, sizeof(si));
  1928. if (qsi(&si)) {
  1929. pr_cpumsf_err(RS_INIT_FAILURE_QSI);
  1930. return -ENODEV;
  1931. }
  1932. if (!si.as && !si.ad)
  1933. return -ENODEV;
  1934. if (si.bsdes != sizeof(struct hws_basic_entry)) {
  1935. pr_cpumsf_err(RS_INIT_FAILURE_BSDES);
  1936. return -EINVAL;
  1937. }
  1938. if (si.ad) {
  1939. sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1940. /* Sampling of diagnostic data authorized,
  1941. * install event into attribute list of PMU device.
  1942. */
  1943. cpumsf_pmu_events_attr[SF_CYCLES_BASIC_DIAG_ATTR_IDX] =
  1944. CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG);
  1945. }
  1946. sfdbg = debug_register(KMSG_COMPONENT, 2, 1, 80);
  1947. if (!sfdbg) {
  1948. pr_err("Registering for s390dbf failed\n");
  1949. return -ENOMEM;
  1950. }
  1951. debug_register_view(sfdbg, &debug_sprintf_view);
  1952. err = register_external_irq(EXT_IRQ_MEASURE_ALERT,
  1953. cpumf_measurement_alert);
  1954. if (err) {
  1955. pr_cpumsf_err(RS_INIT_FAILURE_ALRT);
  1956. debug_unregister(sfdbg);
  1957. goto out;
  1958. }
  1959. err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW);
  1960. if (err) {
  1961. pr_cpumsf_err(RS_INIT_FAILURE_PERF);
  1962. unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
  1963. cpumf_measurement_alert);
  1964. debug_unregister(sfdbg);
  1965. goto out;
  1966. }
  1967. cpuhp_setup_state(CPUHP_AP_PERF_S390_SF_ONLINE, "perf/s390/sf:online",
  1968. s390_pmu_sf_online_cpu, s390_pmu_sf_offline_cpu);
  1969. out:
  1970. return err;
  1971. }
  1972. arch_initcall(init_cpum_sampling_pmu);
  1973. core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0644);