perf_event.c 45 KB

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