bts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * BTS PMU driver for perf
  3. * Copyright (c) 2013-2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #undef DEBUG
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/bitops.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/device.h>
  21. #include <linux/coredump.h>
  22. #include <asm-generic/sizes.h>
  23. #include <asm/perf_event.h>
  24. #include "../perf_event.h"
  25. struct bts_ctx {
  26. struct perf_output_handle handle;
  27. struct debug_store ds_back;
  28. int state;
  29. };
  30. /* BTS context states: */
  31. enum {
  32. /* no ongoing AUX transactions */
  33. BTS_STATE_STOPPED = 0,
  34. /* AUX transaction is on, BTS tracing is disabled */
  35. BTS_STATE_INACTIVE,
  36. /* AUX transaction is on, BTS tracing is running */
  37. BTS_STATE_ACTIVE,
  38. };
  39. static DEFINE_PER_CPU(struct bts_ctx, bts_ctx);
  40. #define BTS_RECORD_SIZE 24
  41. #define BTS_SAFETY_MARGIN 4080
  42. struct bts_phys {
  43. struct page *page;
  44. unsigned long size;
  45. unsigned long offset;
  46. unsigned long displacement;
  47. };
  48. struct bts_buffer {
  49. size_t real_size; /* multiple of BTS_RECORD_SIZE */
  50. unsigned int nr_pages;
  51. unsigned int nr_bufs;
  52. unsigned int cur_buf;
  53. bool snapshot;
  54. local_t data_size;
  55. local_t head;
  56. unsigned long end;
  57. void **data_pages;
  58. struct bts_phys buf[0];
  59. };
  60. static struct pmu bts_pmu;
  61. static int buf_nr_pages(struct page *page)
  62. {
  63. if (!PagePrivate(page))
  64. return 1;
  65. return 1 << page_private(page);
  66. }
  67. static size_t buf_size(struct page *page)
  68. {
  69. return buf_nr_pages(page) * PAGE_SIZE;
  70. }
  71. static void *
  72. bts_buffer_setup_aux(struct perf_event *event, void **pages,
  73. int nr_pages, bool overwrite)
  74. {
  75. struct bts_buffer *buf;
  76. struct page *page;
  77. int cpu = event->cpu;
  78. int node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  79. unsigned long offset;
  80. size_t size = nr_pages << PAGE_SHIFT;
  81. int pg, nbuf, pad;
  82. /* count all the high order buffers */
  83. for (pg = 0, nbuf = 0; pg < nr_pages;) {
  84. page = virt_to_page(pages[pg]);
  85. pg += buf_nr_pages(page);
  86. nbuf++;
  87. }
  88. /*
  89. * to avoid interrupts in overwrite mode, only allow one physical
  90. */
  91. if (overwrite && nbuf > 1)
  92. return NULL;
  93. buf = kzalloc_node(offsetof(struct bts_buffer, buf[nbuf]), GFP_KERNEL, node);
  94. if (!buf)
  95. return NULL;
  96. buf->nr_pages = nr_pages;
  97. buf->nr_bufs = nbuf;
  98. buf->snapshot = overwrite;
  99. buf->data_pages = pages;
  100. buf->real_size = size - size % BTS_RECORD_SIZE;
  101. for (pg = 0, nbuf = 0, offset = 0, pad = 0; nbuf < buf->nr_bufs; nbuf++) {
  102. unsigned int __nr_pages;
  103. page = virt_to_page(pages[pg]);
  104. __nr_pages = buf_nr_pages(page);
  105. buf->buf[nbuf].page = page;
  106. buf->buf[nbuf].offset = offset;
  107. buf->buf[nbuf].displacement = (pad ? BTS_RECORD_SIZE - pad : 0);
  108. buf->buf[nbuf].size = buf_size(page) - buf->buf[nbuf].displacement;
  109. pad = buf->buf[nbuf].size % BTS_RECORD_SIZE;
  110. buf->buf[nbuf].size -= pad;
  111. pg += __nr_pages;
  112. offset += __nr_pages << PAGE_SHIFT;
  113. }
  114. return buf;
  115. }
  116. static void bts_buffer_free_aux(void *data)
  117. {
  118. kfree(data);
  119. }
  120. static unsigned long bts_buffer_offset(struct bts_buffer *buf, unsigned int idx)
  121. {
  122. return buf->buf[idx].offset + buf->buf[idx].displacement;
  123. }
  124. static void
  125. bts_config_buffer(struct bts_buffer *buf)
  126. {
  127. int cpu = raw_smp_processor_id();
  128. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  129. struct bts_phys *phys = &buf->buf[buf->cur_buf];
  130. unsigned long index, thresh = 0, end = phys->size;
  131. struct page *page = phys->page;
  132. index = local_read(&buf->head);
  133. if (!buf->snapshot) {
  134. if (buf->end < phys->offset + buf_size(page))
  135. end = buf->end - phys->offset - phys->displacement;
  136. index -= phys->offset + phys->displacement;
  137. if (end - index > BTS_SAFETY_MARGIN)
  138. thresh = end - BTS_SAFETY_MARGIN;
  139. else if (end - index > BTS_RECORD_SIZE)
  140. thresh = end - BTS_RECORD_SIZE;
  141. else
  142. thresh = end;
  143. }
  144. ds->bts_buffer_base = (u64)(long)page_address(page) + phys->displacement;
  145. ds->bts_index = ds->bts_buffer_base + index;
  146. ds->bts_absolute_maximum = ds->bts_buffer_base + end;
  147. ds->bts_interrupt_threshold = !buf->snapshot
  148. ? ds->bts_buffer_base + thresh
  149. : ds->bts_absolute_maximum + BTS_RECORD_SIZE;
  150. }
  151. static void bts_buffer_pad_out(struct bts_phys *phys, unsigned long head)
  152. {
  153. unsigned long index = head - phys->offset;
  154. memset(page_address(phys->page) + index, 0, phys->size - index);
  155. }
  156. static void bts_update(struct bts_ctx *bts)
  157. {
  158. int cpu = raw_smp_processor_id();
  159. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  160. struct bts_buffer *buf = perf_get_aux(&bts->handle);
  161. unsigned long index = ds->bts_index - ds->bts_buffer_base, old, head;
  162. if (!buf)
  163. return;
  164. head = index + bts_buffer_offset(buf, buf->cur_buf);
  165. old = local_xchg(&buf->head, head);
  166. if (!buf->snapshot) {
  167. if (old == head)
  168. return;
  169. if (ds->bts_index >= ds->bts_absolute_maximum)
  170. perf_aux_output_flag(&bts->handle,
  171. PERF_AUX_FLAG_TRUNCATED);
  172. /*
  173. * old and head are always in the same physical buffer, so we
  174. * can subtract them to get the data size.
  175. */
  176. local_add(head - old, &buf->data_size);
  177. } else {
  178. local_set(&buf->data_size, head);
  179. }
  180. }
  181. static int
  182. bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle);
  183. /*
  184. * Ordering PMU callbacks wrt themselves and the PMI is done by means
  185. * of bts::state, which:
  186. * - is set when bts::handle::event is valid, that is, between
  187. * perf_aux_output_begin() and perf_aux_output_end();
  188. * - is zero otherwise;
  189. * - is ordered against bts::handle::event with a compiler barrier.
  190. */
  191. static void __bts_event_start(struct perf_event *event)
  192. {
  193. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  194. struct bts_buffer *buf = perf_get_aux(&bts->handle);
  195. u64 config = 0;
  196. if (!buf->snapshot)
  197. config |= ARCH_PERFMON_EVENTSEL_INT;
  198. if (!event->attr.exclude_kernel)
  199. config |= ARCH_PERFMON_EVENTSEL_OS;
  200. if (!event->attr.exclude_user)
  201. config |= ARCH_PERFMON_EVENTSEL_USR;
  202. bts_config_buffer(buf);
  203. /*
  204. * local barrier to make sure that ds configuration made it
  205. * before we enable BTS and bts::state goes ACTIVE
  206. */
  207. wmb();
  208. /* INACTIVE/STOPPED -> ACTIVE */
  209. WRITE_ONCE(bts->state, BTS_STATE_ACTIVE);
  210. intel_pmu_enable_bts(config);
  211. }
  212. static void bts_event_start(struct perf_event *event, int flags)
  213. {
  214. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  215. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  216. struct bts_buffer *buf;
  217. buf = perf_aux_output_begin(&bts->handle, event);
  218. if (!buf)
  219. goto fail_stop;
  220. if (bts_buffer_reset(buf, &bts->handle))
  221. goto fail_end_stop;
  222. bts->ds_back.bts_buffer_base = cpuc->ds->bts_buffer_base;
  223. bts->ds_back.bts_absolute_maximum = cpuc->ds->bts_absolute_maximum;
  224. bts->ds_back.bts_interrupt_threshold = cpuc->ds->bts_interrupt_threshold;
  225. perf_event_itrace_started(event);
  226. event->hw.state = 0;
  227. __bts_event_start(event);
  228. return;
  229. fail_end_stop:
  230. perf_aux_output_end(&bts->handle, 0);
  231. fail_stop:
  232. event->hw.state = PERF_HES_STOPPED;
  233. }
  234. static void __bts_event_stop(struct perf_event *event, int state)
  235. {
  236. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  237. /* ACTIVE -> INACTIVE(PMI)/STOPPED(->stop()) */
  238. WRITE_ONCE(bts->state, state);
  239. /*
  240. * No extra synchronization is mandated by the documentation to have
  241. * BTS data stores globally visible.
  242. */
  243. intel_pmu_disable_bts();
  244. }
  245. static void bts_event_stop(struct perf_event *event, int flags)
  246. {
  247. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  248. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  249. struct bts_buffer *buf = NULL;
  250. int state = READ_ONCE(bts->state);
  251. if (state == BTS_STATE_ACTIVE)
  252. __bts_event_stop(event, BTS_STATE_STOPPED);
  253. if (state != BTS_STATE_STOPPED)
  254. buf = perf_get_aux(&bts->handle);
  255. event->hw.state |= PERF_HES_STOPPED;
  256. if (flags & PERF_EF_UPDATE) {
  257. bts_update(bts);
  258. if (buf) {
  259. if (buf->snapshot)
  260. bts->handle.head =
  261. local_xchg(&buf->data_size,
  262. buf->nr_pages << PAGE_SHIFT);
  263. perf_aux_output_end(&bts->handle,
  264. local_xchg(&buf->data_size, 0));
  265. }
  266. cpuc->ds->bts_index = bts->ds_back.bts_buffer_base;
  267. cpuc->ds->bts_buffer_base = bts->ds_back.bts_buffer_base;
  268. cpuc->ds->bts_absolute_maximum = bts->ds_back.bts_absolute_maximum;
  269. cpuc->ds->bts_interrupt_threshold = bts->ds_back.bts_interrupt_threshold;
  270. }
  271. }
  272. void intel_bts_enable_local(void)
  273. {
  274. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  275. int state = READ_ONCE(bts->state);
  276. /*
  277. * Here we transition from INACTIVE to ACTIVE;
  278. * if we instead are STOPPED from the interrupt handler,
  279. * stay that way. Can't be ACTIVE here though.
  280. */
  281. if (WARN_ON_ONCE(state == BTS_STATE_ACTIVE))
  282. return;
  283. if (state == BTS_STATE_STOPPED)
  284. return;
  285. if (bts->handle.event)
  286. __bts_event_start(bts->handle.event);
  287. }
  288. void intel_bts_disable_local(void)
  289. {
  290. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  291. /*
  292. * Here we transition from ACTIVE to INACTIVE;
  293. * do nothing for STOPPED or INACTIVE.
  294. */
  295. if (READ_ONCE(bts->state) != BTS_STATE_ACTIVE)
  296. return;
  297. if (bts->handle.event)
  298. __bts_event_stop(bts->handle.event, BTS_STATE_INACTIVE);
  299. }
  300. static int
  301. bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle)
  302. {
  303. unsigned long head, space, next_space, pad, gap, skip, wakeup;
  304. unsigned int next_buf;
  305. struct bts_phys *phys, *next_phys;
  306. int ret;
  307. if (buf->snapshot)
  308. return 0;
  309. head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
  310. phys = &buf->buf[buf->cur_buf];
  311. space = phys->offset + phys->displacement + phys->size - head;
  312. pad = space;
  313. if (space > handle->size) {
  314. space = handle->size;
  315. space -= space % BTS_RECORD_SIZE;
  316. }
  317. if (space <= BTS_SAFETY_MARGIN) {
  318. /* See if next phys buffer has more space */
  319. next_buf = buf->cur_buf + 1;
  320. if (next_buf >= buf->nr_bufs)
  321. next_buf = 0;
  322. next_phys = &buf->buf[next_buf];
  323. gap = buf_size(phys->page) - phys->displacement - phys->size +
  324. next_phys->displacement;
  325. skip = pad + gap;
  326. if (handle->size >= skip) {
  327. next_space = next_phys->size;
  328. if (next_space + skip > handle->size) {
  329. next_space = handle->size - skip;
  330. next_space -= next_space % BTS_RECORD_SIZE;
  331. }
  332. if (next_space > space || !space) {
  333. if (pad)
  334. bts_buffer_pad_out(phys, head);
  335. ret = perf_aux_output_skip(handle, skip);
  336. if (ret)
  337. return ret;
  338. /* Advance to next phys buffer */
  339. phys = next_phys;
  340. space = next_space;
  341. head = phys->offset + phys->displacement;
  342. /*
  343. * After this, cur_buf and head won't match ds
  344. * anymore, so we must not be racing with
  345. * bts_update().
  346. */
  347. buf->cur_buf = next_buf;
  348. local_set(&buf->head, head);
  349. }
  350. }
  351. }
  352. /* Don't go far beyond wakeup watermark */
  353. wakeup = BTS_SAFETY_MARGIN + BTS_RECORD_SIZE + handle->wakeup -
  354. handle->head;
  355. if (space > wakeup) {
  356. space = wakeup;
  357. space -= space % BTS_RECORD_SIZE;
  358. }
  359. buf->end = head + space;
  360. /*
  361. * If we have no space, the lost notification would have been sent when
  362. * we hit absolute_maximum - see bts_update()
  363. */
  364. if (!space)
  365. return -ENOSPC;
  366. return 0;
  367. }
  368. int intel_bts_interrupt(void)
  369. {
  370. struct debug_store *ds = this_cpu_ptr(&cpu_hw_events)->ds;
  371. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  372. struct perf_event *event = bts->handle.event;
  373. struct bts_buffer *buf;
  374. s64 old_head;
  375. int err = -ENOSPC, handled = 0;
  376. /*
  377. * The only surefire way of knowing if this NMI is ours is by checking
  378. * the write ptr against the PMI threshold.
  379. */
  380. if (ds && (ds->bts_index >= ds->bts_interrupt_threshold))
  381. handled = 1;
  382. /*
  383. * this is wrapped in intel_bts_enable_local/intel_bts_disable_local,
  384. * so we can only be INACTIVE or STOPPED
  385. */
  386. if (READ_ONCE(bts->state) == BTS_STATE_STOPPED)
  387. return handled;
  388. buf = perf_get_aux(&bts->handle);
  389. if (!buf)
  390. return handled;
  391. /*
  392. * Skip snapshot counters: they don't use the interrupt, but
  393. * there's no other way of telling, because the pointer will
  394. * keep moving
  395. */
  396. if (buf->snapshot)
  397. return 0;
  398. old_head = local_read(&buf->head);
  399. bts_update(bts);
  400. /* no new data */
  401. if (old_head == local_read(&buf->head))
  402. return handled;
  403. perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0));
  404. buf = perf_aux_output_begin(&bts->handle, event);
  405. if (buf)
  406. err = bts_buffer_reset(buf, &bts->handle);
  407. if (err) {
  408. WRITE_ONCE(bts->state, BTS_STATE_STOPPED);
  409. if (buf) {
  410. /*
  411. * BTS_STATE_STOPPED should be visible before
  412. * cleared handle::event
  413. */
  414. barrier();
  415. perf_aux_output_end(&bts->handle, 0);
  416. }
  417. }
  418. return 1;
  419. }
  420. static void bts_event_del(struct perf_event *event, int mode)
  421. {
  422. bts_event_stop(event, PERF_EF_UPDATE);
  423. }
  424. static int bts_event_add(struct perf_event *event, int mode)
  425. {
  426. struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
  427. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  428. struct hw_perf_event *hwc = &event->hw;
  429. event->hw.state = PERF_HES_STOPPED;
  430. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  431. return -EBUSY;
  432. if (bts->handle.event)
  433. return -EBUSY;
  434. if (mode & PERF_EF_START) {
  435. bts_event_start(event, 0);
  436. if (hwc->state & PERF_HES_STOPPED)
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. static void bts_event_destroy(struct perf_event *event)
  442. {
  443. x86_release_hardware();
  444. x86_del_exclusive(x86_lbr_exclusive_bts);
  445. }
  446. static int bts_event_init(struct perf_event *event)
  447. {
  448. int ret;
  449. if (event->attr.type != bts_pmu.type)
  450. return -ENOENT;
  451. /*
  452. * BTS leaks kernel addresses even when CPL0 tracing is
  453. * disabled, so disallow intel_bts driver for unprivileged
  454. * users on paranoid systems since it provides trace data
  455. * to the user in a zero-copy fashion.
  456. *
  457. * Note that the default paranoia setting permits unprivileged
  458. * users to profile the kernel.
  459. */
  460. if (event->attr.exclude_kernel && perf_paranoid_kernel() &&
  461. !capable(CAP_SYS_ADMIN))
  462. return -EACCES;
  463. if (x86_add_exclusive(x86_lbr_exclusive_bts))
  464. return -EBUSY;
  465. ret = x86_reserve_hardware();
  466. if (ret) {
  467. x86_del_exclusive(x86_lbr_exclusive_bts);
  468. return ret;
  469. }
  470. event->destroy = bts_event_destroy;
  471. return 0;
  472. }
  473. static void bts_event_read(struct perf_event *event)
  474. {
  475. }
  476. static __init int bts_init(void)
  477. {
  478. if (!boot_cpu_has(X86_FEATURE_DTES64) || !x86_pmu.bts)
  479. return -ENODEV;
  480. if (boot_cpu_has(X86_FEATURE_PTI)) {
  481. /*
  482. * BTS hardware writes through a virtual memory map we must
  483. * either use the kernel physical map, or the user mapping of
  484. * the AUX buffer.
  485. *
  486. * However, since this driver supports per-CPU and per-task inherit
  487. * we cannot use the user mapping since it will not be availble
  488. * if we're not running the owning process.
  489. *
  490. * With PTI we can't use the kernal map either, because its not
  491. * there when we run userspace.
  492. *
  493. * For now, disable this driver when using PTI.
  494. */
  495. return -ENODEV;
  496. }
  497. bts_pmu.capabilities = PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_ITRACE |
  498. PERF_PMU_CAP_EXCLUSIVE;
  499. bts_pmu.task_ctx_nr = perf_sw_context;
  500. bts_pmu.event_init = bts_event_init;
  501. bts_pmu.add = bts_event_add;
  502. bts_pmu.del = bts_event_del;
  503. bts_pmu.start = bts_event_start;
  504. bts_pmu.stop = bts_event_stop;
  505. bts_pmu.read = bts_event_read;
  506. bts_pmu.setup_aux = bts_buffer_setup_aux;
  507. bts_pmu.free_aux = bts_buffer_free_aux;
  508. return perf_pmu_register(&bts_pmu, "intel_bts", -1);
  509. }
  510. arch_initcall(bts_init);