perf_event.c 42 KB

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