xref: /openbmc/linux/kernel/locking/mutex.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 
3316edd9b5SNamhyung Kim #define CREATE_TRACE_POINTS
3416edd9b5SNamhyung Kim #include <trace/events/lock.h>
3516edd9b5SNamhyung Kim 
36bb630f9fSThomas Gleixner #ifndef CONFIG_PREEMPT_RT
37a321fb90SThomas Gleixner #include "mutex.h"
38a321fb90SThomas Gleixner 
3901768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
40e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond)
4101768b42SPeter Zijlstra #else
42e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond)
4301768b42SPeter Zijlstra #endif
4401768b42SPeter Zijlstra 
4501768b42SPeter Zijlstra void
__mutex_init(struct mutex * lock,const char * name,struct lock_class_key * key)4601768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
4701768b42SPeter Zijlstra {
483ca0ff57SPeter Zijlstra 	atomic_long_set(&lock->owner, 0);
49ebf4c55cSThomas Gleixner 	raw_spin_lock_init(&lock->wait_lock);
5001768b42SPeter Zijlstra 	INIT_LIST_HEAD(&lock->wait_list);
5101768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
524d9d951eSJason Low 	osq_lock_init(&lock->osq);
5301768b42SPeter Zijlstra #endif
5401768b42SPeter Zijlstra 
5501768b42SPeter Zijlstra 	debug_mutex_init(lock, name, key);
5601768b42SPeter Zijlstra }
5701768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init);
5801768b42SPeter Zijlstra 
593ca0ff57SPeter Zijlstra /*
603ca0ff57SPeter Zijlstra  * @owner: contains: 'struct task_struct *' to the current lock owner,
613ca0ff57SPeter Zijlstra  * NULL means not owned. Since task_struct pointers are aligned at
62e274795eSPeter Zijlstra  * at least L1_CACHE_BYTES, we have low bits to store extra state.
633ca0ff57SPeter Zijlstra  *
643ca0ff57SPeter Zijlstra  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
659d659ae1SPeter Zijlstra  * Bit1 indicates unlock needs to hand the lock to the top-waiter
66e274795eSPeter Zijlstra  * Bit2 indicates handoff has been done and we're waiting for pickup.
673ca0ff57SPeter Zijlstra  */
683ca0ff57SPeter Zijlstra #define MUTEX_FLAG_WAITERS	0x01
699d659ae1SPeter Zijlstra #define MUTEX_FLAG_HANDOFF	0x02
70e274795eSPeter Zijlstra #define MUTEX_FLAG_PICKUP	0x04
713ca0ff57SPeter Zijlstra 
72e274795eSPeter Zijlstra #define MUTEX_FLAGS		0x07
733ca0ff57SPeter Zijlstra 
745f35d5a6SMukesh Ojha /*
755f35d5a6SMukesh Ojha  * Internal helper function; C doesn't allow us to hide it :/
765f35d5a6SMukesh Ojha  *
775f35d5a6SMukesh Ojha  * DO NOT USE (outside of mutex code).
785f35d5a6SMukesh Ojha  */
__mutex_owner(struct mutex * lock)795f35d5a6SMukesh Ojha static inline struct task_struct *__mutex_owner(struct mutex *lock)
805f35d5a6SMukesh Ojha {
81a037d269SMukesh Ojha 	return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
825f35d5a6SMukesh Ojha }
835f35d5a6SMukesh Ojha 
__owner_task(unsigned long owner)843ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner)
853ca0ff57SPeter Zijlstra {
863ca0ff57SPeter Zijlstra 	return (struct task_struct *)(owner & ~MUTEX_FLAGS);
873ca0ff57SPeter Zijlstra }
883ca0ff57SPeter Zijlstra 
mutex_is_locked(struct mutex * lock)895f35d5a6SMukesh Ojha bool mutex_is_locked(struct mutex *lock)
905f35d5a6SMukesh Ojha {
915f35d5a6SMukesh Ojha 	return __mutex_owner(lock) != NULL;
925f35d5a6SMukesh Ojha }
935f35d5a6SMukesh Ojha EXPORT_SYMBOL(mutex_is_locked);
945f35d5a6SMukesh Ojha 
__owner_flags(unsigned long owner)953ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner)
963ca0ff57SPeter Zijlstra {
973ca0ff57SPeter Zijlstra 	return owner & MUTEX_FLAGS;
983ca0ff57SPeter Zijlstra }
993ca0ff57SPeter Zijlstra 
10012235da8SMaarten Lankhorst /*
10112235da8SMaarten Lankhorst  * Returns: __mutex_owner(lock) on failure or NULL on success.
10212235da8SMaarten Lankhorst  */
__mutex_trylock_common(struct mutex * lock,bool handoff)103ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff)
1043ca0ff57SPeter Zijlstra {
1053ca0ff57SPeter Zijlstra 	unsigned long owner, curr = (unsigned long)current;
1063ca0ff57SPeter Zijlstra 
1073ca0ff57SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
1083ca0ff57SPeter Zijlstra 	for (;;) { /* must loop, can race against a flag */
109ab4e4d9fSPeter Zijlstra 		unsigned long flags = __owner_flags(owner);
110e274795eSPeter Zijlstra 		unsigned long task = owner & ~MUTEX_FLAGS;
1113ca0ff57SPeter Zijlstra 
112e274795eSPeter Zijlstra 		if (task) {
113ad90880dSPeter Zijlstra 			if (flags & MUTEX_FLAG_PICKUP) {
114ad90880dSPeter Zijlstra 				if (task != curr)
115e274795eSPeter Zijlstra 					break;
116e274795eSPeter Zijlstra 				flags &= ~MUTEX_FLAG_PICKUP;
117ad90880dSPeter Zijlstra 			} else if (handoff) {
118ad90880dSPeter Zijlstra 				if (flags & MUTEX_FLAG_HANDOFF)
119ad90880dSPeter Zijlstra 					break;
120ad90880dSPeter Zijlstra 				flags |= MUTEX_FLAG_HANDOFF;
121ad90880dSPeter Zijlstra 			} else {
122ad90880dSPeter Zijlstra 				break;
123ad90880dSPeter Zijlstra 			}
124e274795eSPeter Zijlstra 		} else {
125e6b4457bSPeter Zijlstra 			MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP));
126ad90880dSPeter Zijlstra 			task = curr;
1279d659ae1SPeter Zijlstra 		}
1283ca0ff57SPeter Zijlstra 
129ad90880dSPeter Zijlstra 		if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) {
130ad90880dSPeter Zijlstra 			if (task == curr)
131e274795eSPeter Zijlstra 				return NULL;
132ad90880dSPeter Zijlstra 			break;
133ad90880dSPeter Zijlstra 		}
1343ca0ff57SPeter Zijlstra 	}
135e274795eSPeter Zijlstra 
136e274795eSPeter Zijlstra 	return __owner_task(owner);
137e274795eSPeter Zijlstra }
138e274795eSPeter Zijlstra 
139e274795eSPeter Zijlstra /*
140ad90880dSPeter Zijlstra  * Trylock or set HANDOFF
141ad90880dSPeter Zijlstra  */
__mutex_trylock_or_handoff(struct mutex * lock,bool handoff)142ad90880dSPeter Zijlstra static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff)
143ad90880dSPeter Zijlstra {
144ad90880dSPeter Zijlstra 	return !__mutex_trylock_common(lock, handoff);
145ad90880dSPeter Zijlstra }
146ad90880dSPeter Zijlstra 
147ad90880dSPeter Zijlstra /*
148e274795eSPeter Zijlstra  * Actual trylock that will work on any unlocked state.
149e274795eSPeter Zijlstra  */
__mutex_trylock(struct mutex * lock)150e274795eSPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock)
151e274795eSPeter Zijlstra {
152ad90880dSPeter Zijlstra 	return !__mutex_trylock_common(lock, false);
1533ca0ff57SPeter Zijlstra }
1543ca0ff57SPeter Zijlstra 
1553ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
1563ca0ff57SPeter Zijlstra /*
1573ca0ff57SPeter Zijlstra  * Lockdep annotations are contained to the slow paths for simplicity.
1583ca0ff57SPeter Zijlstra  * There is nothing that would stop spreading the lockdep annotations outwards
1593ca0ff57SPeter Zijlstra  * except more code.
1603ca0ff57SPeter Zijlstra  */
1613ca0ff57SPeter Zijlstra 
1623ca0ff57SPeter Zijlstra /*
1633ca0ff57SPeter Zijlstra  * Optimistic trylock that only works in the uncontended case. Make sure to
1643ca0ff57SPeter Zijlstra  * follow with a __mutex_trylock() before failing.
1653ca0ff57SPeter Zijlstra  */
__mutex_trylock_fast(struct mutex * lock)1663ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
1673ca0ff57SPeter Zijlstra {
1683ca0ff57SPeter Zijlstra 	unsigned long curr = (unsigned long)current;
169c427f695SPeter Zijlstra 	unsigned long zero = 0UL;
1703ca0ff57SPeter Zijlstra 
171c427f695SPeter Zijlstra 	if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
1723ca0ff57SPeter Zijlstra 		return true;
1733ca0ff57SPeter Zijlstra 
1743ca0ff57SPeter Zijlstra 	return false;
1753ca0ff57SPeter Zijlstra }
1763ca0ff57SPeter Zijlstra 
__mutex_unlock_fast(struct mutex * lock)1773ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
1783ca0ff57SPeter Zijlstra {
1793ca0ff57SPeter Zijlstra 	unsigned long curr = (unsigned long)current;
1803ca0ff57SPeter Zijlstra 
181ab4e4d9fSPeter Zijlstra 	return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL);
1823ca0ff57SPeter Zijlstra }
1833ca0ff57SPeter Zijlstra #endif
1843ca0ff57SPeter Zijlstra 
__mutex_set_flag(struct mutex * lock,unsigned long flag)1853ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
1863ca0ff57SPeter Zijlstra {
1873ca0ff57SPeter Zijlstra 	atomic_long_or(flag, &lock->owner);
1883ca0ff57SPeter Zijlstra }
1893ca0ff57SPeter Zijlstra 
__mutex_clear_flag(struct mutex * lock,unsigned long flag)1903ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
1913ca0ff57SPeter Zijlstra {
1923ca0ff57SPeter Zijlstra 	atomic_long_andnot(flag, &lock->owner);
1933ca0ff57SPeter Zijlstra }
1943ca0ff57SPeter Zijlstra 
__mutex_waiter_is_first(struct mutex * lock,struct mutex_waiter * waiter)1959d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
1969d659ae1SPeter Zijlstra {
1979d659ae1SPeter Zijlstra 	return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
1989d659ae1SPeter Zijlstra }
1999d659ae1SPeter Zijlstra 
2009d659ae1SPeter Zijlstra /*
20108295b3bSThomas Hellstrom  * Add @waiter to a given location in the lock wait_list and set the
20208295b3bSThomas Hellstrom  * FLAG_WAITERS flag if it's the first waiter.
20308295b3bSThomas Hellstrom  */
2043a010c49SZqiang static void
__mutex_add_waiter(struct mutex * lock,struct mutex_waiter * waiter,struct list_head * list)20508295b3bSThomas Hellstrom __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
20608295b3bSThomas Hellstrom 		   struct list_head *list)
20708295b3bSThomas Hellstrom {
20808295b3bSThomas Hellstrom 	debug_mutex_add_waiter(lock, waiter, current);
20908295b3bSThomas Hellstrom 
21008295b3bSThomas Hellstrom 	list_add_tail(&waiter->list, list);
21108295b3bSThomas Hellstrom 	if (__mutex_waiter_is_first(lock, waiter))
21208295b3bSThomas Hellstrom 		__mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
21308295b3bSThomas Hellstrom }
21408295b3bSThomas Hellstrom 
2153a010c49SZqiang static void
__mutex_remove_waiter(struct mutex * lock,struct mutex_waiter * waiter)2163a010c49SZqiang __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter)
2173a010c49SZqiang {
2183a010c49SZqiang 	list_del(&waiter->list);
2193a010c49SZqiang 	if (likely(list_empty(&lock->wait_list)))
2203a010c49SZqiang 		__mutex_clear_flag(lock, MUTEX_FLAGS);
2213a010c49SZqiang 
2223a010c49SZqiang 	debug_mutex_remove_waiter(lock, waiter, current);
2233a010c49SZqiang }
2243a010c49SZqiang 
22508295b3bSThomas Hellstrom /*
2269d659ae1SPeter Zijlstra  * Give up ownership to a specific task, when @task = NULL, this is equivalent
227e2db7592SIngo Molnar  * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves
228e274795eSPeter Zijlstra  * WAITERS. Provides RELEASE semantics like a regular unlock, the
229e274795eSPeter Zijlstra  * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
2309d659ae1SPeter Zijlstra  */
__mutex_handoff(struct mutex * lock,struct task_struct * task)2319d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
2329d659ae1SPeter Zijlstra {
2339d659ae1SPeter Zijlstra 	unsigned long owner = atomic_long_read(&lock->owner);
2349d659ae1SPeter Zijlstra 
2359d659ae1SPeter Zijlstra 	for (;;) {
236ab4e4d9fSPeter Zijlstra 		unsigned long new;
2379d659ae1SPeter Zijlstra 
238e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(__owner_task(owner) != current);
239e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
2409d659ae1SPeter Zijlstra 
2419d659ae1SPeter Zijlstra 		new = (owner & MUTEX_FLAG_WAITERS);
2429d659ae1SPeter Zijlstra 		new |= (unsigned long)task;
243e274795eSPeter Zijlstra 		if (task)
244e274795eSPeter Zijlstra 			new |= MUTEX_FLAG_PICKUP;
2459d659ae1SPeter Zijlstra 
246ab4e4d9fSPeter Zijlstra 		if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new))
2479d659ae1SPeter Zijlstra 			break;
2489d659ae1SPeter Zijlstra 	}
2499d659ae1SPeter Zijlstra }
2509d659ae1SPeter Zijlstra 
25101768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
25201768b42SPeter Zijlstra /*
25301768b42SPeter Zijlstra  * We split the mutex lock/unlock logic into separate fastpath and
25401768b42SPeter Zijlstra  * slowpath functions, to reduce the register pressure on the fastpath.
25501768b42SPeter Zijlstra  * We also put the fastpath first in the kernel image, to make sure the
25601768b42SPeter Zijlstra  * branch is predicted by the CPU as default-untaken.
25701768b42SPeter Zijlstra  */
2583ca0ff57SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock);
25901768b42SPeter Zijlstra 
26001768b42SPeter Zijlstra /**
26101768b42SPeter Zijlstra  * mutex_lock - acquire the mutex
26201768b42SPeter Zijlstra  * @lock: the mutex to be acquired
26301768b42SPeter Zijlstra  *
26401768b42SPeter Zijlstra  * Lock the mutex exclusively for this task. If the mutex is not
26501768b42SPeter Zijlstra  * available right now, it will sleep until it can get it.
26601768b42SPeter Zijlstra  *
26701768b42SPeter Zijlstra  * The mutex must later on be released by the same task that
26801768b42SPeter Zijlstra  * acquired it. Recursive locking is not allowed. The task
26901768b42SPeter Zijlstra  * may not exit without first unlocking the mutex. Also, kernel
270139b6fd2SSharon Dvir  * memory where the mutex resides must not be freed with
27101768b42SPeter Zijlstra  * the mutex still locked. The mutex must first be initialized
27201768b42SPeter Zijlstra  * (or statically defined) before it can be locked. memset()-ing
27301768b42SPeter Zijlstra  * the mutex to 0 is not allowed.
27401768b42SPeter Zijlstra  *
27501768b42SPeter Zijlstra  * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
27601768b42SPeter Zijlstra  * checks that will enforce the restrictions and will also do
2777b4ff1adSMauro Carvalho Chehab  * deadlock debugging)
27801768b42SPeter Zijlstra  *
27901768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down().
28001768b42SPeter Zijlstra  */
mutex_lock(struct mutex * lock)28101768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock)
28201768b42SPeter Zijlstra {
28301768b42SPeter Zijlstra 	might_sleep();
28401768b42SPeter Zijlstra 
2853ca0ff57SPeter Zijlstra 	if (!__mutex_trylock_fast(lock))
2863ca0ff57SPeter Zijlstra 		__mutex_lock_slowpath(lock);
2873ca0ff57SPeter Zijlstra }
28801768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock);
28901768b42SPeter Zijlstra #endif
29001768b42SPeter Zijlstra 
2912674bd18SPeter Zijlstra (Intel) #include "ww_mutex.h"
29276916515SDavidlohr Bueso 
29301768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
294c516df97SNicolai Hähnle 
295ad90880dSPeter Zijlstra /*
296ad90880dSPeter Zijlstra  * Trylock variant that returns the owning task on failure.
297ad90880dSPeter Zijlstra  */
__mutex_trylock_or_owner(struct mutex * lock)298ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
299ad90880dSPeter Zijlstra {
300ad90880dSPeter Zijlstra 	return __mutex_trylock_common(lock, false);
301ad90880dSPeter Zijlstra }
302ad90880dSPeter Zijlstra 
303c516df97SNicolai Hähnle static inline
ww_mutex_spin_on_owner(struct mutex * lock,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)304c516df97SNicolai Hähnle bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
305c516df97SNicolai Hähnle 			    struct mutex_waiter *waiter)
306c516df97SNicolai Hähnle {
307c516df97SNicolai Hähnle 	struct ww_mutex *ww;
308c516df97SNicolai Hähnle 
309c516df97SNicolai Hähnle 	ww = container_of(lock, struct ww_mutex, base);
310c516df97SNicolai Hähnle 
31101768b42SPeter Zijlstra 	/*
312c516df97SNicolai Hähnle 	 * If ww->ctx is set the contents are undefined, only
313c516df97SNicolai Hähnle 	 * by acquiring wait_lock there is a guarantee that
314c516df97SNicolai Hähnle 	 * they are not invalid when reading.
315c516df97SNicolai Hähnle 	 *
316c516df97SNicolai Hähnle 	 * As such, when deadlock detection needs to be
317c516df97SNicolai Hähnle 	 * performed the optimistic spinning cannot be done.
318c516df97SNicolai Hähnle 	 *
319c516df97SNicolai Hähnle 	 * Check this in every inner iteration because we may
320c516df97SNicolai Hähnle 	 * be racing against another thread's ww_mutex_lock.
321c516df97SNicolai Hähnle 	 */
322c516df97SNicolai Hähnle 	if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
323c516df97SNicolai Hähnle 		return false;
324c516df97SNicolai Hähnle 
325c516df97SNicolai Hähnle 	/*
326c516df97SNicolai Hähnle 	 * If we aren't on the wait list yet, cancel the spin
327c516df97SNicolai Hähnle 	 * if there are waiters. We want  to avoid stealing the
328c516df97SNicolai Hähnle 	 * lock from a waiter with an earlier stamp, since the
329c516df97SNicolai Hähnle 	 * other thread may already own a lock that we also
330c516df97SNicolai Hähnle 	 * need.
331c516df97SNicolai Hähnle 	 */
332c516df97SNicolai Hähnle 	if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
333c516df97SNicolai Hähnle 		return false;
334c516df97SNicolai Hähnle 
335c516df97SNicolai Hähnle 	/*
336c516df97SNicolai Hähnle 	 * Similarly, stop spinning if we are no longer the
337c516df97SNicolai Hähnle 	 * first waiter.
338c516df97SNicolai Hähnle 	 */
339c516df97SNicolai Hähnle 	if (waiter && !__mutex_waiter_is_first(lock, waiter))
340c516df97SNicolai Hähnle 		return false;
341c516df97SNicolai Hähnle 
342c516df97SNicolai Hähnle 	return true;
343c516df97SNicolai Hähnle }
344c516df97SNicolai Hähnle 
34501768b42SPeter Zijlstra /*
34625f13b40SNicolai Hähnle  * Look out! "owner" is an entirely speculative pointer access and not
34725f13b40SNicolai Hähnle  * reliable.
34825f13b40SNicolai Hähnle  *
34925f13b40SNicolai Hähnle  * "noinline" so that this function shows up on perf profiles.
35001768b42SPeter Zijlstra  */
35101768b42SPeter Zijlstra static noinline
mutex_spin_on_owner(struct mutex * lock,struct task_struct * owner,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)35225f13b40SNicolai Hähnle bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
353c516df97SNicolai Hähnle 			 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
35401768b42SPeter Zijlstra {
35501ac33c1SJason Low 	bool ret = true;
356be1f7bf2SJason Low 
3576c2787f2SYanfei Xu 	lockdep_assert_preemption_disabled();
3586c2787f2SYanfei Xu 
3593ca0ff57SPeter Zijlstra 	while (__mutex_owner(lock) == owner) {
360be1f7bf2SJason Low 		/*
361be1f7bf2SJason Low 		 * Ensure we emit the owner->on_cpu, dereference _after_
3626c2787f2SYanfei Xu 		 * checking lock->owner still matches owner. And we already
3636c2787f2SYanfei Xu 		 * disabled preemption which is equal to the RCU read-side
3646c2787f2SYanfei Xu 		 * crital section in optimistic spinning code. Thus the
3656c2787f2SYanfei Xu 		 * task_strcut structure won't go away during the spinning
3666c2787f2SYanfei Xu 		 * period
367be1f7bf2SJason Low 		 */
368be1f7bf2SJason Low 		barrier();
369be1f7bf2SJason Low 
37005ffc951SPan Xinhui 		/*
37105ffc951SPan Xinhui 		 * Use vcpu_is_preempted to detect lock holder preemption issue.
37205ffc951SPan Xinhui 		 */
373c0bed69dSKefeng Wang 		if (!owner_on_cpu(owner) || need_resched()) {
374be1f7bf2SJason Low 			ret = false;
375be1f7bf2SJason Low 			break;
376be1f7bf2SJason Low 		}
37701768b42SPeter Zijlstra 
378c516df97SNicolai Hähnle 		if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
37925f13b40SNicolai Hähnle 			ret = false;
38025f13b40SNicolai Hähnle 			break;
38125f13b40SNicolai Hähnle 		}
38225f13b40SNicolai Hähnle 
383f2f09a4cSChristian Borntraeger 		cpu_relax();
38401768b42SPeter Zijlstra 	}
38501768b42SPeter Zijlstra 
386be1f7bf2SJason Low 	return ret;
38701768b42SPeter Zijlstra }
38801768b42SPeter Zijlstra 
38901768b42SPeter Zijlstra /*
39001768b42SPeter Zijlstra  * Initial check for entering the mutex spinning loop
39101768b42SPeter Zijlstra  */
mutex_can_spin_on_owner(struct mutex * lock)39201768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock)
39301768b42SPeter Zijlstra {
39401768b42SPeter Zijlstra 	struct task_struct *owner;
39501768b42SPeter Zijlstra 	int retval = 1;
39601768b42SPeter Zijlstra 
3976c2787f2SYanfei Xu 	lockdep_assert_preemption_disabled();
3986c2787f2SYanfei Xu 
39946af29e4SJason Low 	if (need_resched())
40046af29e4SJason Low 		return 0;
40146af29e4SJason Low 
4026c2787f2SYanfei Xu 	/*
4036c2787f2SYanfei Xu 	 * We already disabled preemption which is equal to the RCU read-side
4046c2787f2SYanfei Xu 	 * crital section in optimistic spinning code. Thus the task_strcut
4056c2787f2SYanfei Xu 	 * structure won't go away during the spinning period.
4066c2787f2SYanfei Xu 	 */
4073ca0ff57SPeter Zijlstra 	owner = __mutex_owner(lock);
40801768b42SPeter Zijlstra 	if (owner)
409c0bed69dSKefeng Wang 		retval = owner_on_cpu(owner);
41076916515SDavidlohr Bueso 
41176916515SDavidlohr Bueso 	/*
4123ca0ff57SPeter Zijlstra 	 * If lock->owner is not set, the mutex has been released. Return true
4133ca0ff57SPeter Zijlstra 	 * such that we'll trylock in the spin path, which is a faster option
4143ca0ff57SPeter Zijlstra 	 * than the blocking slow path.
41576916515SDavidlohr Bueso 	 */
4163ca0ff57SPeter Zijlstra 	return retval;
41776916515SDavidlohr Bueso }
41876916515SDavidlohr Bueso 
41976916515SDavidlohr Bueso /*
42076916515SDavidlohr Bueso  * Optimistic spinning.
42176916515SDavidlohr Bueso  *
42276916515SDavidlohr Bueso  * We try to spin for acquisition when we find that the lock owner
42376916515SDavidlohr Bueso  * is currently running on a (different) CPU and while we don't
42476916515SDavidlohr Bueso  * need to reschedule. The rationale is that if the lock owner is
42576916515SDavidlohr Bueso  * running, it is likely to release the lock soon.
42676916515SDavidlohr Bueso  *
42776916515SDavidlohr Bueso  * The mutex spinners are queued up using MCS lock so that only one
42876916515SDavidlohr Bueso  * spinner can compete for the mutex. However, if mutex spinning isn't
42976916515SDavidlohr Bueso  * going to happen, there is no point in going through the lock/unlock
43076916515SDavidlohr Bueso  * overhead.
43176916515SDavidlohr Bueso  *
43276916515SDavidlohr Bueso  * Returns true when the lock was taken, otherwise false, indicating
43376916515SDavidlohr Bueso  * that we need to jump to the slowpath and sleep.
434b341afb3SWaiman Long  *
435b341afb3SWaiman Long  * The waiter flag is set to true if the spinner is a waiter in the wait
436b341afb3SWaiman Long  * queue. The waiter-spinner will spin on the lock directly and concurrently
437b341afb3SWaiman Long  * with the spinner at the head of the OSQ, if present, until the owner is
438b341afb3SWaiman Long  * changed to itself.
43976916515SDavidlohr Bueso  */
440427b1820SPeter Zijlstra static __always_inline bool
mutex_optimistic_spin(struct mutex * lock,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)441427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
4425de2055dSWaiman Long 		      struct mutex_waiter *waiter)
44376916515SDavidlohr Bueso {
444b341afb3SWaiman Long 	if (!waiter) {
445b341afb3SWaiman Long 		/*
446b341afb3SWaiman Long 		 * The purpose of the mutex_can_spin_on_owner() function is
447b341afb3SWaiman Long 		 * to eliminate the overhead of osq_lock() and osq_unlock()
448b341afb3SWaiman Long 		 * in case spinning isn't possible. As a waiter-spinner
449b341afb3SWaiman Long 		 * is not going to take OSQ lock anyway, there is no need
450b341afb3SWaiman Long 		 * to call mutex_can_spin_on_owner().
451b341afb3SWaiman Long 		 */
45276916515SDavidlohr Bueso 		if (!mutex_can_spin_on_owner(lock))
453b341afb3SWaiman Long 			goto fail;
45476916515SDavidlohr Bueso 
455e42f678aSDavidlohr Bueso 		/*
456e42f678aSDavidlohr Bueso 		 * In order to avoid a stampede of mutex spinners trying to
457e42f678aSDavidlohr Bueso 		 * acquire the mutex all at once, the spinners need to take a
458e42f678aSDavidlohr Bueso 		 * MCS (queued) lock first before spinning on the owner field.
459e42f678aSDavidlohr Bueso 		 */
46076916515SDavidlohr Bueso 		if (!osq_lock(&lock->osq))
461b341afb3SWaiman Long 			goto fail;
462b341afb3SWaiman Long 	}
46376916515SDavidlohr Bueso 
464b341afb3SWaiman Long 	for (;;) {
46576916515SDavidlohr Bueso 		struct task_struct *owner;
46676916515SDavidlohr Bueso 
467e274795eSPeter Zijlstra 		/* Try to acquire the mutex... */
468e274795eSPeter Zijlstra 		owner = __mutex_trylock_or_owner(lock);
469e274795eSPeter Zijlstra 		if (!owner)
470e274795eSPeter Zijlstra 			break;
47176916515SDavidlohr Bueso 
47276916515SDavidlohr Bueso 		/*
473e274795eSPeter Zijlstra 		 * There's an owner, wait for it to either
47476916515SDavidlohr Bueso 		 * release the lock or go to sleep.
47576916515SDavidlohr Bueso 		 */
476c516df97SNicolai Hähnle 		if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
477b341afb3SWaiman Long 			goto fail_unlock;
47876916515SDavidlohr Bueso 
47976916515SDavidlohr Bueso 		/*
48076916515SDavidlohr Bueso 		 * The cpu_relax() call is a compiler barrier which forces
48176916515SDavidlohr Bueso 		 * everything in this loop to be re-loaded. We don't need
48276916515SDavidlohr Bueso 		 * memory barriers as we'll eventually observe the right
48376916515SDavidlohr Bueso 		 * values at the cost of a few extra spins.
48476916515SDavidlohr Bueso 		 */
485f2f09a4cSChristian Borntraeger 		cpu_relax();
48676916515SDavidlohr Bueso 	}
48776916515SDavidlohr Bueso 
488b341afb3SWaiman Long 	if (!waiter)
48976916515SDavidlohr Bueso 		osq_unlock(&lock->osq);
490b341afb3SWaiman Long 
491b341afb3SWaiman Long 	return true;
492b341afb3SWaiman Long 
493b341afb3SWaiman Long 
494b341afb3SWaiman Long fail_unlock:
495b341afb3SWaiman Long 	if (!waiter)
496b341afb3SWaiman Long 		osq_unlock(&lock->osq);
497b341afb3SWaiman Long 
498b341afb3SWaiman Long fail:
49976916515SDavidlohr Bueso 	/*
50076916515SDavidlohr Bueso 	 * If we fell out of the spin path because of need_resched(),
50176916515SDavidlohr Bueso 	 * reschedule now, before we try-lock the mutex. This avoids getting
50276916515SDavidlohr Bueso 	 * scheduled out right after we obtained the mutex.
50376916515SDavidlohr Bueso 	 */
5046f942a1fSPeter Zijlstra 	if (need_resched()) {
5056f942a1fSPeter Zijlstra 		/*
5066f942a1fSPeter Zijlstra 		 * We _should_ have TASK_RUNNING here, but just in case
5076f942a1fSPeter Zijlstra 		 * we do not, make it so, otherwise we might get stuck.
5086f942a1fSPeter Zijlstra 		 */
5096f942a1fSPeter Zijlstra 		__set_current_state(TASK_RUNNING);
51076916515SDavidlohr Bueso 		schedule_preempt_disabled();
5116f942a1fSPeter Zijlstra 	}
51276916515SDavidlohr Bueso 
51376916515SDavidlohr Bueso 	return false;
51476916515SDavidlohr Bueso }
51576916515SDavidlohr Bueso #else
516427b1820SPeter Zijlstra static __always_inline bool
mutex_optimistic_spin(struct mutex * lock,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)517427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
5185de2055dSWaiman Long 		      struct mutex_waiter *waiter)
51976916515SDavidlohr Bueso {
52076916515SDavidlohr Bueso 	return false;
52176916515SDavidlohr Bueso }
52201768b42SPeter Zijlstra #endif
52301768b42SPeter Zijlstra 
5243ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
52501768b42SPeter Zijlstra 
52601768b42SPeter Zijlstra /**
52701768b42SPeter Zijlstra  * mutex_unlock - release the mutex
52801768b42SPeter Zijlstra  * @lock: the mutex to be released
52901768b42SPeter Zijlstra  *
53001768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously.
53101768b42SPeter Zijlstra  *
53201768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
53301768b42SPeter Zijlstra  * of a not locked mutex is not allowed.
53401768b42SPeter Zijlstra  *
53501768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) up().
53601768b42SPeter Zijlstra  */
mutex_unlock(struct mutex * lock)53701768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock)
53801768b42SPeter Zijlstra {
5393ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
5403ca0ff57SPeter Zijlstra 	if (__mutex_unlock_fast(lock))
5413ca0ff57SPeter Zijlstra 		return;
54201768b42SPeter Zijlstra #endif
5433ca0ff57SPeter Zijlstra 	__mutex_unlock_slowpath(lock, _RET_IP_);
54401768b42SPeter Zijlstra }
54501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock);
54601768b42SPeter Zijlstra 
54701768b42SPeter Zijlstra /**
54801768b42SPeter Zijlstra  * ww_mutex_unlock - release the w/w mutex
54901768b42SPeter Zijlstra  * @lock: the mutex to be released
55001768b42SPeter Zijlstra  *
55101768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously with any of the
55201768b42SPeter Zijlstra  * ww_mutex_lock* functions (with or without an acquire context). It is
55301768b42SPeter Zijlstra  * forbidden to release the locks after releasing the acquire context.
55401768b42SPeter Zijlstra  *
55501768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
55601768b42SPeter Zijlstra  * of a unlocked mutex is not allowed.
55701768b42SPeter Zijlstra  */
ww_mutex_unlock(struct ww_mutex * lock)55801768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock)
55901768b42SPeter Zijlstra {
560aaa77de1SPeter Zijlstra (Intel) 	__ww_mutex_unlock(lock);
5613ca0ff57SPeter Zijlstra 	mutex_unlock(&lock->base);
56201768b42SPeter Zijlstra }
56301768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock);
56401768b42SPeter Zijlstra 
56501768b42SPeter Zijlstra /*
56601768b42SPeter Zijlstra  * Lock a mutex (possibly interruptible), slowpath:
56701768b42SPeter Zijlstra  */
56801768b42SPeter Zijlstra static __always_inline int __sched
__mutex_lock_common(struct mutex * lock,unsigned int state,unsigned int subclass,struct lockdep_map * nest_lock,unsigned long ip,struct ww_acquire_ctx * ww_ctx,const bool use_ww_ctx)5692f064a59SPeter Zijlstra __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass,
57001768b42SPeter Zijlstra 		    struct lockdep_map *nest_lock, unsigned long ip,
57101768b42SPeter Zijlstra 		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
57201768b42SPeter Zijlstra {
57301768b42SPeter Zijlstra 	struct mutex_waiter waiter;
574a40ca565SWaiman Long 	struct ww_mutex *ww;
57501768b42SPeter Zijlstra 	int ret;
57601768b42SPeter Zijlstra 
5775de2055dSWaiman Long 	if (!use_ww_ctx)
5785de2055dSWaiman Long 		ww_ctx = NULL;
5795de2055dSWaiman Long 
580427b1820SPeter Zijlstra 	might_sleep();
581ea9e0fb8SNicolai Hähnle 
582e6b4457bSPeter Zijlstra 	MUTEX_WARN_ON(lock->magic != lock);
5836c11c6e3SSebastian Andrzej Siewior 
584a40ca565SWaiman Long 	ww = container_of(lock, struct ww_mutex, base);
5855de2055dSWaiman Long 	if (ww_ctx) {
5860422e83dSChris Wilson 		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
5870422e83dSChris Wilson 			return -EALREADY;
58808295b3bSThomas Hellstrom 
58908295b3bSThomas Hellstrom 		/*
59008295b3bSThomas Hellstrom 		 * Reset the wounded flag after a kill. No other process can
59108295b3bSThomas Hellstrom 		 * race and wound us here since they can't have a valid owner
59208295b3bSThomas Hellstrom 		 * pointer if we don't have any locks held.
59308295b3bSThomas Hellstrom 		 */
59408295b3bSThomas Hellstrom 		if (ww_ctx->acquired == 0)
59508295b3bSThomas Hellstrom 			ww_ctx->wounded = 0;
596cf702eddSPeter Zijlstra 
597cf702eddSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
598cf702eddSPeter Zijlstra 		nest_lock = &ww_ctx->dep_map;
599cf702eddSPeter Zijlstra #endif
6000422e83dSChris Wilson 	}
6010422e83dSChris Wilson 
60201768b42SPeter Zijlstra 	preempt_disable();
60301768b42SPeter Zijlstra 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
60401768b42SPeter Zijlstra 
605*dc1f7893SPeter Zijlstra 	trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN);
606e274795eSPeter Zijlstra 	if (__mutex_trylock(lock) ||
6075de2055dSWaiman Long 	    mutex_optimistic_spin(lock, ww_ctx, NULL)) {
60876916515SDavidlohr Bueso 		/* got the lock, yay! */
6093ca0ff57SPeter Zijlstra 		lock_acquired(&lock->dep_map, ip);
6105de2055dSWaiman Long 		if (ww_ctx)
6113ca0ff57SPeter Zijlstra 			ww_mutex_set_context_fastpath(ww, ww_ctx);
612*dc1f7893SPeter Zijlstra 		trace_contention_end(lock, 0);
61301768b42SPeter Zijlstra 		preempt_enable();
61401768b42SPeter Zijlstra 		return 0;
61501768b42SPeter Zijlstra 	}
61601768b42SPeter Zijlstra 
617ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
6181e820c96SJason Low 	/*
6193ca0ff57SPeter Zijlstra 	 * After waiting to acquire the wait_lock, try again.
6201e820c96SJason Low 	 */
621659cf9f5SNicolai Hähnle 	if (__mutex_trylock(lock)) {
6225de2055dSWaiman Long 		if (ww_ctx)
62355f036caSPeter Ziljstra 			__ww_mutex_check_waiters(lock, ww_ctx);
624659cf9f5SNicolai Hähnle 
62501768b42SPeter Zijlstra 		goto skip_wait;
626659cf9f5SNicolai Hähnle 	}
62701768b42SPeter Zijlstra 
62801768b42SPeter Zijlstra 	debug_mutex_lock_common(lock, &waiter);
629c0afb0ffSPeter Zijlstra 	waiter.task = current;
630b857174eSSebastian Andrzej Siewior 	if (use_ww_ctx)
631c0afb0ffSPeter Zijlstra 		waiter.ww_ctx = ww_ctx;
63201768b42SPeter Zijlstra 
6336baa5c60SNicolai Hähnle 	lock_contended(&lock->dep_map, ip);
6346baa5c60SNicolai Hähnle 
6356baa5c60SNicolai Hähnle 	if (!use_ww_ctx) {
63601768b42SPeter Zijlstra 		/* add waiting tasks to the end of the waitqueue (FIFO): */
63708295b3bSThomas Hellstrom 		__mutex_add_waiter(lock, &waiter, &lock->wait_list);
6386baa5c60SNicolai Hähnle 	} else {
63955f036caSPeter Ziljstra 		/*
64055f036caSPeter Ziljstra 		 * Add in stamp order, waking up waiters that must kill
64155f036caSPeter Ziljstra 		 * themselves.
64255f036caSPeter Ziljstra 		 */
6436baa5c60SNicolai Hähnle 		ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
6446baa5c60SNicolai Hähnle 		if (ret)
64555f036caSPeter Ziljstra 			goto err_early_kill;
6466baa5c60SNicolai Hähnle 	}
6476baa5c60SNicolai Hähnle 
648642fa448SDavidlohr Bueso 	set_current_state(state);
649*dc1f7893SPeter Zijlstra 	trace_contention_begin(lock, LCB_F_MUTEX);
65001768b42SPeter Zijlstra 	for (;;) {
651048661a1SPeter Zijlstra 		bool first;
652048661a1SPeter Zijlstra 
6535bbd7e64SPeter Zijlstra 		/*
6545bbd7e64SPeter Zijlstra 		 * Once we hold wait_lock, we're serialized against
6555bbd7e64SPeter Zijlstra 		 * mutex_unlock() handing the lock off to us, do a trylock
6565bbd7e64SPeter Zijlstra 		 * before testing the error conditions to make sure we pick up
6575bbd7e64SPeter Zijlstra 		 * the handoff.
6585bbd7e64SPeter Zijlstra 		 */
659e274795eSPeter Zijlstra 		if (__mutex_trylock(lock))
6605bbd7e64SPeter Zijlstra 			goto acquired;
66101768b42SPeter Zijlstra 
66201768b42SPeter Zijlstra 		/*
66355f036caSPeter Ziljstra 		 * Check for signals and kill conditions while holding
6645bbd7e64SPeter Zijlstra 		 * wait_lock. This ensures the lock cancellation is ordered
6655bbd7e64SPeter Zijlstra 		 * against mutex_unlock() and wake-ups do not go missing.
66601768b42SPeter Zijlstra 		 */
6673bb5f4acSDavidlohr Bueso 		if (signal_pending_state(state, current)) {
66801768b42SPeter Zijlstra 			ret = -EINTR;
66901768b42SPeter Zijlstra 			goto err;
67001768b42SPeter Zijlstra 		}
67101768b42SPeter Zijlstra 
6725de2055dSWaiman Long 		if (ww_ctx) {
67355f036caSPeter Ziljstra 			ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
67401768b42SPeter Zijlstra 			if (ret)
67501768b42SPeter Zijlstra 				goto err;
67601768b42SPeter Zijlstra 		}
67701768b42SPeter Zijlstra 
678ebf4c55cSThomas Gleixner 		raw_spin_unlock(&lock->wait_lock);
67901768b42SPeter Zijlstra 		schedule_preempt_disabled();
6809d659ae1SPeter Zijlstra 
6816baa5c60SNicolai Hähnle 		first = __mutex_waiter_is_first(lock, &waiter);
6825bbd7e64SPeter Zijlstra 
683642fa448SDavidlohr Bueso 		set_current_state(state);
6845bbd7e64SPeter Zijlstra 		/*
6855bbd7e64SPeter Zijlstra 		 * Here we order against unlock; we must either see it change
6865bbd7e64SPeter Zijlstra 		 * state back to RUNNING and fall through the next schedule(),
6875bbd7e64SPeter Zijlstra 		 * or we must see its unlock and acquire.
6885bbd7e64SPeter Zijlstra 		 */
689*dc1f7893SPeter Zijlstra 		if (__mutex_trylock_or_handoff(lock, first))
6905bbd7e64SPeter Zijlstra 			break;
6915bbd7e64SPeter Zijlstra 
692*dc1f7893SPeter Zijlstra 		if (first) {
693*dc1f7893SPeter Zijlstra 			trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN);
694*dc1f7893SPeter Zijlstra 			if (mutex_optimistic_spin(lock, ww_ctx, &waiter))
695*dc1f7893SPeter Zijlstra 				break;
696*dc1f7893SPeter Zijlstra 			trace_contention_begin(lock, LCB_F_MUTEX);
697*dc1f7893SPeter Zijlstra 		}
698*dc1f7893SPeter Zijlstra 
699ebf4c55cSThomas Gleixner 		raw_spin_lock(&lock->wait_lock);
70001768b42SPeter Zijlstra 	}
701ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
7025bbd7e64SPeter Zijlstra acquired:
703642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
70451587bcfSDavidlohr Bueso 
7055de2055dSWaiman Long 	if (ww_ctx) {
70608295b3bSThomas Hellstrom 		/*
70708295b3bSThomas Hellstrom 		 * Wound-Wait; we stole the lock (!first_waiter), check the
70808295b3bSThomas Hellstrom 		 * waiters as anyone might want to wound us.
70908295b3bSThomas Hellstrom 		 */
71008295b3bSThomas Hellstrom 		if (!ww_ctx->is_wait_die &&
71108295b3bSThomas Hellstrom 		    !__mutex_waiter_is_first(lock, &waiter))
71208295b3bSThomas Hellstrom 			__ww_mutex_check_waiters(lock, ww_ctx);
71308295b3bSThomas Hellstrom 	}
71408295b3bSThomas Hellstrom 
7153a010c49SZqiang 	__mutex_remove_waiter(lock, &waiter);
7163ca0ff57SPeter Zijlstra 
71701768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
71801768b42SPeter Zijlstra 
71901768b42SPeter Zijlstra skip_wait:
72001768b42SPeter Zijlstra 	/* got the lock - cleanup and rejoice! */
72101768b42SPeter Zijlstra 	lock_acquired(&lock->dep_map, ip);
722ee042be1SNamhyung Kim 	trace_contention_end(lock, 0);
72301768b42SPeter Zijlstra 
7245de2055dSWaiman Long 	if (ww_ctx)
72555f036caSPeter Ziljstra 		ww_mutex_lock_acquired(ww, ww_ctx);
72601768b42SPeter Zijlstra 
727ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
72801768b42SPeter Zijlstra 	preempt_enable();
72901768b42SPeter Zijlstra 	return 0;
73001768b42SPeter Zijlstra 
73101768b42SPeter Zijlstra err:
732642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
7333a010c49SZqiang 	__mutex_remove_waiter(lock, &waiter);
73455f036caSPeter Ziljstra err_early_kill:
735*dc1f7893SPeter Zijlstra 	trace_contention_end(lock, ret);
736ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
73701768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
7385facae4fSQian Cai 	mutex_release(&lock->dep_map, ip);
73901768b42SPeter Zijlstra 	preempt_enable();
74001768b42SPeter Zijlstra 	return ret;
74101768b42SPeter Zijlstra }
74201768b42SPeter Zijlstra 
743427b1820SPeter Zijlstra static int __sched
__mutex_lock(struct mutex * lock,unsigned int state,unsigned int subclass,struct lockdep_map * nest_lock,unsigned long ip)7442f064a59SPeter Zijlstra __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
745427b1820SPeter Zijlstra 	     struct lockdep_map *nest_lock, unsigned long ip)
746427b1820SPeter Zijlstra {
747427b1820SPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
748427b1820SPeter Zijlstra }
749427b1820SPeter Zijlstra 
750427b1820SPeter Zijlstra static int __sched
__ww_mutex_lock(struct mutex * lock,unsigned int state,unsigned int subclass,unsigned long ip,struct ww_acquire_ctx * ww_ctx)7512f064a59SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
752cf702eddSPeter Zijlstra 		unsigned long ip, struct ww_acquire_ctx *ww_ctx)
753427b1820SPeter Zijlstra {
754cf702eddSPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
755427b1820SPeter Zijlstra }
756427b1820SPeter Zijlstra 
75712235da8SMaarten Lankhorst /**
75812235da8SMaarten Lankhorst  * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context
75912235da8SMaarten Lankhorst  * @ww: mutex to lock
76012235da8SMaarten Lankhorst  * @ww_ctx: optional w/w acquire context
76112235da8SMaarten Lankhorst  *
76212235da8SMaarten Lankhorst  * Trylocks a mutex with the optional acquire context; no deadlock detection is
76312235da8SMaarten Lankhorst  * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
76412235da8SMaarten Lankhorst  *
76512235da8SMaarten Lankhorst  * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is
76612235da8SMaarten Lankhorst  * specified, -EALREADY handling may happen in calls to ww_mutex_trylock.
76712235da8SMaarten Lankhorst  *
76812235da8SMaarten Lankhorst  * A mutex acquired with this function must be released with ww_mutex_unlock.
76912235da8SMaarten Lankhorst  */
ww_mutex_trylock(struct ww_mutex * ww,struct ww_acquire_ctx * ww_ctx)77012235da8SMaarten Lankhorst int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
77112235da8SMaarten Lankhorst {
77212235da8SMaarten Lankhorst 	if (!ww_ctx)
77312235da8SMaarten Lankhorst 		return mutex_trylock(&ww->base);
77412235da8SMaarten Lankhorst 
77512235da8SMaarten Lankhorst 	MUTEX_WARN_ON(ww->base.magic != &ww->base);
77612235da8SMaarten Lankhorst 
77712235da8SMaarten Lankhorst 	/*
77812235da8SMaarten Lankhorst 	 * Reset the wounded flag after a kill. No other process can
77912235da8SMaarten Lankhorst 	 * race and wound us here, since they can't have a valid owner
78012235da8SMaarten Lankhorst 	 * pointer if we don't have any locks held.
78112235da8SMaarten Lankhorst 	 */
78212235da8SMaarten Lankhorst 	if (ww_ctx->acquired == 0)
78312235da8SMaarten Lankhorst 		ww_ctx->wounded = 0;
78412235da8SMaarten Lankhorst 
78512235da8SMaarten Lankhorst 	if (__mutex_trylock(&ww->base)) {
78612235da8SMaarten Lankhorst 		ww_mutex_set_context_fastpath(ww, ww_ctx);
78712235da8SMaarten Lankhorst 		mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_);
78812235da8SMaarten Lankhorst 		return 1;
78912235da8SMaarten Lankhorst 	}
79012235da8SMaarten Lankhorst 
79112235da8SMaarten Lankhorst 	return 0;
79212235da8SMaarten Lankhorst }
79312235da8SMaarten Lankhorst EXPORT_SYMBOL(ww_mutex_trylock);
79412235da8SMaarten Lankhorst 
79501768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
79601768b42SPeter Zijlstra void __sched
mutex_lock_nested(struct mutex * lock,unsigned int subclass)79701768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass)
79801768b42SPeter Zijlstra {
799427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
80001768b42SPeter Zijlstra }
80101768b42SPeter Zijlstra 
80201768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested);
80301768b42SPeter Zijlstra 
80401768b42SPeter Zijlstra void __sched
_mutex_lock_nest_lock(struct mutex * lock,struct lockdep_map * nest)80501768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
80601768b42SPeter Zijlstra {
807427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
80801768b42SPeter Zijlstra }
80901768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
81001768b42SPeter Zijlstra 
81101768b42SPeter Zijlstra int __sched
mutex_lock_killable_nested(struct mutex * lock,unsigned int subclass)81201768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
81301768b42SPeter Zijlstra {
814427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
81501768b42SPeter Zijlstra }
81601768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
81701768b42SPeter Zijlstra 
81801768b42SPeter Zijlstra int __sched
mutex_lock_interruptible_nested(struct mutex * lock,unsigned int subclass)81901768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
82001768b42SPeter Zijlstra {
821427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
82201768b42SPeter Zijlstra }
82301768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
82401768b42SPeter Zijlstra 
8251460cb65STejun Heo void __sched
mutex_lock_io_nested(struct mutex * lock,unsigned int subclass)8261460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
8271460cb65STejun Heo {
8281460cb65STejun Heo 	int token;
8291460cb65STejun Heo 
8301460cb65STejun Heo 	might_sleep();
8311460cb65STejun Heo 
8321460cb65STejun Heo 	token = io_schedule_prepare();
8331460cb65STejun Heo 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
8341460cb65STejun Heo 			    subclass, NULL, _RET_IP_, NULL, 0);
8351460cb65STejun Heo 	io_schedule_finish(token);
8361460cb65STejun Heo }
8371460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
8381460cb65STejun Heo 
83901768b42SPeter Zijlstra static inline int
ww_mutex_deadlock_injection(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)84001768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
84101768b42SPeter Zijlstra {
84201768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
84301768b42SPeter Zijlstra 	unsigned tmp;
84401768b42SPeter Zijlstra 
84501768b42SPeter Zijlstra 	if (ctx->deadlock_inject_countdown-- == 0) {
84601768b42SPeter Zijlstra 		tmp = ctx->deadlock_inject_interval;
84701768b42SPeter Zijlstra 		if (tmp > UINT_MAX/4)
84801768b42SPeter Zijlstra 			tmp = UINT_MAX;
84901768b42SPeter Zijlstra 		else
85001768b42SPeter Zijlstra 			tmp = tmp*2 + tmp + tmp/2;
85101768b42SPeter Zijlstra 
85201768b42SPeter Zijlstra 		ctx->deadlock_inject_interval = tmp;
85301768b42SPeter Zijlstra 		ctx->deadlock_inject_countdown = tmp;
85401768b42SPeter Zijlstra 		ctx->contending_lock = lock;
85501768b42SPeter Zijlstra 
85601768b42SPeter Zijlstra 		ww_mutex_unlock(lock);
85701768b42SPeter Zijlstra 
85801768b42SPeter Zijlstra 		return -EDEADLK;
85901768b42SPeter Zijlstra 	}
86001768b42SPeter Zijlstra #endif
86101768b42SPeter Zijlstra 
86201768b42SPeter Zijlstra 	return 0;
86301768b42SPeter Zijlstra }
86401768b42SPeter Zijlstra 
86501768b42SPeter Zijlstra int __sched
ww_mutex_lock(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)866c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
86701768b42SPeter Zijlstra {
86801768b42SPeter Zijlstra 	int ret;
86901768b42SPeter Zijlstra 
87001768b42SPeter Zijlstra 	might_sleep();
871427b1820SPeter Zijlstra 	ret =  __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
872cf702eddSPeter Zijlstra 			       0, _RET_IP_, ctx);
873ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
87401768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
87501768b42SPeter Zijlstra 
87601768b42SPeter Zijlstra 	return ret;
87701768b42SPeter Zijlstra }
878c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock);
87901768b42SPeter Zijlstra 
88001768b42SPeter Zijlstra int __sched
ww_mutex_lock_interruptible(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)881c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
88201768b42SPeter Zijlstra {
88301768b42SPeter Zijlstra 	int ret;
88401768b42SPeter Zijlstra 
88501768b42SPeter Zijlstra 	might_sleep();
886427b1820SPeter Zijlstra 	ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
887cf702eddSPeter Zijlstra 			      0, _RET_IP_, ctx);
88801768b42SPeter Zijlstra 
889ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
89001768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
89101768b42SPeter Zijlstra 
89201768b42SPeter Zijlstra 	return ret;
89301768b42SPeter Zijlstra }
894c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
89501768b42SPeter Zijlstra 
89601768b42SPeter Zijlstra #endif
89701768b42SPeter Zijlstra 
89801768b42SPeter Zijlstra /*
89901768b42SPeter Zijlstra  * Release the lock, slowpath:
90001768b42SPeter Zijlstra  */
__mutex_unlock_slowpath(struct mutex * lock,unsigned long ip)9013ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
90201768b42SPeter Zijlstra {
9039d659ae1SPeter Zijlstra 	struct task_struct *next = NULL;
904194a6b5bSWaiman Long 	DEFINE_WAKE_Q(wake_q);
905b9c16a0eSPeter Zijlstra 	unsigned long owner;
90601768b42SPeter Zijlstra 
9075facae4fSQian Cai 	mutex_release(&lock->dep_map, ip);
9083ca0ff57SPeter Zijlstra 
90901768b42SPeter Zijlstra 	/*
9109d659ae1SPeter Zijlstra 	 * Release the lock before (potentially) taking the spinlock such that
9119d659ae1SPeter Zijlstra 	 * other contenders can get on with things ASAP.
9129d659ae1SPeter Zijlstra 	 *
9139d659ae1SPeter Zijlstra 	 * Except when HANDOFF, in that case we must not clear the owner field,
9149d659ae1SPeter Zijlstra 	 * but instead set it to the top waiter.
91501768b42SPeter Zijlstra 	 */
9169d659ae1SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
9179d659ae1SPeter Zijlstra 	for (;;) {
918e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(__owner_task(owner) != current);
919e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
9209d659ae1SPeter Zijlstra 
9219d659ae1SPeter Zijlstra 		if (owner & MUTEX_FLAG_HANDOFF)
9229d659ae1SPeter Zijlstra 			break;
9239d659ae1SPeter Zijlstra 
924ab4e4d9fSPeter Zijlstra 		if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) {
9259d659ae1SPeter Zijlstra 			if (owner & MUTEX_FLAG_WAITERS)
9269d659ae1SPeter Zijlstra 				break;
9279d659ae1SPeter Zijlstra 
9283ca0ff57SPeter Zijlstra 			return;
9299d659ae1SPeter Zijlstra 		}
9309d659ae1SPeter Zijlstra 	}
93101768b42SPeter Zijlstra 
932ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
9331d8fe7dcSJason Low 	debug_mutex_unlock(lock);
93401768b42SPeter Zijlstra 	if (!list_empty(&lock->wait_list)) {
93501768b42SPeter Zijlstra 		/* get the first entry from the wait-list: */
93601768b42SPeter Zijlstra 		struct mutex_waiter *waiter =
9379d659ae1SPeter Zijlstra 			list_first_entry(&lock->wait_list,
93801768b42SPeter Zijlstra 					 struct mutex_waiter, list);
93901768b42SPeter Zijlstra 
9409d659ae1SPeter Zijlstra 		next = waiter->task;
9419d659ae1SPeter Zijlstra 
94201768b42SPeter Zijlstra 		debug_mutex_wake_waiter(lock, waiter);
9439d659ae1SPeter Zijlstra 		wake_q_add(&wake_q, next);
94401768b42SPeter Zijlstra 	}
94501768b42SPeter Zijlstra 
9469d659ae1SPeter Zijlstra 	if (owner & MUTEX_FLAG_HANDOFF)
9479d659ae1SPeter Zijlstra 		__mutex_handoff(lock, next);
9489d659ae1SPeter Zijlstra 
949ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
9509d659ae1SPeter Zijlstra 
9511329ce6fSDavidlohr Bueso 	wake_up_q(&wake_q);
95201768b42SPeter Zijlstra }
95301768b42SPeter Zijlstra 
95401768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
95501768b42SPeter Zijlstra /*
95601768b42SPeter Zijlstra  * Here come the less common (and hence less performance-critical) APIs:
95701768b42SPeter Zijlstra  * mutex_lock_interruptible() and mutex_trylock().
95801768b42SPeter Zijlstra  */
95901768b42SPeter Zijlstra static noinline int __sched
96001768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock);
96101768b42SPeter Zijlstra 
96201768b42SPeter Zijlstra static noinline int __sched
96301768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock);
96401768b42SPeter Zijlstra 
96501768b42SPeter Zijlstra /**
96645dbac0eSMatthew Wilcox  * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
96745dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
96801768b42SPeter Zijlstra  *
96945dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal is delivered while the
97045dbac0eSMatthew Wilcox  * process is sleeping, this function will return without acquiring the
97145dbac0eSMatthew Wilcox  * mutex.
97201768b42SPeter Zijlstra  *
97345dbac0eSMatthew Wilcox  * Context: Process context.
97445dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
97545dbac0eSMatthew Wilcox  * signal arrived.
97601768b42SPeter Zijlstra  */
mutex_lock_interruptible(struct mutex * lock)97701768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock)
97801768b42SPeter Zijlstra {
97901768b42SPeter Zijlstra 	might_sleep();
9803ca0ff57SPeter Zijlstra 
9813ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
98201768b42SPeter Zijlstra 		return 0;
9833ca0ff57SPeter Zijlstra 
98401768b42SPeter Zijlstra 	return __mutex_lock_interruptible_slowpath(lock);
98501768b42SPeter Zijlstra }
98601768b42SPeter Zijlstra 
98701768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible);
98801768b42SPeter Zijlstra 
98945dbac0eSMatthew Wilcox /**
99045dbac0eSMatthew Wilcox  * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
99145dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
99245dbac0eSMatthew Wilcox  *
99345dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal which will be fatal to
99445dbac0eSMatthew Wilcox  * the current process is delivered while the process is sleeping, this
99545dbac0eSMatthew Wilcox  * function will return without acquiring the mutex.
99645dbac0eSMatthew Wilcox  *
99745dbac0eSMatthew Wilcox  * Context: Process context.
99845dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
99945dbac0eSMatthew Wilcox  * fatal signal arrived.
100045dbac0eSMatthew Wilcox  */
mutex_lock_killable(struct mutex * lock)100101768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock)
100201768b42SPeter Zijlstra {
100301768b42SPeter Zijlstra 	might_sleep();
10043ca0ff57SPeter Zijlstra 
10053ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
100601768b42SPeter Zijlstra 		return 0;
10073ca0ff57SPeter Zijlstra 
100801768b42SPeter Zijlstra 	return __mutex_lock_killable_slowpath(lock);
100901768b42SPeter Zijlstra }
101001768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable);
101101768b42SPeter Zijlstra 
101245dbac0eSMatthew Wilcox /**
101345dbac0eSMatthew Wilcox  * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
101445dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
101545dbac0eSMatthew Wilcox  *
101645dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  While the task is waiting for this
101745dbac0eSMatthew Wilcox  * mutex, it will be accounted as being in the IO wait state by the
101845dbac0eSMatthew Wilcox  * scheduler.
101945dbac0eSMatthew Wilcox  *
102045dbac0eSMatthew Wilcox  * Context: Process context.
102145dbac0eSMatthew Wilcox  */
mutex_lock_io(struct mutex * lock)10221460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock)
10231460cb65STejun Heo {
10241460cb65STejun Heo 	int token;
10251460cb65STejun Heo 
10261460cb65STejun Heo 	token = io_schedule_prepare();
10271460cb65STejun Heo 	mutex_lock(lock);
10281460cb65STejun Heo 	io_schedule_finish(token);
10291460cb65STejun Heo }
10301460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io);
10311460cb65STejun Heo 
10323ca0ff57SPeter Zijlstra static noinline void __sched
__mutex_lock_slowpath(struct mutex * lock)10333ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock)
103401768b42SPeter Zijlstra {
1035427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
103601768b42SPeter Zijlstra }
103701768b42SPeter Zijlstra 
103801768b42SPeter Zijlstra static noinline int __sched
__mutex_lock_killable_slowpath(struct mutex * lock)103901768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock)
104001768b42SPeter Zijlstra {
1041427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
104201768b42SPeter Zijlstra }
104301768b42SPeter Zijlstra 
104401768b42SPeter Zijlstra static noinline int __sched
__mutex_lock_interruptible_slowpath(struct mutex * lock)104501768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock)
104601768b42SPeter Zijlstra {
1047427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
104801768b42SPeter Zijlstra }
104901768b42SPeter Zijlstra 
105001768b42SPeter Zijlstra static noinline int __sched
__ww_mutex_lock_slowpath(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)105101768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
105201768b42SPeter Zijlstra {
1053cf702eddSPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0,
1054427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
105501768b42SPeter Zijlstra }
105601768b42SPeter Zijlstra 
105701768b42SPeter Zijlstra static noinline int __sched
__ww_mutex_lock_interruptible_slowpath(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)105801768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
105901768b42SPeter Zijlstra 					    struct ww_acquire_ctx *ctx)
106001768b42SPeter Zijlstra {
1061cf702eddSPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0,
1062427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
106301768b42SPeter Zijlstra }
106401768b42SPeter Zijlstra 
106501768b42SPeter Zijlstra #endif
106601768b42SPeter Zijlstra 
106701768b42SPeter Zijlstra /**
106801768b42SPeter Zijlstra  * mutex_trylock - try to acquire the mutex, without waiting
106901768b42SPeter Zijlstra  * @lock: the mutex to be acquired
107001768b42SPeter Zijlstra  *
107101768b42SPeter Zijlstra  * Try to acquire the mutex atomically. Returns 1 if the mutex
107201768b42SPeter Zijlstra  * has been acquired successfully, and 0 on contention.
107301768b42SPeter Zijlstra  *
107401768b42SPeter Zijlstra  * NOTE: this function follows the spin_trylock() convention, so
107501768b42SPeter Zijlstra  * it is negated from the down_trylock() return values! Be careful
107601768b42SPeter Zijlstra  * about this when converting semaphore users to mutexes.
107701768b42SPeter Zijlstra  *
107801768b42SPeter Zijlstra  * This function must not be used in interrupt context. The
107901768b42SPeter Zijlstra  * mutex must be released by the same task that acquired it.
108001768b42SPeter Zijlstra  */
mutex_trylock(struct mutex * lock)108101768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock)
108201768b42SPeter Zijlstra {
10836c11c6e3SSebastian Andrzej Siewior 	bool locked;
108401768b42SPeter Zijlstra 
1085e6b4457bSPeter Zijlstra 	MUTEX_WARN_ON(lock->magic != lock);
10866c11c6e3SSebastian Andrzej Siewior 
10876c11c6e3SSebastian Andrzej Siewior 	locked = __mutex_trylock(lock);
10883ca0ff57SPeter Zijlstra 	if (locked)
10893ca0ff57SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
109001768b42SPeter Zijlstra 
10913ca0ff57SPeter Zijlstra 	return locked;
109201768b42SPeter Zijlstra }
109301768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock);
109401768b42SPeter Zijlstra 
109501768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
109601768b42SPeter Zijlstra int __sched
ww_mutex_lock(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)1097c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
109801768b42SPeter Zijlstra {
109901768b42SPeter Zijlstra 	might_sleep();
110001768b42SPeter Zijlstra 
11013ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1102ea9e0fb8SNicolai Hähnle 		if (ctx)
110301768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
11043ca0ff57SPeter Zijlstra 		return 0;
11053ca0ff57SPeter Zijlstra 	}
11063ca0ff57SPeter Zijlstra 
11073ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_slowpath(lock, ctx);
110801768b42SPeter Zijlstra }
1109c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock);
111001768b42SPeter Zijlstra 
111101768b42SPeter Zijlstra int __sched
ww_mutex_lock_interruptible(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)1112c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
111301768b42SPeter Zijlstra {
111401768b42SPeter Zijlstra 	might_sleep();
111501768b42SPeter Zijlstra 
11163ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1117ea9e0fb8SNicolai Hähnle 		if (ctx)
111801768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
11193ca0ff57SPeter Zijlstra 		return 0;
11203ca0ff57SPeter Zijlstra 	}
11213ca0ff57SPeter Zijlstra 
11223ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
112301768b42SPeter Zijlstra }
1124c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock_interruptible);
112501768b42SPeter Zijlstra 
1126bb630f9fSThomas Gleixner #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
1127bb630f9fSThomas Gleixner #endif /* !CONFIG_PREEMPT_RT */
112801768b42SPeter Zijlstra 
112901768b42SPeter Zijlstra /**
113001768b42SPeter Zijlstra  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
113101768b42SPeter Zijlstra  * @cnt: the atomic which we are to dec
113201768b42SPeter Zijlstra  * @lock: the mutex to return holding if we dec to 0
113301768b42SPeter Zijlstra  *
113401768b42SPeter Zijlstra  * return true and hold lock if we dec to 0, return false otherwise
113501768b42SPeter Zijlstra  */
atomic_dec_and_mutex_lock(atomic_t * cnt,struct mutex * lock)113601768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
113701768b42SPeter Zijlstra {
113801768b42SPeter Zijlstra 	/* dec if we can't possibly hit 0 */
113901768b42SPeter Zijlstra 	if (atomic_add_unless(cnt, -1, 1))
114001768b42SPeter Zijlstra 		return 0;
114101768b42SPeter Zijlstra 	/* we might hit 0, so take the lock */
114201768b42SPeter Zijlstra 	mutex_lock(lock);
114301768b42SPeter Zijlstra 	if (!atomic_dec_and_test(cnt)) {
114401768b42SPeter Zijlstra 		/* when we actually did the dec, we didn't hit 0 */
114501768b42SPeter Zijlstra 		mutex_unlock(lock);
114601768b42SPeter Zijlstra 		return 0;
114701768b42SPeter Zijlstra 	}
114801768b42SPeter Zijlstra 	/* we hit 0, and we hold the lock */
114901768b42SPeter Zijlstra 	return 1;
115001768b42SPeter Zijlstra }
115101768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
1152