perf_cpum_sf.c 60 KB

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