From: Alexei Starovoitov analyze_subprog() follows each chain of calls that passes a pointer to the stack, and calls itself for each frame of the chain. Its own frame has callee_args[], with one entry for each register and each stack argument. "bpf: Allow 24 arguments of static subprogs" makes the array larger. Move the array out of the frame. bpf_compute_subprog_arg_access() allocates MAX_AT_TRACK_REGS entries for each depth one time, and frees them at its end. compute_subprog_args() has two more arrays of this kind, at_out[] and at_stack_arg_entry[]. It has one caller, thus a compiler can inline it into analyze_subprog(). Mark it noinline_for_stack. With clang 18 on arm64 and 24 arguments the frame of analyze_subprog() is 256 bytes and not 944 bytes. The 16 frames of the longest chain take 4K of the 16K of the kernel stack and not 14.75K. Signed-off-by: Alexei Starovoitov --- kernel/bpf/liveness.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c index f51445ead384..bffa490318ce 100644 --- a/kernel/bpf/liveness.c +++ b/kernel/bpf/liveness.c @@ -70,10 +70,14 @@ struct live_stack_query { u32 insn_idx; }; +struct arg_track; + struct bpf_liveness { DECLARE_HASHTABLE(func_instances, 8); /* maps (depth, callsite) to func_instance */ struct live_stack_query live_stack_query; /* cache to avoid repetitive ht lookups */ u32 subprog_calls; /* analyze_subprog() invocations */ + /* MAX_AT_TRACK_REGS for every depth: analyze_subprog() calls itself, keep its frame small */ + struct arg_track *callee_args; }; /* @@ -838,7 +842,7 @@ struct spill_snapshot { /* * Combined register + stack arg tracking: R0-R10 at indices 0-10, - * outgoing stack arg slots at indices MAX_BPF_REG..MAX_BPF_REG+6. + * outgoing stack arg slots at indices MAX_BPF_REG..MAX_AT_TRACK_REGS-1. */ #define MAX_AT_TRACK_REGS (MAX_BPF_REG + MAX_STACK_ARG_SLOTS) @@ -1970,12 +1974,14 @@ static void record_stack_ptrs(struct bpf_verifier_env *env, int idx, struct arg_ * @callee_entry: pre-populated entry state for R1-R5 and stack args * NULL for main (subprog 0). * @info: stores at_in, len for debug printing. + * + * Not inlined: at_out[] and at_stack_arg_entry[] would be in each frame of analyze_subprog(). */ -static int compute_subprog_args(struct bpf_verifier_env *env, - struct subprog_at_info *info, - struct arg_track *callee_entry, - struct func_instance *instance, - u32 *callsites) +static noinline_for_stack int compute_subprog_args(struct bpf_verifier_env *env, + struct subprog_at_info *info, + struct arg_track *callee_entry, + struct func_instance *instance, + u32 *callsites) { int subprog = instance->subprog; struct bpf_insn *insns = env->prog->insnsi; @@ -2308,7 +2314,8 @@ static int analyze_subprog(struct bpf_verifier_env *env, /* For each reachable call site in the subprog, recurse into callees */ for (int p = po_start; p < po_end; p++) { int idx = env->cfg.insn_postorder[p]; - struct arg_track callee_args[MAX_AT_TRACK_REGS] = {}; + struct arg_track *callee_args = env->liveness->callee_args + + depth * MAX_AT_TRACK_REGS; struct arg_track none = { .frame = ARG_NONE }; struct bpf_insn *insn = &insns[idx]; struct func_instance *callee_instance; @@ -2316,6 +2323,7 @@ static int analyze_subprog(struct bpf_verifier_env *env, int caller_reg, cb_callee_reg; j = idx - start; /* relative index within this subprog */ + memset(callee_args, 0, MAX_AT_TRACK_REGS * sizeof(*callee_args)); if (bpf_pseudo_call(insn)) { target = idx + insn->imm + 1; @@ -2438,9 +2446,13 @@ int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env) env->callsite_at_stack = kvzalloc_objs(*env->callsite_at_stack, insn_cnt, GFP_KERNEL_ACCOUNT); - if (!env->callsite_at_stack) { - kvfree(info); - return -ENOMEM; + env->liveness->callee_args = kvcalloc(MAX_CALL_FRAMES * MAX_AT_TRACK_REGS, + sizeof(*env->liveness->callee_args), + GFP_KERNEL_ACCOUNT); + if (!env->callsite_at_stack || !env->liveness->callee_args) { + err = -ENOMEM; + insn_cnt = env->callsite_at_stack ? insn_cnt : 0; + goto out; } /* @@ -2478,6 +2490,8 @@ int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env) kvfree(env->callsite_at_stack[k]); kvfree(env->callsite_at_stack); env->callsite_at_stack = NULL; + kvfree(env->liveness->callee_args); + env->liveness->callee_args = NULL; for (k = 0; k < env->subprog_cnt; k++) kvfree(info[k].at_in); kvfree(info); -- 2.55.0