[PATCH 13/13] selftest/bpf: Add bpf_cookie test for raw_k[ret]probe

From: Jiri Olsa
Date: Tue Jan 04 2022 - 03:11:32 EST


Adding bpf_cookie test for raw_k[ret]probes.

Using the bpf_link_create interface directly and single
trampoline program to trigger the bpf_fentry_test1
execution.

Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
.../selftests/bpf/prog_tests/bpf_cookie.c | 42 +++++++++++++++++++
.../selftests/bpf/progs/test_bpf_cookie.c | 24 ++++++++++-
2 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
index 5eea3c3a40fe..aee01dd3a823 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
@@ -57,6 +57,46 @@ static void kprobe_subtest(struct test_bpf_cookie *skel)
bpf_link__destroy(retlink2);
}

+static void rawkprobe_subtest(struct test_bpf_cookie *skel)
+{
+ DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
+ int err, prog_fd, link1_fd = -1, link2_fd = -1;
+ __u32 duration = 0, retval;
+ __u64 addr;
+
+ kallsyms_find("bpf_fentry_test1", &addr);
+
+ opts.kprobe.addrs = (__u64) &addr;
+ opts.kprobe.cnt = 1;
+ opts.kprobe.bpf_cookie = 0x1;
+ prog_fd = bpf_program__fd(skel->progs.handle_raw_kprobe);
+
+ link1_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_RAW_KPROBE, &opts);
+ if (!ASSERT_GE(link1_fd, 0, "link1_fd"))
+ return;
+
+ opts.flags = BPF_F_KPROBE_RETURN;
+ opts.kprobe.bpf_cookie = 0x2;
+ prog_fd = bpf_program__fd(skel->progs.handle_raw_kretprobe);
+
+ link2_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_RAW_KPROBE, &opts);
+ if (!ASSERT_GE(link2_fd, 0, "link2_fd"))
+ goto cleanup;
+
+ prog_fd = bpf_program__fd(skel->progs.raw_trigger);
+ err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
+ NULL, NULL, &retval, &duration);
+ ASSERT_OK(err, "test_run");
+ ASSERT_EQ(retval, 0, "test_run");
+
+ ASSERT_EQ(skel->bss->raw_kprobe_res, 0x1, "raw_kprobe_res");
+ ASSERT_EQ(skel->bss->raw_kretprobe_res, 0x2, "raw_kretprobe_res");
+
+cleanup:
+ close(link1_fd);
+ close(link2_fd);
+}
+
static void uprobe_subtest(struct test_bpf_cookie *skel)
{
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
@@ -243,6 +283,8 @@ void test_bpf_cookie(void)

if (test__start_subtest("kprobe"))
kprobe_subtest(skel);
+ if (test__start_subtest("rawkprobe"))
+ rawkprobe_subtest(skel);
if (test__start_subtest("uprobe"))
uprobe_subtest(skel);
if (test__start_subtest("tracepoint"))
diff --git a/tools/testing/selftests/bpf/progs/test_bpf_cookie.c b/tools/testing/selftests/bpf/progs/test_bpf_cookie.c
index 2d3a7710e2ce..409f87464b1f 100644
--- a/tools/testing/selftests/bpf/progs/test_bpf_cookie.c
+++ b/tools/testing/selftests/bpf/progs/test_bpf_cookie.c
@@ -8,8 +8,9 @@
int my_tid;

int kprobe_res;
-int kprobe_multi_res;
int kretprobe_res;
+int raw_kprobe_res;
+int raw_kretprobe_res;
int uprobe_res;
int uretprobe_res;
int tp_res;
@@ -37,6 +38,27 @@ int handle_kretprobe(struct pt_regs *ctx)
return 0;
}

+SEC("kprobe/bpf_fentry_test1")
+int handle_raw_kprobe(struct pt_regs *ctx)
+{
+ update(ctx, &raw_kprobe_res);
+ return 0;
+}
+
+SEC("kretprobe/bpf_fentry_test1")
+int handle_raw_kretprobe(struct pt_regs *ctx)
+{
+ update(ctx, &raw_kretprobe_res);
+ return 0;
+}
+
+/* just to trigger bpf_fentry_test1 through tracing test_run */
+SEC("fentry/bpf_modify_return_test")
+int BPF_PROG(raw_trigger)
+{
+ return 0;
+}
+
SEC("uprobe/trigger_func")
int handle_uprobe(struct pt_regs *ctx)
{
--
2.33.1