perf_event.c 38 KB

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