xref: /openbmc/linux/kernel/locking/rtmutex_api.c (revision f7853c34)
1531ae4b0SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2531ae4b0SThomas Gleixner /*
3531ae4b0SThomas Gleixner  * rtmutex API
4531ae4b0SThomas Gleixner  */
5531ae4b0SThomas Gleixner #include <linux/spinlock.h>
6531ae4b0SThomas Gleixner #include <linux/export.h>
7531ae4b0SThomas Gleixner 
8e17ba59bSThomas Gleixner #define RT_MUTEX_BUILD_MUTEX
9531ae4b0SThomas Gleixner #include "rtmutex.c"
10531ae4b0SThomas Gleixner 
11531ae4b0SThomas Gleixner /*
12531ae4b0SThomas Gleixner  * Max number of times we'll walk the boosting chain:
13531ae4b0SThomas Gleixner  */
14531ae4b0SThomas Gleixner int max_lock_depth = 1024;
15531ae4b0SThomas Gleixner 
16531ae4b0SThomas Gleixner /*
17531ae4b0SThomas Gleixner  * Debug aware fast / slowpath lock,trylock,unlock
18531ae4b0SThomas Gleixner  *
19531ae4b0SThomas Gleixner  * The atomic acquire/release ops are compiled away, when either the
20531ae4b0SThomas Gleixner  * architecture does not support cmpxchg or when debugging is enabled.
21531ae4b0SThomas Gleixner  */
__rt_mutex_lock_common(struct rt_mutex * lock,unsigned int state,struct lockdep_map * nest_lock,unsigned int subclass)22531ae4b0SThomas Gleixner static __always_inline int __rt_mutex_lock_common(struct rt_mutex *lock,
23531ae4b0SThomas Gleixner 						  unsigned int state,
24a3642021SSebastian Andrzej Siewior 						  struct lockdep_map *nest_lock,
25531ae4b0SThomas Gleixner 						  unsigned int subclass)
26531ae4b0SThomas Gleixner {
27531ae4b0SThomas Gleixner 	int ret;
28531ae4b0SThomas Gleixner 
29531ae4b0SThomas Gleixner 	might_sleep();
30a3642021SSebastian Andrzej Siewior 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, _RET_IP_);
31830e6accSPeter Zijlstra 	ret = __rt_mutex_lock(&lock->rtmutex, state);
32531ae4b0SThomas Gleixner 	if (ret)
33531ae4b0SThomas Gleixner 		mutex_release(&lock->dep_map, _RET_IP_);
34531ae4b0SThomas Gleixner 	return ret;
35531ae4b0SThomas Gleixner }
36531ae4b0SThomas Gleixner 
rt_mutex_base_init(struct rt_mutex_base * rtb)37830e6accSPeter Zijlstra void rt_mutex_base_init(struct rt_mutex_base *rtb)
38830e6accSPeter Zijlstra {
39830e6accSPeter Zijlstra 	__rt_mutex_base_init(rtb);
40830e6accSPeter Zijlstra }
41830e6accSPeter Zijlstra EXPORT_SYMBOL(rt_mutex_base_init);
42830e6accSPeter Zijlstra 
43531ae4b0SThomas Gleixner #ifdef CONFIG_DEBUG_LOCK_ALLOC
44531ae4b0SThomas Gleixner /**
45531ae4b0SThomas Gleixner  * rt_mutex_lock_nested - lock a rt_mutex
46531ae4b0SThomas Gleixner  *
47531ae4b0SThomas Gleixner  * @lock: the rt_mutex to be locked
48531ae4b0SThomas Gleixner  * @subclass: the lockdep subclass
49531ae4b0SThomas Gleixner  */
rt_mutex_lock_nested(struct rt_mutex * lock,unsigned int subclass)50531ae4b0SThomas Gleixner void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
51531ae4b0SThomas Gleixner {
52a3642021SSebastian Andrzej Siewior 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
53531ae4b0SThomas Gleixner }
54531ae4b0SThomas Gleixner EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
55531ae4b0SThomas Gleixner 
_rt_mutex_lock_nest_lock(struct rt_mutex * lock,struct lockdep_map * nest_lock)56a3642021SSebastian Andrzej Siewior void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
57a3642021SSebastian Andrzej Siewior {
58a3642021SSebastian Andrzej Siewior 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0);
59a3642021SSebastian Andrzej Siewior }
60a3642021SSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(_rt_mutex_lock_nest_lock);
61a3642021SSebastian Andrzej Siewior 
62531ae4b0SThomas Gleixner #else /* !CONFIG_DEBUG_LOCK_ALLOC */
63531ae4b0SThomas Gleixner 
64531ae4b0SThomas Gleixner /**
65531ae4b0SThomas Gleixner  * rt_mutex_lock - lock a rt_mutex
66531ae4b0SThomas Gleixner  *
67531ae4b0SThomas Gleixner  * @lock: the rt_mutex to be locked
68531ae4b0SThomas Gleixner  */
rt_mutex_lock(struct rt_mutex * lock)69531ae4b0SThomas Gleixner void __sched rt_mutex_lock(struct rt_mutex *lock)
70531ae4b0SThomas Gleixner {
71a3642021SSebastian Andrzej Siewior 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, 0);
72531ae4b0SThomas Gleixner }
73531ae4b0SThomas Gleixner EXPORT_SYMBOL_GPL(rt_mutex_lock);
74531ae4b0SThomas Gleixner #endif
75531ae4b0SThomas Gleixner 
76531ae4b0SThomas Gleixner /**
77531ae4b0SThomas Gleixner  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
78531ae4b0SThomas Gleixner  *
79531ae4b0SThomas Gleixner  * @lock:		the rt_mutex to be locked
80531ae4b0SThomas Gleixner  *
81531ae4b0SThomas Gleixner  * Returns:
82531ae4b0SThomas Gleixner  *  0		on success
83531ae4b0SThomas Gleixner  * -EINTR	when interrupted by a signal
84531ae4b0SThomas Gleixner  */
rt_mutex_lock_interruptible(struct rt_mutex * lock)85531ae4b0SThomas Gleixner int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
86531ae4b0SThomas Gleixner {
87a3642021SSebastian Andrzej Siewior 	return __rt_mutex_lock_common(lock, TASK_INTERRUPTIBLE, NULL, 0);
88531ae4b0SThomas Gleixner }
89531ae4b0SThomas Gleixner EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
90531ae4b0SThomas Gleixner 
91531ae4b0SThomas Gleixner /**
92a3642021SSebastian Andrzej Siewior  * rt_mutex_lock_killable - lock a rt_mutex killable
93a3642021SSebastian Andrzej Siewior  *
94a3642021SSebastian Andrzej Siewior  * @lock:		the rt_mutex to be locked
95a3642021SSebastian Andrzej Siewior  *
96a3642021SSebastian Andrzej Siewior  * Returns:
97a3642021SSebastian Andrzej Siewior  *  0		on success
98a3642021SSebastian Andrzej Siewior  * -EINTR	when interrupted by a signal
99a3642021SSebastian Andrzej Siewior  */
rt_mutex_lock_killable(struct rt_mutex * lock)100a3642021SSebastian Andrzej Siewior int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
101a3642021SSebastian Andrzej Siewior {
102a3642021SSebastian Andrzej Siewior 	return __rt_mutex_lock_common(lock, TASK_KILLABLE, NULL, 0);
103a3642021SSebastian Andrzej Siewior }
104a3642021SSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
105a3642021SSebastian Andrzej Siewior 
106a3642021SSebastian Andrzej Siewior /**
107531ae4b0SThomas Gleixner  * rt_mutex_trylock - try to lock a rt_mutex
108531ae4b0SThomas Gleixner  *
109531ae4b0SThomas Gleixner  * @lock:	the rt_mutex to be locked
110531ae4b0SThomas Gleixner  *
111531ae4b0SThomas Gleixner  * This function can only be called in thread context. It's safe to call it
112531ae4b0SThomas Gleixner  * from atomic regions, but not from hard or soft interrupt context.
113531ae4b0SThomas Gleixner  *
114531ae4b0SThomas Gleixner  * Returns:
115531ae4b0SThomas Gleixner  *  1 on success
116531ae4b0SThomas Gleixner  *  0 on contention
117531ae4b0SThomas Gleixner  */
rt_mutex_trylock(struct rt_mutex * lock)118531ae4b0SThomas Gleixner int __sched rt_mutex_trylock(struct rt_mutex *lock)
119531ae4b0SThomas Gleixner {
120531ae4b0SThomas Gleixner 	int ret;
121531ae4b0SThomas Gleixner 
122531ae4b0SThomas Gleixner 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
123531ae4b0SThomas Gleixner 		return 0;
124531ae4b0SThomas Gleixner 
125830e6accSPeter Zijlstra 	ret = __rt_mutex_trylock(&lock->rtmutex);
126531ae4b0SThomas Gleixner 	if (ret)
127531ae4b0SThomas Gleixner 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
128531ae4b0SThomas Gleixner 
129531ae4b0SThomas Gleixner 	return ret;
130531ae4b0SThomas Gleixner }
131531ae4b0SThomas Gleixner EXPORT_SYMBOL_GPL(rt_mutex_trylock);
132531ae4b0SThomas Gleixner 
133531ae4b0SThomas Gleixner /**
134531ae4b0SThomas Gleixner  * rt_mutex_unlock - unlock a rt_mutex
135531ae4b0SThomas Gleixner  *
136531ae4b0SThomas Gleixner  * @lock: the rt_mutex to be unlocked
137531ae4b0SThomas Gleixner  */
rt_mutex_unlock(struct rt_mutex * lock)138531ae4b0SThomas Gleixner void __sched rt_mutex_unlock(struct rt_mutex *lock)
139531ae4b0SThomas Gleixner {
140531ae4b0SThomas Gleixner 	mutex_release(&lock->dep_map, _RET_IP_);
141830e6accSPeter Zijlstra 	__rt_mutex_unlock(&lock->rtmutex);
142531ae4b0SThomas Gleixner }
143531ae4b0SThomas Gleixner EXPORT_SYMBOL_GPL(rt_mutex_unlock);
144531ae4b0SThomas Gleixner 
145531ae4b0SThomas Gleixner /*
146531ae4b0SThomas Gleixner  * Futex variants, must not use fastpath.
147531ae4b0SThomas Gleixner  */
rt_mutex_futex_trylock(struct rt_mutex_base * lock)148830e6accSPeter Zijlstra int __sched rt_mutex_futex_trylock(struct rt_mutex_base *lock)
149531ae4b0SThomas Gleixner {
150531ae4b0SThomas Gleixner 	return rt_mutex_slowtrylock(lock);
151531ae4b0SThomas Gleixner }
152531ae4b0SThomas Gleixner 
__rt_mutex_futex_trylock(struct rt_mutex_base * lock)153830e6accSPeter Zijlstra int __sched __rt_mutex_futex_trylock(struct rt_mutex_base *lock)
154531ae4b0SThomas Gleixner {
155531ae4b0SThomas Gleixner 	return __rt_mutex_slowtrylock(lock);
156531ae4b0SThomas Gleixner }
157531ae4b0SThomas Gleixner 
158531ae4b0SThomas Gleixner /**
159531ae4b0SThomas Gleixner  * __rt_mutex_futex_unlock - Futex variant, that since futex variants
160531ae4b0SThomas Gleixner  * do not use the fast-path, can be simple and will not need to retry.
161531ae4b0SThomas Gleixner  *
162531ae4b0SThomas Gleixner  * @lock:	The rt_mutex to be unlocked
1637980aa39SThomas Gleixner  * @wqh:	The wake queue head from which to get the next lock waiter
164531ae4b0SThomas Gleixner  */
__rt_mutex_futex_unlock(struct rt_mutex_base * lock,struct rt_wake_q_head * wqh)165830e6accSPeter Zijlstra bool __sched __rt_mutex_futex_unlock(struct rt_mutex_base *lock,
1667980aa39SThomas Gleixner 				     struct rt_wake_q_head *wqh)
167531ae4b0SThomas Gleixner {
168531ae4b0SThomas Gleixner 	lockdep_assert_held(&lock->wait_lock);
169531ae4b0SThomas Gleixner 
170531ae4b0SThomas Gleixner 	debug_rt_mutex_unlock(lock);
171531ae4b0SThomas Gleixner 
172531ae4b0SThomas Gleixner 	if (!rt_mutex_has_waiters(lock)) {
173531ae4b0SThomas Gleixner 		lock->owner = NULL;
174531ae4b0SThomas Gleixner 		return false; /* done */
175531ae4b0SThomas Gleixner 	}
176531ae4b0SThomas Gleixner 
177531ae4b0SThomas Gleixner 	/*
178531ae4b0SThomas Gleixner 	 * We've already deboosted, mark_wakeup_next_waiter() will
179531ae4b0SThomas Gleixner 	 * retain preempt_disabled when we drop the wait_lock, to
180531ae4b0SThomas Gleixner 	 * avoid inversion prior to the wakeup.  preempt_disable()
181531ae4b0SThomas Gleixner 	 * therein pairs with rt_mutex_postunlock().
182531ae4b0SThomas Gleixner 	 */
1837980aa39SThomas Gleixner 	mark_wakeup_next_waiter(wqh, lock);
184531ae4b0SThomas Gleixner 
185531ae4b0SThomas Gleixner 	return true; /* call postunlock() */
186531ae4b0SThomas Gleixner }
187531ae4b0SThomas Gleixner 
rt_mutex_futex_unlock(struct rt_mutex_base * lock)188830e6accSPeter Zijlstra void __sched rt_mutex_futex_unlock(struct rt_mutex_base *lock)
189531ae4b0SThomas Gleixner {
1907980aa39SThomas Gleixner 	DEFINE_RT_WAKE_Q(wqh);
191531ae4b0SThomas Gleixner 	unsigned long flags;
192531ae4b0SThomas Gleixner 	bool postunlock;
193531ae4b0SThomas Gleixner 
194531ae4b0SThomas Gleixner 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1957980aa39SThomas Gleixner 	postunlock = __rt_mutex_futex_unlock(lock, &wqh);
196531ae4b0SThomas Gleixner 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
197531ae4b0SThomas Gleixner 
198531ae4b0SThomas Gleixner 	if (postunlock)
1997980aa39SThomas Gleixner 		rt_mutex_postunlock(&wqh);
200531ae4b0SThomas Gleixner }
201531ae4b0SThomas Gleixner 
202531ae4b0SThomas Gleixner /**
203531ae4b0SThomas Gleixner  * __rt_mutex_init - initialize the rt_mutex
204531ae4b0SThomas Gleixner  *
205531ae4b0SThomas Gleixner  * @lock:	The rt_mutex to be initialized
206531ae4b0SThomas Gleixner  * @name:	The lock name used for debugging
207531ae4b0SThomas Gleixner  * @key:	The lock class key used for debugging
208531ae4b0SThomas Gleixner  *
209531ae4b0SThomas Gleixner  * Initialize the rt_mutex to unlocked state.
210531ae4b0SThomas Gleixner  *
211531ae4b0SThomas Gleixner  * Initializing of a locked rt_mutex is not allowed
212531ae4b0SThomas Gleixner  */
__rt_mutex_init(struct rt_mutex * lock,const char * name,struct lock_class_key * key)213531ae4b0SThomas Gleixner void __sched __rt_mutex_init(struct rt_mutex *lock, const char *name,
214531ae4b0SThomas Gleixner 			     struct lock_class_key *key)
215531ae4b0SThomas Gleixner {
216531ae4b0SThomas Gleixner 	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
217830e6accSPeter Zijlstra 	__rt_mutex_base_init(&lock->rtmutex);
218531ae4b0SThomas Gleixner 	lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
219531ae4b0SThomas Gleixner }
220531ae4b0SThomas Gleixner EXPORT_SYMBOL_GPL(__rt_mutex_init);
221531ae4b0SThomas Gleixner 
222531ae4b0SThomas Gleixner /**
223531ae4b0SThomas Gleixner  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
224531ae4b0SThomas Gleixner  *				proxy owner
225531ae4b0SThomas Gleixner  *
226531ae4b0SThomas Gleixner  * @lock:	the rt_mutex to be locked
227531ae4b0SThomas Gleixner  * @proxy_owner:the task to set as owner
228531ae4b0SThomas Gleixner  *
229531ae4b0SThomas Gleixner  * No locking. Caller has to do serializing itself
230531ae4b0SThomas Gleixner  *
231531ae4b0SThomas Gleixner  * Special API call for PI-futex support. This initializes the rtmutex and
232531ae4b0SThomas Gleixner  * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
233531ae4b0SThomas Gleixner  * possible at this point because the pi_state which contains the rtmutex
234531ae4b0SThomas Gleixner  * is not yet visible to other tasks.
235531ae4b0SThomas Gleixner  */
rt_mutex_init_proxy_locked(struct rt_mutex_base * lock,struct task_struct * proxy_owner)236830e6accSPeter Zijlstra void __sched rt_mutex_init_proxy_locked(struct rt_mutex_base *lock,
237531ae4b0SThomas Gleixner 					struct task_struct *proxy_owner)
238531ae4b0SThomas Gleixner {
23951711e82SThomas Gleixner 	static struct lock_class_key pi_futex_key;
24051711e82SThomas Gleixner 
241830e6accSPeter Zijlstra 	__rt_mutex_base_init(lock);
24251711e82SThomas Gleixner 	/*
24351711e82SThomas Gleixner 	 * On PREEMPT_RT the futex hashbucket spinlock becomes 'sleeping'
24451711e82SThomas Gleixner 	 * and rtmutex based. That causes a lockdep false positive, because
24551711e82SThomas Gleixner 	 * some of the futex functions invoke spin_unlock(&hb->lock) with
24651711e82SThomas Gleixner 	 * the wait_lock of the rtmutex associated to the pi_futex held.
24751711e82SThomas Gleixner 	 * spin_unlock() in turn takes wait_lock of the rtmutex on which
24851711e82SThomas Gleixner 	 * the spinlock is based, which makes lockdep notice a lock
24951711e82SThomas Gleixner 	 * recursion. Give the futex/rtmutex wait_lock a separate key.
25051711e82SThomas Gleixner 	 */
25151711e82SThomas Gleixner 	lockdep_set_class(&lock->wait_lock, &pi_futex_key);
252531ae4b0SThomas Gleixner 	rt_mutex_set_owner(lock, proxy_owner);
253531ae4b0SThomas Gleixner }
254531ae4b0SThomas Gleixner 
255531ae4b0SThomas Gleixner /**
256531ae4b0SThomas Gleixner  * rt_mutex_proxy_unlock - release a lock on behalf of owner
257531ae4b0SThomas Gleixner  *
258531ae4b0SThomas Gleixner  * @lock:	the rt_mutex to be locked
259531ae4b0SThomas Gleixner  *
260531ae4b0SThomas Gleixner  * No locking. Caller has to do serializing itself
261531ae4b0SThomas Gleixner  *
262531ae4b0SThomas Gleixner  * Special API call for PI-futex support. This just cleans up the rtmutex
263531ae4b0SThomas Gleixner  * (debugging) state. Concurrent operations on this rt_mutex are not
264531ae4b0SThomas Gleixner  * possible because it belongs to the pi_state which is about to be freed
265531ae4b0SThomas Gleixner  * and it is not longer visible to other tasks.
266531ae4b0SThomas Gleixner  */
rt_mutex_proxy_unlock(struct rt_mutex_base * lock)267830e6accSPeter Zijlstra void __sched rt_mutex_proxy_unlock(struct rt_mutex_base *lock)
268531ae4b0SThomas Gleixner {
269531ae4b0SThomas Gleixner 	debug_rt_mutex_proxy_unlock(lock);
2701c0908d8SMel Gorman 	rt_mutex_clear_owner(lock);
271531ae4b0SThomas Gleixner }
272531ae4b0SThomas Gleixner 
273531ae4b0SThomas Gleixner /**
274531ae4b0SThomas Gleixner  * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
275531ae4b0SThomas Gleixner  * @lock:		the rt_mutex to take
276531ae4b0SThomas Gleixner  * @waiter:		the pre-initialized rt_mutex_waiter
277531ae4b0SThomas Gleixner  * @task:		the task to prepare
278531ae4b0SThomas Gleixner  *
279531ae4b0SThomas Gleixner  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
280531ae4b0SThomas Gleixner  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
281531ae4b0SThomas Gleixner  *
282531ae4b0SThomas Gleixner  * NOTE: does _NOT_ remove the @waiter on failure; must either call
283531ae4b0SThomas Gleixner  * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
284531ae4b0SThomas Gleixner  *
285531ae4b0SThomas Gleixner  * Returns:
286531ae4b0SThomas Gleixner  *  0 - task blocked on lock
287531ae4b0SThomas Gleixner  *  1 - acquired the lock for task, caller should wake it up
288531ae4b0SThomas Gleixner  * <0 - error
289531ae4b0SThomas Gleixner  *
290531ae4b0SThomas Gleixner  * Special API call for PI-futex support.
291531ae4b0SThomas Gleixner  */
__rt_mutex_start_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task)292830e6accSPeter Zijlstra int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
293531ae4b0SThomas Gleixner 					struct rt_mutex_waiter *waiter,
294531ae4b0SThomas Gleixner 					struct task_struct *task)
295531ae4b0SThomas Gleixner {
296531ae4b0SThomas Gleixner 	int ret;
297531ae4b0SThomas Gleixner 
298531ae4b0SThomas Gleixner 	lockdep_assert_held(&lock->wait_lock);
299531ae4b0SThomas Gleixner 
300531ae4b0SThomas Gleixner 	if (try_to_take_rt_mutex(lock, task, NULL))
301531ae4b0SThomas Gleixner 		return 1;
302531ae4b0SThomas Gleixner 
303531ae4b0SThomas Gleixner 	/* We enforce deadlock detection for futexes */
304add46132SPeter Zijlstra 	ret = task_blocks_on_rt_mutex(lock, waiter, task, NULL,
305531ae4b0SThomas Gleixner 				      RT_MUTEX_FULL_CHAINWALK);
306531ae4b0SThomas Gleixner 
307531ae4b0SThomas Gleixner 	if (ret && !rt_mutex_owner(lock)) {
308531ae4b0SThomas Gleixner 		/*
309531ae4b0SThomas Gleixner 		 * Reset the return value. We might have
310531ae4b0SThomas Gleixner 		 * returned with -EDEADLK and the owner
311531ae4b0SThomas Gleixner 		 * released the lock while we were walking the
312531ae4b0SThomas Gleixner 		 * pi chain.  Let the waiter sort it out.
313531ae4b0SThomas Gleixner 		 */
314531ae4b0SThomas Gleixner 		ret = 0;
315531ae4b0SThomas Gleixner 	}
316531ae4b0SThomas Gleixner 
317531ae4b0SThomas Gleixner 	return ret;
318531ae4b0SThomas Gleixner }
319531ae4b0SThomas Gleixner 
320531ae4b0SThomas Gleixner /**
321531ae4b0SThomas Gleixner  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
322531ae4b0SThomas Gleixner  * @lock:		the rt_mutex to take
323531ae4b0SThomas Gleixner  * @waiter:		the pre-initialized rt_mutex_waiter
324531ae4b0SThomas Gleixner  * @task:		the task to prepare
325531ae4b0SThomas Gleixner  *
326531ae4b0SThomas Gleixner  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
327531ae4b0SThomas Gleixner  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
328531ae4b0SThomas Gleixner  *
329531ae4b0SThomas Gleixner  * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
330531ae4b0SThomas Gleixner  * on failure.
331531ae4b0SThomas Gleixner  *
332531ae4b0SThomas Gleixner  * Returns:
333531ae4b0SThomas Gleixner  *  0 - task blocked on lock
334531ae4b0SThomas Gleixner  *  1 - acquired the lock for task, caller should wake it up
335531ae4b0SThomas Gleixner  * <0 - error
336531ae4b0SThomas Gleixner  *
337531ae4b0SThomas Gleixner  * Special API call for PI-futex support.
338531ae4b0SThomas Gleixner  */
rt_mutex_start_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task)339830e6accSPeter Zijlstra int __sched rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
340531ae4b0SThomas Gleixner 				      struct rt_mutex_waiter *waiter,
341531ae4b0SThomas Gleixner 				      struct task_struct *task)
342531ae4b0SThomas Gleixner {
343531ae4b0SThomas Gleixner 	int ret;
344531ae4b0SThomas Gleixner 
345531ae4b0SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
346531ae4b0SThomas Gleixner 	ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
347531ae4b0SThomas Gleixner 	if (unlikely(ret))
348531ae4b0SThomas Gleixner 		remove_waiter(lock, waiter);
349531ae4b0SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
350531ae4b0SThomas Gleixner 
351531ae4b0SThomas Gleixner 	return ret;
352531ae4b0SThomas Gleixner }
353531ae4b0SThomas Gleixner 
354531ae4b0SThomas Gleixner /**
355531ae4b0SThomas Gleixner  * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
356531ae4b0SThomas Gleixner  * @lock:		the rt_mutex we were woken on
357531ae4b0SThomas Gleixner  * @to:			the timeout, null if none. hrtimer should already have
358531ae4b0SThomas Gleixner  *			been started.
359531ae4b0SThomas Gleixner  * @waiter:		the pre-initialized rt_mutex_waiter
360531ae4b0SThomas Gleixner  *
361531ae4b0SThomas Gleixner  * Wait for the lock acquisition started on our behalf by
362531ae4b0SThomas Gleixner  * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
363531ae4b0SThomas Gleixner  * rt_mutex_cleanup_proxy_lock().
364531ae4b0SThomas Gleixner  *
365531ae4b0SThomas Gleixner  * Returns:
366531ae4b0SThomas Gleixner  *  0 - success
367531ae4b0SThomas Gleixner  * <0 - error, one of -EINTR, -ETIMEDOUT
368531ae4b0SThomas Gleixner  *
369531ae4b0SThomas Gleixner  * Special API call for PI-futex support
370531ae4b0SThomas Gleixner  */
rt_mutex_wait_proxy_lock(struct rt_mutex_base * lock,struct hrtimer_sleeper * to,struct rt_mutex_waiter * waiter)371830e6accSPeter Zijlstra int __sched rt_mutex_wait_proxy_lock(struct rt_mutex_base *lock,
372531ae4b0SThomas Gleixner 				     struct hrtimer_sleeper *to,
373531ae4b0SThomas Gleixner 				     struct rt_mutex_waiter *waiter)
374531ae4b0SThomas Gleixner {
375531ae4b0SThomas Gleixner 	int ret;
376531ae4b0SThomas Gleixner 
377531ae4b0SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
378531ae4b0SThomas Gleixner 	/* sleep on the mutex */
379531ae4b0SThomas Gleixner 	set_current_state(TASK_INTERRUPTIBLE);
380add46132SPeter Zijlstra 	ret = rt_mutex_slowlock_block(lock, NULL, TASK_INTERRUPTIBLE, to, waiter);
381531ae4b0SThomas Gleixner 	/*
382531ae4b0SThomas Gleixner 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
383531ae4b0SThomas Gleixner 	 * have to fix that up.
384531ae4b0SThomas Gleixner 	 */
3851c0908d8SMel Gorman 	fixup_rt_mutex_waiters(lock, true);
386531ae4b0SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
387531ae4b0SThomas Gleixner 
388531ae4b0SThomas Gleixner 	return ret;
389531ae4b0SThomas Gleixner }
390531ae4b0SThomas Gleixner 
391531ae4b0SThomas Gleixner /**
392531ae4b0SThomas Gleixner  * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
393531ae4b0SThomas Gleixner  * @lock:		the rt_mutex we were woken on
394531ae4b0SThomas Gleixner  * @waiter:		the pre-initialized rt_mutex_waiter
395531ae4b0SThomas Gleixner  *
396531ae4b0SThomas Gleixner  * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
397531ae4b0SThomas Gleixner  * rt_mutex_wait_proxy_lock().
398531ae4b0SThomas Gleixner  *
399531ae4b0SThomas Gleixner  * Unless we acquired the lock; we're still enqueued on the wait-list and can
400531ae4b0SThomas Gleixner  * in fact still be granted ownership until we're removed. Therefore we can
401531ae4b0SThomas Gleixner  * find we are in fact the owner and must disregard the
402531ae4b0SThomas Gleixner  * rt_mutex_wait_proxy_lock() failure.
403531ae4b0SThomas Gleixner  *
404531ae4b0SThomas Gleixner  * Returns:
405531ae4b0SThomas Gleixner  *  true  - did the cleanup, we done.
406531ae4b0SThomas Gleixner  *  false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
407531ae4b0SThomas Gleixner  *          caller should disregards its return value.
408531ae4b0SThomas Gleixner  *
409531ae4b0SThomas Gleixner  * Special API call for PI-futex support
410531ae4b0SThomas Gleixner  */
rt_mutex_cleanup_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)411830e6accSPeter Zijlstra bool __sched rt_mutex_cleanup_proxy_lock(struct rt_mutex_base *lock,
412531ae4b0SThomas Gleixner 					 struct rt_mutex_waiter *waiter)
413531ae4b0SThomas Gleixner {
414531ae4b0SThomas Gleixner 	bool cleanup = false;
415531ae4b0SThomas Gleixner 
416531ae4b0SThomas Gleixner 	raw_spin_lock_irq(&lock->wait_lock);
417531ae4b0SThomas Gleixner 	/*
418531ae4b0SThomas Gleixner 	 * Do an unconditional try-lock, this deals with the lock stealing
419531ae4b0SThomas Gleixner 	 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
420531ae4b0SThomas Gleixner 	 * sets a NULL owner.
421531ae4b0SThomas Gleixner 	 *
422531ae4b0SThomas Gleixner 	 * We're not interested in the return value, because the subsequent
423531ae4b0SThomas Gleixner 	 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
424531ae4b0SThomas Gleixner 	 * we will own the lock and it will have removed the waiter. If we
425531ae4b0SThomas Gleixner 	 * failed the trylock, we're still not owner and we need to remove
426531ae4b0SThomas Gleixner 	 * ourselves.
427531ae4b0SThomas Gleixner 	 */
428531ae4b0SThomas Gleixner 	try_to_take_rt_mutex(lock, current, waiter);
429531ae4b0SThomas Gleixner 	/*
430531ae4b0SThomas Gleixner 	 * Unless we're the owner; we're still enqueued on the wait_list.
431531ae4b0SThomas Gleixner 	 * So check if we became owner, if not, take us off the wait_list.
432531ae4b0SThomas Gleixner 	 */
433531ae4b0SThomas Gleixner 	if (rt_mutex_owner(lock) != current) {
434531ae4b0SThomas Gleixner 		remove_waiter(lock, waiter);
435531ae4b0SThomas Gleixner 		cleanup = true;
436531ae4b0SThomas Gleixner 	}
437531ae4b0SThomas Gleixner 	/*
438531ae4b0SThomas Gleixner 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
439531ae4b0SThomas Gleixner 	 * have to fix that up.
440531ae4b0SThomas Gleixner 	 */
4411c0908d8SMel Gorman 	fixup_rt_mutex_waiters(lock, false);
442531ae4b0SThomas Gleixner 
443531ae4b0SThomas Gleixner 	raw_spin_unlock_irq(&lock->wait_lock);
444531ae4b0SThomas Gleixner 
445531ae4b0SThomas Gleixner 	return cleanup;
446531ae4b0SThomas Gleixner }
447531ae4b0SThomas Gleixner 
448531ae4b0SThomas Gleixner /*
449531ae4b0SThomas Gleixner  * Recheck the pi chain, in case we got a priority setting
450531ae4b0SThomas Gleixner  *
451531ae4b0SThomas Gleixner  * Called from sched_setscheduler
452531ae4b0SThomas Gleixner  */
rt_mutex_adjust_pi(struct task_struct * task)453531ae4b0SThomas Gleixner void __sched rt_mutex_adjust_pi(struct task_struct *task)
454531ae4b0SThomas Gleixner {
455531ae4b0SThomas Gleixner 	struct rt_mutex_waiter *waiter;
456830e6accSPeter Zijlstra 	struct rt_mutex_base *next_lock;
457531ae4b0SThomas Gleixner 	unsigned long flags;
458531ae4b0SThomas Gleixner 
459531ae4b0SThomas Gleixner 	raw_spin_lock_irqsave(&task->pi_lock, flags);
460531ae4b0SThomas Gleixner 
461531ae4b0SThomas Gleixner 	waiter = task->pi_blocked_on;
462*f7853c34SPeter Zijlstra 	if (!waiter || rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
463531ae4b0SThomas Gleixner 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
464531ae4b0SThomas Gleixner 		return;
465531ae4b0SThomas Gleixner 	}
466531ae4b0SThomas Gleixner 	next_lock = waiter->lock;
467531ae4b0SThomas Gleixner 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
468531ae4b0SThomas Gleixner 
469531ae4b0SThomas Gleixner 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
470531ae4b0SThomas Gleixner 	get_task_struct(task);
471531ae4b0SThomas Gleixner 
472531ae4b0SThomas Gleixner 	rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
473531ae4b0SThomas Gleixner 				   next_lock, NULL, task);
474531ae4b0SThomas Gleixner }
475531ae4b0SThomas Gleixner 
476531ae4b0SThomas Gleixner /*
477531ae4b0SThomas Gleixner  * Performs the wakeup of the top-waiter and re-enables preemption.
478531ae4b0SThomas Gleixner  */
rt_mutex_postunlock(struct rt_wake_q_head * wqh)4797980aa39SThomas Gleixner void __sched rt_mutex_postunlock(struct rt_wake_q_head *wqh)
480531ae4b0SThomas Gleixner {
4817980aa39SThomas Gleixner 	rt_mutex_wake_up_q(wqh);
482531ae4b0SThomas Gleixner }
483531ae4b0SThomas Gleixner 
484531ae4b0SThomas Gleixner #ifdef CONFIG_DEBUG_RT_MUTEXES
rt_mutex_debug_task_free(struct task_struct * task)485531ae4b0SThomas Gleixner void rt_mutex_debug_task_free(struct task_struct *task)
486531ae4b0SThomas Gleixner {
487531ae4b0SThomas Gleixner 	DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
488531ae4b0SThomas Gleixner 	DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
489531ae4b0SThomas Gleixner }
490531ae4b0SThomas Gleixner #endif
491bb630f9fSThomas Gleixner 
492bb630f9fSThomas Gleixner #ifdef CONFIG_PREEMPT_RT
493bb630f9fSThomas Gleixner /* Mutexes */
__mutex_rt_init(struct mutex * mutex,const char * name,struct lock_class_key * key)494bb630f9fSThomas Gleixner void __mutex_rt_init(struct mutex *mutex, const char *name,
495bb630f9fSThomas Gleixner 		     struct lock_class_key *key)
496bb630f9fSThomas Gleixner {
497bb630f9fSThomas Gleixner 	debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
498bb630f9fSThomas Gleixner 	lockdep_init_map_wait(&mutex->dep_map, name, key, 0, LD_WAIT_SLEEP);
499bb630f9fSThomas Gleixner }
500bb630f9fSThomas Gleixner EXPORT_SYMBOL(__mutex_rt_init);
501bb630f9fSThomas Gleixner 
__mutex_lock_common(struct mutex * lock,unsigned int state,unsigned int subclass,struct lockdep_map * nest_lock,unsigned long ip)502bb630f9fSThomas Gleixner static __always_inline int __mutex_lock_common(struct mutex *lock,
503bb630f9fSThomas Gleixner 					       unsigned int state,
504bb630f9fSThomas Gleixner 					       unsigned int subclass,
505bb630f9fSThomas Gleixner 					       struct lockdep_map *nest_lock,
506bb630f9fSThomas Gleixner 					       unsigned long ip)
507bb630f9fSThomas Gleixner {
508bb630f9fSThomas Gleixner 	int ret;
509bb630f9fSThomas Gleixner 
510bb630f9fSThomas Gleixner 	might_sleep();
511bb630f9fSThomas Gleixner 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
512bb630f9fSThomas Gleixner 	ret = __rt_mutex_lock(&lock->rtmutex, state);
513bb630f9fSThomas Gleixner 	if (ret)
514bb630f9fSThomas Gleixner 		mutex_release(&lock->dep_map, ip);
515bb630f9fSThomas Gleixner 	else
516bb630f9fSThomas Gleixner 		lock_acquired(&lock->dep_map, ip);
517bb630f9fSThomas Gleixner 	return ret;
518bb630f9fSThomas Gleixner }
519bb630f9fSThomas Gleixner 
520bb630f9fSThomas Gleixner #ifdef CONFIG_DEBUG_LOCK_ALLOC
mutex_lock_nested(struct mutex * lock,unsigned int subclass)521bb630f9fSThomas Gleixner void __sched mutex_lock_nested(struct mutex *lock, unsigned int subclass)
522bb630f9fSThomas Gleixner {
523bb630f9fSThomas Gleixner 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
524bb630f9fSThomas Gleixner }
525bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_nested);
526bb630f9fSThomas Gleixner 
_mutex_lock_nest_lock(struct mutex * lock,struct lockdep_map * nest_lock)527bb630f9fSThomas Gleixner void __sched _mutex_lock_nest_lock(struct mutex *lock,
528bb630f9fSThomas Gleixner 				   struct lockdep_map *nest_lock)
529bb630f9fSThomas Gleixner {
530bb630f9fSThomas Gleixner 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, nest_lock, _RET_IP_);
531bb630f9fSThomas Gleixner }
532bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
533bb630f9fSThomas Gleixner 
mutex_lock_interruptible_nested(struct mutex * lock,unsigned int subclass)534bb630f9fSThomas Gleixner int __sched mutex_lock_interruptible_nested(struct mutex *lock,
535bb630f9fSThomas Gleixner 					    unsigned int subclass)
536bb630f9fSThomas Gleixner {
537bb630f9fSThomas Gleixner 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
538bb630f9fSThomas Gleixner }
539bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
540bb630f9fSThomas Gleixner 
mutex_lock_killable_nested(struct mutex * lock,unsigned int subclass)541bb630f9fSThomas Gleixner int __sched mutex_lock_killable_nested(struct mutex *lock,
542bb630f9fSThomas Gleixner 					    unsigned int subclass)
543bb630f9fSThomas Gleixner {
544bb630f9fSThomas Gleixner 	return __mutex_lock_common(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
545bb630f9fSThomas Gleixner }
546bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
547bb630f9fSThomas Gleixner 
mutex_lock_io_nested(struct mutex * lock,unsigned int subclass)548bb630f9fSThomas Gleixner void __sched mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
549bb630f9fSThomas Gleixner {
550bb630f9fSThomas Gleixner 	int token;
551bb630f9fSThomas Gleixner 
552bb630f9fSThomas Gleixner 	might_sleep();
553bb630f9fSThomas Gleixner 
554bb630f9fSThomas Gleixner 	token = io_schedule_prepare();
555bb630f9fSThomas Gleixner 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
556bb630f9fSThomas Gleixner 	io_schedule_finish(token);
557bb630f9fSThomas Gleixner }
558bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
559bb630f9fSThomas Gleixner 
560bb630f9fSThomas Gleixner #else /* CONFIG_DEBUG_LOCK_ALLOC */
561bb630f9fSThomas Gleixner 
mutex_lock(struct mutex * lock)562bb630f9fSThomas Gleixner void __sched mutex_lock(struct mutex *lock)
563bb630f9fSThomas Gleixner {
564bb630f9fSThomas Gleixner 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
565bb630f9fSThomas Gleixner }
566bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock);
567bb630f9fSThomas Gleixner 
mutex_lock_interruptible(struct mutex * lock)568bb630f9fSThomas Gleixner int __sched mutex_lock_interruptible(struct mutex *lock)
569bb630f9fSThomas Gleixner {
570bb630f9fSThomas Gleixner 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
571bb630f9fSThomas Gleixner }
572bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock_interruptible);
573bb630f9fSThomas Gleixner 
mutex_lock_killable(struct mutex * lock)574bb630f9fSThomas Gleixner int __sched mutex_lock_killable(struct mutex *lock)
575bb630f9fSThomas Gleixner {
576bb630f9fSThomas Gleixner 	return __mutex_lock_common(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
577bb630f9fSThomas Gleixner }
578bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock_killable);
579bb630f9fSThomas Gleixner 
mutex_lock_io(struct mutex * lock)580bb630f9fSThomas Gleixner void __sched mutex_lock_io(struct mutex *lock)
581bb630f9fSThomas Gleixner {
582bb630f9fSThomas Gleixner 	int token = io_schedule_prepare();
583bb630f9fSThomas Gleixner 
584bb630f9fSThomas Gleixner 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
585bb630f9fSThomas Gleixner 	io_schedule_finish(token);
586bb630f9fSThomas Gleixner }
587bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock_io);
588bb630f9fSThomas Gleixner #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
589bb630f9fSThomas Gleixner 
mutex_trylock(struct mutex * lock)590bb630f9fSThomas Gleixner int __sched mutex_trylock(struct mutex *lock)
591bb630f9fSThomas Gleixner {
592bb630f9fSThomas Gleixner 	int ret;
593bb630f9fSThomas Gleixner 
594bb630f9fSThomas Gleixner 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
595bb630f9fSThomas Gleixner 		return 0;
596bb630f9fSThomas Gleixner 
597bb630f9fSThomas Gleixner 	ret = __rt_mutex_trylock(&lock->rtmutex);
598bb630f9fSThomas Gleixner 	if (ret)
599bb630f9fSThomas Gleixner 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
600bb630f9fSThomas Gleixner 
601bb630f9fSThomas Gleixner 	return ret;
602bb630f9fSThomas Gleixner }
603bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_trylock);
604bb630f9fSThomas Gleixner 
mutex_unlock(struct mutex * lock)605bb630f9fSThomas Gleixner void __sched mutex_unlock(struct mutex *lock)
606bb630f9fSThomas Gleixner {
607bb630f9fSThomas Gleixner 	mutex_release(&lock->dep_map, _RET_IP_);
608bb630f9fSThomas Gleixner 	__rt_mutex_unlock(&lock->rtmutex);
609bb630f9fSThomas Gleixner }
610bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_unlock);
611bb630f9fSThomas Gleixner 
612bb630f9fSThomas Gleixner #endif /* CONFIG_PREEMPT_RT */
613