LoongArch: BPF: Fix sign extension for 12-bit immediates
Subsystems (3)
PATCH 1/1 LoongArch: BPF: Fix sign extension for 12-bit immediates
From: George Guo <[email protected]>
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.
---
--- arch/loongarch/net/bpf_jit.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/loongarch/net/bpf_jit.h +2 -1
@@ -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); +return ; }/* ori rd, $zero, imm_11_0 */
---
base-commit: 6146a0f1dfae5d37442a9ddcba012add260bceb0
change-id: 20251103-1-96faa240e8f4
Best regards,
Signature · 1 line
george <[email protected]>
Hengqi Chen
reviewerQuoted 12 lines · george
From: George Guo <[email protected]>
When loading immediate values that fit within 12-bit signed range, the
move_immfunction incorrectly used zero extension instead of sign extension.The bug was exposed when
scx_simplescheduler failed with-EINVALinops.init()after passing node = -1 toscx_bpf_create_dsq(). Due to incorrect sign extension,node >= (int)nr_node_idsevaluated to true instead of false, causing BPF program failure.
Which bpf prog are you referring to?
Quoted 22 lines
Verified by testing with the
scx_simplescheduler (located intools/sched_ext/). After building withmakeand 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 <[email protected]>Signed-off-by George Guo <[email protected]>---
Signed-off-by george <[email protected]>--- arch/loongarch/net/bpf_jit.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/loongarch/net/bpf_jit.h +0
-0
arch/loongarch/net/bpf_jit.h static inline void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm · line 127
Quoted 7 lines
arch/loongarch/net/bpf_jit.h +2
-1@@ -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); +return ; }
This causes kernel panic on existing bpf selftests.
Quoted 11 lines
arch/loongarch/net/bpf_jit.h +0
-1/* ori rd, $zero, imm_11_0 */ ---base-commit:
6146a0f1dfae5d37442a9ddcba012add260bceb0
change-id: 20251103-1-96faa240e8f4Best regards,
--
george <[email protected]>
Quoted 8 lines · On Tue, 4 Nov 2025 14:53:04 +0800 Hengqi Chen <[email protected]> wrote:
Quoted 7 lines · george
From: George Guo <[email protected]>
When loading immediate values that fit within 12-bit signed range, the
move_immfunction incorrectly used zero extension instead of sign extension.
Quoted 7 lines
Quoted 5 lines
The bug was exposed when
scx_simplescheduler failed with-EINVALinops.init()after passing node = -1 toscx_bpf_create_dsq(). Due to incorrect sign extension,node >= (int)nr_node_idsevaluated to true instead of false, causing BPF program failure.Which bpf prog are you referring to?
this bpf prog: ./tools/sched_ext/build/bin/scx_simple
Quoted 17 lines
Quoted 17 lines
Verified by testing with the
scx_simplescheduler (located intools/sched_ext/). After building withmakeand 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 <[email protected]>Signed-off-by George Guo <[email protected]>---
Signed-off-by george <[email protected]>--- arch/loongarch/net/bpf_jit.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 14 lines
arch/loongarch/net/bpf_jit.h static inline void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm · line 127
Quoted 12 lines
(quoted diff) +0
-0
b/arch/loongarch/net/bpf_jit.hindex5697158fd1645fdc3d83f598b00a9e20dfaa8f6d..f1398eb135b69ae61a27ed81f80b4bb0788cf0a0100644 ---a/arch/loongarch/net/bpf_jit.h+++b/arch/loongarch/net/bpf_jit.h@@ -122,7 +122,8 @@ static inline
void move_imm(structjit_ctx*ctx, enumloongarch_gprrd, long imm/* addiw rd, $zero, imm_11_0 */ if (is_signed_imm12(imm)) { emit_insn(ctx, addiw, rd, LOONGARCH_GPR_ZERO, imm);arch/loongarch/net/bpf_jit.h +2
-1- goto zext; +emit_sext_32 (ctx, rd, is32); +return ; }This causes kernel panic on existing bpf selftests.
Hi Hengqi,
I tried there would kerenl panic even without the patch in kernle 6.18.
The patch is needed, please consider merging it.
Thanks!
Quoted 11 lines
Quoted 11 lines
/* ori rd, $zero, imm_11_0 */---
base-commit:6146a0f1dfae5d37442a9ddcba012add260bceb0
change-id: 20251103-1-96faa240e8f4Best regards,
--
george <[email protected]>
Quoted 33 lines · george
From: George Guo <[email protected]>
When loading immediate values that fit within 12-bit signed range, the
move_immfunction incorrectly used zero extension instead of sign extension.The bug was exposed when
scx_simplescheduler failed with-EINVALinops.init()after passing node = -1 toscx_bpf_create_dsq(). Due to incorrect sign extension,node >= (int)nr_node_idsevaluated to true instead of false, causing BPF program failure.Verified by testing with the
scx_simplescheduler (located intools/sched_ext/). After building withmakeand 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 <[email protected]>Signed-off-by George Guo <[email protected]>---
Signed-off-by george <[email protected]>---
arch/loongarch/net/bpf_jit.h | 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)arch/loongarch/net/bpf_jit.h +0
-0
arch/loongarch/net/bpf_jit.h static inline void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm · line 125
Quoted 5 lines
arch/loongarch/net/bpf_jit.h +1
-1@@ -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?
Signature · 1 line
Xi Ruoyao <[email protected]>
Quoted 29 lines · On Fri, 19 Dec 2025 17:33:17 +0800 Xi Ruoyao <[email protected]> wrote:
Quoted 28 lines · george
From: George Guo <[email protected]>
When loading immediate values that fit within 12-bit signed range, the
move_immfunction incorrectly used zero extension instead of sign extension.The bug was exposed when
scx_simplescheduler failed with-EINVALinops.init()after passing node = -1 toscx_bpf_create_dsq(). Due to incorrect sign extension,node >= (int)nr_node_idsevaluated to true instead of false, causing BPF program failure.Verified by testing with the
scx_simplescheduler (located intools/sched_ext/). After building withmakeand 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 <[email protected]>Signed-off-by George Guo <[email protected]>---
Signed-off-by george <[email protected]>---
arch/loongarch/net/bpf_jit.h | 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 14 lines
arch/loongarch/net/bpf_jit.h static inline void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm · line 125
Quoted 10 lines
(quoted diff) +0
-0
b/arch/loongarch/net/bpf_jit.hindex5697158fd1645fdc3d83f598b00a9e20dfaa8f6d..f1398eb135b69ae61a27ed81f80b4bb0788cf0a0100644 ---a/arch/loongarch/net/bpf_jit.h+++b/arch/loongarch/net/bpf_jit.h@@ -122,7 +122,8 @@ static inline
void move_imm(structjit_ctx*ctx, enumloongarch_gprrd, long imm
/* addiw rd, $zero,imm_11_0*/ if (is_signed_imm12(imm)) {emit_insn(ctx, addiw, rd, LOONGARCH_GPR_ZERO, imm);arch/loongarch/net/bpf_jit.h +1
-1- 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!
Hengqi Chen
reviewerQuoted 47 lines · George Guo
Quoted 29 lines · On Fri, 19 Dec 2025 17:33:17 +0800 Xi Ruoyao <[email protected]> wrote:
Quoted 28 lines · george
From: George Guo <[email protected]>
When loading immediate values that fit within 12-bit signed range, the
move_immfunction incorrectly used zero extension instead of sign extension.The bug was exposed when
scx_simplescheduler failed with-EINVALinops.init()after passing node = -1 toscx_bpf_create_dsq(). Due to incorrect sign extension,node >= (int)nr_node_idsevaluated to true instead of false, causing BPF program failure.Verified by testing with the
scx_simplescheduler (located intools/sched_ext/). After building withmakeand 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 <[email protected]>Signed-off-by George Guo <[email protected]>---
Signed-off-by george <[email protected]>--- arch/loongarch/net/bpf_jit.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)Quoted 14 lines
arch/loongarch/net/bpf_jit.h static inline void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm · line 125
Quoted 10 lines
(quoted diff) +0
-0
b/arch/loongarch/net/bpf_jit.hindex5697158fd1645fdc3d83f598b00a9e20dfaa8f6d..f1398eb135b69ae61a27ed81f80b4bb0788cf0a0100644 ---a/arch/loongarch/net/bpf_jit.h+++b/arch/loongarch/net/bpf_jit.h@@ -122,7 +122,8 @@ static inline
void move_imm(structjit_ctx*ctx, enumloongarch_gprrd, long imm/* addiw rd, $zero, imm_11_0 */ if (is_signed_imm12(imm)) { emit_insn(ctx, addiw, rd, LOONGARCH_GPR_ZERO, imm);arch/loongarch/net/bpf_jit.h +1
-1- 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?
Quoted 4 lines
Hi Ruoyao,
I tried, it's not easy to do that.
It's better merge this patch, then consider next step.
The test_bpf.ko test failed, so probably this is the wrong fix.
Quoted 1 line
Thanks!