2 * BTS PMU driver for perf
3 * Copyright (c) 2013-2014, Intel Corporation.
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.
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
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/bitops.h>
20 #include <linux/types.h>
21 #include <linux/slab.h>
22 #include <linux/debugfs.h>
23 #include <linux/device.h>
24 #include <linux/coredump.h>
26 #include <asm-generic/sizes.h>
27 #include <asm/perf_event.h>
29 #include "../perf_event.h"
32 struct perf_output_handle handle
;
33 struct debug_store ds_back
;
37 /* BTS context states: */
39 /* no ongoing AUX transactions */
40 BTS_STATE_STOPPED
= 0,
41 /* AUX transaction is on, BTS tracing is disabled */
43 /* AUX transaction is on, BTS tracing is running */
47 static DEFINE_PER_CPU(struct bts_ctx
, bts_ctx
);
49 #define BTS_RECORD_SIZE 24
50 #define BTS_SAFETY_MARGIN 4080
56 unsigned long displacement
;
60 size_t real_size
; /* multiple of BTS_RECORD_SIZE */
61 unsigned int nr_pages
;
69 struct bts_phys buf
[0];
72 static struct pmu bts_pmu
;
74 static size_t buf_size(struct page
*page
)
76 return 1 << (PAGE_SHIFT
+ page_private(page
));
80 bts_buffer_setup_aux(struct perf_event
*event
, void **pages
,
81 int nr_pages
, bool overwrite
)
83 struct bts_buffer
*buf
;
86 int node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
88 size_t size
= nr_pages
<< PAGE_SHIFT
;
91 /* count all the high order buffers */
92 for (pg
= 0, nbuf
= 0; pg
< nr_pages
;) {
93 page
= virt_to_page(pages
[pg
]);
94 if (WARN_ON_ONCE(!PagePrivate(page
) && nr_pages
> 1))
96 pg
+= 1 << page_private(page
);
101 * to avoid interrupts in overwrite mode, only allow one physical
103 if (overwrite
&& nbuf
> 1)
106 buf
= kzalloc_node(offsetof(struct bts_buffer
, buf
[nbuf
]), GFP_KERNEL
, node
);
110 buf
->nr_pages
= nr_pages
;
112 buf
->snapshot
= overwrite
;
113 buf
->data_pages
= pages
;
114 buf
->real_size
= size
- size
% BTS_RECORD_SIZE
;
116 for (pg
= 0, nbuf
= 0, offset
= 0, pad
= 0; nbuf
< buf
->nr_bufs
; nbuf
++) {
117 unsigned int __nr_pages
;
119 page
= virt_to_page(pages
[pg
]);
120 __nr_pages
= PagePrivate(page
) ? 1 << page_private(page
) : 1;
121 buf
->buf
[nbuf
].page
= page
;
122 buf
->buf
[nbuf
].offset
= offset
;
123 buf
->buf
[nbuf
].displacement
= (pad
? BTS_RECORD_SIZE
- pad
: 0);
124 buf
->buf
[nbuf
].size
= buf_size(page
) - buf
->buf
[nbuf
].displacement
;
125 pad
= buf
->buf
[nbuf
].size
% BTS_RECORD_SIZE
;
126 buf
->buf
[nbuf
].size
-= pad
;
129 offset
+= __nr_pages
<< PAGE_SHIFT
;
135 static void bts_buffer_free_aux(void *data
)
140 static unsigned long bts_buffer_offset(struct bts_buffer
*buf
, unsigned int idx
)
142 return buf
->buf
[idx
].offset
+ buf
->buf
[idx
].displacement
;
146 bts_config_buffer(struct bts_buffer
*buf
)
148 int cpu
= raw_smp_processor_id();
149 struct debug_store
*ds
= per_cpu(cpu_hw_events
, cpu
).ds
;
150 struct bts_phys
*phys
= &buf
->buf
[buf
->cur_buf
];
151 unsigned long index
, thresh
= 0, end
= phys
->size
;
152 struct page
*page
= phys
->page
;
154 index
= local_read(&buf
->head
);
156 if (!buf
->snapshot
) {
157 if (buf
->end
< phys
->offset
+ buf_size(page
))
158 end
= buf
->end
- phys
->offset
- phys
->displacement
;
160 index
-= phys
->offset
+ phys
->displacement
;
162 if (end
- index
> BTS_SAFETY_MARGIN
)
163 thresh
= end
- BTS_SAFETY_MARGIN
;
164 else if (end
- index
> BTS_RECORD_SIZE
)
165 thresh
= end
- BTS_RECORD_SIZE
;
170 ds
->bts_buffer_base
= (u64
)(long)page_address(page
) + phys
->displacement
;
171 ds
->bts_index
= ds
->bts_buffer_base
+ index
;
172 ds
->bts_absolute_maximum
= ds
->bts_buffer_base
+ end
;
173 ds
->bts_interrupt_threshold
= !buf
->snapshot
174 ? ds
->bts_buffer_base
+ thresh
175 : ds
->bts_absolute_maximum
+ BTS_RECORD_SIZE
;
178 static void bts_buffer_pad_out(struct bts_phys
*phys
, unsigned long head
)
180 unsigned long index
= head
- phys
->offset
;
182 memset(page_address(phys
->page
) + index
, 0, phys
->size
- index
);
185 static void bts_update(struct bts_ctx
*bts
)
187 int cpu
= raw_smp_processor_id();
188 struct debug_store
*ds
= per_cpu(cpu_hw_events
, cpu
).ds
;
189 struct bts_buffer
*buf
= perf_get_aux(&bts
->handle
);
190 unsigned long index
= ds
->bts_index
- ds
->bts_buffer_base
, old
, head
;
195 head
= index
+ bts_buffer_offset(buf
, buf
->cur_buf
);
196 old
= local_xchg(&buf
->head
, head
);
198 if (!buf
->snapshot
) {
202 if (ds
->bts_index
>= ds
->bts_absolute_maximum
)
203 perf_aux_output_flag(&bts
->handle
,
204 PERF_AUX_FLAG_TRUNCATED
);
207 * old and head are always in the same physical buffer, so we
208 * can subtract them to get the data size.
210 local_add(head
- old
, &buf
->data_size
);
212 local_set(&buf
->data_size
, head
);
217 bts_buffer_reset(struct bts_buffer
*buf
, struct perf_output_handle
*handle
);
220 * Ordering PMU callbacks wrt themselves and the PMI is done by means
221 * of bts::state, which:
222 * - is set when bts::handle::event is valid, that is, between
223 * perf_aux_output_begin() and perf_aux_output_end();
224 * - is zero otherwise;
225 * - is ordered against bts::handle::event with a compiler barrier.
228 static void __bts_event_start(struct perf_event
*event
)
230 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
231 struct bts_buffer
*buf
= perf_get_aux(&bts
->handle
);
235 config
|= ARCH_PERFMON_EVENTSEL_INT
;
236 if (!event
->attr
.exclude_kernel
)
237 config
|= ARCH_PERFMON_EVENTSEL_OS
;
238 if (!event
->attr
.exclude_user
)
239 config
|= ARCH_PERFMON_EVENTSEL_USR
;
241 bts_config_buffer(buf
);
244 * local barrier to make sure that ds configuration made it
245 * before we enable BTS and bts::state goes ACTIVE
249 /* INACTIVE/STOPPED -> ACTIVE */
250 WRITE_ONCE(bts
->state
, BTS_STATE_ACTIVE
);
252 intel_pmu_enable_bts(config
);
256 static void bts_event_start(struct perf_event
*event
, int flags
)
258 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
259 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
260 struct bts_buffer
*buf
;
262 buf
= perf_aux_output_begin(&bts
->handle
, event
);
266 if (bts_buffer_reset(buf
, &bts
->handle
))
269 bts
->ds_back
.bts_buffer_base
= cpuc
->ds
->bts_buffer_base
;
270 bts
->ds_back
.bts_absolute_maximum
= cpuc
->ds
->bts_absolute_maximum
;
271 bts
->ds_back
.bts_interrupt_threshold
= cpuc
->ds
->bts_interrupt_threshold
;
273 perf_event_itrace_started(event
);
276 __bts_event_start(event
);
281 perf_aux_output_end(&bts
->handle
, 0);
284 event
->hw
.state
= PERF_HES_STOPPED
;
287 static void __bts_event_stop(struct perf_event
*event
, int state
)
289 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
291 /* ACTIVE -> INACTIVE(PMI)/STOPPED(->stop()) */
292 WRITE_ONCE(bts
->state
, state
);
295 * No extra synchronization is mandated by the documentation to have
296 * BTS data stores globally visible.
298 intel_pmu_disable_bts();
301 static void bts_event_stop(struct perf_event
*event
, int flags
)
303 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
304 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
305 struct bts_buffer
*buf
= NULL
;
306 int state
= READ_ONCE(bts
->state
);
308 if (state
== BTS_STATE_ACTIVE
)
309 __bts_event_stop(event
, BTS_STATE_STOPPED
);
311 if (state
!= BTS_STATE_STOPPED
)
312 buf
= perf_get_aux(&bts
->handle
);
314 event
->hw
.state
|= PERF_HES_STOPPED
;
316 if (flags
& PERF_EF_UPDATE
) {
322 local_xchg(&buf
->data_size
,
323 buf
->nr_pages
<< PAGE_SHIFT
);
324 perf_aux_output_end(&bts
->handle
,
325 local_xchg(&buf
->data_size
, 0));
328 cpuc
->ds
->bts_index
= bts
->ds_back
.bts_buffer_base
;
329 cpuc
->ds
->bts_buffer_base
= bts
->ds_back
.bts_buffer_base
;
330 cpuc
->ds
->bts_absolute_maximum
= bts
->ds_back
.bts_absolute_maximum
;
331 cpuc
->ds
->bts_interrupt_threshold
= bts
->ds_back
.bts_interrupt_threshold
;
335 void intel_bts_enable_local(void)
337 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
338 int state
= READ_ONCE(bts
->state
);
341 * Here we transition from INACTIVE to ACTIVE;
342 * if we instead are STOPPED from the interrupt handler,
343 * stay that way. Can't be ACTIVE here though.
345 if (WARN_ON_ONCE(state
== BTS_STATE_ACTIVE
))
348 if (state
== BTS_STATE_STOPPED
)
351 if (bts
->handle
.event
)
352 __bts_event_start(bts
->handle
.event
);
355 void intel_bts_disable_local(void)
357 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
360 * Here we transition from ACTIVE to INACTIVE;
361 * do nothing for STOPPED or INACTIVE.
363 if (READ_ONCE(bts
->state
) != BTS_STATE_ACTIVE
)
366 if (bts
->handle
.event
)
367 __bts_event_stop(bts
->handle
.event
, BTS_STATE_INACTIVE
);
371 bts_buffer_reset(struct bts_buffer
*buf
, struct perf_output_handle
*handle
)
373 unsigned long head
, space
, next_space
, pad
, gap
, skip
, wakeup
;
374 unsigned int next_buf
;
375 struct bts_phys
*phys
, *next_phys
;
381 head
= handle
->head
& ((buf
->nr_pages
<< PAGE_SHIFT
) - 1);
383 phys
= &buf
->buf
[buf
->cur_buf
];
384 space
= phys
->offset
+ phys
->displacement
+ phys
->size
- head
;
386 if (space
> handle
->size
) {
387 space
= handle
->size
;
388 space
-= space
% BTS_RECORD_SIZE
;
390 if (space
<= BTS_SAFETY_MARGIN
) {
391 /* See if next phys buffer has more space */
392 next_buf
= buf
->cur_buf
+ 1;
393 if (next_buf
>= buf
->nr_bufs
)
395 next_phys
= &buf
->buf
[next_buf
];
396 gap
= buf_size(phys
->page
) - phys
->displacement
- phys
->size
+
397 next_phys
->displacement
;
399 if (handle
->size
>= skip
) {
400 next_space
= next_phys
->size
;
401 if (next_space
+ skip
> handle
->size
) {
402 next_space
= handle
->size
- skip
;
403 next_space
-= next_space
% BTS_RECORD_SIZE
;
405 if (next_space
> space
|| !space
) {
407 bts_buffer_pad_out(phys
, head
);
408 ret
= perf_aux_output_skip(handle
, skip
);
411 /* Advance to next phys buffer */
414 head
= phys
->offset
+ phys
->displacement
;
416 * After this, cur_buf and head won't match ds
417 * anymore, so we must not be racing with
420 buf
->cur_buf
= next_buf
;
421 local_set(&buf
->head
, head
);
426 /* Don't go far beyond wakeup watermark */
427 wakeup
= BTS_SAFETY_MARGIN
+ BTS_RECORD_SIZE
+ handle
->wakeup
-
429 if (space
> wakeup
) {
431 space
-= space
% BTS_RECORD_SIZE
;
434 buf
->end
= head
+ space
;
437 * If we have no space, the lost notification would have been sent when
438 * we hit absolute_maximum - see bts_update()
446 int intel_bts_interrupt(void)
448 struct debug_store
*ds
= this_cpu_ptr(&cpu_hw_events
)->ds
;
449 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
450 struct perf_event
*event
= bts
->handle
.event
;
451 struct bts_buffer
*buf
;
453 int err
= -ENOSPC
, handled
= 0;
456 * The only surefire way of knowing if this NMI is ours is by checking
457 * the write ptr against the PMI threshold.
459 if (ds
&& (ds
->bts_index
>= ds
->bts_interrupt_threshold
))
463 * this is wrapped in intel_bts_enable_local/intel_bts_disable_local,
464 * so we can only be INACTIVE or STOPPED
466 if (READ_ONCE(bts
->state
) == BTS_STATE_STOPPED
)
469 buf
= perf_get_aux(&bts
->handle
);
474 * Skip snapshot counters: they don't use the interrupt, but
475 * there's no other way of telling, because the pointer will
481 old_head
= local_read(&buf
->head
);
485 if (old_head
== local_read(&buf
->head
))
488 perf_aux_output_end(&bts
->handle
, local_xchg(&buf
->data_size
, 0));
490 buf
= perf_aux_output_begin(&bts
->handle
, event
);
492 err
= bts_buffer_reset(buf
, &bts
->handle
);
495 WRITE_ONCE(bts
->state
, BTS_STATE_STOPPED
);
499 * BTS_STATE_STOPPED should be visible before
500 * cleared handle::event
503 perf_aux_output_end(&bts
->handle
, 0);
510 static void bts_event_del(struct perf_event
*event
, int mode
)
512 bts_event_stop(event
, PERF_EF_UPDATE
);
515 static int bts_event_add(struct perf_event
*event
, int mode
)
517 struct bts_ctx
*bts
= this_cpu_ptr(&bts_ctx
);
518 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
519 struct hw_perf_event
*hwc
= &event
->hw
;
521 event
->hw
.state
= PERF_HES_STOPPED
;
523 if (test_bit(INTEL_PMC_IDX_FIXED_BTS
, cpuc
->active_mask
))
526 if (bts
->handle
.event
)
529 if (mode
& PERF_EF_START
) {
530 bts_event_start(event
, 0);
531 if (hwc
->state
& PERF_HES_STOPPED
)
538 static void bts_event_destroy(struct perf_event
*event
)
540 x86_release_hardware();
541 x86_del_exclusive(x86_lbr_exclusive_bts
);
544 static int bts_event_init(struct perf_event
*event
)
548 if (event
->attr
.type
!= bts_pmu
.type
)
552 * BTS leaks kernel addresses even when CPL0 tracing is
553 * disabled, so disallow intel_bts driver for unprivileged
554 * users on paranoid systems since it provides trace data
555 * to the user in a zero-copy fashion.
557 * Note that the default paranoia setting permits unprivileged
558 * users to profile the kernel.
560 if (event
->attr
.exclude_kernel
&& perf_paranoid_kernel() &&
561 !capable(CAP_SYS_ADMIN
))
564 if (x86_add_exclusive(x86_lbr_exclusive_bts
))
567 ret
= x86_reserve_hardware();
569 x86_del_exclusive(x86_lbr_exclusive_bts
);
573 event
->destroy
= bts_event_destroy
;
578 static void bts_event_read(struct perf_event
*event
)
582 static __init
int bts_init(void)
584 if (!boot_cpu_has(X86_FEATURE_DTES64
) || !x86_pmu
.bts
)
587 if (boot_cpu_has(X86_FEATURE_PTI
)) {
589 * BTS hardware writes through a virtual memory map we must
590 * either use the kernel physical map, or the user mapping of
593 * However, since this driver supports per-CPU and per-task inherit
594 * we cannot use the user mapping since it will not be availble
595 * if we're not running the owning process.
597 * With PTI we can't use the kernal map either, because its not
598 * there when we run userspace.
600 * For now, disable this driver when using PTI.
605 bts_pmu
.capabilities
= PERF_PMU_CAP_AUX_NO_SG
| PERF_PMU_CAP_ITRACE
|
606 PERF_PMU_CAP_EXCLUSIVE
;
607 bts_pmu
.task_ctx_nr
= perf_sw_context
;
608 bts_pmu
.event_init
= bts_event_init
;
609 bts_pmu
.add
= bts_event_add
;
610 bts_pmu
.del
= bts_event_del
;
611 bts_pmu
.start
= bts_event_start
;
612 bts_pmu
.stop
= bts_event_stop
;
613 bts_pmu
.read
= bts_event_read
;
614 bts_pmu
.setup_aux
= bts_buffer_setup_aux
;
615 bts_pmu
.free_aux
= bts_buffer_free_aux
;
617 return perf_pmu_register(&bts_pmu
, "intel_bts", -1);
619 arch_initcall(bts_init
);