[PATCH v4 5/6] LoongArch: BPF: Consolidate zero-extension in emit_atomic_rmw()
From: George Guo
Date: Tue Dec 01 2026 - 04:51:46 EST
From: George Guo <guodongtai@xxxxxxxxxx>
Currently, emit_atomic_rmw() emits 'bstrpickd' right after the 32-bit
fetch and xchg instructions in five places, to zero-extend the old value
loaded into src.
In order to support atomics on arena, a later patch adds an exception
table entry for the memory access instruction. add_exception_handler()
takes the last emitted instruction as the one that can fault, so the
zero-extension must come after the entry is added.
So set a zext flag in these five places instead, and emit 'bstrpickd'
once after the switch statement. The emitted instructions are the same.
This is preparation for later patch, no functional changes.
Signed-off-by: George Guo <guodongtai@xxxxxxxxxx>
---
arch/loongarch/net/bpf_jit.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 423c9807a84a..b78d38559598 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -461,6 +461,7 @@ 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;
move_imm(ctx, t1, off);
emit_insn(ctx, addd, t1, dst, t1);
@@ -534,15 +535,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:
@@ -550,7 +550,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:
@@ -558,7 +558,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); */
@@ -585,8 +585,7 @@ 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;
break;
/* r0 = atomic_cmpxchg(dst + off, r0, src); */
case BPF_CMPXCHG:
@@ -614,6 +613,9 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
return -EINVAL;
}
+ if (zext)
+ emit_insn(ctx, bstrpickd, src, src, 31, 0);
+
return 0;
}
--
2.53.0