linux/arch
Andrea Parri e59db0623f riscv, bpf: Make BPF_CMPXCHG fully ordered
According to the prototype formal BPF memory consistency model
discussed e.g. in [1] and following the ordering properties of
the C/in-kernel macro atomic_cmpxchg(), a BPF atomic operation
with the BPF_CMPXCHG modifier is fully ordered.  However, the
current RISC-V JIT lowerings fail to meet such memory ordering
property.  This is illustrated by the following litmus test:

BPF BPF__MP+success_cmpxchg+fence
{
 0:r1=x; 0:r3=y; 0:r5=1;
 1:r2=y; 1:r4=f; 1:r7=x;
}
 P0                               | P1                                         ;
 *(u64 *)(r1 + 0) = 1             | r1 = *(u64 *)(r2 + 0)                      ;
 r2 = cmpxchg_64 (r3 + 0, r4, r5) | r3 = atomic_fetch_add((u64 *)(r4 + 0), r5) ;
                                  | r6 = *(u64 *)(r7 + 0)                      ;
exists (1:r1=1 /\ 1:r6=0)

whose "exists" clause is not satisfiable according to the BPF
memory model.  Using the current RISC-V JIT lowerings, the test
can be mapped to the following RISC-V litmus test:

RISCV RISCV__MP+success_cmpxchg+fence
{
 0:x1=x; 0:x3=y; 0:x5=1;
 1:x2=y; 1:x4=f; 1:x7=x;
}
 P0                 | P1                          ;
 sd x5, 0(x1)       | ld x1, 0(x2)                ;
 L00:               | amoadd.d.aqrl x3, x5, 0(x4) ;
 lr.d x2, 0(x3)     | ld x6, 0(x7)                ;
 bne x2, x4, L01    |                             ;
 sc.d x6, x5, 0(x3) |                             ;
 bne x6, x4, L00    |                             ;
 fence rw, rw       |                             ;
 L01:               |                             ;
exists (1:x1=1 /\ 1:x6=0)

where the two stores in P0 can be reordered.  Update the RISC-V
JIT lowerings/implementation of BPF_CMPXCHG to emit an SC with
RELEASE ("rl") annotation in order to meet the expected memory
ordering guarantees.  The resulting RISC-V JIT lowerings of
BPF_CMPXCHG match the RISC-V lowerings of the C atomic_cmpxchg().

Other lowerings were fixed via 20a759df3b ("riscv, bpf: make
some atomic operations fully ordered").

Fixes: dd642ccb45 ("riscv, bpf: Implement more atomic operations for RV64")
Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Puranjay Mohan <puranjay@kernel.org>
Acked-by: Björn Töpel <bjorn@kernel.org>
Link: https://lpc.events/event/18/contributions/1949/attachments/1665/3441/bpfmemmodel.2024.09.19p.pdf [1]
Link: https://lore.kernel.org/bpf/20241017143628.2673894-1-parri.andrea@gmail.com
2024-10-17 17:14:48 +02:00
..
alpha struct fd layout change (and conversion to accessor helpers) 2024-09-23 09:35:36 -07:00
arc Kbuild updates for v6.12 2024-09-24 13:02:06 -07:00
arm Input updates for v6.12-rc0 2024-09-24 12:42:35 -07:00
arm64 i2c-for-6.12-rc1 2024-09-23 14:34:19 -07:00
csky mm: make arch_get_unmapped_area() take vm_flags by default 2024-09-09 16:39:13 -07:00
hexagon hexagon: vdso: Fix build failure 2024-09-23 08:17:50 -07:00
loongarch ALong with the usual shower of singleton patches, notable patch series in 2024-09-21 07:29:05 -07:00
m68k m68knommu: updates and fixes for v6.12 2024-09-24 10:48:44 -07:00
microblaze microblaze: don't treat zero reserved memory regions as error 2024-08-25 15:26:30 -07:00
mips i2c-for-6.12-rc1 2024-09-23 14:34:19 -07:00
nios2 mm: remove legacy install_special_mapping() code 2024-09-01 20:26:13 -07:00
openrisc openrisc: convert to generic syscall table 2024-07-10 14:23:38 +02:00
parisc ALong with the usual shower of singleton patches, notable patch series in 2024-09-21 07:29:05 -07:00
powerpc This push fixes the following issues: 2024-09-24 10:46:54 -07:00
riscv riscv, bpf: Make BPF_CMPXCHG fully ordered 2024-10-17 17:14:48 +02:00
s390 This push fixes the following issues: 2024-09-24 10:46:54 -07:00
sh ALong with the usual shower of singleton patches, notable patch series in 2024-09-21 07:29:05 -07:00
sparc ALong with the usual shower of singleton patches, notable patch series in 2024-09-21 07:29:05 -07:00
um drm-misc-next for v6.12: 2024-08-08 18:58:46 +02:00
x86 Kbuild updates for v6.12 2024-09-24 13:02:06 -07:00
xtensa ALong with the usual shower of singleton patches, notable patch series in 2024-09-21 07:29:05 -07:00
.gitignore
Kconfig dma-mapping updates for linux 6.12 2024-09-19 11:12:49 +02:00