xref: /linux-6.15/kernel/locking/mutex.c (revision 3cf67d61)
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 <[email protected]>
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 
__owner_task(unsigned long owner)593ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner)
603ca0ff57SPeter Zijlstra {
613ca0ff57SPeter Zijlstra 	return (struct task_struct *)(owner & ~MUTEX_FLAGS);
623ca0ff57SPeter Zijlstra }
633ca0ff57SPeter Zijlstra 
mutex_is_locked(struct mutex * lock)645f35d5a6SMukesh Ojha bool mutex_is_locked(struct mutex *lock)
655f35d5a6SMukesh Ojha {
665f35d5a6SMukesh Ojha 	return __mutex_owner(lock) != NULL;
675f35d5a6SMukesh Ojha }
685f35d5a6SMukesh Ojha EXPORT_SYMBOL(mutex_is_locked);
695f35d5a6SMukesh Ojha 
__owner_flags(unsigned long owner)703ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner)
713ca0ff57SPeter Zijlstra {
723ca0ff57SPeter Zijlstra 	return owner & MUTEX_FLAGS;
733ca0ff57SPeter Zijlstra }
743ca0ff57SPeter Zijlstra 
75*3cf67d61SMasami Hiramatsu (Google) /* Do not use the return value as a pointer directly. */
mutex_get_owner(struct mutex * lock)76*3cf67d61SMasami Hiramatsu (Google) unsigned long mutex_get_owner(struct mutex *lock)
77*3cf67d61SMasami Hiramatsu (Google) {
78*3cf67d61SMasami Hiramatsu (Google) 	unsigned long owner = atomic_long_read(&lock->owner);
79*3cf67d61SMasami Hiramatsu (Google) 
80*3cf67d61SMasami Hiramatsu (Google) 	return (unsigned long)__owner_task(owner);
81*3cf67d61SMasami Hiramatsu (Google) }
82*3cf67d61SMasami Hiramatsu (Google) 
8312235da8SMaarten Lankhorst /*
8412235da8SMaarten Lankhorst  * Returns: __mutex_owner(lock) on failure or NULL on success.
8512235da8SMaarten Lankhorst  */
__mutex_trylock_common(struct mutex * lock,bool handoff)86ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff)
873ca0ff57SPeter Zijlstra {
883ca0ff57SPeter Zijlstra 	unsigned long owner, curr = (unsigned long)current;
893ca0ff57SPeter Zijlstra 
903ca0ff57SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
913ca0ff57SPeter Zijlstra 	for (;;) { /* must loop, can race against a flag */
92ab4e4d9fSPeter Zijlstra 		unsigned long flags = __owner_flags(owner);
93e274795eSPeter Zijlstra 		unsigned long task = owner & ~MUTEX_FLAGS;
943ca0ff57SPeter Zijlstra 
95e274795eSPeter Zijlstra 		if (task) {
96ad90880dSPeter Zijlstra 			if (flags & MUTEX_FLAG_PICKUP) {
97ad90880dSPeter Zijlstra 				if (task != curr)
98e274795eSPeter Zijlstra 					break;
99e274795eSPeter Zijlstra 				flags &= ~MUTEX_FLAG_PICKUP;
100ad90880dSPeter Zijlstra 			} else if (handoff) {
101ad90880dSPeter Zijlstra 				if (flags & MUTEX_FLAG_HANDOFF)
102ad90880dSPeter Zijlstra 					break;
103ad90880dSPeter Zijlstra 				flags |= MUTEX_FLAG_HANDOFF;
104ad90880dSPeter Zijlstra 			} else {
105ad90880dSPeter Zijlstra 				break;
106ad90880dSPeter Zijlstra 			}
107e274795eSPeter Zijlstra 		} else {
108e6b4457bSPeter Zijlstra 			MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP));
109ad90880dSPeter Zijlstra 			task = curr;
1109d659ae1SPeter Zijlstra 		}
1113ca0ff57SPeter Zijlstra 
112ad90880dSPeter Zijlstra 		if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) {
113ad90880dSPeter Zijlstra 			if (task == curr)
114e274795eSPeter Zijlstra 				return NULL;
115ad90880dSPeter Zijlstra 			break;
116ad90880dSPeter Zijlstra 		}
1173ca0ff57SPeter Zijlstra 	}
118e274795eSPeter Zijlstra 
119e274795eSPeter Zijlstra 	return __owner_task(owner);
120e274795eSPeter Zijlstra }
121e274795eSPeter Zijlstra 
122e274795eSPeter Zijlstra /*
123ad90880dSPeter Zijlstra  * Trylock or set HANDOFF
124ad90880dSPeter Zijlstra  */
__mutex_trylock_or_handoff(struct mutex * lock,bool handoff)125ad90880dSPeter Zijlstra static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff)
126ad90880dSPeter Zijlstra {
127ad90880dSPeter Zijlstra 	return !__mutex_trylock_common(lock, handoff);
128ad90880dSPeter Zijlstra }
129ad90880dSPeter Zijlstra 
130ad90880dSPeter Zijlstra /*
131e274795eSPeter Zijlstra  * Actual trylock that will work on any unlocked state.
132e274795eSPeter Zijlstra  */
__mutex_trylock(struct mutex * lock)133e274795eSPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock)
134e274795eSPeter Zijlstra {
135ad90880dSPeter Zijlstra 	return !__mutex_trylock_common(lock, false);
1363ca0ff57SPeter Zijlstra }
1373ca0ff57SPeter Zijlstra 
1383ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
1393ca0ff57SPeter Zijlstra /*
1403ca0ff57SPeter Zijlstra  * Lockdep annotations are contained to the slow paths for simplicity.
1413ca0ff57SPeter Zijlstra  * There is nothing that would stop spreading the lockdep annotations outwards
1423ca0ff57SPeter Zijlstra  * except more code.
1433ca0ff57SPeter Zijlstra  */
1443ca0ff57SPeter Zijlstra 
1453ca0ff57SPeter Zijlstra /*
1463ca0ff57SPeter Zijlstra  * Optimistic trylock that only works in the uncontended case. Make sure to
1473ca0ff57SPeter Zijlstra  * follow with a __mutex_trylock() before failing.
1483ca0ff57SPeter Zijlstra  */
__mutex_trylock_fast(struct mutex * lock)1493ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
1503ca0ff57SPeter Zijlstra {
1513ca0ff57SPeter Zijlstra 	unsigned long curr = (unsigned long)current;
152c427f695SPeter Zijlstra 	unsigned long zero = 0UL;
1533ca0ff57SPeter Zijlstra 
154c427f695SPeter Zijlstra 	MUTEX_WARN_ON(lock->magic != lock);
1553ca0ff57SPeter Zijlstra 
1563ca0ff57SPeter Zijlstra 	if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
1573ca0ff57SPeter Zijlstra 		return true;
1583ca0ff57SPeter Zijlstra 
1593ca0ff57SPeter Zijlstra 	return false;
1603ca0ff57SPeter Zijlstra }
1613ca0ff57SPeter Zijlstra 
__mutex_unlock_fast(struct mutex * lock)1623ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
1633ca0ff57SPeter Zijlstra {
164ab4e4d9fSPeter Zijlstra 	unsigned long curr = (unsigned long)current;
1653ca0ff57SPeter Zijlstra 
1663ca0ff57SPeter Zijlstra 	return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL);
1673ca0ff57SPeter Zijlstra }
1683ca0ff57SPeter Zijlstra #endif
1693ca0ff57SPeter Zijlstra 
__mutex_set_flag(struct mutex * lock,unsigned long flag)1703ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
1713ca0ff57SPeter Zijlstra {
1723ca0ff57SPeter Zijlstra 	atomic_long_or(flag, &lock->owner);
1733ca0ff57SPeter Zijlstra }
1743ca0ff57SPeter Zijlstra 
__mutex_clear_flag(struct mutex * lock,unsigned long flag)1753ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
1763ca0ff57SPeter Zijlstra {
1773ca0ff57SPeter Zijlstra 	atomic_long_andnot(flag, &lock->owner);
1789d659ae1SPeter Zijlstra }
1799d659ae1SPeter Zijlstra 
__mutex_waiter_is_first(struct mutex * lock,struct mutex_waiter * waiter)1809d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
1819d659ae1SPeter Zijlstra {
1829d659ae1SPeter Zijlstra 	return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
1839d659ae1SPeter Zijlstra }
18408295b3bSThomas Hellstrom 
18508295b3bSThomas Hellstrom /*
18608295b3bSThomas Hellstrom  * Add @waiter to a given location in the lock wait_list and set the
1873a010c49SZqiang  * FLAG_WAITERS flag if it's the first waiter.
18808295b3bSThomas Hellstrom  */
18908295b3bSThomas Hellstrom static void
__mutex_add_waiter(struct mutex * lock,struct mutex_waiter * waiter,struct list_head * list)19008295b3bSThomas Hellstrom __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
191*3cf67d61SMasami Hiramatsu (Google) 		   struct list_head *list)
192*3cf67d61SMasami Hiramatsu (Google) {
193*3cf67d61SMasami Hiramatsu (Google) #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
19408295b3bSThomas Hellstrom 	WRITE_ONCE(current->blocker_mutex, lock);
19508295b3bSThomas Hellstrom #endif
19608295b3bSThomas Hellstrom 	debug_mutex_add_waiter(lock, waiter, current);
19708295b3bSThomas Hellstrom 
19808295b3bSThomas Hellstrom 	list_add_tail(&waiter->list, list);
19908295b3bSThomas Hellstrom 	if (__mutex_waiter_is_first(lock, waiter))
20008295b3bSThomas Hellstrom 		__mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
2013a010c49SZqiang }
2023a010c49SZqiang 
2033a010c49SZqiang static void
__mutex_remove_waiter(struct mutex * lock,struct mutex_waiter * waiter)2043a010c49SZqiang __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter)
2053a010c49SZqiang {
2063a010c49SZqiang 	list_del(&waiter->list);
2073a010c49SZqiang 	if (likely(list_empty(&lock->wait_list)))
2083a010c49SZqiang 		__mutex_clear_flag(lock, MUTEX_FLAGS);
209*3cf67d61SMasami Hiramatsu (Google) 
210*3cf67d61SMasami Hiramatsu (Google) 	debug_mutex_remove_waiter(lock, waiter, current);
211*3cf67d61SMasami Hiramatsu (Google) #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
2123a010c49SZqiang 	WRITE_ONCE(current->blocker_mutex, NULL);
2133a010c49SZqiang #endif
21408295b3bSThomas Hellstrom }
2159d659ae1SPeter Zijlstra 
216e2db7592SIngo Molnar /*
217e274795eSPeter Zijlstra  * Give up ownership to a specific task, when @task = NULL, this is equivalent
218e274795eSPeter Zijlstra  * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves
2199d659ae1SPeter Zijlstra  * WAITERS. Provides RELEASE semantics like a regular unlock, the
2209d659ae1SPeter Zijlstra  * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
2219d659ae1SPeter Zijlstra  */
__mutex_handoff(struct mutex * lock,struct task_struct * task)2229d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
2239d659ae1SPeter Zijlstra {
2249d659ae1SPeter Zijlstra 	unsigned long owner = atomic_long_read(&lock->owner);
225ab4e4d9fSPeter Zijlstra 
2269d659ae1SPeter Zijlstra 	for (;;) {
227e6b4457bSPeter Zijlstra 		unsigned long new;
228e6b4457bSPeter Zijlstra 
2299d659ae1SPeter Zijlstra 		MUTEX_WARN_ON(__owner_task(owner) != current);
2309d659ae1SPeter Zijlstra 		MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
2319d659ae1SPeter Zijlstra 
232e274795eSPeter Zijlstra 		new = (owner & MUTEX_FLAG_WAITERS);
233e274795eSPeter Zijlstra 		new |= (unsigned long)task;
2349d659ae1SPeter Zijlstra 		if (task)
235ab4e4d9fSPeter Zijlstra 			new |= MUTEX_FLAG_PICKUP;
2369d659ae1SPeter Zijlstra 
2379d659ae1SPeter Zijlstra 		if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new))
2389d659ae1SPeter Zijlstra 			break;
2399d659ae1SPeter Zijlstra 	}
24001768b42SPeter Zijlstra }
24101768b42SPeter Zijlstra 
24201768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
24301768b42SPeter Zijlstra /*
24401768b42SPeter Zijlstra  * We split the mutex lock/unlock logic into separate fastpath and
24501768b42SPeter Zijlstra  * slowpath functions, to reduce the register pressure on the fastpath.
24601768b42SPeter Zijlstra  * We also put the fastpath first in the kernel image, to make sure the
2473ca0ff57SPeter Zijlstra  * branch is predicted by the CPU as default-untaken.
24801768b42SPeter Zijlstra  */
24901768b42SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock);
25001768b42SPeter Zijlstra 
25101768b42SPeter Zijlstra /**
25201768b42SPeter Zijlstra  * mutex_lock - acquire the mutex
25301768b42SPeter Zijlstra  * @lock: the mutex to be acquired
25401768b42SPeter Zijlstra  *
25501768b42SPeter Zijlstra  * Lock the mutex exclusively for this task. If the mutex is not
25601768b42SPeter Zijlstra  * available right now, it will sleep until it can get it.
25701768b42SPeter Zijlstra  *
25801768b42SPeter Zijlstra  * The mutex must later on be released by the same task that
259139b6fd2SSharon Dvir  * acquired it. Recursive locking is not allowed. The task
26001768b42SPeter Zijlstra  * may not exit without first unlocking the mutex. Also, kernel
26101768b42SPeter Zijlstra  * memory where the mutex resides must not be freed with
26201768b42SPeter Zijlstra  * the mutex still locked. The mutex must first be initialized
26301768b42SPeter Zijlstra  * (or statically defined) before it can be locked. memset()-ing
26401768b42SPeter Zijlstra  * the mutex to 0 is not allowed.
26501768b42SPeter Zijlstra  *
2667b4ff1adSMauro Carvalho Chehab  * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
26701768b42SPeter Zijlstra  * checks that will enforce the restrictions and will also do
26801768b42SPeter Zijlstra  * deadlock debugging)
26901768b42SPeter Zijlstra  *
27001768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down().
27101768b42SPeter Zijlstra  */
mutex_lock(struct mutex * lock)27201768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock)
27301768b42SPeter Zijlstra {
2743ca0ff57SPeter Zijlstra 	might_sleep();
2753ca0ff57SPeter Zijlstra 
2763ca0ff57SPeter Zijlstra 	if (!__mutex_trylock_fast(lock))
27701768b42SPeter Zijlstra 		__mutex_lock_slowpath(lock);
27801768b42SPeter Zijlstra }
27901768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock);
2802674bd18SPeter Zijlstra (Intel) #endif
28176916515SDavidlohr Bueso 
28201768b42SPeter Zijlstra #include "ww_mutex.h"
283c516df97SNicolai Hähnle 
284ad90880dSPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
285ad90880dSPeter Zijlstra 
286ad90880dSPeter Zijlstra /*
287ad90880dSPeter Zijlstra  * Trylock variant that returns the owning task on failure.
288ad90880dSPeter Zijlstra  */
__mutex_trylock_or_owner(struct mutex * lock)289ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
290ad90880dSPeter Zijlstra {
291ad90880dSPeter Zijlstra 	return __mutex_trylock_common(lock, false);
292c516df97SNicolai Hähnle }
293c516df97SNicolai Hähnle 
294c516df97SNicolai Hähnle static inline
ww_mutex_spin_on_owner(struct mutex * lock,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)295c516df97SNicolai Hähnle bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
296c516df97SNicolai Hähnle 			    struct mutex_waiter *waiter)
297c516df97SNicolai Hähnle {
298c516df97SNicolai Hähnle 	struct ww_mutex *ww;
299c516df97SNicolai Hähnle 
30001768b42SPeter Zijlstra 	ww = container_of(lock, struct ww_mutex, base);
301c516df97SNicolai Hähnle 
302c516df97SNicolai Hähnle 	/*
303c516df97SNicolai Hähnle 	 * If ww->ctx is set the contents are undefined, only
304c516df97SNicolai Hähnle 	 * by acquiring wait_lock there is a guarantee that
305c516df97SNicolai Hähnle 	 * they are not invalid when reading.
306c516df97SNicolai Hähnle 	 *
307c516df97SNicolai Hähnle 	 * As such, when deadlock detection needs to be
308c516df97SNicolai Hähnle 	 * performed the optimistic spinning cannot be done.
309c516df97SNicolai Hähnle 	 *
310c516df97SNicolai Hähnle 	 * Check this in every inner iteration because we may
311c516df97SNicolai Hähnle 	 * be racing against another thread's ww_mutex_lock.
312c516df97SNicolai Hähnle 	 */
313c516df97SNicolai Hähnle 	if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
314c516df97SNicolai Hähnle 		return false;
315c516df97SNicolai Hähnle 
316c516df97SNicolai Hähnle 	/*
317c516df97SNicolai Hähnle 	 * If we aren't on the wait list yet, cancel the spin
318c516df97SNicolai Hähnle 	 * if there are waiters. We want  to avoid stealing the
319c516df97SNicolai Hähnle 	 * lock from a waiter with an earlier stamp, since the
320c516df97SNicolai Hähnle 	 * other thread may already own a lock that we also
321c516df97SNicolai Hähnle 	 * need.
322c516df97SNicolai Hähnle 	 */
323c516df97SNicolai Hähnle 	if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
324c516df97SNicolai Hähnle 		return false;
325c516df97SNicolai Hähnle 
326c516df97SNicolai Hähnle 	/*
327c516df97SNicolai Hähnle 	 * Similarly, stop spinning if we are no longer the
328c516df97SNicolai Hähnle 	 * first waiter.
329c516df97SNicolai Hähnle 	 */
330c516df97SNicolai Hähnle 	if (waiter && !__mutex_waiter_is_first(lock, waiter))
331c516df97SNicolai Hähnle 		return false;
332c516df97SNicolai Hähnle 
333c516df97SNicolai Hähnle 	return true;
33401768b42SPeter Zijlstra }
33525f13b40SNicolai Hähnle 
33625f13b40SNicolai Hähnle /*
33725f13b40SNicolai Hähnle  * Look out! "owner" is an entirely speculative pointer access and not
33825f13b40SNicolai Hähnle  * reliable.
33901768b42SPeter Zijlstra  *
34001768b42SPeter Zijlstra  * "noinline" so that this function shows up on perf profiles.
34125f13b40SNicolai Hähnle  */
342c516df97SNicolai Hähnle static noinline
mutex_spin_on_owner(struct mutex * lock,struct task_struct * owner,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)34301768b42SPeter Zijlstra bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
34401ac33c1SJason Low 			 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
345be1f7bf2SJason Low {
3466c2787f2SYanfei Xu 	bool ret = true;
3476c2787f2SYanfei Xu 
3483ca0ff57SPeter Zijlstra 	lockdep_assert_preemption_disabled();
349be1f7bf2SJason Low 
350be1f7bf2SJason Low 	while (__mutex_owner(lock) == owner) {
3516c2787f2SYanfei Xu 		/*
3526c2787f2SYanfei Xu 		 * Ensure we emit the owner->on_cpu, dereference _after_
3536c2787f2SYanfei Xu 		 * checking lock->owner still matches owner. And we already
3546c2787f2SYanfei Xu 		 * disabled preemption which is equal to the RCU read-side
3556c2787f2SYanfei Xu 		 * crital section in optimistic spinning code. Thus the
356be1f7bf2SJason Low 		 * task_strcut structure won't go away during the spinning
357be1f7bf2SJason Low 		 * period
358be1f7bf2SJason Low 		 */
35905ffc951SPan Xinhui 		barrier();
36005ffc951SPan Xinhui 
36105ffc951SPan Xinhui 		/*
362c0bed69dSKefeng Wang 		 * Use vcpu_is_preempted to detect lock holder preemption issue.
363be1f7bf2SJason Low 		 */
364be1f7bf2SJason Low 		if (!owner_on_cpu(owner) || need_resched()) {
365be1f7bf2SJason Low 			ret = false;
36601768b42SPeter Zijlstra 			break;
367c516df97SNicolai Hähnle 		}
36825f13b40SNicolai Hähnle 
36925f13b40SNicolai Hähnle 		if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
37025f13b40SNicolai Hähnle 			ret = false;
37125f13b40SNicolai Hähnle 			break;
372f2f09a4cSChristian Borntraeger 		}
37301768b42SPeter Zijlstra 
37401768b42SPeter Zijlstra 		cpu_relax();
375be1f7bf2SJason Low 	}
37601768b42SPeter Zijlstra 
37701768b42SPeter Zijlstra 	return ret;
37801768b42SPeter Zijlstra }
37901768b42SPeter Zijlstra 
38001768b42SPeter Zijlstra /*
38101768b42SPeter Zijlstra  * Initial check for entering the mutex spinning loop
38201768b42SPeter Zijlstra  */
mutex_can_spin_on_owner(struct mutex * lock)38301768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock)
38401768b42SPeter Zijlstra {
38501768b42SPeter Zijlstra 	struct task_struct *owner;
3866c2787f2SYanfei Xu 	int retval = 1;
3876c2787f2SYanfei Xu 
38846af29e4SJason Low 	lockdep_assert_preemption_disabled();
38946af29e4SJason Low 
39046af29e4SJason Low 	if (need_resched())
3916c2787f2SYanfei Xu 		return 0;
3926c2787f2SYanfei Xu 
3936c2787f2SYanfei Xu 	/*
3946c2787f2SYanfei Xu 	 * We already disabled preemption which is equal to the RCU read-side
3956c2787f2SYanfei Xu 	 * crital section in optimistic spinning code. Thus the task_strcut
3963ca0ff57SPeter Zijlstra 	 * structure won't go away during the spinning period.
39701768b42SPeter Zijlstra 	 */
398c0bed69dSKefeng Wang 	owner = __mutex_owner(lock);
39976916515SDavidlohr Bueso 	if (owner)
40076916515SDavidlohr Bueso 		retval = owner_on_cpu(owner);
4013ca0ff57SPeter Zijlstra 
4023ca0ff57SPeter Zijlstra 	/*
4033ca0ff57SPeter Zijlstra 	 * If lock->owner is not set, the mutex has been released. Return true
40476916515SDavidlohr Bueso 	 * such that we'll trylock in the spin path, which is a faster option
4053ca0ff57SPeter Zijlstra 	 * than the blocking slow path.
40676916515SDavidlohr Bueso 	 */
40776916515SDavidlohr Bueso 	return retval;
40876916515SDavidlohr Bueso }
40976916515SDavidlohr Bueso 
41076916515SDavidlohr Bueso /*
41176916515SDavidlohr Bueso  * Optimistic spinning.
41276916515SDavidlohr Bueso  *
41376916515SDavidlohr Bueso  * We try to spin for acquisition when we find that the lock owner
41476916515SDavidlohr Bueso  * is currently running on a (different) CPU and while we don't
41576916515SDavidlohr Bueso  * need to reschedule. The rationale is that if the lock owner is
41676916515SDavidlohr Bueso  * running, it is likely to release the lock soon.
41776916515SDavidlohr Bueso  *
41876916515SDavidlohr Bueso  * The mutex spinners are queued up using MCS lock so that only one
41976916515SDavidlohr Bueso  * spinner can compete for the mutex. However, if mutex spinning isn't
42076916515SDavidlohr Bueso  * going to happen, there is no point in going through the lock/unlock
42176916515SDavidlohr Bueso  * overhead.
42276916515SDavidlohr Bueso  *
423b341afb3SWaiman Long  * Returns true when the lock was taken, otherwise false, indicating
424b341afb3SWaiman Long  * that we need to jump to the slowpath and sleep.
425b341afb3SWaiman Long  *
426b341afb3SWaiman Long  * The waiter flag is set to true if the spinner is a waiter in the wait
427b341afb3SWaiman Long  * queue. The waiter-spinner will spin on the lock directly and concurrently
42876916515SDavidlohr Bueso  * with the spinner at the head of the OSQ, if present, until the owner is
429427b1820SPeter Zijlstra  * changed to itself.
430427b1820SPeter Zijlstra  */
4315de2055dSWaiman Long static __always_inline bool
mutex_optimistic_spin(struct mutex * lock,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)43276916515SDavidlohr Bueso mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
433b341afb3SWaiman Long 		      struct mutex_waiter *waiter)
434b341afb3SWaiman Long {
435b341afb3SWaiman Long 	if (!waiter) {
436b341afb3SWaiman Long 		/*
437b341afb3SWaiman Long 		 * The purpose of the mutex_can_spin_on_owner() function is
438b341afb3SWaiman Long 		 * to eliminate the overhead of osq_lock() and osq_unlock()
439b341afb3SWaiman Long 		 * in case spinning isn't possible. As a waiter-spinner
440b341afb3SWaiman Long 		 * is not going to take OSQ lock anyway, there is no need
44176916515SDavidlohr Bueso 		 * to call mutex_can_spin_on_owner().
442b341afb3SWaiman Long 		 */
44376916515SDavidlohr Bueso 		if (!mutex_can_spin_on_owner(lock))
444e42f678aSDavidlohr Bueso 			goto fail;
445e42f678aSDavidlohr Bueso 
446e42f678aSDavidlohr Bueso 		/*
447e42f678aSDavidlohr Bueso 		 * In order to avoid a stampede of mutex spinners trying to
448e42f678aSDavidlohr Bueso 		 * acquire the mutex all at once, the spinners need to take a
44976916515SDavidlohr Bueso 		 * MCS (queued) lock first before spinning on the owner field.
450b341afb3SWaiman Long 		 */
451b341afb3SWaiman Long 		if (!osq_lock(&lock->osq))
45276916515SDavidlohr Bueso 			goto fail;
453b341afb3SWaiman Long 	}
45476916515SDavidlohr Bueso 
45576916515SDavidlohr Bueso 	for (;;) {
456e274795eSPeter Zijlstra 		struct task_struct *owner;
457e274795eSPeter Zijlstra 
458e274795eSPeter Zijlstra 		/* Try to acquire the mutex... */
459e274795eSPeter Zijlstra 		owner = __mutex_trylock_or_owner(lock);
46076916515SDavidlohr Bueso 		if (!owner)
46176916515SDavidlohr Bueso 			break;
462e274795eSPeter Zijlstra 
46376916515SDavidlohr Bueso 		/*
46476916515SDavidlohr Bueso 		 * There's an owner, wait for it to either
465c516df97SNicolai Hähnle 		 * release the lock or go to sleep.
466b341afb3SWaiman Long 		 */
46776916515SDavidlohr Bueso 		if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
46876916515SDavidlohr Bueso 			goto fail_unlock;
46976916515SDavidlohr Bueso 
47076916515SDavidlohr Bueso 		/*
47176916515SDavidlohr Bueso 		 * The cpu_relax() call is a compiler barrier which forces
47276916515SDavidlohr Bueso 		 * everything in this loop to be re-loaded. We don't need
47376916515SDavidlohr Bueso 		 * memory barriers as we'll eventually observe the right
474f2f09a4cSChristian Borntraeger 		 * values at the cost of a few extra spins.
47576916515SDavidlohr Bueso 		 */
47676916515SDavidlohr Bueso 		cpu_relax();
477b341afb3SWaiman Long 	}
47876916515SDavidlohr Bueso 
479b341afb3SWaiman Long 	if (!waiter)
480b341afb3SWaiman Long 		osq_unlock(&lock->osq);
481b341afb3SWaiman Long 
482b341afb3SWaiman Long 	return true;
483b341afb3SWaiman Long 
484b341afb3SWaiman Long 
485b341afb3SWaiman Long fail_unlock:
486b341afb3SWaiman Long 	if (!waiter)
487b341afb3SWaiman Long 		osq_unlock(&lock->osq);
48876916515SDavidlohr Bueso 
48976916515SDavidlohr Bueso fail:
49076916515SDavidlohr Bueso 	/*
49176916515SDavidlohr Bueso 	 * If we fell out of the spin path because of need_resched(),
49276916515SDavidlohr Bueso 	 * reschedule now, before we try-lock the mutex. This avoids getting
4936f942a1fSPeter Zijlstra 	 * scheduled out right after we obtained the mutex.
4946f942a1fSPeter Zijlstra 	 */
4956f942a1fSPeter Zijlstra 	if (need_resched()) {
4966f942a1fSPeter Zijlstra 		/*
4976f942a1fSPeter Zijlstra 		 * We _should_ have TASK_RUNNING here, but just in case
4986f942a1fSPeter Zijlstra 		 * we do not, make it so, otherwise we might get stuck.
49976916515SDavidlohr Bueso 		 */
5006f942a1fSPeter Zijlstra 		__set_current_state(TASK_RUNNING);
50176916515SDavidlohr Bueso 		schedule_preempt_disabled();
50276916515SDavidlohr Bueso 	}
50376916515SDavidlohr Bueso 
50476916515SDavidlohr Bueso 	return false;
505427b1820SPeter Zijlstra }
506427b1820SPeter Zijlstra #else
5075de2055dSWaiman Long static __always_inline bool
mutex_optimistic_spin(struct mutex * lock,struct ww_acquire_ctx * ww_ctx,struct mutex_waiter * waiter)50876916515SDavidlohr Bueso mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
50976916515SDavidlohr Bueso 		      struct mutex_waiter *waiter)
51076916515SDavidlohr Bueso {
51101768b42SPeter Zijlstra 	return false;
51201768b42SPeter Zijlstra }
5133ca0ff57SPeter Zijlstra #endif
51401768b42SPeter Zijlstra 
51501768b42SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
51601768b42SPeter Zijlstra 
51701768b42SPeter Zijlstra /**
51801768b42SPeter Zijlstra  * mutex_unlock - release the mutex
51901768b42SPeter Zijlstra  * @lock: the mutex to be released
52001768b42SPeter Zijlstra  *
52101768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously.
52201768b42SPeter Zijlstra  *
52301768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
524a51749abSJann Horn  * of a not locked mutex is not allowed.
525a51749abSJann Horn  *
526a51749abSJann Horn  * The caller must ensure that the mutex stays alive until this function has
527a51749abSJann Horn  * returned - mutex_unlock() can NOT directly be used to release an object such
528a51749abSJann Horn  * that another concurrent task can free it.
52901768b42SPeter Zijlstra  * Mutexes are different from spinlocks & refcounts in this aspect.
53001768b42SPeter Zijlstra  *
53101768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) up().
53201768b42SPeter Zijlstra  */
mutex_unlock(struct mutex * lock)5333ca0ff57SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock)
5343ca0ff57SPeter Zijlstra {
5353ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
53601768b42SPeter Zijlstra 	if (__mutex_unlock_fast(lock))
5373ca0ff57SPeter Zijlstra 		return;
53801768b42SPeter Zijlstra #endif
53901768b42SPeter Zijlstra 	__mutex_unlock_slowpath(lock, _RET_IP_);
54001768b42SPeter Zijlstra }
54101768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock);
54201768b42SPeter Zijlstra 
54301768b42SPeter Zijlstra /**
54401768b42SPeter Zijlstra  * ww_mutex_unlock - release the w/w mutex
54501768b42SPeter Zijlstra  * @lock: the mutex to be released
54601768b42SPeter Zijlstra  *
54701768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously with any of the
54801768b42SPeter Zijlstra  * ww_mutex_lock* functions (with or without an acquire context). It is
54901768b42SPeter Zijlstra  * forbidden to release the locks after releasing the acquire context.
55001768b42SPeter Zijlstra  *
55101768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
55201768b42SPeter Zijlstra  * of a unlocked mutex is not allowed.
55301768b42SPeter Zijlstra  */
ww_mutex_unlock(struct ww_mutex * lock)554aaa77de1SPeter Zijlstra (Intel) void __sched ww_mutex_unlock(struct ww_mutex *lock)
5553ca0ff57SPeter Zijlstra {
55601768b42SPeter Zijlstra 	__ww_mutex_unlock(lock);
55701768b42SPeter Zijlstra 	mutex_unlock(&lock->base);
55801768b42SPeter Zijlstra }
55901768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock);
56001768b42SPeter Zijlstra 
56101768b42SPeter Zijlstra /*
56201768b42SPeter Zijlstra  * Lock a mutex (possibly interruptible), slowpath:
5632f064a59SPeter Zijlstra  */
56401768b42SPeter 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)56501768b42SPeter Zijlstra __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass,
56601768b42SPeter Zijlstra 		    struct lockdep_map *nest_lock, unsigned long ip,
567894d1b3dSPeter Zijlstra 		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
56801768b42SPeter Zijlstra {
569a40ca565SWaiman Long 	DEFINE_WAKE_Q(wake_q);
5705ec58525SJuri Lelli 	struct mutex_waiter waiter;
57101768b42SPeter Zijlstra 	struct ww_mutex *ww;
57201768b42SPeter Zijlstra 	unsigned long flags;
5735de2055dSWaiman Long 	int ret;
5745de2055dSWaiman Long 
5755de2055dSWaiman Long 	if (!use_ww_ctx)
576427b1820SPeter Zijlstra 		ww_ctx = NULL;
577ea9e0fb8SNicolai Hähnle 
578e6b4457bSPeter Zijlstra 	might_sleep();
5796c11c6e3SSebastian Andrzej Siewior 
580a40ca565SWaiman Long 	MUTEX_WARN_ON(lock->magic != lock);
5815de2055dSWaiman Long 
5820422e83dSChris Wilson 	ww = container_of(lock, struct ww_mutex, base);
5830422e83dSChris Wilson 	if (ww_ctx) {
58408295b3bSThomas Hellstrom 		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
58508295b3bSThomas Hellstrom 			return -EALREADY;
58608295b3bSThomas Hellstrom 
58708295b3bSThomas Hellstrom 		/*
58808295b3bSThomas Hellstrom 		 * Reset the wounded flag after a kill. No other process can
58908295b3bSThomas Hellstrom 		 * race and wound us here since they can't have a valid owner
59008295b3bSThomas Hellstrom 		 * pointer if we don't have any locks held.
59108295b3bSThomas Hellstrom 		 */
592cf702eddSPeter Zijlstra 		if (ww_ctx->acquired == 0)
593cf702eddSPeter Zijlstra 			ww_ctx->wounded = 0;
594cf702eddSPeter Zijlstra 
595cf702eddSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
5960422e83dSChris Wilson 		nest_lock = &ww_ctx->dep_map;
5970422e83dSChris Wilson #endif
59801768b42SPeter Zijlstra 	}
59901768b42SPeter Zijlstra 
60001768b42SPeter Zijlstra 	preempt_disable();
601dc1f7893SPeter Zijlstra 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
602e274795eSPeter Zijlstra 
6035de2055dSWaiman Long 	trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN);
60476916515SDavidlohr Bueso 	if (__mutex_trylock(lock) ||
6053ca0ff57SPeter Zijlstra 	    mutex_optimistic_spin(lock, ww_ctx, NULL)) {
6065de2055dSWaiman Long 		/* got the lock, yay! */
6073ca0ff57SPeter Zijlstra 		lock_acquired(&lock->dep_map, ip);
608dc1f7893SPeter Zijlstra 		if (ww_ctx)
60901768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(ww, ww_ctx);
61001768b42SPeter Zijlstra 		trace_contention_end(lock, 0);
61101768b42SPeter Zijlstra 		preempt_enable();
61201768b42SPeter Zijlstra 		return 0;
6135ec58525SJuri Lelli 	}
6141e820c96SJason Low 
6153ca0ff57SPeter Zijlstra 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
6161e820c96SJason Low 	/*
617659cf9f5SNicolai Hähnle 	 * After waiting to acquire the wait_lock, try again.
6185de2055dSWaiman Long 	 */
619894d1b3dSPeter Zijlstra 	if (__mutex_trylock(lock)) {
620659cf9f5SNicolai Hähnle 		if (ww_ctx)
62101768b42SPeter Zijlstra 			__ww_mutex_check_waiters(lock, ww_ctx, &wake_q);
622659cf9f5SNicolai Hähnle 
62301768b42SPeter Zijlstra 		goto skip_wait;
62401768b42SPeter Zijlstra 	}
625c0afb0ffSPeter Zijlstra 
626b857174eSSebastian Andrzej Siewior 	debug_mutex_lock_common(lock, &waiter);
627c0afb0ffSPeter Zijlstra 	waiter.task = current;
62801768b42SPeter Zijlstra 	if (use_ww_ctx)
6296baa5c60SNicolai Hähnle 		waiter.ww_ctx = ww_ctx;
6306baa5c60SNicolai Hähnle 
6316baa5c60SNicolai Hähnle 	lock_contended(&lock->dep_map, ip);
63201768b42SPeter Zijlstra 
63308295b3bSThomas Hellstrom 	if (!use_ww_ctx) {
6346baa5c60SNicolai Hähnle 		/* add waiting tasks to the end of the waitqueue (FIFO): */
63555f036caSPeter Ziljstra 		__mutex_add_waiter(lock, &waiter, &lock->wait_list);
63655f036caSPeter Ziljstra 	} else {
63755f036caSPeter Ziljstra 		/*
63855f036caSPeter Ziljstra 		 * Add in stamp order, waking up waiters that must kill
639894d1b3dSPeter Zijlstra 		 * themselves.
6406baa5c60SNicolai Hähnle 		 */
64155f036caSPeter Ziljstra 		ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx, &wake_q);
6426baa5c60SNicolai Hähnle 		if (ret)
6436baa5c60SNicolai Hähnle 			goto err_early_kill;
644642fa448SDavidlohr Bueso 	}
645dc1f7893SPeter Zijlstra 
64601768b42SPeter Zijlstra 	set_current_state(state);
647048661a1SPeter Zijlstra 	trace_contention_begin(lock, LCB_F_MUTEX);
648048661a1SPeter Zijlstra 	for (;;) {
6495bbd7e64SPeter Zijlstra 		bool first;
6505bbd7e64SPeter Zijlstra 
6515bbd7e64SPeter Zijlstra 		/*
6525bbd7e64SPeter Zijlstra 		 * Once we hold wait_lock, we're serialized against
6535bbd7e64SPeter Zijlstra 		 * mutex_unlock() handing the lock off to us, do a trylock
6545bbd7e64SPeter Zijlstra 		 * before testing the error conditions to make sure we pick up
655e274795eSPeter Zijlstra 		 * the handoff.
6565bbd7e64SPeter Zijlstra 		 */
65701768b42SPeter Zijlstra 		if (__mutex_trylock(lock))
65801768b42SPeter Zijlstra 			goto acquired;
65955f036caSPeter Ziljstra 
6605bbd7e64SPeter Zijlstra 		/*
6615bbd7e64SPeter Zijlstra 		 * Check for signals and kill conditions while holding
66201768b42SPeter Zijlstra 		 * wait_lock. This ensures the lock cancellation is ordered
6633bb5f4acSDavidlohr Bueso 		 * against mutex_unlock() and wake-ups do not go missing.
66401768b42SPeter Zijlstra 		 */
66501768b42SPeter Zijlstra 		if (signal_pending_state(state, current)) {
66601768b42SPeter Zijlstra 			ret = -EINTR;
66701768b42SPeter Zijlstra 			goto err;
6685de2055dSWaiman Long 		}
66955f036caSPeter Ziljstra 
67001768b42SPeter Zijlstra 		if (ww_ctx) {
67101768b42SPeter Zijlstra 			ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
67201768b42SPeter Zijlstra 			if (ret)
67301768b42SPeter Zijlstra 				goto err;
674abfdccd6SJohn Stultz 		}
675894d1b3dSPeter Zijlstra 
67601768b42SPeter Zijlstra 		raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
6779d659ae1SPeter Zijlstra 
6786baa5c60SNicolai Hähnle 		schedule_preempt_disabled();
6795bbd7e64SPeter Zijlstra 
680642fa448SDavidlohr Bueso 		first = __mutex_waiter_is_first(lock, &waiter);
6815bbd7e64SPeter Zijlstra 
6825bbd7e64SPeter Zijlstra 		set_current_state(state);
6835bbd7e64SPeter Zijlstra 		/*
6845bbd7e64SPeter Zijlstra 		 * Here we order against unlock; we must either see it change
6855bbd7e64SPeter Zijlstra 		 * state back to RUNNING and fall through the next schedule(),
686dc1f7893SPeter Zijlstra 		 * or we must see its unlock and acquire.
6875bbd7e64SPeter Zijlstra 		 */
6885bbd7e64SPeter Zijlstra 		if (__mutex_trylock_or_handoff(lock, first))
689dc1f7893SPeter Zijlstra 			break;
690dc1f7893SPeter Zijlstra 
691dc1f7893SPeter Zijlstra 		if (first) {
692dc1f7893SPeter Zijlstra 			trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN);
693dc1f7893SPeter Zijlstra 			if (mutex_optimistic_spin(lock, ww_ctx, &waiter))
694dc1f7893SPeter Zijlstra 				break;
695dc1f7893SPeter Zijlstra 			trace_contention_begin(lock, LCB_F_MUTEX);
6965ec58525SJuri Lelli 		}
69701768b42SPeter Zijlstra 
6985ec58525SJuri Lelli 		raw_spin_lock_irqsave(&lock->wait_lock, flags);
6995bbd7e64SPeter Zijlstra 	}
700642fa448SDavidlohr Bueso 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
70151587bcfSDavidlohr Bueso acquired:
7025de2055dSWaiman Long 	__set_current_state(TASK_RUNNING);
70308295b3bSThomas Hellstrom 
70408295b3bSThomas Hellstrom 	if (ww_ctx) {
70508295b3bSThomas Hellstrom 		/*
70608295b3bSThomas Hellstrom 		 * Wound-Wait; we stole the lock (!first_waiter), check the
70708295b3bSThomas Hellstrom 		 * waiters as anyone might want to wound us.
70808295b3bSThomas Hellstrom 		 */
709894d1b3dSPeter Zijlstra 		if (!ww_ctx->is_wait_die &&
71008295b3bSThomas Hellstrom 		    !__mutex_waiter_is_first(lock, &waiter))
71108295b3bSThomas Hellstrom 			__ww_mutex_check_waiters(lock, ww_ctx, &wake_q);
7123a010c49SZqiang 	}
7133ca0ff57SPeter Zijlstra 
71401768b42SPeter Zijlstra 	__mutex_remove_waiter(lock, &waiter);
71501768b42SPeter Zijlstra 
71601768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
71701768b42SPeter Zijlstra 
71801768b42SPeter Zijlstra skip_wait:
719ee042be1SNamhyung Kim 	/* got the lock - cleanup and rejoice! */
72001768b42SPeter Zijlstra 	lock_acquired(&lock->dep_map, ip);
7215de2055dSWaiman Long 	trace_contention_end(lock, 0);
72255f036caSPeter Ziljstra 
72301768b42SPeter Zijlstra 	if (ww_ctx)
724abfdccd6SJohn Stultz 		ww_mutex_lock_acquired(ww, ww_ctx);
72501768b42SPeter Zijlstra 
72601768b42SPeter Zijlstra 	raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
72701768b42SPeter Zijlstra 	preempt_enable();
72801768b42SPeter Zijlstra 	return 0;
729642fa448SDavidlohr Bueso 
7303a010c49SZqiang err:
73155f036caSPeter Ziljstra 	__set_current_state(TASK_RUNNING);
732dc1f7893SPeter Zijlstra 	__mutex_remove_waiter(lock, &waiter);
733abfdccd6SJohn Stultz err_early_kill:
73401768b42SPeter Zijlstra 	trace_contention_end(lock, ret);
7355facae4fSQian Cai 	raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
73601768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
73701768b42SPeter Zijlstra 	mutex_release(&lock->dep_map, ip);
73801768b42SPeter Zijlstra 	preempt_enable();
73901768b42SPeter Zijlstra 	return ret;
740427b1820SPeter Zijlstra }
7412f064a59SPeter Zijlstra 
742427b1820SPeter Zijlstra static int __sched
__mutex_lock(struct mutex * lock,unsigned int state,unsigned int subclass,struct lockdep_map * nest_lock,unsigned long ip)743427b1820SPeter Zijlstra __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
744427b1820SPeter Zijlstra 	     struct lockdep_map *nest_lock, unsigned long ip)
745427b1820SPeter Zijlstra {
746427b1820SPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
747427b1820SPeter Zijlstra }
7482f064a59SPeter Zijlstra 
749cf702eddSPeter 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)750427b1820SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
751cf702eddSPeter Zijlstra 		unsigned long ip, struct ww_acquire_ctx *ww_ctx)
752427b1820SPeter Zijlstra {
753427b1820SPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
75412235da8SMaarten Lankhorst }
75512235da8SMaarten Lankhorst 
75612235da8SMaarten Lankhorst /**
75712235da8SMaarten Lankhorst  * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context
75812235da8SMaarten Lankhorst  * @ww: mutex to lock
75912235da8SMaarten Lankhorst  * @ww_ctx: optional w/w acquire context
76012235da8SMaarten Lankhorst  *
76112235da8SMaarten Lankhorst  * Trylocks a mutex with the optional acquire context; no deadlock detection is
76212235da8SMaarten Lankhorst  * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
76312235da8SMaarten Lankhorst  *
76412235da8SMaarten Lankhorst  * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is
76512235da8SMaarten Lankhorst  * specified, -EALREADY handling may happen in calls to ww_mutex_trylock.
76612235da8SMaarten Lankhorst  *
76712235da8SMaarten Lankhorst  * A mutex acquired with this function must be released with ww_mutex_unlock.
76812235da8SMaarten Lankhorst  */
ww_mutex_trylock(struct ww_mutex * ww,struct ww_acquire_ctx * ww_ctx)76912235da8SMaarten Lankhorst int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
77012235da8SMaarten Lankhorst {
77112235da8SMaarten Lankhorst 	if (!ww_ctx)
77212235da8SMaarten Lankhorst 		return mutex_trylock(&ww->base);
77312235da8SMaarten Lankhorst 
77412235da8SMaarten Lankhorst 	MUTEX_WARN_ON(ww->base.magic != &ww->base);
77512235da8SMaarten Lankhorst 
77612235da8SMaarten Lankhorst 	/*
77712235da8SMaarten Lankhorst 	 * Reset the wounded flag after a kill. No other process can
77812235da8SMaarten Lankhorst 	 * race and wound us here, since they can't have a valid owner
77912235da8SMaarten Lankhorst 	 * pointer if we don't have any locks held.
78012235da8SMaarten Lankhorst 	 */
78112235da8SMaarten Lankhorst 	if (ww_ctx->acquired == 0)
78212235da8SMaarten Lankhorst 		ww_ctx->wounded = 0;
78312235da8SMaarten Lankhorst 
78412235da8SMaarten Lankhorst 	if (__mutex_trylock(&ww->base)) {
78512235da8SMaarten Lankhorst 		ww_mutex_set_context_fastpath(ww, ww_ctx);
78612235da8SMaarten Lankhorst 		mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_);
78712235da8SMaarten Lankhorst 		return 1;
78812235da8SMaarten Lankhorst 	}
78912235da8SMaarten Lankhorst 
79012235da8SMaarten Lankhorst 	return 0;
79112235da8SMaarten Lankhorst }
79201768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_trylock);
79301768b42SPeter Zijlstra 
79401768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
79501768b42SPeter Zijlstra void __sched
mutex_lock_nested(struct mutex * lock,unsigned int subclass)796427b1820SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass)
79701768b42SPeter Zijlstra {
79801768b42SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
79901768b42SPeter Zijlstra }
80001768b42SPeter Zijlstra 
80101768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested);
80201768b42SPeter Zijlstra 
80301768b42SPeter Zijlstra void __sched
_mutex_lock_nest_lock(struct mutex * lock,struct lockdep_map * nest)804427b1820SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
80501768b42SPeter Zijlstra {
80601768b42SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
80701768b42SPeter Zijlstra }
80801768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
80901768b42SPeter Zijlstra 
81001768b42SPeter Zijlstra int __sched
mutex_lock_killable_nested(struct mutex * lock,unsigned int subclass)811427b1820SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
81201768b42SPeter Zijlstra {
81301768b42SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
81401768b42SPeter Zijlstra }
81501768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
81601768b42SPeter Zijlstra 
81701768b42SPeter Zijlstra int __sched
mutex_lock_interruptible_nested(struct mutex * lock,unsigned int subclass)818427b1820SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
81901768b42SPeter Zijlstra {
82001768b42SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
82101768b42SPeter Zijlstra }
8221460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
8231460cb65STejun Heo 
8241460cb65STejun Heo void __sched
mutex_lock_io_nested(struct mutex * lock,unsigned int subclass)8251460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
8261460cb65STejun Heo {
8271460cb65STejun Heo 	int token;
8281460cb65STejun Heo 
8291460cb65STejun Heo 	might_sleep();
8301460cb65STejun Heo 
8311460cb65STejun Heo 	token = io_schedule_prepare();
8321460cb65STejun Heo 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
8331460cb65STejun Heo 			    subclass, NULL, _RET_IP_, NULL, 0);
8341460cb65STejun Heo 	io_schedule_finish(token);
8351460cb65STejun Heo }
83601768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
83701768b42SPeter Zijlstra 
83801768b42SPeter Zijlstra static inline int
ww_mutex_deadlock_injection(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)83901768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
84001768b42SPeter Zijlstra {
84101768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
84201768b42SPeter Zijlstra 	unsigned tmp;
84301768b42SPeter Zijlstra 
84401768b42SPeter Zijlstra 	if (ctx->deadlock_inject_countdown-- == 0) {
84501768b42SPeter Zijlstra 		tmp = ctx->deadlock_inject_interval;
84601768b42SPeter Zijlstra 		if (tmp > UINT_MAX/4)
84701768b42SPeter Zijlstra 			tmp = UINT_MAX;
84801768b42SPeter Zijlstra 		else
84901768b42SPeter Zijlstra 			tmp = tmp*2 + tmp + tmp/2;
85001768b42SPeter Zijlstra 
85101768b42SPeter Zijlstra 		ctx->deadlock_inject_interval = tmp;
85201768b42SPeter Zijlstra 		ctx->deadlock_inject_countdown = tmp;
85301768b42SPeter Zijlstra 		ctx->contending_lock = lock;
85401768b42SPeter Zijlstra 
85501768b42SPeter Zijlstra 		ww_mutex_unlock(lock);
85601768b42SPeter Zijlstra 
85701768b42SPeter Zijlstra 		return -EDEADLK;
85801768b42SPeter Zijlstra 	}
85901768b42SPeter Zijlstra #endif
86001768b42SPeter Zijlstra 
86101768b42SPeter Zijlstra 	return 0;
86201768b42SPeter Zijlstra }
863c5470b22SNicolai Hähnle 
86401768b42SPeter Zijlstra int __sched
ww_mutex_lock(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)86501768b42SPeter Zijlstra ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
86601768b42SPeter Zijlstra {
86701768b42SPeter Zijlstra 	int ret;
868427b1820SPeter Zijlstra 
869cf702eddSPeter Zijlstra 	might_sleep();
870ea9e0fb8SNicolai Hähnle 	ret =  __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
87101768b42SPeter Zijlstra 			       0, _RET_IP_, ctx);
87201768b42SPeter Zijlstra 	if (!ret && ctx && ctx->acquired > 1)
87301768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
87401768b42SPeter Zijlstra 
875c5470b22SNicolai Hähnle 	return ret;
87601768b42SPeter Zijlstra }
87701768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(ww_mutex_lock);
878c5470b22SNicolai Hähnle 
87901768b42SPeter Zijlstra int __sched
ww_mutex_lock_interruptible(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)88001768b42SPeter Zijlstra ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
88101768b42SPeter Zijlstra {
88201768b42SPeter Zijlstra 	int ret;
883427b1820SPeter Zijlstra 
884cf702eddSPeter Zijlstra 	might_sleep();
88501768b42SPeter Zijlstra 	ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
886ea9e0fb8SNicolai Hähnle 			      0, _RET_IP_, ctx);
88701768b42SPeter Zijlstra 
88801768b42SPeter Zijlstra 	if (!ret && ctx && ctx->acquired > 1)
88901768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
89001768b42SPeter Zijlstra 
891c5470b22SNicolai Hähnle 	return ret;
89201768b42SPeter Zijlstra }
89301768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
89401768b42SPeter Zijlstra 
89501768b42SPeter Zijlstra #endif
89601768b42SPeter Zijlstra 
89701768b42SPeter Zijlstra /*
8983ca0ff57SPeter Zijlstra  * Release the lock, slowpath:
89901768b42SPeter Zijlstra  */
__mutex_unlock_slowpath(struct mutex * lock,unsigned long ip)9009d659ae1SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
901194a6b5bSWaiman Long {
902b9c16a0eSPeter Zijlstra 	struct task_struct *next = NULL;
9035ec58525SJuri Lelli 	DEFINE_WAKE_Q(wake_q);
90401768b42SPeter Zijlstra 	unsigned long owner;
9055facae4fSQian Cai 	unsigned long flags;
9063ca0ff57SPeter Zijlstra 
90701768b42SPeter Zijlstra 	mutex_release(&lock->dep_map, ip);
9089d659ae1SPeter Zijlstra 
9099d659ae1SPeter 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 	 *
91301768b42SPeter Zijlstra 	 * Except when HANDOFF, in that case we must not clear the owner field,
9149d659ae1SPeter Zijlstra 	 * but instead set it to the top waiter.
9159d659ae1SPeter Zijlstra 	 */
916e6b4457bSPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
917e6b4457bSPeter Zijlstra 	for (;;) {
9189d659ae1SPeter Zijlstra 		MUTEX_WARN_ON(__owner_task(owner) != current);
9199d659ae1SPeter Zijlstra 		MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
9209d659ae1SPeter Zijlstra 
9219d659ae1SPeter Zijlstra 		if (owner & MUTEX_FLAG_HANDOFF)
922ab4e4d9fSPeter Zijlstra 			break;
9239d659ae1SPeter Zijlstra 
9249d659ae1SPeter Zijlstra 		if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) {
9259d659ae1SPeter Zijlstra 			if (owner & MUTEX_FLAG_WAITERS)
9263ca0ff57SPeter Zijlstra 				break;
9279d659ae1SPeter Zijlstra 
9289d659ae1SPeter Zijlstra 			return;
92901768b42SPeter Zijlstra 		}
9305ec58525SJuri Lelli 	}
9311d8fe7dcSJason Low 
93201768b42SPeter Zijlstra 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
93301768b42SPeter Zijlstra 	debug_mutex_unlock(lock);
93401768b42SPeter Zijlstra 	if (!list_empty(&lock->wait_list)) {
9359d659ae1SPeter Zijlstra 		/* get the first entry from the wait-list: */
93601768b42SPeter Zijlstra 		struct mutex_waiter *waiter =
93701768b42SPeter Zijlstra 			list_first_entry(&lock->wait_list,
9389d659ae1SPeter Zijlstra 					 struct mutex_waiter, list);
9399d659ae1SPeter Zijlstra 
94001768b42SPeter Zijlstra 		next = waiter->task;
9419d659ae1SPeter Zijlstra 
94201768b42SPeter Zijlstra 		debug_mutex_wake_waiter(lock, waiter);
94301768b42SPeter Zijlstra 		wake_q_add(&wake_q, next);
9449d659ae1SPeter Zijlstra 	}
9459d659ae1SPeter Zijlstra 
9469d659ae1SPeter Zijlstra 	if (owner & MUTEX_FLAG_HANDOFF)
947abfdccd6SJohn Stultz 		__mutex_handoff(lock, next);
94801768b42SPeter Zijlstra 
94901768b42SPeter Zijlstra 	raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
95001768b42SPeter Zijlstra }
95101768b42SPeter Zijlstra 
95201768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
95301768b42SPeter Zijlstra /*
95401768b42SPeter Zijlstra  * Here come the less common (and hence less performance-critical) APIs:
95501768b42SPeter Zijlstra  * mutex_lock_interruptible() and mutex_trylock().
95601768b42SPeter Zijlstra  */
95701768b42SPeter Zijlstra static noinline int __sched
95801768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock);
95901768b42SPeter Zijlstra 
96001768b42SPeter Zijlstra static noinline int __sched
96101768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock);
96245dbac0eSMatthew Wilcox 
96345dbac0eSMatthew Wilcox /**
96401768b42SPeter Zijlstra  * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
96545dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
96645dbac0eSMatthew Wilcox  *
96745dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal is delivered while the
96801768b42SPeter Zijlstra  * process is sleeping, this function will return without acquiring the
96945dbac0eSMatthew Wilcox  * mutex.
97045dbac0eSMatthew Wilcox  *
97145dbac0eSMatthew Wilcox  * Context: Process context.
97201768b42SPeter Zijlstra  * Return: 0 if the lock was successfully acquired or %-EINTR if a
97301768b42SPeter Zijlstra  * signal arrived.
97401768b42SPeter Zijlstra  */
mutex_lock_interruptible(struct mutex * lock)97501768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock)
9763ca0ff57SPeter Zijlstra {
9773ca0ff57SPeter Zijlstra 	might_sleep();
97801768b42SPeter Zijlstra 
9793ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
98001768b42SPeter Zijlstra 		return 0;
98101768b42SPeter Zijlstra 
98201768b42SPeter Zijlstra 	return __mutex_lock_interruptible_slowpath(lock);
98301768b42SPeter Zijlstra }
98401768b42SPeter Zijlstra 
98545dbac0eSMatthew Wilcox EXPORT_SYMBOL(mutex_lock_interruptible);
98645dbac0eSMatthew Wilcox 
98745dbac0eSMatthew Wilcox /**
98845dbac0eSMatthew Wilcox  * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
98945dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
99045dbac0eSMatthew Wilcox  *
99145dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal which will be fatal to
99245dbac0eSMatthew Wilcox  * the current process is delivered while the process is sleeping, this
99345dbac0eSMatthew Wilcox  * function will return without acquiring the mutex.
99445dbac0eSMatthew Wilcox  *
99545dbac0eSMatthew Wilcox  * Context: Process context.
99645dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
99701768b42SPeter Zijlstra  * fatal signal arrived.
99801768b42SPeter Zijlstra  */
mutex_lock_killable(struct mutex * lock)99901768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock)
10003ca0ff57SPeter Zijlstra {
10013ca0ff57SPeter Zijlstra 	might_sleep();
100201768b42SPeter Zijlstra 
10033ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
100401768b42SPeter Zijlstra 		return 0;
100501768b42SPeter Zijlstra 
100601768b42SPeter Zijlstra 	return __mutex_lock_killable_slowpath(lock);
100701768b42SPeter Zijlstra }
100845dbac0eSMatthew Wilcox EXPORT_SYMBOL(mutex_lock_killable);
100945dbac0eSMatthew Wilcox 
101045dbac0eSMatthew Wilcox /**
101145dbac0eSMatthew Wilcox  * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
101245dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
101345dbac0eSMatthew Wilcox  *
101445dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  While the task is waiting for this
101545dbac0eSMatthew Wilcox  * mutex, it will be accounted as being in the IO wait state by the
101645dbac0eSMatthew Wilcox  * scheduler.
101745dbac0eSMatthew Wilcox  *
10181460cb65STejun Heo  * Context: Process context.
10191460cb65STejun Heo  */
mutex_lock_io(struct mutex * lock)10201460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock)
10211460cb65STejun Heo {
10221460cb65STejun Heo 	int token;
10231460cb65STejun Heo 
10241460cb65STejun Heo 	token = io_schedule_prepare();
10251460cb65STejun Heo 	mutex_lock(lock);
10261460cb65STejun Heo 	io_schedule_finish(token);
10271460cb65STejun Heo }
10283ca0ff57SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_io);
10293ca0ff57SPeter Zijlstra 
103001768b42SPeter Zijlstra static noinline void __sched
__mutex_lock_slowpath(struct mutex * lock)1031427b1820SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock)
103201768b42SPeter Zijlstra {
103301768b42SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
103401768b42SPeter Zijlstra }
103501768b42SPeter Zijlstra 
103601768b42SPeter Zijlstra static noinline int __sched
__mutex_lock_killable_slowpath(struct mutex * lock)1037427b1820SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock)
103801768b42SPeter Zijlstra {
103901768b42SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
104001768b42SPeter Zijlstra }
104101768b42SPeter Zijlstra 
104201768b42SPeter Zijlstra static noinline int __sched
__mutex_lock_interruptible_slowpath(struct mutex * lock)1043427b1820SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock)
104401768b42SPeter Zijlstra {
104501768b42SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
104601768b42SPeter Zijlstra }
104701768b42SPeter Zijlstra 
104801768b42SPeter Zijlstra static noinline int __sched
__ww_mutex_lock_slowpath(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)1049cf702eddSPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1050427b1820SPeter Zijlstra {
105101768b42SPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0,
105201768b42SPeter Zijlstra 			       _RET_IP_, ctx);
105301768b42SPeter Zijlstra }
105401768b42SPeter Zijlstra 
105501768b42SPeter Zijlstra static noinline int __sched
__ww_mutex_lock_interruptible_slowpath(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)105601768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
1057cf702eddSPeter Zijlstra 					    struct ww_acquire_ctx *ctx)
1058427b1820SPeter Zijlstra {
105901768b42SPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0,
106001768b42SPeter Zijlstra 			       _RET_IP_, ctx);
106101768b42SPeter Zijlstra }
106201768b42SPeter Zijlstra 
106301768b42SPeter Zijlstra #endif
106401768b42SPeter Zijlstra 
106501768b42SPeter Zijlstra /**
106601768b42SPeter Zijlstra  * mutex_trylock - try to acquire the mutex, without waiting
106701768b42SPeter Zijlstra  * @lock: the mutex to be acquired
106801768b42SPeter Zijlstra  *
106901768b42SPeter Zijlstra  * Try to acquire the mutex atomically. Returns 1 if the mutex
107001768b42SPeter Zijlstra  * has been acquired successfully, and 0 on contention.
107101768b42SPeter Zijlstra  *
107201768b42SPeter Zijlstra  * NOTE: this function follows the spin_trylock() convention, so
107301768b42SPeter Zijlstra  * it is negated from the down_trylock() return values! Be careful
107401768b42SPeter Zijlstra  * about this when converting semaphore users to mutexes.
107501768b42SPeter Zijlstra  *
107601768b42SPeter Zijlstra  * This function must not be used in interrupt context. The
107701768b42SPeter Zijlstra  * mutex must be released by the same task that acquired it.
107801768b42SPeter Zijlstra  */
mutex_trylock(struct mutex * lock)10796c11c6e3SSebastian Andrzej Siewior int __sched mutex_trylock(struct mutex *lock)
108001768b42SPeter Zijlstra {
1081e6b4457bSPeter Zijlstra 	bool locked;
10826c11c6e3SSebastian Andrzej Siewior 
10836c11c6e3SSebastian Andrzej Siewior 	MUTEX_WARN_ON(lock->magic != lock);
10843ca0ff57SPeter Zijlstra 
10853ca0ff57SPeter Zijlstra 	locked = __mutex_trylock(lock);
108601768b42SPeter Zijlstra 	if (locked)
10873ca0ff57SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
108801768b42SPeter Zijlstra 
108901768b42SPeter Zijlstra 	return locked;
109001768b42SPeter Zijlstra }
109101768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock);
109201768b42SPeter Zijlstra 
1093c5470b22SNicolai Hähnle #ifndef CONFIG_DEBUG_LOCK_ALLOC
109401768b42SPeter Zijlstra int __sched
ww_mutex_lock(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)109501768b42SPeter Zijlstra ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
109601768b42SPeter Zijlstra {
10973ca0ff57SPeter Zijlstra 	might_sleep();
1098ea9e0fb8SNicolai Hähnle 
109901768b42SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
11003ca0ff57SPeter Zijlstra 		if (ctx)
11013ca0ff57SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
11023ca0ff57SPeter Zijlstra 		return 0;
11033ca0ff57SPeter Zijlstra 	}
110401768b42SPeter Zijlstra 
1105c5470b22SNicolai Hähnle 	return __ww_mutex_lock_slowpath(lock, ctx);
110601768b42SPeter Zijlstra }
110701768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_lock);
1108c5470b22SNicolai Hähnle 
110901768b42SPeter Zijlstra int __sched
ww_mutex_lock_interruptible(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)111001768b42SPeter Zijlstra ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
111101768b42SPeter Zijlstra {
11123ca0ff57SPeter Zijlstra 	might_sleep();
1113ea9e0fb8SNicolai Hähnle 
111401768b42SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
11153ca0ff57SPeter Zijlstra 		if (ctx)
11163ca0ff57SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
11173ca0ff57SPeter Zijlstra 		return 0;
11183ca0ff57SPeter Zijlstra 	}
111901768b42SPeter Zijlstra 
1120c5470b22SNicolai Hähnle 	return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
112101768b42SPeter Zijlstra }
1122bb630f9fSThomas Gleixner EXPORT_SYMBOL(ww_mutex_lock_interruptible);
1123bb630f9fSThomas Gleixner 
112401768b42SPeter Zijlstra #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
1125957e4808SBrian Foster #endif /* !CONFIG_PREEMPT_RT */
1126957e4808SBrian Foster 
1127957e4808SBrian Foster EXPORT_TRACEPOINT_SYMBOL_GPL(contention_begin);
112801768b42SPeter Zijlstra EXPORT_TRACEPOINT_SYMBOL_GPL(contention_end);
112901768b42SPeter Zijlstra 
113001768b42SPeter Zijlstra /**
113101768b42SPeter Zijlstra  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
113201768b42SPeter Zijlstra  * @cnt: the atomic which we are to dec
113301768b42SPeter Zijlstra  * @lock: the mutex to return holding if we dec to 0
113401768b42SPeter Zijlstra  *
113501768b42SPeter Zijlstra  * return true and hold lock if we dec to 0, return false otherwise
113601768b42SPeter Zijlstra  */
atomic_dec_and_mutex_lock(atomic_t * cnt,struct mutex * lock)113701768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
113801768b42SPeter Zijlstra {
113901768b42SPeter Zijlstra 	/* dec if we can't possibly hit 0 */
114001768b42SPeter Zijlstra 	if (atomic_add_unless(cnt, -1, 1))
114101768b42SPeter Zijlstra 		return 0;
114201768b42SPeter Zijlstra 	/* we might hit 0, so take the lock */
114301768b42SPeter Zijlstra 	mutex_lock(lock);
114401768b42SPeter Zijlstra 	if (!atomic_dec_and_test(cnt)) {
114501768b42SPeter Zijlstra 		/* when we actually did the dec, we didn't hit 0 */
114601768b42SPeter Zijlstra 		mutex_unlock(lock);
114701768b42SPeter Zijlstra 		return 0;
114801768b42SPeter Zijlstra 	}
114901768b42SPeter Zijlstra 	/* we hit 0, and we hold the lock */
115001768b42SPeter Zijlstra 	return 1;
1151 }
1152 EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
1153