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