From: Pu Lehui <pulehui@huaweicloud.com>
To: "Björn Töpel" <bjorn@kernel.org>,
bpf@vger.kernel.org, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Puranjay Mohan <puranjay@kernel.org>,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Alexandre Ghiti <alex@ghiti.fr>, Pu Lehui <pulehui@huawei.com>,
Pu Lehui <pulehui@huaweicloud.com>
Subject: [PATCH bpf-next 2/4] riscv, bpf: Reuse tailcall epilogue across tailcall sites
Date: Tue, 1 Dec 2026 13:34:02 +0000 [thread overview]
Message-ID: <20261201133404.2227100-3-pulehui@huaweicloud.com> (raw)
In-Reply-To: <20261201133404.2227100-1-pulehui@huaweicloud.com>
From: Pu Lehui <pulehui@huawei.com>
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 <pulehui@huawei.com>
---
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
next prev parent reply other threads:[~2026-12-01 13:29 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 13:34 [PATCH bpf-next 0/4] riscv, bpf: Miscellaneous JIT optimizations Pu Lehui
2026-12-01 13:34 ` [PATCH bpf-next 1/4] riscv, bpf: Fix forward jump offset calculation during JIT passes Pu Lehui
2026-12-01 13:34 ` Pu Lehui [this message]
2026-12-01 13:34 ` [PATCH bpf-next 3/4] riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with per-image PLT Pu Lehui
2026-12-01 14:19 ` bot+bpf-ci
2026-12-01 13:34 ` [PATCH bpf-next 4/4] riscv, bpf: Remove arch_bpf_trampoline_skip Pu Lehui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201133404.2227100-3-pulehui@huaweicloud.com \
--to=pulehui@huaweicloud.com \
--cc=alex@ghiti.fr \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bjorn@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=pulehui@huawei.com \
--cc=puranjay@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®