core.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  1. /*
  2. * Performance events x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
  9. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  10. * Copyright (C) 2009 Google, Inc., Stephane Eranian
  11. *
  12. * For licencing details see kernel-base/COPYING
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/capability.h>
  16. #include <linux/notifier.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/export.h>
  20. #include <linux/init.h>
  21. #include <linux/kdebug.h>
  22. #include <linux/sched/mm.h>
  23. #include <linux/sched/clock.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/slab.h>
  26. #include <linux/cpu.h>
  27. #include <linux/bitops.h>
  28. #include <linux/device.h>
  29. #include <linux/nospec.h>
  30. #include <asm/apic.h>
  31. #include <asm/stacktrace.h>
  32. #include <asm/nmi.h>
  33. #include <asm/smp.h>
  34. #include <asm/alternative.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/timer.h>
  38. #include <asm/desc.h>
  39. #include <asm/ldt.h>
  40. #include <asm/unwind.h>
  41. #include "perf_event.h"
  42. struct x86_pmu x86_pmu __read_mostly;
  43. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  44. .enabled = 1,
  45. };
  46. DEFINE_STATIC_KEY_FALSE(rdpmc_always_available_key);
  47. u64 __read_mostly hw_cache_event_ids
  48. [PERF_COUNT_HW_CACHE_MAX]
  49. [PERF_COUNT_HW_CACHE_OP_MAX]
  50. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  51. u64 __read_mostly hw_cache_extra_regs
  52. [PERF_COUNT_HW_CACHE_MAX]
  53. [PERF_COUNT_HW_CACHE_OP_MAX]
  54. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  55. /*
  56. * Propagate event elapsed time into the generic event.
  57. * Can only be executed on the CPU where the event is active.
  58. * Returns the delta events processed.
  59. */
  60. u64 x86_perf_event_update(struct perf_event *event)
  61. {
  62. struct hw_perf_event *hwc = &event->hw;
  63. int shift = 64 - x86_pmu.cntval_bits;
  64. u64 prev_raw_count, new_raw_count;
  65. int idx = hwc->idx;
  66. u64 delta;
  67. if (idx == INTEL_PMC_IDX_FIXED_BTS)
  68. return 0;
  69. /*
  70. * Careful: an NMI might modify the previous event value.
  71. *
  72. * Our tactic to handle this is to first atomically read and
  73. * exchange a new raw count - then add that new-prev delta
  74. * count to the generic event atomically:
  75. */
  76. again:
  77. prev_raw_count = local64_read(&hwc->prev_count);
  78. rdpmcl(hwc->event_base_rdpmc, new_raw_count);
  79. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  80. new_raw_count) != prev_raw_count)
  81. goto again;
  82. /*
  83. * Now we have the new raw value and have updated the prev
  84. * timestamp already. We can now calculate the elapsed delta
  85. * (event-)time and add that to the generic event.
  86. *
  87. * Careful, not all hw sign-extends above the physical width
  88. * of the count.
  89. */
  90. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  91. delta >>= shift;
  92. local64_add(delta, &event->count);
  93. local64_sub(delta, &hwc->period_left);
  94. return new_raw_count;
  95. }
  96. /*
  97. * Find and validate any extra registers to set up.
  98. */
  99. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  100. {
  101. struct hw_perf_event_extra *reg;
  102. struct extra_reg *er;
  103. reg = &event->hw.extra_reg;
  104. if (!x86_pmu.extra_regs)
  105. return 0;
  106. for (er = x86_pmu.extra_regs; er->msr; er++) {
  107. if (er->event != (config & er->config_mask))
  108. continue;
  109. if (event->attr.config1 & ~er->valid_mask)
  110. return -EINVAL;
  111. /* Check if the extra msrs can be safely accessed*/
  112. if (!er->extra_msr_access)
  113. return -ENXIO;
  114. reg->idx = er->idx;
  115. reg->config = event->attr.config1;
  116. reg->reg = er->msr;
  117. break;
  118. }
  119. return 0;
  120. }
  121. static atomic_t active_events;
  122. static atomic_t pmc_refcount;
  123. static DEFINE_MUTEX(pmc_reserve_mutex);
  124. #ifdef CONFIG_X86_LOCAL_APIC
  125. static bool reserve_pmc_hardware(void)
  126. {
  127. int i;
  128. for (i = 0; i < x86_pmu.num_counters; i++) {
  129. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  130. goto perfctr_fail;
  131. }
  132. for (i = 0; i < x86_pmu.num_counters; i++) {
  133. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  134. goto eventsel_fail;
  135. }
  136. return true;
  137. eventsel_fail:
  138. for (i--; i >= 0; i--)
  139. release_evntsel_nmi(x86_pmu_config_addr(i));
  140. i = x86_pmu.num_counters;
  141. perfctr_fail:
  142. for (i--; i >= 0; i--)
  143. release_perfctr_nmi(x86_pmu_event_addr(i));
  144. return false;
  145. }
  146. static void release_pmc_hardware(void)
  147. {
  148. int i;
  149. for (i = 0; i < x86_pmu.num_counters; i++) {
  150. release_perfctr_nmi(x86_pmu_event_addr(i));
  151. release_evntsel_nmi(x86_pmu_config_addr(i));
  152. }
  153. }
  154. #else
  155. static bool reserve_pmc_hardware(void) { return true; }
  156. static void release_pmc_hardware(void) {}
  157. #endif
  158. static bool check_hw_exists(void)
  159. {
  160. u64 val, val_fail = -1, val_new= ~0;
  161. int i, reg, reg_fail = -1, ret = 0;
  162. int bios_fail = 0;
  163. int reg_safe = -1;
  164. /*
  165. * Check to see if the BIOS enabled any of the counters, if so
  166. * complain and bail.
  167. */
  168. for (i = 0; i < x86_pmu.num_counters; i++) {
  169. reg = x86_pmu_config_addr(i);
  170. ret = rdmsrl_safe(reg, &val);
  171. if (ret)
  172. goto msr_fail;
  173. if (val & ARCH_PERFMON_EVENTSEL_ENABLE) {
  174. bios_fail = 1;
  175. val_fail = val;
  176. reg_fail = reg;
  177. } else {
  178. reg_safe = i;
  179. }
  180. }
  181. if (x86_pmu.num_counters_fixed) {
  182. reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  183. ret = rdmsrl_safe(reg, &val);
  184. if (ret)
  185. goto msr_fail;
  186. for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
  187. if (val & (0x03 << i*4)) {
  188. bios_fail = 1;
  189. val_fail = val;
  190. reg_fail = reg;
  191. }
  192. }
  193. }
  194. /*
  195. * If all the counters are enabled, the below test will always
  196. * fail. The tools will also become useless in this scenario.
  197. * Just fail and disable the hardware counters.
  198. */
  199. if (reg_safe == -1) {
  200. reg = reg_safe;
  201. goto msr_fail;
  202. }
  203. /*
  204. * Read the current value, change it and read it back to see if it
  205. * matches, this is needed to detect certain hardware emulators
  206. * (qemu/kvm) that don't trap on the MSR access and always return 0s.
  207. */
  208. reg = x86_pmu_event_addr(reg_safe);
  209. if (rdmsrl_safe(reg, &val))
  210. goto msr_fail;
  211. val ^= 0xffffUL;
  212. ret = wrmsrl_safe(reg, val);
  213. ret |= rdmsrl_safe(reg, &val_new);
  214. if (ret || val != val_new)
  215. goto msr_fail;
  216. /*
  217. * We still allow the PMU driver to operate:
  218. */
  219. if (bios_fail) {
  220. pr_cont("Broken BIOS detected, complain to your hardware vendor.\n");
  221. pr_err(FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n",
  222. reg_fail, val_fail);
  223. }
  224. return true;
  225. msr_fail:
  226. if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
  227. pr_cont("PMU not available due to virtualization, using software events only.\n");
  228. } else {
  229. pr_cont("Broken PMU hardware detected, using software events only.\n");
  230. pr_err("Failed to access perfctr msr (MSR %x is %Lx)\n",
  231. reg, val_new);
  232. }
  233. return false;
  234. }
  235. static void hw_perf_event_destroy(struct perf_event *event)
  236. {
  237. x86_release_hardware();
  238. atomic_dec(&active_events);
  239. }
  240. void hw_perf_lbr_event_destroy(struct perf_event *event)
  241. {
  242. hw_perf_event_destroy(event);
  243. /* undo the lbr/bts event accounting */
  244. x86_del_exclusive(x86_lbr_exclusive_lbr);
  245. }
  246. static inline int x86_pmu_initialized(void)
  247. {
  248. return x86_pmu.handle_irq != NULL;
  249. }
  250. static inline int
  251. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  252. {
  253. struct perf_event_attr *attr = &event->attr;
  254. unsigned int cache_type, cache_op, cache_result;
  255. u64 config, val;
  256. config = attr->config;
  257. cache_type = (config >> 0) & 0xff;
  258. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  259. return -EINVAL;
  260. cache_type = array_index_nospec(cache_type, PERF_COUNT_HW_CACHE_MAX);
  261. cache_op = (config >> 8) & 0xff;
  262. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  263. return -EINVAL;
  264. cache_op = array_index_nospec(cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
  265. cache_result = (config >> 16) & 0xff;
  266. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  267. return -EINVAL;
  268. cache_result = array_index_nospec(cache_result, PERF_COUNT_HW_CACHE_RESULT_MAX);
  269. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  270. if (val == 0)
  271. return -ENOENT;
  272. if (val == -1)
  273. return -EINVAL;
  274. hwc->config |= val;
  275. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  276. return x86_pmu_extra_regs(val, event);
  277. }
  278. int x86_reserve_hardware(void)
  279. {
  280. int err = 0;
  281. if (!atomic_inc_not_zero(&pmc_refcount)) {
  282. mutex_lock(&pmc_reserve_mutex);
  283. if (atomic_read(&pmc_refcount) == 0) {
  284. if (!reserve_pmc_hardware())
  285. err = -EBUSY;
  286. else
  287. reserve_ds_buffers();
  288. }
  289. if (!err)
  290. atomic_inc(&pmc_refcount);
  291. mutex_unlock(&pmc_reserve_mutex);
  292. }
  293. return err;
  294. }
  295. void x86_release_hardware(void)
  296. {
  297. if (atomic_dec_and_mutex_lock(&pmc_refcount, &pmc_reserve_mutex)) {
  298. release_pmc_hardware();
  299. release_ds_buffers();
  300. mutex_unlock(&pmc_reserve_mutex);
  301. }
  302. }
  303. /*
  304. * Check if we can create event of a certain type (that no conflicting events
  305. * are present).
  306. */
  307. int x86_add_exclusive(unsigned int what)
  308. {
  309. int i;
  310. /*
  311. * When lbr_pt_coexist we allow PT to coexist with either LBR or BTS.
  312. * LBR and BTS are still mutually exclusive.
  313. */
  314. if (x86_pmu.lbr_pt_coexist && what == x86_lbr_exclusive_pt)
  315. return 0;
  316. if (!atomic_inc_not_zero(&x86_pmu.lbr_exclusive[what])) {
  317. mutex_lock(&pmc_reserve_mutex);
  318. for (i = 0; i < ARRAY_SIZE(x86_pmu.lbr_exclusive); i++) {
  319. if (i != what && atomic_read(&x86_pmu.lbr_exclusive[i]))
  320. goto fail_unlock;
  321. }
  322. atomic_inc(&x86_pmu.lbr_exclusive[what]);
  323. mutex_unlock(&pmc_reserve_mutex);
  324. }
  325. atomic_inc(&active_events);
  326. return 0;
  327. fail_unlock:
  328. mutex_unlock(&pmc_reserve_mutex);
  329. return -EBUSY;
  330. }
  331. void x86_del_exclusive(unsigned int what)
  332. {
  333. if (x86_pmu.lbr_pt_coexist && what == x86_lbr_exclusive_pt)
  334. return;
  335. atomic_dec(&x86_pmu.lbr_exclusive[what]);
  336. atomic_dec(&active_events);
  337. }
  338. int x86_setup_perfctr(struct perf_event *event)
  339. {
  340. struct perf_event_attr *attr = &event->attr;
  341. struct hw_perf_event *hwc = &event->hw;
  342. u64 config;
  343. if (!is_sampling_event(event)) {
  344. hwc->sample_period = x86_pmu.max_period;
  345. hwc->last_period = hwc->sample_period;
  346. local64_set(&hwc->period_left, hwc->sample_period);
  347. }
  348. if (attr->type == PERF_TYPE_RAW)
  349. return x86_pmu_extra_regs(event->attr.config, event);
  350. if (attr->type == PERF_TYPE_HW_CACHE)
  351. return set_ext_hw_attr(hwc, event);
  352. if (attr->config >= x86_pmu.max_events)
  353. return -EINVAL;
  354. attr->config = array_index_nospec((unsigned long)attr->config, x86_pmu.max_events);
  355. /*
  356. * The generic map:
  357. */
  358. config = x86_pmu.event_map(attr->config);
  359. if (config == 0)
  360. return -ENOENT;
  361. if (config == -1LL)
  362. return -EINVAL;
  363. /*
  364. * Branch tracing:
  365. */
  366. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  367. !attr->freq && hwc->sample_period == 1) {
  368. /* BTS is not supported by this architecture. */
  369. if (!x86_pmu.bts_active)
  370. return -EOPNOTSUPP;
  371. /* BTS is currently only allowed for user-mode. */
  372. if (!attr->exclude_kernel)
  373. return -EOPNOTSUPP;
  374. /* disallow bts if conflicting events are present */
  375. if (x86_add_exclusive(x86_lbr_exclusive_lbr))
  376. return -EBUSY;
  377. event->destroy = hw_perf_lbr_event_destroy;
  378. }
  379. hwc->config |= config;
  380. return 0;
  381. }
  382. /*
  383. * check that branch_sample_type is compatible with
  384. * settings needed for precise_ip > 1 which implies
  385. * using the LBR to capture ALL taken branches at the
  386. * priv levels of the measurement
  387. */
  388. static inline int precise_br_compat(struct perf_event *event)
  389. {
  390. u64 m = event->attr.branch_sample_type;
  391. u64 b = 0;
  392. /* must capture all branches */
  393. if (!(m & PERF_SAMPLE_BRANCH_ANY))
  394. return 0;
  395. m &= PERF_SAMPLE_BRANCH_KERNEL | PERF_SAMPLE_BRANCH_USER;
  396. if (!event->attr.exclude_user)
  397. b |= PERF_SAMPLE_BRANCH_USER;
  398. if (!event->attr.exclude_kernel)
  399. b |= PERF_SAMPLE_BRANCH_KERNEL;
  400. /*
  401. * ignore PERF_SAMPLE_BRANCH_HV, not supported on x86
  402. */
  403. return m == b;
  404. }
  405. int x86_pmu_max_precise(void)
  406. {
  407. int precise = 0;
  408. /* Support for constant skid */
  409. if (x86_pmu.pebs_active && !x86_pmu.pebs_broken) {
  410. precise++;
  411. /* Support for IP fixup */
  412. if (x86_pmu.lbr_nr || x86_pmu.intel_cap.pebs_format >= 2)
  413. precise++;
  414. if (x86_pmu.pebs_prec_dist)
  415. precise++;
  416. }
  417. return precise;
  418. }
  419. int x86_pmu_hw_config(struct perf_event *event)
  420. {
  421. if (event->attr.precise_ip) {
  422. int precise = x86_pmu_max_precise();
  423. if (event->attr.precise_ip > precise)
  424. return -EOPNOTSUPP;
  425. /* There's no sense in having PEBS for non sampling events: */
  426. if (!is_sampling_event(event))
  427. return -EINVAL;
  428. }
  429. /*
  430. * check that PEBS LBR correction does not conflict with
  431. * whatever the user is asking with attr->branch_sample_type
  432. */
  433. if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format < 2) {
  434. u64 *br_type = &event->attr.branch_sample_type;
  435. if (has_branch_stack(event)) {
  436. if (!precise_br_compat(event))
  437. return -EOPNOTSUPP;
  438. /* branch_sample_type is compatible */
  439. } else {
  440. /*
  441. * user did not specify branch_sample_type
  442. *
  443. * For PEBS fixups, we capture all
  444. * the branches at the priv level of the
  445. * event.
  446. */
  447. *br_type = PERF_SAMPLE_BRANCH_ANY;
  448. if (!event->attr.exclude_user)
  449. *br_type |= PERF_SAMPLE_BRANCH_USER;
  450. if (!event->attr.exclude_kernel)
  451. *br_type |= PERF_SAMPLE_BRANCH_KERNEL;
  452. }
  453. }
  454. if (event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK)
  455. event->attach_state |= PERF_ATTACH_TASK_DATA;
  456. /*
  457. * Generate PMC IRQs:
  458. * (keep 'enabled' bit clear for now)
  459. */
  460. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  461. /*
  462. * Count user and OS events unless requested not to
  463. */
  464. if (!event->attr.exclude_user)
  465. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  466. if (!event->attr.exclude_kernel)
  467. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  468. if (event->attr.type == PERF_TYPE_RAW)
  469. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  470. if (event->attr.sample_period && x86_pmu.limit_period) {
  471. if (x86_pmu.limit_period(event, event->attr.sample_period) >
  472. event->attr.sample_period)
  473. return -EINVAL;
  474. }
  475. return x86_setup_perfctr(event);
  476. }
  477. /*
  478. * Setup the hardware configuration for a given attr_type
  479. */
  480. static int __x86_pmu_event_init(struct perf_event *event)
  481. {
  482. int err;
  483. if (!x86_pmu_initialized())
  484. return -ENODEV;
  485. err = x86_reserve_hardware();
  486. if (err)
  487. return err;
  488. atomic_inc(&active_events);
  489. event->destroy = hw_perf_event_destroy;
  490. event->hw.idx = -1;
  491. event->hw.last_cpu = -1;
  492. event->hw.last_tag = ~0ULL;
  493. /* mark unused */
  494. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  495. event->hw.branch_reg.idx = EXTRA_REG_NONE;
  496. return x86_pmu.hw_config(event);
  497. }
  498. void x86_pmu_disable_all(void)
  499. {
  500. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  501. int idx;
  502. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  503. u64 val;
  504. if (!test_bit(idx, cpuc->active_mask))
  505. continue;
  506. rdmsrl(x86_pmu_config_addr(idx), val);
  507. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  508. continue;
  509. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  510. wrmsrl(x86_pmu_config_addr(idx), val);
  511. }
  512. }
  513. /*
  514. * There may be PMI landing after enabled=0. The PMI hitting could be before or
  515. * after disable_all.
  516. *
  517. * If PMI hits before disable_all, the PMU will be disabled in the NMI handler.
  518. * It will not be re-enabled in the NMI handler again, because enabled=0. After
  519. * handling the NMI, disable_all will be called, which will not change the
  520. * state either. If PMI hits after disable_all, the PMU is already disabled
  521. * before entering NMI handler. The NMI handler will not change the state
  522. * either.
  523. *
  524. * So either situation is harmless.
  525. */
  526. static void x86_pmu_disable(struct pmu *pmu)
  527. {
  528. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  529. if (!x86_pmu_initialized())
  530. return;
  531. if (!cpuc->enabled)
  532. return;
  533. cpuc->n_added = 0;
  534. cpuc->enabled = 0;
  535. barrier();
  536. x86_pmu.disable_all();
  537. }
  538. void x86_pmu_enable_all(int added)
  539. {
  540. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  541. int idx;
  542. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  543. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  544. if (!test_bit(idx, cpuc->active_mask))
  545. continue;
  546. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  547. }
  548. }
  549. static struct pmu pmu;
  550. static inline int is_x86_event(struct perf_event *event)
  551. {
  552. return event->pmu == &pmu;
  553. }
  554. /*
  555. * Event scheduler state:
  556. *
  557. * Assign events iterating over all events and counters, beginning
  558. * with events with least weights first. Keep the current iterator
  559. * state in struct sched_state.
  560. */
  561. struct sched_state {
  562. int weight;
  563. int event; /* event index */
  564. int counter; /* counter index */
  565. int unassigned; /* number of events to be assigned left */
  566. int nr_gp; /* number of GP counters used */
  567. unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  568. };
  569. /* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */
  570. #define SCHED_STATES_MAX 2
  571. struct perf_sched {
  572. int max_weight;
  573. int max_events;
  574. int max_gp;
  575. int saved_states;
  576. struct event_constraint **constraints;
  577. struct sched_state state;
  578. struct sched_state saved[SCHED_STATES_MAX];
  579. };
  580. /*
  581. * Initialize interator that runs through all events and counters.
  582. */
  583. static void perf_sched_init(struct perf_sched *sched, struct event_constraint **constraints,
  584. int num, int wmin, int wmax, int gpmax)
  585. {
  586. int idx;
  587. memset(sched, 0, sizeof(*sched));
  588. sched->max_events = num;
  589. sched->max_weight = wmax;
  590. sched->max_gp = gpmax;
  591. sched->constraints = constraints;
  592. for (idx = 0; idx < num; idx++) {
  593. if (constraints[idx]->weight == wmin)
  594. break;
  595. }
  596. sched->state.event = idx; /* start with min weight */
  597. sched->state.weight = wmin;
  598. sched->state.unassigned = num;
  599. }
  600. static void perf_sched_save_state(struct perf_sched *sched)
  601. {
  602. if (WARN_ON_ONCE(sched->saved_states >= SCHED_STATES_MAX))
  603. return;
  604. sched->saved[sched->saved_states] = sched->state;
  605. sched->saved_states++;
  606. }
  607. static bool perf_sched_restore_state(struct perf_sched *sched)
  608. {
  609. if (!sched->saved_states)
  610. return false;
  611. sched->saved_states--;
  612. sched->state = sched->saved[sched->saved_states];
  613. /* continue with next counter: */
  614. clear_bit(sched->state.counter++, sched->state.used);
  615. return true;
  616. }
  617. /*
  618. * Select a counter for the current event to schedule. Return true on
  619. * success.
  620. */
  621. static bool __perf_sched_find_counter(struct perf_sched *sched)
  622. {
  623. struct event_constraint *c;
  624. int idx;
  625. if (!sched->state.unassigned)
  626. return false;
  627. if (sched->state.event >= sched->max_events)
  628. return false;
  629. c = sched->constraints[sched->state.event];
  630. /* Prefer fixed purpose counters */
  631. if (c->idxmsk64 & (~0ULL << INTEL_PMC_IDX_FIXED)) {
  632. idx = INTEL_PMC_IDX_FIXED;
  633. for_each_set_bit_from(idx, c->idxmsk, X86_PMC_IDX_MAX) {
  634. if (!__test_and_set_bit(idx, sched->state.used))
  635. goto done;
  636. }
  637. }
  638. /* Grab the first unused counter starting with idx */
  639. idx = sched->state.counter;
  640. for_each_set_bit_from(idx, c->idxmsk, INTEL_PMC_IDX_FIXED) {
  641. if (!__test_and_set_bit(idx, sched->state.used)) {
  642. if (sched->state.nr_gp++ >= sched->max_gp)
  643. return false;
  644. goto done;
  645. }
  646. }
  647. return false;
  648. done:
  649. sched->state.counter = idx;
  650. if (c->overlap)
  651. perf_sched_save_state(sched);
  652. return true;
  653. }
  654. static bool perf_sched_find_counter(struct perf_sched *sched)
  655. {
  656. while (!__perf_sched_find_counter(sched)) {
  657. if (!perf_sched_restore_state(sched))
  658. return false;
  659. }
  660. return true;
  661. }
  662. /*
  663. * Go through all unassigned events and find the next one to schedule.
  664. * Take events with the least weight first. Return true on success.
  665. */
  666. static bool perf_sched_next_event(struct perf_sched *sched)
  667. {
  668. struct event_constraint *c;
  669. if (!sched->state.unassigned || !--sched->state.unassigned)
  670. return false;
  671. do {
  672. /* next event */
  673. sched->state.event++;
  674. if (sched->state.event >= sched->max_events) {
  675. /* next weight */
  676. sched->state.event = 0;
  677. sched->state.weight++;
  678. if (sched->state.weight > sched->max_weight)
  679. return false;
  680. }
  681. c = sched->constraints[sched->state.event];
  682. } while (c->weight != sched->state.weight);
  683. sched->state.counter = 0; /* start with first counter */
  684. return true;
  685. }
  686. /*
  687. * Assign a counter for each event.
  688. */
  689. int perf_assign_events(struct event_constraint **constraints, int n,
  690. int wmin, int wmax, int gpmax, int *assign)
  691. {
  692. struct perf_sched sched;
  693. perf_sched_init(&sched, constraints, n, wmin, wmax, gpmax);
  694. do {
  695. if (!perf_sched_find_counter(&sched))
  696. break; /* failed */
  697. if (assign)
  698. assign[sched.state.event] = sched.state.counter;
  699. } while (perf_sched_next_event(&sched));
  700. return sched.state.unassigned;
  701. }
  702. EXPORT_SYMBOL_GPL(perf_assign_events);
  703. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  704. {
  705. struct event_constraint *c;
  706. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  707. struct perf_event *e;
  708. int i, wmin, wmax, unsched = 0;
  709. struct hw_perf_event *hwc;
  710. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  711. if (x86_pmu.start_scheduling)
  712. x86_pmu.start_scheduling(cpuc);
  713. for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
  714. cpuc->event_constraint[i] = NULL;
  715. c = x86_pmu.get_event_constraints(cpuc, i, cpuc->event_list[i]);
  716. cpuc->event_constraint[i] = c;
  717. wmin = min(wmin, c->weight);
  718. wmax = max(wmax, c->weight);
  719. }
  720. /*
  721. * fastpath, try to reuse previous register
  722. */
  723. for (i = 0; i < n; i++) {
  724. hwc = &cpuc->event_list[i]->hw;
  725. c = cpuc->event_constraint[i];
  726. /* never assigned */
  727. if (hwc->idx == -1)
  728. break;
  729. /* constraint still honored */
  730. if (!test_bit(hwc->idx, c->idxmsk))
  731. break;
  732. /* not already used */
  733. if (test_bit(hwc->idx, used_mask))
  734. break;
  735. __set_bit(hwc->idx, used_mask);
  736. if (assign)
  737. assign[i] = hwc->idx;
  738. }
  739. /* slow path */
  740. if (i != n) {
  741. int gpmax = x86_pmu.num_counters;
  742. /*
  743. * Do not allow scheduling of more than half the available
  744. * generic counters.
  745. *
  746. * This helps avoid counter starvation of sibling thread by
  747. * ensuring at most half the counters cannot be in exclusive
  748. * mode. There is no designated counters for the limits. Any
  749. * N/2 counters can be used. This helps with events with
  750. * specific counter constraints.
  751. */
  752. if (is_ht_workaround_enabled() && !cpuc->is_fake &&
  753. READ_ONCE(cpuc->excl_cntrs->exclusive_present))
  754. gpmax /= 2;
  755. unsched = perf_assign_events(cpuc->event_constraint, n, wmin,
  756. wmax, gpmax, assign);
  757. }
  758. /*
  759. * In case of success (unsched = 0), mark events as committed,
  760. * so we do not put_constraint() in case new events are added
  761. * and fail to be scheduled
  762. *
  763. * We invoke the lower level commit callback to lock the resource
  764. *
  765. * We do not need to do all of this in case we are called to
  766. * validate an event group (assign == NULL)
  767. */
  768. if (!unsched && assign) {
  769. for (i = 0; i < n; i++) {
  770. e = cpuc->event_list[i];
  771. e->hw.flags |= PERF_X86_EVENT_COMMITTED;
  772. if (x86_pmu.commit_scheduling)
  773. x86_pmu.commit_scheduling(cpuc, i, assign[i]);
  774. }
  775. } else {
  776. for (i = 0; i < n; i++) {
  777. e = cpuc->event_list[i];
  778. /*
  779. * do not put_constraint() on comitted events,
  780. * because they are good to go
  781. */
  782. if ((e->hw.flags & PERF_X86_EVENT_COMMITTED))
  783. continue;
  784. /*
  785. * release events that failed scheduling
  786. */
  787. if (x86_pmu.put_event_constraints)
  788. x86_pmu.put_event_constraints(cpuc, e);
  789. }
  790. }
  791. if (x86_pmu.stop_scheduling)
  792. x86_pmu.stop_scheduling(cpuc);
  793. return unsched ? -EINVAL : 0;
  794. }
  795. /*
  796. * dogrp: true if must collect siblings events (group)
  797. * returns total number of events and error code
  798. */
  799. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  800. {
  801. struct perf_event *event;
  802. int n, max_count;
  803. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  804. /* current number of events already accepted */
  805. n = cpuc->n_events;
  806. if (is_x86_event(leader)) {
  807. if (n >= max_count)
  808. return -EINVAL;
  809. cpuc->event_list[n] = leader;
  810. n++;
  811. }
  812. if (!dogrp)
  813. return n;
  814. for_each_sibling_event(event, leader) {
  815. if (!is_x86_event(event) ||
  816. event->state <= PERF_EVENT_STATE_OFF)
  817. continue;
  818. if (n >= max_count)
  819. return -EINVAL;
  820. cpuc->event_list[n] = event;
  821. n++;
  822. }
  823. return n;
  824. }
  825. static inline void x86_assign_hw_event(struct perf_event *event,
  826. struct cpu_hw_events *cpuc, int i)
  827. {
  828. struct hw_perf_event *hwc = &event->hw;
  829. hwc->idx = cpuc->assign[i];
  830. hwc->last_cpu = smp_processor_id();
  831. hwc->last_tag = ++cpuc->tags[i];
  832. if (hwc->idx == INTEL_PMC_IDX_FIXED_BTS) {
  833. hwc->config_base = 0;
  834. hwc->event_base = 0;
  835. } else if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
  836. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  837. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - INTEL_PMC_IDX_FIXED);
  838. hwc->event_base_rdpmc = (hwc->idx - INTEL_PMC_IDX_FIXED) | 1<<30;
  839. } else {
  840. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  841. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  842. hwc->event_base_rdpmc = x86_pmu_rdpmc_index(hwc->idx);
  843. }
  844. }
  845. /**
  846. * x86_perf_rdpmc_index - Return PMC counter used for event
  847. * @event: the perf_event to which the PMC counter was assigned
  848. *
  849. * The counter assigned to this performance event may change if interrupts
  850. * are enabled. This counter should thus never be used while interrupts are
  851. * enabled. Before this function is used to obtain the assigned counter the
  852. * event should be checked for validity using, for example,
  853. * perf_event_read_local(), within the same interrupt disabled section in
  854. * which this counter is planned to be used.
  855. *
  856. * Return: The index of the performance monitoring counter assigned to
  857. * @perf_event.
  858. */
  859. int x86_perf_rdpmc_index(struct perf_event *event)
  860. {
  861. lockdep_assert_irqs_disabled();
  862. return event->hw.event_base_rdpmc;
  863. }
  864. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  865. struct cpu_hw_events *cpuc,
  866. int i)
  867. {
  868. return hwc->idx == cpuc->assign[i] &&
  869. hwc->last_cpu == smp_processor_id() &&
  870. hwc->last_tag == cpuc->tags[i];
  871. }
  872. static void x86_pmu_start(struct perf_event *event, int flags);
  873. static void x86_pmu_enable(struct pmu *pmu)
  874. {
  875. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  876. struct perf_event *event;
  877. struct hw_perf_event *hwc;
  878. int i, added = cpuc->n_added;
  879. if (!x86_pmu_initialized())
  880. return;
  881. if (cpuc->enabled)
  882. return;
  883. if (cpuc->n_added) {
  884. int n_running = cpuc->n_events - cpuc->n_added;
  885. /*
  886. * apply assignment obtained either from
  887. * hw_perf_group_sched_in() or x86_pmu_enable()
  888. *
  889. * step1: save events moving to new counters
  890. */
  891. for (i = 0; i < n_running; i++) {
  892. event = cpuc->event_list[i];
  893. hwc = &event->hw;
  894. /*
  895. * we can avoid reprogramming counter if:
  896. * - assigned same counter as last time
  897. * - running on same CPU as last time
  898. * - no other event has used the counter since
  899. */
  900. if (hwc->idx == -1 ||
  901. match_prev_assignment(hwc, cpuc, i))
  902. continue;
  903. /*
  904. * Ensure we don't accidentally enable a stopped
  905. * counter simply because we rescheduled.
  906. */
  907. if (hwc->state & PERF_HES_STOPPED)
  908. hwc->state |= PERF_HES_ARCH;
  909. x86_pmu_stop(event, PERF_EF_UPDATE);
  910. }
  911. /*
  912. * step2: reprogram moved events into new counters
  913. */
  914. for (i = 0; i < cpuc->n_events; i++) {
  915. event = cpuc->event_list[i];
  916. hwc = &event->hw;
  917. if (!match_prev_assignment(hwc, cpuc, i))
  918. x86_assign_hw_event(event, cpuc, i);
  919. else if (i < n_running)
  920. continue;
  921. if (hwc->state & PERF_HES_ARCH)
  922. continue;
  923. x86_pmu_start(event, PERF_EF_RELOAD);
  924. }
  925. cpuc->n_added = 0;
  926. perf_events_lapic_init();
  927. }
  928. cpuc->enabled = 1;
  929. barrier();
  930. x86_pmu.enable_all(added);
  931. }
  932. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  933. /*
  934. * Set the next IRQ period, based on the hwc->period_left value.
  935. * To be called with the event disabled in hw:
  936. */
  937. int x86_perf_event_set_period(struct perf_event *event)
  938. {
  939. struct hw_perf_event *hwc = &event->hw;
  940. s64 left = local64_read(&hwc->period_left);
  941. s64 period = hwc->sample_period;
  942. int ret = 0, idx = hwc->idx;
  943. if (idx == INTEL_PMC_IDX_FIXED_BTS)
  944. return 0;
  945. /*
  946. * If we are way outside a reasonable range then just skip forward:
  947. */
  948. if (unlikely(left <= -period)) {
  949. left = period;
  950. local64_set(&hwc->period_left, left);
  951. hwc->last_period = period;
  952. ret = 1;
  953. }
  954. if (unlikely(left <= 0)) {
  955. left += period;
  956. local64_set(&hwc->period_left, left);
  957. hwc->last_period = period;
  958. ret = 1;
  959. }
  960. /*
  961. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  962. */
  963. if (unlikely(left < 2))
  964. left = 2;
  965. if (left > x86_pmu.max_period)
  966. left = x86_pmu.max_period;
  967. if (x86_pmu.limit_period)
  968. left = x86_pmu.limit_period(event, left);
  969. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  970. /*
  971. * The hw event starts counting from this event offset,
  972. * mark it to be able to extra future deltas:
  973. */
  974. local64_set(&hwc->prev_count, (u64)-left);
  975. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  976. /*
  977. * Due to erratum on certan cpu we need
  978. * a second write to be sure the register
  979. * is updated properly
  980. */
  981. if (x86_pmu.perfctr_second_write) {
  982. wrmsrl(hwc->event_base,
  983. (u64)(-left) & x86_pmu.cntval_mask);
  984. }
  985. perf_event_update_userpage(event);
  986. return ret;
  987. }
  988. void x86_pmu_enable_event(struct perf_event *event)
  989. {
  990. if (__this_cpu_read(cpu_hw_events.enabled))
  991. __x86_pmu_enable_event(&event->hw,
  992. ARCH_PERFMON_EVENTSEL_ENABLE);
  993. }
  994. /*
  995. * Add a single event to the PMU.
  996. *
  997. * The event is added to the group of enabled events
  998. * but only if it can be scehduled with existing events.
  999. */
  1000. static int x86_pmu_add(struct perf_event *event, int flags)
  1001. {
  1002. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1003. struct hw_perf_event *hwc;
  1004. int assign[X86_PMC_IDX_MAX];
  1005. int n, n0, ret;
  1006. hwc = &event->hw;
  1007. n0 = cpuc->n_events;
  1008. ret = n = collect_events(cpuc, event, false);
  1009. if (ret < 0)
  1010. goto out;
  1011. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  1012. if (!(flags & PERF_EF_START))
  1013. hwc->state |= PERF_HES_ARCH;
  1014. /*
  1015. * If group events scheduling transaction was started,
  1016. * skip the schedulability test here, it will be performed
  1017. * at commit time (->commit_txn) as a whole.
  1018. *
  1019. * If commit fails, we'll call ->del() on all events
  1020. * for which ->add() was called.
  1021. */
  1022. if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
  1023. goto done_collect;
  1024. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1025. if (ret)
  1026. goto out;
  1027. /*
  1028. * copy new assignment, now we know it is possible
  1029. * will be used by hw_perf_enable()
  1030. */
  1031. memcpy(cpuc->assign, assign, n*sizeof(int));
  1032. done_collect:
  1033. /*
  1034. * Commit the collect_events() state. See x86_pmu_del() and
  1035. * x86_pmu_*_txn().
  1036. */
  1037. cpuc->n_events = n;
  1038. cpuc->n_added += n - n0;
  1039. cpuc->n_txn += n - n0;
  1040. if (x86_pmu.add) {
  1041. /*
  1042. * This is before x86_pmu_enable() will call x86_pmu_start(),
  1043. * so we enable LBRs before an event needs them etc..
  1044. */
  1045. x86_pmu.add(event);
  1046. }
  1047. ret = 0;
  1048. out:
  1049. return ret;
  1050. }
  1051. static void x86_pmu_start(struct perf_event *event, int flags)
  1052. {
  1053. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1054. int idx = event->hw.idx;
  1055. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  1056. return;
  1057. if (WARN_ON_ONCE(idx == -1))
  1058. return;
  1059. if (flags & PERF_EF_RELOAD) {
  1060. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1061. x86_perf_event_set_period(event);
  1062. }
  1063. event->hw.state = 0;
  1064. cpuc->events[idx] = event;
  1065. __set_bit(idx, cpuc->active_mask);
  1066. __set_bit(idx, cpuc->running);
  1067. x86_pmu.enable(event);
  1068. perf_event_update_userpage(event);
  1069. }
  1070. void perf_event_print_debug(void)
  1071. {
  1072. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  1073. u64 pebs, debugctl;
  1074. struct cpu_hw_events *cpuc;
  1075. unsigned long flags;
  1076. int cpu, idx;
  1077. if (!x86_pmu.num_counters)
  1078. return;
  1079. local_irq_save(flags);
  1080. cpu = smp_processor_id();
  1081. cpuc = &per_cpu(cpu_hw_events, cpu);
  1082. if (x86_pmu.version >= 2) {
  1083. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  1084. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1085. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  1086. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  1087. pr_info("\n");
  1088. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  1089. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  1090. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  1091. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  1092. if (x86_pmu.pebs_constraints) {
  1093. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  1094. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  1095. }
  1096. if (x86_pmu.lbr_nr) {
  1097. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  1098. pr_info("CPU#%d: debugctl: %016llx\n", cpu, debugctl);
  1099. }
  1100. }
  1101. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  1102. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1103. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  1104. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  1105. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  1106. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  1107. cpu, idx, pmc_ctrl);
  1108. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  1109. cpu, idx, pmc_count);
  1110. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  1111. cpu, idx, prev_left);
  1112. }
  1113. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1114. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1115. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1116. cpu, idx, pmc_count);
  1117. }
  1118. local_irq_restore(flags);
  1119. }
  1120. void x86_pmu_stop(struct perf_event *event, int flags)
  1121. {
  1122. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1123. struct hw_perf_event *hwc = &event->hw;
  1124. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  1125. x86_pmu.disable(event);
  1126. cpuc->events[hwc->idx] = NULL;
  1127. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  1128. hwc->state |= PERF_HES_STOPPED;
  1129. }
  1130. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  1131. /*
  1132. * Drain the remaining delta count out of a event
  1133. * that we are disabling:
  1134. */
  1135. x86_perf_event_update(event);
  1136. hwc->state |= PERF_HES_UPTODATE;
  1137. }
  1138. }
  1139. static void x86_pmu_del(struct perf_event *event, int flags)
  1140. {
  1141. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1142. int i;
  1143. /*
  1144. * event is descheduled
  1145. */
  1146. event->hw.flags &= ~PERF_X86_EVENT_COMMITTED;
  1147. /*
  1148. * If we're called during a txn, we only need to undo x86_pmu.add.
  1149. * The events never got scheduled and ->cancel_txn will truncate
  1150. * the event_list.
  1151. *
  1152. * XXX assumes any ->del() called during a TXN will only be on
  1153. * an event added during that same TXN.
  1154. */
  1155. if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
  1156. goto do_del;
  1157. /*
  1158. * Not a TXN, therefore cleanup properly.
  1159. */
  1160. x86_pmu_stop(event, PERF_EF_UPDATE);
  1161. for (i = 0; i < cpuc->n_events; i++) {
  1162. if (event == cpuc->event_list[i])
  1163. break;
  1164. }
  1165. if (WARN_ON_ONCE(i == cpuc->n_events)) /* called ->del() without ->add() ? */
  1166. return;
  1167. /* If we have a newly added event; make sure to decrease n_added. */
  1168. if (i >= cpuc->n_events - cpuc->n_added)
  1169. --cpuc->n_added;
  1170. if (x86_pmu.put_event_constraints)
  1171. x86_pmu.put_event_constraints(cpuc, event);
  1172. /* Delete the array entry. */
  1173. while (++i < cpuc->n_events) {
  1174. cpuc->event_list[i-1] = cpuc->event_list[i];
  1175. cpuc->event_constraint[i-1] = cpuc->event_constraint[i];
  1176. }
  1177. --cpuc->n_events;
  1178. perf_event_update_userpage(event);
  1179. do_del:
  1180. if (x86_pmu.del) {
  1181. /*
  1182. * This is after x86_pmu_stop(); so we disable LBRs after any
  1183. * event can need them etc..
  1184. */
  1185. x86_pmu.del(event);
  1186. }
  1187. }
  1188. int x86_pmu_handle_irq(struct pt_regs *regs)
  1189. {
  1190. struct perf_sample_data data;
  1191. struct cpu_hw_events *cpuc;
  1192. struct perf_event *event;
  1193. int idx, handled = 0;
  1194. u64 val;
  1195. cpuc = this_cpu_ptr(&cpu_hw_events);
  1196. /*
  1197. * Some chipsets need to unmask the LVTPC in a particular spot
  1198. * inside the nmi handler. As a result, the unmasking was pushed
  1199. * into all the nmi handlers.
  1200. *
  1201. * This generic handler doesn't seem to have any issues where the
  1202. * unmasking occurs so it was left at the top.
  1203. */
  1204. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1205. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1206. if (!test_bit(idx, cpuc->active_mask)) {
  1207. /*
  1208. * Though we deactivated the counter some cpus
  1209. * might still deliver spurious interrupts still
  1210. * in flight. Catch them:
  1211. */
  1212. if (__test_and_clear_bit(idx, cpuc->running))
  1213. handled++;
  1214. continue;
  1215. }
  1216. event = cpuc->events[idx];
  1217. val = x86_perf_event_update(event);
  1218. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  1219. continue;
  1220. /*
  1221. * event overflow
  1222. */
  1223. handled++;
  1224. perf_sample_data_init(&data, 0, event->hw.last_period);
  1225. if (!x86_perf_event_set_period(event))
  1226. continue;
  1227. if (perf_event_overflow(event, &data, regs))
  1228. x86_pmu_stop(event, 0);
  1229. }
  1230. if (handled)
  1231. inc_irq_stat(apic_perf_irqs);
  1232. return handled;
  1233. }
  1234. void perf_events_lapic_init(void)
  1235. {
  1236. if (!x86_pmu.apic || !x86_pmu_initialized())
  1237. return;
  1238. /*
  1239. * Always use NMI for PMU
  1240. */
  1241. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1242. }
  1243. static int
  1244. perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  1245. {
  1246. u64 start_clock;
  1247. u64 finish_clock;
  1248. int ret;
  1249. /*
  1250. * All PMUs/events that share this PMI handler should make sure to
  1251. * increment active_events for their events.
  1252. */
  1253. if (!atomic_read(&active_events))
  1254. return NMI_DONE;
  1255. start_clock = sched_clock();
  1256. ret = x86_pmu.handle_irq(regs);
  1257. finish_clock = sched_clock();
  1258. perf_sample_event_took(finish_clock - start_clock);
  1259. return ret;
  1260. }
  1261. NOKPROBE_SYMBOL(perf_event_nmi_handler);
  1262. struct event_constraint emptyconstraint;
  1263. struct event_constraint unconstrained;
  1264. static int x86_pmu_prepare_cpu(unsigned int cpu)
  1265. {
  1266. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1267. int i;
  1268. for (i = 0 ; i < X86_PERF_KFREE_MAX; i++)
  1269. cpuc->kfree_on_online[i] = NULL;
  1270. if (x86_pmu.cpu_prepare)
  1271. return x86_pmu.cpu_prepare(cpu);
  1272. return 0;
  1273. }
  1274. static int x86_pmu_dead_cpu(unsigned int cpu)
  1275. {
  1276. if (x86_pmu.cpu_dead)
  1277. x86_pmu.cpu_dead(cpu);
  1278. return 0;
  1279. }
  1280. static int x86_pmu_online_cpu(unsigned int cpu)
  1281. {
  1282. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1283. int i;
  1284. for (i = 0 ; i < X86_PERF_KFREE_MAX; i++) {
  1285. kfree(cpuc->kfree_on_online[i]);
  1286. cpuc->kfree_on_online[i] = NULL;
  1287. }
  1288. return 0;
  1289. }
  1290. static int x86_pmu_starting_cpu(unsigned int cpu)
  1291. {
  1292. if (x86_pmu.cpu_starting)
  1293. x86_pmu.cpu_starting(cpu);
  1294. return 0;
  1295. }
  1296. static int x86_pmu_dying_cpu(unsigned int cpu)
  1297. {
  1298. if (x86_pmu.cpu_dying)
  1299. x86_pmu.cpu_dying(cpu);
  1300. return 0;
  1301. }
  1302. static void __init pmu_check_apic(void)
  1303. {
  1304. if (boot_cpu_has(X86_FEATURE_APIC))
  1305. return;
  1306. x86_pmu.apic = 0;
  1307. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1308. pr_info("no hardware sampling interrupt available.\n");
  1309. /*
  1310. * If we have a PMU initialized but no APIC
  1311. * interrupts, we cannot sample hardware
  1312. * events (user-space has to fall back and
  1313. * sample via a hrtimer based software event):
  1314. */
  1315. pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  1316. }
  1317. static struct attribute_group x86_pmu_format_group __ro_after_init = {
  1318. .name = "format",
  1319. .attrs = NULL,
  1320. };
  1321. /*
  1322. * Remove all undefined events (x86_pmu.event_map(id) == 0)
  1323. * out of events_attr attributes.
  1324. */
  1325. static void __init filter_events(struct attribute **attrs)
  1326. {
  1327. struct device_attribute *d;
  1328. struct perf_pmu_events_attr *pmu_attr;
  1329. int offset = 0;
  1330. int i, j;
  1331. for (i = 0; attrs[i]; i++) {
  1332. d = (struct device_attribute *)attrs[i];
  1333. pmu_attr = container_of(d, struct perf_pmu_events_attr, attr);
  1334. /* str trumps id */
  1335. if (pmu_attr->event_str)
  1336. continue;
  1337. if (x86_pmu.event_map(i + offset))
  1338. continue;
  1339. for (j = i; attrs[j]; j++)
  1340. attrs[j] = attrs[j + 1];
  1341. /* Check the shifted attr. */
  1342. i--;
  1343. /*
  1344. * event_map() is index based, the attrs array is organized
  1345. * by increasing event index. If we shift the events, then
  1346. * we need to compensate for the event_map(), otherwise
  1347. * we are looking up the wrong event in the map
  1348. */
  1349. offset++;
  1350. }
  1351. }
  1352. /* Merge two pointer arrays */
  1353. __init struct attribute **merge_attr(struct attribute **a, struct attribute **b)
  1354. {
  1355. struct attribute **new;
  1356. int j, i;
  1357. for (j = 0; a && a[j]; j++)
  1358. ;
  1359. for (i = 0; b && b[i]; i++)
  1360. j++;
  1361. j++;
  1362. new = kmalloc_array(j, sizeof(struct attribute *), GFP_KERNEL);
  1363. if (!new)
  1364. return NULL;
  1365. j = 0;
  1366. for (i = 0; a && a[i]; i++)
  1367. new[j++] = a[i];
  1368. for (i = 0; b && b[i]; i++)
  1369. new[j++] = b[i];
  1370. new[j] = NULL;
  1371. return new;
  1372. }
  1373. ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, char *page)
  1374. {
  1375. struct perf_pmu_events_attr *pmu_attr = \
  1376. container_of(attr, struct perf_pmu_events_attr, attr);
  1377. u64 config = x86_pmu.event_map(pmu_attr->id);
  1378. /* string trumps id */
  1379. if (pmu_attr->event_str)
  1380. return sprintf(page, "%s", pmu_attr->event_str);
  1381. return x86_pmu.events_sysfs_show(page, config);
  1382. }
  1383. EXPORT_SYMBOL_GPL(events_sysfs_show);
  1384. ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr,
  1385. char *page)
  1386. {
  1387. struct perf_pmu_events_ht_attr *pmu_attr =
  1388. container_of(attr, struct perf_pmu_events_ht_attr, attr);
  1389. /*
  1390. * Report conditional events depending on Hyper-Threading.
  1391. *
  1392. * This is overly conservative as usually the HT special
  1393. * handling is not needed if the other CPU thread is idle.
  1394. *
  1395. * Note this does not (and cannot) handle the case when thread
  1396. * siblings are invisible, for example with virtualization
  1397. * if they are owned by some other guest. The user tool
  1398. * has to re-read when a thread sibling gets onlined later.
  1399. */
  1400. return sprintf(page, "%s",
  1401. topology_max_smt_threads() > 1 ?
  1402. pmu_attr->event_str_ht :
  1403. pmu_attr->event_str_noht);
  1404. }
  1405. EVENT_ATTR(cpu-cycles, CPU_CYCLES );
  1406. EVENT_ATTR(instructions, INSTRUCTIONS );
  1407. EVENT_ATTR(cache-references, CACHE_REFERENCES );
  1408. EVENT_ATTR(cache-misses, CACHE_MISSES );
  1409. EVENT_ATTR(branch-instructions, BRANCH_INSTRUCTIONS );
  1410. EVENT_ATTR(branch-misses, BRANCH_MISSES );
  1411. EVENT_ATTR(bus-cycles, BUS_CYCLES );
  1412. EVENT_ATTR(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND );
  1413. EVENT_ATTR(stalled-cycles-backend, STALLED_CYCLES_BACKEND );
  1414. EVENT_ATTR(ref-cycles, REF_CPU_CYCLES );
  1415. static struct attribute *empty_attrs;
  1416. static struct attribute *events_attr[] = {
  1417. EVENT_PTR(CPU_CYCLES),
  1418. EVENT_PTR(INSTRUCTIONS),
  1419. EVENT_PTR(CACHE_REFERENCES),
  1420. EVENT_PTR(CACHE_MISSES),
  1421. EVENT_PTR(BRANCH_INSTRUCTIONS),
  1422. EVENT_PTR(BRANCH_MISSES),
  1423. EVENT_PTR(BUS_CYCLES),
  1424. EVENT_PTR(STALLED_CYCLES_FRONTEND),
  1425. EVENT_PTR(STALLED_CYCLES_BACKEND),
  1426. EVENT_PTR(REF_CPU_CYCLES),
  1427. NULL,
  1428. };
  1429. static struct attribute_group x86_pmu_events_group __ro_after_init = {
  1430. .name = "events",
  1431. .attrs = events_attr,
  1432. };
  1433. ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event)
  1434. {
  1435. u64 umask = (config & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
  1436. u64 cmask = (config & ARCH_PERFMON_EVENTSEL_CMASK) >> 24;
  1437. bool edge = (config & ARCH_PERFMON_EVENTSEL_EDGE);
  1438. bool pc = (config & ARCH_PERFMON_EVENTSEL_PIN_CONTROL);
  1439. bool any = (config & ARCH_PERFMON_EVENTSEL_ANY);
  1440. bool inv = (config & ARCH_PERFMON_EVENTSEL_INV);
  1441. ssize_t ret;
  1442. /*
  1443. * We have whole page size to spend and just little data
  1444. * to write, so we can safely use sprintf.
  1445. */
  1446. ret = sprintf(page, "event=0x%02llx", event);
  1447. if (umask)
  1448. ret += sprintf(page + ret, ",umask=0x%02llx", umask);
  1449. if (edge)
  1450. ret += sprintf(page + ret, ",edge");
  1451. if (pc)
  1452. ret += sprintf(page + ret, ",pc");
  1453. if (any)
  1454. ret += sprintf(page + ret, ",any");
  1455. if (inv)
  1456. ret += sprintf(page + ret, ",inv");
  1457. if (cmask)
  1458. ret += sprintf(page + ret, ",cmask=0x%02llx", cmask);
  1459. ret += sprintf(page + ret, "\n");
  1460. return ret;
  1461. }
  1462. static struct attribute_group x86_pmu_attr_group;
  1463. static struct attribute_group x86_pmu_caps_group;
  1464. static int __init init_hw_perf_events(void)
  1465. {
  1466. struct x86_pmu_quirk *quirk;
  1467. int err;
  1468. pr_info("Performance Events: ");
  1469. switch (boot_cpu_data.x86_vendor) {
  1470. case X86_VENDOR_INTEL:
  1471. err = intel_pmu_init();
  1472. break;
  1473. case X86_VENDOR_AMD:
  1474. err = amd_pmu_init();
  1475. break;
  1476. default:
  1477. err = -ENOTSUPP;
  1478. }
  1479. if (err != 0) {
  1480. pr_cont("no PMU driver, software events only.\n");
  1481. return 0;
  1482. }
  1483. pmu_check_apic();
  1484. /* sanity check that the hardware exists or is emulated */
  1485. if (!check_hw_exists())
  1486. return 0;
  1487. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1488. x86_pmu.attr_rdpmc = 1; /* enable userspace RDPMC usage by default */
  1489. for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next)
  1490. quirk->func();
  1491. if (!x86_pmu.intel_ctrl)
  1492. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1493. perf_events_lapic_init();
  1494. register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
  1495. unconstrained = (struct event_constraint)
  1496. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1497. 0, x86_pmu.num_counters, 0, 0);
  1498. x86_pmu_format_group.attrs = x86_pmu.format_attrs;
  1499. if (x86_pmu.caps_attrs) {
  1500. struct attribute **tmp;
  1501. tmp = merge_attr(x86_pmu_caps_group.attrs, x86_pmu.caps_attrs);
  1502. if (!WARN_ON(!tmp))
  1503. x86_pmu_caps_group.attrs = tmp;
  1504. }
  1505. if (x86_pmu.event_attrs)
  1506. x86_pmu_events_group.attrs = x86_pmu.event_attrs;
  1507. if (!x86_pmu.events_sysfs_show)
  1508. x86_pmu_events_group.attrs = &empty_attrs;
  1509. else
  1510. filter_events(x86_pmu_events_group.attrs);
  1511. if (x86_pmu.cpu_events) {
  1512. struct attribute **tmp;
  1513. tmp = merge_attr(x86_pmu_events_group.attrs, x86_pmu.cpu_events);
  1514. if (!WARN_ON(!tmp))
  1515. x86_pmu_events_group.attrs = tmp;
  1516. }
  1517. if (x86_pmu.attrs) {
  1518. struct attribute **tmp;
  1519. tmp = merge_attr(x86_pmu_attr_group.attrs, x86_pmu.attrs);
  1520. if (!WARN_ON(!tmp))
  1521. x86_pmu_attr_group.attrs = tmp;
  1522. }
  1523. pr_info("... version: %d\n", x86_pmu.version);
  1524. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1525. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1526. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1527. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1528. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1529. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1530. /*
  1531. * Install callbacks. Core will call them for each online
  1532. * cpu.
  1533. */
  1534. err = cpuhp_setup_state(CPUHP_PERF_X86_PREPARE, "perf/x86:prepare",
  1535. x86_pmu_prepare_cpu, x86_pmu_dead_cpu);
  1536. if (err)
  1537. return err;
  1538. err = cpuhp_setup_state(CPUHP_AP_PERF_X86_STARTING,
  1539. "perf/x86:starting", x86_pmu_starting_cpu,
  1540. x86_pmu_dying_cpu);
  1541. if (err)
  1542. goto out;
  1543. err = cpuhp_setup_state(CPUHP_AP_PERF_X86_ONLINE, "perf/x86:online",
  1544. x86_pmu_online_cpu, NULL);
  1545. if (err)
  1546. goto out1;
  1547. err = perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1548. if (err)
  1549. goto out2;
  1550. return 0;
  1551. out2:
  1552. cpuhp_remove_state(CPUHP_AP_PERF_X86_ONLINE);
  1553. out1:
  1554. cpuhp_remove_state(CPUHP_AP_PERF_X86_STARTING);
  1555. out:
  1556. cpuhp_remove_state(CPUHP_PERF_X86_PREPARE);
  1557. return err;
  1558. }
  1559. early_initcall(init_hw_perf_events);
  1560. static inline void x86_pmu_read(struct perf_event *event)
  1561. {
  1562. if (x86_pmu.read)
  1563. return x86_pmu.read(event);
  1564. x86_perf_event_update(event);
  1565. }
  1566. /*
  1567. * Start group events scheduling transaction
  1568. * Set the flag to make pmu::enable() not perform the
  1569. * schedulability test, it will be performed at commit time
  1570. *
  1571. * We only support PERF_PMU_TXN_ADD transactions. Save the
  1572. * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
  1573. * transactions.
  1574. */
  1575. static void x86_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
  1576. {
  1577. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1578. WARN_ON_ONCE(cpuc->txn_flags); /* txn already in flight */
  1579. cpuc->txn_flags = txn_flags;
  1580. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1581. return;
  1582. perf_pmu_disable(pmu);
  1583. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1584. }
  1585. /*
  1586. * Stop group events scheduling transaction
  1587. * Clear the flag and pmu::enable() will perform the
  1588. * schedulability test.
  1589. */
  1590. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1591. {
  1592. unsigned int txn_flags;
  1593. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1594. WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */
  1595. txn_flags = cpuc->txn_flags;
  1596. cpuc->txn_flags = 0;
  1597. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1598. return;
  1599. /*
  1600. * Truncate collected array by the number of events added in this
  1601. * transaction. See x86_pmu_add() and x86_pmu_*_txn().
  1602. */
  1603. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1604. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1605. perf_pmu_enable(pmu);
  1606. }
  1607. /*
  1608. * Commit group events scheduling transaction
  1609. * Perform the group schedulability test as a whole
  1610. * Return 0 if success
  1611. *
  1612. * Does not cancel the transaction on failure; expects the caller to do this.
  1613. */
  1614. static int x86_pmu_commit_txn(struct pmu *pmu)
  1615. {
  1616. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1617. int assign[X86_PMC_IDX_MAX];
  1618. int n, ret;
  1619. WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */
  1620. if (cpuc->txn_flags & ~PERF_PMU_TXN_ADD) {
  1621. cpuc->txn_flags = 0;
  1622. return 0;
  1623. }
  1624. n = cpuc->n_events;
  1625. if (!x86_pmu_initialized())
  1626. return -EAGAIN;
  1627. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1628. if (ret)
  1629. return ret;
  1630. /*
  1631. * copy new assignment, now we know it is possible
  1632. * will be used by hw_perf_enable()
  1633. */
  1634. memcpy(cpuc->assign, assign, n*sizeof(int));
  1635. cpuc->txn_flags = 0;
  1636. perf_pmu_enable(pmu);
  1637. return 0;
  1638. }
  1639. /*
  1640. * a fake_cpuc is used to validate event groups. Due to
  1641. * the extra reg logic, we need to also allocate a fake
  1642. * per_core and per_cpu structure. Otherwise, group events
  1643. * using extra reg may conflict without the kernel being
  1644. * able to catch this when the last event gets added to
  1645. * the group.
  1646. */
  1647. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1648. {
  1649. kfree(cpuc->shared_regs);
  1650. kfree(cpuc);
  1651. }
  1652. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1653. {
  1654. struct cpu_hw_events *cpuc;
  1655. int cpu = raw_smp_processor_id();
  1656. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1657. if (!cpuc)
  1658. return ERR_PTR(-ENOMEM);
  1659. /* only needed, if we have extra_regs */
  1660. if (x86_pmu.extra_regs) {
  1661. cpuc->shared_regs = allocate_shared_regs(cpu);
  1662. if (!cpuc->shared_regs)
  1663. goto error;
  1664. }
  1665. cpuc->is_fake = 1;
  1666. return cpuc;
  1667. error:
  1668. free_fake_cpuc(cpuc);
  1669. return ERR_PTR(-ENOMEM);
  1670. }
  1671. /*
  1672. * validate that we can schedule this event
  1673. */
  1674. static int validate_event(struct perf_event *event)
  1675. {
  1676. struct cpu_hw_events *fake_cpuc;
  1677. struct event_constraint *c;
  1678. int ret = 0;
  1679. fake_cpuc = allocate_fake_cpuc();
  1680. if (IS_ERR(fake_cpuc))
  1681. return PTR_ERR(fake_cpuc);
  1682. c = x86_pmu.get_event_constraints(fake_cpuc, -1, event);
  1683. if (!c || !c->weight)
  1684. ret = -EINVAL;
  1685. if (x86_pmu.put_event_constraints)
  1686. x86_pmu.put_event_constraints(fake_cpuc, event);
  1687. free_fake_cpuc(fake_cpuc);
  1688. return ret;
  1689. }
  1690. /*
  1691. * validate a single event group
  1692. *
  1693. * validation include:
  1694. * - check events are compatible which each other
  1695. * - events do not compete for the same counter
  1696. * - number of events <= number of counters
  1697. *
  1698. * validation ensures the group can be loaded onto the
  1699. * PMU if it was the only group available.
  1700. */
  1701. static int validate_group(struct perf_event *event)
  1702. {
  1703. struct perf_event *leader = event->group_leader;
  1704. struct cpu_hw_events *fake_cpuc;
  1705. int ret = -EINVAL, n;
  1706. fake_cpuc = allocate_fake_cpuc();
  1707. if (IS_ERR(fake_cpuc))
  1708. return PTR_ERR(fake_cpuc);
  1709. /*
  1710. * the event is not yet connected with its
  1711. * siblings therefore we must first collect
  1712. * existing siblings, then add the new event
  1713. * before we can simulate the scheduling
  1714. */
  1715. n = collect_events(fake_cpuc, leader, true);
  1716. if (n < 0)
  1717. goto out;
  1718. fake_cpuc->n_events = n;
  1719. n = collect_events(fake_cpuc, event, false);
  1720. if (n < 0)
  1721. goto out;
  1722. fake_cpuc->n_events = n;
  1723. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1724. out:
  1725. free_fake_cpuc(fake_cpuc);
  1726. return ret;
  1727. }
  1728. static int x86_pmu_event_init(struct perf_event *event)
  1729. {
  1730. struct pmu *tmp;
  1731. int err;
  1732. switch (event->attr.type) {
  1733. case PERF_TYPE_RAW:
  1734. case PERF_TYPE_HARDWARE:
  1735. case PERF_TYPE_HW_CACHE:
  1736. break;
  1737. default:
  1738. return -ENOENT;
  1739. }
  1740. err = __x86_pmu_event_init(event);
  1741. if (!err) {
  1742. /*
  1743. * we temporarily connect event to its pmu
  1744. * such that validate_group() can classify
  1745. * it as an x86 event using is_x86_event()
  1746. */
  1747. tmp = event->pmu;
  1748. event->pmu = &pmu;
  1749. if (event->group_leader != event)
  1750. err = validate_group(event);
  1751. else
  1752. err = validate_event(event);
  1753. event->pmu = tmp;
  1754. }
  1755. if (err) {
  1756. if (event->destroy)
  1757. event->destroy(event);
  1758. }
  1759. if (READ_ONCE(x86_pmu.attr_rdpmc) &&
  1760. !(event->hw.flags & PERF_X86_EVENT_LARGE_PEBS))
  1761. event->hw.flags |= PERF_X86_EVENT_RDPMC_ALLOWED;
  1762. return err;
  1763. }
  1764. static void refresh_pce(void *ignored)
  1765. {
  1766. load_mm_cr4(this_cpu_read(cpu_tlbstate.loaded_mm));
  1767. }
  1768. static void x86_pmu_event_mapped(struct perf_event *event, struct mm_struct *mm)
  1769. {
  1770. if (!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED))
  1771. return;
  1772. /*
  1773. * This function relies on not being called concurrently in two
  1774. * tasks in the same mm. Otherwise one task could observe
  1775. * perf_rdpmc_allowed > 1 and return all the way back to
  1776. * userspace with CR4.PCE clear while another task is still
  1777. * doing on_each_cpu_mask() to propagate CR4.PCE.
  1778. *
  1779. * For now, this can't happen because all callers hold mmap_sem
  1780. * for write. If this changes, we'll need a different solution.
  1781. */
  1782. lockdep_assert_held_exclusive(&mm->mmap_sem);
  1783. if (atomic_inc_return(&mm->context.perf_rdpmc_allowed) == 1)
  1784. on_each_cpu_mask(mm_cpumask(mm), refresh_pce, NULL, 1);
  1785. }
  1786. static void x86_pmu_event_unmapped(struct perf_event *event, struct mm_struct *mm)
  1787. {
  1788. if (!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED))
  1789. return;
  1790. if (atomic_dec_and_test(&mm->context.perf_rdpmc_allowed))
  1791. on_each_cpu_mask(mm_cpumask(mm), refresh_pce, NULL, 1);
  1792. }
  1793. static int x86_pmu_event_idx(struct perf_event *event)
  1794. {
  1795. int idx = event->hw.idx;
  1796. if (!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED))
  1797. return 0;
  1798. if (x86_pmu.num_counters_fixed && idx >= INTEL_PMC_IDX_FIXED) {
  1799. idx -= INTEL_PMC_IDX_FIXED;
  1800. idx |= 1 << 30;
  1801. }
  1802. return idx + 1;
  1803. }
  1804. static ssize_t get_attr_rdpmc(struct device *cdev,
  1805. struct device_attribute *attr,
  1806. char *buf)
  1807. {
  1808. return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc);
  1809. }
  1810. static ssize_t set_attr_rdpmc(struct device *cdev,
  1811. struct device_attribute *attr,
  1812. const char *buf, size_t count)
  1813. {
  1814. unsigned long val;
  1815. ssize_t ret;
  1816. ret = kstrtoul(buf, 0, &val);
  1817. if (ret)
  1818. return ret;
  1819. if (val > 2)
  1820. return -EINVAL;
  1821. if (x86_pmu.attr_rdpmc_broken)
  1822. return -ENOTSUPP;
  1823. if ((val == 2) != (x86_pmu.attr_rdpmc == 2)) {
  1824. /*
  1825. * Changing into or out of always available, aka
  1826. * perf-event-bypassing mode. This path is extremely slow,
  1827. * but only root can trigger it, so it's okay.
  1828. */
  1829. if (val == 2)
  1830. static_branch_inc(&rdpmc_always_available_key);
  1831. else
  1832. static_branch_dec(&rdpmc_always_available_key);
  1833. on_each_cpu(refresh_pce, NULL, 1);
  1834. }
  1835. x86_pmu.attr_rdpmc = val;
  1836. return count;
  1837. }
  1838. static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc);
  1839. static struct attribute *x86_pmu_attrs[] = {
  1840. &dev_attr_rdpmc.attr,
  1841. NULL,
  1842. };
  1843. static struct attribute_group x86_pmu_attr_group __ro_after_init = {
  1844. .attrs = x86_pmu_attrs,
  1845. };
  1846. static ssize_t max_precise_show(struct device *cdev,
  1847. struct device_attribute *attr,
  1848. char *buf)
  1849. {
  1850. return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu_max_precise());
  1851. }
  1852. static DEVICE_ATTR_RO(max_precise);
  1853. static struct attribute *x86_pmu_caps_attrs[] = {
  1854. &dev_attr_max_precise.attr,
  1855. NULL
  1856. };
  1857. static struct attribute_group x86_pmu_caps_group __ro_after_init = {
  1858. .name = "caps",
  1859. .attrs = x86_pmu_caps_attrs,
  1860. };
  1861. static const struct attribute_group *x86_pmu_attr_groups[] = {
  1862. &x86_pmu_attr_group,
  1863. &x86_pmu_format_group,
  1864. &x86_pmu_events_group,
  1865. &x86_pmu_caps_group,
  1866. NULL,
  1867. };
  1868. static void x86_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
  1869. {
  1870. if (x86_pmu.sched_task)
  1871. x86_pmu.sched_task(ctx, sched_in);
  1872. }
  1873. void perf_check_microcode(void)
  1874. {
  1875. if (x86_pmu.check_microcode)
  1876. x86_pmu.check_microcode();
  1877. }
  1878. static struct pmu pmu = {
  1879. .pmu_enable = x86_pmu_enable,
  1880. .pmu_disable = x86_pmu_disable,
  1881. .attr_groups = x86_pmu_attr_groups,
  1882. .event_init = x86_pmu_event_init,
  1883. .event_mapped = x86_pmu_event_mapped,
  1884. .event_unmapped = x86_pmu_event_unmapped,
  1885. .add = x86_pmu_add,
  1886. .del = x86_pmu_del,
  1887. .start = x86_pmu_start,
  1888. .stop = x86_pmu_stop,
  1889. .read = x86_pmu_read,
  1890. .start_txn = x86_pmu_start_txn,
  1891. .cancel_txn = x86_pmu_cancel_txn,
  1892. .commit_txn = x86_pmu_commit_txn,
  1893. .event_idx = x86_pmu_event_idx,
  1894. .sched_task = x86_pmu_sched_task,
  1895. .task_ctx_size = sizeof(struct x86_perf_task_context),
  1896. };
  1897. void arch_perf_update_userpage(struct perf_event *event,
  1898. struct perf_event_mmap_page *userpg, u64 now)
  1899. {
  1900. struct cyc2ns_data data;
  1901. u64 offset;
  1902. userpg->cap_user_time = 0;
  1903. userpg->cap_user_time_zero = 0;
  1904. userpg->cap_user_rdpmc =
  1905. !!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED);
  1906. userpg->pmc_width = x86_pmu.cntval_bits;
  1907. if (!using_native_sched_clock() || !sched_clock_stable())
  1908. return;
  1909. cyc2ns_read_begin(&data);
  1910. offset = data.cyc2ns_offset + __sched_clock_offset;
  1911. /*
  1912. * Internal timekeeping for enabled/running/stopped times
  1913. * is always in the local_clock domain.
  1914. */
  1915. userpg->cap_user_time = 1;
  1916. userpg->time_mult = data.cyc2ns_mul;
  1917. userpg->time_shift = data.cyc2ns_shift;
  1918. userpg->time_offset = offset - now;
  1919. /*
  1920. * cap_user_time_zero doesn't make sense when we're using a different
  1921. * time base for the records.
  1922. */
  1923. if (!event->attr.use_clockid) {
  1924. userpg->cap_user_time_zero = 1;
  1925. userpg->time_zero = offset;
  1926. }
  1927. cyc2ns_read_end();
  1928. }
  1929. void
  1930. perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  1931. {
  1932. struct unwind_state state;
  1933. unsigned long addr;
  1934. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1935. /* TODO: We don't support guest os callchain now */
  1936. return;
  1937. }
  1938. if (perf_callchain_store(entry, regs->ip))
  1939. return;
  1940. for (unwind_start(&state, current, regs, NULL); !unwind_done(&state);
  1941. unwind_next_frame(&state)) {
  1942. addr = unwind_get_return_address(&state);
  1943. if (!addr || perf_callchain_store(entry, addr))
  1944. return;
  1945. }
  1946. }
  1947. static inline int
  1948. valid_user_frame(const void __user *fp, unsigned long size)
  1949. {
  1950. return (__range_not_ok(fp, size, TASK_SIZE) == 0);
  1951. }
  1952. static unsigned long get_segment_base(unsigned int segment)
  1953. {
  1954. struct desc_struct *desc;
  1955. unsigned int idx = segment >> 3;
  1956. if ((segment & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  1957. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  1958. struct ldt_struct *ldt;
  1959. /* IRQs are off, so this synchronizes with smp_store_release */
  1960. ldt = READ_ONCE(current->active_mm->context.ldt);
  1961. if (!ldt || idx >= ldt->nr_entries)
  1962. return 0;
  1963. desc = &ldt->entries[idx];
  1964. #else
  1965. return 0;
  1966. #endif
  1967. } else {
  1968. if (idx >= GDT_ENTRIES)
  1969. return 0;
  1970. desc = raw_cpu_ptr(gdt_page.gdt) + idx;
  1971. }
  1972. return get_desc_base(desc);
  1973. }
  1974. #ifdef CONFIG_IA32_EMULATION
  1975. #include <linux/compat.h>
  1976. static inline int
  1977. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entry)
  1978. {
  1979. /* 32-bit process in 64-bit kernel. */
  1980. unsigned long ss_base, cs_base;
  1981. struct stack_frame_ia32 frame;
  1982. const void __user *fp;
  1983. if (!test_thread_flag(TIF_IA32))
  1984. return 0;
  1985. cs_base = get_segment_base(regs->cs);
  1986. ss_base = get_segment_base(regs->ss);
  1987. fp = compat_ptr(ss_base + regs->bp);
  1988. pagefault_disable();
  1989. while (entry->nr < entry->max_stack) {
  1990. unsigned long bytes;
  1991. frame.next_frame = 0;
  1992. frame.return_address = 0;
  1993. if (!valid_user_frame(fp, sizeof(frame)))
  1994. break;
  1995. bytes = __copy_from_user_nmi(&frame.next_frame, fp, 4);
  1996. if (bytes != 0)
  1997. break;
  1998. bytes = __copy_from_user_nmi(&frame.return_address, fp+4, 4);
  1999. if (bytes != 0)
  2000. break;
  2001. perf_callchain_store(entry, cs_base + frame.return_address);
  2002. fp = compat_ptr(ss_base + frame.next_frame);
  2003. }
  2004. pagefault_enable();
  2005. return 1;
  2006. }
  2007. #else
  2008. static inline int
  2009. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entry)
  2010. {
  2011. return 0;
  2012. }
  2013. #endif
  2014. void
  2015. perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  2016. {
  2017. struct stack_frame frame;
  2018. const unsigned long __user *fp;
  2019. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  2020. /* TODO: We don't support guest os callchain now */
  2021. return;
  2022. }
  2023. /*
  2024. * We don't know what to do with VM86 stacks.. ignore them for now.
  2025. */
  2026. if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM))
  2027. return;
  2028. fp = (unsigned long __user *)regs->bp;
  2029. perf_callchain_store(entry, regs->ip);
  2030. if (!nmi_uaccess_okay())
  2031. return;
  2032. if (perf_callchain_user32(regs, entry))
  2033. return;
  2034. pagefault_disable();
  2035. while (entry->nr < entry->max_stack) {
  2036. unsigned long bytes;
  2037. frame.next_frame = NULL;
  2038. frame.return_address = 0;
  2039. if (!valid_user_frame(fp, sizeof(frame)))
  2040. break;
  2041. bytes = __copy_from_user_nmi(&frame.next_frame, fp, sizeof(*fp));
  2042. if (bytes != 0)
  2043. break;
  2044. bytes = __copy_from_user_nmi(&frame.return_address, fp + 1, sizeof(*fp));
  2045. if (bytes != 0)
  2046. break;
  2047. perf_callchain_store(entry, frame.return_address);
  2048. fp = (void __user *)frame.next_frame;
  2049. }
  2050. pagefault_enable();
  2051. }
  2052. /*
  2053. * Deal with code segment offsets for the various execution modes:
  2054. *
  2055. * VM86 - the good olde 16 bit days, where the linear address is
  2056. * 20 bits and we use regs->ip + 0x10 * regs->cs.
  2057. *
  2058. * IA32 - Where we need to look at GDT/LDT segment descriptor tables
  2059. * to figure out what the 32bit base address is.
  2060. *
  2061. * X32 - has TIF_X32 set, but is running in x86_64
  2062. *
  2063. * X86_64 - CS,DS,SS,ES are all zero based.
  2064. */
  2065. static unsigned long code_segment_base(struct pt_regs *regs)
  2066. {
  2067. /*
  2068. * For IA32 we look at the GDT/LDT segment base to convert the
  2069. * effective IP to a linear address.
  2070. */
  2071. #ifdef CONFIG_X86_32
  2072. /*
  2073. * If we are in VM86 mode, add the segment offset to convert to a
  2074. * linear address.
  2075. */
  2076. if (regs->flags & X86_VM_MASK)
  2077. return 0x10 * regs->cs;
  2078. if (user_mode(regs) && regs->cs != __USER_CS)
  2079. return get_segment_base(regs->cs);
  2080. #else
  2081. if (user_mode(regs) && !user_64bit_mode(regs) &&
  2082. regs->cs != __USER32_CS)
  2083. return get_segment_base(regs->cs);
  2084. #endif
  2085. return 0;
  2086. }
  2087. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  2088. {
  2089. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  2090. return perf_guest_cbs->get_guest_ip();
  2091. return regs->ip + code_segment_base(regs);
  2092. }
  2093. unsigned long perf_misc_flags(struct pt_regs *regs)
  2094. {
  2095. int misc = 0;
  2096. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  2097. if (perf_guest_cbs->is_user_mode())
  2098. misc |= PERF_RECORD_MISC_GUEST_USER;
  2099. else
  2100. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  2101. } else {
  2102. if (user_mode(regs))
  2103. misc |= PERF_RECORD_MISC_USER;
  2104. else
  2105. misc |= PERF_RECORD_MISC_KERNEL;
  2106. }
  2107. if (regs->flags & PERF_EFLAGS_EXACT)
  2108. misc |= PERF_RECORD_MISC_EXACT_IP;
  2109. return misc;
  2110. }
  2111. void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
  2112. {
  2113. cap->version = x86_pmu.version;
  2114. cap->num_counters_gp = x86_pmu.num_counters;
  2115. cap->num_counters_fixed = x86_pmu.num_counters_fixed;
  2116. cap->bit_width_gp = x86_pmu.cntval_bits;
  2117. cap->bit_width_fixed = x86_pmu.cntval_bits;
  2118. cap->events_mask = (unsigned int)x86_pmu.events_maskl;
  2119. cap->events_mask_len = x86_pmu.events_mask_len;
  2120. }
  2121. EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability);