]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
locking/x86: Implement local_xchg() using CMPXCHG without the LOCK prefix
authorUros Bizjak <ubizjak@gmail.com>
Wed, 24 Jan 2024 10:58:16 +0000 (11:58 +0100)
committerIngo Molnar <mingo@kernel.org>
Fri, 1 Mar 2024 11:54:25 +0000 (12:54 +0100)
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

index 73dba8b9444308d603e5e5db6b76d412aea6b3b5..59aa966dc2127c61be2e3bbe443516a54e70c725 100644 (file)
@@ -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