perf_event.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  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. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  821. if (cpuc->enabled)
  822. __x86_pmu_enable_event(&event->hw,
  823. ARCH_PERFMON_EVENTSEL_ENABLE);
  824. }
  825. /*
  826. * Add a single event to the PMU.
  827. *
  828. * The event is added to the group of enabled events
  829. * but only if it can be scehduled with existing events.
  830. */
  831. static int x86_pmu_add(struct perf_event *event, int flags)
  832. {
  833. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  834. struct hw_perf_event *hwc;
  835. int assign[X86_PMC_IDX_MAX];
  836. int n, n0, ret;
  837. hwc = &event->hw;
  838. perf_pmu_disable(event->pmu);
  839. n0 = cpuc->n_events;
  840. ret = n = collect_events(cpuc, event, false);
  841. if (ret < 0)
  842. goto out;
  843. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  844. if (!(flags & PERF_EF_START))
  845. hwc->state |= PERF_HES_ARCH;
  846. /*
  847. * If group events scheduling transaction was started,
  848. * skip the schedulability test here, it will be peformed
  849. * at commit time (->commit_txn) as a whole
  850. */
  851. if (cpuc->group_flag & PERF_EVENT_TXN)
  852. goto done_collect;
  853. ret = x86_pmu.schedule_events(cpuc, n, assign);
  854. if (ret)
  855. goto out;
  856. /*
  857. * copy new assignment, now we know it is possible
  858. * will be used by hw_perf_enable()
  859. */
  860. memcpy(cpuc->assign, assign, n*sizeof(int));
  861. done_collect:
  862. cpuc->n_events = n;
  863. cpuc->n_added += n - n0;
  864. cpuc->n_txn += n - n0;
  865. ret = 0;
  866. out:
  867. perf_pmu_enable(event->pmu);
  868. return ret;
  869. }
  870. static void x86_pmu_start(struct perf_event *event, int flags)
  871. {
  872. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  873. int idx = event->hw.idx;
  874. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  875. return;
  876. if (WARN_ON_ONCE(idx == -1))
  877. return;
  878. if (flags & PERF_EF_RELOAD) {
  879. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  880. x86_perf_event_set_period(event);
  881. }
  882. event->hw.state = 0;
  883. cpuc->events[idx] = event;
  884. __set_bit(idx, cpuc->active_mask);
  885. __set_bit(idx, cpuc->running);
  886. x86_pmu.enable(event);
  887. perf_event_update_userpage(event);
  888. }
  889. void perf_event_print_debug(void)
  890. {
  891. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  892. u64 pebs;
  893. struct cpu_hw_events *cpuc;
  894. unsigned long flags;
  895. int cpu, idx;
  896. if (!x86_pmu.num_counters)
  897. return;
  898. local_irq_save(flags);
  899. cpu = smp_processor_id();
  900. cpuc = &per_cpu(cpu_hw_events, cpu);
  901. if (x86_pmu.version >= 2) {
  902. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  903. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  904. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  905. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  906. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  907. pr_info("\n");
  908. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  909. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  910. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  911. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  912. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  913. }
  914. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  915. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  916. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  917. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  918. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  919. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  920. cpu, idx, pmc_ctrl);
  921. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  922. cpu, idx, pmc_count);
  923. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  924. cpu, idx, prev_left);
  925. }
  926. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  927. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  928. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  929. cpu, idx, pmc_count);
  930. }
  931. local_irq_restore(flags);
  932. }
  933. static void x86_pmu_stop(struct perf_event *event, int flags)
  934. {
  935. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  936. struct hw_perf_event *hwc = &event->hw;
  937. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  938. x86_pmu.disable(event);
  939. cpuc->events[hwc->idx] = NULL;
  940. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  941. hwc->state |= PERF_HES_STOPPED;
  942. }
  943. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  944. /*
  945. * Drain the remaining delta count out of a event
  946. * that we are disabling:
  947. */
  948. x86_perf_event_update(event);
  949. hwc->state |= PERF_HES_UPTODATE;
  950. }
  951. }
  952. static void x86_pmu_del(struct perf_event *event, int flags)
  953. {
  954. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  955. int i;
  956. /*
  957. * If we're called during a txn, we don't need to do anything.
  958. * The events never got scheduled and ->cancel_txn will truncate
  959. * the event_list.
  960. */
  961. if (cpuc->group_flag & PERF_EVENT_TXN)
  962. return;
  963. x86_pmu_stop(event, PERF_EF_UPDATE);
  964. for (i = 0; i < cpuc->n_events; i++) {
  965. if (event == cpuc->event_list[i]) {
  966. if (x86_pmu.put_event_constraints)
  967. x86_pmu.put_event_constraints(cpuc, event);
  968. while (++i < cpuc->n_events)
  969. cpuc->event_list[i-1] = cpuc->event_list[i];
  970. --cpuc->n_events;
  971. break;
  972. }
  973. }
  974. perf_event_update_userpage(event);
  975. }
  976. static int x86_pmu_handle_irq(struct pt_regs *regs)
  977. {
  978. struct perf_sample_data data;
  979. struct cpu_hw_events *cpuc;
  980. struct perf_event *event;
  981. int idx, handled = 0;
  982. u64 val;
  983. perf_sample_data_init(&data, 0);
  984. cpuc = &__get_cpu_var(cpu_hw_events);
  985. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  986. if (!test_bit(idx, cpuc->active_mask)) {
  987. /*
  988. * Though we deactivated the counter some cpus
  989. * might still deliver spurious interrupts still
  990. * in flight. Catch them:
  991. */
  992. if (__test_and_clear_bit(idx, cpuc->running))
  993. handled++;
  994. continue;
  995. }
  996. event = cpuc->events[idx];
  997. val = x86_perf_event_update(event);
  998. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  999. continue;
  1000. /*
  1001. * event overflow
  1002. */
  1003. handled++;
  1004. data.period = event->hw.last_period;
  1005. if (!x86_perf_event_set_period(event))
  1006. continue;
  1007. if (perf_event_overflow(event, 1, &data, regs))
  1008. x86_pmu_stop(event, 0);
  1009. }
  1010. if (handled)
  1011. inc_irq_stat(apic_perf_irqs);
  1012. return handled;
  1013. }
  1014. void perf_events_lapic_init(void)
  1015. {
  1016. if (!x86_pmu.apic || !x86_pmu_initialized())
  1017. return;
  1018. /*
  1019. * Always use NMI for PMU
  1020. */
  1021. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1022. }
  1023. struct pmu_nmi_state {
  1024. unsigned int marked;
  1025. int handled;
  1026. };
  1027. static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
  1028. static int __kprobes
  1029. perf_event_nmi_handler(struct notifier_block *self,
  1030. unsigned long cmd, void *__args)
  1031. {
  1032. struct die_args *args = __args;
  1033. unsigned int this_nmi;
  1034. int handled;
  1035. if (!atomic_read(&active_events))
  1036. return NOTIFY_DONE;
  1037. switch (cmd) {
  1038. case DIE_NMI:
  1039. case DIE_NMI_IPI:
  1040. break;
  1041. case DIE_NMIUNKNOWN:
  1042. this_nmi = percpu_read(irq_stat.__nmi_count);
  1043. if (this_nmi != __get_cpu_var(pmu_nmi).marked)
  1044. /* let the kernel handle the unknown nmi */
  1045. return NOTIFY_DONE;
  1046. /*
  1047. * This one is a PMU back-to-back nmi. Two events
  1048. * trigger 'simultaneously' raising two back-to-back
  1049. * NMIs. If the first NMI handles both, the latter
  1050. * will be empty and daze the CPU. So, we drop it to
  1051. * avoid false-positive 'unknown nmi' messages.
  1052. */
  1053. return NOTIFY_STOP;
  1054. default:
  1055. return NOTIFY_DONE;
  1056. }
  1057. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1058. handled = x86_pmu.handle_irq(args->regs);
  1059. if (!handled)
  1060. return NOTIFY_DONE;
  1061. this_nmi = percpu_read(irq_stat.__nmi_count);
  1062. if ((handled > 1) ||
  1063. /* the next nmi could be a back-to-back nmi */
  1064. ((__get_cpu_var(pmu_nmi).marked == this_nmi) &&
  1065. (__get_cpu_var(pmu_nmi).handled > 1))) {
  1066. /*
  1067. * We could have two subsequent back-to-back nmis: The
  1068. * first handles more than one counter, the 2nd
  1069. * handles only one counter and the 3rd handles no
  1070. * counter.
  1071. *
  1072. * This is the 2nd nmi because the previous was
  1073. * handling more than one counter. We will mark the
  1074. * next (3rd) and then drop it if unhandled.
  1075. */
  1076. __get_cpu_var(pmu_nmi).marked = this_nmi + 1;
  1077. __get_cpu_var(pmu_nmi).handled = handled;
  1078. }
  1079. return NOTIFY_STOP;
  1080. }
  1081. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1082. .notifier_call = perf_event_nmi_handler,
  1083. .next = NULL,
  1084. .priority = NMI_LOCAL_LOW_PRIOR,
  1085. };
  1086. static struct event_constraint unconstrained;
  1087. static struct event_constraint emptyconstraint;
  1088. static struct event_constraint *
  1089. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1090. {
  1091. struct event_constraint *c;
  1092. if (x86_pmu.event_constraints) {
  1093. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1094. if ((event->hw.config & c->cmask) == c->code)
  1095. return c;
  1096. }
  1097. }
  1098. return &unconstrained;
  1099. }
  1100. #include "perf_event_amd.c"
  1101. #include "perf_event_p6.c"
  1102. #include "perf_event_p4.c"
  1103. #include "perf_event_intel_lbr.c"
  1104. #include "perf_event_intel_ds.c"
  1105. #include "perf_event_intel.c"
  1106. static int __cpuinit
  1107. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1108. {
  1109. unsigned int cpu = (long)hcpu;
  1110. int ret = NOTIFY_OK;
  1111. switch (action & ~CPU_TASKS_FROZEN) {
  1112. case CPU_UP_PREPARE:
  1113. if (x86_pmu.cpu_prepare)
  1114. ret = x86_pmu.cpu_prepare(cpu);
  1115. break;
  1116. case CPU_STARTING:
  1117. if (x86_pmu.cpu_starting)
  1118. x86_pmu.cpu_starting(cpu);
  1119. break;
  1120. case CPU_DYING:
  1121. if (x86_pmu.cpu_dying)
  1122. x86_pmu.cpu_dying(cpu);
  1123. break;
  1124. case CPU_UP_CANCELED:
  1125. case CPU_DEAD:
  1126. if (x86_pmu.cpu_dead)
  1127. x86_pmu.cpu_dead(cpu);
  1128. break;
  1129. default:
  1130. break;
  1131. }
  1132. return ret;
  1133. }
  1134. static void __init pmu_check_apic(void)
  1135. {
  1136. if (cpu_has_apic)
  1137. return;
  1138. x86_pmu.apic = 0;
  1139. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1140. pr_info("no hardware sampling interrupt available.\n");
  1141. }
  1142. int __init init_hw_perf_events(void)
  1143. {
  1144. struct event_constraint *c;
  1145. int err;
  1146. pr_info("Performance Events: ");
  1147. switch (boot_cpu_data.x86_vendor) {
  1148. case X86_VENDOR_INTEL:
  1149. err = intel_pmu_init();
  1150. break;
  1151. case X86_VENDOR_AMD:
  1152. err = amd_pmu_init();
  1153. break;
  1154. default:
  1155. return 0;
  1156. }
  1157. if (err != 0) {
  1158. pr_cont("no PMU driver, software events only.\n");
  1159. return 0;
  1160. }
  1161. pmu_check_apic();
  1162. /* sanity check that the hardware exists or is emulated */
  1163. if (!check_hw_exists())
  1164. return 0;
  1165. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1166. if (x86_pmu.quirks)
  1167. x86_pmu.quirks();
  1168. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1169. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1170. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1171. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1172. }
  1173. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1174. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1175. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1176. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1177. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1178. }
  1179. x86_pmu.intel_ctrl |=
  1180. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1181. perf_events_lapic_init();
  1182. register_die_notifier(&perf_event_nmi_notifier);
  1183. unconstrained = (struct event_constraint)
  1184. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1185. 0, x86_pmu.num_counters);
  1186. if (x86_pmu.event_constraints) {
  1187. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1188. if (c->cmask != X86_RAW_EVENT_MASK)
  1189. continue;
  1190. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1191. c->weight += x86_pmu.num_counters;
  1192. }
  1193. }
  1194. pr_info("... version: %d\n", x86_pmu.version);
  1195. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1196. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1197. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1198. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1199. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1200. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1201. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1202. perf_cpu_notifier(x86_pmu_notifier);
  1203. return 0;
  1204. }
  1205. early_initcall(init_hw_perf_events);
  1206. static inline void x86_pmu_read(struct perf_event *event)
  1207. {
  1208. x86_perf_event_update(event);
  1209. }
  1210. /*
  1211. * Start group events scheduling transaction
  1212. * Set the flag to make pmu::enable() not perform the
  1213. * schedulability test, it will be performed at commit time
  1214. */
  1215. static void x86_pmu_start_txn(struct pmu *pmu)
  1216. {
  1217. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1218. perf_pmu_disable(pmu);
  1219. cpuc->group_flag |= PERF_EVENT_TXN;
  1220. cpuc->n_txn = 0;
  1221. }
  1222. /*
  1223. * Stop group events scheduling transaction
  1224. * Clear the flag and pmu::enable() will perform the
  1225. * schedulability test.
  1226. */
  1227. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1228. {
  1229. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1230. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1231. /*
  1232. * Truncate the collected events.
  1233. */
  1234. cpuc->n_added -= cpuc->n_txn;
  1235. cpuc->n_events -= cpuc->n_txn;
  1236. perf_pmu_enable(pmu);
  1237. }
  1238. /*
  1239. * Commit group events scheduling transaction
  1240. * Perform the group schedulability test as a whole
  1241. * Return 0 if success
  1242. */
  1243. static int x86_pmu_commit_txn(struct pmu *pmu)
  1244. {
  1245. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1246. int assign[X86_PMC_IDX_MAX];
  1247. int n, ret;
  1248. n = cpuc->n_events;
  1249. if (!x86_pmu_initialized())
  1250. return -EAGAIN;
  1251. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1252. if (ret)
  1253. return ret;
  1254. /*
  1255. * copy new assignment, now we know it is possible
  1256. * will be used by hw_perf_enable()
  1257. */
  1258. memcpy(cpuc->assign, assign, n*sizeof(int));
  1259. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1260. perf_pmu_enable(pmu);
  1261. return 0;
  1262. }
  1263. /*
  1264. * validate that we can schedule this event
  1265. */
  1266. static int validate_event(struct perf_event *event)
  1267. {
  1268. struct cpu_hw_events *fake_cpuc;
  1269. struct event_constraint *c;
  1270. int ret = 0;
  1271. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1272. if (!fake_cpuc)
  1273. return -ENOMEM;
  1274. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1275. if (!c || !c->weight)
  1276. ret = -ENOSPC;
  1277. if (x86_pmu.put_event_constraints)
  1278. x86_pmu.put_event_constraints(fake_cpuc, event);
  1279. kfree(fake_cpuc);
  1280. return ret;
  1281. }
  1282. /*
  1283. * validate a single event group
  1284. *
  1285. * validation include:
  1286. * - check events are compatible which each other
  1287. * - events do not compete for the same counter
  1288. * - number of events <= number of counters
  1289. *
  1290. * validation ensures the group can be loaded onto the
  1291. * PMU if it was the only group available.
  1292. */
  1293. static int validate_group(struct perf_event *event)
  1294. {
  1295. struct perf_event *leader = event->group_leader;
  1296. struct cpu_hw_events *fake_cpuc;
  1297. int ret, n;
  1298. ret = -ENOMEM;
  1299. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1300. if (!fake_cpuc)
  1301. goto out;
  1302. /*
  1303. * the event is not yet connected with its
  1304. * siblings therefore we must first collect
  1305. * existing siblings, then add the new event
  1306. * before we can simulate the scheduling
  1307. */
  1308. ret = -ENOSPC;
  1309. n = collect_events(fake_cpuc, leader, true);
  1310. if (n < 0)
  1311. goto out_free;
  1312. fake_cpuc->n_events = n;
  1313. n = collect_events(fake_cpuc, event, false);
  1314. if (n < 0)
  1315. goto out_free;
  1316. fake_cpuc->n_events = n;
  1317. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1318. out_free:
  1319. kfree(fake_cpuc);
  1320. out:
  1321. return ret;
  1322. }
  1323. int x86_pmu_event_init(struct perf_event *event)
  1324. {
  1325. struct pmu *tmp;
  1326. int err;
  1327. switch (event->attr.type) {
  1328. case PERF_TYPE_RAW:
  1329. case PERF_TYPE_HARDWARE:
  1330. case PERF_TYPE_HW_CACHE:
  1331. break;
  1332. default:
  1333. return -ENOENT;
  1334. }
  1335. err = __x86_pmu_event_init(event);
  1336. if (!err) {
  1337. /*
  1338. * we temporarily connect event to its pmu
  1339. * such that validate_group() can classify
  1340. * it as an x86 event using is_x86_event()
  1341. */
  1342. tmp = event->pmu;
  1343. event->pmu = &pmu;
  1344. if (event->group_leader != event)
  1345. err = validate_group(event);
  1346. else
  1347. err = validate_event(event);
  1348. event->pmu = tmp;
  1349. }
  1350. if (err) {
  1351. if (event->destroy)
  1352. event->destroy(event);
  1353. }
  1354. return err;
  1355. }
  1356. static struct pmu pmu = {
  1357. .pmu_enable = x86_pmu_enable,
  1358. .pmu_disable = x86_pmu_disable,
  1359. .event_init = x86_pmu_event_init,
  1360. .add = x86_pmu_add,
  1361. .del = x86_pmu_del,
  1362. .start = x86_pmu_start,
  1363. .stop = x86_pmu_stop,
  1364. .read = x86_pmu_read,
  1365. .start_txn = x86_pmu_start_txn,
  1366. .cancel_txn = x86_pmu_cancel_txn,
  1367. .commit_txn = x86_pmu_commit_txn,
  1368. };
  1369. /*
  1370. * callchain support
  1371. */
  1372. static void
  1373. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1374. {
  1375. /* Ignore warnings */
  1376. }
  1377. static void backtrace_warning(void *data, char *msg)
  1378. {
  1379. /* Ignore warnings */
  1380. }
  1381. static int backtrace_stack(void *data, char *name)
  1382. {
  1383. return 0;
  1384. }
  1385. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1386. {
  1387. struct perf_callchain_entry *entry = data;
  1388. perf_callchain_store(entry, addr);
  1389. }
  1390. static const struct stacktrace_ops backtrace_ops = {
  1391. .warning = backtrace_warning,
  1392. .warning_symbol = backtrace_warning_symbol,
  1393. .stack = backtrace_stack,
  1394. .address = backtrace_address,
  1395. .walk_stack = print_context_stack_bp,
  1396. };
  1397. void
  1398. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1399. {
  1400. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1401. /* TODO: We don't support guest os callchain now */
  1402. return;
  1403. }
  1404. perf_callchain_store(entry, regs->ip);
  1405. dump_trace(NULL, regs, NULL, &backtrace_ops, entry);
  1406. }
  1407. #ifdef CONFIG_COMPAT
  1408. static inline int
  1409. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1410. {
  1411. /* 32-bit process in 64-bit kernel. */
  1412. struct stack_frame_ia32 frame;
  1413. const void __user *fp;
  1414. if (!test_thread_flag(TIF_IA32))
  1415. return 0;
  1416. fp = compat_ptr(regs->bp);
  1417. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1418. unsigned long bytes;
  1419. frame.next_frame = 0;
  1420. frame.return_address = 0;
  1421. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1422. if (bytes != sizeof(frame))
  1423. break;
  1424. if (fp < compat_ptr(regs->sp))
  1425. break;
  1426. perf_callchain_store(entry, frame.return_address);
  1427. fp = compat_ptr(frame.next_frame);
  1428. }
  1429. return 1;
  1430. }
  1431. #else
  1432. static inline int
  1433. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1434. {
  1435. return 0;
  1436. }
  1437. #endif
  1438. void
  1439. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1440. {
  1441. struct stack_frame frame;
  1442. const void __user *fp;
  1443. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1444. /* TODO: We don't support guest os callchain now */
  1445. return;
  1446. }
  1447. fp = (void __user *)regs->bp;
  1448. perf_callchain_store(entry, regs->ip);
  1449. if (perf_callchain_user32(regs, entry))
  1450. return;
  1451. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1452. unsigned long bytes;
  1453. frame.next_frame = NULL;
  1454. frame.return_address = 0;
  1455. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1456. if (bytes != sizeof(frame))
  1457. break;
  1458. if ((unsigned long)fp < regs->sp)
  1459. break;
  1460. perf_callchain_store(entry, frame.return_address);
  1461. fp = frame.next_frame;
  1462. }
  1463. }
  1464. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1465. {
  1466. unsigned long ip;
  1467. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1468. ip = perf_guest_cbs->get_guest_ip();
  1469. else
  1470. ip = instruction_pointer(regs);
  1471. return ip;
  1472. }
  1473. unsigned long perf_misc_flags(struct pt_regs *regs)
  1474. {
  1475. int misc = 0;
  1476. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1477. if (perf_guest_cbs->is_user_mode())
  1478. misc |= PERF_RECORD_MISC_GUEST_USER;
  1479. else
  1480. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1481. } else {
  1482. if (user_mode(regs))
  1483. misc |= PERF_RECORD_MISC_USER;
  1484. else
  1485. misc |= PERF_RECORD_MISC_KERNEL;
  1486. }
  1487. if (regs->flags & PERF_EFLAGS_EXACT)
  1488. misc |= PERF_RECORD_MISC_EXACT_IP;
  1489. return misc;
  1490. }