Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

authored by

Uros Bizjak and committed by
Ingo Molnar
e807c2a3 f3e3620f

+14 -2
+14 -2
arch/x86/include/asm/local.h
··· 131 131 (typeof(l->a.counter) *) old, new); 132 132 } 133 133 134 - /* Always has a lock prefix */ 135 - #define local_xchg(l, n) (xchg(&((l)->a.counter), (n))) 134 + /* 135 + * Implement local_xchg using CMPXCHG instruction without the LOCK prefix. 136 + * XCHG is expensive due to the implied LOCK prefix. The processor 137 + * cannot prefetch cachelines if XCHG is used. 138 + */ 139 + static __always_inline long 140 + local_xchg(local_t *l, long n) 141 + { 142 + long c = local_read(l); 143 + 144 + do { } while (!local_try_cmpxchg(l, &c, n)); 145 + 146 + return c; 147 + } 136 148 137 149 /** 138 150 * local_add_unless - add unless the number is already a given value