perf_event.c 40 KB

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