From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=124.71.93.112 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796131766; b=hviLlNA7VAnJzRikXAFmJSBQ40UTGqggx0a4PdQRy9+YlZIqglAa7itlY74+GTQ8ziwq +fyVFk6CKfm68QA2c7BBLFMN0iGjkGV2Uy1S1TKqVPCmESSIGWI8vQV2vQKWqP+1KVg8F PihdugGLBfzAjjOXv3/TiaYtTKDKrsiRXF+xynH38IRvN1zQBPAek/CX2DeHmI9WvEfwS bwobP+drLK6DUF8u5os1wR06gFvKX5BzWVn+IMMsH6pzz38MlVu/aYhnvbxA6B8TBCRK1 jagh5fJXIzF2PglVlz1p6TQwmTssYhwPqme2WXpIuteA0K2vEevoIlIe5F2kpot7X2A== 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=HtinyaDsPBhkKMLuWPgT5c6b91lTGYyj8mx4Gw3c5zI=; b=H2X50OxP5n8AkRVvSBL7KYF9azpfXcGwXsJNTV39xCVRp9m8+cfgxRsxVRbNyKa1lvCu A5RKsoIk5WjoZxYOZgjf20rFNrDlj3h0KBDZrzJUKMeuL/poZe0ZIhqfLkZThfx7ajpEr ZFa3guuycu22U85xcHT/LoknJujtw/UJC8iSf2Ayv6+DOewD0qvscD0AXmFxtE59/SJwn 5+w/o6cGbhXZ9XxHr2NPa+Wcvh/EMGIRy+o5q/VR453caI97tALxc+NRMAQK2AcC6eOF/ oBl8u2o1uHKqPFr59g2onjX1sRbZTI/wUhFxexFNk2X9KNWEdUv7ibTyEq/vc7N30Fw== 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.112 Received: from dggmsgout12.his.huawei.com (dggmsgout12.his.huawei.com [124.71.93.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F9BF606A320; Tue, 1 Dec 2026 13:29:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6F9BF606A320 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.198]) by dggmsgout12.his.huawei.com (SkyGuard) with ESMTP id 4jf1v61mZTz1ylG; Tue, 1 Dec 2026 21:29:06 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id A84DD40574; 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--.32601S3; 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 1/4] riscv, bpf: Fix forward jump offset calculation during JIT passes Date: Tue, 1 Dec 2026 13:34:01 +0000 Message-Id: <20261201133404.2227100-2-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--.32601S3 X-Coremail-Antispam: 1UD129KBjvJXoW3Gw1Dtr4xWr4xuFW3JFykGrg_yoW3JryrpF s5Cr1fArZ0qr1YqF4Utan8ZFyaya1DWw43GrykCrWxCFW2vr1rJ3WYkw4Y9as8Cry8ur18 Zrs0vr13uw1DXrDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPE14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I2 62IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcV AFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG 0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2Y2ka0xkIwI 1lc7CjxVAaw2AFwI0_GFv_Wryl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_ Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17 CE14v26r4a6rW5MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0 I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I 8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73 UjIFyTuYvjTRMfOzDUUUU X-CM-SenderInfo: psxovxtxl6x35dzhxuhorxvhhfrp/ 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