xref: /openbmc/linux/kernel/locking/mutex.c (revision ee042be16cb455116d0fe99b77c6bc8baf87c8c6)
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
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  */
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 
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 
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 
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  */
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  */
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  */
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  */
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 
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 
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 
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 
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
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
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  */
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  */
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  */
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
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
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  */
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
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
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  */
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  */
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
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 
605e274795eSPeter Zijlstra 	if (__mutex_trylock(lock) ||
6065de2055dSWaiman Long 	    mutex_optimistic_spin(lock, ww_ctx, NULL)) {
60776916515SDavidlohr Bueso 		/* got the lock, yay! */
6083ca0ff57SPeter Zijlstra 		lock_acquired(&lock->dep_map, ip);
6095de2055dSWaiman Long 		if (ww_ctx)
6103ca0ff57SPeter Zijlstra 			ww_mutex_set_context_fastpath(ww, ww_ctx);
61101768b42SPeter Zijlstra 		preempt_enable();
61201768b42SPeter Zijlstra 		return 0;
61301768b42SPeter Zijlstra 	}
61401768b42SPeter Zijlstra 
615ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
6161e820c96SJason Low 	/*
6173ca0ff57SPeter Zijlstra 	 * After waiting to acquire the wait_lock, try again.
6181e820c96SJason Low 	 */
619659cf9f5SNicolai Hähnle 	if (__mutex_trylock(lock)) {
6205de2055dSWaiman Long 		if (ww_ctx)
62155f036caSPeter Ziljstra 			__ww_mutex_check_waiters(lock, ww_ctx);
622659cf9f5SNicolai Hähnle 
62301768b42SPeter Zijlstra 		goto skip_wait;
624659cf9f5SNicolai Hähnle 	}
62501768b42SPeter Zijlstra 
62601768b42SPeter Zijlstra 	debug_mutex_lock_common(lock, &waiter);
627c0afb0ffSPeter Zijlstra 	waiter.task = current;
628b857174eSSebastian Andrzej Siewior 	if (use_ww_ctx)
629c0afb0ffSPeter Zijlstra 		waiter.ww_ctx = ww_ctx;
63001768b42SPeter Zijlstra 
6316baa5c60SNicolai Hähnle 	lock_contended(&lock->dep_map, ip);
6326baa5c60SNicolai Hähnle 
6336baa5c60SNicolai Hähnle 	if (!use_ww_ctx) {
63401768b42SPeter Zijlstra 		/* add waiting tasks to the end of the waitqueue (FIFO): */
63508295b3bSThomas Hellstrom 		__mutex_add_waiter(lock, &waiter, &lock->wait_list);
6366baa5c60SNicolai Hähnle 	} else {
63755f036caSPeter Ziljstra 		/*
63855f036caSPeter Ziljstra 		 * Add in stamp order, waking up waiters that must kill
63955f036caSPeter Ziljstra 		 * themselves.
64055f036caSPeter Ziljstra 		 */
6416baa5c60SNicolai Hähnle 		ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
6426baa5c60SNicolai Hähnle 		if (ret)
64355f036caSPeter Ziljstra 			goto err_early_kill;
6446baa5c60SNicolai Hähnle 	}
6456baa5c60SNicolai Hähnle 
646642fa448SDavidlohr Bueso 	set_current_state(state);
647*ee042be1SNamhyung Kim 	trace_contention_begin(lock, 0);
64801768b42SPeter Zijlstra 	for (;;) {
649048661a1SPeter Zijlstra 		bool first;
650048661a1SPeter 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
6555bbd7e64SPeter Zijlstra 		 * the handoff.
6565bbd7e64SPeter Zijlstra 		 */
657e274795eSPeter Zijlstra 		if (__mutex_trylock(lock))
6585bbd7e64SPeter Zijlstra 			goto acquired;
65901768b42SPeter Zijlstra 
66001768b42SPeter Zijlstra 		/*
66155f036caSPeter Ziljstra 		 * Check for signals and kill conditions while holding
6625bbd7e64SPeter Zijlstra 		 * wait_lock. This ensures the lock cancellation is ordered
6635bbd7e64SPeter Zijlstra 		 * against mutex_unlock() and wake-ups do not go missing.
66401768b42SPeter Zijlstra 		 */
6653bb5f4acSDavidlohr Bueso 		if (signal_pending_state(state, current)) {
66601768b42SPeter Zijlstra 			ret = -EINTR;
66701768b42SPeter Zijlstra 			goto err;
66801768b42SPeter Zijlstra 		}
66901768b42SPeter Zijlstra 
6705de2055dSWaiman Long 		if (ww_ctx) {
67155f036caSPeter Ziljstra 			ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
67201768b42SPeter Zijlstra 			if (ret)
67301768b42SPeter Zijlstra 				goto err;
67401768b42SPeter Zijlstra 		}
67501768b42SPeter Zijlstra 
676ebf4c55cSThomas Gleixner 		raw_spin_unlock(&lock->wait_lock);
67701768b42SPeter Zijlstra 		schedule_preempt_disabled();
6789d659ae1SPeter Zijlstra 
6796baa5c60SNicolai Hähnle 		first = __mutex_waiter_is_first(lock, &waiter);
6805bbd7e64SPeter Zijlstra 
681642fa448SDavidlohr Bueso 		set_current_state(state);
6825bbd7e64SPeter Zijlstra 		/*
6835bbd7e64SPeter Zijlstra 		 * Here we order against unlock; we must either see it change
6845bbd7e64SPeter Zijlstra 		 * state back to RUNNING and fall through the next schedule(),
6855bbd7e64SPeter Zijlstra 		 * or we must see its unlock and acquire.
6865bbd7e64SPeter Zijlstra 		 */
687ad90880dSPeter Zijlstra 		if (__mutex_trylock_or_handoff(lock, first) ||
6885de2055dSWaiman Long 		    (first && mutex_optimistic_spin(lock, ww_ctx, &waiter)))
6895bbd7e64SPeter Zijlstra 			break;
6905bbd7e64SPeter Zijlstra 
691ebf4c55cSThomas Gleixner 		raw_spin_lock(&lock->wait_lock);
69201768b42SPeter Zijlstra 	}
693ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
6945bbd7e64SPeter Zijlstra acquired:
695642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
69651587bcfSDavidlohr Bueso 
6975de2055dSWaiman Long 	if (ww_ctx) {
69808295b3bSThomas Hellstrom 		/*
69908295b3bSThomas Hellstrom 		 * Wound-Wait; we stole the lock (!first_waiter), check the
70008295b3bSThomas Hellstrom 		 * waiters as anyone might want to wound us.
70108295b3bSThomas Hellstrom 		 */
70208295b3bSThomas Hellstrom 		if (!ww_ctx->is_wait_die &&
70308295b3bSThomas Hellstrom 		    !__mutex_waiter_is_first(lock, &waiter))
70408295b3bSThomas Hellstrom 			__ww_mutex_check_waiters(lock, ww_ctx);
70508295b3bSThomas Hellstrom 	}
70608295b3bSThomas Hellstrom 
7073a010c49SZqiang 	__mutex_remove_waiter(lock, &waiter);
7083ca0ff57SPeter Zijlstra 
70901768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
71001768b42SPeter Zijlstra 
71101768b42SPeter Zijlstra skip_wait:
71201768b42SPeter Zijlstra 	/* got the lock - cleanup and rejoice! */
71301768b42SPeter Zijlstra 	lock_acquired(&lock->dep_map, ip);
714*ee042be1SNamhyung Kim 	trace_contention_end(lock, 0);
71501768b42SPeter Zijlstra 
7165de2055dSWaiman Long 	if (ww_ctx)
71755f036caSPeter Ziljstra 		ww_mutex_lock_acquired(ww, ww_ctx);
71801768b42SPeter Zijlstra 
719ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
72001768b42SPeter Zijlstra 	preempt_enable();
72101768b42SPeter Zijlstra 	return 0;
72201768b42SPeter Zijlstra 
72301768b42SPeter Zijlstra err:
724642fa448SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
7253a010c49SZqiang 	__mutex_remove_waiter(lock, &waiter);
726*ee042be1SNamhyung Kim 	trace_contention_end(lock, ret);
72755f036caSPeter Ziljstra err_early_kill:
728ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
72901768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
7305facae4fSQian Cai 	mutex_release(&lock->dep_map, ip);
73101768b42SPeter Zijlstra 	preempt_enable();
73201768b42SPeter Zijlstra 	return ret;
73301768b42SPeter Zijlstra }
73401768b42SPeter Zijlstra 
735427b1820SPeter Zijlstra static int __sched
7362f064a59SPeter Zijlstra __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
737427b1820SPeter Zijlstra 	     struct lockdep_map *nest_lock, unsigned long ip)
738427b1820SPeter Zijlstra {
739427b1820SPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
740427b1820SPeter Zijlstra }
741427b1820SPeter Zijlstra 
742427b1820SPeter Zijlstra static int __sched
7432f064a59SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
744cf702eddSPeter Zijlstra 		unsigned long ip, struct ww_acquire_ctx *ww_ctx)
745427b1820SPeter Zijlstra {
746cf702eddSPeter Zijlstra 	return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
747427b1820SPeter Zijlstra }
748427b1820SPeter Zijlstra 
74912235da8SMaarten Lankhorst /**
75012235da8SMaarten Lankhorst  * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context
75112235da8SMaarten Lankhorst  * @ww: mutex to lock
75212235da8SMaarten Lankhorst  * @ww_ctx: optional w/w acquire context
75312235da8SMaarten Lankhorst  *
75412235da8SMaarten Lankhorst  * Trylocks a mutex with the optional acquire context; no deadlock detection is
75512235da8SMaarten Lankhorst  * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
75612235da8SMaarten Lankhorst  *
75712235da8SMaarten Lankhorst  * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is
75812235da8SMaarten Lankhorst  * specified, -EALREADY handling may happen in calls to ww_mutex_trylock.
75912235da8SMaarten Lankhorst  *
76012235da8SMaarten Lankhorst  * A mutex acquired with this function must be released with ww_mutex_unlock.
76112235da8SMaarten Lankhorst  */
76212235da8SMaarten Lankhorst int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
76312235da8SMaarten Lankhorst {
76412235da8SMaarten Lankhorst 	if (!ww_ctx)
76512235da8SMaarten Lankhorst 		return mutex_trylock(&ww->base);
76612235da8SMaarten Lankhorst 
76712235da8SMaarten Lankhorst 	MUTEX_WARN_ON(ww->base.magic != &ww->base);
76812235da8SMaarten Lankhorst 
76912235da8SMaarten Lankhorst 	/*
77012235da8SMaarten Lankhorst 	 * Reset the wounded flag after a kill. No other process can
77112235da8SMaarten Lankhorst 	 * race and wound us here, since they can't have a valid owner
77212235da8SMaarten Lankhorst 	 * pointer if we don't have any locks held.
77312235da8SMaarten Lankhorst 	 */
77412235da8SMaarten Lankhorst 	if (ww_ctx->acquired == 0)
77512235da8SMaarten Lankhorst 		ww_ctx->wounded = 0;
77612235da8SMaarten Lankhorst 
77712235da8SMaarten Lankhorst 	if (__mutex_trylock(&ww->base)) {
77812235da8SMaarten Lankhorst 		ww_mutex_set_context_fastpath(ww, ww_ctx);
77912235da8SMaarten Lankhorst 		mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_);
78012235da8SMaarten Lankhorst 		return 1;
78112235da8SMaarten Lankhorst 	}
78212235da8SMaarten Lankhorst 
78312235da8SMaarten Lankhorst 	return 0;
78412235da8SMaarten Lankhorst }
78512235da8SMaarten Lankhorst EXPORT_SYMBOL(ww_mutex_trylock);
78612235da8SMaarten Lankhorst 
78701768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
78801768b42SPeter Zijlstra void __sched
78901768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass)
79001768b42SPeter Zijlstra {
791427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
79201768b42SPeter Zijlstra }
79301768b42SPeter Zijlstra 
79401768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested);
79501768b42SPeter Zijlstra 
79601768b42SPeter Zijlstra void __sched
79701768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
79801768b42SPeter Zijlstra {
799427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
80001768b42SPeter Zijlstra }
80101768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
80201768b42SPeter Zijlstra 
80301768b42SPeter Zijlstra int __sched
80401768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
80501768b42SPeter Zijlstra {
806427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
80701768b42SPeter Zijlstra }
80801768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
80901768b42SPeter Zijlstra 
81001768b42SPeter Zijlstra int __sched
81101768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
81201768b42SPeter Zijlstra {
813427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
81401768b42SPeter Zijlstra }
81501768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
81601768b42SPeter Zijlstra 
8171460cb65STejun Heo void __sched
8181460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
8191460cb65STejun Heo {
8201460cb65STejun Heo 	int token;
8211460cb65STejun Heo 
8221460cb65STejun Heo 	might_sleep();
8231460cb65STejun Heo 
8241460cb65STejun Heo 	token = io_schedule_prepare();
8251460cb65STejun Heo 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
8261460cb65STejun Heo 			    subclass, NULL, _RET_IP_, NULL, 0);
8271460cb65STejun Heo 	io_schedule_finish(token);
8281460cb65STejun Heo }
8291460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
8301460cb65STejun Heo 
83101768b42SPeter Zijlstra static inline int
83201768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
83301768b42SPeter Zijlstra {
83401768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
83501768b42SPeter Zijlstra 	unsigned tmp;
83601768b42SPeter Zijlstra 
83701768b42SPeter Zijlstra 	if (ctx->deadlock_inject_countdown-- == 0) {
83801768b42SPeter Zijlstra 		tmp = ctx->deadlock_inject_interval;
83901768b42SPeter Zijlstra 		if (tmp > UINT_MAX/4)
84001768b42SPeter Zijlstra 			tmp = UINT_MAX;
84101768b42SPeter Zijlstra 		else
84201768b42SPeter Zijlstra 			tmp = tmp*2 + tmp + tmp/2;
84301768b42SPeter Zijlstra 
84401768b42SPeter Zijlstra 		ctx->deadlock_inject_interval = tmp;
84501768b42SPeter Zijlstra 		ctx->deadlock_inject_countdown = tmp;
84601768b42SPeter Zijlstra 		ctx->contending_lock = lock;
84701768b42SPeter Zijlstra 
84801768b42SPeter Zijlstra 		ww_mutex_unlock(lock);
84901768b42SPeter Zijlstra 
85001768b42SPeter Zijlstra 		return -EDEADLK;
85101768b42SPeter Zijlstra 	}
85201768b42SPeter Zijlstra #endif
85301768b42SPeter Zijlstra 
85401768b42SPeter Zijlstra 	return 0;
85501768b42SPeter Zijlstra }
85601768b42SPeter Zijlstra 
85701768b42SPeter Zijlstra int __sched
858c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
85901768b42SPeter Zijlstra {
86001768b42SPeter Zijlstra 	int ret;
86101768b42SPeter Zijlstra 
86201768b42SPeter Zijlstra 	might_sleep();
863427b1820SPeter Zijlstra 	ret =  __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
864cf702eddSPeter Zijlstra 			       0, _RET_IP_, ctx);
865ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
86601768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
86701768b42SPeter Zijlstra 
86801768b42SPeter Zijlstra 	return ret;
86901768b42SPeter Zijlstra }
870c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock);
87101768b42SPeter Zijlstra 
87201768b42SPeter Zijlstra int __sched
873c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
87401768b42SPeter Zijlstra {
87501768b42SPeter Zijlstra 	int ret;
87601768b42SPeter Zijlstra 
87701768b42SPeter Zijlstra 	might_sleep();
878427b1820SPeter Zijlstra 	ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
879cf702eddSPeter Zijlstra 			      0, _RET_IP_, ctx);
88001768b42SPeter Zijlstra 
881ea9e0fb8SNicolai Hähnle 	if (!ret && ctx && ctx->acquired > 1)
88201768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
88301768b42SPeter Zijlstra 
88401768b42SPeter Zijlstra 	return ret;
88501768b42SPeter Zijlstra }
886c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
88701768b42SPeter Zijlstra 
88801768b42SPeter Zijlstra #endif
88901768b42SPeter Zijlstra 
89001768b42SPeter Zijlstra /*
89101768b42SPeter Zijlstra  * Release the lock, slowpath:
89201768b42SPeter Zijlstra  */
8933ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
89401768b42SPeter Zijlstra {
8959d659ae1SPeter Zijlstra 	struct task_struct *next = NULL;
896194a6b5bSWaiman Long 	DEFINE_WAKE_Q(wake_q);
897b9c16a0eSPeter Zijlstra 	unsigned long owner;
89801768b42SPeter Zijlstra 
8995facae4fSQian Cai 	mutex_release(&lock->dep_map, ip);
9003ca0ff57SPeter Zijlstra 
90101768b42SPeter Zijlstra 	/*
9029d659ae1SPeter Zijlstra 	 * Release the lock before (potentially) taking the spinlock such that
9039d659ae1SPeter Zijlstra 	 * other contenders can get on with things ASAP.
9049d659ae1SPeter Zijlstra 	 *
9059d659ae1SPeter Zijlstra 	 * Except when HANDOFF, in that case we must not clear the owner field,
9069d659ae1SPeter Zijlstra 	 * but instead set it to the top waiter.
90701768b42SPeter Zijlstra 	 */
9089d659ae1SPeter Zijlstra 	owner = atomic_long_read(&lock->owner);
9099d659ae1SPeter Zijlstra 	for (;;) {
910e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(__owner_task(owner) != current);
911e6b4457bSPeter Zijlstra 		MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
9129d659ae1SPeter Zijlstra 
9139d659ae1SPeter Zijlstra 		if (owner & MUTEX_FLAG_HANDOFF)
9149d659ae1SPeter Zijlstra 			break;
9159d659ae1SPeter Zijlstra 
916ab4e4d9fSPeter Zijlstra 		if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) {
9179d659ae1SPeter Zijlstra 			if (owner & MUTEX_FLAG_WAITERS)
9189d659ae1SPeter Zijlstra 				break;
9199d659ae1SPeter Zijlstra 
9203ca0ff57SPeter Zijlstra 			return;
9219d659ae1SPeter Zijlstra 		}
9229d659ae1SPeter Zijlstra 	}
92301768b42SPeter Zijlstra 
924ebf4c55cSThomas Gleixner 	raw_spin_lock(&lock->wait_lock);
9251d8fe7dcSJason Low 	debug_mutex_unlock(lock);
92601768b42SPeter Zijlstra 	if (!list_empty(&lock->wait_list)) {
92701768b42SPeter Zijlstra 		/* get the first entry from the wait-list: */
92801768b42SPeter Zijlstra 		struct mutex_waiter *waiter =
9299d659ae1SPeter Zijlstra 			list_first_entry(&lock->wait_list,
93001768b42SPeter Zijlstra 					 struct mutex_waiter, list);
93101768b42SPeter Zijlstra 
9329d659ae1SPeter Zijlstra 		next = waiter->task;
9339d659ae1SPeter Zijlstra 
93401768b42SPeter Zijlstra 		debug_mutex_wake_waiter(lock, waiter);
9359d659ae1SPeter Zijlstra 		wake_q_add(&wake_q, next);
93601768b42SPeter Zijlstra 	}
93701768b42SPeter Zijlstra 
9389d659ae1SPeter Zijlstra 	if (owner & MUTEX_FLAG_HANDOFF)
9399d659ae1SPeter Zijlstra 		__mutex_handoff(lock, next);
9409d659ae1SPeter Zijlstra 
941ebf4c55cSThomas Gleixner 	raw_spin_unlock(&lock->wait_lock);
9429d659ae1SPeter Zijlstra 
9431329ce6fSDavidlohr Bueso 	wake_up_q(&wake_q);
94401768b42SPeter Zijlstra }
94501768b42SPeter Zijlstra 
94601768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
94701768b42SPeter Zijlstra /*
94801768b42SPeter Zijlstra  * Here come the less common (and hence less performance-critical) APIs:
94901768b42SPeter Zijlstra  * mutex_lock_interruptible() and mutex_trylock().
95001768b42SPeter Zijlstra  */
95101768b42SPeter Zijlstra static noinline int __sched
95201768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock);
95301768b42SPeter Zijlstra 
95401768b42SPeter Zijlstra static noinline int __sched
95501768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock);
95601768b42SPeter Zijlstra 
95701768b42SPeter Zijlstra /**
95845dbac0eSMatthew Wilcox  * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
95945dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
96001768b42SPeter Zijlstra  *
96145dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal is delivered while the
96245dbac0eSMatthew Wilcox  * process is sleeping, this function will return without acquiring the
96345dbac0eSMatthew Wilcox  * mutex.
96401768b42SPeter Zijlstra  *
96545dbac0eSMatthew Wilcox  * Context: Process context.
96645dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
96745dbac0eSMatthew Wilcox  * signal arrived.
96801768b42SPeter Zijlstra  */
96901768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock)
97001768b42SPeter Zijlstra {
97101768b42SPeter Zijlstra 	might_sleep();
9723ca0ff57SPeter Zijlstra 
9733ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
97401768b42SPeter Zijlstra 		return 0;
9753ca0ff57SPeter Zijlstra 
97601768b42SPeter Zijlstra 	return __mutex_lock_interruptible_slowpath(lock);
97701768b42SPeter Zijlstra }
97801768b42SPeter Zijlstra 
97901768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible);
98001768b42SPeter Zijlstra 
98145dbac0eSMatthew Wilcox /**
98245dbac0eSMatthew Wilcox  * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
98345dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
98445dbac0eSMatthew Wilcox  *
98545dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  If a signal which will be fatal to
98645dbac0eSMatthew Wilcox  * the current process is delivered while the process is sleeping, this
98745dbac0eSMatthew Wilcox  * function will return without acquiring the mutex.
98845dbac0eSMatthew Wilcox  *
98945dbac0eSMatthew Wilcox  * Context: Process context.
99045dbac0eSMatthew Wilcox  * Return: 0 if the lock was successfully acquired or %-EINTR if a
99145dbac0eSMatthew Wilcox  * fatal signal arrived.
99245dbac0eSMatthew Wilcox  */
99301768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock)
99401768b42SPeter Zijlstra {
99501768b42SPeter Zijlstra 	might_sleep();
9963ca0ff57SPeter Zijlstra 
9973ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(lock))
99801768b42SPeter Zijlstra 		return 0;
9993ca0ff57SPeter Zijlstra 
100001768b42SPeter Zijlstra 	return __mutex_lock_killable_slowpath(lock);
100101768b42SPeter Zijlstra }
100201768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable);
100301768b42SPeter Zijlstra 
100445dbac0eSMatthew Wilcox /**
100545dbac0eSMatthew Wilcox  * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
100645dbac0eSMatthew Wilcox  * @lock: The mutex to be acquired.
100745dbac0eSMatthew Wilcox  *
100845dbac0eSMatthew Wilcox  * Lock the mutex like mutex_lock().  While the task is waiting for this
100945dbac0eSMatthew Wilcox  * mutex, it will be accounted as being in the IO wait state by the
101045dbac0eSMatthew Wilcox  * scheduler.
101145dbac0eSMatthew Wilcox  *
101245dbac0eSMatthew Wilcox  * Context: Process context.
101345dbac0eSMatthew Wilcox  */
10141460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock)
10151460cb65STejun Heo {
10161460cb65STejun Heo 	int token;
10171460cb65STejun Heo 
10181460cb65STejun Heo 	token = io_schedule_prepare();
10191460cb65STejun Heo 	mutex_lock(lock);
10201460cb65STejun Heo 	io_schedule_finish(token);
10211460cb65STejun Heo }
10221460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io);
10231460cb65STejun Heo 
10243ca0ff57SPeter Zijlstra static noinline void __sched
10253ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock)
102601768b42SPeter Zijlstra {
1027427b1820SPeter Zijlstra 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
102801768b42SPeter Zijlstra }
102901768b42SPeter Zijlstra 
103001768b42SPeter Zijlstra static noinline int __sched
103101768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock)
103201768b42SPeter Zijlstra {
1033427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
103401768b42SPeter Zijlstra }
103501768b42SPeter Zijlstra 
103601768b42SPeter Zijlstra static noinline int __sched
103701768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock)
103801768b42SPeter Zijlstra {
1039427b1820SPeter Zijlstra 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
104001768b42SPeter Zijlstra }
104101768b42SPeter Zijlstra 
104201768b42SPeter Zijlstra static noinline int __sched
104301768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
104401768b42SPeter Zijlstra {
1045cf702eddSPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0,
1046427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
104701768b42SPeter Zijlstra }
104801768b42SPeter Zijlstra 
104901768b42SPeter Zijlstra static noinline int __sched
105001768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
105101768b42SPeter Zijlstra 					    struct ww_acquire_ctx *ctx)
105201768b42SPeter Zijlstra {
1053cf702eddSPeter Zijlstra 	return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0,
1054427b1820SPeter Zijlstra 			       _RET_IP_, ctx);
105501768b42SPeter Zijlstra }
105601768b42SPeter Zijlstra 
105701768b42SPeter Zijlstra #endif
105801768b42SPeter Zijlstra 
105901768b42SPeter Zijlstra /**
106001768b42SPeter Zijlstra  * mutex_trylock - try to acquire the mutex, without waiting
106101768b42SPeter Zijlstra  * @lock: the mutex to be acquired
106201768b42SPeter Zijlstra  *
106301768b42SPeter Zijlstra  * Try to acquire the mutex atomically. Returns 1 if the mutex
106401768b42SPeter Zijlstra  * has been acquired successfully, and 0 on contention.
106501768b42SPeter Zijlstra  *
106601768b42SPeter Zijlstra  * NOTE: this function follows the spin_trylock() convention, so
106701768b42SPeter Zijlstra  * it is negated from the down_trylock() return values! Be careful
106801768b42SPeter Zijlstra  * about this when converting semaphore users to mutexes.
106901768b42SPeter Zijlstra  *
107001768b42SPeter Zijlstra  * This function must not be used in interrupt context. The
107101768b42SPeter Zijlstra  * mutex must be released by the same task that acquired it.
107201768b42SPeter Zijlstra  */
107301768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock)
107401768b42SPeter Zijlstra {
10756c11c6e3SSebastian Andrzej Siewior 	bool locked;
107601768b42SPeter Zijlstra 
1077e6b4457bSPeter Zijlstra 	MUTEX_WARN_ON(lock->magic != lock);
10786c11c6e3SSebastian Andrzej Siewior 
10796c11c6e3SSebastian Andrzej Siewior 	locked = __mutex_trylock(lock);
10803ca0ff57SPeter Zijlstra 	if (locked)
10813ca0ff57SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
108201768b42SPeter Zijlstra 
10833ca0ff57SPeter Zijlstra 	return locked;
108401768b42SPeter Zijlstra }
108501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock);
108601768b42SPeter Zijlstra 
108701768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
108801768b42SPeter Zijlstra int __sched
1089c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
109001768b42SPeter Zijlstra {
109101768b42SPeter Zijlstra 	might_sleep();
109201768b42SPeter Zijlstra 
10933ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1094ea9e0fb8SNicolai Hähnle 		if (ctx)
109501768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
10963ca0ff57SPeter Zijlstra 		return 0;
10973ca0ff57SPeter Zijlstra 	}
10983ca0ff57SPeter Zijlstra 
10993ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_slowpath(lock, ctx);
110001768b42SPeter Zijlstra }
1101c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock);
110201768b42SPeter Zijlstra 
110301768b42SPeter Zijlstra int __sched
1104c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
110501768b42SPeter Zijlstra {
110601768b42SPeter Zijlstra 	might_sleep();
110701768b42SPeter Zijlstra 
11083ca0ff57SPeter Zijlstra 	if (__mutex_trylock_fast(&lock->base)) {
1109ea9e0fb8SNicolai Hähnle 		if (ctx)
111001768b42SPeter Zijlstra 			ww_mutex_set_context_fastpath(lock, ctx);
11113ca0ff57SPeter Zijlstra 		return 0;
11123ca0ff57SPeter Zijlstra 	}
11133ca0ff57SPeter Zijlstra 
11143ca0ff57SPeter Zijlstra 	return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
111501768b42SPeter Zijlstra }
1116c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock_interruptible);
111701768b42SPeter Zijlstra 
1118bb630f9fSThomas Gleixner #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
1119bb630f9fSThomas Gleixner #endif /* !CONFIG_PREEMPT_RT */
112001768b42SPeter Zijlstra 
112101768b42SPeter Zijlstra /**
112201768b42SPeter Zijlstra  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
112301768b42SPeter Zijlstra  * @cnt: the atomic which we are to dec
112401768b42SPeter Zijlstra  * @lock: the mutex to return holding if we dec to 0
112501768b42SPeter Zijlstra  *
112601768b42SPeter Zijlstra  * return true and hold lock if we dec to 0, return false otherwise
112701768b42SPeter Zijlstra  */
112801768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
112901768b42SPeter Zijlstra {
113001768b42SPeter Zijlstra 	/* dec if we can't possibly hit 0 */
113101768b42SPeter Zijlstra 	if (atomic_add_unless(cnt, -1, 1))
113201768b42SPeter Zijlstra 		return 0;
113301768b42SPeter Zijlstra 	/* we might hit 0, so take the lock */
113401768b42SPeter Zijlstra 	mutex_lock(lock);
113501768b42SPeter Zijlstra 	if (!atomic_dec_and_test(cnt)) {
113601768b42SPeter Zijlstra 		/* when we actually did the dec, we didn't hit 0 */
113701768b42SPeter Zijlstra 		mutex_unlock(lock);
113801768b42SPeter Zijlstra 		return 0;
113901768b42SPeter Zijlstra 	}
114001768b42SPeter Zijlstra 	/* we hit 0, and we hold the lock */
114101768b42SPeter Zijlstra 	return 1;
114201768b42SPeter Zijlstra }
114301768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
1144