perf_event.c 39 KB

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