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 3/5] xen/arm64: use LSE in xchg() and cmpxchg()
Date: Wed, 25 Nov 2026 18:55:50 +0100	[thread overview]
Message-ID: <20261125175555.2110163-4-luca@edera.dev> (raw)
In-Reply-To: <20261125175555.2110163-1-luca@edera.dev>

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



  parent reply	other threads:[~2026-11-25 17:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-26  7:58   ` Jan Beulich
2026-12-01 15:53     ` 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 [this message]
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

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=20261125175555.2110163-4-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.