* [PATCH bpf] selftests/bpf: Skip libarena when the BPF backend lacks 32-bit arena cmpxchg
@ 2026-06-30 2:58 George Guo
2026-06-30 3:16 ` sashiko-bot
2026-06-30 18:06 ` Alexei Starovoitov
0 siblings, 2 replies; 3+ messages in thread
From: George Guo @ 2026-06-30 2:58 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: Eduard Zingerman, Emil Tsalapatis, Mykola Lysenko,
Martin KaFai Lau, Kumar Kartikeya Dwivedi, Huacai Chen,
Tiezhu Yang, George Guo, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
The arena qspinlock pulled in by libarena (bpf_arena_spin_lock.h) performs
a 32-bit atomic compare-and-exchange on an arena (address space 1) pointer
via atomic_try_cmpxchg_acquire() on the 32-bit atomic_t lock word. Not
every LLVM BPF backend can select that operation; releases such as clang 19
abort instruction selection with:
error: unsupported atomic operation, please use 64 bit version
fatal error: error in backend: Cannot select: AtomicCmpSwap<(s32)> ...
In function: arena_spin_lock_slowpath
Because libarena/libarena.skel.h is an unconditional dependency of
test_progs, this aborts the entire selftests/bpf build before test_progs is
ever linked, so none of the other tests can be built or run. This was
observed building selftests/bpf for LoongArch with clang 19, but it is a
property of the BPF backend, not of the host architecture.
Mirror the existing CLANG_HAS_ARENA_ASAN feature probe: detect whether the
backend can compile a 32-bit arena cmpxchg, and only build libarena when it
can. Define HAS_BPF_ARENA in that case and guard prog_tests/libarena.c
behind it - exactly the way prog_tests/libarena_asan.c is guarded behind
HAS_BPF_ARENA_ASAN - so the test still compiles and reports as skipped when
libarena is not built. When the operation is unsupported, libarena is
skipped and the rest of test_progs builds and links as before.
Fixes: d5327480a12a ("selftests/bpf: Add basic libarena scaffolding")
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
tools/testing/selftests/bpf/Makefile | 14 ++++++++++++++
.../selftests/bpf/prog_tests/libarena.c | 19 ++++++++++++++++++-
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index b642ee489ea6..0098d892cb39 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -91,6 +91,17 @@ CLANG_HAS_ARENA_ASAN := $(shell echo 'int x;' | \
-mllvm -asan-shadow-addr-space=1 \
-x c -c - -o /dev/null 2>/dev/null && echo 1)
+# The arena qspinlock used by libarena performs a 32-bit atomic cmpxchg on an
+# arena (address space 1) pointer. Not every LLVM BPF backend can select it;
+# older releases bail out with "unsupported atomic operation, please use 64 bit
+# version", which aborts the whole selftests build. Probe for the operation and
+# skip libarena (and its dependent tests) when the backend cannot handle it.
+CLANG_HAS_ARENA_CMPXCHG32 := $(shell printf '%s\n' \
+ 'struct s { int counter; };' \
+ 'int f(struct s __attribute__((address_space(1))) *l, int o, int n)' \
+ '{ return __sync_val_compare_and_swap(&l->counter, o, n); }' | \
+ $(CLANG) --target=bpf -O2 -x c -c - -o /dev/null 2>/dev/null && echo 1)
+
# Order correspond to 'make run_tests' order
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_progs \
test_sockmap \
@@ -835,10 +846,13 @@ LIBARENA_BPF_DEPS := $(wildcard libarena/Makefile \
libarena/selftests/* \
libarena/*.bpf.o)
+ifneq ($(CLANG_HAS_ARENA_CMPXCHG32),)
LIBARENA_SKEL := libarena/libarena.skel.h
+CFLAGS += -DHAS_BPF_ARENA
$(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)
+$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS)
+endif
ifneq ($(CLANG_HAS_ARENA_ASAN),)
LIBARENA_ASAN_SKEL := libarena/libarena_asan.skel.h
diff --git a/tools/testing/selftests/bpf/prog_tests/libarena.c b/tools/testing/selftests/bpf/prog_tests/libarena.c
index 61ea68dce410..a7ede72c7256 100644
--- a/tools/testing/selftests/bpf/prog_tests/libarena.c
+++ b/tools/testing/selftests/bpf/prog_tests/libarena.c
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
+
+#ifdef HAS_BPF_ARENA
#include <unistd.h>
#include <libarena/common.h>
@@ -198,7 +200,7 @@ static void run_libarena_parallel_test(struct libarena *skel, struct bpf_program
run_libarena_parallel_fini(skel, name, prefixlen);
}
-void test_libarena(void)
+static void run_test(void)
{
struct arena_alloc_reserve_args args;
struct libarena *skel;
@@ -251,3 +253,18 @@ void test_libarena(void)
out:
libarena__destroy(skel);
}
+
+#endif /* HAS_BPF_ARENA */
+
+/*
+ * Run the test only if the BPF backend can compile the arena programs
+ * libarena depends on (see CLANG_HAS_ARENA_CMPXCHG32 in the Makefile).
+ */
+void test_libarena(void)
+{
+#ifdef HAS_BPF_ARENA
+ run_test();
+#else
+ test__skip();
+#endif
+}
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf] selftests/bpf: Skip libarena when the BPF backend lacks 32-bit arena cmpxchg
2026-06-30 2:58 [PATCH bpf] selftests/bpf: Skip libarena when the BPF backend lacks 32-bit arena cmpxchg George Guo
@ 2026-06-30 3:16 ` sashiko-bot
2026-06-30 18:06 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-06-30 3:16 UTC (permalink / raw)
To: George Guo; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] The patch misdiagnoses the root cause of the Clang crash and implements a workaround that incorrectly skips `libarena` tests instead of fixing the underlying build configuration.
Pre-existing issues:
- [High] The patch fails to apply the new 32-bit cmpxchg guard to the ASAN variant of `libarena`, leaving the selftests build vulnerable to the exact same compiler crash it attempts to fix.
--
commit e34c5b107c762422f1dc3835420230ecdcd78708
Author: George Guo <guodongtai@kylinos.cn>
selftests/bpf: Skip libarena when the BPF backend lacks 32-bit arena cmpxchg
This patch introduces a feature probe to detect if the BPF backend supports
32-bit arena cmpxchg operations. It skips building libarena and its
dependent tests if the operation is unsupported to prevent build aborts.
> diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> index b642ee489ea64..0098d892cb39e 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
[ ... ]
> @@ -91,6 +91,17 @@ CLANG_HAS_ARENA_ASAN := $(shell echo 'int x;' | \
> -mllvm -asan-shadow-addr-space=1 \
> -x c -c - -o /dev/null 2>/dev/null && echo 1)
>
> +# The arena qspinlock used by libarena performs a 32-bit atomic cmpxchg on an
> +# arena (address space 1) pointer. Not every LLVM BPF backend can select it;
> +# older releases bail out with "unsupported atomic operation, please use 64 bit
> +# version", which aborts the whole selftests build. Probe for the operation and
> +# skip libarena (and its dependent tests) when the backend cannot handle it.
> +CLANG_HAS_ARENA_CMPXCHG32 := $(shell printf '%s\n' \
> + 'struct s { int counter; };' \
> + 'int f(struct s __attribute__((address_space(1))) *l, int o, int n)' \
> + '{ return __sync_val_compare_and_swap(&l->counter, o, n); }' | \
> + $(CLANG) --target=bpf -O2 -x c -c - -o /dev/null 2>/dev/null && echo 1)
> +
[Severity: High]
Does this feature probe artificially fail by omitting the -mcpu=v4 flag?
Because the probe runs against the default BPF CPU target (which lacks 32-bit
atomic support), it will falsely report that the backend lacks support and
unnecessarily skip the tests.
Could the actual root cause of the build failure be that libarena's
BPF_CFLAGS in tools/testing/selftests/bpf/libarena/Makefile is missing
the -mcpu=v4 flag?
For example, test_progs-cpuv4 successfully compiles progs/arena_spin_lock.c
because it explicitly passes -mcpu=v4. Would adding -mcpu=v4 to the
libarena Makefile fix the compilation error natively instead of skipping
the tests entirely?
> # Order correspond to 'make run_tests' order
> TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_progs \
> test_sockmap \
[ ... ]
> @@ -835,10 +846,13 @@ LIBARENA_BPF_DEPS := $(wildcard libarena/Makefile \
> libarena/selftests/* \
> libarena/*.bpf.o)
>
> +ifneq ($(CLANG_HAS_ARENA_CMPXCHG32),)
> LIBARENA_SKEL := libarena/libarena.skel.h
> +CFLAGS += -DHAS_BPF_ARENA
>
> $(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)
> +$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS)
> +endif
>
> ifneq ($(CLANG_HAS_ARENA_ASAN),)
> LIBARENA_ASAN_SKEL := libarena/libarena_asan.skel.h
[Severity: High]
This is a pre-existing issue, but doesn't the ASAN variant of libarena
also need protection from this compiler crash?
The LIBARENA_ASAN_SKEL build is only guarded by CLANG_HAS_ARENA_ASAN, which
checks for ASAN flag support but not 32-bit cmpxchg support.
Since the ASAN build compiles the exact same buddy.bpf.c source code
containing the 32-bit arena cmpxchg, the compiler will still crash here
with the exact same "Cannot select: AtomicCmpSwap" error if the target
lacks 32-bit atomics.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260630025809.74043-1-dongtai.guo@linux.dev?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH bpf] selftests/bpf: Skip libarena when the BPF backend lacks 32-bit arena cmpxchg
2026-06-30 2:58 [PATCH bpf] selftests/bpf: Skip libarena when the BPF backend lacks 32-bit arena cmpxchg George Guo
2026-06-30 3:16 ` sashiko-bot
@ 2026-06-30 18:06 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-06-30 18:06 UTC (permalink / raw)
To: George Guo, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: Eduard Zingerman, Emil Tsalapatis, Mykola Lysenko,
Martin KaFai Lau, Kumar Kartikeya Dwivedi, Huacai Chen,
Tiezhu Yang, George Guo, bpf, loongarch, linux-kernel
On Mon Jun 29, 2026 at 7:58 PM PDT, George Guo wrote:
> From: George Guo <guodongtai@kylinos.cn>
>
> The arena qspinlock pulled in by libarena (bpf_arena_spin_lock.h) performs
> a 32-bit atomic compare-and-exchange on an arena (address space 1) pointer
> via atomic_try_cmpxchg_acquire() on the 32-bit atomic_t lock word. Not
> every LLVM BPF backend can select that operation; releases such as clang 19
> abort instruction selection with:
Sorry, you have to upgrade your test environment.
We won't be disabling tests because people still have old clang or old gcc
or other older tools in their setups.
pw-bot: cr
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-30 18:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-30 2:58 [PATCH bpf] selftests/bpf: Skip libarena when the BPF backend lacks 32-bit arena cmpxchg George Guo
2026-06-30 3:16 ` sashiko-bot
2026-06-30 18:06 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox