All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] arm64: spinlock: serialise spin_unlock_wait against concurrent lockers
@ 2015-11-27 11:44 Will Deacon
  2015-11-30 15:58 ` Peter Zijlstra
  2015-12-01  0:40 ` Boqun Feng
  0 siblings, 2 replies; 37+ messages in thread
From: Will Deacon @ 2015-11-27 11:44 UTC (permalink / raw)
  To: linux-arm-kernel

Boqun Feng reported a rather nasty ordering issue with spin_unlock_wait
on architectures implementing spin_lock with LL/SC sequences and acquire
semantics:

 | CPU 1                   CPU 2                     CPU 3
 | ==================      ====================      ==============
 |                                                   spin_unlock(&lock);
 |                         spin_lock(&lock):
 |                           r1 = *lock; // r1 == 0;
 |                         o = READ_ONCE(object); // reordered here
 | object = NULL;
 | smp_mb();
 | spin_unlock_wait(&lock);
 |                           *lock = 1;
 | smp_mb();
 | o->dead = true;
 |                         if (o) // true
 |                           BUG_ON(o->dead); // true!!

The crux of the problem is that spin_unlock_wait(&lock) can return on
CPU 1 whilst CPU 2 is in the process of taking the lock. This can be
resolved by upgrading spin_unlock_wait to a LOCK operation, forcing it
to serialise against a concurrent locker and giving it acquire semantics
in the process (although it is not at all clear whether this is needed -
different callers seem to assume different things about the barrier
semantics and architectures are similarly disjoint in their
implementations of the macro).

This patch implements spin_unlock_wait using an LL/SC sequence with
acquire semantics on arm64. For v8.1 systems with the LSE atomics, the
exclusive writeback is omitted, since the spin_lock operation is
indivisible and no intermediate state can be observed.

Signed-off-by: Will Deacon <will.deacon@arm.com>
---
 arch/arm64/include/asm/spinlock.h | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/include/asm/spinlock.h b/arch/arm64/include/asm/spinlock.h
index c85e96d174a5..fc9682bfe002 100644
--- a/arch/arm64/include/asm/spinlock.h
+++ b/arch/arm64/include/asm/spinlock.h
@@ -26,9 +26,28 @@
  * The memory barriers are implicit with the load-acquire and store-release
  * instructions.
  */
+static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
+{
+	unsigned int tmp;
+	arch_spinlock_t lockval;
 
-#define arch_spin_unlock_wait(lock) \
-	do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
+	asm volatile(
+"	sevl\n"
+"1:	wfe\n"
+"2:	ldaxr	%w0, %2\n"
+"	eor	%w1, %w0, %w0, ror #16\n"
+"	cbnz	%w1, 1b\n"
+	ARM64_LSE_ATOMIC_INSN(
+	/* LL/SC */
+"	stxr	%w1, %w0, %2\n"
+"	cbnz	%w1, 2b\n", /* Serialise against any concurrent lockers */
+	/* LSE atomics */
+"	nop\n"
+"	nop\n")
+	: "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
+	:
+	: "memory");
+}
 
 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
 
-- 
2.1.4

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

end of thread, other threads:[~2015-12-11 13:54 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-27 11:44 [PATCH] arm64: spinlock: serialise spin_unlock_wait against concurrent lockers Will Deacon
2015-11-30 15:58 ` Peter Zijlstra
2015-11-30 18:21   ` Paul E. McKenney
2015-12-01 16:40   ` Will Deacon
2015-12-03  0:11     ` Paul E. McKenney
2015-12-03 13:28       ` Peter Zijlstra
2015-12-03 16:32         ` Will Deacon
2015-12-03 17:22           ` Paul E. McKenney
2015-12-04  9:21             ` Peter Zijlstra
2015-12-04 16:07               ` Paul E. McKenney
2015-12-04 16:24                 ` Will Deacon
2015-12-04 16:44                   ` Paul E. McKenney
2015-12-06  7:37                     ` Boqun Feng
2015-12-06 19:23                       ` Paul E. McKenney
2015-12-06 23:28                         ` Boqun Feng
2015-12-07  0:00                           ` Paul E. McKenney
2015-12-07  0:45                             ` Boqun Feng
2015-12-07 10:34                               ` Peter Zijlstra
2015-12-07 15:45                                 ` Paul E. McKenney
2015-12-08  8:42                                   ` Boqun Feng
2015-12-08 19:17                                     ` Paul E. McKenney
2015-12-09  6:43                                       ` Boqun Feng
2015-12-04  9:36             ` Peter Zijlstra
2015-12-04 16:13               ` Paul E. McKenney
2015-12-07  2:12                 ` Michael Ellerman
2015-12-06  8:16             ` Boqun Feng
2015-12-06 19:27               ` Paul E. McKenney
2015-12-07  0:26                 ` Boqun Feng
2015-12-11  8:09                   ` Boqun Feng
2015-12-11  9:46                     ` Will Deacon
2015-12-11 12:20                       ` Boqun Feng
2015-12-11 13:42                       ` Paul E. McKenney
2015-12-11 13:54                         ` Will Deacon
2015-12-01  0:40 ` Boqun Feng
2015-12-01 16:32   ` Will Deacon
2015-12-02  9:40     ` Boqun Feng
2015-12-02 11:16       ` Boqun Feng

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.