perf_event.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  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. /* Grab the first unused counter starting with idx */
  465. idx = sched->state.counter;
  466. for_each_set_bit_cont(idx, c->idxmsk, X86_PMC_IDX_MAX) {
  467. if (!__test_and_set_bit(idx, sched->state.used))
  468. break;
  469. }
  470. sched->state.counter = idx;
  471. if (idx >= X86_PMC_IDX_MAX)
  472. return false;
  473. if (c->overlap)
  474. perf_sched_save_state(sched);
  475. return true;
  476. }
  477. static bool perf_sched_find_counter(struct perf_sched *sched)
  478. {
  479. while (!__perf_sched_find_counter(sched)) {
  480. if (!perf_sched_restore_state(sched))
  481. return false;
  482. }
  483. return true;
  484. }
  485. /*
  486. * Go through all unassigned events and find the next one to schedule.
  487. * Take events with the least weight first. Return true on success.
  488. */
  489. static bool perf_sched_next_event(struct perf_sched *sched)
  490. {
  491. struct event_constraint *c;
  492. if (!sched->state.unassigned || !--sched->state.unassigned)
  493. return false;
  494. do {
  495. /* next event */
  496. sched->state.event++;
  497. if (sched->state.event >= sched->max_events) {
  498. /* next weight */
  499. sched->state.event = 0;
  500. sched->state.weight++;
  501. if (sched->state.weight > sched->max_weight)
  502. return false;
  503. }
  504. c = sched->constraints[sched->state.event];
  505. } while (c->weight != sched->state.weight);
  506. sched->state.counter = 0; /* start with first counter */
  507. return true;
  508. }
  509. /*
  510. * Assign a counter for each event.
  511. */
  512. static int perf_assign_events(struct event_constraint **constraints, int n,
  513. int wmin, int wmax, int *assign)
  514. {
  515. struct perf_sched sched;
  516. perf_sched_init(&sched, constraints, n, wmin, wmax);
  517. do {
  518. if (!perf_sched_find_counter(&sched))
  519. break; /* failed */
  520. if (assign)
  521. assign[sched.state.event] = sched.state.counter;
  522. } while (perf_sched_next_event(&sched));
  523. return sched.state.unassigned;
  524. }
  525. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  526. {
  527. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  528. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  529. int i, wmin, wmax, num = 0;
  530. struct hw_perf_event *hwc;
  531. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  532. for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
  533. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  534. constraints[i] = c;
  535. wmin = min(wmin, c->weight);
  536. wmax = max(wmax, c->weight);
  537. }
  538. /*
  539. * fastpath, try to reuse previous register
  540. */
  541. for (i = 0; i < n; i++) {
  542. hwc = &cpuc->event_list[i]->hw;
  543. c = constraints[i];
  544. /* never assigned */
  545. if (hwc->idx == -1)
  546. break;
  547. /* constraint still honored */
  548. if (!test_bit(hwc->idx, c->idxmsk))
  549. break;
  550. /* not already used */
  551. if (test_bit(hwc->idx, used_mask))
  552. break;
  553. __set_bit(hwc->idx, used_mask);
  554. if (assign)
  555. assign[i] = hwc->idx;
  556. }
  557. /* slow path */
  558. if (i != n)
  559. num = perf_assign_events(constraints, n, wmin, wmax, assign);
  560. /*
  561. * scheduling failed or is just a simulation,
  562. * free resources if necessary
  563. */
  564. if (!assign || num) {
  565. for (i = 0; i < n; i++) {
  566. if (x86_pmu.put_event_constraints)
  567. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  568. }
  569. }
  570. return num ? -EINVAL : 0;
  571. }
  572. /*
  573. * dogrp: true if must collect siblings events (group)
  574. * returns total number of events and error code
  575. */
  576. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  577. {
  578. struct perf_event *event;
  579. int n, max_count;
  580. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  581. /* current number of events already accepted */
  582. n = cpuc->n_events;
  583. if (is_x86_event(leader)) {
  584. if (n >= max_count)
  585. return -EINVAL;
  586. cpuc->event_list[n] = leader;
  587. n++;
  588. }
  589. if (!dogrp)
  590. return n;
  591. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  592. if (!is_x86_event(event) ||
  593. event->state <= PERF_EVENT_STATE_OFF)
  594. continue;
  595. if (n >= max_count)
  596. return -EINVAL;
  597. cpuc->event_list[n] = event;
  598. n++;
  599. }
  600. return n;
  601. }
  602. static inline void x86_assign_hw_event(struct perf_event *event,
  603. struct cpu_hw_events *cpuc, int i)
  604. {
  605. struct hw_perf_event *hwc = &event->hw;
  606. hwc->idx = cpuc->assign[i];
  607. hwc->last_cpu = smp_processor_id();
  608. hwc->last_tag = ++cpuc->tags[i];
  609. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  610. hwc->config_base = 0;
  611. hwc->event_base = 0;
  612. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  613. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  614. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED);
  615. } else {
  616. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  617. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  618. }
  619. }
  620. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  621. struct cpu_hw_events *cpuc,
  622. int i)
  623. {
  624. return hwc->idx == cpuc->assign[i] &&
  625. hwc->last_cpu == smp_processor_id() &&
  626. hwc->last_tag == cpuc->tags[i];
  627. }
  628. static void x86_pmu_start(struct perf_event *event, int flags);
  629. static void x86_pmu_enable(struct pmu *pmu)
  630. {
  631. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  632. struct perf_event *event;
  633. struct hw_perf_event *hwc;
  634. int i, added = cpuc->n_added;
  635. if (!x86_pmu_initialized())
  636. return;
  637. if (cpuc->enabled)
  638. return;
  639. if (cpuc->n_added) {
  640. int n_running = cpuc->n_events - cpuc->n_added;
  641. /*
  642. * apply assignment obtained either from
  643. * hw_perf_group_sched_in() or x86_pmu_enable()
  644. *
  645. * step1: save events moving to new counters
  646. * step2: reprogram moved events into new counters
  647. */
  648. for (i = 0; i < n_running; i++) {
  649. event = cpuc->event_list[i];
  650. hwc = &event->hw;
  651. /*
  652. * we can avoid reprogramming counter if:
  653. * - assigned same counter as last time
  654. * - running on same CPU as last time
  655. * - no other event has used the counter since
  656. */
  657. if (hwc->idx == -1 ||
  658. match_prev_assignment(hwc, cpuc, i))
  659. continue;
  660. /*
  661. * Ensure we don't accidentally enable a stopped
  662. * counter simply because we rescheduled.
  663. */
  664. if (hwc->state & PERF_HES_STOPPED)
  665. hwc->state |= PERF_HES_ARCH;
  666. x86_pmu_stop(event, PERF_EF_UPDATE);
  667. }
  668. for (i = 0; i < cpuc->n_events; i++) {
  669. event = cpuc->event_list[i];
  670. hwc = &event->hw;
  671. if (!match_prev_assignment(hwc, cpuc, i))
  672. x86_assign_hw_event(event, cpuc, i);
  673. else if (i < n_running)
  674. continue;
  675. if (hwc->state & PERF_HES_ARCH)
  676. continue;
  677. x86_pmu_start(event, PERF_EF_RELOAD);
  678. }
  679. cpuc->n_added = 0;
  680. perf_events_lapic_init();
  681. }
  682. cpuc->enabled = 1;
  683. barrier();
  684. x86_pmu.enable_all(added);
  685. }
  686. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  687. /*
  688. * Set the next IRQ period, based on the hwc->period_left value.
  689. * To be called with the event disabled in hw:
  690. */
  691. int x86_perf_event_set_period(struct perf_event *event)
  692. {
  693. struct hw_perf_event *hwc = &event->hw;
  694. s64 left = local64_read(&hwc->period_left);
  695. s64 period = hwc->sample_period;
  696. int ret = 0, idx = hwc->idx;
  697. if (idx == X86_PMC_IDX_FIXED_BTS)
  698. return 0;
  699. /*
  700. * If we are way outside a reasonable range then just skip forward:
  701. */
  702. if (unlikely(left <= -period)) {
  703. left = period;
  704. local64_set(&hwc->period_left, left);
  705. hwc->last_period = period;
  706. ret = 1;
  707. }
  708. if (unlikely(left <= 0)) {
  709. left += period;
  710. local64_set(&hwc->period_left, left);
  711. hwc->last_period = period;
  712. ret = 1;
  713. }
  714. /*
  715. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  716. */
  717. if (unlikely(left < 2))
  718. left = 2;
  719. if (left > x86_pmu.max_period)
  720. left = x86_pmu.max_period;
  721. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  722. /*
  723. * The hw event starts counting from this event offset,
  724. * mark it to be able to extra future deltas:
  725. */
  726. local64_set(&hwc->prev_count, (u64)-left);
  727. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  728. /*
  729. * Due to erratum on certan cpu we need
  730. * a second write to be sure the register
  731. * is updated properly
  732. */
  733. if (x86_pmu.perfctr_second_write) {
  734. wrmsrl(hwc->event_base,
  735. (u64)(-left) & x86_pmu.cntval_mask);
  736. }
  737. perf_event_update_userpage(event);
  738. return ret;
  739. }
  740. void x86_pmu_enable_event(struct perf_event *event)
  741. {
  742. if (__this_cpu_read(cpu_hw_events.enabled))
  743. __x86_pmu_enable_event(&event->hw,
  744. ARCH_PERFMON_EVENTSEL_ENABLE);
  745. }
  746. /*
  747. * Add a single event to the PMU.
  748. *
  749. * The event is added to the group of enabled events
  750. * but only if it can be scehduled with existing events.
  751. */
  752. static int x86_pmu_add(struct perf_event *event, int flags)
  753. {
  754. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  755. struct hw_perf_event *hwc;
  756. int assign[X86_PMC_IDX_MAX];
  757. int n, n0, ret;
  758. hwc = &event->hw;
  759. perf_pmu_disable(event->pmu);
  760. n0 = cpuc->n_events;
  761. ret = n = collect_events(cpuc, event, false);
  762. if (ret < 0)
  763. goto out;
  764. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  765. if (!(flags & PERF_EF_START))
  766. hwc->state |= PERF_HES_ARCH;
  767. /*
  768. * If group events scheduling transaction was started,
  769. * skip the schedulability test here, it will be performed
  770. * at commit time (->commit_txn) as a whole
  771. */
  772. if (cpuc->group_flag & PERF_EVENT_TXN)
  773. goto done_collect;
  774. ret = x86_pmu.schedule_events(cpuc, n, assign);
  775. if (ret)
  776. goto out;
  777. /*
  778. * copy new assignment, now we know it is possible
  779. * will be used by hw_perf_enable()
  780. */
  781. memcpy(cpuc->assign, assign, n*sizeof(int));
  782. done_collect:
  783. cpuc->n_events = n;
  784. cpuc->n_added += n - n0;
  785. cpuc->n_txn += n - n0;
  786. ret = 0;
  787. out:
  788. perf_pmu_enable(event->pmu);
  789. return ret;
  790. }
  791. static void x86_pmu_start(struct perf_event *event, int flags)
  792. {
  793. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  794. int idx = event->hw.idx;
  795. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  796. return;
  797. if (WARN_ON_ONCE(idx == -1))
  798. return;
  799. if (flags & PERF_EF_RELOAD) {
  800. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  801. x86_perf_event_set_period(event);
  802. }
  803. event->hw.state = 0;
  804. cpuc->events[idx] = event;
  805. __set_bit(idx, cpuc->active_mask);
  806. __set_bit(idx, cpuc->running);
  807. x86_pmu.enable(event);
  808. perf_event_update_userpage(event);
  809. }
  810. void perf_event_print_debug(void)
  811. {
  812. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  813. u64 pebs;
  814. struct cpu_hw_events *cpuc;
  815. unsigned long flags;
  816. int cpu, idx;
  817. if (!x86_pmu.num_counters)
  818. return;
  819. local_irq_save(flags);
  820. cpu = smp_processor_id();
  821. cpuc = &per_cpu(cpu_hw_events, cpu);
  822. if (x86_pmu.version >= 2) {
  823. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  824. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  825. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  826. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  827. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  828. pr_info("\n");
  829. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  830. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  831. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  832. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  833. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  834. }
  835. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  836. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  837. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  838. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  839. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  840. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  841. cpu, idx, pmc_ctrl);
  842. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  843. cpu, idx, pmc_count);
  844. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  845. cpu, idx, prev_left);
  846. }
  847. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  848. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  849. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  850. cpu, idx, pmc_count);
  851. }
  852. local_irq_restore(flags);
  853. }
  854. void x86_pmu_stop(struct perf_event *event, int flags)
  855. {
  856. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  857. struct hw_perf_event *hwc = &event->hw;
  858. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  859. x86_pmu.disable(event);
  860. cpuc->events[hwc->idx] = NULL;
  861. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  862. hwc->state |= PERF_HES_STOPPED;
  863. }
  864. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  865. /*
  866. * Drain the remaining delta count out of a event
  867. * that we are disabling:
  868. */
  869. x86_perf_event_update(event);
  870. hwc->state |= PERF_HES_UPTODATE;
  871. }
  872. }
  873. static void x86_pmu_del(struct perf_event *event, int flags)
  874. {
  875. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  876. int i;
  877. /*
  878. * If we're called during a txn, we don't need to do anything.
  879. * The events never got scheduled and ->cancel_txn will truncate
  880. * the event_list.
  881. */
  882. if (cpuc->group_flag & PERF_EVENT_TXN)
  883. return;
  884. x86_pmu_stop(event, PERF_EF_UPDATE);
  885. for (i = 0; i < cpuc->n_events; i++) {
  886. if (event == cpuc->event_list[i]) {
  887. if (x86_pmu.put_event_constraints)
  888. x86_pmu.put_event_constraints(cpuc, event);
  889. while (++i < cpuc->n_events)
  890. cpuc->event_list[i-1] = cpuc->event_list[i];
  891. --cpuc->n_events;
  892. break;
  893. }
  894. }
  895. perf_event_update_userpage(event);
  896. }
  897. int x86_pmu_handle_irq(struct pt_regs *regs)
  898. {
  899. struct perf_sample_data data;
  900. struct cpu_hw_events *cpuc;
  901. struct perf_event *event;
  902. int idx, handled = 0;
  903. u64 val;
  904. perf_sample_data_init(&data, 0);
  905. cpuc = &__get_cpu_var(cpu_hw_events);
  906. /*
  907. * Some chipsets need to unmask the LVTPC in a particular spot
  908. * inside the nmi handler. As a result, the unmasking was pushed
  909. * into all the nmi handlers.
  910. *
  911. * This generic handler doesn't seem to have any issues where the
  912. * unmasking occurs so it was left at the top.
  913. */
  914. apic_write(APIC_LVTPC, APIC_DM_NMI);
  915. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  916. if (!test_bit(idx, cpuc->active_mask)) {
  917. /*
  918. * Though we deactivated the counter some cpus
  919. * might still deliver spurious interrupts still
  920. * in flight. Catch them:
  921. */
  922. if (__test_and_clear_bit(idx, cpuc->running))
  923. handled++;
  924. continue;
  925. }
  926. event = cpuc->events[idx];
  927. val = x86_perf_event_update(event);
  928. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  929. continue;
  930. /*
  931. * event overflow
  932. */
  933. handled++;
  934. data.period = event->hw.last_period;
  935. if (!x86_perf_event_set_period(event))
  936. continue;
  937. if (perf_event_overflow(event, &data, regs))
  938. x86_pmu_stop(event, 0);
  939. }
  940. if (handled)
  941. inc_irq_stat(apic_perf_irqs);
  942. return handled;
  943. }
  944. void perf_events_lapic_init(void)
  945. {
  946. if (!x86_pmu.apic || !x86_pmu_initialized())
  947. return;
  948. /*
  949. * Always use NMI for PMU
  950. */
  951. apic_write(APIC_LVTPC, APIC_DM_NMI);
  952. }
  953. static int __kprobes
  954. perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  955. {
  956. if (!atomic_read(&active_events))
  957. return NMI_DONE;
  958. return x86_pmu.handle_irq(regs);
  959. }
  960. struct event_constraint emptyconstraint;
  961. struct event_constraint unconstrained;
  962. static int __cpuinit
  963. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  964. {
  965. unsigned int cpu = (long)hcpu;
  966. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  967. int ret = NOTIFY_OK;
  968. switch (action & ~CPU_TASKS_FROZEN) {
  969. case CPU_UP_PREPARE:
  970. cpuc->kfree_on_online = NULL;
  971. if (x86_pmu.cpu_prepare)
  972. ret = x86_pmu.cpu_prepare(cpu);
  973. break;
  974. case CPU_STARTING:
  975. if (x86_pmu.cpu_starting)
  976. x86_pmu.cpu_starting(cpu);
  977. break;
  978. case CPU_ONLINE:
  979. kfree(cpuc->kfree_on_online);
  980. break;
  981. case CPU_DYING:
  982. if (x86_pmu.cpu_dying)
  983. x86_pmu.cpu_dying(cpu);
  984. break;
  985. case CPU_UP_CANCELED:
  986. case CPU_DEAD:
  987. if (x86_pmu.cpu_dead)
  988. x86_pmu.cpu_dead(cpu);
  989. break;
  990. default:
  991. break;
  992. }
  993. return ret;
  994. }
  995. static void __init pmu_check_apic(void)
  996. {
  997. if (cpu_has_apic)
  998. return;
  999. x86_pmu.apic = 0;
  1000. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1001. pr_info("no hardware sampling interrupt available.\n");
  1002. }
  1003. static int __init init_hw_perf_events(void)
  1004. {
  1005. struct event_constraint *c;
  1006. int err;
  1007. pr_info("Performance Events: ");
  1008. switch (boot_cpu_data.x86_vendor) {
  1009. case X86_VENDOR_INTEL:
  1010. err = intel_pmu_init();
  1011. break;
  1012. case X86_VENDOR_AMD:
  1013. err = amd_pmu_init();
  1014. break;
  1015. default:
  1016. return 0;
  1017. }
  1018. if (err != 0) {
  1019. pr_cont("no PMU driver, software events only.\n");
  1020. return 0;
  1021. }
  1022. pmu_check_apic();
  1023. /* sanity check that the hardware exists or is emulated */
  1024. if (!check_hw_exists())
  1025. return 0;
  1026. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1027. if (x86_pmu.quirks)
  1028. x86_pmu.quirks();
  1029. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1030. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1031. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1032. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1033. }
  1034. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1035. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1036. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1037. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1038. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1039. }
  1040. x86_pmu.intel_ctrl |=
  1041. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1042. perf_events_lapic_init();
  1043. register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
  1044. unconstrained = (struct event_constraint)
  1045. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1046. 0, x86_pmu.num_counters, 0);
  1047. if (x86_pmu.event_constraints) {
  1048. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1049. if (c->cmask != X86_RAW_EVENT_MASK)
  1050. continue;
  1051. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1052. c->weight += x86_pmu.num_counters;
  1053. }
  1054. }
  1055. pr_info("... version: %d\n", x86_pmu.version);
  1056. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1057. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1058. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1059. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1060. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1061. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1062. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1063. perf_cpu_notifier(x86_pmu_notifier);
  1064. return 0;
  1065. }
  1066. early_initcall(init_hw_perf_events);
  1067. static inline void x86_pmu_read(struct perf_event *event)
  1068. {
  1069. x86_perf_event_update(event);
  1070. }
  1071. /*
  1072. * Start group events scheduling transaction
  1073. * Set the flag to make pmu::enable() not perform the
  1074. * schedulability test, it will be performed at commit time
  1075. */
  1076. static void x86_pmu_start_txn(struct pmu *pmu)
  1077. {
  1078. perf_pmu_disable(pmu);
  1079. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1080. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1081. }
  1082. /*
  1083. * Stop group events scheduling transaction
  1084. * Clear the flag and pmu::enable() will perform the
  1085. * schedulability test.
  1086. */
  1087. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1088. {
  1089. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1090. /*
  1091. * Truncate the collected events.
  1092. */
  1093. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1094. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1095. perf_pmu_enable(pmu);
  1096. }
  1097. /*
  1098. * Commit group events scheduling transaction
  1099. * Perform the group schedulability test as a whole
  1100. * Return 0 if success
  1101. */
  1102. static int x86_pmu_commit_txn(struct pmu *pmu)
  1103. {
  1104. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1105. int assign[X86_PMC_IDX_MAX];
  1106. int n, ret;
  1107. n = cpuc->n_events;
  1108. if (!x86_pmu_initialized())
  1109. return -EAGAIN;
  1110. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1111. if (ret)
  1112. return ret;
  1113. /*
  1114. * copy new assignment, now we know it is possible
  1115. * will be used by hw_perf_enable()
  1116. */
  1117. memcpy(cpuc->assign, assign, n*sizeof(int));
  1118. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1119. perf_pmu_enable(pmu);
  1120. return 0;
  1121. }
  1122. /*
  1123. * a fake_cpuc is used to validate event groups. Due to
  1124. * the extra reg logic, we need to also allocate a fake
  1125. * per_core and per_cpu structure. Otherwise, group events
  1126. * using extra reg may conflict without the kernel being
  1127. * able to catch this when the last event gets added to
  1128. * the group.
  1129. */
  1130. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1131. {
  1132. kfree(cpuc->shared_regs);
  1133. kfree(cpuc);
  1134. }
  1135. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1136. {
  1137. struct cpu_hw_events *cpuc;
  1138. int cpu = raw_smp_processor_id();
  1139. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1140. if (!cpuc)
  1141. return ERR_PTR(-ENOMEM);
  1142. /* only needed, if we have extra_regs */
  1143. if (x86_pmu.extra_regs) {
  1144. cpuc->shared_regs = allocate_shared_regs(cpu);
  1145. if (!cpuc->shared_regs)
  1146. goto error;
  1147. }
  1148. return cpuc;
  1149. error:
  1150. free_fake_cpuc(cpuc);
  1151. return ERR_PTR(-ENOMEM);
  1152. }
  1153. /*
  1154. * validate that we can schedule this event
  1155. */
  1156. static int validate_event(struct perf_event *event)
  1157. {
  1158. struct cpu_hw_events *fake_cpuc;
  1159. struct event_constraint *c;
  1160. int ret = 0;
  1161. fake_cpuc = allocate_fake_cpuc();
  1162. if (IS_ERR(fake_cpuc))
  1163. return PTR_ERR(fake_cpuc);
  1164. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1165. if (!c || !c->weight)
  1166. ret = -EINVAL;
  1167. if (x86_pmu.put_event_constraints)
  1168. x86_pmu.put_event_constraints(fake_cpuc, event);
  1169. free_fake_cpuc(fake_cpuc);
  1170. return ret;
  1171. }
  1172. /*
  1173. * validate a single event group
  1174. *
  1175. * validation include:
  1176. * - check events are compatible which each other
  1177. * - events do not compete for the same counter
  1178. * - number of events <= number of counters
  1179. *
  1180. * validation ensures the group can be loaded onto the
  1181. * PMU if it was the only group available.
  1182. */
  1183. static int validate_group(struct perf_event *event)
  1184. {
  1185. struct perf_event *leader = event->group_leader;
  1186. struct cpu_hw_events *fake_cpuc;
  1187. int ret = -EINVAL, n;
  1188. fake_cpuc = allocate_fake_cpuc();
  1189. if (IS_ERR(fake_cpuc))
  1190. return PTR_ERR(fake_cpuc);
  1191. /*
  1192. * the event is not yet connected with its
  1193. * siblings therefore we must first collect
  1194. * existing siblings, then add the new event
  1195. * before we can simulate the scheduling
  1196. */
  1197. n = collect_events(fake_cpuc, leader, true);
  1198. if (n < 0)
  1199. goto out;
  1200. fake_cpuc->n_events = n;
  1201. n = collect_events(fake_cpuc, event, false);
  1202. if (n < 0)
  1203. goto out;
  1204. fake_cpuc->n_events = n;
  1205. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1206. out:
  1207. free_fake_cpuc(fake_cpuc);
  1208. return ret;
  1209. }
  1210. static int x86_pmu_event_init(struct perf_event *event)
  1211. {
  1212. struct pmu *tmp;
  1213. int err;
  1214. switch (event->attr.type) {
  1215. case PERF_TYPE_RAW:
  1216. case PERF_TYPE_HARDWARE:
  1217. case PERF_TYPE_HW_CACHE:
  1218. break;
  1219. default:
  1220. return -ENOENT;
  1221. }
  1222. err = __x86_pmu_event_init(event);
  1223. if (!err) {
  1224. /*
  1225. * we temporarily connect event to its pmu
  1226. * such that validate_group() can classify
  1227. * it as an x86 event using is_x86_event()
  1228. */
  1229. tmp = event->pmu;
  1230. event->pmu = &pmu;
  1231. if (event->group_leader != event)
  1232. err = validate_group(event);
  1233. else
  1234. err = validate_event(event);
  1235. event->pmu = tmp;
  1236. }
  1237. if (err) {
  1238. if (event->destroy)
  1239. event->destroy(event);
  1240. }
  1241. return err;
  1242. }
  1243. static struct pmu pmu = {
  1244. .pmu_enable = x86_pmu_enable,
  1245. .pmu_disable = x86_pmu_disable,
  1246. .event_init = x86_pmu_event_init,
  1247. .add = x86_pmu_add,
  1248. .del = x86_pmu_del,
  1249. .start = x86_pmu_start,
  1250. .stop = x86_pmu_stop,
  1251. .read = x86_pmu_read,
  1252. .start_txn = x86_pmu_start_txn,
  1253. .cancel_txn = x86_pmu_cancel_txn,
  1254. .commit_txn = x86_pmu_commit_txn,
  1255. };
  1256. /*
  1257. * callchain support
  1258. */
  1259. static int backtrace_stack(void *data, char *name)
  1260. {
  1261. return 0;
  1262. }
  1263. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1264. {
  1265. struct perf_callchain_entry *entry = data;
  1266. perf_callchain_store(entry, addr);
  1267. }
  1268. static const struct stacktrace_ops backtrace_ops = {
  1269. .stack = backtrace_stack,
  1270. .address = backtrace_address,
  1271. .walk_stack = print_context_stack_bp,
  1272. };
  1273. void
  1274. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1275. {
  1276. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1277. /* TODO: We don't support guest os callchain now */
  1278. return;
  1279. }
  1280. perf_callchain_store(entry, regs->ip);
  1281. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1282. }
  1283. #ifdef CONFIG_COMPAT
  1284. static inline int
  1285. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1286. {
  1287. /* 32-bit process in 64-bit kernel. */
  1288. struct stack_frame_ia32 frame;
  1289. const void __user *fp;
  1290. if (!test_thread_flag(TIF_IA32))
  1291. return 0;
  1292. fp = compat_ptr(regs->bp);
  1293. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1294. unsigned long bytes;
  1295. frame.next_frame = 0;
  1296. frame.return_address = 0;
  1297. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1298. if (bytes != sizeof(frame))
  1299. break;
  1300. if (fp < compat_ptr(regs->sp))
  1301. break;
  1302. perf_callchain_store(entry, frame.return_address);
  1303. fp = compat_ptr(frame.next_frame);
  1304. }
  1305. return 1;
  1306. }
  1307. #else
  1308. static inline int
  1309. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1310. {
  1311. return 0;
  1312. }
  1313. #endif
  1314. void
  1315. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1316. {
  1317. struct stack_frame frame;
  1318. const void __user *fp;
  1319. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1320. /* TODO: We don't support guest os callchain now */
  1321. return;
  1322. }
  1323. fp = (void __user *)regs->bp;
  1324. perf_callchain_store(entry, regs->ip);
  1325. if (!current->mm)
  1326. return;
  1327. if (perf_callchain_user32(regs, entry))
  1328. return;
  1329. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1330. unsigned long bytes;
  1331. frame.next_frame = NULL;
  1332. frame.return_address = 0;
  1333. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1334. if (bytes != sizeof(frame))
  1335. break;
  1336. if ((unsigned long)fp < regs->sp)
  1337. break;
  1338. perf_callchain_store(entry, frame.return_address);
  1339. fp = frame.next_frame;
  1340. }
  1341. }
  1342. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1343. {
  1344. unsigned long ip;
  1345. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1346. ip = perf_guest_cbs->get_guest_ip();
  1347. else
  1348. ip = instruction_pointer(regs);
  1349. return ip;
  1350. }
  1351. unsigned long perf_misc_flags(struct pt_regs *regs)
  1352. {
  1353. int misc = 0;
  1354. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1355. if (perf_guest_cbs->is_user_mode())
  1356. misc |= PERF_RECORD_MISC_GUEST_USER;
  1357. else
  1358. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1359. } else {
  1360. if (user_mode(regs))
  1361. misc |= PERF_RECORD_MISC_USER;
  1362. else
  1363. misc |= PERF_RECORD_MISC_KERNEL;
  1364. }
  1365. if (regs->flags & PERF_EFLAGS_EXACT)
  1366. misc |= PERF_RECORD_MISC_EXACT_IP;
  1367. return misc;
  1368. }