perf_event.c 38 KB

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