Skip to content

Commit 396f544

Browse files
anakryikoAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Fix BPF_KRETPROBE macro and use it in attach_probe test
For kretprobes, there is no point in capturing input arguments from pt_regs, as they are going to be, most probably, clobbered by the time probed kernel function returns. So switch BPF_KRETPROBE to accept zero or one argument (optional return result). Fixes: ac06587 ("selftests/bpf: Add BPF_PROG, BPF_KPROBE, and BPF_KRETPROBE macros") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent fd56e00 commit 396f544

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

tools/testing/selftests/bpf/bpf_trace_helpers.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ typeof(name(0)) name(struct pt_regs *ctx) \
9696
static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
9797

9898
#define ___bpf_kretprobe_args0() ctx
99-
#define ___bpf_kretprobe_argsN(x, args...) \
100-
___bpf_kprobe_args(args), (void *)PT_REGS_RET(ctx)
99+
#define ___bpf_kretprobe_args1(x) \
100+
___bpf_kretprobe_args0(), (void *)PT_REGS_RET(ctx)
101101
#define ___bpf_kretprobe_args(args...) \
102-
___bpf_apply(___bpf_kretprobe_args, ___bpf_empty(args))(args)
102+
___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args)
103103

104104
/*
105-
* BPF_KRETPROBE is similar to BPF_KPROBE, except, in addition to listing all
106-
* input kprobe arguments, one last extra argument has to be specified, which
107-
* captures kprobe return value.
105+
* BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional
106+
* return value (in addition to `struct pt_regs *ctx`), but no input
107+
* arguments, because they will be clobbered by the time probed function
108+
* returns.
108109
*/
109110
#define BPF_KRETPROBE(name, args...) \
110111
name(struct pt_regs *ctx); \

tools/testing/selftests/bpf/progs/test_attach_probe.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/ptrace.h>
55
#include <linux/bpf.h>
66
#include <bpf/bpf_helpers.h>
7+
#include "bpf_trace_helpers.h"
78

89
int kprobe_res = 0;
910
int kretprobe_res = 0;
@@ -18,7 +19,7 @@ int handle_kprobe(struct pt_regs *ctx)
1819
}
1920

2021
SEC("kretprobe/sys_nanosleep")
21-
int handle_kretprobe(struct pt_regs *ctx)
22+
int BPF_KRETPROBE(handle_kretprobe)
2223
{
2324
kretprobe_res = 2;
2425
return 0;

tools/testing/selftests/bpf/progs/test_overhead.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ int BPF_KPROBE(prog1, struct task_struct *tsk, const char *buf, bool exec)
1717
}
1818

1919
SEC("kretprobe/__set_task_comm")
20-
int BPF_KRETPROBE(prog2,
21-
struct task_struct *tsk, const char *buf, bool exec,
22-
int ret)
20+
int BPF_KRETPROBE(prog2, int ret)
2321
{
24-
return !PT_REGS_PARM1(ctx) && ret;
22+
return ret;
2523
}
2624

2725
SEC("raw_tp/task_rename")

0 commit comments

Comments
 (0)