perf_event.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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 void 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. reserve_ds_buffers();
  451. }
  452. if (!err)
  453. atomic_inc(&active_events);
  454. mutex_unlock(&pmc_reserve_mutex);
  455. }
  456. if (err)
  457. return err;
  458. event->destroy = hw_perf_event_destroy;
  459. event->hw.idx = -1;
  460. event->hw.last_cpu = -1;
  461. event->hw.last_tag = ~0ULL;
  462. return x86_pmu.hw_config(event);
  463. }
  464. static void x86_pmu_disable_all(void)
  465. {
  466. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  467. int idx;
  468. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  469. u64 val;
  470. if (!test_bit(idx, cpuc->active_mask))
  471. continue;
  472. rdmsrl(x86_pmu.eventsel + idx, val);
  473. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  474. continue;
  475. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  476. wrmsrl(x86_pmu.eventsel + idx, val);
  477. }
  478. }
  479. static void x86_pmu_disable(struct pmu *pmu)
  480. {
  481. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  482. if (!x86_pmu_initialized())
  483. return;
  484. if (!cpuc->enabled)
  485. return;
  486. cpuc->n_added = 0;
  487. cpuc->enabled = 0;
  488. barrier();
  489. x86_pmu.disable_all();
  490. }
  491. static void x86_pmu_enable_all(int added)
  492. {
  493. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  494. int idx;
  495. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  496. struct perf_event *event = cpuc->events[idx];
  497. u64 val;
  498. if (!test_bit(idx, cpuc->active_mask))
  499. continue;
  500. val = event->hw.config;
  501. val |= ARCH_PERFMON_EVENTSEL_ENABLE;
  502. wrmsrl(x86_pmu.eventsel + idx, val);
  503. }
  504. }
  505. static struct pmu pmu;
  506. static inline int is_x86_event(struct perf_event *event)
  507. {
  508. return event->pmu == &pmu;
  509. }
  510. static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  511. {
  512. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  513. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  514. int i, j, w, wmax, num = 0;
  515. struct hw_perf_event *hwc;
  516. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  517. for (i = 0; i < n; i++) {
  518. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  519. constraints[i] = c;
  520. }
  521. /*
  522. * fastpath, try to reuse previous register
  523. */
  524. for (i = 0; i < n; i++) {
  525. hwc = &cpuc->event_list[i]->hw;
  526. c = constraints[i];
  527. /* never assigned */
  528. if (hwc->idx == -1)
  529. break;
  530. /* constraint still honored */
  531. if (!test_bit(hwc->idx, c->idxmsk))
  532. break;
  533. /* not already used */
  534. if (test_bit(hwc->idx, used_mask))
  535. break;
  536. __set_bit(hwc->idx, used_mask);
  537. if (assign)
  538. assign[i] = hwc->idx;
  539. }
  540. if (i == n)
  541. goto done;
  542. /*
  543. * begin slow path
  544. */
  545. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  546. /*
  547. * weight = number of possible counters
  548. *
  549. * 1 = most constrained, only works on one counter
  550. * wmax = least constrained, works on any counter
  551. *
  552. * assign events to counters starting with most
  553. * constrained events.
  554. */
  555. wmax = x86_pmu.num_counters;
  556. /*
  557. * when fixed event counters are present,
  558. * wmax is incremented by 1 to account
  559. * for one more choice
  560. */
  561. if (x86_pmu.num_counters_fixed)
  562. wmax++;
  563. for (w = 1, num = n; num && w <= wmax; w++) {
  564. /* for each event */
  565. for (i = 0; num && i < n; i++) {
  566. c = constraints[i];
  567. hwc = &cpuc->event_list[i]->hw;
  568. if (c->weight != w)
  569. continue;
  570. for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
  571. if (!test_bit(j, used_mask))
  572. break;
  573. }
  574. if (j == X86_PMC_IDX_MAX)
  575. break;
  576. __set_bit(j, used_mask);
  577. if (assign)
  578. assign[i] = j;
  579. num--;
  580. }
  581. }
  582. done:
  583. /*
  584. * scheduling failed or is just a simulation,
  585. * free resources if necessary
  586. */
  587. if (!assign || num) {
  588. for (i = 0; i < n; i++) {
  589. if (x86_pmu.put_event_constraints)
  590. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  591. }
  592. }
  593. return num ? -ENOSPC : 0;
  594. }
  595. /*
  596. * dogrp: true if must collect siblings events (group)
  597. * returns total number of events and error code
  598. */
  599. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  600. {
  601. struct perf_event *event;
  602. int n, max_count;
  603. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  604. /* current number of events already accepted */
  605. n = cpuc->n_events;
  606. if (is_x86_event(leader)) {
  607. if (n >= max_count)
  608. return -ENOSPC;
  609. cpuc->event_list[n] = leader;
  610. n++;
  611. }
  612. if (!dogrp)
  613. return n;
  614. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  615. if (!is_x86_event(event) ||
  616. event->state <= PERF_EVENT_STATE_OFF)
  617. continue;
  618. if (n >= max_count)
  619. return -ENOSPC;
  620. cpuc->event_list[n] = event;
  621. n++;
  622. }
  623. return n;
  624. }
  625. static inline void x86_assign_hw_event(struct perf_event *event,
  626. struct cpu_hw_events *cpuc, int i)
  627. {
  628. struct hw_perf_event *hwc = &event->hw;
  629. hwc->idx = cpuc->assign[i];
  630. hwc->last_cpu = smp_processor_id();
  631. hwc->last_tag = ++cpuc->tags[i];
  632. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  633. hwc->config_base = 0;
  634. hwc->event_base = 0;
  635. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  636. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  637. /*
  638. * We set it so that event_base + idx in wrmsr/rdmsr maps to
  639. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  640. */
  641. hwc->event_base =
  642. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  643. } else {
  644. hwc->config_base = x86_pmu.eventsel;
  645. hwc->event_base = x86_pmu.perfctr;
  646. }
  647. }
  648. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  649. struct cpu_hw_events *cpuc,
  650. int i)
  651. {
  652. return hwc->idx == cpuc->assign[i] &&
  653. hwc->last_cpu == smp_processor_id() &&
  654. hwc->last_tag == cpuc->tags[i];
  655. }
  656. static void x86_pmu_start(struct perf_event *event, int flags);
  657. static void x86_pmu_stop(struct perf_event *event, int flags);
  658. static void x86_pmu_enable(struct pmu *pmu)
  659. {
  660. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  661. struct perf_event *event;
  662. struct hw_perf_event *hwc;
  663. int i, added = cpuc->n_added;
  664. if (!x86_pmu_initialized())
  665. return;
  666. if (cpuc->enabled)
  667. return;
  668. if (cpuc->n_added) {
  669. int n_running = cpuc->n_events - cpuc->n_added;
  670. /*
  671. * apply assignment obtained either from
  672. * hw_perf_group_sched_in() or x86_pmu_enable()
  673. *
  674. * step1: save events moving to new counters
  675. * step2: reprogram moved events into new counters
  676. */
  677. for (i = 0; i < n_running; i++) {
  678. event = cpuc->event_list[i];
  679. hwc = &event->hw;
  680. /*
  681. * we can avoid reprogramming counter if:
  682. * - assigned same counter as last time
  683. * - running on same CPU as last time
  684. * - no other event has used the counter since
  685. */
  686. if (hwc->idx == -1 ||
  687. match_prev_assignment(hwc, cpuc, i))
  688. continue;
  689. /*
  690. * Ensure we don't accidentally enable a stopped
  691. * counter simply because we rescheduled.
  692. */
  693. if (hwc->state & PERF_HES_STOPPED)
  694. hwc->state |= PERF_HES_ARCH;
  695. x86_pmu_stop(event, PERF_EF_UPDATE);
  696. }
  697. for (i = 0; i < cpuc->n_events; i++) {
  698. event = cpuc->event_list[i];
  699. hwc = &event->hw;
  700. if (!match_prev_assignment(hwc, cpuc, i))
  701. x86_assign_hw_event(event, cpuc, i);
  702. else if (i < n_running)
  703. continue;
  704. if (hwc->state & PERF_HES_ARCH)
  705. continue;
  706. x86_pmu_start(event, PERF_EF_RELOAD);
  707. }
  708. cpuc->n_added = 0;
  709. perf_events_lapic_init();
  710. }
  711. cpuc->enabled = 1;
  712. barrier();
  713. x86_pmu.enable_all(added);
  714. }
  715. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  716. u64 enable_mask)
  717. {
  718. wrmsrl(hwc->config_base + hwc->idx, hwc->config | enable_mask);
  719. }
  720. static inline void x86_pmu_disable_event(struct perf_event *event)
  721. {
  722. struct hw_perf_event *hwc = &event->hw;
  723. wrmsrl(hwc->config_base + hwc->idx, hwc->config);
  724. }
  725. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  726. /*
  727. * Set the next IRQ period, based on the hwc->period_left value.
  728. * To be called with the event disabled in hw:
  729. */
  730. static int
  731. x86_perf_event_set_period(struct perf_event *event)
  732. {
  733. struct hw_perf_event *hwc = &event->hw;
  734. s64 left = local64_read(&hwc->period_left);
  735. s64 period = hwc->sample_period;
  736. int ret = 0, idx = hwc->idx;
  737. if (idx == X86_PMC_IDX_FIXED_BTS)
  738. return 0;
  739. /*
  740. * If we are way outside a reasonable range then just skip forward:
  741. */
  742. if (unlikely(left <= -period)) {
  743. left = period;
  744. local64_set(&hwc->period_left, left);
  745. hwc->last_period = period;
  746. ret = 1;
  747. }
  748. if (unlikely(left <= 0)) {
  749. left += period;
  750. local64_set(&hwc->period_left, left);
  751. hwc->last_period = period;
  752. ret = 1;
  753. }
  754. /*
  755. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  756. */
  757. if (unlikely(left < 2))
  758. left = 2;
  759. if (left > x86_pmu.max_period)
  760. left = x86_pmu.max_period;
  761. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  762. /*
  763. * The hw event starts counting from this event offset,
  764. * mark it to be able to extra future deltas:
  765. */
  766. local64_set(&hwc->prev_count, (u64)-left);
  767. wrmsrl(hwc->event_base + idx, (u64)(-left) & x86_pmu.cntval_mask);
  768. /*
  769. * Due to erratum on certan cpu we need
  770. * a second write to be sure the register
  771. * is updated properly
  772. */
  773. if (x86_pmu.perfctr_second_write) {
  774. wrmsrl(hwc->event_base + idx,
  775. (u64)(-left) & x86_pmu.cntval_mask);
  776. }
  777. perf_event_update_userpage(event);
  778. return ret;
  779. }
  780. static void x86_pmu_enable_event(struct perf_event *event)
  781. {
  782. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  783. if (cpuc->enabled)
  784. __x86_pmu_enable_event(&event->hw,
  785. ARCH_PERFMON_EVENTSEL_ENABLE);
  786. }
  787. /*
  788. * Add a single event to the PMU.
  789. *
  790. * The event is added to the group of enabled events
  791. * but only if it can be scehduled with existing events.
  792. */
  793. static int x86_pmu_add(struct perf_event *event, int flags)
  794. {
  795. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  796. struct hw_perf_event *hwc;
  797. int assign[X86_PMC_IDX_MAX];
  798. int n, n0, ret;
  799. hwc = &event->hw;
  800. perf_pmu_disable(event->pmu);
  801. n0 = cpuc->n_events;
  802. ret = n = collect_events(cpuc, event, false);
  803. if (ret < 0)
  804. goto out;
  805. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  806. if (!(flags & PERF_EF_START))
  807. hwc->state |= PERF_HES_ARCH;
  808. /*
  809. * If group events scheduling transaction was started,
  810. * skip the schedulability test here, it will be peformed
  811. * at commit time (->commit_txn) as a whole
  812. */
  813. if (cpuc->group_flag & PERF_EVENT_TXN)
  814. goto done_collect;
  815. ret = x86_pmu.schedule_events(cpuc, n, assign);
  816. if (ret)
  817. goto out;
  818. /*
  819. * copy new assignment, now we know it is possible
  820. * will be used by hw_perf_enable()
  821. */
  822. memcpy(cpuc->assign, assign, n*sizeof(int));
  823. done_collect:
  824. cpuc->n_events = n;
  825. cpuc->n_added += n - n0;
  826. cpuc->n_txn += n - n0;
  827. ret = 0;
  828. out:
  829. perf_pmu_enable(event->pmu);
  830. return ret;
  831. }
  832. static void x86_pmu_start(struct perf_event *event, int flags)
  833. {
  834. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  835. int idx = event->hw.idx;
  836. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  837. return;
  838. if (WARN_ON_ONCE(idx == -1))
  839. return;
  840. if (flags & PERF_EF_RELOAD) {
  841. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  842. x86_perf_event_set_period(event);
  843. }
  844. event->hw.state = 0;
  845. cpuc->events[idx] = event;
  846. __set_bit(idx, cpuc->active_mask);
  847. __set_bit(idx, cpuc->running);
  848. x86_pmu.enable(event);
  849. perf_event_update_userpage(event);
  850. }
  851. void perf_event_print_debug(void)
  852. {
  853. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  854. u64 pebs;
  855. struct cpu_hw_events *cpuc;
  856. unsigned long flags;
  857. int cpu, idx;
  858. if (!x86_pmu.num_counters)
  859. return;
  860. local_irq_save(flags);
  861. cpu = smp_processor_id();
  862. cpuc = &per_cpu(cpu_hw_events, cpu);
  863. if (x86_pmu.version >= 2) {
  864. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  865. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  866. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  867. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  868. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  869. pr_info("\n");
  870. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  871. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  872. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  873. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  874. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  875. }
  876. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  877. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  878. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  879. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  880. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  881. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  882. cpu, idx, pmc_ctrl);
  883. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  884. cpu, idx, pmc_count);
  885. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  886. cpu, idx, prev_left);
  887. }
  888. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  889. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  890. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  891. cpu, idx, pmc_count);
  892. }
  893. local_irq_restore(flags);
  894. }
  895. static void x86_pmu_stop(struct perf_event *event, int flags)
  896. {
  897. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  898. struct hw_perf_event *hwc = &event->hw;
  899. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  900. x86_pmu.disable(event);
  901. cpuc->events[hwc->idx] = NULL;
  902. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  903. hwc->state |= PERF_HES_STOPPED;
  904. }
  905. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  906. /*
  907. * Drain the remaining delta count out of a event
  908. * that we are disabling:
  909. */
  910. x86_perf_event_update(event);
  911. hwc->state |= PERF_HES_UPTODATE;
  912. }
  913. }
  914. static void x86_pmu_del(struct perf_event *event, int flags)
  915. {
  916. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  917. int i;
  918. /*
  919. * If we're called during a txn, we don't need to do anything.
  920. * The events never got scheduled and ->cancel_txn will truncate
  921. * the event_list.
  922. */
  923. if (cpuc->group_flag & PERF_EVENT_TXN)
  924. return;
  925. x86_pmu_stop(event, PERF_EF_UPDATE);
  926. for (i = 0; i < cpuc->n_events; i++) {
  927. if (event == cpuc->event_list[i]) {
  928. if (x86_pmu.put_event_constraints)
  929. x86_pmu.put_event_constraints(cpuc, event);
  930. while (++i < cpuc->n_events)
  931. cpuc->event_list[i-1] = cpuc->event_list[i];
  932. --cpuc->n_events;
  933. break;
  934. }
  935. }
  936. perf_event_update_userpage(event);
  937. }
  938. static int x86_pmu_handle_irq(struct pt_regs *regs)
  939. {
  940. struct perf_sample_data data;
  941. struct cpu_hw_events *cpuc;
  942. struct perf_event *event;
  943. int idx, handled = 0;
  944. u64 val;
  945. perf_sample_data_init(&data, 0);
  946. cpuc = &__get_cpu_var(cpu_hw_events);
  947. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  948. if (!test_bit(idx, cpuc->active_mask)) {
  949. /*
  950. * Though we deactivated the counter some cpus
  951. * might still deliver spurious interrupts still
  952. * in flight. Catch them:
  953. */
  954. if (__test_and_clear_bit(idx, cpuc->running))
  955. handled++;
  956. continue;
  957. }
  958. event = cpuc->events[idx];
  959. val = x86_perf_event_update(event);
  960. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  961. continue;
  962. /*
  963. * event overflow
  964. */
  965. handled++;
  966. data.period = event->hw.last_period;
  967. if (!x86_perf_event_set_period(event))
  968. continue;
  969. if (perf_event_overflow(event, 1, &data, regs))
  970. x86_pmu_stop(event, 0);
  971. }
  972. if (handled)
  973. inc_irq_stat(apic_perf_irqs);
  974. return handled;
  975. }
  976. void perf_events_lapic_init(void)
  977. {
  978. if (!x86_pmu.apic || !x86_pmu_initialized())
  979. return;
  980. /*
  981. * Always use NMI for PMU
  982. */
  983. apic_write(APIC_LVTPC, APIC_DM_NMI);
  984. }
  985. struct pmu_nmi_state {
  986. unsigned int marked;
  987. int handled;
  988. };
  989. static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
  990. static int __kprobes
  991. perf_event_nmi_handler(struct notifier_block *self,
  992. unsigned long cmd, void *__args)
  993. {
  994. struct die_args *args = __args;
  995. unsigned int this_nmi;
  996. int handled;
  997. if (!atomic_read(&active_events))
  998. return NOTIFY_DONE;
  999. switch (cmd) {
  1000. case DIE_NMI:
  1001. case DIE_NMI_IPI:
  1002. break;
  1003. case DIE_NMIUNKNOWN:
  1004. this_nmi = percpu_read(irq_stat.__nmi_count);
  1005. if (this_nmi != __get_cpu_var(pmu_nmi).marked)
  1006. /* let the kernel handle the unknown nmi */
  1007. return NOTIFY_DONE;
  1008. /*
  1009. * This one is a PMU back-to-back nmi. Two events
  1010. * trigger 'simultaneously' raising two back-to-back
  1011. * NMIs. If the first NMI handles both, the latter
  1012. * will be empty and daze the CPU. So, we drop it to
  1013. * avoid false-positive 'unknown nmi' messages.
  1014. */
  1015. return NOTIFY_STOP;
  1016. default:
  1017. return NOTIFY_DONE;
  1018. }
  1019. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1020. handled = x86_pmu.handle_irq(args->regs);
  1021. if (!handled)
  1022. return NOTIFY_DONE;
  1023. this_nmi = percpu_read(irq_stat.__nmi_count);
  1024. if ((handled > 1) ||
  1025. /* the next nmi could be a back-to-back nmi */
  1026. ((__get_cpu_var(pmu_nmi).marked == this_nmi) &&
  1027. (__get_cpu_var(pmu_nmi).handled > 1))) {
  1028. /*
  1029. * We could have two subsequent back-to-back nmis: The
  1030. * first handles more than one counter, the 2nd
  1031. * handles only one counter and the 3rd handles no
  1032. * counter.
  1033. *
  1034. * This is the 2nd nmi because the previous was
  1035. * handling more than one counter. We will mark the
  1036. * next (3rd) and then drop it if unhandled.
  1037. */
  1038. __get_cpu_var(pmu_nmi).marked = this_nmi + 1;
  1039. __get_cpu_var(pmu_nmi).handled = handled;
  1040. }
  1041. return NOTIFY_STOP;
  1042. }
  1043. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1044. .notifier_call = perf_event_nmi_handler,
  1045. .next = NULL,
  1046. .priority = 1
  1047. };
  1048. static struct event_constraint unconstrained;
  1049. static struct event_constraint emptyconstraint;
  1050. static struct event_constraint *
  1051. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1052. {
  1053. struct event_constraint *c;
  1054. if (x86_pmu.event_constraints) {
  1055. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1056. if ((event->hw.config & c->cmask) == c->code)
  1057. return c;
  1058. }
  1059. }
  1060. return &unconstrained;
  1061. }
  1062. #include "perf_event_amd.c"
  1063. #include "perf_event_p6.c"
  1064. #include "perf_event_p4.c"
  1065. #include "perf_event_intel_lbr.c"
  1066. #include "perf_event_intel_ds.c"
  1067. #include "perf_event_intel.c"
  1068. static int __cpuinit
  1069. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1070. {
  1071. unsigned int cpu = (long)hcpu;
  1072. int ret = NOTIFY_OK;
  1073. switch (action & ~CPU_TASKS_FROZEN) {
  1074. case CPU_UP_PREPARE:
  1075. if (x86_pmu.cpu_prepare)
  1076. ret = x86_pmu.cpu_prepare(cpu);
  1077. break;
  1078. case CPU_STARTING:
  1079. if (x86_pmu.cpu_starting)
  1080. x86_pmu.cpu_starting(cpu);
  1081. break;
  1082. case CPU_DYING:
  1083. if (x86_pmu.cpu_dying)
  1084. x86_pmu.cpu_dying(cpu);
  1085. break;
  1086. case CPU_UP_CANCELED:
  1087. case CPU_DEAD:
  1088. if (x86_pmu.cpu_dead)
  1089. x86_pmu.cpu_dead(cpu);
  1090. break;
  1091. default:
  1092. break;
  1093. }
  1094. return ret;
  1095. }
  1096. static void __init pmu_check_apic(void)
  1097. {
  1098. if (cpu_has_apic)
  1099. return;
  1100. x86_pmu.apic = 0;
  1101. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1102. pr_info("no hardware sampling interrupt available.\n");
  1103. }
  1104. void __init init_hw_perf_events(void)
  1105. {
  1106. struct event_constraint *c;
  1107. int err;
  1108. pr_info("Performance Events: ");
  1109. switch (boot_cpu_data.x86_vendor) {
  1110. case X86_VENDOR_INTEL:
  1111. err = intel_pmu_init();
  1112. break;
  1113. case X86_VENDOR_AMD:
  1114. err = amd_pmu_init();
  1115. break;
  1116. default:
  1117. return;
  1118. }
  1119. if (err != 0) {
  1120. pr_cont("no PMU driver, software events only.\n");
  1121. return;
  1122. }
  1123. pmu_check_apic();
  1124. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1125. if (x86_pmu.quirks)
  1126. x86_pmu.quirks();
  1127. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1128. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1129. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1130. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1131. }
  1132. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1133. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1134. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1135. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1136. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1137. }
  1138. x86_pmu.intel_ctrl |=
  1139. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1140. perf_events_lapic_init();
  1141. register_die_notifier(&perf_event_nmi_notifier);
  1142. unconstrained = (struct event_constraint)
  1143. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1144. 0, x86_pmu.num_counters);
  1145. if (x86_pmu.event_constraints) {
  1146. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1147. if (c->cmask != X86_RAW_EVENT_MASK)
  1148. continue;
  1149. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1150. c->weight += x86_pmu.num_counters;
  1151. }
  1152. }
  1153. pr_info("... version: %d\n", x86_pmu.version);
  1154. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1155. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1156. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1157. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1158. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1159. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1160. perf_pmu_register(&pmu);
  1161. perf_cpu_notifier(x86_pmu_notifier);
  1162. }
  1163. static inline void x86_pmu_read(struct perf_event *event)
  1164. {
  1165. x86_perf_event_update(event);
  1166. }
  1167. /*
  1168. * Start group events scheduling transaction
  1169. * Set the flag to make pmu::enable() not perform the
  1170. * schedulability test, it will be performed at commit time
  1171. */
  1172. static void x86_pmu_start_txn(struct pmu *pmu)
  1173. {
  1174. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1175. perf_pmu_disable(pmu);
  1176. cpuc->group_flag |= PERF_EVENT_TXN;
  1177. cpuc->n_txn = 0;
  1178. }
  1179. /*
  1180. * Stop group events scheduling transaction
  1181. * Clear the flag and pmu::enable() will perform the
  1182. * schedulability test.
  1183. */
  1184. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1185. {
  1186. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1187. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1188. /*
  1189. * Truncate the collected events.
  1190. */
  1191. cpuc->n_added -= cpuc->n_txn;
  1192. cpuc->n_events -= cpuc->n_txn;
  1193. perf_pmu_enable(pmu);
  1194. }
  1195. /*
  1196. * Commit group events scheduling transaction
  1197. * Perform the group schedulability test as a whole
  1198. * Return 0 if success
  1199. */
  1200. static int x86_pmu_commit_txn(struct pmu *pmu)
  1201. {
  1202. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1203. int assign[X86_PMC_IDX_MAX];
  1204. int n, ret;
  1205. n = cpuc->n_events;
  1206. if (!x86_pmu_initialized())
  1207. return -EAGAIN;
  1208. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1209. if (ret)
  1210. return ret;
  1211. /*
  1212. * copy new assignment, now we know it is possible
  1213. * will be used by hw_perf_enable()
  1214. */
  1215. memcpy(cpuc->assign, assign, n*sizeof(int));
  1216. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1217. perf_pmu_enable(pmu);
  1218. return 0;
  1219. }
  1220. /*
  1221. * validate that we can schedule this event
  1222. */
  1223. static int validate_event(struct perf_event *event)
  1224. {
  1225. struct cpu_hw_events *fake_cpuc;
  1226. struct event_constraint *c;
  1227. int ret = 0;
  1228. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1229. if (!fake_cpuc)
  1230. return -ENOMEM;
  1231. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1232. if (!c || !c->weight)
  1233. ret = -ENOSPC;
  1234. if (x86_pmu.put_event_constraints)
  1235. x86_pmu.put_event_constraints(fake_cpuc, event);
  1236. kfree(fake_cpuc);
  1237. return ret;
  1238. }
  1239. /*
  1240. * validate a single event group
  1241. *
  1242. * validation include:
  1243. * - check events are compatible which each other
  1244. * - events do not compete for the same counter
  1245. * - number of events <= number of counters
  1246. *
  1247. * validation ensures the group can be loaded onto the
  1248. * PMU if it was the only group available.
  1249. */
  1250. static int validate_group(struct perf_event *event)
  1251. {
  1252. struct perf_event *leader = event->group_leader;
  1253. struct cpu_hw_events *fake_cpuc;
  1254. int ret, n;
  1255. ret = -ENOMEM;
  1256. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1257. if (!fake_cpuc)
  1258. goto out;
  1259. /*
  1260. * the event is not yet connected with its
  1261. * siblings therefore we must first collect
  1262. * existing siblings, then add the new event
  1263. * before we can simulate the scheduling
  1264. */
  1265. ret = -ENOSPC;
  1266. n = collect_events(fake_cpuc, leader, true);
  1267. if (n < 0)
  1268. goto out_free;
  1269. fake_cpuc->n_events = n;
  1270. n = collect_events(fake_cpuc, event, false);
  1271. if (n < 0)
  1272. goto out_free;
  1273. fake_cpuc->n_events = n;
  1274. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1275. out_free:
  1276. kfree(fake_cpuc);
  1277. out:
  1278. return ret;
  1279. }
  1280. int x86_pmu_event_init(struct perf_event *event)
  1281. {
  1282. struct pmu *tmp;
  1283. int err;
  1284. switch (event->attr.type) {
  1285. case PERF_TYPE_RAW:
  1286. case PERF_TYPE_HARDWARE:
  1287. case PERF_TYPE_HW_CACHE:
  1288. break;
  1289. default:
  1290. return -ENOENT;
  1291. }
  1292. err = __x86_pmu_event_init(event);
  1293. if (!err) {
  1294. /*
  1295. * we temporarily connect event to its pmu
  1296. * such that validate_group() can classify
  1297. * it as an x86 event using is_x86_event()
  1298. */
  1299. tmp = event->pmu;
  1300. event->pmu = &pmu;
  1301. if (event->group_leader != event)
  1302. err = validate_group(event);
  1303. else
  1304. err = validate_event(event);
  1305. event->pmu = tmp;
  1306. }
  1307. if (err) {
  1308. if (event->destroy)
  1309. event->destroy(event);
  1310. }
  1311. return err;
  1312. }
  1313. static struct pmu pmu = {
  1314. .pmu_enable = x86_pmu_enable,
  1315. .pmu_disable = x86_pmu_disable,
  1316. .event_init = x86_pmu_event_init,
  1317. .add = x86_pmu_add,
  1318. .del = x86_pmu_del,
  1319. .start = x86_pmu_start,
  1320. .stop = x86_pmu_stop,
  1321. .read = x86_pmu_read,
  1322. .start_txn = x86_pmu_start_txn,
  1323. .cancel_txn = x86_pmu_cancel_txn,
  1324. .commit_txn = x86_pmu_commit_txn,
  1325. };
  1326. /*
  1327. * callchain support
  1328. */
  1329. static void
  1330. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1331. {
  1332. /* Ignore warnings */
  1333. }
  1334. static void backtrace_warning(void *data, char *msg)
  1335. {
  1336. /* Ignore warnings */
  1337. }
  1338. static int backtrace_stack(void *data, char *name)
  1339. {
  1340. return 0;
  1341. }
  1342. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1343. {
  1344. struct perf_callchain_entry *entry = data;
  1345. perf_callchain_store(entry, addr);
  1346. }
  1347. static const struct stacktrace_ops backtrace_ops = {
  1348. .warning = backtrace_warning,
  1349. .warning_symbol = backtrace_warning_symbol,
  1350. .stack = backtrace_stack,
  1351. .address = backtrace_address,
  1352. .walk_stack = print_context_stack_bp,
  1353. };
  1354. void
  1355. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1356. {
  1357. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1358. /* TODO: We don't support guest os callchain now */
  1359. return;
  1360. }
  1361. perf_callchain_store(entry, regs->ip);
  1362. dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
  1363. }
  1364. #ifdef CONFIG_COMPAT
  1365. static inline int
  1366. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1367. {
  1368. /* 32-bit process in 64-bit kernel. */
  1369. struct stack_frame_ia32 frame;
  1370. const void __user *fp;
  1371. if (!test_thread_flag(TIF_IA32))
  1372. return 0;
  1373. fp = compat_ptr(regs->bp);
  1374. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1375. unsigned long bytes;
  1376. frame.next_frame = 0;
  1377. frame.return_address = 0;
  1378. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1379. if (bytes != sizeof(frame))
  1380. break;
  1381. if (fp < compat_ptr(regs->sp))
  1382. break;
  1383. perf_callchain_store(entry, frame.return_address);
  1384. fp = compat_ptr(frame.next_frame);
  1385. }
  1386. return 1;
  1387. }
  1388. #else
  1389. static inline int
  1390. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1391. {
  1392. return 0;
  1393. }
  1394. #endif
  1395. void
  1396. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1397. {
  1398. struct stack_frame frame;
  1399. const void __user *fp;
  1400. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1401. /* TODO: We don't support guest os callchain now */
  1402. return;
  1403. }
  1404. fp = (void __user *)regs->bp;
  1405. perf_callchain_store(entry, regs->ip);
  1406. if (perf_callchain_user32(regs, entry))
  1407. return;
  1408. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1409. unsigned long bytes;
  1410. frame.next_frame = NULL;
  1411. frame.return_address = 0;
  1412. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1413. if (bytes != sizeof(frame))
  1414. break;
  1415. if ((unsigned long)fp < regs->sp)
  1416. break;
  1417. perf_callchain_store(entry, frame.return_address);
  1418. fp = frame.next_frame;
  1419. }
  1420. }
  1421. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1422. {
  1423. unsigned long ip;
  1424. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1425. ip = perf_guest_cbs->get_guest_ip();
  1426. else
  1427. ip = instruction_pointer(regs);
  1428. return ip;
  1429. }
  1430. unsigned long perf_misc_flags(struct pt_regs *regs)
  1431. {
  1432. int misc = 0;
  1433. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1434. if (perf_guest_cbs->is_user_mode())
  1435. misc |= PERF_RECORD_MISC_GUEST_USER;
  1436. else
  1437. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1438. } else {
  1439. if (user_mode(regs))
  1440. misc |= PERF_RECORD_MISC_USER;
  1441. else
  1442. misc |= PERF_RECORD_MISC_KERNEL;
  1443. }
  1444. if (regs->flags & PERF_EFLAGS_EXACT)
  1445. misc |= PERF_RECORD_MISC_EXACT_IP;
  1446. return misc;
  1447. }