mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 1/4] riscv, bpf: Fix forward jump offset calculation during JIT passes
Date: Tue,  1 Dec 2026 13:34:01 +0000	[thread overview]
Message-ID: <20261201133404.2227100-2-pulehui@huaweicloud.com> (raw)
In-Reply-To: <20261201133404.2227100-1-pulehui@huaweicloud.com>

From: Pu Lehui <pulehui@huawei.com>

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 <pulehui@huawei.com>
---
 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


  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 ` Pu Lehui [this message]
2026-12-01 13:34 ` [PATCH bpf-next 2/4] riscv, bpf: Reuse tailcall epilogue across tailcall sites Pu Lehui
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-2-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®