Skip to content

Commit 98b5c2c

Browse files
4astdavem330
authored andcommitted
perf, bpf: allow bpf programs attach to tracepoints
introduce BPF_PROG_TYPE_TRACEPOINT program type and allow it to be attached to the perf tracepoint handler, which will copy the arguments into the per-cpu buffer and pass it to the bpf program as its first argument. The layout of the fields can be discovered by doing 'cat /sys/kernel/debug/tracing/events/sched/sched_switch/format' prior to the compilation of the program with exception that first 8 bytes are reserved and not accessible to the program. This area is used to store the pointer to 'struct pt_regs' which some of the bpf helpers will use: +---------+ | 8 bytes | hidden 'struct pt_regs *' (inaccessible to bpf program) +---------+ | N bytes | static tracepoint fields defined in tracepoint/format (bpf readonly) +---------+ | dynamic | __dynamic_array bytes of tracepoint (inaccessible to bpf yet) +---------+ Not that all of the fields are already dumped to user space via perf ring buffer and broken application access it directly without consulting tracepoint/format. Same rule applies here: static tracepoint fields should only be accessed in a format defined in tracepoint/format. The order of fields and field sizes are not an ABI. Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1e1dcd9 commit 98b5c2c

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Diff for: include/trace/perf.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ perf_trace_##call(void *__data, proto) \
3434
struct trace_event_call *event_call = __data; \
3535
struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
3636
struct trace_event_raw_##call *entry; \
37+
struct bpf_prog *prog = event_call->prog; \
3738
struct pt_regs *__regs; \
3839
u64 __count = 1; \
3940
struct task_struct *__task = NULL; \
@@ -45,7 +46,7 @@ perf_trace_##call(void *__data, proto) \
4546
__data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
4647
\
4748
head = this_cpu_ptr(event_call->perf_events); \
48-
if (__builtin_constant_p(!__task) && !__task && \
49+
if (!prog && __builtin_constant_p(!__task) && !__task && \
4950
hlist_empty(head)) \
5051
return; \
5152
\
@@ -63,6 +64,13 @@ perf_trace_##call(void *__data, proto) \
6364
\
6465
{ assign; } \
6566
\
67+
if (prog) { \
68+
*(struct pt_regs **)entry = __regs; \
69+
if (!trace_call_bpf(prog, entry) || hlist_empty(head)) { \
70+
perf_swevent_put_recursion_context(rctx); \
71+
return; \
72+
} \
73+
} \
6674
perf_trace_buf_submit(entry, __entry_size, rctx, \
6775
event_call->event.type, __count, __regs, \
6876
head, __task); \

Diff for: include/uapi/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ enum bpf_prog_type {
9292
BPF_PROG_TYPE_KPROBE,
9393
BPF_PROG_TYPE_SCHED_CLS,
9494
BPF_PROG_TYPE_SCHED_ACT,
95+
BPF_PROG_TYPE_TRACEPOINT,
9596
};
9697

9798
#define BPF_PSEUDO_MAP_FD 1

Diff for: kernel/events/core.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -6725,12 +6725,13 @@ int perf_swevent_get_recursion_context(void)
67256725
}
67266726
EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
67276727

6728-
inline void perf_swevent_put_recursion_context(int rctx)
6728+
void perf_swevent_put_recursion_context(int rctx)
67296729
{
67306730
struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
67316731

67326732
put_recursion_context(swhash->recursion, rctx);
67336733
}
6734+
EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
67346735

67356736
void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
67366737
{
@@ -7106,6 +7107,7 @@ static void perf_event_free_filter(struct perf_event *event)
71067107

71077108
static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
71087109
{
7110+
bool is_kprobe, is_tracepoint;
71097111
struct bpf_prog *prog;
71107112

71117113
if (event->attr.type != PERF_TYPE_TRACEPOINT)
@@ -7114,15 +7116,18 @@ static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
71147116
if (event->tp_event->prog)
71157117
return -EEXIST;
71167118

7117-
if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
7118-
/* bpf programs can only be attached to u/kprobes */
7119+
is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7120+
is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7121+
if (!is_kprobe && !is_tracepoint)
7122+
/* bpf programs can only be attached to u/kprobe or tracepoint */
71197123
return -EINVAL;
71207124

71217125
prog = bpf_prog_get(prog_fd);
71227126
if (IS_ERR(prog))
71237127
return PTR_ERR(prog);
71247128

7125-
if (prog->type != BPF_PROG_TYPE_KPROBE) {
7129+
if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7130+
(is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
71267131
/* valid fd, but invalid bpf program type */
71277132
bpf_prog_put(prog);
71287133
return -EINVAL;

0 commit comments

Comments
 (0)