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

From: Huacai Chen

Date: Tue Nov 03 2026 - 23:29:51 EST


On Tue, Nov 3, 2026 at 10:24 AM Tiezhu Yang <yangtiezhu@xxxxxxxxxxx> wrote:
>
> On 2026/11/2 下午10:20, Huacai Chen wrote:
> > 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.
>
> ...
>
> >> 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.
>
> This was my initial thought too; it is simpler, but unfortunately
> not enough here.
>
> The 128-bit atomic cmpxchg arch_cmpxchg128() not only depends on
> the hardware SCQ extension, but also requires assembler support,
> which was addressed in commit c8b8f3c50f48 ("LoongArch: Only use
> SC.Q when supported by the assembler").
>
> Therefore, using #ifdef system_has_cmpxchg128 as in the current
> patch is necessary to handle toolchain compatibility properly.
>
> Here is a simple diff to verify this scenario. On a 3A6000 machine
> with hardware SCQ extension, if we hack bpf_jit_supports_arena() to
> return cpu_has_scq directly, but force AS_HAS_SCQ_EXTENSION to 'n'
> to simulate an environment without assembler support, the original
> test error "libbpf: map 'arena': failed to create: -ENOMEM" still
> shows up because the underlying slab allocator lacks the flag
> __CMPXCHG_DOUBLE.
>
> ```
> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> index b5ed4881d3c0..6f572d24d2c3 100644
> --- a/arch/loongarch/Kconfig
> +++ b/arch/loongarch/Kconfig
> @@ -336,7 +336,7 @@ config AS_HAS_LVZ_EXTENSION
> def_bool $(as-instr,hvcl 0)
>
> config AS_HAS_SCQ_EXTENSION
> - def_bool $(as-instr,sc.q \$t0$(comma)\$t1$(comma)\$t2)
> + def_bool n
>
> config CC_HAS_ANNOTATE_TABLEJUMP
> def_bool $(cc-option,-mannotate-tablejump)
> diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> index 8cc78efe722d..1e8049937851 100644
> --- a/arch/loongarch/net/bpf_jit.c
> +++ b/arch/loongarch/net/bpf_jit.c
> @@ -2490,15 +2490,7 @@ bool bpf_jit_bypass_spec_v4(void)
>
> bool bpf_jit_supports_arena(void)
> {
> - /*
> - * 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
> + return cpu_has_scq;
> }
>
> bool bpf_jit_supports_fsession(void)
> ```
>
> So, the current patch has no problem IMO.
OK, then I think we can use:

return __is_defined(system_has_cmpxchg128) && system_has_cmpxchg128();

Huacai

>
> Thanks,
> Tiezhu
>
>