perf_cpum_sf.c 46 KB

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