]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/bpf: Test multi_st_ops and calling kfuncs from different programs
authorAmery Hung <ameryhung@gmail.com>
Wed, 6 Aug 2025 16:25:40 +0000 (09:25 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Wed, 6 Aug 2025 23:01:58 +0000 (16:01 -0700)
Test multi_st_ops and demonstrate how different bpf programs can call
a kfuncs that refers to the struct_ops instance in the same source file
by id. The id is defined as a global vairable and initialized before
attaching the skeleton. Kfuncs that take the id can hide the argument
with a macro to make it almost transparent to bpf program developers.

The test involves two struct_ops returning different values from
.test_1. In syscall and tracing programs, check if the correct value is
returned by a kfunc that calls .test_1.

Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20250806162540.681679-4-ameryhung@gmail.com
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c [new file with mode: 0644]

diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c
new file mode 100644 (file)
index 0000000..fd8762b
--- /dev/null
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "struct_ops_id_ops_mapping1.skel.h"
+#include "struct_ops_id_ops_mapping2.skel.h"
+
+static void test_st_ops_id_ops_mapping(void)
+{
+       struct struct_ops_id_ops_mapping1 *skel1 = NULL;
+       struct struct_ops_id_ops_mapping2 *skel2 = NULL;
+       struct bpf_map_info info = {};
+       __u32 len = sizeof(info);
+       int err, pid, prog1_fd, prog2_fd;
+
+       skel1 = struct_ops_id_ops_mapping1__open_and_load();
+       if (!ASSERT_OK_PTR(skel1, "struct_ops_id_ops_mapping1__open"))
+               goto out;
+
+       skel2 = struct_ops_id_ops_mapping2__open_and_load();
+       if (!ASSERT_OK_PTR(skel2, "struct_ops_id_ops_mapping2__open"))
+               goto out;
+
+       err = bpf_map_get_info_by_fd(bpf_map__fd(skel1->maps.st_ops_map),
+                                    &info, &len);
+       if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
+               goto out;
+
+       skel1->bss->st_ops_id = info.id;
+
+       err = bpf_map_get_info_by_fd(bpf_map__fd(skel2->maps.st_ops_map),
+                                    &info, &len);
+       if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
+               goto out;
+
+       skel2->bss->st_ops_id = info.id;
+
+       err = struct_ops_id_ops_mapping1__attach(skel1);
+       if (!ASSERT_OK(err, "struct_ops_id_ops_mapping1__attach"))
+               goto out;
+
+       err = struct_ops_id_ops_mapping2__attach(skel2);
+       if (!ASSERT_OK(err, "struct_ops_id_ops_mapping2__attach"))
+               goto out;
+
+       /* run tracing prog that calls .test_1 and checks return */
+       pid = getpid();
+       skel1->bss->test_pid = pid;
+       skel2->bss->test_pid = pid;
+       sys_gettid();
+       skel1->bss->test_pid = 0;
+       skel2->bss->test_pid = 0;
+
+       /* run syscall_prog that calls .test_1 and checks return */
+       prog1_fd = bpf_program__fd(skel1->progs.syscall_prog);
+       err = bpf_prog_test_run_opts(prog1_fd, NULL);
+       ASSERT_OK(err, "bpf_prog_test_run_opts");
+
+       prog2_fd = bpf_program__fd(skel2->progs.syscall_prog);
+       err = bpf_prog_test_run_opts(prog2_fd, NULL);
+       ASSERT_OK(err, "bpf_prog_test_run_opts");
+
+       ASSERT_EQ(skel1->bss->test_err, 0, "skel1->bss->test_err");
+       ASSERT_EQ(skel2->bss->test_err, 0, "skel2->bss->test_err");
+
+out:
+       struct_ops_id_ops_mapping1__destroy(skel1);
+       struct_ops_id_ops_mapping2__destroy(skel2);
+}
+
+void test_struct_ops_id_ops_mapping(void)
+{
+       if (test__start_subtest("st_ops_id_ops_mapping"))
+               test_st_ops_id_ops_mapping();
+}
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c b/tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c
new file mode 100644 (file)
index 0000000..ad8bb54
--- /dev/null
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define bpf_kfunc_multi_st_ops_test_1(args) bpf_kfunc_multi_st_ops_test_1(args, st_ops_id)
+int st_ops_id;
+
+int test_pid;
+int test_err;
+
+#define MAP1_MAGIC 1234
+
+SEC("struct_ops")
+int BPF_PROG(test_1, struct st_ops_args *args)
+{
+       return MAP1_MAGIC;
+}
+
+SEC("tp_btf/sys_enter")
+int BPF_PROG(sys_enter, struct pt_regs *regs, long id)
+{
+       struct st_ops_args args = {};
+       struct task_struct *task;
+       int ret;
+
+       task = bpf_get_current_task_btf();
+       if (!test_pid || task->pid != test_pid)
+               return 0;
+
+       ret = bpf_kfunc_multi_st_ops_test_1(&args);
+       if (ret != MAP1_MAGIC)
+               test_err++;
+
+       return 0;
+}
+
+SEC("syscall")
+int syscall_prog(void *ctx)
+{
+       struct st_ops_args args = {};
+       int ret;
+
+       ret = bpf_kfunc_multi_st_ops_test_1(&args);
+       if (ret != MAP1_MAGIC)
+               test_err++;
+
+       return 0;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_multi_st_ops st_ops_map = {
+       .test_1 = (void *)test_1,
+};
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c b/tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c
new file mode 100644 (file)
index 0000000..cea1a2f
--- /dev/null
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define bpf_kfunc_multi_st_ops_test_1(args) bpf_kfunc_multi_st_ops_test_1(args, st_ops_id)
+int st_ops_id;
+
+int test_pid;
+int test_err;
+
+#define MAP2_MAGIC 4567
+
+SEC("struct_ops")
+int BPF_PROG(test_1, struct st_ops_args *args)
+{
+       return MAP2_MAGIC;
+}
+
+SEC("tp_btf/sys_enter")
+int BPF_PROG(sys_enter, struct pt_regs *regs, long id)
+{
+       struct st_ops_args args = {};
+       struct task_struct *task;
+       int ret;
+
+       task = bpf_get_current_task_btf();
+       if (!test_pid || task->pid != test_pid)
+               return 0;
+
+       ret = bpf_kfunc_multi_st_ops_test_1(&args);
+       if (ret != MAP2_MAGIC)
+               test_err++;
+
+       return 0;
+}
+
+SEC("syscall")
+int syscall_prog(void *ctx)
+{
+       struct st_ops_args args = {};
+       int ret;
+
+       ret = bpf_kfunc_multi_st_ops_test_1(&args);
+       if (ret != MAP2_MAGIC)
+               test_err++;
+
+       return 0;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_multi_st_ops st_ops_map = {
+       .test_1 = (void *)test_1,
+};