LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Lukas Stockner <lukas@lukasstockner.de>
To: Hari Bathini <hbathini@linux.ibm.com>,
	Christophe Leroy <chleroy@kernel.org>,
	bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org
Cc: Naveen N Rao <naveen@kernel.org>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Lukas Stockner <lukas@lukasstockner.de>
Subject: [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg
Date: Sun, 22 Nov 2026 20:42:05 +0100	[thread overview]
Message-ID: <20261122194205.218543-5-lukas@lukasstockner.de> (raw)
In-Reply-To: <20261122194205.218543-1-lukas@lukasstockner.de>

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



  parent reply	other threads:[~2026-11-22 22:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Lukas Stockner [this message]
2026-11-22 20:14   ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg bot+bpf-ci
2026-11-28 18:32   ` Christophe Leroy (CS GROUP)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122194205.218543-5-lukas@lukasstockner.de \
    --to=lukas@lukasstockner.de \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chleroy@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=hbathini@linux.ibm.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=naveen@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox