Skip to contentSkip to conversation

LoongArch: Add 128-bit atomic cmpxchg support

PATCH Superseded George Guo 5 participants 9 messages Nov 20, 2025 – Nov 21, 2025 LoongArch

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

George Guo

author
#0 lore raw

cover LoongArch: Add 128-bit atomic cmpxchg support

This patch series adds 128-bit atomic compare-and-exchange support for LoongArch architecture, which fixes BPF scheduler test failures caused by missing 128-bit atomics support.

The series consists of two patches:

1. "LoongArch: Add 128-bit atomic cmpxchg support"
- Implements 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions
- Fixes BPF scheduler test failures (scx_central scx_qmap) where kmalloc_nolock_noprof returns NULL due to missing 128-bit atomics, leading to -ENOMEM errors during scheduler initialization

2. "LoongArch: Enable 128-bit atomics cmpxchg support"
- Adds select HAVE_CMPXCHG_DOUBLE and select HAVE_ALIGNED_STRUCT_PAGE in Kconfig to enable 128-bit atomic cmpxchg support

The issue was identified through BPF scheduler test failures where scx_central and scx_qmap schedulers would fail to initialize. Testing was performed using the scx_qmap scheduler from tools/sched_ext/, confirming that the patches resolve the initialization failures.

---
George Guo (2):
LoongArch: Add 128-bit atomic cmpxchg support
LoongArch: Enable 128-bit atomics cmpxchg support

 arch/loongarch/Kconfig               |  2 ++
 arch/loongarch/include/asm/cmpxchg.h | 46 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

---
base-commit: 8b690556d8fe074b4f9835075050fba3fb180e93
change-id: 20251120-2-d03862b2cf6d

Best regards,

Signature · 1 line
George Guo <[email protected]>
author
#1 lore raw

PATCH 1/2 LoongArch: Add 128-bit atomic cmpxchg support

From: George Guo <[email protected]>

Implement 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions.

At the same time, fix BPF scheduler test failures (scx_central scx_qmap) caused by kmalloc_nolock_noprof returning NULL due to missing 128-bit atomics. The NULL returns led 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.

---
 arch/loongarch/include/asm/cmpxchg.h | 46 ++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
arch/loongarch/include/asm/cmpxchg.h +46 -0
index 979fde61bba8a42cb4f019f13ded2a3119d4aaf4..5f8d418595cf62ec3153dd3825d80ac1fb31e883 100644
@@ -111,6 +111,43 @@ __arch_xchg(volatile void *ptr, unsigned long x, int size)
 	__ret;								\
 })
 
+union __u128_halves {
+	u128 full;
+	struct {
+		u64 low;
+		u64 high;
+	};
+};
+
+#define __cmpxchg128_asm(ld, st, ptr, old, new)				\
+({									\
+	union __u128_halves __old, __new, __ret;			\
+	volatile u64 *__ptr = (volatile u64 *)(ptr);			\
+									\
+	__old.full = (old);                                             \
+	__new.full = (new);						\
+									\
+	__asm__ __volatile__(						\
+	"1:   " ld "  %0, %4          # 128-bit cmpxchg low  \n"	\
+	"     " ld "  %1, %5          # 128-bit cmpxchg high \n"	\
+	"     bne     %0, %z6, 2f			     \n"	\
+	"     bne     %1, %z7, 2f                            \n"	\
+	"     move    $t0, %z8				     \n"	\
+	"     move    $t1, %z9				     \n"	\
+	"     " st "  $t0, $t1, %2                           \n"	\
+	"     beqz    $t0, 1b                                \n"	\
+	"2:                                                  \n"	\
+	__WEAK_LLSC_MB							\
+	: "=&r" (__ret.low), "=&r" (__ret.high),			\
+	  "=ZB" (__ptr[0]), "=ZB" (__ptr[1])				\
+	: "ZB" (__ptr[0]), "ZB" (__ptr[1]),				\
+	  "Jr" (__old.low), "Jr" (__old.high),				\
+	  "Jr" (__new.low), "Jr" (__new.high)				\
+	: "t0", "t1", "memory");					\
+									\
+	__ret.full;							\
+})
+
 static inline unsigned int __cmpxchg_small(volatile void *ptr, unsigned int old,
 					   unsigned int new, unsigned int size)
 {
@@ -198,6 +235,15 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, unsigned int
 	__res;								\
 })
 
+/* cmpxchg128 */
+#define system_has_cmpxchg128()		1
+
+#define arch_cmpxchg128(ptr, o, n)					\
+({									\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 16);				\
+	__cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);			\
+})
+
 #ifdef CONFIG_64BIT
 #define arch_cmpxchg64_local(ptr, o, n)					\
   ({									\
Signature · 1 line
2.48.1
#2 lore raw
Quoted 36 lines · George Guo

From: George Guo <[email protected]>

Implement 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions.

At the same time, fix BPF scheduler test failures (scx_central scx_qmap) caused by kmalloc_nolock_noprof returning NULL due to missing 128-bit atomics. The NULL returns led 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.

---
 arch/loongarch/include/asm/cmpxchg.h | 46 ++++++++++++++++++++++++++++++++++++

 1 file changed, 46 insertions(+)
arch/loongarch/include/asm/cmpxchg.h +0 -0
index 979fde61bba8a42cb4f019f13ded2a3119d4aaf4..5f8d418595cf62ec3153dd3825d80ac1fb31e883 100644
@@ -111,6 +111,43 @@ __arch_xchg(volatile void *ptr, unsigned long x, int size)
  	__ret;								\
  })

arch/loongarch/include/asm/cmpxchg.h +9 -0
+union __u128_halves {
+	u128 full;
+	struct {
+		u64 low;
+		u64 high;
+	};
+};
+
+#define __cmpxchg128_asm(ld, st, ptr, old, new)				\

arch/loongarch/include/asm/cmpxchg.h __arch_xchg(volatile void *ptr, unsigned long x, int size) · line 132

Quoted 10 lines
arch/loongarch/include/asm/cmpxchg.h +10 -0
+({									\
+	union __u128_halves __old, __new, __ret;			\
+	volatile u64 *__ptr = (volatile u64 *)(ptr);			\
+									\
+	__old.full = (old);                                             \
+	__new.full = (new);						\
+									\
+	__asm__ __volatile__(						\
+	"1:   " ld "  %0, %4          # 128-bit cmpxchg low  \n"	\
+	"     " ld "  %1, %5          # 128-bit cmpxchg high \n"	\

This is incorrect. It may happen that:

      SMP 1        |        SMP 2
ll.d $r4, mem      |
                   |   sc.q $t0, $t1, mem
ll.d $r5, mem + 4  |

As the second ll.d instruction raises the LL bit, you lose the info if the first ll.d instruction has succeeded. Thus you cannot figure out if someone has modified the mem during your "critical section."

You should use a normal ld.d for the high word instead. And you need to insert a dbar between ll.d and ld.d to prevent reordering.

Signature · 1 line
Xi Ruoyao <[email protected]>
#3 lore raw
In reply to George Guo
Quoted 48 lines · George Guo

From: George Guo <[email protected]>

Implement 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions.

At the same time, fix BPF scheduler test failures (scx_central scx_qmap) caused by kmalloc_nolock_noprof returning NULL due to missing 128-bit atomics. The NULL returns led 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.

---
 arch/loongarch/include/asm/cmpxchg.h | 46 ++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
arch/loongarch/include/asm/cmpxchg.h +20 -0
index 979fde61bba8a42cb4f019f13ded2a3119d4aaf4..5f8d418595cf62ec3153dd3825d80ac1fb31e883 100644
@@ -111,6 +111,43 @@ __arch_xchg(volatile void *ptr, unsigned long x, int size)
        __ret;                                                          \
 })
 
+union __u128_halves {
+       u128 full;
+       struct {
+               u64 low;
+               u64 high;
+       };
+};
+
+#define __cmpxchg128_asm(ld, st, ptr, old, new)                                \
+({                                                                     \
+       union __u128_halves __old, __new, __ret;                        \
+       volatile u64 *__ptr = (volatile u64 *)(ptr);                    \
+                                                                       \
+       __old.full = (old);                                             \
+       __new.full = (new);                                             \
+                                                                       \
+       __asm__ __volatile__(                                           \
+       "1:   " ld "  %0, %4          # 128-bit cmpxchg low  \n"        \
+       "     " ld "  %1, %5          # 128-bit cmpxchg high \n"        \
+       "     bne     %0, %z6, 2f                            \n"        \

arch/loongarch/include/asm/cmpxchg.h __arch_xchg(volatile void *ptr, unsigned long x, int size) · line 143

Quoted 10 lines
arch/loongarch/include/asm/cmpxchg.h +10 -0
+       "     bne     %1, %z7, 2f                            \n"        \
+       "     move    $t0, %z8                               \n"        \
+       "     move    $t1, %z9                               \n"        \
+       "     " st "  $t0, $t1, %2                           \n"        \
+       "     beqz    $t0, 1b                                \n"        \
+       "2:                                                  \n"        \
+       __WEAK_LLSC_MB                                                  \
+       : "=&r" (__ret.low), "=&r" (__ret.high),                        \
+         "=ZB" (__ptr[0]), "=ZB" (__ptr[1])                            \
+       : "ZB" (__ptr[0]), "ZB" (__ptr[1]),                             \

Address operand constraints:
- ld.d: "m"
- ll.d: "ZC"
- sc.q: "r"

Quoted 11 lines
arch/loongarch/include/asm/cmpxchg.h +7 -0
+         "Jr" (__old.low), "Jr" (__old.high),                          \
+         "Jr" (__new.low), "Jr" (__new.high)                           \
+       : "t0", "t1", "memory");                                        \
+                                                                       \
+       __ret.full;                                                     \
+})
+
 static inline unsigned int __cmpxchg_small(volatile void *ptr, unsigned int old,
                                           unsigned int new, unsigned int size)
 {

arch/loongarch/include/asm/cmpxchg.h __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, unsigned int · line 244

Quoted 10 lines
arch/loongarch/include/asm/cmpxchg.h +7 -0
@@ -198,6 +235,15 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, unsigned int
        __res;                                                          \
 })
 
+/* cmpxchg128 */
+#define system_has_cmpxchg128()                1
+
+#define arch_cmpxchg128(ptr, o, n)                                     \
+({                                                                     \
+       BUILD_BUG_ON(sizeof(*(ptr)) != 16);                             \
+       __cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);                    \

"sc.d" -> "sc.q"

__cmpxchg128_asm doesn’t have multiple variants, so no need to genericize it?

Quoted 10 lines
arch/loongarch/include/asm/cmpxchg.h +2 -1
+})
+
 #ifdef CONFIG_64BIT
 #define arch_cmpxchg64_local(ptr, o, n)                                        \
   ({                                                                   \
 
--

2.48.1

--
Rui

author
#4 lore raw
Quoted 79 lines · On Thu, 20 Nov 2025 17:25:34 +0800 hev <[email protected]> wrote:
Quoted 77 lines · George Guo

From: George Guo <[email protected]>

Implement 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions.

At the same time, fix BPF scheduler test failures (scx_central scx_qmap) caused by kmalloc_nolock_noprof returning NULL due to missing 128-bit atomics. The NULL returns led 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.

---
 arch/loongarch/include/asm/cmpxchg.h | 46

++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)

(quoted diff) +0 -0

b/arch/loongarch/include/asm/cmpxchg.h index 979fde61bba8a42cb4f019f13ded2a3119d4aaf4..5f8d418595cf62ec3153dd3825d80ac1fb31e883 100644 --- a/arch/loongarch/include/asm/cmpxchg.h +++ b/arch/loongarch/include/asm/cmpxchg.h @@ -111,6 +111,43 @@ __arch_xchg(volatile void *ptr, unsigned long x, int size) __ret;

                                                       \ })
(quoted diff) +28 -0
+union __u128_halves {
+       u128 full;
+       struct {
+               u64 low;
+               u64 high;
+       };
+};
+
+#define __cmpxchg128_asm(ld, st, ptr, old, new)
            \ +({
                  \
+       union __u128_halves __old, __new, __ret;
    \
+       volatile u64 *__ptr = (volatile u64 *)(ptr);
    \
+
    \
+       __old.full = (old);
    \
+       __new.full = (new);
    \
+
    \
+       __asm__ __volatile__(
    \
+       "1:   " ld "  %0, %4          # 128-bit cmpxchg low  \n"
    \
+       "     " ld "  %1, %5          # 128-bit cmpxchg high \n"
    \
+       "     bne     %0, %z6, 2f                            \n"
    \
+       "     bne     %1, %z7, 2f                            \n"
    \
+       "     move    $t0, %z8                               \n"
    \
+       "     move    $t1, %z9                               \n"
    \
+       "     " st "  $t0, $t1, %2                           \n"
    \
+       "     beqz    $t0, 1b                                \n"
    \
+       "2:                                                  \n"
    \
+       __WEAK_LLSC_MB
    \
+       : "=&r" (__ret.low), "=&r" (__ret.high),
    \
+         "=ZB" (__ptr[0]), "=ZB" (__ptr[1])
Quoted 9 lines
Quoted 3 lines
    \
+       : "ZB" (__ptr[0]), "ZB" (__ptr[1]),
    \  

Address operand constraints:
- ld.d: "m"
- ll.d: "ZC"
- sc.q: "r"

Thanks for your advice.
Could you tell me how to find these constraints?

Quoted 48 lines
Quoted 18 lines
(quoted diff) +6 -0
+         "Jr" (__old.low), "Jr" (__old.high),
    \
+         "Jr" (__new.low), "Jr" (__new.high)
    \
+       : "t0", "t1", "memory");
    \
+
    \
+       __ret.full;
    \ +})
+
 static inline unsigned int __cmpxchg_small(volatile void *ptr,

unsigned int old, unsigned int new, unsigned int size)
{

(quoted diff) +0 -0
@@ -198,6 +235,15 @@ __cmpxchg(volatile void *ptr, unsigned long
 old, unsigned long new, unsigned int __res;
                                 \ })
 

Review __cmpxchg(volatile void *ptr, unsigned long

Quoted 10 lines
(quoted diff) +6 -0
+/* cmpxchg128 */
+#define system_has_cmpxchg128()                1
+
+#define arch_cmpxchg128(ptr, o, n)
    \ +({
          \
+       BUILD_BUG_ON(sizeof(*(ptr)) != 16);
    \
+       __cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);
    \  

"sc.d" -> "sc.q"

__cmpxchg128_asm doesn’t have multiple variants, so no need to genericize it?

Quoted 11 lines
(quoted diff) +2 -1
+})
+
 #ifdef CONFIG_64BIT
 #define arch_cmpxchg64_local(ptr, o, n)
            \ ({
               \
 
--

2.48.1

--
Rui

#5 lore raw
Quoted 92 lines · George Guo
Quoted 79 lines · On Thu, 20 Nov 2025 17:25:34 +0800 hev <[email protected]> wrote:
Quoted 77 lines · George Guo

From: George Guo <[email protected]>

Implement 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions.

At the same time, fix BPF scheduler test failures (scx_central scx_qmap) caused by kmalloc_nolock_noprof returning NULL due to missing 128-bit atomics. The NULL returns led 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.

---
 arch/loongarch/include/asm/cmpxchg.h | 46

++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)

(quoted diff) +0 -0

b/arch/loongarch/include/asm/cmpxchg.h index 979fde61bba8a42cb4f019f13ded2a3119d4aaf4..5f8d418595cf62ec3153dd3825d80ac1fb31e883 100644 --- a/arch/loongarch/include/asm/cmpxchg.h +++ b/arch/loongarch/include/asm/cmpxchg.h @@ -111,6 +111,43 @@ __arch_xchg(volatile void *ptr, unsigned long x, int size) __ret;

                                                       \ })
(quoted diff) +28 -0
+union __u128_halves {
+       u128 full;
+       struct {
+               u64 low;
+               u64 high;
+       };
+};
+
+#define __cmpxchg128_asm(ld, st, ptr, old, new)
            \ +({
                  \
+       union __u128_halves __old, __new, __ret;
    \
+       volatile u64 *__ptr = (volatile u64 *)(ptr);
    \
+
    \
+       __old.full = (old);
    \
+       __new.full = (new);
    \
+
    \
+       __asm__ __volatile__(
    \
+       "1:   " ld "  %0, %4          # 128-bit cmpxchg low  \n"
    \
+       "     " ld "  %1, %5          # 128-bit cmpxchg high \n"
    \
+       "     bne     %0, %z6, 2f                            \n"
    \
+       "     bne     %1, %z7, 2f                            \n"
    \
+       "     move    $t0, %z8                               \n"
    \
+       "     move    $t1, %z9                               \n"
    \
+       "     " st "  $t0, $t1, %2                           \n"
    \
+       "     beqz    $t0, 1b                                \n"
    \
+       "2:                                                  \n"
    \
+       __WEAK_LLSC_MB
    \
+       : "=&r" (__ret.low), "=&r" (__ret.high),
    \
+         "=ZB" (__ptr[0]), "=ZB" (__ptr[1])
Quoted 9 lines
Quoted 3 lines
    \
+       : "ZB" (__ptr[0]), "ZB" (__ptr[1]),
    \

Address operand constraints:
- ld.d: "m"
- ll.d: "ZC"
- sc.q: "r"

Quoted 2 lines

Thanks for your advice.
Could you tell me how to find these constraints?

https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html

Quoted 49 lines
Quoted 48 lines
Quoted 18 lines
(quoted diff) +6 -0
+         "Jr" (__old.low), "Jr" (__old.high),
    \
+         "Jr" (__new.low), "Jr" (__new.high)
    \
+       : "t0", "t1", "memory");
    \
+
    \
+       __ret.full;
    \ +})
+
 static inline unsigned int __cmpxchg_small(volatile void *ptr,

unsigned int old, unsigned int new, unsigned int size)
{

(quoted diff) +0 -0
@@ -198,6 +235,15 @@ __cmpxchg(volatile void *ptr, unsigned long
 old, unsigned long new, unsigned int __res;
                                 \ })
 

Review __cmpxchg(volatile void *ptr, unsigned long

Quoted 10 lines
(quoted diff) +6 -0
+/* cmpxchg128 */
+#define system_has_cmpxchg128()                1
+
+#define arch_cmpxchg128(ptr, o, n)
    \ +({
          \
+       BUILD_BUG_ON(sizeof(*(ptr)) != 16);
    \
+       __cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);
    \

"sc.d" -> "sc.q"

__cmpxchg128_asm doesn’t have multiple variants, so no need to genericize it?

Quoted 11 lines
(quoted diff) +2 -1
+})
+
 #ifdef CONFIG_64BIT
 #define arch_cmpxchg64_local(ptr, o, n)
            \ ({
               \
 
--

2.48.1

--
Rui

#6 lore raw
In reply to George Guo
Quoted 50 lines · On Thu, 20 Nov 2025 15:45:44 +0800 George Guo <[email protected]> wrote:

From: George Guo <[email protected]>

Implement 128-bit atomic compare-and-exchange using LoongArch's LL.D/SC.Q instructions.

At the same time, fix BPF scheduler test failures (scx_central scx_qmap) caused by kmalloc_nolock_noprof returning NULL due to missing 128-bit atomics. The NULL returns led 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.

---
 arch/loongarch/include/asm/cmpxchg.h | 46 ++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
arch/loongarch/include/asm/cmpxchg.h +23 -0
index 979fde61bba8a42cb4f019f13ded2a3119d4aaf4..5f8d418595cf62ec3153dd3825d80ac1fb31e883 100644
@@ -111,6 +111,43 @@ __arch_xchg(volatile void *ptr, unsigned long x, int size)
 	__ret;								\
 })
 
+union __u128_halves {
+	u128 full;
+	struct {
+		u64 low;
+		u64 high;
+	};
+};
+
+#define __cmpxchg128_asm(ld, st, ptr, old, new)				\
+({									\
+	union __u128_halves __old, __new, __ret;			\
+	volatile u64 *__ptr = (volatile u64 *)(ptr);			\
+									\
+	__old.full = (old);                                             \
+	__new.full = (new);						\
+									\
+	__asm__ __volatile__(						\
+	"1:   " ld "  %0, %4          # 128-bit cmpxchg low  \n"	\
+	"     " ld "  %1, %5          # 128-bit cmpxchg high \n"	\
+	"     bne     %0, %z6, 2f			     \n"	\
+	"     bne     %1, %z7, 2f                            \n"	\
+	"     move    $t0, %z8				     \n"	\
+	"     move    $t1, %z9				     \n"	\

arch/loongarch/include/asm/cmpxchg.h __arch_xchg(volatile void *ptr, unsigned long x, int size) · line 146

Quoted 10 lines
arch/loongarch/include/asm/cmpxchg.h +10 -0
+	"     " st "  $t0, $t1, %2                           \n"	\
+	"     beqz    $t0, 1b                                \n"	\
+	"2:                                                  \n"	\
+	__WEAK_LLSC_MB							\
+	: "=&r" (__ret.low), "=&r" (__ret.high),			\
+	  "=ZB" (__ptr[0]), "=ZB" (__ptr[1])				\
+	: "ZB" (__ptr[0]), "ZB" (__ptr[1]),				\
+	  "Jr" (__old.low), "Jr" (__old.high),				\
+	  "Jr" (__new.low), "Jr" (__new.high)				\
+	: "t0", "t1", "memory");					\

I'd add symbolic names for the asm registers to it easier to read.
eg: [ret_low] "=%r" (__ret.low) and replace %0 with %[rel_row]

David

Quoted 24 lines
arch/loongarch/include/asm/cmpxchg.h +13 -0
+									\
+	__ret.full;							\
+})
+
 static inline unsigned int __cmpxchg_small(volatile void *ptr, unsigned int old,
 					   unsigned int new, unsigned int size)
 {
@@ -198,6 +235,15 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, unsigned int
 	__res;								\
 })
 
+/* cmpxchg128 */
+#define system_has_cmpxchg128()		1
+
+#define arch_cmpxchg128(ptr, o, n)					\
+({									\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 16);				\
+	__cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);			\
+})
+
 #ifdef CONFIG_64BIT
 #define arch_cmpxchg64_local(ptr, o, n)					\
   ({									\
author
#7 lore raw

PATCH 2/2 LoongArch: Enable 128-bit atomics cmpxchg support

In reply to George Guo

From: George Guo <[email protected]>

Add select HAVE_CMPXCHG_DOUBLE and select HAVE_ALIGNED_STRUCT_PAGE in Kconfig to enable 128-bit atomic cmpxchg support on LoongArch.

---
 arch/loongarch/Kconfig | 2 ++
 1 file changed, 2 insertions(+)
arch/loongarch/Kconfig +2 -0
index 5b1116733d881bc2b1b43fb93f20367add4dbc54..6fb2c253969f9ddece5478920423d7326c3ec046 100644
@@ -114,6 +114,7 @@ config LOONGARCH
 	select GENERIC_TIME_VSYSCALL
 	select GPIOLIB
 	select HAS_IOPORT
+	select HAVE_ALIGNED_STRUCT_PAGE
 	select HAVE_ARCH_AUDITSYSCALL
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
@@ -140,6 +141,7 @@ config LOONGARCH
 	select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
 	select HAVE_DYNAMIC_FTRACE_WITH_REGS
 	select HAVE_EBPF_JIT
+	select HAVE_CMPXCHG_DOUBLE
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS if !ARCH_STRICT_ALIGN
 	select HAVE_EXIT_THREAD
 	select HAVE_GENERIC_TIF_BITS
Signature · 1 line
2.48.1
#8 lore raw

Re: [PATCH 2/2] LoongArch: Enable 128-bit atomics cmpxchg support

kernel test robot noticed the following build errors:

Hi George,

kernel test robot noticed the following build errors:

[auto build test ERROR on 8b690556d8fe074b4f9835075050fba3fb180e93]

url:    https://github.com/intel-lab-lkp/linux/commits/George-Guo/LoongArch-Add-128-bit-atomic-cmpxchg-support/20251120-160152
base:   8b690556d8fe074b4f9835075050fba3fb180e93
patch link:    https://lore.kernel.org/r/20251120-2-v1-2-705bdc440550%40linux.dev
patch subject: [PATCH 2/2] LoongArch: Enable 128-bit atomics cmpxchg support
config: loongarch-allnoconfig (https://download.01.org/0day-ci/archive/20251120/[email protected]/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 9e9fe08b16ea2c4d9867fb4974edf2a3776d6ece)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251120/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> mm/slub.c:766:9: error: immediate must be a multiple of 4 in the range [-32768, 32764]
     766 |         return try_cmpxchg_freelist(&slab->freelist_counter.full, &old.full, new.full);
         |                ^
   mm/slab.h:22:32: note: expanded from macro 'try_cmpxchg_freelist'
      22 | # define try_cmpxchg_freelist           try_cmpxchg128
         |                                         ^
   include/linux/atomic/atomic-instrumented.h:4956:2: note: expanded from macro 'try_cmpxchg128'
    4956 |         raw_try_cmpxchg128(__ai_ptr, __ai_oldp, __VA_ARGS__); \
         |         ^
   include/linux/atomic/atomic-arch-fallback.h:326:9: note: expanded from macro 'raw_try_cmpxchg128'
     326 |         ___r = raw_cmpxchg128((_ptr), ___o, (_new)); \
         |                ^
   include/linux/atomic/atomic-arch-fallback.h:141:24: note: expanded from macro 'raw_cmpxchg128'
     141 | #define raw_cmpxchg128 arch_cmpxchg128
         |                        ^
   arch/loongarch/include/asm/cmpxchg.h:244:2: note: expanded from macro 'arch_cmpxchg128'
     244 |         __cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);                    \
         |         ^
   arch/loongarch/include/asm/cmpxchg.h:137:3: note: expanded from macro '__cmpxchg128_asm'
     137 |         "     " st "  $t0, $t1, %2                           \n"        \
         |          ^
   <inline asm>:7:23: note: instantiated into assembly here
       7 |      sc.d  $t0, $t1, $a2, 0                           
         |                       ^
>> mm/slub.c:766:9: error: immediate must be a multiple of 4 in the range [-32768, 32764]
     766 |         return try_cmpxchg_freelist(&slab->freelist_counter.full, &old.full, new.full);
         |                ^
   mm/slab.h:22:32: note: expanded from macro 'try_cmpxchg_freelist'
      22 | # define try_cmpxchg_freelist           try_cmpxchg128
         |                                         ^
   include/linux/atomic/atomic-instrumented.h:4956:2: note: expanded from macro 'try_cmpxchg128'
    4956 |         raw_try_cmpxchg128(__ai_ptr, __ai_oldp, __VA_ARGS__); \
         |         ^
   include/linux/atomic/atomic-arch-fallback.h:326:9: note: expanded from macro 'raw_try_cmpxchg128'
     326 |         ___r = raw_cmpxchg128((_ptr), ___o, (_new)); \
         |                ^
   include/linux/atomic/atomic-arch-fallback.h:141:24: note: expanded from macro 'raw_cmpxchg128'
     141 | #define raw_cmpxchg128 arch_cmpxchg128
         |                        ^
   arch/loongarch/include/asm/cmpxchg.h:244:2: note: expanded from macro 'arch_cmpxchg128'
     244 |         __cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);                    \
         |         ^
   arch/loongarch/include/asm/cmpxchg.h:137:3: note: expanded from macro '__cmpxchg128_asm'
     137 |         "     " st "  $t0, $t1, %2                           \n"        \
         |          ^
   <inline asm>:7:23: note: instantiated into assembly here
       7 |      sc.d  $t0, $t1, $a3, 0                           
         |                       ^
>> mm/slub.c:766:9: error: immediate must be a multiple of 4 in the range [-32768, 32764]
     766 |         return try_cmpxchg_freelist(&slab->freelist_counter.full, &old.full, new.full);
         |                ^
   mm/slab.h:22:32: note: expanded from macro 'try_cmpxchg_freelist'
      22 | # define try_cmpxchg_freelist           try_cmpxchg128
         |                                         ^
   include/linux/atomic/atomic-instrumented.h:4956:2: note: expanded from macro 'try_cmpxchg128'
    4956 |         raw_try_cmpxchg128(__ai_ptr, __ai_oldp, __VA_ARGS__); \
         |         ^
   include/linux/atomic/atomic-arch-fallback.h:326:9: note: expanded from macro 'raw_try_cmpxchg128'
     326 |         ___r = raw_cmpxchg128((_ptr), ___o, (_new)); \
         |                ^
   include/linux/atomic/atomic-arch-fallback.h:141:24: note: expanded from macro 'raw_cmpxchg128'
     141 | #define raw_cmpxchg128 arch_cmpxchg128
         |                        ^
   arch/loongarch/include/asm/cmpxchg.h:244:2: note: expanded from macro 'arch_cmpxchg128'
     244 |         __cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);                    \
         |         ^
   arch/loongarch/include/asm/cmpxchg.h:137:3: note: expanded from macro '__cmpxchg128_asm'
     137 |         "     " st "  $t0, $t1, %2                           \n"        \
         |          ^
   <inline asm>:7:23: note: instantiated into assembly here
       7 |      sc.d  $t0, $t1, $a5, 0                           
         |                       ^
>> mm/slub.c:766:9: error: immediate must be a multiple of 4 in the range [-32768, 32764]
     766 |         return try_cmpxchg_freelist(&slab->freelist_counter.full, &old.full, new.full);
         |                ^
   mm/slab.h:22:32: note: expanded from macro 'try_cmpxchg_freelist'
      22 | # define try_cmpxchg_freelist           try_cmpxchg128
         |                                         ^
   include/linux/atomic/atomic-instrumented.h:4956:2: note: expanded from macro 'try_cmpxchg128'
    4956 |         raw_try_cmpxchg128(__ai_ptr, __ai_oldp, __VA_ARGS__); \
         |         ^
   include/linux/atomic/atomic-arch-fallback.h:326:9: note: expanded from macro 'raw_try_cmpxchg128'
     326 |         ___r = raw_cmpxchg128((_ptr), ___o, (_new)); \
         |                ^
   include/linux/atomic/atomic-arch-fallback.h:141:24: note: expanded from macro 'raw_cmpxchg128'
     141 | #define raw_cmpxchg128 arch_cmpxchg128
         |                        ^
   arch/loongarch/include/asm/cmpxchg.h:244:2: note: expanded from macro 'arch_cmpxchg128'
     244 |         __cmpxchg128_asm("ll.d", "sc.d", ptr, o, n);                    \
         |         ^
   arch/loongarch/include/asm/cmpxchg.h:137:3: note: expanded from macro '__cmpxchg128_asm'
     137 |         "     " st "  $t0, $t1, %2                           \n"        \
         |          ^
   <inline asm>:7:23: note: instantiated into assembly here
       7 |      sc.d  $t0, $t1, $t3, 0                           
         |                       ^

4 errors generated.

vim +766 mm/slub.c

881db7fb03a77a Christoph Lameter 2011-06-01  756  
6801be4f2653e5 Peter Zijlstra    2023-05-31  757  static inline bool
6801be4f2653e5 Peter Zijlstra    2023-05-31  758  __update_freelist_fast(struct slab *slab,
6801be4f2653e5 Peter Zijlstra    2023-05-31  759  		      void *freelist_old, unsigned long counters_old,
6801be4f2653e5 Peter Zijlstra    2023-05-31  760  		      void *freelist_new, unsigned long counters_new)
6801be4f2653e5 Peter Zijlstra    2023-05-31  761  {
6801be4f2653e5 Peter Zijlstra    2023-05-31  762  #ifdef system_has_freelist_aba
6801be4f2653e5 Peter Zijlstra    2023-05-31  763  	freelist_aba_t old = { .freelist = freelist_old, .counter = counters_old };
6801be4f2653e5 Peter Zijlstra    2023-05-31  764  	freelist_aba_t new = { .freelist = freelist_new, .counter = counters_new };
6801be4f2653e5 Peter Zijlstra    2023-05-31  765  
6801be4f2653e5 Peter Zijlstra    2023-05-31 @766  	return try_cmpxchg_freelist(&slab->freelist_counter.full, &old.full, new.full);
6801be4f2653e5 Peter Zijlstra    2023-05-31  767  #else
6801be4f2653e5 Peter Zijlstra    2023-05-31  768  	return false;
6801be4f2653e5 Peter Zijlstra    2023-05-31  769  #endif
6801be4f2653e5 Peter Zijlstra    2023-05-31  770  }
6801be4f2653e5 Peter Zijlstra    2023-05-31  771  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki