From: Alexei Starovoitov A BPF function takes 12 arguments at most: 5 in registers and 7 in stack slots. Rust needs more, because it passes a slice or a trait object in two slots. For a struct with 5 fields derive(Debug) calls core::fmt::Formatter::debug_struct_field5_finish(), which takes up to 23 slots. The limit of 12 is for calls in which the kernel takes part. Only BPF code calls a static subprog. Thus a static subprog can have a different limit: MAX_BPF_STATIC_FUNC_ARGS. callee arguments ------------------- ---------------------------------------- kfunc, trampoline 12, no change global subprog 5, no change static subprog 12 -> 24: 5 registers and 19 stack slots The verifier counts the stack arguments of a static subprog from its code, and JITs get the number of stack slots from the verifier. Thus JITs do not change. The field that counts these slots, and the mask of stack arguments in struct backtrack_state, get wider. BTF still describes 12 parameters at most. The verifier marks BTF of a static subprog with more parameters as unreliable and continues, as before. The test that stores behind the last slot now stores to slot 20: stack arg write offset -160 exceeds max 19 stack args Signed-off-by: Alexei Starovoitov --- include/linux/bpf.h | 15 +++++++++++---- include/linux/bpf_verifier.h | 6 +++--- kernel/bpf/backtrack.c | 2 +- kernel/bpf/verifier.c | 4 ++-- .../selftests/bpf/progs/verifier_stack_arg.c | 6 +++--- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index ca8992702dbd..7e022591dc23 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1008,13 +1008,20 @@ static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT); /* The longest tracepoint has 12 args. * See include/trace/bpf_probe.h * - * Also reuse this macro for maximum number of arguments a BPF function - * or a kfunc can have. Args 1-5 are passed in registers, args 6-12 via - * stack arg slots. The JIT may map some stack arg slots to registers based - * on the native calling convention (e.g., arg 6 to R9 on x86-64). + * Also reuse this macro for maximum number of arguments a kfunc can have and + * BTF of a BPF function can describe. Args 1-5 are passed in registers, args + * 6-12 via stack arg slots. The JIT may map some stack arg slots to registers + * based on the native calling convention (e.g., arg 6 to R9 on x86-64). */ #define MAX_BPF_FUNC_ARGS 12 +/* + * A static BPF function is called by BPF code only, with what the compiler + * makes of its arguments. A slice or a trait object of Rust takes two slots. The + * function that derive(Debug) calls for a struct with five fields takes 23. + */ +#define MAX_BPF_STATIC_FUNC_ARGS 24 + /* The maximum number of arguments passed through registers * a single function may have. */ diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index dfecdfac75ff..54d45407ac1d 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -487,7 +487,7 @@ static_assert(MAX_CALL_FRAMES <= (1 << 4)); static_assert(MAX_BPF_STACK_SLOTS <= (1 << 12)); /* Maximum number of bpf_reg_state objects that can exist at once */ -#define MAX_STACK_ARG_SLOTS (MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS) +#define MAX_STACK_ARG_SLOTS (MAX_BPF_STATIC_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS) #define BPF_ID_MAP_SIZE ((MAX_BPF_REG + MAX_BPF_STACK_SLOTS + MAX_STACK_ARG_SLOTS) * \ MAX_CALL_FRAMES) @@ -1007,7 +1007,7 @@ struct bpf_subprog_info { * a hidden pointer to the result, a pair in two registers, no dead arguments. */ bool stack_args_from_code: 1; - u8 code_stack_args:4; + u8 code_stack_args; u16 stack_arg_cnt; /* incoming + max outgoing */ u16 max_out_stack_arg_cnt; }; @@ -1029,7 +1029,7 @@ struct backtrack_state { u32 frame; u32 reg_masks[MAX_CALL_FRAMES]; unsigned long stack_masks[MAX_CALL_FRAMES][BITS_TO_LONGS(MAX_BPF_STACK_SLOTS)]; - u8 stack_arg_masks[MAX_CALL_FRAMES]; + u32 stack_arg_masks[MAX_CALL_FRAMES]; }; struct bpf_id_pair { diff --git a/kernel/bpf/backtrack.c b/kernel/bpf/backtrack.c index 2d6535f271a6..6d448f503de9 100644 --- a/kernel/bpf/backtrack.c +++ b/kernel/bpf/backtrack.c @@ -223,7 +223,7 @@ static inline unsigned long *bt_stack_mask(struct backtrack_state *bt) return bt_frame_stack_mask(bt, bt->frame); } -static inline u8 bt_stack_arg_mask(struct backtrack_state *bt) +static inline u32 bt_stack_arg_mask(struct backtrack_state *bt) { return bt->stack_arg_masks[bt->frame]; } diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d503dc7cea1f..563313809078 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -4633,7 +4633,7 @@ static int check_stack_write(struct bpf_verifier_env *env, static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_state *state, int off, struct bpf_reg_state *value_reg) { - int max_stack_arg_regs = MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS; + int max_stack_arg_regs = MAX_STACK_ARG_SLOTS; struct bpf_subprog_info *subprog = &env->subprog_info[state->subprogno]; int spi = -off / BPF_REG_SIZE - 1; struct bpf_reg_state *arg; @@ -15257,7 +15257,7 @@ static void prepare_subprog_args(struct bpf_verifier_env *env) cnt = max(cnt, insn->off / BPF_REG_SIZE); } sub->stack_args_from_code = true; - sub->code_stack_args = min(cnt, MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS); + sub->code_stack_args = min(cnt, MAX_STACK_ARG_SLOTS); /* incoming + max outgoing. Calls add the latter. */ sub->stack_arg_cnt = sub->code_stack_args; if (env->prog->aux->func_info_aux) diff --git a/tools/testing/selftests/bpf/progs/verifier_stack_arg.c b/tools/testing/selftests/bpf/progs/verifier_stack_arg.c index d1fd5a402565..c54a9b80ced4 100644 --- a/tools/testing/selftests/bpf/progs/verifier_stack_arg.c +++ b/tools/testing/selftests/bpf/progs/verifier_stack_arg.c @@ -443,7 +443,7 @@ __naked void stack_arg_shared_store(void) SEC("tc") __description("stack_arg: write beyond max outgoing depth") __failure -__msg("stack arg write offset -80 exceeds max 7 stack args") +__msg("stack arg write offset -160 exceeds max 19 stack args") __naked void stack_arg_write_beyond_max(void) { asm volatile ( @@ -452,8 +452,8 @@ __naked void stack_arg_write_beyond_max(void) "r3 = 3;" "r4 = 4;" "r5 = 5;" - /* Write to offset -80, way beyond any callee's needs */ - "*(u64 *)(r11 - 80) = 99;" + /* Write to offset -160, way beyond any callee's needs */ + "*(u64 *)(r11 - 160) = 99;" "*(u64 *)(r11 - 16) = 20;" "*(u64 *)(r11 - 8) = 10;" "call subprog_7args;" -- 2.55.0