* [PATCH 1/5] xen/arm64: detect the LSE atomic instructions
2026-11-25 17:55 [PATCH 0/5] xen/arm64: use LSE atomics when the CPU has them Luca Di Maio
@ 2026-11-25 17:55 ` Luca Di Maio
2026-11-26 7:58 ` Jan Beulich
2026-11-25 17:55 ` [PATCH 2/5] xen/arm64: use LSE in the atomic_t operations Luca Di Maio
` (3 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Luca Di Maio @ 2026-11-25 17:55 UTC (permalink / raw)
To: xen-devel
Cc: Luca Di Maio, Stefano Stabellini, Julien Grall, Bertrand Marquis,
Michal Orzel, Volodymyr Babchuk
Load/store exclusive loops give no forward-progress guarantee, so many
CPUs contending on one cache line can livelock. Add the
ARM64_HAS_LSE_ATOMICS capability and system_uses_lse_atomics(), patched
at boot like Linux's alternative_has_cap_likely(), for the next patches.
Assemblers older than binutils 2.26 lack .arch_extension, so Xen built
with one keeps the exclusive loops.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Luca Di Maio <luca@edera.dev>
---
xen/arch/arm/arch.mk | 3 +++
xen/arch/arm/cpufeature.c | 10 +++++++++
xen/arch/arm/include/asm/arm64/lse.h | 31 +++++++++++++++++++++++++++
xen/arch/arm/include/asm/cpufeature.h | 3 ++-
4 files changed, 46 insertions(+), 1 deletion(-)
create mode 100644 xen/arch/arm/include/asm/arm64/lse.h
diff --git a/xen/arch/arm/arch.mk b/xen/arch/arm/arch.mk
index dea8dbd18a..fe8e02229f 100644
--- a/xen/arch/arm/arch.mk
+++ b/xen/arch/arm/arch.mk
@@ -19,6 +19,9 @@ endif
CFLAGS-$(CONFIG_ARM_64) += -mgeneral-regs-only # No fp registers etc
$(call cc-option-add,CFLAGS-$(CONFIG_ARM_64),CC,-mno-outline-atomics)
+# Binutils >= 2.26
+$(call as-option-add,CFLAGS-$(CONFIG_ARM_64),CC,".arch_extension lse",-DHAVE_AS_LSE)
+
ifneq ($(filter command line environment,$(origin CONFIG_EARLY_PRINTK)),)
$(error You must use 'make menuconfig' to enable/disable early printk now)
endif
diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c
index 8d8cd19a96..144c3789a3 100644
--- a/xen/arch/arm/cpufeature.c
+++ b/xen/arch/arm/cpufeature.c
@@ -22,6 +22,11 @@ static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
{
return system_cpuinfo.isa64.sb;
}
+
+static bool has_lse_atomics(const struct arm_cpu_capabilities *entry)
+{
+ return system_cpuinfo.isa64.atomic >= 2;
+}
#endif
static const struct arm_cpu_capabilities arm_features[] = {
@@ -31,6 +36,11 @@ static const struct arm_cpu_capabilities arm_features[] = {
.capability = ARM_HAS_SB,
.matches = has_sb_instruction,
},
+ {
+ .desc = "LSE atomic instructions",
+ .capability = ARM64_HAS_LSE_ATOMICS,
+ .matches = has_lse_atomics,
+ },
#endif
{},
};
diff --git a/xen/arch/arm/include/asm/arm64/lse.h b/xen/arch/arm/include/asm/arm64/lse.h
new file mode 100644
index 0000000000..dbe75dfc59
--- /dev/null
+++ b/xen/arch/arm/include/asm/arm64/lse.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * As Linux's asm/lse.h: LSE atomics always complete, where load/store
+ * exclusive loops can livelock under contention. Each atomic keeps the
+ * barriers of its load/store exclusive version.
+ */
+#ifndef __ASM_ARM64_LSE_H
+#define __ASM_ARM64_LSE_H
+
+#include <asm/alternative.h>
+
+#define __LSE_PREAMBLE ".arch_extension lse\n"
+
+/* Patched at boot, like Linux's alternative_has_cap_likely(). */
+static always_inline bool system_uses_lse_atomics(void)
+{
+#ifdef HAVE_AS_LSE
+ register_t ret;
+
+ asm_inline volatile ( ALTERNATIVE("mov %0, #0", "mov %0, #1",
+ ARM64_HAS_LSE_ATOMICS)
+ : "=r" (ret) );
+
+ return likely(ret);
+#else
+ /* The compiler drops the LSE blocks, which this assembler can't take. */
+ return false;
+#endif
+}
+
+#endif /* __ASM_ARM64_LSE_H */
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/cpufeature.h
index c554686415..4d097a77ce 100644
--- a/xen/arch/arm/include/asm/cpufeature.h
+++ b/xen/arch/arm/include/asm/cpufeature.h
@@ -76,8 +76,9 @@
#define ARM_WORKAROUND_BHB_SMCC_3 15
#define ARM_HAS_SB 16
#define ARM64_WORKAROUND_1508412 17
+#define ARM64_HAS_LSE_ATOMICS 18
-#define ARM_NCAPS 18
+#define ARM_NCAPS 19
#ifndef __ASSEMBLER__
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 1/5] xen/arm64: detect the LSE atomic instructions
2026-11-25 17:55 ` [PATCH 1/5] xen/arm64: detect the LSE atomic instructions Luca Di Maio
@ 2026-11-26 7:58 ` Jan Beulich
2026-12-01 15:53 ` Luca Di Maio
0 siblings, 1 reply; 8+ messages in thread
From: Jan Beulich @ 2026-11-26 7:58 UTC (permalink / raw)
To: Luca Di Maio
Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
Volodymyr Babchuk, xen-devel
On 25.11.2026 18:55, Luca Di Maio wrote:
> Load/store exclusive loops give no forward-progress guarantee, so many
> CPUs contending on one cache line can livelock. Add the
> ARM64_HAS_LSE_ATOMICS capability and system_uses_lse_atomics(), patched
> at boot like Linux's alternative_has_cap_likely(), for the next patches.
> Assemblers older than binutils 2.26 lack .arch_extension, so Xen built
> with one keeps the exclusive loops.
>
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Luca Di Maio <luca@edera.dev>
> ---
> xen/arch/arm/arch.mk | 3 +++
> xen/arch/arm/cpufeature.c | 10 +++++++++
> xen/arch/arm/include/asm/arm64/lse.h | 31 +++++++++++++++++++++++++++
> xen/arch/arm/include/asm/cpufeature.h | 3 ++-
> 4 files changed, 46 insertions(+), 1 deletion(-)
> create mode 100644 xen/arch/arm/include/asm/arm64/lse.h
>
> diff --git a/xen/arch/arm/arch.mk b/xen/arch/arm/arch.mk
> index dea8dbd18a..fe8e02229f 100644
> --- a/xen/arch/arm/arch.mk
> +++ b/xen/arch/arm/arch.mk
> @@ -19,6 +19,9 @@ endif
> CFLAGS-$(CONFIG_ARM_64) += -mgeneral-regs-only # No fp registers etc
> $(call cc-option-add,CFLAGS-$(CONFIG_ARM_64),CC,-mno-outline-atomics)
>
> +# Binutils >= 2.26
> +$(call as-option-add,CFLAGS-$(CONFIG_ARM_64),CC,".arch_extension lse",-DHAVE_AS_LSE)
Wouldn't this better be done in Kconfig nowadays?
Jan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/5] xen/arm64: detect the LSE atomic instructions
2026-11-26 7:58 ` Jan Beulich
@ 2026-12-01 15:53 ` Luca Di Maio
0 siblings, 0 replies; 8+ messages in thread
From: Luca Di Maio @ 2026-12-01 15:53 UTC (permalink / raw)
To: Jan Beulich
Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
Volodymyr Babchuk, xen-devel
On Thu, Nov 26, 2026 at 08:58:21AM +0100, Jan Beulich wrote:
> On 25.11.2026 18:55, Luca Di Maio wrote:
> > Load/store exclusive loops give no forward-progress guarantee, so many
> > CPUs contending on one cache line can livelock. Add the
> > ARM64_HAS_LSE_ATOMICS capability and system_uses_lse_atomics(), patched
> > at boot like Linux's alternative_has_cap_likely(), for the next patches.
> > Assemblers older than binutils 2.26 lack .arch_extension, so Xen built
> > with one keeps the exclusive loops.
> >
> > Assisted-by: Claude:claude-opus-5-5
> > Signed-off-by: Luca Di Maio <luca@edera.dev>
> > ---
> > xen/arch/arm/arch.mk | 3 +++
> > xen/arch/arm/cpufeature.c | 10 +++++++++
> > xen/arch/arm/include/asm/arm64/lse.h | 31 +++++++++++++++++++++++++++
> > xen/arch/arm/include/asm/cpufeature.h | 3 ++-
> > 4 files changed, 46 insertions(+), 1 deletion(-)
> > create mode 100644 xen/arch/arm/include/asm/arm64/lse.h
> >
> > diff --git a/xen/arch/arm/arch.mk b/xen/arch/arm/arch.mk
> > index dea8dbd18a..fe8e02229f 100644
> > --- a/xen/arch/arm/arch.mk
> > +++ b/xen/arch/arm/arch.mk
> > @@ -19,6 +19,9 @@ endif
> > CFLAGS-$(CONFIG_ARM_64) += -mgeneral-regs-only # No fp registers etc
> > $(call cc-option-add,CFLAGS-$(CONFIG_ARM_64),CC,-mno-outline-atomics)
> >
> > +# Binutils >= 2.26
> > +$(call as-option-add,CFLAGS-$(CONFIG_ARM_64),CC,".arch_extension lse",-DHAVE_AS_LSE)
>
> Wouldn't this better be done in Kconfig nowadays?
>
> Jan
Hi Jan, thanks for the review.
I'll do that in V2
thanks
L.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/5] xen/arm64: use LSE in the atomic_t operations
2026-11-25 17:55 [PATCH 0/5] xen/arm64: use LSE atomics when the CPU has them Luca Di Maio
2026-11-25 17:55 ` [PATCH 1/5] xen/arm64: detect the LSE atomic instructions Luca Di Maio
@ 2026-11-25 17:55 ` Luca Di Maio
2026-11-25 17:55 ` [PATCH 3/5] xen/arm64: use LSE in xchg() and cmpxchg() Luca Di Maio
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Luca Di Maio @ 2026-11-25 17:55 UTC (permalink / raw)
To: xen-devel
Cc: Luca Di Maio, Stefano Stabellini, Julien Grall, Bertrand Marquis,
Michal Orzel, Volodymyr Babchuk
On a two-socket Graviton4 every pCPU of a socket spun in the exclusive
loop of atomic_add_return() on dom0's p2m rwlock, and dom0 hung at boot.
Use the LSE instruction for each operation when the CPU has it, with the
same barriers; CPUs without LSE keep the exclusive loops.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Luca Di Maio <luca@edera.dev>
---
xen/arch/arm/include/asm/arm64/atomic.h | 55 +++++++++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git a/xen/arch/arm/include/asm/arm64/atomic.h b/xen/arch/arm/include/asm/arm64/atomic.h
index 4460165295..a113401db6 100644
--- a/xen/arch/arm/include/asm/arm64/atomic.h
+++ b/xen/arch/arm/include/asm/arm64/atomic.h
@@ -22,6 +22,8 @@
#ifndef __ARCH_ARM_ARM64_ATOMIC
#define __ARCH_ARM_ARM64_ATOMIC
+#include <asm/arm64/lse.h>
+
/*
* AArch64 UP and SMP safe atomic ops. We use load exclusive and
* store exclusive to ensure that these are atomic. We may loop
@@ -32,6 +34,14 @@ static inline void atomic_add(int i, atomic_t *v)
unsigned long tmp;
int result;
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+" stadd %w1, %0"
+ : "+Q" (v->counter)
+ : "r" (i));
+ return;
+ }
+
asm volatile("// atomic_add\n"
"1: ldxr %w0, %2\n"
" add %w0, %w0, %w3\n"
@@ -46,6 +56,16 @@ static inline int atomic_add_return(int i, atomic_t *v)
unsigned long tmp;
int result;
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+" ldaddal %w2, %w0, %1"
+ : "=&r" (result), "+Q" (v->counter)
+ : "r" (i)
+ : "memory");
+ smp_mb();
+ return (int)((unsigned int)result + i);
+ }
+
asm volatile("// atomic_add_return\n"
"1: ldxr %w0, %2\n"
" add %w0, %w0, %w3\n"
@@ -64,6 +84,14 @@ static inline void atomic_sub(int i, atomic_t *v)
unsigned long tmp;
int result;
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+" stadd %w1, %0"
+ : "+Q" (v->counter)
+ : "r" (-(unsigned int)i));
+ return;
+ }
+
asm volatile("// atomic_sub\n"
"1: ldxr %w0, %2\n"
" sub %w0, %w0, %w3\n"
@@ -78,6 +106,16 @@ static inline int atomic_sub_return(int i, atomic_t *v)
unsigned long tmp;
int result;
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+" ldaddal %w2, %w0, %1"
+ : "=&r" (result), "+Q" (v->counter)
+ : "r" (-(unsigned int)i)
+ : "memory");
+ smp_mb();
+ return (int)((unsigned int)result - i);
+ }
+
asm volatile("// atomic_sub_return\n"
"1: ldxr %w0, %2\n"
" sub %w0, %w0, %w3\n"
@@ -96,6 +134,14 @@ static inline void atomic_and(int m, atomic_t *v)
unsigned long tmp;
int result;
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+" stclr %w1, %0"
+ : "+Q" (v->counter)
+ : "r" (~m));
+ return;
+ }
+
asm volatile("// atomic_and\n"
"1: ldxr %w0, %2\n"
" and %w0, %w0, %w3\n"
@@ -112,6 +158,15 @@ static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
smp_mb();
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+" cas %w0, %w2, %1"
+ : "+r" (old), "+Q" (v->counter)
+ : "r" (new));
+ smp_mb();
+ return old;
+ }
+
asm volatile("// atomic_cmpxchg\n"
"1: ldxr %w1, %2\n"
" cmp %w1, %w3\n"
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 3/5] xen/arm64: use LSE in xchg() and cmpxchg()
2026-11-25 17:55 [PATCH 0/5] xen/arm64: use LSE atomics when the CPU has them Luca Di Maio
2026-11-25 17:55 ` [PATCH 1/5] xen/arm64: detect the LSE atomic instructions Luca Di Maio
2026-11-25 17:55 ` [PATCH 2/5] xen/arm64: use LSE in the atomic_t operations Luca Di Maio
@ 2026-11-25 17:55 ` Luca Di Maio
2026-11-25 17:55 ` [PATCH 4/5] xen/arm64: use LSE in the bitops Luca Di Maio
2026-11-25 17:55 ` [PATCH 5/5] xen/arm64: use LSE for arch_fetch_and_add() Luca Di Maio
4 siblings, 0 replies; 8+ messages in thread
From: Luca Di Maio @ 2026-11-25 17:55 UTC (permalink / raw)
To: xen-devel
Cc: Luca Di Maio, Stefano Stabellini, Julien Grall, Bertrand Marquis,
Michal Orzel, Volodymyr Babchuk
As for the atomic_t operations, use SWPAL and CAS when the CPU has LSE,
with the same barriers. A CAS cannot fail spuriously, so the _timeout
variants used by guest atomics succeed on their first try.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Luca Di Maio <luca@edera.dev>
---
xen/arch/arm/include/asm/arm64/cmpxchg.h | 41 ++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/xen/arch/arm/include/asm/arm64/cmpxchg.h b/xen/arch/arm/include/asm/arm64/cmpxchg.h
index f160e8e7bc..442b6e1698 100644
--- a/xen/arch/arm/include/asm/arm64/cmpxchg.h
+++ b/xen/arch/arm/include/asm/arm64/cmpxchg.h
@@ -2,6 +2,7 @@
#define __ASM_ARM64_CMPXCHG_H
#include <xen/bug.h>
+#include <asm/arm64/lse.h>
extern void __bad_xchg(volatile void *ptr, int size);
@@ -11,6 +12,14 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
switch (size) {
case 1:
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+ " swpalb %w2, %w0, %1"
+ : "=&r" (ret), "+Q" (*(volatile u8 *)ptr)
+ : "r" (x)
+ : "memory");
+ break;
+ }
asm volatile("// __xchg1\n"
"1: ldxrb %w0, %2\n"
" stlxrb %w1, %w3, %2\n"
@@ -20,6 +29,14 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
: "memory");
break;
case 2:
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+ " swpalh %w2, %w0, %1"
+ : "=&r" (ret), "+Q" (*(volatile u16 *)ptr)
+ : "r" (x)
+ : "memory");
+ break;
+ }
asm volatile("// __xchg2\n"
"1: ldxrh %w0, %2\n"
" stlxrh %w1, %w3, %2\n"
@@ -29,6 +46,14 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
: "memory");
break;
case 4:
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+ " swpal %w2, %w0, %1"
+ : "=&r" (ret), "+Q" (*(volatile u32 *)ptr)
+ : "r" (x)
+ : "memory");
+ break;
+ }
asm volatile("// __xchg4\n"
"1: ldxr %w0, %2\n"
" stlxr %w1, %w3, %2\n"
@@ -38,6 +63,14 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
: "memory");
break;
case 8:
+ if (system_uses_lse_atomics()) {
+ asm volatile(__LSE_PREAMBLE
+ " swpal %2, %0, %1"
+ : "=&r" (ret), "+Q" (*(volatile u64 *)ptr)
+ : "r" (x)
+ : "memory");
+ break;
+ }
asm volatile("// __xchg8\n"
"1: ldxr %0, %2\n"
" stlxr %w1, %3, %2\n"
@@ -75,6 +108,14 @@ static inline bool __cmpxchg_case_##name(volatile void *ptr, \
unsigned long oldval; \
unsigned long res; \
\
+ if (system_uses_lse_atomics()) { \
+ asm volatile(__LSE_PREAMBLE \
+ " cas" #sz " %" #w "0, %" #w "2, %1" \
+ : "+r" (*old), "+Q" (*(volatile unsigned long *)ptr) \
+ : "r" (new)); \
+ return true; \
+ } \
+ \
do { \
asm volatile("// __cmpxchg_case_" #name "\n" \
" ldxr" #sz " %" #w "1, %2\n" \
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 4/5] xen/arm64: use LSE in the bitops
2026-11-25 17:55 [PATCH 0/5] xen/arm64: use LSE atomics when the CPU has them Luca Di Maio
` (2 preceding siblings ...)
2026-11-25 17:55 ` [PATCH 3/5] xen/arm64: use LSE in xchg() and cmpxchg() Luca Di Maio
@ 2026-11-25 17:55 ` Luca Di Maio
2026-11-25 17:55 ` [PATCH 5/5] xen/arm64: use LSE for arch_fetch_and_add() Luca Di Maio
4 siblings, 0 replies; 8+ messages in thread
From: Luca Di Maio @ 2026-11-25 17:55 UTC (permalink / raw)
To: xen-devel
Cc: Luca Di Maio, Stefano Stabellini, Julien Grall, Bertrand Marquis,
Michal Orzel, Volodymyr Babchuk
As for the atomic_t operations, use ST{SET,CLR,EOR} and
LD{SET,CLR,EOR}AL when the CPU has LSE, with the same barriers; the
macros take the LSE operation as a new argument. The _timeout variants
now succeed on their first try.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Luca Di Maio <luca@edera.dev>
---
xen/arch/arm/arm64/lib/bitops.c | 47 +++++++++++++++++++++++++++------
1 file changed, 39 insertions(+), 8 deletions(-)
diff --git a/xen/arch/arm/arm64/lib/bitops.c b/xen/arch/arm/arm64/lib/bitops.c
index 275a780329..0153e4f68b 100644
--- a/xen/arch/arm/arm64/lib/bitops.c
+++ b/xen/arch/arm/arm64/lib/bitops.c
@@ -15,6 +15,7 @@
*/
#include <xen/bitops.h>
+#include <asm/arm64/lse.h>
#include <asm/system.h>
/*
@@ -28,7 +29,7 @@
* XXX: Rework the interface to use unsigned int.
*/
-#define bitop(name, instr) \
+#define bitop(name, instr, lse_op) \
static always_inline bool int_##name(int nr, volatile void *p, bool timeout,\
unsigned int max_try) \
{ \
@@ -37,6 +38,15 @@ static always_inline bool int_##name(int nr, volatile void *p, bool timeout,\
const uint32_t mask = BITOP_MASK((unsigned int)nr); \
unsigned long res, tmp; \
\
+ if ( system_uses_lse_atomics() ) \
+ { \
+ asm volatile ( __LSE_PREAMBLE \
+ " st" #lse_op " %w1, %0\n" \
+ : "+Q" (*ptr) \
+ : "r" (mask) ); \
+ return true; \
+ } \
+ \
do \
{ \
asm volatile ("// " __stringify(name) "\n" \
@@ -64,7 +74,7 @@ bool name##_timeout(int nr, volatile void *p, unsigned int max_try) \
return int_##name(nr, p, true, max_try); \
}
-#define testop(name, instr) \
+#define testop(name, instr, lse_op) \
static always_inline bool int_##name(int nr, volatile void *p, int *oldbit, \
bool timeout, unsigned int max_try) \
{ \
@@ -74,6 +84,18 @@ static always_inline bool int_##name(int nr, volatile void *p, int *oldbit, \
const uint32_t mask = BITOP_MASK(bit); \
unsigned long res, tmp; \
\
+ if ( system_uses_lse_atomics() ) \
+ { \
+ asm volatile ( __LSE_PREAMBLE \
+ " ld" #lse_op "al %w2, %w0, %1\n" \
+ : "=&r" (tmp), "+Q" (*ptr) \
+ : "r" (mask) \
+ : "memory" ); \
+ dmb(ish); \
+ *oldbit = (tmp >> bit) & 1; \
+ return true; \
+ } \
+ \
do \
{ \
asm volatile ("// " __stringify(name) "\n" \
@@ -112,19 +134,28 @@ bool name##_timeout(int nr, volatile void *p, \
return int_##name(nr, p, oldbit, true, max_try); \
}
-bitop(change_bit, eor)
-bitop(clear_bit, bic)
-bitop(set_bit, orr)
+bitop(change_bit, eor, eor)
+bitop(clear_bit, bic, clr)
+bitop(set_bit, orr, set)
-testop(test_and_change_bit, eor)
-testop(test_and_clear_bit, bic)
-testop(test_and_set_bit, orr)
+testop(test_and_change_bit, eor, eor)
+testop(test_and_clear_bit, bic, clr)
+testop(test_and_set_bit, orr, set)
static always_inline bool int_clear_mask16(uint16_t mask, volatile uint16_t *p,
bool timeout, unsigned int max_try)
{
unsigned long res, tmp;
+ if ( system_uses_lse_atomics() )
+ {
+ asm volatile ( __LSE_PREAMBLE
+ " stclrh %w1, %0\n"
+ : "+Q" (*p)
+ : "r" (mask) );
+ return true;
+ }
+
do
{
asm volatile ("// int_clear_mask16\n"
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 5/5] xen/arm64: use LSE for arch_fetch_and_add()
2026-11-25 17:55 [PATCH 0/5] xen/arm64: use LSE atomics when the CPU has them Luca Di Maio
` (3 preceding siblings ...)
2026-11-25 17:55 ` [PATCH 4/5] xen/arm64: use LSE in the bitops Luca Di Maio
@ 2026-11-25 17:55 ` Luca Di Maio
4 siblings, 0 replies; 8+ messages in thread
From: Luca Di Maio @ 2026-11-25 17:55 UTC (permalink / raw)
To: xen-devel
Cc: Luca Di Maio, Stefano Stabellini, Julien Grall, Bertrand Marquis,
Michal Orzel, Volodymyr Babchuk
The spinlock tickets come from __sync_fetch_and_add(), which the
compiler builds from an exclusive loop for Armv8.0. On arm64, use
LDADDAL when the CPU has LSE and the builtin otherwise; the builtin
macro is now arm32 only, as it would replace the arm64 function.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Luca Di Maio <luca@edera.dev>
---
xen/arch/arm/include/asm/arm64/system.h | 20 ++++++++++++++++++++
xen/arch/arm/include/asm/system.h | 2 ++
2 files changed, 22 insertions(+)
diff --git a/xen/arch/arm/include/asm/arm64/system.h b/xen/arch/arm/include/asm/arm64/system.h
index 2e2ee212a1..4cf3295b16 100644
--- a/xen/arch/arm/include/asm/arm64/system.h
+++ b/xen/arch/arm/include/asm/arm64/system.h
@@ -3,6 +3,7 @@
#define __ASM_ARM64_SYSTEM_H
#include <asm/arm64/cmpxchg.h>
+#include <asm/arm64/lse.h>
/* Uses uimm4 as a bitmask to select the clearing of one or more of
* the DAIF exception mask bits:
@@ -58,6 +59,25 @@ static inline int local_fiq_is_enabled(void)
return !(flags & PSR_FIQ_MASK);
}
+/* For Armv8.0 the compiler builds the builtin from an LL/SC loop. */
+static always_inline uint32_t arch_fetch_and_add(volatile uint32_t *ptr,
+ uint32_t v)
+{
+ if ( system_uses_lse_atomics() )
+ {
+ uint32_t old;
+
+ asm volatile ( __LSE_PREAMBLE
+ "ldaddal %w2, %w0, %1"
+ : "=&r" (old), "+Q" (*ptr)
+ : "r" (v)
+ : "memory" );
+ return old;
+ }
+
+ return __sync_fetch_and_add(ptr, v);
+}
+
#endif
/*
* Local variables:
diff --git a/xen/arch/arm/include/asm/system.h b/xen/arch/arm/include/asm/system.h
index 65d5c8e423..b988f79102 100644
--- a/xen/arch/arm/include/asm/system.h
+++ b/xen/arch/arm/include/asm/system.h
@@ -58,7 +58,9 @@ static inline int local_abort_is_enabled(void)
return !(flags & PSR_ABT_MASK);
}
+#ifdef CONFIG_ARM_32
#define arch_fetch_and_add(x, v) __sync_fetch_and_add(x, v)
+#endif
extern struct vcpu *__context_switch(struct vcpu *prev, struct vcpu *next);
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread