bts.c 15 KB

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