perf_event.c 39 KB

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