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