Factor the function arch_bpf_get_regs_nr() to get the regs count that used by the function args. Signed-off-by: Menglong Dong <dongml2@xxxxxxxxxxxxxxx> --- arch/x86/net/bpf_jit_comp.c | 22 +++++++++++++++------- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 5 +++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 8d2fc436a748..7795917efc41 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -3001,6 +3001,19 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog, return 0; } +int arch_bpf_get_regs_nr(const struct btf_func_model *m) +{ + int nr_regs = m->nr_args; + + /* extra registers for struct arguments */ + for (int i = 0; i < m->nr_args; i++) { + if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG) + nr_regs += (m->arg_size[i] + 7) / 8 - 1; + } + + return nr_regs; +} + /* mov rax, qword ptr [rbp - rounded_stack_depth - 8] */ #define LOAD_TRAMP_TAIL_CALL_CNT_PTR(stack) \ __LOAD_TCC_PTR(-round_up(stack, 8) - 8) @@ -3071,7 +3084,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im struct bpf_tramp_links *tlinks, void *func_addr) { - int i, ret, nr_regs = m->nr_args, stack_size = 0; + int i, ret, nr_regs, stack_size = 0; int regs_off, nregs_off, ip_off, run_ctx_off, arg_stack_off, rbx_off; struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY]; struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT]; @@ -3089,15 +3102,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im WARN_ON_ONCE((flags & BPF_TRAMP_F_INDIRECT) && (flags & ~(BPF_TRAMP_F_INDIRECT | BPF_TRAMP_F_RET_FENTRY_RET))); - /* extra registers for struct arguments */ - for (i = 0; i < m->nr_args; i++) { - if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG) - nr_regs += (m->arg_size[i] + 7) / 8 - 1; - } - /* x86-64 supports up to MAX_BPF_FUNC_ARGS arguments. 1-6 * are passed through regs, the remains are through stack. */ + nr_regs = arch_bpf_get_regs_nr(m); if (nr_regs > MAX_BPF_FUNC_ARGS) return -ENOTSUPP; diff --git a/include/linux/bpf.h b/include/linux/bpf.h index bb3ab1aa3a9d..da5951b0335b 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1220,6 +1220,7 @@ void arch_free_bpf_trampoline(void *image, unsigned int size); int __must_check arch_protect_bpf_trampoline(void *image, unsigned int size); int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, struct bpf_tramp_links *tlinks, void *func_addr); +int arch_bpf_get_regs_nr(const struct btf_func_model *m); u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d6311be5a63a..86a64d843465 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -23274,6 +23274,11 @@ static int do_check_main(struct bpf_verifier_env *env) } +int __weak arch_bpf_get_regs_nr(const struct btf_func_model *m) +{ + return -ENODEV; +} + static void print_verification_stats(struct bpf_verifier_env *env) { int i; -- 2.39.5