perf_event.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. /*
  2. * Performance events x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  10. * Copyright (C) 2009 Google, Inc., Stephane Eranian
  11. *
  12. * For licencing details see kernel-base/COPYING
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/capability.h>
  16. #include <linux/notifier.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/module.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/sched.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/highmem.h>
  25. #include <linux/cpu.h>
  26. #include <linux/bitops.h>
  27. #include <asm/apic.h>
  28. #include <asm/stacktrace.h>
  29. #include <asm/nmi.h>
  30. #include <asm/compat.h>
  31. #if 0
  32. #undef wrmsrl
  33. #define wrmsrl(msr, val) \
  34. do { \
  35. trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
  36. (unsigned long)(val)); \
  37. native_write_msr((msr), (u32)((u64)(val)), \
  38. (u32)((u64)(val) >> 32)); \
  39. } while (0)
  40. #endif
  41. /*
  42. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  43. */
  44. static unsigned long
  45. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  46. {
  47. unsigned long offset, addr = (unsigned long)from;
  48. int type = in_nmi() ? KM_NMI : KM_IRQ0;
  49. unsigned long size, len = 0;
  50. struct page *page;
  51. void *map;
  52. int ret;
  53. do {
  54. ret = __get_user_pages_fast(addr, 1, 0, &page);
  55. if (!ret)
  56. break;
  57. offset = addr & (PAGE_SIZE - 1);
  58. size = min(PAGE_SIZE - offset, n - len);
  59. map = kmap_atomic(page, type);
  60. memcpy(to, map+offset, size);
  61. kunmap_atomic(map, type);
  62. put_page(page);
  63. len += size;
  64. to += size;
  65. addr += size;
  66. } while (len < n);
  67. return len;
  68. }
  69. struct event_constraint {
  70. union {
  71. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  72. u64 idxmsk64;
  73. };
  74. u64 code;
  75. u64 cmask;
  76. int weight;
  77. };
  78. struct amd_nb {
  79. int nb_id; /* NorthBridge id */
  80. int refcnt; /* reference count */
  81. struct perf_event *owners[X86_PMC_IDX_MAX];
  82. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  83. };
  84. #define MAX_LBR_ENTRIES 16
  85. struct cpu_hw_events {
  86. /*
  87. * Generic x86 PMC bits
  88. */
  89. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  90. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  91. int enabled;
  92. int n_events;
  93. int n_added;
  94. int n_txn;
  95. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  96. u64 tags[X86_PMC_IDX_MAX];
  97. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  98. unsigned int group_flag;
  99. /*
  100. * Intel DebugStore bits
  101. */
  102. struct debug_store *ds;
  103. u64 pebs_enabled;
  104. /*
  105. * Intel LBR bits
  106. */
  107. int lbr_users;
  108. void *lbr_context;
  109. struct perf_branch_stack lbr_stack;
  110. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  111. /*
  112. * AMD specific bits
  113. */
  114. struct amd_nb *amd_nb;
  115. };
  116. #define __EVENT_CONSTRAINT(c, n, m, w) {\
  117. { .idxmsk64 = (n) }, \
  118. .code = (c), \
  119. .cmask = (m), \
  120. .weight = (w), \
  121. }
  122. #define EVENT_CONSTRAINT(c, n, m) \
  123. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
  124. /*
  125. * Constraint on the Event code.
  126. */
  127. #define INTEL_EVENT_CONSTRAINT(c, n) \
  128. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  129. /*
  130. * Constraint on the Event code + UMask + fixed-mask
  131. *
  132. * filter mask to validate fixed counter events.
  133. * the following filters disqualify for fixed counters:
  134. * - inv
  135. * - edge
  136. * - cnt-mask
  137. * The other filters are supported by fixed counters.
  138. * The any-thread option is supported starting with v3.
  139. */
  140. #define FIXED_EVENT_CONSTRAINT(c, n) \
  141. EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
  142. /*
  143. * Constraint on the Event code + UMask
  144. */
  145. #define PEBS_EVENT_CONSTRAINT(c, n) \
  146. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  147. #define EVENT_CONSTRAINT_END \
  148. EVENT_CONSTRAINT(0, 0, 0)
  149. #define for_each_event_constraint(e, c) \
  150. for ((e) = (c); (e)->weight; (e)++)
  151. union perf_capabilities {
  152. struct {
  153. u64 lbr_format : 6;
  154. u64 pebs_trap : 1;
  155. u64 pebs_arch_reg : 1;
  156. u64 pebs_format : 4;
  157. u64 smm_freeze : 1;
  158. };
  159. u64 capabilities;
  160. };
  161. /*
  162. * struct x86_pmu - generic x86 pmu
  163. */
  164. struct x86_pmu {
  165. /*
  166. * Generic x86 PMC bits
  167. */
  168. const char *name;
  169. int version;
  170. int (*handle_irq)(struct pt_regs *);
  171. void (*disable_all)(void);
  172. void (*enable_all)(int added);
  173. void (*enable)(struct perf_event *);
  174. void (*disable)(struct perf_event *);
  175. int (*hw_config)(struct perf_event *event);
  176. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  177. unsigned eventsel;
  178. unsigned perfctr;
  179. u64 (*event_map)(int);
  180. int max_events;
  181. int num_counters;
  182. int num_counters_fixed;
  183. int cntval_bits;
  184. u64 cntval_mask;
  185. int apic;
  186. u64 max_period;
  187. struct event_constraint *
  188. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  189. struct perf_event *event);
  190. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  191. struct perf_event *event);
  192. struct event_constraint *event_constraints;
  193. void (*quirks)(void);
  194. int perfctr_second_write;
  195. int (*cpu_prepare)(int cpu);
  196. void (*cpu_starting)(int cpu);
  197. void (*cpu_dying)(int cpu);
  198. void (*cpu_dead)(int cpu);
  199. /*
  200. * Intel Arch Perfmon v2+
  201. */
  202. u64 intel_ctrl;
  203. union perf_capabilities intel_cap;
  204. /*
  205. * Intel DebugStore bits
  206. */
  207. int bts, pebs;
  208. int pebs_record_size;
  209. void (*drain_pebs)(struct pt_regs *regs);
  210. struct event_constraint *pebs_constraints;
  211. /*
  212. * Intel LBR
  213. */
  214. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  215. int lbr_nr; /* hardware stack size */
  216. };
  217. static struct x86_pmu x86_pmu __read_mostly;
  218. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  219. .enabled = 1,
  220. };
  221. static int x86_perf_event_set_period(struct perf_event *event);
  222. /*
  223. * Generalized hw caching related hw_event table, filled
  224. * in on a per model basis. A value of 0 means
  225. * 'not supported', -1 means 'hw_event makes no sense on
  226. * this CPU', any other value means the raw hw_event
  227. * ID.
  228. */
  229. #define C(x) PERF_COUNT_HW_CACHE_##x
  230. static u64 __read_mostly hw_cache_event_ids
  231. [PERF_COUNT_HW_CACHE_MAX]
  232. [PERF_COUNT_HW_CACHE_OP_MAX]
  233. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  234. /*
  235. * Propagate event elapsed time into the generic event.
  236. * Can only be executed on the CPU where the event is active.
  237. * Returns the delta events processed.
  238. */
  239. static u64
  240. x86_perf_event_update(struct perf_event *event)
  241. {
  242. struct hw_perf_event *hwc = &event->hw;
  243. int shift = 64 - x86_pmu.cntval_bits;
  244. u64 prev_raw_count, new_raw_count;
  245. int idx = hwc->idx;
  246. s64 delta;
  247. if (idx == X86_PMC_IDX_FIXED_BTS)
  248. return 0;
  249. /*
  250. * Careful: an NMI might modify the previous event value.
  251. *
  252. * Our tactic to handle this is to first atomically read and
  253. * exchange a new raw count - then add that new-prev delta
  254. * count to the generic event atomically:
  255. */
  256. again:
  257. prev_raw_count = local64_read(&hwc->prev_count);
  258. rdmsrl(hwc->event_base + idx, new_raw_count);
  259. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  260. new_raw_count) != prev_raw_count)
  261. goto again;
  262. /*
  263. * Now we have the new raw value and have updated the prev
  264. * timestamp already. We can now calculate the elapsed delta
  265. * (event-)time and add that to the generic event.
  266. *
  267. * Careful, not all hw sign-extends above the physical width
  268. * of the count.
  269. */
  270. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  271. delta >>= shift;
  272. local64_add(delta, &event->count);
  273. local64_sub(delta, &hwc->period_left);
  274. return new_raw_count;
  275. }
  276. static atomic_t active_events;
  277. static DEFINE_MUTEX(pmc_reserve_mutex);
  278. #ifdef CONFIG_X86_LOCAL_APIC
  279. static bool reserve_pmc_hardware(void)
  280. {
  281. int i;
  282. if (nmi_watchdog == NMI_LOCAL_APIC)
  283. disable_lapic_nmi_watchdog();
  284. for (i = 0; i < x86_pmu.num_counters; i++) {
  285. if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
  286. goto perfctr_fail;
  287. }
  288. for (i = 0; i < x86_pmu.num_counters; i++) {
  289. if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
  290. goto eventsel_fail;
  291. }
  292. return true;
  293. eventsel_fail:
  294. for (i--; i >= 0; i--)
  295. release_evntsel_nmi(x86_pmu.eventsel + i);
  296. i = x86_pmu.num_counters;
  297. perfctr_fail:
  298. for (i--; i >= 0; i--)
  299. release_perfctr_nmi(x86_pmu.perfctr + i);
  300. if (nmi_watchdog == NMI_LOCAL_APIC)
  301. enable_lapic_nmi_watchdog();
  302. return false;
  303. }
  304. static void release_pmc_hardware(void)
  305. {
  306. int i;
  307. for (i = 0; i < x86_pmu.num_counters; i++) {
  308. release_perfctr_nmi(x86_pmu.perfctr + i);
  309. release_evntsel_nmi(x86_pmu.eventsel + i);
  310. }
  311. if (nmi_watchdog == NMI_LOCAL_APIC)
  312. enable_lapic_nmi_watchdog();
  313. }
  314. #else
  315. static bool reserve_pmc_hardware(void) { return true; }
  316. static void release_pmc_hardware(void) {}
  317. #endif
  318. static int reserve_ds_buffers(void);
  319. static void release_ds_buffers(void);
  320. static void hw_perf_event_destroy(struct perf_event *event)
  321. {
  322. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  323. release_pmc_hardware();
  324. release_ds_buffers();
  325. mutex_unlock(&pmc_reserve_mutex);
  326. }
  327. }
  328. static inline int x86_pmu_initialized(void)
  329. {
  330. return x86_pmu.handle_irq != NULL;
  331. }
  332. static inline int
  333. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr)
  334. {
  335. unsigned int cache_type, cache_op, cache_result;
  336. u64 config, val;
  337. config = attr->config;
  338. cache_type = (config >> 0) & 0xff;
  339. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  340. return -EINVAL;
  341. cache_op = (config >> 8) & 0xff;
  342. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  343. return -EINVAL;
  344. cache_result = (config >> 16) & 0xff;
  345. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  346. return -EINVAL;
  347. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  348. if (val == 0)
  349. return -ENOENT;
  350. if (val == -1)
  351. return -EINVAL;
  352. hwc->config |= val;
  353. return 0;
  354. }
  355. static int x86_setup_perfctr(struct perf_event *event)
  356. {
  357. struct perf_event_attr *attr = &event->attr;
  358. struct hw_perf_event *hwc = &event->hw;
  359. u64 config;
  360. if (!hwc->sample_period) {
  361. hwc->sample_period = x86_pmu.max_period;
  362. hwc->last_period = hwc->sample_period;
  363. local64_set(&hwc->period_left, hwc->sample_period);
  364. } else {
  365. /*
  366. * If we have a PMU initialized but no APIC
  367. * interrupts, we cannot sample hardware
  368. * events (user-space has to fall back and
  369. * sample via a hrtimer based software event):
  370. */
  371. if (!x86_pmu.apic)
  372. return -EOPNOTSUPP;
  373. }
  374. if (attr->type == PERF_TYPE_RAW)
  375. return 0;
  376. if (attr->type == PERF_TYPE_HW_CACHE)
  377. return set_ext_hw_attr(hwc, attr);
  378. if (attr->config >= x86_pmu.max_events)
  379. return -EINVAL;
  380. /*
  381. * The generic map:
  382. */
  383. config = x86_pmu.event_map(attr->config);
  384. if (config == 0)
  385. return -ENOENT;
  386. if (config == -1LL)
  387. return -EINVAL;
  388. /*
  389. * Branch tracing:
  390. */
  391. if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  392. (hwc->sample_period == 1)) {
  393. /* BTS is not supported by this architecture. */
  394. if (!x86_pmu.bts)
  395. return -EOPNOTSUPP;
  396. /* BTS is currently only allowed for user-mode. */
  397. if (!attr->exclude_kernel)
  398. return -EOPNOTSUPP;
  399. }
  400. hwc->config |= config;
  401. return 0;
  402. }
  403. static int x86_pmu_hw_config(struct perf_event *event)
  404. {
  405. if (event->attr.precise_ip) {
  406. int precise = 0;
  407. /* Support for constant skid */
  408. if (x86_pmu.pebs)
  409. precise++;
  410. /* Support for IP fixup */
  411. if (x86_pmu.lbr_nr)
  412. precise++;
  413. if (event->attr.precise_ip > precise)
  414. return -EOPNOTSUPP;
  415. }
  416. /*
  417. * Generate PMC IRQs:
  418. * (keep 'enabled' bit clear for now)
  419. */
  420. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  421. /*
  422. * Count user and OS events unless requested not to
  423. */
  424. if (!event->attr.exclude_user)
  425. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  426. if (!event->attr.exclude_kernel)
  427. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  428. if (event->attr.type == PERF_TYPE_RAW)
  429. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  430. return x86_setup_perfctr(event);
  431. }
  432. /*
  433. * Setup the hardware configuration for a given attr_type
  434. */
  435. static int __x86_pmu_event_init(struct perf_event *event)
  436. {
  437. int err;
  438. if (!x86_pmu_initialized())
  439. return -ENODEV;
  440. err = 0;
  441. if (!atomic_inc_not_zero(&active_events)) {
  442. mutex_lock(&pmc_reserve_mutex);
  443. if (atomic_read(&active_events) == 0) {
  444. if (!reserve_pmc_hardware())
  445. err = -EBUSY;
  446. else {
  447. err = reserve_ds_buffers();
  448. if (err)
  449. release_pmc_hardware();
  450. }
  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. x86_pmu.enable(event);
  848. perf_event_update_userpage(event);
  849. }
  850. void perf_event_print_debug(void)
  851. {
  852. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  853. u64 pebs;
  854. struct cpu_hw_events *cpuc;
  855. unsigned long flags;
  856. int cpu, idx;
  857. if (!x86_pmu.num_counters)
  858. return;
  859. local_irq_save(flags);
  860. cpu = smp_processor_id();
  861. cpuc = &per_cpu(cpu_hw_events, cpu);
  862. if (x86_pmu.version >= 2) {
  863. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  864. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  865. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  866. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  867. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  868. pr_info("\n");
  869. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  870. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  871. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  872. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  873. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  874. }
  875. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  876. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  877. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  878. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  879. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  880. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  881. cpu, idx, pmc_ctrl);
  882. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  883. cpu, idx, pmc_count);
  884. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  885. cpu, idx, prev_left);
  886. }
  887. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  888. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  889. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  890. cpu, idx, pmc_count);
  891. }
  892. local_irq_restore(flags);
  893. }
  894. static void x86_pmu_stop(struct perf_event *event, int flags)
  895. {
  896. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  897. struct hw_perf_event *hwc = &event->hw;
  898. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  899. x86_pmu.disable(event);
  900. cpuc->events[hwc->idx] = NULL;
  901. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  902. hwc->state |= PERF_HES_STOPPED;
  903. }
  904. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  905. /*
  906. * Drain the remaining delta count out of a event
  907. * that we are disabling:
  908. */
  909. x86_perf_event_update(event);
  910. hwc->state |= PERF_HES_UPTODATE;
  911. }
  912. }
  913. static void x86_pmu_del(struct perf_event *event, int flags)
  914. {
  915. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  916. int i;
  917. /*
  918. * If we're called during a txn, we don't need to do anything.
  919. * The events never got scheduled and ->cancel_txn will truncate
  920. * the event_list.
  921. */
  922. if (cpuc->group_flag & PERF_EVENT_TXN)
  923. return;
  924. x86_pmu_stop(event, PERF_EF_UPDATE);
  925. for (i = 0; i < cpuc->n_events; i++) {
  926. if (event == cpuc->event_list[i]) {
  927. if (x86_pmu.put_event_constraints)
  928. x86_pmu.put_event_constraints(cpuc, event);
  929. while (++i < cpuc->n_events)
  930. cpuc->event_list[i-1] = cpuc->event_list[i];
  931. --cpuc->n_events;
  932. break;
  933. }
  934. }
  935. perf_event_update_userpage(event);
  936. }
  937. static int x86_pmu_handle_irq(struct pt_regs *regs)
  938. {
  939. struct perf_sample_data data;
  940. struct cpu_hw_events *cpuc;
  941. struct perf_event *event;
  942. struct hw_perf_event *hwc;
  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. continue;
  950. event = cpuc->events[idx];
  951. hwc = &event->hw;
  952. val = x86_perf_event_update(event);
  953. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  954. continue;
  955. /*
  956. * event overflow
  957. */
  958. handled++;
  959. data.period = event->hw.last_period;
  960. if (!x86_perf_event_set_period(event))
  961. continue;
  962. if (perf_event_overflow(event, 1, &data, regs))
  963. x86_pmu_stop(event, 0);
  964. }
  965. if (handled)
  966. inc_irq_stat(apic_perf_irqs);
  967. return handled;
  968. }
  969. void smp_perf_pending_interrupt(struct pt_regs *regs)
  970. {
  971. irq_enter();
  972. ack_APIC_irq();
  973. inc_irq_stat(apic_pending_irqs);
  974. perf_event_do_pending();
  975. irq_exit();
  976. }
  977. void set_perf_event_pending(void)
  978. {
  979. #ifdef CONFIG_X86_LOCAL_APIC
  980. if (!x86_pmu.apic || !x86_pmu_initialized())
  981. return;
  982. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  983. #endif
  984. }
  985. void perf_events_lapic_init(void)
  986. {
  987. if (!x86_pmu.apic || !x86_pmu_initialized())
  988. return;
  989. /*
  990. * Always use NMI for PMU
  991. */
  992. apic_write(APIC_LVTPC, APIC_DM_NMI);
  993. }
  994. struct pmu_nmi_state {
  995. unsigned int marked;
  996. int handled;
  997. };
  998. static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
  999. static int __kprobes
  1000. perf_event_nmi_handler(struct notifier_block *self,
  1001. unsigned long cmd, void *__args)
  1002. {
  1003. struct die_args *args = __args;
  1004. unsigned int this_nmi;
  1005. int handled;
  1006. if (!atomic_read(&active_events))
  1007. return NOTIFY_DONE;
  1008. switch (cmd) {
  1009. case DIE_NMI:
  1010. case DIE_NMI_IPI:
  1011. break;
  1012. case DIE_NMIUNKNOWN:
  1013. this_nmi = percpu_read(irq_stat.__nmi_count);
  1014. if (this_nmi != __get_cpu_var(pmu_nmi).marked)
  1015. /* let the kernel handle the unknown nmi */
  1016. return NOTIFY_DONE;
  1017. /*
  1018. * This one is a PMU back-to-back nmi. Two events
  1019. * trigger 'simultaneously' raising two back-to-back
  1020. * NMIs. If the first NMI handles both, the latter
  1021. * will be empty and daze the CPU. So, we drop it to
  1022. * avoid false-positive 'unknown nmi' messages.
  1023. */
  1024. return NOTIFY_STOP;
  1025. default:
  1026. return NOTIFY_DONE;
  1027. }
  1028. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1029. handled = x86_pmu.handle_irq(args->regs);
  1030. if (!handled)
  1031. return NOTIFY_DONE;
  1032. this_nmi = percpu_read(irq_stat.__nmi_count);
  1033. if ((handled > 1) ||
  1034. /* the next nmi could be a back-to-back nmi */
  1035. ((__get_cpu_var(pmu_nmi).marked == this_nmi) &&
  1036. (__get_cpu_var(pmu_nmi).handled > 1))) {
  1037. /*
  1038. * We could have two subsequent back-to-back nmis: The
  1039. * first handles more than one counter, the 2nd
  1040. * handles only one counter and the 3rd handles no
  1041. * counter.
  1042. *
  1043. * This is the 2nd nmi because the previous was
  1044. * handling more than one counter. We will mark the
  1045. * next (3rd) and then drop it if unhandled.
  1046. */
  1047. __get_cpu_var(pmu_nmi).marked = this_nmi + 1;
  1048. __get_cpu_var(pmu_nmi).handled = handled;
  1049. }
  1050. return NOTIFY_STOP;
  1051. }
  1052. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1053. .notifier_call = perf_event_nmi_handler,
  1054. .next = NULL,
  1055. .priority = 1
  1056. };
  1057. static struct event_constraint unconstrained;
  1058. static struct event_constraint emptyconstraint;
  1059. static struct event_constraint *
  1060. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1061. {
  1062. struct event_constraint *c;
  1063. if (x86_pmu.event_constraints) {
  1064. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1065. if ((event->hw.config & c->cmask) == c->code)
  1066. return c;
  1067. }
  1068. }
  1069. return &unconstrained;
  1070. }
  1071. #include "perf_event_amd.c"
  1072. #include "perf_event_p6.c"
  1073. #include "perf_event_p4.c"
  1074. #include "perf_event_intel_lbr.c"
  1075. #include "perf_event_intel_ds.c"
  1076. #include "perf_event_intel.c"
  1077. static int __cpuinit
  1078. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1079. {
  1080. unsigned int cpu = (long)hcpu;
  1081. int ret = NOTIFY_OK;
  1082. switch (action & ~CPU_TASKS_FROZEN) {
  1083. case CPU_UP_PREPARE:
  1084. if (x86_pmu.cpu_prepare)
  1085. ret = x86_pmu.cpu_prepare(cpu);
  1086. break;
  1087. case CPU_STARTING:
  1088. if (x86_pmu.cpu_starting)
  1089. x86_pmu.cpu_starting(cpu);
  1090. break;
  1091. case CPU_DYING:
  1092. if (x86_pmu.cpu_dying)
  1093. x86_pmu.cpu_dying(cpu);
  1094. break;
  1095. case CPU_UP_CANCELED:
  1096. case CPU_DEAD:
  1097. if (x86_pmu.cpu_dead)
  1098. x86_pmu.cpu_dead(cpu);
  1099. break;
  1100. default:
  1101. break;
  1102. }
  1103. return ret;
  1104. }
  1105. static void __init pmu_check_apic(void)
  1106. {
  1107. if (cpu_has_apic)
  1108. return;
  1109. x86_pmu.apic = 0;
  1110. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1111. pr_info("no hardware sampling interrupt available.\n");
  1112. }
  1113. void __init init_hw_perf_events(void)
  1114. {
  1115. struct event_constraint *c;
  1116. int err;
  1117. pr_info("Performance Events: ");
  1118. switch (boot_cpu_data.x86_vendor) {
  1119. case X86_VENDOR_INTEL:
  1120. err = intel_pmu_init();
  1121. break;
  1122. case X86_VENDOR_AMD:
  1123. err = amd_pmu_init();
  1124. break;
  1125. default:
  1126. return;
  1127. }
  1128. if (err != 0) {
  1129. pr_cont("no PMU driver, software events only.\n");
  1130. return;
  1131. }
  1132. pmu_check_apic();
  1133. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1134. if (x86_pmu.quirks)
  1135. x86_pmu.quirks();
  1136. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1137. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1138. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1139. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1140. }
  1141. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1142. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1143. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1144. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1145. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1146. }
  1147. x86_pmu.intel_ctrl |=
  1148. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1149. perf_events_lapic_init();
  1150. register_die_notifier(&perf_event_nmi_notifier);
  1151. unconstrained = (struct event_constraint)
  1152. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1153. 0, x86_pmu.num_counters);
  1154. if (x86_pmu.event_constraints) {
  1155. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1156. if (c->cmask != X86_RAW_EVENT_MASK)
  1157. continue;
  1158. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1159. c->weight += x86_pmu.num_counters;
  1160. }
  1161. }
  1162. pr_info("... version: %d\n", x86_pmu.version);
  1163. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1164. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1165. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1166. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1167. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1168. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1169. perf_pmu_register(&pmu);
  1170. perf_cpu_notifier(x86_pmu_notifier);
  1171. }
  1172. static inline void x86_pmu_read(struct perf_event *event)
  1173. {
  1174. x86_perf_event_update(event);
  1175. }
  1176. /*
  1177. * Start group events scheduling transaction
  1178. * Set the flag to make pmu::enable() not perform the
  1179. * schedulability test, it will be performed at commit time
  1180. */
  1181. static void x86_pmu_start_txn(struct pmu *pmu)
  1182. {
  1183. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1184. perf_pmu_disable(pmu);
  1185. cpuc->group_flag |= PERF_EVENT_TXN;
  1186. cpuc->n_txn = 0;
  1187. }
  1188. /*
  1189. * Stop group events scheduling transaction
  1190. * Clear the flag and pmu::enable() will perform the
  1191. * schedulability test.
  1192. */
  1193. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1194. {
  1195. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1196. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1197. /*
  1198. * Truncate the collected events.
  1199. */
  1200. cpuc->n_added -= cpuc->n_txn;
  1201. cpuc->n_events -= cpuc->n_txn;
  1202. perf_pmu_enable(pmu);
  1203. }
  1204. /*
  1205. * Commit group events scheduling transaction
  1206. * Perform the group schedulability test as a whole
  1207. * Return 0 if success
  1208. */
  1209. static int x86_pmu_commit_txn(struct pmu *pmu)
  1210. {
  1211. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1212. int assign[X86_PMC_IDX_MAX];
  1213. int n, ret;
  1214. n = cpuc->n_events;
  1215. if (!x86_pmu_initialized())
  1216. return -EAGAIN;
  1217. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1218. if (ret)
  1219. return ret;
  1220. /*
  1221. * copy new assignment, now we know it is possible
  1222. * will be used by hw_perf_enable()
  1223. */
  1224. memcpy(cpuc->assign, assign, n*sizeof(int));
  1225. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1226. perf_pmu_enable(pmu);
  1227. return 0;
  1228. }
  1229. /*
  1230. * validate that we can schedule this event
  1231. */
  1232. static int validate_event(struct perf_event *event)
  1233. {
  1234. struct cpu_hw_events *fake_cpuc;
  1235. struct event_constraint *c;
  1236. int ret = 0;
  1237. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1238. if (!fake_cpuc)
  1239. return -ENOMEM;
  1240. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1241. if (!c || !c->weight)
  1242. ret = -ENOSPC;
  1243. if (x86_pmu.put_event_constraints)
  1244. x86_pmu.put_event_constraints(fake_cpuc, event);
  1245. kfree(fake_cpuc);
  1246. return ret;
  1247. }
  1248. /*
  1249. * validate a single event group
  1250. *
  1251. * validation include:
  1252. * - check events are compatible which each other
  1253. * - events do not compete for the same counter
  1254. * - number of events <= number of counters
  1255. *
  1256. * validation ensures the group can be loaded onto the
  1257. * PMU if it was the only group available.
  1258. */
  1259. static int validate_group(struct perf_event *event)
  1260. {
  1261. struct perf_event *leader = event->group_leader;
  1262. struct cpu_hw_events *fake_cpuc;
  1263. int ret, n;
  1264. ret = -ENOMEM;
  1265. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1266. if (!fake_cpuc)
  1267. goto out;
  1268. /*
  1269. * the event is not yet connected with its
  1270. * siblings therefore we must first collect
  1271. * existing siblings, then add the new event
  1272. * before we can simulate the scheduling
  1273. */
  1274. ret = -ENOSPC;
  1275. n = collect_events(fake_cpuc, leader, true);
  1276. if (n < 0)
  1277. goto out_free;
  1278. fake_cpuc->n_events = n;
  1279. n = collect_events(fake_cpuc, event, false);
  1280. if (n < 0)
  1281. goto out_free;
  1282. fake_cpuc->n_events = n;
  1283. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1284. out_free:
  1285. kfree(fake_cpuc);
  1286. out:
  1287. return ret;
  1288. }
  1289. int x86_pmu_event_init(struct perf_event *event)
  1290. {
  1291. struct pmu *tmp;
  1292. int err;
  1293. switch (event->attr.type) {
  1294. case PERF_TYPE_RAW:
  1295. case PERF_TYPE_HARDWARE:
  1296. case PERF_TYPE_HW_CACHE:
  1297. break;
  1298. default:
  1299. return -ENOENT;
  1300. }
  1301. err = __x86_pmu_event_init(event);
  1302. if (!err) {
  1303. /*
  1304. * we temporarily connect event to its pmu
  1305. * such that validate_group() can classify
  1306. * it as an x86 event using is_x86_event()
  1307. */
  1308. tmp = event->pmu;
  1309. event->pmu = &pmu;
  1310. if (event->group_leader != event)
  1311. err = validate_group(event);
  1312. else
  1313. err = validate_event(event);
  1314. event->pmu = tmp;
  1315. }
  1316. if (err) {
  1317. if (event->destroy)
  1318. event->destroy(event);
  1319. }
  1320. return err;
  1321. }
  1322. static struct pmu pmu = {
  1323. .pmu_enable = x86_pmu_enable,
  1324. .pmu_disable = x86_pmu_disable,
  1325. .event_init = x86_pmu_event_init,
  1326. .add = x86_pmu_add,
  1327. .del = x86_pmu_del,
  1328. .start = x86_pmu_start,
  1329. .stop = x86_pmu_stop,
  1330. .read = x86_pmu_read,
  1331. .start_txn = x86_pmu_start_txn,
  1332. .cancel_txn = x86_pmu_cancel_txn,
  1333. .commit_txn = x86_pmu_commit_txn,
  1334. };
  1335. /*
  1336. * callchain support
  1337. */
  1338. static void
  1339. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1340. {
  1341. /* Ignore warnings */
  1342. }
  1343. static void backtrace_warning(void *data, char *msg)
  1344. {
  1345. /* Ignore warnings */
  1346. }
  1347. static int backtrace_stack(void *data, char *name)
  1348. {
  1349. return 0;
  1350. }
  1351. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1352. {
  1353. struct perf_callchain_entry *entry = data;
  1354. perf_callchain_store(entry, addr);
  1355. }
  1356. static const struct stacktrace_ops backtrace_ops = {
  1357. .warning = backtrace_warning,
  1358. .warning_symbol = backtrace_warning_symbol,
  1359. .stack = backtrace_stack,
  1360. .address = backtrace_address,
  1361. .walk_stack = print_context_stack_bp,
  1362. };
  1363. void
  1364. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1365. {
  1366. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1367. /* TODO: We don't support guest os callchain now */
  1368. return;
  1369. }
  1370. perf_callchain_store(entry, regs->ip);
  1371. dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
  1372. }
  1373. #ifdef CONFIG_COMPAT
  1374. static inline int
  1375. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1376. {
  1377. /* 32-bit process in 64-bit kernel. */
  1378. struct stack_frame_ia32 frame;
  1379. const void __user *fp;
  1380. if (!test_thread_flag(TIF_IA32))
  1381. return 0;
  1382. fp = compat_ptr(regs->bp);
  1383. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1384. unsigned long bytes;
  1385. frame.next_frame = 0;
  1386. frame.return_address = 0;
  1387. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1388. if (bytes != sizeof(frame))
  1389. break;
  1390. if (fp < compat_ptr(regs->sp))
  1391. break;
  1392. perf_callchain_store(entry, frame.return_address);
  1393. fp = compat_ptr(frame.next_frame);
  1394. }
  1395. return 1;
  1396. }
  1397. #else
  1398. static inline int
  1399. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1400. {
  1401. return 0;
  1402. }
  1403. #endif
  1404. void
  1405. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1406. {
  1407. struct stack_frame frame;
  1408. const void __user *fp;
  1409. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1410. /* TODO: We don't support guest os callchain now */
  1411. return;
  1412. }
  1413. fp = (void __user *)regs->bp;
  1414. perf_callchain_store(entry, regs->ip);
  1415. if (perf_callchain_user32(regs, entry))
  1416. return;
  1417. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1418. unsigned long bytes;
  1419. frame.next_frame = NULL;
  1420. frame.return_address = 0;
  1421. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1422. if (bytes != sizeof(frame))
  1423. break;
  1424. if ((unsigned long)fp < regs->sp)
  1425. break;
  1426. perf_callchain_store(entry, frame.return_address);
  1427. fp = frame.next_frame;
  1428. }
  1429. }
  1430. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1431. {
  1432. unsigned long ip;
  1433. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1434. ip = perf_guest_cbs->get_guest_ip();
  1435. else
  1436. ip = instruction_pointer(regs);
  1437. return ip;
  1438. }
  1439. unsigned long perf_misc_flags(struct pt_regs *regs)
  1440. {
  1441. int misc = 0;
  1442. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1443. if (perf_guest_cbs->is_user_mode())
  1444. misc |= PERF_RECORD_MISC_GUEST_USER;
  1445. else
  1446. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1447. } else {
  1448. if (user_mode(regs))
  1449. misc |= PERF_RECORD_MISC_USER;
  1450. else
  1451. misc |= PERF_RECORD_MISC_KERNEL;
  1452. }
  1453. if (regs->flags & PERF_EFLAGS_EXACT)
  1454. misc |= PERF_RECORD_MISC_EXACT_IP;
  1455. return misc;
  1456. }