xref: /linux/kernel/locking/rtmutex.c (revision 387b14684f94483cbbb72843db406ec9a8d0d6d2)
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  *
12*387b1468SMauro 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 */
63167792e2cSThomas Gleixner 		task = rt_mutex_owner(lock);
63267792e2cSThomas Gleixner 		get_task_struct(task);
633b4abf910SThomas Gleixner 		raw_spin_lock(&task->pi_lock);
63467792e2cSThomas Gleixner 
63567792e2cSThomas Gleixner 		/*
63667792e2cSThomas Gleixner 		 * No requeue [11] here. We just do deadlock detection.
63767792e2cSThomas Gleixner 		 *
63867792e2cSThomas Gleixner 		 * [12] Store whether owner is blocked
63967792e2cSThomas Gleixner 		 * itself. Decision is made after dropping the locks
64067792e2cSThomas Gleixner 		 */
64167792e2cSThomas Gleixner 		next_lock = task_blocked_on_lock(task);
64267792e2cSThomas Gleixner 		/*
64367792e2cSThomas Gleixner 		 * Get the top waiter for the next iteration
64467792e2cSThomas Gleixner 		 */
64567792e2cSThomas Gleixner 		top_waiter = rt_mutex_top_waiter(lock);
64667792e2cSThomas Gleixner 
64767792e2cSThomas Gleixner 		/* [13] Drop locks */
648b4abf910SThomas Gleixner 		raw_spin_unlock(&task->pi_lock);
649b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
65067792e2cSThomas Gleixner 
65167792e2cSThomas Gleixner 		/* If owner is not blocked, end of chain. */
65267792e2cSThomas Gleixner 		if (!next_lock)
65367792e2cSThomas Gleixner 			goto out_put_task;
65467792e2cSThomas Gleixner 		goto again;
65567792e2cSThomas Gleixner 	}
65667792e2cSThomas Gleixner 
65767792e2cSThomas Gleixner 	/*
658a57594a1SThomas Gleixner 	 * Store the current top waiter before doing the requeue
659a57594a1SThomas Gleixner 	 * operation on @lock. We need it for the boost/deboost
660a57594a1SThomas Gleixner 	 * decision below.
661a57594a1SThomas Gleixner 	 */
662a57594a1SThomas Gleixner 	prerequeue_top_waiter = rt_mutex_top_waiter(lock);
6631696a8beSPeter Zijlstra 
6649f40a51aSDavidlohr Bueso 	/* [7] Requeue the waiter in the lock waiter tree. */
665fb00aca4SPeter Zijlstra 	rt_mutex_dequeue(lock, waiter);
666e0aad5b4SPeter Zijlstra 
667e0aad5b4SPeter Zijlstra 	/*
668e0aad5b4SPeter Zijlstra 	 * Update the waiter prio fields now that we're dequeued.
669e0aad5b4SPeter Zijlstra 	 *
670e0aad5b4SPeter Zijlstra 	 * These values can have changed through either:
671e0aad5b4SPeter Zijlstra 	 *
672e0aad5b4SPeter Zijlstra 	 *   sys_sched_set_scheduler() / sys_sched_setattr()
673e0aad5b4SPeter Zijlstra 	 *
674e0aad5b4SPeter Zijlstra 	 * or
675e0aad5b4SPeter Zijlstra 	 *
676e0aad5b4SPeter Zijlstra 	 *   DL CBS enforcement advancing the effective deadline.
677e0aad5b4SPeter Zijlstra 	 *
678e0aad5b4SPeter Zijlstra 	 * Even though pi_waiters also uses these fields, and that tree is only
679e0aad5b4SPeter Zijlstra 	 * updated in [11], we can do this here, since we hold [L], which
680e0aad5b4SPeter Zijlstra 	 * serializes all pi_waiters access and rb_erase() does not care about
681e0aad5b4SPeter Zijlstra 	 * the values of the node being removed.
682e0aad5b4SPeter Zijlstra 	 */
6832d3d891dSDario Faggioli 	waiter->prio = task->prio;
684e0aad5b4SPeter Zijlstra 	waiter->deadline = task->dl.deadline;
685e0aad5b4SPeter Zijlstra 
686fb00aca4SPeter Zijlstra 	rt_mutex_enqueue(lock, waiter);
6871696a8beSPeter Zijlstra 
6883eb65aeaSThomas Gleixner 	/* [8] Release the task */
689b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
6902ffa5a5cSThomas Gleixner 	put_task_struct(task);
6912ffa5a5cSThomas Gleixner 
692a57594a1SThomas Gleixner 	/*
6933eb65aeaSThomas Gleixner 	 * [9] check_exit_conditions_3 protected by lock->wait_lock.
6943eb65aeaSThomas Gleixner 	 *
695a57594a1SThomas Gleixner 	 * We must abort the chain walk if there is no lock owner even
696a57594a1SThomas Gleixner 	 * in the dead lock detection case, as we have nothing to
697a57594a1SThomas Gleixner 	 * follow here. This is the end of the chain we are walking.
698a57594a1SThomas Gleixner 	 */
6991696a8beSPeter Zijlstra 	if (!rt_mutex_owner(lock)) {
7001696a8beSPeter Zijlstra 		/*
7013eb65aeaSThomas Gleixner 		 * If the requeue [7] above changed the top waiter,
7023eb65aeaSThomas Gleixner 		 * then we need to wake the new top waiter up to try
7033eb65aeaSThomas Gleixner 		 * to get the lock.
7041696a8beSPeter Zijlstra 		 */
705a57594a1SThomas Gleixner 		if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
7061696a8beSPeter Zijlstra 			wake_up_process(rt_mutex_top_waiter(lock)->task);
707b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
7082ffa5a5cSThomas Gleixner 		return 0;
7091696a8beSPeter Zijlstra 	}
7101696a8beSPeter Zijlstra 
7113eb65aeaSThomas Gleixner 	/* [10] Grab the next task, i.e. the owner of @lock */
7121696a8beSPeter Zijlstra 	task = rt_mutex_owner(lock);
7131696a8beSPeter Zijlstra 	get_task_struct(task);
714b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
7151696a8beSPeter Zijlstra 
7163eb65aeaSThomas Gleixner 	/* [11] requeue the pi waiters if necessary */
7171696a8beSPeter Zijlstra 	if (waiter == rt_mutex_top_waiter(lock)) {
718a57594a1SThomas Gleixner 		/*
719a57594a1SThomas Gleixner 		 * The waiter became the new top (highest priority)
720a57594a1SThomas Gleixner 		 * waiter on the lock. Replace the previous top waiter
7219f40a51aSDavidlohr Bueso 		 * in the owner tasks pi waiters tree with this waiter
722a57594a1SThomas Gleixner 		 * and adjust the priority of the owner.
723a57594a1SThomas Gleixner 		 */
724a57594a1SThomas Gleixner 		rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
725fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(task, waiter);
726acd58620SPeter Zijlstra 		rt_mutex_adjust_prio(task);
7271696a8beSPeter Zijlstra 
728a57594a1SThomas Gleixner 	} else if (prerequeue_top_waiter == waiter) {
729a57594a1SThomas Gleixner 		/*
730a57594a1SThomas Gleixner 		 * The waiter was the top waiter on the lock, but is
731a57594a1SThomas Gleixner 		 * no longer the top prority waiter. Replace waiter in
7329f40a51aSDavidlohr Bueso 		 * the owner tasks pi waiters tree with the new top
733a57594a1SThomas Gleixner 		 * (highest priority) waiter and adjust the priority
734a57594a1SThomas Gleixner 		 * of the owner.
735a57594a1SThomas Gleixner 		 * The new top waiter is stored in @waiter so that
736a57594a1SThomas Gleixner 		 * @waiter == @top_waiter evaluates to true below and
737a57594a1SThomas Gleixner 		 * we continue to deboost the rest of the chain.
738a57594a1SThomas Gleixner 		 */
739fb00aca4SPeter Zijlstra 		rt_mutex_dequeue_pi(task, waiter);
7401696a8beSPeter Zijlstra 		waiter = rt_mutex_top_waiter(lock);
741fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(task, waiter);
742acd58620SPeter Zijlstra 		rt_mutex_adjust_prio(task);
743a57594a1SThomas Gleixner 	} else {
744a57594a1SThomas Gleixner 		/*
745a57594a1SThomas Gleixner 		 * Nothing changed. No need to do any priority
746a57594a1SThomas Gleixner 		 * adjustment.
747a57594a1SThomas Gleixner 		 */
7481696a8beSPeter Zijlstra 	}
7491696a8beSPeter Zijlstra 
75082084984SThomas Gleixner 	/*
7513eb65aeaSThomas Gleixner 	 * [12] check_exit_conditions_4() protected by task->pi_lock
7523eb65aeaSThomas Gleixner 	 * and lock->wait_lock. The actual decisions are made after we
7533eb65aeaSThomas Gleixner 	 * dropped the locks.
7543eb65aeaSThomas Gleixner 	 *
75582084984SThomas Gleixner 	 * Check whether the task which owns the current lock is pi
75682084984SThomas Gleixner 	 * blocked itself. If yes we store a pointer to the lock for
75782084984SThomas Gleixner 	 * the lock chain change detection above. After we dropped
75882084984SThomas Gleixner 	 * task->pi_lock next_lock cannot be dereferenced anymore.
75982084984SThomas Gleixner 	 */
76082084984SThomas Gleixner 	next_lock = task_blocked_on_lock(task);
761a57594a1SThomas Gleixner 	/*
762a57594a1SThomas Gleixner 	 * Store the top waiter of @lock for the end of chain walk
763a57594a1SThomas Gleixner 	 * decision below.
764a57594a1SThomas Gleixner 	 */
7651696a8beSPeter Zijlstra 	top_waiter = rt_mutex_top_waiter(lock);
7663eb65aeaSThomas Gleixner 
7673eb65aeaSThomas Gleixner 	/* [13] Drop the locks */
768b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
769b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
7701696a8beSPeter Zijlstra 
77182084984SThomas Gleixner 	/*
7723eb65aeaSThomas Gleixner 	 * Make the actual exit decisions [12], based on the stored
7733eb65aeaSThomas Gleixner 	 * values.
7743eb65aeaSThomas Gleixner 	 *
77582084984SThomas Gleixner 	 * We reached the end of the lock chain. Stop right here. No
77682084984SThomas Gleixner 	 * point to go back just to figure that out.
77782084984SThomas Gleixner 	 */
77882084984SThomas Gleixner 	if (!next_lock)
77982084984SThomas Gleixner 		goto out_put_task;
78082084984SThomas Gleixner 
781a57594a1SThomas Gleixner 	/*
782a57594a1SThomas Gleixner 	 * If the current waiter is not the top waiter on the lock,
783a57594a1SThomas Gleixner 	 * then we can stop the chain walk here if we are not in full
784a57594a1SThomas Gleixner 	 * deadlock detection mode.
785a57594a1SThomas Gleixner 	 */
7861696a8beSPeter Zijlstra 	if (!detect_deadlock && waiter != top_waiter)
7871696a8beSPeter Zijlstra 		goto out_put_task;
7881696a8beSPeter Zijlstra 
7891696a8beSPeter Zijlstra 	goto again;
7901696a8beSPeter Zijlstra 
7911696a8beSPeter Zijlstra  out_unlock_pi:
792b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&task->pi_lock);
7931696a8beSPeter Zijlstra  out_put_task:
7941696a8beSPeter Zijlstra 	put_task_struct(task);
7951696a8beSPeter Zijlstra 
7961696a8beSPeter Zijlstra 	return ret;
7971696a8beSPeter Zijlstra }
7981696a8beSPeter Zijlstra 
7991696a8beSPeter Zijlstra /*
8001696a8beSPeter Zijlstra  * Try to take an rt-mutex
8011696a8beSPeter Zijlstra  *
802b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled
8031696a8beSPeter Zijlstra  *
804358c331fSThomas Gleixner  * @lock:   The lock to be acquired.
805358c331fSThomas Gleixner  * @task:   The task which wants to acquire the lock
8069f40a51aSDavidlohr Bueso  * @waiter: The waiter that is queued to the lock's wait tree if the
807358c331fSThomas Gleixner  *	    callsite called task_blocked_on_lock(), otherwise NULL
8081696a8beSPeter Zijlstra  */
8091696a8beSPeter Zijlstra static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
8101696a8beSPeter Zijlstra 				struct rt_mutex_waiter *waiter)
8111696a8beSPeter Zijlstra {
812e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
813e0aad5b4SPeter Zijlstra 
8141696a8beSPeter Zijlstra 	/*
815358c331fSThomas Gleixner 	 * Before testing whether we can acquire @lock, we set the
816358c331fSThomas Gleixner 	 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
817358c331fSThomas Gleixner 	 * other tasks which try to modify @lock into the slow path
818358c331fSThomas Gleixner 	 * and they serialize on @lock->wait_lock.
8191696a8beSPeter Zijlstra 	 *
820358c331fSThomas Gleixner 	 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
821358c331fSThomas Gleixner 	 * as explained at the top of this file if and only if:
8221696a8beSPeter Zijlstra 	 *
823358c331fSThomas Gleixner 	 * - There is a lock owner. The caller must fixup the
824358c331fSThomas Gleixner 	 *   transient state if it does a trylock or leaves the lock
825358c331fSThomas Gleixner 	 *   function due to a signal or timeout.
826358c331fSThomas Gleixner 	 *
827358c331fSThomas Gleixner 	 * - @task acquires the lock and there are no other
828358c331fSThomas Gleixner 	 *   waiters. This is undone in rt_mutex_set_owner(@task) at
829358c331fSThomas Gleixner 	 *   the end of this function.
8301696a8beSPeter Zijlstra 	 */
8311696a8beSPeter Zijlstra 	mark_rt_mutex_waiters(lock);
8321696a8beSPeter Zijlstra 
833358c331fSThomas Gleixner 	/*
834358c331fSThomas Gleixner 	 * If @lock has an owner, give up.
835358c331fSThomas Gleixner 	 */
8361696a8beSPeter Zijlstra 	if (rt_mutex_owner(lock))
8371696a8beSPeter Zijlstra 		return 0;
8381696a8beSPeter Zijlstra 
8391696a8beSPeter Zijlstra 	/*
840358c331fSThomas Gleixner 	 * If @waiter != NULL, @task has already enqueued the waiter
8419f40a51aSDavidlohr Bueso 	 * into @lock waiter tree. If @waiter == NULL then this is a
842358c331fSThomas Gleixner 	 * trylock attempt.
843358c331fSThomas Gleixner 	 */
844358c331fSThomas Gleixner 	if (waiter) {
845358c331fSThomas Gleixner 		/*
846358c331fSThomas Gleixner 		 * If waiter is not the highest priority waiter of
847358c331fSThomas Gleixner 		 * @lock, give up.
848358c331fSThomas Gleixner 		 */
849358c331fSThomas Gleixner 		if (waiter != rt_mutex_top_waiter(lock))
850358c331fSThomas Gleixner 			return 0;
851358c331fSThomas Gleixner 
852358c331fSThomas Gleixner 		/*
853358c331fSThomas Gleixner 		 * We can acquire the lock. Remove the waiter from the
8549f40a51aSDavidlohr Bueso 		 * lock waiters tree.
855358c331fSThomas Gleixner 		 */
856358c331fSThomas Gleixner 		rt_mutex_dequeue(lock, waiter);
857358c331fSThomas Gleixner 
858358c331fSThomas Gleixner 	} else {
859358c331fSThomas Gleixner 		/*
860358c331fSThomas Gleixner 		 * If the lock has waiters already we check whether @task is
861358c331fSThomas Gleixner 		 * eligible to take over the lock.
862358c331fSThomas Gleixner 		 *
863358c331fSThomas Gleixner 		 * If there are no other waiters, @task can acquire
864358c331fSThomas Gleixner 		 * the lock.  @task->pi_blocked_on is NULL, so it does
865358c331fSThomas Gleixner 		 * not need to be dequeued.
8661696a8beSPeter Zijlstra 		 */
8671696a8beSPeter Zijlstra 		if (rt_mutex_has_waiters(lock)) {
868358c331fSThomas Gleixner 			/*
869358c331fSThomas Gleixner 			 * If @task->prio is greater than or equal to
870358c331fSThomas Gleixner 			 * the top waiter priority (kernel view),
871358c331fSThomas Gleixner 			 * @task lost.
872358c331fSThomas Gleixner 			 */
87319830e55SPeter Zijlstra 			if (!rt_mutex_waiter_less(task_to_waiter(task),
87419830e55SPeter Zijlstra 						  rt_mutex_top_waiter(lock)))
8751696a8beSPeter Zijlstra 				return 0;
876358c331fSThomas Gleixner 
877358c331fSThomas Gleixner 			/*
878358c331fSThomas Gleixner 			 * The current top waiter stays enqueued. We
879358c331fSThomas Gleixner 			 * don't have to change anything in the lock
880358c331fSThomas Gleixner 			 * waiters order.
881358c331fSThomas Gleixner 			 */
882358c331fSThomas Gleixner 		} else {
883358c331fSThomas Gleixner 			/*
884358c331fSThomas Gleixner 			 * No waiters. Take the lock without the
885358c331fSThomas Gleixner 			 * pi_lock dance.@task->pi_blocked_on is NULL
886358c331fSThomas Gleixner 			 * and we have no waiters to enqueue in @task
8879f40a51aSDavidlohr Bueso 			 * pi waiters tree.
888358c331fSThomas Gleixner 			 */
889358c331fSThomas Gleixner 			goto takeit;
8901696a8beSPeter Zijlstra 		}
8911696a8beSPeter Zijlstra 	}
8921696a8beSPeter Zijlstra 
8931696a8beSPeter Zijlstra 	/*
894358c331fSThomas Gleixner 	 * Clear @task->pi_blocked_on. Requires protection by
895358c331fSThomas Gleixner 	 * @task->pi_lock. Redundant operation for the @waiter == NULL
896358c331fSThomas Gleixner 	 * case, but conditionals are more expensive than a redundant
897358c331fSThomas Gleixner 	 * store.
8981696a8beSPeter Zijlstra 	 */
899b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
900358c331fSThomas Gleixner 	task->pi_blocked_on = NULL;
901358c331fSThomas Gleixner 	/*
902358c331fSThomas Gleixner 	 * Finish the lock acquisition. @task is the new owner. If
903358c331fSThomas Gleixner 	 * other waiters exist we have to insert the highest priority
9049f40a51aSDavidlohr Bueso 	 * waiter into @task->pi_waiters tree.
905358c331fSThomas Gleixner 	 */
906358c331fSThomas Gleixner 	if (rt_mutex_has_waiters(lock))
907358c331fSThomas Gleixner 		rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
908b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
9091696a8beSPeter Zijlstra 
910358c331fSThomas Gleixner takeit:
9111696a8beSPeter Zijlstra 	/* We got the lock. */
9121696a8beSPeter Zijlstra 	debug_rt_mutex_lock(lock);
9131696a8beSPeter Zijlstra 
914358c331fSThomas Gleixner 	/*
915358c331fSThomas Gleixner 	 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
916358c331fSThomas Gleixner 	 * are still waiters or clears it.
917358c331fSThomas Gleixner 	 */
9181696a8beSPeter Zijlstra 	rt_mutex_set_owner(lock, task);
9191696a8beSPeter Zijlstra 
9201696a8beSPeter Zijlstra 	return 1;
9211696a8beSPeter Zijlstra }
9221696a8beSPeter Zijlstra 
9231696a8beSPeter Zijlstra /*
9241696a8beSPeter Zijlstra  * Task blocks on lock.
9251696a8beSPeter Zijlstra  *
9261696a8beSPeter Zijlstra  * Prepare waiter and propagate pi chain
9271696a8beSPeter Zijlstra  *
928b4abf910SThomas Gleixner  * This must be called with lock->wait_lock held and interrupts disabled
9291696a8beSPeter Zijlstra  */
9301696a8beSPeter Zijlstra static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
9311696a8beSPeter Zijlstra 				   struct rt_mutex_waiter *waiter,
9321696a8beSPeter Zijlstra 				   struct task_struct *task,
9338930ed80SThomas Gleixner 				   enum rtmutex_chainwalk chwalk)
9341696a8beSPeter Zijlstra {
9351696a8beSPeter Zijlstra 	struct task_struct *owner = rt_mutex_owner(lock);
9361696a8beSPeter Zijlstra 	struct rt_mutex_waiter *top_waiter = waiter;
93782084984SThomas Gleixner 	struct rt_mutex *next_lock;
9381696a8beSPeter Zijlstra 	int chain_walk = 0, res;
9391696a8beSPeter Zijlstra 
940e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
941e0aad5b4SPeter Zijlstra 
942397335f0SThomas Gleixner 	/*
943397335f0SThomas Gleixner 	 * Early deadlock detection. We really don't want the task to
944397335f0SThomas Gleixner 	 * enqueue on itself just to untangle the mess later. It's not
945397335f0SThomas Gleixner 	 * only an optimization. We drop the locks, so another waiter
946397335f0SThomas Gleixner 	 * can come in before the chain walk detects the deadlock. So
947397335f0SThomas Gleixner 	 * the other will detect the deadlock and return -EDEADLOCK,
948397335f0SThomas Gleixner 	 * which is wrong, as the other waiter is not in a deadlock
949397335f0SThomas Gleixner 	 * situation.
950397335f0SThomas Gleixner 	 */
9513d5c9340SThomas Gleixner 	if (owner == task)
952397335f0SThomas Gleixner 		return -EDEADLK;
953397335f0SThomas Gleixner 
954b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
9551696a8beSPeter Zijlstra 	waiter->task = task;
9561696a8beSPeter Zijlstra 	waiter->lock = lock;
9572d3d891dSDario Faggioli 	waiter->prio = task->prio;
958e0aad5b4SPeter Zijlstra 	waiter->deadline = task->dl.deadline;
9591696a8beSPeter Zijlstra 
9601696a8beSPeter Zijlstra 	/* Get the top priority waiter on the lock */
9611696a8beSPeter Zijlstra 	if (rt_mutex_has_waiters(lock))
9621696a8beSPeter Zijlstra 		top_waiter = rt_mutex_top_waiter(lock);
963fb00aca4SPeter Zijlstra 	rt_mutex_enqueue(lock, waiter);
9641696a8beSPeter Zijlstra 
9651696a8beSPeter Zijlstra 	task->pi_blocked_on = waiter;
9661696a8beSPeter Zijlstra 
967b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
9681696a8beSPeter Zijlstra 
9691696a8beSPeter Zijlstra 	if (!owner)
9701696a8beSPeter Zijlstra 		return 0;
9711696a8beSPeter Zijlstra 
972b4abf910SThomas Gleixner 	raw_spin_lock(&owner->pi_lock);
97382084984SThomas Gleixner 	if (waiter == rt_mutex_top_waiter(lock)) {
974fb00aca4SPeter Zijlstra 		rt_mutex_dequeue_pi(owner, top_waiter);
975fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(owner, waiter);
9761696a8beSPeter Zijlstra 
977acd58620SPeter Zijlstra 		rt_mutex_adjust_prio(owner);
9781696a8beSPeter Zijlstra 		if (owner->pi_blocked_on)
9791696a8beSPeter Zijlstra 			chain_walk = 1;
9808930ed80SThomas Gleixner 	} else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
9811696a8beSPeter Zijlstra 		chain_walk = 1;
98282084984SThomas Gleixner 	}
9831696a8beSPeter Zijlstra 
98482084984SThomas Gleixner 	/* Store the lock on which owner is blocked or NULL */
98582084984SThomas Gleixner 	next_lock = task_blocked_on_lock(owner);
98682084984SThomas Gleixner 
987b4abf910SThomas Gleixner 	raw_spin_unlock(&owner->pi_lock);
98882084984SThomas Gleixner 	/*
98982084984SThomas Gleixner 	 * Even if full deadlock detection is on, if the owner is not
99082084984SThomas Gleixner 	 * blocked itself, we can avoid finding this out in the chain
99182084984SThomas Gleixner 	 * walk.
99282084984SThomas Gleixner 	 */
99382084984SThomas Gleixner 	if (!chain_walk || !next_lock)
9941696a8beSPeter Zijlstra 		return 0;
9951696a8beSPeter Zijlstra 
9961696a8beSPeter Zijlstra 	/*
9971696a8beSPeter Zijlstra 	 * The owner can't disappear while holding a lock,
9981696a8beSPeter Zijlstra 	 * so the owner struct is protected by wait_lock.
9991696a8beSPeter Zijlstra 	 * Gets dropped in rt_mutex_adjust_prio_chain()!
10001696a8beSPeter Zijlstra 	 */
10011696a8beSPeter Zijlstra 	get_task_struct(owner);
10021696a8beSPeter Zijlstra 
1003b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
10041696a8beSPeter Zijlstra 
10058930ed80SThomas Gleixner 	res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
100682084984SThomas Gleixner 					 next_lock, waiter, task);
10071696a8beSPeter Zijlstra 
1008b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
10091696a8beSPeter Zijlstra 
10101696a8beSPeter Zijlstra 	return res;
10111696a8beSPeter Zijlstra }
10121696a8beSPeter Zijlstra 
10131696a8beSPeter Zijlstra /*
10149f40a51aSDavidlohr Bueso  * Remove the top waiter from the current tasks pi waiter tree and
101545ab4effSDavidlohr Bueso  * queue it up.
10161696a8beSPeter Zijlstra  *
1017b4abf910SThomas Gleixner  * Called with lock->wait_lock held and interrupts disabled.
10181696a8beSPeter Zijlstra  */
101945ab4effSDavidlohr Bueso static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
102045ab4effSDavidlohr Bueso 				    struct rt_mutex *lock)
10211696a8beSPeter Zijlstra {
10221696a8beSPeter Zijlstra 	struct rt_mutex_waiter *waiter;
10231696a8beSPeter Zijlstra 
1024b4abf910SThomas Gleixner 	raw_spin_lock(&current->pi_lock);
10251696a8beSPeter Zijlstra 
10261696a8beSPeter Zijlstra 	waiter = rt_mutex_top_waiter(lock);
10271696a8beSPeter Zijlstra 
10281696a8beSPeter Zijlstra 	/*
1029acd58620SPeter Zijlstra 	 * Remove it from current->pi_waiters and deboost.
1030acd58620SPeter Zijlstra 	 *
1031acd58620SPeter Zijlstra 	 * We must in fact deboost here in order to ensure we call
1032acd58620SPeter Zijlstra 	 * rt_mutex_setprio() to update p->pi_top_task before the
1033acd58620SPeter Zijlstra 	 * task unblocks.
10341696a8beSPeter Zijlstra 	 */
1035fb00aca4SPeter Zijlstra 	rt_mutex_dequeue_pi(current, waiter);
1036acd58620SPeter Zijlstra 	rt_mutex_adjust_prio(current);
10371696a8beSPeter Zijlstra 
103827e35715SThomas Gleixner 	/*
103927e35715SThomas Gleixner 	 * As we are waking up the top waiter, and the waiter stays
104027e35715SThomas Gleixner 	 * queued on the lock until it gets the lock, this lock
104127e35715SThomas Gleixner 	 * obviously has waiters. Just set the bit here and this has
104227e35715SThomas Gleixner 	 * the added benefit of forcing all new tasks into the
104327e35715SThomas Gleixner 	 * slow path making sure no task of lower priority than
104427e35715SThomas Gleixner 	 * the top waiter can steal this lock.
104527e35715SThomas Gleixner 	 */
104627e35715SThomas Gleixner 	lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
10471696a8beSPeter Zijlstra 
1048acd58620SPeter Zijlstra 	/*
1049acd58620SPeter Zijlstra 	 * We deboosted before waking the top waiter task such that we don't
1050acd58620SPeter Zijlstra 	 * run two tasks with the 'same' priority (and ensure the
1051acd58620SPeter Zijlstra 	 * p->pi_top_task pointer points to a blocked task). This however can
1052acd58620SPeter Zijlstra 	 * lead to priority inversion if we would get preempted after the
1053acd58620SPeter Zijlstra 	 * deboost but before waking our donor task, hence the preempt_disable()
1054acd58620SPeter Zijlstra 	 * before unlock.
1055acd58620SPeter Zijlstra 	 *
1056acd58620SPeter Zijlstra 	 * Pairs with preempt_enable() in rt_mutex_postunlock();
1057acd58620SPeter Zijlstra 	 */
1058acd58620SPeter Zijlstra 	preempt_disable();
105945ab4effSDavidlohr Bueso 	wake_q_add(wake_q, waiter->task);
1060acd58620SPeter Zijlstra 	raw_spin_unlock(&current->pi_lock);
10611696a8beSPeter Zijlstra }
10621696a8beSPeter Zijlstra 
10631696a8beSPeter Zijlstra /*
10641696a8beSPeter Zijlstra  * Remove a waiter from a lock and give up
10651696a8beSPeter Zijlstra  *
1066b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled. I must
10671696a8beSPeter Zijlstra  * have just failed to try_to_take_rt_mutex().
10681696a8beSPeter Zijlstra  */
10691696a8beSPeter Zijlstra static void remove_waiter(struct rt_mutex *lock,
10701696a8beSPeter Zijlstra 			  struct rt_mutex_waiter *waiter)
10711696a8beSPeter Zijlstra {
10721ca7b860SThomas Gleixner 	bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
10731696a8beSPeter Zijlstra 	struct task_struct *owner = rt_mutex_owner(lock);
10741ca7b860SThomas Gleixner 	struct rt_mutex *next_lock;
10751696a8beSPeter Zijlstra 
1076e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1077e0aad5b4SPeter Zijlstra 
1078b4abf910SThomas Gleixner 	raw_spin_lock(&current->pi_lock);
1079fb00aca4SPeter Zijlstra 	rt_mutex_dequeue(lock, waiter);
10801696a8beSPeter Zijlstra 	current->pi_blocked_on = NULL;
1081b4abf910SThomas Gleixner 	raw_spin_unlock(&current->pi_lock);
10821696a8beSPeter Zijlstra 
10831ca7b860SThomas Gleixner 	/*
10841ca7b860SThomas Gleixner 	 * Only update priority if the waiter was the highest priority
10851ca7b860SThomas Gleixner 	 * waiter of the lock and there is an owner to update.
10861ca7b860SThomas Gleixner 	 */
10871ca7b860SThomas Gleixner 	if (!owner || !is_top_waiter)
10881696a8beSPeter Zijlstra 		return;
10891696a8beSPeter Zijlstra 
1090b4abf910SThomas Gleixner 	raw_spin_lock(&owner->pi_lock);
10911696a8beSPeter Zijlstra 
1092fb00aca4SPeter Zijlstra 	rt_mutex_dequeue_pi(owner, waiter);
10931696a8beSPeter Zijlstra 
10941ca7b860SThomas Gleixner 	if (rt_mutex_has_waiters(lock))
10951ca7b860SThomas Gleixner 		rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
10961696a8beSPeter Zijlstra 
1097acd58620SPeter Zijlstra 	rt_mutex_adjust_prio(owner);
10981696a8beSPeter Zijlstra 
109982084984SThomas Gleixner 	/* Store the lock on which owner is blocked or NULL */
110082084984SThomas Gleixner 	next_lock = task_blocked_on_lock(owner);
11011696a8beSPeter Zijlstra 
1102b4abf910SThomas Gleixner 	raw_spin_unlock(&owner->pi_lock);
11031696a8beSPeter Zijlstra 
11041ca7b860SThomas Gleixner 	/*
11051ca7b860SThomas Gleixner 	 * Don't walk the chain, if the owner task is not blocked
11061ca7b860SThomas Gleixner 	 * itself.
11071ca7b860SThomas Gleixner 	 */
110882084984SThomas Gleixner 	if (!next_lock)
11091696a8beSPeter Zijlstra 		return;
11101696a8beSPeter Zijlstra 
11111696a8beSPeter Zijlstra 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
11121696a8beSPeter Zijlstra 	get_task_struct(owner);
11131696a8beSPeter Zijlstra 
1114b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
11151696a8beSPeter Zijlstra 
11168930ed80SThomas Gleixner 	rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
11178930ed80SThomas Gleixner 				   next_lock, NULL, current);
11181696a8beSPeter Zijlstra 
1119b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
11201696a8beSPeter Zijlstra }
11211696a8beSPeter Zijlstra 
11221696a8beSPeter Zijlstra /*
11231696a8beSPeter Zijlstra  * Recheck the pi chain, in case we got a priority setting
11241696a8beSPeter Zijlstra  *
11251696a8beSPeter Zijlstra  * Called from sched_setscheduler
11261696a8beSPeter Zijlstra  */
11271696a8beSPeter Zijlstra void rt_mutex_adjust_pi(struct task_struct *task)
11281696a8beSPeter Zijlstra {
11291696a8beSPeter Zijlstra 	struct rt_mutex_waiter *waiter;
113082084984SThomas Gleixner 	struct rt_mutex *next_lock;
11311696a8beSPeter Zijlstra 	unsigned long flags;
11321696a8beSPeter Zijlstra 
11331696a8beSPeter Zijlstra 	raw_spin_lock_irqsave(&task->pi_lock, flags);
11341696a8beSPeter Zijlstra 
11351696a8beSPeter Zijlstra 	waiter = task->pi_blocked_on;
113619830e55SPeter Zijlstra 	if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
11371696a8beSPeter Zijlstra 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
11381696a8beSPeter Zijlstra 		return;
11391696a8beSPeter Zijlstra 	}
114082084984SThomas Gleixner 	next_lock = waiter->lock;
11411696a8beSPeter Zijlstra 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
11421696a8beSPeter Zijlstra 
11431696a8beSPeter Zijlstra 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
11441696a8beSPeter Zijlstra 	get_task_struct(task);
114582084984SThomas Gleixner 
11468930ed80SThomas Gleixner 	rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
11478930ed80SThomas Gleixner 				   next_lock, NULL, task);
11481696a8beSPeter Zijlstra }
11491696a8beSPeter Zijlstra 
115050809358SPeter Zijlstra void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
115150809358SPeter Zijlstra {
115250809358SPeter Zijlstra 	debug_rt_mutex_init_waiter(waiter);
115350809358SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->pi_tree_entry);
115450809358SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->tree_entry);
115550809358SPeter Zijlstra 	waiter->task = NULL;
115650809358SPeter Zijlstra }
115750809358SPeter Zijlstra 
11581696a8beSPeter Zijlstra /**
11591696a8beSPeter Zijlstra  * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
11601696a8beSPeter Zijlstra  * @lock:		 the rt_mutex to take
11611696a8beSPeter Zijlstra  * @state:		 the state the task should block in (TASK_INTERRUPTIBLE
11621696a8beSPeter Zijlstra  *			 or TASK_UNINTERRUPTIBLE)
11631696a8beSPeter Zijlstra  * @timeout:		 the pre-initialized and started timer, or NULL for none
11641696a8beSPeter Zijlstra  * @waiter:		 the pre-initialized rt_mutex_waiter
11651696a8beSPeter Zijlstra  *
1166b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled
11671696a8beSPeter Zijlstra  */
11681696a8beSPeter Zijlstra static int __sched
11691696a8beSPeter Zijlstra __rt_mutex_slowlock(struct rt_mutex *lock, int state,
11701696a8beSPeter Zijlstra 		    struct hrtimer_sleeper *timeout,
11711696a8beSPeter Zijlstra 		    struct rt_mutex_waiter *waiter)
11721696a8beSPeter Zijlstra {
11731696a8beSPeter Zijlstra 	int ret = 0;
11741696a8beSPeter Zijlstra 
11751696a8beSPeter Zijlstra 	for (;;) {
11761696a8beSPeter Zijlstra 		/* Try to acquire the lock: */
11771696a8beSPeter Zijlstra 		if (try_to_take_rt_mutex(lock, current, waiter))
11781696a8beSPeter Zijlstra 			break;
11791696a8beSPeter Zijlstra 
11801696a8beSPeter Zijlstra 		/*
11811696a8beSPeter Zijlstra 		 * TASK_INTERRUPTIBLE checks for signals and
11821696a8beSPeter Zijlstra 		 * timeout. Ignored otherwise.
11831696a8beSPeter Zijlstra 		 */
11844009f4b3SSteven Rostedt (VMware) 		if (likely(state == TASK_INTERRUPTIBLE)) {
11851696a8beSPeter Zijlstra 			/* Signal pending? */
11861696a8beSPeter Zijlstra 			if (signal_pending(current))
11871696a8beSPeter Zijlstra 				ret = -EINTR;
11881696a8beSPeter Zijlstra 			if (timeout && !timeout->task)
11891696a8beSPeter Zijlstra 				ret = -ETIMEDOUT;
11901696a8beSPeter Zijlstra 			if (ret)
11911696a8beSPeter Zijlstra 				break;
11921696a8beSPeter Zijlstra 		}
11931696a8beSPeter Zijlstra 
1194b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
11951696a8beSPeter Zijlstra 
11961696a8beSPeter Zijlstra 		debug_rt_mutex_print_deadlock(waiter);
11971696a8beSPeter Zijlstra 
11981b0b7c17SDavidlohr Bueso 		schedule();
11991696a8beSPeter Zijlstra 
1200b4abf910SThomas Gleixner 		raw_spin_lock_irq(&lock->wait_lock);
12011696a8beSPeter Zijlstra 		set_current_state(state);
12021696a8beSPeter Zijlstra 	}
12031696a8beSPeter Zijlstra 
1204afffc6c1SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
12051696a8beSPeter Zijlstra 	return ret;
12061696a8beSPeter Zijlstra }
12071696a8beSPeter Zijlstra 
12083d5c9340SThomas Gleixner static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
12093d5c9340SThomas Gleixner 				     struct rt_mutex_waiter *w)
12103d5c9340SThomas Gleixner {
12113d5c9340SThomas Gleixner 	/*
12123d5c9340SThomas Gleixner 	 * If the result is not -EDEADLOCK or the caller requested
12133d5c9340SThomas Gleixner 	 * deadlock detection, nothing to do here.
12143d5c9340SThomas Gleixner 	 */
12153d5c9340SThomas Gleixner 	if (res != -EDEADLOCK || detect_deadlock)
12163d5c9340SThomas Gleixner 		return;
12173d5c9340SThomas Gleixner 
12183d5c9340SThomas Gleixner 	/*
12193d5c9340SThomas Gleixner 	 * Yell lowdly and stop the task right here.
12203d5c9340SThomas Gleixner 	 */
12213d5c9340SThomas Gleixner 	rt_mutex_print_deadlock(w);
12223d5c9340SThomas Gleixner 	while (1) {
12233d5c9340SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
12243d5c9340SThomas Gleixner 		schedule();
12253d5c9340SThomas Gleixner 	}
12263d5c9340SThomas Gleixner }
12273d5c9340SThomas Gleixner 
12281696a8beSPeter Zijlstra /*
12291696a8beSPeter Zijlstra  * Slow path lock function:
12301696a8beSPeter Zijlstra  */
12311696a8beSPeter Zijlstra static int __sched
12321696a8beSPeter Zijlstra rt_mutex_slowlock(struct rt_mutex *lock, int state,
12331696a8beSPeter Zijlstra 		  struct hrtimer_sleeper *timeout,
12348930ed80SThomas Gleixner 		  enum rtmutex_chainwalk chwalk)
12351696a8beSPeter Zijlstra {
12361696a8beSPeter Zijlstra 	struct rt_mutex_waiter waiter;
1237b4abf910SThomas Gleixner 	unsigned long flags;
12381696a8beSPeter Zijlstra 	int ret = 0;
12391696a8beSPeter Zijlstra 
124050809358SPeter Zijlstra 	rt_mutex_init_waiter(&waiter);
12411696a8beSPeter Zijlstra 
1242b4abf910SThomas Gleixner 	/*
1243b4abf910SThomas Gleixner 	 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1244b4abf910SThomas Gleixner 	 * be called in early boot if the cmpxchg() fast path is disabled
1245b4abf910SThomas Gleixner 	 * (debug, no architecture support). In this case we will acquire the
1246b4abf910SThomas Gleixner 	 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1247b4abf910SThomas Gleixner 	 * enable interrupts in that early boot case. So we need to use the
1248b4abf910SThomas Gleixner 	 * irqsave/restore variants.
1249b4abf910SThomas Gleixner 	 */
1250b4abf910SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
12511696a8beSPeter Zijlstra 
12521696a8beSPeter Zijlstra 	/* Try to acquire the lock again: */
12531696a8beSPeter Zijlstra 	if (try_to_take_rt_mutex(lock, current, NULL)) {
1254b4abf910SThomas Gleixner 		raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
12551696a8beSPeter Zijlstra 		return 0;
12561696a8beSPeter Zijlstra 	}
12571696a8beSPeter Zijlstra 
12581696a8beSPeter Zijlstra 	set_current_state(state);
12591696a8beSPeter Zijlstra 
12601696a8beSPeter Zijlstra 	/* Setup the timer, when timeout != NULL */
1261ccdd92c1SThomas Gleixner 	if (unlikely(timeout))
12621696a8beSPeter Zijlstra 		hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
12631696a8beSPeter Zijlstra 
12648930ed80SThomas Gleixner 	ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
12651696a8beSPeter Zijlstra 
12661696a8beSPeter Zijlstra 	if (likely(!ret))
1267afffc6c1SDavidlohr Bueso 		/* sleep on the mutex */
12681696a8beSPeter Zijlstra 		ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
12691696a8beSPeter Zijlstra 
12703d5c9340SThomas Gleixner 	if (unlikely(ret)) {
12719d3e2d02SSebastian Andrzej Siewior 		__set_current_state(TASK_RUNNING);
12721696a8beSPeter Zijlstra 		remove_waiter(lock, &waiter);
12738930ed80SThomas Gleixner 		rt_mutex_handle_deadlock(ret, chwalk, &waiter);
12743d5c9340SThomas Gleixner 	}
12751696a8beSPeter Zijlstra 
12761696a8beSPeter Zijlstra 	/*
12771696a8beSPeter Zijlstra 	 * try_to_take_rt_mutex() sets the waiter bit
12781696a8beSPeter Zijlstra 	 * unconditionally. We might have to fix that up.
12791696a8beSPeter Zijlstra 	 */
12801696a8beSPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
12811696a8beSPeter Zijlstra 
1282b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
12831696a8beSPeter Zijlstra 
12841696a8beSPeter Zijlstra 	/* Remove pending timer: */
12851696a8beSPeter Zijlstra 	if (unlikely(timeout))
12861696a8beSPeter Zijlstra 		hrtimer_cancel(&timeout->timer);
12871696a8beSPeter Zijlstra 
12881696a8beSPeter Zijlstra 	debug_rt_mutex_free_waiter(&waiter);
12891696a8beSPeter Zijlstra 
12901696a8beSPeter Zijlstra 	return ret;
12911696a8beSPeter Zijlstra }
12921696a8beSPeter Zijlstra 
1293c1e2f0eaSPeter Zijlstra static inline int __rt_mutex_slowtrylock(struct rt_mutex *lock)
1294c1e2f0eaSPeter Zijlstra {
1295c1e2f0eaSPeter Zijlstra 	int ret = try_to_take_rt_mutex(lock, current, NULL);
1296c1e2f0eaSPeter Zijlstra 
1297c1e2f0eaSPeter Zijlstra 	/*
1298c1e2f0eaSPeter Zijlstra 	 * try_to_take_rt_mutex() sets the lock waiters bit
1299c1e2f0eaSPeter Zijlstra 	 * unconditionally. Clean this up.
1300c1e2f0eaSPeter Zijlstra 	 */
1301c1e2f0eaSPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
1302c1e2f0eaSPeter Zijlstra 
1303c1e2f0eaSPeter Zijlstra 	return ret;
1304c1e2f0eaSPeter Zijlstra }
1305c1e2f0eaSPeter Zijlstra 
13061696a8beSPeter Zijlstra /*
13071696a8beSPeter Zijlstra  * Slow path try-lock function:
13081696a8beSPeter Zijlstra  */
130988f2b4c1SThomas Gleixner static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
13101696a8beSPeter Zijlstra {
1311b4abf910SThomas Gleixner 	unsigned long flags;
131288f2b4c1SThomas Gleixner 	int ret;
13131696a8beSPeter Zijlstra 
131488f2b4c1SThomas Gleixner 	/*
131588f2b4c1SThomas Gleixner 	 * If the lock already has an owner we fail to get the lock.
131688f2b4c1SThomas Gleixner 	 * This can be done without taking the @lock->wait_lock as
131788f2b4c1SThomas Gleixner 	 * it is only being read, and this is a trylock anyway.
131888f2b4c1SThomas Gleixner 	 */
131988f2b4c1SThomas Gleixner 	if (rt_mutex_owner(lock))
132088f2b4c1SThomas Gleixner 		return 0;
132188f2b4c1SThomas Gleixner 
132288f2b4c1SThomas Gleixner 	/*
1323b4abf910SThomas Gleixner 	 * The mutex has currently no owner. Lock the wait lock and try to
1324b4abf910SThomas Gleixner 	 * acquire the lock. We use irqsave here to support early boot calls.
132588f2b4c1SThomas Gleixner 	 */
1326b4abf910SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
13271696a8beSPeter Zijlstra 
1328c1e2f0eaSPeter Zijlstra 	ret = __rt_mutex_slowtrylock(lock);
13291696a8beSPeter Zijlstra 
1330b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
13311696a8beSPeter Zijlstra 
13321696a8beSPeter Zijlstra 	return ret;
13331696a8beSPeter Zijlstra }
13341696a8beSPeter Zijlstra 
13351696a8beSPeter Zijlstra /*
1336802ab58dSSebastian Andrzej Siewior  * Slow path to release a rt-mutex.
1337aa2bfe55SPeter Zijlstra  *
1338aa2bfe55SPeter Zijlstra  * Return whether the current task needs to call rt_mutex_postunlock().
13391696a8beSPeter Zijlstra  */
1340802ab58dSSebastian Andrzej Siewior static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1341802ab58dSSebastian Andrzej Siewior 					struct wake_q_head *wake_q)
13421696a8beSPeter Zijlstra {
1343b4abf910SThomas Gleixner 	unsigned long flags;
1344b4abf910SThomas Gleixner 
1345b4abf910SThomas Gleixner 	/* irqsave required to support early boot calls */
1346b4abf910SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
13471696a8beSPeter Zijlstra 
13481696a8beSPeter Zijlstra 	debug_rt_mutex_unlock(lock);
13491696a8beSPeter Zijlstra 
135027e35715SThomas Gleixner 	/*
135127e35715SThomas Gleixner 	 * We must be careful here if the fast path is enabled. If we
135227e35715SThomas Gleixner 	 * have no waiters queued we cannot set owner to NULL here
135327e35715SThomas Gleixner 	 * because of:
135427e35715SThomas Gleixner 	 *
135527e35715SThomas Gleixner 	 * foo->lock->owner = NULL;
135627e35715SThomas Gleixner 	 *			rtmutex_lock(foo->lock);   <- fast path
135727e35715SThomas Gleixner 	 *			free = atomic_dec_and_test(foo->refcnt);
135827e35715SThomas Gleixner 	 *			rtmutex_unlock(foo->lock); <- fast path
135927e35715SThomas Gleixner 	 *			if (free)
136027e35715SThomas Gleixner 	 *				kfree(foo);
136127e35715SThomas Gleixner 	 * raw_spin_unlock(foo->lock->wait_lock);
136227e35715SThomas Gleixner 	 *
136327e35715SThomas Gleixner 	 * So for the fastpath enabled kernel:
136427e35715SThomas Gleixner 	 *
136527e35715SThomas Gleixner 	 * Nothing can set the waiters bit as long as we hold
136627e35715SThomas Gleixner 	 * lock->wait_lock. So we do the following sequence:
136727e35715SThomas Gleixner 	 *
136827e35715SThomas Gleixner 	 *	owner = rt_mutex_owner(lock);
136927e35715SThomas Gleixner 	 *	clear_rt_mutex_waiters(lock);
137027e35715SThomas Gleixner 	 *	raw_spin_unlock(&lock->wait_lock);
137127e35715SThomas Gleixner 	 *	if (cmpxchg(&lock->owner, owner, 0) == owner)
137227e35715SThomas Gleixner 	 *		return;
137327e35715SThomas Gleixner 	 *	goto retry;
137427e35715SThomas Gleixner 	 *
137527e35715SThomas Gleixner 	 * The fastpath disabled variant is simple as all access to
137627e35715SThomas Gleixner 	 * lock->owner is serialized by lock->wait_lock:
137727e35715SThomas Gleixner 	 *
137827e35715SThomas Gleixner 	 *	lock->owner = NULL;
137927e35715SThomas Gleixner 	 *	raw_spin_unlock(&lock->wait_lock);
138027e35715SThomas Gleixner 	 */
138127e35715SThomas Gleixner 	while (!rt_mutex_has_waiters(lock)) {
138227e35715SThomas Gleixner 		/* Drops lock->wait_lock ! */
1383b4abf910SThomas Gleixner 		if (unlock_rt_mutex_safe(lock, flags) == true)
1384802ab58dSSebastian Andrzej Siewior 			return false;
138527e35715SThomas Gleixner 		/* Relock the rtmutex and try again */
1386b4abf910SThomas Gleixner 		raw_spin_lock_irqsave(&lock->wait_lock, flags);
13871696a8beSPeter Zijlstra 	}
13881696a8beSPeter Zijlstra 
138927e35715SThomas Gleixner 	/*
139027e35715SThomas Gleixner 	 * The wakeup next waiter path does not suffer from the above
139127e35715SThomas Gleixner 	 * race. See the comments there.
139245ab4effSDavidlohr Bueso 	 *
139345ab4effSDavidlohr Bueso 	 * Queue the next waiter for wakeup once we release the wait_lock.
139427e35715SThomas Gleixner 	 */
1395802ab58dSSebastian Andrzej Siewior 	mark_wakeup_next_waiter(wake_q, lock);
1396b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
13971696a8beSPeter Zijlstra 
1398aa2bfe55SPeter Zijlstra 	return true; /* call rt_mutex_postunlock() */
13991696a8beSPeter Zijlstra }
14001696a8beSPeter Zijlstra 
14011696a8beSPeter Zijlstra /*
14021696a8beSPeter Zijlstra  * debug aware fast / slowpath lock,trylock,unlock
14031696a8beSPeter Zijlstra  *
14041696a8beSPeter Zijlstra  * The atomic acquire/release ops are compiled away, when either the
14051696a8beSPeter Zijlstra  * architecture does not support cmpxchg or when debugging is enabled.
14061696a8beSPeter Zijlstra  */
14071696a8beSPeter Zijlstra static inline int
14081696a8beSPeter Zijlstra rt_mutex_fastlock(struct rt_mutex *lock, int state,
14091696a8beSPeter Zijlstra 		  int (*slowfn)(struct rt_mutex *lock, int state,
14101696a8beSPeter Zijlstra 				struct hrtimer_sleeper *timeout,
14118930ed80SThomas Gleixner 				enum rtmutex_chainwalk chwalk))
14121696a8beSPeter Zijlstra {
1413fffa954fSPeter Zijlstra 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
14141696a8beSPeter Zijlstra 		return 0;
1415fffa954fSPeter Zijlstra 
14168930ed80SThomas Gleixner 	return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
14171696a8beSPeter Zijlstra }
14181696a8beSPeter Zijlstra 
14191696a8beSPeter Zijlstra static inline int
14201696a8beSPeter Zijlstra rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
14218930ed80SThomas Gleixner 			struct hrtimer_sleeper *timeout,
14228930ed80SThomas Gleixner 			enum rtmutex_chainwalk chwalk,
14231696a8beSPeter Zijlstra 			int (*slowfn)(struct rt_mutex *lock, int state,
14241696a8beSPeter Zijlstra 				      struct hrtimer_sleeper *timeout,
14258930ed80SThomas Gleixner 				      enum rtmutex_chainwalk chwalk))
14261696a8beSPeter Zijlstra {
14278930ed80SThomas Gleixner 	if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1428fffa954fSPeter Zijlstra 	    likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
14291696a8beSPeter Zijlstra 		return 0;
1430fffa954fSPeter Zijlstra 
14318930ed80SThomas Gleixner 	return slowfn(lock, state, timeout, chwalk);
14321696a8beSPeter Zijlstra }
14331696a8beSPeter Zijlstra 
14341696a8beSPeter Zijlstra static inline int
14351696a8beSPeter Zijlstra rt_mutex_fasttrylock(struct rt_mutex *lock,
14361696a8beSPeter Zijlstra 		     int (*slowfn)(struct rt_mutex *lock))
14371696a8beSPeter Zijlstra {
1438fffa954fSPeter Zijlstra 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
14391696a8beSPeter Zijlstra 		return 1;
1440fffa954fSPeter Zijlstra 
14411696a8beSPeter Zijlstra 	return slowfn(lock);
14421696a8beSPeter Zijlstra }
14431696a8beSPeter Zijlstra 
14442a1c6029SXunlei Pang /*
1445aa2bfe55SPeter Zijlstra  * Performs the wakeup of the the top-waiter and re-enables preemption.
14462a1c6029SXunlei Pang  */
1447aa2bfe55SPeter Zijlstra void rt_mutex_postunlock(struct wake_q_head *wake_q)
14482a1c6029SXunlei Pang {
14492a1c6029SXunlei Pang 	wake_up_q(wake_q);
14502a1c6029SXunlei Pang 
14512a1c6029SXunlei Pang 	/* Pairs with preempt_disable() in rt_mutex_slowunlock() */
14522a1c6029SXunlei Pang 	preempt_enable();
14532a1c6029SXunlei Pang }
14542a1c6029SXunlei Pang 
14551696a8beSPeter Zijlstra static inline void
14561696a8beSPeter Zijlstra rt_mutex_fastunlock(struct rt_mutex *lock,
1457802ab58dSSebastian Andrzej Siewior 		    bool (*slowfn)(struct rt_mutex *lock,
1458802ab58dSSebastian Andrzej Siewior 				   struct wake_q_head *wqh))
14591696a8beSPeter Zijlstra {
1460194a6b5bSWaiman Long 	DEFINE_WAKE_Q(wake_q);
1461802ab58dSSebastian Andrzej Siewior 
1462fffa954fSPeter Zijlstra 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1463fffa954fSPeter Zijlstra 		return;
1464802ab58dSSebastian Andrzej Siewior 
1465aa2bfe55SPeter Zijlstra 	if (slowfn(lock, &wake_q))
1466aa2bfe55SPeter Zijlstra 		rt_mutex_postunlock(&wake_q);
1467802ab58dSSebastian Andrzej Siewior }
14681696a8beSPeter Zijlstra 
146962cedf3eSPeter Rosin static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
147062cedf3eSPeter Rosin {
147162cedf3eSPeter Rosin 	might_sleep();
147262cedf3eSPeter Rosin 
147362cedf3eSPeter Rosin 	mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
147462cedf3eSPeter Rosin 	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
147562cedf3eSPeter Rosin }
147662cedf3eSPeter Rosin 
147762cedf3eSPeter Rosin #ifdef CONFIG_DEBUG_LOCK_ALLOC
147862cedf3eSPeter Rosin /**
147962cedf3eSPeter Rosin  * rt_mutex_lock_nested - lock a rt_mutex
148062cedf3eSPeter Rosin  *
148162cedf3eSPeter Rosin  * @lock: the rt_mutex to be locked
148262cedf3eSPeter Rosin  * @subclass: the lockdep subclass
148362cedf3eSPeter Rosin  */
148462cedf3eSPeter Rosin void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
148562cedf3eSPeter Rosin {
148662cedf3eSPeter Rosin 	__rt_mutex_lock(lock, subclass);
148762cedf3eSPeter Rosin }
148862cedf3eSPeter Rosin EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
148962cedf3eSPeter Rosin 
149084818af2SSteven Rostedt (VMware) #else /* !CONFIG_DEBUG_LOCK_ALLOC */
149184818af2SSteven Rostedt (VMware) 
14921696a8beSPeter Zijlstra /**
14931696a8beSPeter Zijlstra  * rt_mutex_lock - lock a rt_mutex
14941696a8beSPeter Zijlstra  *
14951696a8beSPeter Zijlstra  * @lock: the rt_mutex to be locked
14961696a8beSPeter Zijlstra  */
14971696a8beSPeter Zijlstra void __sched rt_mutex_lock(struct rt_mutex *lock)
14981696a8beSPeter Zijlstra {
149962cedf3eSPeter Rosin 	__rt_mutex_lock(lock, 0);
15001696a8beSPeter Zijlstra }
15011696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_lock);
150262cedf3eSPeter Rosin #endif
15031696a8beSPeter Zijlstra 
15041696a8beSPeter Zijlstra /**
15051696a8beSPeter Zijlstra  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
15061696a8beSPeter Zijlstra  *
15071696a8beSPeter Zijlstra  * @lock:		the rt_mutex to be locked
15081696a8beSPeter Zijlstra  *
15091696a8beSPeter Zijlstra  * Returns:
15101696a8beSPeter Zijlstra  *  0		on success
15111696a8beSPeter Zijlstra  * -EINTR	when interrupted by a signal
15121696a8beSPeter Zijlstra  */
1513c051b21fSThomas Gleixner int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
15141696a8beSPeter Zijlstra {
1515f5694788SPeter Zijlstra 	int ret;
1516f5694788SPeter Zijlstra 
15171696a8beSPeter Zijlstra 	might_sleep();
15181696a8beSPeter Zijlstra 
1519f5694788SPeter Zijlstra 	mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1520f5694788SPeter Zijlstra 	ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1521f5694788SPeter Zijlstra 	if (ret)
1522f5694788SPeter Zijlstra 		mutex_release(&lock->dep_map, 1, _RET_IP_);
1523f5694788SPeter Zijlstra 
1524f5694788SPeter Zijlstra 	return ret;
15251696a8beSPeter Zijlstra }
15261696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
15271696a8beSPeter Zijlstra 
1528c051b21fSThomas Gleixner /*
15295293c2efSPeter Zijlstra  * Futex variant, must not use fastpath.
15305293c2efSPeter Zijlstra  */
15315293c2efSPeter Zijlstra int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
15325293c2efSPeter Zijlstra {
15335293c2efSPeter Zijlstra 	return rt_mutex_slowtrylock(lock);
1534c051b21fSThomas Gleixner }
1535c051b21fSThomas Gleixner 
1536c1e2f0eaSPeter Zijlstra int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
1537c1e2f0eaSPeter Zijlstra {
1538c1e2f0eaSPeter Zijlstra 	return __rt_mutex_slowtrylock(lock);
1539c1e2f0eaSPeter Zijlstra }
1540c1e2f0eaSPeter Zijlstra 
15411696a8beSPeter Zijlstra /**
15421696a8beSPeter Zijlstra  * rt_mutex_timed_lock - lock a rt_mutex interruptible
15431696a8beSPeter Zijlstra  *			the timeout structure is provided
15441696a8beSPeter Zijlstra  *			by the caller
15451696a8beSPeter Zijlstra  *
15461696a8beSPeter Zijlstra  * @lock:		the rt_mutex to be locked
15471696a8beSPeter Zijlstra  * @timeout:		timeout structure or NULL (no timeout)
15481696a8beSPeter Zijlstra  *
15491696a8beSPeter Zijlstra  * Returns:
15501696a8beSPeter Zijlstra  *  0		on success
15511696a8beSPeter Zijlstra  * -EINTR	when interrupted by a signal
15521696a8beSPeter Zijlstra  * -ETIMEDOUT	when the timeout expired
15531696a8beSPeter Zijlstra  */
15541696a8beSPeter Zijlstra int
1555c051b21fSThomas Gleixner rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
15561696a8beSPeter Zijlstra {
1557f5694788SPeter Zijlstra 	int ret;
1558f5694788SPeter Zijlstra 
15591696a8beSPeter Zijlstra 	might_sleep();
15601696a8beSPeter Zijlstra 
1561f5694788SPeter Zijlstra 	mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1562f5694788SPeter Zijlstra 	ret = rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
15638930ed80SThomas Gleixner 				       RT_MUTEX_MIN_CHAINWALK,
1564c051b21fSThomas Gleixner 				       rt_mutex_slowlock);
1565f5694788SPeter Zijlstra 	if (ret)
1566f5694788SPeter Zijlstra 		mutex_release(&lock->dep_map, 1, _RET_IP_);
1567f5694788SPeter Zijlstra 
1568f5694788SPeter Zijlstra 	return ret;
15691696a8beSPeter Zijlstra }
15701696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
15711696a8beSPeter Zijlstra 
15721696a8beSPeter Zijlstra /**
15731696a8beSPeter Zijlstra  * rt_mutex_trylock - try to lock a rt_mutex
15741696a8beSPeter Zijlstra  *
15751696a8beSPeter Zijlstra  * @lock:	the rt_mutex to be locked
15761696a8beSPeter Zijlstra  *
15776ce47fd9SThomas Gleixner  * This function can only be called in thread context. It's safe to
15786ce47fd9SThomas Gleixner  * call it from atomic regions, but not from hard interrupt or soft
15796ce47fd9SThomas Gleixner  * interrupt context.
15806ce47fd9SThomas Gleixner  *
15811696a8beSPeter Zijlstra  * Returns 1 on success and 0 on contention
15821696a8beSPeter Zijlstra  */
15831696a8beSPeter Zijlstra int __sched rt_mutex_trylock(struct rt_mutex *lock)
15841696a8beSPeter Zijlstra {
1585f5694788SPeter Zijlstra 	int ret;
1586f5694788SPeter Zijlstra 
1587a461d587SSebastian Andrzej Siewior 	if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
15886ce47fd9SThomas Gleixner 		return 0;
15896ce47fd9SThomas Gleixner 
1590f5694788SPeter Zijlstra 	ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1591f5694788SPeter Zijlstra 	if (ret)
1592f5694788SPeter Zijlstra 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1593f5694788SPeter Zijlstra 
1594f5694788SPeter Zijlstra 	return ret;
15951696a8beSPeter Zijlstra }
15961696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_trylock);
15971696a8beSPeter Zijlstra 
15981696a8beSPeter Zijlstra /**
15991696a8beSPeter Zijlstra  * rt_mutex_unlock - unlock a rt_mutex
16001696a8beSPeter Zijlstra  *
16011696a8beSPeter Zijlstra  * @lock: the rt_mutex to be unlocked
16021696a8beSPeter Zijlstra  */
16031696a8beSPeter Zijlstra void __sched rt_mutex_unlock(struct rt_mutex *lock)
16041696a8beSPeter Zijlstra {
1605f5694788SPeter Zijlstra 	mutex_release(&lock->dep_map, 1, _RET_IP_);
16061696a8beSPeter Zijlstra 	rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
16071696a8beSPeter Zijlstra }
16081696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_unlock);
16091696a8beSPeter Zijlstra 
16101696a8beSPeter Zijlstra /**
16115293c2efSPeter Zijlstra  * Futex variant, that since futex variants do not use the fast-path, can be
16125293c2efSPeter Zijlstra  * simple and will not need to retry.
1613802ab58dSSebastian Andrzej Siewior  */
16145293c2efSPeter Zijlstra bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
16155293c2efSPeter Zijlstra 				    struct wake_q_head *wake_q)
1616802ab58dSSebastian Andrzej Siewior {
16175293c2efSPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1618fffa954fSPeter Zijlstra 
16195293c2efSPeter Zijlstra 	debug_rt_mutex_unlock(lock);
16205293c2efSPeter Zijlstra 
16215293c2efSPeter Zijlstra 	if (!rt_mutex_has_waiters(lock)) {
16225293c2efSPeter Zijlstra 		lock->owner = NULL;
16235293c2efSPeter Zijlstra 		return false; /* done */
16245293c2efSPeter Zijlstra 	}
16255293c2efSPeter Zijlstra 
16262a1c6029SXunlei Pang 	/*
1627def34eaaSMike Galbraith 	 * We've already deboosted, mark_wakeup_next_waiter() will
1628def34eaaSMike Galbraith 	 * retain preempt_disabled when we drop the wait_lock, to
1629def34eaaSMike Galbraith 	 * avoid inversion prior to the wakeup.  preempt_disable()
1630def34eaaSMike Galbraith 	 * therein pairs with rt_mutex_postunlock().
16312a1c6029SXunlei Pang 	 */
1632def34eaaSMike Galbraith 	mark_wakeup_next_waiter(wake_q, lock);
16332a1c6029SXunlei Pang 
1634aa2bfe55SPeter Zijlstra 	return true; /* call postunlock() */
16355293c2efSPeter Zijlstra }
16365293c2efSPeter Zijlstra 
16375293c2efSPeter Zijlstra void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
16385293c2efSPeter Zijlstra {
16395293c2efSPeter Zijlstra 	DEFINE_WAKE_Q(wake_q);
16406b0ef92fSBoqun Feng 	unsigned long flags;
1641aa2bfe55SPeter Zijlstra 	bool postunlock;
16425293c2efSPeter Zijlstra 
16436b0ef92fSBoqun Feng 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1644aa2bfe55SPeter Zijlstra 	postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
16456b0ef92fSBoqun Feng 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
16465293c2efSPeter Zijlstra 
1647aa2bfe55SPeter Zijlstra 	if (postunlock)
1648aa2bfe55SPeter Zijlstra 		rt_mutex_postunlock(&wake_q);
1649802ab58dSSebastian Andrzej Siewior }
1650802ab58dSSebastian Andrzej Siewior 
1651802ab58dSSebastian Andrzej Siewior /**
16521696a8beSPeter Zijlstra  * rt_mutex_destroy - mark a mutex unusable
16531696a8beSPeter Zijlstra  * @lock: the mutex to be destroyed
16541696a8beSPeter Zijlstra  *
16551696a8beSPeter Zijlstra  * This function marks the mutex uninitialized, and any subsequent
16561696a8beSPeter Zijlstra  * use of the mutex is forbidden. The mutex must not be locked when
16571696a8beSPeter Zijlstra  * this function is called.
16581696a8beSPeter Zijlstra  */
16591696a8beSPeter Zijlstra void rt_mutex_destroy(struct rt_mutex *lock)
16601696a8beSPeter Zijlstra {
16611696a8beSPeter Zijlstra 	WARN_ON(rt_mutex_is_locked(lock));
16621696a8beSPeter Zijlstra #ifdef CONFIG_DEBUG_RT_MUTEXES
16631696a8beSPeter Zijlstra 	lock->magic = NULL;
16641696a8beSPeter Zijlstra #endif
16651696a8beSPeter Zijlstra }
16661696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(rt_mutex_destroy);
16671696a8beSPeter Zijlstra 
16681696a8beSPeter Zijlstra /**
16691696a8beSPeter Zijlstra  * __rt_mutex_init - initialize the rt lock
16701696a8beSPeter Zijlstra  *
16711696a8beSPeter Zijlstra  * @lock: the rt lock to be initialized
16721696a8beSPeter Zijlstra  *
16731696a8beSPeter Zijlstra  * Initialize the rt lock to unlocked state.
16741696a8beSPeter Zijlstra  *
16751696a8beSPeter Zijlstra  * Initializing of a locked rt lock is not allowed
16761696a8beSPeter Zijlstra  */
1677f5694788SPeter Zijlstra void __rt_mutex_init(struct rt_mutex *lock, const char *name,
1678f5694788SPeter Zijlstra 		     struct lock_class_key *key)
16791696a8beSPeter Zijlstra {
16801696a8beSPeter Zijlstra 	lock->owner = NULL;
16811696a8beSPeter Zijlstra 	raw_spin_lock_init(&lock->wait_lock);
1682a23ba907SDavidlohr Bueso 	lock->waiters = RB_ROOT_CACHED;
16831696a8beSPeter Zijlstra 
1684cde50a67SLevin, Alexander (Sasha Levin) 	if (name && key)
1685f5694788SPeter Zijlstra 		debug_rt_mutex_init(lock, name, key);
16861696a8beSPeter Zijlstra }
16871696a8beSPeter Zijlstra EXPORT_SYMBOL_GPL(__rt_mutex_init);
16881696a8beSPeter Zijlstra 
16891696a8beSPeter Zijlstra /**
16901696a8beSPeter Zijlstra  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
16911696a8beSPeter Zijlstra  *				proxy owner
16921696a8beSPeter Zijlstra  *
16931696a8beSPeter Zijlstra  * @lock:	the rt_mutex to be locked
16941696a8beSPeter Zijlstra  * @proxy_owner:the task to set as owner
16951696a8beSPeter Zijlstra  *
16961696a8beSPeter Zijlstra  * No locking. Caller has to do serializing itself
169784d82ec5SThomas Gleixner  *
169884d82ec5SThomas Gleixner  * Special API call for PI-futex support. This initializes the rtmutex and
169984d82ec5SThomas Gleixner  * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
170084d82ec5SThomas Gleixner  * possible at this point because the pi_state which contains the rtmutex
170184d82ec5SThomas Gleixner  * is not yet visible to other tasks.
17021696a8beSPeter Zijlstra  */
17031696a8beSPeter Zijlstra void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
17041696a8beSPeter Zijlstra 				struct task_struct *proxy_owner)
17051696a8beSPeter Zijlstra {
1706f5694788SPeter Zijlstra 	__rt_mutex_init(lock, NULL, NULL);
17071696a8beSPeter Zijlstra 	debug_rt_mutex_proxy_lock(lock, proxy_owner);
17081696a8beSPeter Zijlstra 	rt_mutex_set_owner(lock, proxy_owner);
17091696a8beSPeter Zijlstra }
17101696a8beSPeter Zijlstra 
17111696a8beSPeter Zijlstra /**
17121696a8beSPeter Zijlstra  * rt_mutex_proxy_unlock - release a lock on behalf of owner
17131696a8beSPeter Zijlstra  *
17141696a8beSPeter Zijlstra  * @lock:	the rt_mutex to be locked
17151696a8beSPeter Zijlstra  *
17161696a8beSPeter Zijlstra  * No locking. Caller has to do serializing itself
171784d82ec5SThomas Gleixner  *
171884d82ec5SThomas Gleixner  * Special API call for PI-futex support. This merrily cleans up the rtmutex
171984d82ec5SThomas Gleixner  * (debugging) state. Concurrent operations on this rt_mutex are not
172084d82ec5SThomas Gleixner  * possible because it belongs to the pi_state which is about to be freed
172184d82ec5SThomas Gleixner  * and it is not longer visible to other tasks.
17221696a8beSPeter Zijlstra  */
17231696a8beSPeter Zijlstra void rt_mutex_proxy_unlock(struct rt_mutex *lock,
17241696a8beSPeter Zijlstra 			   struct task_struct *proxy_owner)
17251696a8beSPeter Zijlstra {
17261696a8beSPeter Zijlstra 	debug_rt_mutex_proxy_unlock(lock);
17271696a8beSPeter Zijlstra 	rt_mutex_set_owner(lock, NULL);
17281696a8beSPeter Zijlstra }
17291696a8beSPeter Zijlstra 
17301a1fb985SThomas Gleixner /**
17311a1fb985SThomas Gleixner  * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
17321a1fb985SThomas Gleixner  * @lock:		the rt_mutex to take
17331a1fb985SThomas Gleixner  * @waiter:		the pre-initialized rt_mutex_waiter
17341a1fb985SThomas Gleixner  * @task:		the task to prepare
17351a1fb985SThomas Gleixner  *
17361a1fb985SThomas Gleixner  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
17371a1fb985SThomas Gleixner  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
17381a1fb985SThomas Gleixner  *
17391a1fb985SThomas Gleixner  * NOTE: does _NOT_ remove the @waiter on failure; must either call
17401a1fb985SThomas Gleixner  * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
17411a1fb985SThomas Gleixner  *
17421a1fb985SThomas Gleixner  * Returns:
17431a1fb985SThomas Gleixner  *  0 - task blocked on lock
17441a1fb985SThomas Gleixner  *  1 - acquired the lock for task, caller should wake it up
17451a1fb985SThomas Gleixner  * <0 - error
17461a1fb985SThomas Gleixner  *
17471a1fb985SThomas Gleixner  * Special API call for PI-futex support.
17481a1fb985SThomas Gleixner  */
174956222b21SPeter Zijlstra int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
175056222b21SPeter Zijlstra 			      struct rt_mutex_waiter *waiter,
175156222b21SPeter Zijlstra 			      struct task_struct *task)
175256222b21SPeter Zijlstra {
175356222b21SPeter Zijlstra 	int ret;
175456222b21SPeter Zijlstra 
17551a1fb985SThomas Gleixner 	lockdep_assert_held(&lock->wait_lock);
17561a1fb985SThomas Gleixner 
175756222b21SPeter Zijlstra 	if (try_to_take_rt_mutex(lock, task, NULL))
175856222b21SPeter Zijlstra 		return 1;
175956222b21SPeter Zijlstra 
176056222b21SPeter Zijlstra 	/* We enforce deadlock detection for futexes */
176156222b21SPeter Zijlstra 	ret = task_blocks_on_rt_mutex(lock, waiter, task,
176256222b21SPeter Zijlstra 				      RT_MUTEX_FULL_CHAINWALK);
176356222b21SPeter Zijlstra 
176456222b21SPeter Zijlstra 	if (ret && !rt_mutex_owner(lock)) {
176556222b21SPeter Zijlstra 		/*
176656222b21SPeter Zijlstra 		 * Reset the return value. We might have
176756222b21SPeter Zijlstra 		 * returned with -EDEADLK and the owner
176856222b21SPeter Zijlstra 		 * released the lock while we were walking the
176956222b21SPeter Zijlstra 		 * pi chain.  Let the waiter sort it out.
177056222b21SPeter Zijlstra 		 */
177156222b21SPeter Zijlstra 		ret = 0;
177256222b21SPeter Zijlstra 	}
177356222b21SPeter Zijlstra 
177456222b21SPeter Zijlstra 	debug_rt_mutex_print_deadlock(waiter);
177556222b21SPeter Zijlstra 
177656222b21SPeter Zijlstra 	return ret;
177756222b21SPeter Zijlstra }
177856222b21SPeter Zijlstra 
17791696a8beSPeter Zijlstra /**
17801696a8beSPeter Zijlstra  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
17811696a8beSPeter Zijlstra  * @lock:		the rt_mutex to take
17821696a8beSPeter Zijlstra  * @waiter:		the pre-initialized rt_mutex_waiter
17831696a8beSPeter Zijlstra  * @task:		the task to prepare
17841696a8beSPeter Zijlstra  *
17851a1fb985SThomas Gleixner  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
17861a1fb985SThomas Gleixner  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
17871a1fb985SThomas Gleixner  *
17881a1fb985SThomas Gleixner  * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
17891a1fb985SThomas Gleixner  * on failure.
17901a1fb985SThomas Gleixner  *
17911696a8beSPeter Zijlstra  * Returns:
17921696a8beSPeter Zijlstra  *  0 - task blocked on lock
17931696a8beSPeter Zijlstra  *  1 - acquired the lock for task, caller should wake it up
17941696a8beSPeter Zijlstra  * <0 - error
17951696a8beSPeter Zijlstra  *
17961a1fb985SThomas Gleixner  * Special API call for PI-futex support.
17971696a8beSPeter Zijlstra  */
17981696a8beSPeter Zijlstra int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
17991696a8beSPeter Zijlstra 			      struct rt_mutex_waiter *waiter,
1800c051b21fSThomas Gleixner 			      struct task_struct *task)
18011696a8beSPeter Zijlstra {
18021696a8beSPeter Zijlstra 	int ret;
18031696a8beSPeter Zijlstra 
1804b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
180556222b21SPeter Zijlstra 	ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
18061a1fb985SThomas Gleixner 	if (unlikely(ret))
18071a1fb985SThomas Gleixner 		remove_waiter(lock, waiter);
1808b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
18091696a8beSPeter Zijlstra 
18101696a8beSPeter Zijlstra 	return ret;
18111696a8beSPeter Zijlstra }
18121696a8beSPeter Zijlstra 
18131696a8beSPeter Zijlstra /**
18141696a8beSPeter Zijlstra  * rt_mutex_next_owner - return the next owner of the lock
18151696a8beSPeter Zijlstra  *
18161696a8beSPeter Zijlstra  * @lock: the rt lock query
18171696a8beSPeter Zijlstra  *
18181696a8beSPeter Zijlstra  * Returns the next owner of the lock or NULL
18191696a8beSPeter Zijlstra  *
18201696a8beSPeter Zijlstra  * Caller has to serialize against other accessors to the lock
18211696a8beSPeter Zijlstra  * itself.
18221696a8beSPeter Zijlstra  *
18231696a8beSPeter Zijlstra  * Special API call for PI-futex support
18241696a8beSPeter Zijlstra  */
18251696a8beSPeter Zijlstra struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
18261696a8beSPeter Zijlstra {
18271696a8beSPeter Zijlstra 	if (!rt_mutex_has_waiters(lock))
18281696a8beSPeter Zijlstra 		return NULL;
18291696a8beSPeter Zijlstra 
18301696a8beSPeter Zijlstra 	return rt_mutex_top_waiter(lock)->task;
18311696a8beSPeter Zijlstra }
18321696a8beSPeter Zijlstra 
18331696a8beSPeter Zijlstra /**
183438d589f2SPeter Zijlstra  * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
18351696a8beSPeter Zijlstra  * @lock:		the rt_mutex we were woken on
18361696a8beSPeter Zijlstra  * @to:			the timeout, null if none. hrtimer should already have
18371696a8beSPeter Zijlstra  *			been started.
18381696a8beSPeter Zijlstra  * @waiter:		the pre-initialized rt_mutex_waiter
18391696a8beSPeter Zijlstra  *
184038d589f2SPeter Zijlstra  * Wait for the the lock acquisition started on our behalf by
184138d589f2SPeter Zijlstra  * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
184238d589f2SPeter Zijlstra  * rt_mutex_cleanup_proxy_lock().
18431696a8beSPeter Zijlstra  *
18441696a8beSPeter Zijlstra  * Returns:
18451696a8beSPeter Zijlstra  *  0 - success
1846c051b21fSThomas Gleixner  * <0 - error, one of -EINTR, -ETIMEDOUT
18471696a8beSPeter Zijlstra  *
184838d589f2SPeter Zijlstra  * Special API call for PI-futex support
18491696a8beSPeter Zijlstra  */
185038d589f2SPeter Zijlstra int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
18511696a8beSPeter Zijlstra 			       struct hrtimer_sleeper *to,
1852c051b21fSThomas Gleixner 			       struct rt_mutex_waiter *waiter)
18531696a8beSPeter Zijlstra {
18541696a8beSPeter Zijlstra 	int ret;
18551696a8beSPeter Zijlstra 
1856b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
1857afffc6c1SDavidlohr Bueso 	/* sleep on the mutex */
185804dc1b2fSPeter Zijlstra 	set_current_state(TASK_INTERRUPTIBLE);
18591696a8beSPeter Zijlstra 	ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
186004dc1b2fSPeter Zijlstra 	/*
186104dc1b2fSPeter Zijlstra 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
186204dc1b2fSPeter Zijlstra 	 * have to fix that up.
186304dc1b2fSPeter Zijlstra 	 */
186404dc1b2fSPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
1865b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
18661696a8beSPeter Zijlstra 
18671696a8beSPeter Zijlstra 	return ret;
18681696a8beSPeter Zijlstra }
186938d589f2SPeter Zijlstra 
187038d589f2SPeter Zijlstra /**
187138d589f2SPeter Zijlstra  * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
187238d589f2SPeter Zijlstra  * @lock:		the rt_mutex we were woken on
187338d589f2SPeter Zijlstra  * @waiter:		the pre-initialized rt_mutex_waiter
187438d589f2SPeter Zijlstra  *
18751a1fb985SThomas Gleixner  * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
18761a1fb985SThomas Gleixner  * rt_mutex_wait_proxy_lock().
187738d589f2SPeter Zijlstra  *
187838d589f2SPeter Zijlstra  * Unless we acquired the lock; we're still enqueued on the wait-list and can
187938d589f2SPeter Zijlstra  * in fact still be granted ownership until we're removed. Therefore we can
188038d589f2SPeter Zijlstra  * find we are in fact the owner and must disregard the
188138d589f2SPeter Zijlstra  * rt_mutex_wait_proxy_lock() failure.
188238d589f2SPeter Zijlstra  *
188338d589f2SPeter Zijlstra  * Returns:
188438d589f2SPeter Zijlstra  *  true  - did the cleanup, we done.
188538d589f2SPeter Zijlstra  *  false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
188638d589f2SPeter Zijlstra  *          caller should disregards its return value.
188738d589f2SPeter Zijlstra  *
188838d589f2SPeter Zijlstra  * Special API call for PI-futex support
188938d589f2SPeter Zijlstra  */
189038d589f2SPeter Zijlstra bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
189138d589f2SPeter Zijlstra 				 struct rt_mutex_waiter *waiter)
189238d589f2SPeter Zijlstra {
189338d589f2SPeter Zijlstra 	bool cleanup = false;
189438d589f2SPeter Zijlstra 
189538d589f2SPeter Zijlstra 	raw_spin_lock_irq(&lock->wait_lock);
189638d589f2SPeter Zijlstra 	/*
189704dc1b2fSPeter Zijlstra 	 * Do an unconditional try-lock, this deals with the lock stealing
189804dc1b2fSPeter Zijlstra 	 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
189904dc1b2fSPeter Zijlstra 	 * sets a NULL owner.
190004dc1b2fSPeter Zijlstra 	 *
190104dc1b2fSPeter Zijlstra 	 * We're not interested in the return value, because the subsequent
190204dc1b2fSPeter Zijlstra 	 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
190304dc1b2fSPeter Zijlstra 	 * we will own the lock and it will have removed the waiter. If we
190404dc1b2fSPeter Zijlstra 	 * failed the trylock, we're still not owner and we need to remove
190504dc1b2fSPeter Zijlstra 	 * ourselves.
190604dc1b2fSPeter Zijlstra 	 */
190704dc1b2fSPeter Zijlstra 	try_to_take_rt_mutex(lock, current, waiter);
190804dc1b2fSPeter Zijlstra 	/*
190938d589f2SPeter Zijlstra 	 * Unless we're the owner; we're still enqueued on the wait_list.
191038d589f2SPeter Zijlstra 	 * So check if we became owner, if not, take us off the wait_list.
191138d589f2SPeter Zijlstra 	 */
191238d589f2SPeter Zijlstra 	if (rt_mutex_owner(lock) != current) {
191338d589f2SPeter Zijlstra 		remove_waiter(lock, waiter);
191438d589f2SPeter Zijlstra 		cleanup = true;
191538d589f2SPeter Zijlstra 	}
1916cfafcd11SPeter Zijlstra 	/*
1917cfafcd11SPeter Zijlstra 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1918cfafcd11SPeter Zijlstra 	 * have to fix that up.
1919cfafcd11SPeter Zijlstra 	 */
1920cfafcd11SPeter Zijlstra 	fixup_rt_mutex_waiters(lock);
1921cfafcd11SPeter Zijlstra 
192238d589f2SPeter Zijlstra 	raw_spin_unlock_irq(&lock->wait_lock);
192338d589f2SPeter Zijlstra 
192438d589f2SPeter Zijlstra 	return cleanup;
192538d589f2SPeter Zijlstra }
1926