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=MkXLjErdNDpwKtt5nbFZZdiBWD+bZguv3TNSZEk2+eamUH8GrIKbYMLXZIG3vG8Fo2XM 8buG6s0LMGIgvDQC/U8F69NDiYx+rgPzC61oFIn5G/8yji6W54wtgjQI+4Np1aMjWlBRu WpGGysKSQHFhLhh4PZlIdw8c6oAM/pmsjpAYSt1e5IkemXzfzdAkGxB9DX3fMSbrzxqT2 ipV3hVa05uBCEAqowzGUU7F5Ov11vPPGNJ8j2RtnsLPTr0nh04ihJwAhYhb9bpZWr3yi7 5jeUksbx52ItzVO/f5J7IOJEr1vnRWh1DHFHEIEAWW3mew7ros1nTdzYex5izRz5vsA== 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=yDwoewkASMQFE3/4FVsu6bdkceBl/jNNc+zzFRML3C0=; b=ijUE7YXm2pVobwvbKqCIRbE8vuEontOCNNktvuLdv2DkJqIW+4YQ+gHbEDqdXL6VH8xe ngwW7z0jew7NKF2BVtCC9ypvoRgQKTkI8O4QPG0TiTUy/4+7vteyYtfpoKPWeRlwJm6yt xvEsIbFgg52rhox59ic2t+MsIu2Wk6/rrCzwqneycMBzihrzI2WMnOqgLoZ99pkGywzL1 XVCLLbLejD2m7qV4vPUwEwfjsXPQHMbgZmFhJajsxXo4bSyT+0tmCwY3B3w9UOXqxNlFV 59M7KTHDvISrdU5BuoT9fW86uRuq/6oTBqlxo+AGcdcDRJegLGWJHKOgtI6qp+B1nAw== 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 5425A606A31A; Tue, 1 Dec 2026 13:29:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5425A606A31A 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 4jf1v62qFrz1ylG; 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 D1B8940576; 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--.32601S5; 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 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 Message-Id: <20261201133404.2227100-4-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--.32601S5 X-Coremail-Antispam: 1UD129KBjvJXoWfJr4UZr1xXr4fJry3WryfJFb_yoWDuw4fpF Wvkr93ArWqqr4rKF12v3WDZr1Ykws0ga17GrWrCrWrAanaqr95G3Wvkw45tFZ8CFW8Cw1r AFZ0yF13uw4qq3JanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r4a6rW5MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVW8ZVWrXwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0pRl_MsUUUUU= X-CM-SenderInfo: psxovxtxl6x35dzhxuhorxvhhfrp/ From: Pu Lehui 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 --- 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 #include #include +#include #include #include #include #include #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