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