diff mbox series

[v2,bpf-next,9/9] selftests/bpf: add arg:ctx cases to test_global_funcs tests

Message ID 20240102190055.1602698-10-andrii@kernel.org (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Libbpf-side __arg_ctx fallback support | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success SINGLE THREAD; 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/cc_maintainers warning 10 maintainers not CCed: sdf@google.com haoluo@google.com martin.lau@linux.dev kpsingh@kernel.org shuah@kernel.org yonghong.song@linux.dev linux-kselftest@vger.kernel.org mykolal@fb.com song@kernel.org john.fastabend@gmail.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, 52 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-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-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
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-34 success Logs for x86_64-llvm-17 / veristat
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-42 success Logs for x86_64-llvm-18 / veristat
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-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-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-27 success Logs for x86_64-gcc / veristat / veristat 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-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-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-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-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
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-41 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-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-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-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
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-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-13 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-15 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-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for x86_64-gcc / build / build for x86_64 with gcc
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-29 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / veristat
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-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-31 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-24 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Andrii Nakryiko Jan. 2, 2024, 7 p.m. UTC
Add a few extra cases of global funcs with context arguments. This time
rely on "arg:ctx" decl_tag (__arg_ctx macro), but put it next to
"classic" cases where context argument has to be of an exact type that
BPF verifier expects (e.g., bpf_user_pt_regs_t for kprobe/uprobe).

Colocating all these cases separately from other global func args that
rely on arg:xxx decl tags (in verifier_global_subprogs.c) allows for
simpler backwards compatibility testing on old kernels. All the cases in
test_global_func_ctx_args.c are supposed to work on older kernels, which
was manually validated during development.

Acked-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../bpf/progs/test_global_func_ctx_args.c     | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)

Comments

Eduard Zingerman Jan. 3, 2024, 8:57 p.m. UTC | #1
On Tue, 2024-01-02 at 11:00 -0800, Andrii Nakryiko wrote:
> Add a few extra cases of global funcs with context arguments. This time
> rely on "arg:ctx" decl_tag (__arg_ctx macro), but put it next to
> "classic" cases where context argument has to be of an exact type that
> BPF verifier expects (e.g., bpf_user_pt_regs_t for kprobe/uprobe).
> 
> Colocating all these cases separately from other global func args that
> rely on arg:xxx decl tags (in verifier_global_subprogs.c) allows for
> simpler backwards compatibility testing on old kernels. All the cases in
> test_global_func_ctx_args.c are supposed to work on older kernels, which
> was manually validated during development.
> 
> Acked-by: Jiri Olsa <jolsa@kernel.org>
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> ---

At first I thought that we would need to add a new CI job that would
run these tests for some older kernel version.

However, the transformation of the sub-program parameters happens
unconditionally. So it should be possible to read BTF for some of the
programs after they are loaded and check if transformation is applied
as expected. Thus allowing to check __arg_ctx handling on libbpf side
w/o the need to run on old kernel.
I think it's worth it to add such test, wdyt?
Andrii Nakryiko Jan. 3, 2024, 11:17 p.m. UTC | #2
On Wed, Jan 3, 2024 at 12:57 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Tue, 2024-01-02 at 11:00 -0800, Andrii Nakryiko wrote:
> > Add a few extra cases of global funcs with context arguments. This time
> > rely on "arg:ctx" decl_tag (__arg_ctx macro), but put it next to
> > "classic" cases where context argument has to be of an exact type that
> > BPF verifier expects (e.g., bpf_user_pt_regs_t for kprobe/uprobe).
> >
> > Colocating all these cases separately from other global func args that
> > rely on arg:xxx decl tags (in verifier_global_subprogs.c) allows for
> > simpler backwards compatibility testing on old kernels. All the cases in
> > test_global_func_ctx_args.c are supposed to work on older kernels, which
> > was manually validated during development.
> >
> > Acked-by: Jiri Olsa <jolsa@kernel.org>
> > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > ---
>
> At first I thought that we would need to add a new CI job that would
> run these tests for some older kernel version.

libbpf CI will test this on 5.15 kernel, so we will have regression tests

>
> However, the transformation of the sub-program parameters happens
> unconditionally. So it should be possible to read BTF for some of the
> programs after they are loaded and check if transformation is applied
> as expected. Thus allowing to check __arg_ctx handling on libbpf side
> w/o the need to run on old kernel.

Yes, but it's bpf_prog_info to get func_info (actually two calls due
to how API is), parse func_info (pain without libbpf internal helpers
from libbpf_internal.h, and with it's more coupling) to find subprog's
func BTF ID and then check BTF.

It's so painful that I don't think it's worth it given we'll test this
in libbpf CI (and I did manual check locally as well).

Also, nothing actually prevents us from not doing this if the kernel
supports __arg_ctx natively, which is just a painful feature detector
to write, using low-level APIs, which is why I decided that it's
simpler to just do this unconditionally.

> I think it's worth it to add such test, wdyt?
>

I feel like slacking and not adding this :) This will definitely fail
in libbpf CI, if it's wrong.
Eduard Zingerman Jan. 3, 2024, 11:51 p.m. UTC | #3
On Wed, 2024-01-03 at 15:17 -0800, Andrii Nakryiko wrote:
[...]
> > However, the transformation of the sub-program parameters happens
> > unconditionally. So it should be possible to read BTF for some of the
> > programs after they are loaded and check if transformation is applied
> > as expected. Thus allowing to check __arg_ctx handling on libbpf side
> > w/o the need to run on old kernel.
> 
> Yes, but it's bpf_prog_info to get func_info (actually two calls due
> to how API is), parse func_info (pain without libbpf internal helpers
> from libbpf_internal.h, and with it's more coupling) to find subprog's
> func BTF ID and then check BTF.
> 
> It's so painful that I don't think it's worth it given we'll test this
> in libbpf CI (and I did manual check locally as well).
> 
> Also, nothing actually prevents us from not doing this if the kernel
> supports __arg_ctx natively, which is just a painful feature detector
> to write, using low-level APIs, which is why I decided that it's
> simpler to just do this unconditionally.

I agree that there is no need for feature detection in this case.

> > I think it's worth it to add such test, wdyt?
> > 
> 
> I feel like slacking and not adding this :) This will definitely fail
> in libbpf CI, if it's wrong.

Very few people look at libbpf CI results and those results would be
available only after sync.

Idk, I think that some form of testing is necessary for kernel CI.
Either this, or an additional job that executes selected set of tests
on old kernel.
Andrii Nakryiko Jan. 4, 2024, 12:26 a.m. UTC | #4
On Wed, Jan 3, 2024 at 3:51 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Wed, 2024-01-03 at 15:17 -0800, Andrii Nakryiko wrote:
> [...]
> > > However, the transformation of the sub-program parameters happens
> > > unconditionally. So it should be possible to read BTF for some of the
> > > programs after they are loaded and check if transformation is applied
> > > as expected. Thus allowing to check __arg_ctx handling on libbpf side
> > > w/o the need to run on old kernel.
> >
> > Yes, but it's bpf_prog_info to get func_info (actually two calls due
> > to how API is), parse func_info (pain without libbpf internal helpers
> > from libbpf_internal.h, and with it's more coupling) to find subprog's
> > func BTF ID and then check BTF.
> >
> > It's so painful that I don't think it's worth it given we'll test this
> > in libbpf CI (and I did manual check locally as well).
> >
> > Also, nothing actually prevents us from not doing this if the kernel
> > supports __arg_ctx natively, which is just a painful feature detector
> > to write, using low-level APIs, which is why I decided that it's
> > simpler to just do this unconditionally.
>
> I agree that there is no need for feature detection in this case.
>

ok

> > > I think it's worth it to add such test, wdyt?
> > >
> >
> > I feel like slacking and not adding this :) This will definitely fail
> > in libbpf CI, if it's wrong.
>
> Very few people look at libbpf CI results and those results would be
> available only after sync.
>
> Idk, I think that some form of testing is necessary for kernel CI.
> Either this, or an additional job that executes selected set of tests
> on old kernel.

Alright, I'll add a test then.
Eduard Zingerman Jan. 4, 2024, 12:28 a.m. UTC | #5
On Wed, 2024-01-03 at 16:26 -0800, Andrii Nakryiko wrote:
[...]
> > > > I think it's worth it to add such test, wdyt?
> > > > 
> > > 
> > > I feel like slacking and not adding this :) This will definitely fail
> > > in libbpf CI, if it's wrong.
> > 
> > Very few people look at libbpf CI results and those results would be
> > available only after sync.
> > 
> > Idk, I think that some form of testing is necessary for kernel CI.
> > Either this, or an additional job that executes selected set of tests
> > on old kernel.
> 
> Alright, I'll add a test then.

Thank you.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c b/tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c
index 7faa8eef0598..9a06e5eb1fbe 100644
--- a/tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c
+++ b/tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c
@@ -102,3 +102,52 @@  int perf_event_ctx(void *ctx)
 {
 	return perf_event_ctx_subprog(ctx);
 }
+
+/* this global subprog can be now called from many types of entry progs, each
+ * with different context type
+ */
+__weak int subprog_ctx_tag(void *ctx __arg_ctx)
+{
+	return bpf_get_stack(ctx, stack, sizeof(stack), 0);
+}
+
+struct my_struct { int x; };
+
+__weak int subprog_multi_ctx_tags(void *ctx1 __arg_ctx,
+				  struct my_struct *mem,
+				  void *ctx2 __arg_ctx)
+{
+	if (!mem)
+		return 0;
+
+	return bpf_get_stack(ctx1, stack, sizeof(stack), 0) +
+	       mem->x +
+	       bpf_get_stack(ctx2, stack, sizeof(stack), 0);
+}
+
+SEC("?raw_tp")
+__success __log_level(2)
+int arg_tag_ctx_raw_tp(void *ctx)
+{
+	struct my_struct x = { .x = 123 };
+
+	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
+}
+
+SEC("?perf_event")
+__success __log_level(2)
+int arg_tag_ctx_perf(void *ctx)
+{
+	struct my_struct x = { .x = 123 };
+
+	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
+}
+
+SEC("?kprobe")
+__success __log_level(2)
+int arg_tag_ctx_kprobe(void *ctx)
+{
+	struct my_struct x = { .x = 123 };
+
+	return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
+}