perf_event.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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 <pzijlstr@redhat.com>
  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/module.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/sched.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/highmem.h>
  25. #include <linux/cpu.h>
  26. #include <linux/bitops.h>
  27. #include <asm/apic.h>
  28. #include <asm/stacktrace.h>
  29. #include <asm/nmi.h>
  30. #include <asm/compat.h>
  31. #include <asm/smp.h>
  32. #include <asm/alternative.h>
  33. #if 0
  34. #undef wrmsrl
  35. #define wrmsrl(msr, val) \
  36. do { \
  37. trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
  38. (unsigned long)(val)); \
  39. native_write_msr((msr), (u32)((u64)(val)), \
  40. (u32)((u64)(val) >> 32)); \
  41. } while (0)
  42. #endif
  43. /*
  44. * | NHM/WSM | SNB |
  45. * register -------------------------------
  46. * | HT | no HT | HT | no HT |
  47. *-----------------------------------------
  48. * offcore | core | core | cpu | core |
  49. * lbr_sel | core | core | cpu | core |
  50. * ld_lat | cpu | core | cpu | core |
  51. *-----------------------------------------
  52. *
  53. * Given that there is a small number of shared regs,
  54. * we can pre-allocate their slot in the per-cpu
  55. * per-core reg tables.
  56. */
  57. enum extra_reg_type {
  58. EXTRA_REG_NONE = -1, /* not used */
  59. EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */
  60. EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */
  61. EXTRA_REG_MAX /* number of entries needed */
  62. };
  63. /*
  64. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  65. */
  66. static unsigned long
  67. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  68. {
  69. unsigned long offset, addr = (unsigned long)from;
  70. unsigned long size, len = 0;
  71. struct page *page;
  72. void *map;
  73. int ret;
  74. do {
  75. ret = __get_user_pages_fast(addr, 1, 0, &page);
  76. if (!ret)
  77. break;
  78. offset = addr & (PAGE_SIZE - 1);
  79. size = min(PAGE_SIZE - offset, n - len);
  80. map = kmap_atomic(page);
  81. memcpy(to, map+offset, size);
  82. kunmap_atomic(map);
  83. put_page(page);
  84. len += size;
  85. to += size;
  86. addr += size;
  87. } while (len < n);
  88. return len;
  89. }
  90. struct event_constraint {
  91. union {
  92. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  93. u64 idxmsk64;
  94. };
  95. u64 code;
  96. u64 cmask;
  97. int weight;
  98. };
  99. struct amd_nb {
  100. int nb_id; /* NorthBridge id */
  101. int refcnt; /* reference count */
  102. struct perf_event *owners[X86_PMC_IDX_MAX];
  103. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  104. };
  105. struct intel_percore;
  106. #define MAX_LBR_ENTRIES 16
  107. struct cpu_hw_events {
  108. /*
  109. * Generic x86 PMC bits
  110. */
  111. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  112. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  113. unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  114. int enabled;
  115. int n_events;
  116. int n_added;
  117. int n_txn;
  118. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  119. u64 tags[X86_PMC_IDX_MAX];
  120. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  121. unsigned int group_flag;
  122. /*
  123. * Intel DebugStore bits
  124. */
  125. struct debug_store *ds;
  126. u64 pebs_enabled;
  127. /*
  128. * Intel LBR bits
  129. */
  130. int lbr_users;
  131. void *lbr_context;
  132. struct perf_branch_stack lbr_stack;
  133. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  134. /*
  135. * manage shared (per-core, per-cpu) registers
  136. * used on Intel NHM/WSM/SNB
  137. */
  138. struct intel_shared_regs *shared_regs;
  139. /*
  140. * AMD specific bits
  141. */
  142. struct amd_nb *amd_nb;
  143. };
  144. #define __EVENT_CONSTRAINT(c, n, m, w) {\
  145. { .idxmsk64 = (n) }, \
  146. .code = (c), \
  147. .cmask = (m), \
  148. .weight = (w), \
  149. }
  150. #define EVENT_CONSTRAINT(c, n, m) \
  151. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
  152. /*
  153. * Constraint on the Event code.
  154. */
  155. #define INTEL_EVENT_CONSTRAINT(c, n) \
  156. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  157. /*
  158. * Constraint on the Event code + UMask + fixed-mask
  159. *
  160. * filter mask to validate fixed counter events.
  161. * the following filters disqualify for fixed counters:
  162. * - inv
  163. * - edge
  164. * - cnt-mask
  165. * The other filters are supported by fixed counters.
  166. * The any-thread option is supported starting with v3.
  167. */
  168. #define FIXED_EVENT_CONSTRAINT(c, n) \
  169. EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
  170. /*
  171. * Constraint on the Event code + UMask
  172. */
  173. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  174. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  175. #define EVENT_CONSTRAINT_END \
  176. EVENT_CONSTRAINT(0, 0, 0)
  177. #define for_each_event_constraint(e, c) \
  178. for ((e) = (c); (e)->weight; (e)++)
  179. /*
  180. * Per register state.
  181. */
  182. struct er_account {
  183. raw_spinlock_t lock; /* per-core: protect structure */
  184. u64 config; /* extra MSR config */
  185. u64 reg; /* extra MSR number */
  186. atomic_t ref; /* reference count */
  187. };
  188. /*
  189. * Extra registers for specific events.
  190. *
  191. * Some events need large masks and require external MSRs.
  192. * Those extra MSRs end up being shared for all events on
  193. * a PMU and sometimes between PMU of sibling HT threads.
  194. * In either case, the kernel needs to handle conflicting
  195. * accesses to those extra, shared, regs. The data structure
  196. * to manage those registers is stored in cpu_hw_event.
  197. */
  198. struct extra_reg {
  199. unsigned int event;
  200. unsigned int msr;
  201. u64 config_mask;
  202. u64 valid_mask;
  203. int idx; /* per_xxx->regs[] reg index */
  204. };
  205. #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
  206. .event = (e), \
  207. .msr = (ms), \
  208. .config_mask = (m), \
  209. .valid_mask = (vm), \
  210. .idx = EXTRA_REG_##i \
  211. }
  212. #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \
  213. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
  214. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
  215. union perf_capabilities {
  216. struct {
  217. u64 lbr_format : 6;
  218. u64 pebs_trap : 1;
  219. u64 pebs_arch_reg : 1;
  220. u64 pebs_format : 4;
  221. u64 smm_freeze : 1;
  222. };
  223. u64 capabilities;
  224. };
  225. /*
  226. * struct x86_pmu - generic x86 pmu
  227. */
  228. struct x86_pmu {
  229. /*
  230. * Generic x86 PMC bits
  231. */
  232. const char *name;
  233. int version;
  234. int (*handle_irq)(struct pt_regs *);
  235. void (*disable_all)(void);
  236. void (*enable_all)(int added);
  237. void (*enable)(struct perf_event *);
  238. void (*disable)(struct perf_event *);
  239. int (*hw_config)(struct perf_event *event);
  240. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  241. unsigned eventsel;
  242. unsigned perfctr;
  243. u64 (*event_map)(int);
  244. int max_events;
  245. int num_counters;
  246. int num_counters_fixed;
  247. int cntval_bits;
  248. u64 cntval_mask;
  249. int apic;
  250. u64 max_period;
  251. struct event_constraint *
  252. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  253. struct perf_event *event);
  254. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  255. struct perf_event *event);
  256. struct event_constraint *event_constraints;
  257. void (*quirks)(void);
  258. int perfctr_second_write;
  259. int (*cpu_prepare)(int cpu);
  260. void (*cpu_starting)(int cpu);
  261. void (*cpu_dying)(int cpu);
  262. void (*cpu_dead)(int cpu);
  263. /*
  264. * Intel Arch Perfmon v2+
  265. */
  266. u64 intel_ctrl;
  267. union perf_capabilities intel_cap;
  268. /*
  269. * Intel DebugStore bits
  270. */
  271. int bts, pebs;
  272. int bts_active, pebs_active;
  273. int pebs_record_size;
  274. void (*drain_pebs)(struct pt_regs *regs);
  275. struct event_constraint *pebs_constraints;
  276. /*
  277. * Intel LBR
  278. */
  279. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  280. int lbr_nr; /* hardware stack size */
  281. /*
  282. * Extra registers for events
  283. */
  284. struct extra_reg *extra_regs;
  285. unsigned int er_flags;
  286. };
  287. #define ERF_NO_HT_SHARING 1
  288. #define ERF_HAS_RSP_1 2
  289. static struct x86_pmu x86_pmu __read_mostly;
  290. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  291. .enabled = 1,
  292. };
  293. static int x86_perf_event_set_period(struct perf_event *event);
  294. /*
  295. * Generalized hw caching related hw_event table, filled
  296. * in on a per model basis. A value of 0 means
  297. * 'not supported', -1 means 'hw_event makes no sense on
  298. * this CPU', any other value means the raw hw_event
  299. * ID.
  300. */
  301. #define C(x) PERF_COUNT_HW_CACHE_##x
  302. static u64 __read_mostly hw_cache_event_ids
  303. [PERF_COUNT_HW_CACHE_MAX]
  304. [PERF_COUNT_HW_CACHE_OP_MAX]
  305. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  306. static u64 __read_mostly hw_cache_extra_regs
  307. [PERF_COUNT_HW_CACHE_MAX]
  308. [PERF_COUNT_HW_CACHE_OP_MAX]
  309. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  310. /*
  311. * Propagate event elapsed time into the generic event.
  312. * Can only be executed on the CPU where the event is active.
  313. * Returns the delta events processed.
  314. */
  315. static u64
  316. x86_perf_event_update(struct perf_event *event)
  317. {
  318. struct hw_perf_event *hwc = &event->hw;
  319. int shift = 64 - x86_pmu.cntval_bits;
  320. u64 prev_raw_count, new_raw_count;
  321. int idx = hwc->idx;
  322. s64 delta;
  323. if (idx == X86_PMC_IDX_FIXED_BTS)
  324. return 0;
  325. /*
  326. * Careful: an NMI might modify the previous event value.
  327. *
  328. * Our tactic to handle this is to first atomically read and
  329. * exchange a new raw count - then add that new-prev delta
  330. * count to the generic event atomically:
  331. */
  332. again:
  333. prev_raw_count = local64_read(&hwc->prev_count);
  334. rdmsrl(hwc->event_base, new_raw_count);
  335. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  336. new_raw_count) != prev_raw_count)
  337. goto again;
  338. /*
  339. * Now we have the new raw value and have updated the prev
  340. * timestamp already. We can now calculate the elapsed delta
  341. * (event-)time and add that to the generic event.
  342. *
  343. * Careful, not all hw sign-extends above the physical width
  344. * of the count.
  345. */
  346. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  347. delta >>= shift;
  348. local64_add(delta, &event->count);
  349. local64_sub(delta, &hwc->period_left);
  350. return new_raw_count;
  351. }
  352. static inline int x86_pmu_addr_offset(int index)
  353. {
  354. int offset;
  355. /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
  356. alternative_io(ASM_NOP2,
  357. "shll $1, %%eax",
  358. X86_FEATURE_PERFCTR_CORE,
  359. "=a" (offset),
  360. "a" (index));
  361. return offset;
  362. }
  363. static inline unsigned int x86_pmu_config_addr(int index)
  364. {
  365. return x86_pmu.eventsel + x86_pmu_addr_offset(index);
  366. }
  367. static inline unsigned int x86_pmu_event_addr(int index)
  368. {
  369. return x86_pmu.perfctr + x86_pmu_addr_offset(index);
  370. }
  371. /*
  372. * Find and validate any extra registers to set up.
  373. */
  374. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  375. {
  376. struct hw_perf_event_extra *reg;
  377. struct extra_reg *er;
  378. reg = &event->hw.extra_reg;
  379. if (!x86_pmu.extra_regs)
  380. return 0;
  381. for (er = x86_pmu.extra_regs; er->msr; er++) {
  382. if (er->event != (config & er->config_mask))
  383. continue;
  384. if (event->attr.config1 & ~er->valid_mask)
  385. return -EINVAL;
  386. reg->idx = er->idx;
  387. reg->config = event->attr.config1;
  388. reg->reg = er->msr;
  389. break;
  390. }
  391. return 0;
  392. }
  393. static atomic_t active_events;
  394. static DEFINE_MUTEX(pmc_reserve_mutex);
  395. #ifdef CONFIG_X86_LOCAL_APIC
  396. static bool reserve_pmc_hardware(void)
  397. {
  398. int i;
  399. for (i = 0; i < x86_pmu.num_counters; i++) {
  400. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  401. goto perfctr_fail;
  402. }
  403. for (i = 0; i < x86_pmu.num_counters; i++) {
  404. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  405. goto eventsel_fail;
  406. }
  407. return true;
  408. eventsel_fail:
  409. for (i--; i >= 0; i--)
  410. release_evntsel_nmi(x86_pmu_config_addr(i));
  411. i = x86_pmu.num_counters;
  412. perfctr_fail:
  413. for (i--; i >= 0; i--)
  414. release_perfctr_nmi(x86_pmu_event_addr(i));
  415. return false;
  416. }
  417. static void release_pmc_hardware(void)
  418. {
  419. int i;
  420. for (i = 0; i < x86_pmu.num_counters; i++) {
  421. release_perfctr_nmi(x86_pmu_event_addr(i));
  422. release_evntsel_nmi(x86_pmu_config_addr(i));
  423. }
  424. }
  425. #else
  426. static bool reserve_pmc_hardware(void) { return true; }
  427. static void release_pmc_hardware(void) {}
  428. #endif
  429. static bool check_hw_exists(void)
  430. {
  431. u64 val, val_new = 0;
  432. int i, reg, ret = 0;
  433. /*
  434. * Check to see if the BIOS enabled any of the counters, if so
  435. * complain and bail.
  436. */
  437. for (i = 0; i < x86_pmu.num_counters; i++) {
  438. reg = x86_pmu_config_addr(i);
  439. ret = rdmsrl_safe(reg, &val);
  440. if (ret)
  441. goto msr_fail;
  442. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  443. goto bios_fail;
  444. }
  445. if (x86_pmu.num_counters_fixed) {
  446. reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  447. ret = rdmsrl_safe(reg, &val);
  448. if (ret)
  449. goto msr_fail;
  450. for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
  451. if (val & (0x03 << i*4))
  452. goto bios_fail;
  453. }
  454. }
  455. /*
  456. * Now write a value and read it back to see if it matches,
  457. * this is needed to detect certain hardware emulators (qemu/kvm)
  458. * that don't trap on the MSR access and always return 0s.
  459. */
  460. val = 0xabcdUL;
  461. ret = checking_wrmsrl(x86_pmu_event_addr(0), val);
  462. ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new);
  463. if (ret || val != val_new)
  464. goto msr_fail;
  465. return true;
  466. bios_fail:
  467. /*
  468. * We still allow the PMU driver to operate:
  469. */
  470. printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
  471. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
  472. return true;
  473. msr_fail:
  474. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  475. return false;
  476. }
  477. static void reserve_ds_buffers(void);
  478. static void release_ds_buffers(void);
  479. static void hw_perf_event_destroy(struct perf_event *event)
  480. {
  481. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  482. release_pmc_hardware();
  483. release_ds_buffers();
  484. mutex_unlock(&pmc_reserve_mutex);
  485. }
  486. }
  487. static inline int x86_pmu_initialized(void)
  488. {
  489. return x86_pmu.handle_irq != NULL;
  490. }
  491. static inline int
  492. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  493. {
  494. struct perf_event_attr *attr = &event->attr;
  495. unsigned int cache_type, cache_op, cache_result;
  496. u64 config, val;
  497. config = attr->config;
  498. cache_type = (config >> 0) & 0xff;
  499. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  500. return -EINVAL;
  501. cache_op = (config >> 8) & 0xff;
  502. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  503. return -EINVAL;
  504. cache_result = (config >> 16) & 0xff;
  505. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  506. return -EINVAL;
  507. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  508. if (val == 0)
  509. return -ENOENT;
  510. if (val == -1)
  511. return -EINVAL;
  512. hwc->config |= val;
  513. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  514. return x86_pmu_extra_regs(val, event);
  515. }
  516. static int x86_setup_perfctr(struct perf_event *event)
  517. {
  518. struct perf_event_attr *attr = &event->attr;
  519. struct hw_perf_event *hwc = &event->hw;
  520. u64 config;
  521. if (!is_sampling_event(event)) {
  522. hwc->sample_period = x86_pmu.max_period;
  523. hwc->last_period = hwc->sample_period;
  524. local64_set(&hwc->period_left, hwc->sample_period);
  525. } else {
  526. /*
  527. * If we have a PMU initialized but no APIC
  528. * interrupts, we cannot sample hardware
  529. * events (user-space has to fall back and
  530. * sample via a hrtimer based software event):
  531. */
  532. if (!x86_pmu.apic)
  533. return -EOPNOTSUPP;
  534. }
  535. /*
  536. * Do not allow config1 (extended registers) to propagate,
  537. * there's no sane user-space generalization yet:
  538. */
  539. if (attr->type == PERF_TYPE_RAW)
  540. return 0;
  541. if (attr->type == PERF_TYPE_HW_CACHE)
  542. return set_ext_hw_attr(hwc, event);
  543. if (attr->config >= x86_pmu.max_events)
  544. return -EINVAL;
  545. /*
  546. * The generic map:
  547. */
  548. config = x86_pmu.event_map(attr->config);
  549. if (config == 0)
  550. return -ENOENT;
  551. if (config == -1LL)
  552. return -EINVAL;
  553. /*
  554. * Branch tracing:
  555. */
  556. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  557. !attr->freq && hwc->sample_period == 1) {
  558. /* BTS is not supported by this architecture. */
  559. if (!x86_pmu.bts_active)
  560. return -EOPNOTSUPP;
  561. /* BTS is currently only allowed for user-mode. */
  562. if (!attr->exclude_kernel)
  563. return -EOPNOTSUPP;
  564. }
  565. hwc->config |= config;
  566. return 0;
  567. }
  568. static int x86_pmu_hw_config(struct perf_event *event)
  569. {
  570. if (event->attr.precise_ip) {
  571. int precise = 0;
  572. /* Support for constant skid */
  573. if (x86_pmu.pebs_active) {
  574. precise++;
  575. /* Support for IP fixup */
  576. if (x86_pmu.lbr_nr)
  577. precise++;
  578. }
  579. if (event->attr.precise_ip > precise)
  580. return -EOPNOTSUPP;
  581. }
  582. /*
  583. * Generate PMC IRQs:
  584. * (keep 'enabled' bit clear for now)
  585. */
  586. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  587. /*
  588. * Count user and OS events unless requested not to
  589. */
  590. if (!event->attr.exclude_user)
  591. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  592. if (!event->attr.exclude_kernel)
  593. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  594. if (event->attr.type == PERF_TYPE_RAW)
  595. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  596. return x86_setup_perfctr(event);
  597. }
  598. /*
  599. * Setup the hardware configuration for a given attr_type
  600. */
  601. static int __x86_pmu_event_init(struct perf_event *event)
  602. {
  603. int err;
  604. if (!x86_pmu_initialized())
  605. return -ENODEV;
  606. err = 0;
  607. if (!atomic_inc_not_zero(&active_events)) {
  608. mutex_lock(&pmc_reserve_mutex);
  609. if (atomic_read(&active_events) == 0) {
  610. if (!reserve_pmc_hardware())
  611. err = -EBUSY;
  612. else
  613. reserve_ds_buffers();
  614. }
  615. if (!err)
  616. atomic_inc(&active_events);
  617. mutex_unlock(&pmc_reserve_mutex);
  618. }
  619. if (err)
  620. return err;
  621. event->destroy = hw_perf_event_destroy;
  622. event->hw.idx = -1;
  623. event->hw.last_cpu = -1;
  624. event->hw.last_tag = ~0ULL;
  625. /* mark unused */
  626. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  627. return x86_pmu.hw_config(event);
  628. }
  629. static void x86_pmu_disable_all(void)
  630. {
  631. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  632. int idx;
  633. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  634. u64 val;
  635. if (!test_bit(idx, cpuc->active_mask))
  636. continue;
  637. rdmsrl(x86_pmu_config_addr(idx), val);
  638. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  639. continue;
  640. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  641. wrmsrl(x86_pmu_config_addr(idx), val);
  642. }
  643. }
  644. static void x86_pmu_disable(struct pmu *pmu)
  645. {
  646. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  647. if (!x86_pmu_initialized())
  648. return;
  649. if (!cpuc->enabled)
  650. return;
  651. cpuc->n_added = 0;
  652. cpuc->enabled = 0;
  653. barrier();
  654. x86_pmu.disable_all();
  655. }
  656. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  657. u64 enable_mask)
  658. {
  659. if (hwc->extra_reg.reg)
  660. wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
  661. wrmsrl(hwc->config_base, hwc->config | enable_mask);
  662. }
  663. static void x86_pmu_enable_all(int added)
  664. {
  665. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  666. int idx;
  667. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  668. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  669. if (!test_bit(idx, cpuc->active_mask))
  670. continue;
  671. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  672. }
  673. }
  674. static struct pmu pmu;
  675. static inline int is_x86_event(struct perf_event *event)
  676. {
  677. return event->pmu == &pmu;
  678. }
  679. static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  680. {
  681. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  682. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  683. int i, j, w, wmax, num = 0;
  684. struct hw_perf_event *hwc;
  685. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  686. for (i = 0; i < n; i++) {
  687. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  688. constraints[i] = c;
  689. }
  690. /*
  691. * fastpath, try to reuse previous register
  692. */
  693. for (i = 0; i < n; i++) {
  694. hwc = &cpuc->event_list[i]->hw;
  695. c = constraints[i];
  696. /* never assigned */
  697. if (hwc->idx == -1)
  698. break;
  699. /* constraint still honored */
  700. if (!test_bit(hwc->idx, c->idxmsk))
  701. break;
  702. /* not already used */
  703. if (test_bit(hwc->idx, used_mask))
  704. break;
  705. __set_bit(hwc->idx, used_mask);
  706. if (assign)
  707. assign[i] = hwc->idx;
  708. }
  709. if (i == n)
  710. goto done;
  711. /*
  712. * begin slow path
  713. */
  714. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  715. /*
  716. * weight = number of possible counters
  717. *
  718. * 1 = most constrained, only works on one counter
  719. * wmax = least constrained, works on any counter
  720. *
  721. * assign events to counters starting with most
  722. * constrained events.
  723. */
  724. wmax = x86_pmu.num_counters;
  725. /*
  726. * when fixed event counters are present,
  727. * wmax is incremented by 1 to account
  728. * for one more choice
  729. */
  730. if (x86_pmu.num_counters_fixed)
  731. wmax++;
  732. for (w = 1, num = n; num && w <= wmax; w++) {
  733. /* for each event */
  734. for (i = 0; num && i < n; i++) {
  735. c = constraints[i];
  736. hwc = &cpuc->event_list[i]->hw;
  737. if (c->weight != w)
  738. continue;
  739. for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
  740. if (!test_bit(j, used_mask))
  741. break;
  742. }
  743. if (j == X86_PMC_IDX_MAX)
  744. break;
  745. __set_bit(j, used_mask);
  746. if (assign)
  747. assign[i] = j;
  748. num--;
  749. }
  750. }
  751. done:
  752. /*
  753. * scheduling failed or is just a simulation,
  754. * free resources if necessary
  755. */
  756. if (!assign || num) {
  757. for (i = 0; i < n; i++) {
  758. if (x86_pmu.put_event_constraints)
  759. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  760. }
  761. }
  762. return num ? -ENOSPC : 0;
  763. }
  764. /*
  765. * dogrp: true if must collect siblings events (group)
  766. * returns total number of events and error code
  767. */
  768. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  769. {
  770. struct perf_event *event;
  771. int n, max_count;
  772. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  773. /* current number of events already accepted */
  774. n = cpuc->n_events;
  775. if (is_x86_event(leader)) {
  776. if (n >= max_count)
  777. return -ENOSPC;
  778. cpuc->event_list[n] = leader;
  779. n++;
  780. }
  781. if (!dogrp)
  782. return n;
  783. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  784. if (!is_x86_event(event) ||
  785. event->state <= PERF_EVENT_STATE_OFF)
  786. continue;
  787. if (n >= max_count)
  788. return -ENOSPC;
  789. cpuc->event_list[n] = event;
  790. n++;
  791. }
  792. return n;
  793. }
  794. static inline void x86_assign_hw_event(struct perf_event *event,
  795. struct cpu_hw_events *cpuc, int i)
  796. {
  797. struct hw_perf_event *hwc = &event->hw;
  798. hwc->idx = cpuc->assign[i];
  799. hwc->last_cpu = smp_processor_id();
  800. hwc->last_tag = ++cpuc->tags[i];
  801. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  802. hwc->config_base = 0;
  803. hwc->event_base = 0;
  804. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  805. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  806. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED);
  807. } else {
  808. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  809. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  810. }
  811. }
  812. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  813. struct cpu_hw_events *cpuc,
  814. int i)
  815. {
  816. return hwc->idx == cpuc->assign[i] &&
  817. hwc->last_cpu == smp_processor_id() &&
  818. hwc->last_tag == cpuc->tags[i];
  819. }
  820. static void x86_pmu_start(struct perf_event *event, int flags);
  821. static void x86_pmu_stop(struct perf_event *event, int flags);
  822. static void x86_pmu_enable(struct pmu *pmu)
  823. {
  824. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  825. struct perf_event *event;
  826. struct hw_perf_event *hwc;
  827. int i, added = cpuc->n_added;
  828. if (!x86_pmu_initialized())
  829. return;
  830. if (cpuc->enabled)
  831. return;
  832. if (cpuc->n_added) {
  833. int n_running = cpuc->n_events - cpuc->n_added;
  834. /*
  835. * apply assignment obtained either from
  836. * hw_perf_group_sched_in() or x86_pmu_enable()
  837. *
  838. * step1: save events moving to new counters
  839. * step2: reprogram moved events into new counters
  840. */
  841. for (i = 0; i < n_running; i++) {
  842. event = cpuc->event_list[i];
  843. hwc = &event->hw;
  844. /*
  845. * we can avoid reprogramming counter if:
  846. * - assigned same counter as last time
  847. * - running on same CPU as last time
  848. * - no other event has used the counter since
  849. */
  850. if (hwc->idx == -1 ||
  851. match_prev_assignment(hwc, cpuc, i))
  852. continue;
  853. /*
  854. * Ensure we don't accidentally enable a stopped
  855. * counter simply because we rescheduled.
  856. */
  857. if (hwc->state & PERF_HES_STOPPED)
  858. hwc->state |= PERF_HES_ARCH;
  859. x86_pmu_stop(event, PERF_EF_UPDATE);
  860. }
  861. for (i = 0; i < cpuc->n_events; i++) {
  862. event = cpuc->event_list[i];
  863. hwc = &event->hw;
  864. if (!match_prev_assignment(hwc, cpuc, i))
  865. x86_assign_hw_event(event, cpuc, i);
  866. else if (i < n_running)
  867. continue;
  868. if (hwc->state & PERF_HES_ARCH)
  869. continue;
  870. x86_pmu_start(event, PERF_EF_RELOAD);
  871. }
  872. cpuc->n_added = 0;
  873. perf_events_lapic_init();
  874. }
  875. cpuc->enabled = 1;
  876. barrier();
  877. x86_pmu.enable_all(added);
  878. }
  879. static inline void x86_pmu_disable_event(struct perf_event *event)
  880. {
  881. struct hw_perf_event *hwc = &event->hw;
  882. wrmsrl(hwc->config_base, hwc->config);
  883. }
  884. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  885. /*
  886. * Set the next IRQ period, based on the hwc->period_left value.
  887. * To be called with the event disabled in hw:
  888. */
  889. static int
  890. x86_perf_event_set_period(struct perf_event *event)
  891. {
  892. struct hw_perf_event *hwc = &event->hw;
  893. s64 left = local64_read(&hwc->period_left);
  894. s64 period = hwc->sample_period;
  895. int ret = 0, idx = hwc->idx;
  896. if (idx == X86_PMC_IDX_FIXED_BTS)
  897. return 0;
  898. /*
  899. * If we are way outside a reasonable range then just skip forward:
  900. */
  901. if (unlikely(left <= -period)) {
  902. left = period;
  903. local64_set(&hwc->period_left, left);
  904. hwc->last_period = period;
  905. ret = 1;
  906. }
  907. if (unlikely(left <= 0)) {
  908. left += period;
  909. local64_set(&hwc->period_left, left);
  910. hwc->last_period = period;
  911. ret = 1;
  912. }
  913. /*
  914. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  915. */
  916. if (unlikely(left < 2))
  917. left = 2;
  918. if (left > x86_pmu.max_period)
  919. left = x86_pmu.max_period;
  920. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  921. /*
  922. * The hw event starts counting from this event offset,
  923. * mark it to be able to extra future deltas:
  924. */
  925. local64_set(&hwc->prev_count, (u64)-left);
  926. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  927. /*
  928. * Due to erratum on certan cpu we need
  929. * a second write to be sure the register
  930. * is updated properly
  931. */
  932. if (x86_pmu.perfctr_second_write) {
  933. wrmsrl(hwc->event_base,
  934. (u64)(-left) & x86_pmu.cntval_mask);
  935. }
  936. perf_event_update_userpage(event);
  937. return ret;
  938. }
  939. static void x86_pmu_enable_event(struct perf_event *event)
  940. {
  941. if (__this_cpu_read(cpu_hw_events.enabled))
  942. __x86_pmu_enable_event(&event->hw,
  943. ARCH_PERFMON_EVENTSEL_ENABLE);
  944. }
  945. /*
  946. * Add a single event to the PMU.
  947. *
  948. * The event is added to the group of enabled events
  949. * but only if it can be scehduled with existing events.
  950. */
  951. static int x86_pmu_add(struct perf_event *event, int flags)
  952. {
  953. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  954. struct hw_perf_event *hwc;
  955. int assign[X86_PMC_IDX_MAX];
  956. int n, n0, ret;
  957. hwc = &event->hw;
  958. perf_pmu_disable(event->pmu);
  959. n0 = cpuc->n_events;
  960. ret = n = collect_events(cpuc, event, false);
  961. if (ret < 0)
  962. goto out;
  963. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  964. if (!(flags & PERF_EF_START))
  965. hwc->state |= PERF_HES_ARCH;
  966. /*
  967. * If group events scheduling transaction was started,
  968. * skip the schedulability test here, it will be performed
  969. * at commit time (->commit_txn) as a whole
  970. */
  971. if (cpuc->group_flag & PERF_EVENT_TXN)
  972. goto done_collect;
  973. ret = x86_pmu.schedule_events(cpuc, n, assign);
  974. if (ret)
  975. goto out;
  976. /*
  977. * copy new assignment, now we know it is possible
  978. * will be used by hw_perf_enable()
  979. */
  980. memcpy(cpuc->assign, assign, n*sizeof(int));
  981. done_collect:
  982. cpuc->n_events = n;
  983. cpuc->n_added += n - n0;
  984. cpuc->n_txn += n - n0;
  985. ret = 0;
  986. out:
  987. perf_pmu_enable(event->pmu);
  988. return ret;
  989. }
  990. static void x86_pmu_start(struct perf_event *event, int flags)
  991. {
  992. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  993. int idx = event->hw.idx;
  994. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  995. return;
  996. if (WARN_ON_ONCE(idx == -1))
  997. return;
  998. if (flags & PERF_EF_RELOAD) {
  999. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  1000. x86_perf_event_set_period(event);
  1001. }
  1002. event->hw.state = 0;
  1003. cpuc->events[idx] = event;
  1004. __set_bit(idx, cpuc->active_mask);
  1005. __set_bit(idx, cpuc->running);
  1006. x86_pmu.enable(event);
  1007. perf_event_update_userpage(event);
  1008. }
  1009. void perf_event_print_debug(void)
  1010. {
  1011. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  1012. u64 pebs;
  1013. struct cpu_hw_events *cpuc;
  1014. unsigned long flags;
  1015. int cpu, idx;
  1016. if (!x86_pmu.num_counters)
  1017. return;
  1018. local_irq_save(flags);
  1019. cpu = smp_processor_id();
  1020. cpuc = &per_cpu(cpu_hw_events, cpu);
  1021. if (x86_pmu.version >= 2) {
  1022. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  1023. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1024. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  1025. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  1026. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  1027. pr_info("\n");
  1028. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  1029. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  1030. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  1031. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  1032. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  1033. }
  1034. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  1035. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1036. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  1037. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  1038. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  1039. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  1040. cpu, idx, pmc_ctrl);
  1041. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  1042. cpu, idx, pmc_count);
  1043. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  1044. cpu, idx, prev_left);
  1045. }
  1046. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1047. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1048. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1049. cpu, idx, pmc_count);
  1050. }
  1051. local_irq_restore(flags);
  1052. }
  1053. static void x86_pmu_stop(struct perf_event *event, int flags)
  1054. {
  1055. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1056. struct hw_perf_event *hwc = &event->hw;
  1057. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  1058. x86_pmu.disable(event);
  1059. cpuc->events[hwc->idx] = NULL;
  1060. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  1061. hwc->state |= PERF_HES_STOPPED;
  1062. }
  1063. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  1064. /*
  1065. * Drain the remaining delta count out of a event
  1066. * that we are disabling:
  1067. */
  1068. x86_perf_event_update(event);
  1069. hwc->state |= PERF_HES_UPTODATE;
  1070. }
  1071. }
  1072. static void x86_pmu_del(struct perf_event *event, int flags)
  1073. {
  1074. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1075. int i;
  1076. /*
  1077. * If we're called during a txn, we don't need to do anything.
  1078. * The events never got scheduled and ->cancel_txn will truncate
  1079. * the event_list.
  1080. */
  1081. if (cpuc->group_flag & PERF_EVENT_TXN)
  1082. return;
  1083. x86_pmu_stop(event, PERF_EF_UPDATE);
  1084. for (i = 0; i < cpuc->n_events; i++) {
  1085. if (event == cpuc->event_list[i]) {
  1086. if (x86_pmu.put_event_constraints)
  1087. x86_pmu.put_event_constraints(cpuc, event);
  1088. while (++i < cpuc->n_events)
  1089. cpuc->event_list[i-1] = cpuc->event_list[i];
  1090. --cpuc->n_events;
  1091. break;
  1092. }
  1093. }
  1094. perf_event_update_userpage(event);
  1095. }
  1096. static int x86_pmu_handle_irq(struct pt_regs *regs)
  1097. {
  1098. struct perf_sample_data data;
  1099. struct cpu_hw_events *cpuc;
  1100. struct perf_event *event;
  1101. int idx, handled = 0;
  1102. u64 val;
  1103. perf_sample_data_init(&data, 0);
  1104. cpuc = &__get_cpu_var(cpu_hw_events);
  1105. /*
  1106. * Some chipsets need to unmask the LVTPC in a particular spot
  1107. * inside the nmi handler. As a result, the unmasking was pushed
  1108. * into all the nmi handlers.
  1109. *
  1110. * This generic handler doesn't seem to have any issues where the
  1111. * unmasking occurs so it was left at the top.
  1112. */
  1113. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1114. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1115. if (!test_bit(idx, cpuc->active_mask)) {
  1116. /*
  1117. * Though we deactivated the counter some cpus
  1118. * might still deliver spurious interrupts still
  1119. * in flight. Catch them:
  1120. */
  1121. if (__test_and_clear_bit(idx, cpuc->running))
  1122. handled++;
  1123. continue;
  1124. }
  1125. event = cpuc->events[idx];
  1126. val = x86_perf_event_update(event);
  1127. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  1128. continue;
  1129. /*
  1130. * event overflow
  1131. */
  1132. handled++;
  1133. data.period = event->hw.last_period;
  1134. if (!x86_perf_event_set_period(event))
  1135. continue;
  1136. if (perf_event_overflow(event, &data, regs))
  1137. x86_pmu_stop(event, 0);
  1138. }
  1139. if (handled)
  1140. inc_irq_stat(apic_perf_irqs);
  1141. return handled;
  1142. }
  1143. void perf_events_lapic_init(void)
  1144. {
  1145. if (!x86_pmu.apic || !x86_pmu_initialized())
  1146. return;
  1147. /*
  1148. * Always use NMI for PMU
  1149. */
  1150. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1151. }
  1152. struct pmu_nmi_state {
  1153. unsigned int marked;
  1154. int handled;
  1155. };
  1156. static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
  1157. static int __kprobes
  1158. perf_event_nmi_handler(struct notifier_block *self,
  1159. unsigned long cmd, void *__args)
  1160. {
  1161. struct die_args *args = __args;
  1162. unsigned int this_nmi;
  1163. int handled;
  1164. if (!atomic_read(&active_events))
  1165. return NOTIFY_DONE;
  1166. switch (cmd) {
  1167. case DIE_NMI:
  1168. break;
  1169. case DIE_NMIUNKNOWN:
  1170. this_nmi = percpu_read(irq_stat.__nmi_count);
  1171. if (this_nmi != __this_cpu_read(pmu_nmi.marked))
  1172. /* let the kernel handle the unknown nmi */
  1173. return NOTIFY_DONE;
  1174. /*
  1175. * This one is a PMU back-to-back nmi. Two events
  1176. * trigger 'simultaneously' raising two back-to-back
  1177. * NMIs. If the first NMI handles both, the latter
  1178. * will be empty and daze the CPU. So, we drop it to
  1179. * avoid false-positive 'unknown nmi' messages.
  1180. */
  1181. return NOTIFY_STOP;
  1182. default:
  1183. return NOTIFY_DONE;
  1184. }
  1185. handled = x86_pmu.handle_irq(args->regs);
  1186. if (!handled)
  1187. return NOTIFY_DONE;
  1188. this_nmi = percpu_read(irq_stat.__nmi_count);
  1189. if ((handled > 1) ||
  1190. /* the next nmi could be a back-to-back nmi */
  1191. ((__this_cpu_read(pmu_nmi.marked) == this_nmi) &&
  1192. (__this_cpu_read(pmu_nmi.handled) > 1))) {
  1193. /*
  1194. * We could have two subsequent back-to-back nmis: The
  1195. * first handles more than one counter, the 2nd
  1196. * handles only one counter and the 3rd handles no
  1197. * counter.
  1198. *
  1199. * This is the 2nd nmi because the previous was
  1200. * handling more than one counter. We will mark the
  1201. * next (3rd) and then drop it if unhandled.
  1202. */
  1203. __this_cpu_write(pmu_nmi.marked, this_nmi + 1);
  1204. __this_cpu_write(pmu_nmi.handled, handled);
  1205. }
  1206. return NOTIFY_STOP;
  1207. }
  1208. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1209. .notifier_call = perf_event_nmi_handler,
  1210. .next = NULL,
  1211. .priority = NMI_LOCAL_LOW_PRIOR,
  1212. };
  1213. static struct event_constraint unconstrained;
  1214. static struct event_constraint emptyconstraint;
  1215. static struct event_constraint *
  1216. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1217. {
  1218. struct event_constraint *c;
  1219. if (x86_pmu.event_constraints) {
  1220. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1221. if ((event->hw.config & c->cmask) == c->code)
  1222. return c;
  1223. }
  1224. }
  1225. return &unconstrained;
  1226. }
  1227. #include "perf_event_amd.c"
  1228. #include "perf_event_p6.c"
  1229. #include "perf_event_p4.c"
  1230. #include "perf_event_intel_lbr.c"
  1231. #include "perf_event_intel_ds.c"
  1232. #include "perf_event_intel.c"
  1233. static int __cpuinit
  1234. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1235. {
  1236. unsigned int cpu = (long)hcpu;
  1237. int ret = NOTIFY_OK;
  1238. switch (action & ~CPU_TASKS_FROZEN) {
  1239. case CPU_UP_PREPARE:
  1240. if (x86_pmu.cpu_prepare)
  1241. ret = x86_pmu.cpu_prepare(cpu);
  1242. break;
  1243. case CPU_STARTING:
  1244. if (x86_pmu.cpu_starting)
  1245. x86_pmu.cpu_starting(cpu);
  1246. break;
  1247. case CPU_DYING:
  1248. if (x86_pmu.cpu_dying)
  1249. x86_pmu.cpu_dying(cpu);
  1250. break;
  1251. case CPU_UP_CANCELED:
  1252. case CPU_DEAD:
  1253. if (x86_pmu.cpu_dead)
  1254. x86_pmu.cpu_dead(cpu);
  1255. break;
  1256. default:
  1257. break;
  1258. }
  1259. return ret;
  1260. }
  1261. static void __init pmu_check_apic(void)
  1262. {
  1263. if (cpu_has_apic)
  1264. return;
  1265. x86_pmu.apic = 0;
  1266. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1267. pr_info("no hardware sampling interrupt available.\n");
  1268. }
  1269. static int __init init_hw_perf_events(void)
  1270. {
  1271. struct event_constraint *c;
  1272. int err;
  1273. pr_info("Performance Events: ");
  1274. switch (boot_cpu_data.x86_vendor) {
  1275. case X86_VENDOR_INTEL:
  1276. err = intel_pmu_init();
  1277. break;
  1278. case X86_VENDOR_AMD:
  1279. err = amd_pmu_init();
  1280. break;
  1281. default:
  1282. return 0;
  1283. }
  1284. if (err != 0) {
  1285. pr_cont("no PMU driver, software events only.\n");
  1286. return 0;
  1287. }
  1288. pmu_check_apic();
  1289. /* sanity check that the hardware exists or is emulated */
  1290. if (!check_hw_exists())
  1291. return 0;
  1292. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1293. if (x86_pmu.quirks)
  1294. x86_pmu.quirks();
  1295. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1296. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1297. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1298. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1299. }
  1300. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1301. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1302. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1303. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1304. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1305. }
  1306. x86_pmu.intel_ctrl |=
  1307. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1308. perf_events_lapic_init();
  1309. register_die_notifier(&perf_event_nmi_notifier);
  1310. unconstrained = (struct event_constraint)
  1311. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1312. 0, x86_pmu.num_counters);
  1313. if (x86_pmu.event_constraints) {
  1314. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1315. if (c->cmask != X86_RAW_EVENT_MASK)
  1316. continue;
  1317. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1318. c->weight += x86_pmu.num_counters;
  1319. }
  1320. }
  1321. pr_info("... version: %d\n", x86_pmu.version);
  1322. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1323. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1324. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1325. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1326. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1327. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1328. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1329. perf_cpu_notifier(x86_pmu_notifier);
  1330. return 0;
  1331. }
  1332. early_initcall(init_hw_perf_events);
  1333. static inline void x86_pmu_read(struct perf_event *event)
  1334. {
  1335. x86_perf_event_update(event);
  1336. }
  1337. /*
  1338. * Start group events scheduling transaction
  1339. * Set the flag to make pmu::enable() not perform the
  1340. * schedulability test, it will be performed at commit time
  1341. */
  1342. static void x86_pmu_start_txn(struct pmu *pmu)
  1343. {
  1344. perf_pmu_disable(pmu);
  1345. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1346. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1347. }
  1348. /*
  1349. * Stop group events scheduling transaction
  1350. * Clear the flag and pmu::enable() will perform the
  1351. * schedulability test.
  1352. */
  1353. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1354. {
  1355. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1356. /*
  1357. * Truncate the collected events.
  1358. */
  1359. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1360. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1361. perf_pmu_enable(pmu);
  1362. }
  1363. /*
  1364. * Commit group events scheduling transaction
  1365. * Perform the group schedulability test as a whole
  1366. * Return 0 if success
  1367. */
  1368. static int x86_pmu_commit_txn(struct pmu *pmu)
  1369. {
  1370. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1371. int assign[X86_PMC_IDX_MAX];
  1372. int n, ret;
  1373. n = cpuc->n_events;
  1374. if (!x86_pmu_initialized())
  1375. return -EAGAIN;
  1376. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1377. if (ret)
  1378. return ret;
  1379. /*
  1380. * copy new assignment, now we know it is possible
  1381. * will be used by hw_perf_enable()
  1382. */
  1383. memcpy(cpuc->assign, assign, n*sizeof(int));
  1384. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1385. perf_pmu_enable(pmu);
  1386. return 0;
  1387. }
  1388. /*
  1389. * a fake_cpuc is used to validate event groups. Due to
  1390. * the extra reg logic, we need to also allocate a fake
  1391. * per_core and per_cpu structure. Otherwise, group events
  1392. * using extra reg may conflict without the kernel being
  1393. * able to catch this when the last event gets added to
  1394. * the group.
  1395. */
  1396. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1397. {
  1398. kfree(cpuc->shared_regs);
  1399. kfree(cpuc);
  1400. }
  1401. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1402. {
  1403. struct cpu_hw_events *cpuc;
  1404. int cpu = raw_smp_processor_id();
  1405. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1406. if (!cpuc)
  1407. return ERR_PTR(-ENOMEM);
  1408. /* only needed, if we have extra_regs */
  1409. if (x86_pmu.extra_regs) {
  1410. cpuc->shared_regs = allocate_shared_regs(cpu);
  1411. if (!cpuc->shared_regs)
  1412. goto error;
  1413. }
  1414. return cpuc;
  1415. error:
  1416. free_fake_cpuc(cpuc);
  1417. return ERR_PTR(-ENOMEM);
  1418. }
  1419. /*
  1420. * validate that we can schedule this event
  1421. */
  1422. static int validate_event(struct perf_event *event)
  1423. {
  1424. struct cpu_hw_events *fake_cpuc;
  1425. struct event_constraint *c;
  1426. int ret = 0;
  1427. fake_cpuc = allocate_fake_cpuc();
  1428. if (IS_ERR(fake_cpuc))
  1429. return PTR_ERR(fake_cpuc);
  1430. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1431. if (!c || !c->weight)
  1432. ret = -ENOSPC;
  1433. if (x86_pmu.put_event_constraints)
  1434. x86_pmu.put_event_constraints(fake_cpuc, event);
  1435. free_fake_cpuc(fake_cpuc);
  1436. return ret;
  1437. }
  1438. /*
  1439. * validate a single event group
  1440. *
  1441. * validation include:
  1442. * - check events are compatible which each other
  1443. * - events do not compete for the same counter
  1444. * - number of events <= number of counters
  1445. *
  1446. * validation ensures the group can be loaded onto the
  1447. * PMU if it was the only group available.
  1448. */
  1449. static int validate_group(struct perf_event *event)
  1450. {
  1451. struct perf_event *leader = event->group_leader;
  1452. struct cpu_hw_events *fake_cpuc;
  1453. int ret = -ENOSPC, n;
  1454. fake_cpuc = allocate_fake_cpuc();
  1455. if (IS_ERR(fake_cpuc))
  1456. return PTR_ERR(fake_cpuc);
  1457. /*
  1458. * the event is not yet connected with its
  1459. * siblings therefore we must first collect
  1460. * existing siblings, then add the new event
  1461. * before we can simulate the scheduling
  1462. */
  1463. n = collect_events(fake_cpuc, leader, true);
  1464. if (n < 0)
  1465. goto out;
  1466. fake_cpuc->n_events = n;
  1467. n = collect_events(fake_cpuc, event, false);
  1468. if (n < 0)
  1469. goto out;
  1470. fake_cpuc->n_events = n;
  1471. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1472. out:
  1473. free_fake_cpuc(fake_cpuc);
  1474. return ret;
  1475. }
  1476. static int x86_pmu_event_init(struct perf_event *event)
  1477. {
  1478. struct pmu *tmp;
  1479. int err;
  1480. switch (event->attr.type) {
  1481. case PERF_TYPE_RAW:
  1482. case PERF_TYPE_HARDWARE:
  1483. case PERF_TYPE_HW_CACHE:
  1484. break;
  1485. default:
  1486. return -ENOENT;
  1487. }
  1488. err = __x86_pmu_event_init(event);
  1489. if (!err) {
  1490. /*
  1491. * we temporarily connect event to its pmu
  1492. * such that validate_group() can classify
  1493. * it as an x86 event using is_x86_event()
  1494. */
  1495. tmp = event->pmu;
  1496. event->pmu = &pmu;
  1497. if (event->group_leader != event)
  1498. err = validate_group(event);
  1499. else
  1500. err = validate_event(event);
  1501. event->pmu = tmp;
  1502. }
  1503. if (err) {
  1504. if (event->destroy)
  1505. event->destroy(event);
  1506. }
  1507. return err;
  1508. }
  1509. static struct pmu pmu = {
  1510. .pmu_enable = x86_pmu_enable,
  1511. .pmu_disable = x86_pmu_disable,
  1512. .event_init = x86_pmu_event_init,
  1513. .add = x86_pmu_add,
  1514. .del = x86_pmu_del,
  1515. .start = x86_pmu_start,
  1516. .stop = x86_pmu_stop,
  1517. .read = x86_pmu_read,
  1518. .start_txn = x86_pmu_start_txn,
  1519. .cancel_txn = x86_pmu_cancel_txn,
  1520. .commit_txn = x86_pmu_commit_txn,
  1521. };
  1522. /*
  1523. * callchain support
  1524. */
  1525. static int backtrace_stack(void *data, char *name)
  1526. {
  1527. return 0;
  1528. }
  1529. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1530. {
  1531. struct perf_callchain_entry *entry = data;
  1532. perf_callchain_store(entry, addr);
  1533. }
  1534. static const struct stacktrace_ops backtrace_ops = {
  1535. .stack = backtrace_stack,
  1536. .address = backtrace_address,
  1537. .walk_stack = print_context_stack_bp,
  1538. };
  1539. void
  1540. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1541. {
  1542. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1543. /* TODO: We don't support guest os callchain now */
  1544. return;
  1545. }
  1546. perf_callchain_store(entry, regs->ip);
  1547. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1548. }
  1549. #ifdef CONFIG_COMPAT
  1550. static inline int
  1551. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1552. {
  1553. /* 32-bit process in 64-bit kernel. */
  1554. struct stack_frame_ia32 frame;
  1555. const void __user *fp;
  1556. if (!test_thread_flag(TIF_IA32))
  1557. return 0;
  1558. fp = compat_ptr(regs->bp);
  1559. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1560. unsigned long bytes;
  1561. frame.next_frame = 0;
  1562. frame.return_address = 0;
  1563. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1564. if (bytes != sizeof(frame))
  1565. break;
  1566. if (fp < compat_ptr(regs->sp))
  1567. break;
  1568. perf_callchain_store(entry, frame.return_address);
  1569. fp = compat_ptr(frame.next_frame);
  1570. }
  1571. return 1;
  1572. }
  1573. #else
  1574. static inline int
  1575. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1576. {
  1577. return 0;
  1578. }
  1579. #endif
  1580. void
  1581. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1582. {
  1583. struct stack_frame frame;
  1584. const void __user *fp;
  1585. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1586. /* TODO: We don't support guest os callchain now */
  1587. return;
  1588. }
  1589. fp = (void __user *)regs->bp;
  1590. perf_callchain_store(entry, regs->ip);
  1591. if (perf_callchain_user32(regs, entry))
  1592. return;
  1593. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1594. unsigned long bytes;
  1595. frame.next_frame = NULL;
  1596. frame.return_address = 0;
  1597. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1598. if (bytes != sizeof(frame))
  1599. break;
  1600. if ((unsigned long)fp < regs->sp)
  1601. break;
  1602. perf_callchain_store(entry, frame.return_address);
  1603. fp = frame.next_frame;
  1604. }
  1605. }
  1606. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1607. {
  1608. unsigned long ip;
  1609. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1610. ip = perf_guest_cbs->get_guest_ip();
  1611. else
  1612. ip = instruction_pointer(regs);
  1613. return ip;
  1614. }
  1615. unsigned long perf_misc_flags(struct pt_regs *regs)
  1616. {
  1617. int misc = 0;
  1618. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1619. if (perf_guest_cbs->is_user_mode())
  1620. misc |= PERF_RECORD_MISC_GUEST_USER;
  1621. else
  1622. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1623. } else {
  1624. if (user_mode(regs))
  1625. misc |= PERF_RECORD_MISC_USER;
  1626. else
  1627. misc |= PERF_RECORD_MISC_KERNEL;
  1628. }
  1629. if (regs->flags & PERF_EFLAGS_EXACT)
  1630. misc |= PERF_RECORD_MISC_EXACT_IP;
  1631. return misc;
  1632. }