Skip to content

Commit cf5f5ce

Browse files
yonghong-songdavem330
authored andcommitted
bpf: add support for sys_enter_* and sys_exit_* tracepoints
Currently, bpf programs cannot be attached to sys_enter_* and sys_exit_* style tracepoints. The iovisor/bcc issue torvalds#748 (iovisor/bcc#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 The legality of bpf program ctx access is also checked. Function trace_event_get_offsets returns correct max offset for each specific syscall tracepoint, which is compared against the maximum offset access in bpf program. Signed-off-by: Yonghong Song <yhs@fb.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d226a2b commit cf5f5ce

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

include/linux/syscalls.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,20 @@ extern struct trace_event_functions exit_syscall_print_funcs;
172172
static struct syscall_metadata __used \
173173
__attribute__((section("__syscalls_metadata"))) \
174174
*__p_syscall_meta_##sname = &__syscall_meta_##sname;
175+
176+
static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
177+
{
178+
return tp_event->class == &event_class_syscall_enter ||
179+
tp_event->class == &event_class_syscall_exit;
180+
}
181+
175182
#else
176183
#define SYSCALL_METADATA(sname, nb, ...)
184+
185+
static inline int is_syscall_trace_event(struct trace_event_call *tp_event)
186+
{
187+
return 0;
188+
}
177189
#endif
178190

179191
#define SYSCALL_DEFINE0(sname) \

kernel/events/core.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8050,7 +8050,7 @@ static void perf_event_free_bpf_handler(struct perf_event *event)
80508050

80518051
static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
80528052
{
8053-
bool is_kprobe, is_tracepoint;
8053+
bool is_kprobe, is_tracepoint, is_syscall_tp;
80548054
struct bpf_prog *prog;
80558055

80568056
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)
80618061

80628062
is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
80638063
is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
8064-
if (!is_kprobe && !is_tracepoint)
8064+
is_syscall_tp = is_syscall_trace_event(event->tp_event);
8065+
if (!is_kprobe && !is_tracepoint && !is_syscall_tp)
80658066
/* bpf programs can only be attached to u/kprobe or tracepoint */
80668067
return -EINVAL;
80678068

@@ -8070,13 +8071,14 @@ static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
80708071
return PTR_ERR(prog);
80718072

80728073
if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
8073-
(is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
8074+
(is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
8075+
(is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
80748076
/* valid fd, but invalid bpf program type */
80758077
bpf_prog_put(prog);
80768078
return -EINVAL;
80778079
}
80788080

8079-
if (is_tracepoint) {
8081+
if (is_tracepoint || is_syscall_tp) {
80808082
int off = trace_event_get_offsets(event->tp_event);
80818083

80828084
if (prog->aux->max_ctx_offset > off) {

kernel/trace/trace_syscalls.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,29 @@ static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
559559
static int sys_perf_refcount_enter;
560560
static int sys_perf_refcount_exit;
561561

562+
static int perf_call_bpf_enter(struct bpf_prog *prog, struct pt_regs *regs,
563+
struct syscall_metadata *sys_data,
564+
struct syscall_trace_enter *rec) {
565+
struct syscall_tp_t {
566+
unsigned long long regs;
567+
unsigned long syscall_nr;
568+
unsigned long args[sys_data->nb_args];
569+
} param;
570+
int i;
571+
572+
*(struct pt_regs **)&param = regs;
573+
param.syscall_nr = rec->nr;
574+
for (i = 0; i < sys_data->nb_args; i++)
575+
param.args[i] = rec->args[i];
576+
return trace_call_bpf(prog, &param);
577+
}
578+
562579
static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
563580
{
564581
struct syscall_metadata *sys_data;
565582
struct syscall_trace_enter *rec;
566583
struct hlist_head *head;
584+
struct bpf_prog *prog;
567585
int syscall_nr;
568586
int rctx;
569587
int size;
@@ -578,8 +596,9 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
578596
if (!sys_data)
579597
return;
580598

599+
prog = READ_ONCE(sys_data->enter_event->prog);
581600
head = this_cpu_ptr(sys_data->enter_event->perf_events);
582-
if (hlist_empty(head))
601+
if (!prog && hlist_empty(head))
583602
return;
584603

585604
/* get the size after alignment with the u32 buffer size field */
@@ -594,6 +613,13 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
594613
rec->nr = syscall_nr;
595614
syscall_get_arguments(current, regs, 0, sys_data->nb_args,
596615
(unsigned long *)&rec->args);
616+
617+
if ((prog && !perf_call_bpf_enter(prog, regs, sys_data, rec)) ||
618+
hlist_empty(head)) {
619+
perf_swevent_put_recursion_context(rctx);
620+
return;
621+
}
622+
597623
perf_trace_buf_submit(rec, size, rctx,
598624
sys_data->enter_event->event.type, 1, regs,
599625
head, NULL);
@@ -633,11 +659,26 @@ static void perf_sysenter_disable(struct trace_event_call *call)
633659
mutex_unlock(&syscall_trace_lock);
634660
}
635661

662+
static int perf_call_bpf_exit(struct bpf_prog *prog, struct pt_regs *regs,
663+
struct syscall_trace_exit *rec) {
664+
struct syscall_tp_t {
665+
unsigned long long regs;
666+
unsigned long syscall_nr;
667+
unsigned long ret;
668+
} param;
669+
670+
*(struct pt_regs **)&param = regs;
671+
param.syscall_nr = rec->nr;
672+
param.ret = rec->ret;
673+
return trace_call_bpf(prog, &param);
674+
}
675+
636676
static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
637677
{
638678
struct syscall_metadata *sys_data;
639679
struct syscall_trace_exit *rec;
640680
struct hlist_head *head;
681+
struct bpf_prog *prog;
641682
int syscall_nr;
642683
int rctx;
643684
int size;
@@ -652,8 +693,9 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
652693
if (!sys_data)
653694
return;
654695

696+
prog = READ_ONCE(sys_data->exit_event->prog);
655697
head = this_cpu_ptr(sys_data->exit_event->perf_events);
656-
if (hlist_empty(head))
698+
if (!prog && hlist_empty(head))
657699
return;
658700

659701
/* We can probably do that at build time */
@@ -666,6 +708,13 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
666708

667709
rec->nr = syscall_nr;
668710
rec->ret = syscall_get_return_value(current, regs);
711+
712+
if ((prog && !perf_call_bpf_exit(prog, regs, rec)) ||
713+
hlist_empty(head)) {
714+
perf_swevent_put_recursion_context(rctx);
715+
return;
716+
}
717+
669718
perf_trace_buf_submit(rec, size, rctx, sys_data->exit_event->event.type,
670719
1, regs, head, NULL);
671720
}

0 commit comments

Comments
 (0)