]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/bpf: fix implementation of smp_mb()
authorPuranjay Mohan <puranjay@kernel.org>
Thu, 10 Jul 2025 17:54:33 +0000 (17:54 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 17 Jul 2025 01:38:52 +0000 (18:38 -0700)
As BPF doesn't include any barrier instructions, smp_mb() is implemented
by doing a dummy value returning atomic operation. Such an operation
acts a full barrier as enforced by LKMM and also by the work in progress
BPF memory model.

If the returned value is not used, clang[1] can optimize the value
returning atomic instruction in to a normal atomic instruction which
provides no ordering guarantees.

Mark the variable as volatile so the above optimization is never
performed and smp_mb() works as expected.

[1] https://godbolt.org/z/qzze7bG6z

Fixes: 88d706ba7cc5 ("selftests/bpf: Introduce arena spin lock")
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20250710175434.18829-2-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/bpf_atomic.h

index a9674e54432228ff066c08b5bf1c3dbb346987fa..c550e571196774486a2bc13fe28c3995460aaf20 100644 (file)
@@ -61,7 +61,7 @@ extern bool CONFIG_X86_64 __kconfig __weak;
 
 #define smp_mb()                                 \
        ({                                       \
-               unsigned long __val;             \
+               volatile unsigned long __val;    \
                __sync_fetch_and_add(&__val, 0); \
        })