diff mbox series

[bpf-next,v3,3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

Message ID 20250124144411.13468-4-chen.dylane@gmail.com (mailing list archive)
State Changes Requested
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-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 success Logs for aarch64-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-19 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-18 success Logs for s390x-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 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-30 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-gcc / veristat-meta / x86_64-gcc veristat_meta
bpf/vmtest-bpf-next-VM_Test-32 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-35 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-36 success Logs for x86_64-llvm-17 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-17 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-38 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 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-45 success Logs for x86_64-llvm-18 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-40 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-46 success Logs for x86_64-llvm-18 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-23 fail Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 fail Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 fail Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-33 fail 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-25 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
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 WARNING: line length of 81 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-28 fail Logs for x86_64-gcc / veristat-kernel / x86_64-gcc veristat_kernel
bpf/vmtest-bpf-next-VM_Test-24 fail 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-34 fail 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-43 fail Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-42 fail 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-41 fail 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-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-7 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-10 success Logs for x86_64-gcc / build-release

Commit Message

Tao Chen Jan. 24, 2025, 2:44 p.m. UTC
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@gmail.com>
---
 .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)

Comments

Jiri Olsa Jan. 24, 2025, 4:27 p.m. UTC | #1
On Fri, Jan 24, 2025 at 10:44:11PM +0800, Tao Chen wrote:
> 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@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>  1 file changed, 35 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..d9d69941f694 100644
> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>  		ASSERT_EQ(res, d->supported, buf);
>  	}
>  }
> +
> +void test_libbpf_probe_kfuncs(void)
> +{
> +	int ret, kfunc_id;
> +	char *kfunc = "bpf_cpumask_create";
> +	struct btf *btf;
> +
> +	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
> +	if (!ASSERT_OK_PTR(btf, "btf_parse"))
> +		return;
> +
> +	kfunc_id = btf__find_by_name_kind(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);
> +	ASSERT_EQ(ret, 1, kfunc);
> +
> +	/* 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);
> +	ASSERT_EQ(ret, 0, kfunc);
> +
> +	/* invalid kfunc id */
> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
> +	ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
> +
> +	/* invalid prog type */
> +	ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
> +	if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
> +		goto cleanup;

nit no need for the goto

jirka

> +
> +cleanup:
> +	btf__free(btf);
> +}
> -- 
> 2.43.0
>
Andrii Nakryiko Jan. 24, 2025, 6:48 p.m. UTC | #2
On Fri, Jan 24, 2025 at 6:44 AM Tao Chen <chen.dylane@gmail.com> wrote:
>
> 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@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>  1 file changed, 35 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..d9d69941f694 100644
> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>                 ASSERT_EQ(res, d->supported, buf);
>         }
>  }
> +
> +void test_libbpf_probe_kfuncs(void)
> +{
> +       int ret, kfunc_id;
> +       char *kfunc = "bpf_cpumask_create";
> +       struct btf *btf;
> +
> +       btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
> +       if (!ASSERT_OK_PTR(btf, "btf_parse"))
> +               return;
> +
> +       kfunc_id = btf__find_by_name_kind(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);
> +       ASSERT_EQ(ret, 1, kfunc);
> +
> +       /* 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);
> +       ASSERT_EQ(ret, 0, kfunc);
> +
> +       /* invalid kfunc id */
> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
> +       ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
> +
> +       /* invalid prog type */
> +       ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
> +       if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))

we have ASSERT_ERR(), wouldn't it work here?


let's also add a test for kfunc in module (we have bpf_testmod, we
should be able to test something out of there)

> +               goto cleanup;
> +
> +cleanup:
> +       btf__free(btf);
> +}
> --
> 2.43.0
>
Tao Chen Jan. 25, 2025, 2:56 p.m. UTC | #3
在 2025/1/25 00:27, Jiri Olsa 写道:
> On Fri, Jan 24, 2025 at 10:44:11PM +0800, Tao Chen wrote:
>> 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@gmail.com>
>> ---
>>   .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>>   1 file changed, 35 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..d9d69941f694 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>>   		ASSERT_EQ(res, d->supported, buf);
>>   	}
>>   }
>> +
>> +void test_libbpf_probe_kfuncs(void)
>> +{
>> +	int ret, kfunc_id;
>> +	char *kfunc = "bpf_cpumask_create";
>> +	struct btf *btf;
>> +
>> +	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
>> +	if (!ASSERT_OK_PTR(btf, "btf_parse"))
>> +		return;
>> +
>> +	kfunc_id = btf__find_by_name_kind(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);
>> +	ASSERT_EQ(ret, 1, kfunc);
>> +
>> +	/* 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);
>> +	ASSERT_EQ(ret, 0, kfunc);
>> +
>> +	/* invalid kfunc id */
>> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
>> +	ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
>> +
>> +	/* invalid prog type */
>> +	ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
>> +	if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
>> +		goto cleanup;
> 
> nit no need for the goto
> 
> jirka
> 

Ack. Will fix.

>> +
>> +cleanup:
>> +	btf__free(btf);
>> +}
>> -- 
>> 2.43.0
>>
Tao Chen Jan. 25, 2025, 3:07 p.m. UTC | #4
在 2025/1/25 02:48, Andrii Nakryiko 写道:
> On Fri, Jan 24, 2025 at 6:44 AM Tao Chen <chen.dylane@gmail.com> wrote:
>>
>> 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@gmail.com>
>> ---
>>   .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>>   1 file changed, 35 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..d9d69941f694 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>>                  ASSERT_EQ(res, d->supported, buf);
>>          }
>>   }
>> +
>> +void test_libbpf_probe_kfuncs(void)
>> +{
>> +       int ret, kfunc_id;
>> +       char *kfunc = "bpf_cpumask_create";
>> +       struct btf *btf;
>> +
>> +       btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
>> +       if (!ASSERT_OK_PTR(btf, "btf_parse"))
>> +               return;
>> +
>> +       kfunc_id = btf__find_by_name_kind(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);
>> +       ASSERT_EQ(ret, 1, kfunc);
>> +
>> +       /* 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);
>> +       ASSERT_EQ(ret, 0, kfunc);
>> +
>> +       /* invalid kfunc id */
>> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
>> +       ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
>> +
>> +       /* invalid prog type */
>> +       ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
>> +       if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
> 
> we have ASSERT_ERR(), wouldn't it work here?
> 
> 
> let's also add a test for kfunc in module (we have bpf_testmod, we
> should be able to test something out of there)

Ok, i will add it in v4.

> 
>> +               goto cleanup;
>> +
>> +cleanup:
>> +       btf__free(btf);
>> +}
>> --
>> 2.43.0
>>
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..d9d69941f694 100644
--- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
+++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
@@ -126,3 +126,38 @@  void test_libbpf_probe_helpers(void)
 		ASSERT_EQ(res, d->supported, buf);
 	}
 }
+
+void test_libbpf_probe_kfuncs(void)
+{
+	int ret, kfunc_id;
+	char *kfunc = "bpf_cpumask_create";
+	struct btf *btf;
+
+	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
+	if (!ASSERT_OK_PTR(btf, "btf_parse"))
+		return;
+
+	kfunc_id = btf__find_by_name_kind(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);
+	ASSERT_EQ(ret, 1, kfunc);
+
+	/* 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);
+	ASSERT_EQ(ret, 0, kfunc);
+
+	/* invalid kfunc id */
+	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
+	ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
+
+	/* invalid prog type */
+	ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
+	if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
+		goto cleanup;
+
+cleanup:
+	btf__free(btf);
+}