* [PATCH bpf-next 0/4] riscv, bpf: Miscellaneous JIT optimizations
@ 2026-12-01 13:34 Pu Lehui
2026-12-01 13:34 ` [PATCH bpf-next 1/4] riscv, bpf: Fix forward jump offset calculation during JIT passes Pu Lehui
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Pu Lehui @ 2026-12-01 13:34 UTC (permalink / raw)
To: Björn Töpel, bpf, linux-riscv, linux-kernel
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
From: Pu Lehui <pulehui@huawei.com>
This series brings four improvements to the RISC-V BPF JIT:
- Patch 1 fixes forward jump offset calculation across JIT passes so
large programs (e.g. no_limits/dump) converge in 2 passes instead of
hitting NR_JIT_ITERATIONS.
- Patch 2 reuses the tailcall epilogue across multiple bpf_tail_call()
sites on RV64 to shrink JIT image size.
- Patch 3 introduces a per-image PLT on RV64 to eliminate stop_machine()
in bpf_arch_text_poke(), reducing trampoline attach/detach latency.
- Patch 4 removes the now-redundant RV64 arch_bpf_trampoline_skip() and
4-byte-aligns the skip nop in invoke_bpf_prog().
This series has been tested with riscv-bpf-daily and passed:
https://github.com/pulehui/riscv-bpf-daily/pull/26
Pu Lehui (4):
riscv, bpf: Fix forward jump offset calculation during JIT passes
riscv, bpf: Reuse tailcall epilogue across tailcall sites
riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with
per-image PLT
riscv, bpf: Remove arch_bpf_trampoline_skip
arch/riscv/net/bpf_jit.h | 19 ++--
arch/riscv/net/bpf_jit_comp32.c | 5 +-
arch/riscv/net/bpf_jit_comp64.c | 192 +++++++++++++++++++++++---------
arch/riscv/net/bpf_jit_core.c | 51 ++++-----
4 files changed, 176 insertions(+), 91 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next 1/4] riscv, bpf: Fix forward jump offset calculation during JIT passes
2026-12-01 13:34 [PATCH bpf-next 0/4] riscv, bpf: Miscellaneous JIT optimizations Pu Lehui
@ 2026-12-01 13:34 ` Pu Lehui
2026-12-01 13:34 ` [PATCH bpf-next 2/4] riscv, bpf: Reuse tailcall epilogue across tailcall sites Pu Lehui
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Pu Lehui @ 2026-12-01 13:34 UTC (permalink / raw)
To: Björn Töpel, bpf, linux-riscv, linux-kernel
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
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next 2/4] riscv, bpf: Reuse tailcall epilogue across tailcall sites
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
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 13:34 ` [PATCH bpf-next 4/4] riscv, bpf: Remove arch_bpf_trampoline_skip Pu Lehui
3 siblings, 0 replies; 6+ messages in thread
From: Pu Lehui @ 2026-12-01 13:34 UTC (permalink / raw)
To: Björn Töpel, bpf, linux-riscv, linux-kernel
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
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next 3/4] riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with per-image PLT
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 ` [PATCH bpf-next 2/4] riscv, bpf: Reuse tailcall epilogue across tailcall sites Pu Lehui
@ 2026-12-01 13:34 ` 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
3 siblings, 1 reply; 6+ messages in thread
From: Pu Lehui @ 2026-12-01 13:34 UTC (permalink / raw)
To: Björn Töpel, bpf, linux-riscv, linux-kernel
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
From: Pu Lehui <pulehui@huawei.com>
On RV64, updating bpf trampolines on kernel functions relies on ftrace,
which has recently been optimized to patch text atomically. However,
updating bpf trampolines on bpf programs uses bpf_arch_text_poke(),
which currently relies on stop_machine().
When the target trampoline lies beyond the +/- 1MB range of a direct jal
instruction, an auipc/jalr pair is required. Because updating a
two-instruction sequence cannot be executed atomically, it must use
patch_text() to halt all online CPUs, introducing noticeable
stop-the-world latency and jitter during bpf trampoline updates.
Optimize this by introducing a per-image PLT at the end of each bpf
JITed image, and pre-emitting an auipc instruction at the prologue entry
at JIT time to compute the upper bits of the PLT offset. When updating
trampolines, the instruction following the auipc is patched: for short
jumps, directly with a jal targeting the trampoline; for long jumps,
with a jalr targeting the PLT along with updating the 64-bit aligned PLT
target. Both cases modify at most a single instruction via
patch_text_nosync(), eliminating stop_machine().
Per-cycle time for 1000 attach + detach cycles on bpf prog:
Before After Reduction
Avg 211.60 us 120.26 us -91.34 us (-43.17%, 1.76x speedup)
P99 772.75 us 176.00 us -596.75 us (-77.22%, 4.39x speedup)
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
arch/riscv/net/bpf_jit.h | 5 ++
arch/riscv/net/bpf_jit_comp64.c | 151 ++++++++++++++++++++++++++------
arch/riscv/net/bpf_jit_core.c | 18 ++--
3 files changed, 138 insertions(+), 36 deletions(-)
diff --git a/arch/riscv/net/bpf_jit.h b/arch/riscv/net/bpf_jit.h
index 28f024b5f722..37d8947dc06b 100644
--- a/arch/riscv/net/bpf_jit.h
+++ b/arch/riscv/net/bpf_jit.h
@@ -751,6 +751,11 @@ static inline u16 rvc_swsp(u32 imm8, u8 rs2)
return rv_css_insn(0x6, imm, rs2, 0x2);
}
+static inline u16 rvc_nop(void)
+{
+ return rv_ci_insn(0, 0, 0, 0x1);
+}
+
/* RVZACAS instructions. */
static inline u32 rvzacas_amocas_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
{
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 1846d0a6e030..3a533e26a0f1 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -9,14 +9,20 @@
#include <linux/bpf.h>
#include <linux/filter.h>
#include <linux/memory.h>
+#include <linux/smp.h>
#include <linux/stop_machine.h>
#include <asm/text-patching.h>
#include <asm/cfi.h>
#include <asm/percpu.h>
#include "bpf_jit.h"
+/*
+ * fentry slot layout:
+ * +0: auipc t1, %pcrel_hi(plt) (fixed at JIT compile time)
+ * +4: nop | jal | jalr t1, %pcrel_lo (atomically patched at RV_FENTRY_POKE_OFF)
+ */
#define RV_FENTRY_NINSNS 2
-#define RV_FENTRY_NBYTES (RV_FENTRY_NINSNS * 4)
+#define RV_FENTRY_POKE_OFF 4
/* imm that allows emit_imm to emit max count insns */
#define RV_MAX_COUNT_IMM 0x7FFF7FF7FF7FF7FF
/* fentry and TCC init insns will be skipped on tailcall */
@@ -28,6 +34,18 @@
static int emit_jump_and_link(u8 rd, s64 rvoff, bool can_relax,
struct rv_jit_context *ctx);
+/*
+ * Per-image PLT stub for long jumps:
+ * auipc t1, 0
+ * ld t1, 12(t1)
+ * jr t1
+ * .quad target
+ */
+struct bpf_plt {
+ u32 insns[3];
+ u64 target;
+} __packed;
+
static const int regmap[] = {
[BPF_REG_0] = RV_REG_T6,
[BPF_REG_1] = RV_REG_A0,
@@ -865,22 +883,39 @@ static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,
return 0;
}
-static int gen_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)
+static int gen_poke_insn(void *target, void *ip, void *plt_slot, u32 *insn,
+ u64 *plt_target, enum bpf_text_poke_type t)
{
- s64 rvoff;
- struct rv_jit_context ctx;
-
- ctx.ninsns = 0;
- ctx.insns = (u16 *)insns;
+ s64 off, upper, lower;
+ u32 auipc_insn;
if (!target) {
- emit(rv_nop(), &ctx);
- emit(rv_nop(), &ctx);
+ *insn = rv_nop();
+ return 0;
+ }
+
+ off = (s64)(target - ip);
+ if (is_21b_int(off)) {
+ *insn = rv_jal((t == BPF_MOD_CALL) ? RV_REG_T0 : RV_REG_ZERO, off >> 1);
return 0;
}
- rvoff = (s64)(target - ip);
- return emit_jump_and_link(is_call ? RV_REG_T0 : RV_REG_ZERO, rvoff, false, &ctx);
+ /* long jumps must go via PLT */
+ if (!plt_slot)
+ return -EFAULT;
+
+ off = (s64)(plt_slot - ip + RV_FENTRY_POKE_OFF);
+ upper = (off + (1 << 11)) >> 12;
+ lower = off & 0xfff;
+
+ auipc_insn = rv_auipc(RV_REG_T1, upper);
+ if (memcmp(ip - RV_FENTRY_POKE_OFF, &auipc_insn, sizeof(auipc_insn)))
+ return -EFAULT;
+
+ /* link t0 even for JUMP so PLT switches to CALL stay safe */
+ *insn = rv_jalr(RV_REG_T0, RV_REG_T1, lower);
+ *plt_target = (u64)target;
+ return 0;
}
int arch_bpf_trampoline_skip(void *nop, void *target)
@@ -905,37 +940,94 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
enum bpf_text_poke_type new_t, void *old_addr,
void *new_addr)
{
- u32 old_insns[RV_FENTRY_NINSNS], new_insns[RV_FENTRY_NINSNS];
- bool is_call;
+ u64 old_plt = 0ULL, new_plt = 0ULL;
+ u32 old_insn, new_insn;
+ u64 *plt_target = NULL;
+ void *plt_slot = NULL;
+ struct bpf_prog *prog;
int ret;
- if (!is_kernel_text((unsigned long)ip) &&
- !is_bpf_text_address((unsigned long)ip))
+ if (!is_bpf_text_address((unsigned long)ip))
return -ENOTSUPP;
- is_call = old_t == BPF_MOD_CALL;
- ret = gen_jump_or_nops(old_addr, ip, old_insns, is_call);
- if (ret)
- return ret;
+ scoped_guard(rcu) {
+ prog = bpf_prog_ksym_find((unsigned long)ip);
+ /* prog entry is the fentry slot */
+ if (prog && ip == prog->bpf_func) {
+ plt_target = (u64 *)(ip + prog->jited_len);
+ if (!IS_ALIGNED((unsigned long)plt_target, sizeof(u64)))
+ return -EFAULT;
+ plt_slot = (void *)plt_target - offsetof(struct bpf_plt, target);
+ ip += RV_FENTRY_POKE_OFF;
+ }
+ }
- if (memcmp(ip, old_insns, RV_FENTRY_NBYTES))
+ if (!IS_ALIGNED((unsigned long)ip, sizeof(u32)))
return -EFAULT;
- is_call = new_t == BPF_MOD_CALL;
- ret = gen_jump_or_nops(new_addr, ip, new_insns, is_call);
+ ret = gen_poke_insn(old_addr, ip, plt_slot, &old_insn, &old_plt, old_t);
if (ret)
return ret;
- cpus_read_lock();
- mutex_lock(&text_mutex);
- if (memcmp(ip, new_insns, RV_FENTRY_NBYTES))
- ret = patch_text(ip, new_insns, RV_FENTRY_NBYTES);
- mutex_unlock(&text_mutex);
- cpus_read_unlock();
+ ret = gen_poke_insn(new_addr, ip, plt_slot, &new_insn, &new_plt, new_t);
+ if (ret)
+ return ret;
+
+ guard(mutex)(&text_mutex);
+
+ if (old_plt && *plt_target != old_plt)
+ return -EFAULT;
+
+ if (memcmp(ip, &old_insn, sizeof(old_insn)))
+ return -EFAULT;
+
+ if (new_plt && *plt_target != new_plt) {
+ /* 8-byte aligned plt_target ensures an atomic 64-bit store */
+ ret = patch_insn_write(plt_target, &new_plt, sizeof(new_plt));
+ if (ret)
+ return ret;
+
+ /* ensure all CPUs see the new target before patching ip */
+ if (old_insn != new_insn)
+ kick_all_cpus_sync();
+ }
+
+ if (old_insn != new_insn)
+ ret = patch_text_nosync(ip, &new_insn, sizeof(new_insn));
return ret;
}
+static void emit_plt(struct rv_jit_context *ctx)
+{
+ struct bpf_plt *plt;
+ int plt_off, pad;
+ s64 upper;
+ u32 insn;
+
+ /* make sure plt->target is 8-byte aligned */
+ pad = -(ninsns_rvoff(ctx->ninsns) + offsetof(struct bpf_plt, target)) & 7;
+ if (rvc_enabled() && (pad & 2))
+ emitc(rvc_nop(), ctx);
+ if (pad & 4)
+ emit(rv_nop(), ctx);
+
+ plt_off = ninsns_rvoff(ctx->ninsns);
+ emit(rv_auipc(RV_REG_T1, 0), ctx);
+ emit(rv_ld(RV_REG_T1, offsetof(struct bpf_plt, target), RV_REG_T1), ctx);
+ emit(rv_jalr(RV_REG_ZERO, RV_REG_T1, 0), ctx);
+
+ if (ctx->insns) {
+ plt = (void *)ctx->insns + plt_off;
+ plt->target = 0ULL;
+
+ /* fix up first fentry slot with auipc to the PLT */
+ upper = (plt_off - cfi_get_offset() + (1 << 11)) >> 12;
+ insn = rv_auipc(RV_REG_T1, upper);
+ *(u32 *)((void *)ctx->insns + cfi_get_offset()) = insn;
+ }
+}
+
static void store_args(int nr_arg_slots, int args_off, int stack_args_off,
struct rv_jit_context *ctx)
{
@@ -2201,7 +2293,7 @@ void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog)
/* bpf prog starts here as kcfi skipped during prog->bpf_func setup */
- /* nops reserved for auipc+jalr pair */
+ /* fentry slot: auipc (fixed up in emit_plt) + nop/jal/jalr */
for (i = 0; i < RV_FENTRY_NINSNS; i++)
emit(rv_nop(), ctx);
@@ -2270,6 +2362,7 @@ void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog)
void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
{
__build_epilogue(false, ctx);
+ emit_plt(ctx);
}
bool bpf_jit_supports_kfunc_call(void)
diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c
index 5efada7b081a..2188788dc25a 100644
--- a/arch/riscv/net/bpf_jit_core.c
+++ b/arch/riscv/net/bpf_jit_core.c
@@ -16,6 +16,7 @@
/* Number of iterations to try until offsets converge. */
#define NR_JIT_ITERATIONS 32
+#define PLT_TARGET_NBYTES (IS_ENABLED(CONFIG_64BIT) ? sizeof(u64) : 0)
static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset)
{
@@ -172,17 +173,20 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
bpf_jit_build_epilogue(ctx);
- if (ctx->ninsns == prev_ninsns) {
+ /* compare before variable PLT alignment padding */
+ if (ctx->offset[prog->len] == prev_ninsns) {
+ /* obtain the actual prog size */
+ prog_size = sizeof(*ctx->insns) * ctx->ninsns;
+
if (jit_data->header)
break;
- /* obtain the actual image size */
+
extable_size = prog->aux->num_exentries *
sizeof(struct exception_table_entry);
- prog_size = sizeof(*ctx->insns) * ctx->ninsns;
jit_data->ro_header =
- bpf_jit_binary_pack_alloc(prog_size + extable_size,
- &jit_data->ro_image, sizeof(u32),
+ bpf_jit_binary_pack_alloc(prog_size + PLT_TARGET_NBYTES + extable_size,
+ &jit_data->ro_image, sizeof(u64),
&jit_data->header, &jit_data->image,
bpf_fill_ill_insns,
bpf_prog_was_classic(prog));
@@ -203,7 +207,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
* potentially shrink more (auipc/jalr -> jal).
*/
}
- prev_ninsns = ctx->ninsns;
+ prev_ninsns = ctx->offset[prog->len];
}
if (i == NR_JIT_ITERATIONS) {
@@ -212,7 +216,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
}
if (extable_size)
- prog->aux->extable = (void *)ctx->ro_insns + prog_size;
+ prog->aux->extable = (void *)ctx->ro_insns + prog_size + PLT_TARGET_NBYTES;
skip_init_ctx:
pass++;
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next 4/4] riscv, bpf: Remove arch_bpf_trampoline_skip
2026-12-01 13:34 [PATCH bpf-next 0/4] riscv, bpf: Miscellaneous JIT optimizations Pu Lehui
` (2 preceding siblings ...)
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 13:34 ` Pu Lehui
3 siblings, 0 replies; 6+ messages in thread
From: Pu Lehui @ 2026-12-01 13:34 UTC (permalink / raw)
To: Björn Töpel, bpf, linux-riscv, linux-kernel
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
From: Pu Lehui <pulehui@huawei.com>
Now that the preceding commit extends bpf_arch_text_poke() to cover the
functionality of arch_bpf_trampoline_skip(), the RV64-specific
implementation is redundant. Remove it in favor of the generic weak
implementation which calls bpf_arch_text_poke().
Also align the skip nop in invoke_bpf_prog() to 4 bytes with c.nop when
RVC is enabled, as bpf_arch_text_poke() requires 4-byte alignment for
atomic single-instruction patching.
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
arch/riscv/net/bpf_jit_comp64.c | 25 +++++--------------------
1 file changed, 5 insertions(+), 20 deletions(-)
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 3a533e26a0f1..89634c3bfa86 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -10,7 +10,6 @@
#include <linux/filter.h>
#include <linux/memory.h>
#include <linux/smp.h>
-#include <linux/stop_machine.h>
#include <asm/text-patching.h>
#include <asm/cfi.h>
#include <asm/percpu.h>
@@ -918,24 +917,6 @@ static int gen_poke_insn(void *target, void *ip, void *plt_slot, u32 *insn,
return 0;
}
-int arch_bpf_trampoline_skip(void *nop, void *target)
-{
- u32 old_insn = rv_nop();
- u32 new_insn = rv_jal(RV_REG_ZERO, (target - nop) >> 1);
- int ret;
-
- if (memcmp(nop, &old_insn, sizeof(old_insn)))
- return -EFAULT;
-
- cpus_read_lock();
- mutex_lock(&text_mutex);
- ret = patch_text(nop, &new_insn, sizeof(new_insn));
- mutex_unlock(&text_mutex);
- cpus_read_unlock();
-
- return ret;
-}
-
int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
enum bpf_text_poke_type new_t, void *old_addr,
void *new_addr)
@@ -1083,8 +1064,12 @@ static int invoke_bpf_prog(struct bpf_tramp_image *im, struct bpf_tramp_node *no
int ret, branch_off;
struct bpf_prog *p = node->link->prog;
int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
- void *skip = ctx->ro_insns + ctx->ninsns;
+ void *skip;
+ /* make sure skip address is 4-byte aligned */
+ if (rvc_enabled() && (ninsns_rvoff(ctx->ninsns) & 2))
+ emitc(rvc_nop(), ctx);
+ skip = ctx->ro_insns + ctx->ninsns;
/* nop, patched to a jal over this prog when it is detached */
emit(rv_nop(), ctx);
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next 3/4] riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with per-image PLT
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
0 siblings, 0 replies; 6+ messages in thread
From: bot+bpf-ci @ 2026-12-01 14:19 UTC (permalink / raw)
To: pulehui, bjorn, bpf, linux-riscv, linux-kernel
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, puranjay, pjw, palmer,
alex, pulehui, pulehui, ast, andrii, daniel, martin.lau, eddyz87,
yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1341 bytes --]
> riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with per-image PLT
TL;DR: Nit only: the fentry slot layout comment writes "jalr t1, %pcrel_lo",
but the code emits jalr with t0 as link register and t1 as base.
> diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
> --- a/arch/riscv/net/bpf_jit_comp64.c
> +++ b/arch/riscv/net/bpf_jit_comp64.c
[ ... ]
> +/*
> + * fentry slot layout:
> + * +0: auipc t1, %pcrel_hi(plt) (fixed at JIT compile time)
> + * +4: nop | jal | jalr t1, %pcrel_lo (atomically patched at RV_FENTRY_POKE_OFF)
> + */
This isn't a bug, but the long-jump form here reads as "jalr t1, ...", which
by the convention used in the rest of this comment would make t1 the link
register.
gen_poke_insn() actually emits:
/* link t0 even for JUMP so PLT switches to CALL stay safe */
*insn = rv_jalr(RV_REG_T0, RV_REG_T1, lower);
so rd is t0 and t1 is the base. The trampoline saves t0 as the return
address of the traced function and returns through it.
Would something like "jalr t0, %pcrel_lo(t1)" match the code better?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46660875400
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-01 14:19 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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
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®