perf_event.c 39 KB

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