Skip to content

Commit 67fe459

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: add arg:ctx cases to test_global_funcs tests
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2f38fe6 commit 67fe459

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,52 @@ int perf_event_ctx(void *ctx)
102102
{
103103
return perf_event_ctx_subprog(ctx);
104104
}
105+
106+
/* this global subprog can be now called from many types of entry progs, each
107+
* with different context type
108+
*/
109+
__weak int subprog_ctx_tag(void *ctx __arg_ctx)
110+
{
111+
return bpf_get_stack(ctx, stack, sizeof(stack), 0);
112+
}
113+
114+
struct my_struct { int x; };
115+
116+
__weak int subprog_multi_ctx_tags(void *ctx1 __arg_ctx,
117+
struct my_struct *mem,
118+
void *ctx2 __arg_ctx)
119+
{
120+
if (!mem)
121+
return 0;
122+
123+
return bpf_get_stack(ctx1, stack, sizeof(stack), 0) +
124+
mem->x +
125+
bpf_get_stack(ctx2, stack, sizeof(stack), 0);
126+
}
127+
128+
SEC("?raw_tp")
129+
__success __log_level(2)
130+
int arg_tag_ctx_raw_tp(void *ctx)
131+
{
132+
struct my_struct x = { .x = 123 };
133+
134+
return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
135+
}
136+
137+
SEC("?perf_event")
138+
__success __log_level(2)
139+
int arg_tag_ctx_perf(void *ctx)
140+
{
141+
struct my_struct x = { .x = 123 };
142+
143+
return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
144+
}
145+
146+
SEC("?kprobe")
147+
__success __log_level(2)
148+
int arg_tag_ctx_kprobe(void *ctx)
149+
{
150+
struct my_struct x = { .x = 123 };
151+
152+
return subprog_ctx_tag(ctx) + subprog_multi_ctx_tags(ctx, &x, ctx);
153+
}

0 commit comments

Comments
 (0)