perf_cpum_sf.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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. rate = freq_to_sample_rate(&si, attr->sample_freq);
  656. rate = hw_limit_rate(&si, rate);
  657. attr->freq = 0;
  658. attr->sample_period = rate;
  659. } else {
  660. /* The min/max sampling rates specifies the valid range
  661. * of sample periods. If the specified sample period is
  662. * out of range, limit the period to the range boundary.
  663. */
  664. rate = hw_limit_rate(&si, hwc->sample_period);
  665. /* The perf core maintains a maximum sample rate that is
  666. * configurable through the sysctl interface. Ensure the
  667. * sampling rate does not exceed this value. This also helps
  668. * to avoid throttling when pushing samples with
  669. * perf_event_overflow().
  670. */
  671. if (sample_rate_to_freq(&si, rate) >
  672. sysctl_perf_event_sample_rate) {
  673. err = -EINVAL;
  674. debug_sprintf_event(sfdbg, 1, "Sampling rate exceeds maximum perf sample rate\n");
  675. goto out;
  676. }
  677. }
  678. SAMPL_RATE(hwc) = rate;
  679. hw_init_period(hwc, SAMPL_RATE(hwc));
  680. /* Initialize sample data overflow accounting */
  681. hwc->extra_reg.reg = REG_OVERFLOW;
  682. OVERFLOW_REG(hwc) = 0;
  683. /* Allocate the per-CPU sampling buffer using the CPU information
  684. * from the event. If the event is not pinned to a particular
  685. * CPU (event->cpu == -1; or cpuhw == NULL), allocate sampling
  686. * buffers for each online CPU.
  687. */
  688. if (cpuhw)
  689. /* Event is pinned to a particular CPU */
  690. err = allocate_buffers(cpuhw, hwc);
  691. else {
  692. /* Event is not pinned, allocate sampling buffer on
  693. * each online CPU
  694. */
  695. for_each_online_cpu(cpu) {
  696. cpuhw = &per_cpu(cpu_hw_sf, cpu);
  697. err = allocate_buffers(cpuhw, hwc);
  698. if (err)
  699. break;
  700. }
  701. }
  702. out:
  703. return err;
  704. }
  705. static int cpumsf_pmu_event_init(struct perf_event *event)
  706. {
  707. int err;
  708. /* No support for taken branch sampling */
  709. if (has_branch_stack(event))
  710. return -EOPNOTSUPP;
  711. switch (event->attr.type) {
  712. case PERF_TYPE_RAW:
  713. if ((event->attr.config != PERF_EVENT_CPUM_SF) &&
  714. (event->attr.config != PERF_EVENT_CPUM_SF_DIAG))
  715. return -ENOENT;
  716. break;
  717. case PERF_TYPE_HARDWARE:
  718. /* Support sampling of CPU cycles in addition to the
  719. * counter facility. However, the counter facility
  720. * is more precise and, hence, restrict this PMU to
  721. * sampling events only.
  722. */
  723. if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES)
  724. return -ENOENT;
  725. if (!is_sampling_event(event))
  726. return -ENOENT;
  727. break;
  728. default:
  729. return -ENOENT;
  730. }
  731. /* Check online status of the CPU to which the event is pinned */
  732. if (event->cpu >= nr_cpumask_bits ||
  733. (event->cpu >= 0 && !cpu_online(event->cpu)))
  734. return -ENODEV;
  735. /* Force reset of idle/hv excludes regardless of what the
  736. * user requested.
  737. */
  738. if (event->attr.exclude_hv)
  739. event->attr.exclude_hv = 0;
  740. if (event->attr.exclude_idle)
  741. event->attr.exclude_idle = 0;
  742. err = __hw_perf_event_init(event);
  743. if (unlikely(err))
  744. if (event->destroy)
  745. event->destroy(event);
  746. return err;
  747. }
  748. static void cpumsf_pmu_enable(struct pmu *pmu)
  749. {
  750. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  751. struct hw_perf_event *hwc;
  752. int err;
  753. if (cpuhw->flags & PMU_F_ENABLED)
  754. return;
  755. if (cpuhw->flags & PMU_F_ERR_MASK)
  756. return;
  757. /* Check whether to extent the sampling buffer.
  758. *
  759. * Two conditions trigger an increase of the sampling buffer for a
  760. * perf event:
  761. * 1. Postponed buffer allocations from the event initialization.
  762. * 2. Sampling overflows that contribute to pending allocations.
  763. *
  764. * Note that the extend_sampling_buffer() function disables the sampling
  765. * facility, but it can be fully re-enabled using sampling controls that
  766. * have been saved in cpumsf_pmu_disable().
  767. */
  768. if (cpuhw->event) {
  769. hwc = &cpuhw->event->hw;
  770. /* Account number of overflow-designated buffer extents */
  771. sfb_account_overflows(cpuhw, hwc);
  772. if (sfb_has_pending_allocs(&cpuhw->sfb, hwc))
  773. extend_sampling_buffer(&cpuhw->sfb, hwc);
  774. }
  775. /* (Re)enable the PMU and sampling facility */
  776. cpuhw->flags |= PMU_F_ENABLED;
  777. barrier();
  778. err = lsctl(&cpuhw->lsctl);
  779. if (err) {
  780. cpuhw->flags &= ~PMU_F_ENABLED;
  781. pr_err("Loading sampling controls failed: op=%i err=%i\n",
  782. 1, err);
  783. return;
  784. }
  785. debug_sprintf_event(sfdbg, 6, "pmu_enable: es=%i cs=%i ed=%i cd=%i "
  786. "tear=%p dear=%p\n", cpuhw->lsctl.es, cpuhw->lsctl.cs,
  787. cpuhw->lsctl.ed, cpuhw->lsctl.cd,
  788. (void *) cpuhw->lsctl.tear, (void *) cpuhw->lsctl.dear);
  789. }
  790. static void cpumsf_pmu_disable(struct pmu *pmu)
  791. {
  792. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  793. struct hws_lsctl_request_block inactive;
  794. struct hws_qsi_info_block si;
  795. int err;
  796. if (!(cpuhw->flags & PMU_F_ENABLED))
  797. return;
  798. if (cpuhw->flags & PMU_F_ERR_MASK)
  799. return;
  800. /* Switch off sampling activation control */
  801. inactive = cpuhw->lsctl;
  802. inactive.cs = 0;
  803. inactive.cd = 0;
  804. err = lsctl(&inactive);
  805. if (err) {
  806. pr_err("Loading sampling controls failed: op=%i err=%i\n",
  807. 2, err);
  808. return;
  809. }
  810. /* Save state of TEAR and DEAR register contents */
  811. if (!qsi(&si)) {
  812. /* TEAR/DEAR values are valid only if the sampling facility is
  813. * enabled. Note that cpumsf_pmu_disable() might be called even
  814. * for a disabled sampling facility because cpumsf_pmu_enable()
  815. * controls the enable/disable state.
  816. */
  817. if (si.es) {
  818. cpuhw->lsctl.tear = si.tear;
  819. cpuhw->lsctl.dear = si.dear;
  820. }
  821. } else
  822. debug_sprintf_event(sfdbg, 3, "cpumsf_pmu_disable: "
  823. "qsi() failed with err=%i\n", err);
  824. cpuhw->flags &= ~PMU_F_ENABLED;
  825. }
  826. /* perf_exclude_event() - Filter event
  827. * @event: The perf event
  828. * @regs: pt_regs structure
  829. * @sde_regs: Sample-data-entry (sde) regs structure
  830. *
  831. * Filter perf events according to their exclude specification.
  832. *
  833. * Return non-zero if the event shall be excluded.
  834. */
  835. static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
  836. struct perf_sf_sde_regs *sde_regs)
  837. {
  838. if (event->attr.exclude_user && user_mode(regs))
  839. return 1;
  840. if (event->attr.exclude_kernel && !user_mode(regs))
  841. return 1;
  842. if (event->attr.exclude_guest && sde_regs->in_guest)
  843. return 1;
  844. if (event->attr.exclude_host && !sde_regs->in_guest)
  845. return 1;
  846. return 0;
  847. }
  848. /* perf_push_sample() - Push samples to perf
  849. * @event: The perf event
  850. * @sample: Hardware sample data
  851. *
  852. * Use the hardware sample data to create perf event sample. The sample
  853. * is the pushed to the event subsystem and the function checks for
  854. * possible event overflows. If an event overflow occurs, the PMU is
  855. * stopped.
  856. *
  857. * Return non-zero if an event overflow occurred.
  858. */
  859. static int perf_push_sample(struct perf_event *event, struct sf_raw_sample *sfr)
  860. {
  861. int overflow;
  862. struct pt_regs regs;
  863. struct perf_sf_sde_regs *sde_regs;
  864. struct perf_sample_data data;
  865. struct perf_raw_record raw;
  866. /* Setup perf sample */
  867. perf_sample_data_init(&data, 0, event->hw.last_period);
  868. raw.size = sfr->size;
  869. raw.data = sfr;
  870. data.raw = &raw;
  871. /* Setup pt_regs to look like an CPU-measurement external interrupt
  872. * using the Program Request Alert code. The regs.int_parm_long
  873. * field which is unused contains additional sample-data-entry related
  874. * indicators.
  875. */
  876. memset(&regs, 0, sizeof(regs));
  877. regs.int_code = 0x1407;
  878. regs.int_parm = CPU_MF_INT_SF_PRA;
  879. sde_regs = (struct perf_sf_sde_regs *) &regs.int_parm_long;
  880. regs.psw.addr = sfr->basic.ia;
  881. if (sfr->basic.T)
  882. regs.psw.mask |= PSW_MASK_DAT;
  883. if (sfr->basic.W)
  884. regs.psw.mask |= PSW_MASK_WAIT;
  885. if (sfr->basic.P)
  886. regs.psw.mask |= PSW_MASK_PSTATE;
  887. switch (sfr->basic.AS) {
  888. case 0x0:
  889. regs.psw.mask |= PSW_ASC_PRIMARY;
  890. break;
  891. case 0x1:
  892. regs.psw.mask |= PSW_ASC_ACCREG;
  893. break;
  894. case 0x2:
  895. regs.psw.mask |= PSW_ASC_SECONDARY;
  896. break;
  897. case 0x3:
  898. regs.psw.mask |= PSW_ASC_HOME;
  899. break;
  900. }
  901. /*
  902. * A non-zero guest program parameter indicates a guest
  903. * sample.
  904. * Note that some early samples might be misaccounted to
  905. * the host.
  906. */
  907. if (sfr->basic.gpp)
  908. sde_regs->in_guest = 1;
  909. overflow = 0;
  910. if (perf_exclude_event(event, &regs, sde_regs))
  911. goto out;
  912. if (perf_event_overflow(event, &data, &regs)) {
  913. overflow = 1;
  914. event->pmu->stop(event, 0);
  915. }
  916. perf_event_update_userpage(event);
  917. out:
  918. return overflow;
  919. }
  920. static void perf_event_count_update(struct perf_event *event, u64 count)
  921. {
  922. local64_add(count, &event->count);
  923. }
  924. static int sample_format_is_valid(struct hws_combined_entry *sample,
  925. unsigned int flags)
  926. {
  927. if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
  928. /* Only basic-sampling data entries with data-entry-format
  929. * version of 0x0001 can be processed.
  930. */
  931. if (sample->basic.def != 0x0001)
  932. return 0;
  933. if (flags & PERF_CPUM_SF_DIAG_MODE)
  934. /* The data-entry-format number of diagnostic-sampling data
  935. * entries can vary. Because diagnostic data is just passed
  936. * through, do only a sanity check on the DEF.
  937. */
  938. if (sample->diag.def < 0x8001)
  939. return 0;
  940. return 1;
  941. }
  942. static int sample_is_consistent(struct hws_combined_entry *sample,
  943. unsigned long flags)
  944. {
  945. /* This check applies only to basic-sampling data entries of potentially
  946. * combined-sampling data entries. Invalid entries cannot be processed
  947. * by the PMU and, thus, do not deliver an associated
  948. * diagnostic-sampling data entry.
  949. */
  950. if (unlikely(!(flags & PERF_CPUM_SF_BASIC_MODE)))
  951. return 0;
  952. /*
  953. * Samples are skipped, if they are invalid or for which the
  954. * instruction address is not predictable, i.e., the wait-state bit is
  955. * set.
  956. */
  957. if (sample->basic.I || sample->basic.W)
  958. return 0;
  959. return 1;
  960. }
  961. static void reset_sample_slot(struct hws_combined_entry *sample,
  962. unsigned long flags)
  963. {
  964. if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
  965. sample->basic.def = 0;
  966. if (flags & PERF_CPUM_SF_DIAG_MODE)
  967. sample->diag.def = 0;
  968. }
  969. static void sfr_store_sample(struct sf_raw_sample *sfr,
  970. struct hws_combined_entry *sample)
  971. {
  972. if (likely(sfr->format & PERF_CPUM_SF_BASIC_MODE))
  973. sfr->basic = sample->basic;
  974. if (sfr->format & PERF_CPUM_SF_DIAG_MODE)
  975. memcpy(&sfr->diag, &sample->diag, sfr->dsdes);
  976. }
  977. static void debug_sample_entry(struct hws_combined_entry *sample,
  978. struct hws_trailer_entry *te,
  979. unsigned long flags)
  980. {
  981. debug_sprintf_event(sfdbg, 4, "hw_collect_samples: Found unknown "
  982. "sampling data entry: te->f=%i basic.def=%04x (%p)"
  983. " diag.def=%04x (%p)\n", te->f,
  984. sample->basic.def, &sample->basic,
  985. (flags & PERF_CPUM_SF_DIAG_MODE)
  986. ? sample->diag.def : 0xFFFF,
  987. (flags & PERF_CPUM_SF_DIAG_MODE)
  988. ? &sample->diag : NULL);
  989. }
  990. /* hw_collect_samples() - Walk through a sample-data-block and collect samples
  991. * @event: The perf event
  992. * @sdbt: Sample-data-block table
  993. * @overflow: Event overflow counter
  994. *
  995. * Walks through a sample-data-block and collects sampling data entries that are
  996. * then pushed to the perf event subsystem. Depending on the sampling function,
  997. * there can be either basic-sampling or combined-sampling data entries. A
  998. * combined-sampling data entry consists of a basic- and a diagnostic-sampling
  999. * data entry. The sampling function is determined by the flags in the perf
  1000. * event hardware structure. The function always works with a combined-sampling
  1001. * data entry but ignores the the diagnostic portion if it is not available.
  1002. *
  1003. * Note that the implementation focuses on basic-sampling data entries and, if
  1004. * such an entry is not valid, the entire combined-sampling data entry is
  1005. * ignored.
  1006. *
  1007. * The overflow variables counts the number of samples that has been discarded
  1008. * due to a perf event overflow.
  1009. */
  1010. static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt,
  1011. unsigned long long *overflow)
  1012. {
  1013. unsigned long flags = SAMPL_FLAGS(&event->hw);
  1014. struct hws_combined_entry *sample;
  1015. struct hws_trailer_entry *te;
  1016. struct sf_raw_sample *sfr;
  1017. size_t sample_size;
  1018. /* Prepare and initialize raw sample data */
  1019. sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(&event->hw);
  1020. sfr->format = flags & PERF_CPUM_SF_MODE_MASK;
  1021. sample_size = event_sample_size(&event->hw);
  1022. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1023. sample = (struct hws_combined_entry *) *sdbt;
  1024. while ((unsigned long *) sample < (unsigned long *) te) {
  1025. /* Check for an empty sample */
  1026. if (!sample->basic.def)
  1027. break;
  1028. /* Update perf event period */
  1029. perf_event_count_update(event, SAMPL_RATE(&event->hw));
  1030. /* Check sampling data entry */
  1031. if (sample_format_is_valid(sample, flags)) {
  1032. /* If an event overflow occurred, the PMU is stopped to
  1033. * throttle event delivery. Remaining sample data is
  1034. * discarded.
  1035. */
  1036. if (!*overflow) {
  1037. if (sample_is_consistent(sample, flags)) {
  1038. /* Deliver sample data to perf */
  1039. sfr_store_sample(sfr, sample);
  1040. *overflow = perf_push_sample(event, sfr);
  1041. }
  1042. } else
  1043. /* Count discarded samples */
  1044. *overflow += 1;
  1045. } else {
  1046. debug_sample_entry(sample, te, flags);
  1047. /* Sample slot is not yet written or other record.
  1048. *
  1049. * This condition can occur if the buffer was reused
  1050. * from a combined basic- and diagnostic-sampling.
  1051. * If only basic-sampling is then active, entries are
  1052. * written into the larger diagnostic entries.
  1053. * This is typically the case for sample-data-blocks
  1054. * that are not full. Stop processing if the first
  1055. * invalid format was detected.
  1056. */
  1057. if (!te->f)
  1058. break;
  1059. }
  1060. /* Reset sample slot and advance to next sample */
  1061. reset_sample_slot(sample, flags);
  1062. sample += sample_size;
  1063. }
  1064. }
  1065. /* hw_perf_event_update() - Process sampling buffer
  1066. * @event: The perf event
  1067. * @flush_all: Flag to also flush partially filled sample-data-blocks
  1068. *
  1069. * Processes the sampling buffer and create perf event samples.
  1070. * The sampling buffer position are retrieved and saved in the TEAR_REG
  1071. * register of the specified perf event.
  1072. *
  1073. * Only full sample-data-blocks are processed. Specify the flash_all flag
  1074. * to also walk through partially filled sample-data-blocks. It is ignored
  1075. * if PERF_CPUM_SF_FULL_BLOCKS is set. The PERF_CPUM_SF_FULL_BLOCKS flag
  1076. * enforces the processing of full sample-data-blocks only (trailer entries
  1077. * with the block-full-indicator bit set).
  1078. */
  1079. static void hw_perf_event_update(struct perf_event *event, int flush_all)
  1080. {
  1081. struct hw_perf_event *hwc = &event->hw;
  1082. struct hws_trailer_entry *te;
  1083. unsigned long *sdbt;
  1084. unsigned long long event_overflow, sampl_overflow, num_sdb, te_flags;
  1085. int done;
  1086. if (flush_all && SDB_FULL_BLOCKS(hwc))
  1087. flush_all = 0;
  1088. sdbt = (unsigned long *) TEAR_REG(hwc);
  1089. done = event_overflow = sampl_overflow = num_sdb = 0;
  1090. while (!done) {
  1091. /* Get the trailer entry of the sample-data-block */
  1092. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1093. /* Leave loop if no more work to do (block full indicator) */
  1094. if (!te->f) {
  1095. done = 1;
  1096. if (!flush_all)
  1097. break;
  1098. }
  1099. /* Check the sample overflow count */
  1100. if (te->overflow)
  1101. /* Account sample overflows and, if a particular limit
  1102. * is reached, extend the sampling buffer.
  1103. * For details, see sfb_account_overflows().
  1104. */
  1105. sampl_overflow += te->overflow;
  1106. /* Timestamps are valid for full sample-data-blocks only */
  1107. debug_sprintf_event(sfdbg, 6, "hw_perf_event_update: sdbt=%p "
  1108. "overflow=%llu timestamp=0x%llx\n",
  1109. sdbt, te->overflow,
  1110. (te->f) ? trailer_timestamp(te) : 0ULL);
  1111. /* Collect all samples from a single sample-data-block and
  1112. * flag if an (perf) event overflow happened. If so, the PMU
  1113. * is stopped and remaining samples will be discarded.
  1114. */
  1115. hw_collect_samples(event, sdbt, &event_overflow);
  1116. num_sdb++;
  1117. /* Reset trailer (using compare-double-and-swap) */
  1118. do {
  1119. te_flags = te->flags & ~SDB_TE_BUFFER_FULL_MASK;
  1120. te_flags |= SDB_TE_ALERT_REQ_MASK;
  1121. } while (!cmpxchg_double(&te->flags, &te->overflow,
  1122. te->flags, te->overflow,
  1123. te_flags, 0ULL));
  1124. /* Advance to next sample-data-block */
  1125. sdbt++;
  1126. if (is_link_entry(sdbt))
  1127. sdbt = get_next_sdbt(sdbt);
  1128. /* Update event hardware registers */
  1129. TEAR_REG(hwc) = (unsigned long) sdbt;
  1130. /* Stop processing sample-data if all samples of the current
  1131. * sample-data-block were flushed even if it was not full.
  1132. */
  1133. if (flush_all && done)
  1134. break;
  1135. /* If an event overflow happened, discard samples by
  1136. * processing any remaining sample-data-blocks.
  1137. */
  1138. if (event_overflow)
  1139. flush_all = 1;
  1140. }
  1141. /* Account sample overflows in the event hardware structure */
  1142. if (sampl_overflow)
  1143. OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) +
  1144. sampl_overflow, 1 + num_sdb);
  1145. if (sampl_overflow || event_overflow)
  1146. debug_sprintf_event(sfdbg, 4, "hw_perf_event_update: "
  1147. "overflow stats: sample=%llu event=%llu\n",
  1148. sampl_overflow, event_overflow);
  1149. }
  1150. static void cpumsf_pmu_read(struct perf_event *event)
  1151. {
  1152. /* Nothing to do ... updates are interrupt-driven */
  1153. }
  1154. /* Activate sampling control.
  1155. * Next call of pmu_enable() starts sampling.
  1156. */
  1157. static void cpumsf_pmu_start(struct perf_event *event, int flags)
  1158. {
  1159. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1160. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  1161. return;
  1162. if (flags & PERF_EF_RELOAD)
  1163. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1164. perf_pmu_disable(event->pmu);
  1165. event->hw.state = 0;
  1166. cpuhw->lsctl.cs = 1;
  1167. if (SAMPL_DIAG_MODE(&event->hw))
  1168. cpuhw->lsctl.cd = 1;
  1169. perf_pmu_enable(event->pmu);
  1170. }
  1171. /* Deactivate sampling control.
  1172. * Next call of pmu_enable() stops sampling.
  1173. */
  1174. static void cpumsf_pmu_stop(struct perf_event *event, int flags)
  1175. {
  1176. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1177. if (event->hw.state & PERF_HES_STOPPED)
  1178. return;
  1179. perf_pmu_disable(event->pmu);
  1180. cpuhw->lsctl.cs = 0;
  1181. cpuhw->lsctl.cd = 0;
  1182. event->hw.state |= PERF_HES_STOPPED;
  1183. if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
  1184. hw_perf_event_update(event, 1);
  1185. event->hw.state |= PERF_HES_UPTODATE;
  1186. }
  1187. perf_pmu_enable(event->pmu);
  1188. }
  1189. static int cpumsf_pmu_add(struct perf_event *event, int flags)
  1190. {
  1191. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1192. int err;
  1193. if (cpuhw->flags & PMU_F_IN_USE)
  1194. return -EAGAIN;
  1195. if (!cpuhw->sfb.sdbt)
  1196. return -EINVAL;
  1197. err = 0;
  1198. perf_pmu_disable(event->pmu);
  1199. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  1200. /* Set up sampling controls. Always program the sampling register
  1201. * using the SDB-table start. Reset TEAR_REG event hardware register
  1202. * that is used by hw_perf_event_update() to store the sampling buffer
  1203. * position after samples have been flushed.
  1204. */
  1205. cpuhw->lsctl.s = 0;
  1206. cpuhw->lsctl.h = 1;
  1207. cpuhw->lsctl.tear = (unsigned long) cpuhw->sfb.sdbt;
  1208. cpuhw->lsctl.dear = *(unsigned long *) cpuhw->sfb.sdbt;
  1209. cpuhw->lsctl.interval = SAMPL_RATE(&event->hw);
  1210. hw_reset_registers(&event->hw, cpuhw->sfb.sdbt);
  1211. /* Ensure sampling functions are in the disabled state. If disabled,
  1212. * switch on sampling enable control. */
  1213. if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) {
  1214. err = -EAGAIN;
  1215. goto out;
  1216. }
  1217. cpuhw->lsctl.es = 1;
  1218. if (SAMPL_DIAG_MODE(&event->hw))
  1219. cpuhw->lsctl.ed = 1;
  1220. /* Set in_use flag and store event */
  1221. cpuhw->event = event;
  1222. cpuhw->flags |= PMU_F_IN_USE;
  1223. if (flags & PERF_EF_START)
  1224. cpumsf_pmu_start(event, PERF_EF_RELOAD);
  1225. out:
  1226. perf_event_update_userpage(event);
  1227. perf_pmu_enable(event->pmu);
  1228. return err;
  1229. }
  1230. static void cpumsf_pmu_del(struct perf_event *event, int flags)
  1231. {
  1232. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1233. perf_pmu_disable(event->pmu);
  1234. cpumsf_pmu_stop(event, PERF_EF_UPDATE);
  1235. cpuhw->lsctl.es = 0;
  1236. cpuhw->lsctl.ed = 0;
  1237. cpuhw->flags &= ~PMU_F_IN_USE;
  1238. cpuhw->event = NULL;
  1239. perf_event_update_userpage(event);
  1240. perf_pmu_enable(event->pmu);
  1241. }
  1242. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF);
  1243. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG);
  1244. static struct attribute *cpumsf_pmu_events_attr[] = {
  1245. CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC),
  1246. NULL,
  1247. NULL,
  1248. };
  1249. PMU_FORMAT_ATTR(event, "config:0-63");
  1250. static struct attribute *cpumsf_pmu_format_attr[] = {
  1251. &format_attr_event.attr,
  1252. NULL,
  1253. };
  1254. static struct attribute_group cpumsf_pmu_events_group = {
  1255. .name = "events",
  1256. .attrs = cpumsf_pmu_events_attr,
  1257. };
  1258. static struct attribute_group cpumsf_pmu_format_group = {
  1259. .name = "format",
  1260. .attrs = cpumsf_pmu_format_attr,
  1261. };
  1262. static const struct attribute_group *cpumsf_pmu_attr_groups[] = {
  1263. &cpumsf_pmu_events_group,
  1264. &cpumsf_pmu_format_group,
  1265. NULL,
  1266. };
  1267. static struct pmu cpumf_sampling = {
  1268. .pmu_enable = cpumsf_pmu_enable,
  1269. .pmu_disable = cpumsf_pmu_disable,
  1270. .event_init = cpumsf_pmu_event_init,
  1271. .add = cpumsf_pmu_add,
  1272. .del = cpumsf_pmu_del,
  1273. .start = cpumsf_pmu_start,
  1274. .stop = cpumsf_pmu_stop,
  1275. .read = cpumsf_pmu_read,
  1276. .attr_groups = cpumsf_pmu_attr_groups,
  1277. };
  1278. static void cpumf_measurement_alert(struct ext_code ext_code,
  1279. unsigned int alert, unsigned long unused)
  1280. {
  1281. struct cpu_hw_sf *cpuhw;
  1282. if (!(alert & CPU_MF_INT_SF_MASK))
  1283. return;
  1284. inc_irq_stat(IRQEXT_CMS);
  1285. cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1286. /* Measurement alerts are shared and might happen when the PMU
  1287. * is not reserved. Ignore these alerts in this case. */
  1288. if (!(cpuhw->flags & PMU_F_RESERVED))
  1289. return;
  1290. /* The processing below must take care of multiple alert events that
  1291. * might be indicated concurrently. */
  1292. /* Program alert request */
  1293. if (alert & CPU_MF_INT_SF_PRA) {
  1294. if (cpuhw->flags & PMU_F_IN_USE)
  1295. hw_perf_event_update(cpuhw->event, 0);
  1296. else
  1297. WARN_ON_ONCE(!(cpuhw->flags & PMU_F_IN_USE));
  1298. }
  1299. /* Report measurement alerts only for non-PRA codes */
  1300. if (alert != CPU_MF_INT_SF_PRA)
  1301. debug_sprintf_event(sfdbg, 6, "measurement alert: 0x%x\n", alert);
  1302. /* Sampling authorization change request */
  1303. if (alert & CPU_MF_INT_SF_SACA)
  1304. qsi(&cpuhw->qsi);
  1305. /* Loss of sample data due to high-priority machine activities */
  1306. if (alert & CPU_MF_INT_SF_LSDA) {
  1307. pr_err("Sample data was lost\n");
  1308. cpuhw->flags |= PMU_F_ERR_LSDA;
  1309. sf_disable();
  1310. }
  1311. /* Invalid sampling buffer entry */
  1312. if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) {
  1313. pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
  1314. alert);
  1315. cpuhw->flags |= PMU_F_ERR_IBE;
  1316. sf_disable();
  1317. }
  1318. }
  1319. static int cpumf_pmu_notifier(struct notifier_block *self,
  1320. unsigned long action, void *hcpu)
  1321. {
  1322. unsigned int cpu = (long) hcpu;
  1323. int flags;
  1324. /* Ignore the notification if no events are scheduled on the PMU.
  1325. * This might be racy...
  1326. */
  1327. if (!atomic_read(&num_events))
  1328. return NOTIFY_OK;
  1329. switch (action & ~CPU_TASKS_FROZEN) {
  1330. case CPU_ONLINE:
  1331. case CPU_ONLINE_FROZEN:
  1332. flags = PMC_INIT;
  1333. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  1334. break;
  1335. case CPU_DOWN_PREPARE:
  1336. flags = PMC_RELEASE;
  1337. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  1338. break;
  1339. default:
  1340. break;
  1341. }
  1342. return NOTIFY_OK;
  1343. }
  1344. static int param_get_sfb_size(char *buffer, const struct kernel_param *kp)
  1345. {
  1346. if (!cpum_sf_avail())
  1347. return -ENODEV;
  1348. return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1349. }
  1350. static int param_set_sfb_size(const char *val, const struct kernel_param *kp)
  1351. {
  1352. int rc;
  1353. unsigned long min, max;
  1354. if (!cpum_sf_avail())
  1355. return -ENODEV;
  1356. if (!val || !strlen(val))
  1357. return -EINVAL;
  1358. /* Valid parameter values: "min,max" or "max" */
  1359. min = CPUM_SF_MIN_SDB;
  1360. max = CPUM_SF_MAX_SDB;
  1361. if (strchr(val, ','))
  1362. rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL;
  1363. else
  1364. rc = kstrtoul(val, 10, &max);
  1365. if (min < 2 || min >= max || max > get_num_physpages())
  1366. rc = -EINVAL;
  1367. if (rc)
  1368. return rc;
  1369. sfb_set_limits(min, max);
  1370. pr_info("The sampling buffer limits have changed to: "
  1371. "min=%lu max=%lu (diag=x%lu)\n",
  1372. CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR);
  1373. return 0;
  1374. }
  1375. #define param_check_sfb_size(name, p) __param_check(name, p, void)
  1376. static const struct kernel_param_ops param_ops_sfb_size = {
  1377. .set = param_set_sfb_size,
  1378. .get = param_get_sfb_size,
  1379. };
  1380. #define RS_INIT_FAILURE_QSI 0x0001
  1381. #define RS_INIT_FAILURE_BSDES 0x0002
  1382. #define RS_INIT_FAILURE_ALRT 0x0003
  1383. #define RS_INIT_FAILURE_PERF 0x0004
  1384. static void __init pr_cpumsf_err(unsigned int reason)
  1385. {
  1386. pr_err("Sampling facility support for perf is not available: "
  1387. "reason=%04x\n", reason);
  1388. }
  1389. static int __init init_cpum_sampling_pmu(void)
  1390. {
  1391. struct hws_qsi_info_block si;
  1392. int err;
  1393. if (!cpum_sf_avail())
  1394. return -ENODEV;
  1395. memset(&si, 0, sizeof(si));
  1396. if (qsi(&si)) {
  1397. pr_cpumsf_err(RS_INIT_FAILURE_QSI);
  1398. return -ENODEV;
  1399. }
  1400. if (si.bsdes != sizeof(struct hws_basic_entry)) {
  1401. pr_cpumsf_err(RS_INIT_FAILURE_BSDES);
  1402. return -EINVAL;
  1403. }
  1404. if (si.ad) {
  1405. sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1406. cpumsf_pmu_events_attr[1] =
  1407. CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG);
  1408. }
  1409. sfdbg = debug_register(KMSG_COMPONENT, 2, 1, 80);
  1410. if (!sfdbg)
  1411. pr_err("Registering for s390dbf failed\n");
  1412. debug_register_view(sfdbg, &debug_sprintf_view);
  1413. err = register_external_irq(EXT_IRQ_MEASURE_ALERT,
  1414. cpumf_measurement_alert);
  1415. if (err) {
  1416. pr_cpumsf_err(RS_INIT_FAILURE_ALRT);
  1417. goto out;
  1418. }
  1419. err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW);
  1420. if (err) {
  1421. pr_cpumsf_err(RS_INIT_FAILURE_PERF);
  1422. unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
  1423. cpumf_measurement_alert);
  1424. goto out;
  1425. }
  1426. perf_cpu_notifier(cpumf_pmu_notifier);
  1427. out:
  1428. return err;
  1429. }
  1430. arch_initcall(init_cpum_sampling_pmu);
  1431. core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0640);