xref: /openbmc/linux/kernel/locking/mutex.c (revision 4bd19084faa61a8c68586e74f03f5776179f65c2)
101768b42SPeter Zijlstra /*
267a6de49SPeter Zijlstra  * kernel/locking/mutex.c
301768b42SPeter Zijlstra  *
401768b42SPeter Zijlstra  * Mutexes: blocking mutual exclusion locks
501768b42SPeter Zijlstra  *
601768b42SPeter Zijlstra  * Started by Ingo Molnar:
701768b42SPeter Zijlstra  *
801768b42SPeter Zijlstra  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
901768b42SPeter Zijlstra  *
1001768b42SPeter Zijlstra  * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
1101768b42SPeter Zijlstra  * David Howells for suggestions and improvements.
1201768b42SPeter Zijlstra  *
1301768b42SPeter Zijlstra  *  - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
1401768b42SPeter Zijlstra  *    from the -rt tree, where it was originally implemented for rtmutexes
1501768b42SPeter Zijlstra  *    by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
1601768b42SPeter Zijlstra  *    and Sven Dietrich.
1701768b42SPeter Zijlstra  *
18214e0aedSDavidlohr Bueso  * Also see Documentation/locking/mutex-design.txt.
1901768b42SPeter Zijlstra  */
2001768b42SPeter Zijlstra #include <linux/mutex.h>
2101768b42SPeter Zijlstra #include <linux/ww_mutex.h>
2201768b42SPeter Zijlstra #include <linux/sched.h>
2301768b42SPeter Zijlstra #include <linux/sched/rt.h>
2401768b42SPeter Zijlstra #include <linux/export.h>
2501768b42SPeter Zijlstra #include <linux/spinlock.h>
2601768b42SPeter Zijlstra #include <linux/interrupt.h>
2701768b42SPeter Zijlstra #include <linux/debug_locks.h>
28c9122da1SPeter Zijlstra #include "mcs_spinlock.h"
2901768b42SPeter Zijlstra 
3001768b42SPeter Zijlstra /*
3101768b42SPeter Zijlstra  * In the DEBUG case we are using the "NULL fastpath" for mutexes,
3201768b42SPeter Zijlstra  * which forces all calls into the slowpath:
3301768b42SPeter Zijlstra  */
3401768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
3501768b42SPeter Zijlstra # include "mutex-debug.h"
3601768b42SPeter Zijlstra # include <asm-generic/mutex-null.h>
376f008e72SPeter Zijlstra /*
386f008e72SPeter Zijlstra  * Must be 0 for the debug case so we do not do the unlock outside of the
396f008e72SPeter Zijlstra  * wait_lock region. debug_mutex_unlock() will do the actual unlock in this
406f008e72SPeter Zijlstra  * case.
416f008e72SPeter Zijlstra  */
426f008e72SPeter Zijlstra # undef __mutex_slowpath_needs_to_unlock
436f008e72SPeter Zijlstra # define  __mutex_slowpath_needs_to_unlock()	0
4401768b42SPeter Zijlstra #else
4501768b42SPeter Zijlstra # include "mutex.h"
4601768b42SPeter Zijlstra # include <asm/mutex.h>
4701768b42SPeter Zijlstra #endif
4801768b42SPeter Zijlstra 
4901768b42SPeter Zijlstra void
5001768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
5101768b42SPeter Zijlstra {
5201768b42SPeter Zijlstra 	atomic_set(&lock->count, 1);
5301768b42SPeter Zijlstra 	spin_lock_init(&lock->wait_lock);
5401768b42SPeter Zijlstra 	INIT_LIST_HEAD(&lock->wait_list);
5501768b42SPeter Zijlstra 	mutex_clear_owner(lock);
5601768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
574d9d951eSJason Low 	osq_lock_init(&lock->osq);
5801768b42SPeter Zijlstra #endif
5901768b42SPeter Zijlstra 
6001768b42SPeter Zijlstra 	debug_mutex_init(lock, name, key);
6101768b42SPeter Zijlstra }
6201768b42SPeter Zijlstra 
6301768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init);
6401768b42SPeter Zijlstra 
6501768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
6601768b42SPeter Zijlstra /*
6701768b42SPeter Zijlstra  * We split the mutex lock/unlock logic into separate fastpath and
6801768b42SPeter Zijlstra  * slowpath functions, to reduce the register pressure on the fastpath.
6901768b42SPeter Zijlstra  * We also put the fastpath first in the kernel image, to make sure the
7001768b42SPeter Zijlstra  * branch is predicted by the CPU as default-untaken.
7101768b42SPeter Zijlstra  */
7222d9fd34SAndi Kleen __visible void __sched __mutex_lock_slowpath(atomic_t *lock_count);
7301768b42SPeter Zijlstra 
7401768b42SPeter Zijlstra /**
7501768b42SPeter Zijlstra  * mutex_lock - acquire the mutex
7601768b42SPeter Zijlstra  * @lock: the mutex to be acquired
7701768b42SPeter Zijlstra  *
7801768b42SPeter Zijlstra  * Lock the mutex exclusively for this task. If the mutex is not
7901768b42SPeter Zijlstra  * available right now, it will sleep until it can get it.
8001768b42SPeter Zijlstra  *
8101768b42SPeter Zijlstra  * The mutex must later on be released by the same task that
8201768b42SPeter Zijlstra  * acquired it. Recursive locking is not allowed. The task
8301768b42SPeter Zijlstra  * may not exit without first unlocking the mutex. Also, kernel
8401768b42SPeter Zijlstra  * memory where the mutex resides mutex must not be freed with
8501768b42SPeter Zijlstra  * the mutex still locked. The mutex must first be initialized
8601768b42SPeter Zijlstra  * (or statically defined) before it can be locked. memset()-ing
8701768b42SPeter Zijlstra  * the mutex to 0 is not allowed.
8801768b42SPeter Zijlstra  *
8901768b42SPeter Zijlstra  * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging
9001768b42SPeter Zijlstra  *   checks that will enforce the restrictions and will also do
9101768b42SPeter Zijlstra  *   deadlock debugging. )
9201768b42SPeter Zijlstra  *
9301768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down().
9401768b42SPeter Zijlstra  */
9501768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock)
9601768b42SPeter Zijlstra {
9701768b42SPeter Zijlstra 	might_sleep();
9801768b42SPeter Zijlstra 	/*
9901768b42SPeter Zijlstra 	 * The locking fastpath is the 1->0 transition from
10001768b42SPeter Zijlstra 	 * 'unlocked' into 'locked' state.
10101768b42SPeter Zijlstra 	 */
10201768b42SPeter Zijlstra 	__mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
10301768b42SPeter Zijlstra 	mutex_set_owner(lock);
10401768b42SPeter Zijlstra }
10501768b42SPeter Zijlstra 
10601768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock);
10701768b42SPeter Zijlstra #endif
10801768b42SPeter Zijlstra 
10976916515SDavidlohr Bueso static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
11076916515SDavidlohr Bueso 						   struct ww_acquire_ctx *ww_ctx)
11176916515SDavidlohr Bueso {
11276916515SDavidlohr Bueso #ifdef CONFIG_DEBUG_MUTEXES
11376916515SDavidlohr Bueso 	/*
11476916515SDavidlohr Bueso 	 * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
11576916515SDavidlohr Bueso 	 * but released with a normal mutex_unlock in this call.
11676916515SDavidlohr Bueso 	 *
11776916515SDavidlohr Bueso 	 * This should never happen, always use ww_mutex_unlock.
11876916515SDavidlohr Bueso 	 */
11976916515SDavidlohr Bueso 	DEBUG_LOCKS_WARN_ON(ww->ctx);
12076916515SDavidlohr Bueso 
12176916515SDavidlohr Bueso 	/*
12276916515SDavidlohr Bueso 	 * Not quite done after calling ww_acquire_done() ?
12376916515SDavidlohr Bueso 	 */
12476916515SDavidlohr Bueso 	DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
12576916515SDavidlohr Bueso 
12676916515SDavidlohr Bueso 	if (ww_ctx->contending_lock) {
12776916515SDavidlohr Bueso 		/*
12876916515SDavidlohr Bueso 		 * After -EDEADLK you tried to
12976916515SDavidlohr Bueso 		 * acquire a different ww_mutex? Bad!
13076916515SDavidlohr Bueso 		 */
13176916515SDavidlohr Bueso 		DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
13276916515SDavidlohr Bueso 
13376916515SDavidlohr Bueso 		/*
13476916515SDavidlohr Bueso 		 * You called ww_mutex_lock after receiving -EDEADLK,
13576916515SDavidlohr Bueso 		 * but 'forgot' to unlock everything else first?
13676916515SDavidlohr Bueso 		 */
13776916515SDavidlohr Bueso 		DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
13876916515SDavidlohr Bueso 		ww_ctx->contending_lock = NULL;
13976916515SDavidlohr Bueso 	}
14076916515SDavidlohr Bueso 
14176916515SDavidlohr Bueso 	/*
14276916515SDavidlohr Bueso 	 * Naughty, using a different class will lead to undefined behavior!
14376916515SDavidlohr Bueso 	 */
14476916515SDavidlohr Bueso 	DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
14576916515SDavidlohr Bueso #endif
14676916515SDavidlohr Bueso 	ww_ctx->acquired++;
14776916515SDavidlohr Bueso }
14876916515SDavidlohr Bueso 
14976916515SDavidlohr Bueso /*
150*4bd19084SDavidlohr Bueso  * After acquiring lock with fastpath or when we lost out in contested
15176916515SDavidlohr Bueso  * slowpath, set ctx and wake up any waiters so they can recheck.
15276916515SDavidlohr Bueso  *
15376916515SDavidlohr Bueso  * This function is never called when CONFIG_DEBUG_LOCK_ALLOC is set,
15476916515SDavidlohr Bueso  * as the fastpath and opportunistic spinning are disabled in that case.
15576916515SDavidlohr Bueso  */
15676916515SDavidlohr Bueso static __always_inline void
15776916515SDavidlohr Bueso ww_mutex_set_context_fastpath(struct ww_mutex *lock,
15876916515SDavidlohr Bueso 			       struct ww_acquire_ctx *ctx)
15976916515SDavidlohr Bueso {
16076916515SDavidlohr Bueso 	unsigned long flags;
16176916515SDavidlohr Bueso 	struct mutex_waiter *cur;
16276916515SDavidlohr Bueso 
16376916515SDavidlohr Bueso 	ww_mutex_lock_acquired(lock, ctx);
16476916515SDavidlohr Bueso 
16576916515SDavidlohr Bueso 	lock->ctx = ctx;
16676916515SDavidlohr Bueso 
16776916515SDavidlohr Bueso 	/*
16876916515SDavidlohr Bueso 	 * The lock->ctx update should be visible on all cores before
16976916515SDavidlohr Bueso 	 * the atomic read is done, otherwise contended waiters might be
17076916515SDavidlohr Bueso 	 * missed. The contended waiters will either see ww_ctx == NULL
17176916515SDavidlohr Bueso 	 * and keep spinning, or it will acquire wait_lock, add itself
17276916515SDavidlohr Bueso 	 * to waiter list and sleep.
17376916515SDavidlohr Bueso 	 */
17476916515SDavidlohr Bueso 	smp_mb(); /* ^^^ */
17576916515SDavidlohr Bueso 
17676916515SDavidlohr Bueso 	/*
17776916515SDavidlohr Bueso 	 * Check if lock is contended, if not there is nobody to wake up
17876916515SDavidlohr Bueso 	 */
17976916515SDavidlohr Bueso 	if (likely(atomic_read(&lock->base.count) == 0))
18076916515SDavidlohr Bueso 		return;
18176916515SDavidlohr Bueso 
18276916515SDavidlohr Bueso 	/*
18376916515SDavidlohr Bueso 	 * Uh oh, we raced in fastpath, wake up everyone in this case,
18476916515SDavidlohr Bueso 	 * so they can see the new lock->ctx.
18576916515SDavidlohr Bueso 	 */
18676916515SDavidlohr Bueso 	spin_lock_mutex(&lock->base.wait_lock, flags);
18776916515SDavidlohr Bueso 	list_for_each_entry(cur, &lock->base.wait_list, list) {
18876916515SDavidlohr Bueso 		debug_mutex_wake_waiter(&lock->base, cur);
18976916515SDavidlohr Bueso 		wake_up_process(cur->task);
19076916515SDavidlohr Bueso 	}
19176916515SDavidlohr Bueso 	spin_unlock_mutex(&lock->base.wait_lock, flags);
19276916515SDavidlohr Bueso }
19376916515SDavidlohr Bueso 
194*4bd19084SDavidlohr Bueso /*
195*4bd19084SDavidlohr Bueso  * After acquiring lock in the slowpath set ctx and wake up any
196*4bd19084SDavidlohr Bueso  * waiters so they can recheck.
197*4bd19084SDavidlohr Bueso  *
198*4bd19084SDavidlohr Bueso  * Callers must hold the mutex wait_lock.
199*4bd19084SDavidlohr Bueso  */
200*4bd19084SDavidlohr Bueso static __always_inline void
201*4bd19084SDavidlohr Bueso ww_mutex_set_context_slowpath(struct ww_mutex *lock,
202*4bd19084SDavidlohr Bueso 			      struct ww_acquire_ctx *ctx)
203*4bd19084SDavidlohr Bueso {
204*4bd19084SDavidlohr Bueso 	struct mutex_waiter *cur;
205*4bd19084SDavidlohr Bueso 
206*4bd19084SDavidlohr Bueso 	ww_mutex_lock_acquired(lock, ctx);
207*4bd19084SDavidlohr Bueso 	lock->ctx = ctx;
208*4bd19084SDavidlohr Bueso 
209*4bd19084SDavidlohr Bueso 	/*
210*4bd19084SDavidlohr Bueso 	 * Give any possible sleeping processes the chance to wake up,
211*4bd19084SDavidlohr Bueso 	 * so they can recheck if they have to back off.
212*4bd19084SDavidlohr Bueso 	 */
213*4bd19084SDavidlohr Bueso 	list_for_each_entry(cur, &lock->base.wait_list, list) {
214*4bd19084SDavidlohr Bueso 		debug_mutex_wake_waiter(&lock->base, cur);
215*4bd19084SDavidlohr Bueso 		wake_up_process(cur->task);
216*4bd19084SDavidlohr Bueso 	}
217*4bd19084SDavidlohr Bueso }
21876916515SDavidlohr Bueso 
21901768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
22001768b42SPeter Zijlstra static inline bool owner_running(struct mutex *lock, struct task_struct *owner)
22101768b42SPeter Zijlstra {
22201768b42SPeter Zijlstra 	if (lock->owner != owner)
22301768b42SPeter Zijlstra 		return false;
22401768b42SPeter Zijlstra 
22501768b42SPeter Zijlstra 	/*
22601768b42SPeter Zijlstra 	 * Ensure we emit the owner->on_cpu, dereference _after_ checking
22701768b42SPeter Zijlstra 	 * lock->owner still matches owner, if that fails, owner might
22801768b42SPeter Zijlstra 	 * point to free()d memory, if it still matches, the rcu_read_lock()
22901768b42SPeter Zijlstra 	 * ensures the memory stays valid.
23001768b42SPeter Zijlstra 	 */
23101768b42SPeter Zijlstra 	barrier();
23201768b42SPeter Zijlstra 
23301768b42SPeter Zijlstra 	return owner->on_cpu;
23401768b42SPeter Zijlstra }
23501768b42SPeter Zijlstra 
23601768b42SPeter Zijlstra /*
23701768b42SPeter Zijlstra  * Look out! "owner" is an entirely speculative pointer
23801768b42SPeter Zijlstra  * access and not reliable.
23901768b42SPeter Zijlstra  */
24001768b42SPeter Zijlstra static noinline
24101768b42SPeter Zijlstra int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
24201768b42SPeter Zijlstra {
24301768b42SPeter Zijlstra 	rcu_read_lock();
24401768b42SPeter Zijlstra 	while (owner_running(lock, owner)) {
24501768b42SPeter Zijlstra 		if (need_resched())
24601768b42SPeter Zijlstra 			break;
24701768b42SPeter Zijlstra 
2483a6bfbc9SDavidlohr Bueso 		cpu_relax_lowlatency();
24901768b42SPeter Zijlstra 	}
25001768b42SPeter Zijlstra 	rcu_read_unlock();
25101768b42SPeter Zijlstra 
25201768b42SPeter Zijlstra 	/*
25301768b42SPeter Zijlstra 	 * We break out the loop above on need_resched() and when the
25401768b42SPeter Zijlstra 	 * owner changed, which is a sign for heavy contention. Return
25501768b42SPeter Zijlstra 	 * success only when lock->owner is NULL.
25601768b42SPeter Zijlstra 	 */
25701768b42SPeter Zijlstra 	return lock->owner == NULL;
25801768b42SPeter Zijlstra }
25901768b42SPeter Zijlstra 
26001768b42SPeter Zijlstra /*
26101768b42SPeter Zijlstra  * Initial check for entering the mutex spinning loop
26201768b42SPeter Zijlstra  */
26301768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock)
26401768b42SPeter Zijlstra {
26501768b42SPeter Zijlstra 	struct task_struct *owner;
26601768b42SPeter Zijlstra 	int retval = 1;
26701768b42SPeter Zijlstra 
26846af29e4SJason Low 	if (need_resched())
26946af29e4SJason Low 		return 0;
27046af29e4SJason Low 
27101768b42SPeter Zijlstra 	rcu_read_lock();
27201768b42SPeter Zijlstra 	owner = ACCESS_ONCE(lock->owner);
27301768b42SPeter Zijlstra 	if (owner)
27401768b42SPeter Zijlstra 		retval = owner->on_cpu;
27501768b42SPeter Zijlstra 	rcu_read_unlock();
27601768b42SPeter Zijlstra 	/*
27701768b42SPeter Zijlstra 	 * if lock->owner is not set, the mutex owner may have just acquired
27801768b42SPeter Zijlstra 	 * it and not set the owner yet or the mutex has been released.
27901768b42SPeter Zijlstra 	 */
28001768b42SPeter Zijlstra 	return retval;
28101768b42SPeter Zijlstra }
28276916515SDavidlohr Bueso 
28376916515SDavidlohr Bueso /*
28476916515SDavidlohr Bueso  * Atomically try to take the lock when it is available
28576916515SDavidlohr Bueso  */
28676916515SDavidlohr Bueso static inline bool mutex_try_to_acquire(struct mutex *lock)
28776916515SDavidlohr Bueso {
28876916515SDavidlohr Bueso 	return !mutex_is_locked(lock) &&
28976916515SDavidlohr Bueso 		(atomic_cmpxchg(&lock->count, 1, 0) == 1);
29076916515SDavidlohr Bueso }
29176916515SDavidlohr Bueso 
29276916515SDavidlohr Bueso /*
29376916515SDavidlohr Bueso  * Optimistic spinning.
29476916515SDavidlohr Bueso  *
29576916515SDavidlohr Bueso  * We try to spin for acquisition when we find that the lock owner
29676916515SDavidlohr Bueso  * is currently running on a (different) CPU and while we don't
29776916515SDavidlohr Bueso  * need to reschedule. The rationale is that if the lock owner is
29876916515SDavidlohr Bueso  * running, it is likely to release the lock soon.
29976916515SDavidlohr Bueso  *
30076916515SDavidlohr Bueso  * Since this needs the lock owner, and this mutex implementation
30176916515SDavidlohr Bueso  * doesn't track the owner atomically in the lock field, we need to
30276916515SDavidlohr Bueso  * track it non-atomically.
30376916515SDavidlohr Bueso  *
30476916515SDavidlohr Bueso  * We can't do this for DEBUG_MUTEXES because that relies on wait_lock
30576916515SDavidlohr Bueso  * to serialize everything.
30676916515SDavidlohr Bueso  *
30776916515SDavidlohr Bueso  * The mutex spinners are queued up using MCS lock so that only one
30876916515SDavidlohr Bueso  * spinner can compete for the mutex. However, if mutex spinning isn't
30976916515SDavidlohr Bueso  * going to happen, there is no point in going through the lock/unlock
31076916515SDavidlohr Bueso  * overhead.
31176916515SDavidlohr Bueso  *
31276916515SDavidlohr Bueso  * Returns true when the lock was taken, otherwise false, indicating
31376916515SDavidlohr Bueso  * that we need to jump to the slowpath and sleep.
31476916515SDavidlohr Bueso  */
31576916515SDavidlohr Bueso static bool mutex_optimistic_spin(struct mutex *lock,
31676916515SDavidlohr Bueso 				  struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
31776916515SDavidlohr Bueso {
31876916515SDavidlohr Bueso 	struct task_struct *task = current;
31976916515SDavidlohr Bueso 
32076916515SDavidlohr Bueso 	if (!mutex_can_spin_on_owner(lock))
32176916515SDavidlohr Bueso 		goto done;
32276916515SDavidlohr Bueso 
323e42f678aSDavidlohr Bueso 	/*
324e42f678aSDavidlohr Bueso 	 * In order to avoid a stampede of mutex spinners trying to
325e42f678aSDavidlohr Bueso 	 * acquire the mutex all at once, the spinners need to take a
326e42f678aSDavidlohr Bueso 	 * MCS (queued) lock first before spinning on the owner field.
327e42f678aSDavidlohr Bueso 	 */
32876916515SDavidlohr Bueso 	if (!osq_lock(&lock->osq))
32976916515SDavidlohr Bueso 		goto done;
33076916515SDavidlohr Bueso 
33176916515SDavidlohr Bueso 	while (true) {
33276916515SDavidlohr Bueso 		struct task_struct *owner;
33376916515SDavidlohr Bueso 
33476916515SDavidlohr Bueso 		if (use_ww_ctx && ww_ctx->acquired > 0) {
33576916515SDavidlohr Bueso 			struct ww_mutex *ww;
33676916515SDavidlohr Bueso 
33776916515SDavidlohr Bueso 			ww = container_of(lock, struct ww_mutex, base);
33876916515SDavidlohr Bueso 			/*
33976916515SDavidlohr Bueso 			 * If ww->ctx is set the contents are undefined, only
34076916515SDavidlohr Bueso 			 * by acquiring wait_lock there is a guarantee that
34176916515SDavidlohr Bueso 			 * they are not invalid when reading.
34276916515SDavidlohr Bueso 			 *
34376916515SDavidlohr Bueso 			 * As such, when deadlock detection needs to be
34476916515SDavidlohr Bueso 			 * performed the optimistic spinning cannot be done.
34576916515SDavidlohr Bueso 			 */
34676916515SDavidlohr Bueso 			if (ACCESS_ONCE(ww->ctx))
34776916515SDavidlohr Bueso 				break;
34876916515SDavidlohr Bueso 		}
34976916515SDavidlohr Bueso 
35076916515SDavidlohr Bueso 		/*
35176916515SDavidlohr Bueso 		 * If there's an owner, wait for it to either
35276916515SDavidlohr Bueso 		 * release the lock or go to sleep.
35376916515SDavidlohr Bueso 		 */
35476916515SDavidlohr Bueso 		owner = ACCESS_ONCE(lock->owner);
35576916515SDavidlohr Bueso 		if (owner && !mutex_spin_on_owner(lock, owner))
35676916515SDavidlohr Bueso 			break;
35776916515SDavidlohr Bueso 
35876916515SDavidlohr Bueso 		/* Try to acquire the mutex if it is unlocked. */
35976916515SDavidlohr Bueso 		if (mutex_try_to_acquire(lock)) {
36076916515SDavidlohr Bueso 			lock_acquired(&lock->dep_map, ip);
36176916515SDavidlohr Bueso 
36276916515SDavidlohr Bueso 			if (use_ww_ctx) {
36376916515SDavidlohr Bueso 				struct ww_mutex *ww;
36476916515SDavidlohr Bueso 				ww = container_of(lock, struct ww_mutex, base);
36576916515SDavidlohr Bueso 
36676916515SDavidlohr Bueso 				ww_mutex_set_context_fastpath(ww, ww_ctx);
36776916515SDavidlohr Bueso 			}
36876916515SDavidlohr Bueso 
36976916515SDavidlohr Bueso 			mutex_set_owner(lock);
37076916515SDavidlohr Bueso 			osq_unlock(&lock->osq);
37176916515SDavidlohr Bueso 			return true;
37276916515SDavidlohr Bueso 		}
37376916515SDavidlohr Bueso 
37476916515SDavidlohr Bueso 		/*
37576916515SDavidlohr Bueso 		 * When there's no owner, we might have preempted between the
37676916515SDavidlohr Bueso 		 * owner acquiring the lock and setting the owner field. If
37776916515SDavidlohr Bueso 		 * we're an RT task that will live-lock because we won't let
37876916515SDavidlohr Bueso 		 * the owner complete.
37976916515SDavidlohr Bueso 		 */
38076916515SDavidlohr Bueso 		if (!owner && (need_resched() || rt_task(task)))
38176916515SDavidlohr Bueso 			break;
38276916515SDavidlohr Bueso 
38376916515SDavidlohr Bueso 		/*
38476916515SDavidlohr Bueso 		 * The cpu_relax() call is a compiler barrier which forces
38576916515SDavidlohr Bueso 		 * everything in this loop to be re-loaded. We don't need
38676916515SDavidlohr Bueso 		 * memory barriers as we'll eventually observe the right
38776916515SDavidlohr Bueso 		 * values at the cost of a few extra spins.
38876916515SDavidlohr Bueso 		 */
38976916515SDavidlohr Bueso 		cpu_relax_lowlatency();
39076916515SDavidlohr Bueso 	}
39176916515SDavidlohr Bueso 
39276916515SDavidlohr Bueso 	osq_unlock(&lock->osq);
39376916515SDavidlohr Bueso done:
39476916515SDavidlohr Bueso 	/*
39576916515SDavidlohr Bueso 	 * If we fell out of the spin path because of need_resched(),
39676916515SDavidlohr Bueso 	 * reschedule now, before we try-lock the mutex. This avoids getting
39776916515SDavidlohr Bueso 	 * scheduled out right after we obtained the mutex.
39876916515SDavidlohr Bueso 	 */
3996f942a1fSPeter Zijlstra 	if (need_resched()) {
4006f942a1fSPeter Zijlstra 		/*
4016f942a1fSPeter Zijlstra 		 * We _should_ have TASK_RUNNING here, but just in case
4026f942a1fSPeter Zijlstra 		 * we do not, make it so, otherwise we might get stuck.
4036f942a1fSPeter Zijlstra 		 */
4046f942a1fSPeter Zijlstra 		__set_current_state(TASK_RUNNING);
40576916515SDavidlohr Bueso 		schedule_preempt_disabled();
4066f942a1fSPeter Zijlstra 	}
40776916515SDavidlohr Bueso 
40876916515SDavidlohr Bueso 	return false;
40976916515SDavidlohr Bueso }
41076916515SDavidlohr Bueso #else
41176916515SDavidlohr Bueso static bool mutex_optimistic_spin(struct mutex *lock,
41276916515SDavidlohr Bueso 				  struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
41376916515SDavidlohr Bueso {
41476916515SDavidlohr Bueso 	return false;
41576916515SDavidlohr Bueso }
41601768b42SPeter Zijlstra #endif
41701768b42SPeter Zijlstra 
41822d9fd34SAndi Kleen __visible __used noinline
41922d9fd34SAndi Kleen void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
42001768b42SPeter Zijlstra 
42101768b42SPeter Zijlstra /**
42201768b42SPeter Zijlstra  * mutex_unlock - release the mutex
42301768b42SPeter Zijlstra  * @lock: the mutex to be released
42401768b42SPeter Zijlstra  *
42501768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously.
42601768b42SPeter Zijlstra  *
42701768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
42801768b42SPeter Zijlstra  * of a not locked mutex is not allowed.
42901768b42SPeter Zijlstra  *
43001768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) up().
43101768b42SPeter Zijlstra  */
43201768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock)
43301768b42SPeter Zijlstra {
43401768b42SPeter Zijlstra 	/*
43501768b42SPeter Zijlstra 	 * The unlocking fastpath is the 0->1 transition from 'locked'
43601768b42SPeter Zijlstra 	 * into 'unlocked' state:
43701768b42SPeter Zijlstra 	 */
43801768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_MUTEXES
43901768b42SPeter Zijlstra 	/*
44001768b42SPeter Zijlstra 	 * When debugging is enabled we must not clear the owner before time,
44101768b42SPeter Zijlstra 	 * the slow path will always be taken, and that clears the owner field
44201768b42SPeter Zijlstra 	 * after verifying that it was indeed current.
44301768b42SPeter Zijlstra 	 */
44401768b42SPeter Zijlstra 	mutex_clear_owner(lock);
44501768b42SPeter Zijlstra #endif
44601768b42SPeter Zijlstra 	__mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
44701768b42SPeter Zijlstra }
44801768b42SPeter Zijlstra 
44901768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock);
45001768b42SPeter Zijlstra 
45101768b42SPeter Zijlstra /**
45201768b42SPeter Zijlstra  * ww_mutex_unlock - release the w/w mutex
45301768b42SPeter Zijlstra  * @lock: the mutex to be released
45401768b42SPeter Zijlstra  *
45501768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously with any of the
45601768b42SPeter Zijlstra  * ww_mutex_lock* functions (with or without an acquire context). It is
45701768b42SPeter Zijlstra  * forbidden to release the locks after releasing the acquire context.
45801768b42SPeter Zijlstra  *
45901768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
46001768b42SPeter Zijlstra  * of a unlocked mutex is not allowed.
46101768b42SPeter Zijlstra  */
46201768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock)
46301768b42SPeter Zijlstra {
46401768b42SPeter Zijlstra 	/*
46501768b42SPeter Zijlstra 	 * The unlocking fastpath is the 0->1 transition from 'locked'
46601768b42SPeter Zijlstra 	 * into 'unlocked' state:
46701768b42SPeter Zijlstra 	 */
46801768b42SPeter Zijlstra 	if (lock->ctx) {
46901768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
47001768b42SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
47101768b42SPeter Zijlstra #endif
47201768b42SPeter Zijlstra 		if (lock->ctx->acquired > 0)
47301768b42SPeter Zijlstra 			lock->ctx->acquired--;
47401768b42SPeter Zijlstra 		lock->ctx = NULL;
47501768b42SPeter Zijlstra 	}
47601768b42SPeter Zijlstra 
47701768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_MUTEXES
47801768b42SPeter Zijlstra 	/*
47901768b42SPeter Zijlstra 	 * When debugging is enabled we must not clear the owner before time,
48001768b42SPeter Zijlstra 	 * the slow path will always be taken, and that clears the owner field
48101768b42SPeter Zijlstra 	 * after verifying that it was indeed current.
48201768b42SPeter Zijlstra 	 */
48301768b42SPeter Zijlstra 	mutex_clear_owner(&lock->base);
48401768b42SPeter Zijlstra #endif
48501768b42SPeter Zijlstra 	__mutex_fastpath_unlock(&lock->base.count, __mutex_unlock_slowpath);
48601768b42SPeter Zijlstra }
48701768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock);
48801768b42SPeter Zijlstra 
48901768b42SPeter Zijlstra static inline int __sched
49063dc47e9SDavidlohr Bueso __ww_mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx)
49101768b42SPeter Zijlstra {
49201768b42SPeter Zijlstra 	struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
49301768b42SPeter Zijlstra 	struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
49401768b42SPeter Zijlstra 
49501768b42SPeter Zijlstra 	if (!hold_ctx)
49601768b42SPeter Zijlstra 		return 0;
49701768b42SPeter Zijlstra 
49801768b42SPeter Zijlstra 	if (unlikely(ctx == hold_ctx))
49901768b42SPeter Zijlstra 		return -EALREADY;
50001768b42SPeter Zijlstra 
50101768b42SPeter Zijlstra 	if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
50201768b42SPeter Zijlstra 	    (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
50301768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
50401768b42SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
50501768b42SPeter Zijlstra 		ctx->contending_lock = ww;
50601768b42SPeter Zijlstra #endif
50701768b42SPeter Zijlstra 		return -EDEADLK;
50801768b42SPeter Zijlstra 	}
50901768b42SPeter Zijlstra 
51001768b42SPeter Zijlstra 	return 0;
51101768b42SPeter Zijlstra }
51201768b42SPeter Zijlstra 
51301768b42SPeter Zijlstra /*
51401768b42SPeter Zijlstra  * Lock a mutex (possibly interruptible), slowpath:
51501768b42SPeter Zijlstra  */
51601768b42SPeter Zijlstra static __always_inline int __sched
51701768b42SPeter Zijlstra __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
51801768b42SPeter Zijlstra 		    struct lockdep_map *nest_lock, unsigned long ip,
51901768b42SPeter Zijlstra 		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
52001768b42SPeter Zijlstra {
52101768b42SPeter Zijlstra 	struct task_struct *task = current;
52201768b42SPeter Zijlstra 	struct mutex_waiter waiter;
52301768b42SPeter Zijlstra 	unsigned long flags;
52401768b42SPeter Zijlstra 	int ret;
52501768b42SPeter Zijlstra 
52601768b42SPeter Zijlstra 	preempt_disable();
52701768b42SPeter Zijlstra 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
52801768b42SPeter Zijlstra 
52976916515SDavidlohr Bueso 	if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) {
53076916515SDavidlohr Bueso 		/* got the lock, yay! */
53101768b42SPeter Zijlstra 		preempt_enable();
53201768b42SPeter Zijlstra 		return 0;
53301768b42SPeter Zijlstra 	}
53401768b42SPeter Zijlstra 
53501768b42SPeter Zijlstra 	spin_lock_mutex(&lock->wait_lock, flags);
53601768b42SPeter Zijlstra 
5371e820c96SJason Low 	/*
5381e820c96SJason Low 	 * Once more, try to acquire the lock. Only try-lock the mutex if
5390d968dd8SJason Low 	 * it is unlocked to reduce unnecessary xchg() operations.
5401e820c96SJason Low 	 */
5410d968dd8SJason Low 	if (!mutex_is_locked(lock) && (atomic_xchg(&lock->count, 0) == 1))
54201768b42SPeter Zijlstra 		goto skip_wait;
54301768b42SPeter Zijlstra 
54401768b42SPeter Zijlstra 	debug_mutex_lock_common(lock, &waiter);
54501768b42SPeter Zijlstra 	debug_mutex_add_waiter(lock, &waiter, task_thread_info(task));
54601768b42SPeter Zijlstra 
54701768b42SPeter Zijlstra 	/* add waiting tasks to the end of the waitqueue (FIFO): */
54801768b42SPeter Zijlstra 	list_add_tail(&waiter.list, &lock->wait_list);
54901768b42SPeter Zijlstra 	waiter.task = task;
55001768b42SPeter Zijlstra 
55101768b42SPeter Zijlstra 	lock_contended(&lock->dep_map, ip);
55201768b42SPeter Zijlstra 
55301768b42SPeter Zijlstra 	for (;;) {
55401768b42SPeter Zijlstra 		/*
55501768b42SPeter Zijlstra 		 * Lets try to take the lock again - this is needed even if
55601768b42SPeter Zijlstra 		 * we get here for the first time (shortly after failing to
55701768b42SPeter Zijlstra 		 * acquire the lock), to make sure that we get a wakeup once
55801768b42SPeter Zijlstra 		 * it's unlocked. Later on, if we sleep, this is the
55901768b42SPeter Zijlstra 		 * operation that gives us the lock. We xchg it to -1, so
56001768b42SPeter Zijlstra 		 * that when we release the lock, we properly wake up the
5611e820c96SJason Low 		 * other waiters. We only attempt the xchg if the count is
5621e820c96SJason Low 		 * non-negative in order to avoid unnecessary xchg operations:
56301768b42SPeter Zijlstra 		 */
5641e820c96SJason Low 		if (atomic_read(&lock->count) >= 0 &&
56501768b42SPeter Zijlstra 		    (atomic_xchg(&lock->count, -1) == 1))
56601768b42SPeter Zijlstra 			break;
56701768b42SPeter Zijlstra 
56801768b42SPeter Zijlstra 		/*
56901768b42SPeter Zijlstra 		 * got a signal? (This code gets eliminated in the
57001768b42SPeter Zijlstra 		 * TASK_UNINTERRUPTIBLE case.)
57101768b42SPeter Zijlstra 		 */
57201768b42SPeter Zijlstra 		if (unlikely(signal_pending_state(state, task))) {
57301768b42SPeter Zijlstra 			ret = -EINTR;
57401768b42SPeter Zijlstra 			goto err;
57501768b42SPeter Zijlstra 		}
57601768b42SPeter Zijlstra 
57701768b42SPeter Zijlstra 		if (use_ww_ctx && ww_ctx->acquired > 0) {
57863dc47e9SDavidlohr Bueso 			ret = __ww_mutex_lock_check_stamp(lock, ww_ctx);
57901768b42SPeter Zijlstra 			if (ret)
58001768b42SPeter Zijlstra 				goto err;
58101768b42SPeter Zijlstra 		}
58201768b42SPeter Zijlstra 
58301768b42SPeter Zijlstra 		__set_task_state(task, state);
58401768b42SPeter Zijlstra 
58501768b42SPeter Zijlstra 		/* didn't get the lock, go to sleep: */
58601768b42SPeter Zijlstra 		spin_unlock_mutex(&lock->wait_lock, flags);
58701768b42SPeter Zijlstra 		schedule_preempt_disabled();
58801768b42SPeter Zijlstra 		spin_lock_mutex(&lock->wait_lock, flags);
58901768b42SPeter Zijlstra 	}
59001768b42SPeter Zijlstra 	mutex_remove_waiter(lock, &waiter, current_thread_info());
59101768b42SPeter Zijlstra 	/* set it to 0 if there are no waiters left: */
59201768b42SPeter Zijlstra 	if (likely(list_empty(&lock->wait_list)))
59301768b42SPeter Zijlstra 		atomic_set(&lock->count, 0);
59401768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
59501768b42SPeter Zijlstra 
59601768b42SPeter Zijlstra skip_wait:
59701768b42SPeter Zijlstra 	/* got the lock - cleanup and rejoice! */
59801768b42SPeter Zijlstra 	lock_acquired(&lock->dep_map, ip);
59901768b42SPeter Zijlstra 	mutex_set_owner(lock);
60001768b42SPeter Zijlstra 
60101768b42SPeter Zijlstra 	if (use_ww_ctx) {
60201768b42SPeter Zijlstra 		struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
603*4bd19084SDavidlohr Bueso 		ww_mutex_set_context_slowpath(ww, ww_ctx);
60401768b42SPeter Zijlstra 	}
60501768b42SPeter Zijlstra 
60601768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
60701768b42SPeter Zijlstra 	preempt_enable();
60801768b42SPeter Zijlstra 	return 0;
60901768b42SPeter Zijlstra 
61001768b42SPeter Zijlstra err:
61101768b42SPeter Zijlstra 	mutex_remove_waiter(lock, &waiter, task_thread_info(task));
61201768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
61301768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
61401768b42SPeter Zijlstra 	mutex_release(&lock->dep_map, 1, ip);
61501768b42SPeter Zijlstra 	preempt_enable();
61601768b42SPeter Zijlstra 	return ret;
61701768b42SPeter Zijlstra }
61801768b42SPeter Zijlstra 
61901768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
62001768b42SPeter Zijlstra void __sched
62101768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass)
62201768b42SPeter Zijlstra {
62301768b42SPeter Zijlstra 	might_sleep();
62401768b42SPeter Zijlstra 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
62501768b42SPeter Zijlstra 			    subclass, NULL, _RET_IP_, NULL, 0);
62601768b42SPeter Zijlstra }
62701768b42SPeter Zijlstra 
62801768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested);
62901768b42SPeter Zijlstra 
63001768b42SPeter Zijlstra void __sched
63101768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
63201768b42SPeter Zijlstra {
63301768b42SPeter Zijlstra 	might_sleep();
63401768b42SPeter Zijlstra 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
63501768b42SPeter Zijlstra 			    0, nest, _RET_IP_, NULL, 0);
63601768b42SPeter Zijlstra }
63701768b42SPeter Zijlstra 
63801768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
63901768b42SPeter Zijlstra 
64001768b42SPeter Zijlstra int __sched
64101768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
64201768b42SPeter Zijlstra {
64301768b42SPeter Zijlstra 	might_sleep();
64401768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_KILLABLE,
64501768b42SPeter Zijlstra 				   subclass, NULL, _RET_IP_, NULL, 0);
64601768b42SPeter Zijlstra }
64701768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
64801768b42SPeter Zijlstra 
64901768b42SPeter Zijlstra int __sched
65001768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
65101768b42SPeter Zijlstra {
65201768b42SPeter Zijlstra 	might_sleep();
65301768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
65401768b42SPeter Zijlstra 				   subclass, NULL, _RET_IP_, NULL, 0);
65501768b42SPeter Zijlstra }
65601768b42SPeter Zijlstra 
65701768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
65801768b42SPeter Zijlstra 
65901768b42SPeter Zijlstra static inline int
66001768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
66101768b42SPeter Zijlstra {
66201768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
66301768b42SPeter Zijlstra 	unsigned tmp;
66401768b42SPeter Zijlstra 
66501768b42SPeter Zijlstra 	if (ctx->deadlock_inject_countdown-- == 0) {
66601768b42SPeter Zijlstra 		tmp = ctx->deadlock_inject_interval;
66701768b42SPeter Zijlstra 		if (tmp > UINT_MAX/4)
66801768b42SPeter Zijlstra 			tmp = UINT_MAX;
66901768b42SPeter Zijlstra 		else
67001768b42SPeter Zijlstra 			tmp = tmp*2 + tmp + tmp/2;
67101768b42SPeter Zijlstra 
67201768b42SPeter Zijlstra 		ctx->deadlock_inject_interval = tmp;
67301768b42SPeter Zijlstra 		ctx->deadlock_inject_countdown = tmp;
67401768b42SPeter Zijlstra 		ctx->contending_lock = lock;
67501768b42SPeter Zijlstra 
67601768b42SPeter Zijlstra 		ww_mutex_unlock(lock);
67701768b42SPeter Zijlstra 
67801768b42SPeter Zijlstra 		return -EDEADLK;
67901768b42SPeter Zijlstra 	}
68001768b42SPeter Zijlstra #endif
68101768b42SPeter Zijlstra 
68201768b42SPeter Zijlstra 	return 0;
68301768b42SPeter Zijlstra }
68401768b42SPeter Zijlstra 
68501768b42SPeter Zijlstra int __sched
68601768b42SPeter Zijlstra __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
68701768b42SPeter Zijlstra {
68801768b42SPeter Zijlstra 	int ret;
68901768b42SPeter Zijlstra 
69001768b42SPeter Zijlstra 	might_sleep();
69101768b42SPeter Zijlstra 	ret =  __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE,
69201768b42SPeter Zijlstra 				   0, &ctx->dep_map, _RET_IP_, ctx, 1);
69301768b42SPeter Zijlstra 	if (!ret && ctx->acquired > 1)
69401768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
69501768b42SPeter Zijlstra 
69601768b42SPeter Zijlstra 	return ret;
69701768b42SPeter Zijlstra }
69801768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(__ww_mutex_lock);
69901768b42SPeter Zijlstra 
70001768b42SPeter Zijlstra int __sched
70101768b42SPeter Zijlstra __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
70201768b42SPeter Zijlstra {
70301768b42SPeter Zijlstra 	int ret;
70401768b42SPeter Zijlstra 
70501768b42SPeter Zijlstra 	might_sleep();
70601768b42SPeter Zijlstra 	ret = __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE,
70701768b42SPeter Zijlstra 				  0, &ctx->dep_map, _RET_IP_, ctx, 1);
70801768b42SPeter Zijlstra 
70901768b42SPeter Zijlstra 	if (!ret && ctx->acquired > 1)
71001768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
71101768b42SPeter Zijlstra 
71201768b42SPeter Zijlstra 	return ret;
71301768b42SPeter Zijlstra }
71401768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
71501768b42SPeter Zijlstra 
71601768b42SPeter Zijlstra #endif
71701768b42SPeter Zijlstra 
71801768b42SPeter Zijlstra /*
71901768b42SPeter Zijlstra  * Release the lock, slowpath:
72001768b42SPeter Zijlstra  */
72101768b42SPeter Zijlstra static inline void
722242489cfSDavidlohr Bueso __mutex_unlock_common_slowpath(struct mutex *lock, int nested)
72301768b42SPeter Zijlstra {
72401768b42SPeter Zijlstra 	unsigned long flags;
72501768b42SPeter Zijlstra 
72601768b42SPeter Zijlstra 	/*
72742fa566bSDavidlohr Bueso 	 * As a performance measurement, release the lock before doing other
72842fa566bSDavidlohr Bueso 	 * wakeup related duties to follow. This allows other tasks to acquire
72942fa566bSDavidlohr Bueso 	 * the lock sooner, while still handling cleanups in past unlock calls.
73042fa566bSDavidlohr Bueso 	 * This can be done as we do not enforce strict equivalence between the
73142fa566bSDavidlohr Bueso 	 * mutex counter and wait_list.
73242fa566bSDavidlohr Bueso 	 *
73342fa566bSDavidlohr Bueso 	 *
73442fa566bSDavidlohr Bueso 	 * Some architectures leave the lock unlocked in the fastpath failure
73501768b42SPeter Zijlstra 	 * case, others need to leave it locked. In the later case we have to
73642fa566bSDavidlohr Bueso 	 * unlock it here - as the lock counter is currently 0 or negative.
73701768b42SPeter Zijlstra 	 */
73801768b42SPeter Zijlstra 	if (__mutex_slowpath_needs_to_unlock())
73901768b42SPeter Zijlstra 		atomic_set(&lock->count, 1);
74001768b42SPeter Zijlstra 
7411d8fe7dcSJason Low 	spin_lock_mutex(&lock->wait_lock, flags);
7421d8fe7dcSJason Low 	mutex_release(&lock->dep_map, nested, _RET_IP_);
7431d8fe7dcSJason Low 	debug_mutex_unlock(lock);
7441d8fe7dcSJason Low 
74501768b42SPeter Zijlstra 	if (!list_empty(&lock->wait_list)) {
74601768b42SPeter Zijlstra 		/* get the first entry from the wait-list: */
74701768b42SPeter Zijlstra 		struct mutex_waiter *waiter =
74801768b42SPeter Zijlstra 				list_entry(lock->wait_list.next,
74901768b42SPeter Zijlstra 					   struct mutex_waiter, list);
75001768b42SPeter Zijlstra 
75101768b42SPeter Zijlstra 		debug_mutex_wake_waiter(lock, waiter);
75201768b42SPeter Zijlstra 
75301768b42SPeter Zijlstra 		wake_up_process(waiter->task);
75401768b42SPeter Zijlstra 	}
75501768b42SPeter Zijlstra 
75601768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
75701768b42SPeter Zijlstra }
75801768b42SPeter Zijlstra 
75901768b42SPeter Zijlstra /*
76001768b42SPeter Zijlstra  * Release the lock, slowpath:
76101768b42SPeter Zijlstra  */
76222d9fd34SAndi Kleen __visible void
76301768b42SPeter Zijlstra __mutex_unlock_slowpath(atomic_t *lock_count)
76401768b42SPeter Zijlstra {
765242489cfSDavidlohr Bueso 	struct mutex *lock = container_of(lock_count, struct mutex, count);
766242489cfSDavidlohr Bueso 
767242489cfSDavidlohr Bueso 	__mutex_unlock_common_slowpath(lock, 1);
76801768b42SPeter Zijlstra }
76901768b42SPeter Zijlstra 
77001768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
77101768b42SPeter Zijlstra /*
77201768b42SPeter Zijlstra  * Here come the less common (and hence less performance-critical) APIs:
77301768b42SPeter Zijlstra  * mutex_lock_interruptible() and mutex_trylock().
77401768b42SPeter Zijlstra  */
77501768b42SPeter Zijlstra static noinline int __sched
77601768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock);
77701768b42SPeter Zijlstra 
77801768b42SPeter Zijlstra static noinline int __sched
77901768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock);
78001768b42SPeter Zijlstra 
78101768b42SPeter Zijlstra /**
78201768b42SPeter Zijlstra  * mutex_lock_interruptible - acquire the mutex, interruptible
78301768b42SPeter Zijlstra  * @lock: the mutex to be acquired
78401768b42SPeter Zijlstra  *
78501768b42SPeter Zijlstra  * Lock the mutex like mutex_lock(), and return 0 if the mutex has
78601768b42SPeter Zijlstra  * been acquired or sleep until the mutex becomes available. If a
78701768b42SPeter Zijlstra  * signal arrives while waiting for the lock then this function
78801768b42SPeter Zijlstra  * returns -EINTR.
78901768b42SPeter Zijlstra  *
79001768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down_interruptible().
79101768b42SPeter Zijlstra  */
79201768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock)
79301768b42SPeter Zijlstra {
79401768b42SPeter Zijlstra 	int ret;
79501768b42SPeter Zijlstra 
79601768b42SPeter Zijlstra 	might_sleep();
79701768b42SPeter Zijlstra 	ret =  __mutex_fastpath_lock_retval(&lock->count);
79801768b42SPeter Zijlstra 	if (likely(!ret)) {
79901768b42SPeter Zijlstra 		mutex_set_owner(lock);
80001768b42SPeter Zijlstra 		return 0;
80101768b42SPeter Zijlstra 	} else
80201768b42SPeter Zijlstra 		return __mutex_lock_interruptible_slowpath(lock);
80301768b42SPeter Zijlstra }
80401768b42SPeter Zijlstra 
80501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible);
80601768b42SPeter Zijlstra 
80701768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock)
80801768b42SPeter Zijlstra {
80901768b42SPeter Zijlstra 	int ret;
81001768b42SPeter Zijlstra 
81101768b42SPeter Zijlstra 	might_sleep();
81201768b42SPeter Zijlstra 	ret = __mutex_fastpath_lock_retval(&lock->count);
81301768b42SPeter Zijlstra 	if (likely(!ret)) {
81401768b42SPeter Zijlstra 		mutex_set_owner(lock);
81501768b42SPeter Zijlstra 		return 0;
81601768b42SPeter Zijlstra 	} else
81701768b42SPeter Zijlstra 		return __mutex_lock_killable_slowpath(lock);
81801768b42SPeter Zijlstra }
81901768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable);
82001768b42SPeter Zijlstra 
82122d9fd34SAndi Kleen __visible void __sched
82201768b42SPeter Zijlstra __mutex_lock_slowpath(atomic_t *lock_count)
82301768b42SPeter Zijlstra {
82401768b42SPeter Zijlstra 	struct mutex *lock = container_of(lock_count, struct mutex, count);
82501768b42SPeter Zijlstra 
82601768b42SPeter Zijlstra 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
82701768b42SPeter Zijlstra 			    NULL, _RET_IP_, NULL, 0);
82801768b42SPeter Zijlstra }
82901768b42SPeter Zijlstra 
83001768b42SPeter Zijlstra static noinline int __sched
83101768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock)
83201768b42SPeter Zijlstra {
83301768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_KILLABLE, 0,
83401768b42SPeter Zijlstra 				   NULL, _RET_IP_, NULL, 0);
83501768b42SPeter Zijlstra }
83601768b42SPeter Zijlstra 
83701768b42SPeter Zijlstra static noinline int __sched
83801768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock)
83901768b42SPeter Zijlstra {
84001768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0,
84101768b42SPeter Zijlstra 				   NULL, _RET_IP_, NULL, 0);
84201768b42SPeter Zijlstra }
84301768b42SPeter Zijlstra 
84401768b42SPeter Zijlstra static noinline int __sched
84501768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
84601768b42SPeter Zijlstra {
84701768b42SPeter Zijlstra 	return __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE, 0,
84801768b42SPeter Zijlstra 				   NULL, _RET_IP_, ctx, 1);
84901768b42SPeter Zijlstra }
85001768b42SPeter Zijlstra 
85101768b42SPeter Zijlstra static noinline int __sched
85201768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
85301768b42SPeter Zijlstra 					    struct ww_acquire_ctx *ctx)
85401768b42SPeter Zijlstra {
85501768b42SPeter Zijlstra 	return __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE, 0,
85601768b42SPeter Zijlstra 				   NULL, _RET_IP_, ctx, 1);
85701768b42SPeter Zijlstra }
85801768b42SPeter Zijlstra 
85901768b42SPeter Zijlstra #endif
86001768b42SPeter Zijlstra 
86101768b42SPeter Zijlstra /*
86201768b42SPeter Zijlstra  * Spinlock based trylock, we take the spinlock and check whether we
86301768b42SPeter Zijlstra  * can get the lock:
86401768b42SPeter Zijlstra  */
86501768b42SPeter Zijlstra static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
86601768b42SPeter Zijlstra {
86701768b42SPeter Zijlstra 	struct mutex *lock = container_of(lock_count, struct mutex, count);
86801768b42SPeter Zijlstra 	unsigned long flags;
86901768b42SPeter Zijlstra 	int prev;
87001768b42SPeter Zijlstra 
87172d5305dSJason Low 	/* No need to trylock if the mutex is locked. */
87272d5305dSJason Low 	if (mutex_is_locked(lock))
87372d5305dSJason Low 		return 0;
87472d5305dSJason Low 
87501768b42SPeter Zijlstra 	spin_lock_mutex(&lock->wait_lock, flags);
87601768b42SPeter Zijlstra 
87701768b42SPeter Zijlstra 	prev = atomic_xchg(&lock->count, -1);
87801768b42SPeter Zijlstra 	if (likely(prev == 1)) {
87901768b42SPeter Zijlstra 		mutex_set_owner(lock);
88001768b42SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
88101768b42SPeter Zijlstra 	}
88201768b42SPeter Zijlstra 
88301768b42SPeter Zijlstra 	/* Set it back to 0 if there are no waiters: */
88401768b42SPeter Zijlstra 	if (likely(list_empty(&lock->wait_list)))
88501768b42SPeter Zijlstra 		atomic_set(&lock->count, 0);
88601768b42SPeter Zijlstra 
88701768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
88801768b42SPeter Zijlstra 
88901768b42SPeter Zijlstra 	return prev == 1;
89001768b42SPeter Zijlstra }
89101768b42SPeter Zijlstra 
89201768b42SPeter Zijlstra /**
89301768b42SPeter Zijlstra  * mutex_trylock - try to acquire the mutex, without waiting
89401768b42SPeter Zijlstra  * @lock: the mutex to be acquired
89501768b42SPeter Zijlstra  *
89601768b42SPeter Zijlstra  * Try to acquire the mutex atomically. Returns 1 if the mutex
89701768b42SPeter Zijlstra  * has been acquired successfully, and 0 on contention.
89801768b42SPeter Zijlstra  *
89901768b42SPeter Zijlstra  * NOTE: this function follows the spin_trylock() convention, so
90001768b42SPeter Zijlstra  * it is negated from the down_trylock() return values! Be careful
90101768b42SPeter Zijlstra  * about this when converting semaphore users to mutexes.
90201768b42SPeter Zijlstra  *
90301768b42SPeter Zijlstra  * This function must not be used in interrupt context. The
90401768b42SPeter Zijlstra  * mutex must be released by the same task that acquired it.
90501768b42SPeter Zijlstra  */
90601768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock)
90701768b42SPeter Zijlstra {
90801768b42SPeter Zijlstra 	int ret;
90901768b42SPeter Zijlstra 
91001768b42SPeter Zijlstra 	ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath);
91101768b42SPeter Zijlstra 	if (ret)
91201768b42SPeter Zijlstra 		mutex_set_owner(lock);
91301768b42SPeter Zijlstra 
91401768b42SPeter Zijlstra 	return ret;
91501768b42SPeter Zijlstra }
91601768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock);
91701768b42SPeter Zijlstra 
91801768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
91901768b42SPeter Zijlstra int __sched
92001768b42SPeter Zijlstra __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
92101768b42SPeter Zijlstra {
92201768b42SPeter Zijlstra 	int ret;
92301768b42SPeter Zijlstra 
92401768b42SPeter Zijlstra 	might_sleep();
92501768b42SPeter Zijlstra 
92601768b42SPeter Zijlstra 	ret = __mutex_fastpath_lock_retval(&lock->base.count);
92701768b42SPeter Zijlstra 
92801768b42SPeter Zijlstra 	if (likely(!ret)) {
92901768b42SPeter Zijlstra 		ww_mutex_set_context_fastpath(lock, ctx);
93001768b42SPeter Zijlstra 		mutex_set_owner(&lock->base);
93101768b42SPeter Zijlstra 	} else
93201768b42SPeter Zijlstra 		ret = __ww_mutex_lock_slowpath(lock, ctx);
93301768b42SPeter Zijlstra 	return ret;
93401768b42SPeter Zijlstra }
93501768b42SPeter Zijlstra EXPORT_SYMBOL(__ww_mutex_lock);
93601768b42SPeter Zijlstra 
93701768b42SPeter Zijlstra int __sched
93801768b42SPeter Zijlstra __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
93901768b42SPeter Zijlstra {
94001768b42SPeter Zijlstra 	int ret;
94101768b42SPeter Zijlstra 
94201768b42SPeter Zijlstra 	might_sleep();
94301768b42SPeter Zijlstra 
94401768b42SPeter Zijlstra 	ret = __mutex_fastpath_lock_retval(&lock->base.count);
94501768b42SPeter Zijlstra 
94601768b42SPeter Zijlstra 	if (likely(!ret)) {
94701768b42SPeter Zijlstra 		ww_mutex_set_context_fastpath(lock, ctx);
94801768b42SPeter Zijlstra 		mutex_set_owner(&lock->base);
94901768b42SPeter Zijlstra 	} else
95001768b42SPeter Zijlstra 		ret = __ww_mutex_lock_interruptible_slowpath(lock, ctx);
95101768b42SPeter Zijlstra 	return ret;
95201768b42SPeter Zijlstra }
95301768b42SPeter Zijlstra EXPORT_SYMBOL(__ww_mutex_lock_interruptible);
95401768b42SPeter Zijlstra 
95501768b42SPeter Zijlstra #endif
95601768b42SPeter Zijlstra 
95701768b42SPeter Zijlstra /**
95801768b42SPeter Zijlstra  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
95901768b42SPeter Zijlstra  * @cnt: the atomic which we are to dec
96001768b42SPeter Zijlstra  * @lock: the mutex to return holding if we dec to 0
96101768b42SPeter Zijlstra  *
96201768b42SPeter Zijlstra  * return true and hold lock if we dec to 0, return false otherwise
96301768b42SPeter Zijlstra  */
96401768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
96501768b42SPeter Zijlstra {
96601768b42SPeter Zijlstra 	/* dec if we can't possibly hit 0 */
96701768b42SPeter Zijlstra 	if (atomic_add_unless(cnt, -1, 1))
96801768b42SPeter Zijlstra 		return 0;
96901768b42SPeter Zijlstra 	/* we might hit 0, so take the lock */
97001768b42SPeter Zijlstra 	mutex_lock(lock);
97101768b42SPeter Zijlstra 	if (!atomic_dec_and_test(cnt)) {
97201768b42SPeter Zijlstra 		/* when we actually did the dec, we didn't hit 0 */
97301768b42SPeter Zijlstra 		mutex_unlock(lock);
97401768b42SPeter Zijlstra 		return 0;
97501768b42SPeter Zijlstra 	}
97601768b42SPeter Zijlstra 	/* we hit 0, and we hold the lock */
97701768b42SPeter Zijlstra 	return 1;
97801768b42SPeter Zijlstra }
97901768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
980