xref: /openbmc/linux/kernel/locking/mutex.c (revision 01768b42dc97a67b4fb33a2535c49fc1969880df)
1*01768b42SPeter Zijlstra /*
2*01768b42SPeter Zijlstra  * kernel/mutex.c
3*01768b42SPeter Zijlstra  *
4*01768b42SPeter Zijlstra  * Mutexes: blocking mutual exclusion locks
5*01768b42SPeter Zijlstra  *
6*01768b42SPeter Zijlstra  * Started by Ingo Molnar:
7*01768b42SPeter Zijlstra  *
8*01768b42SPeter Zijlstra  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9*01768b42SPeter Zijlstra  *
10*01768b42SPeter Zijlstra  * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
11*01768b42SPeter Zijlstra  * David Howells for suggestions and improvements.
12*01768b42SPeter Zijlstra  *
13*01768b42SPeter Zijlstra  *  - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
14*01768b42SPeter Zijlstra  *    from the -rt tree, where it was originally implemented for rtmutexes
15*01768b42SPeter Zijlstra  *    by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
16*01768b42SPeter Zijlstra  *    and Sven Dietrich.
17*01768b42SPeter Zijlstra  *
18*01768b42SPeter Zijlstra  * Also see Documentation/mutex-design.txt.
19*01768b42SPeter Zijlstra  */
20*01768b42SPeter Zijlstra #include <linux/mutex.h>
21*01768b42SPeter Zijlstra #include <linux/ww_mutex.h>
22*01768b42SPeter Zijlstra #include <linux/sched.h>
23*01768b42SPeter Zijlstra #include <linux/sched/rt.h>
24*01768b42SPeter Zijlstra #include <linux/export.h>
25*01768b42SPeter Zijlstra #include <linux/spinlock.h>
26*01768b42SPeter Zijlstra #include <linux/interrupt.h>
27*01768b42SPeter Zijlstra #include <linux/debug_locks.h>
28*01768b42SPeter Zijlstra 
29*01768b42SPeter Zijlstra /*
30*01768b42SPeter Zijlstra  * In the DEBUG case we are using the "NULL fastpath" for mutexes,
31*01768b42SPeter Zijlstra  * which forces all calls into the slowpath:
32*01768b42SPeter Zijlstra  */
33*01768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
34*01768b42SPeter Zijlstra # include "mutex-debug.h"
35*01768b42SPeter Zijlstra # include <asm-generic/mutex-null.h>
36*01768b42SPeter Zijlstra #else
37*01768b42SPeter Zijlstra # include "mutex.h"
38*01768b42SPeter Zijlstra # include <asm/mutex.h>
39*01768b42SPeter Zijlstra #endif
40*01768b42SPeter Zijlstra 
41*01768b42SPeter Zijlstra /*
42*01768b42SPeter Zijlstra  * A negative mutex count indicates that waiters are sleeping waiting for the
43*01768b42SPeter Zijlstra  * mutex.
44*01768b42SPeter Zijlstra  */
45*01768b42SPeter Zijlstra #define	MUTEX_SHOW_NO_WAITER(mutex)	(atomic_read(&(mutex)->count) >= 0)
46*01768b42SPeter Zijlstra 
47*01768b42SPeter Zijlstra void
48*01768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
49*01768b42SPeter Zijlstra {
50*01768b42SPeter Zijlstra 	atomic_set(&lock->count, 1);
51*01768b42SPeter Zijlstra 	spin_lock_init(&lock->wait_lock);
52*01768b42SPeter Zijlstra 	INIT_LIST_HEAD(&lock->wait_list);
53*01768b42SPeter Zijlstra 	mutex_clear_owner(lock);
54*01768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
55*01768b42SPeter Zijlstra 	lock->spin_mlock = NULL;
56*01768b42SPeter Zijlstra #endif
57*01768b42SPeter Zijlstra 
58*01768b42SPeter Zijlstra 	debug_mutex_init(lock, name, key);
59*01768b42SPeter Zijlstra }
60*01768b42SPeter Zijlstra 
61*01768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init);
62*01768b42SPeter Zijlstra 
63*01768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
64*01768b42SPeter Zijlstra /*
65*01768b42SPeter Zijlstra  * We split the mutex lock/unlock logic into separate fastpath and
66*01768b42SPeter Zijlstra  * slowpath functions, to reduce the register pressure on the fastpath.
67*01768b42SPeter Zijlstra  * We also put the fastpath first in the kernel image, to make sure the
68*01768b42SPeter Zijlstra  * branch is predicted by the CPU as default-untaken.
69*01768b42SPeter Zijlstra  */
70*01768b42SPeter Zijlstra static __used noinline void __sched
71*01768b42SPeter Zijlstra __mutex_lock_slowpath(atomic_t *lock_count);
72*01768b42SPeter Zijlstra 
73*01768b42SPeter Zijlstra /**
74*01768b42SPeter Zijlstra  * mutex_lock - acquire the mutex
75*01768b42SPeter Zijlstra  * @lock: the mutex to be acquired
76*01768b42SPeter Zijlstra  *
77*01768b42SPeter Zijlstra  * Lock the mutex exclusively for this task. If the mutex is not
78*01768b42SPeter Zijlstra  * available right now, it will sleep until it can get it.
79*01768b42SPeter Zijlstra  *
80*01768b42SPeter Zijlstra  * The mutex must later on be released by the same task that
81*01768b42SPeter Zijlstra  * acquired it. Recursive locking is not allowed. The task
82*01768b42SPeter Zijlstra  * may not exit without first unlocking the mutex. Also, kernel
83*01768b42SPeter Zijlstra  * memory where the mutex resides mutex must not be freed with
84*01768b42SPeter Zijlstra  * the mutex still locked. The mutex must first be initialized
85*01768b42SPeter Zijlstra  * (or statically defined) before it can be locked. memset()-ing
86*01768b42SPeter Zijlstra  * the mutex to 0 is not allowed.
87*01768b42SPeter Zijlstra  *
88*01768b42SPeter Zijlstra  * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging
89*01768b42SPeter Zijlstra  *   checks that will enforce the restrictions and will also do
90*01768b42SPeter Zijlstra  *   deadlock debugging. )
91*01768b42SPeter Zijlstra  *
92*01768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down().
93*01768b42SPeter Zijlstra  */
94*01768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock)
95*01768b42SPeter Zijlstra {
96*01768b42SPeter Zijlstra 	might_sleep();
97*01768b42SPeter Zijlstra 	/*
98*01768b42SPeter Zijlstra 	 * The locking fastpath is the 1->0 transition from
99*01768b42SPeter Zijlstra 	 * 'unlocked' into 'locked' state.
100*01768b42SPeter Zijlstra 	 */
101*01768b42SPeter Zijlstra 	__mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
102*01768b42SPeter Zijlstra 	mutex_set_owner(lock);
103*01768b42SPeter Zijlstra }
104*01768b42SPeter Zijlstra 
105*01768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock);
106*01768b42SPeter Zijlstra #endif
107*01768b42SPeter Zijlstra 
108*01768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
109*01768b42SPeter Zijlstra /*
110*01768b42SPeter Zijlstra  * In order to avoid a stampede of mutex spinners from acquiring the mutex
111*01768b42SPeter Zijlstra  * more or less simultaneously, the spinners need to acquire a MCS lock
112*01768b42SPeter Zijlstra  * first before spinning on the owner field.
113*01768b42SPeter Zijlstra  *
114*01768b42SPeter Zijlstra  * We don't inline mspin_lock() so that perf can correctly account for the
115*01768b42SPeter Zijlstra  * time spent in this lock function.
116*01768b42SPeter Zijlstra  */
117*01768b42SPeter Zijlstra struct mspin_node {
118*01768b42SPeter Zijlstra 	struct mspin_node *next ;
119*01768b42SPeter Zijlstra 	int		  locked;	/* 1 if lock acquired */
120*01768b42SPeter Zijlstra };
121*01768b42SPeter Zijlstra #define	MLOCK(mutex)	((struct mspin_node **)&((mutex)->spin_mlock))
122*01768b42SPeter Zijlstra 
123*01768b42SPeter Zijlstra static noinline
124*01768b42SPeter Zijlstra void mspin_lock(struct mspin_node **lock, struct mspin_node *node)
125*01768b42SPeter Zijlstra {
126*01768b42SPeter Zijlstra 	struct mspin_node *prev;
127*01768b42SPeter Zijlstra 
128*01768b42SPeter Zijlstra 	/* Init node */
129*01768b42SPeter Zijlstra 	node->locked = 0;
130*01768b42SPeter Zijlstra 	node->next   = NULL;
131*01768b42SPeter Zijlstra 
132*01768b42SPeter Zijlstra 	prev = xchg(lock, node);
133*01768b42SPeter Zijlstra 	if (likely(prev == NULL)) {
134*01768b42SPeter Zijlstra 		/* Lock acquired */
135*01768b42SPeter Zijlstra 		node->locked = 1;
136*01768b42SPeter Zijlstra 		return;
137*01768b42SPeter Zijlstra 	}
138*01768b42SPeter Zijlstra 	ACCESS_ONCE(prev->next) = node;
139*01768b42SPeter Zijlstra 	smp_wmb();
140*01768b42SPeter Zijlstra 	/* Wait until the lock holder passes the lock down */
141*01768b42SPeter Zijlstra 	while (!ACCESS_ONCE(node->locked))
142*01768b42SPeter Zijlstra 		arch_mutex_cpu_relax();
143*01768b42SPeter Zijlstra }
144*01768b42SPeter Zijlstra 
145*01768b42SPeter Zijlstra static void mspin_unlock(struct mspin_node **lock, struct mspin_node *node)
146*01768b42SPeter Zijlstra {
147*01768b42SPeter Zijlstra 	struct mspin_node *next = ACCESS_ONCE(node->next);
148*01768b42SPeter Zijlstra 
149*01768b42SPeter Zijlstra 	if (likely(!next)) {
150*01768b42SPeter Zijlstra 		/*
151*01768b42SPeter Zijlstra 		 * Release the lock by setting it to NULL
152*01768b42SPeter Zijlstra 		 */
153*01768b42SPeter Zijlstra 		if (cmpxchg(lock, node, NULL) == node)
154*01768b42SPeter Zijlstra 			return;
155*01768b42SPeter Zijlstra 		/* Wait until the next pointer is set */
156*01768b42SPeter Zijlstra 		while (!(next = ACCESS_ONCE(node->next)))
157*01768b42SPeter Zijlstra 			arch_mutex_cpu_relax();
158*01768b42SPeter Zijlstra 	}
159*01768b42SPeter Zijlstra 	ACCESS_ONCE(next->locked) = 1;
160*01768b42SPeter Zijlstra 	smp_wmb();
161*01768b42SPeter Zijlstra }
162*01768b42SPeter Zijlstra 
163*01768b42SPeter Zijlstra /*
164*01768b42SPeter Zijlstra  * Mutex spinning code migrated from kernel/sched/core.c
165*01768b42SPeter Zijlstra  */
166*01768b42SPeter Zijlstra 
167*01768b42SPeter Zijlstra static inline bool owner_running(struct mutex *lock, struct task_struct *owner)
168*01768b42SPeter Zijlstra {
169*01768b42SPeter Zijlstra 	if (lock->owner != owner)
170*01768b42SPeter Zijlstra 		return false;
171*01768b42SPeter Zijlstra 
172*01768b42SPeter Zijlstra 	/*
173*01768b42SPeter Zijlstra 	 * Ensure we emit the owner->on_cpu, dereference _after_ checking
174*01768b42SPeter Zijlstra 	 * lock->owner still matches owner, if that fails, owner might
175*01768b42SPeter Zijlstra 	 * point to free()d memory, if it still matches, the rcu_read_lock()
176*01768b42SPeter Zijlstra 	 * ensures the memory stays valid.
177*01768b42SPeter Zijlstra 	 */
178*01768b42SPeter Zijlstra 	barrier();
179*01768b42SPeter Zijlstra 
180*01768b42SPeter Zijlstra 	return owner->on_cpu;
181*01768b42SPeter Zijlstra }
182*01768b42SPeter Zijlstra 
183*01768b42SPeter Zijlstra /*
184*01768b42SPeter Zijlstra  * Look out! "owner" is an entirely speculative pointer
185*01768b42SPeter Zijlstra  * access and not reliable.
186*01768b42SPeter Zijlstra  */
187*01768b42SPeter Zijlstra static noinline
188*01768b42SPeter Zijlstra int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
189*01768b42SPeter Zijlstra {
190*01768b42SPeter Zijlstra 	rcu_read_lock();
191*01768b42SPeter Zijlstra 	while (owner_running(lock, owner)) {
192*01768b42SPeter Zijlstra 		if (need_resched())
193*01768b42SPeter Zijlstra 			break;
194*01768b42SPeter Zijlstra 
195*01768b42SPeter Zijlstra 		arch_mutex_cpu_relax();
196*01768b42SPeter Zijlstra 	}
197*01768b42SPeter Zijlstra 	rcu_read_unlock();
198*01768b42SPeter Zijlstra 
199*01768b42SPeter Zijlstra 	/*
200*01768b42SPeter Zijlstra 	 * We break out the loop above on need_resched() and when the
201*01768b42SPeter Zijlstra 	 * owner changed, which is a sign for heavy contention. Return
202*01768b42SPeter Zijlstra 	 * success only when lock->owner is NULL.
203*01768b42SPeter Zijlstra 	 */
204*01768b42SPeter Zijlstra 	return lock->owner == NULL;
205*01768b42SPeter Zijlstra }
206*01768b42SPeter Zijlstra 
207*01768b42SPeter Zijlstra /*
208*01768b42SPeter Zijlstra  * Initial check for entering the mutex spinning loop
209*01768b42SPeter Zijlstra  */
210*01768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock)
211*01768b42SPeter Zijlstra {
212*01768b42SPeter Zijlstra 	struct task_struct *owner;
213*01768b42SPeter Zijlstra 	int retval = 1;
214*01768b42SPeter Zijlstra 
215*01768b42SPeter Zijlstra 	rcu_read_lock();
216*01768b42SPeter Zijlstra 	owner = ACCESS_ONCE(lock->owner);
217*01768b42SPeter Zijlstra 	if (owner)
218*01768b42SPeter Zijlstra 		retval = owner->on_cpu;
219*01768b42SPeter Zijlstra 	rcu_read_unlock();
220*01768b42SPeter Zijlstra 	/*
221*01768b42SPeter Zijlstra 	 * if lock->owner is not set, the mutex owner may have just acquired
222*01768b42SPeter Zijlstra 	 * it and not set the owner yet or the mutex has been released.
223*01768b42SPeter Zijlstra 	 */
224*01768b42SPeter Zijlstra 	return retval;
225*01768b42SPeter Zijlstra }
226*01768b42SPeter Zijlstra #endif
227*01768b42SPeter Zijlstra 
228*01768b42SPeter Zijlstra static __used noinline void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
229*01768b42SPeter Zijlstra 
230*01768b42SPeter Zijlstra /**
231*01768b42SPeter Zijlstra  * mutex_unlock - release the mutex
232*01768b42SPeter Zijlstra  * @lock: the mutex to be released
233*01768b42SPeter Zijlstra  *
234*01768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously.
235*01768b42SPeter Zijlstra  *
236*01768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
237*01768b42SPeter Zijlstra  * of a not locked mutex is not allowed.
238*01768b42SPeter Zijlstra  *
239*01768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) up().
240*01768b42SPeter Zijlstra  */
241*01768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock)
242*01768b42SPeter Zijlstra {
243*01768b42SPeter Zijlstra 	/*
244*01768b42SPeter Zijlstra 	 * The unlocking fastpath is the 0->1 transition from 'locked'
245*01768b42SPeter Zijlstra 	 * into 'unlocked' state:
246*01768b42SPeter Zijlstra 	 */
247*01768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_MUTEXES
248*01768b42SPeter Zijlstra 	/*
249*01768b42SPeter Zijlstra 	 * When debugging is enabled we must not clear the owner before time,
250*01768b42SPeter Zijlstra 	 * the slow path will always be taken, and that clears the owner field
251*01768b42SPeter Zijlstra 	 * after verifying that it was indeed current.
252*01768b42SPeter Zijlstra 	 */
253*01768b42SPeter Zijlstra 	mutex_clear_owner(lock);
254*01768b42SPeter Zijlstra #endif
255*01768b42SPeter Zijlstra 	__mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
256*01768b42SPeter Zijlstra }
257*01768b42SPeter Zijlstra 
258*01768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock);
259*01768b42SPeter Zijlstra 
260*01768b42SPeter Zijlstra /**
261*01768b42SPeter Zijlstra  * ww_mutex_unlock - release the w/w mutex
262*01768b42SPeter Zijlstra  * @lock: the mutex to be released
263*01768b42SPeter Zijlstra  *
264*01768b42SPeter Zijlstra  * Unlock a mutex that has been locked by this task previously with any of the
265*01768b42SPeter Zijlstra  * ww_mutex_lock* functions (with or without an acquire context). It is
266*01768b42SPeter Zijlstra  * forbidden to release the locks after releasing the acquire context.
267*01768b42SPeter Zijlstra  *
268*01768b42SPeter Zijlstra  * This function must not be used in interrupt context. Unlocking
269*01768b42SPeter Zijlstra  * of a unlocked mutex is not allowed.
270*01768b42SPeter Zijlstra  */
271*01768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock)
272*01768b42SPeter Zijlstra {
273*01768b42SPeter Zijlstra 	/*
274*01768b42SPeter Zijlstra 	 * The unlocking fastpath is the 0->1 transition from 'locked'
275*01768b42SPeter Zijlstra 	 * into 'unlocked' state:
276*01768b42SPeter Zijlstra 	 */
277*01768b42SPeter Zijlstra 	if (lock->ctx) {
278*01768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
279*01768b42SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
280*01768b42SPeter Zijlstra #endif
281*01768b42SPeter Zijlstra 		if (lock->ctx->acquired > 0)
282*01768b42SPeter Zijlstra 			lock->ctx->acquired--;
283*01768b42SPeter Zijlstra 		lock->ctx = NULL;
284*01768b42SPeter Zijlstra 	}
285*01768b42SPeter Zijlstra 
286*01768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_MUTEXES
287*01768b42SPeter Zijlstra 	/*
288*01768b42SPeter Zijlstra 	 * When debugging is enabled we must not clear the owner before time,
289*01768b42SPeter Zijlstra 	 * the slow path will always be taken, and that clears the owner field
290*01768b42SPeter Zijlstra 	 * after verifying that it was indeed current.
291*01768b42SPeter Zijlstra 	 */
292*01768b42SPeter Zijlstra 	mutex_clear_owner(&lock->base);
293*01768b42SPeter Zijlstra #endif
294*01768b42SPeter Zijlstra 	__mutex_fastpath_unlock(&lock->base.count, __mutex_unlock_slowpath);
295*01768b42SPeter Zijlstra }
296*01768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock);
297*01768b42SPeter Zijlstra 
298*01768b42SPeter Zijlstra static inline int __sched
299*01768b42SPeter Zijlstra __mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx)
300*01768b42SPeter Zijlstra {
301*01768b42SPeter Zijlstra 	struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
302*01768b42SPeter Zijlstra 	struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
303*01768b42SPeter Zijlstra 
304*01768b42SPeter Zijlstra 	if (!hold_ctx)
305*01768b42SPeter Zijlstra 		return 0;
306*01768b42SPeter Zijlstra 
307*01768b42SPeter Zijlstra 	if (unlikely(ctx == hold_ctx))
308*01768b42SPeter Zijlstra 		return -EALREADY;
309*01768b42SPeter Zijlstra 
310*01768b42SPeter Zijlstra 	if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
311*01768b42SPeter Zijlstra 	    (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
312*01768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
313*01768b42SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
314*01768b42SPeter Zijlstra 		ctx->contending_lock = ww;
315*01768b42SPeter Zijlstra #endif
316*01768b42SPeter Zijlstra 		return -EDEADLK;
317*01768b42SPeter Zijlstra 	}
318*01768b42SPeter Zijlstra 
319*01768b42SPeter Zijlstra 	return 0;
320*01768b42SPeter Zijlstra }
321*01768b42SPeter Zijlstra 
322*01768b42SPeter Zijlstra static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
323*01768b42SPeter Zijlstra 						   struct ww_acquire_ctx *ww_ctx)
324*01768b42SPeter Zijlstra {
325*01768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES
326*01768b42SPeter Zijlstra 	/*
327*01768b42SPeter Zijlstra 	 * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
328*01768b42SPeter Zijlstra 	 * but released with a normal mutex_unlock in this call.
329*01768b42SPeter Zijlstra 	 *
330*01768b42SPeter Zijlstra 	 * This should never happen, always use ww_mutex_unlock.
331*01768b42SPeter Zijlstra 	 */
332*01768b42SPeter Zijlstra 	DEBUG_LOCKS_WARN_ON(ww->ctx);
333*01768b42SPeter Zijlstra 
334*01768b42SPeter Zijlstra 	/*
335*01768b42SPeter Zijlstra 	 * Not quite done after calling ww_acquire_done() ?
336*01768b42SPeter Zijlstra 	 */
337*01768b42SPeter Zijlstra 	DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
338*01768b42SPeter Zijlstra 
339*01768b42SPeter Zijlstra 	if (ww_ctx->contending_lock) {
340*01768b42SPeter Zijlstra 		/*
341*01768b42SPeter Zijlstra 		 * After -EDEADLK you tried to
342*01768b42SPeter Zijlstra 		 * acquire a different ww_mutex? Bad!
343*01768b42SPeter Zijlstra 		 */
344*01768b42SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
345*01768b42SPeter Zijlstra 
346*01768b42SPeter Zijlstra 		/*
347*01768b42SPeter Zijlstra 		 * You called ww_mutex_lock after receiving -EDEADLK,
348*01768b42SPeter Zijlstra 		 * but 'forgot' to unlock everything else first?
349*01768b42SPeter Zijlstra 		 */
350*01768b42SPeter Zijlstra 		DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
351*01768b42SPeter Zijlstra 		ww_ctx->contending_lock = NULL;
352*01768b42SPeter Zijlstra 	}
353*01768b42SPeter Zijlstra 
354*01768b42SPeter Zijlstra 	/*
355*01768b42SPeter Zijlstra 	 * Naughty, using a different class will lead to undefined behavior!
356*01768b42SPeter Zijlstra 	 */
357*01768b42SPeter Zijlstra 	DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
358*01768b42SPeter Zijlstra #endif
359*01768b42SPeter Zijlstra 	ww_ctx->acquired++;
360*01768b42SPeter Zijlstra }
361*01768b42SPeter Zijlstra 
362*01768b42SPeter Zijlstra /*
363*01768b42SPeter Zijlstra  * after acquiring lock with fastpath or when we lost out in contested
364*01768b42SPeter Zijlstra  * slowpath, set ctx and wake up any waiters so they can recheck.
365*01768b42SPeter Zijlstra  *
366*01768b42SPeter Zijlstra  * This function is never called when CONFIG_DEBUG_LOCK_ALLOC is set,
367*01768b42SPeter Zijlstra  * as the fastpath and opportunistic spinning are disabled in that case.
368*01768b42SPeter Zijlstra  */
369*01768b42SPeter Zijlstra static __always_inline void
370*01768b42SPeter Zijlstra ww_mutex_set_context_fastpath(struct ww_mutex *lock,
371*01768b42SPeter Zijlstra 			       struct ww_acquire_ctx *ctx)
372*01768b42SPeter Zijlstra {
373*01768b42SPeter Zijlstra 	unsigned long flags;
374*01768b42SPeter Zijlstra 	struct mutex_waiter *cur;
375*01768b42SPeter Zijlstra 
376*01768b42SPeter Zijlstra 	ww_mutex_lock_acquired(lock, ctx);
377*01768b42SPeter Zijlstra 
378*01768b42SPeter Zijlstra 	lock->ctx = ctx;
379*01768b42SPeter Zijlstra 
380*01768b42SPeter Zijlstra 	/*
381*01768b42SPeter Zijlstra 	 * The lock->ctx update should be visible on all cores before
382*01768b42SPeter Zijlstra 	 * the atomic read is done, otherwise contended waiters might be
383*01768b42SPeter Zijlstra 	 * missed. The contended waiters will either see ww_ctx == NULL
384*01768b42SPeter Zijlstra 	 * and keep spinning, or it will acquire wait_lock, add itself
385*01768b42SPeter Zijlstra 	 * to waiter list and sleep.
386*01768b42SPeter Zijlstra 	 */
387*01768b42SPeter Zijlstra 	smp_mb(); /* ^^^ */
388*01768b42SPeter Zijlstra 
389*01768b42SPeter Zijlstra 	/*
390*01768b42SPeter Zijlstra 	 * Check if lock is contended, if not there is nobody to wake up
391*01768b42SPeter Zijlstra 	 */
392*01768b42SPeter Zijlstra 	if (likely(atomic_read(&lock->base.count) == 0))
393*01768b42SPeter Zijlstra 		return;
394*01768b42SPeter Zijlstra 
395*01768b42SPeter Zijlstra 	/*
396*01768b42SPeter Zijlstra 	 * Uh oh, we raced in fastpath, wake up everyone in this case,
397*01768b42SPeter Zijlstra 	 * so they can see the new lock->ctx.
398*01768b42SPeter Zijlstra 	 */
399*01768b42SPeter Zijlstra 	spin_lock_mutex(&lock->base.wait_lock, flags);
400*01768b42SPeter Zijlstra 	list_for_each_entry(cur, &lock->base.wait_list, list) {
401*01768b42SPeter Zijlstra 		debug_mutex_wake_waiter(&lock->base, cur);
402*01768b42SPeter Zijlstra 		wake_up_process(cur->task);
403*01768b42SPeter Zijlstra 	}
404*01768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->base.wait_lock, flags);
405*01768b42SPeter Zijlstra }
406*01768b42SPeter Zijlstra 
407*01768b42SPeter Zijlstra /*
408*01768b42SPeter Zijlstra  * Lock a mutex (possibly interruptible), slowpath:
409*01768b42SPeter Zijlstra  */
410*01768b42SPeter Zijlstra static __always_inline int __sched
411*01768b42SPeter Zijlstra __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
412*01768b42SPeter Zijlstra 		    struct lockdep_map *nest_lock, unsigned long ip,
413*01768b42SPeter Zijlstra 		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
414*01768b42SPeter Zijlstra {
415*01768b42SPeter Zijlstra 	struct task_struct *task = current;
416*01768b42SPeter Zijlstra 	struct mutex_waiter waiter;
417*01768b42SPeter Zijlstra 	unsigned long flags;
418*01768b42SPeter Zijlstra 	int ret;
419*01768b42SPeter Zijlstra 
420*01768b42SPeter Zijlstra 	preempt_disable();
421*01768b42SPeter Zijlstra 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
422*01768b42SPeter Zijlstra 
423*01768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
424*01768b42SPeter Zijlstra 	/*
425*01768b42SPeter Zijlstra 	 * Optimistic spinning.
426*01768b42SPeter Zijlstra 	 *
427*01768b42SPeter Zijlstra 	 * We try to spin for acquisition when we find that there are no
428*01768b42SPeter Zijlstra 	 * pending waiters and the lock owner is currently running on a
429*01768b42SPeter Zijlstra 	 * (different) CPU.
430*01768b42SPeter Zijlstra 	 *
431*01768b42SPeter Zijlstra 	 * The rationale is that if the lock owner is running, it is likely to
432*01768b42SPeter Zijlstra 	 * release the lock soon.
433*01768b42SPeter Zijlstra 	 *
434*01768b42SPeter Zijlstra 	 * Since this needs the lock owner, and this mutex implementation
435*01768b42SPeter Zijlstra 	 * doesn't track the owner atomically in the lock field, we need to
436*01768b42SPeter Zijlstra 	 * track it non-atomically.
437*01768b42SPeter Zijlstra 	 *
438*01768b42SPeter Zijlstra 	 * We can't do this for DEBUG_MUTEXES because that relies on wait_lock
439*01768b42SPeter Zijlstra 	 * to serialize everything.
440*01768b42SPeter Zijlstra 	 *
441*01768b42SPeter Zijlstra 	 * The mutex spinners are queued up using MCS lock so that only one
442*01768b42SPeter Zijlstra 	 * spinner can compete for the mutex. However, if mutex spinning isn't
443*01768b42SPeter Zijlstra 	 * going to happen, there is no point in going through the lock/unlock
444*01768b42SPeter Zijlstra 	 * overhead.
445*01768b42SPeter Zijlstra 	 */
446*01768b42SPeter Zijlstra 	if (!mutex_can_spin_on_owner(lock))
447*01768b42SPeter Zijlstra 		goto slowpath;
448*01768b42SPeter Zijlstra 
449*01768b42SPeter Zijlstra 	for (;;) {
450*01768b42SPeter Zijlstra 		struct task_struct *owner;
451*01768b42SPeter Zijlstra 		struct mspin_node  node;
452*01768b42SPeter Zijlstra 
453*01768b42SPeter Zijlstra 		if (use_ww_ctx && ww_ctx->acquired > 0) {
454*01768b42SPeter Zijlstra 			struct ww_mutex *ww;
455*01768b42SPeter Zijlstra 
456*01768b42SPeter Zijlstra 			ww = container_of(lock, struct ww_mutex, base);
457*01768b42SPeter Zijlstra 			/*
458*01768b42SPeter Zijlstra 			 * If ww->ctx is set the contents are undefined, only
459*01768b42SPeter Zijlstra 			 * by acquiring wait_lock there is a guarantee that
460*01768b42SPeter Zijlstra 			 * they are not invalid when reading.
461*01768b42SPeter Zijlstra 			 *
462*01768b42SPeter Zijlstra 			 * As such, when deadlock detection needs to be
463*01768b42SPeter Zijlstra 			 * performed the optimistic spinning cannot be done.
464*01768b42SPeter Zijlstra 			 */
465*01768b42SPeter Zijlstra 			if (ACCESS_ONCE(ww->ctx))
466*01768b42SPeter Zijlstra 				goto slowpath;
467*01768b42SPeter Zijlstra 		}
468*01768b42SPeter Zijlstra 
469*01768b42SPeter Zijlstra 		/*
470*01768b42SPeter Zijlstra 		 * If there's an owner, wait for it to either
471*01768b42SPeter Zijlstra 		 * release the lock or go to sleep.
472*01768b42SPeter Zijlstra 		 */
473*01768b42SPeter Zijlstra 		mspin_lock(MLOCK(lock), &node);
474*01768b42SPeter Zijlstra 		owner = ACCESS_ONCE(lock->owner);
475*01768b42SPeter Zijlstra 		if (owner && !mutex_spin_on_owner(lock, owner)) {
476*01768b42SPeter Zijlstra 			mspin_unlock(MLOCK(lock), &node);
477*01768b42SPeter Zijlstra 			goto slowpath;
478*01768b42SPeter Zijlstra 		}
479*01768b42SPeter Zijlstra 
480*01768b42SPeter Zijlstra 		if ((atomic_read(&lock->count) == 1) &&
481*01768b42SPeter Zijlstra 		    (atomic_cmpxchg(&lock->count, 1, 0) == 1)) {
482*01768b42SPeter Zijlstra 			lock_acquired(&lock->dep_map, ip);
483*01768b42SPeter Zijlstra 			if (use_ww_ctx) {
484*01768b42SPeter Zijlstra 				struct ww_mutex *ww;
485*01768b42SPeter Zijlstra 				ww = container_of(lock, struct ww_mutex, base);
486*01768b42SPeter Zijlstra 
487*01768b42SPeter Zijlstra 				ww_mutex_set_context_fastpath(ww, ww_ctx);
488*01768b42SPeter Zijlstra 			}
489*01768b42SPeter Zijlstra 
490*01768b42SPeter Zijlstra 			mutex_set_owner(lock);
491*01768b42SPeter Zijlstra 			mspin_unlock(MLOCK(lock), &node);
492*01768b42SPeter Zijlstra 			preempt_enable();
493*01768b42SPeter Zijlstra 			return 0;
494*01768b42SPeter Zijlstra 		}
495*01768b42SPeter Zijlstra 		mspin_unlock(MLOCK(lock), &node);
496*01768b42SPeter Zijlstra 
497*01768b42SPeter Zijlstra 		/*
498*01768b42SPeter Zijlstra 		 * When there's no owner, we might have preempted between the
499*01768b42SPeter Zijlstra 		 * owner acquiring the lock and setting the owner field. If
500*01768b42SPeter Zijlstra 		 * we're an RT task that will live-lock because we won't let
501*01768b42SPeter Zijlstra 		 * the owner complete.
502*01768b42SPeter Zijlstra 		 */
503*01768b42SPeter Zijlstra 		if (!owner && (need_resched() || rt_task(task)))
504*01768b42SPeter Zijlstra 			goto slowpath;
505*01768b42SPeter Zijlstra 
506*01768b42SPeter Zijlstra 		/*
507*01768b42SPeter Zijlstra 		 * The cpu_relax() call is a compiler barrier which forces
508*01768b42SPeter Zijlstra 		 * everything in this loop to be re-loaded. We don't need
509*01768b42SPeter Zijlstra 		 * memory barriers as we'll eventually observe the right
510*01768b42SPeter Zijlstra 		 * values at the cost of a few extra spins.
511*01768b42SPeter Zijlstra 		 */
512*01768b42SPeter Zijlstra 		arch_mutex_cpu_relax();
513*01768b42SPeter Zijlstra 	}
514*01768b42SPeter Zijlstra slowpath:
515*01768b42SPeter Zijlstra #endif
516*01768b42SPeter Zijlstra 	spin_lock_mutex(&lock->wait_lock, flags);
517*01768b42SPeter Zijlstra 
518*01768b42SPeter Zijlstra 	/* once more, can we acquire the lock? */
519*01768b42SPeter Zijlstra 	if (MUTEX_SHOW_NO_WAITER(lock) && (atomic_xchg(&lock->count, 0) == 1))
520*01768b42SPeter Zijlstra 		goto skip_wait;
521*01768b42SPeter Zijlstra 
522*01768b42SPeter Zijlstra 	debug_mutex_lock_common(lock, &waiter);
523*01768b42SPeter Zijlstra 	debug_mutex_add_waiter(lock, &waiter, task_thread_info(task));
524*01768b42SPeter Zijlstra 
525*01768b42SPeter Zijlstra 	/* add waiting tasks to the end of the waitqueue (FIFO): */
526*01768b42SPeter Zijlstra 	list_add_tail(&waiter.list, &lock->wait_list);
527*01768b42SPeter Zijlstra 	waiter.task = task;
528*01768b42SPeter Zijlstra 
529*01768b42SPeter Zijlstra 	lock_contended(&lock->dep_map, ip);
530*01768b42SPeter Zijlstra 
531*01768b42SPeter Zijlstra 	for (;;) {
532*01768b42SPeter Zijlstra 		/*
533*01768b42SPeter Zijlstra 		 * Lets try to take the lock again - this is needed even if
534*01768b42SPeter Zijlstra 		 * we get here for the first time (shortly after failing to
535*01768b42SPeter Zijlstra 		 * acquire the lock), to make sure that we get a wakeup once
536*01768b42SPeter Zijlstra 		 * it's unlocked. Later on, if we sleep, this is the
537*01768b42SPeter Zijlstra 		 * operation that gives us the lock. We xchg it to -1, so
538*01768b42SPeter Zijlstra 		 * that when we release the lock, we properly wake up the
539*01768b42SPeter Zijlstra 		 * other waiters:
540*01768b42SPeter Zijlstra 		 */
541*01768b42SPeter Zijlstra 		if (MUTEX_SHOW_NO_WAITER(lock) &&
542*01768b42SPeter Zijlstra 		    (atomic_xchg(&lock->count, -1) == 1))
543*01768b42SPeter Zijlstra 			break;
544*01768b42SPeter Zijlstra 
545*01768b42SPeter Zijlstra 		/*
546*01768b42SPeter Zijlstra 		 * got a signal? (This code gets eliminated in the
547*01768b42SPeter Zijlstra 		 * TASK_UNINTERRUPTIBLE case.)
548*01768b42SPeter Zijlstra 		 */
549*01768b42SPeter Zijlstra 		if (unlikely(signal_pending_state(state, task))) {
550*01768b42SPeter Zijlstra 			ret = -EINTR;
551*01768b42SPeter Zijlstra 			goto err;
552*01768b42SPeter Zijlstra 		}
553*01768b42SPeter Zijlstra 
554*01768b42SPeter Zijlstra 		if (use_ww_ctx && ww_ctx->acquired > 0) {
555*01768b42SPeter Zijlstra 			ret = __mutex_lock_check_stamp(lock, ww_ctx);
556*01768b42SPeter Zijlstra 			if (ret)
557*01768b42SPeter Zijlstra 				goto err;
558*01768b42SPeter Zijlstra 		}
559*01768b42SPeter Zijlstra 
560*01768b42SPeter Zijlstra 		__set_task_state(task, state);
561*01768b42SPeter Zijlstra 
562*01768b42SPeter Zijlstra 		/* didn't get the lock, go to sleep: */
563*01768b42SPeter Zijlstra 		spin_unlock_mutex(&lock->wait_lock, flags);
564*01768b42SPeter Zijlstra 		schedule_preempt_disabled();
565*01768b42SPeter Zijlstra 		spin_lock_mutex(&lock->wait_lock, flags);
566*01768b42SPeter Zijlstra 	}
567*01768b42SPeter Zijlstra 	mutex_remove_waiter(lock, &waiter, current_thread_info());
568*01768b42SPeter Zijlstra 	/* set it to 0 if there are no waiters left: */
569*01768b42SPeter Zijlstra 	if (likely(list_empty(&lock->wait_list)))
570*01768b42SPeter Zijlstra 		atomic_set(&lock->count, 0);
571*01768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
572*01768b42SPeter Zijlstra 
573*01768b42SPeter Zijlstra skip_wait:
574*01768b42SPeter Zijlstra 	/* got the lock - cleanup and rejoice! */
575*01768b42SPeter Zijlstra 	lock_acquired(&lock->dep_map, ip);
576*01768b42SPeter Zijlstra 	mutex_set_owner(lock);
577*01768b42SPeter Zijlstra 
578*01768b42SPeter Zijlstra 	if (use_ww_ctx) {
579*01768b42SPeter Zijlstra 		struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
580*01768b42SPeter Zijlstra 		struct mutex_waiter *cur;
581*01768b42SPeter Zijlstra 
582*01768b42SPeter Zijlstra 		/*
583*01768b42SPeter Zijlstra 		 * This branch gets optimized out for the common case,
584*01768b42SPeter Zijlstra 		 * and is only important for ww_mutex_lock.
585*01768b42SPeter Zijlstra 		 */
586*01768b42SPeter Zijlstra 		ww_mutex_lock_acquired(ww, ww_ctx);
587*01768b42SPeter Zijlstra 		ww->ctx = ww_ctx;
588*01768b42SPeter Zijlstra 
589*01768b42SPeter Zijlstra 		/*
590*01768b42SPeter Zijlstra 		 * Give any possible sleeping processes the chance to wake up,
591*01768b42SPeter Zijlstra 		 * so they can recheck if they have to back off.
592*01768b42SPeter Zijlstra 		 */
593*01768b42SPeter Zijlstra 		list_for_each_entry(cur, &lock->wait_list, list) {
594*01768b42SPeter Zijlstra 			debug_mutex_wake_waiter(lock, cur);
595*01768b42SPeter Zijlstra 			wake_up_process(cur->task);
596*01768b42SPeter Zijlstra 		}
597*01768b42SPeter Zijlstra 	}
598*01768b42SPeter Zijlstra 
599*01768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
600*01768b42SPeter Zijlstra 	preempt_enable();
601*01768b42SPeter Zijlstra 	return 0;
602*01768b42SPeter Zijlstra 
603*01768b42SPeter Zijlstra err:
604*01768b42SPeter Zijlstra 	mutex_remove_waiter(lock, &waiter, task_thread_info(task));
605*01768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
606*01768b42SPeter Zijlstra 	debug_mutex_free_waiter(&waiter);
607*01768b42SPeter Zijlstra 	mutex_release(&lock->dep_map, 1, ip);
608*01768b42SPeter Zijlstra 	preempt_enable();
609*01768b42SPeter Zijlstra 	return ret;
610*01768b42SPeter Zijlstra }
611*01768b42SPeter Zijlstra 
612*01768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
613*01768b42SPeter Zijlstra void __sched
614*01768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass)
615*01768b42SPeter Zijlstra {
616*01768b42SPeter Zijlstra 	might_sleep();
617*01768b42SPeter Zijlstra 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
618*01768b42SPeter Zijlstra 			    subclass, NULL, _RET_IP_, NULL, 0);
619*01768b42SPeter Zijlstra }
620*01768b42SPeter Zijlstra 
621*01768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested);
622*01768b42SPeter Zijlstra 
623*01768b42SPeter Zijlstra void __sched
624*01768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
625*01768b42SPeter Zijlstra {
626*01768b42SPeter Zijlstra 	might_sleep();
627*01768b42SPeter Zijlstra 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
628*01768b42SPeter Zijlstra 			    0, nest, _RET_IP_, NULL, 0);
629*01768b42SPeter Zijlstra }
630*01768b42SPeter Zijlstra 
631*01768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
632*01768b42SPeter Zijlstra 
633*01768b42SPeter Zijlstra int __sched
634*01768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
635*01768b42SPeter Zijlstra {
636*01768b42SPeter Zijlstra 	might_sleep();
637*01768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_KILLABLE,
638*01768b42SPeter Zijlstra 				   subclass, NULL, _RET_IP_, NULL, 0);
639*01768b42SPeter Zijlstra }
640*01768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
641*01768b42SPeter Zijlstra 
642*01768b42SPeter Zijlstra int __sched
643*01768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
644*01768b42SPeter Zijlstra {
645*01768b42SPeter Zijlstra 	might_sleep();
646*01768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
647*01768b42SPeter Zijlstra 				   subclass, NULL, _RET_IP_, NULL, 0);
648*01768b42SPeter Zijlstra }
649*01768b42SPeter Zijlstra 
650*01768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
651*01768b42SPeter Zijlstra 
652*01768b42SPeter Zijlstra static inline int
653*01768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
654*01768b42SPeter Zijlstra {
655*01768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
656*01768b42SPeter Zijlstra 	unsigned tmp;
657*01768b42SPeter Zijlstra 
658*01768b42SPeter Zijlstra 	if (ctx->deadlock_inject_countdown-- == 0) {
659*01768b42SPeter Zijlstra 		tmp = ctx->deadlock_inject_interval;
660*01768b42SPeter Zijlstra 		if (tmp > UINT_MAX/4)
661*01768b42SPeter Zijlstra 			tmp = UINT_MAX;
662*01768b42SPeter Zijlstra 		else
663*01768b42SPeter Zijlstra 			tmp = tmp*2 + tmp + tmp/2;
664*01768b42SPeter Zijlstra 
665*01768b42SPeter Zijlstra 		ctx->deadlock_inject_interval = tmp;
666*01768b42SPeter Zijlstra 		ctx->deadlock_inject_countdown = tmp;
667*01768b42SPeter Zijlstra 		ctx->contending_lock = lock;
668*01768b42SPeter Zijlstra 
669*01768b42SPeter Zijlstra 		ww_mutex_unlock(lock);
670*01768b42SPeter Zijlstra 
671*01768b42SPeter Zijlstra 		return -EDEADLK;
672*01768b42SPeter Zijlstra 	}
673*01768b42SPeter Zijlstra #endif
674*01768b42SPeter Zijlstra 
675*01768b42SPeter Zijlstra 	return 0;
676*01768b42SPeter Zijlstra }
677*01768b42SPeter Zijlstra 
678*01768b42SPeter Zijlstra int __sched
679*01768b42SPeter Zijlstra __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
680*01768b42SPeter Zijlstra {
681*01768b42SPeter Zijlstra 	int ret;
682*01768b42SPeter Zijlstra 
683*01768b42SPeter Zijlstra 	might_sleep();
684*01768b42SPeter Zijlstra 	ret =  __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE,
685*01768b42SPeter Zijlstra 				   0, &ctx->dep_map, _RET_IP_, ctx, 1);
686*01768b42SPeter Zijlstra 	if (!ret && ctx->acquired > 1)
687*01768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
688*01768b42SPeter Zijlstra 
689*01768b42SPeter Zijlstra 	return ret;
690*01768b42SPeter Zijlstra }
691*01768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(__ww_mutex_lock);
692*01768b42SPeter Zijlstra 
693*01768b42SPeter Zijlstra int __sched
694*01768b42SPeter Zijlstra __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
695*01768b42SPeter Zijlstra {
696*01768b42SPeter Zijlstra 	int ret;
697*01768b42SPeter Zijlstra 
698*01768b42SPeter Zijlstra 	might_sleep();
699*01768b42SPeter Zijlstra 	ret = __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE,
700*01768b42SPeter Zijlstra 				  0, &ctx->dep_map, _RET_IP_, ctx, 1);
701*01768b42SPeter Zijlstra 
702*01768b42SPeter Zijlstra 	if (!ret && ctx->acquired > 1)
703*01768b42SPeter Zijlstra 		return ww_mutex_deadlock_injection(lock, ctx);
704*01768b42SPeter Zijlstra 
705*01768b42SPeter Zijlstra 	return ret;
706*01768b42SPeter Zijlstra }
707*01768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
708*01768b42SPeter Zijlstra 
709*01768b42SPeter Zijlstra #endif
710*01768b42SPeter Zijlstra 
711*01768b42SPeter Zijlstra /*
712*01768b42SPeter Zijlstra  * Release the lock, slowpath:
713*01768b42SPeter Zijlstra  */
714*01768b42SPeter Zijlstra static inline void
715*01768b42SPeter Zijlstra __mutex_unlock_common_slowpath(atomic_t *lock_count, int nested)
716*01768b42SPeter Zijlstra {
717*01768b42SPeter Zijlstra 	struct mutex *lock = container_of(lock_count, struct mutex, count);
718*01768b42SPeter Zijlstra 	unsigned long flags;
719*01768b42SPeter Zijlstra 
720*01768b42SPeter Zijlstra 	spin_lock_mutex(&lock->wait_lock, flags);
721*01768b42SPeter Zijlstra 	mutex_release(&lock->dep_map, nested, _RET_IP_);
722*01768b42SPeter Zijlstra 	debug_mutex_unlock(lock);
723*01768b42SPeter Zijlstra 
724*01768b42SPeter Zijlstra 	/*
725*01768b42SPeter Zijlstra 	 * some architectures leave the lock unlocked in the fastpath failure
726*01768b42SPeter Zijlstra 	 * case, others need to leave it locked. In the later case we have to
727*01768b42SPeter Zijlstra 	 * unlock it here
728*01768b42SPeter Zijlstra 	 */
729*01768b42SPeter Zijlstra 	if (__mutex_slowpath_needs_to_unlock())
730*01768b42SPeter Zijlstra 		atomic_set(&lock->count, 1);
731*01768b42SPeter Zijlstra 
732*01768b42SPeter Zijlstra 	if (!list_empty(&lock->wait_list)) {
733*01768b42SPeter Zijlstra 		/* get the first entry from the wait-list: */
734*01768b42SPeter Zijlstra 		struct mutex_waiter *waiter =
735*01768b42SPeter Zijlstra 				list_entry(lock->wait_list.next,
736*01768b42SPeter Zijlstra 					   struct mutex_waiter, list);
737*01768b42SPeter Zijlstra 
738*01768b42SPeter Zijlstra 		debug_mutex_wake_waiter(lock, waiter);
739*01768b42SPeter Zijlstra 
740*01768b42SPeter Zijlstra 		wake_up_process(waiter->task);
741*01768b42SPeter Zijlstra 	}
742*01768b42SPeter Zijlstra 
743*01768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
744*01768b42SPeter Zijlstra }
745*01768b42SPeter Zijlstra 
746*01768b42SPeter Zijlstra /*
747*01768b42SPeter Zijlstra  * Release the lock, slowpath:
748*01768b42SPeter Zijlstra  */
749*01768b42SPeter Zijlstra static __used noinline void
750*01768b42SPeter Zijlstra __mutex_unlock_slowpath(atomic_t *lock_count)
751*01768b42SPeter Zijlstra {
752*01768b42SPeter Zijlstra 	__mutex_unlock_common_slowpath(lock_count, 1);
753*01768b42SPeter Zijlstra }
754*01768b42SPeter Zijlstra 
755*01768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
756*01768b42SPeter Zijlstra /*
757*01768b42SPeter Zijlstra  * Here come the less common (and hence less performance-critical) APIs:
758*01768b42SPeter Zijlstra  * mutex_lock_interruptible() and mutex_trylock().
759*01768b42SPeter Zijlstra  */
760*01768b42SPeter Zijlstra static noinline int __sched
761*01768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock);
762*01768b42SPeter Zijlstra 
763*01768b42SPeter Zijlstra static noinline int __sched
764*01768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock);
765*01768b42SPeter Zijlstra 
766*01768b42SPeter Zijlstra /**
767*01768b42SPeter Zijlstra  * mutex_lock_interruptible - acquire the mutex, interruptible
768*01768b42SPeter Zijlstra  * @lock: the mutex to be acquired
769*01768b42SPeter Zijlstra  *
770*01768b42SPeter Zijlstra  * Lock the mutex like mutex_lock(), and return 0 if the mutex has
771*01768b42SPeter Zijlstra  * been acquired or sleep until the mutex becomes available. If a
772*01768b42SPeter Zijlstra  * signal arrives while waiting for the lock then this function
773*01768b42SPeter Zijlstra  * returns -EINTR.
774*01768b42SPeter Zijlstra  *
775*01768b42SPeter Zijlstra  * This function is similar to (but not equivalent to) down_interruptible().
776*01768b42SPeter Zijlstra  */
777*01768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock)
778*01768b42SPeter Zijlstra {
779*01768b42SPeter Zijlstra 	int ret;
780*01768b42SPeter Zijlstra 
781*01768b42SPeter Zijlstra 	might_sleep();
782*01768b42SPeter Zijlstra 	ret =  __mutex_fastpath_lock_retval(&lock->count);
783*01768b42SPeter Zijlstra 	if (likely(!ret)) {
784*01768b42SPeter Zijlstra 		mutex_set_owner(lock);
785*01768b42SPeter Zijlstra 		return 0;
786*01768b42SPeter Zijlstra 	} else
787*01768b42SPeter Zijlstra 		return __mutex_lock_interruptible_slowpath(lock);
788*01768b42SPeter Zijlstra }
789*01768b42SPeter Zijlstra 
790*01768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible);
791*01768b42SPeter Zijlstra 
792*01768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock)
793*01768b42SPeter Zijlstra {
794*01768b42SPeter Zijlstra 	int ret;
795*01768b42SPeter Zijlstra 
796*01768b42SPeter Zijlstra 	might_sleep();
797*01768b42SPeter Zijlstra 	ret = __mutex_fastpath_lock_retval(&lock->count);
798*01768b42SPeter Zijlstra 	if (likely(!ret)) {
799*01768b42SPeter Zijlstra 		mutex_set_owner(lock);
800*01768b42SPeter Zijlstra 		return 0;
801*01768b42SPeter Zijlstra 	} else
802*01768b42SPeter Zijlstra 		return __mutex_lock_killable_slowpath(lock);
803*01768b42SPeter Zijlstra }
804*01768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable);
805*01768b42SPeter Zijlstra 
806*01768b42SPeter Zijlstra static __used noinline void __sched
807*01768b42SPeter Zijlstra __mutex_lock_slowpath(atomic_t *lock_count)
808*01768b42SPeter Zijlstra {
809*01768b42SPeter Zijlstra 	struct mutex *lock = container_of(lock_count, struct mutex, count);
810*01768b42SPeter Zijlstra 
811*01768b42SPeter Zijlstra 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
812*01768b42SPeter Zijlstra 			    NULL, _RET_IP_, NULL, 0);
813*01768b42SPeter Zijlstra }
814*01768b42SPeter Zijlstra 
815*01768b42SPeter Zijlstra static noinline int __sched
816*01768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock)
817*01768b42SPeter Zijlstra {
818*01768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_KILLABLE, 0,
819*01768b42SPeter Zijlstra 				   NULL, _RET_IP_, NULL, 0);
820*01768b42SPeter Zijlstra }
821*01768b42SPeter Zijlstra 
822*01768b42SPeter Zijlstra static noinline int __sched
823*01768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock)
824*01768b42SPeter Zijlstra {
825*01768b42SPeter Zijlstra 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0,
826*01768b42SPeter Zijlstra 				   NULL, _RET_IP_, NULL, 0);
827*01768b42SPeter Zijlstra }
828*01768b42SPeter Zijlstra 
829*01768b42SPeter Zijlstra static noinline int __sched
830*01768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
831*01768b42SPeter Zijlstra {
832*01768b42SPeter Zijlstra 	return __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE, 0,
833*01768b42SPeter Zijlstra 				   NULL, _RET_IP_, ctx, 1);
834*01768b42SPeter Zijlstra }
835*01768b42SPeter Zijlstra 
836*01768b42SPeter Zijlstra static noinline int __sched
837*01768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
838*01768b42SPeter Zijlstra 					    struct ww_acquire_ctx *ctx)
839*01768b42SPeter Zijlstra {
840*01768b42SPeter Zijlstra 	return __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE, 0,
841*01768b42SPeter Zijlstra 				   NULL, _RET_IP_, ctx, 1);
842*01768b42SPeter Zijlstra }
843*01768b42SPeter Zijlstra 
844*01768b42SPeter Zijlstra #endif
845*01768b42SPeter Zijlstra 
846*01768b42SPeter Zijlstra /*
847*01768b42SPeter Zijlstra  * Spinlock based trylock, we take the spinlock and check whether we
848*01768b42SPeter Zijlstra  * can get the lock:
849*01768b42SPeter Zijlstra  */
850*01768b42SPeter Zijlstra static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
851*01768b42SPeter Zijlstra {
852*01768b42SPeter Zijlstra 	struct mutex *lock = container_of(lock_count, struct mutex, count);
853*01768b42SPeter Zijlstra 	unsigned long flags;
854*01768b42SPeter Zijlstra 	int prev;
855*01768b42SPeter Zijlstra 
856*01768b42SPeter Zijlstra 	spin_lock_mutex(&lock->wait_lock, flags);
857*01768b42SPeter Zijlstra 
858*01768b42SPeter Zijlstra 	prev = atomic_xchg(&lock->count, -1);
859*01768b42SPeter Zijlstra 	if (likely(prev == 1)) {
860*01768b42SPeter Zijlstra 		mutex_set_owner(lock);
861*01768b42SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
862*01768b42SPeter Zijlstra 	}
863*01768b42SPeter Zijlstra 
864*01768b42SPeter Zijlstra 	/* Set it back to 0 if there are no waiters: */
865*01768b42SPeter Zijlstra 	if (likely(list_empty(&lock->wait_list)))
866*01768b42SPeter Zijlstra 		atomic_set(&lock->count, 0);
867*01768b42SPeter Zijlstra 
868*01768b42SPeter Zijlstra 	spin_unlock_mutex(&lock->wait_lock, flags);
869*01768b42SPeter Zijlstra 
870*01768b42SPeter Zijlstra 	return prev == 1;
871*01768b42SPeter Zijlstra }
872*01768b42SPeter Zijlstra 
873*01768b42SPeter Zijlstra /**
874*01768b42SPeter Zijlstra  * mutex_trylock - try to acquire the mutex, without waiting
875*01768b42SPeter Zijlstra  * @lock: the mutex to be acquired
876*01768b42SPeter Zijlstra  *
877*01768b42SPeter Zijlstra  * Try to acquire the mutex atomically. Returns 1 if the mutex
878*01768b42SPeter Zijlstra  * has been acquired successfully, and 0 on contention.
879*01768b42SPeter Zijlstra  *
880*01768b42SPeter Zijlstra  * NOTE: this function follows the spin_trylock() convention, so
881*01768b42SPeter Zijlstra  * it is negated from the down_trylock() return values! Be careful
882*01768b42SPeter Zijlstra  * about this when converting semaphore users to mutexes.
883*01768b42SPeter Zijlstra  *
884*01768b42SPeter Zijlstra  * This function must not be used in interrupt context. The
885*01768b42SPeter Zijlstra  * mutex must be released by the same task that acquired it.
886*01768b42SPeter Zijlstra  */
887*01768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock)
888*01768b42SPeter Zijlstra {
889*01768b42SPeter Zijlstra 	int ret;
890*01768b42SPeter Zijlstra 
891*01768b42SPeter Zijlstra 	ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath);
892*01768b42SPeter Zijlstra 	if (ret)
893*01768b42SPeter Zijlstra 		mutex_set_owner(lock);
894*01768b42SPeter Zijlstra 
895*01768b42SPeter Zijlstra 	return ret;
896*01768b42SPeter Zijlstra }
897*01768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock);
898*01768b42SPeter Zijlstra 
899*01768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC
900*01768b42SPeter Zijlstra int __sched
901*01768b42SPeter Zijlstra __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
902*01768b42SPeter Zijlstra {
903*01768b42SPeter Zijlstra 	int ret;
904*01768b42SPeter Zijlstra 
905*01768b42SPeter Zijlstra 	might_sleep();
906*01768b42SPeter Zijlstra 
907*01768b42SPeter Zijlstra 	ret = __mutex_fastpath_lock_retval(&lock->base.count);
908*01768b42SPeter Zijlstra 
909*01768b42SPeter Zijlstra 	if (likely(!ret)) {
910*01768b42SPeter Zijlstra 		ww_mutex_set_context_fastpath(lock, ctx);
911*01768b42SPeter Zijlstra 		mutex_set_owner(&lock->base);
912*01768b42SPeter Zijlstra 	} else
913*01768b42SPeter Zijlstra 		ret = __ww_mutex_lock_slowpath(lock, ctx);
914*01768b42SPeter Zijlstra 	return ret;
915*01768b42SPeter Zijlstra }
916*01768b42SPeter Zijlstra EXPORT_SYMBOL(__ww_mutex_lock);
917*01768b42SPeter Zijlstra 
918*01768b42SPeter Zijlstra int __sched
919*01768b42SPeter Zijlstra __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
920*01768b42SPeter Zijlstra {
921*01768b42SPeter Zijlstra 	int ret;
922*01768b42SPeter Zijlstra 
923*01768b42SPeter Zijlstra 	might_sleep();
924*01768b42SPeter Zijlstra 
925*01768b42SPeter Zijlstra 	ret = __mutex_fastpath_lock_retval(&lock->base.count);
926*01768b42SPeter Zijlstra 
927*01768b42SPeter Zijlstra 	if (likely(!ret)) {
928*01768b42SPeter Zijlstra 		ww_mutex_set_context_fastpath(lock, ctx);
929*01768b42SPeter Zijlstra 		mutex_set_owner(&lock->base);
930*01768b42SPeter Zijlstra 	} else
931*01768b42SPeter Zijlstra 		ret = __ww_mutex_lock_interruptible_slowpath(lock, ctx);
932*01768b42SPeter Zijlstra 	return ret;
933*01768b42SPeter Zijlstra }
934*01768b42SPeter Zijlstra EXPORT_SYMBOL(__ww_mutex_lock_interruptible);
935*01768b42SPeter Zijlstra 
936*01768b42SPeter Zijlstra #endif
937*01768b42SPeter Zijlstra 
938*01768b42SPeter Zijlstra /**
939*01768b42SPeter Zijlstra  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
940*01768b42SPeter Zijlstra  * @cnt: the atomic which we are to dec
941*01768b42SPeter Zijlstra  * @lock: the mutex to return holding if we dec to 0
942*01768b42SPeter Zijlstra  *
943*01768b42SPeter Zijlstra  * return true and hold lock if we dec to 0, return false otherwise
944*01768b42SPeter Zijlstra  */
945*01768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
946*01768b42SPeter Zijlstra {
947*01768b42SPeter Zijlstra 	/* dec if we can't possibly hit 0 */
948*01768b42SPeter Zijlstra 	if (atomic_add_unless(cnt, -1, 1))
949*01768b42SPeter Zijlstra 		return 0;
950*01768b42SPeter Zijlstra 	/* we might hit 0, so take the lock */
951*01768b42SPeter Zijlstra 	mutex_lock(lock);
952*01768b42SPeter Zijlstra 	if (!atomic_dec_and_test(cnt)) {
953*01768b42SPeter Zijlstra 		/* when we actually did the dec, we didn't hit 0 */
954*01768b42SPeter Zijlstra 		mutex_unlock(lock);
955*01768b42SPeter Zijlstra 		return 0;
956*01768b42SPeter Zijlstra 	}
957*01768b42SPeter Zijlstra 	/* we hit 0, and we hold the lock */
958*01768b42SPeter Zijlstra 	return 1;
959*01768b42SPeter Zijlstra }
960*01768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
961