perf_cpum_sf.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * Performance event support for the System z CPU-measurement Sampling Facility
  3. *
  4. * Copyright IBM Corp. 2013
  5. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (version 2 only)
  9. * as published by the Free Software Foundation.
  10. */
  11. #define KMSG_COMPONENT "cpum_sf"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/percpu.h>
  17. #include <linux/notifier.h>
  18. #include <linux/export.h>
  19. #include <linux/slab.h>
  20. #include <linux/mm.h>
  21. #include <linux/moduleparam.h>
  22. #include <asm/cpu_mf.h>
  23. #include <asm/irq.h>
  24. #include <asm/debug.h>
  25. #include <asm/timex.h>
  26. /* Minimum number of sample-data-block-tables:
  27. * At least one table is required for the sampling buffer structure.
  28. * A single table contains up to 511 pointers to sample-data-blocks.
  29. */
  30. #define CPUM_SF_MIN_SDBT 1
  31. /* Number of sample-data-blocks per sample-data-block-table (SDBT):
  32. * A table contains SDB pointers (8 bytes) and one table-link entry
  33. * that points to the origin of the next SDBT.
  34. */
  35. #define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8)
  36. /* Maximum page offset for an SDBT table-link entry:
  37. * If this page offset is reached, a table-link entry to the next SDBT
  38. * must be added.
  39. */
  40. #define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8)
  41. static inline int require_table_link(const void *sdbt)
  42. {
  43. return ((unsigned long) sdbt & ~PAGE_MASK) == CPUM_SF_SDBT_TL_OFFSET;
  44. }
  45. /* Minimum and maximum sampling buffer sizes:
  46. *
  47. * This number represents the maximum size of the sampling buffer taking
  48. * the number of sample-data-block-tables into account. Note that these
  49. * numbers apply to the basic-sampling function only.
  50. * The maximum number of SDBs is increased by CPUM_SF_SDB_DIAG_FACTOR if
  51. * the diagnostic-sampling function is active.
  52. *
  53. * Sampling buffer size Buffer characteristics
  54. * ---------------------------------------------------
  55. * 64KB == 16 pages (4KB per page)
  56. * 1 page for SDB-tables
  57. * 15 pages for SDBs
  58. *
  59. * 32MB == 8192 pages (4KB per page)
  60. * 16 pages for SDB-tables
  61. * 8176 pages for SDBs
  62. */
  63. static unsigned long __read_mostly CPUM_SF_MIN_SDB = 15;
  64. static unsigned long __read_mostly CPUM_SF_MAX_SDB = 8176;
  65. static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR = 1;
  66. struct sf_buffer {
  67. unsigned long *sdbt; /* Sample-data-block-table origin */
  68. /* buffer characteristics (required for buffer increments) */
  69. unsigned long num_sdb; /* Number of sample-data-blocks */
  70. unsigned long num_sdbt; /* Number of sample-data-block-tables */
  71. unsigned long *tail; /* last sample-data-block-table */
  72. };
  73. struct cpu_hw_sf {
  74. /* CPU-measurement sampling information block */
  75. struct hws_qsi_info_block qsi;
  76. /* CPU-measurement sampling control block */
  77. struct hws_lsctl_request_block lsctl;
  78. struct sf_buffer sfb; /* Sampling buffer */
  79. unsigned int flags; /* Status flags */
  80. struct perf_event *event; /* Scheduled perf event */
  81. };
  82. static DEFINE_PER_CPU(struct cpu_hw_sf, cpu_hw_sf);
  83. /* Debug feature */
  84. static debug_info_t *sfdbg;
  85. /*
  86. * sf_disable() - Switch off sampling facility
  87. */
  88. static int sf_disable(void)
  89. {
  90. struct hws_lsctl_request_block sreq;
  91. memset(&sreq, 0, sizeof(sreq));
  92. return lsctl(&sreq);
  93. }
  94. /*
  95. * sf_buffer_available() - Check for an allocated sampling buffer
  96. */
  97. static int sf_buffer_available(struct cpu_hw_sf *cpuhw)
  98. {
  99. return !!cpuhw->sfb.sdbt;
  100. }
  101. /*
  102. * deallocate sampling facility buffer
  103. */
  104. static void free_sampling_buffer(struct sf_buffer *sfb)
  105. {
  106. unsigned long *sdbt, *curr;
  107. if (!sfb->sdbt)
  108. return;
  109. sdbt = sfb->sdbt;
  110. curr = sdbt;
  111. /* Free the SDBT after all SDBs are processed... */
  112. while (1) {
  113. if (!*curr || !sdbt)
  114. break;
  115. /* Process table-link entries */
  116. if (is_link_entry(curr)) {
  117. curr = get_next_sdbt(curr);
  118. if (sdbt)
  119. free_page((unsigned long) sdbt);
  120. /* If the origin is reached, sampling buffer is freed */
  121. if (curr == sfb->sdbt)
  122. break;
  123. else
  124. sdbt = curr;
  125. } else {
  126. /* Process SDB pointer */
  127. if (*curr) {
  128. free_page(*curr);
  129. curr++;
  130. }
  131. }
  132. }
  133. debug_sprintf_event(sfdbg, 5,
  134. "free_sampling_buffer: freed sdbt=%p\n", sfb->sdbt);
  135. memset(sfb, 0, sizeof(*sfb));
  136. }
  137. static int alloc_sample_data_block(unsigned long *sdbt, gfp_t gfp_flags)
  138. {
  139. unsigned long sdb, *trailer;
  140. /* Allocate and initialize sample-data-block */
  141. sdb = get_zeroed_page(gfp_flags);
  142. if (!sdb)
  143. return -ENOMEM;
  144. trailer = trailer_entry_ptr(sdb);
  145. *trailer = SDB_TE_ALERT_REQ_MASK;
  146. /* Link SDB into the sample-data-block-table */
  147. *sdbt = sdb;
  148. return 0;
  149. }
  150. /*
  151. * realloc_sampling_buffer() - extend sampler memory
  152. *
  153. * Allocates new sample-data-blocks and adds them to the specified sampling
  154. * buffer memory.
  155. *
  156. * Important: This modifies the sampling buffer and must be called when the
  157. * sampling facility is disabled.
  158. *
  159. * Returns zero on success, non-zero otherwise.
  160. */
  161. static int realloc_sampling_buffer(struct sf_buffer *sfb,
  162. unsigned long num_sdb, gfp_t gfp_flags)
  163. {
  164. int i, rc;
  165. unsigned long *new, *tail;
  166. if (!sfb->sdbt || !sfb->tail)
  167. return -EINVAL;
  168. if (!is_link_entry(sfb->tail))
  169. return -EINVAL;
  170. /* Append to the existing sampling buffer, overwriting the table-link
  171. * register.
  172. * The tail variables always points to the "tail" (last and table-link)
  173. * entry in an SDB-table.
  174. */
  175. tail = sfb->tail;
  176. /* Do a sanity check whether the table-link entry points to
  177. * the sampling buffer origin.
  178. */
  179. if (sfb->sdbt != get_next_sdbt(tail)) {
  180. debug_sprintf_event(sfdbg, 3, "realloc_sampling_buffer: "
  181. "sampling buffer is not linked: origin=%p"
  182. "tail=%p\n",
  183. (void *) sfb->sdbt, (void *) tail);
  184. return -EINVAL;
  185. }
  186. /* Allocate remaining SDBs */
  187. rc = 0;
  188. for (i = 0; i < num_sdb; i++) {
  189. /* Allocate a new SDB-table if it is full. */
  190. if (require_table_link(tail)) {
  191. new = (unsigned long *) get_zeroed_page(gfp_flags);
  192. if (!new) {
  193. rc = -ENOMEM;
  194. break;
  195. }
  196. sfb->num_sdbt++;
  197. /* Link current page to tail of chain */
  198. *tail = (unsigned long)(void *) new + 1;
  199. tail = new;
  200. }
  201. /* Allocate a new sample-data-block.
  202. * If there is not enough memory, stop the realloc process
  203. * and simply use what was allocated. If this is a temporary
  204. * issue, a new realloc call (if required) might succeed.
  205. */
  206. rc = alloc_sample_data_block(tail, gfp_flags);
  207. if (rc)
  208. break;
  209. sfb->num_sdb++;
  210. tail++;
  211. }
  212. /* Link sampling buffer to its origin */
  213. *tail = (unsigned long) sfb->sdbt + 1;
  214. sfb->tail = tail;
  215. debug_sprintf_event(sfdbg, 4, "realloc_sampling_buffer: new buffer"
  216. " settings: sdbt=%lu sdb=%lu\n",
  217. sfb->num_sdbt, sfb->num_sdb);
  218. return rc;
  219. }
  220. /*
  221. * allocate_sampling_buffer() - allocate sampler memory
  222. *
  223. * Allocates and initializes a sampling buffer structure using the
  224. * specified number of sample-data-blocks (SDB). For each allocation,
  225. * a 4K page is used. The number of sample-data-block-tables (SDBT)
  226. * are calculated from SDBs.
  227. * Also set the ALERT_REQ mask in each SDBs trailer.
  228. *
  229. * Returns zero on success, non-zero otherwise.
  230. */
  231. static int alloc_sampling_buffer(struct sf_buffer *sfb, unsigned long num_sdb)
  232. {
  233. int rc;
  234. if (sfb->sdbt)
  235. return -EINVAL;
  236. /* Allocate the sample-data-block-table origin */
  237. sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  238. if (!sfb->sdbt)
  239. return -ENOMEM;
  240. sfb->num_sdb = 0;
  241. sfb->num_sdbt = 1;
  242. /* Link the table origin to point to itself to prepare for
  243. * realloc_sampling_buffer() invocation.
  244. */
  245. sfb->tail = sfb->sdbt;
  246. *sfb->tail = (unsigned long)(void *) sfb->sdbt + 1;
  247. /* Allocate requested number of sample-data-blocks */
  248. rc = realloc_sampling_buffer(sfb, num_sdb, GFP_KERNEL);
  249. if (rc) {
  250. free_sampling_buffer(sfb);
  251. debug_sprintf_event(sfdbg, 4, "alloc_sampling_buffer: "
  252. "realloc_sampling_buffer failed with rc=%i\n", rc);
  253. } else
  254. debug_sprintf_event(sfdbg, 4,
  255. "alloc_sampling_buffer: tear=%p dear=%p\n",
  256. sfb->sdbt, (void *) *sfb->sdbt);
  257. return rc;
  258. }
  259. static void sfb_set_limits(unsigned long min, unsigned long max)
  260. {
  261. struct hws_qsi_info_block si;
  262. CPUM_SF_MIN_SDB = min;
  263. CPUM_SF_MAX_SDB = max;
  264. memset(&si, 0, sizeof(si));
  265. if (!qsi(&si))
  266. CPUM_SF_SDB_DIAG_FACTOR = DIV_ROUND_UP(si.dsdes, si.bsdes);
  267. }
  268. static unsigned long sfb_max_limit(struct hw_perf_event *hwc)
  269. {
  270. return SAMPL_DIAG_MODE(hwc) ? CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR
  271. : CPUM_SF_MAX_SDB;
  272. }
  273. static unsigned long sfb_pending_allocs(struct sf_buffer *sfb,
  274. struct hw_perf_event *hwc)
  275. {
  276. if (!sfb->sdbt)
  277. return SFB_ALLOC_REG(hwc);
  278. if (SFB_ALLOC_REG(hwc) > sfb->num_sdb)
  279. return SFB_ALLOC_REG(hwc) - sfb->num_sdb;
  280. return 0;
  281. }
  282. static int sfb_has_pending_allocs(struct sf_buffer *sfb,
  283. struct hw_perf_event *hwc)
  284. {
  285. return sfb_pending_allocs(sfb, hwc) > 0;
  286. }
  287. static void sfb_account_allocs(unsigned long num, struct hw_perf_event *hwc)
  288. {
  289. /* Limit the number of SDBs to not exceed the maximum */
  290. num = min_t(unsigned long, num, sfb_max_limit(hwc) - SFB_ALLOC_REG(hwc));
  291. if (num)
  292. SFB_ALLOC_REG(hwc) += num;
  293. }
  294. static void sfb_init_allocs(unsigned long num, struct hw_perf_event *hwc)
  295. {
  296. SFB_ALLOC_REG(hwc) = 0;
  297. sfb_account_allocs(num, hwc);
  298. }
  299. static size_t event_sample_size(struct hw_perf_event *hwc)
  300. {
  301. struct sf_raw_sample *sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
  302. size_t sample_size;
  303. /* The sample size depends on the sampling function: The basic-sampling
  304. * function must be always enabled, diagnostic-sampling function is
  305. * optional.
  306. */
  307. sample_size = sfr->bsdes;
  308. if (SAMPL_DIAG_MODE(hwc))
  309. sample_size += sfr->dsdes;
  310. return sample_size;
  311. }
  312. static void deallocate_buffers(struct cpu_hw_sf *cpuhw)
  313. {
  314. if (cpuhw->sfb.sdbt)
  315. free_sampling_buffer(&cpuhw->sfb);
  316. }
  317. static int allocate_buffers(struct cpu_hw_sf *cpuhw, struct hw_perf_event *hwc)
  318. {
  319. unsigned long n_sdb, freq, factor;
  320. size_t sfr_size, sample_size;
  321. struct sf_raw_sample *sfr;
  322. /* Allocate raw sample buffer
  323. *
  324. * The raw sample buffer is used to temporarily store sampling data
  325. * entries for perf raw sample processing. The buffer size mainly
  326. * depends on the size of diagnostic-sampling data entries which is
  327. * machine-specific. The exact size calculation includes:
  328. * 1. The first 4 bytes of diagnostic-sampling data entries are
  329. * already reflected in the sf_raw_sample structure. Subtract
  330. * these bytes.
  331. * 2. The perf raw sample data must be 8-byte aligned (u64) and
  332. * perf's internal data size must be considered too. So add
  333. * an additional u32 for correct alignment and subtract before
  334. * allocating the buffer.
  335. * 3. Store the raw sample buffer pointer in the perf event
  336. * hardware structure.
  337. */
  338. sfr_size = ALIGN((sizeof(*sfr) - sizeof(sfr->diag) + cpuhw->qsi.dsdes) +
  339. sizeof(u32), sizeof(u64));
  340. sfr_size -= sizeof(u32);
  341. sfr = kzalloc(sfr_size, GFP_KERNEL);
  342. if (!sfr)
  343. return -ENOMEM;
  344. sfr->size = sfr_size;
  345. sfr->bsdes = cpuhw->qsi.bsdes;
  346. sfr->dsdes = cpuhw->qsi.dsdes;
  347. RAWSAMPLE_REG(hwc) = (unsigned long) sfr;
  348. /* Calculate sampling buffers using 4K pages
  349. *
  350. * 1. Determine the sample data size which depends on the used
  351. * sampling functions, for example, basic-sampling or
  352. * basic-sampling with diagnostic-sampling.
  353. *
  354. * 2. Use the sampling frequency as input. The sampling buffer is
  355. * designed for almost one second. This can be adjusted through
  356. * the "factor" variable.
  357. * In any case, alloc_sampling_buffer() sets the Alert Request
  358. * Control indicator to trigger a measurement-alert to harvest
  359. * sample-data-blocks (sdb).
  360. *
  361. * 3. Compute the number of sample-data-blocks and ensure a minimum
  362. * of CPUM_SF_MIN_SDB. Also ensure the upper limit does not
  363. * exceed a "calculated" maximum. The symbolic maximum is
  364. * designed for basic-sampling only and needs to be increased if
  365. * diagnostic-sampling is active.
  366. * See also the remarks for these symbolic constants.
  367. *
  368. * 4. Compute the number of sample-data-block-tables (SDBT) and
  369. * ensure a minimum of CPUM_SF_MIN_SDBT (one table can manage up
  370. * to 511 SDBs).
  371. */
  372. sample_size = event_sample_size(hwc);
  373. freq = sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc));
  374. factor = 1;
  375. n_sdb = DIV_ROUND_UP(freq, factor * ((PAGE_SIZE-64) / sample_size));
  376. if (n_sdb < CPUM_SF_MIN_SDB)
  377. n_sdb = CPUM_SF_MIN_SDB;
  378. /* If there is already a sampling buffer allocated, it is very likely
  379. * that the sampling facility is enabled too. If the event to be
  380. * initialized requires a greater sampling buffer, the allocation must
  381. * be postponed. Changing the sampling buffer requires the sampling
  382. * facility to be in the disabled state. So, account the number of
  383. * required SDBs and let cpumsf_pmu_enable() resize the buffer just
  384. * before the event is started.
  385. */
  386. sfb_init_allocs(n_sdb, hwc);
  387. if (sf_buffer_available(cpuhw))
  388. return 0;
  389. debug_sprintf_event(sfdbg, 3,
  390. "allocate_buffers: rate=%lu f=%lu sdb=%lu/%lu"
  391. " sample_size=%lu cpuhw=%p\n",
  392. SAMPL_RATE(hwc), freq, n_sdb, sfb_max_limit(hwc),
  393. sample_size, cpuhw);
  394. return alloc_sampling_buffer(&cpuhw->sfb,
  395. sfb_pending_allocs(&cpuhw->sfb, hwc));
  396. }
  397. static unsigned long min_percent(unsigned int percent, unsigned long base,
  398. unsigned long min)
  399. {
  400. return min_t(unsigned long, min, DIV_ROUND_UP(percent * base, 100));
  401. }
  402. static unsigned long compute_sfb_extent(unsigned long ratio, unsigned long base)
  403. {
  404. /* Use a percentage-based approach to extend the sampling facility
  405. * buffer. Accept up to 5% sample data loss.
  406. * Vary the extents between 1% to 5% of the current number of
  407. * sample-data-blocks.
  408. */
  409. if (ratio <= 5)
  410. return 0;
  411. if (ratio <= 25)
  412. return min_percent(1, base, 1);
  413. if (ratio <= 50)
  414. return min_percent(1, base, 1);
  415. if (ratio <= 75)
  416. return min_percent(2, base, 2);
  417. if (ratio <= 100)
  418. return min_percent(3, base, 3);
  419. if (ratio <= 250)
  420. return min_percent(4, base, 4);
  421. return min_percent(5, base, 8);
  422. }
  423. static void sfb_account_overflows(struct cpu_hw_sf *cpuhw,
  424. struct hw_perf_event *hwc)
  425. {
  426. unsigned long ratio, num;
  427. if (!OVERFLOW_REG(hwc))
  428. return;
  429. /* The sample_overflow contains the average number of sample data
  430. * that has been lost because sample-data-blocks were full.
  431. *
  432. * Calculate the total number of sample data entries that has been
  433. * discarded. Then calculate the ratio of lost samples to total samples
  434. * per second in percent.
  435. */
  436. ratio = DIV_ROUND_UP(100 * OVERFLOW_REG(hwc) * cpuhw->sfb.num_sdb,
  437. sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc)));
  438. /* Compute number of sample-data-blocks */
  439. num = compute_sfb_extent(ratio, cpuhw->sfb.num_sdb);
  440. if (num)
  441. sfb_account_allocs(num, hwc);
  442. debug_sprintf_event(sfdbg, 5, "sfb: overflow: overflow=%llu ratio=%lu"
  443. " num=%lu\n", OVERFLOW_REG(hwc), ratio, num);
  444. OVERFLOW_REG(hwc) = 0;
  445. }
  446. /* extend_sampling_buffer() - Extend sampling buffer
  447. * @sfb: Sampling buffer structure (for local CPU)
  448. * @hwc: Perf event hardware structure
  449. *
  450. * Use this function to extend the sampling buffer based on the overflow counter
  451. * and postponed allocation extents stored in the specified Perf event hardware.
  452. *
  453. * Important: This function disables the sampling facility in order to safely
  454. * change the sampling buffer structure. Do not call this function
  455. * when the PMU is active.
  456. */
  457. static void extend_sampling_buffer(struct sf_buffer *sfb,
  458. struct hw_perf_event *hwc)
  459. {
  460. unsigned long num, num_old;
  461. int rc;
  462. num = sfb_pending_allocs(sfb, hwc);
  463. if (!num)
  464. return;
  465. num_old = sfb->num_sdb;
  466. /* Disable the sampling facility to reset any states and also
  467. * clear pending measurement alerts.
  468. */
  469. sf_disable();
  470. /* Extend the sampling buffer.
  471. * This memory allocation typically happens in an atomic context when
  472. * called by perf. Because this is a reallocation, it is fine if the
  473. * new SDB-request cannot be satisfied immediately.
  474. */
  475. rc = realloc_sampling_buffer(sfb, num, GFP_ATOMIC);
  476. if (rc)
  477. debug_sprintf_event(sfdbg, 5, "sfb: extend: realloc "
  478. "failed with rc=%i\n", rc);
  479. if (sfb_has_pending_allocs(sfb, hwc))
  480. debug_sprintf_event(sfdbg, 5, "sfb: extend: "
  481. "req=%lu alloc=%lu remaining=%lu\n",
  482. num, sfb->num_sdb - num_old,
  483. sfb_pending_allocs(sfb, hwc));
  484. }
  485. /* Number of perf events counting hardware events */
  486. static atomic_t num_events;
  487. /* Used to avoid races in calling reserve/release_cpumf_hardware */
  488. static DEFINE_MUTEX(pmc_reserve_mutex);
  489. #define PMC_INIT 0
  490. #define PMC_RELEASE 1
  491. #define PMC_FAILURE 2
  492. static void setup_pmc_cpu(void *flags)
  493. {
  494. int err;
  495. struct cpu_hw_sf *cpusf = this_cpu_ptr(&cpu_hw_sf);
  496. err = 0;
  497. switch (*((int *) flags)) {
  498. case PMC_INIT:
  499. memset(cpusf, 0, sizeof(*cpusf));
  500. err = qsi(&cpusf->qsi);
  501. if (err)
  502. break;
  503. cpusf->flags |= PMU_F_RESERVED;
  504. err = sf_disable();
  505. if (err)
  506. pr_err("Switching off the sampling facility failed "
  507. "with rc=%i\n", err);
  508. debug_sprintf_event(sfdbg, 5,
  509. "setup_pmc_cpu: initialized: cpuhw=%p\n", cpusf);
  510. break;
  511. case PMC_RELEASE:
  512. cpusf->flags &= ~PMU_F_RESERVED;
  513. err = sf_disable();
  514. if (err) {
  515. pr_err("Switching off the sampling facility failed "
  516. "with rc=%i\n", err);
  517. } else
  518. deallocate_buffers(cpusf);
  519. debug_sprintf_event(sfdbg, 5,
  520. "setup_pmc_cpu: released: cpuhw=%p\n", cpusf);
  521. break;
  522. }
  523. if (err)
  524. *((int *) flags) |= PMC_FAILURE;
  525. }
  526. static void release_pmc_hardware(void)
  527. {
  528. int flags = PMC_RELEASE;
  529. irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  530. on_each_cpu(setup_pmc_cpu, &flags, 1);
  531. perf_release_sampling();
  532. }
  533. static int reserve_pmc_hardware(void)
  534. {
  535. int flags = PMC_INIT;
  536. int err;
  537. err = perf_reserve_sampling();
  538. if (err)
  539. return err;
  540. on_each_cpu(setup_pmc_cpu, &flags, 1);
  541. if (flags & PMC_FAILURE) {
  542. release_pmc_hardware();
  543. return -ENODEV;
  544. }
  545. irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  546. return 0;
  547. }
  548. static void hw_perf_event_destroy(struct perf_event *event)
  549. {
  550. /* Free raw sample buffer */
  551. if (RAWSAMPLE_REG(&event->hw))
  552. kfree((void *) RAWSAMPLE_REG(&event->hw));
  553. /* Release PMC if this is the last perf event */
  554. if (!atomic_add_unless(&num_events, -1, 1)) {
  555. mutex_lock(&pmc_reserve_mutex);
  556. if (atomic_dec_return(&num_events) == 0)
  557. release_pmc_hardware();
  558. mutex_unlock(&pmc_reserve_mutex);
  559. }
  560. }
  561. static void hw_init_period(struct hw_perf_event *hwc, u64 period)
  562. {
  563. hwc->sample_period = period;
  564. hwc->last_period = hwc->sample_period;
  565. local64_set(&hwc->period_left, hwc->sample_period);
  566. }
  567. static void hw_reset_registers(struct hw_perf_event *hwc,
  568. unsigned long *sdbt_origin)
  569. {
  570. struct sf_raw_sample *sfr;
  571. /* (Re)set to first sample-data-block-table */
  572. TEAR_REG(hwc) = (unsigned long) sdbt_origin;
  573. /* (Re)set raw sampling buffer register */
  574. sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
  575. memset(&sfr->basic, 0, sizeof(sfr->basic));
  576. memset(&sfr->diag, 0, sfr->dsdes);
  577. }
  578. static unsigned long hw_limit_rate(const struct hws_qsi_info_block *si,
  579. unsigned long rate)
  580. {
  581. return clamp_t(unsigned long, rate,
  582. si->min_sampl_rate, si->max_sampl_rate);
  583. }
  584. static int __hw_perf_event_init(struct perf_event *event)
  585. {
  586. struct cpu_hw_sf *cpuhw;
  587. struct hws_qsi_info_block si;
  588. struct perf_event_attr *attr = &event->attr;
  589. struct hw_perf_event *hwc = &event->hw;
  590. unsigned long rate;
  591. int cpu, err;
  592. /* Reserve CPU-measurement sampling facility */
  593. err = 0;
  594. if (!atomic_inc_not_zero(&num_events)) {
  595. mutex_lock(&pmc_reserve_mutex);
  596. if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
  597. err = -EBUSY;
  598. else
  599. atomic_inc(&num_events);
  600. mutex_unlock(&pmc_reserve_mutex);
  601. }
  602. event->destroy = hw_perf_event_destroy;
  603. if (err)
  604. goto out;
  605. /* Access per-CPU sampling information (query sampling info) */
  606. /*
  607. * The event->cpu value can be -1 to count on every CPU, for example,
  608. * when attaching to a task. If this is specified, use the query
  609. * sampling info from the current CPU, otherwise use event->cpu to
  610. * retrieve the per-CPU information.
  611. * Later, cpuhw indicates whether to allocate sampling buffers for a
  612. * particular CPU (cpuhw!=NULL) or each online CPU (cpuw==NULL).
  613. */
  614. memset(&si, 0, sizeof(si));
  615. cpuhw = NULL;
  616. if (event->cpu == -1)
  617. qsi(&si);
  618. else {
  619. /* Event is pinned to a particular CPU, retrieve the per-CPU
  620. * sampling structure for accessing the CPU-specific QSI.
  621. */
  622. cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
  623. si = cpuhw->qsi;
  624. }
  625. /* Check sampling facility authorization and, if not authorized,
  626. * fall back to other PMUs. It is safe to check any CPU because
  627. * the authorization is identical for all configured CPUs.
  628. */
  629. if (!si.as) {
  630. err = -ENOENT;
  631. goto out;
  632. }
  633. /* Always enable basic sampling */
  634. SAMPL_FLAGS(hwc) = PERF_CPUM_SF_BASIC_MODE;
  635. /* Check if diagnostic sampling is requested. Deny if the required
  636. * sampling authorization is missing.
  637. */
  638. if (attr->config == PERF_EVENT_CPUM_SF_DIAG) {
  639. if (!si.ad) {
  640. err = -EPERM;
  641. goto out;
  642. }
  643. SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_DIAG_MODE;
  644. }
  645. /* Check and set other sampling flags */
  646. if (attr->config1 & PERF_CPUM_SF_FULL_BLOCKS)
  647. SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FULL_BLOCKS;
  648. /* The sampling information (si) contains information about the
  649. * min/max sampling intervals and the CPU speed. So calculate the
  650. * correct sampling interval and avoid the whole period adjust
  651. * feedback loop.
  652. */
  653. rate = 0;
  654. if (attr->freq) {
  655. if (!attr->sample_freq) {
  656. err = -EINVAL;
  657. goto out;
  658. }
  659. rate = freq_to_sample_rate(&si, attr->sample_freq);
  660. rate = hw_limit_rate(&si, rate);
  661. attr->freq = 0;
  662. attr->sample_period = rate;
  663. } else {
  664. /* The min/max sampling rates specifies the valid range
  665. * of sample periods. If the specified sample period is
  666. * out of range, limit the period to the range boundary.
  667. */
  668. rate = hw_limit_rate(&si, hwc->sample_period);
  669. /* The perf core maintains a maximum sample rate that is
  670. * configurable through the sysctl interface. Ensure the
  671. * sampling rate does not exceed this value. This also helps
  672. * to avoid throttling when pushing samples with
  673. * perf_event_overflow().
  674. */
  675. if (sample_rate_to_freq(&si, rate) >
  676. sysctl_perf_event_sample_rate) {
  677. err = -EINVAL;
  678. debug_sprintf_event(sfdbg, 1, "Sampling rate exceeds maximum perf sample rate\n");
  679. goto out;
  680. }
  681. }
  682. SAMPL_RATE(hwc) = rate;
  683. hw_init_period(hwc, SAMPL_RATE(hwc));
  684. /* Initialize sample data overflow accounting */
  685. hwc->extra_reg.reg = REG_OVERFLOW;
  686. OVERFLOW_REG(hwc) = 0;
  687. /* Allocate the per-CPU sampling buffer using the CPU information
  688. * from the event. If the event is not pinned to a particular
  689. * CPU (event->cpu == -1; or cpuhw == NULL), allocate sampling
  690. * buffers for each online CPU.
  691. */
  692. if (cpuhw)
  693. /* Event is pinned to a particular CPU */
  694. err = allocate_buffers(cpuhw, hwc);
  695. else {
  696. /* Event is not pinned, allocate sampling buffer on
  697. * each online CPU
  698. */
  699. for_each_online_cpu(cpu) {
  700. cpuhw = &per_cpu(cpu_hw_sf, cpu);
  701. err = allocate_buffers(cpuhw, hwc);
  702. if (err)
  703. break;
  704. }
  705. }
  706. out:
  707. return err;
  708. }
  709. static int cpumsf_pmu_event_init(struct perf_event *event)
  710. {
  711. int err;
  712. /* No support for taken branch sampling */
  713. if (has_branch_stack(event))
  714. return -EOPNOTSUPP;
  715. switch (event->attr.type) {
  716. case PERF_TYPE_RAW:
  717. if ((event->attr.config != PERF_EVENT_CPUM_SF) &&
  718. (event->attr.config != PERF_EVENT_CPUM_SF_DIAG))
  719. return -ENOENT;
  720. break;
  721. case PERF_TYPE_HARDWARE:
  722. /* Support sampling of CPU cycles in addition to the
  723. * counter facility. However, the counter facility
  724. * is more precise and, hence, restrict this PMU to
  725. * sampling events only.
  726. */
  727. if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES)
  728. return -ENOENT;
  729. if (!is_sampling_event(event))
  730. return -ENOENT;
  731. break;
  732. default:
  733. return -ENOENT;
  734. }
  735. /* Check online status of the CPU to which the event is pinned */
  736. if (event->cpu >= nr_cpumask_bits ||
  737. (event->cpu >= 0 && !cpu_online(event->cpu)))
  738. return -ENODEV;
  739. /* Force reset of idle/hv excludes regardless of what the
  740. * user requested.
  741. */
  742. if (event->attr.exclude_hv)
  743. event->attr.exclude_hv = 0;
  744. if (event->attr.exclude_idle)
  745. event->attr.exclude_idle = 0;
  746. err = __hw_perf_event_init(event);
  747. if (unlikely(err))
  748. if (event->destroy)
  749. event->destroy(event);
  750. return err;
  751. }
  752. static void cpumsf_pmu_enable(struct pmu *pmu)
  753. {
  754. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  755. struct hw_perf_event *hwc;
  756. int err;
  757. if (cpuhw->flags & PMU_F_ENABLED)
  758. return;
  759. if (cpuhw->flags & PMU_F_ERR_MASK)
  760. return;
  761. /* Check whether to extent the sampling buffer.
  762. *
  763. * Two conditions trigger an increase of the sampling buffer for a
  764. * perf event:
  765. * 1. Postponed buffer allocations from the event initialization.
  766. * 2. Sampling overflows that contribute to pending allocations.
  767. *
  768. * Note that the extend_sampling_buffer() function disables the sampling
  769. * facility, but it can be fully re-enabled using sampling controls that
  770. * have been saved in cpumsf_pmu_disable().
  771. */
  772. if (cpuhw->event) {
  773. hwc = &cpuhw->event->hw;
  774. /* Account number of overflow-designated buffer extents */
  775. sfb_account_overflows(cpuhw, hwc);
  776. if (sfb_has_pending_allocs(&cpuhw->sfb, hwc))
  777. extend_sampling_buffer(&cpuhw->sfb, hwc);
  778. }
  779. /* (Re)enable the PMU and sampling facility */
  780. cpuhw->flags |= PMU_F_ENABLED;
  781. barrier();
  782. err = lsctl(&cpuhw->lsctl);
  783. if (err) {
  784. cpuhw->flags &= ~PMU_F_ENABLED;
  785. pr_err("Loading sampling controls failed: op=%i err=%i\n",
  786. 1, err);
  787. return;
  788. }
  789. debug_sprintf_event(sfdbg, 6, "pmu_enable: es=%i cs=%i ed=%i cd=%i "
  790. "tear=%p dear=%p\n", cpuhw->lsctl.es, cpuhw->lsctl.cs,
  791. cpuhw->lsctl.ed, cpuhw->lsctl.cd,
  792. (void *) cpuhw->lsctl.tear, (void *) cpuhw->lsctl.dear);
  793. }
  794. static void cpumsf_pmu_disable(struct pmu *pmu)
  795. {
  796. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  797. struct hws_lsctl_request_block inactive;
  798. struct hws_qsi_info_block si;
  799. int err;
  800. if (!(cpuhw->flags & PMU_F_ENABLED))
  801. return;
  802. if (cpuhw->flags & PMU_F_ERR_MASK)
  803. return;
  804. /* Switch off sampling activation control */
  805. inactive = cpuhw->lsctl;
  806. inactive.cs = 0;
  807. inactive.cd = 0;
  808. err = lsctl(&inactive);
  809. if (err) {
  810. pr_err("Loading sampling controls failed: op=%i err=%i\n",
  811. 2, err);
  812. return;
  813. }
  814. /* Save state of TEAR and DEAR register contents */
  815. if (!qsi(&si)) {
  816. /* TEAR/DEAR values are valid only if the sampling facility is
  817. * enabled. Note that cpumsf_pmu_disable() might be called even
  818. * for a disabled sampling facility because cpumsf_pmu_enable()
  819. * controls the enable/disable state.
  820. */
  821. if (si.es) {
  822. cpuhw->lsctl.tear = si.tear;
  823. cpuhw->lsctl.dear = si.dear;
  824. }
  825. } else
  826. debug_sprintf_event(sfdbg, 3, "cpumsf_pmu_disable: "
  827. "qsi() failed with err=%i\n", err);
  828. cpuhw->flags &= ~PMU_F_ENABLED;
  829. }
  830. /* perf_exclude_event() - Filter event
  831. * @event: The perf event
  832. * @regs: pt_regs structure
  833. * @sde_regs: Sample-data-entry (sde) regs structure
  834. *
  835. * Filter perf events according to their exclude specification.
  836. *
  837. * Return non-zero if the event shall be excluded.
  838. */
  839. static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
  840. struct perf_sf_sde_regs *sde_regs)
  841. {
  842. if (event->attr.exclude_user && user_mode(regs))
  843. return 1;
  844. if (event->attr.exclude_kernel && !user_mode(regs))
  845. return 1;
  846. if (event->attr.exclude_guest && sde_regs->in_guest)
  847. return 1;
  848. if (event->attr.exclude_host && !sde_regs->in_guest)
  849. return 1;
  850. return 0;
  851. }
  852. /* perf_push_sample() - Push samples to perf
  853. * @event: The perf event
  854. * @sample: Hardware sample data
  855. *
  856. * Use the hardware sample data to create perf event sample. The sample
  857. * is the pushed to the event subsystem and the function checks for
  858. * possible event overflows. If an event overflow occurs, the PMU is
  859. * stopped.
  860. *
  861. * Return non-zero if an event overflow occurred.
  862. */
  863. static int perf_push_sample(struct perf_event *event, struct sf_raw_sample *sfr)
  864. {
  865. int overflow;
  866. struct pt_regs regs;
  867. struct perf_sf_sde_regs *sde_regs;
  868. struct perf_sample_data data;
  869. struct perf_raw_record raw;
  870. /* Setup perf sample */
  871. perf_sample_data_init(&data, 0, event->hw.last_period);
  872. raw.size = sfr->size;
  873. raw.data = sfr;
  874. data.raw = &raw;
  875. /* Setup pt_regs to look like an CPU-measurement external interrupt
  876. * using the Program Request Alert code. The regs.int_parm_long
  877. * field which is unused contains additional sample-data-entry related
  878. * indicators.
  879. */
  880. memset(&regs, 0, sizeof(regs));
  881. regs.int_code = 0x1407;
  882. regs.int_parm = CPU_MF_INT_SF_PRA;
  883. sde_regs = (struct perf_sf_sde_regs *) &regs.int_parm_long;
  884. regs.psw.addr = sfr->basic.ia;
  885. if (sfr->basic.T)
  886. regs.psw.mask |= PSW_MASK_DAT;
  887. if (sfr->basic.W)
  888. regs.psw.mask |= PSW_MASK_WAIT;
  889. if (sfr->basic.P)
  890. regs.psw.mask |= PSW_MASK_PSTATE;
  891. switch (sfr->basic.AS) {
  892. case 0x0:
  893. regs.psw.mask |= PSW_ASC_PRIMARY;
  894. break;
  895. case 0x1:
  896. regs.psw.mask |= PSW_ASC_ACCREG;
  897. break;
  898. case 0x2:
  899. regs.psw.mask |= PSW_ASC_SECONDARY;
  900. break;
  901. case 0x3:
  902. regs.psw.mask |= PSW_ASC_HOME;
  903. break;
  904. }
  905. /*
  906. * A non-zero guest program parameter indicates a guest
  907. * sample.
  908. * Note that some early samples might be misaccounted to
  909. * the host.
  910. */
  911. if (sfr->basic.gpp)
  912. sde_regs->in_guest = 1;
  913. overflow = 0;
  914. if (perf_exclude_event(event, &regs, sde_regs))
  915. goto out;
  916. if (perf_event_overflow(event, &data, &regs)) {
  917. overflow = 1;
  918. event->pmu->stop(event, 0);
  919. }
  920. perf_event_update_userpage(event);
  921. out:
  922. return overflow;
  923. }
  924. static void perf_event_count_update(struct perf_event *event, u64 count)
  925. {
  926. local64_add(count, &event->count);
  927. }
  928. static int sample_format_is_valid(struct hws_combined_entry *sample,
  929. unsigned int flags)
  930. {
  931. if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
  932. /* Only basic-sampling data entries with data-entry-format
  933. * version of 0x0001 can be processed.
  934. */
  935. if (sample->basic.def != 0x0001)
  936. return 0;
  937. if (flags & PERF_CPUM_SF_DIAG_MODE)
  938. /* The data-entry-format number of diagnostic-sampling data
  939. * entries can vary. Because diagnostic data is just passed
  940. * through, do only a sanity check on the DEF.
  941. */
  942. if (sample->diag.def < 0x8001)
  943. return 0;
  944. return 1;
  945. }
  946. static int sample_is_consistent(struct hws_combined_entry *sample,
  947. unsigned long flags)
  948. {
  949. /* This check applies only to basic-sampling data entries of potentially
  950. * combined-sampling data entries. Invalid entries cannot be processed
  951. * by the PMU and, thus, do not deliver an associated
  952. * diagnostic-sampling data entry.
  953. */
  954. if (unlikely(!(flags & PERF_CPUM_SF_BASIC_MODE)))
  955. return 0;
  956. /*
  957. * Samples are skipped, if they are invalid or for which the
  958. * instruction address is not predictable, i.e., the wait-state bit is
  959. * set.
  960. */
  961. if (sample->basic.I || sample->basic.W)
  962. return 0;
  963. return 1;
  964. }
  965. static void reset_sample_slot(struct hws_combined_entry *sample,
  966. unsigned long flags)
  967. {
  968. if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
  969. sample->basic.def = 0;
  970. if (flags & PERF_CPUM_SF_DIAG_MODE)
  971. sample->diag.def = 0;
  972. }
  973. static void sfr_store_sample(struct sf_raw_sample *sfr,
  974. struct hws_combined_entry *sample)
  975. {
  976. if (likely(sfr->format & PERF_CPUM_SF_BASIC_MODE))
  977. sfr->basic = sample->basic;
  978. if (sfr->format & PERF_CPUM_SF_DIAG_MODE)
  979. memcpy(&sfr->diag, &sample->diag, sfr->dsdes);
  980. }
  981. static void debug_sample_entry(struct hws_combined_entry *sample,
  982. struct hws_trailer_entry *te,
  983. unsigned long flags)
  984. {
  985. debug_sprintf_event(sfdbg, 4, "hw_collect_samples: Found unknown "
  986. "sampling data entry: te->f=%i basic.def=%04x (%p)"
  987. " diag.def=%04x (%p)\n", te->f,
  988. sample->basic.def, &sample->basic,
  989. (flags & PERF_CPUM_SF_DIAG_MODE)
  990. ? sample->diag.def : 0xFFFF,
  991. (flags & PERF_CPUM_SF_DIAG_MODE)
  992. ? &sample->diag : NULL);
  993. }
  994. /* hw_collect_samples() - Walk through a sample-data-block and collect samples
  995. * @event: The perf event
  996. * @sdbt: Sample-data-block table
  997. * @overflow: Event overflow counter
  998. *
  999. * Walks through a sample-data-block and collects sampling data entries that are
  1000. * then pushed to the perf event subsystem. Depending on the sampling function,
  1001. * there can be either basic-sampling or combined-sampling data entries. A
  1002. * combined-sampling data entry consists of a basic- and a diagnostic-sampling
  1003. * data entry. The sampling function is determined by the flags in the perf
  1004. * event hardware structure. The function always works with a combined-sampling
  1005. * data entry but ignores the the diagnostic portion if it is not available.
  1006. *
  1007. * Note that the implementation focuses on basic-sampling data entries and, if
  1008. * such an entry is not valid, the entire combined-sampling data entry is
  1009. * ignored.
  1010. *
  1011. * The overflow variables counts the number of samples that has been discarded
  1012. * due to a perf event overflow.
  1013. */
  1014. static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt,
  1015. unsigned long long *overflow)
  1016. {
  1017. unsigned long flags = SAMPL_FLAGS(&event->hw);
  1018. struct hws_combined_entry *sample;
  1019. struct hws_trailer_entry *te;
  1020. struct sf_raw_sample *sfr;
  1021. size_t sample_size;
  1022. /* Prepare and initialize raw sample data */
  1023. sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(&event->hw);
  1024. sfr->format = flags & PERF_CPUM_SF_MODE_MASK;
  1025. sample_size = event_sample_size(&event->hw);
  1026. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1027. sample = (struct hws_combined_entry *) *sdbt;
  1028. while ((unsigned long *) sample < (unsigned long *) te) {
  1029. /* Check for an empty sample */
  1030. if (!sample->basic.def)
  1031. break;
  1032. /* Update perf event period */
  1033. perf_event_count_update(event, SAMPL_RATE(&event->hw));
  1034. /* Check sampling data entry */
  1035. if (sample_format_is_valid(sample, flags)) {
  1036. /* If an event overflow occurred, the PMU is stopped to
  1037. * throttle event delivery. Remaining sample data is
  1038. * discarded.
  1039. */
  1040. if (!*overflow) {
  1041. if (sample_is_consistent(sample, flags)) {
  1042. /* Deliver sample data to perf */
  1043. sfr_store_sample(sfr, sample);
  1044. *overflow = perf_push_sample(event, sfr);
  1045. }
  1046. } else
  1047. /* Count discarded samples */
  1048. *overflow += 1;
  1049. } else {
  1050. debug_sample_entry(sample, te, flags);
  1051. /* Sample slot is not yet written or other record.
  1052. *
  1053. * This condition can occur if the buffer was reused
  1054. * from a combined basic- and diagnostic-sampling.
  1055. * If only basic-sampling is then active, entries are
  1056. * written into the larger diagnostic entries.
  1057. * This is typically the case for sample-data-blocks
  1058. * that are not full. Stop processing if the first
  1059. * invalid format was detected.
  1060. */
  1061. if (!te->f)
  1062. break;
  1063. }
  1064. /* Reset sample slot and advance to next sample */
  1065. reset_sample_slot(sample, flags);
  1066. sample += sample_size;
  1067. }
  1068. }
  1069. /* hw_perf_event_update() - Process sampling buffer
  1070. * @event: The perf event
  1071. * @flush_all: Flag to also flush partially filled sample-data-blocks
  1072. *
  1073. * Processes the sampling buffer and create perf event samples.
  1074. * The sampling buffer position are retrieved and saved in the TEAR_REG
  1075. * register of the specified perf event.
  1076. *
  1077. * Only full sample-data-blocks are processed. Specify the flash_all flag
  1078. * to also walk through partially filled sample-data-blocks. It is ignored
  1079. * if PERF_CPUM_SF_FULL_BLOCKS is set. The PERF_CPUM_SF_FULL_BLOCKS flag
  1080. * enforces the processing of full sample-data-blocks only (trailer entries
  1081. * with the block-full-indicator bit set).
  1082. */
  1083. static void hw_perf_event_update(struct perf_event *event, int flush_all)
  1084. {
  1085. struct hw_perf_event *hwc = &event->hw;
  1086. struct hws_trailer_entry *te;
  1087. unsigned long *sdbt;
  1088. unsigned long long event_overflow, sampl_overflow, num_sdb, te_flags;
  1089. int done;
  1090. if (flush_all && SDB_FULL_BLOCKS(hwc))
  1091. flush_all = 0;
  1092. sdbt = (unsigned long *) TEAR_REG(hwc);
  1093. done = event_overflow = sampl_overflow = num_sdb = 0;
  1094. while (!done) {
  1095. /* Get the trailer entry of the sample-data-block */
  1096. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1097. /* Leave loop if no more work to do (block full indicator) */
  1098. if (!te->f) {
  1099. done = 1;
  1100. if (!flush_all)
  1101. break;
  1102. }
  1103. /* Check the sample overflow count */
  1104. if (te->overflow)
  1105. /* Account sample overflows and, if a particular limit
  1106. * is reached, extend the sampling buffer.
  1107. * For details, see sfb_account_overflows().
  1108. */
  1109. sampl_overflow += te->overflow;
  1110. /* Timestamps are valid for full sample-data-blocks only */
  1111. debug_sprintf_event(sfdbg, 6, "hw_perf_event_update: sdbt=%p "
  1112. "overflow=%llu timestamp=0x%llx\n",
  1113. sdbt, te->overflow,
  1114. (te->f) ? trailer_timestamp(te) : 0ULL);
  1115. /* Collect all samples from a single sample-data-block and
  1116. * flag if an (perf) event overflow happened. If so, the PMU
  1117. * is stopped and remaining samples will be discarded.
  1118. */
  1119. hw_collect_samples(event, sdbt, &event_overflow);
  1120. num_sdb++;
  1121. /* Reset trailer (using compare-double-and-swap) */
  1122. do {
  1123. te_flags = te->flags & ~SDB_TE_BUFFER_FULL_MASK;
  1124. te_flags |= SDB_TE_ALERT_REQ_MASK;
  1125. } while (!cmpxchg_double(&te->flags, &te->overflow,
  1126. te->flags, te->overflow,
  1127. te_flags, 0ULL));
  1128. /* Advance to next sample-data-block */
  1129. sdbt++;
  1130. if (is_link_entry(sdbt))
  1131. sdbt = get_next_sdbt(sdbt);
  1132. /* Update event hardware registers */
  1133. TEAR_REG(hwc) = (unsigned long) sdbt;
  1134. /* Stop processing sample-data if all samples of the current
  1135. * sample-data-block were flushed even if it was not full.
  1136. */
  1137. if (flush_all && done)
  1138. break;
  1139. /* If an event overflow happened, discard samples by
  1140. * processing any remaining sample-data-blocks.
  1141. */
  1142. if (event_overflow)
  1143. flush_all = 1;
  1144. }
  1145. /* Account sample overflows in the event hardware structure */
  1146. if (sampl_overflow)
  1147. OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) +
  1148. sampl_overflow, 1 + num_sdb);
  1149. if (sampl_overflow || event_overflow)
  1150. debug_sprintf_event(sfdbg, 4, "hw_perf_event_update: "
  1151. "overflow stats: sample=%llu event=%llu\n",
  1152. sampl_overflow, event_overflow);
  1153. }
  1154. static void cpumsf_pmu_read(struct perf_event *event)
  1155. {
  1156. /* Nothing to do ... updates are interrupt-driven */
  1157. }
  1158. /* Activate sampling control.
  1159. * Next call of pmu_enable() starts sampling.
  1160. */
  1161. static void cpumsf_pmu_start(struct perf_event *event, int flags)
  1162. {
  1163. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1164. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  1165. return;
  1166. if (flags & PERF_EF_RELOAD)
  1167. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1168. perf_pmu_disable(event->pmu);
  1169. event->hw.state = 0;
  1170. cpuhw->lsctl.cs = 1;
  1171. if (SAMPL_DIAG_MODE(&event->hw))
  1172. cpuhw->lsctl.cd = 1;
  1173. perf_pmu_enable(event->pmu);
  1174. }
  1175. /* Deactivate sampling control.
  1176. * Next call of pmu_enable() stops sampling.
  1177. */
  1178. static void cpumsf_pmu_stop(struct perf_event *event, int flags)
  1179. {
  1180. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1181. if (event->hw.state & PERF_HES_STOPPED)
  1182. return;
  1183. perf_pmu_disable(event->pmu);
  1184. cpuhw->lsctl.cs = 0;
  1185. cpuhw->lsctl.cd = 0;
  1186. event->hw.state |= PERF_HES_STOPPED;
  1187. if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
  1188. hw_perf_event_update(event, 1);
  1189. event->hw.state |= PERF_HES_UPTODATE;
  1190. }
  1191. perf_pmu_enable(event->pmu);
  1192. }
  1193. static int cpumsf_pmu_add(struct perf_event *event, int flags)
  1194. {
  1195. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1196. int err;
  1197. if (cpuhw->flags & PMU_F_IN_USE)
  1198. return -EAGAIN;
  1199. if (!cpuhw->sfb.sdbt)
  1200. return -EINVAL;
  1201. err = 0;
  1202. perf_pmu_disable(event->pmu);
  1203. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  1204. /* Set up sampling controls. Always program the sampling register
  1205. * using the SDB-table start. Reset TEAR_REG event hardware register
  1206. * that is used by hw_perf_event_update() to store the sampling buffer
  1207. * position after samples have been flushed.
  1208. */
  1209. cpuhw->lsctl.s = 0;
  1210. cpuhw->lsctl.h = 1;
  1211. cpuhw->lsctl.tear = (unsigned long) cpuhw->sfb.sdbt;
  1212. cpuhw->lsctl.dear = *(unsigned long *) cpuhw->sfb.sdbt;
  1213. cpuhw->lsctl.interval = SAMPL_RATE(&event->hw);
  1214. hw_reset_registers(&event->hw, cpuhw->sfb.sdbt);
  1215. /* Ensure sampling functions are in the disabled state. If disabled,
  1216. * switch on sampling enable control. */
  1217. if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) {
  1218. err = -EAGAIN;
  1219. goto out;
  1220. }
  1221. cpuhw->lsctl.es = 1;
  1222. if (SAMPL_DIAG_MODE(&event->hw))
  1223. cpuhw->lsctl.ed = 1;
  1224. /* Set in_use flag and store event */
  1225. cpuhw->event = event;
  1226. cpuhw->flags |= PMU_F_IN_USE;
  1227. if (flags & PERF_EF_START)
  1228. cpumsf_pmu_start(event, PERF_EF_RELOAD);
  1229. out:
  1230. perf_event_update_userpage(event);
  1231. perf_pmu_enable(event->pmu);
  1232. return err;
  1233. }
  1234. static void cpumsf_pmu_del(struct perf_event *event, int flags)
  1235. {
  1236. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1237. perf_pmu_disable(event->pmu);
  1238. cpumsf_pmu_stop(event, PERF_EF_UPDATE);
  1239. cpuhw->lsctl.es = 0;
  1240. cpuhw->lsctl.ed = 0;
  1241. cpuhw->flags &= ~PMU_F_IN_USE;
  1242. cpuhw->event = NULL;
  1243. perf_event_update_userpage(event);
  1244. perf_pmu_enable(event->pmu);
  1245. }
  1246. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF);
  1247. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG);
  1248. static struct attribute *cpumsf_pmu_events_attr[] = {
  1249. CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC),
  1250. NULL,
  1251. NULL,
  1252. };
  1253. PMU_FORMAT_ATTR(event, "config:0-63");
  1254. static struct attribute *cpumsf_pmu_format_attr[] = {
  1255. &format_attr_event.attr,
  1256. NULL,
  1257. };
  1258. static struct attribute_group cpumsf_pmu_events_group = {
  1259. .name = "events",
  1260. .attrs = cpumsf_pmu_events_attr,
  1261. };
  1262. static struct attribute_group cpumsf_pmu_format_group = {
  1263. .name = "format",
  1264. .attrs = cpumsf_pmu_format_attr,
  1265. };
  1266. static const struct attribute_group *cpumsf_pmu_attr_groups[] = {
  1267. &cpumsf_pmu_events_group,
  1268. &cpumsf_pmu_format_group,
  1269. NULL,
  1270. };
  1271. static struct pmu cpumf_sampling = {
  1272. .pmu_enable = cpumsf_pmu_enable,
  1273. .pmu_disable = cpumsf_pmu_disable,
  1274. .event_init = cpumsf_pmu_event_init,
  1275. .add = cpumsf_pmu_add,
  1276. .del = cpumsf_pmu_del,
  1277. .start = cpumsf_pmu_start,
  1278. .stop = cpumsf_pmu_stop,
  1279. .read = cpumsf_pmu_read,
  1280. .attr_groups = cpumsf_pmu_attr_groups,
  1281. };
  1282. static void cpumf_measurement_alert(struct ext_code ext_code,
  1283. unsigned int alert, unsigned long unused)
  1284. {
  1285. struct cpu_hw_sf *cpuhw;
  1286. if (!(alert & CPU_MF_INT_SF_MASK))
  1287. return;
  1288. inc_irq_stat(IRQEXT_CMS);
  1289. cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1290. /* Measurement alerts are shared and might happen when the PMU
  1291. * is not reserved. Ignore these alerts in this case. */
  1292. if (!(cpuhw->flags & PMU_F_RESERVED))
  1293. return;
  1294. /* The processing below must take care of multiple alert events that
  1295. * might be indicated concurrently. */
  1296. /* Program alert request */
  1297. if (alert & CPU_MF_INT_SF_PRA) {
  1298. if (cpuhw->flags & PMU_F_IN_USE)
  1299. hw_perf_event_update(cpuhw->event, 0);
  1300. else
  1301. WARN_ON_ONCE(!(cpuhw->flags & PMU_F_IN_USE));
  1302. }
  1303. /* Report measurement alerts only for non-PRA codes */
  1304. if (alert != CPU_MF_INT_SF_PRA)
  1305. debug_sprintf_event(sfdbg, 6, "measurement alert: 0x%x\n", alert);
  1306. /* Sampling authorization change request */
  1307. if (alert & CPU_MF_INT_SF_SACA)
  1308. qsi(&cpuhw->qsi);
  1309. /* Loss of sample data due to high-priority machine activities */
  1310. if (alert & CPU_MF_INT_SF_LSDA) {
  1311. pr_err("Sample data was lost\n");
  1312. cpuhw->flags |= PMU_F_ERR_LSDA;
  1313. sf_disable();
  1314. }
  1315. /* Invalid sampling buffer entry */
  1316. if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) {
  1317. pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
  1318. alert);
  1319. cpuhw->flags |= PMU_F_ERR_IBE;
  1320. sf_disable();
  1321. }
  1322. }
  1323. static int cpumf_pmu_notifier(struct notifier_block *self,
  1324. unsigned long action, void *hcpu)
  1325. {
  1326. unsigned int cpu = (long) hcpu;
  1327. int flags;
  1328. /* Ignore the notification if no events are scheduled on the PMU.
  1329. * This might be racy...
  1330. */
  1331. if (!atomic_read(&num_events))
  1332. return NOTIFY_OK;
  1333. switch (action & ~CPU_TASKS_FROZEN) {
  1334. case CPU_ONLINE:
  1335. case CPU_ONLINE_FROZEN:
  1336. flags = PMC_INIT;
  1337. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  1338. break;
  1339. case CPU_DOWN_PREPARE:
  1340. flags = PMC_RELEASE;
  1341. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  1342. break;
  1343. default:
  1344. break;
  1345. }
  1346. return NOTIFY_OK;
  1347. }
  1348. static int param_get_sfb_size(char *buffer, const struct kernel_param *kp)
  1349. {
  1350. if (!cpum_sf_avail())
  1351. return -ENODEV;
  1352. return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1353. }
  1354. static int param_set_sfb_size(const char *val, const struct kernel_param *kp)
  1355. {
  1356. int rc;
  1357. unsigned long min, max;
  1358. if (!cpum_sf_avail())
  1359. return -ENODEV;
  1360. if (!val || !strlen(val))
  1361. return -EINVAL;
  1362. /* Valid parameter values: "min,max" or "max" */
  1363. min = CPUM_SF_MIN_SDB;
  1364. max = CPUM_SF_MAX_SDB;
  1365. if (strchr(val, ','))
  1366. rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL;
  1367. else
  1368. rc = kstrtoul(val, 10, &max);
  1369. if (min < 2 || min >= max || max > get_num_physpages())
  1370. rc = -EINVAL;
  1371. if (rc)
  1372. return rc;
  1373. sfb_set_limits(min, max);
  1374. pr_info("The sampling buffer limits have changed to: "
  1375. "min=%lu max=%lu (diag=x%lu)\n",
  1376. CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR);
  1377. return 0;
  1378. }
  1379. #define param_check_sfb_size(name, p) __param_check(name, p, void)
  1380. static const struct kernel_param_ops param_ops_sfb_size = {
  1381. .set = param_set_sfb_size,
  1382. .get = param_get_sfb_size,
  1383. };
  1384. #define RS_INIT_FAILURE_QSI 0x0001
  1385. #define RS_INIT_FAILURE_BSDES 0x0002
  1386. #define RS_INIT_FAILURE_ALRT 0x0003
  1387. #define RS_INIT_FAILURE_PERF 0x0004
  1388. static void __init pr_cpumsf_err(unsigned int reason)
  1389. {
  1390. pr_err("Sampling facility support for perf is not available: "
  1391. "reason=%04x\n", reason);
  1392. }
  1393. static int __init init_cpum_sampling_pmu(void)
  1394. {
  1395. struct hws_qsi_info_block si;
  1396. int err;
  1397. if (!cpum_sf_avail())
  1398. return -ENODEV;
  1399. memset(&si, 0, sizeof(si));
  1400. if (qsi(&si)) {
  1401. pr_cpumsf_err(RS_INIT_FAILURE_QSI);
  1402. return -ENODEV;
  1403. }
  1404. if (si.bsdes != sizeof(struct hws_basic_entry)) {
  1405. pr_cpumsf_err(RS_INIT_FAILURE_BSDES);
  1406. return -EINVAL;
  1407. }
  1408. if (si.ad) {
  1409. sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1410. cpumsf_pmu_events_attr[1] =
  1411. CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG);
  1412. }
  1413. sfdbg = debug_register(KMSG_COMPONENT, 2, 1, 80);
  1414. if (!sfdbg)
  1415. pr_err("Registering for s390dbf failed\n");
  1416. debug_register_view(sfdbg, &debug_sprintf_view);
  1417. err = register_external_irq(EXT_IRQ_MEASURE_ALERT,
  1418. cpumf_measurement_alert);
  1419. if (err) {
  1420. pr_cpumsf_err(RS_INIT_FAILURE_ALRT);
  1421. goto out;
  1422. }
  1423. err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW);
  1424. if (err) {
  1425. pr_cpumsf_err(RS_INIT_FAILURE_PERF);
  1426. unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
  1427. cpumf_measurement_alert);
  1428. goto out;
  1429. }
  1430. perf_cpu_notifier(cpumf_pmu_notifier);
  1431. out:
  1432. return err;
  1433. }
  1434. arch_initcall(init_cpum_sampling_pmu);
  1435. core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0640);