bpf: add support for sys_{enter|exit}_* tracepoints
Subsystems (6)
v3 cover bpf: add support for sys_{enter|exit}_* tracepoints
Currently, bpf programs cannot be attached to sys_enter_* and sys_exit_* style tracepoints. The main reason is that syscalls/sys_enter_* and syscalls/sys_exit_* tracepoints are treated differently from other tracepoints and there
is no bpf hook to it.
This patch set adds bpf support for these syscalls tracepoints and also adds a test case for it.
Changes from v2:
- Fix a build issue
Changes from v1:
- Do not use TRACE_EVENT_FL_CAP_ANY to identify syscall tracepoint. Instead use trace_event_call->class.
Yonghong Song (2):
bpf: add support for sys_enter_* and sys_exit_* tracepoints
bpf: add a test case for syscalls/sys_{enter|exit}_* tracepoints
include/linux/syscalls.h | 12 ++++++++ kernel/events/core.c | 8 +++-- kernel/trace/trace_syscalls.c | 53 ++++++++++++++++++++++++++++++-- samples/bpf/Makefile | 4 +++ samples/bpf/syscall_tp_kern.c | 62 +++++++++++++++++++++++++++++++++++++ samples/bpf/syscall_tp_user.c | 71 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 samples/bpf/syscall_tp_kern.c create mode 100644 samples/bpf/syscall_tp_user.c
Signature · 1 line
2.9.4
PATCH v3 2/2 bpf: add a test case for syscalls/sys_{enter|exit}_* tracepoints
--- samples/bpf/Makefile | 4 +++ samples/bpf/syscall_tp_kern.c | 62 +++++++++++++++++++++++++++++++++++++ samples/bpf/syscall_tp_user.c | 71 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 samples/bpf/syscall_tp_kern.c create mode 100644 samples/bpf/syscall_tp_user.c
samples/bpf/Makefile +4 -0
@@ -39,6 +39,7 @@ hostprogs-y += per_socket_stats_example hostprogs-y += load_sock_ops hostprogs-y += xdp_redirect hostprogs-y += xdp_redirect_map +hostprogs-y += syscall_tp # Libbpf dependencies LIBBPF := ../../tools/lib/bpf/bpf.o
@@ -82,6 +83,7 @@ test_map_in_map-objs := bpf_load.o $(LIBBPF) test_map_in_map_user.o per_socket_stats_example-objs := $(LIBBPF) cookie_uid_helper_example.o xdp_redirect-objs := bpf_load.o $(LIBBPF) xdp_redirect_user.o xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o +syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o # Tell kbuild to always build the programs always := $(hostprogs-y)
@@ -125,6 +127,7 @@ always += tcp_iw_kern.o always += tcp_clamp_kern.o always += xdp_redirect_kern.o always += xdp_redirect_map_kern.o +always += syscall_tp_kern.o HOSTCFLAGS += -I$(objtree)/usr/include HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -163,6 +166,7 @@ HOSTLOADLIBES_xdp_tx_iptunnel += -lelf HOSTLOADLIBES_test_map_in_map += -lelf HOSTLOADLIBES_xdp_redirect += -lelf HOSTLOADLIBES_xdp_redirect_map += -lelf +HOSTLOADLIBES_syscall_tp += -lelf # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: # make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
samples/bpf/syscall_tp_kern.c new file +62 -0
62 lines not on this page · raw message
samples/bpf/syscall_tp_user.c new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.9.4
PATCH v3 1/2 bpf: add support for sys_enter_* and sys_exit_* tracepoints
In reply to Yonghong SongCurrently, bpf programs cannot be attached to sys_enter_* and sys_exit_* style tracepoints. The iovisor/bcc issue #748
(https://github.com/iovisor/bcc/issues/748) documents this issue. For example, if you try to attach a bpf program to tracepoints syscalls/sys_enter_newfstat, you will get the following error:
# ./tools/trace.py t:syscalls:sys_enter_newfstat Ioctl(PERF_EVENT_IOC_SET_BPF): Invalid argument Failed to attach BPF to tracepoint
The main reason is that syscalls/sys_enter_* and syscalls/sys_exit_* tracepoints are treated differently from other tracepoints and there is no bpf hook to it.
This patch adds bpf support for these syscalls tracepoints by
. permitting bpf attachment in ioctl PERF_EVENT_IOC_SET_BPF . calling bpf programs in perf_syscall_enter and perf_syscall_exit
--- include/linux/syscalls.h | 12 ++++++++++ kernel/events/core.c | 8 ++++--- kernel/trace/trace_syscalls.c | 53 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 5 deletions(-)
include/linux/syscalls.h +12 -0
@@ -172,8 +172,20 @@ extern struct trace_event_functions exit_syscall_print_funcs; static struct syscall_metadata __used \ __attribute__((section("__syscalls_metadata"))) \ *__p_syscall_meta_##sname = &__syscall_meta_##sname; + +static inline int is_syscall_trace_event (struct trace_event_call *tp_event) +{ +return tp_event->class == &event_class_syscall_enter || + tp_event->class == &event_class_syscall_exit; +} +#else #define SYSCALL_METADATA (sname, nb, ...) + +static inline int is_syscall_trace_event (struct trace_event_call *tp_event) +{ +return 0 ; +}#endif #define SYSCALL_DEFINE0 (sname) \
kernel/events/core.c +5 -3
@@ -8050,7 +8050,7 @@ static void perf_event_free_bpf_handler(struct perf_event *event)static int perf_event_set_bpf_prog (struct perf_event *event,u32 prog_fd) { -bool is_kprobe, is_tracepoint; +bool is_kprobe, is_tracepoint, is_syscall_tp;struct bpf_prog *prog;if (event->attr.type !=PERF_TYPE_TRACEPOINT )
@@ -8061,7 +8061,8 @@ static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) is_kprobe = event->tp_event->flags &TRACE_EVENT_FL_UKPROBE ; is_tracepoint = event->tp_event->flags &TRACE_EVENT_FL_TRACEPOINT ; -if (!is_kprobe && !is_tracepoint) + is_syscall_tp =is_syscall_trace_event (event->tp_event); +if (!is_kprobe && !is_tracepoint && !is_syscall_tp)/* bpf programs can only be attached to u/kprobe or tracepoint */ return -EINVAL ;
@@ -8070,7 +8071,8 @@ static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)return PTR_ERR (prog);if ((is_kprobe && prog->type !=BPF_PROG_TYPE_KPROBE ) || - (is_tracepoint && prog->type !=BPF_PROG_TYPE_TRACEPOINT )) { + (is_tracepoint && prog->type !=BPF_PROG_TYPE_TRACEPOINT ) || + (is_syscall_tp && prog->type !=BPF_PROG_TYPE_TRACEPOINT )) {/* valid fd, but invalid bpf program type */ bpf_prog_put (prog);return -EINVAL ;
kernel/trace/trace_syscalls.c +51 -2
101 lines not on this page · raw message
Signature · 1 line
2.9.4
Re: [PATCH net-next v3 2/2] bpf: add a test case for syscalls/sys_{enter|exit}_* tracepoints
In reply to Yonghong SongQuoted 1 line · Yonghong Song
Signed-off-by Yonghong Song <[email protected]>
Re: [PATCH net-next v3 1/2] bpf: add support for sys_enter_* and sys_exit_* tracepoints
In reply to Yonghong SongQuoted 12 lines · Yonghong Song
kernel/trace/trace_syscalls.c +2
-1@@ -578,8 +596,9 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)if (!sys_data)return ; + prog =READ_ONCE (sys_data->enter_event->prog); head =this_cpu_ptr (sys_data->enter_event->perf_events); -if (hlist_empty (head)) +if (!prog &&hlist_empty (head))return ;/* get the size after alignment with the u32 buffer size field */
kernel/trace/trace_syscalls.c static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) · line 621
Quoted 9 lines
kernel/trace/trace_syscalls.c +6
-0@@ -594,6 +613,13 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) rec->nr = syscall_nr;syscall_get_arguments (current, regs,0 , sys_data->nb_args, (unsigned long *)&rec->args); + +if ((prog && !perf_call_bpf_enter (prog, regs, sys_data, rec)) || +hlist_empty (head)) { +perf_swevent_put_recursion_context (rctx); +return ; + }
hmm. if I read the patch correctly that makes it different from kprobe/uprobe/tracepoints+bpf behavior. Why make it different and force user space to perf_event_open() on every cpu?
In other cases it's the job of the bpf program to filter by cpu if necessary and that is well understood by bcc scripts.
Re: [PATCH net-next v3 1/2] bpf: add support for sys_enter_* and sys_exit_* tracepoints
Quoted 24 lines · Alexei Starovoitov
Quoted 23 lines · Yonghong Song
kernel/trace/trace_syscalls.c +2
-1@@ -578,8 +596,9 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)if (!sys_data)return ; + prog =READ_ONCE (sys_data->enter_event->prog); head =this_cpu_ptr (sys_data->enter_event->perf_events); -if (hlist_empty (head)) +if (!prog &&hlist_empty (head))return ;/* get the size after alignment with the u32 buffer size field */kernel/trace/trace_syscalls.c +5
-0@@ -594,6 +613,13 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) rec->nr = syscall_nr;syscall_get_arguments (current, regs,0 , sys_data->nb_args, (unsigned long *)&rec->args); + +if ((prog && !perf_call_bpf_enter (prog, regs, sys_data, rec)) || +hlist_empty (head)) { +perf_swevent_put_recursion_context (rctx); +return ;
Quoted 8 lines
Review static void perf_syscall_enter(void *ignore, struct · line 622
Quoted 1 line
(quoted diff) +1
-0+ }hmm. if I read the patch correctly that makes it different from kprobe/uprobe/tracepoints+bpf behavior. Why make it different and force user space to
perf_event_open()on every cpu?
In other cases it's the job of the bpf program to filter by cpu if necessary and that is well understood by bcc scripts.
The patch actually does allow the bpf program to track all cpus. The test:
Quoted 2 lines
Review static void perf_syscall_enter(void *ignore, struct · line 624
Quoted 2 lines
(quoted diff) +1
-0+ if (!prog && hlist_empty(head)) return;
ensures that if prog is not empty, it will not return even if the event in the current cpu is empty. Later on, perf_call_bpf_enter will be called if prog is not empty. This ensures that
the bpf program will execute regardless of the current cpu.
Maybe I missed anything here?
Re: [PATCH net-next v3 1/2] bpf: add support for sys_enter_* and sys_exit_* tracepoints
Quoted 36 lines · Y Song
Quoted 24 lines · Alexei Starovoitov
Quoted 23 lines · Yonghong Song
kernel/trace/trace_syscalls.c +2
-1@@ -578,8 +596,9 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)if (!sys_data)return ; + prog =READ_ONCE (sys_data->enter_event->prog); head =this_cpu_ptr (sys_data->enter_event->perf_events); -if (hlist_empty (head)) +if (!prog &&hlist_empty (head))return ;/* get the size after alignment with the u32 buffer size field */kernel/trace/trace_syscalls.c +5
-0@@ -594,6 +613,13 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) rec->nr = syscall_nr;syscall_get_arguments (current, regs,0 , sys_data->nb_args, (unsigned long *)&rec->args); + +if ((prog && !perf_call_bpf_enter (prog, regs, sys_data, rec)) || +hlist_empty (head)) { +perf_swevent_put_recursion_context (rctx); +return ;Quoted 8 lines
Review static void perf_syscall_enter(void *ignore, struct · line 622
Quoted 1 line
(quoted diff) +1
-0+ }hmm. if I read the patch correctly that makes it different from kprobe/uprobe/tracepoints+bpf behavior. Why make it different and force user space to
perf_event_open()on every cpu?
In other cases it's the job of the bpf program to filter by cpu if necessary and that is well understood by bcc scripts.The patch actually does allow the bpf program to track all cpus. The test:
Quoted 8 lines
Quoted 2 lines
Review static void perf_syscall_enter(void *ignore, struct · line 624
Quoted 2 lines
(quoted diff) +1
-0+ if (!prog && hlist_empty(head)) return;ensures that if prog is not empty, it will not return even if the event in the current cpu is empty. Later on,
perf_call_bpf_enterwill be called if prog is not empty. This ensures that
the bpf program will execute regardless of the current cpu.Maybe I missed anything here?
you're right. sorry. misread && for ||.
That part looks good indeed.
Another question...
that part:
if (is_tracepoint) {
int off = trace_event_get_offsets(event->tp_event);
if (prog->aux->max_ctx_offset > off) {seems to be not used in this new path...
or new is_syscall_tp is also is_tracepoint ?
If so, then it's ok...
and trace_event_get_offsets() returns the actual number of syscall args or always upper bound of 6?
just curious how this new code checks that bpf prog cannot access args[6+].
Thanks!
Re: [PATCH net-next v3 1/2] bpf: add support for sys_enter_* and sys_exit_* tracepoints
Quoted 52 lines · Alexei Starovoitov
Quoted 39 lines · Y Song
Quoted 27 lines · Alexei Starovoitov
Quoted 26 lines · Yonghong Song
kernel/trace/trace_syscalls.c +2
-1@@ -578,8 +596,9 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)if (!sys_data)return ; + prog =READ_ONCE (sys_data->enter_event->prog); head =this_cpu_ptr (sys_data->enter_event->perf_events); -if (hlist_empty (head)) +if (!prog &&hlist_empty (head))return ;/* get the size after alignment with the u32 buffer sizefield */
(quoted diff) +0
-0@@ -594,6 +613,13 @@ static void perf_syscall_enter(void *ignore,struct
pt_regs*regs, long id)rec->nr = syscall_nr; syscall_get_arguments(current, regs, 0, sys_data->nb_args, (unsigned long *)&rec->args);(quoted diff) +2
-0+ + if ((prog && !perf_call_bpf_enter(prog, regs, sys_data,rec)) ||
kernel/trace/trace_syscalls.c +3
-0+ hlist_empty(head)) { +perf_swevent_put_recursion_context (rctx); +return ;Quoted 8 lines
Quoted 1 line
+ }
hmm. if I read the patch correctly that makes it different from kprobe/uprobe/tracepoints+bpf behavior. Why make it different and force user space to
perf_event_open()on every cpu?
In other cases it's the job of the bpf program to filter by cpu if necessary and that is well understood by bcc scripts.The patch actually does allow the bpf program to track all cpus. The test:
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
+ if (!prog &&
hlist_empty(head))return;ensures that if prog is not empty, it will not return even if the event in the current cpu is empty. Later on,
perf_call_bpf_enterwill be called if prog is not empty. This ensures that
the bpf program will execute regardless of the current cpu.Maybe I missed anything here?
you're right. sorry. misread && for ||.
That part looks good indeed.
Quoted 8 lines
Another question...
that part:if (is_tracepoint) { int off = trace_event_get_offsets(event->tp_event); if (prog->aux->max_ctx_offset > off) {seems to be not used in this new path...
or newis_syscall_tpis alsois_tracepoint?
Good catch! I think I need "is_tracepoint || is_syscall_tp" here. If trace_event_get_offsets can get the correct offset for the current particular syscall_{enter|exit}_* event, we will be find. I will double check this and have another patch.
Quoted 3 lines
If so, then it's ok...
andtrace_event_get_offsets()returns the actual number of syscall args or always upper bound of 6?
Since the specific event is fed here, I think the actual number will be returned.
Quoted 5 lines
just curious how this new code checks that bpf prog cannot access args[6+].
Thanks!