From nobody Tue Sep 16 15:43:09 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92682C5479D for ; Tue, 3 Jan 2023 01:30:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232543AbjACBam (ORCPT ); Mon, 2 Jan 2023 20:30:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230080AbjACBai (ORCPT ); Mon, 2 Jan 2023 20:30:38 -0500 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C86DCCF4; Mon, 2 Jan 2023 17:30:36 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4NmFY31stQz4f3v4p; Tue, 3 Jan 2023 09:30:31 +0800 (CST) Received: from localhost.localdomain (unknown [10.67.175.61]) by APP3 (Coremail) with SMTP id _Ch0CgA3MR84hbNjzHakAw--.49033S2; Tue, 03 Jan 2023 09:30:33 +0800 (CST) From: Pu Lehui To: bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Pu Lehui , Pu Lehui Subject: [PATCH bpf-next] bpf, x86: Simplify the parsing logic of structure parameters Date: Tue, 3 Jan 2023 09:31:58 +0800 Message-Id: <20230103013158.1945869-1-pulehui@huaweicloud.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _Ch0CgA3M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xZFpf9x0J UdHUDUUUUU= X-CM-SenderInfo: psxovxtxl6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Pu Lehui Extra_nregs of structure parameters and nr_args can be added directly at the beginning, and using a flip flag to identifiy structure parameters. Meantime, renaming some variables to make them more sense. Signed-off-by: Pu Lehui Acked-by: Yonghong Song --- arch/x86/net/bpf_jit_comp.c | 99 +++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index e3e2b57e4e13..e7b72299f5a4 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1839,62 +1839,57 @@ st: if (is_imm8(insn->off)) return proglen; } =20 -static void save_regs(const struct btf_func_model *m, u8 **prog, int nr_ar= gs, +static void save_regs(const struct btf_func_model *m, u8 **prog, int nr_re= gs, int stack_size) { - int i, j, arg_size, nr_regs; + int i, j, arg_size; + bool is_struct =3D false; + /* Store function arguments to stack. * For a function that accepts two pointers the sequence will be: * mov QWORD PTR [rbp-0x10],rdi * mov QWORD PTR [rbp-0x8],rsi */ - for (i =3D 0, j =3D 0; i < min(nr_args, 6); i++) { - if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG) { - nr_regs =3D (m->arg_size[i] + 7) / 8; + for (i =3D 0, j =3D 0; i < min(nr_regs, 6); i++) { + arg_size =3D m->arg_size[j]; + if (arg_size > 8) { arg_size =3D 8; - } else { - nr_regs =3D 1; - arg_size =3D m->arg_size[i]; + is_struct ^=3D 1; } =20 - while (nr_regs) { - emit_stx(prog, bytes_to_bpf_size(arg_size), - BPF_REG_FP, - j =3D=3D 5 ? X86_REG_R9 : BPF_REG_1 + j, - -(stack_size - j * 8)); - nr_regs--; - j++; - } + emit_stx(prog, bytes_to_bpf_size(arg_size), + BPF_REG_FP, + i =3D=3D 5 ? X86_REG_R9 : BPF_REG_1 + i, + -(stack_size - i * 8)); + + j =3D is_struct ? j : j + 1; } } =20 -static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr= _args, +static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr= _regs, int stack_size) { - int i, j, arg_size, nr_regs; + int i, j, arg_size; + bool is_struct =3D false; =20 /* Restore function arguments from stack. * For a function that accepts two pointers the sequence will be: * EMIT4(0x48, 0x8B, 0x7D, 0xF0); mov rdi,QWORD PTR [rbp-0x10] * EMIT4(0x48, 0x8B, 0x75, 0xF8); mov rsi,QWORD PTR [rbp-0x8] */ - for (i =3D 0, j =3D 0; i < min(nr_args, 6); i++) { - if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG) { - nr_regs =3D (m->arg_size[i] + 7) / 8; + for (i =3D 0, j =3D 0; i < min(nr_regs, 6); i++) { + arg_size =3D m->arg_size[j]; + if (arg_size > 8) { arg_size =3D 8; - } else { - nr_regs =3D 1; - arg_size =3D m->arg_size[i]; + is_struct ^=3D 1; } =20 - while (nr_regs) { - emit_ldx(prog, bytes_to_bpf_size(arg_size), - j =3D=3D 5 ? X86_REG_R9 : BPF_REG_1 + j, - BPF_REG_FP, - -(stack_size - j * 8)); - nr_regs--; - j++; - } + emit_ldx(prog, bytes_to_bpf_size(arg_size), + i =3D=3D 5 ? X86_REG_R9 : BPF_REG_1 + i, + BPF_REG_FP, + -(stack_size - i * 8)); + + j =3D is_struct ? j : j + 1; } } =20 @@ -2120,8 +2115,8 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *image, void *i struct bpf_tramp_links *tlinks, void *func_addr) { - int ret, i, nr_args =3D m->nr_args, extra_nregs =3D 0; - int regs_off, ip_off, args_off, stack_size =3D nr_args * 8, run_ctx_off; + int i, ret, nr_regs =3D m->nr_args, stack_size =3D 0; + int regs_off, nregs_off, ip_off, run_ctx_off; struct bpf_tramp_links *fentry =3D &tlinks[BPF_TRAMP_FENTRY]; struct bpf_tramp_links *fexit =3D &tlinks[BPF_TRAMP_FEXIT]; struct bpf_tramp_links *fmod_ret =3D &tlinks[BPF_TRAMP_MODIFY_RETURN]; @@ -2130,17 +2125,14 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_im= age *im, void *image, void *i u8 *prog; bool save_ret; =20 - /* x86-64 supports up to 6 arguments. 7+ can be added in the future */ - if (nr_args > 6) - return -ENOTSUPP; - - for (i =3D 0; i < MAX_BPF_FUNC_ARGS; i++) { + /* extra registers for struct arguments */ + for (i =3D 0; i < m->nr_args; i++) if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG) - extra_nregs +=3D (m->arg_size[i] + 7) / 8 - 1; - } - if (nr_args + extra_nregs > 6) + nr_regs +=3D (m->arg_size[i] + 7) / 8 - 1; + + /* x86-64 supports up to 6 arguments. 7+ can be added in the future */ + if (nr_regs > 6) return -ENOTSUPP; - stack_size +=3D extra_nregs * 8; =20 /* Generated trampoline stack layout: * @@ -2154,7 +2146,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *image, void *i * [ ... ] * RBP - regs_off [ reg_arg1 ] program's ctx pointer * - * RBP - args_off [ arg regs count ] always + * RBP - nregs_off [ regs count ] always * * RBP - ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag * @@ -2166,11 +2158,12 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_im= age *im, void *image, void *i if (save_ret) stack_size +=3D 8; =20 + stack_size +=3D nr_regs * 8; regs_off =3D stack_size; =20 - /* args count */ + /* regs count */ stack_size +=3D 8; - args_off =3D stack_size; + nregs_off =3D stack_size; =20 if (flags & BPF_TRAMP_F_IP_ARG) stack_size +=3D 8; /* room for IP address argument */ @@ -2198,11 +2191,11 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_im= age *im, void *image, void *i EMIT1(0x53); /* push rbx */ =20 /* Store number of argument registers of the traced function: - * mov rax, nr_args + extra_nregs - * mov QWORD PTR [rbp - args_off], rax + * mov rax, nr_regs + * mov QWORD PTR [rbp - nregs_off], rax */ - emit_mov_imm64(&prog, BPF_REG_0, 0, (u32) nr_args + extra_nregs); - emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -args_off); + emit_mov_imm64(&prog, BPF_REG_0, 0, (u32) nr_regs); + emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -nregs_off); =20 if (flags & BPF_TRAMP_F_IP_ARG) { /* Store IP address of the traced function: @@ -2213,7 +2206,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *image, void *i emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -ip_off); } =20 - save_regs(m, &prog, nr_args, regs_off); + save_regs(m, &prog, nr_regs, regs_off); =20 if (flags & BPF_TRAMP_F_CALL_ORIG) { /* arg1: mov rdi, im */ @@ -2243,7 +2236,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *image, void *i } =20 if (flags & BPF_TRAMP_F_CALL_ORIG) { - restore_regs(m, &prog, nr_args, regs_off); + restore_regs(m, &prog, nr_regs, regs_off); =20 if (flags & BPF_TRAMP_F_ORIG_STACK) { emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, 8); @@ -2284,7 +2277,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *image, void *i } =20 if (flags & BPF_TRAMP_F_RESTORE_REGS) - restore_regs(m, &prog, nr_args, regs_off); + restore_regs(m, &prog, nr_regs, regs_off); =20 /* This needs to be done regardless. If there were fmod_ret programs, * the return value is only updated on the stack and still needs to be --=20 2.25.1