diff mbox series

[bpf-next,v8,4/4] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

Message ID 20250221163335.262143-5-chen.dylane@linux.dev (mailing list archive)
State New
Delegated to: BPF
Headers show
Series Add prog_kfunc feature probe | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for s390x-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-11 success Logs for aarch64-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-20 success Logs for s390x-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-21 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-18 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for aarch64-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-gcc / veristat-kernel / x86_64-gcc veristat_kernel
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-gcc / veristat-meta / x86_64-gcc veristat_meta
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-17 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-17 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-43 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-44 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-45 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-49 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-50 success Logs for x86_64-llvm-18 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-51 success Logs for x86_64-llvm-18 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-46 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-47 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-48 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success Errors and warnings before: 26 (+1) this patch: 26 (+1)
netdev/cc_maintainers warning 9 maintainers not CCed: kpsingh@kernel.org sdf@fomichev.me yonghong.song@linux.dev song@kernel.org shuah@kernel.org john.fastabend@gmail.com linux-kselftest@vger.kernel.org mykolal@fb.com martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: Alignment should match open parenthesis WARNING: line length of 81 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc

Commit Message

Tao Chen Feb. 21, 2025, 4:33 p.m. UTC
Add selftests for prog_kfunc feature probing. Thanks for
Eduard providing the libbpf_probe_func_many test case.

 ./test_progs -t libbpf_probe_kfuncs
 #153     libbpf_probe_kfuncs:OK
 Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

 ./test_progs -t libbpf_probe_kfuncs_many
 #154     libbpf_probe_kfuncs_many:OK
 Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Cc: Tao Chen <dylane.chen@didiglobal.com>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Co-developed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Tao Chen <chen.dylane@linux.dev>
---
 .../selftests/bpf/prog_tests/libbpf_probes.c  | 173 ++++++++++++++++++
 1 file changed, 173 insertions(+)

Comments

Eduard Zingerman Feb. 21, 2025, 6:19 p.m. UTC | #1
On Sat, 2025-02-22 at 00:33 +0800, Tao Chen wrote:

[...]

> +static const struct {
> +	const char *name;
> +	int code;
> +} program_types[] = {
> +#define _T(n) { #n, BPF_PROG_TYPE_##n }
> +	_T(KPROBE),
> +	_T(XDP),
> +	_T(SYSCALL),
> +	_T(SCHED_CLS),
> +	_T(SCHED_ACT),
> +	_T(SK_SKB),
> +	_T(SOCKET_FILTER),
> +	_T(CGROUP_SKB),
> +	_T(LWT_OUT),
> +	_T(LWT_IN),
> +	_T(LWT_XMIT),
> +	_T(LWT_SEG6LOCAL),
> +	_T(NETFILTER),
> +	_T(CGROUP_SOCK_ADDR),
> +	_T(SCHED_ACT)
> +#undef _T
> +};
> +
> +void test_libbpf_probe_kfuncs_many(void)
> +{

Hi Tao,

Sorry, probably some miscommunication from my side.
I did not mean this test for inclusion, it was meant as a one time
manual inspection of libbpf_probe_bpf_kfunc results.
Just as a sanity check before series is merged.
As an automated test it does not provide much meaningful signal.

> +	int i, kfunc_id, ret, id;
> +	const struct btf_type *t;
> +	struct btf *btf = NULL;
> +	const char *kfunc;
> +	const char *tag;
> +
> +	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
> +	if (!ASSERT_OK_PTR(btf, "btf_parse"))
> +		return;
> +	for (id = 0; id < btf__type_cnt(btf); ++id) {
> +		t = btf__type_by_id(btf, id);
> +		if (!t)
> +			continue;
> +		if (!btf_is_decl_tag(t))
> +			continue;
> +		tag = btf__name_by_offset(btf, t->name_off);
> +		if (strcmp(tag, "bpf_kfunc") != 0)
> +			continue;
> +		kfunc_id = t->type;
> +		t = btf__type_by_id(btf, kfunc_id);
> +		if (!btf_is_func(t))
> +			continue;
> +		kfunc = btf__name_by_offset(btf, t->name_off);
> +		for (i = 0; i < ARRAY_SIZE(program_types); ++i) {
> +			ret = libbpf_probe_bpf_kfunc(program_types[i].code,
> +						     kfunc_id, -1, NULL);
> +			if (ret < 0) {
> +				ASSERT_FAIL("kfunc:%s use prog type:%d",
> +				      kfunc, program_types[i].code);
> +				goto cleanup;
> +			}
> +		}
> +	}
> +cleanup:
> +	btf__free(btf);
> +}
Tao Chen Feb. 22, 2025, 3 a.m. UTC | #2
在 2025/2/22 02:19, Eduard Zingerman 写道:
> On Sat, 2025-02-22 at 00:33 +0800, Tao Chen wrote:
> 
> [...]
> 
>> +static const struct {
>> +	const char *name;
>> +	int code;
>> +} program_types[] = {
>> +#define _T(n) { #n, BPF_PROG_TYPE_##n }
>> +	_T(KPROBE),
>> +	_T(XDP),
>> +	_T(SYSCALL),
>> +	_T(SCHED_CLS),
>> +	_T(SCHED_ACT),
>> +	_T(SK_SKB),
>> +	_T(SOCKET_FILTER),
>> +	_T(CGROUP_SKB),
>> +	_T(LWT_OUT),
>> +	_T(LWT_IN),
>> +	_T(LWT_XMIT),
>> +	_T(LWT_SEG6LOCAL),
>> +	_T(NETFILTER),
>> +	_T(CGROUP_SOCK_ADDR),
>> +	_T(SCHED_ACT)
>> +#undef _T
>> +};
>> +
>> +void test_libbpf_probe_kfuncs_many(void)
>> +{
> 
> Hi Tao,
> 
> Sorry, probably some miscommunication from my side.
> I did not mean this test for inclusion, it was meant as a one time
> manual inspection of libbpf_probe_bpf_kfunc results.
> Just as a sanity check before series is merged.
> As an automated test it does not provide much meaningful signal.
> 

Ok, i will resend v8 without this case, and check all the prog types 
later with your program. Thanks.

>> +	int i, kfunc_id, ret, id;
>> +	const struct btf_type *t;
>> +	struct btf *btf = NULL;
>> +	const char *kfunc;
>> +	const char *tag;
>> +
>> +	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
>> +	if (!ASSERT_OK_PTR(btf, "btf_parse"))
>> +		return;
>> +	for (id = 0; id < btf__type_cnt(btf); ++id) {
>> +		t = btf__type_by_id(btf, id);
>> +		if (!t)
>> +			continue;
>> +		if (!btf_is_decl_tag(t))
>> +			continue;
>> +		tag = btf__name_by_offset(btf, t->name_off);
>> +		if (strcmp(tag, "bpf_kfunc") != 0)
>> +			continue;
>> +		kfunc_id = t->type;
>> +		t = btf__type_by_id(btf, kfunc_id);
>> +		if (!btf_is_func(t))
>> +			continue;
>> +		kfunc = btf__name_by_offset(btf, t->name_off);
>> +		for (i = 0; i < ARRAY_SIZE(program_types); ++i) {
>> +			ret = libbpf_probe_bpf_kfunc(program_types[i].code,
>> +						     kfunc_id, -1, NULL);
>> +			if (ret < 0) {
>> +				ASSERT_FAIL("kfunc:%s use prog type:%d",
>> +				      kfunc, program_types[i].code);
>> +				goto cleanup;
>> +			}
>> +		}
>> +	}
>> +cleanup:
>> +	btf__free(btf);
>> +}
> 
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
index 4ed46ed58a7b..db408fd67add 100644
--- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
+++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
@@ -126,3 +126,176 @@  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)
+			return -1;
+
+		fd = bpf_btf_get_fd_by_id(id);
+		if (fd < 0) {
+			if (errno == ENOENT)
+				continue;
+			return -1;
+		}
+		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) {
+			close(fd);
+			return -1;
+		}
+		/* find target module BTF */
+		if (!strcmp(name, module))
+			break;
+
+		close(fd);
+	}
+
+	return fd;
+}
+
+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, -1, 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, -1, NULL);
+	if (!ASSERT_EQ(ret, 0, "kfunc in vmlinux not suuport"))
+		goto cleanup;
+
+	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, -1, NULL);
+	if (!ASSERT_EQ(ret, 0, "invalid kfunc id:-1"))
+		goto cleanup;
+
+	ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, -1, 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_GE(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);
+}
+
+static const struct {
+	const char *name;
+	int code;
+} program_types[] = {
+#define _T(n) { #n, BPF_PROG_TYPE_##n }
+	_T(KPROBE),
+	_T(XDP),
+	_T(SYSCALL),
+	_T(SCHED_CLS),
+	_T(SCHED_ACT),
+	_T(SK_SKB),
+	_T(SOCKET_FILTER),
+	_T(CGROUP_SKB),
+	_T(LWT_OUT),
+	_T(LWT_IN),
+	_T(LWT_XMIT),
+	_T(LWT_SEG6LOCAL),
+	_T(NETFILTER),
+	_T(CGROUP_SOCK_ADDR),
+	_T(SCHED_ACT)
+#undef _T
+};
+
+void test_libbpf_probe_kfuncs_many(void)
+{
+	int i, kfunc_id, ret, id;
+	const struct btf_type *t;
+	struct btf *btf = NULL;
+	const char *kfunc;
+	const char *tag;
+
+	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
+	if (!ASSERT_OK_PTR(btf, "btf_parse"))
+		return;
+	for (id = 0; id < btf__type_cnt(btf); ++id) {
+		t = btf__type_by_id(btf, id);
+		if (!t)
+			continue;
+		if (!btf_is_decl_tag(t))
+			continue;
+		tag = btf__name_by_offset(btf, t->name_off);
+		if (strcmp(tag, "bpf_kfunc") != 0)
+			continue;
+		kfunc_id = t->type;
+		t = btf__type_by_id(btf, kfunc_id);
+		if (!btf_is_func(t))
+			continue;
+		kfunc = btf__name_by_offset(btf, t->name_off);
+		for (i = 0; i < ARRAY_SIZE(program_types); ++i) {
+			ret = libbpf_probe_bpf_kfunc(program_types[i].code,
+						     kfunc_id, -1, NULL);
+			if (ret < 0) {
+				ASSERT_FAIL("kfunc:%s use prog type:%d",
+				      kfunc, program_types[i].code);
+				goto cleanup;
+			}
+		}
+	}
+cleanup:
+	btf__free(btf);
+}