perf_event.c 40 KB

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