perf_event.c 40 KB

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