powerpc32/bpf: fix BPF-to-BPF calls and instruction folding
Subsystems (4)
v2 cover powerpc32/bpf: fix BPF-to-BPF calls and instruction folding
This series fixes several 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-7 are for the 3-operand folding logic.
First, the code wasn't updated when MOVSX was added, so it currently gets folded away as if it was a regular MOV. Patch 4 adds a check for this. 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 ops. Patch 5 fixes this. Additionally, several ALU operations currently don't handle the folded second operand correctly, so Patch 6 fixes them.
Finally, Patch 7 prevents the JIT from folding instructions if the second one is a jump target, since the jump would otherwise target the folded result and incorrectly also execute the MOV.
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 v1 of this series to v6.6 and tested it on the WS-AP3715i, and with the fixes applied the CLAT program now works fine. A WIP version of this series has also been deployed on several APs in production without problems.
Lukas Stockner (7):
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 MOVSX into following instruction powerpc32/bpf: fix MOV folding with in-place operations powerpc32/bpf: use src2_reg for all ALU operations powerpc32/bpf: don't fold MOV into jump targets
---
v1 -> v2: Reworked MOV folding patch to fix additional issues
v1: https://lore.kernel.org/bpf/[email protected]/
arch/powerpc/net/bpf_jit.h | 4 +- arch/powerpc/net/bpf_jit_comp.c | 8 +-- arch/powerpc/net/bpf_jit_comp32.c | 83 ++++++++++++++++++++++++++----- arch/powerpc/net/bpf_jit_comp64.c | 5 +- include/linux/bpf.h | 2 + kernel/bpf/core.c | 9 ++++ 6 files changed, 90 insertions(+), 21 deletions(-)
Signature · 1 line
2.56.0
PATCH v2 1/7 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.
--- arch/powerpc/net/bpf_jit_comp32.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
arch/powerpc/net/bpf_jit_comp32.c +12 -4
@@ -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 *ctxvoid 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
PATCH v2 2/7 powerpc32/bpf: fetch fifth argument in BPF subprograms
In reply to Lukas StocknerCommit 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.
--- arch/powerpc/net/bpf_jit_comp32.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/powerpc/net/bpf_jit_comp32.c +7 -0
@@ -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
PATCH v2 3/7 powerpc32/bpf: mark all argument registers seen in function calls
In reply to Lukas StocknerThe 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.
--- arch/powerpc/net/bpf_jit_comp32.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
arch/powerpc/net/bpf_jit_comp32.c +16 -1
@@ -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 codeif (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
[email protected]
AI reviewRe: [PATCH v2 3/7] 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?
Quoted 1 line
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?
Quoted 16 lines
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.
Quoted 5 lines
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 <[email protected]>
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:
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
PATCH v2 4/7 powerpc32/bpf: don't fold MOVSX into following instruction
In reply to Lukas StocknerThe 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.
--- arch/powerpc/net/bpf_jit_comp32.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
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 codeif (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 ;
Signature · 1 line
2.56.0
PATCH v2 5/7 powerpc32/bpf: fix MOV folding with in-place operations
In reply to Lukas StocknerThe 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.
--- arch/powerpc/net/bpf_jit_comp32.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/powerpc/net/bpf_jit_comp32.c +4 -0
@@ -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 ; }
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 5/7] powerpc32/bpf: fix MOV folding with in-place operations
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() --
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 <[email protected]>
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.
Quoted 5 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 379
Quoted 9 lines
arch/powerpc/net/bpf_jit_comp32.c +4
-0@@ -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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v2 6/7 powerpc32/bpf: use src2_reg for all ALU operations
In reply to Lukas StocknerALU 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.
--- arch/powerpc/net/bpf_jit_comp32.c | 32 ++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-)
arch/powerpc/net/bpf_jit_comp32.c +27 -5
@@ -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 codeEMIT (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 codecase 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 codecase 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 codeif (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 codeEMIT (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 codebreak ;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 codecase 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 codebreak ;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 ;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 6/7] powerpc32/bpf: use src2_reg for all ALU operations
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() --
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 <[email protected]>
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.
Quoted 4 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 929
Quoted 11 lines
arch/powerpc/net/bpf_jit_comp32.c +3
-1@@ -901,7 +921,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codebreak ;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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v2 7/7 powerpc32/bpf: don't fold MOV into jump targets
In reply to Lukas StocknerIf 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.
--- 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(-)
arch/powerpc/net/bpf_jit.h +2 -2
@@ -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);
arch/powerpc/net/bpf_jit_comp.c +4 -4
@@ -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_prif (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;
arch/powerpc/net/bpf_jit_comp32.c +3 -3
@@ -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) {
arch/powerpc/net/bpf_jit_comp64.c +3 -2
@@ -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;
include/linux/bpf.h +2 -0
@@ -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,
kernel/bpf/core.c +9 -0
@@ -1662,6 +1662,15 @@ bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const strucreturn 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;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets
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…
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 <[email protected]>
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.
kernel/bpf/core.c bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc · line 1991
Quoted 13 lines
kernel/bpf/core.c +5
-0@@ -1984,6 +1984,15 @@ bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const strucreturn 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.
kernel/bpf/core.c bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc · line 1672
Quoted 3 lines
kernel/bpf/core.c +3
-0+ 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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets
In reply to Lukas StocknerTL;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
Quoted 1 line
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
Quoted 3 lines
arch/powerpc/net/bpf_jit_comp32.c +0
-0
[ ... ]
Quoted 6 lines
arch/powerpc/net/bpf_jit_comp32.c +1
-1@@ -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)) {
[ ... ]
Quoted 3 lines
kernel/bpf/core.c +0
-0
[ ... ]
kernel/bpf/core.c bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc · line 1991
Quoted 9 lines
kernel/bpf/core.c +5
-0@@ -1984,6 +1984,15 @@ bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const strucreturn 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?
kernel/bpf/core.c bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc · line 1671
Quoted 2 lines
kernel/bpf/core.c +2
-0+ 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?
kernel/bpf/core.c bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc · line 1672
Quoted 1 line
kernel/bpf/core.c +1
-0+}
[ ... ]
---
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