perf_event.c 42 KB

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