bts.c 15 KB

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