[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250206051557.27913-5-chen.dylane@gmail.com>
Date: Thu, 6 Feb 2025 13:15:57 +0800
From: Tao Chen <chen.dylane@...il.com>
To: ast@...nel.org,
daniel@...earbox.net,
andrii@...nel.org,
eddyz87@...il.com,
haoluo@...gle.com,
jolsa@...nel.org,
qmo@...nel.org
Cc: bpf@...r.kernel.org,
linux-kernel@...r.kernel.org,
chen.dylane@...il.com
Subject: [PATCH bpf-next v4 4/4] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests
Add selftests for prog_kfunc feature probing.
./test_progs -t libbpf_probe_kfuncs
#153 libbpf_probe_kfuncs:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
Signed-off-by: Tao Chen <chen.dylane@...il.com>
---
.../selftests/bpf/prog_tests/libbpf_probes.c | 118 ++++++++++++++++++
1 file changed, 118 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
index 4ed46ed58a7b..8f249ca7d5d7 100644
--- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
+++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
@@ -126,3 +126,121 @@ void test_libbpf_probe_helpers(void)
ASSERT_EQ(res, d->supported, buf);
}
}
+
+static int module_btf_fd(char *module)
+{
+ int fd, err;
+ __u32 id = 0, len;
+ struct bpf_btf_info info;
+ char name[64];
+
+ while (true) {
+ err = bpf_btf_get_next_id(id, &id);
+ if (err && (errno == ENOENT || errno == EPERM))
+ return 0;
+ if (err) {
+ err = -errno;
+ return err;
+ }
+ fd = bpf_btf_get_fd_by_id(id);
+ if (fd < 0) {
+ if (errno == ENOENT)
+ continue;
+ err = -errno;
+ return err;
+ }
+ len = sizeof(info);
+ memset(&info, 0, sizeof(info));
+ info.name = ptr_to_u64(name);
+ info.name_len = sizeof(name);
+ err = bpf_btf_get_info_by_fd(fd, &info, &len);
+ if (err) {
+ err = -errno;
+ goto err_out;
+ }
+ /* find target module btf */
+ if (!strcmp(name, module))
+ break;
+
+ close(fd);
+ }
+
+ return fd;
+err_out:
+ close(fd);
+ return err;
+}
+
+void test_libbpf_probe_kfuncs(void)
+{
+ int ret, kfunc_id, fd;
+ char *kfunc = "bpf_cpumask_create";
+ struct btf *vmlinux_btf = NULL;
+ struct btf *module_btf = NULL;
+
+ vmlinux_btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
+ if (!ASSERT_OK_PTR(vmlinux_btf, "btf_parse"))
+ return;
+
+ kfunc_id = btf__find_by_name_kind(vmlinux_btf, kfunc, BTF_KIND_FUNC);
+ if (!ASSERT_GT(kfunc_id, 0, kfunc))
+ goto cleanup;
+
+ /* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_cpumask_create */
+ ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 0, NULL);
+ if (!ASSERT_EQ(ret, 1, "kfunc in vmlinux support"))
+ goto cleanup;
+
+ /* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_cpumask_create */
+ ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, 0, NULL);
+ if (!ASSERT_EQ(ret, 0, "kfunc in vmlinux not suuport"))
+ goto cleanup;
+
+ ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
+ if (!ASSERT_EQ(ret, 0, "invalid kfunc id:-1"))
+ goto cleanup;
+
+ ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
+ if (!ASSERT_ERR(ret, "invalid prog type:100000"))
+ goto cleanup;
+
+ if (!env.has_testmod)
+ goto cleanup;
+
+ module_btf = btf__load_module_btf("bpf_testmod", vmlinux_btf);
+ if (!ASSERT_OK_PTR(module_btf, "load module BTF"))
+ goto cleanup;
+
+ kfunc_id = btf__find_by_name(module_btf, "bpf_kfunc_call_test1");
+ if (!ASSERT_GT(kfunc_id, 0, "func not found"))
+ goto cleanup;
+
+ fd = module_btf_fd("bpf_testmod");
+ if (!ASSERT_GT(fd, 0, "module btf fd"))
+ goto cleanup;
+
+ /* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_kfunc_call_test1 in bpf_testmod */
+ ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, fd, NULL);
+ if (!ASSERT_EQ(ret, 1, "kfunc in module btf support"))
+ goto cleanup_fd;
+
+ /* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_kfunc_call_test1
+ * in bpf_testmod
+ */
+ ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, fd, NULL);
+ if (!ASSERT_EQ(ret, 0, "kfunc in module btf not support"))
+ goto cleanup_fd;
+
+ ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, -1, fd, NULL);
+ if (!ASSERT_EQ(ret, 0, "invalid kfunc id in module btf"))
+ goto cleanup_fd;
+
+ ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 100, NULL);
+ ASSERT_EQ(ret, 0, "invalid btf fd in module btf");
+
+cleanup_fd:
+ close(fd);
+cleanup:
+ btf__free(vmlinux_btf);
+ btf__free(module_btf);
+}
--
2.43.0
Powered by blists - more mailing lists