perf_event.c 43 KB

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