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