perf_cpum_sf.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. /*
  2. * Performance event support for the System z CPU-measurement Sampling Facility
  3. *
  4. * Copyright IBM Corp. 2013
  5. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (version 2 only)
  9. * as published by the Free Software Foundation.
  10. */
  11. #define KMSG_COMPONENT "cpum_sf"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/percpu.h>
  17. #include <linux/notifier.h>
  18. #include <linux/export.h>
  19. #include <linux/slab.h>
  20. #include <linux/mm.h>
  21. #include <linux/moduleparam.h>
  22. #include <asm/cpu_mf.h>
  23. #include <asm/irq.h>
  24. #include <asm/debug.h>
  25. #include <asm/timex.h>
  26. /* Minimum number of sample-data-block-tables:
  27. * At least one table is required for the sampling buffer structure.
  28. * A single table contains up to 511 pointers to sample-data-blocks.
  29. */
  30. #define CPUM_SF_MIN_SDBT 1
  31. /* Number of sample-data-blocks per sample-data-block-table (SDBT):
  32. * A table contains SDB pointers (8 bytes) and one table-link entry
  33. * that points to the origin of the next SDBT.
  34. */
  35. #define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8)
  36. /* Maximum page offset for an SDBT table-link entry:
  37. * If this page offset is reached, a table-link entry to the next SDBT
  38. * must be added.
  39. */
  40. #define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8)
  41. static inline int require_table_link(const void *sdbt)
  42. {
  43. return ((unsigned long) sdbt & ~PAGE_MASK) == CPUM_SF_SDBT_TL_OFFSET;
  44. }
  45. /* Minimum and maximum sampling buffer sizes:
  46. *
  47. * This number represents the maximum size of the sampling buffer taking
  48. * the number of sample-data-block-tables into account. Note that these
  49. * numbers apply to the basic-sampling function only.
  50. * The maximum number of SDBs is increased by CPUM_SF_SDB_DIAG_FACTOR if
  51. * the diagnostic-sampling function is active.
  52. *
  53. * Sampling buffer size Buffer characteristics
  54. * ---------------------------------------------------
  55. * 64KB == 16 pages (4KB per page)
  56. * 1 page for SDB-tables
  57. * 15 pages for SDBs
  58. *
  59. * 32MB == 8192 pages (4KB per page)
  60. * 16 pages for SDB-tables
  61. * 8176 pages for SDBs
  62. */
  63. static unsigned long __read_mostly CPUM_SF_MIN_SDB = 15;
  64. static unsigned long __read_mostly CPUM_SF_MAX_SDB = 8176;
  65. static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR = 1;
  66. struct sf_buffer {
  67. unsigned long *sdbt; /* Sample-data-block-table origin */
  68. /* buffer characteristics (required for buffer increments) */
  69. unsigned long num_sdb; /* Number of sample-data-blocks */
  70. unsigned long num_sdbt; /* Number of sample-data-block-tables */
  71. unsigned long *tail; /* last sample-data-block-table */
  72. };
  73. struct cpu_hw_sf {
  74. /* CPU-measurement sampling information block */
  75. struct hws_qsi_info_block qsi;
  76. /* CPU-measurement sampling control block */
  77. struct hws_lsctl_request_block lsctl;
  78. struct sf_buffer sfb; /* Sampling buffer */
  79. unsigned int flags; /* Status flags */
  80. struct perf_event *event; /* Scheduled perf event */
  81. };
  82. static DEFINE_PER_CPU(struct cpu_hw_sf, cpu_hw_sf);
  83. /* Debug feature */
  84. static debug_info_t *sfdbg;
  85. /*
  86. * sf_disable() - Switch off sampling facility
  87. */
  88. static int sf_disable(void)
  89. {
  90. struct hws_lsctl_request_block sreq;
  91. memset(&sreq, 0, sizeof(sreq));
  92. return lsctl(&sreq);
  93. }
  94. /*
  95. * sf_buffer_available() - Check for an allocated sampling buffer
  96. */
  97. static int sf_buffer_available(struct cpu_hw_sf *cpuhw)
  98. {
  99. return !!cpuhw->sfb.sdbt;
  100. }
  101. /*
  102. * deallocate sampling facility buffer
  103. */
  104. static void free_sampling_buffer(struct sf_buffer *sfb)
  105. {
  106. unsigned long *sdbt, *curr;
  107. if (!sfb->sdbt)
  108. return;
  109. sdbt = sfb->sdbt;
  110. curr = sdbt;
  111. /* Free the SDBT after all SDBs are processed... */
  112. while (1) {
  113. if (!*curr || !sdbt)
  114. break;
  115. /* Process table-link entries */
  116. if (is_link_entry(curr)) {
  117. curr = get_next_sdbt(curr);
  118. if (sdbt)
  119. free_page((unsigned long) sdbt);
  120. /* If the origin is reached, sampling buffer is freed */
  121. if (curr == sfb->sdbt)
  122. break;
  123. else
  124. sdbt = curr;
  125. } else {
  126. /* Process SDB pointer */
  127. if (*curr) {
  128. free_page(*curr);
  129. curr++;
  130. }
  131. }
  132. }
  133. debug_sprintf_event(sfdbg, 5,
  134. "free_sampling_buffer: freed sdbt=%p\n", sfb->sdbt);
  135. memset(sfb, 0, sizeof(*sfb));
  136. }
  137. static int alloc_sample_data_block(unsigned long *sdbt, gfp_t gfp_flags)
  138. {
  139. unsigned long sdb, *trailer;
  140. /* Allocate and initialize sample-data-block */
  141. sdb = get_zeroed_page(gfp_flags);
  142. if (!sdb)
  143. return -ENOMEM;
  144. trailer = trailer_entry_ptr(sdb);
  145. *trailer = SDB_TE_ALERT_REQ_MASK;
  146. /* Link SDB into the sample-data-block-table */
  147. *sdbt = sdb;
  148. return 0;
  149. }
  150. /*
  151. * realloc_sampling_buffer() - extend sampler memory
  152. *
  153. * Allocates new sample-data-blocks and adds them to the specified sampling
  154. * buffer memory.
  155. *
  156. * Important: This modifies the sampling buffer and must be called when the
  157. * sampling facility is disabled.
  158. *
  159. * Returns zero on success, non-zero otherwise.
  160. */
  161. static int realloc_sampling_buffer(struct sf_buffer *sfb,
  162. unsigned long num_sdb, gfp_t gfp_flags)
  163. {
  164. int i, rc;
  165. unsigned long *new, *tail;
  166. if (!sfb->sdbt || !sfb->tail)
  167. return -EINVAL;
  168. if (!is_link_entry(sfb->tail))
  169. return -EINVAL;
  170. /* Append to the existing sampling buffer, overwriting the table-link
  171. * register.
  172. * The tail variables always points to the "tail" (last and table-link)
  173. * entry in an SDB-table.
  174. */
  175. tail = sfb->tail;
  176. /* Do a sanity check whether the table-link entry points to
  177. * the sampling buffer origin.
  178. */
  179. if (sfb->sdbt != get_next_sdbt(tail)) {
  180. debug_sprintf_event(sfdbg, 3, "realloc_sampling_buffer: "
  181. "sampling buffer is not linked: origin=%p"
  182. "tail=%p\n",
  183. (void *) sfb->sdbt, (void *) tail);
  184. return -EINVAL;
  185. }
  186. /* Allocate remaining SDBs */
  187. rc = 0;
  188. for (i = 0; i < num_sdb; i++) {
  189. /* Allocate a new SDB-table if it is full. */
  190. if (require_table_link(tail)) {
  191. new = (unsigned long *) get_zeroed_page(gfp_flags);
  192. if (!new) {
  193. rc = -ENOMEM;
  194. break;
  195. }
  196. sfb->num_sdbt++;
  197. /* Link current page to tail of chain */
  198. *tail = (unsigned long)(void *) new + 1;
  199. tail = new;
  200. }
  201. /* Allocate a new sample-data-block.
  202. * If there is not enough memory, stop the realloc process
  203. * and simply use what was allocated. If this is a temporary
  204. * issue, a new realloc call (if required) might succeed.
  205. */
  206. rc = alloc_sample_data_block(tail, gfp_flags);
  207. if (rc)
  208. break;
  209. sfb->num_sdb++;
  210. tail++;
  211. }
  212. /* Link sampling buffer to its origin */
  213. *tail = (unsigned long) sfb->sdbt + 1;
  214. sfb->tail = tail;
  215. debug_sprintf_event(sfdbg, 4, "realloc_sampling_buffer: new buffer"
  216. " settings: sdbt=%lu sdb=%lu\n",
  217. sfb->num_sdbt, sfb->num_sdb);
  218. return rc;
  219. }
  220. /*
  221. * allocate_sampling_buffer() - allocate sampler memory
  222. *
  223. * Allocates and initializes a sampling buffer structure using the
  224. * specified number of sample-data-blocks (SDB). For each allocation,
  225. * a 4K page is used. The number of sample-data-block-tables (SDBT)
  226. * are calculated from SDBs.
  227. * Also set the ALERT_REQ mask in each SDBs trailer.
  228. *
  229. * Returns zero on success, non-zero otherwise.
  230. */
  231. static int alloc_sampling_buffer(struct sf_buffer *sfb, unsigned long num_sdb)
  232. {
  233. int rc;
  234. if (sfb->sdbt)
  235. return -EINVAL;
  236. /* Allocate the sample-data-block-table origin */
  237. sfb->sdbt = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  238. if (!sfb->sdbt)
  239. return -ENOMEM;
  240. sfb->num_sdb = 0;
  241. sfb->num_sdbt = 1;
  242. /* Link the table origin to point to itself to prepare for
  243. * realloc_sampling_buffer() invocation.
  244. */
  245. sfb->tail = sfb->sdbt;
  246. *sfb->tail = (unsigned long)(void *) sfb->sdbt + 1;
  247. /* Allocate requested number of sample-data-blocks */
  248. rc = realloc_sampling_buffer(sfb, num_sdb, GFP_KERNEL);
  249. if (rc) {
  250. free_sampling_buffer(sfb);
  251. debug_sprintf_event(sfdbg, 4, "alloc_sampling_buffer: "
  252. "realloc_sampling_buffer failed with rc=%i\n", rc);
  253. } else
  254. debug_sprintf_event(sfdbg, 4,
  255. "alloc_sampling_buffer: tear=%p dear=%p\n",
  256. sfb->sdbt, (void *) *sfb->sdbt);
  257. return rc;
  258. }
  259. static void sfb_set_limits(unsigned long min, unsigned long max)
  260. {
  261. struct hws_qsi_info_block si;
  262. CPUM_SF_MIN_SDB = min;
  263. CPUM_SF_MAX_SDB = max;
  264. memset(&si, 0, sizeof(si));
  265. if (!qsi(&si))
  266. CPUM_SF_SDB_DIAG_FACTOR = DIV_ROUND_UP(si.dsdes, si.bsdes);
  267. }
  268. static unsigned long sfb_max_limit(struct hw_perf_event *hwc)
  269. {
  270. return SAMPL_DIAG_MODE(hwc) ? CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR
  271. : CPUM_SF_MAX_SDB;
  272. }
  273. static unsigned long sfb_pending_allocs(struct sf_buffer *sfb,
  274. struct hw_perf_event *hwc)
  275. {
  276. if (!sfb->sdbt)
  277. return SFB_ALLOC_REG(hwc);
  278. if (SFB_ALLOC_REG(hwc) > sfb->num_sdb)
  279. return SFB_ALLOC_REG(hwc) - sfb->num_sdb;
  280. return 0;
  281. }
  282. static int sfb_has_pending_allocs(struct sf_buffer *sfb,
  283. struct hw_perf_event *hwc)
  284. {
  285. return sfb_pending_allocs(sfb, hwc) > 0;
  286. }
  287. static void sfb_account_allocs(unsigned long num, struct hw_perf_event *hwc)
  288. {
  289. /* Limit the number of SDBs to not exceed the maximum */
  290. num = min_t(unsigned long, num, sfb_max_limit(hwc) - SFB_ALLOC_REG(hwc));
  291. if (num)
  292. SFB_ALLOC_REG(hwc) += num;
  293. }
  294. static void sfb_init_allocs(unsigned long num, struct hw_perf_event *hwc)
  295. {
  296. SFB_ALLOC_REG(hwc) = 0;
  297. sfb_account_allocs(num, hwc);
  298. }
  299. static size_t event_sample_size(struct hw_perf_event *hwc)
  300. {
  301. struct sf_raw_sample *sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
  302. size_t sample_size;
  303. /* The sample size depends on the sampling function: The basic-sampling
  304. * function must be always enabled, diagnostic-sampling function is
  305. * optional.
  306. */
  307. sample_size = sfr->bsdes;
  308. if (SAMPL_DIAG_MODE(hwc))
  309. sample_size += sfr->dsdes;
  310. return sample_size;
  311. }
  312. static void deallocate_buffers(struct cpu_hw_sf *cpuhw)
  313. {
  314. if (cpuhw->sfb.sdbt)
  315. free_sampling_buffer(&cpuhw->sfb);
  316. }
  317. static int allocate_buffers(struct cpu_hw_sf *cpuhw, struct hw_perf_event *hwc)
  318. {
  319. unsigned long n_sdb, freq, factor;
  320. size_t sfr_size, sample_size;
  321. struct sf_raw_sample *sfr;
  322. /* Allocate raw sample buffer
  323. *
  324. * The raw sample buffer is used to temporarily store sampling data
  325. * entries for perf raw sample processing. The buffer size mainly
  326. * depends on the size of diagnostic-sampling data entries which is
  327. * machine-specific. The exact size calculation includes:
  328. * 1. The first 4 bytes of diagnostic-sampling data entries are
  329. * already reflected in the sf_raw_sample structure. Subtract
  330. * these bytes.
  331. * 2. The perf raw sample data must be 8-byte aligned (u64) and
  332. * perf's internal data size must be considered too. So add
  333. * an additional u32 for correct alignment and subtract before
  334. * allocating the buffer.
  335. * 3. Store the raw sample buffer pointer in the perf event
  336. * hardware structure.
  337. */
  338. sfr_size = ALIGN((sizeof(*sfr) - sizeof(sfr->diag) + cpuhw->qsi.dsdes) +
  339. sizeof(u32), sizeof(u64));
  340. sfr_size -= sizeof(u32);
  341. sfr = kzalloc(sfr_size, GFP_KERNEL);
  342. if (!sfr)
  343. return -ENOMEM;
  344. sfr->size = sfr_size;
  345. sfr->bsdes = cpuhw->qsi.bsdes;
  346. sfr->dsdes = cpuhw->qsi.dsdes;
  347. RAWSAMPLE_REG(hwc) = (unsigned long) sfr;
  348. /* Calculate sampling buffers using 4K pages
  349. *
  350. * 1. Determine the sample data size which depends on the used
  351. * sampling functions, for example, basic-sampling or
  352. * basic-sampling with diagnostic-sampling.
  353. *
  354. * 2. Use the sampling frequency as input. The sampling buffer is
  355. * designed for almost one second. This can be adjusted through
  356. * the "factor" variable.
  357. * In any case, alloc_sampling_buffer() sets the Alert Request
  358. * Control indicator to trigger a measurement-alert to harvest
  359. * sample-data-blocks (sdb).
  360. *
  361. * 3. Compute the number of sample-data-blocks and ensure a minimum
  362. * of CPUM_SF_MIN_SDB. Also ensure the upper limit does not
  363. * exceed a "calculated" maximum. The symbolic maximum is
  364. * designed for basic-sampling only and needs to be increased if
  365. * diagnostic-sampling is active.
  366. * See also the remarks for these symbolic constants.
  367. *
  368. * 4. Compute the number of sample-data-block-tables (SDBT) and
  369. * ensure a minimum of CPUM_SF_MIN_SDBT (one table can manage up
  370. * to 511 SDBs).
  371. */
  372. sample_size = event_sample_size(hwc);
  373. freq = sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc));
  374. factor = 1;
  375. n_sdb = DIV_ROUND_UP(freq, factor * ((PAGE_SIZE-64) / sample_size));
  376. if (n_sdb < CPUM_SF_MIN_SDB)
  377. n_sdb = CPUM_SF_MIN_SDB;
  378. /* If there is already a sampling buffer allocated, it is very likely
  379. * that the sampling facility is enabled too. If the event to be
  380. * initialized requires a greater sampling buffer, the allocation must
  381. * be postponed. Changing the sampling buffer requires the sampling
  382. * facility to be in the disabled state. So, account the number of
  383. * required SDBs and let cpumsf_pmu_enable() resize the buffer just
  384. * before the event is started.
  385. */
  386. sfb_init_allocs(n_sdb, hwc);
  387. if (sf_buffer_available(cpuhw))
  388. return 0;
  389. debug_sprintf_event(sfdbg, 3,
  390. "allocate_buffers: rate=%lu f=%lu sdb=%lu/%lu"
  391. " sample_size=%lu cpuhw=%p\n",
  392. SAMPL_RATE(hwc), freq, n_sdb, sfb_max_limit(hwc),
  393. sample_size, cpuhw);
  394. return alloc_sampling_buffer(&cpuhw->sfb,
  395. sfb_pending_allocs(&cpuhw->sfb, hwc));
  396. }
  397. static unsigned long min_percent(unsigned int percent, unsigned long base,
  398. unsigned long min)
  399. {
  400. return min_t(unsigned long, min, DIV_ROUND_UP(percent * base, 100));
  401. }
  402. static unsigned long compute_sfb_extent(unsigned long ratio, unsigned long base)
  403. {
  404. /* Use a percentage-based approach to extend the sampling facility
  405. * buffer. Accept up to 5% sample data loss.
  406. * Vary the extents between 1% to 5% of the current number of
  407. * sample-data-blocks.
  408. */
  409. if (ratio <= 5)
  410. return 0;
  411. if (ratio <= 25)
  412. return min_percent(1, base, 1);
  413. if (ratio <= 50)
  414. return min_percent(1, base, 1);
  415. if (ratio <= 75)
  416. return min_percent(2, base, 2);
  417. if (ratio <= 100)
  418. return min_percent(3, base, 3);
  419. if (ratio <= 250)
  420. return min_percent(4, base, 4);
  421. return min_percent(5, base, 8);
  422. }
  423. static void sfb_account_overflows(struct cpu_hw_sf *cpuhw,
  424. struct hw_perf_event *hwc)
  425. {
  426. unsigned long ratio, num;
  427. if (!OVERFLOW_REG(hwc))
  428. return;
  429. /* The sample_overflow contains the average number of sample data
  430. * that has been lost because sample-data-blocks were full.
  431. *
  432. * Calculate the total number of sample data entries that has been
  433. * discarded. Then calculate the ratio of lost samples to total samples
  434. * per second in percent.
  435. */
  436. ratio = DIV_ROUND_UP(100 * OVERFLOW_REG(hwc) * cpuhw->sfb.num_sdb,
  437. sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc)));
  438. /* Compute number of sample-data-blocks */
  439. num = compute_sfb_extent(ratio, cpuhw->sfb.num_sdb);
  440. if (num)
  441. sfb_account_allocs(num, hwc);
  442. debug_sprintf_event(sfdbg, 5, "sfb: overflow: overflow=%llu ratio=%lu"
  443. " num=%lu\n", OVERFLOW_REG(hwc), ratio, num);
  444. OVERFLOW_REG(hwc) = 0;
  445. }
  446. /* extend_sampling_buffer() - Extend sampling buffer
  447. * @sfb: Sampling buffer structure (for local CPU)
  448. * @hwc: Perf event hardware structure
  449. *
  450. * Use this function to extend the sampling buffer based on the overflow counter
  451. * and postponed allocation extents stored in the specified Perf event hardware.
  452. *
  453. * Important: This function disables the sampling facility in order to safely
  454. * change the sampling buffer structure. Do not call this function
  455. * when the PMU is active.
  456. */
  457. static void extend_sampling_buffer(struct sf_buffer *sfb,
  458. struct hw_perf_event *hwc)
  459. {
  460. unsigned long num, num_old;
  461. int rc;
  462. num = sfb_pending_allocs(sfb, hwc);
  463. if (!num)
  464. return;
  465. num_old = sfb->num_sdb;
  466. /* Disable the sampling facility to reset any states and also
  467. * clear pending measurement alerts.
  468. */
  469. sf_disable();
  470. /* Extend the sampling buffer.
  471. * This memory allocation typically happens in an atomic context when
  472. * called by perf. Because this is a reallocation, it is fine if the
  473. * new SDB-request cannot be satisfied immediately.
  474. */
  475. rc = realloc_sampling_buffer(sfb, num, GFP_ATOMIC);
  476. if (rc)
  477. debug_sprintf_event(sfdbg, 5, "sfb: extend: realloc "
  478. "failed with rc=%i\n", rc);
  479. if (sfb_has_pending_allocs(sfb, hwc))
  480. debug_sprintf_event(sfdbg, 5, "sfb: extend: "
  481. "req=%lu alloc=%lu remaining=%lu\n",
  482. num, sfb->num_sdb - num_old,
  483. sfb_pending_allocs(sfb, hwc));
  484. }
  485. /* Number of perf events counting hardware events */
  486. static atomic_t num_events;
  487. /* Used to avoid races in calling reserve/release_cpumf_hardware */
  488. static DEFINE_MUTEX(pmc_reserve_mutex);
  489. #define PMC_INIT 0
  490. #define PMC_RELEASE 1
  491. #define PMC_FAILURE 2
  492. static void setup_pmc_cpu(void *flags)
  493. {
  494. int err;
  495. struct cpu_hw_sf *cpusf = this_cpu_ptr(&cpu_hw_sf);
  496. err = 0;
  497. switch (*((int *) flags)) {
  498. case PMC_INIT:
  499. memset(cpusf, 0, sizeof(*cpusf));
  500. err = qsi(&cpusf->qsi);
  501. if (err)
  502. break;
  503. cpusf->flags |= PMU_F_RESERVED;
  504. err = sf_disable();
  505. if (err)
  506. pr_err("Switching off the sampling facility failed "
  507. "with rc=%i\n", err);
  508. debug_sprintf_event(sfdbg, 5,
  509. "setup_pmc_cpu: initialized: cpuhw=%p\n", cpusf);
  510. break;
  511. case PMC_RELEASE:
  512. cpusf->flags &= ~PMU_F_RESERVED;
  513. err = sf_disable();
  514. if (err) {
  515. pr_err("Switching off the sampling facility failed "
  516. "with rc=%i\n", err);
  517. } else
  518. deallocate_buffers(cpusf);
  519. debug_sprintf_event(sfdbg, 5,
  520. "setup_pmc_cpu: released: cpuhw=%p\n", cpusf);
  521. break;
  522. }
  523. if (err)
  524. *((int *) flags) |= PMC_FAILURE;
  525. }
  526. static void release_pmc_hardware(void)
  527. {
  528. int flags = PMC_RELEASE;
  529. irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  530. on_each_cpu(setup_pmc_cpu, &flags, 1);
  531. perf_release_sampling();
  532. }
  533. static int reserve_pmc_hardware(void)
  534. {
  535. int flags = PMC_INIT;
  536. int err;
  537. err = perf_reserve_sampling();
  538. if (err)
  539. return err;
  540. on_each_cpu(setup_pmc_cpu, &flags, 1);
  541. if (flags & PMC_FAILURE) {
  542. release_pmc_hardware();
  543. return -ENODEV;
  544. }
  545. irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  546. return 0;
  547. }
  548. static void hw_perf_event_destroy(struct perf_event *event)
  549. {
  550. /* Free raw sample buffer */
  551. if (RAWSAMPLE_REG(&event->hw))
  552. kfree((void *) RAWSAMPLE_REG(&event->hw));
  553. /* Release PMC if this is the last perf event */
  554. if (!atomic_add_unless(&num_events, -1, 1)) {
  555. mutex_lock(&pmc_reserve_mutex);
  556. if (atomic_dec_return(&num_events) == 0)
  557. release_pmc_hardware();
  558. mutex_unlock(&pmc_reserve_mutex);
  559. }
  560. }
  561. static void hw_init_period(struct hw_perf_event *hwc, u64 period)
  562. {
  563. hwc->sample_period = period;
  564. hwc->last_period = hwc->sample_period;
  565. local64_set(&hwc->period_left, hwc->sample_period);
  566. }
  567. static void hw_reset_registers(struct hw_perf_event *hwc,
  568. unsigned long *sdbt_origin)
  569. {
  570. struct sf_raw_sample *sfr;
  571. /* (Re)set to first sample-data-block-table */
  572. TEAR_REG(hwc) = (unsigned long) sdbt_origin;
  573. /* (Re)set raw sampling buffer register */
  574. sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(hwc);
  575. memset(&sfr->basic, 0, sizeof(sfr->basic));
  576. memset(&sfr->diag, 0, sfr->dsdes);
  577. }
  578. static unsigned long hw_limit_rate(const struct hws_qsi_info_block *si,
  579. unsigned long rate)
  580. {
  581. return clamp_t(unsigned long, rate,
  582. si->min_sampl_rate, si->max_sampl_rate);
  583. }
  584. static int __hw_perf_event_init(struct perf_event *event)
  585. {
  586. struct cpu_hw_sf *cpuhw;
  587. struct hws_qsi_info_block si;
  588. struct perf_event_attr *attr = &event->attr;
  589. struct hw_perf_event *hwc = &event->hw;
  590. unsigned long rate;
  591. int cpu, err;
  592. /* Reserve CPU-measurement sampling facility */
  593. err = 0;
  594. if (!atomic_inc_not_zero(&num_events)) {
  595. mutex_lock(&pmc_reserve_mutex);
  596. if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
  597. err = -EBUSY;
  598. else
  599. atomic_inc(&num_events);
  600. mutex_unlock(&pmc_reserve_mutex);
  601. }
  602. event->destroy = hw_perf_event_destroy;
  603. if (err)
  604. goto out;
  605. /* Access per-CPU sampling information (query sampling info) */
  606. /*
  607. * The event->cpu value can be -1 to count on every CPU, for example,
  608. * when attaching to a task. If this is specified, use the query
  609. * sampling info from the current CPU, otherwise use event->cpu to
  610. * retrieve the per-CPU information.
  611. * Later, cpuhw indicates whether to allocate sampling buffers for a
  612. * particular CPU (cpuhw!=NULL) or each online CPU (cpuw==NULL).
  613. */
  614. memset(&si, 0, sizeof(si));
  615. cpuhw = NULL;
  616. if (event->cpu == -1)
  617. qsi(&si);
  618. else {
  619. /* Event is pinned to a particular CPU, retrieve the per-CPU
  620. * sampling structure for accessing the CPU-specific QSI.
  621. */
  622. cpuhw = &per_cpu(cpu_hw_sf, event->cpu);
  623. si = cpuhw->qsi;
  624. }
  625. /* Check sampling facility authorization and, if not authorized,
  626. * fall back to other PMUs. It is safe to check any CPU because
  627. * the authorization is identical for all configured CPUs.
  628. */
  629. if (!si.as) {
  630. err = -ENOENT;
  631. goto out;
  632. }
  633. /* Always enable basic sampling */
  634. SAMPL_FLAGS(hwc) = PERF_CPUM_SF_BASIC_MODE;
  635. /* Check if diagnostic sampling is requested. Deny if the required
  636. * sampling authorization is missing.
  637. */
  638. if (attr->config == PERF_EVENT_CPUM_SF_DIAG) {
  639. if (!si.ad) {
  640. err = -EPERM;
  641. goto out;
  642. }
  643. SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_DIAG_MODE;
  644. }
  645. /* Check and set other sampling flags */
  646. if (attr->config1 & PERF_CPUM_SF_FULL_BLOCKS)
  647. SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FULL_BLOCKS;
  648. /* The sampling information (si) contains information about the
  649. * min/max sampling intervals and the CPU speed. So calculate the
  650. * correct sampling interval and avoid the whole period adjust
  651. * feedback loop.
  652. */
  653. rate = 0;
  654. if (attr->freq) {
  655. rate = freq_to_sample_rate(&si, attr->sample_freq);
  656. rate = hw_limit_rate(&si, rate);
  657. attr->freq = 0;
  658. attr->sample_period = rate;
  659. } else {
  660. /* The min/max sampling rates specifies the valid range
  661. * of sample periods. If the specified sample period is
  662. * out of range, limit the period to the range boundary.
  663. */
  664. rate = hw_limit_rate(&si, hwc->sample_period);
  665. /* The perf core maintains a maximum sample rate that is
  666. * configurable through the sysctl interface. Ensure the
  667. * sampling rate does not exceed this value. This also helps
  668. * to avoid throttling when pushing samples with
  669. * perf_event_overflow().
  670. */
  671. if (sample_rate_to_freq(&si, rate) >
  672. sysctl_perf_event_sample_rate) {
  673. err = -EINVAL;
  674. debug_sprintf_event(sfdbg, 1, "Sampling rate exceeds maximum perf sample rate\n");
  675. goto out;
  676. }
  677. }
  678. SAMPL_RATE(hwc) = rate;
  679. hw_init_period(hwc, SAMPL_RATE(hwc));
  680. /* Initialize sample data overflow accounting */
  681. hwc->extra_reg.reg = REG_OVERFLOW;
  682. OVERFLOW_REG(hwc) = 0;
  683. /* Allocate the per-CPU sampling buffer using the CPU information
  684. * from the event. If the event is not pinned to a particular
  685. * CPU (event->cpu == -1; or cpuhw == NULL), allocate sampling
  686. * buffers for each online CPU.
  687. */
  688. if (cpuhw)
  689. /* Event is pinned to a particular CPU */
  690. err = allocate_buffers(cpuhw, hwc);
  691. else {
  692. /* Event is not pinned, allocate sampling buffer on
  693. * each online CPU
  694. */
  695. for_each_online_cpu(cpu) {
  696. cpuhw = &per_cpu(cpu_hw_sf, cpu);
  697. err = allocate_buffers(cpuhw, hwc);
  698. if (err)
  699. break;
  700. }
  701. }
  702. out:
  703. return err;
  704. }
  705. static int cpumsf_pmu_event_init(struct perf_event *event)
  706. {
  707. int err;
  708. /* No support for taken branch sampling */
  709. if (has_branch_stack(event))
  710. return -EOPNOTSUPP;
  711. switch (event->attr.type) {
  712. case PERF_TYPE_RAW:
  713. if ((event->attr.config != PERF_EVENT_CPUM_SF) &&
  714. (event->attr.config != PERF_EVENT_CPUM_SF_DIAG))
  715. return -ENOENT;
  716. break;
  717. case PERF_TYPE_HARDWARE:
  718. /* Support sampling of CPU cycles in addition to the
  719. * counter facility. However, the counter facility
  720. * is more precise and, hence, restrict this PMU to
  721. * sampling events only.
  722. */
  723. if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES)
  724. return -ENOENT;
  725. if (!is_sampling_event(event))
  726. return -ENOENT;
  727. break;
  728. default:
  729. return -ENOENT;
  730. }
  731. /* Check online status of the CPU to which the event is pinned */
  732. if (event->cpu >= nr_cpumask_bits ||
  733. (event->cpu >= 0 && !cpu_online(event->cpu)))
  734. return -ENODEV;
  735. /* Force reset of idle/hv excludes regardless of what the
  736. * user requested.
  737. */
  738. if (event->attr.exclude_hv)
  739. event->attr.exclude_hv = 0;
  740. if (event->attr.exclude_idle)
  741. event->attr.exclude_idle = 0;
  742. err = __hw_perf_event_init(event);
  743. if (unlikely(err))
  744. if (event->destroy)
  745. event->destroy(event);
  746. return err;
  747. }
  748. static void cpumsf_pmu_enable(struct pmu *pmu)
  749. {
  750. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  751. struct hw_perf_event *hwc;
  752. int err;
  753. if (cpuhw->flags & PMU_F_ENABLED)
  754. return;
  755. if (cpuhw->flags & PMU_F_ERR_MASK)
  756. return;
  757. /* Check whether to extent the sampling buffer.
  758. *
  759. * Two conditions trigger an increase of the sampling buffer for a
  760. * perf event:
  761. * 1. Postponed buffer allocations from the event initialization.
  762. * 2. Sampling overflows that contribute to pending allocations.
  763. *
  764. * Note that the extend_sampling_buffer() function disables the sampling
  765. * facility, but it can be fully re-enabled using sampling controls that
  766. * have been saved in cpumsf_pmu_disable().
  767. */
  768. if (cpuhw->event) {
  769. hwc = &cpuhw->event->hw;
  770. /* Account number of overflow-designated buffer extents */
  771. sfb_account_overflows(cpuhw, hwc);
  772. if (sfb_has_pending_allocs(&cpuhw->sfb, hwc))
  773. extend_sampling_buffer(&cpuhw->sfb, hwc);
  774. }
  775. /* (Re)enable the PMU and sampling facility */
  776. cpuhw->flags |= PMU_F_ENABLED;
  777. barrier();
  778. err = lsctl(&cpuhw->lsctl);
  779. if (err) {
  780. cpuhw->flags &= ~PMU_F_ENABLED;
  781. pr_err("Loading sampling controls failed: op=%i err=%i\n",
  782. 1, err);
  783. return;
  784. }
  785. debug_sprintf_event(sfdbg, 6, "pmu_enable: es=%i cs=%i ed=%i cd=%i "
  786. "tear=%p dear=%p\n", cpuhw->lsctl.es, cpuhw->lsctl.cs,
  787. cpuhw->lsctl.ed, cpuhw->lsctl.cd,
  788. (void *) cpuhw->lsctl.tear, (void *) cpuhw->lsctl.dear);
  789. }
  790. static void cpumsf_pmu_disable(struct pmu *pmu)
  791. {
  792. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  793. struct hws_lsctl_request_block inactive;
  794. struct hws_qsi_info_block si;
  795. int err;
  796. if (!(cpuhw->flags & PMU_F_ENABLED))
  797. return;
  798. if (cpuhw->flags & PMU_F_ERR_MASK)
  799. return;
  800. /* Switch off sampling activation control */
  801. inactive = cpuhw->lsctl;
  802. inactive.cs = 0;
  803. inactive.cd = 0;
  804. err = lsctl(&inactive);
  805. if (err) {
  806. pr_err("Loading sampling controls failed: op=%i err=%i\n",
  807. 2, err);
  808. return;
  809. }
  810. /* Save state of TEAR and DEAR register contents */
  811. if (!qsi(&si)) {
  812. /* TEAR/DEAR values are valid only if the sampling facility is
  813. * enabled. Note that cpumsf_pmu_disable() might be called even
  814. * for a disabled sampling facility because cpumsf_pmu_enable()
  815. * controls the enable/disable state.
  816. */
  817. if (si.es) {
  818. cpuhw->lsctl.tear = si.tear;
  819. cpuhw->lsctl.dear = si.dear;
  820. }
  821. } else
  822. debug_sprintf_event(sfdbg, 3, "cpumsf_pmu_disable: "
  823. "qsi() failed with err=%i\n", err);
  824. cpuhw->flags &= ~PMU_F_ENABLED;
  825. }
  826. /* perf_exclude_event() - Filter event
  827. * @event: The perf event
  828. * @regs: pt_regs structure
  829. * @sde_regs: Sample-data-entry (sde) regs structure
  830. *
  831. * Filter perf events according to their exclude specification.
  832. *
  833. * Return non-zero if the event shall be excluded.
  834. */
  835. static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
  836. struct perf_sf_sde_regs *sde_regs)
  837. {
  838. if (event->attr.exclude_user && user_mode(regs))
  839. return 1;
  840. if (event->attr.exclude_kernel && !user_mode(regs))
  841. return 1;
  842. if (event->attr.exclude_guest && sde_regs->in_guest)
  843. return 1;
  844. if (event->attr.exclude_host && !sde_regs->in_guest)
  845. return 1;
  846. return 0;
  847. }
  848. /* perf_push_sample() - Push samples to perf
  849. * @event: The perf event
  850. * @sample: Hardware sample data
  851. *
  852. * Use the hardware sample data to create perf event sample. The sample
  853. * is the pushed to the event subsystem and the function checks for
  854. * possible event overflows. If an event overflow occurs, the PMU is
  855. * stopped.
  856. *
  857. * Return non-zero if an event overflow occurred.
  858. */
  859. static int perf_push_sample(struct perf_event *event, struct sf_raw_sample *sfr)
  860. {
  861. int overflow;
  862. struct pt_regs regs;
  863. struct perf_sf_sde_regs *sde_regs;
  864. struct perf_sample_data data;
  865. struct perf_raw_record raw;
  866. /* Setup perf sample */
  867. perf_sample_data_init(&data, 0, event->hw.last_period);
  868. raw.size = sfr->size;
  869. raw.data = sfr;
  870. data.raw = &raw;
  871. /* Setup pt_regs to look like an CPU-measurement external interrupt
  872. * using the Program Request Alert code. The regs.int_parm_long
  873. * field which is unused contains additional sample-data-entry related
  874. * indicators.
  875. */
  876. memset(&regs, 0, sizeof(regs));
  877. regs.int_code = 0x1407;
  878. regs.int_parm = CPU_MF_INT_SF_PRA;
  879. sde_regs = (struct perf_sf_sde_regs *) &regs.int_parm_long;
  880. regs.psw.addr = sfr->basic.ia;
  881. if (sfr->basic.T)
  882. regs.psw.mask |= PSW_MASK_DAT;
  883. if (sfr->basic.W)
  884. regs.psw.mask |= PSW_MASK_WAIT;
  885. if (sfr->basic.P)
  886. regs.psw.mask |= PSW_MASK_PSTATE;
  887. switch (sfr->basic.AS) {
  888. case 0x0:
  889. regs.psw.mask |= PSW_ASC_PRIMARY;
  890. break;
  891. case 0x1:
  892. regs.psw.mask |= PSW_ASC_ACCREG;
  893. break;
  894. case 0x2:
  895. regs.psw.mask |= PSW_ASC_SECONDARY;
  896. break;
  897. case 0x3:
  898. regs.psw.mask |= PSW_ASC_HOME;
  899. break;
  900. }
  901. /* The host-program-parameter (hpp) contains the sie control
  902. * block that is set by sie64a() in entry64.S. Check if hpp
  903. * refers to a valid control block and set sde_regs flags
  904. * accordingly. This would allow to use hpp values for other
  905. * purposes too.
  906. * For now, simply use a non-zero value as guest indicator.
  907. */
  908. if (sfr->basic.hpp)
  909. sde_regs->in_guest = 1;
  910. overflow = 0;
  911. if (perf_exclude_event(event, &regs, sde_regs))
  912. goto out;
  913. if (perf_event_overflow(event, &data, &regs)) {
  914. overflow = 1;
  915. event->pmu->stop(event, 0);
  916. }
  917. perf_event_update_userpage(event);
  918. out:
  919. return overflow;
  920. }
  921. static void perf_event_count_update(struct perf_event *event, u64 count)
  922. {
  923. local64_add(count, &event->count);
  924. }
  925. static int sample_format_is_valid(struct hws_combined_entry *sample,
  926. unsigned int flags)
  927. {
  928. if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
  929. /* Only basic-sampling data entries with data-entry-format
  930. * version of 0x0001 can be processed.
  931. */
  932. if (sample->basic.def != 0x0001)
  933. return 0;
  934. if (flags & PERF_CPUM_SF_DIAG_MODE)
  935. /* The data-entry-format number of diagnostic-sampling data
  936. * entries can vary. Because diagnostic data is just passed
  937. * through, do only a sanity check on the DEF.
  938. */
  939. if (sample->diag.def < 0x8001)
  940. return 0;
  941. return 1;
  942. }
  943. static int sample_is_consistent(struct hws_combined_entry *sample,
  944. unsigned long flags)
  945. {
  946. /* This check applies only to basic-sampling data entries of potentially
  947. * combined-sampling data entries. Invalid entries cannot be processed
  948. * by the PMU and, thus, do not deliver an associated
  949. * diagnostic-sampling data entry.
  950. */
  951. if (unlikely(!(flags & PERF_CPUM_SF_BASIC_MODE)))
  952. return 0;
  953. /*
  954. * Samples are skipped, if they are invalid or for which the
  955. * instruction address is not predictable, i.e., the wait-state bit is
  956. * set.
  957. */
  958. if (sample->basic.I || sample->basic.W)
  959. return 0;
  960. return 1;
  961. }
  962. static void reset_sample_slot(struct hws_combined_entry *sample,
  963. unsigned long flags)
  964. {
  965. if (likely(flags & PERF_CPUM_SF_BASIC_MODE))
  966. sample->basic.def = 0;
  967. if (flags & PERF_CPUM_SF_DIAG_MODE)
  968. sample->diag.def = 0;
  969. }
  970. static void sfr_store_sample(struct sf_raw_sample *sfr,
  971. struct hws_combined_entry *sample)
  972. {
  973. if (likely(sfr->format & PERF_CPUM_SF_BASIC_MODE))
  974. sfr->basic = sample->basic;
  975. if (sfr->format & PERF_CPUM_SF_DIAG_MODE)
  976. memcpy(&sfr->diag, &sample->diag, sfr->dsdes);
  977. }
  978. static void debug_sample_entry(struct hws_combined_entry *sample,
  979. struct hws_trailer_entry *te,
  980. unsigned long flags)
  981. {
  982. debug_sprintf_event(sfdbg, 4, "hw_collect_samples: Found unknown "
  983. "sampling data entry: te->f=%i basic.def=%04x (%p)"
  984. " diag.def=%04x (%p)\n", te->f,
  985. sample->basic.def, &sample->basic,
  986. (flags & PERF_CPUM_SF_DIAG_MODE)
  987. ? sample->diag.def : 0xFFFF,
  988. (flags & PERF_CPUM_SF_DIAG_MODE)
  989. ? &sample->diag : NULL);
  990. }
  991. /* hw_collect_samples() - Walk through a sample-data-block and collect samples
  992. * @event: The perf event
  993. * @sdbt: Sample-data-block table
  994. * @overflow: Event overflow counter
  995. *
  996. * Walks through a sample-data-block and collects sampling data entries that are
  997. * then pushed to the perf event subsystem. Depending on the sampling function,
  998. * there can be either basic-sampling or combined-sampling data entries. A
  999. * combined-sampling data entry consists of a basic- and a diagnostic-sampling
  1000. * data entry. The sampling function is determined by the flags in the perf
  1001. * event hardware structure. The function always works with a combined-sampling
  1002. * data entry but ignores the the diagnostic portion if it is not available.
  1003. *
  1004. * Note that the implementation focuses on basic-sampling data entries and, if
  1005. * such an entry is not valid, the entire combined-sampling data entry is
  1006. * ignored.
  1007. *
  1008. * The overflow variables counts the number of samples that has been discarded
  1009. * due to a perf event overflow.
  1010. */
  1011. static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt,
  1012. unsigned long long *overflow)
  1013. {
  1014. unsigned long flags = SAMPL_FLAGS(&event->hw);
  1015. struct hws_combined_entry *sample;
  1016. struct hws_trailer_entry *te;
  1017. struct sf_raw_sample *sfr;
  1018. size_t sample_size;
  1019. /* Prepare and initialize raw sample data */
  1020. sfr = (struct sf_raw_sample *) RAWSAMPLE_REG(&event->hw);
  1021. sfr->format = flags & PERF_CPUM_SF_MODE_MASK;
  1022. sample_size = event_sample_size(&event->hw);
  1023. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1024. sample = (struct hws_combined_entry *) *sdbt;
  1025. while ((unsigned long *) sample < (unsigned long *) te) {
  1026. /* Check for an empty sample */
  1027. if (!sample->basic.def)
  1028. break;
  1029. /* Update perf event period */
  1030. perf_event_count_update(event, SAMPL_RATE(&event->hw));
  1031. /* Check sampling data entry */
  1032. if (sample_format_is_valid(sample, flags)) {
  1033. /* If an event overflow occurred, the PMU is stopped to
  1034. * throttle event delivery. Remaining sample data is
  1035. * discarded.
  1036. */
  1037. if (!*overflow) {
  1038. if (sample_is_consistent(sample, flags)) {
  1039. /* Deliver sample data to perf */
  1040. sfr_store_sample(sfr, sample);
  1041. *overflow = perf_push_sample(event, sfr);
  1042. }
  1043. } else
  1044. /* Count discarded samples */
  1045. *overflow += 1;
  1046. } else {
  1047. debug_sample_entry(sample, te, flags);
  1048. /* Sample slot is not yet written or other record.
  1049. *
  1050. * This condition can occur if the buffer was reused
  1051. * from a combined basic- and diagnostic-sampling.
  1052. * If only basic-sampling is then active, entries are
  1053. * written into the larger diagnostic entries.
  1054. * This is typically the case for sample-data-blocks
  1055. * that are not full. Stop processing if the first
  1056. * invalid format was detected.
  1057. */
  1058. if (!te->f)
  1059. break;
  1060. }
  1061. /* Reset sample slot and advance to next sample */
  1062. reset_sample_slot(sample, flags);
  1063. sample += sample_size;
  1064. }
  1065. }
  1066. /* hw_perf_event_update() - Process sampling buffer
  1067. * @event: The perf event
  1068. * @flush_all: Flag to also flush partially filled sample-data-blocks
  1069. *
  1070. * Processes the sampling buffer and create perf event samples.
  1071. * The sampling buffer position are retrieved and saved in the TEAR_REG
  1072. * register of the specified perf event.
  1073. *
  1074. * Only full sample-data-blocks are processed. Specify the flash_all flag
  1075. * to also walk through partially filled sample-data-blocks. It is ignored
  1076. * if PERF_CPUM_SF_FULL_BLOCKS is set. The PERF_CPUM_SF_FULL_BLOCKS flag
  1077. * enforces the processing of full sample-data-blocks only (trailer entries
  1078. * with the block-full-indicator bit set).
  1079. */
  1080. static void hw_perf_event_update(struct perf_event *event, int flush_all)
  1081. {
  1082. struct hw_perf_event *hwc = &event->hw;
  1083. struct hws_trailer_entry *te;
  1084. unsigned long *sdbt;
  1085. unsigned long long event_overflow, sampl_overflow, num_sdb, te_flags;
  1086. int done;
  1087. if (flush_all && SDB_FULL_BLOCKS(hwc))
  1088. flush_all = 0;
  1089. sdbt = (unsigned long *) TEAR_REG(hwc);
  1090. done = event_overflow = sampl_overflow = num_sdb = 0;
  1091. while (!done) {
  1092. /* Get the trailer entry of the sample-data-block */
  1093. te = (struct hws_trailer_entry *) trailer_entry_ptr(*sdbt);
  1094. /* Leave loop if no more work to do (block full indicator) */
  1095. if (!te->f) {
  1096. done = 1;
  1097. if (!flush_all)
  1098. break;
  1099. }
  1100. /* Check the sample overflow count */
  1101. if (te->overflow)
  1102. /* Account sample overflows and, if a particular limit
  1103. * is reached, extend the sampling buffer.
  1104. * For details, see sfb_account_overflows().
  1105. */
  1106. sampl_overflow += te->overflow;
  1107. /* Timestamps are valid for full sample-data-blocks only */
  1108. debug_sprintf_event(sfdbg, 6, "hw_perf_event_update: sdbt=%p "
  1109. "overflow=%llu timestamp=0x%llx\n",
  1110. sdbt, te->overflow,
  1111. (te->f) ? trailer_timestamp(te) : 0ULL);
  1112. /* Collect all samples from a single sample-data-block and
  1113. * flag if an (perf) event overflow happened. If so, the PMU
  1114. * is stopped and remaining samples will be discarded.
  1115. */
  1116. hw_collect_samples(event, sdbt, &event_overflow);
  1117. num_sdb++;
  1118. /* Reset trailer (using compare-double-and-swap) */
  1119. do {
  1120. te_flags = te->flags & ~SDB_TE_BUFFER_FULL_MASK;
  1121. te_flags |= SDB_TE_ALERT_REQ_MASK;
  1122. } while (!cmpxchg_double(&te->flags, &te->overflow,
  1123. te->flags, te->overflow,
  1124. te_flags, 0ULL));
  1125. /* Advance to next sample-data-block */
  1126. sdbt++;
  1127. if (is_link_entry(sdbt))
  1128. sdbt = get_next_sdbt(sdbt);
  1129. /* Update event hardware registers */
  1130. TEAR_REG(hwc) = (unsigned long) sdbt;
  1131. /* Stop processing sample-data if all samples of the current
  1132. * sample-data-block were flushed even if it was not full.
  1133. */
  1134. if (flush_all && done)
  1135. break;
  1136. /* If an event overflow happened, discard samples by
  1137. * processing any remaining sample-data-blocks.
  1138. */
  1139. if (event_overflow)
  1140. flush_all = 1;
  1141. }
  1142. /* Account sample overflows in the event hardware structure */
  1143. if (sampl_overflow)
  1144. OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) +
  1145. sampl_overflow, 1 + num_sdb);
  1146. if (sampl_overflow || event_overflow)
  1147. debug_sprintf_event(sfdbg, 4, "hw_perf_event_update: "
  1148. "overflow stats: sample=%llu event=%llu\n",
  1149. sampl_overflow, event_overflow);
  1150. }
  1151. static void cpumsf_pmu_read(struct perf_event *event)
  1152. {
  1153. /* Nothing to do ... updates are interrupt-driven */
  1154. }
  1155. /* Activate sampling control.
  1156. * Next call of pmu_enable() starts sampling.
  1157. */
  1158. static void cpumsf_pmu_start(struct perf_event *event, int flags)
  1159. {
  1160. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1161. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  1162. return;
  1163. if (flags & PERF_EF_RELOAD)
  1164. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1165. perf_pmu_disable(event->pmu);
  1166. event->hw.state = 0;
  1167. cpuhw->lsctl.cs = 1;
  1168. if (SAMPL_DIAG_MODE(&event->hw))
  1169. cpuhw->lsctl.cd = 1;
  1170. perf_pmu_enable(event->pmu);
  1171. }
  1172. /* Deactivate sampling control.
  1173. * Next call of pmu_enable() stops sampling.
  1174. */
  1175. static void cpumsf_pmu_stop(struct perf_event *event, int flags)
  1176. {
  1177. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1178. if (event->hw.state & PERF_HES_STOPPED)
  1179. return;
  1180. perf_pmu_disable(event->pmu);
  1181. cpuhw->lsctl.cs = 0;
  1182. cpuhw->lsctl.cd = 0;
  1183. event->hw.state |= PERF_HES_STOPPED;
  1184. if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
  1185. hw_perf_event_update(event, 1);
  1186. event->hw.state |= PERF_HES_UPTODATE;
  1187. }
  1188. perf_pmu_enable(event->pmu);
  1189. }
  1190. static int cpumsf_pmu_add(struct perf_event *event, int flags)
  1191. {
  1192. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1193. int err;
  1194. if (cpuhw->flags & PMU_F_IN_USE)
  1195. return -EAGAIN;
  1196. if (!cpuhw->sfb.sdbt)
  1197. return -EINVAL;
  1198. err = 0;
  1199. perf_pmu_disable(event->pmu);
  1200. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  1201. /* Set up sampling controls. Always program the sampling register
  1202. * using the SDB-table start. Reset TEAR_REG event hardware register
  1203. * that is used by hw_perf_event_update() to store the sampling buffer
  1204. * position after samples have been flushed.
  1205. */
  1206. cpuhw->lsctl.s = 0;
  1207. cpuhw->lsctl.h = 1;
  1208. cpuhw->lsctl.tear = (unsigned long) cpuhw->sfb.sdbt;
  1209. cpuhw->lsctl.dear = *(unsigned long *) cpuhw->sfb.sdbt;
  1210. cpuhw->lsctl.interval = SAMPL_RATE(&event->hw);
  1211. hw_reset_registers(&event->hw, cpuhw->sfb.sdbt);
  1212. /* Ensure sampling functions are in the disabled state. If disabled,
  1213. * switch on sampling enable control. */
  1214. if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) {
  1215. err = -EAGAIN;
  1216. goto out;
  1217. }
  1218. cpuhw->lsctl.es = 1;
  1219. if (SAMPL_DIAG_MODE(&event->hw))
  1220. cpuhw->lsctl.ed = 1;
  1221. /* Set in_use flag and store event */
  1222. cpuhw->event = event;
  1223. cpuhw->flags |= PMU_F_IN_USE;
  1224. if (flags & PERF_EF_START)
  1225. cpumsf_pmu_start(event, PERF_EF_RELOAD);
  1226. out:
  1227. perf_event_update_userpage(event);
  1228. perf_pmu_enable(event->pmu);
  1229. return err;
  1230. }
  1231. static void cpumsf_pmu_del(struct perf_event *event, int flags)
  1232. {
  1233. struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1234. perf_pmu_disable(event->pmu);
  1235. cpumsf_pmu_stop(event, PERF_EF_UPDATE);
  1236. cpuhw->lsctl.es = 0;
  1237. cpuhw->lsctl.ed = 0;
  1238. cpuhw->flags &= ~PMU_F_IN_USE;
  1239. cpuhw->event = NULL;
  1240. perf_event_update_userpage(event);
  1241. perf_pmu_enable(event->pmu);
  1242. }
  1243. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF);
  1244. CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG);
  1245. static struct attribute *cpumsf_pmu_events_attr[] = {
  1246. CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC),
  1247. NULL,
  1248. NULL,
  1249. };
  1250. PMU_FORMAT_ATTR(event, "config:0-63");
  1251. static struct attribute *cpumsf_pmu_format_attr[] = {
  1252. &format_attr_event.attr,
  1253. NULL,
  1254. };
  1255. static struct attribute_group cpumsf_pmu_events_group = {
  1256. .name = "events",
  1257. .attrs = cpumsf_pmu_events_attr,
  1258. };
  1259. static struct attribute_group cpumsf_pmu_format_group = {
  1260. .name = "format",
  1261. .attrs = cpumsf_pmu_format_attr,
  1262. };
  1263. static const struct attribute_group *cpumsf_pmu_attr_groups[] = {
  1264. &cpumsf_pmu_events_group,
  1265. &cpumsf_pmu_format_group,
  1266. NULL,
  1267. };
  1268. static struct pmu cpumf_sampling = {
  1269. .pmu_enable = cpumsf_pmu_enable,
  1270. .pmu_disable = cpumsf_pmu_disable,
  1271. .event_init = cpumsf_pmu_event_init,
  1272. .add = cpumsf_pmu_add,
  1273. .del = cpumsf_pmu_del,
  1274. .start = cpumsf_pmu_start,
  1275. .stop = cpumsf_pmu_stop,
  1276. .read = cpumsf_pmu_read,
  1277. .attr_groups = cpumsf_pmu_attr_groups,
  1278. };
  1279. static void cpumf_measurement_alert(struct ext_code ext_code,
  1280. unsigned int alert, unsigned long unused)
  1281. {
  1282. struct cpu_hw_sf *cpuhw;
  1283. if (!(alert & CPU_MF_INT_SF_MASK))
  1284. return;
  1285. inc_irq_stat(IRQEXT_CMS);
  1286. cpuhw = this_cpu_ptr(&cpu_hw_sf);
  1287. /* Measurement alerts are shared and might happen when the PMU
  1288. * is not reserved. Ignore these alerts in this case. */
  1289. if (!(cpuhw->flags & PMU_F_RESERVED))
  1290. return;
  1291. /* The processing below must take care of multiple alert events that
  1292. * might be indicated concurrently. */
  1293. /* Program alert request */
  1294. if (alert & CPU_MF_INT_SF_PRA) {
  1295. if (cpuhw->flags & PMU_F_IN_USE)
  1296. hw_perf_event_update(cpuhw->event, 0);
  1297. else
  1298. WARN_ON_ONCE(!(cpuhw->flags & PMU_F_IN_USE));
  1299. }
  1300. /* Report measurement alerts only for non-PRA codes */
  1301. if (alert != CPU_MF_INT_SF_PRA)
  1302. debug_sprintf_event(sfdbg, 6, "measurement alert: 0x%x\n", alert);
  1303. /* Sampling authorization change request */
  1304. if (alert & CPU_MF_INT_SF_SACA)
  1305. qsi(&cpuhw->qsi);
  1306. /* Loss of sample data due to high-priority machine activities */
  1307. if (alert & CPU_MF_INT_SF_LSDA) {
  1308. pr_err("Sample data was lost\n");
  1309. cpuhw->flags |= PMU_F_ERR_LSDA;
  1310. sf_disable();
  1311. }
  1312. /* Invalid sampling buffer entry */
  1313. if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) {
  1314. pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
  1315. alert);
  1316. cpuhw->flags |= PMU_F_ERR_IBE;
  1317. sf_disable();
  1318. }
  1319. }
  1320. static int cpumf_pmu_notifier(struct notifier_block *self,
  1321. unsigned long action, void *hcpu)
  1322. {
  1323. unsigned int cpu = (long) hcpu;
  1324. int flags;
  1325. /* Ignore the notification if no events are scheduled on the PMU.
  1326. * This might be racy...
  1327. */
  1328. if (!atomic_read(&num_events))
  1329. return NOTIFY_OK;
  1330. switch (action & ~CPU_TASKS_FROZEN) {
  1331. case CPU_ONLINE:
  1332. case CPU_ONLINE_FROZEN:
  1333. flags = PMC_INIT;
  1334. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  1335. break;
  1336. case CPU_DOWN_PREPARE:
  1337. flags = PMC_RELEASE;
  1338. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  1339. break;
  1340. default:
  1341. break;
  1342. }
  1343. return NOTIFY_OK;
  1344. }
  1345. static int param_get_sfb_size(char *buffer, const struct kernel_param *kp)
  1346. {
  1347. if (!cpum_sf_avail())
  1348. return -ENODEV;
  1349. return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1350. }
  1351. static int param_set_sfb_size(const char *val, const struct kernel_param *kp)
  1352. {
  1353. int rc;
  1354. unsigned long min, max;
  1355. if (!cpum_sf_avail())
  1356. return -ENODEV;
  1357. if (!val || !strlen(val))
  1358. return -EINVAL;
  1359. /* Valid parameter values: "min,max" or "max" */
  1360. min = CPUM_SF_MIN_SDB;
  1361. max = CPUM_SF_MAX_SDB;
  1362. if (strchr(val, ','))
  1363. rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL;
  1364. else
  1365. rc = kstrtoul(val, 10, &max);
  1366. if (min < 2 || min >= max || max > get_num_physpages())
  1367. rc = -EINVAL;
  1368. if (rc)
  1369. return rc;
  1370. sfb_set_limits(min, max);
  1371. pr_info("The sampling buffer limits have changed to: "
  1372. "min=%lu max=%lu (diag=x%lu)\n",
  1373. CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR);
  1374. return 0;
  1375. }
  1376. #define param_check_sfb_size(name, p) __param_check(name, p, void)
  1377. static const struct kernel_param_ops param_ops_sfb_size = {
  1378. .set = param_set_sfb_size,
  1379. .get = param_get_sfb_size,
  1380. };
  1381. #define RS_INIT_FAILURE_QSI 0x0001
  1382. #define RS_INIT_FAILURE_BSDES 0x0002
  1383. #define RS_INIT_FAILURE_ALRT 0x0003
  1384. #define RS_INIT_FAILURE_PERF 0x0004
  1385. static void __init pr_cpumsf_err(unsigned int reason)
  1386. {
  1387. pr_err("Sampling facility support for perf is not available: "
  1388. "reason=%04x\n", reason);
  1389. }
  1390. static int __init init_cpum_sampling_pmu(void)
  1391. {
  1392. struct hws_qsi_info_block si;
  1393. int err;
  1394. if (!cpum_sf_avail())
  1395. return -ENODEV;
  1396. memset(&si, 0, sizeof(si));
  1397. if (qsi(&si)) {
  1398. pr_cpumsf_err(RS_INIT_FAILURE_QSI);
  1399. return -ENODEV;
  1400. }
  1401. if (si.bsdes != sizeof(struct hws_basic_entry)) {
  1402. pr_cpumsf_err(RS_INIT_FAILURE_BSDES);
  1403. return -EINVAL;
  1404. }
  1405. if (si.ad) {
  1406. sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB);
  1407. cpumsf_pmu_events_attr[1] =
  1408. CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG);
  1409. }
  1410. sfdbg = debug_register(KMSG_COMPONENT, 2, 1, 80);
  1411. if (!sfdbg)
  1412. pr_err("Registering for s390dbf failed\n");
  1413. debug_register_view(sfdbg, &debug_sprintf_view);
  1414. err = register_external_irq(EXT_IRQ_MEASURE_ALERT,
  1415. cpumf_measurement_alert);
  1416. if (err) {
  1417. pr_cpumsf_err(RS_INIT_FAILURE_ALRT);
  1418. goto out;
  1419. }
  1420. err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW);
  1421. if (err) {
  1422. pr_cpumsf_err(RS_INIT_FAILURE_PERF);
  1423. unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
  1424. cpumf_measurement_alert);
  1425. goto out;
  1426. }
  1427. perf_cpu_notifier(cpumf_pmu_notifier);
  1428. out:
  1429. return err;
  1430. }
  1431. arch_initcall(init_cpum_sampling_pmu);
  1432. core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0640);