mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] x86/asm: Implement local_xchg using CMPXCHG without lock prefix
@ 2024-01-24 10:58 Uros Bizjak
  2024-03-01 12:38 ` [tip: locking/core] locking/x86: Implement local_xchg() using CMPXCHG without the LOCK prefix tip-bot2 for Uros Bizjak
  0 siblings, 1 reply; 3+ messages in thread
From: Uros Bizjak @ 2024-01-24 10:58 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Uros Bizjak, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin

Implement local_xchg using CMPXCHG instruction without lock prefix.
XCHG is expensive due to the implied lock prefix.  The processor
cannot prefetch cachelines if XCHG is used.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
---
 arch/x86/include/asm/local.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/local.h b/arch/x86/include/asm/local.h
index 73dba8b94443..f9af6908aa2f 100644
--- a/arch/x86/include/asm/local.h
+++ b/arch/x86/include/asm/local.h
@@ -131,8 +131,20 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)
 				 (typeof(l->a.counter) *) old, new);
 }
 
-/* Always has a lock prefix */
-#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
+/*
+ * Implement local_xchg using CMPXCHG instruction without lock prefix.
+ * XCHG is expensive due to the implied lock prefix.  The processor
+ * cannot prefetch cachelines if XCHG is used.
+ */
+static __always_inline long
+local_xchg(local_t *l, long n)
+{
+	long c = local_read(l);
+
+	do { } while (!local_try_cmpxchg(l, &c, n));
+
+	return c;
+}
 
 /**
  * local_add_unless - add unless the number is already a given value
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tip: locking/core] locking/x86: Implement local_xchg() using CMPXCHG without the LOCK prefix
  2024-01-24 10:58 [PATCH] x86/asm: Implement local_xchg using CMPXCHG without lock prefix Uros Bizjak
@ 2024-03-01 12:38 ` tip-bot2 for Uros Bizjak
  0 siblings, 0 replies; 3+ messages in thread
From: tip-bot2 for Uros Bizjak @ 2024-03-01 12:38 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Uros Bizjak, Ingo Molnar, Peter Zijlstra, Waiman Long,
	Will Deacon, Thomas Gleixner, Linus Torvalds, Paul E. McKenney,
	H. Peter Anvin, x86, linux-kernel

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     e807c2a37044a51de89d6d4f8a1f5ecfb3752f36
Gitweb:        https://git.kernel.org/tip/e807c2a37044a51de89d6d4f8a1f5ecfb3752f36
Author:        Uros Bizjak <ubizjak@gmail.com>
AuthorDate:    Wed, 24 Jan 2024 11:58:16 +01:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Fri, 01 Mar 2024 12:54:25 +01:00

locking/x86: Implement local_xchg() using CMPXCHG without the LOCK prefix

Implement local_xchg() using the CMPXCHG instruction without the LOCK prefix.
XCHG is expensive due to the implied LOCK prefix.  The processor
cannot prefetch cachelines if XCHG is used.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Link: https://lore.kernel.org/r/20240124105816.612670-1-ubizjak@gmail.com
---
 arch/x86/include/asm/local.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/local.h b/arch/x86/include/asm/local.h
index 73dba8b..59aa966 100644
--- a/arch/x86/include/asm/local.h
+++ b/arch/x86/include/asm/local.h
@@ -131,8 +131,20 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)
 				 (typeof(l->a.counter) *) old, new);
 }
 
-/* Always has a lock prefix */
-#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
+/*
+ * Implement local_xchg using CMPXCHG instruction without the LOCK prefix.
+ * XCHG is expensive due to the implied LOCK prefix.  The processor
+ * cannot prefetch cachelines if XCHG is used.
+ */
+static __always_inline long
+local_xchg(local_t *l, long n)
+{
+	long c = local_read(l);
+
+	do { } while (!local_try_cmpxchg(l, &c, n));
+
+	return c;
+}
 
 /**
  * local_add_unless - add unless the number is already a given value

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86/asm: Implement local_xchg using CMPXCHG without lock prefix
@ 2024-03-01 21:04 Rebecca Stemmler
  0 siblings, 0 replies; 3+ messages in thread
From: Rebecca Stemmler @ 2024-03-01 21:04 UTC (permalink / raw)
  To: ubizjak; +Cc: bp, dave.hansen, hpa, linux-kernel, mingo, tglx, x86


Sent from my iPhone

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-03-01 21:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-24 10:58 [PATCH] x86/asm: Implement local_xchg using CMPXCHG without lock prefix Uros Bizjak
2024-03-01 12:38 ` [tip: locking/core] locking/x86: Implement local_xchg() using CMPXCHG without the LOCK prefix tip-bot2 for Uros Bizjak
2024-03-01 21:04 [PATCH] x86/asm: Implement local_xchg using CMPXCHG without lock prefix Rebecca Stemmler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®