perf_event.c 36 KB

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