perf_cpum_sf.c 59 KB

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