perf_event.c 38 KB

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