[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/5] xen/arm64: detect the LSE atomic instructions



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@xxxxxxxxx>
---
 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




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.