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