perf_cpum_sf.c 58 KB

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