bpf: Add multi-level pointer parameter support for trampolines
Subsystems (5)
v5 cover bpf: Add multi-level pointer parameter support for trampolines
This is v5 of a series adding support for new pointer types for trampoline parameters.
Originally, only support for multi-level pointers was proposed. As suggested during review, it was extended to single-level pointers. During discussion, it was proposed to add support for any single or multi-level pointer type that is not a single-level pointer to a structure, with the condition if (!btf_type_is_struct_ptr(t)). The safety of this condition is based on BTF data verification performed for modules and programs, and vmlinux BTF being trusted to not contain invalid types, so it is not possible for invalid types, like PTR->DATASEC, PTR->FUNC, PTR->VAR and corresponding multi-level pointers, to reach btf_ctx_access.
These changes appear to be a safe extension since any future support for arrays and output values would require annotation (similar to Microsoft SAL), which differentiates between current unannotated scalar cases and new annotated cases.
This series adds BPF verifier support for single- and multi-level pointer parameters and return values in BPF trampolines. The implementation treats these parameters as SCALAR_VALUE.
This is consistent with existing pointers to int and void that are already treated as SCALAR.
This provides consistent logic for single- and multi-level pointers:
if the type is treated as SCALAR for a single-level pointer, the same applies to multi-level pointers, except for pointers to structs which are currently PTR_TO_BTF_ID. However, in the case of multi-level pointers, they are treated as scalar since the verifier lacks the context to infer the size of their target memory regions.
Background:
Prior to these changes, accessing multi-level pointer parameters or return values through BPF trampoline context arrays resulted in verification failures in btf_ctx_access, producing errors such as:
func '%s' arg%d type %s is not a struct
For example, consider a BPF program that logs an input parameter of type struct posix_acl **:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
bpf_printk("__posix_acl_chmod ppacl = %px\n", ppacl);
return 0;
}This program failed BPF verification with the following error:
libbpf: prog 'trace_posix_acl_chmod': -- BEGIN PROG LOAD LOG -- 0: R1=ctx() R10=fp0
; int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp, umode_t mode) @ posix_acl_monitor.bpf.c:23
0: (79) r6 = *(u64 *)(r1 +16) ; R1=ctx() R6_w=scalar()
1: (79) r1 = *(u64 *)(r1 +0)
func '__posix_acl_chmod' arg0 type PTR is not a struct invalid bpf_context access off=0 size=8
processed 2 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --
The common workaround involved using helper functions to fetch parameter values by passing the address of the context array entry:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
struct posix_acl **pp;
bpf_probe_read_kernel(&pp, sizeof(ppacl), &ctx[0]);
bpf_printk("__posix_acl_chmod %px\n", pp);
return 0;
}This approach introduced helper call overhead and created inconsistency with parameter access patterns.
Improvements:
With this patch, trampoline programs can directly access multi-level pointer parameters, eliminating helper call overhead and explicit ctx access while ensuring consistent parameter handling. For example, the following ctx access with a helper call:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
struct posix_acl **pp;
bpf_probe_read_kernel(&pp, sizeof(pp), &ctx[0]);
bpf_printk("__posix_acl_chmod %px\n", pp);
...
}is replaced by a load instruction:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
bpf_printk("__posix_acl_chmod %px\n", ppacl);
...
}The bpf_core_cast macro can be used for deeper level dereferences.
v1 -> v2:
* corrected maintainer's email
v2 -> v3:
* Addressed reviewers' feedback:
* Changed the register type from PTR_TO_MEM to SCALAR_VALUE.
* Modified tests to accommodate SCALAR_VALUE handling.
* Fixed a compilation error for loongarch
* https://lore.kernel.org/oe-kbuild-all/[email protected]/
* Addressed AI bot review
* Added a commentary to address a NULL pointer case
* Removed WARN_ON
* Fixed a commentary
v3 -> v4:
* Added more consistent support for single and multi-level pointers
as suggested by reviewers.
* added single level pointers to enum 32 and 64
* added single level pointers to functions
* harmonized support for single and multi-level pointer types
* added new tests to support the above changes
* Removed create_bad_kaddr that allocated and invalidated kernel VA for tests, and replaced it with hardcoded values similar to bpf_testmod_return_ptr as suggested by reviewers.
v4 -> v5:
* As suggested, extended support to single-level pointers and
covered all supported valid pointer (single- and multi-level) types with a wider condition if (!btf_type_is_struct_ptr(t)).
* As requested, simplified tests by keeping only tests that check
the verifier log for scalar().
Slava Imameev (2):
bpf: Support pointer param types via SCALAR_VALUE for trampolines selftests/bpf: Add trampolines single and multi-level pointer params
test coverage
kernel/bpf/btf.c | 17 ++--- net/bpf/test_run.c | 17 +++++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_ctx_ptr_param.c | 68 +++++++++++++++++++ 4 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c
Signature · 1 line
2.34.1
PATCH v5 1/2 bpf: Support pointer param types via SCALAR_VALUE for trampolines
Add BPF verifier support for single- and multi-level pointer parameters and return values in BPF trampolines by treating these parameters as SCALAR_VALUE.
This extends the existing support for int and void pointers that are already treated as SCALAR_VALUE.
This provides consistent logic for single and multi-level pointers:
if a type is treated as SCALAR for a single-level pointer, the same applies to multi-level pointers. The exception is pointer-to-struct, which is currently PTR_TO_BTF_ID for single-level but treated as scalar for multi-level pointers since the verifier lacks context to infer the size of target memory regions.
Safety is ensured by existing BTF verification, which rejects invalid pointer types at the BTF verification stage.
--- kernel/bpf/btf.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
kernel/bpf/btf.c +7 -10
@@ -6508,13 +6508,6 @@ struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)return prog->aux->attach_btf; } -static bool is_void_or_int_ptr (struct btf *btf,const struct btf_type *t) -{ -/* skip modifiers */ - t =btf_type_skip_modifiers (btf, t->type,NULL ); -return btf_type_is_void (t) ||btf_type_is_int (t); -} -u32 btf_ctx_arg_idx (struct btf *btf,const struct btf_type *func_proto,int off) {
@@ -6903,10 +6896,14 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, }/* -* If it's a pointer to void, it's the same as scalar from the verifier -* safety POV. Either way, no futher pointer walking is allowed. +* If it's a single or multilevel pointer, except a pointer +* to a structure, it's the same as scalar from the verifier +* safety POV. Multilevel pointers to structures are treated as +* scalars. The verifier lacks the context to infer the size of +* their target memory regions. Either way, no further pointer +* walking is allowed. */ -if (is_void_or_int_ptr (btf, t)) +if (!btf_type_is_struct_ptr (btf, t))return true;/* this is a pointer to another type */
Signature · 1 line
2.34.1
PATCH v5 2/2 selftests/bpf: Add trampolines single and multi-level pointer params test coverage
In reply to Slava ImameevAdd single and multi-level pointer parameters and return value test coverage for BPF trampolines. Includes verifier tests for single and multi-level pointers. The tests check verifier logs for pointers inferred as scalar() type.
--- net/bpf/test_run.c | 17 +++++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_ctx_ptr_param.c | 68 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c
net/bpf/test_run.c +17 -0
@@ -567,6 +567,23 @@ noinline void bpf_fentry_test_sinfo(struct skb_shared_info *sinfo) { } +noinline void bpf_fentry_test_ppvoid(void **pp) +{ +} + +noinline void bpf_fentry_test_pppvoid(void ***ppp) +{ +} + +noinline void bpf_fentry_test_ppfile(struct file **ppf) +{ +} + +noinline struct file **bpf_fexit_test_ret_ppfile(void) +{ +return (struct file **)NULL ; +} + __bpf_kfunc int bpf_modify_return_test(int a, int *b) { *b +=1 ;
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -115,6 +115,7 @@#include "verifier_lsm.skel.h" #include "verifier_jit_inline.skel.h" #include "irq.skel.h" +#include "verifier_ctx_ptr_param.skel.h" #define MAX_ENTRIES 11
@@ -259,6 +260,7 @@ void test_verifier_lsm(void) { RUN(verifier_lsm); }void test_irq (void ) {RUN (irq); }void test_verifier_mtu (void ) {RUN (verifier_mtu); }void test_verifier_jit_inline (void ) {RUN (verifier_jit_inline); } +void test_verifier_ctx_ptr_param (void ) {RUN (verifier_ctx_ptr_param); }static int init_test_val_map (struct bpf_object *obj,char *map_name) {
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +68 -0
68 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH bpf-next v5 2/2] selftests/bpf: Add trampolines single and multi-level pointer params test coverage
The description says "struct file ***" (triple pointer), but bpf_fentry_test_ppfile() takes a struct file ** (double pointer):
Quoted 5 lines
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +0
-0
[ ... ]
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c line 41
Quoted 2 lines
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +2
-0+SEC("fentry/bpf_fentry_test_ppfile") +__description ("fentry/struct file***: struct file *** inferred as scalar" )
The description says "struct file ***" (triple pointer), but bpf_fentry_test_ppfile() takes a struct file ** (double pointer):
net/bpf/test_run.c:
noinline void bpf_fentry_test_ppfile(struct file **ppf)
Should the description be
"fentry/struct file**: struct file ** inferred as scalar" instead?
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c line 45
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +4
-0+__success __retval(0) +__log_level(2) +__msg("R1=ctx() R2=scalar()") +__naked void fentry_ppfile_as_scalar (void )
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/23052798110
Re: [PATCH bpf-next v5 2/2] selftests/bpf: Add trampolines single and multi-level pointer params test coverage
Quoted 15 lines · On Fri, Mar 13, 2026 at 6:55 AM <[email protected]> wrote:
Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +0
-0[ ... ]
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c line 41
Quoted 2 lines
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +2
-0+SEC("fentry/bpf_fentry_test_ppfile") +__description ("fentry/struct file***: struct file *** inferred as scalar" )The description says "struct file ***" (triple pointer), but
bpf_fentry_test_ppfile()takes a struct file ** (double pointer):
Quoted 5 lines
net/bpf/test_run.c:
noinline voidbpf_fentry_test_ppfile(struct file **ppf)Should the description be
"fentry/struct file**: struct file ** inferred as scalar" instead?
Pls don't ignore bot reviews...
Do you agree or not?
Re: [PATCH bpf-next v5 2/2] selftests/bpf: Add trampolines single and multi-level pointer params test coverage
Quoted 18 lines · On Sat, 14 Mar 2026 02:42:28 Alexei Starovoitov wrote:
Quoted 2 lines · On Fri, Mar 13, 2026 at 6:55=E2=80=AFAM <[email protected]> wrote:
Review
Quoted 2 lines
(quoted diff) +0
-0
b/tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.cQuoted 9 lines
Quoted 4 lines
new file mode 100644
index 000000000000..b507dc850543tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +0
-0[ ... ]
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c
Quoted 2 lines
tools/testing/selftests/bpf/progs/verifier_ctx_ptr_param.c new file +2
-0+SEC("fentry/bpf_fentry_test_ppfile") +__description("fentry/struct file***: struct file *** inferred as scal=ar")
Quoted 4 lines
The description says "struct file ***" (triple pointer), but
bpf_fentry_test_ppfile()takes a struct file ** (double pointer):
Quoted 8 lines
Quoted 1 line
net/bpf/test_run.c:noinline void
bpf_fentry_test_ppfile(struct file **ppf)Quoted 3 lines
Should the description be
"fentry/struct file**: struct file ** inferred as scalar" instead?Pls don't ignore bot reviews...
Do you agree or not?
I'm not ignoring the bot - I'm in Australia and pushed this patch late at night at 12:05 AM Saturday. The bot replied 50 minutes later at 12:55 AM Saturday, and I was already not at my desktop.
I will make the necessary changes to the test description. The bot is correct that it should have been "struct file **" - I mistakenly carried over the triple pointer description when copy-pasting the comments.