perf_event.c 37 KB

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