Skip to content

Commit f0e4b1b

Browse files
georgejguochenhuacai
authored andcommitted
LoongArch: Add 128-bit atomic cmpxchg support
Implement 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions. At the same time, this fix the BPF scheduler test failures (scx_central and scx_qmap) caused by kmalloc_nolock_noprof() returning NULL, due to missing 128-bit atomics. The NULL returns lead to -ENOMEM errors during scheduler initialization, causing test cases to fail. Verified by testing with the scx_qmap scheduler (located in tools/sched_ext/). Building with `make` and running ./tools/sched_ext/build/bin/scx_qmap. Link: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/commit/?id=5fb750e8a9ae Acked-by: Hengqi Chen <hengqi.chen@gmail.com> Tested-by: Hengqi Chen <hengqi.chen@gmail.com> Co-developed-by:: Xi Ruoyao <xry111@xry111.site> Signed-off-by: Xi Ruoyao <xry111@xry111.site> Signed-off-by: George Guo <guodongtai@kylinos.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 48543c4 commit f0e4b1b

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

arch/loongarch/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ config LOONGARCH
114114
select GENERIC_TIME_VSYSCALL
115115
select GPIOLIB
116116
select HAS_IOPORT
117+
select HAVE_ALIGNED_STRUCT_PAGE
117118
select HAVE_ARCH_AUDITSYSCALL
118119
select HAVE_ARCH_BITREVERSE
119120
select HAVE_ARCH_JUMP_LABEL
@@ -130,6 +131,7 @@ config LOONGARCH
130131
select HAVE_ARCH_TRANSPARENT_HUGEPAGE
131132
select HAVE_ARCH_USERFAULTFD_MINOR if USERFAULTFD
132133
select HAVE_ASM_MODVERSIONS
134+
select HAVE_CMPXCHG_DOUBLE
133135
select HAVE_CMPXCHG_LOCAL
134136
select HAVE_CONTEXT_TRACKING_USER
135137
select HAVE_C_RECORDMCOUNT

arch/loongarch/include/asm/cmpxchg.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/bits.h>
99
#include <linux/build_bug.h>
1010
#include <asm/barrier.h>
11+
#include <asm/cpu-features.h>
1112

1213
#define __xchg_amo_asm(amswap_db, m, val) \
1314
({ \
@@ -236,6 +237,59 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, unsigned int
236237
BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
237238
arch_cmpxchg((ptr), (o), (n)); \
238239
})
240+
241+
union __u128_halves {
242+
u128 full;
243+
struct {
244+
u64 low;
245+
u64 high;
246+
};
247+
};
248+
249+
#define system_has_cmpxchg128() cpu_opt(LOONGARCH_CPU_SCQ)
250+
251+
#define __arch_cmpxchg128(ptr, old, new, llsc_mb) \
252+
({ \
253+
union __u128_halves __old, __new, __ret; \
254+
volatile u64 *__ptr = (volatile u64 *)(ptr); \
255+
\
256+
__old.full = (old); \
257+
__new.full = (new); \
258+
\
259+
__asm__ __volatile__( \
260+
"1: ll.d %0, %3 # 128-bit cmpxchg low \n" \
261+
llsc_mb \
262+
" ld.d %1, %4 # 128-bit cmpxchg high \n" \
263+
" move $t0, %0 \n" \
264+
" move $t1, %1 \n" \
265+
" bne %0, %z5, 2f \n" \
266+
" bne %1, %z6, 2f \n" \
267+
" move $t0, %z7 \n" \
268+
" move $t1, %z8 \n" \
269+
"2: sc.q $t0, $t1, %2 \n" \
270+
" beqz $t0, 1b \n" \
271+
llsc_mb \
272+
: "=&r" (__ret.low), "=&r" (__ret.high) \
273+
: "r" (__ptr), \
274+
"ZC" (__ptr[0]), "m" (__ptr[1]), \
275+
"Jr" (__old.low), "Jr" (__old.high), \
276+
"Jr" (__new.low), "Jr" (__new.high) \
277+
: "t0", "t1", "memory"); \
278+
\
279+
__ret.full; \
280+
})
281+
282+
#define arch_cmpxchg128(ptr, o, n) \
283+
({ \
284+
BUILD_BUG_ON(sizeof(*(ptr)) != 16); \
285+
__arch_cmpxchg128(ptr, o, n, __WEAK_LLSC_MB); \
286+
})
287+
288+
#define arch_cmpxchg128_local(ptr, o, n) \
289+
({ \
290+
BUILD_BUG_ON(sizeof(*(ptr)) != 16); \
291+
__arch_cmpxchg128(ptr, o, n, ""); \
292+
})
239293
#else
240294
#include <asm-generic/cmpxchg-local.h>
241295
#define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n))

0 commit comments

Comments
 (0)