Skip to contentSkip to conversation

x86, ticketlock: spin_unlock_wait() can livelock

PATCH In review Oleg Nesterov 3 participants 6 messages Dec 1, 2014 – Dec 9, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86, ticketlock: spin_unlock_wait() can livelock

Please review, I'm afraid I could miss something.

OTOH, this almost looks like a bug to me. And in fact I have a bug report which looks as if spin_unlock_wait() actually spins "forever" until the system panics, although most probably the are other problems in kernel/sched which lead to rq->lock contention.

Do we need a barrier() in arch_spin_unlock_wait() ? I guess no, and the current code doesn't have it.

Perhaps it should use __ticket_lock_spinning() like arch_spin_lock() ?

And probably we should add the lockdep annotations.

Oleg.

#1 lore raw

PATCH 1/1 x86, ticketlock: spin_unlock_wait() can livelock

arch_spin_unlock_wait() looks very suboptimal, to the point I think this is just wrong and can lead to livelock: if the lock is heavily contended we can never see head == tail.

But we do not need to wait for arch_spin_is_locked() == F. If it is locked we only need to wait until the current owner drops this lock. So we could simply spin until old_head != lock->tickets.head in this case, but .head can overflow and thus we can't check "unlocked" only once before the main loop.

Also, the "unlocked" check can ignore TICKET_SLOWPATH_FLAG bit.

---
 arch/x86/include/asm/spinlock.h |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)
arch/x86/include/asm/spinlock.h +13 -1
index 9295016..a4efe47 100644
@@ -183,8 +183,20 @@ static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
 
 static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
 {
-	while (arch_spin_is_locked(lock))
+	__ticket_t head = ACCESS_ONCE(lock->tickets.head);
+
+	for (;;) {
+		struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
+		/*
+		 * We need to check "unlocked" in a loop, tmp.head == head
+		 * can be false positive because of overflow.
+		 */
+		if (tmp.head == (tmp.tail & ~TICKET_SLOWPATH_FLAG) ||
+		    tmp.head != head)
+			break;
+
 		cpu_relax();
+	}
 }
 
 /*
Signature · 1 line
1.5.5.1
#2 lore raw
Quoted 6 lines · Oleg Nesterov

But we do not need to wait for arch_spin_is_locked() == F. If it is locked we only need to wait until the current owner drops this lock. So we could simply spin until old_head != lock->tickets.head in this case, but .head can overflow and thus we can't check "unlocked" only once before the main loop.

Ack. Looks like a good idea. Can you point to the report that you think this might be the cause of?

Linus

#5 lore raw

[tip:core/locking] x86/ticketlock: Fix spin_unlock_wait() livelock

In reply to Oleg Nesterov
x86/ticketlock: Fix spin_unlock_wait() livelock
Commit-ID:  78bff1c8684fb94f1ae7283688f90188b53fc433
Gitweb:     http://git.kernel.org/tip/78bff1c8684fb94f1ae7283688f90188b53fc433
Author:     Oleg Nesterov <[email protected]>
AuthorDate: Mon, 1 Dec 2014 22:34:17 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 8 Dec 2014 11:36:44 +0100

x86/ticketlock: Fix spin_unlock_wait() livelock

arch_spin_unlock_wait() looks very suboptimal, to the point I think this is just wrong and can lead to livelock: if the lock is heavily contended we can never see head == tail.

But we do not need to wait for arch_spin_is_locked() == F. If it is locked we only need to wait until the current owner drops this lock. So we could simply spin until old_head != lock->tickets.head in this case, but .head can overflow and thus we can't check "unlocked" only once before the main loop.

Also, the "unlocked" check can ignore TICKET_SLOWPATH_FLAG bit.

---
 arch/x86/include/asm/spinlock.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/x86/include/asm/spinlock.h +13 -1
index bf156de..abc34e9 100644
@@ -184,8 +184,20 @@ static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
 
 static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
 {
-	while (arch_spin_is_locked(lock))
+	__ticket_t head = ACCESS_ONCE(lock->tickets.head);
+
+	for (;;) {
+		struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
+		/*
+		 * We need to check "unlocked" in a loop, tmp.head == head
+		 * can be false positive because of overflow.
+		 */
+		if (tmp.head == (tmp.tail & ~TICKET_SLOWPATH_FLAG) ||
+		    tmp.head != head)
+			break;
+
 		cpu_relax();
+	}
 }
 
 /*