xref: /openbmc/linux/kernel/locking/mutex.c (revision c427f69564e2a844c5fcf2804042609342513da0)
101768b42SPeter Zijlstra /*
267a6de49SPeter Zijlstra  * kernel/locking/mutex.c
301768b42SPeter Zijlstra  *
401768b42SPeter Zijlstra  * Mutexes: blocking mutual exclusion locks
501768b42SPeter Zijlstra  *
601768b42SPeter Zijlstra  * Started by Ingo Molnar:
701768b42SPeter Zijlstra  *
801768b42SPeter Zijlstra  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
901768b42SPeter Zijlstra  *
1001768b42SPeter Zijlstra  * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
1101768b42SPeter Zijlstra  * David Howells for suggestions and improvements.
1201768b42SPeter Zijlstra  *
1301768b42SPeter Zijlstra  *  - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
1401768b42SPeter Zijlstra  *    from the -rt tree, where it was originally implemented for rtmutexes
1501768b42SPeter Zijlstra  *    by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
1601768b42SPeter Zijlstra  *    and Sven Dietrich.
1701768b42SPeter Zijlstra  *
18214e0aedSDavidlohr Bueso  * Also see Documentation/locking/mutex-design.txt.
1901768b42SPeter Zijlstra  */
2001768b42SPeter Zijlstra #include <linux/mutex.h>
2101768b42SPeter Zijlstra #include <linux/ww_mutex.h>
22174cd4b1SIngo Molnar #include <linux/sched/signal.h>
2301768b42SPeter Zijlstra #include <linux/sched/rt.h>
2484f001e1SIngo Molnar #include <linux/sched/wake_q.h>
25b17b0153SIngo Molnar #include <linux/sched/debug.h>
2601768b42SPeter Zijlstra #include <linux/export.h>
2701768b42SPeter Zijlstra #include <linux/spinlock.h>
2801768b42SPeter Zijlstra #include <linux/interrupt.h>
2901768b42SPeter Zijlstra #include <linux/debug_locks.h>
307a215f89SDavidlohr Bueso #include <linux/osq_lock.h>
3101768b42SPeter Zijlstra 
3201768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
3301768b42SPeter Zijlstra # include "mutex-debug.h"
3401768b42SPeter Zijlstra #else
3501768b42SPeter Zijlstra # include "mutex.h"
3601768b42SPeter Zijlstra #endif
3701768b42SPeter Zijlstra 
3801768b42SPeter Zijlstra void
3901768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
4001768b42SPeter Zijlstra {
413ca0ff57SPeter Zijlstra 	atomic_long_set(&lock->owner, 0);
4201768b42SPeter Zijlstra 	spin_lock_init(&lock->wait_lock);
4301768b42SPeter Zijlstra 	INIT_LIST_HEAD(&lock->wait_list);
4401768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
454d9d951eSJason Low 	osq_lock_init(&lock->osq);
4601768b42SPeter Zijlstra #endif
4701768b42SPeter Zijlstra 
4801768b42SPeter Zijlstra 	debug_mutex_init(lock, name, key);
4901768b42SPeter Zijlstra }
5001768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init);
5101768b42SPeter Zijlstra 
523ca0ff57SPeter Zijlstra /*
533ca0ff57SPeter Zijlstra  * @owner: contains: 'struct task_struct *' to the current lock owner,
543ca0ff57SPeter Zijlstra  * NULL means not owned. Since task_struct pointers are aligned at
55e274795eSPeter Zijlstra  * at least L1_CACHE_BYTES, we have low bits to store extra state.
563ca0ff57SPeter Zijlstra  *
573ca0ff57SPeter Zijlstra  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
589d659ae1SPeter Zijlstra  * Bit1 indicates unlock needs to hand the lock to the top-waiter
59e274795eSPeter Zijlstra  * Bit2 indicates handoff has been done and we're waiting for pickup.
603ca0ff57SPeter Zijlstra  */
613ca0ff57SPeter Zijlstra #define MUTEX_FLAG_WAITERS	0x01
629d659ae1SPeter Zijlstra #define MUTEX_FLAG_HANDOFF	0x02
63e274795eSPeter Zijlstra #define MUTEX_FLAG_PICKUP	0x04
643ca0ff57SPeter Zijlstra 
65e274795eSPeter Zijlstra #define MUTEX_FLAGS		0x07
663ca0ff57SPeter Zijlstra 
673ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner)
683ca0ff57SPeter Zijlstra {
693ca0ff57SPeter Zijlstra 	return (struct task_struct *)(owner & ~MUTEX_FLAGS);
703ca0ff57SPeter Zijlstra }
713ca0ff57SPeter Zijlstra 
723ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner)
733ca0ff57SPeter Zijlstra {
743ca0ff57SPeter Zijlstra 	return owner & MUTEX_FLAGS;
753ca0ff57SPeter Zijlstra }
763ca0ff57SPeter Zijlstra 
773ca0ff57SPeter Zijlstra /*
78e274795eSPeter Zijlstra  * Trylock variant that retuns the owning task on failure.
793ca0ff57SPeter Zijlstra  */
80e274795eSPeter Zijlstra static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
813ca0ff57SPeter Zijlstra {
823ca0ff57SPeter Zijlstra 	unsigned long owner, curr = (unsigned long)current;
833ca0ff57SPeter Zijlstra 
843ca0ff57SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
853ca0ff57SPeter Zijlstra 	for (;;) { /* must loop, can race against a flag */
869d659ae1SPeter Zijlstra 		unsigned long old, flags = __owner_flags(owner);
87e274795eSPeter Zijlstra 		unsigned long task = owner & ~MUTEX_FLAGS;
883ca0ff57SPeter Zijlstra 
89e274795eSPeter Zijlstra 		if (task) {
90e274795eSPeter Zijlstra 			if (likely(task != curr))
91e274795eSPeter Zijlstra 				break;
929d659ae1SPeter Zijlstra 
93e274795eSPeter Zijlstra 			if (likely(!(flags & MUTEX_FLAG_PICKUP)))
94e274795eSPeter Zijlstra 				break;
95e274795eSPeter Zijlstra 
96e274795eSPeter Zijlstra 			flags &= ~MUTEX_FLAG_PICKUP;
97e274795eSPeter Zijlstra 		} else {
98e274795eSPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
99e274795eSPeter Zijlstra 			DEBUG_LOCKS_WARN_ON(flags & MUTEX_FLAG_PICKUP);
100e274795eSPeter Zijlstra #endif
1019d659ae1SPeter Zijlstra 		}
1023ca0ff57SPeter Zijlstra 
1039d659ae1SPeter Zijlstra 		/*
1049d659ae1SPeter Zijlstra 		 * We set the HANDOFF bit, we must make sure it doesn't live
1059d659ae1SPeter Zijlstra 		 * past the point where we acquire it. This would be possible
1069d659ae1SPeter Zijlstra 		 * if we (accidentally) set the bit on an unlocked mutex.
1079d659ae1SPeter Zijlstra 		 */
1089d659ae1SPeter Zijlstra 		flags &= ~MUTEX_FLAG_HANDOFF;
1099d659ae1SPeter Zijlstra 
1109d659ae1SPeter Zijlstra 		old = atomic_long_cmpxchg_acquire(&lock->owner, owner, curr | flags);
1113ca0ff57SPeter Zijlstra 		if (old == owner)
112e274795eSPeter Zijlstra 			return NULL;
1133ca0ff57SPeter Zijlstra 
1143ca0ff57SPeter Zijlstra 		owner = old;
1153ca0ff57SPeter Zijlstra 	}
116e274795eSPeter Zijlstra 
117e274795eSPeter Zijlstra 	return __owner_task(owner);
118e274795eSPeter Zijlstra }
119e274795eSPeter Zijlstra 
120e274795eSPeter Zijlstra /*
121e274795eSPeter Zijlstra  * Actual trylock that will work on any unlocked state.
122e274795eSPeter Zijlstra  */
123e274795eSPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock)
124e274795eSPeter Zijlstra {
125e274795eSPeter Zijlstra 	return !__mutex_trylock_or_owner(lock);
1263ca0ff57SPeter Zijlstra }
1273ca0ff57SPeter Zijlstra 
1283ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
1293ca0ff57SPeter Zijlstra /*
1303ca0ff57SPeter Zijlstra  * Lockdep annotations are contained to the slow paths for simplicity.
1313ca0ff57SPeter Zijlstra  * There is nothing that would stop spreading the lockdep annotations outwards
1323ca0ff57SPeter Zijlstra  * except more code.
1333ca0ff57SPeter Zijlstra  */
1343ca0ff57SPeter Zijlstra 
1353ca0ff57SPeter Zijlstra /*
1363ca0ff57SPeter Zijlstra  * Optimistic trylock that only works in the uncontended case. Make sure to
1373ca0ff57SPeter Zijlstra  * follow with a __mutex_trylock() before failing.
1383ca0ff57SPeter Zijlstra  */
1393ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
1403ca0ff57SPeter Zijlstra {
1413ca0ff57SPeter Zijlstra 	unsigned long curr = (unsigned long)current;
142*c427f695SPeter Zijlstra 	unsigned long zero = 0UL;
1433ca0ff57SPeter Zijlstra 
144*c427f695SPeter Zijlstra 	if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
1453ca0ff57SPeter Zijlstra 		return true;
1463ca0ff57SPeter Zijlstra 
1473ca0ff57SPeter Zijlstra 	return false;
1483ca0ff57SPeter Zijlstra }
1493ca0ff57SPeter Zijlstra 
1503ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
1513ca0ff57SPeter Zijlstra {
1523ca0ff57SPeter Zijlstra 	unsigned long curr = (unsigned long)current;
1533ca0ff57SPeter Zijlstra 
1543ca0ff57SPeter Zijlstra 	if (atomic_long_cmpxchg_release(&lock->owner, curr, 0UL) == curr)
1553ca0ff57SPeter Zijlstra 		return true;
1563ca0ff57SPeter Zijlstra 
1573ca0ff57SPeter Zijlstra 	return false;
1583ca0ff57SPeter Zijlstra }
1593ca0ff57SPeter Zijlstra #endif
1603ca0ff57SPeter Zijlstra 
1613ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
1623ca0ff57SPeter Zijlstra {
1633ca0ff57SPeter Zijlstra 	atomic_long_or(flag, &lock->owner);
1643ca0ff57SPeter Zijlstra }
1653ca0ff57SPeter Zijlstra 
1663ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
1673ca0ff57SPeter Zijlstra {
1683ca0ff57SPeter Zijlstra 	atomic_long_andnot(flag, &lock->owner);
1693ca0ff57SPeter Zijlstra }
1703ca0ff57SPeter Zijlstra 
1719d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
1729d659ae1SPeter Zijlstra {
1739d659ae1SPeter Zijlstra 	return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
1749d659ae1SPeter Zijlstra }
1759d659ae1SPeter Zijlstra 
1769d659ae1SPeter Zijlstra /*
1779d659ae1SPeter Zijlstra  * Give up ownership to a specific task, when @task = NULL, this is equivalent
178e274795eSPeter Zijlstra  * to a regular unlock. Sets PICKUP on a handoff, clears HANDOF, preserves
179e274795eSPeter Zijlstra  * WAITERS. Provides RELEASE semantics like a regular unlock, the
180e274795eSPeter Zijlstra  * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
1819d659ae1SPeter Zijlstra  */
1829d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
1839d659ae1SPeter Zijlstra {
1849d659ae1SPeter Zijlstra 	unsigned long owner = atomic_long_read(&lock->owner);
1859d659ae1SPeter Zijlstra 
1869d659ae1SPeter Zijlstra 	for (;;) {
1879d659ae1SPeter Zijlstra 		unsigned long old, new;
1889d659ae1SPeter Zijlstra 
1899d659ae1SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
1909d659ae1SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
191e274795eSPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
1929d659ae1SPeter Zijlstra #endif
1939d659ae1SPeter Zijlstra 
1949d659ae1SPeter Zijlstra 		new = (owner & MUTEX_FLAG_WAITERS);
1959d659ae1SPeter Zijlstra 		new |= (unsigned long)task;
196e274795eSPeter Zijlstra 		if (task)
197e274795eSPeter Zijlstra 			new |= MUTEX_FLAG_PICKUP;
1989d659ae1SPeter Zijlstra 
1999d659ae1SPeter Zijlstra 		old = atomic_long_cmpxchg_release(&lock->owner, owner, new);
2009d659ae1SPeter Zijlstra 		if (old == owner)
2019d659ae1SPeter Zijlstra 			break;
2029d659ae1SPeter Zijlstra 
2039d659ae1SPeter Zijlstra 		owner = old;
2049d659ae1SPeter Zijlstra 	}
2059d659ae1SPeter Zijlstra }
2069d659ae1SPeter Zijlstra 
20701768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
20801768b42SPeter Zijlstra /*
20901768b42SPeter Zijlstra  * We split the mutex lock/unlock logic into separate fastpath and
21001768b42SPeter Zijlstra  * slowpath functions, to reduce the register pressure on the fastpath.
21101768b42SPeter Zijlstra  * We also put the fastpath first in the kernel image, to make sure the
21201768b42SPeter Zijlstra  * branch is predicted by the CPU as default-untaken.
21301768b42SPeter Zijlstra  */
2143ca0ff57SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock);
21501768b42SPeter Zijlstra 
21601768b42SPeter Zijlstra /**
21701768b42SPeter Zijlstra  * mutex_lock - acquire the mutex
21801768b42SPeter Zijlstra  * @lock: the mutex to be acquired
21901768b42SPeter Zijlstra  *
22001768b42SPeter Zijlstra  * Lock the mutex exclusively for this task. If the mutex is not
22101768b42SPeter Zijlstra  * available right now, it will sleep until it can get it.
22201768b42SPeter Zijlstra  *
22301768b42SPeter Zijlstra  * The mutex must later on be released by the same task that
22401768b42SPeter Zijlstra  * acquired it. Recursive locking is not allowed. The task
22501768b42SPeter Zijlstra  * may not exit without first unlocking the mutex. Also, kernel
226139b6fd2SSharon Dvir  * memory where the mutex resides must not be freed with
22701768b42SPeter Zijlstra  * the mutex still locked. The mutex must first be initialized
22801768b42SPeter Zijlstra  * (or statically defined) before it can be locked. memset()-ing
22901768b42SPeter Zijlstra  * the mutex to 0 is not allowed.
23001768b42SPeter Zijlstra  *
23101768b42SPeter Zijlstra  * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
23201768b42SPeter Zijlstra  * checks that will enforce the restrictions and will also do
2337b4ff1adSMauro Carvalho Chehab  * deadlock debugging)
23401768b42SPeter Zijlstra  *
23501768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down().
23601768b42SPeter Zijlstra  */
23701768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock)
23801768b42SPeter Zijlstra {
23901768b42SPeter Zijlstra 	might_sleep();
24001768b42SPeter Zijlstra 
2413ca0ff57SPeter Zijlstra 	if (!__mutex_trylock_fast(lock))
2423ca0ff57SPeter Zijlstra 		__mutex_lock_slowpath(lock);
2433ca0ff57SPeter Zijlstra }
24401768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock);
24501768b42SPeter Zijlstra #endif
24601768b42SPeter Zijlstra 
247427b1820SPeter Zijlstra static __always_inline void
248427b1820SPeter Zijlstra ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
24976916515SDavidlohr Bueso {
25076916515SDavidlohr Bueso #ifdef CONFIG_DEBUG_MUTEXES
25176916515SDavidlohr Bueso 	/*
25276916515SDavidlohr Bueso 	 * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
25376916515SDavidlohr Bueso 	 * but released with a normal mutex_unlock in this call.
25476916515SDavidlohr Bueso 	 *
25576916515SDavidlohr Bueso 	 * This should never happen, always use ww_mutex_unlock.
25676916515SDavidlohr Bueso 	 */
25776916515SDavidlohr Bueso 	DEBUG_LOCKS_WARN_ON(ww->ctx);
25876916515SDavidlohr Bueso 
25976916515SDavidlohr Bueso 	/*
26076916515SDavidlohr Bueso 	 * Not quite done after calling ww_acquire_done() ?
26176916515SDavidlohr Bueso 	 */
26276916515SDavidlohr Bueso 	DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
26376916515SDavidlohr Bueso 
26476916515SDavidlohr Bueso 	if (ww_ctx->contending_lock) {
26576916515SDavidlohr Bueso 		/*
26676916515SDavidlohr Bueso 		 * After -EDEADLK you tried to
26776916515SDavidlohr Bueso 		 * acquire a different ww_mutex? Bad!
26876916515SDavidlohr Bueso 		 */
26976916515SDavidlohr Bueso 		DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
27076916515SDavidlohr Bueso 
27176916515SDavidlohr Bueso 		/*
27276916515SDavidlohr Bueso 		 * You called ww_mutex_lock after receiving -EDEADLK,
27376916515SDavidlohr Bueso 		 * but 'forgot' to unlock everything else first?
27476916515SDavidlohr Bueso 		 */
27576916515SDavidlohr Bueso 		DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
27676916515SDavidlohr Bueso 		ww_ctx->contending_lock = NULL;
27776916515SDavidlohr Bueso 	}
27876916515SDavidlohr Bueso 
27976916515SDavidlohr Bueso 	/*
28076916515SDavidlohr Bueso 	 * Naughty, using a different class will lead to undefined behavior!
28176916515SDavidlohr Bueso 	 */
28276916515SDavidlohr Bueso 	DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
28376916515SDavidlohr Bueso #endif
28476916515SDavidlohr Bueso 	ww_ctx->acquired++;
28576916515SDavidlohr Bueso }
28676916515SDavidlohr Bueso 
2873822da3eSNicolai Hähnle static inline bool __sched
2883822da3eSNicolai Hähnle __ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
2893822da3eSNicolai Hähnle {
2903822da3eSNicolai Hähnle 	return a->stamp - b->stamp <= LONG_MAX &&
2913822da3eSNicolai Hähnle 	       (a->stamp != b->stamp || a > b);
2923822da3eSNicolai Hähnle }
2933822da3eSNicolai Hähnle 
29476916515SDavidlohr Bueso /*
295659cf9f5SNicolai Hähnle  * Wake up any waiters that may have to back off when the lock is held by the
296659cf9f5SNicolai Hähnle  * given context.
297659cf9f5SNicolai Hähnle  *
298659cf9f5SNicolai Hähnle  * Due to the invariants on the wait list, this can only affect the first
299659cf9f5SNicolai Hähnle  * waiter with a context.
300659cf9f5SNicolai Hähnle  *
301659cf9f5SNicolai Hähnle  * The current task must not be on the wait list.
302659cf9f5SNicolai Hähnle  */
303659cf9f5SNicolai Hähnle static void __sched
304659cf9f5SNicolai Hähnle __ww_mutex_wakeup_for_backoff(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
305659cf9f5SNicolai Hähnle {
306659cf9f5SNicolai Hähnle 	struct mutex_waiter *cur;
307659cf9f5SNicolai Hähnle 
308659cf9f5SNicolai Hähnle 	lockdep_assert_held(&lock->wait_lock);
309659cf9f5SNicolai Hähnle 
310659cf9f5SNicolai Hähnle 	list_for_each_entry(cur, &lock->wait_list, list) {
311659cf9f5SNicolai Hähnle 		if (!cur->ww_ctx)
312659cf9f5SNicolai Hähnle 			continue;
313659cf9f5SNicolai Hähnle 
314659cf9f5SNicolai Hähnle 		if (cur->ww_ctx->acquired > 0 &&
315659cf9f5SNicolai Hähnle 		    __ww_ctx_stamp_after(cur->ww_ctx, ww_ctx)) {
316659cf9f5SNicolai Hähnle 			debug_mutex_wake_waiter(lock, cur);
317659cf9f5SNicolai Hähnle 			wake_up_process(cur->task);
318659cf9f5SNicolai Hähnle 		}
319659cf9f5SNicolai Hähnle 
320659cf9f5SNicolai Hähnle 		break;
321659cf9f5SNicolai Hähnle 	}
322659cf9f5SNicolai Hähnle }
323659cf9f5SNicolai Hähnle 
32476916515SDavidlohr Bueso /*
3254bd19084SDavidlohr Bueso  * After acquiring lock with fastpath or when we lost out in contested
32676916515SDavidlohr Bueso  * slowpath, set ctx and wake up any waiters so they can recheck.
32776916515SDavidlohr Bueso  */
32876916515SDavidlohr Bueso static __always_inline void
329427b1820SPeter Zijlstra ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
33076916515SDavidlohr Bueso {
33176916515SDavidlohr Bueso 	ww_mutex_lock_acquired(lock, ctx);
33276916515SDavidlohr Bueso 
33376916515SDavidlohr Bueso 	lock->ctx = ctx;
33476916515SDavidlohr Bueso 
33576916515SDavidlohr Bueso 	/*
33676916515SDavidlohr Bueso 	 * The lock->ctx update should be visible on all cores before
33776916515SDavidlohr Bueso 	 * the atomic read is done, otherwise contended waiters might be
33876916515SDavidlohr Bueso 	 * missed. The contended waiters will either see ww_ctx == NULL
33976916515SDavidlohr Bueso 	 * and keep spinning, or it will acquire wait_lock, add itself
34076916515SDavidlohr Bueso 	 * to waiter list and sleep.
34176916515SDavidlohr Bueso 	 */
34276916515SDavidlohr Bueso 	smp_mb(); /* ^^^ */
34376916515SDavidlohr Bueso 
34476916515SDavidlohr Bueso 	/*
34576916515SDavidlohr Bueso 	 * Check if lock is contended, if not there is nobody to wake up
34676916515SDavidlohr Bueso 	 */
3473ca0ff57SPeter Zijlstra 	if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS)))
34876916515SDavidlohr Bueso 		return;
34976916515SDavidlohr Bueso 
35076916515SDavidlohr Bueso 	/*
35176916515SDavidlohr Bueso 	 * Uh oh, we raced in fastpath, wake up everyone in this case,
35276916515SDavidlohr Bueso 	 * so they can see the new lock->ctx.
35376916515SDavidlohr Bueso 	 */
354b9c16a0eSPeter Zijlstra 	spin_lock(&lock->base.wait_lock);
355659cf9f5SNicolai Hähnle 	__ww_mutex_wakeup_for_backoff(&lock->base, ctx);
356b9c16a0eSPeter Zijlstra 	spin_unlock(&lock->base.wait_lock);
35776916515SDavidlohr Bueso }
35876916515SDavidlohr Bueso 
3594bd19084SDavidlohr Bueso /*
360659cf9f5SNicolai Hähnle  * After acquiring lock in the slowpath set ctx.
361659cf9f5SNicolai Hähnle  *
362659cf9f5SNicolai Hähnle  * Unlike for the fast path, the caller ensures that waiters are woken up where
363659cf9f5SNicolai Hähnle  * necessary.
3644bd19084SDavidlohr Bueso  *
3654bd19084SDavidlohr Bueso  * Callers must hold the mutex wait_lock.
3664bd19084SDavidlohr Bueso  */
3674bd19084SDavidlohr Bueso static __always_inline void
368427b1820SPeter Zijlstra ww_mutex_set_context_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
3694bd19084SDavidlohr Bueso {
3704bd19084SDavidlohr Bueso 	ww_mutex_lock_acquired(lock, ctx);
3714bd19084SDavidlohr Bueso 	lock->ctx = ctx;
3724bd19084SDavidlohr Bueso }
37376916515SDavidlohr Bueso 
37401768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
375c516df97SNicolai Hähnle 
376c516df97SNicolai Hähnle static inline
377c516df97SNicolai Hähnle bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
378c516df97SNicolai Hähnle 			    struct mutex_waiter *waiter)
379c516df97SNicolai Hähnle {
380c516df97SNicolai Hähnle 	struct ww_mutex *ww;
381c516df97SNicolai Hähnle 
382c516df97SNicolai Hähnle 	ww = container_of(lock, struct ww_mutex, base);
383c516df97SNicolai Hähnle 
38401768b42SPeter Zijlstra 	/*
385c516df97SNicolai Hähnle 	 * If ww->ctx is set the contents are undefined, only
386c516df97SNicolai Hähnle 	 * by acquiring wait_lock there is a guarantee that
387c516df97SNicolai Hähnle 	 * they are not invalid when reading.
388c516df97SNicolai Hähnle 	 *
389c516df97SNicolai Hähnle 	 * As such, when deadlock detection needs to be
390c516df97SNicolai Hähnle 	 * performed the optimistic spinning cannot be done.
391c516df97SNicolai Hähnle 	 *
392c516df97SNicolai Hähnle 	 * Check this in every inner iteration because we may
393c516df97SNicolai Hähnle 	 * be racing against another thread's ww_mutex_lock.
394c516df97SNicolai Hähnle 	 */
395c516df97SNicolai Hähnle 	if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
396c516df97SNicolai Hähnle 		return false;
397c516df97SNicolai Hähnle 
398c516df97SNicolai Hähnle 	/*
399c516df97SNicolai Hähnle 	 * If we aren't on the wait list yet, cancel the spin
400c516df97SNicolai Hähnle 	 * if there are waiters. We want  to avoid stealing the
401c516df97SNicolai Hähnle 	 * lock from a waiter with an earlier stamp, since the
402c516df97SNicolai Hähnle 	 * other thread may already own a lock that we also
403c516df97SNicolai Hähnle 	 * need.
404c516df97SNicolai Hähnle 	 */
405c516df97SNicolai Hähnle 	if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
406c516df97SNicolai Hähnle 		return false;
407c516df97SNicolai Hähnle 
408c516df97SNicolai Hähnle 	/*
409c516df97SNicolai Hähnle 	 * Similarly, stop spinning if we are no longer the
410c516df97SNicolai Hähnle 	 * first waiter.
411c516df97SNicolai Hähnle 	 */
412c516df97SNicolai Hähnle 	if (waiter && !__mutex_waiter_is_first(lock, waiter))
413c516df97SNicolai Hähnle 		return false;
414c516df97SNicolai Hähnle 
415c516df97SNicolai Hähnle 	return true;
416c516df97SNicolai Hähnle }
417c516df97SNicolai Hähnle 
41801768b42SPeter Zijlstra /*
41925f13b40SNicolai Hähnle  * Look out! "owner" is an entirely speculative pointer access and not
42025f13b40SNicolai Hähnle  * reliable.
42125f13b40SNicolai Hähnle  *
42225f13b40SNicolai Hähnle  * "noinline" so that this function shows up on perf profiles.
42301768b42SPeter Zijlstra  */
42401768b42SPeter Zijlstra static noinline
42525f13b40SNicolai Hähnle bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
426c516df97SNicolai Hähnle 			 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
42701768b42SPeter Zijlstra {
42801ac33c1SJason Low 	bool ret = true;
429be1f7bf2SJason Low 
43001768b42SPeter Zijlstra 	rcu_read_lock();
4313ca0ff57SPeter Zijlstra 	while (__mutex_owner(lock) == owner) {
432be1f7bf2SJason Low 		/*
433be1f7bf2SJason Low 		 * Ensure we emit the owner->on_cpu, dereference _after_
43401ac33c1SJason Low 		 * checking lock->owner still matches owner. If that fails,
43501ac33c1SJason Low 		 * owner might point to freed memory. If it still matches,
436be1f7bf2SJason Low 		 * the rcu_read_lock() ensures the memory stays valid.
437be1f7bf2SJason Low 		 */
438be1f7bf2SJason Low 		barrier();
439be1f7bf2SJason Low 
44005ffc951SPan Xinhui 		/*
44105ffc951SPan Xinhui 		 * Use vcpu_is_preempted to detect lock holder preemption issue.
44205ffc951SPan Xinhui 		 */
44305ffc951SPan Xinhui 		if (!owner->on_cpu || need_resched() ||
44405ffc951SPan Xinhui 				vcpu_is_preempted(task_cpu(owner))) {
445be1f7bf2SJason Low 			ret = false;
446be1f7bf2SJason Low 			break;
447be1f7bf2SJason Low 		}
44801768b42SPeter Zijlstra 
449c516df97SNicolai Hähnle 		if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
45025f13b40SNicolai Hähnle 			ret = false;
45125f13b40SNicolai Hähnle 			break;
45225f13b40SNicolai Hähnle 		}
45325f13b40SNicolai Hähnle 
454f2f09a4cSChristian Borntraeger 		cpu_relax();
45501768b42SPeter Zijlstra 	}
45601768b42SPeter Zijlstra 	rcu_read_unlock();
45701768b42SPeter Zijlstra 
458be1f7bf2SJason Low 	return ret;
45901768b42SPeter Zijlstra }
46001768b42SPeter Zijlstra 
46101768b42SPeter Zijlstra /*
46201768b42SPeter Zijlstra  * Initial check for entering the mutex spinning loop
46301768b42SPeter Zijlstra  */
46401768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock)
46501768b42SPeter Zijlstra {
46601768b42SPeter Zijlstra 	struct task_struct *owner;
46701768b42SPeter Zijlstra 	int retval = 1;
46801768b42SPeter Zijlstra 
46946af29e4SJason Low 	if (need_resched())
47046af29e4SJason Low 		return 0;
47146af29e4SJason Low 
47201768b42SPeter Zijlstra 	rcu_read_lock();
4733ca0ff57SPeter Zijlstra 	owner = __mutex_owner(lock);
47405ffc951SPan Xinhui 
47505ffc951SPan Xinhui 	/*
47605ffc951SPan Xinhui 	 * As lock holder preemption issue, we both skip spinning if task is not
47705ffc951SPan Xinhui 	 * on cpu or its cpu is preempted
47805ffc951SPan Xinhui 	 */
47901768b42SPeter Zijlstra 	if (owner)
48005ffc951SPan Xinhui 		retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
48101768b42SPeter Zijlstra 	rcu_read_unlock();
48276916515SDavidlohr Bueso 
48376916515SDavidlohr Bueso 	/*
4843ca0ff57SPeter Zijlstra 	 * If lock->owner is not set, the mutex has been released. Return true
4853ca0ff57SPeter Zijlstra 	 * such that we'll trylock in the spin path, which is a faster option
4863ca0ff57SPeter Zijlstra 	 * than the blocking slow path.
48776916515SDavidlohr Bueso 	 */
4883ca0ff57SPeter Zijlstra 	return retval;
48976916515SDavidlohr Bueso }
49076916515SDavidlohr Bueso 
49176916515SDavidlohr Bueso /*
49276916515SDavidlohr Bueso  * Optimistic spinning.
49376916515SDavidlohr Bueso  *
49476916515SDavidlohr Bueso  * We try to spin for acquisition when we find that the lock owner
49576916515SDavidlohr Bueso  * is currently running on a (different) CPU and while we don't
49676916515SDavidlohr Bueso  * need to reschedule. The rationale is that if the lock owner is
49776916515SDavidlohr Bueso  * running, it is likely to release the lock soon.
49876916515SDavidlohr Bueso  *
49976916515SDavidlohr Bueso  * The mutex spinners are queued up using MCS lock so that only one
50076916515SDavidlohr Bueso  * spinner can compete for the mutex. However, if mutex spinning isn't
50176916515SDavidlohr Bueso  * going to happen, there is no point in going through the lock/unlock
50276916515SDavidlohr Bueso  * overhead.
50376916515SDavidlohr Bueso  *
50476916515SDavidlohr Bueso  * Returns true when the lock was taken, otherwise false, indicating
50576916515SDavidlohr Bueso  * that we need to jump to the slowpath and sleep.
506b341afb3SWaiman Long  *
507b341afb3SWaiman Long  * The waiter flag is set to true if the spinner is a waiter in the wait
508b341afb3SWaiman Long  * queue. The waiter-spinner will spin on the lock directly and concurrently
509b341afb3SWaiman Long  * with the spinner at the head of the OSQ, if present, until the owner is
510b341afb3SWaiman Long  * changed to itself.
51176916515SDavidlohr Bueso  */
512427b1820SPeter Zijlstra static __always_inline bool
513427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
514c516df97SNicolai Hähnle 		      const bool use_ww_ctx, struct mutex_waiter *waiter)
51576916515SDavidlohr Bueso {
516b341afb3SWaiman Long 	if (!waiter) {
517b341afb3SWaiman Long 		/*
518b341afb3SWaiman Long 		 * The purpose of the mutex_can_spin_on_owner() function is
519b341afb3SWaiman Long 		 * to eliminate the overhead of osq_lock() and osq_unlock()
520b341afb3SWaiman Long 		 * in case spinning isn't possible. As a waiter-spinner
521b341afb3SWaiman Long 		 * is not going to take OSQ lock anyway, there is no need
522b341afb3SWaiman Long 		 * to call mutex_can_spin_on_owner().
523b341afb3SWaiman Long 		 */
52476916515SDavidlohr Bueso 		if (!mutex_can_spin_on_owner(lock))
525b341afb3SWaiman Long 			goto fail;
52676916515SDavidlohr Bueso 
527e42f678aSDavidlohr Bueso 		/*
528e42f678aSDavidlohr Bueso 		 * In order to avoid a stampede of mutex spinners trying to
529e42f678aSDavidlohr Bueso 		 * acquire the mutex all at once, the spinners need to take a
530e42f678aSDavidlohr Bueso 		 * MCS (queued) lock first before spinning on the owner field.
531e42f678aSDavidlohr Bueso 		 */
53276916515SDavidlohr Bueso 		if (!osq_lock(&lock->osq))
533b341afb3SWaiman Long 			goto fail;
534b341afb3SWaiman Long 	}
53576916515SDavidlohr Bueso 
536b341afb3SWaiman Long 	for (;;) {
53776916515SDavidlohr Bueso 		struct task_struct *owner;
53876916515SDavidlohr Bueso 
539e274795eSPeter Zijlstra 		/* Try to acquire the mutex... */
540e274795eSPeter Zijlstra 		owner = __mutex_trylock_or_owner(lock);
541e274795eSPeter Zijlstra 		if (!owner)
542e274795eSPeter Zijlstra 			break;
54376916515SDavidlohr Bueso 
54476916515SDavidlohr Bueso 		/*
545e274795eSPeter Zijlstra 		 * There's an owner, wait for it to either
54676916515SDavidlohr Bueso 		 * release the lock or go to sleep.
54776916515SDavidlohr Bueso 		 */
548c516df97SNicolai Hähnle 		if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
549b341afb3SWaiman Long 			goto fail_unlock;
55076916515SDavidlohr Bueso 
55176916515SDavidlohr Bueso 		/*
55276916515SDavidlohr Bueso 		 * The cpu_relax() call is a compiler barrier which forces
55376916515SDavidlohr Bueso 		 * everything in this loop to be re-loaded. We don't need
55476916515SDavidlohr Bueso 		 * memory barriers as we'll eventually observe the right
55576916515SDavidlohr Bueso 		 * values at the cost of a few extra spins.
55676916515SDavidlohr Bueso 		 */
557f2f09a4cSChristian Borntraeger 		cpu_relax();
55876916515SDavidlohr Bueso 	}
55976916515SDavidlohr Bueso 
560b341afb3SWaiman Long 	if (!waiter)
56176916515SDavidlohr Bueso 		osq_unlock(&lock->osq);
562b341afb3SWaiman Long 
563b341afb3SWaiman Long 	return true;
564b341afb3SWaiman Long 
565b341afb3SWaiman Long 
566b341afb3SWaiman Long fail_unlock:
567b341afb3SWaiman Long 	if (!waiter)
568b341afb3SWaiman Long 		osq_unlock(&lock->osq);
569b341afb3SWaiman Long 
570b341afb3SWaiman Long fail:
57176916515SDavidlohr Bueso 	/*
57276916515SDavidlohr Bueso 	 * If we fell out of the spin path because of need_resched(),
57376916515SDavidlohr Bueso 	 * reschedule now, before we try-lock the mutex. This avoids getting
57476916515SDavidlohr Bueso 	 * scheduled out right after we obtained the mutex.
57576916515SDavidlohr Bueso 	 */
5766f942a1fSPeter Zijlstra 	if (need_resched()) {
5776f942a1fSPeter Zijlstra 		/*
5786f942a1fSPeter Zijlstra 		 * We _should_ have TASK_RUNNING here, but just in case
5796f942a1fSPeter Zijlstra 		 * we do not, make it so, otherwise we might get stuck.
5806f942a1fSPeter Zijlstra 		 */
5816f942a1fSPeter Zijlstra 		__set_current_state(TASK_RUNNING);
58276916515SDavidlohr Bueso 		schedule_preempt_disabled();
5836f942a1fSPeter Zijlstra 	}
58476916515SDavidlohr Bueso 
58576916515SDavidlohr Bueso 	return false;
58676916515SDavidlohr Bueso }
58776916515SDavidlohr Bueso #else
588427b1820SPeter Zijlstra static __always_inline bool
589427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
590c516df97SNicolai Hähnle 		      const bool use_ww_ctx, struct mutex_waiter *waiter)
59176916515SDavidlohr Bueso {
59276916515SDavidlohr Bueso 	return false;
59376916515SDavidlohr Bueso }
59401768b42SPeter Zijlstra #endif
59501768b42SPeter Zijlstra 
5963ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
59701768b42SPeter Zijlstra 
59801768b42SPeter Zijlstra /**
59901768b42SPeter Zijlstra  * mutex_unlock - release the mutex
60001768b42SPeter Zijlstra  * @lock: the mutex to be released
60101768b42SPeter Zijlstra  *
60201768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously.
60301768b42SPeter Zijlstra  *
60401768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
60501768b42SPeter Zijlstra  * of a not locked mutex is not allowed.
60601768b42SPeter Zijlstra  *
60701768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) up().
60801768b42SPeter Zijlstra  */
60901768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock)
61001768b42SPeter Zijlstra {
6113ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
6123ca0ff57SPeter Zijlstra 	if (__mutex_unlock_fast(lock))
6133ca0ff57SPeter Zijlstra 		return;
61401768b42SPeter Zijlstra #endif
6153ca0ff57SPeter Zijlstra 	__mutex_unlock_slowpath(lock, _RET_IP_);
61601768b42SPeter Zijlstra }
61701768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock);
61801768b42SPeter Zijlstra 
61901768b42SPeter Zijlstra /**
62001768b42SPeter Zijlstra  * ww_mutex_unlock - release the w/w mutex
62101768b42SPeter Zijlstra  * @lock: the mutex to be released
62201768b42SPeter Zijlstra  *
62301768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously with any of the
62401768b42SPeter Zijlstra  * ww_mutex_lock* functions (with or without an acquire context). It is
62501768b42SPeter Zijlstra  * forbidden to release the locks after releasing the acquire context.
62601768b42SPeter Zijlstra  *
62701768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
62801768b42SPeter Zijlstra  * of a unlocked mutex is not allowed.
62901768b42SPeter Zijlstra  */
63001768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock)
63101768b42SPeter Zijlstra {
63201768b42SPeter Zijlstra 	/*
63301768b42SPeter Zijlstra 	 * The unlocking fastpath is the 0->1 transition from 'locked'
63401768b42SPeter Zijlstra 	 * into 'unlocked' state:
63501768b42SPeter Zijlstra 	 */
63601768b42SPeter Zijlstra 	if (lock->ctx) {
63701768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
63801768b42SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
63901768b42SPeter Zijlstra #endif
64001768b42SPeter Zijlstra 		if (lock->ctx->acquired > 0)
64101768b42SPeter Zijlstra 			lock->ctx->acquired--;
64201768b42SPeter Zijlstra 		lock->ctx = NULL;
64301768b42SPeter Zijlstra 	}
64401768b42SPeter Zijlstra 
6453ca0ff57SPeter Zijlstra 	mutex_unlock(&lock->base);
64601768b42SPeter Zijlstra }
64701768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock);
64801768b42SPeter Zijlstra 
64901768b42SPeter Zijlstra static inline int __sched
650200b1874SNicolai Hähnle __ww_mutex_lock_check_stamp(struct mutex *lock, struct mutex_waiter *waiter,
651200b1874SNicolai Hähnle 			    struct ww_acquire_ctx *ctx)
65201768b42SPeter Zijlstra {
65301768b42SPeter Zijlstra 	struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
6544d3199e4SDavidlohr Bueso 	struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
655200b1874SNicolai Hähnle 	struct mutex_waiter *cur;
65601768b42SPeter Zijlstra 
657200b1874SNicolai Hähnle 	if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx))
658200b1874SNicolai Hähnle 		goto deadlock;
659200b1874SNicolai Hähnle 
660200b1874SNicolai Hähnle 	/*
661200b1874SNicolai Hähnle 	 * If there is a waiter in front of us that has a context, then its
662200b1874SNicolai Hähnle 	 * stamp is earlier than ours and we must back off.
663200b1874SNicolai Hähnle 	 */
664200b1874SNicolai Hähnle 	cur = waiter;
665200b1874SNicolai Hähnle 	list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) {
666200b1874SNicolai Hähnle 		if (cur->ww_ctx)
667200b1874SNicolai Hähnle 			goto deadlock;
668200b1874SNicolai Hähnle 	}
669200b1874SNicolai Hähnle 
67001768b42SPeter Zijlstra 	return 0;
67101768b42SPeter Zijlstra 
672200b1874SNicolai Hähnle deadlock:
67301768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
67401768b42SPeter Zijlstra 	DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
67501768b42SPeter Zijlstra 	ctx->contending_lock = ww;
67601768b42SPeter Zijlstra #endif
67701768b42SPeter Zijlstra 	return -EDEADLK;
67801768b42SPeter Zijlstra }
67901768b42SPeter Zijlstra 
6806baa5c60SNicolai Hähnle static inline int __sched
6816baa5c60SNicolai Hähnle __ww_mutex_add_waiter(struct mutex_waiter *waiter,
6826baa5c60SNicolai Hähnle 		      struct mutex *lock,
6836baa5c60SNicolai Hähnle 		      struct ww_acquire_ctx *ww_ctx)
6846baa5c60SNicolai Hähnle {
6856baa5c60SNicolai Hähnle 	struct mutex_waiter *cur;
6866baa5c60SNicolai Hähnle 	struct list_head *pos;
6876baa5c60SNicolai Hähnle 
6886baa5c60SNicolai Hähnle 	if (!ww_ctx) {
6896baa5c60SNicolai Hähnle 		list_add_tail(&waiter->list, &lock->wait_list);
6906baa5c60SNicolai Hähnle 		return 0;
6916baa5c60SNicolai Hähnle 	}
6926baa5c60SNicolai Hähnle 
6936baa5c60SNicolai Hähnle 	/*
6946baa5c60SNicolai Hähnle 	 * Add the waiter before the first waiter with a higher stamp.
6956baa5c60SNicolai Hähnle 	 * Waiters without a context are skipped to avoid starving
6966baa5c60SNicolai Hähnle 	 * them.
6976baa5c60SNicolai Hähnle 	 */
6986baa5c60SNicolai Hähnle 	pos = &lock->wait_list;
6996baa5c60SNicolai Hähnle 	list_for_each_entry_reverse(cur, &lock->wait_list, list) {
7006baa5c60SNicolai Hähnle 		if (!cur->ww_ctx)
7016baa5c60SNicolai Hähnle 			continue;
7026baa5c60SNicolai Hähnle 
7036baa5c60SNicolai Hähnle 		if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) {
7046baa5c60SNicolai Hähnle 			/* Back off immediately if necessary. */
7056baa5c60SNicolai Hähnle 			if (ww_ctx->acquired > 0) {
7066baa5c60SNicolai Hähnle #ifdef CONFIG_DEBUG_MUTEXES
7076baa5c60SNicolai Hähnle 				struct ww_mutex *ww;
7086baa5c60SNicolai Hähnle 
7096baa5c60SNicolai Hähnle 				ww = container_of(lock, struct ww_mutex, base);
7106baa5c60SNicolai Hähnle 				DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock);
7116baa5c60SNicolai Hähnle 				ww_ctx->contending_lock = ww;
7126baa5c60SNicolai Hähnle #endif
7136baa5c60SNicolai Hähnle 				return -EDEADLK;
7146baa5c60SNicolai Hähnle 			}
7156baa5c60SNicolai Hähnle 
7166baa5c60SNicolai Hähnle 			break;
7176baa5c60SNicolai Hähnle 		}
7186baa5c60SNicolai Hähnle 
7196baa5c60SNicolai Hähnle 		pos = &cur->list;
720200b1874SNicolai Hähnle 
721200b1874SNicolai Hähnle 		/*
722200b1874SNicolai Hähnle 		 * Wake up the waiter so that it gets a chance to back
723200b1874SNicolai Hähnle 		 * off.
724200b1874SNicolai Hähnle 		 */
725200b1874SNicolai Hähnle 		if (cur->ww_ctx->acquired > 0) {
726200b1874SNicolai Hähnle 			debug_mutex_wake_waiter(lock, cur);
727200b1874SNicolai Hähnle 			wake_up_process(cur->task);
728200b1874SNicolai Hähnle 		}
7296baa5c60SNicolai Hähnle 	}
7306baa5c60SNicolai Hähnle 
7316baa5c60SNicolai Hähnle 	list_add_tail(&waiter->list, pos);
73201768b42SPeter Zijlstra 	return 0;
73301768b42SPeter Zijlstra }
73401768b42SPeter Zijlstra 
73501768b42SPeter Zijlstra /*
73601768b42SPeter Zijlstra  * Lock a mutex (possibly interruptible), slowpath:
73701768b42SPeter Zijlstra  */
73801768b42SPeter Zijlstra static __always_inline int __sched
73901768b42SPeter Zijlstra __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
74001768b42SPeter Zijlstra 		    struct lockdep_map *nest_lock, unsigned long ip,
74101768b42SPeter Zijlstra 		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
74201768b42SPeter Zijlstra {
74301768b42SPeter Zijlstra 	struct mutex_waiter waiter;
7449d659ae1SPeter Zijlstra 	bool first = false;
745a40ca565SWaiman Long 	struct ww_mutex *ww;
74601768b42SPeter Zijlstra 	int ret;
74701768b42SPeter Zijlstra 
748427b1820SPeter Zijlstra 	might_sleep();
749ea9e0fb8SNicolai Hähnle 
750a40ca565SWaiman Long 	ww = container_of(lock, struct ww_mutex, base);
751ea9e0fb8SNicolai Hähnle 	if (use_ww_ctx && ww_ctx) {
7520422e83dSChris Wilson 		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
7530422e83dSChris Wilson 			return -EALREADY;
7540422e83dSChris Wilson 	}
7550422e83dSChris Wilson 
75601768b42SPeter Zijlstra 	preempt_disable();
75701768b42SPeter Zijlstra 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
75801768b42SPeter Zijlstra 
759e274795eSPeter Zijlstra 	if (__mutex_trylock(lock) ||
760c516df97SNicolai Hähnle 	    mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, NULL)) {
76176916515SDavidlohr Bueso 		/* got the lock, yay! */
7623ca0ff57SPeter Zijlstra 		lock_acquired(&lock->dep_map, ip);
763ea9e0fb8SNicolai Hähnle 		if (use_ww_ctx && ww_ctx)
7643ca0ff57SPeter Zijlstra 			ww_mutex_set_context_fastpath(ww, ww_ctx);
76501768b42SPeter Zijlstra 		preempt_enable();
76601768b42SPeter Zijlstra 		return 0;
76701768b42SPeter Zijlstra 	}
76801768b42SPeter Zijlstra 
769b9c16a0eSPeter Zijlstra 	spin_lock(&lock->wait_lock);
7701e820c96SJason Low 	/*
7713ca0ff57SPeter Zijlstra 	 * After waiting to acquire the wait_lock, try again.
7721e820c96SJason Low 	 */
773659cf9f5SNicolai Hähnle 	if (__mutex_trylock(lock)) {
774659cf9f5SNicolai Hähnle 		if (use_ww_ctx && ww_ctx)
775659cf9f5SNicolai Hähnle 			__ww_mutex_wakeup_for_backoff(lock, ww_ctx);
776659cf9f5SNicolai Hähnle 
77701768b42SPeter Zijlstra 		goto skip_wait;
778659cf9f5SNicolai Hähnle 	}
77901768b42SPeter Zijlstra 
78001768b42SPeter Zijlstra 	debug_mutex_lock_common(lock, &waiter);
781d269a8b8SDavidlohr Bueso 	debug_mutex_add_waiter(lock, &waiter, current);
78201768b42SPeter Zijlstra 
7836baa5c60SNicolai Hähnle 	lock_contended(&lock->dep_map, ip);
7846baa5c60SNicolai Hähnle 
7856baa5c60SNicolai Hähnle 	if (!use_ww_ctx) {
78601768b42SPeter Zijlstra 		/* add waiting tasks to the end of the waitqueue (FIFO): */
78701768b42SPeter Zijlstra 		list_add_tail(&waiter.list, &lock->wait_list);
788977625a6SNicolai Hähnle 
789977625a6SNicolai Hähnle #ifdef CONFIG_DEBUG_MUTEXES
790977625a6SNicolai Hähnle 		waiter.ww_ctx = MUTEX_POISON_WW_CTX;
791977625a6SNicolai Hähnle #endif
7926baa5c60SNicolai Hähnle 	} else {
7936baa5c60SNicolai Hähnle 		/* Add in stamp order, waking up waiters that must back off. */
7946baa5c60SNicolai Hähnle 		ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
7956baa5c60SNicolai Hähnle 		if (ret)
7966baa5c60SNicolai Hähnle 			goto err_early_backoff;
7976baa5c60SNicolai Hähnle 
7986baa5c60SNicolai Hähnle 		waiter.ww_ctx = ww_ctx;
7996baa5c60SNicolai Hähnle 	}
8006baa5c60SNicolai Hähnle 
801d269a8b8SDavidlohr Bueso 	waiter.task = current;
80201768b42SPeter Zijlstra 
8039d659ae1SPeter Zijlstra 	if (__mutex_waiter_is_first(lock, &waiter))
8043ca0ff57SPeter Zijlstra 		__mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
8053ca0ff57SPeter Zijlstra 
806642fa448SDavidlohr Bueso 	set_current_state(state);
80701768b42SPeter Zijlstra 	for (;;) {
8085bbd7e64SPeter Zijlstra 		/*
8095bbd7e64SPeter Zijlstra 		 * Once we hold wait_lock, we're serialized against
8105bbd7e64SPeter Zijlstra 		 * mutex_unlock() handing the lock off to us, do a trylock
8115bbd7e64SPeter Zijlstra 		 * before testing the error conditions to make sure we pick up
8125bbd7e64SPeter Zijlstra 		 * the handoff.
8135bbd7e64SPeter Zijlstra 		 */
814e274795eSPeter Zijlstra 		if (__mutex_trylock(lock))
8155bbd7e64SPeter Zijlstra 			goto acquired;
81601768b42SPeter Zijlstra 
81701768b42SPeter Zijlstra 		/*
8185bbd7e64SPeter Zijlstra 		 * Check for signals and wound conditions while holding
8195bbd7e64SPeter Zijlstra 		 * wait_lock. This ensures the lock cancellation is ordered
8205bbd7e64SPeter Zijlstra 		 * against mutex_unlock() and wake-ups do not go missing.
82101768b42SPeter Zijlstra 		 */
822d269a8b8SDavidlohr Bueso 		if (unlikely(signal_pending_state(state, current))) {
82301768b42SPeter Zijlstra 			ret = -EINTR;
82401768b42SPeter Zijlstra 			goto err;
82501768b42SPeter Zijlstra 		}
82601768b42SPeter Zijlstra 
827ea9e0fb8SNicolai Hähnle 		if (use_ww_ctx && ww_ctx && ww_ctx->acquired > 0) {
828200b1874SNicolai Hähnle 			ret = __ww_mutex_lock_check_stamp(lock, &waiter, ww_ctx);
82901768b42SPeter Zijlstra 			if (ret)
83001768b42SPeter Zijlstra 				goto err;
83101768b42SPeter Zijlstra 		}
83201768b42SPeter Zijlstra 
833b9c16a0eSPeter Zijlstra 		spin_unlock(&lock->wait_lock);
83401768b42SPeter Zijlstra 		schedule_preempt_disabled();
8359d659ae1SPeter Zijlstra 
8366baa5c60SNicolai Hähnle 		/*
8376baa5c60SNicolai Hähnle 		 * ww_mutex needs to always recheck its position since its waiter
8386baa5c60SNicolai Hähnle 		 * list is not FIFO ordered.
8396baa5c60SNicolai Hähnle 		 */
8406baa5c60SNicolai Hähnle 		if ((use_ww_ctx && ww_ctx) || !first) {
8416baa5c60SNicolai Hähnle 			first = __mutex_waiter_is_first(lock, &waiter);
8426baa5c60SNicolai Hähnle 			if (first)
8439d659ae1SPeter Zijlstra 				__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
8449d659ae1SPeter Zijlstra 		}
8455bbd7e64SPeter Zijlstra 
846642fa448SDavidlohr Bueso 		set_current_state(state);
8475bbd7e64SPeter Zijlstra 		/*
8485bbd7e64SPeter Zijlstra 		 * Here we order against unlock; we must either see it change
8495bbd7e64SPeter Zijlstra 		 * state back to RUNNING and fall through the next schedule(),
8505bbd7e64SPeter Zijlstra 		 * or we must see its unlock and acquire.
8515bbd7e64SPeter Zijlstra 		 */
852e274795eSPeter Zijlstra 		if (__mutex_trylock(lock) ||
853c516df97SNicolai Hähnle 		    (first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, &waiter)))
8545bbd7e64SPeter Zijlstra 			break;
8555bbd7e64SPeter Zijlstra 
856b9c16a0eSPeter Zijlstra 		spin_lock(&lock->wait_lock);
85701768b42SPeter Zijlstra 	}
858b9c16a0eSPeter Zijlstra 	spin_lock(&lock->wait_lock);
8595bbd7e64SPeter Zijlstra acquired:
860642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
86151587bcfSDavidlohr Bueso 
862d269a8b8SDavidlohr Bueso 	mutex_remove_waiter(lock, &waiter, current);
86301768b42SPeter Zijlstra 	if (likely(list_empty(&lock->wait_list)))
8649d659ae1SPeter Zijlstra 		__mutex_clear_flag(lock, MUTEX_FLAGS);
8653ca0ff57SPeter Zijlstra 
86601768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
86701768b42SPeter Zijlstra 
86801768b42SPeter Zijlstra skip_wait:
86901768b42SPeter Zijlstra 	/* got the lock - cleanup and rejoice! */
87001768b42SPeter Zijlstra 	lock_acquired(&lock->dep_map, ip);
87101768b42SPeter Zijlstra 
872ea9e0fb8SNicolai Hähnle 	if (use_ww_ctx && ww_ctx)
8734bd19084SDavidlohr Bueso 		ww_mutex_set_context_slowpath(ww, ww_ctx);
87401768b42SPeter Zijlstra 
875b9c16a0eSPeter Zijlstra 	spin_unlock(&lock->wait_lock);
87601768b42SPeter Zijlstra 	preempt_enable();
87701768b42SPeter Zijlstra 	return 0;
87801768b42SPeter Zijlstra 
87901768b42SPeter Zijlstra err:
880642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
881d269a8b8SDavidlohr Bueso 	mutex_remove_waiter(lock, &waiter, current);
8826baa5c60SNicolai Hähnle err_early_backoff:
883b9c16a0eSPeter Zijlstra 	spin_unlock(&lock->wait_lock);
88401768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
88501768b42SPeter Zijlstra 	mutex_release(&lock->dep_map, 1, ip);
88601768b42SPeter Zijlstra 	preempt_enable();
88701768b42SPeter Zijlstra 	return ret;
88801768b42SPeter Zijlstra }
88901768b42SPeter Zijlstra 
890427b1820SPeter Zijlstra static int __sched
891427b1820SPeter Zijlstra __mutex_lock(struct mutex *lock, long state, unsigned int subclass,
892427b1820SPeter Zijlstra 	     struct lockdep_map *nest_lock, unsigned long ip)
893427b1820SPeter Zijlstra {
894427b1820SPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
895427b1820SPeter Zijlstra }
896427b1820SPeter Zijlstra 
897427b1820SPeter Zijlstra static int __sched
898427b1820SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, long state, unsigned int subclass,
899427b1820SPeter Zijlstra 		struct lockdep_map *nest_lock, unsigned long ip,
900427b1820SPeter Zijlstra 		struct ww_acquire_ctx *ww_ctx)
901427b1820SPeter Zijlstra {
902427b1820SPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, ww_ctx, true);
903427b1820SPeter Zijlstra }
904427b1820SPeter Zijlstra 
90501768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
90601768b42SPeter Zijlstra void __sched
90701768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass)
90801768b42SPeter Zijlstra {
909427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
91001768b42SPeter Zijlstra }
91101768b42SPeter Zijlstra 
91201768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested);
91301768b42SPeter Zijlstra 
91401768b42SPeter Zijlstra void __sched
91501768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
91601768b42SPeter Zijlstra {
917427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
91801768b42SPeter Zijlstra }
91901768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
92001768b42SPeter Zijlstra 
92101768b42SPeter Zijlstra int __sched
92201768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
92301768b42SPeter Zijlstra {
924427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
92501768b42SPeter Zijlstra }
92601768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
92701768b42SPeter Zijlstra 
92801768b42SPeter Zijlstra int __sched
92901768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
93001768b42SPeter Zijlstra {
931427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
93201768b42SPeter Zijlstra }
93301768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
93401768b42SPeter Zijlstra 
9351460cb65STejun Heo void __sched
9361460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
9371460cb65STejun Heo {
9381460cb65STejun Heo 	int token;
9391460cb65STejun Heo 
9401460cb65STejun Heo 	might_sleep();
9411460cb65STejun Heo 
9421460cb65STejun Heo 	token = io_schedule_prepare();
9431460cb65STejun Heo 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
9441460cb65STejun Heo 			    subclass, NULL, _RET_IP_, NULL, 0);
9451460cb65STejun Heo 	io_schedule_finish(token);
9461460cb65STejun Heo }
9471460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
9481460cb65STejun Heo 
94901768b42SPeter Zijlstra static inline int
95001768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
95101768b42SPeter Zijlstra {
95201768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
95301768b42SPeter Zijlstra 	unsigned tmp;
95401768b42SPeter Zijlstra 
95501768b42SPeter Zijlstra 	if (ctx->deadlock_inject_countdown-- == 0) {
95601768b42SPeter Zijlstra 		tmp = ctx->deadlock_inject_interval;
95701768b42SPeter Zijlstra 		if (tmp > UINT_MAX/4)
95801768b42SPeter Zijlstra 			tmp = UINT_MAX;
95901768b42SPeter Zijlstra 		else
96001768b42SPeter Zijlstra 			tmp = tmp*2 + tmp + tmp/2;
96101768b42SPeter Zijlstra 
96201768b42SPeter Zijlstra 		ctx->deadlock_inject_interval = tmp;
96301768b42SPeter Zijlstra 		ctx->deadlock_inject_countdown = tmp;
96401768b42SPeter Zijlstra 		ctx->contending_lock = lock;
96501768b42SPeter Zijlstra 
96601768b42SPeter Zijlstra 		ww_mutex_unlock(lock);
96701768b42SPeter Zijlstra 
96801768b42SPeter Zijlstra 		return -EDEADLK;
96901768b42SPeter Zijlstra 	}
97001768b42SPeter Zijlstra #endif
97101768b42SPeter Zijlstra 
97201768b42SPeter Zijlstra 	return 0;
97301768b42SPeter Zijlstra }
97401768b42SPeter Zijlstra 
97501768b42SPeter Zijlstra int __sched
976c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
97701768b42SPeter Zijlstra {
97801768b42SPeter Zijlstra 	int ret;
97901768b42SPeter Zijlstra 
98001768b42SPeter Zijlstra 	might_sleep();
981427b1820SPeter Zijlstra 	ret =  __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
982ea9e0fb8SNicolai Hähnle 			       0, ctx ? &ctx->dep_map : NULL, _RET_IP_,
983427b1820SPeter Zijlstra 			       ctx);
984ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
98501768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
98601768b42SPeter Zijlstra 
98701768b42SPeter Zijlstra 	return ret;
98801768b42SPeter Zijlstra }
989c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock);
99001768b42SPeter Zijlstra 
99101768b42SPeter Zijlstra int __sched
992c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
99301768b42SPeter Zijlstra {
99401768b42SPeter Zijlstra 	int ret;
99501768b42SPeter Zijlstra 
99601768b42SPeter Zijlstra 	might_sleep();
997427b1820SPeter Zijlstra 	ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
998ea9e0fb8SNicolai Hähnle 			      0, ctx ? &ctx->dep_map : NULL, _RET_IP_,
999427b1820SPeter Zijlstra 			      ctx);
100001768b42SPeter Zijlstra 
1001ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
100201768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
100301768b42SPeter Zijlstra 
100401768b42SPeter Zijlstra 	return ret;
100501768b42SPeter Zijlstra }
1006c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
100701768b42SPeter Zijlstra 
100801768b42SPeter Zijlstra #endif
100901768b42SPeter Zijlstra 
101001768b42SPeter Zijlstra /*
101101768b42SPeter Zijlstra  * Release the lock, slowpath:
101201768b42SPeter Zijlstra  */
10133ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
101401768b42SPeter Zijlstra {
10159d659ae1SPeter Zijlstra 	struct task_struct *next = NULL;
1016194a6b5bSWaiman Long 	DEFINE_WAKE_Q(wake_q);
1017b9c16a0eSPeter Zijlstra 	unsigned long owner;
101801768b42SPeter Zijlstra 
10193ca0ff57SPeter Zijlstra 	mutex_release(&lock->dep_map, 1, ip);
10203ca0ff57SPeter Zijlstra 
102101768b42SPeter Zijlstra 	/*
10229d659ae1SPeter Zijlstra 	 * Release the lock before (potentially) taking the spinlock such that
10239d659ae1SPeter Zijlstra 	 * other contenders can get on with things ASAP.
10249d659ae1SPeter Zijlstra 	 *
10259d659ae1SPeter Zijlstra 	 * Except when HANDOFF, in that case we must not clear the owner field,
10269d659ae1SPeter Zijlstra 	 * but instead set it to the top waiter.
102701768b42SPeter Zijlstra 	 */
10289d659ae1SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
10299d659ae1SPeter Zijlstra 	for (;;) {
10309d659ae1SPeter Zijlstra 		unsigned long old;
10319d659ae1SPeter Zijlstra 
10329d659ae1SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
10339d659ae1SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
1034e274795eSPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
10359d659ae1SPeter Zijlstra #endif
10369d659ae1SPeter Zijlstra 
10379d659ae1SPeter Zijlstra 		if (owner & MUTEX_FLAG_HANDOFF)
10389d659ae1SPeter Zijlstra 			break;
10399d659ae1SPeter Zijlstra 
10409d659ae1SPeter Zijlstra 		old = atomic_long_cmpxchg_release(&lock->owner, owner,
10419d659ae1SPeter Zijlstra 						  __owner_flags(owner));
10429d659ae1SPeter Zijlstra 		if (old == owner) {
10439d659ae1SPeter Zijlstra 			if (owner & MUTEX_FLAG_WAITERS)
10449d659ae1SPeter Zijlstra 				break;
10459d659ae1SPeter Zijlstra 
10463ca0ff57SPeter Zijlstra 			return;
10479d659ae1SPeter Zijlstra 		}
10489d659ae1SPeter Zijlstra 
10499d659ae1SPeter Zijlstra 		owner = old;
10509d659ae1SPeter Zijlstra 	}
105101768b42SPeter Zijlstra 
1052b9c16a0eSPeter Zijlstra 	spin_lock(&lock->wait_lock);
10531d8fe7dcSJason Low 	debug_mutex_unlock(lock);
105401768b42SPeter Zijlstra 	if (!list_empty(&lock->wait_list)) {
105501768b42SPeter Zijlstra 		/* get the first entry from the wait-list: */
105601768b42SPeter Zijlstra 		struct mutex_waiter *waiter =
10579d659ae1SPeter Zijlstra 			list_first_entry(&lock->wait_list,
105801768b42SPeter Zijlstra 					 struct mutex_waiter, list);
105901768b42SPeter Zijlstra 
10609d659ae1SPeter Zijlstra 		next = waiter->task;
10619d659ae1SPeter Zijlstra 
106201768b42SPeter Zijlstra 		debug_mutex_wake_waiter(lock, waiter);
10639d659ae1SPeter Zijlstra 		wake_q_add(&wake_q, next);
106401768b42SPeter Zijlstra 	}
106501768b42SPeter Zijlstra 
10669d659ae1SPeter Zijlstra 	if (owner & MUTEX_FLAG_HANDOFF)
10679d659ae1SPeter Zijlstra 		__mutex_handoff(lock, next);
10689d659ae1SPeter Zijlstra 
1069b9c16a0eSPeter Zijlstra 	spin_unlock(&lock->wait_lock);
10709d659ae1SPeter Zijlstra 
10711329ce6fSDavidlohr Bueso 	wake_up_q(&wake_q);
107201768b42SPeter Zijlstra }
107301768b42SPeter Zijlstra 
107401768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
107501768b42SPeter Zijlstra /*
107601768b42SPeter Zijlstra  * Here come the less common (and hence less performance-critical) APIs:
107701768b42SPeter Zijlstra  * mutex_lock_interruptible() and mutex_trylock().
107801768b42SPeter Zijlstra  */
107901768b42SPeter Zijlstra static noinline int __sched
108001768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock);
108101768b42SPeter Zijlstra 
108201768b42SPeter Zijlstra static noinline int __sched
108301768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock);
108401768b42SPeter Zijlstra 
108501768b42SPeter Zijlstra /**
108645dbac0eSMatthew Wilcox  * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
108745dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
108801768b42SPeter Zijlstra  *
108945dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal is delivered while the
109045dbac0eSMatthew Wilcox  * process is sleeping, this function will return without acquiring the
109145dbac0eSMatthew Wilcox  * mutex.
109201768b42SPeter Zijlstra  *
109345dbac0eSMatthew Wilcox  * Context: Process context.
109445dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
109545dbac0eSMatthew Wilcox  * signal arrived.
109601768b42SPeter Zijlstra  */
109701768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock)
109801768b42SPeter Zijlstra {
109901768b42SPeter Zijlstra 	might_sleep();
11003ca0ff57SPeter Zijlstra 
11013ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
110201768b42SPeter Zijlstra 		return 0;
11033ca0ff57SPeter Zijlstra 
110401768b42SPeter Zijlstra 	return __mutex_lock_interruptible_slowpath(lock);
110501768b42SPeter Zijlstra }
110601768b42SPeter Zijlstra 
110701768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible);
110801768b42SPeter Zijlstra 
110945dbac0eSMatthew Wilcox /**
111045dbac0eSMatthew Wilcox  * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
111145dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
111245dbac0eSMatthew Wilcox  *
111345dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal which will be fatal to
111445dbac0eSMatthew Wilcox  * the current process is delivered while the process is sleeping, this
111545dbac0eSMatthew Wilcox  * function will return without acquiring the mutex.
111645dbac0eSMatthew Wilcox  *
111745dbac0eSMatthew Wilcox  * Context: Process context.
111845dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
111945dbac0eSMatthew Wilcox  * fatal signal arrived.
112045dbac0eSMatthew Wilcox  */
112101768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock)
112201768b42SPeter Zijlstra {
112301768b42SPeter Zijlstra 	might_sleep();
11243ca0ff57SPeter Zijlstra 
11253ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
112601768b42SPeter Zijlstra 		return 0;
11273ca0ff57SPeter Zijlstra 
112801768b42SPeter Zijlstra 	return __mutex_lock_killable_slowpath(lock);
112901768b42SPeter Zijlstra }
113001768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable);
113101768b42SPeter Zijlstra 
113245dbac0eSMatthew Wilcox /**
113345dbac0eSMatthew Wilcox  * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
113445dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
113545dbac0eSMatthew Wilcox  *
113645dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  While the task is waiting for this
113745dbac0eSMatthew Wilcox  * mutex, it will be accounted as being in the IO wait state by the
113845dbac0eSMatthew Wilcox  * scheduler.
113945dbac0eSMatthew Wilcox  *
114045dbac0eSMatthew Wilcox  * Context: Process context.
114145dbac0eSMatthew Wilcox  */
11421460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock)
11431460cb65STejun Heo {
11441460cb65STejun Heo 	int token;
11451460cb65STejun Heo 
11461460cb65STejun Heo 	token = io_schedule_prepare();
11471460cb65STejun Heo 	mutex_lock(lock);
11481460cb65STejun Heo 	io_schedule_finish(token);
11491460cb65STejun Heo }
11501460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io);
11511460cb65STejun Heo 
11523ca0ff57SPeter Zijlstra static noinline void __sched
11533ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock)
115401768b42SPeter Zijlstra {
1155427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
115601768b42SPeter Zijlstra }
115701768b42SPeter Zijlstra 
115801768b42SPeter Zijlstra static noinline int __sched
115901768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock)
116001768b42SPeter Zijlstra {
1161427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
116201768b42SPeter Zijlstra }
116301768b42SPeter Zijlstra 
116401768b42SPeter Zijlstra static noinline int __sched
116501768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock)
116601768b42SPeter Zijlstra {
1167427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
116801768b42SPeter Zijlstra }
116901768b42SPeter Zijlstra 
117001768b42SPeter Zijlstra static noinline int __sched
117101768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
117201768b42SPeter Zijlstra {
1173427b1820SPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, NULL,
1174427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
117501768b42SPeter Zijlstra }
117601768b42SPeter Zijlstra 
117701768b42SPeter Zijlstra static noinline int __sched
117801768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
117901768b42SPeter Zijlstra 					    struct ww_acquire_ctx *ctx)
118001768b42SPeter Zijlstra {
1181427b1820SPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, NULL,
1182427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
118301768b42SPeter Zijlstra }
118401768b42SPeter Zijlstra 
118501768b42SPeter Zijlstra #endif
118601768b42SPeter Zijlstra 
118701768b42SPeter Zijlstra /**
118801768b42SPeter Zijlstra  * mutex_trylock - try to acquire the mutex, without waiting
118901768b42SPeter Zijlstra  * @lock: the mutex to be acquired
119001768b42SPeter Zijlstra  *
119101768b42SPeter Zijlstra  * Try to acquire the mutex atomically. Returns 1 if the mutex
119201768b42SPeter Zijlstra  * has been acquired successfully, and 0 on contention.
119301768b42SPeter Zijlstra  *
119401768b42SPeter Zijlstra  * NOTE: this function follows the spin_trylock() convention, so
119501768b42SPeter Zijlstra  * it is negated from the down_trylock() return values! Be careful
119601768b42SPeter Zijlstra  * about this when converting semaphore users to mutexes.
119701768b42SPeter Zijlstra  *
119801768b42SPeter Zijlstra  * This function must not be used in interrupt context. The
119901768b42SPeter Zijlstra  * mutex must be released by the same task that acquired it.
120001768b42SPeter Zijlstra  */
120101768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock)
120201768b42SPeter Zijlstra {
1203e274795eSPeter Zijlstra 	bool locked = __mutex_trylock(lock);
120401768b42SPeter Zijlstra 
12053ca0ff57SPeter Zijlstra 	if (locked)
12063ca0ff57SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
120701768b42SPeter Zijlstra 
12083ca0ff57SPeter Zijlstra 	return locked;
120901768b42SPeter Zijlstra }
121001768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock);
121101768b42SPeter Zijlstra 
121201768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
121301768b42SPeter Zijlstra int __sched
1214c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
121501768b42SPeter Zijlstra {
121601768b42SPeter Zijlstra 	might_sleep();
121701768b42SPeter Zijlstra 
12183ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1219ea9e0fb8SNicolai Hähnle 		if (ctx)
122001768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
12213ca0ff57SPeter Zijlstra 		return 0;
12223ca0ff57SPeter Zijlstra 	}
12233ca0ff57SPeter Zijlstra 
12243ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_slowpath(lock, ctx);
122501768b42SPeter Zijlstra }
1226c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock);
122701768b42SPeter Zijlstra 
122801768b42SPeter Zijlstra int __sched
1229c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
123001768b42SPeter Zijlstra {
123101768b42SPeter Zijlstra 	might_sleep();
123201768b42SPeter Zijlstra 
12333ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1234ea9e0fb8SNicolai Hähnle 		if (ctx)
123501768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
12363ca0ff57SPeter Zijlstra 		return 0;
12373ca0ff57SPeter Zijlstra 	}
12383ca0ff57SPeter Zijlstra 
12393ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
124001768b42SPeter Zijlstra }
1241c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock_interruptible);
124201768b42SPeter Zijlstra 
124301768b42SPeter Zijlstra #endif
124401768b42SPeter Zijlstra 
124501768b42SPeter Zijlstra /**
124601768b42SPeter Zijlstra  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
124701768b42SPeter Zijlstra  * @cnt: the atomic which we are to dec
124801768b42SPeter Zijlstra  * @lock: the mutex to return holding if we dec to 0
124901768b42SPeter Zijlstra  *
125001768b42SPeter Zijlstra  * return true and hold lock if we dec to 0, return false otherwise
125101768b42SPeter Zijlstra  */
125201768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
125301768b42SPeter Zijlstra {
125401768b42SPeter Zijlstra 	/* dec if we can't possibly hit 0 */
125501768b42SPeter Zijlstra 	if (atomic_add_unless(cnt, -1, 1))
125601768b42SPeter Zijlstra 		return 0;
125701768b42SPeter Zijlstra 	/* we might hit 0, so take the lock */
125801768b42SPeter Zijlstra 	mutex_lock(lock);
125901768b42SPeter Zijlstra 	if (!atomic_dec_and_test(cnt)) {
126001768b42SPeter Zijlstra 		/* when we actually did the dec, we didn't hit 0 */
126101768b42SPeter Zijlstra 		mutex_unlock(lock);
126201768b42SPeter Zijlstra 		return 0;
126301768b42SPeter Zijlstra 	}
126401768b42SPeter Zijlstra 	/* we hit 0, and we hold the lock */
126501768b42SPeter Zijlstra 	return 1;
126601768b42SPeter Zijlstra }
126701768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
1268