xref: /linux/kernel/locking/rtmutex.c (revision 7b3c92b85a65c2db1f542265bc98e1f9e3056eba)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21696a8beSPeter Zijlstra /*
31696a8beSPeter Zijlstra  * RT-Mutexes: simple blocking mutual exclusion locks with PI support
41696a8beSPeter Zijlstra  *
51696a8beSPeter Zijlstra  * started by Ingo Molnar and Thomas Gleixner.
61696a8beSPeter Zijlstra  *
71696a8beSPeter Zijlstra  *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
81696a8beSPeter Zijlstra  *  Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
91696a8beSPeter Zijlstra  *  Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
101696a8beSPeter Zijlstra  *  Copyright (C) 2006 Esben Nielsen
111696a8beSPeter Zijlstra  *
12387b1468SMauro Carvalho Chehab  *  See Documentation/locking/rt-mutex-design.rst for details.
131696a8beSPeter Zijlstra  */
141696a8beSPeter Zijlstra #include <linux/spinlock.h>
151696a8beSPeter Zijlstra #include <linux/export.h>
16174cd4b1SIngo Molnar #include <linux/sched/signal.h>
171696a8beSPeter Zijlstra #include <linux/sched/rt.h>
18fb00aca4SPeter Zijlstra #include <linux/sched/deadline.h>
1984f001e1SIngo Molnar #include <linux/sched/wake_q.h>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
211696a8beSPeter Zijlstra #include <linux/timer.h>
221696a8beSPeter Zijlstra 
231696a8beSPeter Zijlstra #include "rtmutex_common.h"
241696a8beSPeter Zijlstra 
251696a8beSPeter Zijlstra /*
261696a8beSPeter Zijlstra  * lock->owner state tracking:
271696a8beSPeter Zijlstra  *
281696a8beSPeter Zijlstra  * lock->owner holds the task_struct pointer of the owner. Bit 0
291696a8beSPeter Zijlstra  * is used to keep track of the "lock has waiters" state.
301696a8beSPeter Zijlstra  *
311696a8beSPeter Zijlstra  * owner	bit0
321696a8beSPeter Zijlstra  * NULL		0	lock is free (fast acquire possible)
331696a8beSPeter Zijlstra  * NULL		1	lock is free and has waiters and the top waiter
341696a8beSPeter Zijlstra  *				is going to take the lock*
351696a8beSPeter Zijlstra  * taskpointer	0	lock is held (fast release possible)
361696a8beSPeter Zijlstra  * taskpointer	1	lock is held and has waiters**
371696a8beSPeter Zijlstra  *
381696a8beSPeter Zijlstra  * The fast atomic compare exchange based acquire and release is only
391696a8beSPeter Zijlstra  * possible when bit 0 of lock->owner is 0.
401696a8beSPeter Zijlstra  *
411696a8beSPeter Zijlstra  * (*) It also can be a transitional state when grabbing the lock
421696a8beSPeter Zijlstra  * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
431696a8beSPeter Zijlstra  * we need to set the bit0 before looking at the lock, and the owner may be
441696a8beSPeter Zijlstra  * NULL in this small time, hence this can be a transitional state.
451696a8beSPeter Zijlstra  *
461696a8beSPeter Zijlstra  * (**) There is a small time when bit 0 is set but there are no
471696a8beSPeter Zijlstra  * waiters. This can happen when grabbing the lock in the slow path.
481696a8beSPeter Zijlstra  * To prevent a cmpxchg of the owner releasing the lock, we need to
491696a8beSPeter Zijlstra  * set this bit before looking at the lock.
501696a8beSPeter Zijlstra  */
511696a8beSPeter Zijlstra 
521696a8beSPeter Zijlstra static void
531696a8beSPeter Zijlstra rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
541696a8beSPeter Zijlstra {
551696a8beSPeter Zijlstra 	unsigned long val = (unsigned long)owner;
561696a8beSPeter Zijlstra 
571696a8beSPeter Zijlstra 	if (rt_mutex_has_waiters(lock))
581696a8beSPeter Zijlstra 		val |= RT_MUTEX_HAS_WAITERS;
591696a8beSPeter Zijlstra 
601696a8beSPeter Zijlstra 	lock->owner = (struct task_struct *)val;
611696a8beSPeter Zijlstra }
621696a8beSPeter Zijlstra 
631696a8beSPeter Zijlstra static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
641696a8beSPeter Zijlstra {
651696a8beSPeter Zijlstra 	lock->owner = (struct task_struct *)
661696a8beSPeter Zijlstra 			((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
671696a8beSPeter Zijlstra }
681696a8beSPeter Zijlstra 
691696a8beSPeter Zijlstra static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
701696a8beSPeter Zijlstra {
71dbb26055SThomas Gleixner 	unsigned long owner, *p = (unsigned long *) &lock->owner;
72dbb26055SThomas Gleixner 
73dbb26055SThomas Gleixner 	if (rt_mutex_has_waiters(lock))
74dbb26055SThomas Gleixner 		return;
75dbb26055SThomas Gleixner 
76dbb26055SThomas Gleixner 	/*
77dbb26055SThomas Gleixner 	 * The rbtree has no waiters enqueued, now make sure that the
78dbb26055SThomas Gleixner 	 * lock->owner still has the waiters bit set, otherwise the
79dbb26055SThomas Gleixner 	 * following can happen:
80dbb26055SThomas Gleixner 	 *
81dbb26055SThomas Gleixner 	 * CPU 0	CPU 1		CPU2
82dbb26055SThomas Gleixner 	 * l->owner=T1
83dbb26055SThomas Gleixner 	 *		rt_mutex_lock(l)
84dbb26055SThomas Gleixner 	 *		lock(l->lock)
85dbb26055SThomas Gleixner 	 *		l->owner = T1 | HAS_WAITERS;
86dbb26055SThomas Gleixner 	 *		enqueue(T2)
87dbb26055SThomas Gleixner 	 *		boost()
88dbb26055SThomas Gleixner 	 *		  unlock(l->lock)
89dbb26055SThomas Gleixner 	 *		block()
90dbb26055SThomas Gleixner 	 *
91dbb26055SThomas Gleixner 	 *				rt_mutex_lock(l)
92dbb26055SThomas Gleixner 	 *				lock(l->lock)
93dbb26055SThomas Gleixner 	 *				l->owner = T1 | HAS_WAITERS;
94dbb26055SThomas Gleixner 	 *				enqueue(T3)
95dbb26055SThomas Gleixner 	 *				boost()
96dbb26055SThomas Gleixner 	 *				  unlock(l->lock)
97dbb26055SThomas Gleixner 	 *				block()
98dbb26055SThomas Gleixner 	 *		signal(->T2)	signal(->T3)
99dbb26055SThomas Gleixner 	 *		lock(l->lock)
100dbb26055SThomas Gleixner 	 *		dequeue(T2)
101dbb26055SThomas Gleixner 	 *		deboost()
102dbb26055SThomas Gleixner 	 *		  unlock(l->lock)
103dbb26055SThomas Gleixner 	 *				lock(l->lock)
104dbb26055SThomas Gleixner 	 *				dequeue(T3)
105dbb26055SThomas Gleixner 	 *				 ==> wait list is empty
106dbb26055SThomas Gleixner 	 *				deboost()
107dbb26055SThomas Gleixner 	 *				 unlock(l->lock)
108dbb26055SThomas Gleixner 	 *		lock(l->lock)
109dbb26055SThomas Gleixner 	 *		fixup_rt_mutex_waiters()
110dbb26055SThomas Gleixner 	 *		  if (wait_list_empty(l) {
111dbb26055SThomas Gleixner 	 *		    l->owner = owner
112dbb26055SThomas Gleixner 	 *		    owner = l->owner & ~HAS_WAITERS;
113dbb26055SThomas Gleixner 	 *		      ==> l->owner = T1
114dbb26055SThomas Gleixner 	 *		  }
115dbb26055SThomas Gleixner 	 *				lock(l->lock)
116dbb26055SThomas Gleixner 	 * rt_mutex_unlock(l)		fixup_rt_mutex_waiters()
117dbb26055SThomas Gleixner 	 *				  if (wait_list_empty(l) {
118dbb26055SThomas Gleixner 	 *				    owner = l->owner & ~HAS_WAITERS;
119dbb26055SThomas Gleixner 	 * cmpxchg(l->owner, T1, NULL)
120dbb26055SThomas Gleixner 	 *  ===> Success (l->owner = NULL)
121dbb26055SThomas Gleixner 	 *
122dbb26055SThomas Gleixner 	 *				    l->owner = owner
123dbb26055SThomas Gleixner 	 *				      ==> l->owner = T1
124dbb26055SThomas Gleixner 	 *				  }
125dbb26055SThomas Gleixner 	 *
126dbb26055SThomas Gleixner 	 * With the check for the waiter bit in place T3 on CPU2 will not
127dbb26055SThomas Gleixner 	 * overwrite. All tasks fiddling with the waiters bit are
128dbb26055SThomas Gleixner 	 * serialized by l->lock, so nothing else can modify the waiters
129dbb26055SThomas Gleixner 	 * bit. If the bit is set then nothing can change l->owner either
130dbb26055SThomas Gleixner 	 * so the simple RMW is safe. The cmpxchg() will simply fail if it
131dbb26055SThomas Gleixner 	 * happens in the middle of the RMW because the waiters bit is
132dbb26055SThomas Gleixner 	 * still set.
133dbb26055SThomas Gleixner 	 */
134dbb26055SThomas Gleixner 	owner = READ_ONCE(*p);
135dbb26055SThomas Gleixner 	if (owner & RT_MUTEX_HAS_WAITERS)
136dbb26055SThomas Gleixner 		WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
1371696a8beSPeter Zijlstra }
1381696a8beSPeter Zijlstra 
1391696a8beSPeter Zijlstra /*
140cede8841SSebastian Andrzej Siewior  * We can speed up the acquire/release, if there's no debugging state to be
141cede8841SSebastian Andrzej Siewior  * set up.
1421696a8beSPeter Zijlstra  */
143cede8841SSebastian Andrzej Siewior #ifndef CONFIG_DEBUG_RT_MUTEXES
144700318d1SDavidlohr Bueso # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
145700318d1SDavidlohr Bueso # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
146700318d1SDavidlohr Bueso # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
147700318d1SDavidlohr Bueso 
148700318d1SDavidlohr Bueso /*
149700318d1SDavidlohr Bueso  * Callers must hold the ->wait_lock -- which is the whole purpose as we force
150700318d1SDavidlohr Bueso  * all future threads that attempt to [Rmw] the lock to the slowpath. As such
151700318d1SDavidlohr Bueso  * relaxed semantics suffice.
152700318d1SDavidlohr Bueso  */
1531696a8beSPeter Zijlstra static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
1541696a8beSPeter Zijlstra {
1551696a8beSPeter Zijlstra 	unsigned long owner, *p = (unsigned long *) &lock->owner;
1561696a8beSPeter Zijlstra 
1571696a8beSPeter Zijlstra 	do {
1581696a8beSPeter Zijlstra 		owner = *p;
159700318d1SDavidlohr Bueso 	} while (cmpxchg_relaxed(p, owner,
160700318d1SDavidlohr Bueso 				 owner | RT_MUTEX_HAS_WAITERS) != owner);
1611696a8beSPeter Zijlstra }
16227e35715SThomas Gleixner 
16327e35715SThomas Gleixner /*
16427e35715SThomas Gleixner  * Safe fastpath aware unlock:
16527e35715SThomas Gleixner  * 1) Clear the waiters bit
16627e35715SThomas Gleixner  * 2) Drop lock->wait_lock
16727e35715SThomas Gleixner  * 3) Try to unlock the lock with cmpxchg
16827e35715SThomas Gleixner  */
169b4abf910SThomas Gleixner static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
170b4abf910SThomas Gleixner 					unsigned long flags)
17127e35715SThomas Gleixner 	__releases(lock->wait_lock)
17227e35715SThomas Gleixner {
17327e35715SThomas Gleixner 	struct task_struct *owner = rt_mutex_owner(lock);
17427e35715SThomas Gleixner 
17527e35715SThomas Gleixner 	clear_rt_mutex_waiters(lock);
176b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
17727e35715SThomas Gleixner 	/*
17827e35715SThomas Gleixner 	 * If a new waiter comes in between the unlock and the cmpxchg
17927e35715SThomas Gleixner 	 * we have two situations:
18027e35715SThomas Gleixner 	 *
18127e35715SThomas Gleixner 	 * unlock(wait_lock);
18227e35715SThomas Gleixner 	 *					lock(wait_lock);
18327e35715SThomas Gleixner 	 * cmpxchg(p, owner, 0) == owner
18427e35715SThomas Gleixner 	 *					mark_rt_mutex_waiters(lock);
18527e35715SThomas Gleixner 	 *					acquire(lock);
18627e35715SThomas Gleixner 	 * or:
18727e35715SThomas Gleixner 	 *
18827e35715SThomas Gleixner 	 * unlock(wait_lock);
18927e35715SThomas Gleixner 	 *					lock(wait_lock);
19027e35715SThomas Gleixner 	 *					mark_rt_mutex_waiters(lock);
19127e35715SThomas Gleixner 	 *
19227e35715SThomas Gleixner 	 * cmpxchg(p, owner, 0) != owner
19327e35715SThomas Gleixner 	 *					enqueue_waiter();
19427e35715SThomas Gleixner 	 *					unlock(wait_lock);
19527e35715SThomas Gleixner 	 * lock(wait_lock);
19627e35715SThomas Gleixner 	 * wake waiter();
19727e35715SThomas Gleixner 	 * unlock(wait_lock);
19827e35715SThomas Gleixner 	 *					lock(wait_lock);
19927e35715SThomas Gleixner 	 *					acquire(lock);
20027e35715SThomas Gleixner 	 */
201700318d1SDavidlohr Bueso 	return rt_mutex_cmpxchg_release(lock, owner, NULL);
20227e35715SThomas Gleixner }
20327e35715SThomas Gleixner 
2041696a8beSPeter Zijlstra #else
205700318d1SDavidlohr Bueso # define rt_mutex_cmpxchg_relaxed(l,c,n)	(0)
206700318d1SDavidlohr Bueso # define rt_mutex_cmpxchg_acquire(l,c,n)	(0)
207700318d1SDavidlohr Bueso # define rt_mutex_cmpxchg_release(l,c,n)	(0)
208700318d1SDavidlohr Bueso 
2091696a8beSPeter Zijlstra static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
2101696a8beSPeter Zijlstra {
2111696a8beSPeter Zijlstra 	lock->owner = (struct task_struct *)
2121696a8beSPeter Zijlstra 			((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
2131696a8beSPeter Zijlstra }
21427e35715SThomas Gleixner 
21527e35715SThomas Gleixner /*
21627e35715SThomas Gleixner  * Simple slow path only version: lock->owner is protected by lock->wait_lock.
21727e35715SThomas Gleixner  */
218b4abf910SThomas Gleixner static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
219b4abf910SThomas Gleixner 					unsigned long flags)
22027e35715SThomas Gleixner 	__releases(lock->wait_lock)
22127e35715SThomas Gleixner {
22227e35715SThomas Gleixner 	lock->owner = NULL;
223b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
22427e35715SThomas Gleixner 	return true;
22527e35715SThomas Gleixner }
2261696a8beSPeter Zijlstra #endif
2271696a8beSPeter Zijlstra 
22819830e55SPeter Zijlstra /*
22919830e55SPeter Zijlstra  * Only use with rt_mutex_waiter_{less,equal}()
23019830e55SPeter Zijlstra  */
23119830e55SPeter Zijlstra #define task_to_waiter(p)	\
23219830e55SPeter Zijlstra 	&(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
23319830e55SPeter Zijlstra 
234fb00aca4SPeter Zijlstra static inline int
235fb00aca4SPeter Zijlstra rt_mutex_waiter_less(struct rt_mutex_waiter *left,
236fb00aca4SPeter Zijlstra 		     struct rt_mutex_waiter *right)
237fb00aca4SPeter Zijlstra {
2382d3d891dSDario Faggioli 	if (left->prio < right->prio)
239fb00aca4SPeter Zijlstra 		return 1;
240fb00aca4SPeter Zijlstra 
2411696a8beSPeter Zijlstra 	/*
2422d3d891dSDario Faggioli 	 * If both waiters have dl_prio(), we check the deadlines of the
2432d3d891dSDario Faggioli 	 * associated tasks.
2442d3d891dSDario Faggioli 	 * If left waiter has a dl_prio(), and we didn't return 1 above,
2452d3d891dSDario Faggioli 	 * then right waiter has a dl_prio() too.
246fb00aca4SPeter Zijlstra 	 */
2472d3d891dSDario Faggioli 	if (dl_prio(left->prio))
248e0aad5b4SPeter Zijlstra 		return dl_time_before(left->deadline, right->deadline);
249fb00aca4SPeter Zijlstra 
250fb00aca4SPeter Zijlstra 	return 0;
251fb00aca4SPeter Zijlstra }
252fb00aca4SPeter Zijlstra 
25319830e55SPeter Zijlstra static inline int
25419830e55SPeter Zijlstra rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
25519830e55SPeter Zijlstra 		      struct rt_mutex_waiter *right)
25619830e55SPeter Zijlstra {
25719830e55SPeter Zijlstra 	if (left->prio != right->prio)
25819830e55SPeter Zijlstra 		return 0;
25919830e55SPeter Zijlstra 
26019830e55SPeter Zijlstra 	/*
26119830e55SPeter Zijlstra 	 * If both waiters have dl_prio(), we check the deadlines of the
26219830e55SPeter Zijlstra 	 * associated tasks.
26319830e55SPeter Zijlstra 	 * If left waiter has a dl_prio(), and we didn't return 0 above,
26419830e55SPeter Zijlstra 	 * then right waiter has a dl_prio() too.
26519830e55SPeter Zijlstra 	 */
26619830e55SPeter Zijlstra 	if (dl_prio(left->prio))
26719830e55SPeter Zijlstra 		return left->deadline == right->deadline;
26819830e55SPeter Zijlstra 
26919830e55SPeter Zijlstra 	return 1;
27019830e55SPeter Zijlstra }
27119830e55SPeter Zijlstra 
272fb00aca4SPeter Zijlstra static void
273fb00aca4SPeter Zijlstra rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
274fb00aca4SPeter Zijlstra {
275a23ba907SDavidlohr Bueso 	struct rb_node **link = &lock->waiters.rb_root.rb_node;
276fb00aca4SPeter Zijlstra 	struct rb_node *parent = NULL;
277fb00aca4SPeter Zijlstra 	struct rt_mutex_waiter *entry;
278a23ba907SDavidlohr Bueso 	bool leftmost = true;
279fb00aca4SPeter Zijlstra 
280fb00aca4SPeter Zijlstra 	while (*link) {
281fb00aca4SPeter Zijlstra 		parent = *link;
282fb00aca4SPeter Zijlstra 		entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
283fb00aca4SPeter Zijlstra 		if (rt_mutex_waiter_less(waiter, entry)) {
284fb00aca4SPeter Zijlstra 			link = &parent->rb_left;
285fb00aca4SPeter Zijlstra 		} else {
286fb00aca4SPeter Zijlstra 			link = &parent->rb_right;
287a23ba907SDavidlohr Bueso 			leftmost = false;
288fb00aca4SPeter Zijlstra 		}
289fb00aca4SPeter Zijlstra 	}
290fb00aca4SPeter Zijlstra 
291fb00aca4SPeter Zijlstra 	rb_link_node(&waiter->tree_entry, parent, link);
292a23ba907SDavidlohr Bueso 	rb_insert_color_cached(&waiter->tree_entry, &lock->waiters, leftmost);
293fb00aca4SPeter Zijlstra }
294fb00aca4SPeter Zijlstra 
295fb00aca4SPeter Zijlstra static void
296fb00aca4SPeter Zijlstra rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
297fb00aca4SPeter Zijlstra {
298fb00aca4SPeter Zijlstra 	if (RB_EMPTY_NODE(&waiter->tree_entry))
299fb00aca4SPeter Zijlstra 		return;
300fb00aca4SPeter Zijlstra 
301a23ba907SDavidlohr Bueso 	rb_erase_cached(&waiter->tree_entry, &lock->waiters);
302fb00aca4SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->tree_entry);
303fb00aca4SPeter Zijlstra }
304fb00aca4SPeter Zijlstra 
305fb00aca4SPeter Zijlstra static void
306fb00aca4SPeter Zijlstra rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
307fb00aca4SPeter Zijlstra {
308a23ba907SDavidlohr Bueso 	struct rb_node **link = &task->pi_waiters.rb_root.rb_node;
309fb00aca4SPeter Zijlstra 	struct rb_node *parent = NULL;
310fb00aca4SPeter Zijlstra 	struct rt_mutex_waiter *entry;
311a23ba907SDavidlohr Bueso 	bool leftmost = true;
312fb00aca4SPeter Zijlstra 
313fb00aca4SPeter Zijlstra 	while (*link) {
314fb00aca4SPeter Zijlstra 		parent = *link;
315fb00aca4SPeter Zijlstra 		entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
316fb00aca4SPeter Zijlstra 		if (rt_mutex_waiter_less(waiter, entry)) {
317fb00aca4SPeter Zijlstra 			link = &parent->rb_left;
318fb00aca4SPeter Zijlstra 		} else {
319fb00aca4SPeter Zijlstra 			link = &parent->rb_right;
320a23ba907SDavidlohr Bueso 			leftmost = false;
321fb00aca4SPeter Zijlstra 		}
322fb00aca4SPeter Zijlstra 	}
323fb00aca4SPeter Zijlstra 
324fb00aca4SPeter Zijlstra 	rb_link_node(&waiter->pi_tree_entry, parent, link);
325a23ba907SDavidlohr Bueso 	rb_insert_color_cached(&waiter->pi_tree_entry, &task->pi_waiters, leftmost);
326fb00aca4SPeter Zijlstra }
327fb00aca4SPeter Zijlstra 
328fb00aca4SPeter Zijlstra static void
329fb00aca4SPeter Zijlstra rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
330fb00aca4SPeter Zijlstra {
331fb00aca4SPeter Zijlstra 	if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
332fb00aca4SPeter Zijlstra 		return;
333fb00aca4SPeter Zijlstra 
334a23ba907SDavidlohr Bueso 	rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
335fb00aca4SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->pi_tree_entry);
336fb00aca4SPeter Zijlstra }
337fb00aca4SPeter Zijlstra 
338acd58620SPeter Zijlstra static void rt_mutex_adjust_prio(struct task_struct *p)
339e96a7705SXunlei Pang {
340acd58620SPeter Zijlstra 	struct task_struct *pi_task = NULL;
341e96a7705SXunlei Pang 
342acd58620SPeter Zijlstra 	lockdep_assert_held(&p->pi_lock);
343e96a7705SXunlei Pang 
344acd58620SPeter Zijlstra 	if (task_has_pi_waiters(p))
345acd58620SPeter Zijlstra 		pi_task = task_top_pi_waiter(p)->task;
3461696a8beSPeter Zijlstra 
347acd58620SPeter Zijlstra 	rt_mutex_setprio(p, pi_task);
3481696a8beSPeter Zijlstra }
3491696a8beSPeter Zijlstra 
3501696a8beSPeter Zijlstra /*
3518930ed80SThomas Gleixner  * Deadlock detection is conditional:
3528930ed80SThomas Gleixner  *
3538930ed80SThomas Gleixner  * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
3548930ed80SThomas Gleixner  * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
3558930ed80SThomas Gleixner  *
3568930ed80SThomas Gleixner  * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
3578930ed80SThomas Gleixner  * conducted independent of the detect argument.
3588930ed80SThomas Gleixner  *
3598930ed80SThomas Gleixner  * If the waiter argument is NULL this indicates the deboost path and
3608930ed80SThomas Gleixner  * deadlock detection is disabled independent of the detect argument
3618930ed80SThomas Gleixner  * and the config settings.
3628930ed80SThomas Gleixner  */
3638930ed80SThomas Gleixner static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
3648930ed80SThomas Gleixner 					  enum rtmutex_chainwalk chwalk)
3658930ed80SThomas Gleixner {
3668930ed80SThomas Gleixner 	/*
3678930ed80SThomas Gleixner 	 * This is just a wrapper function for the following call,
3688930ed80SThomas Gleixner 	 * because debug_rt_mutex_detect_deadlock() smells like a magic
3698930ed80SThomas Gleixner 	 * debug feature and I wanted to keep the cond function in the
3708930ed80SThomas Gleixner 	 * main source file along with the comments instead of having
3718930ed80SThomas Gleixner 	 * two of the same in the headers.
3728930ed80SThomas Gleixner 	 */
3738930ed80SThomas Gleixner 	return debug_rt_mutex_detect_deadlock(waiter, chwalk);
3748930ed80SThomas Gleixner }
3758930ed80SThomas Gleixner 
3768930ed80SThomas Gleixner /*
3771696a8beSPeter Zijlstra  * Max number of times we'll walk the boosting chain:
3781696a8beSPeter Zijlstra  */
3791696a8beSPeter Zijlstra int max_lock_depth = 1024;
3801696a8beSPeter Zijlstra 
38182084984SThomas Gleixner static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
38282084984SThomas Gleixner {
38382084984SThomas Gleixner 	return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
38482084984SThomas Gleixner }
38582084984SThomas Gleixner 
3861696a8beSPeter Zijlstra /*
3871696a8beSPeter Zijlstra  * Adjust the priority chain. Also used for deadlock detection.
3881696a8beSPeter Zijlstra  * Decreases task's usage by one - may thus free the task.
3891696a8beSPeter Zijlstra  *
39082084984SThomas Gleixner  * @task:	the task owning the mutex (owner) for which a chain walk is
39182084984SThomas Gleixner  *		probably needed
392e6beaa36STom(JeHyeon) Yeon  * @chwalk:	do we have to carry out deadlock detection?
3931696a8beSPeter Zijlstra  * @orig_lock:	the mutex (can be NULL if we are walking the chain to recheck
3941696a8beSPeter Zijlstra  *		things for a task that has just got its priority adjusted, and
3951696a8beSPeter Zijlstra  *		is waiting on a mutex)
39682084984SThomas Gleixner  * @next_lock:	the mutex on which the owner of @orig_lock was blocked before
39782084984SThomas Gleixner  *		we dropped its pi_lock. Is never dereferenced, only used for
39882084984SThomas Gleixner  *		comparison to detect lock chain changes.
3991696a8beSPeter Zijlstra  * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
4001696a8beSPeter Zijlstra  *		its priority to the mutex owner (can be NULL in the case
4011696a8beSPeter Zijlstra  *		depicted above or if the top waiter is gone away and we are
4021696a8beSPeter Zijlstra  *		actually deboosting the owner)
4031696a8beSPeter Zijlstra  * @top_task:	the current top waiter
4041696a8beSPeter Zijlstra  *
4051696a8beSPeter Zijlstra  * Returns 0 or -EDEADLK.
4063eb65aeaSThomas Gleixner  *
4073eb65aeaSThomas Gleixner  * Chain walk basics and protection scope
4083eb65aeaSThomas Gleixner  *
4093eb65aeaSThomas Gleixner  * [R] refcount on task
4103eb65aeaSThomas Gleixner  * [P] task->pi_lock held
4113eb65aeaSThomas Gleixner  * [L] rtmutex->wait_lock held
4123eb65aeaSThomas Gleixner  *
4133eb65aeaSThomas Gleixner  * Step	Description				Protected by
4143eb65aeaSThomas Gleixner  *	function arguments:
4153eb65aeaSThomas Gleixner  *	@task					[R]
4163eb65aeaSThomas Gleixner  *	@orig_lock if != NULL			@top_task is blocked on it
4173eb65aeaSThomas Gleixner  *	@next_lock				Unprotected. Cannot be
4183eb65aeaSThomas Gleixner  *						dereferenced. Only used for
4193eb65aeaSThomas Gleixner  *						comparison.
4203eb65aeaSThomas Gleixner  *	@orig_waiter if != NULL			@top_task is blocked on it
4213eb65aeaSThomas Gleixner  *	@top_task				current, or in case of proxy
4223eb65aeaSThomas Gleixner  *						locking protected by calling
4233eb65aeaSThomas Gleixner  *						code
4243eb65aeaSThomas Gleixner  *	again:
4253eb65aeaSThomas Gleixner  *	  loop_sanity_check();
4263eb65aeaSThomas Gleixner  *	retry:
4273eb65aeaSThomas Gleixner  * [1]	  lock(task->pi_lock);			[R] acquire [P]
4283eb65aeaSThomas Gleixner  * [2]	  waiter = task->pi_blocked_on;		[P]
4293eb65aeaSThomas Gleixner  * [3]	  check_exit_conditions_1();		[P]
4303eb65aeaSThomas Gleixner  * [4]	  lock = waiter->lock;			[P]
4313eb65aeaSThomas Gleixner  * [5]	  if (!try_lock(lock->wait_lock)) {	[P] try to acquire [L]
4323eb65aeaSThomas Gleixner  *	    unlock(task->pi_lock);		release [P]
4333eb65aeaSThomas Gleixner  *	    goto retry;
4343eb65aeaSThomas Gleixner  *	  }
4353eb65aeaSThomas Gleixner  * [6]	  check_exit_conditions_2();		[P] + [L]
4363eb65aeaSThomas Gleixner  * [7]	  requeue_lock_waiter(lock, waiter);	[P] + [L]
4373eb65aeaSThomas Gleixner  * [8]	  unlock(task->pi_lock);		release [P]
4383eb65aeaSThomas Gleixner  *	  put_task_struct(task);		release [R]
4393eb65aeaSThomas Gleixner  * [9]	  check_exit_conditions_3();		[L]
4403eb65aeaSThomas Gleixner  * [10]	  task = owner(lock);			[L]
4413eb65aeaSThomas Gleixner  *	  get_task_struct(task);		[L] acquire [R]
4423eb65aeaSThomas Gleixner  *	  lock(task->pi_lock);			[L] acquire [P]
4433eb65aeaSThomas Gleixner  * [11]	  requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
4443eb65aeaSThomas Gleixner  * [12]	  check_exit_conditions_4();		[P] + [L]
4453eb65aeaSThomas Gleixner  * [13]	  unlock(task->pi_lock);		release [P]
4463eb65aeaSThomas Gleixner  *	  unlock(lock->wait_lock);		release [L]
4473eb65aeaSThomas Gleixner  *	  goto again;
4481696a8beSPeter Zijlstra  */
4491696a8beSPeter Zijlstra static int rt_mutex_adjust_prio_chain(struct task_struct *task,
4508930ed80SThomas Gleixner 				      enum rtmutex_chainwalk chwalk,
4511696a8beSPeter Zijlstra 				      struct rt_mutex *orig_lock,
45282084984SThomas Gleixner 				      struct rt_mutex *next_lock,
4531696a8beSPeter Zijlstra 				      struct rt_mutex_waiter *orig_waiter,
4541696a8beSPeter Zijlstra 				      struct task_struct *top_task)
4551696a8beSPeter Zijlstra {
4561696a8beSPeter Zijlstra 	struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
457a57594a1SThomas Gleixner 	struct rt_mutex_waiter *prerequeue_top_waiter;
4588930ed80SThomas Gleixner 	int ret = 0, depth = 0;
459a57594a1SThomas Gleixner 	struct rt_mutex *lock;
4608930ed80SThomas Gleixner 	bool detect_deadlock;
46167792e2cSThomas Gleixner 	bool requeue = true;
4621696a8beSPeter Zijlstra 
4638930ed80SThomas Gleixner 	detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
4641696a8beSPeter Zijlstra 
4651696a8beSPeter Zijlstra 	/*
4661696a8beSPeter Zijlstra 	 * The (de)boosting is a step by step approach with a lot of
4671696a8beSPeter Zijlstra 	 * pitfalls. We want this to be preemptible and we want hold a
4681696a8beSPeter Zijlstra 	 * maximum of two locks per step. So we have to check
4691696a8beSPeter Zijlstra 	 * carefully whether things change under us.
4701696a8beSPeter Zijlstra 	 */
4711696a8beSPeter Zijlstra  again:
4723eb65aeaSThomas Gleixner 	/*
4733eb65aeaSThomas Gleixner 	 * We limit the lock chain length for each invocation.
4743eb65aeaSThomas Gleixner 	 */
4751696a8beSPeter Zijlstra 	if (++depth > max_lock_depth) {
4761696a8beSPeter Zijlstra 		static int prev_max;
4771696a8beSPeter Zijlstra 
4781696a8beSPeter Zijlstra 		/*
4791696a8beSPeter Zijlstra 		 * Print this only once. If the admin changes the limit,
4801696a8beSPeter Zijlstra 		 * print a new message when reaching the limit again.
4811696a8beSPeter Zijlstra 		 */
4821696a8beSPeter Zijlstra 		if (prev_max != max_lock_depth) {
4831696a8beSPeter Zijlstra 			prev_max = max_lock_depth;
4841696a8beSPeter Zijlstra 			printk(KERN_WARNING "Maximum lock depth %d reached "
4851696a8beSPeter Zijlstra 			       "task: %s (%d)\n", max_lock_depth,
4861696a8beSPeter Zijlstra 			       top_task->comm, task_pid_nr(top_task));
4871696a8beSPeter Zijlstra 		}
4881696a8beSPeter Zijlstra 		put_task_struct(task);
4891696a8beSPeter Zijlstra 
4903d5c9340SThomas Gleixner 		return -EDEADLK;
4911696a8beSPeter Zijlstra 	}
4923eb65aeaSThomas Gleixner 
4933eb65aeaSThomas Gleixner 	/*
4943eb65aeaSThomas Gleixner 	 * We are fully preemptible here and only hold the refcount on
4953eb65aeaSThomas Gleixner 	 * @task. So everything can have changed under us since the
4963eb65aeaSThomas Gleixner 	 * caller or our own code below (goto retry/again) dropped all
4973eb65aeaSThomas Gleixner 	 * locks.
4983eb65aeaSThomas Gleixner 	 */
4991696a8beSPeter Zijlstra  retry:
5001696a8beSPeter Zijlstra 	/*
5013eb65aeaSThomas Gleixner 	 * [1] Task cannot go away as we did a get_task() before !
5021696a8beSPeter Zijlstra 	 */
503b4abf910SThomas Gleixner 	raw_spin_lock_irq(&task->pi_lock);
5041696a8beSPeter Zijlstra 
5053eb65aeaSThomas Gleixner 	/*
5063eb65aeaSThomas Gleixner 	 * [2] Get the waiter on which @task is blocked on.
5073eb65aeaSThomas Gleixner 	 */
5081696a8beSPeter Zijlstra 	waiter = task->pi_blocked_on;
5093eb65aeaSThomas Gleixner 
5103eb65aeaSThomas Gleixner 	/*
5113eb65aeaSThomas Gleixner 	 * [3] check_exit_conditions_1() protected by task->pi_lock.
5123eb65aeaSThomas Gleixner 	 */
5133eb65aeaSThomas Gleixner 
5141696a8beSPeter Zijlstra 	/*
5151696a8beSPeter Zijlstra 	 * Check whether the end of the boosting chain has been
5161696a8beSPeter Zijlstra 	 * reached or the state of the chain has changed while we
5171696a8beSPeter Zijlstra 	 * dropped the locks.
5181696a8beSPeter Zijlstra 	 */
5191696a8beSPeter Zijlstra 	if (!waiter)
5201696a8beSPeter Zijlstra 		goto out_unlock_pi;
5211696a8beSPeter Zijlstra 
5221696a8beSPeter Zijlstra 	/*
5231696a8beSPeter Zijlstra 	 * Check the orig_waiter state. After we dropped the locks,
5241696a8beSPeter Zijlstra 	 * the previous owner of the lock might have released the lock.
5251696a8beSPeter Zijlstra 	 */
5261696a8beSPeter Zijlstra 	if (orig_waiter && !rt_mutex_owner(orig_lock))
5271696a8beSPeter Zijlstra 		goto out_unlock_pi;
5281696a8beSPeter Zijlstra 
5291696a8beSPeter Zijlstra 	/*
53082084984SThomas Gleixner 	 * We dropped all locks after taking a refcount on @task, so
53182084984SThomas Gleixner 	 * the task might have moved on in the lock chain or even left
53282084984SThomas Gleixner 	 * the chain completely and blocks now on an unrelated lock or
53382084984SThomas Gleixner 	 * on @orig_lock.
53482084984SThomas Gleixner 	 *
53582084984SThomas Gleixner 	 * We stored the lock on which @task was blocked in @next_lock,
53682084984SThomas Gleixner 	 * so we can detect the chain change.
53782084984SThomas Gleixner 	 */
53882084984SThomas Gleixner 	if (next_lock != waiter->lock)
53982084984SThomas Gleixner 		goto out_unlock_pi;
54082084984SThomas Gleixner 
54182084984SThomas Gleixner 	/*
5421696a8beSPeter Zijlstra 	 * Drop out, when the task has no waiters. Note,
5431696a8beSPeter Zijlstra 	 * top_waiter can be NULL, when we are in the deboosting
5441696a8beSPeter Zijlstra 	 * mode!
5451696a8beSPeter Zijlstra 	 */
546397335f0SThomas Gleixner 	if (top_waiter) {
547397335f0SThomas Gleixner 		if (!task_has_pi_waiters(task))
5481696a8beSPeter Zijlstra 			goto out_unlock_pi;
549397335f0SThomas Gleixner 		/*
550397335f0SThomas Gleixner 		 * If deadlock detection is off, we stop here if we
55167792e2cSThomas Gleixner 		 * are not the top pi waiter of the task. If deadlock
55267792e2cSThomas Gleixner 		 * detection is enabled we continue, but stop the
55367792e2cSThomas Gleixner 		 * requeueing in the chain walk.
554397335f0SThomas Gleixner 		 */
55567792e2cSThomas Gleixner 		if (top_waiter != task_top_pi_waiter(task)) {
55667792e2cSThomas Gleixner 			if (!detect_deadlock)
557397335f0SThomas Gleixner 				goto out_unlock_pi;
55867792e2cSThomas Gleixner 			else
55967792e2cSThomas Gleixner 				requeue = false;
56067792e2cSThomas Gleixner 		}
561397335f0SThomas Gleixner 	}
5621696a8beSPeter Zijlstra 
5631696a8beSPeter Zijlstra 	/*
56467792e2cSThomas Gleixner 	 * If the waiter priority is the same as the task priority
56567792e2cSThomas Gleixner 	 * then there is no further priority adjustment necessary.  If
56667792e2cSThomas Gleixner 	 * deadlock detection is off, we stop the chain walk. If its
56767792e2cSThomas Gleixner 	 * enabled we continue, but stop the requeueing in the chain
56867792e2cSThomas Gleixner 	 * walk.
5691696a8beSPeter Zijlstra 	 */
57019830e55SPeter Zijlstra 	if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
57167792e2cSThomas Gleixner 		if (!detect_deadlock)
5721696a8beSPeter Zijlstra 			goto out_unlock_pi;
57367792e2cSThomas Gleixner 		else
57467792e2cSThomas Gleixner 			requeue = false;
57567792e2cSThomas Gleixner 	}
5761696a8beSPeter Zijlstra 
5773eb65aeaSThomas Gleixner 	/*
5783eb65aeaSThomas Gleixner 	 * [4] Get the next lock
5793eb65aeaSThomas Gleixner 	 */
5801696a8beSPeter Zijlstra 	lock = waiter->lock;
5813eb65aeaSThomas Gleixner 	/*
5823eb65aeaSThomas Gleixner 	 * [5] We need to trylock here as we are holding task->pi_lock,
5833eb65aeaSThomas Gleixner 	 * which is the reverse lock order versus the other rtmutex
5843eb65aeaSThomas Gleixner 	 * operations.
5853eb65aeaSThomas Gleixner 	 */
5861696a8beSPeter Zijlstra 	if (!raw_spin_trylock(&lock->wait_lock)) {
587b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&task->pi_lock);
5881696a8beSPeter Zijlstra 		cpu_relax();
5891696a8beSPeter Zijlstra 		goto retry;
5901696a8beSPeter Zijlstra 	}
5911696a8beSPeter Zijlstra 
592397335f0SThomas Gleixner 	/*
5933eb65aeaSThomas Gleixner 	 * [6] check_exit_conditions_2() protected by task->pi_lock and
5943eb65aeaSThomas Gleixner 	 * lock->wait_lock.
5953eb65aeaSThomas Gleixner 	 *
596397335f0SThomas Gleixner 	 * Deadlock detection. If the lock is the same as the original
597397335f0SThomas Gleixner 	 * lock which caused us to walk the lock chain or if the
598397335f0SThomas Gleixner 	 * current lock is owned by the task which initiated the chain
599397335f0SThomas Gleixner 	 * walk, we detected a deadlock.
600397335f0SThomas Gleixner 	 */
6011696a8beSPeter Zijlstra 	if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
6028930ed80SThomas Gleixner 		debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
6031696a8beSPeter Zijlstra 		raw_spin_unlock(&lock->wait_lock);
6043d5c9340SThomas Gleixner 		ret = -EDEADLK;
6051696a8beSPeter Zijlstra 		goto out_unlock_pi;
6061696a8beSPeter Zijlstra 	}
6071696a8beSPeter Zijlstra 
608a57594a1SThomas Gleixner 	/*
60967792e2cSThomas Gleixner 	 * If we just follow the lock chain for deadlock detection, no
61067792e2cSThomas Gleixner 	 * need to do all the requeue operations. To avoid a truckload
61167792e2cSThomas Gleixner 	 * of conditionals around the various places below, just do the
61267792e2cSThomas Gleixner 	 * minimum chain walk checks.
61367792e2cSThomas Gleixner 	 */
61467792e2cSThomas Gleixner 	if (!requeue) {
61567792e2cSThomas Gleixner 		/*
61667792e2cSThomas Gleixner 		 * No requeue[7] here. Just release @task [8]
61767792e2cSThomas Gleixner 		 */
618b4abf910SThomas Gleixner 		raw_spin_unlock(&task->pi_lock);
61967792e2cSThomas Gleixner 		put_task_struct(task);
62067792e2cSThomas Gleixner 
62167792e2cSThomas Gleixner 		/*
62267792e2cSThomas Gleixner 		 * [9] check_exit_conditions_3 protected by lock->wait_lock.
62367792e2cSThomas Gleixner 		 * If there is no owner of the lock, end of chain.
62467792e2cSThomas Gleixner 		 */
62567792e2cSThomas Gleixner 		if (!rt_mutex_owner(lock)) {
626b4abf910SThomas Gleixner 			raw_spin_unlock_irq(&lock->wait_lock);
62767792e2cSThomas Gleixner 			return 0;
62867792e2cSThomas Gleixner 		}
62967792e2cSThomas Gleixner 
63067792e2cSThomas Gleixner 		/* [10] Grab the next task, i.e. owner of @lock */
631*7b3c92b8SMatthew Wilcox (Oracle) 		task = get_task_struct(rt_mutex_owner(lock));
632b4abf910SThomas Gleixner 		raw_spin_lock(&task->pi_lock);
63367792e2cSThomas Gleixner 
63467792e2cSThomas Gleixner 		/*
63567792e2cSThomas Gleixner 		 * No requeue [11] here. We just do deadlock detection.
63667792e2cSThomas Gleixner 		 *
63767792e2cSThomas Gleixner 		 * [12] Store whether owner is blocked
63867792e2cSThomas Gleixner 		 * itself. Decision is made after dropping the locks
63967792e2cSThomas Gleixner 		 */
64067792e2cSThomas Gleixner 		next_lock = task_blocked_on_lock(task);
64167792e2cSThomas Gleixner 		/*
64267792e2cSThomas Gleixner 		 * Get the top waiter for the next iteration
64367792e2cSThomas Gleixner 		 */
64467792e2cSThomas Gleixner 		top_waiter = rt_mutex_top_waiter(lock);
64567792e2cSThomas Gleixner 
64667792e2cSThomas Gleixner 		/* [13] Drop locks */
647b4abf910SThomas Gleixner 		raw_spin_unlock(&task->pi_lock);
648b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
64967792e2cSThomas Gleixner 
65067792e2cSThomas Gleixner 		/* If owner is not blocked, end of chain. */
65167792e2cSThomas Gleixner 		if (!next_lock)
65267792e2cSThomas Gleixner 			goto out_put_task;
65367792e2cSThomas Gleixner 		goto again;
65467792e2cSThomas Gleixner 	}
65567792e2cSThomas Gleixner 
65667792e2cSThomas Gleixner 	/*
657a57594a1SThomas Gleixner 	 * Store the current top waiter before doing the requeue
658a57594a1SThomas Gleixner 	 * operation on @lock. We need it for the boost/deboost
659a57594a1SThomas Gleixner 	 * decision below.
660a57594a1SThomas Gleixner 	 */
661a57594a1SThomas Gleixner 	prerequeue_top_waiter = rt_mutex_top_waiter(lock);
6621696a8beSPeter Zijlstra 
6639f40a51aSDavidlohr Bueso 	/* [7] Requeue the waiter in the lock waiter tree. */
664fb00aca4SPeter Zijlstra 	rt_mutex_dequeue(lock, waiter);
665e0aad5b4SPeter Zijlstra 
666e0aad5b4SPeter Zijlstra 	/*
667e0aad5b4SPeter Zijlstra 	 * Update the waiter prio fields now that we're dequeued.
668e0aad5b4SPeter Zijlstra 	 *
669e0aad5b4SPeter Zijlstra 	 * These values can have changed through either:
670e0aad5b4SPeter Zijlstra 	 *
671e0aad5b4SPeter Zijlstra 	 *   sys_sched_set_scheduler() / sys_sched_setattr()
672e0aad5b4SPeter Zijlstra 	 *
673e0aad5b4SPeter Zijlstra 	 * or
674e0aad5b4SPeter Zijlstra 	 *
675e0aad5b4SPeter Zijlstra 	 *   DL CBS enforcement advancing the effective deadline.
676e0aad5b4SPeter Zijlstra 	 *
677e0aad5b4SPeter Zijlstra 	 * Even though pi_waiters also uses these fields, and that tree is only
678e0aad5b4SPeter Zijlstra 	 * updated in [11], we can do this here, since we hold [L], which
679e0aad5b4SPeter Zijlstra 	 * serializes all pi_waiters access and rb_erase() does not care about
680e0aad5b4SPeter Zijlstra 	 * the values of the node being removed.
681e0aad5b4SPeter Zijlstra 	 */
6822d3d891dSDario Faggioli 	waiter->prio = task->prio;
683e0aad5b4SPeter Zijlstra 	waiter->deadline = task->dl.deadline;
684e0aad5b4SPeter Zijlstra 
685fb00aca4SPeter Zijlstra 	rt_mutex_enqueue(lock, waiter);
6861696a8beSPeter Zijlstra 
6873eb65aeaSThomas Gleixner 	/* [8] Release the task */
688b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
6892ffa5a5cSThomas Gleixner 	put_task_struct(task);
6902ffa5a5cSThomas Gleixner 
691a57594a1SThomas Gleixner 	/*
6923eb65aeaSThomas Gleixner 	 * [9] check_exit_conditions_3 protected by lock->wait_lock.
6933eb65aeaSThomas Gleixner 	 *
694a57594a1SThomas Gleixner 	 * We must abort the chain walk if there is no lock owner even
695a57594a1SThomas Gleixner 	 * in the dead lock detection case, as we have nothing to
696a57594a1SThomas Gleixner 	 * follow here. This is the end of the chain we are walking.
697a57594a1SThomas Gleixner 	 */
6981696a8beSPeter Zijlstra 	if (!rt_mutex_owner(lock)) {
6991696a8beSPeter Zijlstra 		/*
7003eb65aeaSThomas Gleixner 		 * If the requeue [7] above changed the top waiter,
7013eb65aeaSThomas Gleixner 		 * then we need to wake the new top waiter up to try
7023eb65aeaSThomas Gleixner 		 * to get the lock.
7031696a8beSPeter Zijlstra 		 */
704a57594a1SThomas Gleixner 		if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
7051696a8beSPeter Zijlstra 			wake_up_process(rt_mutex_top_waiter(lock)->task);
706b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
7072ffa5a5cSThomas Gleixner 		return 0;
7081696a8beSPeter Zijlstra 	}
7091696a8beSPeter Zijlstra 
7103eb65aeaSThomas Gleixner 	/* [10] Grab the next task, i.e. the owner of @lock */
711*7b3c92b8SMatthew Wilcox (Oracle) 	task = get_task_struct(rt_mutex_owner(lock));
712b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
7131696a8beSPeter Zijlstra 
7143eb65aeaSThomas Gleixner 	/* [11] requeue the pi waiters if necessary */
7151696a8beSPeter Zijlstra 	if (waiter == rt_mutex_top_waiter(lock)) {
716a57594a1SThomas Gleixner 		/*
717a57594a1SThomas Gleixner 		 * The waiter became the new top (highest priority)
718a57594a1SThomas Gleixner 		 * waiter on the lock. Replace the previous top waiter
7199f40a51aSDavidlohr Bueso 		 * in the owner tasks pi waiters tree with this waiter
720a57594a1SThomas Gleixner 		 * and adjust the priority of the owner.
721a57594a1SThomas Gleixner 		 */
722a57594a1SThomas Gleixner 		rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
723fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(task, waiter);
724acd58620SPeter Zijlstra 		rt_mutex_adjust_prio(task);
7251696a8beSPeter Zijlstra 
726a57594a1SThomas Gleixner 	} else if (prerequeue_top_waiter == waiter) {
727a57594a1SThomas Gleixner 		/*
728a57594a1SThomas Gleixner 		 * The waiter was the top waiter on the lock, but is
729a57594a1SThomas Gleixner 		 * no longer the top prority waiter. Replace waiter in
7309f40a51aSDavidlohr Bueso 		 * the owner tasks pi waiters tree with the new top
731a57594a1SThomas Gleixner 		 * (highest priority) waiter and adjust the priority
732a57594a1SThomas Gleixner 		 * of the owner.
733a57594a1SThomas Gleixner 		 * The new top waiter is stored in @waiter so that
734a57594a1SThomas Gleixner 		 * @waiter == @top_waiter evaluates to true below and
735a57594a1SThomas Gleixner 		 * we continue to deboost the rest of the chain.
736a57594a1SThomas Gleixner 		 */
737fb00aca4SPeter Zijlstra 		rt_mutex_dequeue_pi(task, waiter);
7381696a8beSPeter Zijlstra 		waiter = rt_mutex_top_waiter(lock);
739fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(task, waiter);
740acd58620SPeter Zijlstra 		rt_mutex_adjust_prio(task);
741a57594a1SThomas Gleixner 	} else {
742a57594a1SThomas Gleixner 		/*
743a57594a1SThomas Gleixner 		 * Nothing changed. No need to do any priority
744a57594a1SThomas Gleixner 		 * adjustment.
745a57594a1SThomas Gleixner 		 */
7461696a8beSPeter Zijlstra 	}
7471696a8beSPeter Zijlstra 
74882084984SThomas Gleixner 	/*
7493eb65aeaSThomas Gleixner 	 * [12] check_exit_conditions_4() protected by task->pi_lock
7503eb65aeaSThomas Gleixner 	 * and lock->wait_lock. The actual decisions are made after we
7513eb65aeaSThomas Gleixner 	 * dropped the locks.
7523eb65aeaSThomas Gleixner 	 *
75382084984SThomas Gleixner 	 * Check whether the task which owns the current lock is pi
75482084984SThomas Gleixner 	 * blocked itself. If yes we store a pointer to the lock for
75582084984SThomas Gleixner 	 * the lock chain change detection above. After we dropped
75682084984SThomas Gleixner 	 * task->pi_lock next_lock cannot be dereferenced anymore.
75782084984SThomas Gleixner 	 */
75882084984SThomas Gleixner 	next_lock = task_blocked_on_lock(task);
759a57594a1SThomas Gleixner 	/*
760a57594a1SThomas Gleixner 	 * Store the top waiter of @lock for the end of chain walk
761a57594a1SThomas Gleixner 	 * decision below.
762a57594a1SThomas Gleixner 	 */
7631696a8beSPeter Zijlstra 	top_waiter = rt_mutex_top_waiter(lock);
7643eb65aeaSThomas Gleixner 
7653eb65aeaSThomas Gleixner 	/* [13] Drop the locks */
766b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
767b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
7681696a8beSPeter Zijlstra 
76982084984SThomas Gleixner 	/*
7703eb65aeaSThomas Gleixner 	 * Make the actual exit decisions [12], based on the stored
7713eb65aeaSThomas Gleixner 	 * values.
7723eb65aeaSThomas Gleixner 	 *
77382084984SThomas Gleixner 	 * We reached the end of the lock chain. Stop right here. No
77482084984SThomas Gleixner 	 * point to go back just to figure that out.
77582084984SThomas Gleixner 	 */
77682084984SThomas Gleixner 	if (!next_lock)
77782084984SThomas Gleixner 		goto out_put_task;
77882084984SThomas Gleixner 
779a57594a1SThomas Gleixner 	/*
780a57594a1SThomas Gleixner 	 * If the current waiter is not the top waiter on the lock,
781a57594a1SThomas Gleixner 	 * then we can stop the chain walk here if we are not in full
782a57594a1SThomas Gleixner 	 * deadlock detection mode.
783a57594a1SThomas Gleixner 	 */
7841696a8beSPeter Zijlstra 	if (!detect_deadlock && waiter != top_waiter)
7851696a8beSPeter Zijlstra 		goto out_put_task;
7861696a8beSPeter Zijlstra 
7871696a8beSPeter Zijlstra 	goto again;
7881696a8beSPeter Zijlstra 
7891696a8beSPeter Zijlstra  out_unlock_pi:
790b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&task->pi_lock);
7911696a8beSPeter Zijlstra  out_put_task:
7921696a8beSPeter Zijlstra 	put_task_struct(task);
7931696a8beSPeter Zijlstra 
7941696a8beSPeter Zijlstra 	return ret;
7951696a8beSPeter Zijlstra }
7961696a8beSPeter Zijlstra 
7971696a8beSPeter Zijlstra /*
7981696a8beSPeter Zijlstra  * Try to take an rt-mutex
7991696a8beSPeter Zijlstra  *
800b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled
8011696a8beSPeter Zijlstra  *
802358c331fSThomas Gleixner  * @lock:   The lock to be acquired.
803358c331fSThomas Gleixner  * @task:   The task which wants to acquire the lock
8049f40a51aSDavidlohr Bueso  * @waiter: The waiter that is queued to the lock's wait tree if the
805358c331fSThomas Gleixner  *	    callsite called task_blocked_on_lock(), otherwise NULL
8061696a8beSPeter Zijlstra  */
8071696a8beSPeter Zijlstra static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
8081696a8beSPeter Zijlstra 				struct rt_mutex_waiter *waiter)
8091696a8beSPeter Zijlstra {
810e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
811e0aad5b4SPeter Zijlstra 
8121696a8beSPeter Zijlstra 	/*
813358c331fSThomas Gleixner 	 * Before testing whether we can acquire @lock, we set the
814358c331fSThomas Gleixner 	 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
815358c331fSThomas Gleixner 	 * other tasks which try to modify @lock into the slow path
816358c331fSThomas Gleixner 	 * and they serialize on @lock->wait_lock.
8171696a8beSPeter Zijlstra 	 *
818358c331fSThomas Gleixner 	 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
819358c331fSThomas Gleixner 	 * as explained at the top of this file if and only if:
8201696a8beSPeter Zijlstra 	 *
821358c331fSThomas Gleixner 	 * - There is a lock owner. The caller must fixup the
822358c331fSThomas Gleixner 	 *   transient state if it does a trylock or leaves the lock
823358c331fSThomas Gleixner 	 *   function due to a signal or timeout.
824358c331fSThomas Gleixner 	 *
825358c331fSThomas Gleixner 	 * - @task acquires the lock and there are no other
826358c331fSThomas Gleixner 	 *   waiters. This is undone in rt_mutex_set_owner(@task) at
827358c331fSThomas Gleixner 	 *   the end of this function.
8281696a8beSPeter Zijlstra 	 */
8291696a8beSPeter Zijlstra 	mark_rt_mutex_waiters(lock);
8301696a8beSPeter Zijlstra 
831358c331fSThomas Gleixner 	/*
832358c331fSThomas Gleixner 	 * If @lock has an owner, give up.
833358c331fSThomas Gleixner 	 */
8341696a8beSPeter Zijlstra 	if (rt_mutex_owner(lock))
8351696a8beSPeter Zijlstra 		return 0;
8361696a8beSPeter Zijlstra 
8371696a8beSPeter Zijlstra 	/*
838358c331fSThomas Gleixner 	 * If @waiter != NULL, @task has already enqueued the waiter
8399f40a51aSDavidlohr Bueso 	 * into @lock waiter tree. If @waiter == NULL then this is a
840358c331fSThomas Gleixner 	 * trylock attempt.
841358c331fSThomas Gleixner 	 */
842358c331fSThomas Gleixner 	if (waiter) {
843358c331fSThomas Gleixner 		/*
844358c331fSThomas Gleixner 		 * If waiter is not the highest priority waiter of
845358c331fSThomas Gleixner 		 * @lock, give up.
846358c331fSThomas Gleixner 		 */
847358c331fSThomas Gleixner 		if (waiter != rt_mutex_top_waiter(lock))
848358c331fSThomas Gleixner 			return 0;
849358c331fSThomas Gleixner 
850358c331fSThomas Gleixner 		/*
851358c331fSThomas Gleixner 		 * We can acquire the lock. Remove the waiter from the
8529f40a51aSDavidlohr Bueso 		 * lock waiters tree.
853358c331fSThomas Gleixner 		 */
854358c331fSThomas Gleixner 		rt_mutex_dequeue(lock, waiter);
855358c331fSThomas Gleixner 
856358c331fSThomas Gleixner 	} else {
857358c331fSThomas Gleixner 		/*
858358c331fSThomas Gleixner 		 * If the lock has waiters already we check whether @task is
859358c331fSThomas Gleixner 		 * eligible to take over the lock.
860358c331fSThomas Gleixner 		 *
861358c331fSThomas Gleixner 		 * If there are no other waiters, @task can acquire
862358c331fSThomas Gleixner 		 * the lock.  @task->pi_blocked_on is NULL, so it does
863358c331fSThomas Gleixner 		 * not need to be dequeued.
8641696a8beSPeter Zijlstra 		 */
8651696a8beSPeter Zijlstra 		if (rt_mutex_has_waiters(lock)) {
866358c331fSThomas Gleixner 			/*
867358c331fSThomas Gleixner 			 * If @task->prio is greater than or equal to
868358c331fSThomas Gleixner 			 * the top waiter priority (kernel view),
869358c331fSThomas Gleixner 			 * @task lost.
870358c331fSThomas Gleixner 			 */
87119830e55SPeter Zijlstra 			if (!rt_mutex_waiter_less(task_to_waiter(task),
87219830e55SPeter Zijlstra 						  rt_mutex_top_waiter(lock)))
8731696a8beSPeter Zijlstra 				return 0;
874358c331fSThomas Gleixner 
875358c331fSThomas Gleixner 			/*
876358c331fSThomas Gleixner 			 * The current top waiter stays enqueued. We
877358c331fSThomas Gleixner 			 * don't have to change anything in the lock
878358c331fSThomas Gleixner 			 * waiters order.
879358c331fSThomas Gleixner 			 */
880358c331fSThomas Gleixner 		} else {
881358c331fSThomas Gleixner 			/*
882358c331fSThomas Gleixner 			 * No waiters. Take the lock without the
883358c331fSThomas Gleixner 			 * pi_lock dance.@task->pi_blocked_on is NULL
884358c331fSThomas Gleixner 			 * and we have no waiters to enqueue in @task
8859f40a51aSDavidlohr Bueso 			 * pi waiters tree.
886358c331fSThomas Gleixner 			 */
887358c331fSThomas Gleixner 			goto takeit;
8881696a8beSPeter Zijlstra 		}
8891696a8beSPeter Zijlstra 	}
8901696a8beSPeter Zijlstra 
8911696a8beSPeter Zijlstra 	/*
892358c331fSThomas Gleixner 	 * Clear @task->pi_blocked_on. Requires protection by
893358c331fSThomas Gleixner 	 * @task->pi_lock. Redundant operation for the @waiter == NULL
894358c331fSThomas Gleixner 	 * case, but conditionals are more expensive than a redundant
895358c331fSThomas Gleixner 	 * store.
8961696a8beSPeter Zijlstra 	 */
897b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
898358c331fSThomas Gleixner 	task->pi_blocked_on = NULL;
899358c331fSThomas Gleixner 	/*
900358c331fSThomas Gleixner 	 * Finish the lock acquisition. @task is the new owner. If
901358c331fSThomas Gleixner 	 * other waiters exist we have to insert the highest priority
9029f40a51aSDavidlohr Bueso 	 * waiter into @task->pi_waiters tree.
903358c331fSThomas Gleixner 	 */
904358c331fSThomas Gleixner 	if (rt_mutex_has_waiters(lock))
905358c331fSThomas Gleixner 		rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
906b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
9071696a8beSPeter Zijlstra 
908358c331fSThomas Gleixner takeit:
9091696a8beSPeter Zijlstra 	/* We got the lock. */
9101696a8beSPeter Zijlstra 	debug_rt_mutex_lock(lock);
9111696a8beSPeter Zijlstra 
912358c331fSThomas Gleixner 	/*
913358c331fSThomas Gleixner 	 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
914358c331fSThomas Gleixner 	 * are still waiters or clears it.
915358c331fSThomas Gleixner 	 */
9161696a8beSPeter Zijlstra 	rt_mutex_set_owner(lock, task);
9171696a8beSPeter Zijlstra 
9181696a8beSPeter Zijlstra 	return 1;
9191696a8beSPeter Zijlstra }
9201696a8beSPeter Zijlstra 
9211696a8beSPeter Zijlstra /*
9221696a8beSPeter Zijlstra  * Task blocks on lock.
9231696a8beSPeter Zijlstra  *
9241696a8beSPeter Zijlstra  * Prepare waiter and propagate pi chain
9251696a8beSPeter Zijlstra  *
926b4abf910SThomas Gleixner  * This must be called with lock->wait_lock held and interrupts disabled
9271696a8beSPeter Zijlstra  */
9281696a8beSPeter Zijlstra static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
9291696a8beSPeter Zijlstra 				   struct rt_mutex_waiter *waiter,
9301696a8beSPeter Zijlstra 				   struct task_struct *task,
9318930ed80SThomas Gleixner 				   enum rtmutex_chainwalk chwalk)
9321696a8beSPeter Zijlstra {
9331696a8beSPeter Zijlstra 	struct task_struct *owner = rt_mutex_owner(lock);
9341696a8beSPeter Zijlstra 	struct rt_mutex_waiter *top_waiter = waiter;
93582084984SThomas Gleixner 	struct rt_mutex *next_lock;
9361696a8beSPeter Zijlstra 	int chain_walk = 0, res;
9371696a8beSPeter Zijlstra 
938e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
939e0aad5b4SPeter Zijlstra 
940397335f0SThomas Gleixner 	/*
941397335f0SThomas Gleixner 	 * Early deadlock detection. We really don't want the task to
942397335f0SThomas Gleixner 	 * enqueue on itself just to untangle the mess later. It's not
943397335f0SThomas Gleixner 	 * only an optimization. We drop the locks, so another waiter
944397335f0SThomas Gleixner 	 * can come in before the chain walk detects the deadlock. So
945397335f0SThomas Gleixner 	 * the other will detect the deadlock and return -EDEADLOCK,
946397335f0SThomas Gleixner 	 * which is wrong, as the other waiter is not in a deadlock
947397335f0SThomas Gleixner 	 * situation.
948397335f0SThomas Gleixner 	 */
9493d5c9340SThomas Gleixner 	if (owner == task)
950397335f0SThomas Gleixner 		return -EDEADLK;
951397335f0SThomas Gleixner 
952b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
9531696a8beSPeter Zijlstra 	waiter->task = task;
9541696a8beSPeter Zijlstra 	waiter->lock = lock;
9552d3d891dSDario Faggioli 	waiter->prio = task->prio;
956e0aad5b4SPeter Zijlstra 	waiter->deadline = task->dl.deadline;
9571696a8beSPeter Zijlstra 
9581696a8beSPeter Zijlstra 	/* Get the top priority waiter on the lock */
9591696a8beSPeter Zijlstra 	if (rt_mutex_has_waiters(lock))
9601696a8beSPeter Zijlstra 		top_waiter = rt_mutex_top_waiter(lock);
961fb00aca4SPeter Zijlstra 	rt_mutex_enqueue(lock, waiter);
9621696a8beSPeter Zijlstra 
9631696a8beSPeter Zijlstra 	task->pi_blocked_on = waiter;
9641696a8beSPeter Zijlstra 
965b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
9661696a8beSPeter Zijlstra 
9671696a8beSPeter Zijlstra 	if (!owner)
9681696a8beSPeter Zijlstra 		return 0;
9691696a8beSPeter Zijlstra 
970b4abf910SThomas Gleixner 	raw_spin_lock(&owner->pi_lock);
97182084984SThomas Gleixner 	if (waiter == rt_mutex_top_waiter(lock)) {
972fb00aca4SPeter Zijlstra 		rt_mutex_dequeue_pi(owner, top_waiter);
973fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(owner, waiter);
9741696a8beSPeter Zijlstra 
975acd58620SPeter Zijlstra 		rt_mutex_adjust_prio(owner);
9761696a8beSPeter Zijlstra 		if (owner->pi_blocked_on)
9771696a8beSPeter Zijlstra 			chain_walk = 1;
9788930ed80SThomas Gleixner 	} else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
9791696a8beSPeter Zijlstra 		chain_walk = 1;
98082084984SThomas Gleixner 	}
9811696a8beSPeter Zijlstra 
98282084984SThomas Gleixner 	/* Store the lock on which owner is blocked or NULL */
98382084984SThomas Gleixner 	next_lock = task_blocked_on_lock(owner);
98482084984SThomas Gleixner 
985b4abf910SThomas Gleixner 	raw_spin_unlock(&owner->pi_lock);
98682084984SThomas Gleixner 	/*
98782084984SThomas Gleixner 	 * Even if full deadlock detection is on, if the owner is not
98882084984SThomas Gleixner 	 * blocked itself, we can avoid finding this out in the chain
98982084984SThomas Gleixner 	 * walk.
99082084984SThomas Gleixner 	 */
99182084984SThomas Gleixner 	if (!chain_walk || !next_lock)
9921696a8beSPeter Zijlstra 		return 0;
9931696a8beSPeter Zijlstra 
9941696a8beSPeter Zijlstra 	/*
9951696a8beSPeter Zijlstra 	 * The owner can't disappear while holding a lock,
9961696a8beSPeter Zijlstra 	 * so the owner struct is protected by wait_lock.
9971696a8beSPeter Zijlstra 	 * Gets dropped in rt_mutex_adjust_prio_chain()!
9981696a8beSPeter Zijlstra 	 */
9991696a8beSPeter Zijlstra 	get_task_struct(owner);
10001696a8beSPeter Zijlstra 
1001b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
10021696a8beSPeter Zijlstra 
10038930ed80SThomas Gleixner 	res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
100482084984SThomas Gleixner 					 next_lock, waiter, task);
10051696a8beSPeter Zijlstra 
1006b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
10071696a8beSPeter Zijlstra 
10081696a8beSPeter Zijlstra 	return res;
10091696a8beSPeter Zijlstra }
10101696a8beSPeter Zijlstra 
10111696a8beSPeter Zijlstra /*
10129f40a51aSDavidlohr Bueso  * Remove the top waiter from the current tasks pi waiter tree and
101345ab4effSDavidlohr Bueso  * queue it up.
10141696a8beSPeter Zijlstra  *
1015b4abf910SThomas Gleixner  * Called with lock->wait_lock held and interrupts disabled.
10161696a8beSPeter Zijlstra  */
101745ab4effSDavidlohr Bueso static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
101845ab4effSDavidlohr Bueso 				    struct rt_mutex *lock)
10191696a8beSPeter Zijlstra {
10201696a8beSPeter Zijlstra 	struct rt_mutex_waiter *waiter;
10211696a8beSPeter Zijlstra 
1022b4abf910SThomas Gleixner 	raw_spin_lock(&current->pi_lock);
10231696a8beSPeter Zijlstra 
10241696a8beSPeter Zijlstra 	waiter = rt_mutex_top_waiter(lock);
10251696a8beSPeter Zijlstra 
10261696a8beSPeter Zijlstra 	/*
1027acd58620SPeter Zijlstra 	 * Remove it from current->pi_waiters and deboost.
1028acd58620SPeter Zijlstra 	 *
1029acd58620SPeter Zijlstra 	 * We must in fact deboost here in order to ensure we call
1030acd58620SPeter Zijlstra 	 * rt_mutex_setprio() to update p->pi_top_task before the
1031acd58620SPeter Zijlstra 	 * task unblocks.
10321696a8beSPeter Zijlstra 	 */
1033fb00aca4SPeter Zijlstra 	rt_mutex_dequeue_pi(current, waiter);
1034acd58620SPeter Zijlstra 	rt_mutex_adjust_prio(current);
10351696a8beSPeter Zijlstra 
103627e35715SThomas Gleixner 	/*
103727e35715SThomas Gleixner 	 * As we are waking up the top waiter, and the waiter stays
103827e35715SThomas Gleixner 	 * queued on the lock until it gets the lock, this lock
103927e35715SThomas Gleixner 	 * obviously has waiters. Just set the bit here and this has
104027e35715SThomas Gleixner 	 * the added benefit of forcing all new tasks into the
104127e35715SThomas Gleixner 	 * slow path making sure no task of lower priority than
104227e35715SThomas Gleixner 	 * the top waiter can steal this lock.
104327e35715SThomas Gleixner 	 */
104427e35715SThomas Gleixner 	lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
10451696a8beSPeter Zijlstra 
1046acd58620SPeter Zijlstra 	/*
1047acd58620SPeter Zijlstra 	 * We deboosted before waking the top waiter task such that we don't
1048acd58620SPeter Zijlstra 	 * run two tasks with the 'same' priority (and ensure the
1049acd58620SPeter Zijlstra 	 * p->pi_top_task pointer points to a blocked task). This however can
1050acd58620SPeter Zijlstra 	 * lead to priority inversion if we would get preempted after the
1051acd58620SPeter Zijlstra 	 * deboost but before waking our donor task, hence the preempt_disable()
1052acd58620SPeter Zijlstra 	 * before unlock.
1053acd58620SPeter Zijlstra 	 *
1054acd58620SPeter Zijlstra 	 * Pairs with preempt_enable() in rt_mutex_postunlock();
1055acd58620SPeter Zijlstra 	 */
1056acd58620SPeter Zijlstra 	preempt_disable();
105745ab4effSDavidlohr Bueso 	wake_q_add(wake_q, waiter->task);
1058acd58620SPeter Zijlstra 	raw_spin_unlock(&current->pi_lock);
10591696a8beSPeter Zijlstra }
10601696a8beSPeter Zijlstra 
10611696a8beSPeter Zijlstra /*
10621696a8beSPeter Zijlstra  * Remove a waiter from a lock and give up
10631696a8beSPeter Zijlstra  *
1064b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled. I must
10651696a8beSPeter Zijlstra  * have just failed to try_to_take_rt_mutex().
10661696a8beSPeter Zijlstra  */
10671696a8beSPeter Zijlstra static void remove_waiter(struct rt_mutex *lock,
10681696a8beSPeter Zijlstra 			  struct rt_mutex_waiter *waiter)
10691696a8beSPeter Zijlstra {
10701ca7b860SThomas Gleixner 	bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
10711696a8beSPeter Zijlstra 	struct task_struct *owner = rt_mutex_owner(lock);
10721ca7b860SThomas Gleixner 	struct rt_mutex *next_lock;
10731696a8beSPeter Zijlstra 
1074e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1075e0aad5b4SPeter Zijlstra 
1076b4abf910SThomas Gleixner 	raw_spin_lock(&current->pi_lock);
1077fb00aca4SPeter Zijlstra 	rt_mutex_dequeue(lock, waiter);
10781696a8beSPeter Zijlstra 	current->pi_blocked_on = NULL;
1079b4abf910SThomas Gleixner 	raw_spin_unlock(&current->pi_lock);
10801696a8beSPeter Zijlstra 
10811ca7b860SThomas Gleixner 	/*
10821ca7b860SThomas Gleixner 	 * Only update priority if the waiter was the highest priority
10831ca7b860SThomas Gleixner 	 * waiter of the lock and there is an owner to update.
10841ca7b860SThomas Gleixner 	 */
10851ca7b860SThomas Gleixner 	if (!owner || !is_top_waiter)
10861696a8beSPeter Zijlstra 		return;
10871696a8beSPeter Zijlstra 
1088b4abf910SThomas Gleixner 	raw_spin_lock(&owner->pi_lock);
10891696a8beSPeter Zijlstra 
1090fb00aca4SPeter Zijlstra 	rt_mutex_dequeue_pi(owner, waiter);
10911696a8beSPeter Zijlstra 
10921ca7b860SThomas Gleixner 	if (rt_mutex_has_waiters(lock))
10931ca7b860SThomas Gleixner 		rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
10941696a8beSPeter Zijlstra 
1095acd58620SPeter Zijlstra 	rt_mutex_adjust_prio(owner);
10961696a8beSPeter Zijlstra 
109782084984SThomas Gleixner 	/* Store the lock on which owner is blocked or NULL */
109882084984SThomas Gleixner 	next_lock = task_blocked_on_lock(owner);
10991696a8beSPeter Zijlstra 
1100b4abf910SThomas Gleixner 	raw_spin_unlock(&owner->pi_lock);
11011696a8beSPeter Zijlstra 
11021ca7b860SThomas Gleixner 	/*
11031ca7b860SThomas Gleixner 	 * Don't walk the chain, if the owner task is not blocked
11041ca7b860SThomas Gleixner 	 * itself.
11051ca7b860SThomas Gleixner 	 */
110682084984SThomas Gleixner 	if (!next_lock)
11071696a8beSPeter Zijlstra 		return;
11081696a8beSPeter Zijlstra 
11091696a8beSPeter Zijlstra 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
11101696a8beSPeter Zijlstra 	get_task_struct(owner);
11111696a8beSPeter Zijlstra 
1112b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
11131696a8beSPeter Zijlstra 
11148930ed80SThomas Gleixner 	rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
11158930ed80SThomas Gleixner 				   next_lock, NULL, current);
11161696a8beSPeter Zijlstra 
1117b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
11181696a8beSPeter Zijlstra }
11191696a8beSPeter Zijlstra 
11201696a8beSPeter Zijlstra /*
11211696a8beSPeter Zijlstra  * Recheck the pi chain, in case we got a priority setting
11221696a8beSPeter Zijlstra  *
11231696a8beSPeter Zijlstra  * Called from sched_setscheduler
11241696a8beSPeter Zijlstra  */
11251696a8beSPeter Zijlstra void rt_mutex_adjust_pi(struct task_struct *task)
11261696a8beSPeter Zijlstra {
11271696a8beSPeter Zijlstra 	struct rt_mutex_waiter *waiter;
112882084984SThomas Gleixner 	struct rt_mutex *next_lock;
11291696a8beSPeter Zijlstra 	unsigned long flags;
11301696a8beSPeter Zijlstra 
11311696a8beSPeter Zijlstra 	raw_spin_lock_irqsave(&task->pi_lock, flags);
11321696a8beSPeter Zijlstra 
11331696a8beSPeter Zijlstra 	waiter = task->pi_blocked_on;
113419830e55SPeter Zijlstra 	if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
11351696a8beSPeter Zijlstra 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
11361696a8beSPeter Zijlstra 		return;
11371696a8beSPeter Zijlstra 	}
113882084984SThomas Gleixner 	next_lock = waiter->lock;
11391696a8beSPeter Zijlstra 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
11401696a8beSPeter Zijlstra 
11411696a8beSPeter Zijlstra 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
11421696a8beSPeter Zijlstra 	get_task_struct(task);
114382084984SThomas Gleixner 
11448930ed80SThomas Gleixner 	rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
11458930ed80SThomas Gleixner 				   next_lock, NULL, task);
11461696a8beSPeter Zijlstra }
11471696a8beSPeter Zijlstra 
114850809358SPeter Zijlstra void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
114950809358SPeter Zijlstra {
115050809358SPeter Zijlstra 	debug_rt_mutex_init_waiter(waiter);
115150809358SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->pi_tree_entry);
115250809358SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->tree_entry);
115350809358SPeter Zijlstra 	waiter->task = NULL;
115450809358SPeter Zijlstra }
115550809358SPeter Zijlstra 
11561696a8beSPeter Zijlstra /**
11571696a8beSPeter Zijlstra  * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
11581696a8beSPeter Zijlstra  * @lock:		 the rt_mutex to take
11591696a8beSPeter Zijlstra  * @state:		 the state the task should block in (TASK_INTERRUPTIBLE
11601696a8beSPeter Zijlstra  *			 or TASK_UNINTERRUPTIBLE)
11611696a8beSPeter Zijlstra  * @timeout:		 the pre-initialized and started timer, or NULL for none
11621696a8beSPeter Zijlstra  * @waiter:		 the pre-initialized rt_mutex_waiter
11631696a8beSPeter Zijlstra  *
1164b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled
11651696a8beSPeter Zijlstra  */
11661696a8beSPeter Zijlstra static int __sched
11671696a8beSPeter Zijlstra __rt_mutex_slowlock(struct rt_mutex *lock, int state,
11681696a8beSPeter Zijlstra 		    struct hrtimer_sleeper *timeout,
11691696a8beSPeter Zijlstra 		    struct rt_mutex_waiter *waiter)
11701696a8beSPeter Zijlstra {
11711696a8beSPeter Zijlstra 	int ret = 0;
11721696a8beSPeter Zijlstra 
11731696a8beSPeter Zijlstra 	for (;;) {
11741696a8beSPeter Zijlstra 		/* Try to acquire the lock: */
11751696a8beSPeter Zijlstra 		if (try_to_take_rt_mutex(lock, current, waiter))
11761696a8beSPeter Zijlstra 			break;
11771696a8beSPeter Zijlstra 
11781696a8beSPeter Zijlstra 		/*
11791696a8beSPeter Zijlstra 		 * TASK_INTERRUPTIBLE checks for signals and
11801696a8beSPeter Zijlstra 		 * timeout. Ignored otherwise.
11811696a8beSPeter Zijlstra 		 */
11824009f4b3SSteven Rostedt (VMware) 		if (likely(state == TASK_INTERRUPTIBLE)) {
11831696a8beSPeter Zijlstra 			/* Signal pending? */
11841696a8beSPeter Zijlstra 			if (signal_pending(current))
11851696a8beSPeter Zijlstra 				ret = -EINTR;
11861696a8beSPeter Zijlstra 			if (timeout && !timeout->task)
11871696a8beSPeter Zijlstra 				ret = -ETIMEDOUT;
11881696a8beSPeter Zijlstra 			if (ret)
11891696a8beSPeter Zijlstra 				break;
11901696a8beSPeter Zijlstra 		}
11911696a8beSPeter Zijlstra 
1192b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
11931696a8beSPeter Zijlstra 
11941696a8beSPeter Zijlstra 		debug_rt_mutex_print_deadlock(waiter);
11951696a8beSPeter Zijlstra 
11961b0b7c17SDavidlohr Bueso 		schedule();
11971696a8beSPeter Zijlstra 
1198b4abf910SThomas Gleixner 		raw_spin_lock_irq(&lock->wait_lock);
11991696a8beSPeter Zijlstra 		set_current_state(state);
12001696a8beSPeter Zijlstra 	}
12011696a8beSPeter Zijlstra 
1202afffc6c1SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
12031696a8beSPeter Zijlstra 	return ret;
12041696a8beSPeter Zijlstra }
12051696a8beSPeter Zijlstra 
12063d5c9340SThomas Gleixner static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
12073d5c9340SThomas Gleixner 				     struct rt_mutex_waiter *w)
12083d5c9340SThomas Gleixner {
12093d5c9340SThomas Gleixner 	/*
12103d5c9340SThomas Gleixner 	 * If the result is not -EDEADLOCK or the caller requested
12113d5c9340SThomas Gleixner 	 * deadlock detection, nothing to do here.
12123d5c9340SThomas Gleixner 	 */
12133d5c9340SThomas Gleixner 	if (res != -EDEADLOCK || detect_deadlock)
12143d5c9340SThomas Gleixner 		return;
12153d5c9340SThomas Gleixner 
12163d5c9340SThomas Gleixner 	/*
12173d5c9340SThomas Gleixner 	 * Yell lowdly and stop the task right here.
12183d5c9340SThomas Gleixner 	 */
12193d5c9340SThomas Gleixner 	rt_mutex_print_deadlock(w);
12203d5c9340SThomas Gleixner 	while (1) {
12213d5c9340SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
12223d5c9340SThomas Gleixner 		schedule();
12233d5c9340SThomas Gleixner 	}
12243d5c9340SThomas Gleixner }
12253d5c9340SThomas Gleixner 
12261696a8beSPeter Zijlstra /*
12271696a8beSPeter Zijlstra  * Slow path lock function:
12281696a8beSPeter Zijlstra  */
12291696a8beSPeter Zijlstra static int __sched
12301696a8beSPeter Zijlstra rt_mutex_slowlock(struct rt_mutex *lock, int state,
12311696a8beSPeter Zijlstra 		  struct hrtimer_sleeper *timeout,
12328930ed80SThomas Gleixner 		  enum rtmutex_chainwalk chwalk)
12331696a8beSPeter Zijlstra {
12341696a8beSPeter Zijlstra 	struct rt_mutex_waiter waiter;
1235b4abf910SThomas Gleixner 	unsigned long flags;
12361696a8beSPeter Zijlstra 	int ret = 0;
12371696a8beSPeter Zijlstra 
123850809358SPeter Zijlstra 	rt_mutex_init_waiter(&waiter);
12391696a8beSPeter Zijlstra 
1240b4abf910SThomas Gleixner 	/*
1241b4abf910SThomas Gleixner 	 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1242b4abf910SThomas Gleixner 	 * be called in early boot if the cmpxchg() fast path is disabled
1243b4abf910SThomas Gleixner 	 * (debug, no architecture support). In this case we will acquire the
1244b4abf910SThomas Gleixner 	 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1245b4abf910SThomas Gleixner 	 * enable interrupts in that early boot case. So we need to use the
1246b4abf910SThomas Gleixner 	 * irqsave/restore variants.
1247b4abf910SThomas Gleixner 	 */
1248b4abf910SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
12491696a8beSPeter Zijlstra 
12501696a8beSPeter Zijlstra 	/* Try to acquire the lock again: */
12511696a8beSPeter Zijlstra 	if (try_to_take_rt_mutex(lock, current, NULL)) {
1252b4abf910SThomas Gleixner 		raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
12531696a8beSPeter Zijlstra 		return 0;
12541696a8beSPeter Zijlstra 	}
12551696a8beSPeter Zijlstra 
12561696a8beSPeter Zijlstra 	set_current_state(state);
12571696a8beSPeter Zijlstra 
12581696a8beSPeter Zijlstra 	/* Setup the timer, when timeout != NULL */
1259ccdd92c1SThomas Gleixner 	if (unlikely(timeout))
12601696a8beSPeter Zijlstra 		hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
12611696a8beSPeter Zijlstra 
12628930ed80SThomas Gleixner 	ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
12631696a8beSPeter Zijlstra 
12641696a8beSPeter Zijlstra 	if (likely(!ret))
1265afffc6c1SDavidlohr Bueso 		/* sleep on the mutex */
12661696a8beSPeter Zijlstra 		ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
12671696a8beSPeter Zijlstra 
12683d5c9340SThomas Gleixner 	if (unlikely(ret)) {
12699d3e2d02SSebastian Andrzej Siewior 		__set_current_state(TASK_RUNNING);
12701696a8beSPeter Zijlstra 		remove_waiter(lock, &waiter);
12718930ed80SThomas Gleixner 		rt_mutex_handle_deadlock(ret, chwalk, &waiter);
12723d5c9340SThomas Gleixner 	}
12731696a8beSPeter Zijlstra 
12741696a8beSPeter Zijlstra 	/*
12751696a8beSPeter Zijlstra 	 * try_to_take_rt_mutex() sets the waiter bit
12761696a8beSPeter Zijlstra 	 * unconditionally. We might have to fix that up.
12771696a8beSPeter Zijlstra 	 */
12781696a8beSPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
12791696a8beSPeter Zijlstra 
1280b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
12811696a8beSPeter Zijlstra 
12821696a8beSPeter Zijlstra 	/* Remove pending timer: */
12831696a8beSPeter Zijlstra 	if (unlikely(timeout))
12841696a8beSPeter Zijlstra 		hrtimer_cancel(&timeout->timer);
12851696a8beSPeter Zijlstra 
12861696a8beSPeter Zijlstra 	debug_rt_mutex_free_waiter(&waiter);
12871696a8beSPeter Zijlstra 
12881696a8beSPeter Zijlstra 	return ret;
12891696a8beSPeter Zijlstra }
12901696a8beSPeter Zijlstra 
1291c1e2f0eaSPeter Zijlstra static inline int __rt_mutex_slowtrylock(struct rt_mutex *lock)
1292c1e2f0eaSPeter Zijlstra {
1293c1e2f0eaSPeter Zijlstra 	int ret = try_to_take_rt_mutex(lock, current, NULL);
1294c1e2f0eaSPeter Zijlstra 
1295c1e2f0eaSPeter Zijlstra 	/*
1296c1e2f0eaSPeter Zijlstra 	 * try_to_take_rt_mutex() sets the lock waiters bit
1297c1e2f0eaSPeter Zijlstra 	 * unconditionally. Clean this up.
1298c1e2f0eaSPeter Zijlstra 	 */
1299c1e2f0eaSPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
1300c1e2f0eaSPeter Zijlstra 
1301c1e2f0eaSPeter Zijlstra 	return ret;
1302c1e2f0eaSPeter Zijlstra }
1303c1e2f0eaSPeter Zijlstra 
13041696a8beSPeter Zijlstra /*
13051696a8beSPeter Zijlstra  * Slow path try-lock function:
13061696a8beSPeter Zijlstra  */
130788f2b4c1SThomas Gleixner static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
13081696a8beSPeter Zijlstra {
1309b4abf910SThomas Gleixner 	unsigned long flags;
131088f2b4c1SThomas Gleixner 	int ret;
13111696a8beSPeter Zijlstra 
131288f2b4c1SThomas Gleixner 	/*
131388f2b4c1SThomas Gleixner 	 * If the lock already has an owner we fail to get the lock.
131488f2b4c1SThomas Gleixner 	 * This can be done without taking the @lock->wait_lock as
131588f2b4c1SThomas Gleixner 	 * it is only being read, and this is a trylock anyway.
131688f2b4c1SThomas Gleixner 	 */
131788f2b4c1SThomas Gleixner 	if (rt_mutex_owner(lock))
131888f2b4c1SThomas Gleixner 		return 0;
131988f2b4c1SThomas Gleixner 
132088f2b4c1SThomas Gleixner 	/*
1321b4abf910SThomas Gleixner 	 * The mutex has currently no owner. Lock the wait lock and try to
1322b4abf910SThomas Gleixner 	 * acquire the lock. We use irqsave here to support early boot calls.
132388f2b4c1SThomas Gleixner 	 */
1324b4abf910SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
13251696a8beSPeter Zijlstra 
1326c1e2f0eaSPeter Zijlstra 	ret = __rt_mutex_slowtrylock(lock);
13271696a8beSPeter Zijlstra 
1328b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
13291696a8beSPeter Zijlstra 
13301696a8beSPeter Zijlstra 	return ret;
13311696a8beSPeter Zijlstra }
13321696a8beSPeter Zijlstra 
13331696a8beSPeter Zijlstra /*
1334802ab58dSSebastian Andrzej Siewior  * Slow path to release a rt-mutex.
1335aa2bfe55SPeter Zijlstra  *
1336aa2bfe55SPeter Zijlstra  * Return whether the current task needs to call rt_mutex_postunlock().
13371696a8beSPeter Zijlstra  */
1338802ab58dSSebastian Andrzej Siewior static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1339802ab58dSSebastian Andrzej Siewior 					struct wake_q_head *wake_q)
13401696a8beSPeter Zijlstra {
1341b4abf910SThomas Gleixner 	unsigned long flags;
1342b4abf910SThomas Gleixner 
1343b4abf910SThomas Gleixner 	/* irqsave required to support early boot calls */
1344b4abf910SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
13451696a8beSPeter Zijlstra 
13461696a8beSPeter Zijlstra 	debug_rt_mutex_unlock(lock);
13471696a8beSPeter Zijlstra 
134827e35715SThomas Gleixner 	/*
134927e35715SThomas Gleixner 	 * We must be careful here if the fast path is enabled. If we
135027e35715SThomas Gleixner 	 * have no waiters queued we cannot set owner to NULL here
135127e35715SThomas Gleixner 	 * because of:
135227e35715SThomas Gleixner 	 *
135327e35715SThomas Gleixner 	 * foo->lock->owner = NULL;
135427e35715SThomas Gleixner 	 *			rtmutex_lock(foo->lock);   <- fast path
135527e35715SThomas Gleixner 	 *			free = atomic_dec_and_test(foo->refcnt);
135627e35715SThomas Gleixner 	 *			rtmutex_unlock(foo->lock); <- fast path
135727e35715SThomas Gleixner 	 *			if (free)
135827e35715SThomas Gleixner 	 *				kfree(foo);
135927e35715SThomas Gleixner 	 * raw_spin_unlock(foo->lock->wait_lock);
136027e35715SThomas Gleixner 	 *
136127e35715SThomas Gleixner 	 * So for the fastpath enabled kernel:
136227e35715SThomas Gleixner 	 *
136327e35715SThomas Gleixner 	 * Nothing can set the waiters bit as long as we hold
136427e35715SThomas Gleixner 	 * lock->wait_lock. So we do the following sequence:
136527e35715SThomas Gleixner 	 *
136627e35715SThomas Gleixner 	 *	owner = rt_mutex_owner(lock);
136727e35715SThomas Gleixner 	 *	clear_rt_mutex_waiters(lock);
136827e35715SThomas Gleixner 	 *	raw_spin_unlock(&lock->wait_lock);
136927e35715SThomas Gleixner 	 *	if (cmpxchg(&lock->owner, owner, 0) == owner)
137027e35715SThomas Gleixner 	 *		return;
137127e35715SThomas Gleixner 	 *	goto retry;
137227e35715SThomas Gleixner 	 *
137327e35715SThomas Gleixner 	 * The fastpath disabled variant is simple as all access to
137427e35715SThomas Gleixner 	 * lock->owner is serialized by lock->wait_lock:
137527e35715SThomas Gleixner 	 *
137627e35715SThomas Gleixner 	 *	lock->owner = NULL;
137727e35715SThomas Gleixner 	 *	raw_spin_unlock(&lock->wait_lock);
137827e35715SThomas Gleixner 	 */
137927e35715SThomas Gleixner 	while (!rt_mutex_has_waiters(lock)) {
138027e35715SThomas Gleixner 		/* Drops lock->wait_lock ! */
1381b4abf910SThomas Gleixner 		if (unlock_rt_mutex_safe(lock, flags) == true)
1382802ab58dSSebastian Andrzej Siewior 			return false;
138327e35715SThomas Gleixner 		/* Relock the rtmutex and try again */
1384b4abf910SThomas Gleixner 		raw_spin_lock_irqsave(&lock->wait_lock, flags);
13851696a8beSPeter Zijlstra 	}
13861696a8beSPeter Zijlstra 
138727e35715SThomas Gleixner 	/*
138827e35715SThomas Gleixner 	 * The wakeup next waiter path does not suffer from the above
138927e35715SThomas Gleixner 	 * race. See the comments there.
139045ab4effSDavidlohr Bueso 	 *
139145ab4effSDavidlohr Bueso 	 * Queue the next waiter for wakeup once we release the wait_lock.
139227e35715SThomas Gleixner 	 */
1393802ab58dSSebastian Andrzej Siewior 	mark_wakeup_next_waiter(wake_q, lock);
1394b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
13951696a8beSPeter Zijlstra 
1396aa2bfe55SPeter Zijlstra 	return true; /* call rt_mutex_postunlock() */
13971696a8beSPeter Zijlstra }
13981696a8beSPeter Zijlstra 
13991696a8beSPeter Zijlstra /*
14001696a8beSPeter Zijlstra  * debug aware fast / slowpath lock,trylock,unlock
14011696a8beSPeter Zijlstra  *
14021696a8beSPeter Zijlstra  * The atomic acquire/release ops are compiled away, when either the
14031696a8beSPeter Zijlstra  * architecture does not support cmpxchg or when debugging is enabled.
14041696a8beSPeter Zijlstra  */
14051696a8beSPeter Zijlstra static inline int
14061696a8beSPeter Zijlstra rt_mutex_fastlock(struct rt_mutex *lock, int state,
14071696a8beSPeter Zijlstra 		  int (*slowfn)(struct rt_mutex *lock, int state,
14081696a8beSPeter Zijlstra 				struct hrtimer_sleeper *timeout,
14098930ed80SThomas Gleixner 				enum rtmutex_chainwalk chwalk))
14101696a8beSPeter Zijlstra {
1411fffa954fSPeter Zijlstra 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
14121696a8beSPeter Zijlstra 		return 0;
1413fffa954fSPeter Zijlstra 
14148930ed80SThomas Gleixner 	return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
14151696a8beSPeter Zijlstra }
14161696a8beSPeter Zijlstra 
14171696a8beSPeter Zijlstra static inline int
14181696a8beSPeter Zijlstra rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
14198930ed80SThomas Gleixner 			struct hrtimer_sleeper *timeout,
14208930ed80SThomas Gleixner 			enum rtmutex_chainwalk chwalk,
14211696a8beSPeter Zijlstra 			int (*slowfn)(struct rt_mutex *lock, int state,
14221696a8beSPeter Zijlstra 				      struct hrtimer_sleeper *timeout,
14238930ed80SThomas Gleixner 				      enum rtmutex_chainwalk chwalk))
14241696a8beSPeter Zijlstra {
14258930ed80SThomas Gleixner 	if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1426fffa954fSPeter Zijlstra 	    likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
14271696a8beSPeter Zijlstra 		return 0;
1428fffa954fSPeter Zijlstra 
14298930ed80SThomas Gleixner 	return slowfn(lock, state, timeout, chwalk);
14301696a8beSPeter Zijlstra }
14311696a8beSPeter Zijlstra 
14321696a8beSPeter Zijlstra static inline int
14331696a8beSPeter Zijlstra rt_mutex_fasttrylock(struct rt_mutex *lock,
14341696a8beSPeter Zijlstra 		     int (*slowfn)(struct rt_mutex *lock))
14351696a8beSPeter Zijlstra {
1436fffa954fSPeter Zijlstra 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
14371696a8beSPeter Zijlstra 		return 1;
1438fffa954fSPeter Zijlstra 
14391696a8beSPeter Zijlstra 	return slowfn(lock);
14401696a8beSPeter Zijlstra }
14411696a8beSPeter Zijlstra 
14422a1c6029SXunlei Pang /*
1443aa2bfe55SPeter Zijlstra  * Performs the wakeup of the the top-waiter and re-enables preemption.
14442a1c6029SXunlei Pang  */
1445aa2bfe55SPeter Zijlstra void rt_mutex_postunlock(struct wake_q_head *wake_q)
14462a1c6029SXunlei Pang {
14472a1c6029SXunlei Pang 	wake_up_q(wake_q);
14482a1c6029SXunlei Pang 
14492a1c6029SXunlei Pang 	/* Pairs with preempt_disable() in rt_mutex_slowunlock() */
14502a1c6029SXunlei Pang 	preempt_enable();
14512a1c6029SXunlei Pang }
14522a1c6029SXunlei Pang 
14531696a8beSPeter Zijlstra static inline void
14541696a8beSPeter Zijlstra rt_mutex_fastunlock(struct rt_mutex *lock,
1455802ab58dSSebastian Andrzej Siewior 		    bool (*slowfn)(struct rt_mutex *lock,
1456802ab58dSSebastian Andrzej Siewior 				   struct wake_q_head *wqh))
14571696a8beSPeter Zijlstra {
1458194a6b5bSWaiman Long 	DEFINE_WAKE_Q(wake_q);
1459802ab58dSSebastian Andrzej Siewior 
1460fffa954fSPeter Zijlstra 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1461fffa954fSPeter Zijlstra 		return;
1462802ab58dSSebastian Andrzej Siewior 
1463aa2bfe55SPeter Zijlstra 	if (slowfn(lock, &wake_q))
1464aa2bfe55SPeter Zijlstra 		rt_mutex_postunlock(&wake_q);
1465802ab58dSSebastian Andrzej Siewior }
14661696a8beSPeter Zijlstra 
146762cedf3eSPeter Rosin static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
146862cedf3eSPeter Rosin {
146962cedf3eSPeter Rosin 	might_sleep();
147062cedf3eSPeter Rosin 
147162cedf3eSPeter Rosin 	mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
147262cedf3eSPeter Rosin 	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
147362cedf3eSPeter Rosin }
147462cedf3eSPeter Rosin 
147562cedf3eSPeter Rosin #ifdef CONFIG_DEBUG_LOCK_ALLOC
147662cedf3eSPeter Rosin /**
147762cedf3eSPeter Rosin  * rt_mutex_lock_nested - lock a rt_mutex
147862cedf3eSPeter Rosin  *
147962cedf3eSPeter Rosin  * @lock: the rt_mutex to be locked
148062cedf3eSPeter Rosin  * @subclass: the lockdep subclass
148162cedf3eSPeter Rosin  */
148262cedf3eSPeter Rosin void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
148362cedf3eSPeter Rosin {
148462cedf3eSPeter Rosin 	__rt_mutex_lock(lock, subclass);
148562cedf3eSPeter Rosin }
148662cedf3eSPeter Rosin EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
148762cedf3eSPeter Rosin 
148884818af2SSteven Rostedt (VMware) #else /* !CONFIG_DEBUG_LOCK_ALLOC */
148984818af2SSteven Rostedt (VMware) 
14901696a8beSPeter Zijlstra /**
14911696a8beSPeter Zijlstra  * rt_mutex_lock - lock a rt_mutex
14921696a8beSPeter Zijlstra  *
14931696a8beSPeter Zijlstra  * @lock: the rt_mutex to be locked
14941696a8beSPeter Zijlstra  */
14951696a8beSPeter Zijlstra void __sched rt_mutex_lock(struct rt_mutex *lock)
14961696a8beSPeter Zijlstra {
149762cedf3eSPeter Rosin 	__rt_mutex_lock(lock, 0);
14981696a8beSPeter Zijlstra }
14991696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_lock);
150062cedf3eSPeter Rosin #endif
15011696a8beSPeter Zijlstra 
15021696a8beSPeter Zijlstra /**
15031696a8beSPeter Zijlstra  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
15041696a8beSPeter Zijlstra  *
15051696a8beSPeter Zijlstra  * @lock:		the rt_mutex to be locked
15061696a8beSPeter Zijlstra  *
15071696a8beSPeter Zijlstra  * Returns:
15081696a8beSPeter Zijlstra  *  0		on success
15091696a8beSPeter Zijlstra  * -EINTR	when interrupted by a signal
15101696a8beSPeter Zijlstra  */
1511c051b21fSThomas Gleixner int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
15121696a8beSPeter Zijlstra {
1513f5694788SPeter Zijlstra 	int ret;
1514f5694788SPeter Zijlstra 
15151696a8beSPeter Zijlstra 	might_sleep();
15161696a8beSPeter Zijlstra 
1517f5694788SPeter Zijlstra 	mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1518f5694788SPeter Zijlstra 	ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1519f5694788SPeter Zijlstra 	if (ret)
1520f5694788SPeter Zijlstra 		mutex_release(&lock->dep_map, 1, _RET_IP_);
1521f5694788SPeter Zijlstra 
1522f5694788SPeter Zijlstra 	return ret;
15231696a8beSPeter Zijlstra }
15241696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
15251696a8beSPeter Zijlstra 
1526c051b21fSThomas Gleixner /*
15275293c2efSPeter Zijlstra  * Futex variant, must not use fastpath.
15285293c2efSPeter Zijlstra  */
15295293c2efSPeter Zijlstra int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
15305293c2efSPeter Zijlstra {
15315293c2efSPeter Zijlstra 	return rt_mutex_slowtrylock(lock);
1532c051b21fSThomas Gleixner }
1533c051b21fSThomas Gleixner 
1534c1e2f0eaSPeter Zijlstra int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
1535c1e2f0eaSPeter Zijlstra {
1536c1e2f0eaSPeter Zijlstra 	return __rt_mutex_slowtrylock(lock);
1537c1e2f0eaSPeter Zijlstra }
1538c1e2f0eaSPeter Zijlstra 
15391696a8beSPeter Zijlstra /**
15401696a8beSPeter Zijlstra  * rt_mutex_timed_lock - lock a rt_mutex interruptible
15411696a8beSPeter Zijlstra  *			the timeout structure is provided
15421696a8beSPeter Zijlstra  *			by the caller
15431696a8beSPeter Zijlstra  *
15441696a8beSPeter Zijlstra  * @lock:		the rt_mutex to be locked
15451696a8beSPeter Zijlstra  * @timeout:		timeout structure or NULL (no timeout)
15461696a8beSPeter Zijlstra  *
15471696a8beSPeter Zijlstra  * Returns:
15481696a8beSPeter Zijlstra  *  0		on success
15491696a8beSPeter Zijlstra  * -EINTR	when interrupted by a signal
15501696a8beSPeter Zijlstra  * -ETIMEDOUT	when the timeout expired
15511696a8beSPeter Zijlstra  */
15521696a8beSPeter Zijlstra int
1553c051b21fSThomas Gleixner rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
15541696a8beSPeter Zijlstra {
1555f5694788SPeter Zijlstra 	int ret;
1556f5694788SPeter Zijlstra 
15571696a8beSPeter Zijlstra 	might_sleep();
15581696a8beSPeter Zijlstra 
1559f5694788SPeter Zijlstra 	mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1560f5694788SPeter Zijlstra 	ret = rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
15618930ed80SThomas Gleixner 				       RT_MUTEX_MIN_CHAINWALK,
1562c051b21fSThomas Gleixner 				       rt_mutex_slowlock);
1563f5694788SPeter Zijlstra 	if (ret)
1564f5694788SPeter Zijlstra 		mutex_release(&lock->dep_map, 1, _RET_IP_);
1565f5694788SPeter Zijlstra 
1566f5694788SPeter Zijlstra 	return ret;
15671696a8beSPeter Zijlstra }
15681696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
15691696a8beSPeter Zijlstra 
15701696a8beSPeter Zijlstra /**
15711696a8beSPeter Zijlstra  * rt_mutex_trylock - try to lock a rt_mutex
15721696a8beSPeter Zijlstra  *
15731696a8beSPeter Zijlstra  * @lock:	the rt_mutex to be locked
15741696a8beSPeter Zijlstra  *
15756ce47fd9SThomas Gleixner  * This function can only be called in thread context. It's safe to
15766ce47fd9SThomas Gleixner  * call it from atomic regions, but not from hard interrupt or soft
15776ce47fd9SThomas Gleixner  * interrupt context.
15786ce47fd9SThomas Gleixner  *
15791696a8beSPeter Zijlstra  * Returns 1 on success and 0 on contention
15801696a8beSPeter Zijlstra  */
15811696a8beSPeter Zijlstra int __sched rt_mutex_trylock(struct rt_mutex *lock)
15821696a8beSPeter Zijlstra {
1583f5694788SPeter Zijlstra 	int ret;
1584f5694788SPeter Zijlstra 
1585a461d587SSebastian Andrzej Siewior 	if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
15866ce47fd9SThomas Gleixner 		return 0;
15876ce47fd9SThomas Gleixner 
1588f5694788SPeter Zijlstra 	ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1589f5694788SPeter Zijlstra 	if (ret)
1590f5694788SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1591f5694788SPeter Zijlstra 
1592f5694788SPeter Zijlstra 	return ret;
15931696a8beSPeter Zijlstra }
15941696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_trylock);
15951696a8beSPeter Zijlstra 
15961696a8beSPeter Zijlstra /**
15971696a8beSPeter Zijlstra  * rt_mutex_unlock - unlock a rt_mutex
15981696a8beSPeter Zijlstra  *
15991696a8beSPeter Zijlstra  * @lock: the rt_mutex to be unlocked
16001696a8beSPeter Zijlstra  */
16011696a8beSPeter Zijlstra void __sched rt_mutex_unlock(struct rt_mutex *lock)
16021696a8beSPeter Zijlstra {
1603f5694788SPeter Zijlstra 	mutex_release(&lock->dep_map, 1, _RET_IP_);
16041696a8beSPeter Zijlstra 	rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
16051696a8beSPeter Zijlstra }
16061696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_unlock);
16071696a8beSPeter Zijlstra 
16081696a8beSPeter Zijlstra /**
16095293c2efSPeter Zijlstra  * Futex variant, that since futex variants do not use the fast-path, can be
16105293c2efSPeter Zijlstra  * simple and will not need to retry.
1611802ab58dSSebastian Andrzej Siewior  */
16125293c2efSPeter Zijlstra bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
16135293c2efSPeter Zijlstra 				    struct wake_q_head *wake_q)
1614802ab58dSSebastian Andrzej Siewior {
16155293c2efSPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1616fffa954fSPeter Zijlstra 
16175293c2efSPeter Zijlstra 	debug_rt_mutex_unlock(lock);
16185293c2efSPeter Zijlstra 
16195293c2efSPeter Zijlstra 	if (!rt_mutex_has_waiters(lock)) {
16205293c2efSPeter Zijlstra 		lock->owner = NULL;
16215293c2efSPeter Zijlstra 		return false; /* done */
16225293c2efSPeter Zijlstra 	}
16235293c2efSPeter Zijlstra 
16242a1c6029SXunlei Pang 	/*
1625def34eaaSMike Galbraith 	 * We've already deboosted, mark_wakeup_next_waiter() will
1626def34eaaSMike Galbraith 	 * retain preempt_disabled when we drop the wait_lock, to
1627def34eaaSMike Galbraith 	 * avoid inversion prior to the wakeup.  preempt_disable()
1628def34eaaSMike Galbraith 	 * therein pairs with rt_mutex_postunlock().
16292a1c6029SXunlei Pang 	 */
1630def34eaaSMike Galbraith 	mark_wakeup_next_waiter(wake_q, lock);
16312a1c6029SXunlei Pang 
1632aa2bfe55SPeter Zijlstra 	return true; /* call postunlock() */
16335293c2efSPeter Zijlstra }
16345293c2efSPeter Zijlstra 
16355293c2efSPeter Zijlstra void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
16365293c2efSPeter Zijlstra {
16375293c2efSPeter Zijlstra 	DEFINE_WAKE_Q(wake_q);
16386b0ef92fSBoqun Feng 	unsigned long flags;
1639aa2bfe55SPeter Zijlstra 	bool postunlock;
16405293c2efSPeter Zijlstra 
16416b0ef92fSBoqun Feng 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1642aa2bfe55SPeter Zijlstra 	postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
16436b0ef92fSBoqun Feng 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
16445293c2efSPeter Zijlstra 
1645aa2bfe55SPeter Zijlstra 	if (postunlock)
1646aa2bfe55SPeter Zijlstra 		rt_mutex_postunlock(&wake_q);
1647802ab58dSSebastian Andrzej Siewior }
1648802ab58dSSebastian Andrzej Siewior 
1649802ab58dSSebastian Andrzej Siewior /**
16501696a8beSPeter Zijlstra  * rt_mutex_destroy - mark a mutex unusable
16511696a8beSPeter Zijlstra  * @lock: the mutex to be destroyed
16521696a8beSPeter Zijlstra  *
16531696a8beSPeter Zijlstra  * This function marks the mutex uninitialized, and any subsequent
16541696a8beSPeter Zijlstra  * use of the mutex is forbidden. The mutex must not be locked when
16551696a8beSPeter Zijlstra  * this function is called.
16561696a8beSPeter Zijlstra  */
16571696a8beSPeter Zijlstra void rt_mutex_destroy(struct rt_mutex *lock)
16581696a8beSPeter Zijlstra {
16591696a8beSPeter Zijlstra 	WARN_ON(rt_mutex_is_locked(lock));
16601696a8beSPeter Zijlstra #ifdef CONFIG_DEBUG_RT_MUTEXES
16611696a8beSPeter Zijlstra 	lock->magic = NULL;
16621696a8beSPeter Zijlstra #endif
16631696a8beSPeter Zijlstra }
16641696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_destroy);
16651696a8beSPeter Zijlstra 
16661696a8beSPeter Zijlstra /**
16671696a8beSPeter Zijlstra  * __rt_mutex_init - initialize the rt lock
16681696a8beSPeter Zijlstra  *
16691696a8beSPeter Zijlstra  * @lock: the rt lock to be initialized
16701696a8beSPeter Zijlstra  *
16711696a8beSPeter Zijlstra  * Initialize the rt lock to unlocked state.
16721696a8beSPeter Zijlstra  *
16731696a8beSPeter Zijlstra  * Initializing of a locked rt lock is not allowed
16741696a8beSPeter Zijlstra  */
1675f5694788SPeter Zijlstra void __rt_mutex_init(struct rt_mutex *lock, const char *name,
1676f5694788SPeter Zijlstra 		     struct lock_class_key *key)
16771696a8beSPeter Zijlstra {
16781696a8beSPeter Zijlstra 	lock->owner = NULL;
16791696a8beSPeter Zijlstra 	raw_spin_lock_init(&lock->wait_lock);
1680a23ba907SDavidlohr Bueso 	lock->waiters = RB_ROOT_CACHED;
16811696a8beSPeter Zijlstra 
1682cde50a67SLevin, Alexander (Sasha Levin) 	if (name && key)
1683f5694788SPeter Zijlstra 		debug_rt_mutex_init(lock, name, key);
16841696a8beSPeter Zijlstra }
16851696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(__rt_mutex_init);
16861696a8beSPeter Zijlstra 
16871696a8beSPeter Zijlstra /**
16881696a8beSPeter Zijlstra  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
16891696a8beSPeter Zijlstra  *				proxy owner
16901696a8beSPeter Zijlstra  *
16911696a8beSPeter Zijlstra  * @lock:	the rt_mutex to be locked
16921696a8beSPeter Zijlstra  * @proxy_owner:the task to set as owner
16931696a8beSPeter Zijlstra  *
16941696a8beSPeter Zijlstra  * No locking. Caller has to do serializing itself
169584d82ec5SThomas Gleixner  *
169684d82ec5SThomas Gleixner  * Special API call for PI-futex support. This initializes the rtmutex and
169784d82ec5SThomas Gleixner  * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
169884d82ec5SThomas Gleixner  * possible at this point because the pi_state which contains the rtmutex
169984d82ec5SThomas Gleixner  * is not yet visible to other tasks.
17001696a8beSPeter Zijlstra  */
17011696a8beSPeter Zijlstra void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
17021696a8beSPeter Zijlstra 				struct task_struct *proxy_owner)
17031696a8beSPeter Zijlstra {
1704f5694788SPeter Zijlstra 	__rt_mutex_init(lock, NULL, NULL);
17051696a8beSPeter Zijlstra 	debug_rt_mutex_proxy_lock(lock, proxy_owner);
17061696a8beSPeter Zijlstra 	rt_mutex_set_owner(lock, proxy_owner);
17071696a8beSPeter Zijlstra }
17081696a8beSPeter Zijlstra 
17091696a8beSPeter Zijlstra /**
17101696a8beSPeter Zijlstra  * rt_mutex_proxy_unlock - release a lock on behalf of owner
17111696a8beSPeter Zijlstra  *
17121696a8beSPeter Zijlstra  * @lock:	the rt_mutex to be locked
17131696a8beSPeter Zijlstra  *
17141696a8beSPeter Zijlstra  * No locking. Caller has to do serializing itself
171584d82ec5SThomas Gleixner  *
171684d82ec5SThomas Gleixner  * Special API call for PI-futex support. This merrily cleans up the rtmutex
171784d82ec5SThomas Gleixner  * (debugging) state. Concurrent operations on this rt_mutex are not
171884d82ec5SThomas Gleixner  * possible because it belongs to the pi_state which is about to be freed
171984d82ec5SThomas Gleixner  * and it is not longer visible to other tasks.
17201696a8beSPeter Zijlstra  */
17211696a8beSPeter Zijlstra void rt_mutex_proxy_unlock(struct rt_mutex *lock,
17221696a8beSPeter Zijlstra 			   struct task_struct *proxy_owner)
17231696a8beSPeter Zijlstra {
17241696a8beSPeter Zijlstra 	debug_rt_mutex_proxy_unlock(lock);
17251696a8beSPeter Zijlstra 	rt_mutex_set_owner(lock, NULL);
17261696a8beSPeter Zijlstra }
17271696a8beSPeter Zijlstra 
17281a1fb985SThomas Gleixner /**
17291a1fb985SThomas Gleixner  * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
17301a1fb985SThomas Gleixner  * @lock:		the rt_mutex to take
17311a1fb985SThomas Gleixner  * @waiter:		the pre-initialized rt_mutex_waiter
17321a1fb985SThomas Gleixner  * @task:		the task to prepare
17331a1fb985SThomas Gleixner  *
17341a1fb985SThomas Gleixner  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
17351a1fb985SThomas Gleixner  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
17361a1fb985SThomas Gleixner  *
17371a1fb985SThomas Gleixner  * NOTE: does _NOT_ remove the @waiter on failure; must either call
17381a1fb985SThomas Gleixner  * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
17391a1fb985SThomas Gleixner  *
17401a1fb985SThomas Gleixner  * Returns:
17411a1fb985SThomas Gleixner  *  0 - task blocked on lock
17421a1fb985SThomas Gleixner  *  1 - acquired the lock for task, caller should wake it up
17431a1fb985SThomas Gleixner  * <0 - error
17441a1fb985SThomas Gleixner  *
17451a1fb985SThomas Gleixner  * Special API call for PI-futex support.
17461a1fb985SThomas Gleixner  */
174756222b21SPeter Zijlstra int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
174856222b21SPeter Zijlstra 			      struct rt_mutex_waiter *waiter,
174956222b21SPeter Zijlstra 			      struct task_struct *task)
175056222b21SPeter Zijlstra {
175156222b21SPeter Zijlstra 	int ret;
175256222b21SPeter Zijlstra 
17531a1fb985SThomas Gleixner 	lockdep_assert_held(&lock->wait_lock);
17541a1fb985SThomas Gleixner 
175556222b21SPeter Zijlstra 	if (try_to_take_rt_mutex(lock, task, NULL))
175656222b21SPeter Zijlstra 		return 1;
175756222b21SPeter Zijlstra 
175856222b21SPeter Zijlstra 	/* We enforce deadlock detection for futexes */
175956222b21SPeter Zijlstra 	ret = task_blocks_on_rt_mutex(lock, waiter, task,
176056222b21SPeter Zijlstra 				      RT_MUTEX_FULL_CHAINWALK);
176156222b21SPeter Zijlstra 
176256222b21SPeter Zijlstra 	if (ret && !rt_mutex_owner(lock)) {
176356222b21SPeter Zijlstra 		/*
176456222b21SPeter Zijlstra 		 * Reset the return value. We might have
176556222b21SPeter Zijlstra 		 * returned with -EDEADLK and the owner
176656222b21SPeter Zijlstra 		 * released the lock while we were walking the
176756222b21SPeter Zijlstra 		 * pi chain.  Let the waiter sort it out.
176856222b21SPeter Zijlstra 		 */
176956222b21SPeter Zijlstra 		ret = 0;
177056222b21SPeter Zijlstra 	}
177156222b21SPeter Zijlstra 
177256222b21SPeter Zijlstra 	debug_rt_mutex_print_deadlock(waiter);
177356222b21SPeter Zijlstra 
177456222b21SPeter Zijlstra 	return ret;
177556222b21SPeter Zijlstra }
177656222b21SPeter Zijlstra 
17771696a8beSPeter Zijlstra /**
17781696a8beSPeter Zijlstra  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
17791696a8beSPeter Zijlstra  * @lock:		the rt_mutex to take
17801696a8beSPeter Zijlstra  * @waiter:		the pre-initialized rt_mutex_waiter
17811696a8beSPeter Zijlstra  * @task:		the task to prepare
17821696a8beSPeter Zijlstra  *
17831a1fb985SThomas Gleixner  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
17841a1fb985SThomas Gleixner  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
17851a1fb985SThomas Gleixner  *
17861a1fb985SThomas Gleixner  * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
17871a1fb985SThomas Gleixner  * on failure.
17881a1fb985SThomas Gleixner  *
17891696a8beSPeter Zijlstra  * Returns:
17901696a8beSPeter Zijlstra  *  0 - task blocked on lock
17911696a8beSPeter Zijlstra  *  1 - acquired the lock for task, caller should wake it up
17921696a8beSPeter Zijlstra  * <0 - error
17931696a8beSPeter Zijlstra  *
17941a1fb985SThomas Gleixner  * Special API call for PI-futex support.
17951696a8beSPeter Zijlstra  */
17961696a8beSPeter Zijlstra int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
17971696a8beSPeter Zijlstra 			      struct rt_mutex_waiter *waiter,
1798c051b21fSThomas Gleixner 			      struct task_struct *task)
17991696a8beSPeter Zijlstra {
18001696a8beSPeter Zijlstra 	int ret;
18011696a8beSPeter Zijlstra 
1802b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
180356222b21SPeter Zijlstra 	ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
18041a1fb985SThomas Gleixner 	if (unlikely(ret))
18051a1fb985SThomas Gleixner 		remove_waiter(lock, waiter);
1806b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
18071696a8beSPeter Zijlstra 
18081696a8beSPeter Zijlstra 	return ret;
18091696a8beSPeter Zijlstra }
18101696a8beSPeter Zijlstra 
18111696a8beSPeter Zijlstra /**
18121696a8beSPeter Zijlstra  * rt_mutex_next_owner - return the next owner of the lock
18131696a8beSPeter Zijlstra  *
18141696a8beSPeter Zijlstra  * @lock: the rt lock query
18151696a8beSPeter Zijlstra  *
18161696a8beSPeter Zijlstra  * Returns the next owner of the lock or NULL
18171696a8beSPeter Zijlstra  *
18181696a8beSPeter Zijlstra  * Caller has to serialize against other accessors to the lock
18191696a8beSPeter Zijlstra  * itself.
18201696a8beSPeter Zijlstra  *
18211696a8beSPeter Zijlstra  * Special API call for PI-futex support
18221696a8beSPeter Zijlstra  */
18231696a8beSPeter Zijlstra struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
18241696a8beSPeter Zijlstra {
18251696a8beSPeter Zijlstra 	if (!rt_mutex_has_waiters(lock))
18261696a8beSPeter Zijlstra 		return NULL;
18271696a8beSPeter Zijlstra 
18281696a8beSPeter Zijlstra 	return rt_mutex_top_waiter(lock)->task;
18291696a8beSPeter Zijlstra }
18301696a8beSPeter Zijlstra 
18311696a8beSPeter Zijlstra /**
183238d589f2SPeter Zijlstra  * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
18331696a8beSPeter Zijlstra  * @lock:		the rt_mutex we were woken on
18341696a8beSPeter Zijlstra  * @to:			the timeout, null if none. hrtimer should already have
18351696a8beSPeter Zijlstra  *			been started.
18361696a8beSPeter Zijlstra  * @waiter:		the pre-initialized rt_mutex_waiter
18371696a8beSPeter Zijlstra  *
183838d589f2SPeter Zijlstra  * Wait for the the lock acquisition started on our behalf by
183938d589f2SPeter Zijlstra  * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
184038d589f2SPeter Zijlstra  * rt_mutex_cleanup_proxy_lock().
18411696a8beSPeter Zijlstra  *
18421696a8beSPeter Zijlstra  * Returns:
18431696a8beSPeter Zijlstra  *  0 - success
1844c051b21fSThomas Gleixner  * <0 - error, one of -EINTR, -ETIMEDOUT
18451696a8beSPeter Zijlstra  *
184638d589f2SPeter Zijlstra  * Special API call for PI-futex support
18471696a8beSPeter Zijlstra  */
184838d589f2SPeter Zijlstra int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
18491696a8beSPeter Zijlstra 			       struct hrtimer_sleeper *to,
1850c051b21fSThomas Gleixner 			       struct rt_mutex_waiter *waiter)
18511696a8beSPeter Zijlstra {
18521696a8beSPeter Zijlstra 	int ret;
18531696a8beSPeter Zijlstra 
1854b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
1855afffc6c1SDavidlohr Bueso 	/* sleep on the mutex */
185604dc1b2fSPeter Zijlstra 	set_current_state(TASK_INTERRUPTIBLE);
18571696a8beSPeter Zijlstra 	ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
185804dc1b2fSPeter Zijlstra 	/*
185904dc1b2fSPeter Zijlstra 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
186004dc1b2fSPeter Zijlstra 	 * have to fix that up.
186104dc1b2fSPeter Zijlstra 	 */
186204dc1b2fSPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
1863b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
18641696a8beSPeter Zijlstra 
18651696a8beSPeter Zijlstra 	return ret;
18661696a8beSPeter Zijlstra }
186738d589f2SPeter Zijlstra 
186838d589f2SPeter Zijlstra /**
186938d589f2SPeter Zijlstra  * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
187038d589f2SPeter Zijlstra  * @lock:		the rt_mutex we were woken on
187138d589f2SPeter Zijlstra  * @waiter:		the pre-initialized rt_mutex_waiter
187238d589f2SPeter Zijlstra  *
18731a1fb985SThomas Gleixner  * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
18741a1fb985SThomas Gleixner  * rt_mutex_wait_proxy_lock().
187538d589f2SPeter Zijlstra  *
187638d589f2SPeter Zijlstra  * Unless we acquired the lock; we're still enqueued on the wait-list and can
187738d589f2SPeter Zijlstra  * in fact still be granted ownership until we're removed. Therefore we can
187838d589f2SPeter Zijlstra  * find we are in fact the owner and must disregard the
187938d589f2SPeter Zijlstra  * rt_mutex_wait_proxy_lock() failure.
188038d589f2SPeter Zijlstra  *
188138d589f2SPeter Zijlstra  * Returns:
188238d589f2SPeter Zijlstra  *  true  - did the cleanup, we done.
188338d589f2SPeter Zijlstra  *  false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
188438d589f2SPeter Zijlstra  *          caller should disregards its return value.
188538d589f2SPeter Zijlstra  *
188638d589f2SPeter Zijlstra  * Special API call for PI-futex support
188738d589f2SPeter Zijlstra  */
188838d589f2SPeter Zijlstra bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
188938d589f2SPeter Zijlstra 				 struct rt_mutex_waiter *waiter)
189038d589f2SPeter Zijlstra {
189138d589f2SPeter Zijlstra 	bool cleanup = false;
189238d589f2SPeter Zijlstra 
189338d589f2SPeter Zijlstra 	raw_spin_lock_irq(&lock->wait_lock);
189438d589f2SPeter Zijlstra 	/*
189504dc1b2fSPeter Zijlstra 	 * Do an unconditional try-lock, this deals with the lock stealing
189604dc1b2fSPeter Zijlstra 	 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
189704dc1b2fSPeter Zijlstra 	 * sets a NULL owner.
189804dc1b2fSPeter Zijlstra 	 *
189904dc1b2fSPeter Zijlstra 	 * We're not interested in the return value, because the subsequent
190004dc1b2fSPeter Zijlstra 	 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
190104dc1b2fSPeter Zijlstra 	 * we will own the lock and it will have removed the waiter. If we
190204dc1b2fSPeter Zijlstra 	 * failed the trylock, we're still not owner and we need to remove
190304dc1b2fSPeter Zijlstra 	 * ourselves.
190404dc1b2fSPeter Zijlstra 	 */
190504dc1b2fSPeter Zijlstra 	try_to_take_rt_mutex(lock, current, waiter);
190604dc1b2fSPeter Zijlstra 	/*
190738d589f2SPeter Zijlstra 	 * Unless we're the owner; we're still enqueued on the wait_list.
190838d589f2SPeter Zijlstra 	 * So check if we became owner, if not, take us off the wait_list.
190938d589f2SPeter Zijlstra 	 */
191038d589f2SPeter Zijlstra 	if (rt_mutex_owner(lock) != current) {
191138d589f2SPeter Zijlstra 		remove_waiter(lock, waiter);
191238d589f2SPeter Zijlstra 		cleanup = true;
191338d589f2SPeter Zijlstra 	}
1914cfafcd11SPeter Zijlstra 	/*
1915cfafcd11SPeter Zijlstra 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1916cfafcd11SPeter Zijlstra 	 * have to fix that up.
1917cfafcd11SPeter Zijlstra 	 */
1918cfafcd11SPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
1919cfafcd11SPeter Zijlstra 
192038d589f2SPeter Zijlstra 	raw_spin_unlock_irq(&lock->wait_lock);
192138d589f2SPeter Zijlstra 
192238d589f2SPeter Zijlstra 	return cleanup;
192338d589f2SPeter Zijlstra }
1924