Skip to content

Commit e274795

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/mutex: Fix mutex handoff
While reviewing the ww_mutex patches, I noticed that it was still possible to (incorrectly) succeed for (incorrect) code like: mutex_lock(&a); mutex_lock(&a); This was possible if the second mutex_lock() would block (as expected) but then receive a spurious wakeup. At that point it would find itself at the front of the queue, request a handoff and instantly claim ownership and continue, since owner would point to itself. Avoid this scenario and simplify the code by introducing a third low bit to signal handoff pickup. So once we request handoff, unlock clears the handoff bit and sets the pickup bit along with the new owner. This also removes the need for the .handoff argument to __mutex_trylock(), since that becomes superfluous with PICKUP. In order to guarantee enough low bits, ensure task_struct alignment is at least L1_CACHE_BYTES (which seems a good ideal regardless). Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Fixes: 9d659ae ("locking/mutex: Add lock handoff to avoid starvation") Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 52b9412 commit e274795

3 files changed

Lines changed: 57 additions & 59 deletions

File tree

‎include/linux/mutex.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct mutex {
6565

6666
static inline struct task_struct *__mutex_owner(struct mutex *lock)
6767
{
68-
return (struct task_struct *)(atomic_long_read(&lock->owner) & ~0x03);
68+
return (struct task_struct *)(atomic_long_read(&lock->owner) & ~0x07);
6969
}
7070

7171
/*

‎kernel/fork.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,13 @@ void __init fork_init(void)
432432
int i;
433433
#ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
434434
#ifndef ARCH_MIN_TASKALIGN
435-
#define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
435+
#define ARCH_MIN_TASKALIGN 0
436436
#endif
437+
int align = min_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN);
438+
437439
/* create a slab on which task_structs can be allocated */
438440
task_struct_cachep = kmem_cache_create("task_struct",
439-
arch_task_struct_size, ARCH_MIN_TASKALIGN,
441+
arch_task_struct_size, align,
440442
SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, NULL);
441443
#endif
442444

‎kernel/locking/mutex.c‎

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ EXPORT_SYMBOL(__mutex_init);
5050
/*
5151
* @owner: contains: 'struct task_struct *' to the current lock owner,
5252
* NULL means not owned. Since task_struct pointers are aligned at
53-
* ARCH_MIN_TASKALIGN (which is at least sizeof(void *)), we have low
54-
* bits to store extra state.
53+
* at least L1_CACHE_BYTES, we have low bits to store extra state.
5554
*
5655
* Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
5756
* Bit1 indicates unlock needs to hand the lock to the top-waiter
57+
* Bit2 indicates handoff has been done and we're waiting for pickup.
5858
*/
5959
#define MUTEX_FLAG_WAITERS 0x01
6060
#define MUTEX_FLAG_HANDOFF 0x02
61+
#define MUTEX_FLAG_PICKUP 0x04
6162

62-
#define MUTEX_FLAGS 0x03
63+
#define MUTEX_FLAGS 0x07
6364

6465
static inline struct task_struct *__owner_task(unsigned long owner)
6566
{
@@ -72,54 +73,54 @@ static inline unsigned long __owner_flags(unsigned long owner)
7273
}
7374

7475
/*
75-
* Actual trylock that will work on any unlocked state.
76-
*
77-
* When setting the owner field, we must preserve the low flag bits.
78-
*
79-
* Be careful with @handoff, only set that in a wait-loop (where you set
80-
* HANDOFF) to avoid recursive lock attempts.
76+
* Trylock variant that retuns the owning task on failure.
8177
*/
82-
static inline bool __mutex_trylock(struct mutex *lock, const bool handoff)
78+
static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
8379
{
8480
unsigned long owner, curr = (unsigned long)current;
8581

8682
owner = atomic_long_read(&lock->owner);
8783
for (;;) { /* must loop, can race against a flag */
8884
unsigned long old, flags = __owner_flags(owner);
85+
unsigned long task = owner & ~MUTEX_FLAGS;
86+
87+
if (task) {
88+
if (likely(task != curr))
89+
break;
90+
91+
if (likely(!(flags & MUTEX_FLAG_PICKUP)))
92+
break;
8993

90-
if (__owner_task(owner)) {
91-
if (handoff && unlikely(__owner_task(owner) == current)) {
92-
/*
93-
* Provide ACQUIRE semantics for the lock-handoff.
94-
*
95-
* We cannot easily use load-acquire here, since
96-
* the actual load is a failed cmpxchg, which
97-
* doesn't imply any barriers.
98-
*
99-
* Also, this is a fairly unlikely scenario, and
100-
* this contains the cost.
101-
*/
102-
smp_mb(); /* ACQUIRE */
103-
return true;
104-
}
105-
106-
return false;
94+
flags &= ~MUTEX_FLAG_PICKUP;
95+
} else {
96+
#ifdef CONFIG_DEBUG_MUTEXES
97+
DEBUG_LOCKS_WARN_ON(flags & MUTEX_FLAG_PICKUP);
98+
#endif
10799
}
108100

109101
/*
110102
* We set the HANDOFF bit, we must make sure it doesn't live
111103
* past the point where we acquire it. This would be possible
112104
* if we (accidentally) set the bit on an unlocked mutex.
113105
*/
114-
if (handoff)
115-
flags &= ~MUTEX_FLAG_HANDOFF;
106+
flags &= ~MUTEX_FLAG_HANDOFF;
116107

117108
old = atomic_long_cmpxchg_acquire(&lock->owner, owner, curr | flags);
118109
if (old == owner)
119-
return true;
110+
return NULL;
120111

121112
owner = old;
122113
}
114+
115+
return __owner_task(owner);
116+
}
117+
118+
/*
119+
* Actual trylock that will work on any unlocked state.
120+
*/
121+
static inline bool __mutex_trylock(struct mutex *lock)
122+
{
123+
return !__mutex_trylock_or_owner(lock);
123124
}
124125

125126
#ifndef CONFIG_DEBUG_LOCK_ALLOC
@@ -171,9 +172,9 @@ static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_wait
171172

172173
/*
173174
* Give up ownership to a specific task, when @task = NULL, this is equivalent
174-
* to a regular unlock. Clears HANDOFF, preserves WAITERS. Provides RELEASE
175-
* semantics like a regular unlock, the __mutex_trylock() provides matching
176-
* ACQUIRE semantics for the handoff.
175+
* to a regular unlock. Sets PICKUP on a handoff, clears HANDOF, preserves
176+
* WAITERS. Provides RELEASE semantics like a regular unlock, the
177+
* __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
177178
*/
178179
static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
179180
{
@@ -184,10 +185,13 @@ static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
184185

185186
#ifdef CONFIG_DEBUG_MUTEXES
186187
DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
188+
DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
187189
#endif
188190

189191
new = (owner & MUTEX_FLAG_WAITERS);
190192
new |= (unsigned long)task;
193+
if (task)
194+
new |= MUTEX_FLAG_PICKUP;
191195

192196
old = atomic_long_cmpxchg_release(&lock->owner, owner, new);
193197
if (old == owner)
@@ -435,8 +439,6 @@ static bool mutex_optimistic_spin(struct mutex *lock,
435439
struct ww_acquire_ctx *ww_ctx,
436440
const bool use_ww_ctx, const bool waiter)
437441
{
438-
struct task_struct *task = current;
439-
440442
if (!waiter) {
441443
/*
442444
* The purpose of the mutex_can_spin_on_owner() function is
@@ -476,24 +478,17 @@ static bool mutex_optimistic_spin(struct mutex *lock,
476478
goto fail_unlock;
477479
}
478480

481+
/* Try to acquire the mutex... */
482+
owner = __mutex_trylock_or_owner(lock);
483+
if (!owner)
484+
break;
485+
479486
/*
480-
* If there's an owner, wait for it to either
487+
* There's an owner, wait for it to either
481488
* release the lock or go to sleep.
482489
*/
483-
owner = __mutex_owner(lock);
484-
if (owner) {
485-
if (waiter && owner == task) {
486-
smp_mb(); /* ACQUIRE */
487-
break;
488-
}
489-
490-
if (!mutex_spin_on_owner(lock, owner))
491-
goto fail_unlock;
492-
}
493-
494-
/* Try to acquire the mutex if it is unlocked. */
495-
if (__mutex_trylock(lock, waiter))
496-
break;
490+
if (!mutex_spin_on_owner(lock, owner))
491+
goto fail_unlock;
497492

498493
/*
499494
* The cpu_relax() call is a compiler barrier which forces
@@ -637,7 +632,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
637632
preempt_disable();
638633
mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
639634

640-
if (__mutex_trylock(lock, false) ||
635+
if (__mutex_trylock(lock) ||
641636
mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, false)) {
642637
/* got the lock, yay! */
643638
lock_acquired(&lock->dep_map, ip);
@@ -651,7 +646,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
651646
/*
652647
* After waiting to acquire the wait_lock, try again.
653648
*/
654-
if (__mutex_trylock(lock, false))
649+
if (__mutex_trylock(lock))
655650
goto skip_wait;
656651

657652
debug_mutex_lock_common(lock, &waiter);
@@ -674,7 +669,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
674669
* before testing the error conditions to make sure we pick up
675670
* the handoff.
676671
*/
677-
if (__mutex_trylock(lock, first))
672+
if (__mutex_trylock(lock))
678673
goto acquired;
679674

680675
/*
@@ -707,8 +702,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
707702
* state back to RUNNING and fall through the next schedule(),
708703
* or we must see its unlock and acquire.
709704
*/
710-
if ((first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, true)) ||
711-
__mutex_trylock(lock, first))
705+
if (__mutex_trylock(lock) ||
706+
(first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, true)))
712707
break;
713708

714709
spin_lock_mutex(&lock->wait_lock, flags);
@@ -865,6 +860,7 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne
865860

866861
#ifdef CONFIG_DEBUG_MUTEXES
867862
DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
863+
DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
868864
#endif
869865

870866
if (owner & MUTEX_FLAG_HANDOFF)
@@ -1003,7 +999,7 @@ __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
1003999
*/
10041000
int __sched mutex_trylock(struct mutex *lock)
10051001
{
1006-
bool locked = __mutex_trylock(lock, false);
1002+
bool locked = __mutex_trylock(lock);
10071003

10081004
if (locked)
10091005
mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);

0 commit comments

Comments
 (0)