On Tue, Mar 15, 2022 at 5:44 PM Kui-Feng Lee <kuifeng@xxxxxx> wrote: > > Make sure BPF cookies are correct for fentry/fexit/fmod_ret. > > Signed-off-by: Kui-Feng Lee <kuifeng@xxxxxx> > --- > .../selftests/bpf/prog_tests/bpf_cookie.c | 61 +++++++++++++++++++ > .../selftests/bpf/progs/test_bpf_cookie.c | 24 ++++++++ > 2 files changed, 85 insertions(+) > > diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c > index 0612e79a9281..6d06c5046e9c 100644 > --- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c > +++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c > @@ -237,6 +237,65 @@ static void pe_subtest(struct test_bpf_cookie *skel) > bpf_link__destroy(link); > } > > +static void tracing_subtest(struct test_bpf_cookie *skel) > +{ > + __u64 cookie; > + int prog_fd; > + int fentry_fd = -1, fexit_fd = -1, fmod_ret_fd = -1; > + struct bpf_test_run_opts opts; > + > + skel->bss->fentry_res = 0; > + skel->bss->fexit_res = 0; > + > + cookie = 0x100000; > + prog_fd = bpf_program__fd(skel->progs.fentry_test1); > + if (!ASSERT_GE(prog_fd, 0, "fentry.prog_fd")) > + return; this can't return <= 0 if skeleton was loaded, don't bother doing these checks, they just distract from the main objective of the test > + fentry_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie); > + if (!ASSERT_GE(fentry_fd, 0, "fentry.open")) > + return; > + > + cookie = 0x200000; > + prog_fd = bpf_program__fd(skel->progs.fexit_test1); > + if (!ASSERT_GE(prog_fd, 0, "fexit.prog_fd")) > + goto cleanup; > + fexit_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie); > + if (!ASSERT_GE(fexit_fd, 0, "fexit.open")) > + goto cleanup; > + > + cookie = 0x300000; > + prog_fd = bpf_program__fd(skel->progs.fmod_ret_test); > + if (!ASSERT_GE(prog_fd, 0, "fmod_ret.prog_fd")) > + goto cleanup; > + fmod_ret_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie); > + if (!ASSERT_GE(fmod_ret_fd, 0, "fmod_ret.opoen")) > + goto cleanup; > + > + bzero(&opts, sizeof(opts)); > + opts.sz = sizeof(opts); > + opts.repeat = 1; We have LIBBPF_OPTS() macro for working with opts, please use that. But I think in this case NULL for opts will be equivalent. Seems like kernel just ignores repeat parameter for tracing programs (and for others repeat == 0 means repeat == 1). > + prog_fd = bpf_program__fd(skel->progs.fentry_test1); > + bpf_prog_test_run_opts(prog_fd, &opts); > + > + bzero(&opts, sizeof(opts)); > + opts.sz = sizeof(opts); > + opts.repeat = 1; > + prog_fd = bpf_program__fd(skel->progs.fmod_ret_test); > + bpf_prog_test_run_opts(prog_fd, &opts); > + > + ASSERT_EQ(skel->bss->fentry_res, 0x100000, "fentry_res"); > + ASSERT_EQ(skel->bss->fexit_res, 0x200000, "fexit_res"); > + ASSERT_EQ(skel->bss->fmod_ret_res, 0x300000, "fmod_ret_res"); > + > +cleanup: > + if (fentry_fd >= 0) > + close(fentry_fd); > + if (fexit_fd >= 0) > + close(fexit_fd); > + if (fmod_ret_fd >= 0) > + close(fmod_ret_fd); > +} > + > void test_bpf_cookie(void) > { > struct test_bpf_cookie *skel; > @@ -255,6 +314,8 @@ void test_bpf_cookie(void) > tp_subtest(skel); > if (test__start_subtest("perf_event")) > pe_subtest(skel); > + if (test__start_subtest("tracing")) > + tracing_subtest(skel); kprobes are also tracing, but this one is specifically about BPF trampoline-based programs. Let's use "trampoline" here to disambiguate. > > test_bpf_cookie__destroy(skel); > } > diff --git a/tools/testing/selftests/bpf/progs/test_bpf_cookie.c b/tools/testing/selftests/bpf/progs/test_bpf_cookie.c > index 2d3a7710e2ce..a9f83f46e7b7 100644 > --- a/tools/testing/selftests/bpf/progs/test_bpf_cookie.c > +++ b/tools/testing/selftests/bpf/progs/test_bpf_cookie.c > @@ -14,6 +14,9 @@ int uprobe_res; > int uretprobe_res; > int tp_res; > int pe_res; > +int fentry_res; > +int fexit_res; > +int fmod_ret_res; > > static void update(void *ctx, int *res) > { > @@ -82,4 +85,25 @@ int handle_pe(struct pt_regs *ctx) > return 0; > } > > +SEC("fentry/bpf_fentry_test1") > +int BPF_PROG(fentry_test1, int a) > +{ > + update(ctx, &fentry_res); > + return 0; > +} > + > +SEC("fexit/bpf_fentry_test1") > +int BPF_PROG(fexit_test1, int a, int ret) > +{ > + update(ctx, &fexit_res); > + return 0; > +} > + > +SEC("fmod_ret/bpf_modify_return_test") > +int BPF_PROG(fmod_ret_test, int _a, int *_b, int _ret) > +{ > + update(ctx, &fmod_ret_res); > + return 1234; > +} > + > char _license[] SEC("license") = "GPL"; > -- > 2.30.2 >