perf_event.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /*
  2. * Performance events x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  10. * Copyright (C) 2009 Google, Inc., Stephane Eranian
  11. *
  12. * For licencing details see kernel-base/COPYING
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/capability.h>
  16. #include <linux/notifier.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/module.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/sched.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/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. #include <asm/smp.h>
  31. #include <asm/alternative.h>
  32. #include "perf_event.h"
  33. #if 0
  34. #undef wrmsrl
  35. #define wrmsrl(msr, val) \
  36. do { \
  37. trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
  38. (unsigned long)(val)); \
  39. native_write_msr((msr), (u32)((u64)(val)), \
  40. (u32)((u64)(val) >> 32)); \
  41. } while (0)
  42. #endif
  43. struct x86_pmu x86_pmu __read_mostly;
  44. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  45. .enabled = 1,
  46. };
  47. u64 __read_mostly hw_cache_event_ids
  48. [PERF_COUNT_HW_CACHE_MAX]
  49. [PERF_COUNT_HW_CACHE_OP_MAX]
  50. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  51. u64 __read_mostly hw_cache_extra_regs
  52. [PERF_COUNT_HW_CACHE_MAX]
  53. [PERF_COUNT_HW_CACHE_OP_MAX]
  54. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  55. /*
  56. * Propagate event elapsed time into the generic event.
  57. * Can only be executed on the CPU where the event is active.
  58. * Returns the delta events processed.
  59. */
  60. u64 x86_perf_event_update(struct perf_event *event)
  61. {
  62. struct hw_perf_event *hwc = &event->hw;
  63. int shift = 64 - x86_pmu.cntval_bits;
  64. u64 prev_raw_count, new_raw_count;
  65. int idx = hwc->idx;
  66. s64 delta;
  67. if (idx == X86_PMC_IDX_FIXED_BTS)
  68. return 0;
  69. /*
  70. * Careful: an NMI might modify the previous event value.
  71. *
  72. * Our tactic to handle this is to first atomically read and
  73. * exchange a new raw count - then add that new-prev delta
  74. * count to the generic event atomically:
  75. */
  76. again:
  77. prev_raw_count = local64_read(&hwc->prev_count);
  78. rdmsrl(hwc->event_base, new_raw_count);
  79. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  80. new_raw_count) != prev_raw_count)
  81. goto again;
  82. /*
  83. * Now we have the new raw value and have updated the prev
  84. * timestamp already. We can now calculate the elapsed delta
  85. * (event-)time and add that to the generic event.
  86. *
  87. * Careful, not all hw sign-extends above the physical width
  88. * of the count.
  89. */
  90. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  91. delta >>= shift;
  92. local64_add(delta, &event->count);
  93. local64_sub(delta, &hwc->period_left);
  94. return new_raw_count;
  95. }
  96. /*
  97. * Find and validate any extra registers to set up.
  98. */
  99. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  100. {
  101. struct hw_perf_event_extra *reg;
  102. struct extra_reg *er;
  103. reg = &event->hw.extra_reg;
  104. if (!x86_pmu.extra_regs)
  105. return 0;
  106. for (er = x86_pmu.extra_regs; er->msr; er++) {
  107. if (er->event != (config & er->config_mask))
  108. continue;
  109. if (event->attr.config1 & ~er->valid_mask)
  110. return -EINVAL;
  111. reg->idx = er->idx;
  112. reg->config = event->attr.config1;
  113. reg->reg = er->msr;
  114. break;
  115. }
  116. return 0;
  117. }
  118. static atomic_t active_events;
  119. static DEFINE_MUTEX(pmc_reserve_mutex);
  120. #ifdef CONFIG_X86_LOCAL_APIC
  121. static bool reserve_pmc_hardware(void)
  122. {
  123. int i;
  124. for (i = 0; i < x86_pmu.num_counters; i++) {
  125. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  126. goto perfctr_fail;
  127. }
  128. for (i = 0; i < x86_pmu.num_counters; i++) {
  129. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  130. goto eventsel_fail;
  131. }
  132. return true;
  133. eventsel_fail:
  134. for (i--; i >= 0; i--)
  135. release_evntsel_nmi(x86_pmu_config_addr(i));
  136. i = x86_pmu.num_counters;
  137. perfctr_fail:
  138. for (i--; i >= 0; i--)
  139. release_perfctr_nmi(x86_pmu_event_addr(i));
  140. return false;
  141. }
  142. static void release_pmc_hardware(void)
  143. {
  144. int i;
  145. for (i = 0; i < x86_pmu.num_counters; i++) {
  146. release_perfctr_nmi(x86_pmu_event_addr(i));
  147. release_evntsel_nmi(x86_pmu_config_addr(i));
  148. }
  149. }
  150. #else
  151. static bool reserve_pmc_hardware(void) { return true; }
  152. static void release_pmc_hardware(void) {}
  153. #endif
  154. static bool check_hw_exists(void)
  155. {
  156. u64 val, val_new = 0;
  157. int i, reg, ret = 0;
  158. /*
  159. * Check to see if the BIOS enabled any of the counters, if so
  160. * complain and bail.
  161. */
  162. for (i = 0; i < x86_pmu.num_counters; i++) {
  163. reg = x86_pmu_config_addr(i);
  164. ret = rdmsrl_safe(reg, &val);
  165. if (ret)
  166. goto msr_fail;
  167. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  168. goto bios_fail;
  169. }
  170. if (x86_pmu.num_counters_fixed) {
  171. reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  172. ret = rdmsrl_safe(reg, &val);
  173. if (ret)
  174. goto msr_fail;
  175. for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
  176. if (val & (0x03 << i*4))
  177. goto bios_fail;
  178. }
  179. }
  180. /*
  181. * Now write a value and read it back to see if it matches,
  182. * this is needed to detect certain hardware emulators (qemu/kvm)
  183. * that don't trap on the MSR access and always return 0s.
  184. */
  185. val = 0xabcdUL;
  186. ret = checking_wrmsrl(x86_pmu_event_addr(0), val);
  187. ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new);
  188. if (ret || val != val_new)
  189. goto msr_fail;
  190. return true;
  191. bios_fail:
  192. /*
  193. * We still allow the PMU driver to operate:
  194. */
  195. printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
  196. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
  197. return true;
  198. msr_fail:
  199. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  200. return false;
  201. }
  202. static void hw_perf_event_destroy(struct perf_event *event)
  203. {
  204. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  205. release_pmc_hardware();
  206. release_ds_buffers();
  207. mutex_unlock(&pmc_reserve_mutex);
  208. }
  209. }
  210. static inline int x86_pmu_initialized(void)
  211. {
  212. return x86_pmu.handle_irq != NULL;
  213. }
  214. static inline int
  215. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  216. {
  217. struct perf_event_attr *attr = &event->attr;
  218. unsigned int cache_type, cache_op, cache_result;
  219. u64 config, val;
  220. config = attr->config;
  221. cache_type = (config >> 0) & 0xff;
  222. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  223. return -EINVAL;
  224. cache_op = (config >> 8) & 0xff;
  225. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  226. return -EINVAL;
  227. cache_result = (config >> 16) & 0xff;
  228. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  229. return -EINVAL;
  230. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  231. if (val == 0)
  232. return -ENOENT;
  233. if (val == -1)
  234. return -EINVAL;
  235. hwc->config |= val;
  236. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  237. return x86_pmu_extra_regs(val, event);
  238. }
  239. int x86_setup_perfctr(struct perf_event *event)
  240. {
  241. struct perf_event_attr *attr = &event->attr;
  242. struct hw_perf_event *hwc = &event->hw;
  243. u64 config;
  244. if (!is_sampling_event(event)) {
  245. hwc->sample_period = x86_pmu.max_period;
  246. hwc->last_period = hwc->sample_period;
  247. local64_set(&hwc->period_left, hwc->sample_period);
  248. } else {
  249. /*
  250. * If we have a PMU initialized but no APIC
  251. * interrupts, we cannot sample hardware
  252. * events (user-space has to fall back and
  253. * sample via a hrtimer based software event):
  254. */
  255. if (!x86_pmu.apic)
  256. return -EOPNOTSUPP;
  257. }
  258. if (attr->type == PERF_TYPE_RAW)
  259. return x86_pmu_extra_regs(event->attr.config, event);
  260. if (attr->type == PERF_TYPE_HW_CACHE)
  261. return set_ext_hw_attr(hwc, event);
  262. if (attr->config >= x86_pmu.max_events)
  263. return -EINVAL;
  264. /*
  265. * The generic map:
  266. */
  267. config = x86_pmu.event_map(attr->config);
  268. if (config == 0)
  269. return -ENOENT;
  270. if (config == -1LL)
  271. return -EINVAL;
  272. /*
  273. * Branch tracing:
  274. */
  275. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  276. !attr->freq && hwc->sample_period == 1) {
  277. /* BTS is not supported by this architecture. */
  278. if (!x86_pmu.bts_active)
  279. return -EOPNOTSUPP;
  280. /* BTS is currently only allowed for user-mode. */
  281. if (!attr->exclude_kernel)
  282. return -EOPNOTSUPP;
  283. }
  284. hwc->config |= config;
  285. return 0;
  286. }
  287. int x86_pmu_hw_config(struct perf_event *event)
  288. {
  289. if (event->attr.precise_ip) {
  290. int precise = 0;
  291. /* Support for constant skid */
  292. if (x86_pmu.pebs_active) {
  293. precise++;
  294. /* Support for IP fixup */
  295. if (x86_pmu.lbr_nr)
  296. precise++;
  297. }
  298. if (event->attr.precise_ip > precise)
  299. return -EOPNOTSUPP;
  300. }
  301. /*
  302. * Generate PMC IRQs:
  303. * (keep 'enabled' bit clear for now)
  304. */
  305. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  306. /*
  307. * Count user and OS events unless requested not to
  308. */
  309. if (!event->attr.exclude_user)
  310. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  311. if (!event->attr.exclude_kernel)
  312. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  313. if (event->attr.type == PERF_TYPE_RAW)
  314. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  315. return x86_setup_perfctr(event);
  316. }
  317. /*
  318. * Setup the hardware configuration for a given attr_type
  319. */
  320. static int __x86_pmu_event_init(struct perf_event *event)
  321. {
  322. int err;
  323. if (!x86_pmu_initialized())
  324. return -ENODEV;
  325. err = 0;
  326. if (!atomic_inc_not_zero(&active_events)) {
  327. mutex_lock(&pmc_reserve_mutex);
  328. if (atomic_read(&active_events) == 0) {
  329. if (!reserve_pmc_hardware())
  330. err = -EBUSY;
  331. else
  332. reserve_ds_buffers();
  333. }
  334. if (!err)
  335. atomic_inc(&active_events);
  336. mutex_unlock(&pmc_reserve_mutex);
  337. }
  338. if (err)
  339. return err;
  340. event->destroy = hw_perf_event_destroy;
  341. event->hw.idx = -1;
  342. event->hw.last_cpu = -1;
  343. event->hw.last_tag = ~0ULL;
  344. /* mark unused */
  345. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  346. return x86_pmu.hw_config(event);
  347. }
  348. void x86_pmu_disable_all(void)
  349. {
  350. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  351. int idx;
  352. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  353. u64 val;
  354. if (!test_bit(idx, cpuc->active_mask))
  355. continue;
  356. rdmsrl(x86_pmu_config_addr(idx), val);
  357. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  358. continue;
  359. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  360. wrmsrl(x86_pmu_config_addr(idx), val);
  361. }
  362. }
  363. static void x86_pmu_disable(struct pmu *pmu)
  364. {
  365. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  366. if (!x86_pmu_initialized())
  367. return;
  368. if (!cpuc->enabled)
  369. return;
  370. cpuc->n_added = 0;
  371. cpuc->enabled = 0;
  372. barrier();
  373. x86_pmu.disable_all();
  374. }
  375. void x86_pmu_enable_all(int added)
  376. {
  377. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  378. int idx;
  379. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  380. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  381. if (!test_bit(idx, cpuc->active_mask))
  382. continue;
  383. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  384. }
  385. }
  386. static struct pmu pmu;
  387. static inline int is_x86_event(struct perf_event *event)
  388. {
  389. return event->pmu == &pmu;
  390. }
  391. /*
  392. * Event scheduler state:
  393. *
  394. * Assign events iterating over all events and counters, beginning
  395. * with events with least weights first. Keep the current iterator
  396. * state in struct sched_state.
  397. */
  398. struct sched_state {
  399. int weight;
  400. int event; /* event index */
  401. int counter; /* counter index */
  402. int unassigned; /* number of events to be assigned left */
  403. unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  404. };
  405. /* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */
  406. #define SCHED_STATES_MAX 2
  407. struct perf_sched {
  408. int max_weight;
  409. int max_events;
  410. struct event_constraint **constraints;
  411. struct sched_state state;
  412. int saved_states;
  413. struct sched_state saved[SCHED_STATES_MAX];
  414. };
  415. /*
  416. * Initialize interator that runs through all events and counters.
  417. */
  418. static void perf_sched_init(struct perf_sched *sched, struct event_constraint **c,
  419. int num, int wmin, int wmax)
  420. {
  421. int idx;
  422. memset(sched, 0, sizeof(*sched));
  423. sched->max_events = num;
  424. sched->max_weight = wmax;
  425. sched->constraints = c;
  426. for (idx = 0; idx < num; idx++) {
  427. if (c[idx]->weight == wmin)
  428. break;
  429. }
  430. sched->state.event = idx; /* start with min weight */
  431. sched->state.weight = wmin;
  432. sched->state.unassigned = num;
  433. }
  434. static void perf_sched_save_state(struct perf_sched *sched)
  435. {
  436. if (WARN_ON_ONCE(sched->saved_states >= SCHED_STATES_MAX))
  437. return;
  438. sched->saved[sched->saved_states] = sched->state;
  439. sched->saved_states++;
  440. }
  441. static bool perf_sched_restore_state(struct perf_sched *sched)
  442. {
  443. if (!sched->saved_states)
  444. return false;
  445. sched->saved_states--;
  446. sched->state = sched->saved[sched->saved_states];
  447. /* continue with next counter: */
  448. clear_bit(sched->state.counter++, sched->state.used);
  449. return true;
  450. }
  451. /*
  452. * Select a counter for the current event to schedule. Return true on
  453. * success.
  454. */
  455. static bool __perf_sched_find_counter(struct perf_sched *sched)
  456. {
  457. struct event_constraint *c;
  458. int idx;
  459. if (!sched->state.unassigned)
  460. return false;
  461. if (sched->state.event >= sched->max_events)
  462. return false;
  463. c = sched->constraints[sched->state.event];
  464. /* Prefer fixed purpose counters */
  465. if (x86_pmu.num_counters_fixed) {
  466. idx = X86_PMC_IDX_FIXED;
  467. for_each_set_bit_cont(idx, c->idxmsk, X86_PMC_IDX_MAX) {
  468. if (!__test_and_set_bit(idx, sched->state.used))
  469. goto done;
  470. }
  471. }
  472. /* Grab the first unused counter starting with idx */
  473. idx = sched->state.counter;
  474. for_each_set_bit_cont(idx, c->idxmsk, X86_PMC_IDX_FIXED) {
  475. if (!__test_and_set_bit(idx, sched->state.used))
  476. goto done;
  477. }
  478. return false;
  479. done:
  480. sched->state.counter = idx;
  481. if (c->overlap)
  482. perf_sched_save_state(sched);
  483. return true;
  484. }
  485. static bool perf_sched_find_counter(struct perf_sched *sched)
  486. {
  487. while (!__perf_sched_find_counter(sched)) {
  488. if (!perf_sched_restore_state(sched))
  489. return false;
  490. }
  491. return true;
  492. }
  493. /*
  494. * Go through all unassigned events and find the next one to schedule.
  495. * Take events with the least weight first. Return true on success.
  496. */
  497. static bool perf_sched_next_event(struct perf_sched *sched)
  498. {
  499. struct event_constraint *c;
  500. if (!sched->state.unassigned || !--sched->state.unassigned)
  501. return false;
  502. do {
  503. /* next event */
  504. sched->state.event++;
  505. if (sched->state.event >= sched->max_events) {
  506. /* next weight */
  507. sched->state.event = 0;
  508. sched->state.weight++;
  509. if (sched->state.weight > sched->max_weight)
  510. return false;
  511. }
  512. c = sched->constraints[sched->state.event];
  513. } while (c->weight != sched->state.weight);
  514. sched->state.counter = 0; /* start with first counter */
  515. return true;
  516. }
  517. /*
  518. * Assign a counter for each event.
  519. */
  520. static int perf_assign_events(struct event_constraint **constraints, int n,
  521. int wmin, int wmax, int *assign)
  522. {
  523. struct perf_sched sched;
  524. perf_sched_init(&sched, constraints, n, wmin, wmax);
  525. do {
  526. if (!perf_sched_find_counter(&sched))
  527. break; /* failed */
  528. if (assign)
  529. assign[sched.state.event] = sched.state.counter;
  530. } while (perf_sched_next_event(&sched));
  531. return sched.state.unassigned;
  532. }
  533. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  534. {
  535. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  536. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  537. int i, wmin, wmax, num = 0;
  538. struct hw_perf_event *hwc;
  539. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  540. for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
  541. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  542. constraints[i] = c;
  543. wmin = min(wmin, c->weight);
  544. wmax = max(wmax, c->weight);
  545. }
  546. /*
  547. * fastpath, try to reuse previous register
  548. */
  549. for (i = 0; i < n; i++) {
  550. hwc = &cpuc->event_list[i]->hw;
  551. c = constraints[i];
  552. /* never assigned */
  553. if (hwc->idx == -1)
  554. break;
  555. /* constraint still honored */
  556. if (!test_bit(hwc->idx, c->idxmsk))
  557. break;
  558. /* not already used */
  559. if (test_bit(hwc->idx, used_mask))
  560. break;
  561. __set_bit(hwc->idx, used_mask);
  562. if (assign)
  563. assign[i] = hwc->idx;
  564. }
  565. /* slow path */
  566. if (i != n)
  567. num = perf_assign_events(constraints, n, wmin, wmax, assign);
  568. /*
  569. * scheduling failed or is just a simulation,
  570. * free resources if necessary
  571. */
  572. if (!assign || num) {
  573. for (i = 0; i < n; i++) {
  574. if (x86_pmu.put_event_constraints)
  575. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  576. }
  577. }
  578. return num ? -EINVAL : 0;
  579. }
  580. /*
  581. * dogrp: true if must collect siblings events (group)
  582. * returns total number of events and error code
  583. */
  584. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  585. {
  586. struct perf_event *event;
  587. int n, max_count;
  588. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  589. /* current number of events already accepted */
  590. n = cpuc->n_events;
  591. if (is_x86_event(leader)) {
  592. if (n >= max_count)
  593. return -EINVAL;
  594. cpuc->event_list[n] = leader;
  595. n++;
  596. }
  597. if (!dogrp)
  598. return n;
  599. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  600. if (!is_x86_event(event) ||
  601. event->state <= PERF_EVENT_STATE_OFF)
  602. continue;
  603. if (n >= max_count)
  604. return -EINVAL;
  605. cpuc->event_list[n] = event;
  606. n++;
  607. }
  608. return n;
  609. }
  610. static inline void x86_assign_hw_event(struct perf_event *event,
  611. struct cpu_hw_events *cpuc, int i)
  612. {
  613. struct hw_perf_event *hwc = &event->hw;
  614. hwc->idx = cpuc->assign[i];
  615. hwc->last_cpu = smp_processor_id();
  616. hwc->last_tag = ++cpuc->tags[i];
  617. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  618. hwc->config_base = 0;
  619. hwc->event_base = 0;
  620. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  621. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  622. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED);
  623. } else {
  624. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  625. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  626. }
  627. }
  628. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  629. struct cpu_hw_events *cpuc,
  630. int i)
  631. {
  632. return hwc->idx == cpuc->assign[i] &&
  633. hwc->last_cpu == smp_processor_id() &&
  634. hwc->last_tag == cpuc->tags[i];
  635. }
  636. static void x86_pmu_start(struct perf_event *event, int flags);
  637. static void x86_pmu_enable(struct pmu *pmu)
  638. {
  639. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  640. struct perf_event *event;
  641. struct hw_perf_event *hwc;
  642. int i, added = cpuc->n_added;
  643. if (!x86_pmu_initialized())
  644. return;
  645. if (cpuc->enabled)
  646. return;
  647. if (cpuc->n_added) {
  648. int n_running = cpuc->n_events - cpuc->n_added;
  649. /*
  650. * apply assignment obtained either from
  651. * hw_perf_group_sched_in() or x86_pmu_enable()
  652. *
  653. * step1: save events moving to new counters
  654. * step2: reprogram moved events into new counters
  655. */
  656. for (i = 0; i < n_running; i++) {
  657. event = cpuc->event_list[i];
  658. hwc = &event->hw;
  659. /*
  660. * we can avoid reprogramming counter if:
  661. * - assigned same counter as last time
  662. * - running on same CPU as last time
  663. * - no other event has used the counter since
  664. */
  665. if (hwc->idx == -1 ||
  666. match_prev_assignment(hwc, cpuc, i))
  667. continue;
  668. /*
  669. * Ensure we don't accidentally enable a stopped
  670. * counter simply because we rescheduled.
  671. */
  672. if (hwc->state & PERF_HES_STOPPED)
  673. hwc->state |= PERF_HES_ARCH;
  674. x86_pmu_stop(event, PERF_EF_UPDATE);
  675. }
  676. for (i = 0; i < cpuc->n_events; i++) {
  677. event = cpuc->event_list[i];
  678. hwc = &event->hw;
  679. if (!match_prev_assignment(hwc, cpuc, i))
  680. x86_assign_hw_event(event, cpuc, i);
  681. else if (i < n_running)
  682. continue;
  683. if (hwc->state & PERF_HES_ARCH)
  684. continue;
  685. x86_pmu_start(event, PERF_EF_RELOAD);
  686. }
  687. cpuc->n_added = 0;
  688. perf_events_lapic_init();
  689. }
  690. cpuc->enabled = 1;
  691. barrier();
  692. x86_pmu.enable_all(added);
  693. }
  694. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  695. /*
  696. * Set the next IRQ period, based on the hwc->period_left value.
  697. * To be called with the event disabled in hw:
  698. */
  699. int x86_perf_event_set_period(struct perf_event *event)
  700. {
  701. struct hw_perf_event *hwc = &event->hw;
  702. s64 left = local64_read(&hwc->period_left);
  703. s64 period = hwc->sample_period;
  704. int ret = 0, idx = hwc->idx;
  705. if (idx == X86_PMC_IDX_FIXED_BTS)
  706. return 0;
  707. /*
  708. * If we are way outside a reasonable range then just skip forward:
  709. */
  710. if (unlikely(left <= -period)) {
  711. left = period;
  712. local64_set(&hwc->period_left, left);
  713. hwc->last_period = period;
  714. ret = 1;
  715. }
  716. if (unlikely(left <= 0)) {
  717. left += period;
  718. local64_set(&hwc->period_left, left);
  719. hwc->last_period = period;
  720. ret = 1;
  721. }
  722. /*
  723. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  724. */
  725. if (unlikely(left < 2))
  726. left = 2;
  727. if (left > x86_pmu.max_period)
  728. left = x86_pmu.max_period;
  729. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  730. /*
  731. * The hw event starts counting from this event offset,
  732. * mark it to be able to extra future deltas:
  733. */
  734. local64_set(&hwc->prev_count, (u64)-left);
  735. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  736. /*
  737. * Due to erratum on certan cpu we need
  738. * a second write to be sure the register
  739. * is updated properly
  740. */
  741. if (x86_pmu.perfctr_second_write) {
  742. wrmsrl(hwc->event_base,
  743. (u64)(-left) & x86_pmu.cntval_mask);
  744. }
  745. perf_event_update_userpage(event);
  746. return ret;
  747. }
  748. void x86_pmu_enable_event(struct perf_event *event)
  749. {
  750. if (__this_cpu_read(cpu_hw_events.enabled))
  751. __x86_pmu_enable_event(&event->hw,
  752. ARCH_PERFMON_EVENTSEL_ENABLE);
  753. }
  754. /*
  755. * Add a single event to the PMU.
  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. static int x86_pmu_add(struct perf_event *event, int flags)
  761. {
  762. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  763. struct hw_perf_event *hwc;
  764. int assign[X86_PMC_IDX_MAX];
  765. int n, n0, ret;
  766. hwc = &event->hw;
  767. perf_pmu_disable(event->pmu);
  768. n0 = cpuc->n_events;
  769. ret = n = collect_events(cpuc, event, false);
  770. if (ret < 0)
  771. goto out;
  772. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  773. if (!(flags & PERF_EF_START))
  774. hwc->state |= PERF_HES_ARCH;
  775. /*
  776. * If group events scheduling transaction was started,
  777. * skip the schedulability test here, it will be performed
  778. * at commit time (->commit_txn) as a whole
  779. */
  780. if (cpuc->group_flag & PERF_EVENT_TXN)
  781. goto done_collect;
  782. ret = x86_pmu.schedule_events(cpuc, n, assign);
  783. if (ret)
  784. goto out;
  785. /*
  786. * copy new assignment, now we know it is possible
  787. * will be used by hw_perf_enable()
  788. */
  789. memcpy(cpuc->assign, assign, n*sizeof(int));
  790. done_collect:
  791. cpuc->n_events = n;
  792. cpuc->n_added += n - n0;
  793. cpuc->n_txn += n - n0;
  794. ret = 0;
  795. out:
  796. perf_pmu_enable(event->pmu);
  797. return ret;
  798. }
  799. static void x86_pmu_start(struct perf_event *event, int flags)
  800. {
  801. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  802. int idx = event->hw.idx;
  803. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  804. return;
  805. if (WARN_ON_ONCE(idx == -1))
  806. return;
  807. if (flags & PERF_EF_RELOAD) {
  808. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  809. x86_perf_event_set_period(event);
  810. }
  811. event->hw.state = 0;
  812. cpuc->events[idx] = event;
  813. __set_bit(idx, cpuc->active_mask);
  814. __set_bit(idx, cpuc->running);
  815. x86_pmu.enable(event);
  816. perf_event_update_userpage(event);
  817. }
  818. void perf_event_print_debug(void)
  819. {
  820. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  821. u64 pebs;
  822. struct cpu_hw_events *cpuc;
  823. unsigned long flags;
  824. int cpu, idx;
  825. if (!x86_pmu.num_counters)
  826. return;
  827. local_irq_save(flags);
  828. cpu = smp_processor_id();
  829. cpuc = &per_cpu(cpu_hw_events, cpu);
  830. if (x86_pmu.version >= 2) {
  831. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  832. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  833. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  834. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  835. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  836. pr_info("\n");
  837. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  838. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  839. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  840. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  841. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  842. }
  843. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  844. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  845. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  846. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  847. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  848. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  849. cpu, idx, pmc_ctrl);
  850. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  851. cpu, idx, pmc_count);
  852. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  853. cpu, idx, prev_left);
  854. }
  855. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  856. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  857. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  858. cpu, idx, pmc_count);
  859. }
  860. local_irq_restore(flags);
  861. }
  862. void x86_pmu_stop(struct perf_event *event, int flags)
  863. {
  864. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  865. struct hw_perf_event *hwc = &event->hw;
  866. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  867. x86_pmu.disable(event);
  868. cpuc->events[hwc->idx] = NULL;
  869. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  870. hwc->state |= PERF_HES_STOPPED;
  871. }
  872. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  873. /*
  874. * Drain the remaining delta count out of a event
  875. * that we are disabling:
  876. */
  877. x86_perf_event_update(event);
  878. hwc->state |= PERF_HES_UPTODATE;
  879. }
  880. }
  881. static void x86_pmu_del(struct perf_event *event, int flags)
  882. {
  883. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  884. int i;
  885. /*
  886. * If we're called during a txn, we don't need to do anything.
  887. * The events never got scheduled and ->cancel_txn will truncate
  888. * the event_list.
  889. */
  890. if (cpuc->group_flag & PERF_EVENT_TXN)
  891. return;
  892. x86_pmu_stop(event, PERF_EF_UPDATE);
  893. for (i = 0; i < cpuc->n_events; i++) {
  894. if (event == cpuc->event_list[i]) {
  895. if (x86_pmu.put_event_constraints)
  896. x86_pmu.put_event_constraints(cpuc, event);
  897. while (++i < cpuc->n_events)
  898. cpuc->event_list[i-1] = cpuc->event_list[i];
  899. --cpuc->n_events;
  900. break;
  901. }
  902. }
  903. perf_event_update_userpage(event);
  904. }
  905. int x86_pmu_handle_irq(struct pt_regs *regs)
  906. {
  907. struct perf_sample_data data;
  908. struct cpu_hw_events *cpuc;
  909. struct perf_event *event;
  910. int idx, handled = 0;
  911. u64 val;
  912. perf_sample_data_init(&data, 0);
  913. cpuc = &__get_cpu_var(cpu_hw_events);
  914. /*
  915. * Some chipsets need to unmask the LVTPC in a particular spot
  916. * inside the nmi handler. As a result, the unmasking was pushed
  917. * into all the nmi handlers.
  918. *
  919. * This generic handler doesn't seem to have any issues where the
  920. * unmasking occurs so it was left at the top.
  921. */
  922. apic_write(APIC_LVTPC, APIC_DM_NMI);
  923. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  924. if (!test_bit(idx, cpuc->active_mask)) {
  925. /*
  926. * Though we deactivated the counter some cpus
  927. * might still deliver spurious interrupts still
  928. * in flight. Catch them:
  929. */
  930. if (__test_and_clear_bit(idx, cpuc->running))
  931. handled++;
  932. continue;
  933. }
  934. event = cpuc->events[idx];
  935. val = x86_perf_event_update(event);
  936. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  937. continue;
  938. /*
  939. * event overflow
  940. */
  941. handled++;
  942. data.period = event->hw.last_period;
  943. if (!x86_perf_event_set_period(event))
  944. continue;
  945. if (perf_event_overflow(event, &data, regs))
  946. x86_pmu_stop(event, 0);
  947. }
  948. if (handled)
  949. inc_irq_stat(apic_perf_irqs);
  950. return handled;
  951. }
  952. void perf_events_lapic_init(void)
  953. {
  954. if (!x86_pmu.apic || !x86_pmu_initialized())
  955. return;
  956. /*
  957. * Always use NMI for PMU
  958. */
  959. apic_write(APIC_LVTPC, APIC_DM_NMI);
  960. }
  961. static int __kprobes
  962. perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  963. {
  964. if (!atomic_read(&active_events))
  965. return NMI_DONE;
  966. return x86_pmu.handle_irq(regs);
  967. }
  968. struct event_constraint emptyconstraint;
  969. struct event_constraint unconstrained;
  970. static int __cpuinit
  971. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  972. {
  973. unsigned int cpu = (long)hcpu;
  974. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  975. int ret = NOTIFY_OK;
  976. switch (action & ~CPU_TASKS_FROZEN) {
  977. case CPU_UP_PREPARE:
  978. cpuc->kfree_on_online = NULL;
  979. if (x86_pmu.cpu_prepare)
  980. ret = x86_pmu.cpu_prepare(cpu);
  981. break;
  982. case CPU_STARTING:
  983. if (x86_pmu.cpu_starting)
  984. x86_pmu.cpu_starting(cpu);
  985. break;
  986. case CPU_ONLINE:
  987. kfree(cpuc->kfree_on_online);
  988. break;
  989. case CPU_DYING:
  990. if (x86_pmu.cpu_dying)
  991. x86_pmu.cpu_dying(cpu);
  992. break;
  993. case CPU_UP_CANCELED:
  994. case CPU_DEAD:
  995. if (x86_pmu.cpu_dead)
  996. x86_pmu.cpu_dead(cpu);
  997. break;
  998. default:
  999. break;
  1000. }
  1001. return ret;
  1002. }
  1003. static void __init pmu_check_apic(void)
  1004. {
  1005. if (cpu_has_apic)
  1006. return;
  1007. x86_pmu.apic = 0;
  1008. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1009. pr_info("no hardware sampling interrupt available.\n");
  1010. }
  1011. static int __init init_hw_perf_events(void)
  1012. {
  1013. struct x86_pmu_quirk *quirk;
  1014. struct event_constraint *c;
  1015. int err;
  1016. pr_info("Performance Events: ");
  1017. switch (boot_cpu_data.x86_vendor) {
  1018. case X86_VENDOR_INTEL:
  1019. err = intel_pmu_init();
  1020. break;
  1021. case X86_VENDOR_AMD:
  1022. err = amd_pmu_init();
  1023. break;
  1024. default:
  1025. return 0;
  1026. }
  1027. if (err != 0) {
  1028. pr_cont("no PMU driver, software events only.\n");
  1029. return 0;
  1030. }
  1031. pmu_check_apic();
  1032. /* sanity check that the hardware exists or is emulated */
  1033. if (!check_hw_exists())
  1034. return 0;
  1035. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1036. for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next)
  1037. quirk->func();
  1038. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1039. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1040. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1041. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1042. }
  1043. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1044. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1045. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1046. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1047. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1048. }
  1049. x86_pmu.intel_ctrl |=
  1050. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1051. perf_events_lapic_init();
  1052. register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
  1053. unconstrained = (struct event_constraint)
  1054. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1055. 0, x86_pmu.num_counters, 0);
  1056. if (x86_pmu.event_constraints) {
  1057. /*
  1058. * event on fixed counter2 (REF_CYCLES) only works on this
  1059. * counter, so do not extend mask to generic counters
  1060. */
  1061. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1062. if (c->cmask != X86_RAW_EVENT_MASK
  1063. || c->idxmsk64 == X86_PMC_MSK_FIXED_REF_CYCLES) {
  1064. continue;
  1065. }
  1066. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1067. c->weight += x86_pmu.num_counters;
  1068. }
  1069. }
  1070. pr_info("... version: %d\n", x86_pmu.version);
  1071. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1072. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1073. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1074. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1075. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1076. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1077. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1078. perf_cpu_notifier(x86_pmu_notifier);
  1079. return 0;
  1080. }
  1081. early_initcall(init_hw_perf_events);
  1082. static inline void x86_pmu_read(struct perf_event *event)
  1083. {
  1084. x86_perf_event_update(event);
  1085. }
  1086. /*
  1087. * Start group events scheduling transaction
  1088. * Set the flag to make pmu::enable() not perform the
  1089. * schedulability test, it will be performed at commit time
  1090. */
  1091. static void x86_pmu_start_txn(struct pmu *pmu)
  1092. {
  1093. perf_pmu_disable(pmu);
  1094. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1095. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1096. }
  1097. /*
  1098. * Stop group events scheduling transaction
  1099. * Clear the flag and pmu::enable() will perform the
  1100. * schedulability test.
  1101. */
  1102. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1103. {
  1104. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1105. /*
  1106. * Truncate the collected events.
  1107. */
  1108. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1109. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1110. perf_pmu_enable(pmu);
  1111. }
  1112. /*
  1113. * Commit group events scheduling transaction
  1114. * Perform the group schedulability test as a whole
  1115. * Return 0 if success
  1116. */
  1117. static int x86_pmu_commit_txn(struct pmu *pmu)
  1118. {
  1119. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1120. int assign[X86_PMC_IDX_MAX];
  1121. int n, ret;
  1122. n = cpuc->n_events;
  1123. if (!x86_pmu_initialized())
  1124. return -EAGAIN;
  1125. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1126. if (ret)
  1127. return ret;
  1128. /*
  1129. * copy new assignment, now we know it is possible
  1130. * will be used by hw_perf_enable()
  1131. */
  1132. memcpy(cpuc->assign, assign, n*sizeof(int));
  1133. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1134. perf_pmu_enable(pmu);
  1135. return 0;
  1136. }
  1137. /*
  1138. * a fake_cpuc is used to validate event groups. Due to
  1139. * the extra reg logic, we need to also allocate a fake
  1140. * per_core and per_cpu structure. Otherwise, group events
  1141. * using extra reg may conflict without the kernel being
  1142. * able to catch this when the last event gets added to
  1143. * the group.
  1144. */
  1145. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1146. {
  1147. kfree(cpuc->shared_regs);
  1148. kfree(cpuc);
  1149. }
  1150. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1151. {
  1152. struct cpu_hw_events *cpuc;
  1153. int cpu = raw_smp_processor_id();
  1154. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1155. if (!cpuc)
  1156. return ERR_PTR(-ENOMEM);
  1157. /* only needed, if we have extra_regs */
  1158. if (x86_pmu.extra_regs) {
  1159. cpuc->shared_regs = allocate_shared_regs(cpu);
  1160. if (!cpuc->shared_regs)
  1161. goto error;
  1162. }
  1163. return cpuc;
  1164. error:
  1165. free_fake_cpuc(cpuc);
  1166. return ERR_PTR(-ENOMEM);
  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 = allocate_fake_cpuc();
  1177. if (IS_ERR(fake_cpuc))
  1178. return PTR_ERR(fake_cpuc);
  1179. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1180. if (!c || !c->weight)
  1181. ret = -EINVAL;
  1182. if (x86_pmu.put_event_constraints)
  1183. x86_pmu.put_event_constraints(fake_cpuc, event);
  1184. free_fake_cpuc(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 = -EINVAL, n;
  1203. fake_cpuc = allocate_fake_cpuc();
  1204. if (IS_ERR(fake_cpuc))
  1205. return PTR_ERR(fake_cpuc);
  1206. /*
  1207. * the event is not yet connected with its
  1208. * siblings therefore we must first collect
  1209. * existing siblings, then add the new event
  1210. * before we can simulate the scheduling
  1211. */
  1212. n = collect_events(fake_cpuc, leader, true);
  1213. if (n < 0)
  1214. goto out;
  1215. fake_cpuc->n_events = n;
  1216. n = collect_events(fake_cpuc, event, false);
  1217. if (n < 0)
  1218. goto out;
  1219. fake_cpuc->n_events = n;
  1220. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1221. out:
  1222. free_fake_cpuc(fake_cpuc);
  1223. return ret;
  1224. }
  1225. static int x86_pmu_event_init(struct perf_event *event)
  1226. {
  1227. struct pmu *tmp;
  1228. int err;
  1229. switch (event->attr.type) {
  1230. case PERF_TYPE_RAW:
  1231. case PERF_TYPE_HARDWARE:
  1232. case PERF_TYPE_HW_CACHE:
  1233. break;
  1234. default:
  1235. return -ENOENT;
  1236. }
  1237. err = __x86_pmu_event_init(event);
  1238. if (!err) {
  1239. /*
  1240. * we temporarily connect event to its pmu
  1241. * such that validate_group() can classify
  1242. * it as an x86 event using is_x86_event()
  1243. */
  1244. tmp = event->pmu;
  1245. event->pmu = &pmu;
  1246. if (event->group_leader != event)
  1247. err = validate_group(event);
  1248. else
  1249. err = validate_event(event);
  1250. event->pmu = tmp;
  1251. }
  1252. if (err) {
  1253. if (event->destroy)
  1254. event->destroy(event);
  1255. }
  1256. return err;
  1257. }
  1258. static struct pmu pmu = {
  1259. .pmu_enable = x86_pmu_enable,
  1260. .pmu_disable = x86_pmu_disable,
  1261. .event_init = x86_pmu_event_init,
  1262. .add = x86_pmu_add,
  1263. .del = x86_pmu_del,
  1264. .start = x86_pmu_start,
  1265. .stop = x86_pmu_stop,
  1266. .read = x86_pmu_read,
  1267. .start_txn = x86_pmu_start_txn,
  1268. .cancel_txn = x86_pmu_cancel_txn,
  1269. .commit_txn = x86_pmu_commit_txn,
  1270. };
  1271. /*
  1272. * callchain support
  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. perf_callchain_store(entry, addr);
  1282. }
  1283. static const struct stacktrace_ops backtrace_ops = {
  1284. .stack = backtrace_stack,
  1285. .address = backtrace_address,
  1286. .walk_stack = print_context_stack_bp,
  1287. };
  1288. void
  1289. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1290. {
  1291. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1292. /* TODO: We don't support guest os callchain now */
  1293. return;
  1294. }
  1295. perf_callchain_store(entry, regs->ip);
  1296. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1297. }
  1298. #ifdef CONFIG_COMPAT
  1299. static inline int
  1300. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1301. {
  1302. /* 32-bit process in 64-bit kernel. */
  1303. struct stack_frame_ia32 frame;
  1304. const void __user *fp;
  1305. if (!test_thread_flag(TIF_IA32))
  1306. return 0;
  1307. fp = compat_ptr(regs->bp);
  1308. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1309. unsigned long bytes;
  1310. frame.next_frame = 0;
  1311. frame.return_address = 0;
  1312. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1313. if (bytes != sizeof(frame))
  1314. break;
  1315. if (fp < compat_ptr(regs->sp))
  1316. break;
  1317. perf_callchain_store(entry, frame.return_address);
  1318. fp = compat_ptr(frame.next_frame);
  1319. }
  1320. return 1;
  1321. }
  1322. #else
  1323. static inline int
  1324. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1325. {
  1326. return 0;
  1327. }
  1328. #endif
  1329. void
  1330. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1331. {
  1332. struct stack_frame frame;
  1333. const void __user *fp;
  1334. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1335. /* TODO: We don't support guest os callchain now */
  1336. return;
  1337. }
  1338. fp = (void __user *)regs->bp;
  1339. perf_callchain_store(entry, regs->ip);
  1340. if (!current->mm)
  1341. return;
  1342. if (perf_callchain_user32(regs, entry))
  1343. return;
  1344. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1345. unsigned long bytes;
  1346. frame.next_frame = NULL;
  1347. frame.return_address = 0;
  1348. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1349. if (bytes != sizeof(frame))
  1350. break;
  1351. if ((unsigned long)fp < regs->sp)
  1352. break;
  1353. perf_callchain_store(entry, frame.return_address);
  1354. fp = frame.next_frame;
  1355. }
  1356. }
  1357. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1358. {
  1359. unsigned long ip;
  1360. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1361. ip = perf_guest_cbs->get_guest_ip();
  1362. else
  1363. ip = instruction_pointer(regs);
  1364. return ip;
  1365. }
  1366. unsigned long perf_misc_flags(struct pt_regs *regs)
  1367. {
  1368. int misc = 0;
  1369. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1370. if (perf_guest_cbs->is_user_mode())
  1371. misc |= PERF_RECORD_MISC_GUEST_USER;
  1372. else
  1373. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1374. } else {
  1375. if (user_mode(regs))
  1376. misc |= PERF_RECORD_MISC_USER;
  1377. else
  1378. misc |= PERF_RECORD_MISC_KERNEL;
  1379. }
  1380. if (regs->flags & PERF_EFLAGS_EXACT)
  1381. misc |= PERF_RECORD_MISC_EXACT_IP;
  1382. return misc;
  1383. }
  1384. void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
  1385. {
  1386. cap->version = x86_pmu.version;
  1387. cap->num_counters_gp = x86_pmu.num_counters;
  1388. cap->num_counters_fixed = x86_pmu.num_counters_fixed;
  1389. cap->bit_width_gp = x86_pmu.cntval_bits;
  1390. cap->bit_width_fixed = x86_pmu.cntval_bits;
  1391. cap->events_mask = (unsigned int)x86_pmu.events_maskl;
  1392. cap->events_mask_len = x86_pmu.events_mask_len;
  1393. }
  1394. EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability);