From: Pu Lehui Running the no_limits/dump selftest on RV64 fails to converge within NR_JIT_ITERATIONS (32) passes. The test generates ~560k instructions with dense short forward branches that fail to settle in time. In build_body(), ctx->offset[] is updated immediately after emitting each instruction. When rv_offset() computes a forward jump, the source offset reflects the current pass while the target offset still holds the previous pass's value. This cross-pass mismatch inflates forward jump distances by the cumulative shrinkage of preceding instructions, allowing only a small batch of branches to shrink per pass. Similarly, epilogue_offset() mixes the current ninsns with the previous pass's epilogue offset. Fix this by keeping the previous pass's offset intact in ctx->offset[] while emitting the current instruction. In rv_offset(), use previous-pass offsets for both source and target on forward jumps, and current-pass offsets on backward jumps. Also replace epilogue_offset() with rv_offset() targeting the end of the program. With this fix, no_limits/dump converges in 2 passes. Furthermore, extend ctx->offset[] to prog->len + 1 entries and record the start of each instruction directly instead of shifting the array afterward for bpf_prog_update_insn_ptrs(). Fixes: 2353ecc6f91f ("bpf, riscv: add BPF JIT for RV64G") Signed-off-by: Pu Lehui --- arch/riscv/net/bpf_jit.h | 13 ++----------- arch/riscv/net/bpf_jit_comp32.c | 5 ++--- arch/riscv/net/bpf_jit_comp64.c | 5 ++--- arch/riscv/net/bpf_jit_core.c | 31 +++++++++++++------------------ 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/arch/riscv/net/bpf_jit.h b/arch/riscv/net/bpf_jit.h index dbe1ac8cd0de..3603bbfcb120 100644 --- a/arch/riscv/net/bpf_jit.h +++ b/arch/riscv/net/bpf_jit.h @@ -78,8 +78,6 @@ struct rv_jit_context { u16 *insns; /* RV insns */ u16 *ro_insns; int ninsns; - int prologue_len; - int epilogue_offset; int *offset; /* BPF to RV */ int nexentries; int ex_insn_off; @@ -133,13 +131,6 @@ static inline void emitc(const u16 insn, struct rv_jit_context *ctx) ctx->ninsns++; } -static inline int epilogue_offset(struct rv_jit_context *ctx) -{ - int to = ctx->epilogue_offset, from = ctx->ninsns; - - return ninsns_rvoff(to - from); -} - /* Return -1 or inverted cond. */ static inline int invert_bpf_cond(u8 cond) { @@ -228,8 +219,8 @@ static inline int rv_offset(int insn, int off, struct rv_jit_context *ctx) int from, to; off++; /* BPF branch is from PC+1, RV is from PC */ - from = (insn > 0) ? ctx->offset[insn - 1] : ctx->prologue_len; - to = (insn + off > 0) ? ctx->offset[insn + off - 1] : ctx->prologue_len; + from = (off >= 0) ? ctx->offset[insn] : ctx->ninsns; + to = ctx->offset[insn + off]; return ninsns_rvoff(to - from); } diff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp32.c index a9e0bd5cc81d..a32fb59b74e9 100644 --- a/arch/riscv/net/bpf_jit_comp32.c +++ b/arch/riscv/net/bpf_jit_comp32.c @@ -787,8 +787,7 @@ static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx) const s8 *arr_reg = bpf2rv32[BPF_REG_2]; const s8 *idx_reg = bpf2rv32[BPF_REG_3]; - tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] : - ctx->offset[0]; + tc_ninsn = ctx->offset[insn + 1] - ctx->offset[insn]; /* max_entries = array->map.max_entries; */ off = offsetof(struct bpf_array, map.max_entries); @@ -1305,7 +1304,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, if (i == ctx->prog->len - 1) break; - rvoff = epilogue_offset(ctx); + rvoff = rv_offset(i, ctx->prog->len - 1 - i, ctx); emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx); break; diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c index a7a7d2f9da85..dfea7d24155e 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -342,8 +342,7 @@ static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx) * if (index >= array->map.max_entries) * goto out; */ - tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] : - ctx->offset[0]; + tc_ninsn = ctx->offset[insn + 1] - ctx->offset[insn]; emit_zextw(RV_REG_A2, RV_REG_A2, ctx); off = offsetof(struct bpf_array, map.max_entries); @@ -1962,7 +1961,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, if (i == ctx->prog->len - 1) break; - rvoff = epilogue_offset(ctx); + rvoff = rv_offset(i, ctx->prog->len - 1 - i, ctx); ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx); if (ret) return ret; diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c index 138e9fa90963..91b14945d3f2 100644 --- a/arch/riscv/net/bpf_jit_core.c +++ b/arch/riscv/net/bpf_jit_core.c @@ -24,19 +24,24 @@ static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset) for (i = 0; i < prog->len; i++) { const struct bpf_insn *insn = &prog->insnsi[i]; + int start_ninsns = ctx->ninsns; int ret; ret = bpf_jit_emit_insn(insn, ctx, extra_pass); + if (ret < 0) + return ret; if (offset) - offset[i] = ctx->ninsns; + offset[i] = start_ninsns; if (ret > 0) { i++; /* skip the next instruction */ if (offset) - offset[i] = ctx->ninsns; + offset[i] = start_ninsns; } - if (ret < 0) - return ret; } + + if (offset) + offset[i] = ctx->ninsns; + return 0; } @@ -142,14 +147,14 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr if (prog->aux->jits_use_priv_stack && alloc_priv_stack(prog)) goto out_free_hdr; - ctx->offset = kvzalloc_objs(int, prog->len); + ctx->offset = kvzalloc_objs(int, prog->len + 1); if (!ctx->offset) goto out_free_hdr; if (build_body(ctx, extra_pass, NULL)) goto out_free_hdr; - for (i = 0; i < prog->len; i++) { + for (i = 0; i <= prog->len; i++) { prev_ninsns += 32; ctx->offset[i] = prev_ninsns; } @@ -159,12 +164,10 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr ctx->ninsns = 0; bpf_jit_build_prologue(ctx, bpf_is_subprog(prog)); - ctx->prologue_len = ctx->ninsns; if (build_body(ctx, extra_pass, ctx->offset)) goto out_free_hdr; - ctx->epilogue_offset = ctx->ninsns; bpf_jit_build_epilogue(ctx); if (ctx->ninsns == prev_ninsns) { @@ -236,17 +239,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr prog->jited_len = prog_size - cfi_get_offset(); if (!prog->is_func || extra_pass) { - for (i = 0; i < prog->len; i++) + for (i = 0; i <= prog->len; i++) ctx->offset[i] = ninsns_rvoff(ctx->offset[i]); - bpf_prog_fill_jited_linfo(prog, ctx->offset); - - /* - * bpf_prog_update_insn_ptrs() wants the start of each insn, so - * shift ctx->offset[] by one and get insn 0 from the prologue. - */ - for (i = prog->len - 1; i > 0; i--) - ctx->offset[i] = ctx->offset[i - 1]; - ctx->offset[0] = ninsns_rvoff(ctx->prologue_len); + bpf_prog_fill_jited_linfo(prog, ctx->offset + 1); bpf_prog_update_insn_ptrs(prog, ctx->offset, jit_data->ro_image); goto out_free_ctx; } -- 2.34.1