xref: /openbmc/linux/kernel/locking/mutex.c (revision 2674bd181f3338dc2c58a59caa766dc9d5779784)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
201768b42SPeter Zijlstra /*
367a6de49SPeter Zijlstra  * kernel/locking/mutex.c
401768b42SPeter Zijlstra  *
501768b42SPeter Zijlstra  * Mutexes: blocking mutual exclusion locks
601768b42SPeter Zijlstra  *
701768b42SPeter Zijlstra  * Started by Ingo Molnar:
801768b42SPeter Zijlstra  *
901768b42SPeter Zijlstra  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
1001768b42SPeter Zijlstra  *
1101768b42SPeter Zijlstra  * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
1201768b42SPeter Zijlstra  * David Howells for suggestions and improvements.
1301768b42SPeter Zijlstra  *
1401768b42SPeter Zijlstra  *  - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
1501768b42SPeter Zijlstra  *    from the -rt tree, where it was originally implemented for rtmutexes
1601768b42SPeter Zijlstra  *    by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
1701768b42SPeter Zijlstra  *    and Sven Dietrich.
1801768b42SPeter Zijlstra  *
19387b1468SMauro Carvalho Chehab  * Also see Documentation/locking/mutex-design.rst.
2001768b42SPeter Zijlstra  */
2101768b42SPeter Zijlstra #include <linux/mutex.h>
2201768b42SPeter Zijlstra #include <linux/ww_mutex.h>
23174cd4b1SIngo Molnar #include <linux/sched/signal.h>
2401768b42SPeter Zijlstra #include <linux/sched/rt.h>
2584f001e1SIngo Molnar #include <linux/sched/wake_q.h>
26b17b0153SIngo Molnar #include <linux/sched/debug.h>
2701768b42SPeter Zijlstra #include <linux/export.h>
2801768b42SPeter Zijlstra #include <linux/spinlock.h>
2901768b42SPeter Zijlstra #include <linux/interrupt.h>
3001768b42SPeter Zijlstra #include <linux/debug_locks.h>
317a215f89SDavidlohr Bueso #include <linux/osq_lock.h>
3201768b42SPeter Zijlstra 
33a321fb90SThomas Gleixner #include "mutex.h"
34a321fb90SThomas Gleixner 
3501768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
36e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond)
3701768b42SPeter Zijlstra #else
38e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond)
3901768b42SPeter Zijlstra #endif
4001768b42SPeter Zijlstra 
4101768b42SPeter Zijlstra void
4201768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
4301768b42SPeter Zijlstra {
443ca0ff57SPeter Zijlstra 	atomic_long_set(&lock->owner, 0);
45ebf4c55cSThomas Gleixner 	raw_spin_lock_init(&lock->wait_lock);
4601768b42SPeter Zijlstra 	INIT_LIST_HEAD(&lock->wait_list);
4701768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
484d9d951eSJason Low 	osq_lock_init(&lock->osq);
4901768b42SPeter Zijlstra #endif
5001768b42SPeter Zijlstra 
5101768b42SPeter Zijlstra 	debug_mutex_init(lock, name, key);
5201768b42SPeter Zijlstra }
5301768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init);
5401768b42SPeter Zijlstra 
553ca0ff57SPeter Zijlstra /*
563ca0ff57SPeter Zijlstra  * @owner: contains: 'struct task_struct *' to the current lock owner,
573ca0ff57SPeter Zijlstra  * NULL means not owned. Since task_struct pointers are aligned at
58e274795eSPeter Zijlstra  * at least L1_CACHE_BYTES, we have low bits to store extra state.
593ca0ff57SPeter Zijlstra  *
603ca0ff57SPeter Zijlstra  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
619d659ae1SPeter Zijlstra  * Bit1 indicates unlock needs to hand the lock to the top-waiter
62e274795eSPeter Zijlstra  * Bit2 indicates handoff has been done and we're waiting for pickup.
633ca0ff57SPeter Zijlstra  */
643ca0ff57SPeter Zijlstra #define MUTEX_FLAG_WAITERS	0x01
659d659ae1SPeter Zijlstra #define MUTEX_FLAG_HANDOFF	0x02
66e274795eSPeter Zijlstra #define MUTEX_FLAG_PICKUP	0x04
673ca0ff57SPeter Zijlstra 
68e274795eSPeter Zijlstra #define MUTEX_FLAGS		0x07
693ca0ff57SPeter Zijlstra 
705f35d5a6SMukesh Ojha /*
715f35d5a6SMukesh Ojha  * Internal helper function; C doesn't allow us to hide it :/
725f35d5a6SMukesh Ojha  *
735f35d5a6SMukesh Ojha  * DO NOT USE (outside of mutex code).
745f35d5a6SMukesh Ojha  */
755f35d5a6SMukesh Ojha static inline struct task_struct *__mutex_owner(struct mutex *lock)
765f35d5a6SMukesh Ojha {
77a037d269SMukesh Ojha 	return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
785f35d5a6SMukesh Ojha }
795f35d5a6SMukesh Ojha 
803ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner)
813ca0ff57SPeter Zijlstra {
823ca0ff57SPeter Zijlstra 	return (struct task_struct *)(owner & ~MUTEX_FLAGS);
833ca0ff57SPeter Zijlstra }
843ca0ff57SPeter Zijlstra 
855f35d5a6SMukesh Ojha bool mutex_is_locked(struct mutex *lock)
865f35d5a6SMukesh Ojha {
875f35d5a6SMukesh Ojha 	return __mutex_owner(lock) != NULL;
885f35d5a6SMukesh Ojha }
895f35d5a6SMukesh Ojha EXPORT_SYMBOL(mutex_is_locked);
905f35d5a6SMukesh Ojha 
913ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner)
923ca0ff57SPeter Zijlstra {
933ca0ff57SPeter Zijlstra 	return owner & MUTEX_FLAGS;
943ca0ff57SPeter Zijlstra }
953ca0ff57SPeter Zijlstra 
96ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff)
973ca0ff57SPeter Zijlstra {
983ca0ff57SPeter Zijlstra 	unsigned long owner, curr = (unsigned long)current;
993ca0ff57SPeter Zijlstra 
1003ca0ff57SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
1013ca0ff57SPeter Zijlstra 	for (;;) { /* must loop, can race against a flag */
102ab4e4d9fSPeter Zijlstra 		unsigned long flags = __owner_flags(owner);
103e274795eSPeter Zijlstra 		unsigned long task = owner & ~MUTEX_FLAGS;
1043ca0ff57SPeter Zijlstra 
105e274795eSPeter Zijlstra 		if (task) {
106ad90880dSPeter Zijlstra 			if (flags & MUTEX_FLAG_PICKUP) {
107ad90880dSPeter Zijlstra 				if (task != curr)
108e274795eSPeter Zijlstra 					break;
109e274795eSPeter Zijlstra 				flags &= ~MUTEX_FLAG_PICKUP;
110ad90880dSPeter Zijlstra 			} else if (handoff) {
111ad90880dSPeter Zijlstra 				if (flags & MUTEX_FLAG_HANDOFF)
112ad90880dSPeter Zijlstra 					break;
113ad90880dSPeter Zijlstra 				flags |= MUTEX_FLAG_HANDOFF;
114ad90880dSPeter Zijlstra 			} else {
115ad90880dSPeter Zijlstra 				break;
116ad90880dSPeter Zijlstra 			}
117e274795eSPeter Zijlstra 		} else {
118e6b4457bSPeter Zijlstra 			MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP));
119ad90880dSPeter Zijlstra 			task = curr;
1209d659ae1SPeter Zijlstra 		}
1213ca0ff57SPeter Zijlstra 
122ad90880dSPeter Zijlstra 		if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) {
123ad90880dSPeter Zijlstra 			if (task == curr)
124e274795eSPeter Zijlstra 				return NULL;
125ad90880dSPeter Zijlstra 			break;
126ad90880dSPeter Zijlstra 		}
1273ca0ff57SPeter Zijlstra 	}
128e274795eSPeter Zijlstra 
129e274795eSPeter Zijlstra 	return __owner_task(owner);
130e274795eSPeter Zijlstra }
131e274795eSPeter Zijlstra 
132e274795eSPeter Zijlstra /*
133ad90880dSPeter Zijlstra  * Trylock or set HANDOFF
134ad90880dSPeter Zijlstra  */
135ad90880dSPeter Zijlstra static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff)
136ad90880dSPeter Zijlstra {
137ad90880dSPeter Zijlstra 	return !__mutex_trylock_common(lock, handoff);
138ad90880dSPeter Zijlstra }
139ad90880dSPeter Zijlstra 
140ad90880dSPeter Zijlstra /*
141e274795eSPeter Zijlstra  * Actual trylock that will work on any unlocked state.
142e274795eSPeter Zijlstra  */
143e274795eSPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock)
144e274795eSPeter Zijlstra {
145ad90880dSPeter Zijlstra 	return !__mutex_trylock_common(lock, false);
1463ca0ff57SPeter Zijlstra }
1473ca0ff57SPeter Zijlstra 
1483ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
1493ca0ff57SPeter Zijlstra /*
1503ca0ff57SPeter Zijlstra  * Lockdep annotations are contained to the slow paths for simplicity.
1513ca0ff57SPeter Zijlstra  * There is nothing that would stop spreading the lockdep annotations outwards
1523ca0ff57SPeter Zijlstra  * except more code.
1533ca0ff57SPeter Zijlstra  */
1543ca0ff57SPeter Zijlstra 
1553ca0ff57SPeter Zijlstra /*
1563ca0ff57SPeter Zijlstra  * Optimistic trylock that only works in the uncontended case. Make sure to
1573ca0ff57SPeter Zijlstra  * follow with a __mutex_trylock() before failing.
1583ca0ff57SPeter Zijlstra  */
1593ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
1603ca0ff57SPeter Zijlstra {
1613ca0ff57SPeter Zijlstra 	unsigned long curr = (unsigned long)current;
162c427f695SPeter Zijlstra 	unsigned long zero = 0UL;
1633ca0ff57SPeter Zijlstra 
164c427f695SPeter Zijlstra 	if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
1653ca0ff57SPeter Zijlstra 		return true;
1663ca0ff57SPeter Zijlstra 
1673ca0ff57SPeter Zijlstra 	return false;
1683ca0ff57SPeter Zijlstra }
1693ca0ff57SPeter Zijlstra 
1703ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
1713ca0ff57SPeter Zijlstra {
1723ca0ff57SPeter Zijlstra 	unsigned long curr = (unsigned long)current;
1733ca0ff57SPeter Zijlstra 
174ab4e4d9fSPeter Zijlstra 	return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL);
1753ca0ff57SPeter Zijlstra }
1763ca0ff57SPeter Zijlstra #endif
1773ca0ff57SPeter Zijlstra 
1783ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
1793ca0ff57SPeter Zijlstra {
1803ca0ff57SPeter Zijlstra 	atomic_long_or(flag, &lock->owner);
1813ca0ff57SPeter Zijlstra }
1823ca0ff57SPeter Zijlstra 
1833ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
1843ca0ff57SPeter Zijlstra {
1853ca0ff57SPeter Zijlstra 	atomic_long_andnot(flag, &lock->owner);
1863ca0ff57SPeter Zijlstra }
1873ca0ff57SPeter Zijlstra 
1889d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
1899d659ae1SPeter Zijlstra {
1909d659ae1SPeter Zijlstra 	return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
1919d659ae1SPeter Zijlstra }
1929d659ae1SPeter Zijlstra 
1939d659ae1SPeter Zijlstra /*
19408295b3bSThomas Hellstrom  * Add @waiter to a given location in the lock wait_list and set the
19508295b3bSThomas Hellstrom  * FLAG_WAITERS flag if it's the first waiter.
19608295b3bSThomas Hellstrom  */
1973a010c49SZqiang static void
19808295b3bSThomas Hellstrom __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
19908295b3bSThomas Hellstrom 		   struct list_head *list)
20008295b3bSThomas Hellstrom {
20108295b3bSThomas Hellstrom 	debug_mutex_add_waiter(lock, waiter, current);
20208295b3bSThomas Hellstrom 
20308295b3bSThomas Hellstrom 	list_add_tail(&waiter->list, list);
20408295b3bSThomas Hellstrom 	if (__mutex_waiter_is_first(lock, waiter))
20508295b3bSThomas Hellstrom 		__mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
20608295b3bSThomas Hellstrom }
20708295b3bSThomas Hellstrom 
2083a010c49SZqiang static void
2093a010c49SZqiang __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter)
2103a010c49SZqiang {
2113a010c49SZqiang 	list_del(&waiter->list);
2123a010c49SZqiang 	if (likely(list_empty(&lock->wait_list)))
2133a010c49SZqiang 		__mutex_clear_flag(lock, MUTEX_FLAGS);
2143a010c49SZqiang 
2153a010c49SZqiang 	debug_mutex_remove_waiter(lock, waiter, current);
2163a010c49SZqiang }
2173a010c49SZqiang 
21808295b3bSThomas Hellstrom /*
2199d659ae1SPeter Zijlstra  * Give up ownership to a specific task, when @task = NULL, this is equivalent
220e2db7592SIngo Molnar  * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves
221e274795eSPeter Zijlstra  * WAITERS. Provides RELEASE semantics like a regular unlock, the
222e274795eSPeter Zijlstra  * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
2239d659ae1SPeter Zijlstra  */
2249d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
2259d659ae1SPeter Zijlstra {
2269d659ae1SPeter Zijlstra 	unsigned long owner = atomic_long_read(&lock->owner);
2279d659ae1SPeter Zijlstra 
2289d659ae1SPeter Zijlstra 	for (;;) {
229ab4e4d9fSPeter Zijlstra 		unsigned long new;
2309d659ae1SPeter Zijlstra 
231e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(__owner_task(owner) != current);
232e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
2339d659ae1SPeter Zijlstra 
2349d659ae1SPeter Zijlstra 		new = (owner & MUTEX_FLAG_WAITERS);
2359d659ae1SPeter Zijlstra 		new |= (unsigned long)task;
236e274795eSPeter Zijlstra 		if (task)
237e274795eSPeter Zijlstra 			new |= MUTEX_FLAG_PICKUP;
2389d659ae1SPeter Zijlstra 
239ab4e4d9fSPeter Zijlstra 		if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new))
2409d659ae1SPeter Zijlstra 			break;
2419d659ae1SPeter Zijlstra 	}
2429d659ae1SPeter Zijlstra }
2439d659ae1SPeter Zijlstra 
24401768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
24501768b42SPeter Zijlstra /*
24601768b42SPeter Zijlstra  * We split the mutex lock/unlock logic into separate fastpath and
24701768b42SPeter Zijlstra  * slowpath functions, to reduce the register pressure on the fastpath.
24801768b42SPeter Zijlstra  * We also put the fastpath first in the kernel image, to make sure the
24901768b42SPeter Zijlstra  * branch is predicted by the CPU as default-untaken.
25001768b42SPeter Zijlstra  */
2513ca0ff57SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock);
25201768b42SPeter Zijlstra 
25301768b42SPeter Zijlstra /**
25401768b42SPeter Zijlstra  * mutex_lock - acquire the mutex
25501768b42SPeter Zijlstra  * @lock: the mutex to be acquired
25601768b42SPeter Zijlstra  *
25701768b42SPeter Zijlstra  * Lock the mutex exclusively for this task. If the mutex is not
25801768b42SPeter Zijlstra  * available right now, it will sleep until it can get it.
25901768b42SPeter Zijlstra  *
26001768b42SPeter Zijlstra  * The mutex must later on be released by the same task that
26101768b42SPeter Zijlstra  * acquired it. Recursive locking is not allowed. The task
26201768b42SPeter Zijlstra  * may not exit without first unlocking the mutex. Also, kernel
263139b6fd2SSharon Dvir  * memory where the mutex resides must not be freed with
26401768b42SPeter Zijlstra  * the mutex still locked. The mutex must first be initialized
26501768b42SPeter Zijlstra  * (or statically defined) before it can be locked. memset()-ing
26601768b42SPeter Zijlstra  * the mutex to 0 is not allowed.
26701768b42SPeter Zijlstra  *
26801768b42SPeter Zijlstra  * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
26901768b42SPeter Zijlstra  * checks that will enforce the restrictions and will also do
2707b4ff1adSMauro Carvalho Chehab  * deadlock debugging)
27101768b42SPeter Zijlstra  *
27201768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down().
27301768b42SPeter Zijlstra  */
27401768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock)
27501768b42SPeter Zijlstra {
27601768b42SPeter Zijlstra 	might_sleep();
27701768b42SPeter Zijlstra 
2783ca0ff57SPeter Zijlstra 	if (!__mutex_trylock_fast(lock))
2793ca0ff57SPeter Zijlstra 		__mutex_lock_slowpath(lock);
2803ca0ff57SPeter Zijlstra }
28101768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock);
28201768b42SPeter Zijlstra #endif
28301768b42SPeter Zijlstra 
284*2674bd18SPeter Zijlstra (Intel) #include "ww_mutex.h"
28576916515SDavidlohr Bueso 
28601768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
287c516df97SNicolai Hähnle 
288ad90880dSPeter Zijlstra /*
289ad90880dSPeter Zijlstra  * Trylock variant that returns the owning task on failure.
290ad90880dSPeter Zijlstra  */
291ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
292ad90880dSPeter Zijlstra {
293ad90880dSPeter Zijlstra 	return __mutex_trylock_common(lock, false);
294ad90880dSPeter Zijlstra }
295ad90880dSPeter Zijlstra 
296c516df97SNicolai Hähnle static inline
297c516df97SNicolai Hähnle bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
298c516df97SNicolai Hähnle 			    struct mutex_waiter *waiter)
299c516df97SNicolai Hähnle {
300c516df97SNicolai Hähnle 	struct ww_mutex *ww;
301c516df97SNicolai Hähnle 
302c516df97SNicolai Hähnle 	ww = container_of(lock, struct ww_mutex, base);
303c516df97SNicolai Hähnle 
30401768b42SPeter Zijlstra 	/*
305c516df97SNicolai Hähnle 	 * If ww->ctx is set the contents are undefined, only
306c516df97SNicolai Hähnle 	 * by acquiring wait_lock there is a guarantee that
307c516df97SNicolai Hähnle 	 * they are not invalid when reading.
308c516df97SNicolai Hähnle 	 *
309c516df97SNicolai Hähnle 	 * As such, when deadlock detection needs to be
310c516df97SNicolai Hähnle 	 * performed the optimistic spinning cannot be done.
311c516df97SNicolai Hähnle 	 *
312c516df97SNicolai Hähnle 	 * Check this in every inner iteration because we may
313c516df97SNicolai Hähnle 	 * be racing against another thread's ww_mutex_lock.
314c516df97SNicolai Hähnle 	 */
315c516df97SNicolai Hähnle 	if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
316c516df97SNicolai Hähnle 		return false;
317c516df97SNicolai Hähnle 
318c516df97SNicolai Hähnle 	/*
319c516df97SNicolai Hähnle 	 * If we aren't on the wait list yet, cancel the spin
320c516df97SNicolai Hähnle 	 * if there are waiters. We want  to avoid stealing the
321c516df97SNicolai Hähnle 	 * lock from a waiter with an earlier stamp, since the
322c516df97SNicolai Hähnle 	 * other thread may already own a lock that we also
323c516df97SNicolai Hähnle 	 * need.
324c516df97SNicolai Hähnle 	 */
325c516df97SNicolai Hähnle 	if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
326c516df97SNicolai Hähnle 		return false;
327c516df97SNicolai Hähnle 
328c516df97SNicolai Hähnle 	/*
329c516df97SNicolai Hähnle 	 * Similarly, stop spinning if we are no longer the
330c516df97SNicolai Hähnle 	 * first waiter.
331c516df97SNicolai Hähnle 	 */
332c516df97SNicolai Hähnle 	if (waiter && !__mutex_waiter_is_first(lock, waiter))
333c516df97SNicolai Hähnle 		return false;
334c516df97SNicolai Hähnle 
335c516df97SNicolai Hähnle 	return true;
336c516df97SNicolai Hähnle }
337c516df97SNicolai Hähnle 
33801768b42SPeter Zijlstra /*
33925f13b40SNicolai Hähnle  * Look out! "owner" is an entirely speculative pointer access and not
34025f13b40SNicolai Hähnle  * reliable.
34125f13b40SNicolai Hähnle  *
34225f13b40SNicolai Hähnle  * "noinline" so that this function shows up on perf profiles.
34301768b42SPeter Zijlstra  */
34401768b42SPeter Zijlstra static noinline
34525f13b40SNicolai Hähnle bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
346c516df97SNicolai Hähnle 			 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
34701768b42SPeter Zijlstra {
34801ac33c1SJason Low 	bool ret = true;
349be1f7bf2SJason Low 
35001768b42SPeter Zijlstra 	rcu_read_lock();
3513ca0ff57SPeter Zijlstra 	while (__mutex_owner(lock) == owner) {
352be1f7bf2SJason Low 		/*
353be1f7bf2SJason Low 		 * Ensure we emit the owner->on_cpu, dereference _after_
35401ac33c1SJason Low 		 * checking lock->owner still matches owner. If that fails,
35501ac33c1SJason Low 		 * owner might point to freed memory. If it still matches,
356be1f7bf2SJason Low 		 * the rcu_read_lock() ensures the memory stays valid.
357be1f7bf2SJason Low 		 */
358be1f7bf2SJason Low 		barrier();
359be1f7bf2SJason Low 
36005ffc951SPan Xinhui 		/*
36105ffc951SPan Xinhui 		 * Use vcpu_is_preempted to detect lock holder preemption issue.
36205ffc951SPan Xinhui 		 */
36305ffc951SPan Xinhui 		if (!owner->on_cpu || need_resched() ||
36405ffc951SPan Xinhui 				vcpu_is_preempted(task_cpu(owner))) {
365be1f7bf2SJason Low 			ret = false;
366be1f7bf2SJason Low 			break;
367be1f7bf2SJason Low 		}
36801768b42SPeter Zijlstra 
369c516df97SNicolai Hähnle 		if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
37025f13b40SNicolai Hähnle 			ret = false;
37125f13b40SNicolai Hähnle 			break;
37225f13b40SNicolai Hähnle 		}
37325f13b40SNicolai Hähnle 
374f2f09a4cSChristian Borntraeger 		cpu_relax();
37501768b42SPeter Zijlstra 	}
37601768b42SPeter Zijlstra 	rcu_read_unlock();
37701768b42SPeter Zijlstra 
378be1f7bf2SJason Low 	return ret;
37901768b42SPeter Zijlstra }
38001768b42SPeter Zijlstra 
38101768b42SPeter Zijlstra /*
38201768b42SPeter Zijlstra  * Initial check for entering the mutex spinning loop
38301768b42SPeter Zijlstra  */
38401768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock)
38501768b42SPeter Zijlstra {
38601768b42SPeter Zijlstra 	struct task_struct *owner;
38701768b42SPeter Zijlstra 	int retval = 1;
38801768b42SPeter Zijlstra 
38946af29e4SJason Low 	if (need_resched())
39046af29e4SJason Low 		return 0;
39146af29e4SJason Low 
39201768b42SPeter Zijlstra 	rcu_read_lock();
3933ca0ff57SPeter Zijlstra 	owner = __mutex_owner(lock);
39405ffc951SPan Xinhui 
39505ffc951SPan Xinhui 	/*
39605ffc951SPan Xinhui 	 * As lock holder preemption issue, we both skip spinning if task is not
39705ffc951SPan Xinhui 	 * on cpu or its cpu is preempted
39805ffc951SPan Xinhui 	 */
39901768b42SPeter Zijlstra 	if (owner)
40005ffc951SPan Xinhui 		retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
40101768b42SPeter Zijlstra 	rcu_read_unlock();
40276916515SDavidlohr Bueso 
40376916515SDavidlohr Bueso 	/*
4043ca0ff57SPeter Zijlstra 	 * If lock->owner is not set, the mutex has been released. Return true
4053ca0ff57SPeter Zijlstra 	 * such that we'll trylock in the spin path, which is a faster option
4063ca0ff57SPeter Zijlstra 	 * than the blocking slow path.
40776916515SDavidlohr Bueso 	 */
4083ca0ff57SPeter Zijlstra 	return retval;
40976916515SDavidlohr Bueso }
41076916515SDavidlohr Bueso 
41176916515SDavidlohr Bueso /*
41276916515SDavidlohr Bueso  * Optimistic spinning.
41376916515SDavidlohr Bueso  *
41476916515SDavidlohr Bueso  * We try to spin for acquisition when we find that the lock owner
41576916515SDavidlohr Bueso  * is currently running on a (different) CPU and while we don't
41676916515SDavidlohr Bueso  * need to reschedule. The rationale is that if the lock owner is
41776916515SDavidlohr Bueso  * running, it is likely to release the lock soon.
41876916515SDavidlohr Bueso  *
41976916515SDavidlohr Bueso  * The mutex spinners are queued up using MCS lock so that only one
42076916515SDavidlohr Bueso  * spinner can compete for the mutex. However, if mutex spinning isn't
42176916515SDavidlohr Bueso  * going to happen, there is no point in going through the lock/unlock
42276916515SDavidlohr Bueso  * overhead.
42376916515SDavidlohr Bueso  *
42476916515SDavidlohr Bueso  * Returns true when the lock was taken, otherwise false, indicating
42576916515SDavidlohr Bueso  * that we need to jump to the slowpath and sleep.
426b341afb3SWaiman Long  *
427b341afb3SWaiman Long  * The waiter flag is set to true if the spinner is a waiter in the wait
428b341afb3SWaiman Long  * queue. The waiter-spinner will spin on the lock directly and concurrently
429b341afb3SWaiman Long  * with the spinner at the head of the OSQ, if present, until the owner is
430b341afb3SWaiman Long  * changed to itself.
43176916515SDavidlohr Bueso  */
432427b1820SPeter Zijlstra static __always_inline bool
433427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
4345de2055dSWaiman Long 		      struct mutex_waiter *waiter)
43576916515SDavidlohr Bueso {
436b341afb3SWaiman Long 	if (!waiter) {
437b341afb3SWaiman Long 		/*
438b341afb3SWaiman Long 		 * The purpose of the mutex_can_spin_on_owner() function is
439b341afb3SWaiman Long 		 * to eliminate the overhead of osq_lock() and osq_unlock()
440b341afb3SWaiman Long 		 * in case spinning isn't possible. As a waiter-spinner
441b341afb3SWaiman Long 		 * is not going to take OSQ lock anyway, there is no need
442b341afb3SWaiman Long 		 * to call mutex_can_spin_on_owner().
443b341afb3SWaiman Long 		 */
44476916515SDavidlohr Bueso 		if (!mutex_can_spin_on_owner(lock))
445b341afb3SWaiman Long 			goto fail;
44676916515SDavidlohr Bueso 
447e42f678aSDavidlohr Bueso 		/*
448e42f678aSDavidlohr Bueso 		 * In order to avoid a stampede of mutex spinners trying to
449e42f678aSDavidlohr Bueso 		 * acquire the mutex all at once, the spinners need to take a
450e42f678aSDavidlohr Bueso 		 * MCS (queued) lock first before spinning on the owner field.
451e42f678aSDavidlohr Bueso 		 */
45276916515SDavidlohr Bueso 		if (!osq_lock(&lock->osq))
453b341afb3SWaiman Long 			goto fail;
454b341afb3SWaiman Long 	}
45576916515SDavidlohr Bueso 
456b341afb3SWaiman Long 	for (;;) {
45776916515SDavidlohr Bueso 		struct task_struct *owner;
45876916515SDavidlohr Bueso 
459e274795eSPeter Zijlstra 		/* Try to acquire the mutex... */
460e274795eSPeter Zijlstra 		owner = __mutex_trylock_or_owner(lock);
461e274795eSPeter Zijlstra 		if (!owner)
462e274795eSPeter Zijlstra 			break;
46376916515SDavidlohr Bueso 
46476916515SDavidlohr Bueso 		/*
465e274795eSPeter Zijlstra 		 * There's an owner, wait for it to either
46676916515SDavidlohr Bueso 		 * release the lock or go to sleep.
46776916515SDavidlohr Bueso 		 */
468c516df97SNicolai Hähnle 		if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
469b341afb3SWaiman Long 			goto fail_unlock;
47076916515SDavidlohr Bueso 
47176916515SDavidlohr Bueso 		/*
47276916515SDavidlohr Bueso 		 * The cpu_relax() call is a compiler barrier which forces
47376916515SDavidlohr Bueso 		 * everything in this loop to be re-loaded. We don't need
47476916515SDavidlohr Bueso 		 * memory barriers as we'll eventually observe the right
47576916515SDavidlohr Bueso 		 * values at the cost of a few extra spins.
47676916515SDavidlohr Bueso 		 */
477f2f09a4cSChristian Borntraeger 		cpu_relax();
47876916515SDavidlohr Bueso 	}
47976916515SDavidlohr Bueso 
480b341afb3SWaiman Long 	if (!waiter)
48176916515SDavidlohr Bueso 		osq_unlock(&lock->osq);
482b341afb3SWaiman Long 
483b341afb3SWaiman Long 	return true;
484b341afb3SWaiman Long 
485b341afb3SWaiman Long 
486b341afb3SWaiman Long fail_unlock:
487b341afb3SWaiman Long 	if (!waiter)
488b341afb3SWaiman Long 		osq_unlock(&lock->osq);
489b341afb3SWaiman Long 
490b341afb3SWaiman Long fail:
49176916515SDavidlohr Bueso 	/*
49276916515SDavidlohr Bueso 	 * If we fell out of the spin path because of need_resched(),
49376916515SDavidlohr Bueso 	 * reschedule now, before we try-lock the mutex. This avoids getting
49476916515SDavidlohr Bueso 	 * scheduled out right after we obtained the mutex.
49576916515SDavidlohr Bueso 	 */
4966f942a1fSPeter Zijlstra 	if (need_resched()) {
4976f942a1fSPeter Zijlstra 		/*
4986f942a1fSPeter Zijlstra 		 * We _should_ have TASK_RUNNING here, but just in case
4996f942a1fSPeter Zijlstra 		 * we do not, make it so, otherwise we might get stuck.
5006f942a1fSPeter Zijlstra 		 */
5016f942a1fSPeter Zijlstra 		__set_current_state(TASK_RUNNING);
50276916515SDavidlohr Bueso 		schedule_preempt_disabled();
5036f942a1fSPeter Zijlstra 	}
50476916515SDavidlohr Bueso 
50576916515SDavidlohr Bueso 	return false;
50676916515SDavidlohr Bueso }
50776916515SDavidlohr Bueso #else
508427b1820SPeter Zijlstra static __always_inline bool
509427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
5105de2055dSWaiman Long 		      struct mutex_waiter *waiter)
51176916515SDavidlohr Bueso {
51276916515SDavidlohr Bueso 	return false;
51376916515SDavidlohr Bueso }
51401768b42SPeter Zijlstra #endif
51501768b42SPeter Zijlstra 
5163ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
51701768b42SPeter Zijlstra 
51801768b42SPeter Zijlstra /**
51901768b42SPeter Zijlstra  * mutex_unlock - release the mutex
52001768b42SPeter Zijlstra  * @lock: the mutex to be released
52101768b42SPeter Zijlstra  *
52201768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously.
52301768b42SPeter Zijlstra  *
52401768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
52501768b42SPeter Zijlstra  * of a not locked mutex is not allowed.
52601768b42SPeter Zijlstra  *
52701768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) up().
52801768b42SPeter Zijlstra  */
52901768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock)
53001768b42SPeter Zijlstra {
5313ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
5323ca0ff57SPeter Zijlstra 	if (__mutex_unlock_fast(lock))
5333ca0ff57SPeter Zijlstra 		return;
53401768b42SPeter Zijlstra #endif
5353ca0ff57SPeter Zijlstra 	__mutex_unlock_slowpath(lock, _RET_IP_);
53601768b42SPeter Zijlstra }
53701768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock);
53801768b42SPeter Zijlstra 
53901768b42SPeter Zijlstra /**
54001768b42SPeter Zijlstra  * ww_mutex_unlock - release the w/w mutex
54101768b42SPeter Zijlstra  * @lock: the mutex to be released
54201768b42SPeter Zijlstra  *
54301768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously with any of the
54401768b42SPeter Zijlstra  * ww_mutex_lock* functions (with or without an acquire context). It is
54501768b42SPeter Zijlstra  * forbidden to release the locks after releasing the acquire context.
54601768b42SPeter Zijlstra  *
54701768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
54801768b42SPeter Zijlstra  * of a unlocked mutex is not allowed.
54901768b42SPeter Zijlstra  */
55001768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock)
55101768b42SPeter Zijlstra {
552aaa77de1SPeter Zijlstra (Intel) 	__ww_mutex_unlock(lock);
5533ca0ff57SPeter Zijlstra 	mutex_unlock(&lock->base);
55401768b42SPeter Zijlstra }
55501768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock);
55601768b42SPeter Zijlstra 
55701768b42SPeter Zijlstra /*
55801768b42SPeter Zijlstra  * Lock a mutex (possibly interruptible), slowpath:
55901768b42SPeter Zijlstra  */
56001768b42SPeter Zijlstra static __always_inline int __sched
5612f064a59SPeter Zijlstra __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass,
56201768b42SPeter Zijlstra 		    struct lockdep_map *nest_lock, unsigned long ip,
56301768b42SPeter Zijlstra 		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
56401768b42SPeter Zijlstra {
56501768b42SPeter Zijlstra 	struct mutex_waiter waiter;
566a40ca565SWaiman Long 	struct ww_mutex *ww;
56701768b42SPeter Zijlstra 	int ret;
56801768b42SPeter Zijlstra 
5695de2055dSWaiman Long 	if (!use_ww_ctx)
5705de2055dSWaiman Long 		ww_ctx = NULL;
5715de2055dSWaiman Long 
572427b1820SPeter Zijlstra 	might_sleep();
573ea9e0fb8SNicolai Hähnle 
574e6b4457bSPeter Zijlstra 	MUTEX_WARN_ON(lock->magic != lock);
5756c11c6e3SSebastian Andrzej Siewior 
576a40ca565SWaiman Long 	ww = container_of(lock, struct ww_mutex, base);
5775de2055dSWaiman Long 	if (ww_ctx) {
5780422e83dSChris Wilson 		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
5790422e83dSChris Wilson 			return -EALREADY;
58008295b3bSThomas Hellstrom 
58108295b3bSThomas Hellstrom 		/*
58208295b3bSThomas Hellstrom 		 * Reset the wounded flag after a kill. No other process can
58308295b3bSThomas Hellstrom 		 * race and wound us here since they can't have a valid owner
58408295b3bSThomas Hellstrom 		 * pointer if we don't have any locks held.
58508295b3bSThomas Hellstrom 		 */
58608295b3bSThomas Hellstrom 		if (ww_ctx->acquired == 0)
58708295b3bSThomas Hellstrom 			ww_ctx->wounded = 0;
588cf702eddSPeter Zijlstra 
589cf702eddSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
590cf702eddSPeter Zijlstra 		nest_lock = &ww_ctx->dep_map;
591cf702eddSPeter Zijlstra #endif
5920422e83dSChris Wilson 	}
5930422e83dSChris Wilson 
59401768b42SPeter Zijlstra 	preempt_disable();
59501768b42SPeter Zijlstra 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
59601768b42SPeter Zijlstra 
597e274795eSPeter Zijlstra 	if (__mutex_trylock(lock) ||
5985de2055dSWaiman Long 	    mutex_optimistic_spin(lock, ww_ctx, NULL)) {
59976916515SDavidlohr Bueso 		/* got the lock, yay! */
6003ca0ff57SPeter Zijlstra 		lock_acquired(&lock->dep_map, ip);
6015de2055dSWaiman Long 		if (ww_ctx)
6023ca0ff57SPeter Zijlstra 			ww_mutex_set_context_fastpath(ww, ww_ctx);
60301768b42SPeter Zijlstra 		preempt_enable();
60401768b42SPeter Zijlstra 		return 0;
60501768b42SPeter Zijlstra 	}
60601768b42SPeter Zijlstra 
607ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
6081e820c96SJason Low 	/*
6093ca0ff57SPeter Zijlstra 	 * After waiting to acquire the wait_lock, try again.
6101e820c96SJason Low 	 */
611659cf9f5SNicolai Hähnle 	if (__mutex_trylock(lock)) {
6125de2055dSWaiman Long 		if (ww_ctx)
61355f036caSPeter Ziljstra 			__ww_mutex_check_waiters(lock, ww_ctx);
614659cf9f5SNicolai Hähnle 
61501768b42SPeter Zijlstra 		goto skip_wait;
616659cf9f5SNicolai Hähnle 	}
61701768b42SPeter Zijlstra 
61801768b42SPeter Zijlstra 	debug_mutex_lock_common(lock, &waiter);
619c0afb0ffSPeter Zijlstra 	waiter.task = current;
620c0afb0ffSPeter Zijlstra 	if (ww_ctx)
621c0afb0ffSPeter Zijlstra 		waiter.ww_ctx = ww_ctx;
62201768b42SPeter Zijlstra 
6236baa5c60SNicolai Hähnle 	lock_contended(&lock->dep_map, ip);
6246baa5c60SNicolai Hähnle 
6256baa5c60SNicolai Hähnle 	if (!use_ww_ctx) {
62601768b42SPeter Zijlstra 		/* add waiting tasks to the end of the waitqueue (FIFO): */
62708295b3bSThomas Hellstrom 		__mutex_add_waiter(lock, &waiter, &lock->wait_list);
6286baa5c60SNicolai Hähnle 	} else {
62955f036caSPeter Ziljstra 		/*
63055f036caSPeter Ziljstra 		 * Add in stamp order, waking up waiters that must kill
63155f036caSPeter Ziljstra 		 * themselves.
63255f036caSPeter Ziljstra 		 */
6336baa5c60SNicolai Hähnle 		ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
6346baa5c60SNicolai Hähnle 		if (ret)
63555f036caSPeter Ziljstra 			goto err_early_kill;
6366baa5c60SNicolai Hähnle 	}
6376baa5c60SNicolai Hähnle 
638642fa448SDavidlohr Bueso 	set_current_state(state);
63901768b42SPeter Zijlstra 	for (;;) {
640048661a1SPeter Zijlstra 		bool first;
641048661a1SPeter Zijlstra 
6425bbd7e64SPeter Zijlstra 		/*
6435bbd7e64SPeter Zijlstra 		 * Once we hold wait_lock, we're serialized against
6445bbd7e64SPeter Zijlstra 		 * mutex_unlock() handing the lock off to us, do a trylock
6455bbd7e64SPeter Zijlstra 		 * before testing the error conditions to make sure we pick up
6465bbd7e64SPeter Zijlstra 		 * the handoff.
6475bbd7e64SPeter Zijlstra 		 */
648e274795eSPeter Zijlstra 		if (__mutex_trylock(lock))
6495bbd7e64SPeter Zijlstra 			goto acquired;
65001768b42SPeter Zijlstra 
65101768b42SPeter Zijlstra 		/*
65255f036caSPeter Ziljstra 		 * Check for signals and kill conditions while holding
6535bbd7e64SPeter Zijlstra 		 * wait_lock. This ensures the lock cancellation is ordered
6545bbd7e64SPeter Zijlstra 		 * against mutex_unlock() and wake-ups do not go missing.
65501768b42SPeter Zijlstra 		 */
6563bb5f4acSDavidlohr Bueso 		if (signal_pending_state(state, current)) {
65701768b42SPeter Zijlstra 			ret = -EINTR;
65801768b42SPeter Zijlstra 			goto err;
65901768b42SPeter Zijlstra 		}
66001768b42SPeter Zijlstra 
6615de2055dSWaiman Long 		if (ww_ctx) {
66255f036caSPeter Ziljstra 			ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
66301768b42SPeter Zijlstra 			if (ret)
66401768b42SPeter Zijlstra 				goto err;
66501768b42SPeter Zijlstra 		}
66601768b42SPeter Zijlstra 
667ebf4c55cSThomas Gleixner 		raw_spin_unlock(&lock->wait_lock);
66801768b42SPeter Zijlstra 		schedule_preempt_disabled();
6699d659ae1SPeter Zijlstra 
6706baa5c60SNicolai Hähnle 		first = __mutex_waiter_is_first(lock, &waiter);
6715bbd7e64SPeter Zijlstra 
672642fa448SDavidlohr Bueso 		set_current_state(state);
6735bbd7e64SPeter Zijlstra 		/*
6745bbd7e64SPeter Zijlstra 		 * Here we order against unlock; we must either see it change
6755bbd7e64SPeter Zijlstra 		 * state back to RUNNING and fall through the next schedule(),
6765bbd7e64SPeter Zijlstra 		 * or we must see its unlock and acquire.
6775bbd7e64SPeter Zijlstra 		 */
678ad90880dSPeter Zijlstra 		if (__mutex_trylock_or_handoff(lock, first) ||
6795de2055dSWaiman Long 		    (first && mutex_optimistic_spin(lock, ww_ctx, &waiter)))
6805bbd7e64SPeter Zijlstra 			break;
6815bbd7e64SPeter Zijlstra 
682ebf4c55cSThomas Gleixner 		raw_spin_lock(&lock->wait_lock);
68301768b42SPeter Zijlstra 	}
684ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
6855bbd7e64SPeter Zijlstra acquired:
686642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
68751587bcfSDavidlohr Bueso 
6885de2055dSWaiman Long 	if (ww_ctx) {
68908295b3bSThomas Hellstrom 		/*
69008295b3bSThomas Hellstrom 		 * Wound-Wait; we stole the lock (!first_waiter), check the
69108295b3bSThomas Hellstrom 		 * waiters as anyone might want to wound us.
69208295b3bSThomas Hellstrom 		 */
69308295b3bSThomas Hellstrom 		if (!ww_ctx->is_wait_die &&
69408295b3bSThomas Hellstrom 		    !__mutex_waiter_is_first(lock, &waiter))
69508295b3bSThomas Hellstrom 			__ww_mutex_check_waiters(lock, ww_ctx);
69608295b3bSThomas Hellstrom 	}
69708295b3bSThomas Hellstrom 
6983a010c49SZqiang 	__mutex_remove_waiter(lock, &waiter);
6993ca0ff57SPeter Zijlstra 
70001768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
70101768b42SPeter Zijlstra 
70201768b42SPeter Zijlstra skip_wait:
70301768b42SPeter Zijlstra 	/* got the lock - cleanup and rejoice! */
70401768b42SPeter Zijlstra 	lock_acquired(&lock->dep_map, ip);
70501768b42SPeter Zijlstra 
7065de2055dSWaiman Long 	if (ww_ctx)
70755f036caSPeter Ziljstra 		ww_mutex_lock_acquired(ww, ww_ctx);
70801768b42SPeter Zijlstra 
709ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
71001768b42SPeter Zijlstra 	preempt_enable();
71101768b42SPeter Zijlstra 	return 0;
71201768b42SPeter Zijlstra 
71301768b42SPeter Zijlstra err:
714642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
7153a010c49SZqiang 	__mutex_remove_waiter(lock, &waiter);
71655f036caSPeter Ziljstra err_early_kill:
717ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
71801768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
7195facae4fSQian Cai 	mutex_release(&lock->dep_map, ip);
72001768b42SPeter Zijlstra 	preempt_enable();
72101768b42SPeter Zijlstra 	return ret;
72201768b42SPeter Zijlstra }
72301768b42SPeter Zijlstra 
724427b1820SPeter Zijlstra static int __sched
7252f064a59SPeter Zijlstra __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
726427b1820SPeter Zijlstra 	     struct lockdep_map *nest_lock, unsigned long ip)
727427b1820SPeter Zijlstra {
728427b1820SPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
729427b1820SPeter Zijlstra }
730427b1820SPeter Zijlstra 
731427b1820SPeter Zijlstra static int __sched
7322f064a59SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
733cf702eddSPeter Zijlstra 		unsigned long ip, struct ww_acquire_ctx *ww_ctx)
734427b1820SPeter Zijlstra {
735cf702eddSPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
736427b1820SPeter Zijlstra }
737427b1820SPeter Zijlstra 
73801768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
73901768b42SPeter Zijlstra void __sched
74001768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass)
74101768b42SPeter Zijlstra {
742427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
74301768b42SPeter Zijlstra }
74401768b42SPeter Zijlstra 
74501768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested);
74601768b42SPeter Zijlstra 
74701768b42SPeter Zijlstra void __sched
74801768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
74901768b42SPeter Zijlstra {
750427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
75101768b42SPeter Zijlstra }
75201768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
75301768b42SPeter Zijlstra 
75401768b42SPeter Zijlstra int __sched
75501768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
75601768b42SPeter Zijlstra {
757427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
75801768b42SPeter Zijlstra }
75901768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
76001768b42SPeter Zijlstra 
76101768b42SPeter Zijlstra int __sched
76201768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
76301768b42SPeter Zijlstra {
764427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
76501768b42SPeter Zijlstra }
76601768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
76701768b42SPeter Zijlstra 
7681460cb65STejun Heo void __sched
7691460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
7701460cb65STejun Heo {
7711460cb65STejun Heo 	int token;
7721460cb65STejun Heo 
7731460cb65STejun Heo 	might_sleep();
7741460cb65STejun Heo 
7751460cb65STejun Heo 	token = io_schedule_prepare();
7761460cb65STejun Heo 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
7771460cb65STejun Heo 			    subclass, NULL, _RET_IP_, NULL, 0);
7781460cb65STejun Heo 	io_schedule_finish(token);
7791460cb65STejun Heo }
7801460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
7811460cb65STejun Heo 
78201768b42SPeter Zijlstra static inline int
78301768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
78401768b42SPeter Zijlstra {
78501768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
78601768b42SPeter Zijlstra 	unsigned tmp;
78701768b42SPeter Zijlstra 
78801768b42SPeter Zijlstra 	if (ctx->deadlock_inject_countdown-- == 0) {
78901768b42SPeter Zijlstra 		tmp = ctx->deadlock_inject_interval;
79001768b42SPeter Zijlstra 		if (tmp > UINT_MAX/4)
79101768b42SPeter Zijlstra 			tmp = UINT_MAX;
79201768b42SPeter Zijlstra 		else
79301768b42SPeter Zijlstra 			tmp = tmp*2 + tmp + tmp/2;
79401768b42SPeter Zijlstra 
79501768b42SPeter Zijlstra 		ctx->deadlock_inject_interval = tmp;
79601768b42SPeter Zijlstra 		ctx->deadlock_inject_countdown = tmp;
79701768b42SPeter Zijlstra 		ctx->contending_lock = lock;
79801768b42SPeter Zijlstra 
79901768b42SPeter Zijlstra 		ww_mutex_unlock(lock);
80001768b42SPeter Zijlstra 
80101768b42SPeter Zijlstra 		return -EDEADLK;
80201768b42SPeter Zijlstra 	}
80301768b42SPeter Zijlstra #endif
80401768b42SPeter Zijlstra 
80501768b42SPeter Zijlstra 	return 0;
80601768b42SPeter Zijlstra }
80701768b42SPeter Zijlstra 
80801768b42SPeter Zijlstra int __sched
809c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
81001768b42SPeter Zijlstra {
81101768b42SPeter Zijlstra 	int ret;
81201768b42SPeter Zijlstra 
81301768b42SPeter Zijlstra 	might_sleep();
814427b1820SPeter Zijlstra 	ret =  __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
815cf702eddSPeter Zijlstra 			       0, _RET_IP_, ctx);
816ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
81701768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
81801768b42SPeter Zijlstra 
81901768b42SPeter Zijlstra 	return ret;
82001768b42SPeter Zijlstra }
821c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock);
82201768b42SPeter Zijlstra 
82301768b42SPeter Zijlstra int __sched
824c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
82501768b42SPeter Zijlstra {
82601768b42SPeter Zijlstra 	int ret;
82701768b42SPeter Zijlstra 
82801768b42SPeter Zijlstra 	might_sleep();
829427b1820SPeter Zijlstra 	ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
830cf702eddSPeter Zijlstra 			      0, _RET_IP_, ctx);
83101768b42SPeter Zijlstra 
832ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
83301768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
83401768b42SPeter Zijlstra 
83501768b42SPeter Zijlstra 	return ret;
83601768b42SPeter Zijlstra }
837c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
83801768b42SPeter Zijlstra 
83901768b42SPeter Zijlstra #endif
84001768b42SPeter Zijlstra 
84101768b42SPeter Zijlstra /*
84201768b42SPeter Zijlstra  * Release the lock, slowpath:
84301768b42SPeter Zijlstra  */
8443ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
84501768b42SPeter Zijlstra {
8469d659ae1SPeter Zijlstra 	struct task_struct *next = NULL;
847194a6b5bSWaiman Long 	DEFINE_WAKE_Q(wake_q);
848b9c16a0eSPeter Zijlstra 	unsigned long owner;
84901768b42SPeter Zijlstra 
8505facae4fSQian Cai 	mutex_release(&lock->dep_map, ip);
8513ca0ff57SPeter Zijlstra 
85201768b42SPeter Zijlstra 	/*
8539d659ae1SPeter Zijlstra 	 * Release the lock before (potentially) taking the spinlock such that
8549d659ae1SPeter Zijlstra 	 * other contenders can get on with things ASAP.
8559d659ae1SPeter Zijlstra 	 *
8569d659ae1SPeter Zijlstra 	 * Except when HANDOFF, in that case we must not clear the owner field,
8579d659ae1SPeter Zijlstra 	 * but instead set it to the top waiter.
85801768b42SPeter Zijlstra 	 */
8599d659ae1SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
8609d659ae1SPeter Zijlstra 	for (;;) {
861e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(__owner_task(owner) != current);
862e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
8639d659ae1SPeter Zijlstra 
8649d659ae1SPeter Zijlstra 		if (owner & MUTEX_FLAG_HANDOFF)
8659d659ae1SPeter Zijlstra 			break;
8669d659ae1SPeter Zijlstra 
867ab4e4d9fSPeter Zijlstra 		if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) {
8689d659ae1SPeter Zijlstra 			if (owner & MUTEX_FLAG_WAITERS)
8699d659ae1SPeter Zijlstra 				break;
8709d659ae1SPeter Zijlstra 
8713ca0ff57SPeter Zijlstra 			return;
8729d659ae1SPeter Zijlstra 		}
8739d659ae1SPeter Zijlstra 	}
87401768b42SPeter Zijlstra 
875ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
8761d8fe7dcSJason Low 	debug_mutex_unlock(lock);
87701768b42SPeter Zijlstra 	if (!list_empty(&lock->wait_list)) {
87801768b42SPeter Zijlstra 		/* get the first entry from the wait-list: */
87901768b42SPeter Zijlstra 		struct mutex_waiter *waiter =
8809d659ae1SPeter Zijlstra 			list_first_entry(&lock->wait_list,
88101768b42SPeter Zijlstra 					 struct mutex_waiter, list);
88201768b42SPeter Zijlstra 
8839d659ae1SPeter Zijlstra 		next = waiter->task;
8849d659ae1SPeter Zijlstra 
88501768b42SPeter Zijlstra 		debug_mutex_wake_waiter(lock, waiter);
8869d659ae1SPeter Zijlstra 		wake_q_add(&wake_q, next);
88701768b42SPeter Zijlstra 	}
88801768b42SPeter Zijlstra 
8899d659ae1SPeter Zijlstra 	if (owner & MUTEX_FLAG_HANDOFF)
8909d659ae1SPeter Zijlstra 		__mutex_handoff(lock, next);
8919d659ae1SPeter Zijlstra 
892ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
8939d659ae1SPeter Zijlstra 
8941329ce6fSDavidlohr Bueso 	wake_up_q(&wake_q);
89501768b42SPeter Zijlstra }
89601768b42SPeter Zijlstra 
89701768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
89801768b42SPeter Zijlstra /*
89901768b42SPeter Zijlstra  * Here come the less common (and hence less performance-critical) APIs:
90001768b42SPeter Zijlstra  * mutex_lock_interruptible() and mutex_trylock().
90101768b42SPeter Zijlstra  */
90201768b42SPeter Zijlstra static noinline int __sched
90301768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock);
90401768b42SPeter Zijlstra 
90501768b42SPeter Zijlstra static noinline int __sched
90601768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock);
90701768b42SPeter Zijlstra 
90801768b42SPeter Zijlstra /**
90945dbac0eSMatthew Wilcox  * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
91045dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
91101768b42SPeter Zijlstra  *
91245dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal is delivered while the
91345dbac0eSMatthew Wilcox  * process is sleeping, this function will return without acquiring the
91445dbac0eSMatthew Wilcox  * mutex.
91501768b42SPeter Zijlstra  *
91645dbac0eSMatthew Wilcox  * Context: Process context.
91745dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
91845dbac0eSMatthew Wilcox  * signal arrived.
91901768b42SPeter Zijlstra  */
92001768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock)
92101768b42SPeter Zijlstra {
92201768b42SPeter Zijlstra 	might_sleep();
9233ca0ff57SPeter Zijlstra 
9243ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
92501768b42SPeter Zijlstra 		return 0;
9263ca0ff57SPeter Zijlstra 
92701768b42SPeter Zijlstra 	return __mutex_lock_interruptible_slowpath(lock);
92801768b42SPeter Zijlstra }
92901768b42SPeter Zijlstra 
93001768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible);
93101768b42SPeter Zijlstra 
93245dbac0eSMatthew Wilcox /**
93345dbac0eSMatthew Wilcox  * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
93445dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
93545dbac0eSMatthew Wilcox  *
93645dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal which will be fatal to
93745dbac0eSMatthew Wilcox  * the current process is delivered while the process is sleeping, this
93845dbac0eSMatthew Wilcox  * function will return without acquiring the mutex.
93945dbac0eSMatthew Wilcox  *
94045dbac0eSMatthew Wilcox  * Context: Process context.
94145dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
94245dbac0eSMatthew Wilcox  * fatal signal arrived.
94345dbac0eSMatthew Wilcox  */
94401768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock)
94501768b42SPeter Zijlstra {
94601768b42SPeter Zijlstra 	might_sleep();
9473ca0ff57SPeter Zijlstra 
9483ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
94901768b42SPeter Zijlstra 		return 0;
9503ca0ff57SPeter Zijlstra 
95101768b42SPeter Zijlstra 	return __mutex_lock_killable_slowpath(lock);
95201768b42SPeter Zijlstra }
95301768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable);
95401768b42SPeter Zijlstra 
95545dbac0eSMatthew Wilcox /**
95645dbac0eSMatthew Wilcox  * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
95745dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
95845dbac0eSMatthew Wilcox  *
95945dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  While the task is waiting for this
96045dbac0eSMatthew Wilcox  * mutex, it will be accounted as being in the IO wait state by the
96145dbac0eSMatthew Wilcox  * scheduler.
96245dbac0eSMatthew Wilcox  *
96345dbac0eSMatthew Wilcox  * Context: Process context.
96445dbac0eSMatthew Wilcox  */
9651460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock)
9661460cb65STejun Heo {
9671460cb65STejun Heo 	int token;
9681460cb65STejun Heo 
9691460cb65STejun Heo 	token = io_schedule_prepare();
9701460cb65STejun Heo 	mutex_lock(lock);
9711460cb65STejun Heo 	io_schedule_finish(token);
9721460cb65STejun Heo }
9731460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io);
9741460cb65STejun Heo 
9753ca0ff57SPeter Zijlstra static noinline void __sched
9763ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock)
97701768b42SPeter Zijlstra {
978427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
97901768b42SPeter Zijlstra }
98001768b42SPeter Zijlstra 
98101768b42SPeter Zijlstra static noinline int __sched
98201768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock)
98301768b42SPeter Zijlstra {
984427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
98501768b42SPeter Zijlstra }
98601768b42SPeter Zijlstra 
98701768b42SPeter Zijlstra static noinline int __sched
98801768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock)
98901768b42SPeter Zijlstra {
990427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
99101768b42SPeter Zijlstra }
99201768b42SPeter Zijlstra 
99301768b42SPeter Zijlstra static noinline int __sched
99401768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
99501768b42SPeter Zijlstra {
996cf702eddSPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0,
997427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
99801768b42SPeter Zijlstra }
99901768b42SPeter Zijlstra 
100001768b42SPeter Zijlstra static noinline int __sched
100101768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
100201768b42SPeter Zijlstra 					    struct ww_acquire_ctx *ctx)
100301768b42SPeter Zijlstra {
1004cf702eddSPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0,
1005427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
100601768b42SPeter Zijlstra }
100701768b42SPeter Zijlstra 
100801768b42SPeter Zijlstra #endif
100901768b42SPeter Zijlstra 
101001768b42SPeter Zijlstra /**
101101768b42SPeter Zijlstra  * mutex_trylock - try to acquire the mutex, without waiting
101201768b42SPeter Zijlstra  * @lock: the mutex to be acquired
101301768b42SPeter Zijlstra  *
101401768b42SPeter Zijlstra  * Try to acquire the mutex atomically. Returns 1 if the mutex
101501768b42SPeter Zijlstra  * has been acquired successfully, and 0 on contention.
101601768b42SPeter Zijlstra  *
101701768b42SPeter Zijlstra  * NOTE: this function follows the spin_trylock() convention, so
101801768b42SPeter Zijlstra  * it is negated from the down_trylock() return values! Be careful
101901768b42SPeter Zijlstra  * about this when converting semaphore users to mutexes.
102001768b42SPeter Zijlstra  *
102101768b42SPeter Zijlstra  * This function must not be used in interrupt context. The
102201768b42SPeter Zijlstra  * mutex must be released by the same task that acquired it.
102301768b42SPeter Zijlstra  */
102401768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock)
102501768b42SPeter Zijlstra {
10266c11c6e3SSebastian Andrzej Siewior 	bool locked;
102701768b42SPeter Zijlstra 
1028e6b4457bSPeter Zijlstra 	MUTEX_WARN_ON(lock->magic != lock);
10296c11c6e3SSebastian Andrzej Siewior 
10306c11c6e3SSebastian Andrzej Siewior 	locked = __mutex_trylock(lock);
10313ca0ff57SPeter Zijlstra 	if (locked)
10323ca0ff57SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
103301768b42SPeter Zijlstra 
10343ca0ff57SPeter Zijlstra 	return locked;
103501768b42SPeter Zijlstra }
103601768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock);
103701768b42SPeter Zijlstra 
103801768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
103901768b42SPeter Zijlstra int __sched
1040c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
104101768b42SPeter Zijlstra {
104201768b42SPeter Zijlstra 	might_sleep();
104301768b42SPeter Zijlstra 
10443ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1045ea9e0fb8SNicolai Hähnle 		if (ctx)
104601768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
10473ca0ff57SPeter Zijlstra 		return 0;
10483ca0ff57SPeter Zijlstra 	}
10493ca0ff57SPeter Zijlstra 
10503ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_slowpath(lock, ctx);
105101768b42SPeter Zijlstra }
1052c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock);
105301768b42SPeter Zijlstra 
105401768b42SPeter Zijlstra int __sched
1055c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
105601768b42SPeter Zijlstra {
105701768b42SPeter Zijlstra 	might_sleep();
105801768b42SPeter Zijlstra 
10593ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1060ea9e0fb8SNicolai Hähnle 		if (ctx)
106101768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
10623ca0ff57SPeter Zijlstra 		return 0;
10633ca0ff57SPeter Zijlstra 	}
10643ca0ff57SPeter Zijlstra 
10653ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
106601768b42SPeter Zijlstra }
1067c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock_interruptible);
106801768b42SPeter Zijlstra 
106901768b42SPeter Zijlstra #endif
107001768b42SPeter Zijlstra 
107101768b42SPeter Zijlstra /**
107201768b42SPeter Zijlstra  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
107301768b42SPeter Zijlstra  * @cnt: the atomic which we are to dec
107401768b42SPeter Zijlstra  * @lock: the mutex to return holding if we dec to 0
107501768b42SPeter Zijlstra  *
107601768b42SPeter Zijlstra  * return true and hold lock if we dec to 0, return false otherwise
107701768b42SPeter Zijlstra  */
107801768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
107901768b42SPeter Zijlstra {
108001768b42SPeter Zijlstra 	/* dec if we can't possibly hit 0 */
108101768b42SPeter Zijlstra 	if (atomic_add_unless(cnt, -1, 1))
108201768b42SPeter Zijlstra 		return 0;
108301768b42SPeter Zijlstra 	/* we might hit 0, so take the lock */
108401768b42SPeter Zijlstra 	mutex_lock(lock);
108501768b42SPeter Zijlstra 	if (!atomic_dec_and_test(cnt)) {
108601768b42SPeter Zijlstra 		/* when we actually did the dec, we didn't hit 0 */
108701768b42SPeter Zijlstra 		mutex_unlock(lock);
108801768b42SPeter Zijlstra 		return 0;
108901768b42SPeter Zijlstra 	}
109001768b42SPeter Zijlstra 	/* we hit 0, and we hold the lock */
109101768b42SPeter Zijlstra 	return 1;
109201768b42SPeter Zijlstra }
109301768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
1094