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 3/4] riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with per-image PLT
Date: Tue,  1 Dec 2026 13:34:03 +0000	[thread overview]
Message-ID: <20261201133404.2227100-4-pulehui@huaweicloud.com> (raw)
In-Reply-To: <20261201133404.2227100-1-pulehui@huaweicloud.com>

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


  parent 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 ` [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 [this message]
2026-12-01 14:19   ` [PATCH bpf-next 3/4] riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with per-image PLT 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-4-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®