perf_cpum_sf.c 59 KB

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