perf_event.c 40 KB

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