* [PATCH v2 1/7] powerpc32/bpf: fix argument passing for subprograms
2026-12-01 16:53 [PATCH v2 0/7] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
@ 2026-12-01 16:53 ` Lukas Stockner
2026-12-01 16:53 ` [PATCH v2 2/7] powerpc32/bpf: fetch fifth argument in BPF subprograms Lukas Stockner
` (5 subsequent siblings)
6 siblings, 0 replies; 13+ messages in thread
From: Lukas Stockner @ 2026-12-01 16:53 UTC (permalink / raw)
To: Hari Bathini, Christophe Leroy, Naveen N Rao, bpf, linuxppc-dev
Cc: 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] 13+ messages in thread* [PATCH v2 2/7] powerpc32/bpf: fetch fifth argument in BPF subprograms
2026-12-01 16:53 [PATCH v2 0/7] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
2026-12-01 16:53 ` [PATCH v2 1/7] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
@ 2026-12-01 16:53 ` Lukas Stockner
2026-12-01 16:53 ` [PATCH v2 3/7] powerpc32/bpf: mark all argument registers seen in function calls Lukas Stockner
` (4 subsequent siblings)
6 siblings, 0 replies; 13+ messages in thread
From: Lukas Stockner @ 2026-12-01 16:53 UTC (permalink / raw)
To: Hari Bathini, Christophe Leroy, Naveen N Rao, bpf, linuxppc-dev
Cc: 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] 13+ messages in thread* [PATCH v2 3/7] powerpc32/bpf: mark all argument registers seen in function calls
2026-12-01 16:53 [PATCH v2 0/7] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
2026-12-01 16:53 ` [PATCH v2 1/7] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
2026-12-01 16:53 ` [PATCH v2 2/7] powerpc32/bpf: fetch fifth argument in BPF subprograms Lukas Stockner
@ 2026-12-01 16:53 ` Lukas Stockner
2026-12-01 17:46 ` bot+bpf-ci
2026-12-01 16:53 ` [PATCH v2 4/7] powerpc32/bpf: don't fold MOVSX into following instruction Lukas Stockner
` (3 subsequent siblings)
6 siblings, 1 reply; 13+ messages in thread
From: Lukas Stockner @ 2026-12-01 16:53 UTC (permalink / raw)
To: Hari Bathini, Christophe Leroy, Naveen N Rao, bpf, linuxppc-dev
Cc: 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] 13+ messages in thread* Re: [PATCH v2 3/7] powerpc32/bpf: mark all argument registers seen in function calls
2026-12-01 16:53 ` [PATCH v2 3/7] powerpc32/bpf: mark all argument registers seen in function calls Lukas Stockner
@ 2026-12-01 17:46 ` bot+bpf-ci
0 siblings, 0 replies; 13+ messages in thread
From: bot+bpf-ci @ 2026-12-01 17:46 UTC (permalink / raw)
To: lukas, hbathini, chleroy, naveen, bpf, linuxppc-dev
Cc: maddy, mpe, ast, daniel, andrii, lukas, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2318 bytes --]
> powerpc32/bpf: mark all argument registers seen in function calls
TL;DR: Commit message only: the first Fixes: tag (51c66ad849a7) looks wrong,
since the register remapping bug came from 40272035e1d0; should the tag be
replaced?
> powerpc32/bpf: mark all argument registers seen in function calls
>
> 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>
Is the first Fixes: tag pointing at the right commit?
51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32") used a fixed
BPF-to-PPC register mapping with no register reallocation, so an unseen
argument register could not be overwritten by a remapped register.
The remapping that causes this problem, including the tail call case where
SEEN_FUNC is not set and the full reallocation applies, was introduced by:
Fixes: 40272035e1d0 ("powerpc/bpf: Reallocate BPF registers to volatile registers when possible on PPC32")
The a8936569a07b tag looks correct, since it enabled reallocation of
BPF_REG_5, BPF_REG_AX and TMP_REG even when SEEN_FUNC is set. Should the
51c66ad849a7 tag be replaced with 40272035e1d0?
---
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/46701268444
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 4/7] powerpc32/bpf: don't fold MOVSX into following instruction
2026-12-01 16:53 [PATCH v2 0/7] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
` (2 preceding siblings ...)
2026-12-01 16:53 ` [PATCH v2 3/7] powerpc32/bpf: mark all argument registers seen in function calls Lukas Stockner
@ 2026-12-01 16:53 ` Lukas Stockner
2026-12-01 16:53 ` [PATCH v2 5/7] powerpc32/bpf: fix MOV folding with in-place operations Lukas Stockner
` (2 subsequent siblings)
6 siblings, 0 replies; 13+ messages in thread
From: Lukas Stockner @ 2026-12-01 16:53 UTC (permalink / raw)
To: Hari Bathini, Christophe Leroy, Naveen N Rao, bpf, linuxppc-dev
Cc: 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, but currently it doesn't
check whether the MOV is supposed to perform sign-extension, so MOVSX
effectively gets transformed into a plain MOV.
To fix this, just skip 3-op folding for MOVSX.
Fixes: 8ecf3c1dab1c ("powerpc/bpf/32: Fix failing test_bpf tests")
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..c6cc562cf88c 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 &&
+ insn[i - 1].off == 0) {
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] 13+ messages in thread* [PATCH v2 5/7] powerpc32/bpf: fix MOV folding with in-place operations
2026-12-01 16:53 [PATCH v2 0/7] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
` (3 preceding siblings ...)
2026-12-01 16:53 ` [PATCH v2 4/7] powerpc32/bpf: don't fold MOVSX into following instruction Lukas Stockner
@ 2026-12-01 16:53 ` Lukas Stockner
2026-12-01 17:18 ` sashiko-bot
2026-12-01 16:53 ` [PATCH v2 6/7] powerpc32/bpf: use src2_reg for all ALU operations Lukas Stockner
2026-12-01 16:53 ` [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets Lukas Stockner
6 siblings, 1 reply; 13+ messages in thread
From: Lukas Stockner @ 2026-12-01 16:53 UTC (permalink / raw)
To: Hari Bathini, Christophe Leroy, Naveen N Rao, bpf, linuxppc-dev
Cc: Madhavan Srinivasan, Michael Ellerman, Alexei Starovoitov,
Daniel Borkmann, Andrii Nakryiko, Lukas Stockner
The current 3-operand folding combines a MOV to a register with a
subsequent ALU operation to the same register into 3-op instructions,
which use the source of the MOV and the operand of the ALU op as
the two source operands.
However, if the source of the ALU op is the same register that the
previous MOV was supposed to go to, the resulting instruction will
end up using the previous content of the destination register.
For example, "r2 = r1; r2 += r2" gets folded to "r2 = r1 + r2", while
the correct result would be "r2 = r1 + r1".
Therefore, check for this, and use the MOV source for both operands
in that case.
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 | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index c6cc562cf88c..2c3d94c94ed7 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -371,6 +371,10 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
insn[i - 1].off == 0) {
src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
src2_reg_h = src2_reg - 1;
+ if (BPF_SRC(code) == BPF_X && insn[i].src_reg == insn[i - 1].dst_reg) {
+ src_reg = src2_reg;
+ src_reg_h = src2_reg_h;
+ }
ctx->idx = addrs[i - 1] / 4;
}
--
2.56.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 5/7] powerpc32/bpf: fix MOV folding with in-place operations
2026-12-01 16:53 ` [PATCH v2 5/7] powerpc32/bpf: fix MOV folding with in-place operations Lukas Stockner
@ 2026-12-01 17:18 ` sashiko-bot
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-01 17:18 UTC (permalink / raw)
To: Lukas Stockner, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] powerpc32/bpf: chained MOV folding via in-place MOV loses
source in bpf_jit_build_body()
--
commit 2571d56115a2072d4ce2b83007a90bba1702c77c
Author: Lukas Stockner <lukas@lukasstockner.de>
powerpc32/bpf: fix MOV folding with in-place operations
This commit updates the 3-operand MOV+ALU folding in bpf_jit_build_body() to
use the MOV source register for both operands when the subsequent ALU
instruction reads from the MOV destination register.
> diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
> index c6cc562cf88c8..2c3d94c94ed78 100644
> --- a/arch/powerpc/net/bpf_jit_comp32.c
> +++ b/arch/powerpc/net/bpf_jit_comp32.c
> @@ -371,6 +371,10 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
> insn[i - 1].off == 0) {
> src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
> src2_reg_h = src2_reg - 1;
> + if (BPF_SRC(code) == BPF_X && insn[i].src_reg == insn[i - 1].dst_reg) {
> + src_reg = src2_reg;
> + src_reg_h = src2_reg_h;
> + }
> ctx->idx = addrs[i - 1] / 4;
> }
[Severity: High]
Can this lose the original MOV source register when an in-place MOV (such as
"w2 = w2" for 32-bit zero-extension) sits between a MOV and a third ALU
instruction on the same destination register?
For a sequence like "r2 = r1; w2 = w2; w2 += w3":
1. At "w2 = w2" (i - 1), the new check folds it with "r2 = r1" (i - 2),
rewinds ctx->idx to addrs[i - 2] / 4, records addrs[i - 1] = addrs[i - 2],
and emits the move from r1 into r2.
2. At "w2 += w3" (i), insn[i - 1] is itself a BPF_MOV | BPF_X with
dst_reg == r2, so it matches the folding check again. However, src2_reg
is read from the unmodified insn[i - 1].src_reg (r2 instead of r1), and
ctx->idx is rewound to addrs[i - 1] / 4 (which equals addrs[i - 2] / 4).
The emitted instruction for "w2 += w3" then overwrites the move from r1 and
computes r2 = r2 + r3 using the stale value of r2 instead of r1.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201165313.413960-1-lukas@lukasstockner.de?part=5
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 6/7] powerpc32/bpf: use src2_reg for all ALU operations
2026-12-01 16:53 [PATCH v2 0/7] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
` (4 preceding siblings ...)
2026-12-01 16:53 ` [PATCH v2 5/7] powerpc32/bpf: fix MOV folding with in-place operations Lukas Stockner
@ 2026-12-01 16:53 ` Lukas Stockner
2026-12-01 17:13 ` sashiko-bot
2026-12-01 16:53 ` [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets Lukas Stockner
6 siblings, 1 reply; 13+ messages in thread
From: Lukas Stockner @ 2026-12-01 16:53 UTC (permalink / raw)
To: Hari Bathini, Christophe Leroy, Naveen N Rao, bpf, linuxppc-dev
Cc: Madhavan Srinivasan, Michael Ellerman, Alexei Starovoitov,
Daniel Borkmann, Andrii Nakryiko, Lukas Stockner
ALU operations in the PPC32 eBPF JIT are supposed to use src2_reg, not
dst_reg, as the second source operand in order to work correctly when
a MOV was folded into them.
However, a few operations don't correctly do this yet, so fix them.
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 | 32 ++++++++++++++++++++++++++-----
1 file changed, 27 insertions(+), 5 deletions(-)
diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index 2c3d94c94ed7..83457b21d0a8 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -495,7 +495,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
} else if (imm > 0 && is_power_of_2(imm)) {
imm = ilog2(imm);
EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg_h, imm, 0, 31 - imm));
- EMIT(PPC_RAW_RLWIMI(dst_reg_h, dst_reg, imm, 32 - imm, 31));
+ EMIT(PPC_RAW_RLWIMI(dst_reg_h, src2_reg, imm, 32 - imm, 31));
EMIT(PPC_RAW_SLWI(dst_reg, src2_reg, imm));
} else {
bpf_set_seen_register(ctx, tmp_reg);
@@ -603,6 +603,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
EMIT(PPC_RAW_SUBFZE(dst_reg_h, src2_reg_h));
imm = -imm;
src2_reg = dst_reg;
+ src2_reg_h = dst_reg_h;
}
if (imm == 1) {
EMIT(PPC_RAW_MR(dst_reg, src2_reg));
@@ -635,6 +636,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
if (imm >= 0)
EMIT(PPC_RAW_LI(dst_reg_h, 0));
+ else if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
fallthrough;
case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
if (!IMM_H(imm)) {
@@ -660,6 +663,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
/* Sign-extended */
if (imm < 0)
EMIT(PPC_RAW_LI(dst_reg_h, -1));
+ else if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
fallthrough;
case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
if (IMM_L(imm)) {
@@ -668,6 +673,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
}
if (IMM_H(imm))
EMIT(PPC_RAW_ORIS(dst_reg, src2_reg, IMM_H(imm)));
+ else if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
break;
case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
if (dst_reg == src_reg) {
@@ -687,6 +694,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
if (imm < 0)
EMIT(PPC_RAW_NOR(dst_reg_h, src2_reg_h, src2_reg_h));
+ else if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
fallthrough;
case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
if (IMM_L(imm)) {
@@ -695,6 +704,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
}
if (IMM_H(imm))
EMIT(PPC_RAW_XORIS(dst_reg, src2_reg, IMM_H(imm)));
+ else if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
break;
case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
EMIT(PPC_RAW_SLW(dst_reg, src2_reg, src_reg));
@@ -720,7 +731,10 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
if (imm < 0)
return -EINVAL;
if (!imm) {
- EMIT(PPC_RAW_MR(dst_reg, src2_reg));
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
+ if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
} else if (imm < 32) {
EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg_h, imm, 0, 31 - imm));
EMIT(PPC_RAW_RLWIMI(dst_reg_h, src2_reg, imm, 32 - imm, 31));
@@ -742,7 +756,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
EMIT(PPC_RAW_SRW(dst_reg, src2_reg, src_reg));
EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
EMIT(PPC_RAW_SLW(_R0, src2_reg_h, _R0));
- EMIT(PPC_RAW_SRW(tmp_reg, dst_reg_h, tmp_reg));
+ EMIT(PPC_RAW_SRW(tmp_reg, src2_reg_h, tmp_reg));
EMIT(PPC_RAW_OR(dst_reg, dst_reg, _R0));
EMIT(PPC_RAW_SRW(dst_reg_h, src2_reg_h, src_reg));
EMIT(PPC_RAW_OR(dst_reg, dst_reg, tmp_reg));
@@ -834,8 +848,11 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
break;
case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
/* special mov32 for zext */
- if (imm == 1)
+ if (imm == 1) {
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
EMIT(PPC_RAW_LI(dst_reg_h, 0));
+ }
else if (off == 8)
EMIT(PPC_RAW_EXTSB(dst_reg, src_reg));
else if (off == 16)
@@ -858,6 +875,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
case BPF_ALU64 | BPF_END | BPF_FROM_LE:
switch (imm) {
case 16:
+ /* Copy lower half if needed. */
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
/* Copy 16 bits to upper part */
EMIT(PPC_RAW_RLWIMI(dst_reg, src2_reg, 16, 0, 15));
/* Rotate 8 bits right & mask */
@@ -901,7 +921,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
break;
case 32:
case 64:
- /* nop */
+ /* nop, just move if needed */
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
break;
}
break;
--
2.56.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 6/7] powerpc32/bpf: use src2_reg for all ALU operations
2026-12-01 16:53 ` [PATCH v2 6/7] powerpc32/bpf: use src2_reg for all ALU operations Lukas Stockner
@ 2026-12-01 17:13 ` sashiko-bot
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-01 17:13 UTC (permalink / raw)
To: Lukas Stockner, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] powerpc32/bpf: missing high-word move for be64 in
bpf_jit_build_body()
--
commit 01965c5e00d0935c024d80bbbd1744d93c885dbb
Author: Lukas Stockner <lukas@lukasstockner.de>
powerpc32/bpf: use src2_reg for all ALU operations
This commit updates ALU operations in the PPC32 eBPF JIT to use src2_reg
and src2_reg_h instead of dst_reg and dst_reg_h so that folded MOV
instructions work properly.
> diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
> index 2c3d94c94ed78..83457b21d0a8b 100644
> --- a/arch/powerpc/net/bpf_jit_comp32.c
> +++ b/arch/powerpc/net/bpf_jit_comp32.c
[ ... ]
> @@ -901,7 +921,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
> break;
> case 32:
> case 64:
> - /* nop */
> + /* nop, just move if needed */
> + if (dst_reg != src2_reg)
> + EMIT(PPC_RAW_MR(dst_reg, src2_reg));
> break;
> }
> break;
[Severity: High]
Does case 64 of BPF_ALU | BPF_END | BPF_FROM_BE also need to copy
src2_reg_h into dst_reg_h when dst_reg_h != src2_reg_h?
BPF_END | BPF_FROM_BE uses class BPF_ALU even for 64-bit operands
(imm == 64), and the 32-bit zero-extension at the end of
bpf_jit_build_body() explicitly skips (BPF_OP(code) == BPF_END && imm == 64).
When a preceding 64-bit MOV is folded into a be64 instruction, rewinding
ctx->idx discards both the low-word and high-word register moves from the
MOV, leaving dst_reg_h with its previous stale value.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201165313.413960-1-lukas@lukasstockner.de?part=6
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets
2026-12-01 16:53 [PATCH v2 0/7] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
` (5 preceding siblings ...)
2026-12-01 16:53 ` [PATCH v2 6/7] powerpc32/bpf: use src2_reg for all ALU operations Lukas Stockner
@ 2026-12-01 16:53 ` Lukas Stockner
2026-12-01 17:24 ` sashiko-bot
2026-12-01 17:46 ` bot+bpf-ci
6 siblings, 2 replies; 13+ messages in thread
From: Lukas Stockner @ 2026-12-01 16:53 UTC (permalink / raw)
To: Hari Bathini, Christophe Leroy, Naveen N Rao, bpf, linuxppc-dev
Cc: Madhavan Srinivasan, Michael Ellerman, Alexei Starovoitov,
Daniel Borkmann, Andrii Nakryiko, Lukas Stockner
If a BPF program jumps to an ALU instruction that was combined with
a preceding MOV, it will incorrectly execute both the MOV and the
ALU operation.
Therefore, avoid 3-op folding if the second instruction is a jump
target.
Fixes: 19daf0aef84f ("powerpc/bpf/32: perform three operands ALU operations")
Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
---
arch/powerpc/net/bpf_jit.h | 4 ++--
arch/powerpc/net/bpf_jit_comp.c | 8 ++++----
arch/powerpc/net/bpf_jit_comp32.c | 6 +++---
arch/powerpc/net/bpf_jit_comp64.c | 5 +++--
include/linux/bpf.h | 2 ++
kernel/bpf/core.c | 9 +++++++++
6 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/arch/powerpc/net/bpf_jit.h b/arch/powerpc/net/bpf_jit.h
index 4da8bde92e1e..fea87cc88b85 100644
--- a/arch/powerpc/net/bpf_jit.h
+++ b/arch/powerpc/net/bpf_jit.h
@@ -224,8 +224,8 @@ static inline void bpf_clear_seen_register(struct codegen_context *ctx, int i)
void bpf_jit_init_reg_mapping(struct codegen_context *ctx);
int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *ctx, u64 func);
-int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codegen_context *ctx,
- u32 *addrs, int pass, bool extra_pass);
+int bpf_jit_build_body(struct bpf_verifier_env *env, struct bpf_prog *fp, u32 *image, u32 *fimage,
+ struct codegen_context *ctx, u32 *addrs, int pass, bool extra_pass);
void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx);
void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context *ctx);
void bpf_jit_build_fentry_stubs(u32 *image, u32 *fimage, struct codegen_context *ctx);
diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index ce2966c52ac5..01151a78f2b2 100644
--- a/arch/powerpc/net/bpf_jit_comp.c
+++ b/arch/powerpc/net/bpf_jit_comp.c
@@ -289,7 +289,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
}
/* Scouting faux-generate pass 0 */
- if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false))
+ if (bpf_jit_build_body(env, fp, NULL, NULL, &cgctx, addrs, 0, false))
/* We hit something illegal or unsupported. */
goto out_err;
@@ -303,7 +303,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
cgctx.idx = 0;
cgctx.alt_exit_addr = 0;
- if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false))
+ if (bpf_jit_build_body(env, fp, NULL, NULL, &cgctx, addrs, 0, false))
goto out_err;
}
@@ -344,8 +344,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
cgctx.exentry_idx = 0;
cgctx.alt_exit_addr = 0;
bpf_jit_build_prologue(code_base, &cgctx);
- if (bpf_jit_build_body(fp, code_base, fcode_base, &cgctx, addrs, pass,
- extra_pass)) {
+ if (bpf_jit_build_body(env, fp, code_base, fcode_base, &cgctx, addrs,
+ pass, extra_pass)) {
bpf_arch_text_copy(&fhdr->size, &hdr->size, sizeof(hdr->size));
bpf_jit_binary_pack_free(fhdr, hdr);
goto out_err;
diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index 83457b21d0a8..ec2d5ce2f70a 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -334,8 +334,8 @@ static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 o
}
/* Assemble the body code between the prologue & epilogue */
-int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codegen_context *ctx,
- u32 *addrs, int pass, bool extra_pass)
+int bpf_jit_build_body(struct bpf_verifier_env *env, struct bpf_prog *fp, u32 *image, u32 *fimage,
+ struct codegen_context *ctx, u32 *addrs, int pass, bool extra_pass)
{
const struct bpf_insn *insn = fp->insnsi;
int flen = fp->len;
@@ -368,7 +368,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
(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].off == 0) {
+ insn[i - 1].off == 0 && !bpf_insn_is_jump_target(env, fp, i)) {
src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
src2_reg_h = src2_reg - 1;
if (BPF_SRC(code) == BPF_X && insn[i].src_reg == insn[i - 1].dst_reg) {
diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c
index c1e86fe0bd79..d7edb2723f42 100644
--- a/arch/powerpc/net/bpf_jit_comp64.c
+++ b/arch/powerpc/net/bpf_jit_comp64.c
@@ -964,8 +964,9 @@ static int emit_atomic_ld_st(const struct bpf_insn insn, struct codegen_context
}
/* Assemble the body code between the prologue & epilogue */
-int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codegen_context *ctx,
- u32 *addrs, int pass, bool extra_pass)
+int bpf_jit_build_body(struct bpf_verifier_env *env __always_unused, struct bpf_prog *fp,
+ u32 *image, u32 *fimage, struct codegen_context *ctx, u32 *addrs, int pass,
+ bool extra_pass)
{
enum stf_barrier_type stf_barrier = stf_barrier_type_get();
bool sync_emitted, ori31_emitted;
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 1dd9db16f324..c1a7a0be3a9c 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1697,6 +1697,8 @@ void bpf_jit_uncharge_modmem(u32 size);
bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog,
int insn_idx);
+bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog,
+ int insn_idx);
u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog);
#else
static inline int bpf_trampoline_link_prog(struct bpf_tramp_node *node,
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 0bfd23a41401..27c200f43dd5 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1662,6 +1662,15 @@ bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc
return env->insn_aux_data[insn_idx].indirect_target;
}
+bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog,
+ int insn_idx)
+{
+ if (!env)
+ return false;
+ insn_idx += prog->aux->subprog_start;
+ return env->insn_aux_data[insn_idx].jump_target;
+}
+
u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog)
{
const struct bpf_subprog_info *sub;
--
2.56.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets
2026-12-01 16:53 ` [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets Lukas Stockner
@ 2026-12-01 17:24 ` sashiko-bot
2026-12-01 17:46 ` bot+bpf-ci
1 sibling, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-01 17:24 UTC (permalink / raw)
To: Lukas Stockner, bpf
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: jump_target lost across verifier patching in
bpf_insn_is_jump_target()
- [High] powerpc32/bpf: bpf_insn_is_jump_target() returns false when env
is NULL
--
commit fa51b274b4205b85901c47e5fab0c0ea5cb7a081
Author: Lukas Stockner <lukas@lukasstockner.de>
powerpc32/bpf: don't fold MOV into jump targets
This commit adds bpf_insn_is_jump_target() and uses it in the 32-bit PowerPC
BPF JIT to avoid folding a preceding BPF_MOV into an ALU instruction when the
ALU instruction is a jump target.
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index facb0d6d2e8d8..466c20a7aa5ad 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -1984,6 +1984,15 @@ bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc
> return env->insn_aux_data[insn_idx].indirect_target;
> }
>
> +bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog,
> + int insn_idx)
> +{
> + if (!env)
> + return false;
[Severity: High]
Should bpf_insn_is_jump_target() return true when env is NULL?
Classic BPF programs migrated via bpf_migrate_filter() call
bpf_prog_select_runtime() with env == NULL. Returning false allows
bpf_jit_build_body() to still fold a preceding BPF_MOV64_REG(BPF_REG_A, ...)
(such as from BPF_MISC | BPF_TXA) into a jump-target ALU instruction on
BPF_REG_A, corrupting the accumulator when a branch jumps to that ALU
instruction.
> + insn_idx += prog->aux->subprog_start;
> + return env->insn_aux_data[insn_idx].jump_target;
> +}
[Severity: High]
Can env->insn_aux_data[insn_idx].jump_target be cleared or stale by the time
the JIT runs?
jump_target is populated during bpf_check_cfg(), before verifier instruction
patching. When a jump-target instruction is expanded into a multi-instruction
patch, jumps are redirected to slot[p->target], but patch_list_adj_aux()
copies the old aux entry to slot[p->self] and only moves indirect_target and
nospec to slot[p->target]:
kernel/bpf/fixups.c:patch_list_adj_aux() {
...
/* jumps to the replaced insn land on p->target, so the barrier goes before it */
if (p->target != p->self) {
slot[p->target].indirect_target = last->indirect_target;
slot[p->target].nospec = last->nospec;
last->indirect_target = last->nospec = 0;
}
}
Because bpf_jit_needs_zext() is true on PowerPC,
bpf_opt_subreg_zext_lo32_rnd_hi32() expands any zero-extended 32-bit ALU
instruction into the ALU instruction at slot[0] (p->target) followed by
BPF_ZEXT_REG at slot[1] (p->self). That leaves jump_target cleared on the
actual jump target at slot[0], so bpf_insn_is_jump_target() returns false and
bpf_jit_build_body() still folds a preceding BPF_MOV into it.
Similarly, when a jump-target instruction is deleted (p->len == 0, such as in
bpf_opt_remove_nops()), patch_list_remap_aux() drops its jump_target flag
instead of propagating it to the next surviving instruction.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201165313.413960-1-lukas@lukasstockner.de?part=7
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets
2026-12-01 16:53 ` [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets Lukas Stockner
2026-12-01 17:24 ` sashiko-bot
@ 2026-12-01 17:46 ` bot+bpf-ci
1 sibling, 0 replies; 13+ messages in thread
From: bot+bpf-ci @ 2026-12-01 17:46 UTC (permalink / raw)
To: lukas, hbathini, chleroy, naveen, bpf, linuxppc-dev
Cc: maddy, mpe, ast, daniel, andrii, lukas, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 4552 bytes --]
> powerpc32/bpf: don't fold MOV into jump targets
TL;DR:
- ppc32 MOV folding may still miscompile jump targets for classic BPF (no
verifier env) and after verifier rewrites move jump_target
- Possible wrong results or an out-of-bounds access
> 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
[ ... ]
> @@ -368,7 +368,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
> (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].off == 0) {
> + insn[i - 1].off == 0 && !bpf_insn_is_jump_target(env, fp, i)) {
[ ... ]
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
[ ... ]
> @@ -1984,6 +1984,15 @@ bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc
> return env->insn_aux_data[insn_idx].indirect_target;
> }
>
> +bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog,
> + int insn_idx)
> +{
> + if (!env)
> + return false;
Without a verifier env this reports "not a jump target", so the ppc32 JIT
keeps folding the MOV into the next ALU insn even when that insn is a
branch target. The NULL default is safe in bpf_insn_is_indirect_target()
because programs without a verifier have no indirect jumps, but they do
have direct jumps.
bpf_prog_select_runtime() calls __bpf_prog_select_runtime(NULL, fp, err),
which reaches bpf_int_jit_compile(NULL, fp). This is the path for classic
BPF (bpf_migrate_filter(): SO_ATTACH_FILTER, seccomp, bpf_prog_create()).
bpf_convert_filter() emits:
case BPF_MISC | BPF_TXA:
*insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
so a classic filter such as:
jeq #0, L, 0
txa
L: add #1
ret a
becomes 'if A == 0 goto L; A = X; L: w0 += 1'. The fold condition
matches, and the taken branch computes X + 1 instead of A + 1.
Can this path, reachable without privileges, still miscompile? Should the
NULL env case return true, or should the JIT compute branch targets itself
from fp->insnsi?
> + insn_idx += prog->aux->subprog_start;
> + return env->insn_aux_data[insn_idx].jump_target;
Is jump_target still valid at JIT time? It is set once by
mark_jump_target() in bpf_check_cfg(), but the rewrite passes later in
bpf_check() move the instructions around.
When a pass replaces an insn with a multi-insn patch through
bpf_patch_list_add(), jumps to the old insn land on the first insn of the
patch, while patch_list_adj_aux() copies the old aux data (including
jump_target) to the last insn of the patch. Only indirect_target and
nospec are moved to the landing insn:
if (p->target != p->self) {
slot[p->target].indirect_target = last->indirect_target;
slot[p->target].nospec = last->nospec;
PowerPC has bpf_jit_needs_zext() == true, so
bpf_opt_subreg_zext_lo32_rnd_hi32() rewrites ALU32 insns whose upper half
is live into [insn, BPF_ZEXT_REG(dst)] via bpf_patch_list_add(). After
that, the ALU32 insn at the jump target has jump_target == 0 and the zext
after it has jump_target == 1.
For example:
if w3 > 15 goto L
w1 = w3 // MOV X, off 0
L: w1 += 1 // ALU32, jump target
r1 <<= 3 // reads r1 as 64-bit, so zext is inserted after L
bpf_insn_is_jump_target(env, fp, L) now returns false, so
bpf_jit_build_body() still folds the MOV into the ADD, and the taken
branch lands on the fused 'w1 = w3 + 1'. Since w3 is unbounded here, the
runtime value differs from what the verifier tracked, which could lead to
an out-of-bounds access.
Jumps inserted by rewrites also never call mark_jump_target(). The other
users of bpf_is_jump_target() (verifier.c) run before any rewrite, so this
is the first consumer after the rewrites.
Would it be more robust to have the ppc32 JIT compute branch targets from
fp->insnsi (as arm64 does with ctx.tb/find_tb()), or to have
patch_list_adj_aux() move jump_target to p->target and mark the targets of
inserted jumps?
> +}
[ ... ]
---
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/46701268444
^ permalink raw reply [flat|nested] 13+ messages in thread