Re: [PATCH v3 5/5] LoongArch: BPF: Add support for arena atomics

From: Tiezhu Yang

Date: Mon Nov 30 2026 - 04:52:09 EST


On 11/26/26 11:40 AM, George Guo wrote:
From: George Guo <guodongtai@xxxxxxxxxx>

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.

...

static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
{
const u8 t1 = LOONGARCH_GPR_T1;
@@ -429,9 +436,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;

...

+ bool zext = false;
+ u8 zext_reg = src;

...

switch (imm) {
@@ -502,15 +515,14 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amadddbd, src, t1, t3);
break;
}
- if (!isdw)
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = !isdw;
break;
case BPF_AND | BPF_FETCH:
if (isdw) {
emit_insn(ctx, amanddbd, src, t1, t3);
} else {
emit_insn(ctx, amanddbw, src, t1, t3);
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = true;
}
break;
case BPF_OR | BPF_FETCH:
@@ -518,7 +530,7 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amordbd, src, t1, t3);
} else {
emit_insn(ctx, amordbw, src, t1, t3);
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = true;
}
break;
case BPF_XOR | BPF_FETCH:
@@ -526,7 +538,7 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amxordbd, src, t1, t3);
} else {
emit_insn(ctx, amxordbw, src, t1, t3);
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = true;
}
break;
/* src = atomic_xchg(dst + off, src); */
@@ -553,11 +565,30 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amswapdbd, src, t1, t3);
break;
}
- if (!isdw)
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = !isdw;

...

+
+ if (zext)
+ emit_insn(ctx, bstrpickd, zext_reg, zext_reg, 31, 0);
+
return 0;
}

To make this patch easier to review, please split the refactoring
of the 5 bstrpick.d zero-extensions on src (using the zext flag)
into a separate preparatory cleanup patch right before this one.

That way, the preparatory patch is a pure no-op refactoring, and
this patch will focus solely on the arena atomics additions.

Thanks,
Tiezhu