Add a bpf_fexit_test_ret_ppfile() to bpf_testmod which takes an argument. The vmlinux function of the same name takes none, so the module has a FUNC of its own for it. Check in module_fentry_shadow that the ID of the vmlinux function is refused with the BTF of the module, both at load and at the creation of a tracing multi link. Check in module_attach that libbpf takes the function of the module for "bpf_testmod:bpf_fexit_test_ret_ppfile", for both fentry and fentry.multi. Only x86_64 has tracing multi, so the multi parts are skipped elsewhere. Split module_fentry_shadow into the subtests shadow and multi for that. Signed-off-by: Anton Protopopov --- .../selftests/bpf/prog_tests/module_attach.c | 11 +++ .../bpf/prog_tests/module_fentry_shadow.c | 96 +++++++++++++++++-- .../selftests/bpf/progs/test_module_attach.c | 16 ++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 10 ++ 4 files changed, 126 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/module_attach.c b/tools/testing/selftests/bpf/prog_tests/module_attach.c index 92c336333fcb..c63d7f282fd8 100644 --- a/tools/testing/selftests/bpf/prog_tests/module_attach.c +++ b/tools/testing/selftests/bpf/prog_tests/module_attach.c @@ -179,6 +179,17 @@ void test_module_attach(void) READ_SZ, "bpf_testmod:bpf_testmod_test_read", 0); } + /* libbpf must pick the function of the module, not the one of vmlinux */ + if (test__start_subtest("handle_fentry_own_func")) + test_module_attach_prog("handle_fentry_own_func", 0, NULL, 0); + if (test__start_subtest("handle_fentry_multi_own_func")) { +#ifdef __x86_64__ + test_module_attach_prog("handle_fentry_multi_own_func", 0, NULL, 0); +#else + printf("%s:SKIP:tracing multi is supported only on x86_64\n", __func__); + test__skip(); +#endif + } if (test__start_subtest("handle_fexit")) test_module_attach_prog("handle_fexit", READ_SZ, NULL, -EIO); if (test__start_subtest("handle_fexit_ret")) diff --git a/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c b/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c index bea05f78de5f..0d44a6a0ace6 100644 --- a/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c +++ b/tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c @@ -8,6 +8,7 @@ static const char *module_name = "bpf_testmod"; static const char *symbol_name = "bpf_fentry_shadow_test"; +static const char *other_name = "bpf_fexit_test_ret_ppfile"; static int get_bpf_testmod_btf_fd(void) { @@ -53,20 +54,16 @@ static int get_bpf_testmod_btf_fd(void) return -ENOENT; } -void test_module_fentry_shadow(void) +static void test_shadow(void) { struct btf *vmlinux_btf = NULL, *mod_btf = NULL; - int err, i; + int err, i, fd; + __s32 id; int btf_fd[2] = {}; int prog_fd[2] = {}; int link_fd[2] = {}; __s32 btf_id[2] = {}; - if (!env.has_testmod) { - test__skip(); - return; - } - LIBBPF_OPTS(bpf_prog_load_opts, load_opts, .expected_attach_type = BPF_TRACE_FENTRY, ); @@ -120,6 +117,25 @@ void test_module_fentry_shadow(void) err = bpf_prog_test_run_opts(prog_fd[0], NULL); ASSERT_OK(err, "running test"); + /* + * The bpf_fexit_test_ret_ppfile() of bpf_testmod has a prototype other + * than the one of vmlinux, so the vmlinux one is refused with the BTF + * of the module + */ + id = btf__find_by_name_kind_own(mod_btf, other_name, BTF_KIND_FUNC); + if (!ASSERT_GT(id, 0, "module func")) + goto out; + id = btf__find_by_name_kind(vmlinux_btf, other_name, BTF_KIND_FUNC); + if (!ASSERT_GT(id, 0, "vmlinux func")) + goto out; + load_opts.attach_btf_id = id; + load_opts.attach_btf_obj_fd = btf_fd[1]; + fd = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL", trace_program, + ARRAY_SIZE(trace_program), &load_opts); + ASSERT_EQ(fd, -EINVAL, "vmlinux func with module btf"); + if (fd >= 0) + close(fd); + out: btf__free(vmlinux_btf); btf__free(mod_btf); @@ -132,3 +148,69 @@ void test_module_fentry_shadow(void) close(link_fd[i]); } } + +/* A multi program gets the ID with its link */ +static void test_multi(void) +{ + LIBBPF_OPTS(bpf_prog_load_opts, load_opts, + .expected_attach_type = BPF_TRACE_FENTRY_MULTI, + ); + LIBBPF_OPTS(bpf_link_create_opts, link_opts, + .tracing_multi.cnt = 1, + ); + const struct bpf_insn trace_program[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + struct btf *vmlinux_btf; + int btf_fd, fd, link_fd; + __u32 ids[1]; + __s32 id; + +#ifndef __x86_64__ + printf("%s:SKIP:tracing multi is supported only on x86_64\n", __func__); + test__skip(); + return; +#endif + + vmlinux_btf = btf__load_vmlinux_btf(); + if (!ASSERT_OK_PTR(vmlinux_btf, "load_vmlinux_btf")) + return; + id = btf__find_by_name_kind(vmlinux_btf, other_name, BTF_KIND_FUNC); + btf__free(vmlinux_btf); + if (!ASSERT_GT(id, 0, "vmlinux func")) + return; + + btf_fd = get_bpf_testmod_btf_fd(); + if (!ASSERT_GE(btf_fd, 0, "get_bpf_testmod_btf_fd")) + return; + + load_opts.attach_btf_obj_fd = btf_fd; + fd = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL", trace_program, + ARRAY_SIZE(trace_program), &load_opts); + if (!ASSERT_GE(fd, 0, "load multi")) + goto out; + + ids[0] = id; + link_opts.tracing_multi.ids = ids; + link_fd = bpf_link_create(fd, 0, BPF_TRACE_FENTRY_MULTI, &link_opts); + ASSERT_EQ(link_fd, -EINVAL, "vmlinux func with module btf, multi"); + if (link_fd >= 0) + close(link_fd); + close(fd); +out: + close(btf_fd); +} + +void test_module_fentry_shadow(void) +{ + if (!env.has_testmod) { + test__skip(); + return; + } + + if (test__start_subtest("shadow")) + test_shadow(); + if (test__start_subtest("multi")) + test_multi(); +} diff --git a/tools/testing/selftests/bpf/progs/test_module_attach.c b/tools/testing/selftests/bpf/progs/test_module_attach.c index 5609e388fb58..3f98562443ac 100644 --- a/tools/testing/selftests/bpf/progs/test_module_attach.c +++ b/tools/testing/selftests/bpf/progs/test_module_attach.c @@ -56,6 +56,22 @@ int BPF_PROG(handle_fentry, return 0; } +/* + * bpf_testmod has a function of its own with the name of a vmlinux function + * of another prototype + */ +SEC("?fentry/bpf_testmod:bpf_fexit_test_ret_ppfile") +int BPF_PROG(handle_fentry_own_func, int a) +{ + return 0; +} + +SEC("?fentry.multi/bpf_testmod:bpf_fexit_test_ret_ppfile") +int BPF_PROG(handle_fentry_multi_own_func) +{ + return 0; +} + SEC("?fentry") int BPF_PROG(handle_fentry_manual, struct file *file, struct kobject *kobj, diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c index 86d25638a727..2e72b54d2445 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c @@ -922,6 +922,16 @@ noinline int bpf_fentry_shadow_test(int a) } EXPORT_SYMBOL_GPL(bpf_fentry_shadow_test); +/* + * Has the name of a vmlinux function, but not its prototype, so it has a + * FUNC of its own + */ +noinline int bpf_fexit_test_ret_ppfile(int a) +{ + return a; +} +EXPORT_SYMBOL_GPL(bpf_fexit_test_ret_ppfile); + __bpf_hook_end(); static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = { -- 2.43.0