perf_counter.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*
  2. * Performance counter 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. *
  10. * For licencing details see kernel-base/COPYING
  11. */
  12. #include <linux/perf_counter.h>
  13. #include <linux/capability.h>
  14. #include <linux/notifier.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/module.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/sched.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/apic.h>
  22. #include <asm/stacktrace.h>
  23. #include <asm/nmi.h>
  24. static u64 perf_counter_mask __read_mostly;
  25. struct cpu_hw_counters {
  26. struct perf_counter *counters[X86_PMC_IDX_MAX];
  27. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  28. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  29. unsigned long interrupts;
  30. int enabled;
  31. };
  32. /*
  33. * struct x86_pmu - generic x86 pmu
  34. */
  35. struct x86_pmu {
  36. const char *name;
  37. int version;
  38. int (*handle_irq)(struct pt_regs *, int);
  39. void (*disable_all)(void);
  40. void (*enable_all)(void);
  41. void (*enable)(struct hw_perf_counter *, int);
  42. void (*disable)(struct hw_perf_counter *, int);
  43. unsigned eventsel;
  44. unsigned perfctr;
  45. u64 (*event_map)(int);
  46. u64 (*raw_event)(u64);
  47. int max_events;
  48. int num_counters;
  49. int num_counters_fixed;
  50. int counter_bits;
  51. u64 counter_mask;
  52. u64 max_period;
  53. u64 intel_ctrl;
  54. };
  55. static struct x86_pmu x86_pmu __read_mostly;
  56. static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
  57. .enabled = 1,
  58. };
  59. /*
  60. * Intel PerfMon v3. Used on Core2 and later.
  61. */
  62. static const u64 intel_perfmon_event_map[] =
  63. {
  64. [PERF_COUNT_CPU_CYCLES] = 0x003c,
  65. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  66. [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
  67. [PERF_COUNT_CACHE_MISSES] = 0x412e,
  68. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  69. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  70. [PERF_COUNT_BUS_CYCLES] = 0x013c,
  71. };
  72. static u64 intel_pmu_event_map(int event)
  73. {
  74. return intel_perfmon_event_map[event];
  75. }
  76. static u64 intel_pmu_raw_event(u64 event)
  77. {
  78. #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
  79. #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  80. #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
  81. #define CORE_EVNTSEL_MASK \
  82. (CORE_EVNTSEL_EVENT_MASK | \
  83. CORE_EVNTSEL_UNIT_MASK | \
  84. CORE_EVNTSEL_COUNTER_MASK)
  85. return event & CORE_EVNTSEL_MASK;
  86. }
  87. /*
  88. * AMD Performance Monitor K7 and later.
  89. */
  90. static const u64 amd_perfmon_event_map[] =
  91. {
  92. [PERF_COUNT_CPU_CYCLES] = 0x0076,
  93. [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
  94. [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
  95. [PERF_COUNT_CACHE_MISSES] = 0x0081,
  96. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
  97. [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
  98. };
  99. static u64 amd_pmu_event_map(int event)
  100. {
  101. return amd_perfmon_event_map[event];
  102. }
  103. static u64 amd_pmu_raw_event(u64 event)
  104. {
  105. #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
  106. #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
  107. #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
  108. #define K7_EVNTSEL_MASK \
  109. (K7_EVNTSEL_EVENT_MASK | \
  110. K7_EVNTSEL_UNIT_MASK | \
  111. K7_EVNTSEL_COUNTER_MASK)
  112. return event & K7_EVNTSEL_MASK;
  113. }
  114. /*
  115. * Propagate counter elapsed time into the generic counter.
  116. * Can only be executed on the CPU where the counter is active.
  117. * Returns the delta events processed.
  118. */
  119. static u64
  120. x86_perf_counter_update(struct perf_counter *counter,
  121. struct hw_perf_counter *hwc, int idx)
  122. {
  123. int shift = 64 - x86_pmu.counter_bits;
  124. u64 prev_raw_count, new_raw_count;
  125. s64 delta;
  126. /*
  127. * Careful: an NMI might modify the previous counter value.
  128. *
  129. * Our tactic to handle this is to first atomically read and
  130. * exchange a new raw count - then add that new-prev delta
  131. * count to the generic counter atomically:
  132. */
  133. again:
  134. prev_raw_count = atomic64_read(&hwc->prev_count);
  135. rdmsrl(hwc->counter_base + idx, new_raw_count);
  136. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  137. new_raw_count) != prev_raw_count)
  138. goto again;
  139. /*
  140. * Now we have the new raw value and have updated the prev
  141. * timestamp already. We can now calculate the elapsed delta
  142. * (counter-)time and add that to the generic counter.
  143. *
  144. * Careful, not all hw sign-extends above the physical width
  145. * of the count.
  146. */
  147. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  148. delta >>= shift;
  149. atomic64_add(delta, &counter->count);
  150. atomic64_sub(delta, &hwc->period_left);
  151. return new_raw_count;
  152. }
  153. static atomic_t active_counters;
  154. static DEFINE_MUTEX(pmc_reserve_mutex);
  155. static bool reserve_pmc_hardware(void)
  156. {
  157. int i;
  158. if (nmi_watchdog == NMI_LOCAL_APIC)
  159. disable_lapic_nmi_watchdog();
  160. for (i = 0; i < x86_pmu.num_counters; i++) {
  161. if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
  162. goto perfctr_fail;
  163. }
  164. for (i = 0; i < x86_pmu.num_counters; i++) {
  165. if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
  166. goto eventsel_fail;
  167. }
  168. return true;
  169. eventsel_fail:
  170. for (i--; i >= 0; i--)
  171. release_evntsel_nmi(x86_pmu.eventsel + i);
  172. i = x86_pmu.num_counters;
  173. perfctr_fail:
  174. for (i--; i >= 0; i--)
  175. release_perfctr_nmi(x86_pmu.perfctr + i);
  176. if (nmi_watchdog == NMI_LOCAL_APIC)
  177. enable_lapic_nmi_watchdog();
  178. return false;
  179. }
  180. static void release_pmc_hardware(void)
  181. {
  182. int i;
  183. for (i = 0; i < x86_pmu.num_counters; i++) {
  184. release_perfctr_nmi(x86_pmu.perfctr + i);
  185. release_evntsel_nmi(x86_pmu.eventsel + i);
  186. }
  187. if (nmi_watchdog == NMI_LOCAL_APIC)
  188. enable_lapic_nmi_watchdog();
  189. }
  190. static void hw_perf_counter_destroy(struct perf_counter *counter)
  191. {
  192. if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
  193. release_pmc_hardware();
  194. mutex_unlock(&pmc_reserve_mutex);
  195. }
  196. }
  197. static inline int x86_pmu_initialized(void)
  198. {
  199. return x86_pmu.handle_irq != NULL;
  200. }
  201. /*
  202. * Setup the hardware configuration for a given hw_event_type
  203. */
  204. static int __hw_perf_counter_init(struct perf_counter *counter)
  205. {
  206. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  207. struct hw_perf_counter *hwc = &counter->hw;
  208. int err;
  209. if (!x86_pmu_initialized())
  210. return -ENODEV;
  211. err = 0;
  212. if (!atomic_inc_not_zero(&active_counters)) {
  213. mutex_lock(&pmc_reserve_mutex);
  214. if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
  215. err = -EBUSY;
  216. else
  217. atomic_inc(&active_counters);
  218. mutex_unlock(&pmc_reserve_mutex);
  219. }
  220. if (err)
  221. return err;
  222. /*
  223. * Generate PMC IRQs:
  224. * (keep 'enabled' bit clear for now)
  225. */
  226. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  227. /*
  228. * Count user and OS events unless requested not to.
  229. */
  230. if (!hw_event->exclude_user)
  231. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  232. if (!hw_event->exclude_kernel)
  233. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  234. /*
  235. * If privileged enough, allow NMI events:
  236. */
  237. hwc->nmi = 0;
  238. if (hw_event->nmi) {
  239. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  240. return -EACCES;
  241. hwc->nmi = 1;
  242. }
  243. perf_counters_lapic_init(hwc->nmi);
  244. if (!hwc->irq_period)
  245. hwc->irq_period = x86_pmu.max_period;
  246. atomic64_set(&hwc->period_left,
  247. min(x86_pmu.max_period, hwc->irq_period));
  248. /*
  249. * Raw event type provide the config in the event structure
  250. */
  251. if (perf_event_raw(hw_event)) {
  252. hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
  253. } else {
  254. if (perf_event_id(hw_event) >= x86_pmu.max_events)
  255. return -EINVAL;
  256. /*
  257. * The generic map:
  258. */
  259. hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
  260. }
  261. counter->destroy = hw_perf_counter_destroy;
  262. return 0;
  263. }
  264. static void intel_pmu_disable_all(void)
  265. {
  266. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  267. }
  268. static void amd_pmu_disable_all(void)
  269. {
  270. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  271. int idx;
  272. if (!cpuc->enabled)
  273. return;
  274. cpuc->enabled = 0;
  275. /*
  276. * ensure we write the disable before we start disabling the
  277. * counters proper, so that amd_pmu_enable_counter() does the
  278. * right thing.
  279. */
  280. barrier();
  281. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  282. u64 val;
  283. if (!test_bit(idx, cpuc->active_mask))
  284. continue;
  285. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  286. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  287. continue;
  288. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  289. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  290. }
  291. }
  292. void hw_perf_disable(void)
  293. {
  294. if (!x86_pmu_initialized())
  295. return;
  296. return x86_pmu.disable_all();
  297. }
  298. static void intel_pmu_enable_all(void)
  299. {
  300. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  301. }
  302. static void amd_pmu_enable_all(void)
  303. {
  304. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  305. int idx;
  306. if (cpuc->enabled)
  307. return;
  308. cpuc->enabled = 1;
  309. barrier();
  310. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  311. u64 val;
  312. if (!test_bit(idx, cpuc->active_mask))
  313. continue;
  314. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  315. if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
  316. continue;
  317. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  318. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  319. }
  320. }
  321. void hw_perf_enable(void)
  322. {
  323. if (!x86_pmu_initialized())
  324. return;
  325. x86_pmu.enable_all();
  326. }
  327. static inline u64 intel_pmu_get_status(void)
  328. {
  329. u64 status;
  330. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  331. return status;
  332. }
  333. static inline void intel_pmu_ack_status(u64 ack)
  334. {
  335. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  336. }
  337. static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  338. {
  339. int err;
  340. err = checking_wrmsrl(hwc->config_base + idx,
  341. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  342. }
  343. static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  344. {
  345. int err;
  346. err = checking_wrmsrl(hwc->config_base + idx,
  347. hwc->config);
  348. }
  349. static inline void
  350. intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
  351. {
  352. int idx = __idx - X86_PMC_IDX_FIXED;
  353. u64 ctrl_val, mask;
  354. int err;
  355. mask = 0xfULL << (idx * 4);
  356. rdmsrl(hwc->config_base, ctrl_val);
  357. ctrl_val &= ~mask;
  358. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  359. }
  360. static inline void
  361. intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  362. {
  363. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  364. intel_pmu_disable_fixed(hwc, idx);
  365. return;
  366. }
  367. x86_pmu_disable_counter(hwc, idx);
  368. }
  369. static inline void
  370. amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
  371. {
  372. x86_pmu_disable_counter(hwc, idx);
  373. }
  374. static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
  375. /*
  376. * Set the next IRQ period, based on the hwc->period_left value.
  377. * To be called with the counter disabled in hw:
  378. */
  379. static void
  380. x86_perf_counter_set_period(struct perf_counter *counter,
  381. struct hw_perf_counter *hwc, int idx)
  382. {
  383. s64 left = atomic64_read(&hwc->period_left);
  384. s64 period = min(x86_pmu.max_period, hwc->irq_period);
  385. int err;
  386. /*
  387. * If we are way outside a reasoable range then just skip forward:
  388. */
  389. if (unlikely(left <= -period)) {
  390. left = period;
  391. atomic64_set(&hwc->period_left, left);
  392. }
  393. if (unlikely(left <= 0)) {
  394. left += period;
  395. atomic64_set(&hwc->period_left, left);
  396. }
  397. /*
  398. * Quirk: certain CPUs dont like it if just 1 event is left:
  399. */
  400. if (unlikely(left < 2))
  401. left = 2;
  402. per_cpu(prev_left[idx], smp_processor_id()) = left;
  403. /*
  404. * The hw counter starts counting from this counter offset,
  405. * mark it to be able to extra future deltas:
  406. */
  407. atomic64_set(&hwc->prev_count, (u64)-left);
  408. err = checking_wrmsrl(hwc->counter_base + idx,
  409. (u64)(-left) & x86_pmu.counter_mask);
  410. }
  411. static inline void
  412. intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
  413. {
  414. int idx = __idx - X86_PMC_IDX_FIXED;
  415. u64 ctrl_val, bits, mask;
  416. int err;
  417. /*
  418. * Enable IRQ generation (0x8),
  419. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  420. * if requested:
  421. */
  422. bits = 0x8ULL;
  423. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  424. bits |= 0x2;
  425. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  426. bits |= 0x1;
  427. bits <<= (idx * 4);
  428. mask = 0xfULL << (idx * 4);
  429. rdmsrl(hwc->config_base, ctrl_val);
  430. ctrl_val &= ~mask;
  431. ctrl_val |= bits;
  432. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  433. }
  434. static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  435. {
  436. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  437. intel_pmu_enable_fixed(hwc, idx);
  438. return;
  439. }
  440. x86_pmu_enable_counter(hwc, idx);
  441. }
  442. static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
  443. {
  444. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  445. if (cpuc->enabled)
  446. x86_pmu_enable_counter(hwc, idx);
  447. else
  448. x86_pmu_disable_counter(hwc, idx);
  449. }
  450. static int
  451. fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
  452. {
  453. unsigned int event;
  454. if (!x86_pmu.num_counters_fixed)
  455. return -1;
  456. if (unlikely(hwc->nmi))
  457. return -1;
  458. event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  459. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
  460. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  461. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
  462. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  463. if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
  464. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  465. return -1;
  466. }
  467. /*
  468. * Find a PMC slot for the freshly enabled / scheduled in counter:
  469. */
  470. static int x86_pmu_enable(struct perf_counter *counter)
  471. {
  472. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  473. struct hw_perf_counter *hwc = &counter->hw;
  474. int idx;
  475. idx = fixed_mode_idx(counter, hwc);
  476. if (idx >= 0) {
  477. /*
  478. * Try to get the fixed counter, if that is already taken
  479. * then try to get a generic counter:
  480. */
  481. if (test_and_set_bit(idx, cpuc->used_mask))
  482. goto try_generic;
  483. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  484. /*
  485. * We set it so that counter_base + idx in wrmsr/rdmsr maps to
  486. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  487. */
  488. hwc->counter_base =
  489. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  490. hwc->idx = idx;
  491. } else {
  492. idx = hwc->idx;
  493. /* Try to get the previous generic counter again */
  494. if (test_and_set_bit(idx, cpuc->used_mask)) {
  495. try_generic:
  496. idx = find_first_zero_bit(cpuc->used_mask,
  497. x86_pmu.num_counters);
  498. if (idx == x86_pmu.num_counters)
  499. return -EAGAIN;
  500. set_bit(idx, cpuc->used_mask);
  501. hwc->idx = idx;
  502. }
  503. hwc->config_base = x86_pmu.eventsel;
  504. hwc->counter_base = x86_pmu.perfctr;
  505. }
  506. x86_pmu.disable(hwc, idx);
  507. cpuc->counters[idx] = counter;
  508. set_bit(idx, cpuc->active_mask);
  509. x86_perf_counter_set_period(counter, hwc, idx);
  510. x86_pmu.enable(hwc, idx);
  511. return 0;
  512. }
  513. void perf_counter_print_debug(void)
  514. {
  515. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  516. struct cpu_hw_counters *cpuc;
  517. unsigned long flags;
  518. int cpu, idx;
  519. if (!x86_pmu.num_counters)
  520. return;
  521. local_irq_save(flags);
  522. cpu = smp_processor_id();
  523. cpuc = &per_cpu(cpu_hw_counters, cpu);
  524. if (x86_pmu.version >= 2) {
  525. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  526. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  527. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  528. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  529. pr_info("\n");
  530. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  531. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  532. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  533. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  534. }
  535. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  536. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  537. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  538. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  539. prev_left = per_cpu(prev_left[idx], cpu);
  540. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  541. cpu, idx, pmc_ctrl);
  542. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  543. cpu, idx, pmc_count);
  544. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  545. cpu, idx, prev_left);
  546. }
  547. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  548. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  549. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  550. cpu, idx, pmc_count);
  551. }
  552. local_irq_restore(flags);
  553. }
  554. static void x86_pmu_disable(struct perf_counter *counter)
  555. {
  556. struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
  557. struct hw_perf_counter *hwc = &counter->hw;
  558. int idx = hwc->idx;
  559. /*
  560. * Must be done before we disable, otherwise the nmi handler
  561. * could reenable again:
  562. */
  563. clear_bit(idx, cpuc->active_mask);
  564. x86_pmu.disable(hwc, idx);
  565. /*
  566. * Make sure the cleared pointer becomes visible before we
  567. * (potentially) free the counter:
  568. */
  569. barrier();
  570. /*
  571. * Drain the remaining delta count out of a counter
  572. * that we are disabling:
  573. */
  574. x86_perf_counter_update(counter, hwc, idx);
  575. cpuc->counters[idx] = NULL;
  576. clear_bit(idx, cpuc->used_mask);
  577. }
  578. /*
  579. * Save and restart an expired counter. Called by NMI contexts,
  580. * so it has to be careful about preempting normal counter ops:
  581. */
  582. static void intel_pmu_save_and_restart(struct perf_counter *counter)
  583. {
  584. struct hw_perf_counter *hwc = &counter->hw;
  585. int idx = hwc->idx;
  586. x86_perf_counter_update(counter, hwc, idx);
  587. x86_perf_counter_set_period(counter, hwc, idx);
  588. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  589. intel_pmu_enable_counter(hwc, idx);
  590. }
  591. /*
  592. * Maximum interrupt frequency of 100KHz per CPU
  593. */
  594. #define PERFMON_MAX_INTERRUPTS (100000/HZ)
  595. /*
  596. * This handler is triggered by the local APIC, so the APIC IRQ handling
  597. * rules apply:
  598. */
  599. static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
  600. {
  601. struct cpu_hw_counters *cpuc;
  602. struct cpu_hw_counters;
  603. int bit, cpu, loops;
  604. u64 ack, status;
  605. cpu = smp_processor_id();
  606. cpuc = &per_cpu(cpu_hw_counters, cpu);
  607. perf_disable();
  608. status = intel_pmu_get_status();
  609. if (!status) {
  610. perf_enable();
  611. return 0;
  612. }
  613. loops = 0;
  614. again:
  615. if (++loops > 100) {
  616. WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
  617. perf_counter_print_debug();
  618. return 1;
  619. }
  620. inc_irq_stat(apic_perf_irqs);
  621. ack = status;
  622. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  623. struct perf_counter *counter = cpuc->counters[bit];
  624. clear_bit(bit, (unsigned long *) &status);
  625. if (!test_bit(bit, cpuc->active_mask))
  626. continue;
  627. intel_pmu_save_and_restart(counter);
  628. if (perf_counter_overflow(counter, nmi, regs, 0))
  629. intel_pmu_disable_counter(&counter->hw, bit);
  630. }
  631. intel_pmu_ack_status(ack);
  632. /*
  633. * Repeat if there is more work to be done:
  634. */
  635. status = intel_pmu_get_status();
  636. if (status)
  637. goto again;
  638. if (++cpuc->interrupts != PERFMON_MAX_INTERRUPTS)
  639. perf_enable();
  640. return 1;
  641. }
  642. static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
  643. {
  644. int cpu, idx, throttle = 0, handled = 0;
  645. struct cpu_hw_counters *cpuc;
  646. struct perf_counter *counter;
  647. struct hw_perf_counter *hwc;
  648. u64 val;
  649. cpu = smp_processor_id();
  650. cpuc = &per_cpu(cpu_hw_counters, cpu);
  651. if (++cpuc->interrupts == PERFMON_MAX_INTERRUPTS) {
  652. throttle = 1;
  653. __perf_disable();
  654. cpuc->enabled = 0;
  655. barrier();
  656. }
  657. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  658. int disable = 0;
  659. if (!test_bit(idx, cpuc->active_mask))
  660. continue;
  661. counter = cpuc->counters[idx];
  662. hwc = &counter->hw;
  663. if (counter->hw_event.nmi != nmi)
  664. goto next;
  665. val = x86_perf_counter_update(counter, hwc, idx);
  666. if (val & (1ULL << (x86_pmu.counter_bits - 1)))
  667. goto next;
  668. /* counter overflow */
  669. x86_perf_counter_set_period(counter, hwc, idx);
  670. handled = 1;
  671. inc_irq_stat(apic_perf_irqs);
  672. disable = perf_counter_overflow(counter, nmi, regs, 0);
  673. next:
  674. if (disable || throttle)
  675. amd_pmu_disable_counter(hwc, idx);
  676. }
  677. return handled;
  678. }
  679. void perf_counter_unthrottle(void)
  680. {
  681. struct cpu_hw_counters *cpuc;
  682. if (!x86_pmu_initialized())
  683. return;
  684. cpuc = &__get_cpu_var(cpu_hw_counters);
  685. if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
  686. /*
  687. * Clear them before re-enabling irqs/NMIs again:
  688. */
  689. cpuc->interrupts = 0;
  690. perf_enable();
  691. } else {
  692. cpuc->interrupts = 0;
  693. }
  694. }
  695. void smp_perf_counter_interrupt(struct pt_regs *regs)
  696. {
  697. irq_enter();
  698. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  699. ack_APIC_irq();
  700. x86_pmu.handle_irq(regs, 0);
  701. irq_exit();
  702. }
  703. void smp_perf_pending_interrupt(struct pt_regs *regs)
  704. {
  705. irq_enter();
  706. ack_APIC_irq();
  707. inc_irq_stat(apic_pending_irqs);
  708. perf_counter_do_pending();
  709. irq_exit();
  710. }
  711. void set_perf_counter_pending(void)
  712. {
  713. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  714. }
  715. void perf_counters_lapic_init(int nmi)
  716. {
  717. u32 apic_val;
  718. if (!x86_pmu_initialized())
  719. return;
  720. /*
  721. * Enable the performance counter vector in the APIC LVT:
  722. */
  723. apic_val = apic_read(APIC_LVTERR);
  724. apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
  725. if (nmi)
  726. apic_write(APIC_LVTPC, APIC_DM_NMI);
  727. else
  728. apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
  729. apic_write(APIC_LVTERR, apic_val);
  730. }
  731. static int __kprobes
  732. perf_counter_nmi_handler(struct notifier_block *self,
  733. unsigned long cmd, void *__args)
  734. {
  735. struct die_args *args = __args;
  736. struct pt_regs *regs;
  737. if (!atomic_read(&active_counters))
  738. return NOTIFY_DONE;
  739. switch (cmd) {
  740. case DIE_NMI:
  741. case DIE_NMI_IPI:
  742. break;
  743. default:
  744. return NOTIFY_DONE;
  745. }
  746. regs = args->regs;
  747. apic_write(APIC_LVTPC, APIC_DM_NMI);
  748. /*
  749. * Can't rely on the handled return value to say it was our NMI, two
  750. * counters could trigger 'simultaneously' raising two back-to-back NMIs.
  751. *
  752. * If the first NMI handles both, the latter will be empty and daze
  753. * the CPU.
  754. */
  755. x86_pmu.handle_irq(regs, 1);
  756. return NOTIFY_STOP;
  757. }
  758. static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
  759. .notifier_call = perf_counter_nmi_handler,
  760. .next = NULL,
  761. .priority = 1
  762. };
  763. static struct x86_pmu intel_pmu = {
  764. .name = "Intel",
  765. .handle_irq = intel_pmu_handle_irq,
  766. .disable_all = intel_pmu_disable_all,
  767. .enable_all = intel_pmu_enable_all,
  768. .enable = intel_pmu_enable_counter,
  769. .disable = intel_pmu_disable_counter,
  770. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  771. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  772. .event_map = intel_pmu_event_map,
  773. .raw_event = intel_pmu_raw_event,
  774. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  775. /*
  776. * Intel PMCs cannot be accessed sanely above 32 bit width,
  777. * so we install an artificial 1<<31 period regardless of
  778. * the generic counter period:
  779. */
  780. .max_period = (1ULL << 31) - 1,
  781. };
  782. static struct x86_pmu amd_pmu = {
  783. .name = "AMD",
  784. .handle_irq = amd_pmu_handle_irq,
  785. .disable_all = amd_pmu_disable_all,
  786. .enable_all = amd_pmu_enable_all,
  787. .enable = amd_pmu_enable_counter,
  788. .disable = amd_pmu_disable_counter,
  789. .eventsel = MSR_K7_EVNTSEL0,
  790. .perfctr = MSR_K7_PERFCTR0,
  791. .event_map = amd_pmu_event_map,
  792. .raw_event = amd_pmu_raw_event,
  793. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  794. .num_counters = 4,
  795. .counter_bits = 48,
  796. .counter_mask = (1ULL << 48) - 1,
  797. /* use highest bit to detect overflow */
  798. .max_period = (1ULL << 47) - 1,
  799. };
  800. static int intel_pmu_init(void)
  801. {
  802. union cpuid10_edx edx;
  803. union cpuid10_eax eax;
  804. unsigned int unused;
  805. unsigned int ebx;
  806. int version;
  807. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  808. return -ENODEV;
  809. /*
  810. * Check whether the Architectural PerfMon supports
  811. * Branch Misses Retired Event or not.
  812. */
  813. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  814. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  815. return -ENODEV;
  816. version = eax.split.version_id;
  817. if (version < 2)
  818. return -ENODEV;
  819. x86_pmu = intel_pmu;
  820. x86_pmu.version = version;
  821. x86_pmu.num_counters = eax.split.num_counters;
  822. /*
  823. * Quirk: v2 perfmon does not report fixed-purpose counters, so
  824. * assume at least 3 counters:
  825. */
  826. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  827. x86_pmu.counter_bits = eax.split.bit_width;
  828. x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
  829. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  830. return 0;
  831. }
  832. static int amd_pmu_init(void)
  833. {
  834. x86_pmu = amd_pmu;
  835. return 0;
  836. }
  837. void __init init_hw_perf_counters(void)
  838. {
  839. int err;
  840. switch (boot_cpu_data.x86_vendor) {
  841. case X86_VENDOR_INTEL:
  842. err = intel_pmu_init();
  843. break;
  844. case X86_VENDOR_AMD:
  845. err = amd_pmu_init();
  846. break;
  847. default:
  848. return;
  849. }
  850. if (err != 0)
  851. return;
  852. pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
  853. pr_info("... version: %d\n", x86_pmu.version);
  854. pr_info("... bit width: %d\n", x86_pmu.counter_bits);
  855. pr_info("... num counters: %d\n", x86_pmu.num_counters);
  856. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  857. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  858. WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
  859. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  860. }
  861. perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
  862. perf_max_counters = x86_pmu.num_counters;
  863. pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
  864. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  865. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  866. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  867. WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
  868. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  869. }
  870. pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
  871. perf_counter_mask |=
  872. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  873. pr_info("... counter mask: %016Lx\n", perf_counter_mask);
  874. perf_counters_lapic_init(1);
  875. register_die_notifier(&perf_counter_nmi_notifier);
  876. }
  877. static inline void x86_pmu_read(struct perf_counter *counter)
  878. {
  879. x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
  880. }
  881. static const struct pmu pmu = {
  882. .enable = x86_pmu_enable,
  883. .disable = x86_pmu_disable,
  884. .read = x86_pmu_read,
  885. };
  886. const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  887. {
  888. int err;
  889. err = __hw_perf_counter_init(counter);
  890. if (err)
  891. return ERR_PTR(err);
  892. return &pmu;
  893. }
  894. /*
  895. * callchain support
  896. */
  897. static inline
  898. void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
  899. {
  900. if (entry->nr < MAX_STACK_DEPTH)
  901. entry->ip[entry->nr++] = ip;
  902. }
  903. static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
  904. static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
  905. static void
  906. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  907. {
  908. /* Ignore warnings */
  909. }
  910. static void backtrace_warning(void *data, char *msg)
  911. {
  912. /* Ignore warnings */
  913. }
  914. static int backtrace_stack(void *data, char *name)
  915. {
  916. /* Don't bother with IRQ stacks for now */
  917. return -1;
  918. }
  919. static void backtrace_address(void *data, unsigned long addr, int reliable)
  920. {
  921. struct perf_callchain_entry *entry = data;
  922. if (reliable)
  923. callchain_store(entry, addr);
  924. }
  925. static const struct stacktrace_ops backtrace_ops = {
  926. .warning = backtrace_warning,
  927. .warning_symbol = backtrace_warning_symbol,
  928. .stack = backtrace_stack,
  929. .address = backtrace_address,
  930. };
  931. static void
  932. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  933. {
  934. unsigned long bp;
  935. char *stack;
  936. int nr = entry->nr;
  937. callchain_store(entry, instruction_pointer(regs));
  938. stack = ((char *)regs + sizeof(struct pt_regs));
  939. #ifdef CONFIG_FRAME_POINTER
  940. bp = frame_pointer(regs);
  941. #else
  942. bp = 0;
  943. #endif
  944. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
  945. entry->kernel = entry->nr - nr;
  946. }
  947. struct stack_frame {
  948. const void __user *next_fp;
  949. unsigned long return_address;
  950. };
  951. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  952. {
  953. int ret;
  954. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  955. return 0;
  956. ret = 1;
  957. pagefault_disable();
  958. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  959. ret = 0;
  960. pagefault_enable();
  961. return ret;
  962. }
  963. static void
  964. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  965. {
  966. struct stack_frame frame;
  967. const void __user *fp;
  968. int nr = entry->nr;
  969. regs = (struct pt_regs *)current->thread.sp0 - 1;
  970. fp = (void __user *)regs->bp;
  971. callchain_store(entry, regs->ip);
  972. while (entry->nr < MAX_STACK_DEPTH) {
  973. frame.next_fp = NULL;
  974. frame.return_address = 0;
  975. if (!copy_stack_frame(fp, &frame))
  976. break;
  977. if ((unsigned long)fp < user_stack_pointer(regs))
  978. break;
  979. callchain_store(entry, frame.return_address);
  980. fp = frame.next_fp;
  981. }
  982. entry->user = entry->nr - nr;
  983. }
  984. static void
  985. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  986. {
  987. int is_user;
  988. if (!regs)
  989. return;
  990. is_user = user_mode(regs);
  991. if (!current || current->pid == 0)
  992. return;
  993. if (is_user && current->state != TASK_RUNNING)
  994. return;
  995. if (!is_user)
  996. perf_callchain_kernel(regs, entry);
  997. if (current->mm)
  998. perf_callchain_user(regs, entry);
  999. }
  1000. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1001. {
  1002. struct perf_callchain_entry *entry;
  1003. if (in_nmi())
  1004. entry = &__get_cpu_var(nmi_entry);
  1005. else
  1006. entry = &__get_cpu_var(irq_entry);
  1007. entry->nr = 0;
  1008. entry->hv = 0;
  1009. entry->kernel = 0;
  1010. entry->user = 0;
  1011. perf_do_callchain(regs, entry);
  1012. return entry;
  1013. }