Re: [PATCH] LoongArch: BPF: Fix sign extension for 12-bit immediates
From: George Guo
Date: Mon Dec 29 2025 - 02:06:45 EST
On Fri, 19 Dec 2025 17:33:17 +0800
Xi Ruoyao <xry111@xxxxxxxxxxx> wrote:
> On Mon, 2025-11-03 at 16:42 +0800, george wrote:
> > From: George Guo <guodongtai@xxxxxxxxxx>
> >
> > When loading immediate values that fit within 12-bit signed range,
> > the move_imm function incorrectly used zero extension instead of
> > sign extension.
> >
> > The bug was exposed when scx_simple scheduler failed with -EINVAL
> > in ops.init() after passing node = -1 to scx_bpf_create_dsq().
> > Due to incorrect sign extension, `node >= (int)nr_node_ids`
> > evaluated to true instead of false, causing BPF program failure.
> >
> > Verified by testing with the scx_simple scheduler (located in
> > tools/sched_ext/). After building with `make` and running
> > ./tools/sched_ext/build/bin/scx_simple, the scheduler now
> > initializes successfully with this fix.
> >
> > Fix this by using sign extension (sext) instead of zero extension
> > for signed immediate values in move_imm.
> >
> > Fixes: 5dc615520c4d ("LoongArch: Add BPF JIT support")
> > Reported-by: Bing Huang <huangbing@xxxxxxxxxx>
> > Signed-off-by: George Guo <guodongtai@xxxxxxxxxx>
> > ---
> > Signed-off-by: george <dongtai.guo@xxxxxxxxx>
> > ---
> > arch/loongarch/net/bpf_jit.h | 3 ++-
> > 1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/loongarch/net/bpf_jit.h
> > b/arch/loongarch/net/bpf_jit.h index
> > 5697158fd1645fdc3d83f598b00a9e20dfaa8f6d..f1398eb135b69ae61a27ed81f80b4bb0788cf0a0
> > 100644 --- a/arch/loongarch/net/bpf_jit.h +++
> > b/arch/loongarch/net/bpf_jit.h @@ -122,7 +122,8 @@ static inline
> > void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm
> > /* addiw rd, $zero, imm_11_0 */ if (is_signed_imm12(imm)) {
> > emit_insn(ctx, addiw, rd, LOONGARCH_GPR_ZERO, imm);
> > - goto zext;
> > + emit_sext_32(ctx, rd, is32);
>
> The addi.w instruction already produces the sign-extended value. Why
> do we need to sign-extend it again?
>
Hi Ruoyao,
I tried, it's not easy to do that.
It's better merge this patch, then consider next step.
Thanks!