perf_event.c 40 KB

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