* Re: [PATCH v4 6/6] LoongArch: BPF: Add support for arena atomics
[not found] <sashiko-outbox-243049@kernel.org>
@ 2026-12-03 1:41 ` George Guo
0 siblings, 0 replies; 2+ messages in thread
From: George Guo @ 2026-12-03 1:41 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: sashiko-reviews, hengqi.chen, loongarch, bpf, linux-kernel
On 2026/12/1 10:11, sashiko-bot@kernel.org wrote:
> Can an unaligned BPF_PROBE_ATOMIC read-modify-write access trigger a kernel
> Oops in do_ale() instead of recovering through ex_handler_bpf()?
>
> check_ptr_alignment() returns 0 unconditionally for PTR_TO_ARENA, so the
> verifier allows unaligned arena addresses to reach the atomic instructions
> emitted by emit_atomic_rmw(). On LoongArch, AM* atomic instructions require
> natural alignment and raise EXCCODE_ALE when unaligned.
>
> However, neither do_ale() nor emulate_load_store_insn() consults
> fixup_exception(regs) on kernel alignment faults:
Yes, this is a real issue. I reproduced it with an atomic one byte off
an aligned arena address, the kernel panics at the first amadd_db.w:
Kernel ale access[#1]:
...
ERA: ffff8000020136f4 bpf_prog_53d40cccca5faaf3_unaligned+0x10c/0x284
ESTAT: 00090000 [ALE] (IS= ECode=9 EsubCode=0)
BADV: ffff80008d600001
...
Kernel panic - not syncing: Fatal exception
The exception table entry added by this patch is fine, the problem is
that the trap code never looks it up for ALE. This is fixed by Tiezhu's
patch "LoongArch: Call fixup_exception() for unaligned accesses", which
calls fixup_exception() on the failure paths of do_ale() and
emulate_load_store_insn():
https://lore.kernel.org/loongarch/20261203012908.27076-1-yangtiezhu@loongson.cn/
With that patch, a new arena_atomics/unaligned subtest (32-bit fetch
add and fetch or, 64-bit xchg and cmpxchg, all one byte off) passed on
Loongson-3C6000, with both CONFIG_ARCH_STRICT_ALIGN=y and
CONFIG_ARCH_STRICT_ALIGN=n:
$ sudo ./test_progs -a arena_atomics/unaligned
#3/10 arena_atomics/unaligned:OK
#3 arena_atomics:OK
Summary: 1/1 PASSED, 0 SKIPPED, 0/0 FAILED
The test is archived in the v1 thread of that patch:
https://lore.kernel.org/loongarch/20261202134056.40836-1-dongtai.guo@linux.dev/
I will send it formally after the LoongArch BPF JIT support is merged.
Thanks,
George
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH v4 6/6] LoongArch: BPF: Add support for arena atomics
2026-12-01 9:51 [PATCH v4 0/6] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
@ 2026-12-01 9:51 ` George Guo
0 siblings, 0 replies; 2+ messages in thread
From: George Guo @ 2026-12-01 9:51 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
In order to support the atomic instructions on arena pointers
(BPF_PROBE_ATOMIC), add the arena base held in REG_ARENA to the address
and add an exception table entry for the memory access instruction of
read-modify-write, fetch, xchg, cmpxchg, load-acquire and store-release.
If the memory access faults, clear the register that the instruction
loads into, as the verifier expects: dst_reg for load-acquire, r0 for
cmpxchg and src_reg for the fetch instructions. The zero-extension of
the 32-bit fetch instructions is emitted after the memory access, so the
cleared register remains zero.
The ll/sc loop of cmpxchg is not suitable for arena: both ll and sc can
fault if another CPU frees the page between them, but the verifier only
reserves one exception table entry for each instruction. So emit a
single amcas_db.{w/d} instruction for cmpxchg on arena, and make
bpf_jit_supports_insn() return cpu_has_lamcas for it, just like RISC-V
does with Zacas.
This is similar to commit fb7cefabae81 ("riscv, bpf: Add support arena
atomics for RV64") and commit 1519f488e8ce ("bpf, riscv: Clear fetch
destination on faulting arena atomic").
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
arch/loongarch/include/asm/inst.h | 4 +
arch/loongarch/net/bpf_jit.c | 123 ++++++++++++++++++++++++++++--
2 files changed, 122 insertions(+), 5 deletions(-)
diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/include/asm/inst.h
index e061f966f7d1..1fc99b7d70be 100644
--- a/arch/loongarch/include/asm/inst.h
+++ b/arch/loongarch/include/asm/inst.h
@@ -198,6 +198,8 @@ enum reg3_op {
fstxs_op = 0x7070,
fstxd_op = 0x7078,
scq_op = 0x70ae,
+ amcasdbw_op = 0x70b6,
+ amcasdbd_op = 0x70b7,
amswapb_op = 0x70b8,
amswaph_op = 0x70b9,
amaddb_op = 0x70ba,
@@ -817,6 +819,8 @@ DEF_EMIT_REG3_FORMAT(amorw, amorw_op)
DEF_EMIT_REG3_FORMAT(amord, amord_op)
DEF_EMIT_REG3_FORMAT(amxorw, amxorw_op)
DEF_EMIT_REG3_FORMAT(amxord, amxord_op)
+DEF_EMIT_REG3_FORMAT(amcasdbw, amcasdbw_op)
+DEF_EMIT_REG3_FORMAT(amcasdbd, amcasdbd_op)
DEF_EMIT_REG3_FORMAT(amswapb, amswapb_op)
DEF_EMIT_REG3_FORMAT(amswaph, amswaph_op)
DEF_EMIT_REG3_FORMAT(amswapw, amswapw_op)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index b78d38559598..abf96ed6cd64 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -450,6 +450,13 @@ static void emit_store_stack_imm64(struct jit_ctx *ctx, int reg, int stack_off,
emit_insn(ctx, std, reg, LOONGARCH_GPR_FP, stack_off);
}
+#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
+#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
+#define REG_DONT_CLEAR_MARKER 0
+
+static int add_exception_handler(const struct bpf_insn *insn,
+ struct jit_ctx *ctx, int dst_reg);
+
static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
{
const u8 t1 = LOONGARCH_GPR_T1;
@@ -461,10 +468,15 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
const s16 off = insn->off;
const s32 imm = insn->imm;
const bool isdw = BPF_SIZE(insn->code) == BPF_DW;
+ const bool arena = BPF_MODE(insn->code) == BPF_PROBE_ATOMIC;
bool zext = false;
+ u8 zext_reg = src;
+ int ret;
move_imm(ctx, t1, off);
emit_insn(ctx, addd, t1, dst, t1);
+ if (arena)
+ emit_insn(ctx, addd, t1, t1, REG_ARENA);
move_reg(ctx, t3, src);
switch (imm) {
@@ -589,6 +601,26 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
break;
/* r0 = atomic_cmpxchg(dst + off, r0, src); */
case BPF_CMPXCHG:
+ if (arena) {
+ /*
+ * Another CPU can free the arena page between the ll and
+ * the sc of an ll/sc loop, so both can fault, but the
+ * verifier reserves a single exception table entry per
+ * instruction. Use amcas, which bpf_jit_supports_insn()
+ * requires for cmpxchg on arena.
+ */
+ if (!cpu_has_lamcas) {
+ pr_err_once("bpf-jit: amcas instruction is not supported\n");
+ return -EINVAL;
+ }
+ if (isdw)
+ emit_insn(ctx, amcasdbd, r0, t1, t3);
+ else
+ emit_insn(ctx, amcasdbw, r0, t1, t3);
+ zext = !isdw;
+ zext_reg = r0;
+ break;
+ }
move_reg(ctx, t2, r0);
if (isdw) {
emit_insn(ctx, lld, r0, t1, 0);
@@ -613,8 +645,23 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
return -EINVAL;
}
+ /*
+ * On arena, the memory access is the last instruction emitted so far.
+ * If it faults, clear the register it loads into, as the verifier
+ * expects, and resume at the next instruction: zero-extending the
+ * cleared register keeps it zero.
+ */
+ if (arena) {
+ int load_reg = bpf_atomic_load_reg(insn);
+
+ ret = add_exception_handler(insn, ctx, load_reg < 0 ?
+ REG_DONT_CLEAR_MARKER : regmap[load_reg]);
+ if (ret)
+ return ret;
+ }
+
if (zext)
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ emit_insn(ctx, bstrpickd, zext_reg, zext_reg, 31, 0);
return 0;
}
@@ -626,10 +673,37 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx)
const u8 dst = regmap[insn->dst_reg];
const s16 off = insn->off;
const s32 imm = insn->imm;
+ const bool arena = BPF_MODE(insn->code) == BPF_PROBE_ATOMIC;
+ int ret;
switch (imm) {
/* dst_reg = load_acquire(src_reg + off16) */
case BPF_LOAD_ACQ:
+ if (arena) {
+ /* t1 = src + off + arena_vm_start; load from [t1]. */
+ move_imm(ctx, t1, off);
+ emit_insn(ctx, addd, t1, src, t1);
+ emit_insn(ctx, addd, t1, t1, REG_ARENA);
+ switch (BPF_SIZE(insn->code)) {
+ case BPF_B:
+ emit_insn(ctx, ldbu, dst, t1, 0);
+ break;
+ case BPF_H:
+ emit_insn(ctx, ldhu, dst, t1, 0);
+ break;
+ case BPF_W:
+ emit_insn(ctx, ldwu, dst, t1, 0);
+ break;
+ case BPF_DW:
+ emit_insn(ctx, ldd, dst, t1, 0);
+ break;
+ }
+ ret = add_exception_handler(insn, ctx, dst);
+ if (ret)
+ return ret;
+ emit_insn(ctx, dbar, 0b10100);
+ break;
+ }
switch (BPF_SIZE(insn->code)) {
case BPF_B:
if (is_signed_imm12(off)) {
@@ -668,6 +742,31 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx)
break;
/* store_release(dst_reg + off16, src_reg) */
case BPF_STORE_REL:
+ if (arena) {
+ /* t1 = dst + off + arena_vm_start; store to [t1]. */
+ emit_insn(ctx, dbar, 0b10010);
+ move_imm(ctx, t1, off);
+ emit_insn(ctx, addd, t1, dst, t1);
+ emit_insn(ctx, addd, t1, t1, REG_ARENA);
+ switch (BPF_SIZE(insn->code)) {
+ case BPF_B:
+ emit_insn(ctx, stb, src, t1, 0);
+ break;
+ case BPF_H:
+ emit_insn(ctx, sth, src, t1, 0);
+ break;
+ case BPF_W:
+ emit_insn(ctx, stw, src, t1, 0);
+ break;
+ case BPF_DW:
+ emit_insn(ctx, std, src, t1, 0);
+ break;
+ }
+ ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER);
+ if (ret)
+ return ret;
+ break;
+ }
emit_insn(ctx, dbar, 0b10010);
switch (BPF_SIZE(insn->code)) {
case BPF_B:
@@ -712,9 +811,6 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx)
return 0;
}
-#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
-#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
-#define REG_DONT_CLEAR_MARKER 0
bool ex_handler_bpf(const struct exception_table_entry *ex,
struct pt_regs *regs)
@@ -744,7 +840,8 @@ static int add_exception_handler(const struct bpf_insn *insn,
if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
- BPF_MODE(insn->code) != BPF_PROBE_MEM32SX)
+ BPF_MODE(insn->code) != BPF_PROBE_MEM32SX &&
+ BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
return 0;
if (WARN_ON_ONCE(ctx->num_exentries >= ctx->prog->aux->num_exentries))
@@ -1625,6 +1722,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
case BPF_STX | BPF_ATOMIC | BPF_H:
case BPF_STX | BPF_ATOMIC | BPF_W:
case BPF_STX | BPF_ATOMIC | BPF_DW:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_B:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_H:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
if (!bpf_atomic_is_load_store(insn))
ret = emit_atomic_rmw(insn, ctx);
else
@@ -2719,6 +2820,18 @@ bool bpf_jit_supports_arena(void)
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
+ if (!in_arena)
+ return true;
+
+ switch (insn->code) {
+ case BPF_STX | BPF_ATOMIC | BPF_W:
+ case BPF_STX | BPF_ATOMIC | BPF_DW:
+ /* cmpxchg on arena is emitted as amcas, see emit_atomic_rmw() */
+ if (insn->imm == BPF_CMPXCHG)
+ return cpu_has_lamcas;
+ break;
+ }
+
return true;
}
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 1:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <sashiko-outbox-243049@kernel.org>
2026-12-03 1:41 ` [PATCH v4 6/6] LoongArch: BPF: Add support for arena atomics George Guo
2026-12-01 9:51 [PATCH v4 0/6] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
2026-12-01 9:51 ` [PATCH v4 6/6] LoongArch: BPF: Add support for arena atomics George Guo
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®