perf_event.c 40 KB

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