[PATCH v2 1/2] LoongArch: BPF: Make arena support depend on 128-bit atomic cmpxchg
From: Tiezhu Yang
Date: Sun Nov 08 2026 - 23:04:18 EST
When testing bpf selftest "jit_insns/cast_to_user", there are different
behaviors on different Loongson machines.
(1) Loongson-3A6000
$ sudo ./test_progs -a jit_insns/cast_to_user
...
Summary: 1/1 PASSED, 0 SKIPPED, 0/0 FAILED
(2) Loongson-3A5000
$ sudo ./test_progs -a jit_insns/cast_to_user
...
libbpf: map 'arena': failed to create: -ENOMEM
run_subtest:FAIL:unexpected_load_failure unexpected error: -12 (errno 12)
...
Summary: 0/0 PASSED, 0 SKIPPED, 1/1 FAILED
This is because the arena range tree uses kmalloc_nolock(), which requires
128-bit atomic cmpxchg; this is provided by not only hardware SCQ extension
but also assembler support on LoongArch. On CPUs without the SCQ extension,
or if the older versions of assembler do not support that sc.q instruction,
arena map creation fails with a misleading -ENOMEM error.
Since system_has_cmpxchg128() and arch_cmpxchg128() are only available when
CONFIG_AS_HAS_SCQ_EXTENSION is enabled, guard bpf_jit_supports_arena() with
an #ifdef block to preserve compile-time robustness first, and then safely
return system_has_cmpxchg128() if defined, otherwise falling back to false.
This ensures that arena map creation cleanly fails with -EOPNOTSUPP on the
systems without either the assembler support or the hardware SCQ extension.
With this patch, here is the test result on Loongson-3A5000 without SCQ
extension:
$ sudo ./test_progs -a jit_insns/cast_to_user
...
libbpf: map 'arena': failed to create: -EOPNOTSUPP
run_subtest:FAIL:unexpected_load_failure unexpected error: -95 (errno 95)
...
Summary: 0/0 PASSED, 0 SKIPPED, 1/1 FAILED
It is better to mark the test status as SKIPPED rather than logging a
false-alarm failure if the feature is not supported, but this belongs
to a separate bpf selftest patch.
This is similar to commit 536b523b4073 ("bpf, riscv: Make arena support
depend on ZACAS").
Note that BPF should not depend on a SLUB debug-mode escape hatch, so
no need to consider the slub_debug exception.
Fixes: 4fdb5dd8aeba ("LoongArch: BPF: Implement bpf_addr_space_cast instruction")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
arch/loongarch/net/bpf_jit.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 3ab039d0afb6..b845083d1d74 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -2489,7 +2489,16 @@ bool bpf_jit_bypass_spec_v4(void)
bool bpf_jit_supports_arena(void)
{
- return true;
+ /*
+ * The arena range tree uses kmalloc_nolock(), which requires 128-bit
+ * atomic cmpxchg; this is provided by not only hardware SCQ extension
+ * but also assembler support on LoongArch.
+ */
+#ifdef system_has_cmpxchg128
+ return system_has_cmpxchg128();
+#else
+ return false;
+#endif
}
bool bpf_jit_supports_fsession(void)
--
2.42.0