All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luca Di Maio <luca@edera.dev>
To: xen-devel@lists.xenproject.org
Cc: Luca Di Maio <luca@edera.dev>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Julien Grall <julien@xen.org>,
	Bertrand Marquis <bertrand.marquis@arm.com>,
	Michal Orzel <michal.orzel@amd.com>,
	Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>
Subject: [PATCH v2 1/5] xen/arm64: detect the LSE atomic instructions
Date: Tue,  1 Dec 2026 17:08:11 +0100	[thread overview]
Message-ID: <20261201160817.466800-2-luca@edera.dev> (raw)
In-Reply-To: <20261201160817.466800-1-luca@edera.dev>

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>
---
v2: move the assembler check to Kconfig (Jan)
---
 xen/arch/arm/Kconfig                  |  4 ++++
 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, 47 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/include/asm/arm64/lse.h

diff --git a/xen/arch/arm/Kconfig b/xen/arch/arm/Kconfig
index b9c5c40678..b07c15e1cf 100644
--- a/xen/arch/arm/Kconfig
+++ b/xen/arch/arm/Kconfig
@@ -11,6 +11,10 @@ config ARM_64
 	select HAS_FAST_MULTIPLY
 	select HAS_VPCI_GUEST_SUPPORT if PCI_PASSTHROUGH
 
+config HAS_AS_LSE
+	def_bool $(as-instr,.arch_extension lse)
+	depends on ARM_64
+
 config ARM
 	def_bool y
 	select DOMAIN_BUILD_HELPERS
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..3db2df3dfc
--- /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 CONFIG_HAS_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



  reply	other threads:[~2026-12-01 16:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 16:08 [PATCH v2 0/5] xen/arm64: use LSE atomics when the CPU has them Luca Di Maio
2026-12-01 16:08 ` Luca Di Maio [this message]
2026-12-01 16:08 ` [PATCH v2 2/5] xen/arm64: use LSE in the atomic_t operations Luca Di Maio
2026-12-01 16:08 ` [PATCH v2 3/5] xen/arm64: use LSE in xchg() and cmpxchg() Luca Di Maio
2026-12-01 16:08 ` [PATCH v2 4/5] xen/arm64: use LSE in the bitops Luca Di Maio
2026-12-01 16:08 ` [PATCH v2 5/5] xen/arm64: use LSE for arch_fetch_and_add() Luca Di Maio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201160817.466800-2-luca@edera.dev \
    --to=luca@edera.dev \
    --cc=Volodymyr_Babchuk@epam.com \
    --cc=bertrand.marquis@arm.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.