Skip to contentSkip to conversation

LoongArch: BPF: Don't override subprog's return value

PATCH Applied Hengqi Chen 3 participants 5 messages Mar 25, 2025 – Mar 30, 2025

Subsystems (3)
lore
Share & tools
lore

Hengqi Chen

author reviewer
#0 lore raw

PATCH 1/1 LoongArch: BPF: Don't override subprog's return value

The verifier test calls: div by 0 in subprog triggers a panic at the ld.bu instruction. The ld.bu insn is trying to load byte from memory address returned by the subprog. The subprog actually set the correct address at the a5 register (dedicated register for BPF return values). But at commit 73c359d1d356 ("LoongArch: BPF: Sign-extend return values") we also sign extended a5 to the a0 register (return value in LoongArch). For function call insn, we later propagate the a0 register back to a5 register. This is right for native calls but wrong for bpf2bpf calls which expect zero-extended return value in a5 register. So only move a0 to a5 for native calls (i.e. non-BPF_PSEUDO_CALL).

Fixes 73c359d1d356 ("LoongArch: BPF: Sign-extend return values")
---
 arch/loongarch/net/bpf_jit.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/loongarch/net/bpf_jit.c +3 -1
index a06bf89fed67..73ff1a657aa5 100644
@@ -907,7 +907,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
 		move_addr(ctx, t1, func_addr);
 		emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);
-		move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
+
+		if (insn->src_reg != BPF_PSEUDO_CALL)
+			move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
 		break;
 
 	/* tail call */
Signature · 1 line
2.43.5
maintainer
#1 lore raw
Quoted 22 lines · Hengqi Chen

The verifier test calls: div by 0 in subprog triggers a panic at the ld.bu instruction. The ld.bu insn is trying to load byte from memory address returned by the subprog. The subprog actually set the correct address at the a5 register (dedicated register for BPF return values). But at commit 73c359d1d356 ("LoongArch: BPF: Sign-extend return values") we also sign extended a5 to the a0 register (return value in LoongArch). For function call insn, we later propagate the a0 register back to a5 register. This is right for native calls but wrong for bpf2bpf calls which expect zero-extended return value in a5 register. So only move a0 to a5 for native calls (i.e. non-BPF_PSEUDO_CALL).

Fixes 73c359d1d356 ("LoongArch: BPF: Sign-extend return values")
---
  arch/loongarch/net/bpf_jit.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/loongarch/net/bpf_jit.c +0 -0
index a06bf89fed67..73ff1a657aa5 100644

arch/loongarch/net/bpf_jit.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext · line 915

Quoted 10 lines
arch/loongarch/net/bpf_jit.c +3 -1
@@ -907,7 +907,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
  		move_addr(ctx, t1, func_addr);
  		emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);
-		move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
+
+		if (insn->src_reg != BPF_PSEUDO_CALL)
+			move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
  		break;
 
  	/* tail call */

In my opinion, it is better to give a test case and show the test result without and with this change.

The test case should be put in the commit message at least and then added into tools/testing/selftests/bpf or lib/test_bpf.c if not exist.

Thanks,
Tiezhu

maintainer
#2 lore raw
Quoted 36 lines · Tiezhu Yang
Quoted 23 lines · Hengqi Chen

The verifier test calls: div by 0 in subprog triggers a panic at the ld.bu instruction. The ld.bu insn is trying to load byte from memory address returned by the subprog. The subprog actually set the correct address at the a5 register (dedicated register for BPF return values). But at commit 73c359d1d356 ("LoongArch: BPF: Sign-extend return values") we also sign extended a5 to the a0 register (return value in LoongArch). For function call insn, we later propagate the a0 register back to a5 register. This is right for native calls but wrong for bpf2bpf calls which expect zero-extended return value in a5 register. So only move a0 to a5 for native calls (i.e. non-BPF_PSEUDO_CALL).

Fixes 73c359d1d356 ("LoongArch: BPF: Sign-extend return values")

---
  arch/loongarch/net/bpf_jit.c | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
arch/loongarch/net/bpf_jit.c +0 -0
index a06bf89fed67..73ff1a657aa5 100644
@@ -907,7 +907,9 @@ static int build_insn(const struct bpf_insn *insn, 

struct jit_ctx *ctx, bool ext

Quoted 8 lines

          move_addr(ctx, t1, func_addr); emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0); -        move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0); +
+        if (insn->src_reg != BPF_PSEUDO_CALL)
+            move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0); break;
      /* tail call */

In my opinion, it is better to give a test case and show the test result without and with this change.

Quoted 2 lines

The test case should be put in the commit message at least and then added into tools/testing/selftests/bpf or lib/test_bpf.c if not exist.

Oh, it seems that there is a test case calls: div by 0 in subprog, so it will be great to add a test case in lib/test_bpf.c to test bpf jit if possible.

Thanks,
Tiezhu

author reviewer
#3 lore raw

Hi Tiezhu,

Quoted 38 lines · Tiezhu Yang
Quoted 36 lines · Tiezhu Yang
Quoted 25 lines · Hengqi Chen

The verifier test calls: div by 0 in subprog triggers a panic at the ld.bu instruction. The ld.bu insn is trying to load byte from memory address returned by the subprog. The subprog actually set the correct address at the a5 register (dedicated register for BPF return values). But at commit 73c359d1d356 ("LoongArch: BPF: Sign-extend return values") we also sign extended a5 to the a0 register (return value in LoongArch). For function call insn, we later propagate the a0 register back to a5 register. This is right for native calls but wrong for bpf2bpf calls which expect zero-extended return value in a5 register. So only move a0 to a5 for native calls (i.e. non-BPF_PSEUDO_CALL).

Fixes 73c359d1d356 ("LoongArch: BPF: Sign-extend return values")
---
  arch/loongarch/net/bpf_jit.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/loongarch/net/bpf_jit.c +0 -0
index a06bf89fed67..73ff1a657aa5 100644
@@ -907,7 +907,9 @@ static int build_insn(const struct bpf_insn *insn,

struct jit_ctx *ctx, bool ext

          move_addr(ctx, t1, func_addr);
          emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);

arch/loongarch/net/bpf_jit.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext · line 915

Quoted 6 lines
arch/loongarch/net/bpf_jit.c +3 -1
-        move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
+
+        if (insn->src_reg != BPF_PSEUDO_CALL)
+            move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
          break;
      /* tail call */

In my opinion, it is better to give a test case and show the test result without and with this change.

Quoted 7 lines
Quoted 2 lines

The test case should be put in the commit message at least and then added into tools/testing/selftests/bpf or lib/test_bpf.c if not exist.

Oh, it seems that there is a test case calls: div by 0 in subprog, so it will be great to add a test case in lib/test_bpf.c to test bpf jit if possible.

Don't think this is necessary, the verifier test is well maintained and part of the BPF CI.

Actually, I am not familiar with the test_bpf.ko :)

Quoted 3 lines

Thanks,
Tiezhu

maintainer Applied
#4 lore raw
In reply to Hengqi Chen

Applied, thanks.

Huacai

Quoted 36 lines · Hengqi Chen

The verifier test calls: div by 0 in subprog triggers a panic at the ld.bu instruction. The ld.bu insn is trying to load byte from memory address returned by the subprog. The subprog actually set the correct address at the a5 register (dedicated register for BPF return values). But at commit 73c359d1d356 ("LoongArch: BPF: Sign-extend return values") we also sign extended a5 to the a0 register (return value in LoongArch). For function call insn, we later propagate the a0 register back to a5 register. This is right for native calls but wrong for bpf2bpf calls which expect zero-extended return value in a5 register. So only move a0 to a5 for native calls (i.e. non-BPF_PSEUDO_CALL).

Fixes 73c359d1d356 ("LoongArch: BPF: Sign-extend return values")
---
 arch/loongarch/net/bpf_jit.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/loongarch/net/bpf_jit.c +3 -2
index a06bf89fed67..73ff1a657aa5 100644
@@ -907,7 +907,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
                move_addr(ctx, t1, func_addr);
                emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);
-               move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
+
+               if (insn->src_reg != BPF_PSEUDO_CALL)
+                       move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
                break;
 
        /* tail call */
--

2.43.5