LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding
@ 2026-11-22 19:42 Lukas Stockner
  2026-11-22 19:42 ` [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Lukas Stockner @ 2026-11-22 19:42 UTC (permalink / raw)
  To: Hari Bathini, Christophe Leroy, bpf, linuxppc-dev
  Cc: Naveen N Rao, Madhavan Srinivasan, Michael Ellerman,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Lukas Stockner

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(-)

-- 
2.56.0



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms
  2026-11-22 19:42 [PATCH 0/4] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
@ 2026-11-22 19:42 ` Lukas Stockner
  2026-11-23  7:46   ` Christophe Leroy (CS GROUP)
  2026-11-22 19:42 ` [PATCH 2/4] powerpc32/bpf: fetch fifth argument in BPF subprograms Lukas Stockner
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Lukas Stockner @ 2026-11-22 19:42 UTC (permalink / raw)
  To: Hari Bathini, Christophe Leroy, bpf, linuxppc-dev
  Cc: Naveen N Rao, Madhavan Srinivasan, Michael Ellerman,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Lukas Stockner

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")
Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
---
 arch/powerpc/net/bpf_jit_comp32.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index f5b9441cf46a..261ac0646b96 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -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



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/4] powerpc32/bpf: fetch fifth argument in BPF subprograms
  2026-11-22 19:42 [PATCH 0/4] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
  2026-11-22 19:42 ` [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
@ 2026-11-22 19:42 ` Lukas Stockner
  2026-11-22 19:42 ` [PATCH 3/4] powerpc32/bpf: mark all argument registers seen in function calls Lukas Stockner
  2026-11-22 19:42 ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg Lukas Stockner
  3 siblings, 0 replies; 8+ messages in thread
From: Lukas Stockner @ 2026-11-22 19:42 UTC (permalink / raw)
  To: Hari Bathini, Christophe Leroy, bpf, linuxppc-dev
  Cc: Naveen N Rao, Madhavan Srinivasan, Michael Ellerman,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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")
Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
---
 arch/powerpc/net/bpf_jit_comp32.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index 261ac0646b96..9f55dd4bdfd6 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -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));
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/4] powerpc32/bpf: mark all argument registers seen in function calls
  2026-11-22 19:42 [PATCH 0/4] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
  2026-11-22 19:42 ` [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
  2026-11-22 19:42 ` [PATCH 2/4] powerpc32/bpf: fetch fifth argument in BPF subprograms Lukas Stockner
@ 2026-11-22 19:42 ` Lukas Stockner
  2026-11-22 19:42 ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg Lukas Stockner
  3 siblings, 0 replies; 8+ messages in thread
From: Lukas Stockner @ 2026-11-22 19:42 UTC (permalink / raw)
  To: Hari Bathini, Christophe Leroy, bpf, linuxppc-dev
  Cc: Naveen N Rao, Madhavan Srinivasan, Michael Ellerman,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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")
Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
---
 arch/powerpc/net/bpf_jit_comp32.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index 9f55dd4bdfd6..7414e350d2d4 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -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)
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg
  2026-11-22 19:42 [PATCH 0/4] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
                   ` (2 preceding siblings ...)
  2026-11-22 19:42 ` [PATCH 3/4] powerpc32/bpf: mark all argument registers seen in function calls Lukas Stockner
@ 2026-11-22 19:42 ` Lukas Stockner
  2026-11-22 20:14   ` bot+bpf-ci
  2026-11-28 18:32   ` Christophe Leroy (CS GROUP)
  3 siblings, 2 replies; 8+ messages in thread
From: Lukas Stockner @ 2026-11-22 19:42 UTC (permalink / raw)
  To: Hari Bathini, Christophe Leroy, bpf, linuxppc-dev
  Cc: Naveen N Rao, Madhavan Srinivasan, Michael Ellerman,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	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")
Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
---
 arch/powerpc/net/bpf_jit_comp32.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index 7414e350d2d4..3ba50d6696a2 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -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;
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg
  2026-11-22 19:42 ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg Lukas Stockner
@ 2026-11-22 20:14   ` bot+bpf-ci
  2026-11-28 18:32   ` Christophe Leroy (CS GROUP)
  1 sibling, 0 replies; 8+ messages in thread
From: bot+bpf-ci @ 2026-11-22 20:14 UTC (permalink / raw)
  To: lukas, hbathini, chleroy, bpf, linuxppc-dev
  Cc: naveen, maddy, mpe, ast, daniel, andrii, lukas, ast, andrii,
	daniel, martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 4873 bytes --]

> 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

> diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
> --- a/arch/powerpc/net/bpf_jit_comp32.c
> +++ b/arch/powerpc/net/bpf_jit_comp32.c

[ ... ]

> @@ -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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms
  2026-11-22 19:42 ` [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
@ 2026-11-23  7:46   ` Christophe Leroy (CS GROUP)
  0 siblings, 0 replies; 8+ messages in thread
From: Christophe Leroy (CS GROUP) @ 2026-11-23  7:46 UTC (permalink / raw)
  To: Lukas Stockner, Hari Bathini, bpf, linuxppc-dev
  Cc: Naveen N Rao, Madhavan Srinivasan, Michael Ellerman,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko



Le 22/11/2026 à 20:42, Lukas Stockner a écrit :
> [Vous ne recevez pas souvent de courriers de lukas@lukasstockner.de. 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.
> 
> 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.

> Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
> ---
>   arch/powerpc/net/bpf_jit_comp32.c | 16 ++++++++++++----
>   1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
> index f5b9441cf46a..261ac0646b96 100644
> --- a/arch/powerpc/net/bpf_jit_comp32.c
> +++ b/arch/powerpc/net/bpf_jit_comp32.c
> @@ -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
> 



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg
  2026-11-22 19:42 ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg Lukas Stockner
  2026-11-22 20:14   ` bot+bpf-ci
@ 2026-11-28 18:32   ` Christophe Leroy (CS GROUP)
  1 sibling, 0 replies; 8+ messages in thread
From: Christophe Leroy (CS GROUP) @ 2026-11-28 18:32 UTC (permalink / raw)
  To: Lukas Stockner, Hari Bathini, bpf, linuxppc-dev
  Cc: Naveen N Rao, Madhavan Srinivasan, Michael Ellerman,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko



Le 22/11/2026 à 20:42, Lukas Stockner a écrit :
> [Vous ne recevez pas souvent de courriers de lukas@lukasstockner.de. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
> 
> 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.

You exemple is "r2 = r1; r2 = r2 + r2", it should become "r2 = r1 + r1".

I think you should do that instead:

diff --git a/arch/powerpc/net/bpf_jit_comp32.c 
b/arch/powerpc/net/bpf_jit_comp32.c
index bfdc50740da8..a2ec60f2f5da 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -355,6 +355,10 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 
*image, u32 *fimage, struct code
  		    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1) {
  			src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
  			src2_reg_h = src2_reg - 1;
+			if (src_reg == dst_reg) {
+				src_reg = src2_reg;
+				src_reg_h = src2_reg_h;
+			}
  			ctx->idx = addrs[i - 1] / 4;
  		}


> 
> 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.

What is w2 ?

Are you talking about the zero extension added by commit 8ecf3c1dab1c 
("powerpc/bpf/32: Fix failing test_bpf tests") ? Maybe I missed 
something in that commit which was after commit 19daf0aef84f.

Or you are just talking about the /* special mov32 for zext */ ? This 
one is normally skipped by insn[i - 1].imm != 1

> 
> Fixes: 19daf0aef84f ("powerpc/bpf/32: perform three operands ALU operations")
> Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
> ---
>   arch/powerpc/net/bpf_jit_comp32.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
> index 7414e350d2d4..3ba50d6696a2 100644
> --- a/arch/powerpc/net/bpf_jit_comp32.c
> +++ b/arch/powerpc/net/bpf_jit_comp32.c
> @@ -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;

What about that instead:

diff --git a/arch/powerpc/net/bpf_jit_comp32.c 
b/arch/powerpc/net/bpf_jit_comp32.c
index bfdc50740da8..842498efa414 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -352,9 +352,14 @@ 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 &&
+		    !insn[i - 1].off) {
  			src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
  			src2_reg_h = src2_reg - 1;
+			if (src_reg == dst_reg) {
+				src_reg = src2_reg;
+				src_reg_h = src2_reg_h;
+			}
  			ctx->idx = addrs[i - 1] / 4;
  		}




^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-28 18:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 19:42 [PATCH 0/4] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
2026-11-22 19:42 ` [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
2026-11-23  7:46   ` Christophe Leroy (CS GROUP)
2026-11-22 19:42 ` [PATCH 2/4] powerpc32/bpf: fetch fifth argument in BPF subprograms Lukas Stockner
2026-11-22 19:42 ` [PATCH 3/4] powerpc32/bpf: mark all argument registers seen in function calls Lukas Stockner
2026-11-22 19:42 ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg Lukas Stockner
2026-11-22 20:14   ` bot+bpf-ci
2026-11-28 18:32   ` Christophe Leroy (CS GROUP)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox