On 11/28/26 7:32 PM, Christophe Leroy (CS GROUP) wrote:
> 
> 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;
>          }

Good point, that's actually all it takes to handle this correctly (assuming 
everything else
correctly handles src_reg2 etc, more on that below).

>>
>> 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
w2 here is notation for the lower half of r2, so "w2 = w2" is supposed to be 
the special zext mov32, yes.
insn[i - 1].imm != 1 only checks the first instruction (always mov), the case 
that crashed for us
was a regular mov (imm == 0) followed by a zext mov32.

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

Looks good, but it also needs a bunch of extra fixes because some of the code 
below
doesn't use src2_reg correctly. For the zext mov32 case, it only zeroes 
dst_reg_h,
so if dst_reg != src2_reg due to folding, the actual move is dropped).

The review bot above also flagged more cases (and a jump corner case), so I'll
follow up with a v2 series that has all these fixes.

Thanks for the review!

Reply via email to