xref: /linux-6.15/kernel/locking/rtmutex.c (revision b76b44fb)
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 <[email protected]>
81696a8beSPeter Zijlstra  *  Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <[email protected]>
91696a8beSPeter Zijlstra  *  Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
101696a8beSPeter Zijlstra  *  Copyright (C) 2006 Esben Nielsen
11992caf7fSSteven Rostedt  * Adaptive Spinlocks:
12992caf7fSSteven Rostedt  *  Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
13992caf7fSSteven Rostedt  *				     and Peter Morreale,
14992caf7fSSteven Rostedt  * Adaptive Spinlocks simplification:
15992caf7fSSteven Rostedt  *  Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <[email protected]>
161696a8beSPeter Zijlstra  *
17387b1468SMauro Carvalho Chehab  *  See Documentation/locking/rt-mutex-design.rst for details.
181696a8beSPeter Zijlstra  */
19531ae4b0SThomas Gleixner #include <linux/sched.h>
20531ae4b0SThomas Gleixner #include <linux/sched/debug.h>
21531ae4b0SThomas Gleixner #include <linux/sched/deadline.h>
22174cd4b1SIngo Molnar #include <linux/sched/signal.h>
231696a8beSPeter Zijlstra #include <linux/sched/rt.h>
2484f001e1SIngo Molnar #include <linux/sched/wake_q.h>
25add46132SPeter Zijlstra #include <linux/ww_mutex.h>
261696a8beSPeter Zijlstra 
27ee042be1SNamhyung Kim #include <trace/events/lock.h>
28ee042be1SNamhyung Kim 
291696a8beSPeter Zijlstra #include "rtmutex_common.h"
30*b76b44fbSWaiman Long #include "lock_events.h"
311696a8beSPeter Zijlstra 
32add46132SPeter Zijlstra #ifndef WW_RT
33add46132SPeter Zijlstra # define build_ww_mutex()	(false)
34add46132SPeter Zijlstra # define ww_container_of(rtm)	NULL
35add46132SPeter Zijlstra 
__ww_mutex_add_waiter(struct rt_mutex_waiter * waiter,struct rt_mutex * lock,struct ww_acquire_ctx * ww_ctx,struct wake_q_head * wake_q)36add46132SPeter Zijlstra static inline int __ww_mutex_add_waiter(struct rt_mutex_waiter *waiter,
37add46132SPeter Zijlstra 					struct rt_mutex *lock,
38894d1b3dSPeter Zijlstra 					struct ww_acquire_ctx *ww_ctx,
39894d1b3dSPeter Zijlstra 					struct wake_q_head *wake_q)
40add46132SPeter Zijlstra {
41add46132SPeter Zijlstra 	return 0;
42add46132SPeter Zijlstra }
43add46132SPeter Zijlstra 
__ww_mutex_check_waiters(struct rt_mutex * lock,struct ww_acquire_ctx * ww_ctx,struct wake_q_head * wake_q)44add46132SPeter Zijlstra static inline void __ww_mutex_check_waiters(struct rt_mutex *lock,
45894d1b3dSPeter Zijlstra 					    struct ww_acquire_ctx *ww_ctx,
46894d1b3dSPeter Zijlstra 					    struct wake_q_head *wake_q)
47add46132SPeter Zijlstra {
48add46132SPeter Zijlstra }
49add46132SPeter Zijlstra 
ww_mutex_lock_acquired(struct ww_mutex * lock,struct ww_acquire_ctx * ww_ctx)50add46132SPeter Zijlstra static inline void ww_mutex_lock_acquired(struct ww_mutex *lock,
51add46132SPeter Zijlstra 					  struct ww_acquire_ctx *ww_ctx)
52add46132SPeter Zijlstra {
53add46132SPeter Zijlstra }
54add46132SPeter Zijlstra 
__ww_mutex_check_kill(struct rt_mutex * lock,struct rt_mutex_waiter * waiter,struct ww_acquire_ctx * ww_ctx)55add46132SPeter Zijlstra static inline int __ww_mutex_check_kill(struct rt_mutex *lock,
56add46132SPeter Zijlstra 					struct rt_mutex_waiter *waiter,
57add46132SPeter Zijlstra 					struct ww_acquire_ctx *ww_ctx)
58add46132SPeter Zijlstra {
59add46132SPeter Zijlstra 	return 0;
60add46132SPeter Zijlstra }
61add46132SPeter Zijlstra 
62add46132SPeter Zijlstra #else
63add46132SPeter Zijlstra # define build_ww_mutex()	(true)
64add46132SPeter Zijlstra # define ww_container_of(rtm)	container_of(rtm, struct ww_mutex, base)
65add46132SPeter Zijlstra # include "ww_mutex.h"
66add46132SPeter Zijlstra #endif
67add46132SPeter Zijlstra 
681696a8beSPeter Zijlstra /*
691696a8beSPeter Zijlstra  * lock->owner state tracking:
701696a8beSPeter Zijlstra  *
711696a8beSPeter Zijlstra  * lock->owner holds the task_struct pointer of the owner. Bit 0
721696a8beSPeter Zijlstra  * is used to keep track of the "lock has waiters" state.
731696a8beSPeter Zijlstra  *
741696a8beSPeter Zijlstra  * owner	bit0
751696a8beSPeter Zijlstra  * NULL		0	lock is free (fast acquire possible)
761696a8beSPeter Zijlstra  * NULL		1	lock is free and has waiters and the top waiter
771696a8beSPeter Zijlstra  *				is going to take the lock*
781696a8beSPeter Zijlstra  * taskpointer	0	lock is held (fast release possible)
791696a8beSPeter Zijlstra  * taskpointer	1	lock is held and has waiters**
801696a8beSPeter Zijlstra  *
811696a8beSPeter Zijlstra  * The fast atomic compare exchange based acquire and release is only
821696a8beSPeter Zijlstra  * possible when bit 0 of lock->owner is 0.
831696a8beSPeter Zijlstra  *
841696a8beSPeter Zijlstra  * (*) It also can be a transitional state when grabbing the lock
851696a8beSPeter Zijlstra  * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
861696a8beSPeter Zijlstra  * we need to set the bit0 before looking at the lock, and the owner may be
871696a8beSPeter Zijlstra  * NULL in this small time, hence this can be a transitional state.
881696a8beSPeter Zijlstra  *
891696a8beSPeter Zijlstra  * (**) There is a small time when bit 0 is set but there are no
901696a8beSPeter Zijlstra  * waiters. This can happen when grabbing the lock in the slow path.
911696a8beSPeter Zijlstra  * To prevent a cmpxchg of the owner releasing the lock, we need to
921696a8beSPeter Zijlstra  * set this bit before looking at the lock.
931696a8beSPeter Zijlstra  */
941696a8beSPeter Zijlstra 
951c0908d8SMel Gorman static __always_inline struct task_struct *
rt_mutex_owner_encode(struct rt_mutex_base * lock,struct task_struct * owner)961c0908d8SMel Gorman rt_mutex_owner_encode(struct rt_mutex_base *lock, struct task_struct *owner)
971696a8beSPeter Zijlstra {
981696a8beSPeter Zijlstra 	unsigned long val = (unsigned long)owner;
991696a8beSPeter Zijlstra 
1001696a8beSPeter Zijlstra 	if (rt_mutex_has_waiters(lock))
1011696a8beSPeter Zijlstra 		val |= RT_MUTEX_HAS_WAITERS;
1021696a8beSPeter Zijlstra 
1031c0908d8SMel Gorman 	return (struct task_struct *)val;
1041c0908d8SMel Gorman }
1051c0908d8SMel Gorman 
1061c0908d8SMel Gorman static __always_inline void
rt_mutex_set_owner(struct rt_mutex_base * lock,struct task_struct * owner)1071c0908d8SMel Gorman rt_mutex_set_owner(struct rt_mutex_base *lock, struct task_struct *owner)
1081c0908d8SMel Gorman {
1091c0908d8SMel Gorman 	/*
1101c0908d8SMel Gorman 	 * lock->wait_lock is held but explicit acquire semantics are needed
1111c0908d8SMel Gorman 	 * for a new lock owner so WRITE_ONCE is insufficient.
1121c0908d8SMel Gorman 	 */
1131c0908d8SMel Gorman 	xchg_acquire(&lock->owner, rt_mutex_owner_encode(lock, owner));
1141c0908d8SMel Gorman }
1151c0908d8SMel Gorman 
rt_mutex_clear_owner(struct rt_mutex_base * lock)1161c0908d8SMel Gorman static __always_inline void rt_mutex_clear_owner(struct rt_mutex_base *lock)
1171c0908d8SMel Gorman {
1181c0908d8SMel Gorman 	/* lock->wait_lock is held so the unlock provides release semantics. */
1191c0908d8SMel Gorman 	WRITE_ONCE(lock->owner, rt_mutex_owner_encode(lock, NULL));
1201696a8beSPeter Zijlstra }
1211696a8beSPeter Zijlstra 
clear_rt_mutex_waiters(struct rt_mutex_base * lock)122830e6accSPeter Zijlstra static __always_inline void clear_rt_mutex_waiters(struct rt_mutex_base *lock)
1231696a8beSPeter Zijlstra {
1241696a8beSPeter Zijlstra 	lock->owner = (struct task_struct *)
1251696a8beSPeter Zijlstra 			((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
1261696a8beSPeter Zijlstra }
1271696a8beSPeter Zijlstra 
1281c0908d8SMel Gorman static __always_inline void
fixup_rt_mutex_waiters(struct rt_mutex_base * lock,bool acquire_lock)1291c0908d8SMel Gorman fixup_rt_mutex_waiters(struct rt_mutex_base *lock, bool acquire_lock)
1301696a8beSPeter Zijlstra {
131dbb26055SThomas Gleixner 	unsigned long owner, *p = (unsigned long *) &lock->owner;
132dbb26055SThomas Gleixner 
133dbb26055SThomas Gleixner 	if (rt_mutex_has_waiters(lock))
134dbb26055SThomas Gleixner 		return;
135dbb26055SThomas Gleixner 
136dbb26055SThomas Gleixner 	/*
137dbb26055SThomas Gleixner 	 * The rbtree has no waiters enqueued, now make sure that the
138dbb26055SThomas Gleixner 	 * lock->owner still has the waiters bit set, otherwise the
139dbb26055SThomas Gleixner 	 * following can happen:
140dbb26055SThomas Gleixner 	 *
141dbb26055SThomas Gleixner 	 * CPU 0	CPU 1		CPU2
142dbb26055SThomas Gleixner 	 * l->owner=T1
143dbb26055SThomas Gleixner 	 *		rt_mutex_lock(l)
144dbb26055SThomas Gleixner 	 *		lock(l->lock)
145dbb26055SThomas Gleixner 	 *		l->owner = T1 | HAS_WAITERS;
146dbb26055SThomas Gleixner 	 *		enqueue(T2)
147dbb26055SThomas Gleixner 	 *		boost()
148dbb26055SThomas Gleixner 	 *		  unlock(l->lock)
149dbb26055SThomas Gleixner 	 *		block()
150dbb26055SThomas Gleixner 	 *
151dbb26055SThomas Gleixner 	 *				rt_mutex_lock(l)
152dbb26055SThomas Gleixner 	 *				lock(l->lock)
153dbb26055SThomas Gleixner 	 *				l->owner = T1 | HAS_WAITERS;
154dbb26055SThomas Gleixner 	 *				enqueue(T3)
155dbb26055SThomas Gleixner 	 *				boost()
156dbb26055SThomas Gleixner 	 *				  unlock(l->lock)
157dbb26055SThomas Gleixner 	 *				block()
158dbb26055SThomas Gleixner 	 *		signal(->T2)	signal(->T3)
159dbb26055SThomas Gleixner 	 *		lock(l->lock)
160dbb26055SThomas Gleixner 	 *		dequeue(T2)
161dbb26055SThomas Gleixner 	 *		deboost()
162dbb26055SThomas Gleixner 	 *		  unlock(l->lock)
163dbb26055SThomas Gleixner 	 *				lock(l->lock)
164dbb26055SThomas Gleixner 	 *				dequeue(T3)
165dbb26055SThomas Gleixner 	 *				 ==> wait list is empty
166dbb26055SThomas Gleixner 	 *				deboost()
167dbb26055SThomas Gleixner 	 *				 unlock(l->lock)
168dbb26055SThomas Gleixner 	 *		lock(l->lock)
169dbb26055SThomas Gleixner 	 *		fixup_rt_mutex_waiters()
170dbb26055SThomas Gleixner 	 *		  if (wait_list_empty(l) {
171dbb26055SThomas Gleixner 	 *		    l->owner = owner
172dbb26055SThomas Gleixner 	 *		    owner = l->owner & ~HAS_WAITERS;
173dbb26055SThomas Gleixner 	 *		      ==> l->owner = T1
174dbb26055SThomas Gleixner 	 *		  }
175dbb26055SThomas Gleixner 	 *				lock(l->lock)
176dbb26055SThomas Gleixner 	 * rt_mutex_unlock(l)		fixup_rt_mutex_waiters()
177dbb26055SThomas Gleixner 	 *				  if (wait_list_empty(l) {
178dbb26055SThomas Gleixner 	 *				    owner = l->owner & ~HAS_WAITERS;
179dbb26055SThomas Gleixner 	 * cmpxchg(l->owner, T1, NULL)
180dbb26055SThomas Gleixner 	 *  ===> Success (l->owner = NULL)
181dbb26055SThomas Gleixner 	 *
182dbb26055SThomas Gleixner 	 *				    l->owner = owner
183dbb26055SThomas Gleixner 	 *				      ==> l->owner = T1
184dbb26055SThomas Gleixner 	 *				  }
185dbb26055SThomas Gleixner 	 *
186dbb26055SThomas Gleixner 	 * With the check for the waiter bit in place T3 on CPU2 will not
187dbb26055SThomas Gleixner 	 * overwrite. All tasks fiddling with the waiters bit are
188dbb26055SThomas Gleixner 	 * serialized by l->lock, so nothing else can modify the waiters
189dbb26055SThomas Gleixner 	 * bit. If the bit is set then nothing can change l->owner either
190dbb26055SThomas Gleixner 	 * so the simple RMW is safe. The cmpxchg() will simply fail if it
191dbb26055SThomas Gleixner 	 * happens in the middle of the RMW because the waiters bit is
192dbb26055SThomas Gleixner 	 * still set.
193dbb26055SThomas Gleixner 	 */
194dbb26055SThomas Gleixner 	owner = READ_ONCE(*p);
1951c0908d8SMel Gorman 	if (owner & RT_MUTEX_HAS_WAITERS) {
1961c0908d8SMel Gorman 		/*
1971c0908d8SMel Gorman 		 * See rt_mutex_set_owner() and rt_mutex_clear_owner() on
1981c0908d8SMel Gorman 		 * why xchg_acquire() is used for updating owner for
1991c0908d8SMel Gorman 		 * locking and WRITE_ONCE() for unlocking.
2001c0908d8SMel Gorman 		 *
2011c0908d8SMel Gorman 		 * WRITE_ONCE() would work for the acquire case too, but
2021c0908d8SMel Gorman 		 * in case that the lock acquisition failed it might
2031c0908d8SMel Gorman 		 * force other lockers into the slow path unnecessarily.
2041c0908d8SMel Gorman 		 */
2051c0908d8SMel Gorman 		if (acquire_lock)
2061c0908d8SMel Gorman 			xchg_acquire(p, owner & ~RT_MUTEX_HAS_WAITERS);
2071c0908d8SMel Gorman 		else
208dbb26055SThomas Gleixner 			WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
2091696a8beSPeter Zijlstra 	}
2101c0908d8SMel Gorman }
2111696a8beSPeter Zijlstra 
2121696a8beSPeter Zijlstra /*
213cede8841SSebastian Andrzej Siewior  * We can speed up the acquire/release, if there's no debugging state to be
214cede8841SSebastian Andrzej Siewior  * set up.
2151696a8beSPeter Zijlstra  */
216cede8841SSebastian Andrzej Siewior #ifndef CONFIG_DEBUG_RT_MUTEXES
rt_mutex_cmpxchg_acquire(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)217830e6accSPeter Zijlstra static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
21878515930SSebastian Andrzej Siewior 						     struct task_struct *old,
21978515930SSebastian Andrzej Siewior 						     struct task_struct *new)
22078515930SSebastian Andrzej Siewior {
221709e0b62SThomas Gleixner 	return try_cmpxchg_acquire(&lock->owner, &old, new);
22278515930SSebastian Andrzej Siewior }
22378515930SSebastian Andrzej Siewior 
rt_mutex_try_acquire(struct rt_mutex_base * lock)224af9f0063SSebastian Andrzej Siewior static __always_inline bool rt_mutex_try_acquire(struct rt_mutex_base *lock)
225af9f0063SSebastian Andrzej Siewior {
226af9f0063SSebastian Andrzej Siewior 	return rt_mutex_cmpxchg_acquire(lock, NULL, current);
227af9f0063SSebastian Andrzej Siewior }
228af9f0063SSebastian Andrzej Siewior 
rt_mutex_cmpxchg_release(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)229830e6accSPeter Zijlstra static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
23078515930SSebastian Andrzej Siewior 						     struct task_struct *old,
23178515930SSebastian Andrzej Siewior 						     struct task_struct *new)
23278515930SSebastian Andrzej Siewior {
233709e0b62SThomas Gleixner 	return try_cmpxchg_release(&lock->owner, &old, new);
23478515930SSebastian Andrzej Siewior }
235700318d1SDavidlohr Bueso 
236700318d1SDavidlohr Bueso /*
237700318d1SDavidlohr Bueso  * Callers must hold the ->wait_lock -- which is the whole purpose as we force
238700318d1SDavidlohr Bueso  * all future threads that attempt to [Rmw] the lock to the slowpath. As such
239700318d1SDavidlohr Bueso  * relaxed semantics suffice.
240700318d1SDavidlohr Bueso  */
mark_rt_mutex_waiters(struct rt_mutex_base * lock)241830e6accSPeter Zijlstra static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
2421696a8beSPeter Zijlstra {
243ce3576ebSUros Bizjak 	unsigned long *p = (unsigned long *) &lock->owner;
244ce3576ebSUros Bizjak 	unsigned long owner, new;
2451696a8beSPeter Zijlstra 
246ce3576ebSUros Bizjak 	owner = READ_ONCE(*p);
2471696a8beSPeter Zijlstra 	do {
248ce3576ebSUros Bizjak 		new = owner | RT_MUTEX_HAS_WAITERS;
249ce3576ebSUros Bizjak 	} while (!try_cmpxchg_relaxed(p, &owner, new));
2501c0908d8SMel Gorman 
2511c0908d8SMel Gorman 	/*
2521c0908d8SMel Gorman 	 * The cmpxchg loop above is relaxed to avoid back-to-back ACQUIRE
2531c0908d8SMel Gorman 	 * operations in the event of contention. Ensure the successful
2541c0908d8SMel Gorman 	 * cmpxchg is visible.
2551c0908d8SMel Gorman 	 */
2561c0908d8SMel Gorman 	smp_mb__after_atomic();
2571696a8beSPeter Zijlstra }
25827e35715SThomas Gleixner 
25927e35715SThomas Gleixner /*
26027e35715SThomas Gleixner  * Safe fastpath aware unlock:
26127e35715SThomas Gleixner  * 1) Clear the waiters bit
26227e35715SThomas Gleixner  * 2) Drop lock->wait_lock
26327e35715SThomas Gleixner  * 3) Try to unlock the lock with cmpxchg
26427e35715SThomas Gleixner  */
unlock_rt_mutex_safe(struct rt_mutex_base * lock,unsigned long flags)265830e6accSPeter Zijlstra static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
266b4abf910SThomas Gleixner 						 unsigned long flags)
26727e35715SThomas Gleixner 	__releases(lock->wait_lock)
26827e35715SThomas Gleixner {
26927e35715SThomas Gleixner 	struct task_struct *owner = rt_mutex_owner(lock);
27027e35715SThomas Gleixner 
27127e35715SThomas Gleixner 	clear_rt_mutex_waiters(lock);
272b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
27327e35715SThomas Gleixner 	/*
27427e35715SThomas Gleixner 	 * If a new waiter comes in between the unlock and the cmpxchg
27527e35715SThomas Gleixner 	 * we have two situations:
27627e35715SThomas Gleixner 	 *
27727e35715SThomas Gleixner 	 * unlock(wait_lock);
27827e35715SThomas Gleixner 	 *					lock(wait_lock);
27927e35715SThomas Gleixner 	 * cmpxchg(p, owner, 0) == owner
28027e35715SThomas Gleixner 	 *					mark_rt_mutex_waiters(lock);
28127e35715SThomas Gleixner 	 *					acquire(lock);
28227e35715SThomas Gleixner 	 * or:
28327e35715SThomas Gleixner 	 *
28427e35715SThomas Gleixner 	 * unlock(wait_lock);
28527e35715SThomas Gleixner 	 *					lock(wait_lock);
28627e35715SThomas Gleixner 	 *					mark_rt_mutex_waiters(lock);
28727e35715SThomas Gleixner 	 *
28827e35715SThomas Gleixner 	 * cmpxchg(p, owner, 0) != owner
28927e35715SThomas Gleixner 	 *					enqueue_waiter();
29027e35715SThomas Gleixner 	 *					unlock(wait_lock);
29127e35715SThomas Gleixner 	 * lock(wait_lock);
29227e35715SThomas Gleixner 	 * wake waiter();
29327e35715SThomas Gleixner 	 * unlock(wait_lock);
29427e35715SThomas Gleixner 	 *					lock(wait_lock);
29527e35715SThomas Gleixner 	 *					acquire(lock);
29627e35715SThomas Gleixner 	 */
297700318d1SDavidlohr Bueso 	return rt_mutex_cmpxchg_release(lock, owner, NULL);
29827e35715SThomas Gleixner }
29927e35715SThomas Gleixner 
3001696a8beSPeter Zijlstra #else
rt_mutex_cmpxchg_acquire(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)301830e6accSPeter Zijlstra static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
30278515930SSebastian Andrzej Siewior 						     struct task_struct *old,
30378515930SSebastian Andrzej Siewior 						     struct task_struct *new)
30478515930SSebastian Andrzej Siewior {
30578515930SSebastian Andrzej Siewior 	return false;
30678515930SSebastian Andrzej Siewior 
30778515930SSebastian Andrzej Siewior }
30878515930SSebastian Andrzej Siewior 
309af9f0063SSebastian Andrzej Siewior static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock);
310af9f0063SSebastian Andrzej Siewior 
rt_mutex_try_acquire(struct rt_mutex_base * lock)311af9f0063SSebastian Andrzej Siewior static __always_inline bool rt_mutex_try_acquire(struct rt_mutex_base *lock)
312af9f0063SSebastian Andrzej Siewior {
313af9f0063SSebastian Andrzej Siewior 	/*
314af9f0063SSebastian Andrzej Siewior 	 * With debug enabled rt_mutex_cmpxchg trylock() will always fail.
315af9f0063SSebastian Andrzej Siewior 	 *
316af9f0063SSebastian Andrzej Siewior 	 * Avoid unconditionally taking the slow path by using
317af9f0063SSebastian Andrzej Siewior 	 * rt_mutex_slow_trylock() which is covered by the debug code and can
318af9f0063SSebastian Andrzej Siewior 	 * acquire a non-contended rtmutex.
319af9f0063SSebastian Andrzej Siewior 	 */
320af9f0063SSebastian Andrzej Siewior 	return rt_mutex_slowtrylock(lock);
321af9f0063SSebastian Andrzej Siewior }
322af9f0063SSebastian Andrzej Siewior 
rt_mutex_cmpxchg_release(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)323830e6accSPeter Zijlstra static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
32478515930SSebastian Andrzej Siewior 						     struct task_struct *old,
32578515930SSebastian Andrzej Siewior 						     struct task_struct *new)
32678515930SSebastian Andrzej Siewior {
32778515930SSebastian Andrzej Siewior 	return false;
32878515930SSebastian Andrzej Siewior }
329700318d1SDavidlohr Bueso 
mark_rt_mutex_waiters(struct rt_mutex_base * lock)330830e6accSPeter Zijlstra static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
3311696a8beSPeter Zijlstra {
3321696a8beSPeter Zijlstra 	lock->owner = (struct task_struct *)
3331696a8beSPeter Zijlstra 			((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
3341696a8beSPeter Zijlstra }
33527e35715SThomas Gleixner 
33627e35715SThomas Gleixner /*
33727e35715SThomas Gleixner  * Simple slow path only version: lock->owner is protected by lock->wait_lock.
33827e35715SThomas Gleixner  */
unlock_rt_mutex_safe(struct rt_mutex_base * lock,unsigned long flags)339830e6accSPeter Zijlstra static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
340b4abf910SThomas Gleixner 						 unsigned long flags)
34127e35715SThomas Gleixner 	__releases(lock->wait_lock)
34227e35715SThomas Gleixner {
34327e35715SThomas Gleixner 	lock->owner = NULL;
344b4abf910SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
34527e35715SThomas Gleixner 	return true;
34627e35715SThomas Gleixner }
3471696a8beSPeter Zijlstra #endif
3481696a8beSPeter Zijlstra 
__waiter_prio(struct task_struct * task)349715f7f9eSPeter Zijlstra static __always_inline int __waiter_prio(struct task_struct *task)
350715f7f9eSPeter Zijlstra {
351715f7f9eSPeter Zijlstra 	int prio = task->prio;
352715f7f9eSPeter Zijlstra 
353ae04f69dSQais Yousef 	if (!rt_or_dl_prio(prio))
354715f7f9eSPeter Zijlstra 		return DEFAULT_PRIO;
355715f7f9eSPeter Zijlstra 
356715f7f9eSPeter Zijlstra 	return prio;
357715f7f9eSPeter Zijlstra }
358715f7f9eSPeter Zijlstra 
359f7853c34SPeter Zijlstra /*
360f7853c34SPeter Zijlstra  * Update the waiter->tree copy of the sort keys.
361f7853c34SPeter Zijlstra  */
362715f7f9eSPeter Zijlstra static __always_inline void
waiter_update_prio(struct rt_mutex_waiter * waiter,struct task_struct * task)363715f7f9eSPeter Zijlstra waiter_update_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
364715f7f9eSPeter Zijlstra {
365f7853c34SPeter Zijlstra 	lockdep_assert_held(&waiter->lock->wait_lock);
366f7853c34SPeter Zijlstra 	lockdep_assert(RB_EMPTY_NODE(&waiter->tree.entry));
367f7853c34SPeter Zijlstra 
368f7853c34SPeter Zijlstra 	waiter->tree.prio = __waiter_prio(task);
369f7853c34SPeter Zijlstra 	waiter->tree.deadline = task->dl.deadline;
370715f7f9eSPeter Zijlstra }
371715f7f9eSPeter Zijlstra 
37219830e55SPeter Zijlstra /*
373f7853c34SPeter Zijlstra  * Update the waiter->pi_tree copy of the sort keys (from the tree copy).
37419830e55SPeter Zijlstra  */
375f7853c34SPeter Zijlstra static __always_inline void
waiter_clone_prio(struct rt_mutex_waiter * waiter,struct task_struct * task)376f7853c34SPeter Zijlstra waiter_clone_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
377f7853c34SPeter Zijlstra {
378f7853c34SPeter Zijlstra 	lockdep_assert_held(&waiter->lock->wait_lock);
379f7853c34SPeter Zijlstra 	lockdep_assert_held(&task->pi_lock);
380f7853c34SPeter Zijlstra 	lockdep_assert(RB_EMPTY_NODE(&waiter->pi_tree.entry));
38119830e55SPeter Zijlstra 
382f7853c34SPeter Zijlstra 	waiter->pi_tree.prio = waiter->tree.prio;
383f7853c34SPeter Zijlstra 	waiter->pi_tree.deadline = waiter->tree.deadline;
384f7853c34SPeter Zijlstra }
385f7853c34SPeter Zijlstra 
386f7853c34SPeter Zijlstra /*
387f7853c34SPeter Zijlstra  * Only use with rt_waiter_node_{less,equal}()
388f7853c34SPeter Zijlstra  */
389f7853c34SPeter Zijlstra #define task_to_waiter_node(p)	\
390f7853c34SPeter Zijlstra 	&(struct rt_waiter_node){ .prio = __waiter_prio(p), .deadline = (p)->dl.deadline }
391f7853c34SPeter Zijlstra #define task_to_waiter(p)	\
392f7853c34SPeter Zijlstra 	&(struct rt_mutex_waiter){ .tree = *task_to_waiter_node(p) }
393f7853c34SPeter Zijlstra 
rt_waiter_node_less(struct rt_waiter_node * left,struct rt_waiter_node * right)394f7853c34SPeter Zijlstra static __always_inline int rt_waiter_node_less(struct rt_waiter_node *left,
395f7853c34SPeter Zijlstra 					       struct rt_waiter_node *right)
396fb00aca4SPeter Zijlstra {
3972d3d891dSDario Faggioli 	if (left->prio < right->prio)
398fb00aca4SPeter Zijlstra 		return 1;
399fb00aca4SPeter Zijlstra 
4001696a8beSPeter Zijlstra 	/*
4012d3d891dSDario Faggioli 	 * If both waiters have dl_prio(), we check the deadlines of the
4022d3d891dSDario Faggioli 	 * associated tasks.
4032d3d891dSDario Faggioli 	 * If left waiter has a dl_prio(), and we didn't return 1 above,
4042d3d891dSDario Faggioli 	 * then right waiter has a dl_prio() too.
405fb00aca4SPeter Zijlstra 	 */
4062d3d891dSDario Faggioli 	if (dl_prio(left->prio))
407e0aad5b4SPeter Zijlstra 		return dl_time_before(left->deadline, right->deadline);
408fb00aca4SPeter Zijlstra 
409fb00aca4SPeter Zijlstra 	return 0;
410fb00aca4SPeter Zijlstra }
411fb00aca4SPeter Zijlstra 
rt_waiter_node_equal(struct rt_waiter_node * left,struct rt_waiter_node * right)412f7853c34SPeter Zijlstra static __always_inline int rt_waiter_node_equal(struct rt_waiter_node *left,
413f7853c34SPeter Zijlstra 						 struct rt_waiter_node *right)
41419830e55SPeter Zijlstra {
41519830e55SPeter Zijlstra 	if (left->prio != right->prio)
41619830e55SPeter Zijlstra 		return 0;
41719830e55SPeter Zijlstra 
41819830e55SPeter Zijlstra 	/*
41919830e55SPeter Zijlstra 	 * If both waiters have dl_prio(), we check the deadlines of the
42019830e55SPeter Zijlstra 	 * associated tasks.
42119830e55SPeter Zijlstra 	 * If left waiter has a dl_prio(), and we didn't return 0 above,
42219830e55SPeter Zijlstra 	 * then right waiter has a dl_prio() too.
42319830e55SPeter Zijlstra 	 */
42419830e55SPeter Zijlstra 	if (dl_prio(left->prio))
42519830e55SPeter Zijlstra 		return left->deadline == right->deadline;
42619830e55SPeter Zijlstra 
42719830e55SPeter Zijlstra 	return 1;
42819830e55SPeter Zijlstra }
42919830e55SPeter Zijlstra 
rt_mutex_steal(struct rt_mutex_waiter * waiter,struct rt_mutex_waiter * top_waiter)43048eb3f4fSGregory Haskins static inline bool rt_mutex_steal(struct rt_mutex_waiter *waiter,
43148eb3f4fSGregory Haskins 				  struct rt_mutex_waiter *top_waiter)
43248eb3f4fSGregory Haskins {
433f7853c34SPeter Zijlstra 	if (rt_waiter_node_less(&waiter->tree, &top_waiter->tree))
43448eb3f4fSGregory Haskins 		return true;
43548eb3f4fSGregory Haskins 
43648eb3f4fSGregory Haskins #ifdef RT_MUTEX_BUILD_SPINLOCKS
43748eb3f4fSGregory Haskins 	/*
43848eb3f4fSGregory Haskins 	 * Note that RT tasks are excluded from same priority (lateral)
43948eb3f4fSGregory Haskins 	 * steals to prevent the introduction of an unbounded latency.
44048eb3f4fSGregory Haskins 	 */
441ae04f69dSQais Yousef 	if (rt_or_dl_prio(waiter->tree.prio))
44248eb3f4fSGregory Haskins 		return false;
44348eb3f4fSGregory Haskins 
444f7853c34SPeter Zijlstra 	return rt_waiter_node_equal(&waiter->tree, &top_waiter->tree);
44548eb3f4fSGregory Haskins #else
44648eb3f4fSGregory Haskins 	return false;
44748eb3f4fSGregory Haskins #endif
44848eb3f4fSGregory Haskins }
44948eb3f4fSGregory Haskins 
4505a798725SPeter Zijlstra #define __node_2_waiter(node) \
451f7853c34SPeter Zijlstra 	rb_entry((node), struct rt_mutex_waiter, tree.entry)
4525a798725SPeter Zijlstra 
__waiter_less(struct rb_node * a,const struct rb_node * b)453d7a2edb8SThomas Gleixner static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
4545a798725SPeter Zijlstra {
455add46132SPeter Zijlstra 	struct rt_mutex_waiter *aw = __node_2_waiter(a);
456add46132SPeter Zijlstra 	struct rt_mutex_waiter *bw = __node_2_waiter(b);
457add46132SPeter Zijlstra 
458f7853c34SPeter Zijlstra 	if (rt_waiter_node_less(&aw->tree, &bw->tree))
459add46132SPeter Zijlstra 		return 1;
460add46132SPeter Zijlstra 
461add46132SPeter Zijlstra 	if (!build_ww_mutex())
462add46132SPeter Zijlstra 		return 0;
463add46132SPeter Zijlstra 
464f7853c34SPeter Zijlstra 	if (rt_waiter_node_less(&bw->tree, &aw->tree))
465add46132SPeter Zijlstra 		return 0;
466add46132SPeter Zijlstra 
467add46132SPeter Zijlstra 	/* NOTE: relies on waiter->ww_ctx being set before insertion */
468add46132SPeter Zijlstra 	if (aw->ww_ctx) {
469add46132SPeter Zijlstra 		if (!bw->ww_ctx)
470add46132SPeter Zijlstra 			return 1;
471add46132SPeter Zijlstra 
472add46132SPeter Zijlstra 		return (signed long)(aw->ww_ctx->stamp -
473add46132SPeter Zijlstra 				     bw->ww_ctx->stamp) < 0;
474add46132SPeter Zijlstra 	}
475add46132SPeter Zijlstra 
476add46132SPeter Zijlstra 	return 0;
4775a798725SPeter Zijlstra }
4785a798725SPeter Zijlstra 
479d7a2edb8SThomas Gleixner static __always_inline void
rt_mutex_enqueue(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)480830e6accSPeter Zijlstra rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
481fb00aca4SPeter Zijlstra {
482f7853c34SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
483f7853c34SPeter Zijlstra 
484f7853c34SPeter Zijlstra 	rb_add_cached(&waiter->tree.entry, &lock->waiters, __waiter_less);
485fb00aca4SPeter Zijlstra }
486fb00aca4SPeter Zijlstra 
487d7a2edb8SThomas Gleixner static __always_inline void
rt_mutex_dequeue(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)488830e6accSPeter Zijlstra rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
489fb00aca4SPeter Zijlstra {
490f7853c34SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
491f7853c34SPeter Zijlstra 
492f7853c34SPeter Zijlstra 	if (RB_EMPTY_NODE(&waiter->tree.entry))
493fb00aca4SPeter Zijlstra 		return;
494fb00aca4SPeter Zijlstra 
495f7853c34SPeter Zijlstra 	rb_erase_cached(&waiter->tree.entry, &lock->waiters);
496f7853c34SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->tree.entry);
497fb00aca4SPeter Zijlstra }
498fb00aca4SPeter Zijlstra 
499f7853c34SPeter Zijlstra #define __node_2_rt_node(node) \
500f7853c34SPeter Zijlstra 	rb_entry((node), struct rt_waiter_node, entry)
5015a798725SPeter Zijlstra 
__pi_waiter_less(struct rb_node * a,const struct rb_node * b)502f7853c34SPeter Zijlstra static __always_inline bool __pi_waiter_less(struct rb_node *a, const struct rb_node *b)
5035a798725SPeter Zijlstra {
504f7853c34SPeter Zijlstra 	return rt_waiter_node_less(__node_2_rt_node(a), __node_2_rt_node(b));
5055a798725SPeter Zijlstra }
5065a798725SPeter Zijlstra 
507d7a2edb8SThomas Gleixner static __always_inline void
rt_mutex_enqueue_pi(struct task_struct * task,struct rt_mutex_waiter * waiter)508fb00aca4SPeter Zijlstra rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
509fb00aca4SPeter Zijlstra {
510f7853c34SPeter Zijlstra 	lockdep_assert_held(&task->pi_lock);
511f7853c34SPeter Zijlstra 
512f7853c34SPeter Zijlstra 	rb_add_cached(&waiter->pi_tree.entry, &task->pi_waiters, __pi_waiter_less);
513fb00aca4SPeter Zijlstra }
514fb00aca4SPeter Zijlstra 
515d7a2edb8SThomas Gleixner static __always_inline void
rt_mutex_dequeue_pi(struct task_struct * task,struct rt_mutex_waiter * waiter)516fb00aca4SPeter Zijlstra rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
517fb00aca4SPeter Zijlstra {
518f7853c34SPeter Zijlstra 	lockdep_assert_held(&task->pi_lock);
519f7853c34SPeter Zijlstra 
520f7853c34SPeter Zijlstra 	if (RB_EMPTY_NODE(&waiter->pi_tree.entry))
521fb00aca4SPeter Zijlstra 		return;
522fb00aca4SPeter Zijlstra 
523f7853c34SPeter Zijlstra 	rb_erase_cached(&waiter->pi_tree.entry, &task->pi_waiters);
524f7853c34SPeter Zijlstra 	RB_CLEAR_NODE(&waiter->pi_tree.entry);
525fb00aca4SPeter Zijlstra }
526fb00aca4SPeter Zijlstra 
rt_mutex_adjust_prio(struct rt_mutex_base * lock,struct task_struct * p)527f7853c34SPeter Zijlstra static __always_inline void rt_mutex_adjust_prio(struct rt_mutex_base *lock,
528f7853c34SPeter Zijlstra 						 struct task_struct *p)
529e96a7705SXunlei Pang {
530acd58620SPeter Zijlstra 	struct task_struct *pi_task = NULL;
531e96a7705SXunlei Pang 
532f7853c34SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
533f7853c34SPeter Zijlstra 	lockdep_assert(rt_mutex_owner(lock) == p);
534acd58620SPeter Zijlstra 	lockdep_assert_held(&p->pi_lock);
535e96a7705SXunlei Pang 
536acd58620SPeter Zijlstra 	if (task_has_pi_waiters(p))
537acd58620SPeter Zijlstra 		pi_task = task_top_pi_waiter(p)->task;
5381696a8beSPeter Zijlstra 
539acd58620SPeter Zijlstra 	rt_mutex_setprio(p, pi_task);
5401696a8beSPeter Zijlstra }
5411696a8beSPeter Zijlstra 
542b576e640SThomas Gleixner /* RT mutex specific wake_q wrappers */
rt_mutex_wake_q_add_task(struct rt_wake_q_head * wqh,struct task_struct * task,unsigned int wake_state)5439321f815SThomas Gleixner static __always_inline void rt_mutex_wake_q_add_task(struct rt_wake_q_head *wqh,
5449321f815SThomas Gleixner 						     struct task_struct *task,
5459321f815SThomas Gleixner 						     unsigned int wake_state)
5469321f815SThomas Gleixner {
5479321f815SThomas Gleixner 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && wake_state == TASK_RTLOCK_WAIT) {
5489321f815SThomas Gleixner 		if (IS_ENABLED(CONFIG_PROVE_LOCKING))
5499321f815SThomas Gleixner 			WARN_ON_ONCE(wqh->rtlock_task);
5509321f815SThomas Gleixner 		get_task_struct(task);
5519321f815SThomas Gleixner 		wqh->rtlock_task = task;
5529321f815SThomas Gleixner 	} else {
5539321f815SThomas Gleixner 		wake_q_add(&wqh->head, task);
5549321f815SThomas Gleixner 	}
5559321f815SThomas Gleixner }
5569321f815SThomas Gleixner 
rt_mutex_wake_q_add(struct rt_wake_q_head * wqh,struct rt_mutex_waiter * w)557b576e640SThomas Gleixner static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh,
558b576e640SThomas Gleixner 						struct rt_mutex_waiter *w)
559b576e640SThomas Gleixner {
5609321f815SThomas Gleixner 	rt_mutex_wake_q_add_task(wqh, w->task, w->wake_state);
561456cfbc6SThomas Gleixner }
562b576e640SThomas Gleixner 
rt_mutex_wake_up_q(struct rt_wake_q_head * wqh)563b576e640SThomas Gleixner static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
564b576e640SThomas Gleixner {
565456cfbc6SThomas Gleixner 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) {
566456cfbc6SThomas Gleixner 		wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT);
567456cfbc6SThomas Gleixner 		put_task_struct(wqh->rtlock_task);
568456cfbc6SThomas Gleixner 		wqh->rtlock_task = NULL;
569456cfbc6SThomas Gleixner 	}
570456cfbc6SThomas Gleixner 
571456cfbc6SThomas Gleixner 	if (!wake_q_empty(&wqh->head))
572b576e640SThomas Gleixner 		wake_up_q(&wqh->head);
573b576e640SThomas Gleixner 
574b576e640SThomas Gleixner 	/* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
575b576e640SThomas Gleixner 	preempt_enable();
576b576e640SThomas Gleixner }
577b576e640SThomas Gleixner 
5781696a8beSPeter Zijlstra /*
5798930ed80SThomas Gleixner  * Deadlock detection is conditional:
5808930ed80SThomas Gleixner  *
5818930ed80SThomas Gleixner  * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
5828930ed80SThomas Gleixner  * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
5838930ed80SThomas Gleixner  *
5848930ed80SThomas Gleixner  * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
5858930ed80SThomas Gleixner  * conducted independent of the detect argument.
5868930ed80SThomas Gleixner  *
5878930ed80SThomas Gleixner  * If the waiter argument is NULL this indicates the deboost path and
5888930ed80SThomas Gleixner  * deadlock detection is disabled independent of the detect argument
5898930ed80SThomas Gleixner  * and the config settings.
5908930ed80SThomas Gleixner  */
591d7a2edb8SThomas Gleixner static __always_inline bool
rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter * waiter,enum rtmutex_chainwalk chwalk)592d7a2edb8SThomas Gleixner rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
5938930ed80SThomas Gleixner 			      enum rtmutex_chainwalk chwalk)
5948930ed80SThomas Gleixner {
59507d25971SZhen Lei 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
596f7efc479SThomas Gleixner 		return waiter != NULL;
597f7efc479SThomas Gleixner 	return chwalk == RT_MUTEX_FULL_CHAINWALK;
5988930ed80SThomas Gleixner }
5998930ed80SThomas Gleixner 
task_blocked_on_lock(struct task_struct * p)600830e6accSPeter Zijlstra static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p)
60182084984SThomas Gleixner {
60282084984SThomas Gleixner 	return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
60382084984SThomas Gleixner }
60482084984SThomas Gleixner 
6051696a8beSPeter Zijlstra /*
6061696a8beSPeter Zijlstra  * Adjust the priority chain. Also used for deadlock detection.
6071696a8beSPeter Zijlstra  * Decreases task's usage by one - may thus free the task.
6081696a8beSPeter Zijlstra  *
60982084984SThomas Gleixner  * @task:	the task owning the mutex (owner) for which a chain walk is
61082084984SThomas Gleixner  *		probably needed
611e6beaa36STom(JeHyeon) Yeon  * @chwalk:	do we have to carry out deadlock detection?
6121696a8beSPeter Zijlstra  * @orig_lock:	the mutex (can be NULL if we are walking the chain to recheck
6131696a8beSPeter Zijlstra  *		things for a task that has just got its priority adjusted, and
6141696a8beSPeter Zijlstra  *		is waiting on a mutex)
61582084984SThomas Gleixner  * @next_lock:	the mutex on which the owner of @orig_lock was blocked before
61682084984SThomas Gleixner  *		we dropped its pi_lock. Is never dereferenced, only used for
61782084984SThomas Gleixner  *		comparison to detect lock chain changes.
6181696a8beSPeter Zijlstra  * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
6191696a8beSPeter Zijlstra  *		its priority to the mutex owner (can be NULL in the case
6201696a8beSPeter Zijlstra  *		depicted above or if the top waiter is gone away and we are
6211696a8beSPeter Zijlstra  *		actually deboosting the owner)
6221696a8beSPeter Zijlstra  * @top_task:	the current top waiter
6231696a8beSPeter Zijlstra  *
6241696a8beSPeter Zijlstra  * Returns 0 or -EDEADLK.
6253eb65aeaSThomas Gleixner  *
6263eb65aeaSThomas Gleixner  * Chain walk basics and protection scope
6273eb65aeaSThomas Gleixner  *
6283eb65aeaSThomas Gleixner  * [R] refcount on task
629f7853c34SPeter Zijlstra  * [Pn] task->pi_lock held
6303eb65aeaSThomas Gleixner  * [L] rtmutex->wait_lock held
6313eb65aeaSThomas Gleixner  *
632f7853c34SPeter Zijlstra  * Normal locking order:
633f7853c34SPeter Zijlstra  *
634f7853c34SPeter Zijlstra  *   rtmutex->wait_lock
635f7853c34SPeter Zijlstra  *     task->pi_lock
636f7853c34SPeter Zijlstra  *
6373eb65aeaSThomas Gleixner  * Step	Description				Protected by
6383eb65aeaSThomas Gleixner  *	function arguments:
6393eb65aeaSThomas Gleixner  *	@task					[R]
6403eb65aeaSThomas Gleixner  *	@orig_lock if != NULL			@top_task is blocked on it
6413eb65aeaSThomas Gleixner  *	@next_lock				Unprotected. Cannot be
6423eb65aeaSThomas Gleixner  *						dereferenced. Only used for
6433eb65aeaSThomas Gleixner  *						comparison.
6443eb65aeaSThomas Gleixner  *	@orig_waiter if != NULL			@top_task is blocked on it
6453eb65aeaSThomas Gleixner  *	@top_task				current, or in case of proxy
6463eb65aeaSThomas Gleixner  *						locking protected by calling
6473eb65aeaSThomas Gleixner  *						code
6483eb65aeaSThomas Gleixner  *	again:
6493eb65aeaSThomas Gleixner  *	  loop_sanity_check();
6503eb65aeaSThomas Gleixner  *	retry:
651f7853c34SPeter Zijlstra  * [1]	  lock(task->pi_lock);			[R] acquire [P1]
652f7853c34SPeter Zijlstra  * [2]	  waiter = task->pi_blocked_on;		[P1]
653f7853c34SPeter Zijlstra  * [3]	  check_exit_conditions_1();		[P1]
654f7853c34SPeter Zijlstra  * [4]	  lock = waiter->lock;			[P1]
655f7853c34SPeter Zijlstra  * [5]	  if (!try_lock(lock->wait_lock)) {	[P1] try to acquire [L]
656f7853c34SPeter Zijlstra  *	    unlock(task->pi_lock);		release [P1]
6573eb65aeaSThomas Gleixner  *	    goto retry;
6583eb65aeaSThomas Gleixner  *	  }
659f7853c34SPeter Zijlstra  * [6]	  check_exit_conditions_2();		[P1] + [L]
660f7853c34SPeter Zijlstra  * [7]	  requeue_lock_waiter(lock, waiter);	[P1] + [L]
661f7853c34SPeter Zijlstra  * [8]	  unlock(task->pi_lock);		release [P1]
6623eb65aeaSThomas Gleixner  *	  put_task_struct(task);		release [R]
6633eb65aeaSThomas Gleixner  * [9]	  check_exit_conditions_3();		[L]
6643eb65aeaSThomas Gleixner  * [10]	  task = owner(lock);			[L]
6653eb65aeaSThomas Gleixner  *	  get_task_struct(task);		[L] acquire [R]
666f7853c34SPeter Zijlstra  *	  lock(task->pi_lock);			[L] acquire [P2]
667f7853c34SPeter Zijlstra  * [11]	  requeue_pi_waiter(tsk, waiters(lock));[P2] + [L]
668f7853c34SPeter Zijlstra  * [12]	  check_exit_conditions_4();		[P2] + [L]
669f7853c34SPeter Zijlstra  * [13]	  unlock(task->pi_lock);		release [P2]
6703eb65aeaSThomas Gleixner  *	  unlock(lock->wait_lock);		release [L]
6713eb65aeaSThomas Gleixner  *	  goto again;
672f7853c34SPeter Zijlstra  *
673f7853c34SPeter Zijlstra  * Where P1 is the blocking task and P2 is the lock owner; going up one step
674f7853c34SPeter Zijlstra  * the owner becomes the next blocked task etc..
675f7853c34SPeter Zijlstra  *
676f7853c34SPeter Zijlstra *
6771696a8beSPeter Zijlstra  */
rt_mutex_adjust_prio_chain(struct task_struct * task,enum rtmutex_chainwalk chwalk,struct rt_mutex_base * orig_lock,struct rt_mutex_base * next_lock,struct rt_mutex_waiter * orig_waiter,struct task_struct * top_task)678d7a2edb8SThomas Gleixner static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
6798930ed80SThomas Gleixner 					      enum rtmutex_chainwalk chwalk,
680830e6accSPeter Zijlstra 					      struct rt_mutex_base *orig_lock,
681830e6accSPeter Zijlstra 					      struct rt_mutex_base *next_lock,
6821696a8beSPeter Zijlstra 					      struct rt_mutex_waiter *orig_waiter,
6831696a8beSPeter Zijlstra 					      struct task_struct *top_task)
6841696a8beSPeter Zijlstra {
6851696a8beSPeter Zijlstra 	struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
686a57594a1SThomas Gleixner 	struct rt_mutex_waiter *prerequeue_top_waiter;
6878930ed80SThomas Gleixner 	int ret = 0, depth = 0;
688830e6accSPeter Zijlstra 	struct rt_mutex_base *lock;
6898930ed80SThomas Gleixner 	bool detect_deadlock;
69067792e2cSThomas Gleixner 	bool requeue = true;
6911696a8beSPeter Zijlstra 
6928930ed80SThomas Gleixner 	detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
6931696a8beSPeter Zijlstra 
6941696a8beSPeter Zijlstra 	/*
6951696a8beSPeter Zijlstra 	 * The (de)boosting is a step by step approach with a lot of
6961696a8beSPeter Zijlstra 	 * pitfalls. We want this to be preemptible and we want hold a
6971696a8beSPeter Zijlstra 	 * maximum of two locks per step. So we have to check
6981696a8beSPeter Zijlstra 	 * carefully whether things change under us.
6991696a8beSPeter Zijlstra 	 */
7001696a8beSPeter Zijlstra  again:
7013eb65aeaSThomas Gleixner 	/*
7023eb65aeaSThomas Gleixner 	 * We limit the lock chain length for each invocation.
7033eb65aeaSThomas Gleixner 	 */
7041696a8beSPeter Zijlstra 	if (++depth > max_lock_depth) {
7051696a8beSPeter Zijlstra 		static int prev_max;
7061696a8beSPeter Zijlstra 
7071696a8beSPeter Zijlstra 		/*
7081696a8beSPeter Zijlstra 		 * Print this only once. If the admin changes the limit,
7091696a8beSPeter Zijlstra 		 * print a new message when reaching the limit again.
7101696a8beSPeter Zijlstra 		 */
7111696a8beSPeter Zijlstra 		if (prev_max != max_lock_depth) {
7121696a8beSPeter Zijlstra 			prev_max = max_lock_depth;
7131696a8beSPeter Zijlstra 			printk(KERN_WARNING "Maximum lock depth %d reached "
7141696a8beSPeter Zijlstra 			       "task: %s (%d)\n", max_lock_depth,
7151696a8beSPeter Zijlstra 			       top_task->comm, task_pid_nr(top_task));
7161696a8beSPeter Zijlstra 		}
7171696a8beSPeter Zijlstra 		put_task_struct(task);
7181696a8beSPeter Zijlstra 
7193d5c9340SThomas Gleixner 		return -EDEADLK;
7201696a8beSPeter Zijlstra 	}
7213eb65aeaSThomas Gleixner 
7223eb65aeaSThomas Gleixner 	/*
7233eb65aeaSThomas Gleixner 	 * We are fully preemptible here and only hold the refcount on
7243eb65aeaSThomas Gleixner 	 * @task. So everything can have changed under us since the
7253eb65aeaSThomas Gleixner 	 * caller or our own code below (goto retry/again) dropped all
7263eb65aeaSThomas Gleixner 	 * locks.
7273eb65aeaSThomas Gleixner 	 */
7281696a8beSPeter Zijlstra  retry:
7291696a8beSPeter Zijlstra 	/*
7303eb65aeaSThomas Gleixner 	 * [1] Task cannot go away as we did a get_task() before !
7311696a8beSPeter Zijlstra 	 */
732b4abf910SThomas Gleixner 	raw_spin_lock_irq(&task->pi_lock);
7331696a8beSPeter Zijlstra 
7343eb65aeaSThomas Gleixner 	/*
7353eb65aeaSThomas Gleixner 	 * [2] Get the waiter on which @task is blocked on.
7363eb65aeaSThomas Gleixner 	 */
7371696a8beSPeter Zijlstra 	waiter = task->pi_blocked_on;
7383eb65aeaSThomas Gleixner 
7393eb65aeaSThomas Gleixner 	/*
7403eb65aeaSThomas Gleixner 	 * [3] check_exit_conditions_1() protected by task->pi_lock.
7413eb65aeaSThomas Gleixner 	 */
7423eb65aeaSThomas Gleixner 
7431696a8beSPeter Zijlstra 	/*
7441696a8beSPeter Zijlstra 	 * Check whether the end of the boosting chain has been
7451696a8beSPeter Zijlstra 	 * reached or the state of the chain has changed while we
7461696a8beSPeter Zijlstra 	 * dropped the locks.
7471696a8beSPeter Zijlstra 	 */
7481696a8beSPeter Zijlstra 	if (!waiter)
7491696a8beSPeter Zijlstra 		goto out_unlock_pi;
7501696a8beSPeter Zijlstra 
7511696a8beSPeter Zijlstra 	/*
7521696a8beSPeter Zijlstra 	 * Check the orig_waiter state. After we dropped the locks,
7531696a8beSPeter Zijlstra 	 * the previous owner of the lock might have released the lock.
7541696a8beSPeter Zijlstra 	 */
7551696a8beSPeter Zijlstra 	if (orig_waiter && !rt_mutex_owner(orig_lock))
7561696a8beSPeter Zijlstra 		goto out_unlock_pi;
7571696a8beSPeter Zijlstra 
7581696a8beSPeter Zijlstra 	/*
75982084984SThomas Gleixner 	 * We dropped all locks after taking a refcount on @task, so
76082084984SThomas Gleixner 	 * the task might have moved on in the lock chain or even left
76182084984SThomas Gleixner 	 * the chain completely and blocks now on an unrelated lock or
76282084984SThomas Gleixner 	 * on @orig_lock.
76382084984SThomas Gleixner 	 *
76482084984SThomas Gleixner 	 * We stored the lock on which @task was blocked in @next_lock,
76582084984SThomas Gleixner 	 * so we can detect the chain change.
76682084984SThomas Gleixner 	 */
76782084984SThomas Gleixner 	if (next_lock != waiter->lock)
76882084984SThomas Gleixner 		goto out_unlock_pi;
76982084984SThomas Gleixner 
77082084984SThomas Gleixner 	/*
7716467822bSPeter Zijlstra 	 * There could be 'spurious' loops in the lock graph due to ww_mutex,
7726467822bSPeter Zijlstra 	 * consider:
7736467822bSPeter Zijlstra 	 *
7746467822bSPeter Zijlstra 	 *   P1: A, ww_A, ww_B
7756467822bSPeter Zijlstra 	 *   P2: ww_B, ww_A
7766467822bSPeter Zijlstra 	 *   P3: A
7776467822bSPeter Zijlstra 	 *
7786467822bSPeter Zijlstra 	 * P3 should not return -EDEADLK because it gets trapped in the cycle
7796467822bSPeter Zijlstra 	 * created by P1 and P2 (which will resolve -- and runs into
7806467822bSPeter Zijlstra 	 * max_lock_depth above). Therefore disable detect_deadlock such that
7816467822bSPeter Zijlstra 	 * the below termination condition can trigger once all relevant tasks
7826467822bSPeter Zijlstra 	 * are boosted.
7836467822bSPeter Zijlstra 	 *
7846467822bSPeter Zijlstra 	 * Even when we start with ww_mutex we can disable deadlock detection,
7856467822bSPeter Zijlstra 	 * since we would supress a ww_mutex induced deadlock at [6] anyway.
7866467822bSPeter Zijlstra 	 * Supressing it here however is not sufficient since we might still
7876467822bSPeter Zijlstra 	 * hit [6] due to adjustment driven iteration.
7886467822bSPeter Zijlstra 	 *
7896467822bSPeter Zijlstra 	 * NOTE: if someone were to create a deadlock between 2 ww_classes we'd
7906467822bSPeter Zijlstra 	 * utterly fail to report it; lockdep should.
7916467822bSPeter Zijlstra 	 */
7926467822bSPeter Zijlstra 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && waiter->ww_ctx && detect_deadlock)
7936467822bSPeter Zijlstra 		detect_deadlock = false;
7946467822bSPeter Zijlstra 
7956467822bSPeter Zijlstra 	/*
7961696a8beSPeter Zijlstra 	 * Drop out, when the task has no waiters. Note,
7971696a8beSPeter Zijlstra 	 * top_waiter can be NULL, when we are in the deboosting
7981696a8beSPeter Zijlstra 	 * mode!
7991696a8beSPeter Zijlstra 	 */
800397335f0SThomas Gleixner 	if (top_waiter) {
801397335f0SThomas Gleixner 		if (!task_has_pi_waiters(task))
8021696a8beSPeter Zijlstra 			goto out_unlock_pi;
803397335f0SThomas Gleixner 		/*
804397335f0SThomas Gleixner 		 * If deadlock detection is off, we stop here if we
80567792e2cSThomas Gleixner 		 * are not the top pi waiter of the task. If deadlock
80667792e2cSThomas Gleixner 		 * detection is enabled we continue, but stop the
80767792e2cSThomas Gleixner 		 * requeueing in the chain walk.
808397335f0SThomas Gleixner 		 */
80967792e2cSThomas Gleixner 		if (top_waiter != task_top_pi_waiter(task)) {
81067792e2cSThomas Gleixner 			if (!detect_deadlock)
811397335f0SThomas Gleixner 				goto out_unlock_pi;
81267792e2cSThomas Gleixner 			else
81367792e2cSThomas Gleixner 				requeue = false;
81467792e2cSThomas Gleixner 		}
815397335f0SThomas Gleixner 	}
8161696a8beSPeter Zijlstra 
8171696a8beSPeter Zijlstra 	/*
81867792e2cSThomas Gleixner 	 * If the waiter priority is the same as the task priority
81967792e2cSThomas Gleixner 	 * then there is no further priority adjustment necessary.  If
82067792e2cSThomas Gleixner 	 * deadlock detection is off, we stop the chain walk. If its
82167792e2cSThomas Gleixner 	 * enabled we continue, but stop the requeueing in the chain
82267792e2cSThomas Gleixner 	 * walk.
8231696a8beSPeter Zijlstra 	 */
824f7853c34SPeter Zijlstra 	if (rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
82567792e2cSThomas Gleixner 		if (!detect_deadlock)
8261696a8beSPeter Zijlstra 			goto out_unlock_pi;
82767792e2cSThomas Gleixner 		else
82867792e2cSThomas Gleixner 			requeue = false;
82967792e2cSThomas Gleixner 	}
8301696a8beSPeter Zijlstra 
8313eb65aeaSThomas Gleixner 	/*
832f7853c34SPeter Zijlstra 	 * [4] Get the next lock; per holding task->pi_lock we can't unblock
833f7853c34SPeter Zijlstra 	 * and guarantee @lock's existence.
8343eb65aeaSThomas Gleixner 	 */
8351696a8beSPeter Zijlstra 	lock = waiter->lock;
8363eb65aeaSThomas Gleixner 	/*
8373eb65aeaSThomas Gleixner 	 * [5] We need to trylock here as we are holding task->pi_lock,
8383eb65aeaSThomas Gleixner 	 * which is the reverse lock order versus the other rtmutex
8393eb65aeaSThomas Gleixner 	 * operations.
840f7853c34SPeter Zijlstra 	 *
841f7853c34SPeter Zijlstra 	 * Per the above, holding task->pi_lock guarantees lock exists, so
842f7853c34SPeter Zijlstra 	 * inverting this lock order is infeasible from a life-time
843f7853c34SPeter Zijlstra 	 * perspective.
8443eb65aeaSThomas Gleixner 	 */
8451696a8beSPeter Zijlstra 	if (!raw_spin_trylock(&lock->wait_lock)) {
846b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&task->pi_lock);
8471696a8beSPeter Zijlstra 		cpu_relax();
8481696a8beSPeter Zijlstra 		goto retry;
8491696a8beSPeter Zijlstra 	}
8501696a8beSPeter Zijlstra 
851397335f0SThomas Gleixner 	/*
8523eb65aeaSThomas Gleixner 	 * [6] check_exit_conditions_2() protected by task->pi_lock and
8533eb65aeaSThomas Gleixner 	 * lock->wait_lock.
8543eb65aeaSThomas Gleixner 	 *
855397335f0SThomas Gleixner 	 * Deadlock detection. If the lock is the same as the original
856397335f0SThomas Gleixner 	 * lock which caused us to walk the lock chain or if the
857397335f0SThomas Gleixner 	 * current lock is owned by the task which initiated the chain
858397335f0SThomas Gleixner 	 * walk, we detected a deadlock.
859397335f0SThomas Gleixner 	 */
8601696a8beSPeter Zijlstra 	if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
8613d5c9340SThomas Gleixner 		ret = -EDEADLK;
862a055fcc1SPeter Zijlstra 
863a055fcc1SPeter Zijlstra 		/*
864a055fcc1SPeter Zijlstra 		 * When the deadlock is due to ww_mutex; also see above. Don't
865a055fcc1SPeter Zijlstra 		 * report the deadlock and instead let the ww_mutex wound/die
866a055fcc1SPeter Zijlstra 		 * logic pick which of the contending threads gets -EDEADLK.
867a055fcc1SPeter Zijlstra 		 *
868a055fcc1SPeter Zijlstra 		 * NOTE: assumes the cycle only contains a single ww_class; any
869a055fcc1SPeter Zijlstra 		 * other configuration and we fail to report; also, see
870a055fcc1SPeter Zijlstra 		 * lockdep.
871a055fcc1SPeter Zijlstra 		 */
872e5480572SPeter Zijlstra 		if (IS_ENABLED(CONFIG_PREEMPT_RT) && orig_waiter && orig_waiter->ww_ctx)
873a055fcc1SPeter Zijlstra 			ret = 0;
874a055fcc1SPeter Zijlstra 
875a055fcc1SPeter Zijlstra 		raw_spin_unlock(&lock->wait_lock);
8761696a8beSPeter Zijlstra 		goto out_unlock_pi;
8771696a8beSPeter Zijlstra 	}
8781696a8beSPeter Zijlstra 
879a57594a1SThomas Gleixner 	/*
88067792e2cSThomas Gleixner 	 * If we just follow the lock chain for deadlock detection, no
88167792e2cSThomas Gleixner 	 * need to do all the requeue operations. To avoid a truckload
88267792e2cSThomas Gleixner 	 * of conditionals around the various places below, just do the
88367792e2cSThomas Gleixner 	 * minimum chain walk checks.
88467792e2cSThomas Gleixner 	 */
88567792e2cSThomas Gleixner 	if (!requeue) {
88667792e2cSThomas Gleixner 		/*
88767792e2cSThomas Gleixner 		 * No requeue[7] here. Just release @task [8]
88867792e2cSThomas Gleixner 		 */
889b4abf910SThomas Gleixner 		raw_spin_unlock(&task->pi_lock);
89067792e2cSThomas Gleixner 		put_task_struct(task);
89167792e2cSThomas Gleixner 
89267792e2cSThomas Gleixner 		/*
89367792e2cSThomas Gleixner 		 * [9] check_exit_conditions_3 protected by lock->wait_lock.
89467792e2cSThomas Gleixner 		 * If there is no owner of the lock, end of chain.
89567792e2cSThomas Gleixner 		 */
89667792e2cSThomas Gleixner 		if (!rt_mutex_owner(lock)) {
897b4abf910SThomas Gleixner 			raw_spin_unlock_irq(&lock->wait_lock);
89867792e2cSThomas Gleixner 			return 0;
89967792e2cSThomas Gleixner 		}
90067792e2cSThomas Gleixner 
90167792e2cSThomas Gleixner 		/* [10] Grab the next task, i.e. owner of @lock */
9027b3c92b8SMatthew Wilcox (Oracle) 		task = get_task_struct(rt_mutex_owner(lock));
903b4abf910SThomas Gleixner 		raw_spin_lock(&task->pi_lock);
90467792e2cSThomas Gleixner 
90567792e2cSThomas Gleixner 		/*
90667792e2cSThomas Gleixner 		 * No requeue [11] here. We just do deadlock detection.
90767792e2cSThomas Gleixner 		 *
90867792e2cSThomas Gleixner 		 * [12] Store whether owner is blocked
90967792e2cSThomas Gleixner 		 * itself. Decision is made after dropping the locks
91067792e2cSThomas Gleixner 		 */
91167792e2cSThomas Gleixner 		next_lock = task_blocked_on_lock(task);
91267792e2cSThomas Gleixner 		/*
91367792e2cSThomas Gleixner 		 * Get the top waiter for the next iteration
91467792e2cSThomas Gleixner 		 */
91567792e2cSThomas Gleixner 		top_waiter = rt_mutex_top_waiter(lock);
91667792e2cSThomas Gleixner 
91767792e2cSThomas Gleixner 		/* [13] Drop locks */
918b4abf910SThomas Gleixner 		raw_spin_unlock(&task->pi_lock);
919b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
92067792e2cSThomas Gleixner 
92167792e2cSThomas Gleixner 		/* If owner is not blocked, end of chain. */
92267792e2cSThomas Gleixner 		if (!next_lock)
92367792e2cSThomas Gleixner 			goto out_put_task;
92467792e2cSThomas Gleixner 		goto again;
92567792e2cSThomas Gleixner 	}
92667792e2cSThomas Gleixner 
92767792e2cSThomas Gleixner 	/*
928a57594a1SThomas Gleixner 	 * Store the current top waiter before doing the requeue
929a57594a1SThomas Gleixner 	 * operation on @lock. We need it for the boost/deboost
930a57594a1SThomas Gleixner 	 * decision below.
931a57594a1SThomas Gleixner 	 */
932a57594a1SThomas Gleixner 	prerequeue_top_waiter = rt_mutex_top_waiter(lock);
9331696a8beSPeter Zijlstra 
9349f40a51aSDavidlohr Bueso 	/* [7] Requeue the waiter in the lock waiter tree. */
935fb00aca4SPeter Zijlstra 	rt_mutex_dequeue(lock, waiter);
936e0aad5b4SPeter Zijlstra 
937e0aad5b4SPeter Zijlstra 	/*
938e0aad5b4SPeter Zijlstra 	 * Update the waiter prio fields now that we're dequeued.
939e0aad5b4SPeter Zijlstra 	 *
940e0aad5b4SPeter Zijlstra 	 * These values can have changed through either:
941e0aad5b4SPeter Zijlstra 	 *
942e0aad5b4SPeter Zijlstra 	 *   sys_sched_set_scheduler() / sys_sched_setattr()
943e0aad5b4SPeter Zijlstra 	 *
944e0aad5b4SPeter Zijlstra 	 * or
945e0aad5b4SPeter Zijlstra 	 *
946e0aad5b4SPeter Zijlstra 	 *   DL CBS enforcement advancing the effective deadline.
947e0aad5b4SPeter Zijlstra 	 */
948715f7f9eSPeter Zijlstra 	waiter_update_prio(waiter, task);
949e0aad5b4SPeter Zijlstra 
950fb00aca4SPeter Zijlstra 	rt_mutex_enqueue(lock, waiter);
9511696a8beSPeter Zijlstra 
952f7853c34SPeter Zijlstra 	/*
953f7853c34SPeter Zijlstra 	 * [8] Release the (blocking) task in preparation for
954f7853c34SPeter Zijlstra 	 * taking the owner task in [10].
955f7853c34SPeter Zijlstra 	 *
956f7853c34SPeter Zijlstra 	 * Since we hold lock->waiter_lock, task cannot unblock, even if we
957f7853c34SPeter Zijlstra 	 * release task->pi_lock.
958f7853c34SPeter Zijlstra 	 */
959b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
9602ffa5a5cSThomas Gleixner 	put_task_struct(task);
9612ffa5a5cSThomas Gleixner 
962a57594a1SThomas Gleixner 	/*
9633eb65aeaSThomas Gleixner 	 * [9] check_exit_conditions_3 protected by lock->wait_lock.
9643eb65aeaSThomas Gleixner 	 *
965a57594a1SThomas Gleixner 	 * We must abort the chain walk if there is no lock owner even
966a57594a1SThomas Gleixner 	 * in the dead lock detection case, as we have nothing to
967a57594a1SThomas Gleixner 	 * follow here. This is the end of the chain we are walking.
968a57594a1SThomas Gleixner 	 */
9691696a8beSPeter Zijlstra 	if (!rt_mutex_owner(lock)) {
9701696a8beSPeter Zijlstra 		/*
9713eb65aeaSThomas Gleixner 		 * If the requeue [7] above changed the top waiter,
9723eb65aeaSThomas Gleixner 		 * then we need to wake the new top waiter up to try
9733eb65aeaSThomas Gleixner 		 * to get the lock.
9741696a8beSPeter Zijlstra 		 */
975db370a8bSWander Lairson Costa 		top_waiter = rt_mutex_top_waiter(lock);
976db370a8bSWander Lairson Costa 		if (prerequeue_top_waiter != top_waiter)
977db370a8bSWander Lairson Costa 			wake_up_state(top_waiter->task, top_waiter->wake_state);
978b4abf910SThomas Gleixner 		raw_spin_unlock_irq(&lock->wait_lock);
9792ffa5a5cSThomas Gleixner 		return 0;
9801696a8beSPeter Zijlstra 	}
9811696a8beSPeter Zijlstra 
982f7853c34SPeter Zijlstra 	/*
983f7853c34SPeter Zijlstra 	 * [10] Grab the next task, i.e. the owner of @lock
984f7853c34SPeter Zijlstra 	 *
985f7853c34SPeter Zijlstra 	 * Per holding lock->wait_lock and checking for !owner above, there
986f7853c34SPeter Zijlstra 	 * must be an owner and it cannot go away.
987f7853c34SPeter Zijlstra 	 */
9887b3c92b8SMatthew Wilcox (Oracle) 	task = get_task_struct(rt_mutex_owner(lock));
989b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
9901696a8beSPeter Zijlstra 
9913eb65aeaSThomas Gleixner 	/* [11] requeue the pi waiters if necessary */
9921696a8beSPeter Zijlstra 	if (waiter == rt_mutex_top_waiter(lock)) {
993a57594a1SThomas Gleixner 		/*
994a57594a1SThomas Gleixner 		 * The waiter became the new top (highest priority)
995a57594a1SThomas Gleixner 		 * waiter on the lock. Replace the previous top waiter
9969f40a51aSDavidlohr Bueso 		 * in the owner tasks pi waiters tree with this waiter
997a57594a1SThomas Gleixner 		 * and adjust the priority of the owner.
998a57594a1SThomas Gleixner 		 */
999a57594a1SThomas Gleixner 		rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
1000f7853c34SPeter Zijlstra 		waiter_clone_prio(waiter, task);
1001fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(task, waiter);
1002f7853c34SPeter Zijlstra 		rt_mutex_adjust_prio(lock, task);
10031696a8beSPeter Zijlstra 
1004a57594a1SThomas Gleixner 	} else if (prerequeue_top_waiter == waiter) {
1005a57594a1SThomas Gleixner 		/*
1006a57594a1SThomas Gleixner 		 * The waiter was the top waiter on the lock, but is
1007e2db7592SIngo Molnar 		 * no longer the top priority waiter. Replace waiter in
10089f40a51aSDavidlohr Bueso 		 * the owner tasks pi waiters tree with the new top
1009a57594a1SThomas Gleixner 		 * (highest priority) waiter and adjust the priority
1010a57594a1SThomas Gleixner 		 * of the owner.
1011a57594a1SThomas Gleixner 		 * The new top waiter is stored in @waiter so that
1012a57594a1SThomas Gleixner 		 * @waiter == @top_waiter evaluates to true below and
1013a57594a1SThomas Gleixner 		 * we continue to deboost the rest of the chain.
1014a57594a1SThomas Gleixner 		 */
1015fb00aca4SPeter Zijlstra 		rt_mutex_dequeue_pi(task, waiter);
10161696a8beSPeter Zijlstra 		waiter = rt_mutex_top_waiter(lock);
1017f7853c34SPeter Zijlstra 		waiter_clone_prio(waiter, task);
1018fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(task, waiter);
1019f7853c34SPeter Zijlstra 		rt_mutex_adjust_prio(lock, task);
1020a57594a1SThomas Gleixner 	} else {
1021a57594a1SThomas Gleixner 		/*
1022a57594a1SThomas Gleixner 		 * Nothing changed. No need to do any priority
1023a57594a1SThomas Gleixner 		 * adjustment.
1024a57594a1SThomas Gleixner 		 */
10251696a8beSPeter Zijlstra 	}
10261696a8beSPeter Zijlstra 
102782084984SThomas Gleixner 	/*
10283eb65aeaSThomas Gleixner 	 * [12] check_exit_conditions_4() protected by task->pi_lock
10293eb65aeaSThomas Gleixner 	 * and lock->wait_lock. The actual decisions are made after we
10303eb65aeaSThomas Gleixner 	 * dropped the locks.
10313eb65aeaSThomas Gleixner 	 *
103282084984SThomas Gleixner 	 * Check whether the task which owns the current lock is pi
103382084984SThomas Gleixner 	 * blocked itself. If yes we store a pointer to the lock for
103482084984SThomas Gleixner 	 * the lock chain change detection above. After we dropped
103582084984SThomas Gleixner 	 * task->pi_lock next_lock cannot be dereferenced anymore.
103682084984SThomas Gleixner 	 */
103782084984SThomas Gleixner 	next_lock = task_blocked_on_lock(task);
1038a57594a1SThomas Gleixner 	/*
1039a57594a1SThomas Gleixner 	 * Store the top waiter of @lock for the end of chain walk
1040a57594a1SThomas Gleixner 	 * decision below.
1041a57594a1SThomas Gleixner 	 */
10421696a8beSPeter Zijlstra 	top_waiter = rt_mutex_top_waiter(lock);
10433eb65aeaSThomas Gleixner 
10443eb65aeaSThomas Gleixner 	/* [13] Drop the locks */
1045b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
1046b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
10471696a8beSPeter Zijlstra 
104882084984SThomas Gleixner 	/*
10493eb65aeaSThomas Gleixner 	 * Make the actual exit decisions [12], based on the stored
10503eb65aeaSThomas Gleixner 	 * values.
10513eb65aeaSThomas Gleixner 	 *
105282084984SThomas Gleixner 	 * We reached the end of the lock chain. Stop right here. No
105382084984SThomas Gleixner 	 * point to go back just to figure that out.
105482084984SThomas Gleixner 	 */
105582084984SThomas Gleixner 	if (!next_lock)
105682084984SThomas Gleixner 		goto out_put_task;
105782084984SThomas Gleixner 
1058a57594a1SThomas Gleixner 	/*
1059a57594a1SThomas Gleixner 	 * If the current waiter is not the top waiter on the lock,
1060a57594a1SThomas Gleixner 	 * then we can stop the chain walk here if we are not in full
1061a57594a1SThomas Gleixner 	 * deadlock detection mode.
1062a57594a1SThomas Gleixner 	 */
10631696a8beSPeter Zijlstra 	if (!detect_deadlock && waiter != top_waiter)
10641696a8beSPeter Zijlstra 		goto out_put_task;
10651696a8beSPeter Zijlstra 
10661696a8beSPeter Zijlstra 	goto again;
10671696a8beSPeter Zijlstra 
10681696a8beSPeter Zijlstra  out_unlock_pi:
1069b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&task->pi_lock);
10701696a8beSPeter Zijlstra  out_put_task:
10711696a8beSPeter Zijlstra 	put_task_struct(task);
10721696a8beSPeter Zijlstra 
10731696a8beSPeter Zijlstra 	return ret;
10741696a8beSPeter Zijlstra }
10751696a8beSPeter Zijlstra 
10761696a8beSPeter Zijlstra /*
10771696a8beSPeter Zijlstra  * Try to take an rt-mutex
10781696a8beSPeter Zijlstra  *
1079b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled
10801696a8beSPeter Zijlstra  *
1081358c331fSThomas Gleixner  * @lock:   The lock to be acquired.
1082358c331fSThomas Gleixner  * @task:   The task which wants to acquire the lock
10839f40a51aSDavidlohr Bueso  * @waiter: The waiter that is queued to the lock's wait tree if the
1084358c331fSThomas Gleixner  *	    callsite called task_blocked_on_lock(), otherwise NULL
10851696a8beSPeter Zijlstra  */
1086d7a2edb8SThomas Gleixner static int __sched
try_to_take_rt_mutex(struct rt_mutex_base * lock,struct task_struct * task,struct rt_mutex_waiter * waiter)1087830e6accSPeter Zijlstra try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
10881696a8beSPeter Zijlstra 		     struct rt_mutex_waiter *waiter)
10891696a8beSPeter Zijlstra {
1090e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1091e0aad5b4SPeter Zijlstra 
10921696a8beSPeter Zijlstra 	/*
1093358c331fSThomas Gleixner 	 * Before testing whether we can acquire @lock, we set the
1094358c331fSThomas Gleixner 	 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
1095358c331fSThomas Gleixner 	 * other tasks which try to modify @lock into the slow path
1096358c331fSThomas Gleixner 	 * and they serialize on @lock->wait_lock.
10971696a8beSPeter Zijlstra 	 *
1098358c331fSThomas Gleixner 	 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
1099358c331fSThomas Gleixner 	 * as explained at the top of this file if and only if:
11001696a8beSPeter Zijlstra 	 *
1101358c331fSThomas Gleixner 	 * - There is a lock owner. The caller must fixup the
1102358c331fSThomas Gleixner 	 *   transient state if it does a trylock or leaves the lock
1103358c331fSThomas Gleixner 	 *   function due to a signal or timeout.
1104358c331fSThomas Gleixner 	 *
1105358c331fSThomas Gleixner 	 * - @task acquires the lock and there are no other
1106358c331fSThomas Gleixner 	 *   waiters. This is undone in rt_mutex_set_owner(@task) at
1107358c331fSThomas Gleixner 	 *   the end of this function.
11081696a8beSPeter Zijlstra 	 */
11091696a8beSPeter Zijlstra 	mark_rt_mutex_waiters(lock);
11101696a8beSPeter Zijlstra 
1111358c331fSThomas Gleixner 	/*
1112358c331fSThomas Gleixner 	 * If @lock has an owner, give up.
1113358c331fSThomas Gleixner 	 */
11141696a8beSPeter Zijlstra 	if (rt_mutex_owner(lock))
11151696a8beSPeter Zijlstra 		return 0;
11161696a8beSPeter Zijlstra 
11171696a8beSPeter Zijlstra 	/*
1118358c331fSThomas Gleixner 	 * If @waiter != NULL, @task has already enqueued the waiter
11199f40a51aSDavidlohr Bueso 	 * into @lock waiter tree. If @waiter == NULL then this is a
1120358c331fSThomas Gleixner 	 * trylock attempt.
1121358c331fSThomas Gleixner 	 */
1122358c331fSThomas Gleixner 	if (waiter) {
112348eb3f4fSGregory Haskins 		struct rt_mutex_waiter *top_waiter = rt_mutex_top_waiter(lock);
1124358c331fSThomas Gleixner 
1125358c331fSThomas Gleixner 		/*
112648eb3f4fSGregory Haskins 		 * If waiter is the highest priority waiter of @lock,
112748eb3f4fSGregory Haskins 		 * or allowed to steal it, take it over.
112848eb3f4fSGregory Haskins 		 */
112948eb3f4fSGregory Haskins 		if (waiter == top_waiter || rt_mutex_steal(waiter, top_waiter)) {
113048eb3f4fSGregory Haskins 			/*
1131358c331fSThomas Gleixner 			 * We can acquire the lock. Remove the waiter from the
11329f40a51aSDavidlohr Bueso 			 * lock waiters tree.
1133358c331fSThomas Gleixner 			 */
1134358c331fSThomas Gleixner 			rt_mutex_dequeue(lock, waiter);
113548eb3f4fSGregory Haskins 		} else {
113648eb3f4fSGregory Haskins 			return 0;
113748eb3f4fSGregory Haskins 		}
1138358c331fSThomas Gleixner 	} else {
1139358c331fSThomas Gleixner 		/*
1140358c331fSThomas Gleixner 		 * If the lock has waiters already we check whether @task is
1141358c331fSThomas Gleixner 		 * eligible to take over the lock.
1142358c331fSThomas Gleixner 		 *
1143358c331fSThomas Gleixner 		 * If there are no other waiters, @task can acquire
1144358c331fSThomas Gleixner 		 * the lock.  @task->pi_blocked_on is NULL, so it does
1145358c331fSThomas Gleixner 		 * not need to be dequeued.
11461696a8beSPeter Zijlstra 		 */
11471696a8beSPeter Zijlstra 		if (rt_mutex_has_waiters(lock)) {
114848eb3f4fSGregory Haskins 			/* Check whether the trylock can steal it. */
114948eb3f4fSGregory Haskins 			if (!rt_mutex_steal(task_to_waiter(task),
115019830e55SPeter Zijlstra 					    rt_mutex_top_waiter(lock)))
11511696a8beSPeter Zijlstra 				return 0;
1152358c331fSThomas Gleixner 
1153358c331fSThomas Gleixner 			/*
1154358c331fSThomas Gleixner 			 * The current top waiter stays enqueued. We
1155358c331fSThomas Gleixner 			 * don't have to change anything in the lock
1156358c331fSThomas Gleixner 			 * waiters order.
1157358c331fSThomas Gleixner 			 */
1158358c331fSThomas Gleixner 		} else {
1159358c331fSThomas Gleixner 			/*
1160358c331fSThomas Gleixner 			 * No waiters. Take the lock without the
1161358c331fSThomas Gleixner 			 * pi_lock dance.@task->pi_blocked_on is NULL
1162358c331fSThomas Gleixner 			 * and we have no waiters to enqueue in @task
11639f40a51aSDavidlohr Bueso 			 * pi waiters tree.
1164358c331fSThomas Gleixner 			 */
1165358c331fSThomas Gleixner 			goto takeit;
11661696a8beSPeter Zijlstra 		}
11671696a8beSPeter Zijlstra 	}
11681696a8beSPeter Zijlstra 
11691696a8beSPeter Zijlstra 	/*
1170358c331fSThomas Gleixner 	 * Clear @task->pi_blocked_on. Requires protection by
1171358c331fSThomas Gleixner 	 * @task->pi_lock. Redundant operation for the @waiter == NULL
1172358c331fSThomas Gleixner 	 * case, but conditionals are more expensive than a redundant
1173358c331fSThomas Gleixner 	 * store.
11741696a8beSPeter Zijlstra 	 */
1175b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
1176358c331fSThomas Gleixner 	task->pi_blocked_on = NULL;
1177358c331fSThomas Gleixner 	/*
1178358c331fSThomas Gleixner 	 * Finish the lock acquisition. @task is the new owner. If
1179358c331fSThomas Gleixner 	 * other waiters exist we have to insert the highest priority
11809f40a51aSDavidlohr Bueso 	 * waiter into @task->pi_waiters tree.
1181358c331fSThomas Gleixner 	 */
1182358c331fSThomas Gleixner 	if (rt_mutex_has_waiters(lock))
1183358c331fSThomas Gleixner 		rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
1184b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
11851696a8beSPeter Zijlstra 
1186358c331fSThomas Gleixner takeit:
1187358c331fSThomas Gleixner 	/*
1188358c331fSThomas Gleixner 	 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
1189358c331fSThomas Gleixner 	 * are still waiters or clears it.
1190358c331fSThomas Gleixner 	 */
11911696a8beSPeter Zijlstra 	rt_mutex_set_owner(lock, task);
11921696a8beSPeter Zijlstra 
11931696a8beSPeter Zijlstra 	return 1;
11941696a8beSPeter Zijlstra }
11951696a8beSPeter Zijlstra 
11961696a8beSPeter Zijlstra /*
11971696a8beSPeter Zijlstra  * Task blocks on lock.
11981696a8beSPeter Zijlstra  *
11991696a8beSPeter Zijlstra  * Prepare waiter and propagate pi chain
12001696a8beSPeter Zijlstra  *
1201b4abf910SThomas Gleixner  * This must be called with lock->wait_lock held and interrupts disabled
12021696a8beSPeter Zijlstra  */
task_blocks_on_rt_mutex(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task,struct ww_acquire_ctx * ww_ctx,enum rtmutex_chainwalk chwalk,struct wake_q_head * wake_q)1203830e6accSPeter Zijlstra static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
12041696a8beSPeter Zijlstra 					   struct rt_mutex_waiter *waiter,
12051696a8beSPeter Zijlstra 					   struct task_struct *task,
1206add46132SPeter Zijlstra 					   struct ww_acquire_ctx *ww_ctx,
1207894d1b3dSPeter Zijlstra 					   enum rtmutex_chainwalk chwalk,
1208894d1b3dSPeter Zijlstra 					   struct wake_q_head *wake_q)
12091696a8beSPeter Zijlstra {
12101696a8beSPeter Zijlstra 	struct task_struct *owner = rt_mutex_owner(lock);
12111696a8beSPeter Zijlstra 	struct rt_mutex_waiter *top_waiter = waiter;
1212830e6accSPeter Zijlstra 	struct rt_mutex_base *next_lock;
12131696a8beSPeter Zijlstra 	int chain_walk = 0, res;
12141696a8beSPeter Zijlstra 
1215e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1216e0aad5b4SPeter Zijlstra 
1217397335f0SThomas Gleixner 	/*
1218397335f0SThomas Gleixner 	 * Early deadlock detection. We really don't want the task to
1219397335f0SThomas Gleixner 	 * enqueue on itself just to untangle the mess later. It's not
1220397335f0SThomas Gleixner 	 * only an optimization. We drop the locks, so another waiter
1221397335f0SThomas Gleixner 	 * can come in before the chain walk detects the deadlock. So
1222397335f0SThomas Gleixner 	 * the other will detect the deadlock and return -EDEADLOCK,
1223397335f0SThomas Gleixner 	 * which is wrong, as the other waiter is not in a deadlock
1224397335f0SThomas Gleixner 	 * situation.
122502ea9fc9SPeter Zijlstra 	 *
122602ea9fc9SPeter Zijlstra 	 * Except for ww_mutex, in that case the chain walk must already deal
122702ea9fc9SPeter Zijlstra 	 * with spurious cycles, see the comments at [3] and [6].
1228397335f0SThomas Gleixner 	 */
122902ea9fc9SPeter Zijlstra 	if (owner == task && !(build_ww_mutex() && ww_ctx))
1230397335f0SThomas Gleixner 		return -EDEADLK;
1231397335f0SThomas Gleixner 
1232b4abf910SThomas Gleixner 	raw_spin_lock(&task->pi_lock);
12331696a8beSPeter Zijlstra 	waiter->task = task;
12341696a8beSPeter Zijlstra 	waiter->lock = lock;
1235715f7f9eSPeter Zijlstra 	waiter_update_prio(waiter, task);
1236f7853c34SPeter Zijlstra 	waiter_clone_prio(waiter, task);
12371696a8beSPeter Zijlstra 
12381696a8beSPeter Zijlstra 	/* Get the top priority waiter on the lock */
12391696a8beSPeter Zijlstra 	if (rt_mutex_has_waiters(lock))
12401696a8beSPeter Zijlstra 		top_waiter = rt_mutex_top_waiter(lock);
1241fb00aca4SPeter Zijlstra 	rt_mutex_enqueue(lock, waiter);
12421696a8beSPeter Zijlstra 
12431696a8beSPeter Zijlstra 	task->pi_blocked_on = waiter;
12441696a8beSPeter Zijlstra 
1245b4abf910SThomas Gleixner 	raw_spin_unlock(&task->pi_lock);
12461696a8beSPeter Zijlstra 
1247add46132SPeter Zijlstra 	if (build_ww_mutex() && ww_ctx) {
1248add46132SPeter Zijlstra 		struct rt_mutex *rtm;
1249add46132SPeter Zijlstra 
1250add46132SPeter Zijlstra 		/* Check whether the waiter should back out immediately */
1251add46132SPeter Zijlstra 		rtm = container_of(lock, struct rt_mutex, rtmutex);
1252894d1b3dSPeter Zijlstra 		res = __ww_mutex_add_waiter(waiter, rtm, ww_ctx, wake_q);
125337e8abffSThomas Gleixner 		if (res) {
125437e8abffSThomas Gleixner 			raw_spin_lock(&task->pi_lock);
125537e8abffSThomas Gleixner 			rt_mutex_dequeue(lock, waiter);
125637e8abffSThomas Gleixner 			task->pi_blocked_on = NULL;
125737e8abffSThomas Gleixner 			raw_spin_unlock(&task->pi_lock);
1258add46132SPeter Zijlstra 			return res;
1259add46132SPeter Zijlstra 		}
126037e8abffSThomas Gleixner 	}
1261add46132SPeter Zijlstra 
12621696a8beSPeter Zijlstra 	if (!owner)
12631696a8beSPeter Zijlstra 		return 0;
12641696a8beSPeter Zijlstra 
1265b4abf910SThomas Gleixner 	raw_spin_lock(&owner->pi_lock);
126682084984SThomas Gleixner 	if (waiter == rt_mutex_top_waiter(lock)) {
1267fb00aca4SPeter Zijlstra 		rt_mutex_dequeue_pi(owner, top_waiter);
1268fb00aca4SPeter Zijlstra 		rt_mutex_enqueue_pi(owner, waiter);
12691696a8beSPeter Zijlstra 
1270f7853c34SPeter Zijlstra 		rt_mutex_adjust_prio(lock, owner);
12711696a8beSPeter Zijlstra 		if (owner->pi_blocked_on)
12721696a8beSPeter Zijlstra 			chain_walk = 1;
12738930ed80SThomas Gleixner 	} else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
12741696a8beSPeter Zijlstra 		chain_walk = 1;
127582084984SThomas Gleixner 	}
12761696a8beSPeter Zijlstra 
127782084984SThomas Gleixner 	/* Store the lock on which owner is blocked or NULL */
127882084984SThomas Gleixner 	next_lock = task_blocked_on_lock(owner);
127982084984SThomas Gleixner 
1280b4abf910SThomas Gleixner 	raw_spin_unlock(&owner->pi_lock);
128182084984SThomas Gleixner 	/*
128282084984SThomas Gleixner 	 * Even if full deadlock detection is on, if the owner is not
128382084984SThomas Gleixner 	 * blocked itself, we can avoid finding this out in the chain
128482084984SThomas Gleixner 	 * walk.
128582084984SThomas Gleixner 	 */
128682084984SThomas Gleixner 	if (!chain_walk || !next_lock)
12871696a8beSPeter Zijlstra 		return 0;
12881696a8beSPeter Zijlstra 
12891696a8beSPeter Zijlstra 	/*
12901696a8beSPeter Zijlstra 	 * The owner can't disappear while holding a lock,
12911696a8beSPeter Zijlstra 	 * so the owner struct is protected by wait_lock.
12921696a8beSPeter Zijlstra 	 * Gets dropped in rt_mutex_adjust_prio_chain()!
12931696a8beSPeter Zijlstra 	 */
12941696a8beSPeter Zijlstra 	get_task_struct(owner);
12951696a8beSPeter Zijlstra 
1296abfdccd6SJohn Stultz 	raw_spin_unlock_irq_wake(&lock->wait_lock, wake_q);
12971696a8beSPeter Zijlstra 
12988930ed80SThomas Gleixner 	res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
129982084984SThomas Gleixner 					 next_lock, waiter, task);
13001696a8beSPeter Zijlstra 
1301b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
13021696a8beSPeter Zijlstra 
13031696a8beSPeter Zijlstra 	return res;
13041696a8beSPeter Zijlstra }
13051696a8beSPeter Zijlstra 
13061696a8beSPeter Zijlstra /*
13079f40a51aSDavidlohr Bueso  * Remove the top waiter from the current tasks pi waiter tree and
130845ab4effSDavidlohr Bueso  * queue it up.
13091696a8beSPeter Zijlstra  *
1310b4abf910SThomas Gleixner  * Called with lock->wait_lock held and interrupts disabled.
13111696a8beSPeter Zijlstra  */
mark_wakeup_next_waiter(struct rt_wake_q_head * wqh,struct rt_mutex_base * lock)13127980aa39SThomas Gleixner static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh,
1313830e6accSPeter Zijlstra 					    struct rt_mutex_base *lock)
13141696a8beSPeter Zijlstra {
13151696a8beSPeter Zijlstra 	struct rt_mutex_waiter *waiter;
13161696a8beSPeter Zijlstra 
1317f7853c34SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1318f7853c34SPeter Zijlstra 
1319b4abf910SThomas Gleixner 	raw_spin_lock(&current->pi_lock);
13201696a8beSPeter Zijlstra 
13211696a8beSPeter Zijlstra 	waiter = rt_mutex_top_waiter(lock);
13221696a8beSPeter Zijlstra 
13231696a8beSPeter Zijlstra 	/*
1324acd58620SPeter Zijlstra 	 * Remove it from current->pi_waiters and deboost.
1325acd58620SPeter Zijlstra 	 *
1326acd58620SPeter Zijlstra 	 * We must in fact deboost here in order to ensure we call
1327acd58620SPeter Zijlstra 	 * rt_mutex_setprio() to update p->pi_top_task before the
1328acd58620SPeter Zijlstra 	 * task unblocks.
13291696a8beSPeter Zijlstra 	 */
1330fb00aca4SPeter Zijlstra 	rt_mutex_dequeue_pi(current, waiter);
1331f7853c34SPeter Zijlstra 	rt_mutex_adjust_prio(lock, current);
13321696a8beSPeter Zijlstra 
133327e35715SThomas Gleixner 	/*
133427e35715SThomas Gleixner 	 * As we are waking up the top waiter, and the waiter stays
133527e35715SThomas Gleixner 	 * queued on the lock until it gets the lock, this lock
133627e35715SThomas Gleixner 	 * obviously has waiters. Just set the bit here and this has
133727e35715SThomas Gleixner 	 * the added benefit of forcing all new tasks into the
133827e35715SThomas Gleixner 	 * slow path making sure no task of lower priority than
133927e35715SThomas Gleixner 	 * the top waiter can steal this lock.
134027e35715SThomas Gleixner 	 */
134127e35715SThomas Gleixner 	lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
13421696a8beSPeter Zijlstra 
1343acd58620SPeter Zijlstra 	/*
1344acd58620SPeter Zijlstra 	 * We deboosted before waking the top waiter task such that we don't
1345acd58620SPeter Zijlstra 	 * run two tasks with the 'same' priority (and ensure the
1346acd58620SPeter Zijlstra 	 * p->pi_top_task pointer points to a blocked task). This however can
1347acd58620SPeter Zijlstra 	 * lead to priority inversion if we would get preempted after the
1348acd58620SPeter Zijlstra 	 * deboost but before waking our donor task, hence the preempt_disable()
1349acd58620SPeter Zijlstra 	 * before unlock.
1350acd58620SPeter Zijlstra 	 *
13517980aa39SThomas Gleixner 	 * Pairs with preempt_enable() in rt_mutex_wake_up_q();
1352acd58620SPeter Zijlstra 	 */
1353acd58620SPeter Zijlstra 	preempt_disable();
13547980aa39SThomas Gleixner 	rt_mutex_wake_q_add(wqh, waiter);
1355acd58620SPeter Zijlstra 	raw_spin_unlock(&current->pi_lock);
13561696a8beSPeter Zijlstra }
13571696a8beSPeter Zijlstra 
__rt_mutex_slowtrylock(struct rt_mutex_base * lock)1358e17ba59bSThomas Gleixner static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1359e17ba59bSThomas Gleixner {
1360e17ba59bSThomas Gleixner 	int ret = try_to_take_rt_mutex(lock, current, NULL);
1361e17ba59bSThomas Gleixner 
1362e17ba59bSThomas Gleixner 	/*
1363e17ba59bSThomas Gleixner 	 * try_to_take_rt_mutex() sets the lock waiters bit
1364e17ba59bSThomas Gleixner 	 * unconditionally. Clean this up.
1365e17ba59bSThomas Gleixner 	 */
13661c0908d8SMel Gorman 	fixup_rt_mutex_waiters(lock, true);
1367e17ba59bSThomas Gleixner 
1368e17ba59bSThomas Gleixner 	return ret;
1369e17ba59bSThomas Gleixner }
1370e17ba59bSThomas Gleixner 
1371e17ba59bSThomas Gleixner /*
1372e17ba59bSThomas Gleixner  * Slow path try-lock function:
1373e17ba59bSThomas Gleixner  */
rt_mutex_slowtrylock(struct rt_mutex_base * lock)1374e17ba59bSThomas Gleixner static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1375e17ba59bSThomas Gleixner {
1376e17ba59bSThomas Gleixner 	unsigned long flags;
1377e17ba59bSThomas Gleixner 	int ret;
1378e17ba59bSThomas Gleixner 
1379e17ba59bSThomas Gleixner 	/*
1380e17ba59bSThomas Gleixner 	 * If the lock already has an owner we fail to get the lock.
1381e17ba59bSThomas Gleixner 	 * This can be done without taking the @lock->wait_lock as
1382e17ba59bSThomas Gleixner 	 * it is only being read, and this is a trylock anyway.
1383e17ba59bSThomas Gleixner 	 */
1384e17ba59bSThomas Gleixner 	if (rt_mutex_owner(lock))
1385e17ba59bSThomas Gleixner 		return 0;
1386e17ba59bSThomas Gleixner 
1387e17ba59bSThomas Gleixner 	/*
1388e17ba59bSThomas Gleixner 	 * The mutex has currently no owner. Lock the wait lock and try to
1389e17ba59bSThomas Gleixner 	 * acquire the lock. We use irqsave here to support early boot calls.
1390e17ba59bSThomas Gleixner 	 */
1391e17ba59bSThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1392e17ba59bSThomas Gleixner 
1393e17ba59bSThomas Gleixner 	ret = __rt_mutex_slowtrylock(lock);
1394e17ba59bSThomas Gleixner 
1395e17ba59bSThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1396e17ba59bSThomas Gleixner 
1397e17ba59bSThomas Gleixner 	return ret;
1398e17ba59bSThomas Gleixner }
1399e17ba59bSThomas Gleixner 
__rt_mutex_trylock(struct rt_mutex_base * lock)1400e17ba59bSThomas Gleixner static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock)
1401e17ba59bSThomas Gleixner {
1402e17ba59bSThomas Gleixner 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1403e17ba59bSThomas Gleixner 		return 1;
1404e17ba59bSThomas Gleixner 
1405e17ba59bSThomas Gleixner 	return rt_mutex_slowtrylock(lock);
1406e17ba59bSThomas Gleixner }
1407e17ba59bSThomas Gleixner 
1408e17ba59bSThomas Gleixner /*
1409e17ba59bSThomas Gleixner  * Slow path to release a rt-mutex.
1410e17ba59bSThomas Gleixner  */
rt_mutex_slowunlock(struct rt_mutex_base * lock)1411e17ba59bSThomas Gleixner static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
1412e17ba59bSThomas Gleixner {
1413e17ba59bSThomas Gleixner 	DEFINE_RT_WAKE_Q(wqh);
1414e17ba59bSThomas Gleixner 	unsigned long flags;
1415e17ba59bSThomas Gleixner 
1416e17ba59bSThomas Gleixner 	/* irqsave required to support early boot calls */
1417e17ba59bSThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1418e17ba59bSThomas Gleixner 
1419e17ba59bSThomas Gleixner 	debug_rt_mutex_unlock(lock);
1420e17ba59bSThomas Gleixner 
1421e17ba59bSThomas Gleixner 	/*
1422e17ba59bSThomas Gleixner 	 * We must be careful here if the fast path is enabled. If we
1423e17ba59bSThomas Gleixner 	 * have no waiters queued we cannot set owner to NULL here
1424e17ba59bSThomas Gleixner 	 * because of:
1425e17ba59bSThomas Gleixner 	 *
1426e17ba59bSThomas Gleixner 	 * foo->lock->owner = NULL;
1427e17ba59bSThomas Gleixner 	 *			rtmutex_lock(foo->lock);   <- fast path
1428e17ba59bSThomas Gleixner 	 *			free = atomic_dec_and_test(foo->refcnt);
1429e17ba59bSThomas Gleixner 	 *			rtmutex_unlock(foo->lock); <- fast path
1430e17ba59bSThomas Gleixner 	 *			if (free)
1431e17ba59bSThomas Gleixner 	 *				kfree(foo);
1432e17ba59bSThomas Gleixner 	 * raw_spin_unlock(foo->lock->wait_lock);
1433e17ba59bSThomas Gleixner 	 *
1434e17ba59bSThomas Gleixner 	 * So for the fastpath enabled kernel:
1435e17ba59bSThomas Gleixner 	 *
1436e17ba59bSThomas Gleixner 	 * Nothing can set the waiters bit as long as we hold
1437e17ba59bSThomas Gleixner 	 * lock->wait_lock. So we do the following sequence:
1438e17ba59bSThomas Gleixner 	 *
1439e17ba59bSThomas Gleixner 	 *	owner = rt_mutex_owner(lock);
1440e17ba59bSThomas Gleixner 	 *	clear_rt_mutex_waiters(lock);
1441e17ba59bSThomas Gleixner 	 *	raw_spin_unlock(&lock->wait_lock);
1442e17ba59bSThomas Gleixner 	 *	if (cmpxchg(&lock->owner, owner, 0) == owner)
1443e17ba59bSThomas Gleixner 	 *		return;
1444e17ba59bSThomas Gleixner 	 *	goto retry;
1445e17ba59bSThomas Gleixner 	 *
1446e17ba59bSThomas Gleixner 	 * The fastpath disabled variant is simple as all access to
1447e17ba59bSThomas Gleixner 	 * lock->owner is serialized by lock->wait_lock:
1448e17ba59bSThomas Gleixner 	 *
1449e17ba59bSThomas Gleixner 	 *	lock->owner = NULL;
1450e17ba59bSThomas Gleixner 	 *	raw_spin_unlock(&lock->wait_lock);
1451e17ba59bSThomas Gleixner 	 */
1452e17ba59bSThomas Gleixner 	while (!rt_mutex_has_waiters(lock)) {
1453e17ba59bSThomas Gleixner 		/* Drops lock->wait_lock ! */
1454e17ba59bSThomas Gleixner 		if (unlock_rt_mutex_safe(lock, flags) == true)
1455e17ba59bSThomas Gleixner 			return;
1456e17ba59bSThomas Gleixner 		/* Relock the rtmutex and try again */
1457e17ba59bSThomas Gleixner 		raw_spin_lock_irqsave(&lock->wait_lock, flags);
1458e17ba59bSThomas Gleixner 	}
1459e17ba59bSThomas Gleixner 
1460e17ba59bSThomas Gleixner 	/*
1461e17ba59bSThomas Gleixner 	 * The wakeup next waiter path does not suffer from the above
1462e17ba59bSThomas Gleixner 	 * race. See the comments there.
1463e17ba59bSThomas Gleixner 	 *
1464e17ba59bSThomas Gleixner 	 * Queue the next waiter for wakeup once we release the wait_lock.
1465e17ba59bSThomas Gleixner 	 */
1466e17ba59bSThomas Gleixner 	mark_wakeup_next_waiter(&wqh, lock);
1467e17ba59bSThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1468e17ba59bSThomas Gleixner 
1469e17ba59bSThomas Gleixner 	rt_mutex_wake_up_q(&wqh);
1470e17ba59bSThomas Gleixner }
1471e17ba59bSThomas Gleixner 
__rt_mutex_unlock(struct rt_mutex_base * lock)1472e17ba59bSThomas Gleixner static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
1473e17ba59bSThomas Gleixner {
1474e17ba59bSThomas Gleixner 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1475e17ba59bSThomas Gleixner 		return;
1476e17ba59bSThomas Gleixner 
1477e17ba59bSThomas Gleixner 	rt_mutex_slowunlock(lock);
1478e17ba59bSThomas Gleixner }
1479e17ba59bSThomas Gleixner 
1480992caf7fSSteven Rostedt #ifdef CONFIG_SMP
rtmutex_spin_on_owner(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * owner)1481992caf7fSSteven Rostedt static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1482992caf7fSSteven Rostedt 				  struct rt_mutex_waiter *waiter,
1483992caf7fSSteven Rostedt 				  struct task_struct *owner)
1484992caf7fSSteven Rostedt {
1485992caf7fSSteven Rostedt 	bool res = true;
1486992caf7fSSteven Rostedt 
1487992caf7fSSteven Rostedt 	rcu_read_lock();
1488992caf7fSSteven Rostedt 	for (;;) {
1489992caf7fSSteven Rostedt 		/* If owner changed, trylock again. */
1490992caf7fSSteven Rostedt 		if (owner != rt_mutex_owner(lock))
1491992caf7fSSteven Rostedt 			break;
1492992caf7fSSteven Rostedt 		/*
1493992caf7fSSteven Rostedt 		 * Ensure that @owner is dereferenced after checking that
1494992caf7fSSteven Rostedt 		 * the lock owner still matches @owner. If that fails,
1495992caf7fSSteven Rostedt 		 * @owner might point to freed memory. If it still matches,
1496992caf7fSSteven Rostedt 		 * the rcu_read_lock() ensures the memory stays valid.
1497992caf7fSSteven Rostedt 		 */
1498992caf7fSSteven Rostedt 		barrier();
1499992caf7fSSteven Rostedt 		/*
1500992caf7fSSteven Rostedt 		 * Stop spinning when:
1501992caf7fSSteven Rostedt 		 *  - the lock owner has been scheduled out
1502992caf7fSSteven Rostedt 		 *  - current is not longer the top waiter
1503992caf7fSSteven Rostedt 		 *  - current is requested to reschedule (redundant
1504992caf7fSSteven Rostedt 		 *    for CONFIG_PREEMPT_RCU=y)
1505992caf7fSSteven Rostedt 		 *  - the VCPU on which owner runs is preempted
1506992caf7fSSteven Rostedt 		 */
1507c0bed69dSKefeng Wang 		if (!owner_on_cpu(owner) || need_resched() ||
1508f16cc980SThomas Gleixner 		    !rt_mutex_waiter_is_top_waiter(lock, waiter)) {
1509992caf7fSSteven Rostedt 			res = false;
1510992caf7fSSteven Rostedt 			break;
1511992caf7fSSteven Rostedt 		}
1512992caf7fSSteven Rostedt 		cpu_relax();
1513992caf7fSSteven Rostedt 	}
1514992caf7fSSteven Rostedt 	rcu_read_unlock();
1515992caf7fSSteven Rostedt 	return res;
1516992caf7fSSteven Rostedt }
1517992caf7fSSteven Rostedt #else
rtmutex_spin_on_owner(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * owner)1518992caf7fSSteven Rostedt static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1519992caf7fSSteven Rostedt 				  struct rt_mutex_waiter *waiter,
1520992caf7fSSteven Rostedt 				  struct task_struct *owner)
1521992caf7fSSteven Rostedt {
1522992caf7fSSteven Rostedt 	return false;
1523992caf7fSSteven Rostedt }
1524992caf7fSSteven Rostedt #endif
1525992caf7fSSteven Rostedt 
1526e17ba59bSThomas Gleixner #ifdef RT_MUTEX_BUILD_MUTEX
1527e17ba59bSThomas Gleixner /*
1528e17ba59bSThomas Gleixner  * Functions required for:
1529e17ba59bSThomas Gleixner  *	- rtmutex, futex on all kernels
1530e17ba59bSThomas Gleixner  *	- mutex and rwsem substitutions on RT kernels
1531e17ba59bSThomas Gleixner  */
1532e17ba59bSThomas Gleixner 
15331696a8beSPeter Zijlstra /*
15341696a8beSPeter Zijlstra  * Remove a waiter from a lock and give up
15351696a8beSPeter Zijlstra  *
1536e17ba59bSThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled. It must
15371696a8beSPeter Zijlstra  * have just failed to try_to_take_rt_mutex().
15381696a8beSPeter Zijlstra  */
remove_waiter(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)1539830e6accSPeter Zijlstra static void __sched remove_waiter(struct rt_mutex_base *lock,
15401696a8beSPeter Zijlstra 				  struct rt_mutex_waiter *waiter)
15411696a8beSPeter Zijlstra {
15421ca7b860SThomas Gleixner 	bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
15431696a8beSPeter Zijlstra 	struct task_struct *owner = rt_mutex_owner(lock);
1544830e6accSPeter Zijlstra 	struct rt_mutex_base *next_lock;
15451696a8beSPeter Zijlstra 
1546e0aad5b4SPeter Zijlstra 	lockdep_assert_held(&lock->wait_lock);
1547e0aad5b4SPeter Zijlstra 
1548b4abf910SThomas Gleixner 	raw_spin_lock(&current->pi_lock);
1549fb00aca4SPeter Zijlstra 	rt_mutex_dequeue(lock, waiter);
15501696a8beSPeter Zijlstra 	current->pi_blocked_on = NULL;
1551b4abf910SThomas Gleixner 	raw_spin_unlock(&current->pi_lock);
15521696a8beSPeter Zijlstra 
15531ca7b860SThomas Gleixner 	/*
15541ca7b860SThomas Gleixner 	 * Only update priority if the waiter was the highest priority
15551ca7b860SThomas Gleixner 	 * waiter of the lock and there is an owner to update.
15561ca7b860SThomas Gleixner 	 */
15571ca7b860SThomas Gleixner 	if (!owner || !is_top_waiter)
15581696a8beSPeter Zijlstra 		return;
15591696a8beSPeter Zijlstra 
1560b4abf910SThomas Gleixner 	raw_spin_lock(&owner->pi_lock);
15611696a8beSPeter Zijlstra 
1562fb00aca4SPeter Zijlstra 	rt_mutex_dequeue_pi(owner, waiter);
15631696a8beSPeter Zijlstra 
15641ca7b860SThomas Gleixner 	if (rt_mutex_has_waiters(lock))
15651ca7b860SThomas Gleixner 		rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
15661696a8beSPeter Zijlstra 
1567f7853c34SPeter Zijlstra 	rt_mutex_adjust_prio(lock, owner);
15681696a8beSPeter Zijlstra 
156982084984SThomas Gleixner 	/* Store the lock on which owner is blocked or NULL */
157082084984SThomas Gleixner 	next_lock = task_blocked_on_lock(owner);
15711696a8beSPeter Zijlstra 
1572b4abf910SThomas Gleixner 	raw_spin_unlock(&owner->pi_lock);
15731696a8beSPeter Zijlstra 
15741ca7b860SThomas Gleixner 	/*
15751ca7b860SThomas Gleixner 	 * Don't walk the chain, if the owner task is not blocked
15761ca7b860SThomas Gleixner 	 * itself.
15771ca7b860SThomas Gleixner 	 */
157882084984SThomas Gleixner 	if (!next_lock)
15791696a8beSPeter Zijlstra 		return;
15801696a8beSPeter Zijlstra 
15811696a8beSPeter Zijlstra 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
15821696a8beSPeter Zijlstra 	get_task_struct(owner);
15831696a8beSPeter Zijlstra 
1584b4abf910SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
15851696a8beSPeter Zijlstra 
15868930ed80SThomas Gleixner 	rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
15878930ed80SThomas Gleixner 				   next_lock, NULL, current);
15881696a8beSPeter Zijlstra 
1589b4abf910SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
15901696a8beSPeter Zijlstra }
15911696a8beSPeter Zijlstra 
15921696a8beSPeter Zijlstra /**
1593ebbdc41eSThomas Gleixner  * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop
15941696a8beSPeter Zijlstra  * @lock:		 the rt_mutex to take
1595add46132SPeter Zijlstra  * @ww_ctx:		 WW mutex context pointer
15961696a8beSPeter Zijlstra  * @state:		 the state the task should block in (TASK_INTERRUPTIBLE
15971696a8beSPeter Zijlstra  *			 or TASK_UNINTERRUPTIBLE)
15981696a8beSPeter Zijlstra  * @timeout:		 the pre-initialized and started timer, or NULL for none
15991696a8beSPeter Zijlstra  * @waiter:		 the pre-initialized rt_mutex_waiter
16004a077914SJohn Stultz  * @wake_q:		 wake_q of tasks to wake when we drop the lock->wait_lock
16011696a8beSPeter Zijlstra  *
1602b4abf910SThomas Gleixner  * Must be called with lock->wait_lock held and interrupts disabled
16031696a8beSPeter Zijlstra  */
rt_mutex_slowlock_block(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state,struct hrtimer_sleeper * timeout,struct rt_mutex_waiter * waiter,struct wake_q_head * wake_q)1604ebbdc41eSThomas Gleixner static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
1605add46132SPeter Zijlstra 					   struct ww_acquire_ctx *ww_ctx,
1606830e6accSPeter Zijlstra 					   unsigned int state,
16071696a8beSPeter Zijlstra 					   struct hrtimer_sleeper *timeout,
16084a077914SJohn Stultz 					   struct rt_mutex_waiter *waiter,
16094a077914SJohn Stultz 					   struct wake_q_head *wake_q)
161077abd3b7SSebastian Andrzej Siewior 	__releases(&lock->wait_lock) __acquires(&lock->wait_lock)
16111696a8beSPeter Zijlstra {
1612add46132SPeter Zijlstra 	struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1613992caf7fSSteven Rostedt 	struct task_struct *owner;
16141696a8beSPeter Zijlstra 	int ret = 0;
16151696a8beSPeter Zijlstra 
1616*b76b44fbSWaiman Long 	lockevent_inc(rtmutex_slow_block);
16171696a8beSPeter Zijlstra 	for (;;) {
16181696a8beSPeter Zijlstra 		/* Try to acquire the lock: */
1619*b76b44fbSWaiman Long 		if (try_to_take_rt_mutex(lock, current, waiter)) {
1620*b76b44fbSWaiman Long 			lockevent_inc(rtmutex_slow_acq3);
16211696a8beSPeter Zijlstra 			break;
1622*b76b44fbSWaiman Long 		}
16231696a8beSPeter Zijlstra 
1624a51a327fSThomas Gleixner 		if (timeout && !timeout->task) {
16251696a8beSPeter Zijlstra 			ret = -ETIMEDOUT;
1626a51a327fSThomas Gleixner 			break;
1627a51a327fSThomas Gleixner 		}
1628a51a327fSThomas Gleixner 		if (signal_pending_state(state, current)) {
1629a51a327fSThomas Gleixner 			ret = -EINTR;
16301696a8beSPeter Zijlstra 			break;
16311696a8beSPeter Zijlstra 		}
16321696a8beSPeter Zijlstra 
1633add46132SPeter Zijlstra 		if (build_ww_mutex() && ww_ctx) {
1634add46132SPeter Zijlstra 			ret = __ww_mutex_check_kill(rtm, waiter, ww_ctx);
1635add46132SPeter Zijlstra 			if (ret)
1636add46132SPeter Zijlstra 				break;
1637add46132SPeter Zijlstra 		}
1638add46132SPeter Zijlstra 
1639992caf7fSSteven Rostedt 		if (waiter == rt_mutex_top_waiter(lock))
1640992caf7fSSteven Rostedt 			owner = rt_mutex_owner(lock);
1641992caf7fSSteven Rostedt 		else
1642992caf7fSSteven Rostedt 			owner = NULL;
1643abfdccd6SJohn Stultz 		raw_spin_unlock_irq_wake(&lock->wait_lock, wake_q);
16441696a8beSPeter Zijlstra 
1645*b76b44fbSWaiman Long 		if (!owner || !rtmutex_spin_on_owner(lock, waiter, owner)) {
1646*b76b44fbSWaiman Long 			lockevent_inc(rtmutex_slow_sleep);
1647d14f9e93SSebastian Andrzej Siewior 			rt_mutex_schedule();
1648*b76b44fbSWaiman Long 		}
16491696a8beSPeter Zijlstra 
1650b4abf910SThomas Gleixner 		raw_spin_lock_irq(&lock->wait_lock);
16511696a8beSPeter Zijlstra 		set_current_state(state);
16521696a8beSPeter Zijlstra 	}
16531696a8beSPeter Zijlstra 
1654afffc6c1SDavidlohr Bueso 	__set_current_state(TASK_RUNNING);
16551696a8beSPeter Zijlstra 	return ret;
16561696a8beSPeter Zijlstra }
16571696a8beSPeter Zijlstra 
rt_mutex_handle_deadlock(int res,int detect_deadlock,struct rt_mutex_base * lock,struct rt_mutex_waiter * w)1658d7a2edb8SThomas Gleixner static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
1659d33d2603SRoland Xu 					     struct rt_mutex_base *lock,
16603d5c9340SThomas Gleixner 					     struct rt_mutex_waiter *w)
16613d5c9340SThomas Gleixner {
16623d5c9340SThomas Gleixner 	/*
16633d5c9340SThomas Gleixner 	 * If the result is not -EDEADLOCK or the caller requested
16643d5c9340SThomas Gleixner 	 * deadlock detection, nothing to do here.
16653d5c9340SThomas Gleixner 	 */
16663d5c9340SThomas Gleixner 	if (res != -EDEADLOCK || detect_deadlock)
16673d5c9340SThomas Gleixner 		return;
16683d5c9340SThomas Gleixner 
1669add46132SPeter Zijlstra 	if (build_ww_mutex() && w->ww_ctx)
1670add46132SPeter Zijlstra 		return;
1671add46132SPeter Zijlstra 
1672d33d2603SRoland Xu 	raw_spin_unlock_irq(&lock->wait_lock);
1673d33d2603SRoland Xu 
16746d41c675SSebastian Andrzej Siewior 	WARN(1, "rtmutex deadlock detected\n");
1675d33d2603SRoland Xu 
16763d5c9340SThomas Gleixner 	while (1) {
16773d5c9340SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
1678d14f9e93SSebastian Andrzej Siewior 		rt_mutex_schedule();
16793d5c9340SThomas Gleixner 	}
16803d5c9340SThomas Gleixner }
16813d5c9340SThomas Gleixner 
1682ebbdc41eSThomas Gleixner /**
1683ebbdc41eSThomas Gleixner  * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held
1684ebbdc41eSThomas Gleixner  * @lock:	The rtmutex to block lock
1685add46132SPeter Zijlstra  * @ww_ctx:	WW mutex context pointer
1686ebbdc41eSThomas Gleixner  * @state:	The task state for sleeping
1687ebbdc41eSThomas Gleixner  * @chwalk:	Indicator whether full or partial chainwalk is requested
1688ebbdc41eSThomas Gleixner  * @waiter:	Initializer waiter for blocking
1689894d1b3dSPeter Zijlstra  * @wake_q:	The wake_q to wake tasks after we release the wait_lock
16901696a8beSPeter Zijlstra  */
__rt_mutex_slowlock(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state,enum rtmutex_chainwalk chwalk,struct rt_mutex_waiter * waiter,struct wake_q_head * wake_q)1691ebbdc41eSThomas Gleixner static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,
1692add46132SPeter Zijlstra 				       struct ww_acquire_ctx *ww_ctx,
1693830e6accSPeter Zijlstra 				       unsigned int state,
1694ebbdc41eSThomas Gleixner 				       enum rtmutex_chainwalk chwalk,
1695894d1b3dSPeter Zijlstra 				       struct rt_mutex_waiter *waiter,
1696894d1b3dSPeter Zijlstra 				       struct wake_q_head *wake_q)
1697ebbdc41eSThomas Gleixner {
1698add46132SPeter Zijlstra 	struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1699add46132SPeter Zijlstra 	struct ww_mutex *ww = ww_container_of(rtm);
1700ebbdc41eSThomas Gleixner 	int ret;
1701ebbdc41eSThomas Gleixner 
1702ebbdc41eSThomas Gleixner 	lockdep_assert_held(&lock->wait_lock);
1703*b76b44fbSWaiman Long 	lockevent_inc(rtmutex_slowlock);
1704ebbdc41eSThomas Gleixner 
1705ebbdc41eSThomas Gleixner 	/* Try to acquire the lock again: */
1706add46132SPeter Zijlstra 	if (try_to_take_rt_mutex(lock, current, NULL)) {
1707add46132SPeter Zijlstra 		if (build_ww_mutex() && ww_ctx) {
1708894d1b3dSPeter Zijlstra 			__ww_mutex_check_waiters(rtm, ww_ctx, wake_q);
1709add46132SPeter Zijlstra 			ww_mutex_lock_acquired(ww, ww_ctx);
1710add46132SPeter Zijlstra 		}
1711*b76b44fbSWaiman Long 		lockevent_inc(rtmutex_slow_acq1);
1712ebbdc41eSThomas Gleixner 		return 0;
1713add46132SPeter Zijlstra 	}
1714ebbdc41eSThomas Gleixner 
1715ebbdc41eSThomas Gleixner 	set_current_state(state);
1716ebbdc41eSThomas Gleixner 
1717ee042be1SNamhyung Kim 	trace_contention_begin(lock, LCB_F_RT);
1718ee042be1SNamhyung Kim 
1719894d1b3dSPeter Zijlstra 	ret = task_blocks_on_rt_mutex(lock, waiter, current, ww_ctx, chwalk, wake_q);
1720ebbdc41eSThomas Gleixner 	if (likely(!ret))
17214a077914SJohn Stultz 		ret = rt_mutex_slowlock_block(lock, ww_ctx, state, NULL, waiter, wake_q);
1722ebbdc41eSThomas Gleixner 
1723add46132SPeter Zijlstra 	if (likely(!ret)) {
1724add46132SPeter Zijlstra 		/* acquired the lock */
1725add46132SPeter Zijlstra 		if (build_ww_mutex() && ww_ctx) {
1726add46132SPeter Zijlstra 			if (!ww_ctx->is_wait_die)
1727894d1b3dSPeter Zijlstra 				__ww_mutex_check_waiters(rtm, ww_ctx, wake_q);
1728add46132SPeter Zijlstra 			ww_mutex_lock_acquired(ww, ww_ctx);
1729add46132SPeter Zijlstra 		}
1730*b76b44fbSWaiman Long 		lockevent_inc(rtmutex_slow_acq2);
1731add46132SPeter Zijlstra 	} else {
1732ebbdc41eSThomas Gleixner 		__set_current_state(TASK_RUNNING);
1733ebbdc41eSThomas Gleixner 		remove_waiter(lock, waiter);
1734d33d2603SRoland Xu 		rt_mutex_handle_deadlock(ret, chwalk, lock, waiter);
1735*b76b44fbSWaiman Long 		lockevent_inc(rtmutex_deadlock);
1736ebbdc41eSThomas Gleixner 	}
1737ebbdc41eSThomas Gleixner 
1738ebbdc41eSThomas Gleixner 	/*
1739ebbdc41eSThomas Gleixner 	 * try_to_take_rt_mutex() sets the waiter bit
1740ebbdc41eSThomas Gleixner 	 * unconditionally. We might have to fix that up.
1741ebbdc41eSThomas Gleixner 	 */
17421c0908d8SMel Gorman 	fixup_rt_mutex_waiters(lock, true);
1743ee042be1SNamhyung Kim 
1744ee042be1SNamhyung Kim 	trace_contention_end(lock, ret);
1745ee042be1SNamhyung Kim 
1746ebbdc41eSThomas Gleixner 	return ret;
1747ebbdc41eSThomas Gleixner }
1748ebbdc41eSThomas Gleixner 
__rt_mutex_slowlock_locked(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state,struct wake_q_head * wake_q)1749ebbdc41eSThomas Gleixner static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock,
1750add46132SPeter Zijlstra 					     struct ww_acquire_ctx *ww_ctx,
1751894d1b3dSPeter Zijlstra 					     unsigned int state,
1752894d1b3dSPeter Zijlstra 					     struct wake_q_head *wake_q)
17531696a8beSPeter Zijlstra {
17541696a8beSPeter Zijlstra 	struct rt_mutex_waiter waiter;
1755ebbdc41eSThomas Gleixner 	int ret;
17561696a8beSPeter Zijlstra 
175750809358SPeter Zijlstra 	rt_mutex_init_waiter(&waiter);
1758add46132SPeter Zijlstra 	waiter.ww_ctx = ww_ctx;
17591696a8beSPeter Zijlstra 
1760add46132SPeter Zijlstra 	ret = __rt_mutex_slowlock(lock, ww_ctx, state, RT_MUTEX_MIN_CHAINWALK,
1761894d1b3dSPeter Zijlstra 				  &waiter, wake_q);
1762ebbdc41eSThomas Gleixner 
1763ebbdc41eSThomas Gleixner 	debug_rt_mutex_free_waiter(&waiter);
1764*b76b44fbSWaiman Long 	lockevent_cond_inc(rtmutex_slow_wake, !wake_q_empty(wake_q));
1765ebbdc41eSThomas Gleixner 	return ret;
1766ebbdc41eSThomas Gleixner }
1767ebbdc41eSThomas Gleixner 
1768ebbdc41eSThomas Gleixner /*
1769ebbdc41eSThomas Gleixner  * rt_mutex_slowlock - Locking slowpath invoked when fast path fails
1770ebbdc41eSThomas Gleixner  * @lock:	The rtmutex to block lock
1771add46132SPeter Zijlstra  * @ww_ctx:	WW mutex context pointer
1772ebbdc41eSThomas Gleixner  * @state:	The task state for sleeping
1773ebbdc41eSThomas Gleixner  */
rt_mutex_slowlock(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state)1774ebbdc41eSThomas Gleixner static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
1775add46132SPeter Zijlstra 				     struct ww_acquire_ctx *ww_ctx,
1776ebbdc41eSThomas Gleixner 				     unsigned int state)
1777ebbdc41eSThomas Gleixner {
1778894d1b3dSPeter Zijlstra 	DEFINE_WAKE_Q(wake_q);
1779ebbdc41eSThomas Gleixner 	unsigned long flags;
1780ebbdc41eSThomas Gleixner 	int ret;
1781ebbdc41eSThomas Gleixner 
1782b4abf910SThomas Gleixner 	/*
1783d14f9e93SSebastian Andrzej Siewior 	 * Do all pre-schedule work here, before we queue a waiter and invoke
1784d14f9e93SSebastian Andrzej Siewior 	 * PI -- any such work that trips on rtlock (PREEMPT_RT spinlock) would
1785d14f9e93SSebastian Andrzej Siewior 	 * otherwise recurse back into task_blocks_on_rt_mutex() through
1786d14f9e93SSebastian Andrzej Siewior 	 * rtlock_slowlock() and will then enqueue a second waiter for this
1787d14f9e93SSebastian Andrzej Siewior 	 * same task and things get really confusing real fast.
1788d14f9e93SSebastian Andrzej Siewior 	 */
1789d14f9e93SSebastian Andrzej Siewior 	rt_mutex_pre_schedule();
1790d14f9e93SSebastian Andrzej Siewior 
1791d14f9e93SSebastian Andrzej Siewior 	/*
1792b4abf910SThomas Gleixner 	 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1793b4abf910SThomas Gleixner 	 * be called in early boot if the cmpxchg() fast path is disabled
1794b4abf910SThomas Gleixner 	 * (debug, no architecture support). In this case we will acquire the
1795b4abf910SThomas Gleixner 	 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1796b4abf910SThomas Gleixner 	 * enable interrupts in that early boot case. So we need to use the
1797b4abf910SThomas Gleixner 	 * irqsave/restore variants.
1798b4abf910SThomas Gleixner 	 */
1799b4abf910SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1800894d1b3dSPeter Zijlstra 	ret = __rt_mutex_slowlock_locked(lock, ww_ctx, state, &wake_q);
1801abfdccd6SJohn Stultz 	raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
1802d14f9e93SSebastian Andrzej Siewior 	rt_mutex_post_schedule();
18031696a8beSPeter Zijlstra 
18041696a8beSPeter Zijlstra 	return ret;
18051696a8beSPeter Zijlstra }
18061696a8beSPeter Zijlstra 
__rt_mutex_lock(struct rt_mutex_base * lock,unsigned int state)1807830e6accSPeter Zijlstra static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
1808531ae4b0SThomas Gleixner 					   unsigned int state)
1809531ae4b0SThomas Gleixner {
181045f67f30SThomas Gleixner 	lockdep_assert(!current->pi_blocked_on);
181145f67f30SThomas Gleixner 
1812af9f0063SSebastian Andrzej Siewior 	if (likely(rt_mutex_try_acquire(lock)))
1813531ae4b0SThomas Gleixner 		return 0;
1814531ae4b0SThomas Gleixner 
1815add46132SPeter Zijlstra 	return rt_mutex_slowlock(lock, NULL, state);
1816531ae4b0SThomas Gleixner }
1817e17ba59bSThomas Gleixner #endif /* RT_MUTEX_BUILD_MUTEX */
18181c143c4bSThomas Gleixner 
18191c143c4bSThomas Gleixner #ifdef RT_MUTEX_BUILD_SPINLOCKS
18201c143c4bSThomas Gleixner /*
18211c143c4bSThomas Gleixner  * Functions required for spin/rw_lock substitution on RT kernels
18221c143c4bSThomas Gleixner  */
18231c143c4bSThomas Gleixner 
18241c143c4bSThomas Gleixner /**
18251c143c4bSThomas Gleixner  * rtlock_slowlock_locked - Slow path lock acquisition for RT locks
18261c143c4bSThomas Gleixner  * @lock:	The underlying RT mutex
1827894d1b3dSPeter Zijlstra  * @wake_q:	The wake_q to wake tasks after we release the wait_lock
18281c143c4bSThomas Gleixner  */
rtlock_slowlock_locked(struct rt_mutex_base * lock,struct wake_q_head * wake_q)1829894d1b3dSPeter Zijlstra static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock,
1830894d1b3dSPeter Zijlstra 					   struct wake_q_head *wake_q)
183177abd3b7SSebastian Andrzej Siewior 	__releases(&lock->wait_lock) __acquires(&lock->wait_lock)
18321c143c4bSThomas Gleixner {
18331c143c4bSThomas Gleixner 	struct rt_mutex_waiter waiter;
1834992caf7fSSteven Rostedt 	struct task_struct *owner;
18351c143c4bSThomas Gleixner 
18361c143c4bSThomas Gleixner 	lockdep_assert_held(&lock->wait_lock);
1837*b76b44fbSWaiman Long 	lockevent_inc(rtlock_slowlock);
18381c143c4bSThomas Gleixner 
1839*b76b44fbSWaiman Long 	if (try_to_take_rt_mutex(lock, current, NULL)) {
1840*b76b44fbSWaiman Long 		lockevent_inc(rtlock_slow_acq1);
18411c143c4bSThomas Gleixner 		return;
1842*b76b44fbSWaiman Long 	}
18431c143c4bSThomas Gleixner 
18441c143c4bSThomas Gleixner 	rt_mutex_init_rtlock_waiter(&waiter);
18451c143c4bSThomas Gleixner 
18461c143c4bSThomas Gleixner 	/* Save current state and set state to TASK_RTLOCK_WAIT */
18471c143c4bSThomas Gleixner 	current_save_and_set_rtlock_wait_state();
18481c143c4bSThomas Gleixner 
1849ee042be1SNamhyung Kim 	trace_contention_begin(lock, LCB_F_RT);
1850ee042be1SNamhyung Kim 
1851894d1b3dSPeter Zijlstra 	task_blocks_on_rt_mutex(lock, &waiter, current, NULL, RT_MUTEX_MIN_CHAINWALK, wake_q);
18521c143c4bSThomas Gleixner 
18531c143c4bSThomas Gleixner 	for (;;) {
18541c143c4bSThomas Gleixner 		/* Try to acquire the lock again */
1855*b76b44fbSWaiman Long 		if (try_to_take_rt_mutex(lock, current, &waiter)) {
1856*b76b44fbSWaiman Long 			lockevent_inc(rtlock_slow_acq2);
18571c143c4bSThomas Gleixner 			break;
1858*b76b44fbSWaiman Long 		}
18591c143c4bSThomas Gleixner 
1860992caf7fSSteven Rostedt 		if (&waiter == rt_mutex_top_waiter(lock))
1861992caf7fSSteven Rostedt 			owner = rt_mutex_owner(lock);
1862992caf7fSSteven Rostedt 		else
1863992caf7fSSteven Rostedt 			owner = NULL;
1864abfdccd6SJohn Stultz 		raw_spin_unlock_irq_wake(&lock->wait_lock, wake_q);
18651c143c4bSThomas Gleixner 
1866*b76b44fbSWaiman Long 		if (!owner || !rtmutex_spin_on_owner(lock, &waiter, owner)) {
1867*b76b44fbSWaiman Long 			lockevent_inc(rtlock_slow_sleep);
18681c143c4bSThomas Gleixner 			schedule_rtlock();
1869*b76b44fbSWaiman Long 		}
18701c143c4bSThomas Gleixner 
18711c143c4bSThomas Gleixner 		raw_spin_lock_irq(&lock->wait_lock);
18721c143c4bSThomas Gleixner 		set_current_state(TASK_RTLOCK_WAIT);
18731c143c4bSThomas Gleixner 	}
18741c143c4bSThomas Gleixner 
18751c143c4bSThomas Gleixner 	/* Restore the task state */
18761c143c4bSThomas Gleixner 	current_restore_rtlock_saved_state();
18771c143c4bSThomas Gleixner 
18781c143c4bSThomas Gleixner 	/*
18791c143c4bSThomas Gleixner 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally.
18801c143c4bSThomas Gleixner 	 * We might have to fix that up:
18811c143c4bSThomas Gleixner 	 */
18821c0908d8SMel Gorman 	fixup_rt_mutex_waiters(lock, true);
18831c143c4bSThomas Gleixner 	debug_rt_mutex_free_waiter(&waiter);
1884ee042be1SNamhyung Kim 
1885ee042be1SNamhyung Kim 	trace_contention_end(lock, 0);
1886*b76b44fbSWaiman Long 	lockevent_cond_inc(rtlock_slow_wake, !wake_q_empty(wake_q));
18871c143c4bSThomas Gleixner }
18881c143c4bSThomas Gleixner 
rtlock_slowlock(struct rt_mutex_base * lock)18891c143c4bSThomas Gleixner static __always_inline void __sched rtlock_slowlock(struct rt_mutex_base *lock)
18901c143c4bSThomas Gleixner {
18911c143c4bSThomas Gleixner 	unsigned long flags;
1892894d1b3dSPeter Zijlstra 	DEFINE_WAKE_Q(wake_q);
18931c143c4bSThomas Gleixner 
18941c143c4bSThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1895894d1b3dSPeter Zijlstra 	rtlock_slowlock_locked(lock, &wake_q);
1896abfdccd6SJohn Stultz 	raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
18971c143c4bSThomas Gleixner }
18981c143c4bSThomas Gleixner 
18991c143c4bSThomas Gleixner #endif /* RT_MUTEX_BUILD_SPINLOCKS */
1900