diff mbox series

[bpf-next,2/2] selftests/bpf: validate nulled-out struct_ops program is handled properly

Message ID 20240428030954.3918764-2-andrii@kernel.org (mailing list archive)
State Accepted
Commit 1bba3b3d373dbafae891e7cb06b8c82c8d62aba1
Delegated to: BPF
Headers show
Series [bpf-next,1/2] libbpf: handle nulled-out program in struct_ops correctly | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
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: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 13 maintainers not CCed: kpsingh@kernel.org john.fastabend@gmail.com thinker.li@gmail.com shuah@kernel.org jolsa@kernel.org eddyz87@gmail.com song@kernel.org yonghong.song@linux.dev linux-kselftest@vger.kernel.org martin.lau@linux.dev mykolal@fb.com haoluo@google.com sdf@google.com
netdev/build_clang success Errors and warnings before: 8 this patch: 8
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: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 66 lines checked
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-25 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-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-39 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-40 success 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-38 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-32 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-24 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on 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-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 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-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-37 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-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success 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-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-30 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-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-8 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-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
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-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18

Commit Message

Andrii Nakryiko April 28, 2024, 3:09 a.m. UTC
Add a selftests validating that it's possible to have some struct_ops
callback set declaratively, then disable it (by setting to NULL)
programmatically. Libbpf should detect that such program should be
loaded, even if host kernel doesn't have type information for it.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../bpf/prog_tests/test_struct_ops_module.c    | 18 ++++++++++++++++--
 .../selftests/bpf/progs/struct_ops_module.c    |  7 +++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

Comments

Martin KaFai Lau April 29, 2024, 9:29 p.m. UTC | #1
On 4/27/24 8:09 PM, Andrii Nakryiko wrote:
> Add a selftests validating that it's possible to have some struct_ops
> callback set declaratively, then disable it (by setting to NULL)
> programmatically. Libbpf should detect that such program should be

such program should be /not/ loaded ?

> loaded, even if host kernel doesn't have type information for it.
> 

> @@ -103,6 +104,10 @@ static void test_struct_ops_not_zeroed(void)
>   	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
>   		return;
>   
> +	skel->struct_ops.testmod_zeroed->zeroed = 0;
> +	/* zeroed_op prog should be not loaded automatically now */
> +	skel->struct_ops.testmod_zeroed->zeroed_op = NULL;
> +
>   	err = struct_ops_module__load(skel);
>   	ASSERT_OK(err, "struct_ops_module_load");
>   
> @@ -118,6 +123,7 @@ static void test_struct_ops_not_zeroed(void)
>   	 * value of "zeroed" is non-zero.
>   	 */
>   	skel->struct_ops.testmod_zeroed->zeroed = 0xdeadbeef;
> +	skel->struct_ops.testmod_zeroed->zeroed_op = NULL;
>   	err = struct_ops_module__load(skel);
>   	ASSERT_ERR(err, "struct_ops_module_load_not_zeroed");
>
Andrii Nakryiko April 29, 2024, 10:35 p.m. UTC | #2
On Mon, Apr 29, 2024 at 2:29 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>
> On 4/27/24 8:09 PM, Andrii Nakryiko wrote:
> > Add a selftests validating that it's possible to have some struct_ops
> > callback set declaratively, then disable it (by setting to NULL)
> > programmatically. Libbpf should detect that such program should be
>
> such program should be /not/ loaded ?

yep, can you fix it up while applying or should I send a new revision?

>
> > loaded, even if host kernel doesn't have type information for it.
> >
>
> > @@ -103,6 +104,10 @@ static void test_struct_ops_not_zeroed(void)
> >       if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
> >               return;
> >
> > +     skel->struct_ops.testmod_zeroed->zeroed = 0;
> > +     /* zeroed_op prog should be not loaded automatically now */
> > +     skel->struct_ops.testmod_zeroed->zeroed_op = NULL;
> > +
> >       err = struct_ops_module__load(skel);
> >       ASSERT_OK(err, "struct_ops_module_load");
> >
> > @@ -118,6 +123,7 @@ static void test_struct_ops_not_zeroed(void)
> >        * value of "zeroed" is non-zero.
> >        */
> >       skel->struct_ops.testmod_zeroed->zeroed = 0xdeadbeef;
> > +     skel->struct_ops.testmod_zeroed->zeroed_op = NULL;
> >       err = struct_ops_module__load(skel);
> >       ASSERT_ERR(err, "struct_ops_module_load_not_zeroed");
> >
>
Martin KaFai Lau April 29, 2024, 11:46 p.m. UTC | #3
On 4/29/24 3:35 PM, Andrii Nakryiko wrote:
> On Mon, Apr 29, 2024 at 2:29 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>>
>> On 4/27/24 8:09 PM, Andrii Nakryiko wrote:
>>> Add a selftests validating that it's possible to have some struct_ops
>>> callback set declaratively, then disable it (by setting to NULL)
>>> programmatically. Libbpf should detect that such program should be
>>
>> such program should be /not/ loaded ?
> 
> yep, can you fix it up while applying or should I send a new revision?

I will take care of it. No need to respin.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
index 7cf2b9ddd3e1..bd39586abd5a 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
@@ -66,6 +66,7 @@  static void test_struct_ops_load(void)
 	 * auto-loading, or it will fail to load.
 	 */
 	bpf_program__set_autoload(skel->progs.test_2, false);
+	bpf_map__set_autocreate(skel->maps.testmod_zeroed, false);
 
 	err = struct_ops_module__load(skel);
 	if (!ASSERT_OK(err, "struct_ops_module_load"))
@@ -103,6 +104,10 @@  static void test_struct_ops_not_zeroed(void)
 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
 		return;
 
+	skel->struct_ops.testmod_zeroed->zeroed = 0;
+	/* zeroed_op prog should be not loaded automatically now */
+	skel->struct_ops.testmod_zeroed->zeroed_op = NULL;
+
 	err = struct_ops_module__load(skel);
 	ASSERT_OK(err, "struct_ops_module_load");
 
@@ -118,6 +123,7 @@  static void test_struct_ops_not_zeroed(void)
 	 * value of "zeroed" is non-zero.
 	 */
 	skel->struct_ops.testmod_zeroed->zeroed = 0xdeadbeef;
+	skel->struct_ops.testmod_zeroed->zeroed_op = NULL;
 	err = struct_ops_module__load(skel);
 	ASSERT_ERR(err, "struct_ops_module_load_not_zeroed");
 
@@ -148,15 +154,23 @@  static void test_struct_ops_incompatible(void)
 {
 	struct struct_ops_module *skel;
 	struct bpf_link *link;
+	int err;
 
-	skel = struct_ops_module__open_and_load();
-	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+	skel = struct_ops_module__open();
+	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
 		return;
 
+	bpf_map__set_autocreate(skel->maps.testmod_zeroed, false);
+
+	err = struct_ops_module__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
 	link = bpf_map__attach_struct_ops(skel->maps.testmod_incompatible);
 	if (ASSERT_OK_PTR(link, "attach_struct_ops"))
 		bpf_link__destroy(link);
 
+cleanup:
 	struct_ops_module__destroy(skel);
 }
 
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c
index 63b065dae002..40109be2b3ae 100644
--- a/tools/testing/selftests/bpf/progs/struct_ops_module.c
+++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c
@@ -63,10 +63,17 @@  struct bpf_testmod_ops___zeroed {
 	int zeroed;
 };
 
+SEC("?struct_ops/test_3")
+int BPF_PROG(zeroed_op)
+{
+	return 1;
+}
+
 SEC(".struct_ops.link")
 struct bpf_testmod_ops___zeroed testmod_zeroed = {
 	.test_1 = (void *)test_1,
 	.test_2 = (void *)test_2_v2,
+	.zeroed_op = (void *)zeroed_op,
 };
 
 struct bpf_testmod_ops___incompatible {