perf_event.c 39 KB

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