LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>
To: Lukas Stockner <lukas@lukasstockner.de>,
	Hari Bathini <hbathini@linux.ibm.com>,
	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>
Subject: Re: [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg
Date: Sat, 28 Nov 2026 19:32:49 +0100	[thread overview]
Message-ID: <d0ad1f7a-28b2-4c10-9c5e-57fe2df585ba@kernel.org> (raw)
In-Reply-To: <20261122194205.218543-5-lukas@lukasstockner.de>



Le 22/11/2026 à 20:42, Lukas Stockner a écrit :
> [Vous ne recevez pas souvent de courriers de lukas@lukasstockner.de. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
> 
> 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.

You exemple is "r2 = r1; r2 = r2 + r2", it should become "r2 = r1 + r1".

I think you should do that instead:

diff --git a/arch/powerpc/net/bpf_jit_comp32.c 
b/arch/powerpc/net/bpf_jit_comp32.c
index bfdc50740da8..a2ec60f2f5da 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -355,6 +355,10 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 
*image, u32 *fimage, struct code
  		    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1) {
  			src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
  			src2_reg_h = src2_reg - 1;
+			if (src_reg == dst_reg) {
+				src_reg = src2_reg;
+				src_reg_h = src2_reg_h;
+			}
  			ctx->idx = addrs[i - 1] / 4;
  		}


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

What is w2 ?

Are you talking about the zero extension added by commit 8ecf3c1dab1c 
("powerpc/bpf/32: Fix failing test_bpf tests") ? Maybe I missed 
something in that commit which was after commit 19daf0aef84f.

Or you are just talking about the /* special mov32 for zext */ ? This 
one is normally skipped by insn[i - 1].imm != 1

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

What about that instead:

diff --git a/arch/powerpc/net/bpf_jit_comp32.c 
b/arch/powerpc/net/bpf_jit_comp32.c
index bfdc50740da8..842498efa414 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -352,9 +352,14 @@ 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) {
  			src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
  			src2_reg_h = src2_reg - 1;
+			if (src_reg == dst_reg) {
+				src_reg = src2_reg;
+				src_reg_h = src2_reg_h;
+			}
  			ctx->idx = addrs[i - 1] / 4;
  		}




      parent reply	other threads:[~2026-11-28 18:33 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 ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg Lukas Stockner
2026-11-22 20:14   ` bot+bpf-ci
2026-11-28 18:32   ` Christophe Leroy (CS GROUP) [this message]

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=d0ad1f7a-28b2-4c10-9c5e-57fe2df585ba@kernel.org \
    --to=chleroy@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=hbathini@linux.ibm.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=lukas@lukasstockner.de \
    --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