linux/arch
Puranjay Mohan b1e7cee961 powerpc/bpf: enforce full ordering for ATOMIC operations with BPF_FETCH
The Linux Kernel Memory Model [1][2] requires RMW operations that have a
return value to be fully ordered.

BPF atomic operations with BPF_FETCH (including BPF_XCHG and
BPF_CMPXCHG) return a value back so they need to be JITed to fully
ordered operations. POWERPC currently emits relaxed operations for
these.

We can show this by running the following litmus-test:

  PPC SB+atomic_add+fetch

  {
      0:r0=x;  (* dst reg assuming offset is 0 *)
      0:r1=2;  (* src reg *)
      0:r2=1;
      0:r4=y;  (* P0 writes to this, P1 reads this *)
      0:r5=z;  (* P1 writes to this, P0 reads this *)
      0:r6=0;

      1:r2=1;
      1:r4=y;
      1:r5=z;
  }

  P0                      | P1            ;
  stw         r2, 0(r4)   | stw  r2,0(r5) ;
                          |               ;
  loop:lwarx  r3, r6, r0  |               ;
  mr          r8, r3      |               ;
  add         r3, r3, r1  | sync          ;
  stwcx.      r3, r6, r0  |               ;
  bne         loop        |               ;
  mr          r1, r8      |               ;
                          |               ;
  lwa         r7, 0(r5)   | lwa  r7,0(r4) ;

  ~exists(0:r7=0 /\ 1:r7=0)

  Witnesses
  Positive: 9 Negative: 3
  Condition ~exists (0:r7=0 /\ 1:r7=0)
  Observation SB+atomic_add+fetch Sometimes 3 9

This test shows that the older store in P0 is reordered with a newer
load to a different address. Although there is a RMW operation with
fetch between them. Adding a sync before and after RMW fixes the issue:

  Witnesses
  Positive: 9 Negative: 0
  Condition ~exists (0:r7=0 /\ 1:r7=0)
  Observation SB+atomic_add+fetch Never 0 9

[1] https://www.kernel.org/doc/Documentation/memory-barriers.txt
[2] https://www.kernel.org/doc/Documentation/atomic_t.txt

Fixes: aea7ef8a82 ("powerpc/bpf/32: add support for BPF_ATOMIC bitwise operations")
Fixes: 2d9206b227 ("powerpc/bpf/32: Add instructions for atomic_[cmp]xchg")
Fixes: dbe6e2456f ("powerpc/bpf/64: add support for atomic fetch operations")
Fixes: 1e82dfaa78 ("powerpc/bpf/64: Add instructions for atomic_[cmp]xchg")
Cc: stable@vger.kernel.org # v6.0+
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Reviewed-by: Naveen N Rao <naveen@kernel.org>
Acked-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20240513100248.110535-1-puranjay@kernel.org
2024-05-29 22:12:42 +10:00
..
alpha mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
arc asm-generic cleanups for 6.10 2024-05-20 15:18:34 -07:00
arm mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
arm64 16 hotfixes, 11 of which are cc:stable. 2024-05-25 15:10:33 -07:00
csky The usual shower of singleton fixes and minor series all over MM, 2024-05-19 09:21:03 -07:00
hexagon hexagon: vmlinux.lds.S: handle attributes section 2024-03-26 11:07:23 -07:00
loongarch - A series ("kbuild: enable more warnings by default") from Arnd 2024-05-22 18:59:29 -07:00
m68k mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
microblaze mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
mips mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
nios2 Kbuild updates for v6.10 2024-05-18 12:39:20 -07:00
openrisc openrisc: Move FPU state out of pt_regs 2024-04-15 15:20:39 +01:00
parisc mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
powerpc powerpc/bpf: enforce full ordering for ATOMIC operations with BPF_FETCH 2024-05-29 22:12:42 +10:00
riscv RISC-V Patches for the 6.10 Merge Window, Part 2 2024-05-24 10:46:35 -07:00
s390 mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
sh mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
sparc Jeff Xu's implementation of the mseal() syscall. 2024-05-24 12:47:28 -07:00
um This pull request contains the following changes for UML: 2024-05-25 13:17:48 -07:00
x86 Misc fixes: 2024-05-25 14:48:40 -07:00
xtensa mseal: wire up mseal syscall 2024-05-23 19:40:26 -07:00
.gitignore
Kconfig arch: add ARCH_HAS_KERNEL_FPU_SUPPORT 2024-05-19 14:36:17 -07:00