bpf: Allow void return type for global subprogs
Subsystems (4)
Emil Tsalapatis
reviewerv6 cover bpf: Allow void return type for global subprogs
Global subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type. Exception callbacks are excluded from this change, and still require a scalar return type.
Adding additional logic for void globals significantly complicates check_return_code(), mainly because of how the new feature interacts with bpf_throw(). To avoid complicating the code, refactor the return value range validation logic into two separate functions, one for program exits through BPF_EXIT and one for program exits through bpf_throw(). The changes are non-functional.
Also adjust existing selftests that were ensuring that global subprogs with void return types fail to verify. These tests should now load successfully.
Patches 1-3 refactor check_return_code into two separate functions, one for BPF_EXIT and one for bpf_throw(). No functional changes.
Patches 4-5 enable void globals in the verifier and add additional selftests.
v5 -> v6
(https://lore.kernel.org/bpf/[email protected]/)
Patch 1:
- Removed unused variables (kernel test robot)
- Add break; on empty default to suppress warning (kernel test robot)
- Added explanation on why change in switch statement for BPF_PROG_TYPE_TRACING in check_return_code is a no-op
Patches 2/3:
- Added From: tags to Eduard's patches and changed my Reviewed-by to Signed-off-by to properly denote patch route (submitting-patches.rst)
- Renamed check_subprogram_return_code for into check_global_subprog_return_code (Eduard)
Patch 4:
- Remove unnecessary subprog_is_global() check in check_return_code (Eduard)
- Fix indentation on patch 4 (AI)
Patch 5:
- Fix unused data in selftests (Eduard)
v4 -> v5
(https://lore.kernel.org/bpf/[email protected]/)
- Split value validation logic from throw/nonthrow to main prog/subprog (Eduard)
- Turn return_retval_range() to bool and incorporate return_32bit in retval (Eduard)
- Adjust if-else chain in return_retval_range for cgroup programs into switch statement to bring it in line with the other program types
v3 -> v4
(https://lore.kernel.org/bpf/[email protected]/)
- Factor out bpf_throw() return value validation logic into its own function (Eduard)
- Validate the range of bpf_throw() return values from void globals (Eduard)
- Move new selftest to verifier_global_subprogs.c (Eduard)
- Remove unnecessary verifier_bug_if() statements (Eduard)
- Add new test to ensure void globals can call bpf_throw
v2 -> v3
(https://lore.kernel.org/bpf/[email protected]/)
- Allow freplace for global programs that return void and add selftests (Kumar)
v1 -> v2
(https://lore.kernel.org/bpf/[email protected]/)
- Only mark R0 as valid for non-void global functions (AI)
- Rename subprog_is_void to subprog_returns_void (Kumar)
- Add test that R0 is invalid after calling a void global (Alexei)
- Add check to ensure exception callbacks do not return void and associated selftest
Eduard Zingerman (2):
bpf: Extract program_returns_void() for clarity
bpf: extract check_global_subprog_return_code() for clarity
Emil Tsalapatis (3):
bpf: Factor out program return value calculation
bpf: Allow void global functions in the verifier
selftests: bpf: Add tests for void global subprogs
include/linux/bpf_verifier.h | 1 + kernel/bpf/btf.c | 7 +- kernel/bpf/verifier.c | 349 ++++++++++++------ .../selftests/bpf/prog_tests/exceptions.c | 1 + .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 24 ++ .../testing/selftests/bpf/progs/exceptions.c | 14 + .../selftests/bpf/progs/exceptions_fail.c | 35 +- .../bpf/progs/freplace_int_with_void.c | 11 + .../selftests/bpf/progs/freplace_void.c | 10 + .../selftests/bpf/progs/test_global_func7.c | 2 +- .../bpf/progs/verifier_global_subprogs.c | 19 + 11 files changed, 344 insertions(+), 129 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/freplace_int_with_void.c create mode 100644 tools/testing/selftests/bpf/progs/freplace_void.c
Signature · 1 line
2.49.0
Emil Tsalapatis
reviewerPATCH v6 1/5 bpf: Factor out program return value calculation
Factor the return value range calculation logic in check_return_code out of the function in preparation for separating the return value validation logic for BPF_EXIT and bpf_throw().
No functional changes. The change made in return_retval_code's handling of PROG_TRACING program types (not error'ing on the default case) is a no-op because the match on the program attach type is exhaustive.
--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 226 +++++++++++++++++++---------------- 2 files changed, 126 insertions(+), 101 deletions(-)
include/linux/bpf_verifier.h +1 -0
@@ -265,6 +265,7 @@ struct bpf_reference_state {struct bpf_retval_range {s32 minval;s32 maxval; +bool return_32bit; };/* state of the program:
kernel/bpf/verifier.c +125 -101
281 lines not on this page · raw message
Signature · 1 line
2.49.0
Emil Tsalapatis
reviewerPATCH v6 2/5 bpf: Extract program_returns_void() for clarity
In reply to Emil TsalapatisFrom: Eduard Zingerman <[email protected]>
From: Eduard Zingerman <[email protected]>
The check_return_code function has explicit checks on whether a program type can return void. Factor this logic out to reuse it later for both main progs and subprogs.
--- kernel/bpf/verifier.c | 64 ++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 28 deletions(-)
kernel/bpf/verifier.c +36 -28
@@ -17912,6 +17912,28 @@ static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_return true; } +static bool program_returns_void (struct bpf_verifier_env *env) +{ +const struct bpf_prog *prog = env->prog; +enum bpf_prog_type prog_type =resolve_prog_type (prog); + +switch (prog_type) { +case BPF_PROG_TYPE_LSM : +/* See return_retval_range, for BPF_LSM_CGROUP can be 0 or 0-1 depending on hook. */ +if (prog->expected_attach_type !=BPF_LSM_CGROUP && + !prog->aux->attach_func_proto->type) +return true; +break ; +case BPF_PROG_TYPE_STRUCT_OPS : +if (!prog->aux->attach_func_proto->type) +return true; +break ; +default : +break ; + } +return false; +} +static int check_return_code (struct bpf_verifier_env *env,int regno,const char *reg_name) {const char *exit_ctx ="At program exit" ;
@@ -17927,35 +17949,21 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char/* LSM and struct_ops func-ptr's return type could be "void" */ if (!is_subprog || frame->in_exception_callback_fn) { -switch (prog_type) { -case BPF_PROG_TYPE_LSM : -if (prog->expected_attach_type ==BPF_LSM_CGROUP ) -/* See return_retval_range, can be 0 or 0-1 depending on hook. */ -break ; -if (!prog->aux->attach_func_proto->type) -return 0 ; -break ; -case BPF_PROG_TYPE_STRUCT_OPS : -if (!prog->aux->attach_func_proto->type) -return 0 ; - -if (frame->in_exception_callback_fn) -break ; +if (program_returns_void (env)) +return 0 ; + } -/* Allow a struct_ops program to return a referenced kptr if it -* matches the operator's return type and is in its unmodified -* form. A scalar zero (i.e., a null pointer) is also allowed. -*/ - reg_type = reg->btf ?btf_type_by_id (reg->btf, reg->btf_id) :NULL ; - ret_type =btf_type_resolve_ptr (prog->aux->attach_btf, - prog->aux->attach_func_proto->type, -NULL ); -if (ret_type && ret_type == reg_type && reg->ref_obj_id) -return __check_ptr_off_reg (env, reg, regno, false); -break ; -default : -break ; - } +if (!is_subprog && prog_type ==BPF_PROG_TYPE_STRUCT_OPS ) { +/* Allow a struct_ops program to return a referenced kptr if it +* matches the operator's return type and is in its unmodified +* form. A scalar zero (i.e., a null pointer) is also allowed. +*/ + reg_type = reg->btf ?btf_type_by_id (reg->btf, reg->btf_id) :NULL ; + ret_type =btf_type_resolve_ptr (prog->aux->attach_btf, + prog->aux->attach_func_proto->type, +NULL ); +if (ret_type && ret_type == reg_type && reg->ref_obj_id) +return __check_ptr_off_reg (env, reg, regno, false); }/* eBPF calling convention is such that R0 is used
Signature · 1 line
2.49.0
Emil Tsalapatis
reviewerPATCH v6 3/5 bpf: extract check_global_subprog_return_code() for clarity
In reply to Emil TsalapatisFrom: Eduard Zingerman <[email protected]>
From: Eduard Zingerman <[email protected]>
Both main progs and subprogs use the same function in the verifier, check_return_code, to verify the type and value range of the register being returned. However, subprogs only need a subset of the logic in check_return_code. this also goes the way - check_return_code explicitly checks whether it is handling a subprogram in multiple places, complicating the logic. Separate the handling of the two into separate functions.
--- kernel/bpf/verifier.c | 62 ++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 18 deletions(-)
kernel/bpf/verifier.c +44 -18
@@ -17943,17 +17943,14 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const charstruct bpf_retval_range range =retval_range (0 ,1 );enum bpf_prog_type prog_type =resolve_prog_type (env->prog);struct bpf_func_state *frame = env->cur_state->frame[0 ]; -const bool is_subprog = frame->subprogno;const struct btf_type *reg_type, *ret_type =NULL ;int err;/* LSM and struct_ops func-ptr's return type could be "void" */ -if (!is_subprog || frame->in_exception_callback_fn) { -if (program_returns_void (env)) -return 0 ; - } +if (!frame->in_async_callback_fn &&program_returns_void (env)) +return 0 ; -if (!is_subprog && prog_type ==BPF_PROG_TYPE_STRUCT_OPS ) { +if (prog_type ==BPF_PROG_TYPE_STRUCT_OPS ) {/* Allow a struct_ops program to return a referenced kptr if it * matches the operator's return type and is in its unmodified * form. A scalar zero (i.e., a null pointer) is also allowed.
@@ -17987,15 +17984,6 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char goto enforce_retval; } -if (is_subprog && !frame->in_exception_callback_fn) { -if (reg->type !=SCALAR_VALUE ) { -verbose (env,"At subprogram exit the register R%d is not a scalar value (%s)\n" , - regno,reg_type_str (env, reg->type)); -return -EINVAL ; - } -return 0 ; - } -if (prog_type ==BPF_PROG_TYPE_STRUCT_OPS && !ret_type)return 0 ;
@@ -18018,8 +18006,7 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const charif (!retval_range_within (range, reg)) {verbose_invalid_scalar (env, reg, range, exit_ctx, reg_name); -if (!is_subprog && - prog->expected_attach_type ==BPF_LSM_CGROUP && +if (prog->expected_attach_type ==BPF_LSM_CGROUP && prog_type ==BPF_PROG_TYPE_LSM && !prog->aux->attach_func_proto->type)verbose (env,"Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n" );
@@ -18032,6 +18019,29 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const charreturn 0 ; } +static int check_global_subprog_return_code (struct bpf_verifier_env *env) +{ +struct bpf_reg_state *reg =reg_state (env,BPF_REG_0 ); +int err; + + err =check_reg_arg (env,BPF_REG_0 ,SRC_OP ); +if (err) +return err; + +if (is_pointer_value (env,BPF_REG_0 )) { +verbose (env,"R%d leaks addr as return value\n" ,BPF_REG_0 ); +return -EACCES ; + } + +if (reg->type !=SCALAR_VALUE ) { +verbose (env,"At subprogram exit the register R0 is not a scalar value (%s)\n" , +reg_type_str (env, reg->type)); +return -EINVAL ; + } + +return 0 ; +} +static void mark_subprog_changes_pkt_data (struct bpf_verifier_env *env,int off) {struct bpf_subprog_info *subprog;
@@ -20825,6 +20835,8 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state, bool exception_exit) { +struct bpf_func_state *cur_frame =cur_func (env); +/* We must do check_reference_leak here before * prepare_func_exit to handle the case when * state->curframe > 0, it may be a callback function,
@@ -20858,7 +20870,21 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,return 0 ; } - err =check_return_code (env,BPF_REG_0 ,"R0" ); +/* +* Return from a regular global subprogram differs from return +* from the main program or async/exception callback. +* Main program exit implies return code restrictions +* that depend on program type. +* Exit from exception callback is equivalent to main program exit. +* Exit from async callback implies return code restrictions +* that depend on async scheduling mechanism. +*/ +if (cur_frame->subprogno && + !cur_frame->in_async_callback_fn && + !cur_frame->in_exception_callback_fn) + err =check_global_subprog_return_code (env); +else + err =check_return_code (env,BPF_REG_0 ,"R0" );if (err)return err;return PROCESS_BPF_EXIT ;
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH bpf-next v6 3/5] bpf: extract check_global_subprog_return_code() for clarity
check_return_code() has two callers: process_bpf_exit_full() and check_kfunc_call(). The new dispatch in process_bpf_exit_full() correctly routes global subprogs to the new function, but…
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c static int check_return_code(...) · line 17997
Quoted 12 lines
kernel/bpf/verifier.c +0
-8@@ -17987,15 +17984,6 @@ static int check_return_code(...) goto enforce_retval; } -if (is_subprog && !frame->in_exception_callback_fn) { -if (reg->type !=SCALAR_VALUE ) { -verbose (env,"At subprogram exit the register R%d is not a scalar value (%s)\n" , - regno,reg_type_str (env, reg->type)); -return -EINVAL ; - } -return 0 ; - }
check_return_code() has two callers: process_bpf_exit_full() and check_kfunc_call(). The new dispatch in process_bpf_exit_full() correctly routes global subprogs to the new function, but check_kfunc_call() still calls check_return_code() directly when validating bpf_throw() arguments:
check_kfunc_call() {
...
if (meta.func_id == special_kfunc_list[KF_bpf_throw]) {
...
if (!env->exception_callback_subprog) {
err = check_return_code(env, BPF_REG_1, "R1");When verifying a global subprog (frame[0]->subprogno != 0), the removed block would have accepted any scalar R1 and returned early. With it gone, R1 now falls through to full return value range validation.
Could this reject previously accepted programs? For instance, in a struct_ops program where return_retval_range() returns (0, 0), calling bpf_throw(1) from a global subprog would now fail verification where it previously succeeded.
The cover letter for this series indicates patches 4-5 may complete the bpf_throw refactoring, but they are outside the provided range.
[ ... ]
Quoted 4 lines
kernel/bpf/verifier.c +0
-0Both main progs and subprogs use the same function in the verifier,
check_return_code, to verify the type and value range of the register being returned. However, subprogs only need a subset of the logic incheck_return_code. this also goes the way -check_return_codeexplicitly
"this also goes the way" -- should this read "this also goes the other way"?
Quoted 2 lines
checks whether it is handling a subprogram in multiple places, complicating the logic. Separate the handling of the two into separate functions.
---
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/22527094257
AI-authorship-score: low
AI-authorship-explanation: Grammar error in commit message and iterative human review process across v1-v6 suggest human authorship, though the cover letter notes AI assistance on a different patch in the series.
issues-found: 2
issue-severity-score: low
issue-severity-explanation: Behavioral change in check_kfunc_call bpf_throw path could reject previously accepted programs, but the impact is limited to an unusual code pattern (global subprogs calling bpf_throw without exception callback) and is likely addressed in subsequent patches outside the provided range.
Emil Tsalapatis
reviewerRe: [PATCH bpf-next v6 3/5] bpf: extract check_global_subprog_return_code() for clarity
Quoted 38 lines · bot+bpf-ci
Quoted 4 lines
kernel/bpf/verifier.c +0
-0[ ... ]
kernel/bpf/verifier.c static int check_return_code(...) · line 17997
Quoted 12 lines
kernel/bpf/verifier.c +0
-8@@ -17987,15 +17984,6 @@ static int check_return_code(...) goto enforce_retval; } -if (is_subprog && !frame->in_exception_callback_fn) { -if (reg->type !=SCALAR_VALUE ) { -verbose (env,"At subprogram exit the register R%d is not a scalar value (%s)\n" , - regno,reg_type_str (env, reg->type)); -return -EINVAL ; - } -return 0 ; - }
check_return_code()has two callers:process_bpf_exit_full()andcheck_kfunc_call(). The new dispatch inprocess_bpf_exit_full()correctly routes global subprogs to the new function, butcheck_kfunc_call()still callscheck_return_code()directly when validatingbpf_throw()arguments:check_kfunc_call() { ... if (meta.func_id == special_kfunc_list[KF_bpf_throw]) { ... if (!env->exception_callback_subprog) { err = check_return_code(env, BPF_REG_1, "R1");When verifying a global subprog (frame[0]->subprogno != 0), the removed block would have accepted any scalar R1 and returned early. With it gone, R1 now falls through to full return value range validation.
Quoted 5 lines
Could this reject previously accepted programs? For instance, in a
struct_opsprogram wherereturn_retval_range()returns (0, 0), callingbpf_throw(1)from a global subprog would now fail verification where it previously succeeded.
This is accurate, but this change of behavior is actually a bugfix, not a regression. Eduard, can you confirm?
Quoted 6 lines
The cover letter for this series indicates patches 4-5 may complete the
bpf_throwrefactoring, but they are outside the provided range.[ ... ]
Quoted 8 lines
Quoted 4 lines
kernel/bpf/verifier.c +0
-0Both main progs and subprogs use the same function in the verifier,
check_return_code, to verify the type and value range of the register being returned. However, subprogs only need a subset of the logic incheck_return_code. this also goes the way -check_return_codeexplicitly"this also goes the way" -- should this read "this also goes the other way"?
Ack, this is a typo (my typo, to be exact). I can fix it in a next version if necessary.
Quoted 14 lines
Quoted 2 lines
checks whether it is handling a subprogram in multiple places, complicating the logic. Separate the handling of the two into separate functions.
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/22527094257
AI-authorship-score: low
AI-authorship-explanation: Grammar error in commit message and iterative human review process across v1-v6 suggest human authorship, though the cover letter notes AI assistance on a different patch in the series.
issues-found: 2
issue-severity-score: low
issue-severity-explanation: Behavioral change incheck_kfunc_callbpf_throwpath could reject previously accepted programs, but the impact is limited to an unusual code pattern (global subprogs callingbpf_throwwithout exception callback) and is likely addressed in subsequent patches outside the provided range.
Eduard Zingerman
maintainerRe: [PATCH bpf-next v6 3/5] bpf: extract check_global_subprog_return_code() for clarity
Quoted 40 lines · Emil Tsalapatis
Quoted 39 lines · bot+bpf-ci
Quoted 4 lines
kernel/bpf/verifier.c +0
-0[ ... ]
kernel/bpf/verifier.c static int check_return_code(...) · line 17998
Quoted 13 lines
kernel/bpf/verifier.c +0
-8@@ -17987,15 +17984,6 @@ static int check_return_code(...) goto enforce_retval; } -if (is_subprog && !frame->in_exception_callback_fn) { -if (reg->type !=SCALAR_VALUE ) { -verbose (env,"At subprogram exit the register R%d is not a scalar value (%s)\n", - regno,reg_type_str (env, reg->type)); -return -EINVAL ; - } -return 0 ; - }
check_return_code()has two callers:process_bpf_exit_full()andcheck_kfunc_call(). The new dispatch inprocess_bpf_exit_full()correctly routes global subprogs to the new function, butcheck_kfunc_call()still callscheck_return_code()directly when validatingbpf_throw()arguments:
check_kfunc_call(){
...
if (meta.func_id == special_kfunc_list[KF_bpf_throw]) {
...
if (!env->exception_callback_subprog) { err =check_return_code(env, BPF_REG_1, "R1");When verifying a global subprog (frame[0]->subprogno != 0), the removed block would have accepted any scalar R1 and returned early. With it gone, R1 now falls through to full return value range validation.
Quoted 8 lines
Quoted 5 lines
Could this reject previously accepted programs? For instance, in a
struct_opsprogram wherereturn_retval_range()returns (0, 0), callingbpf_throw(1)from a global subprog would now fail verification where it previously succeeded.This is accurate, but this change of behavior is actually a bugfix, not a regression. Eduard, can you confirm?
Yes, given the documented bpf_throw() behaviour the returned value should go through the same checks as main program exit.
I should have noted this change in the commit log, but I think I did not realize there is a behaviour change.
Quoted 37 lines
Quoted 6 lines
The cover letter for this series indicates patches 4-5 may complete the
bpf_throwrefactoring, but they are outside the provided range.[ ... ]
Quoted 8 lines
Quoted 4 lines
kernel/bpf/verifier.c +0
-0Both main progs and subprogs use the same function in the verifier,
check_return_code, to verify the type and value range of the register being returned. However, subprogs only need a subset of the logic incheck_return_code. this also goes the way -check_return_codeexplicitly"this also goes the way" -- should this read "this also goes the other way"?
Ack, this is a typo (my typo, to be exact). I can fix it in a next version if necessary.
Quoted 19 lines
Quoted 2 lines
checks whether it is handling a subprogram in multiple places, complicating the logic. Separate the handling of the two into separate functions.
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/22527094257
AI-authorship-score: low
AI-authorship-explanation: Grammar error in commit message and iterative human review process across v1-v6 suggest human authorship, though the cover letter notes AI assistance on a different patch in the series.
issues-found: 2
issue-severity-score: low
issue-severity-explanation: Behavioral change incheck_kfunc_callbpf_throwpath could reject previously accepted programs, but the impact is limited to an unusual code pattern (global subprogs callingbpf_throwwithout exception callback) and is likely addressed in subsequent patches outside the provided range.
Emil Tsalapatis
reviewerPATCH v6 4/5 bpf: Allow void global functions in the verifier
In reply to Emil TsalapatisGlobal subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type.
--- kernel/bpf/btf.c | 7 ++- kernel/bpf/verifier.c | 61 ++++++++++++++++--- .../selftests/bpf/progs/exceptions_fail.c | 2 +- .../selftests/bpf/progs/test_global_func7.c | 2 +- 4 files changed, 60 insertions(+), 12 deletions(-)
kernel/bpf/btf.c +4 -3
@@ -7836,15 +7836,16 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) tname, nargs, MAX_BPF_FUNC_REG_ARGS);return -EINVAL ; } -/* check that function returns int, exception cb also requires this */ +/* check that function is void or returns int, exception cb also requires this */ t =btf_type_by_id (btf, t->type);while (btf_type_is_modifier (t)) t =btf_type_by_id (btf, t->type); -if (!btf_type_is_int (t) && !btf_is_any_enum (t)) { +if (!btf_type_is_void (t) && !btf_type_is_int (t) && !btf_is_any_enum (t)) {if (!is_global)return -EINVAL ;bpf_log (log, -"Global function %s() doesn't return scalar. Only those are supported.\n" , +"Global function %s() return value not void or scalar. " +"Only those are supported.\n" , tname);return -EINVAL ; }
kernel/bpf/verifier.c +54 -7
100 lines not on this page · raw message
tools/testing/selftests/bpf/progs/exceptions_fail.c +1 -1
@@ -51,7 +51,7 @@ __noinline int exception_cb_ok_arg_small(int a) SEC("?tc") __exception_cb(exception_cb_bad_ret_type) -__failure __msg("Global function exception_cb_bad_ret_type() doesn't return scalar.") +__failure __msg("Global function exception_cb_bad_ret_type() return value not void or scalar.")int reject_exception_cb_type_1 (struct __sk_buff *ctx) {bpf_throw (0 );
tools/testing/selftests/bpf/progs/test_global_func7.c +1 -1
@@ -12,7 +12,7 @@ void foo(struct __sk_buff *skb) } SEC("tc") -__failure __msg("foo() doesn't return scalar") +__successint global_func7 (struct __sk_buff *skb) {foo (skb);
Signature · 1 line
2.49.0
Emil Tsalapatis
reviewerPATCH v6 5/5 selftests: bpf: Add tests for void global subprogs
In reply to Emil TsalapatisAdd additional testing for void global functions. The tests ensure that calls to void global functions properly keep R0 invalid. Also make sure that exception callbacks still require a return value.
--- .../selftests/bpf/prog_tests/exceptions.c | 1 + .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 24 +++++++++++++ .../testing/selftests/bpf/progs/exceptions.c | 14 ++++++++ .../selftests/bpf/progs/exceptions_fail.c | 35 +++++++++++++++++-- .../bpf/progs/freplace_int_with_void.c | 11 ++++++ .../selftests/bpf/progs/freplace_void.c | 10 ++++++ .../bpf/progs/verifier_global_subprogs.c | 19 ++++++++++ 7 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/freplace_int_with_void.c create mode 100644 tools/testing/selftests/bpf/progs/freplace_void.c
tools/testing/selftests/bpf/prog_tests/exceptions.c +1 -0
@@ -83,6 +83,7 @@ static void test_exceptions_success(void)RUN_SUCCESS (exception_assert_range_with,1 );RUN_SUCCESS (exception_bad_assert_range,0 );RUN_SUCCESS (exception_bad_assert_range_with,10 ); +RUN_SUCCESS (exception_throw_from_void_global,11 );#define RUN_EXT (load_ret, attach_err, expr, msg, after_link) \ { \
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +24 -0
@@ -347,6 +347,17 @@ static void test_func_sockmap_update(void) prog_name, false, NULL); } +static void test_func_replace_void (void ) +{ +const char *prog_name[] = { +"freplace/foo" , + }; +test_fexit_bpf2bpf_common ("./freplace_void.bpf.o" , +"./test_global_func7.bpf.o" , +ARRAY_SIZE (prog_name), + prog_name, false,NULL ); +} +static void test_obj_load_failure_common (const char *obj_file,const char *target_obj_file,const char *exp_msg)
@@ -432,6 +443,15 @@ static void test_func_replace_global_func(void) prog_name, false, NULL); } +static void test_func_replace_int_with_void (void ) +{ +/* Make sure we can't freplace with the wrong type */ +test_obj_load_failure_common ("freplace_int_with_void.bpf.o" , +"./test_global_func2.bpf.o" , +"Return type UNKNOWN of test_freplace_int_with_void()" +" doesn't match type INT of global_func2()" ); +} +static int find_prog_btf_id (const char *name,__u32 attach_prog_fd) {struct bpf_prog_info info = {};
@@ -597,4 +617,8 @@ void serial_test_fexit_bpf2bpf(void)test_fentry_to_cgroup_bpf ();if (test__start_subtest ("func_replace_progmap" ))test_func_replace_progmap (); +if (test__start_subtest ("freplace_int_with_void" )) +test_func_replace_int_with_void (); +if (test__start_subtest ("freplace_void" )) +test_func_replace_void (); }
tools/testing/selftests/bpf/progs/exceptions.c +14 -0
@@ -109,6 +109,20 @@ int exception_tail_call(struct __sk_buff *ctx) {return ret +8 ; } +__weak +void throw_11 (void ) +{ +bpf_throw (11 ); +} + +SEC("tc") +int exception_throw_from_void_global (struct __sk_buff *ctx) +{ +throw_11 (); + +return 0 ; +} + __noinline int exception_ext_global(struct __sk_buff *ctx) {volatile int ret =0 ;
tools/testing/selftests/bpf/progs/exceptions_fail.c +32 -3
@@ -29,11 +29,15 @@ struct { private(A) struct bpf_spin_lock lock; private(A) struct bpf_rb_root rbtree __contains(foo, node); -__noinline void *exception_cb_bad_ret_type(u64 cookie) +__noinline void *exception_cb_bad_ret_type1(u64 cookie) {return NULL ; } +__noinline void exception_cb_bad_ret_type2(u64 cookie) +{ +} + __noinline int exception_cb_bad_arg_0(void) {return 0 ;
@@ -50,8 +54,8 @@ __noinline int exception_cb_ok_arg_small(int a) } SEC("?tc") -__exception_cb (exception_cb_bad_ret_type) -__failure __msg("Global function exception_cb_bad_ret_type() return value not void or scalar.") +__exception_cb (exception_cb_bad_ret_type1) +__failure __msg("Global function exception_cb_bad_ret_type1() return value not void or scalar.")int reject_exception_cb_type_1 (struct __sk_buff *ctx) {bpf_throw (0 );
@@ -85,6 +89,15 @@ int reject_exception_cb_type_4(struct __sk_buff *ctx)return 0 ; } +SEC("?tc") +__exception_cb (exception_cb_bad_ret_type2) +__failure __msg("exception cb cannot return void") +int reject_exception_cb_type_5 (struct __sk_buff *ctx) +{ +bpf_throw (0 ); +return 0 ; +} +__noinline static int timer_cb(void *map, int *key, struct bpf_timer *timer) {
@@ -346,4 +359,20 @@ int reject_exception_throw_cb_diff(struct __sk_buff *ctx)return 0 ; } +__weak +void foo (void ) +{ +bpf_throw (1 ); +} + +SEC("?fentry/bpf_check") +__failure __msg("At program exit the register R1 has smin=1 smax=1 should") +int reject_out_of_range_global_throw(struct __sk_buff *skb) +{ +foo (); + +return 0 ; +} + + char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/freplace_int_with_void.c new file +11 -0
@@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <linux/pkt_cls.h> +#include <bpf/bpf_helpers.h> + +SEC("freplace/global_func2") +void test_freplace_int_with_void (struct __sk_buff *skb) +{ +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/freplace_void.c new file +10 -0
@@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +SEC("freplace/foo") +void test_freplace_void (struct __sk_buff *skb) +{ +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c +19 -0
@@ -388,4 +388,23 @@ int arg_tag_dynptr(struct xdp_md *ctx)return subprog_dynptr (&dptr); } +__weak +void foo (void ) +{ +} + +SEC("?tc") +__failure __msg("R0 !read_ok") +int return_from_void_global(struct __sk_buff *skb) +{ +foo (); + + asmvolatile ( +"r1 = r0;" + ::: + ); + +return 0 ; +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.49.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 5 lines · On Sat, 28 Feb 2026 13:47:54 -0500 you wrote:
Global subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type. Exception callbacks are excluded from this change, and still require a scalar return type.
Quoted 8 lines
Adding additional logic for void globals significantly complicates
check_return_code(), mainly because of how the new feature interacts withbpf_throw(). To avoid complicating the code, refactor the return value range validation logic into two separate functions, one for program exits throughBPF_EXITand one for program exits throughbpf_throw(). The changes are non-functional.[...]
Here is the summary with links:
- [bpf-next,v6,1/5] bpf: Factor out program return value calculation
https://git.kernel.org/bpf/bpf-next/c/83419c8fdbbc - [bpf-next,v6,2/5] bpf: Extract program_returns_void() for clarity https://git.kernel.org/bpf/bpf-next/c/63ec29623943 - [bpf-next,v6,3/5] bpf: extract check_global_subprog_return_code() for clarity https://git.kernel.org/bpf/bpf-next/c/69ca55e63101 - [bpf-next,v6,4/5] bpf: Allow void global functions in the verifier https://git.kernel.org/bpf/bpf-next/c/8446ded1e1a0 - [bpf-next,v6,5/5] selftests: bpf: Add tests for void global subprogs https://git.kernel.org/bpf/bpf-next/c/422f1efabd13
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html