From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=124.71.93.99 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796131766; b=rzjSc33Vh0I1xnYgPwDStmKYBVjLjYnq7X6X/LJ2eEVzCIx5x5BDGXZnZpQhxpK0PTxd uoDNdwWocPmHrFPsM55SXN700Lby0N623vgpAgpOao9deE/t5gcwljHU+oFH7M7MGemnO bfne6B9Hz5/ERdKjenUAaK4RD0P65nLfJVhP970LXrGnlrwEWsI04t46ax4qG7wRkHbYw iBYOgkKlEihqZQ2a+s8lampNjkFoHfe4AgS28sotzhjmOTqxF21TFaCN30HQwZMOtC/sN 7+DYru12TO8+GJRt9y0wxS1R+dqHajGUc3Zzdnu4D6ge19hwWUDYexkodRD6S2Hcn6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796131766; h=DMARC-Filter:Received:Received:Received:From:To:Cc:Subject:Date: Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam: X-CM-SenderInfo; bh=bue1ZScmwT0U1RKsiJTHeIDLoyMcr4RbrUPbQVtKSO4=; b=zLmuIxlqYBmHY16kcOp7wo4VDkCS25Y4Gyta6pjyOBnO4AfXiCUV2/R8bJ3F911WpDyx YcarUYS0P4M7AeVPdXT8UjoTRcyPstUyQkiQnbKLTwAgVEnrEci8c0xef6uaB4TCT1gqe etubxxK6VzoOtCP1roUcUjPQwm+d1cKr+oalzdVxeSleH8w5t5TxtkHj8An//C9jBWRsh Uv8DjhVhe8hXQN6eiX7z7sRV2E+UN5gXGZL70m65+FgEbqxhOT5xnXoCWrJo2FTgFWJJ3 1U/ILXoLBDtTWzSJf98hA/cv0rdPA745Rmv0cCJV8kliw3tMbqetde5MS32YxZ2nq8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.remote-ip=124.71.93.99 Received: from dggmsgout11.his.huawei.com (dggmsgout11.his.huawei.com [124.71.93.99]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4202E606A325; Tue, 1 Dec 2026 13:29:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4202E606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.170]) by dggmsgout11.his.huawei.com (SkyGuard) with ESMTP id 4jf1v45TPGz1c1md; Tue, 1 Dec 2026 21:29:04 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id B70D84056B; Tue, 1 Dec 2026 21:29:20 +0800 (CST) Received: from ultra.huawei.com (unknown [10.90.53.71]) by APP4 (Coremail) with UTF8SMTPA id gCh0CgCHveyvyw5roXYmGg--.32601S4; Tue, 01 Dec 2026 21:29:20 +0800 (CST) From: Pu Lehui To: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Puranjay Mohan , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , Pu Lehui , Pu Lehui Subject: [PATCH bpf-next 2/4] riscv, bpf: Reuse tailcall epilogue across tailcall sites Date: Tue, 1 Dec 2026 13:34:02 +0000 Message-Id: <20261201133404.2227100-3-pulehui@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261201133404.2227100-1-pulehui@huaweicloud.com> References: <20261201133404.2227100-1-pulehui@huaweicloud.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:gCh0CgCHveyvyw5roXYmGg--.32601S4 X-Coremail-Antispam: 1UD129KBjvJXoWxuFyUur1fAFWfZw1xWw48WFg_yoWrCrWkpF WUKwn3CrZYqF1IgF9rJFWDXr1akw4qva43Gr1UC3yFkanFqrWkW3WFk34Y9ry5ury8WrWr XFZ8Kr1fu3WkA37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r4a6rW5MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVW8ZVWrXwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0pRHKZLUUUUU= X-CM-SenderInfo: psxovxtxl6x35dzhxuhorxvhhfrp/ From: Pu Lehui Currently, the RV64 BPF JIT emits a full tailcall epilogue inline at every bpf_tail_call() site. For programs with multiple tailcalls, duplicating the register restoration and stack unwinding sequences bloats the JIT image. Emit the tailcall epilogue only at the first bpf_tail_call() site and record its offset in ctx->tc_epilog_off, then have subsequent tailcall sites jump to it after loading the target address. Keeping the normal epilogue separate preserves ret (jr ra) for hardware return address stack (RAS) prediction and avoids extra instructions on the normal exit path. Also rename the fixed_addr parameter of emit_jump_and_link() and emit_call() to can_relax to better reflect its meaning. Tested with synthetic programs containing 10 and 100 distinct bpf_tail_call() sites on RV64 (with RVC enabled): Program Before After Savings tailcall_10 1368 B 1272 B -96 B (-7.02%) tailcall_100 11816 B 10800 B -1016 B (-8.60%) Signed-off-by: Pu Lehui --- arch/riscv/net/bpf_jit.h | 1 + arch/riscv/net/bpf_jit_comp64.c | 19 +++++++++++++++---- arch/riscv/net/bpf_jit_core.c | 2 ++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/arch/riscv/net/bpf_jit.h b/arch/riscv/net/bpf_jit.h index 3603bbfcb120..28f024b5f722 100644 --- a/arch/riscv/net/bpf_jit.h +++ b/arch/riscv/net/bpf_jit.h @@ -86,6 +86,7 @@ struct rv_jit_context { int stack_size; int tcc_offset; int stack_arg_sz; + int tc_epilog_off; u64 arena_vm_start; u64 user_vm_start; }; diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c index dfea7d24155e..1846d0a6e030 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -25,6 +25,9 @@ #define RV_REG_TCC RV_REG_T5 #define RV_REG_ARENA RV_REG_S7 /* For storing arena_vm_start */ +static int emit_jump_and_link(u8 rd, s64 rvoff, bool can_relax, + struct rv_jit_context *ctx); + static const int regmap[] = { [BPF_REG_0] = RV_REG_T6, [BPF_REG_1] = RV_REG_A0, @@ -380,6 +383,14 @@ static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx) if (is_12b_check(off, insn)) return -1; emit_ld(RV_REG_T3, off, RV_REG_T2, ctx); + + /* reuse the tailcall epilogue emitted at the first site */ + if (ctx->tc_epilog_off) { + off = ninsns_rvoff(ctx->tc_epilog_off - ctx->ninsns); + return emit_jump_and_link(RV_REG_ZERO, off, true, ctx); + } + + ctx->tc_epilog_off = ctx->ninsns; __build_epilogue(true, ctx); return 0; } @@ -405,12 +416,12 @@ static void init_regs(u8 *rd, u8 *rs, const struct bpf_insn *insn, *rs = bpf_to_rv_reg(insn->src_reg, ctx); } -static int emit_jump_and_link(u8 rd, s64 rvoff, bool fixed_addr, +static int emit_jump_and_link(u8 rd, s64 rvoff, bool can_relax, struct rv_jit_context *ctx) { s64 upper, lower; - if (rvoff && fixed_addr && is_21b_int(rvoff)) { + if (can_relax && rvoff && is_21b_int(rvoff)) { emit(rv_jal(rd, rvoff >> 1), ctx); return 0; } else if (in_auipc_jalr_range(rvoff)) { @@ -431,7 +442,7 @@ static bool is_signed_bpf_cond(u8 cond) cond == BPF_JSGE || cond == BPF_JSLE; } -static int emit_call(u64 addr, bool fixed_addr, struct rv_jit_context *ctx) +static int emit_call(u64 addr, bool can_relax, struct rv_jit_context *ctx) { s64 off = 0; u64 ip; @@ -445,7 +456,7 @@ static int emit_call(u64 addr, bool fixed_addr, struct rv_jit_context *ctx) off = addr - ip; } - return emit_jump_and_link(RV_REG_RA, off, fixed_addr, ctx); + return emit_jump_and_link(RV_REG_RA, off, can_relax, ctx); } static inline void emit_kcfi(u32 hash, struct rv_jit_context *ctx) diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c index 91b14945d3f2..5efada7b081a 100644 --- a/arch/riscv/net/bpf_jit_core.c +++ b/arch/riscv/net/bpf_jit_core.c @@ -22,6 +22,8 @@ static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset) const struct bpf_prog *prog = ctx->prog; int i; + /* reset per-pass tailcall epilogue offset */ + ctx->tc_epilog_off = 0; for (i = 0; i < prog->len; i++) { const struct bpf_insn *insn = &prog->insnsi[i]; int start_ninsns = ctx->ninsns; -- 2.34.1