Skip to contentSkip to conversation

powerpc32/bpf: fix BPF-to-BPF calls and instruction folding

PATCH In review Lukas Stockner 3 participants 7 messages Nov 22 – Nov 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover powerpc32/bpf: fix BPF-to-BPF calls and instruction folding

This series fixes four bugs in the 32-bit PowerPC eBPF JIT.

The current function prologue/epilogue assumes the calling convention for main programs, while generated calls follow helper calling convention. Therefore, patch 1 makes it generate a helper-style prologue/epilogue for subprograms instead.

Patch 2 and 3 are needed to fix the additional arguments in subprograms. Currently, the fifth argument is not being loaded in the prologue, and all of them may be overwritten by register reallocation logic when they're only passed on to another call.

Patch 4 is for the 3-operand folding logic. When the second instruction uses the same source and destination register, the current folding doesn't work because it will end up using the previous value of the register, even though it should have been written by the first instruction. We encountered this for the case of verifier-inserted zero- extension MOVs, but it also affects other ALU operations.

Testing:
We found these bugs in an eBPF-based CLAT program on various WiFi APs, where it would trigger kernels panics on PPC32 hardware (e.g. Enterasys WS-AP3715i with Freescale P1010 CPU).
While working on the fixes, I tested in QEMU on g3beige using small reproducers (generated by GLM 5.3 Flash).
Afterwards, we backported this series to v6.6 and tested it on the WS-AP3715i, and with the fixes applied the CLAT program now works fine. A previous version of this series (before some cleanups) has also been deployed on several APs in production without problems.

Lukas Stockner (4):
powerpc32/bpf: fix argument passing for subprograms powerpc32/bpf: fetch fifth argument in BPF subprograms powerpc32/bpf: mark all argument registers seen in function calls powerpc32/bpf: don't fold MOV into instructions that use dst_reg

 arch/powerpc/net/bpf_jit_comp32.c | 43 ++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 6 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/4 powerpc32/bpf: fix argument passing for subprograms

Main programs receive a single 32-bit pointer as the first argument, so the current prologue converts it to the 64-bit BPF_REG_1 by moving R3 to R4 and zeroing R3.

However, subprograms are called like helper functions, so there the first argument already is 64-bit - but the prologue still does its conversion, so the argument ends up with its halves swapped and the upper one zeroed. The same problem applies to the return value.

In particular, in the common case where the first argument is a context pointer, the callee ends up with the upper (zeroed) 32 bits, which then triggers a kernel panic as soon as it's used.

Therefore, make JIT emit the appropriate 64-bit prologue/epilogue for subprograms.

Fixes 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32")
---
 arch/powerpc/net/bpf_jit_comp32.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
arch/powerpc/net/bpf_jit_comp32.c +12 -4
index f5b9441cf46a..261ac0646b96 100644
@@ -179,9 +179,11 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
 	if (ctx->seen & SEEN_TAILCALL)
 		EMIT(PPC_RAW_STW(_R4, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC)));
 
-	/* First arg comes in as a 32 bits pointer. */
-	EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3));
-	EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0));
+	/* Programs receive the first arg as a 32 bits pointer, subprograms don't. */
+	if (!ctx->is_subprog) {
+		EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3));
+		EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0));
+	}
 
 	/*
 	 * We need a stack frame, but we don't necessarily need to
@@ -231,7 +233,13 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx
 
 void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context *ctx)
 {
-	EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0)));
+	/* Programs return a 32-bit value, while subprograms return 64 bit. */
+	if (ctx->is_subprog) {
+		EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0) - 1));
+		EMIT(PPC_RAW_MR(_R4, bpf_to_ppc(BPF_REG_0)));
+	} else {
+		EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0)));
+	}
 
 	bpf_jit_emit_common_epilogue(image, ctx);
 
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms

Le 22/11/2026 à 20:42, Lukas Stockner a écrit :

Quoted 11 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Main programs receive a single 32-bit pointer as the first argument, so the current prologue converts it to the 64-bit BPF_REG_1 by moving R3 to R4 and zeroing R3.

However, subprograms are called like helper functions, so there the first argument already is 64-bit - but the prologue still does its conversion, so the argument ends up with its halves swapped and the upper one zeroed. The same problem applies to the return value.

Quoted 8 lines

In particular, in the common case where the first argument is a context pointer, the callee ends up with the upper (zeroed) 32 bits, which then triggers a kernel panic as soon as it's used.

Therefore, make JIT emit the appropriate 64-bit prologue/epilogue for subprograms.

Fixes 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32")

Take care ctx->is_subprog only exists since commit 2ed2d8f6fb38 ("powerpc64/bpf: Support tailcalls with subprogs") which means automatic backport won't work.

Quoted 42 lines
---
  arch/powerpc/net/bpf_jit_comp32.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
arch/powerpc/net/bpf_jit_comp32.c +12 -5
index f5b9441cf46a..261ac0646b96 100644
@@ -179,9 +179,11 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
         if (ctx->seen & SEEN_TAILCALL)
                 EMIT(PPC_RAW_STW(_R4, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC)));
 
-       /* First arg comes in as a 32 bits pointer. */
-       EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3));
-       EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0));
+       /* Programs receive the first arg as a 32 bits pointer, subprograms don't. */
+       if (!ctx->is_subprog) {
+               EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3));
+               EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0));
+       }
 
         /*
          * We need a stack frame, but we don't necessarily need to
@@ -231,7 +233,13 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx
 
  void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context *ctx)
  {
-       EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0)));
+       /* Programs return a 32-bit value, while subprograms return 64 bit. */
+       if (ctx->is_subprog) {
+               EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0) - 1));
+               EMIT(PPC_RAW_MR(_R4, bpf_to_ppc(BPF_REG_0)));
+       } else {
+               EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0)));
+       }
 
         bpf_jit_emit_common_epilogue(image, ctx);
 
--

2.56.0

#3 lore raw

PATCH 2/4 powerpc32/bpf: fetch fifth argument in BPF subprograms

In reply to Lukas Stockner

Commit 7dd0e2848764 ("powerpc/bpf/32: BPF prog is never called with more than one arg") removed the BPF_REG_5 load since it's not used for program arguments, but that is only true for regular programs - subprograms may get up to five arguments, so the load is required there.

Fixes 7dd0e2848764 ("powerpc/bpf/32: BPF prog is never called with more than one arg")
---
 arch/powerpc/net/bpf_jit_comp32.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/powerpc/net/bpf_jit_comp32.c +7 -0
index 261ac0646b96..9f55dd4bdfd6 100644
@@ -199,6 +199,13 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
 		if (bpf_is_seen_register(ctx, i))
 			EMIT(PPC_RAW_STW(i, _R1, bpf_jit_stack_offsetof(ctx, i)));
 
+	/* If this is a subprogram, load fifth argument from the stack */
+	if (ctx->is_subprog && bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_5))) {
+		int offset = bpf_has_stack_frame(ctx) ? BPF_PPC_STACKFRAME(ctx) : 0;
+		EMIT(PPC_RAW_LWZ(bpf_to_ppc(BPF_REG_5) - 1, _R1, offset + 8));
+		EMIT(PPC_RAW_LWZ(bpf_to_ppc(BPF_REG_5), _R1, offset + 12));
+	}
+
 	/* Setup frame pointer to point to the bpf stack area */
 	if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_FP))) {
 		EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_FP) - 1, 0));
Signature · 1 line
2.56.0
#4 lore raw

PATCH 3/4 powerpc32/bpf: mark all argument registers seen in function calls

In reply to Lukas Stockner

The JIT code tracks which registers are used and remaps based on that, but currently does not account for the fact that function calls implicitly use registers because the called function will treat them as arguments.

Therefore, if a function merely passes through one of its arguments to another function call, it will incorrectly be considered unused and may be overwritten with another register's contents.

In practice, this is unlikely to affect main programs as they only get one argument (the context, which they are very likely to touch somehow in addition to passing it on), so they'll have to touch other registers if they want to pass them as arguments. Subprograms, however, are much likelier to hit this.

Similarly, tail calls pass R1 to the callee, so mark it used as well.

Fixes 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32")
Fixes a8936569a07b ("powerpc/bpf: Always reallocate BPF_REG_5, BPF_REG_AX and TMP_REG when possible")
---
 arch/powerpc/net/bpf_jit_comp32.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
arch/powerpc/net/bpf_jit_comp32.c +16 -1
index 9f55dd4bdfd6..7414e350d2d4 100644
@@ -339,7 +339,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 {
 	const struct bpf_insn *insn = fp->insnsi;
 	int flen = fp->len;
-	int i, ret;
+	int i, ret, max_arg;
 
 	/* Start of epilogue code - will only be valid 2nd pass onwards */
 	u32 exit_addr = addrs[flen];
@@ -1183,6 +1183,18 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 			if (ret < 0)
 				return ret;
 
+			/*
+			 * We don't know which arguments the called function will use,
+			 * so if this is a subprogram, assume the worst case where it
+			 * uses all of them. Regular programs only get one argument,
+			 * so any others would have been explicitly set.
+			 */
+			max_arg = ctx->is_subprog ? BPF_REG_5 : BPF_REG_1;
+			for (int reg = BPF_REG_1; reg <= max_arg; ++reg) {
+				bpf_set_seen_register(ctx, bpf_to_ppc(reg) - 1);
+				bpf_set_seen_register(ctx, bpf_to_ppc(reg));
+			}
+
 			if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_5))) {
 				EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5) - 1, _R1, 8));
 				EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5), _R1, 12));
@@ -1412,6 +1424,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 		 * Tail call
 		 */
 		case BPF_JMP | BPF_TAIL_CALL:
+			bpf_set_seen_register(ctx, bpf_to_ppc(BPF_REG_1) - 1);
+			bpf_set_seen_register(ctx, bpf_to_ppc(BPF_REG_1));
+
 			ctx->seen |= SEEN_TAILCALL;
 			ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
 			if (ret < 0)
Signature · 1 line
2.56.0
#5 lore raw

PATCH 4/4 powerpc32/bpf: don't fold MOV into instructions that use dst_reg

In reply to Lukas Stockner

The PPC32 eBPF JIT folds MOVs followed by an ALU operation to the same destination into 3-operand PPC instructions by dropping the MOV and reusing its source as the second operand for the second instruction.

That is wrong when the second instruction also uses the destination register as its source, since the MOV that would have assigned it is dropped, so the folded instruction will end up using the previous content of the register.

For example, "r2 = r1; r2 += r2" gets folded to "r2 = r1 + r2", while the correct result would be 2 * r1.

In particular, this also happens for zero-extension MOVs inserted by the verifier, where "r2 = r1; w2 = w2" becomes just "w2 = w2", so the actual assignment is skipped.

Fixes 19daf0aef84f ("powerpc/bpf/32: perform three operands ALU operations")
---
 arch/powerpc/net/bpf_jit_comp32.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/net/bpf_jit_comp32.c +2 -1
index 7414e350d2d4..3ba50d6696a2 100644
@@ -367,7 +367,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 		if (i && (BPF_CLASS(code) == BPF_ALU64 || BPF_CLASS(code) == BPF_ALU) &&
 		    (BPF_CLASS(prevcode) == BPF_ALU64 || BPF_CLASS(prevcode) == BPF_ALU) &&
 		    BPF_OP(prevcode) == BPF_MOV && BPF_SRC(prevcode) == BPF_X &&
-		    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1) {
+		    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1 &&
+		    !(BPF_SRC(code) == BPF_X && insn[i].src_reg == insn[i].dst_reg)) {
 			src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
 			src2_reg_h = src2_reg - 1;
 			ctx->idx = addrs[i - 1] / 4;
Signature · 1 line
2.56.0
#6 lore raw

Re: [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg

TL;DR: - PPC32 JIT MOV-fold fix is incomplete: several ALU handlers still read stale dst, giving wrong results - Pre-existing: fold ignores sign-extending MOV and jump targets, also computing wrong…
Quoted 1 line

powerpc32/bpf: don't fold MOV into instructions that use dst_reg

TL;DR:
- PPC32 JIT MOV-fold fix is incomplete: several ALU handlers still read stale dst, giving wrong results
- Pre-existing: fold ignores sign-extending MOV and jump targets, also computing wrong values

Quoted 3 lines
arch/powerpc/net/bpf_jit_comp32.c +0 -0

[ ... ]

arch/powerpc/net/bpf_jit_comp32.c int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code · line 374

Quoted 10 lines
arch/powerpc/net/bpf_jit_comp32.c +2 -1
@@ -367,7 +367,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 		if (i && (BPF_CLASS(code) == BPF_ALU64 || BPF_CLASS(code) == BPF_ALU) &&
 		    (BPF_CLASS(prevcode) == BPF_ALU64 || BPF_CLASS(prevcode) == BPF_ALU) &&
 		    BPF_OP(prevcode) == BPF_MOV && BPF_SRC(prevcode) == BPF_X &&
-		    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1) {
+		    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1 &&
+		    !(BPF_SRC(code) == BPF_X && insn[i].src_reg == insn[i].dst_reg)) {
 			src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
 			src2_reg_h = src2_reg - 1;
 			ctx->idx = addrs[i - 1] / 4;

The subject says the JIT should no longer fold a MOV into an instruction that uses dst_reg, but this check only covers the case where the explicit BPF_X source operand equals dst. Several handlers in the same switch still read dst_reg/dst_reg_h directly, or never write dst_reg_h (or anything at all), instead of using src2_reg/src2_reg_h. Folding still drops the MOV for these, so they compute from the stale destination register:

- BPF_ALU64 | BPF_RSH | BPF_X emits EMIT(PPC_RAW_SRW(tmp_reg, dst_reg_h, tmp_reg)), which reads dst_reg_h, while ARSH_X uses src2_reg_h in the same position. For "r2 = r1; r2 >>= r3" with r3 >= 32, the low word comes from the old high word of r2, not r1.

- BPF_ALU64 | BPF_MUL | BPF_K with a power-of-2 imm emits EMIT(PPC_RAW_RLWIMI(dst_reg_h, dst_reg, imm, 32 - imm, 31)), which reads the old low word of dst. LSH_K uses src2_reg at the same spot.

- BPF_END | BPF_FROM_LE with imm 16 emits EMIT(PPC_RAW_RLWIMI(dst_reg, src2_reg, 16, 0, 15)), which keeps bits 16-31 of the old dst, so "r2 = r1; r2 = le16 r2" returns (b0 << 8 | stale byte) instead of (b0 << 8 | b1).

- BPF_ALU | BPF_END | BPF_FROM_BE with imm 32/64 emits nothing (/* nop */), so after the rewind (ctx->idx = addrs[i - 1] / 4) the MOV disappears completely and "r2 = r1; r2 = be64 r2" leaves r2 unchanged. BPF_TO_BE is 0x08 == BPF_X but src_reg is required to be 0, so the new check only blocks this for dst r0.

- BPF_ALU64 | BPF_OR | BPF_K and BPF_XOR | BPF_K with imm >= 0, and BPF_AND | BPF_K with imm < 0, never write dst_reg_h. After the fold it should hold src2_reg_h, but it keeps the old high word: "r2 = r1; r2 |= 4" gives hi(r2) = old hi(r2). With imm == 0, OR_K and XOR_K emit nothing for the low word either.

- BPF_ALU64 | BPF_LSH | BPF_K with imm == 0 only runs EMIT(PPC_RAW_MR(dst_reg, src2_reg)), so the high word is not copied. RSH_K and ARSH_K emit both MRs.

The verifier accepts all of these sequences, and "copy then op on the copy" is a common shape in compiler output. c88da29b4d2c converted the other operands of the RSH_X sequence and missed this one.

Should the fold be restricted to an allow-list of handlers known to read only src2_reg/src2_reg_h and write all of dst, or should these handlers be converted to src2_reg* as part of this fix?

This next part isn't introduced by this patch, but it is in the condition being edited. Two more preconditions seem to be missing, and both let the fold drop work that the MOV must still do.

First, insn[i - 1].off is not checked. Since 8ecf3c1dab1c, ALU and ALU64 MOV X with off 8/16/32 is a sign-extending move (EXTSB/EXTSH/SRAWI in the BPF_MOV handler). The fold replaces it with a plain register reference, so "r2 = (s8)r1; r2 += r3" becomes:

addc r2,r1,r3
adde r2h,r1h,r3h

and the sign extension is lost. The same happens for "w2 = (s16)w1; w2 += w3".

Second, the fold does not check whether insn i is a jump target. When it folds, addrs[i] == addrs[i - 1], and jumps branch to addrs[i + 1 + off], so a jump that skips the MOV lands on the folded instruction, which reads the MOV's source:

    0: r2 = r5
    1: if r3 == 0 goto 3
    2: r2 = r1
    3: r2 += r4

When r3 == 0, the folded insn 3 computes r1 + r4 instead of r5 + r4.

Both cases make the JIT compute a value other than the one the verifier tracked. Since this patch is already tightening the condition, should it also require insn[i - 1].off == 0 and skip the fold when insn i is a jump target?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45063388022