Re: [PATCH v1 1/2] LoongArch: BPF: Make arena support depend on SCQ extension

From: Huacai Chen

Date: Mon Nov 02 2026 - 09:22:10 EST


Hi, Tiezhu,

On Mon, Nov 2, 2026 at 3:28 PM Tiezhu Yang <yangtiezhu@xxxxxxxxxxx> wrote:
>
> 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
> #193/2 jit_insns/cast_to_user:OK
> #193 jit_insns:OK
> 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)
> ...
> #193/2 jit_insns/cast_to_user:FAIL
> #193 jit_insns:FAIL
> 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 support, provided by SCQ extension on LoongArch. On
> CPUs without the SCQ extension, arena map creation fails with a misleading
> -ENOMEM error.
>
> Make bpf_jit_supports_arena() return system_has_cmpxchg128() where it is
> defined, so arena map creation fails with -EOPNOTSUPP on CPUs without SCQ
> extension.
>
> Furthermore, given that the macro system_has_cmpxchg128() is only defined
> when CONFIG_AS_HAS_SCQ_EXTENSION is enabled, guard it with an #ifdef to
> preserve compile-time robustness and reject arena otherwise.
>
> 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)
> ...
> #193/2 jit_insns/cast_to_user:FAIL
> #193 jit_insns:FAIL
> 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").
>
> 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 | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> index 3ab039d0afb6..01c12da168ee 100644
> --- a/arch/loongarch/net/bpf_jit.c
> +++ b/arch/loongarch/net/bpf_jit.c
> @@ -2489,7 +2489,15 @@ 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 support, provided by SCQ extension on LoongArch.
> + */
> +#ifdef system_has_cmpxchg128
> + return system_has_cmpxchg128();
> +#else
> + return false;
> +#endif
You can "return cpu_has_scq" directly.

Huacai

> }
>
> bool bpf_jit_supports_fsession(void)
> --
> 2.42.0
>