perf_event.c 39 KB

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