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 /*
178d12b802fSPeter Zijlstra * mark_wakeup_next_waiter() deboosts and retains preemption
179d12b802fSPeter Zijlstra * disabled when dropping the wait_lock, to avoid inversion prior
180d12b802fSPeter Zijlstra * to the wakeup. preempt_disable() therein pairs with the
181d12b802fSPeter Zijlstra * preempt_enable() in 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
278894d1b3dSPeter Zijlstra * @wake_q: the wake_q to wake tasks after we release the wait_lock
279531ae4b0SThomas Gleixner *
280531ae4b0SThomas Gleixner * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
281531ae4b0SThomas Gleixner * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
282531ae4b0SThomas Gleixner *
283531ae4b0SThomas Gleixner * NOTE: does _NOT_ remove the @waiter on failure; must either call
284531ae4b0SThomas Gleixner * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
285531ae4b0SThomas Gleixner *
286531ae4b0SThomas Gleixner * Returns:
287531ae4b0SThomas Gleixner * 0 - task blocked on lock
288531ae4b0SThomas Gleixner * 1 - acquired the lock for task, caller should wake it up
289531ae4b0SThomas Gleixner * <0 - error
290531ae4b0SThomas Gleixner *
291531ae4b0SThomas Gleixner * Special API call for PI-futex support.
292531ae4b0SThomas Gleixner */
__rt_mutex_start_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task,struct wake_q_head * wake_q)293830e6accSPeter Zijlstra int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
294531ae4b0SThomas Gleixner struct rt_mutex_waiter *waiter,
295894d1b3dSPeter Zijlstra struct task_struct *task,
296894d1b3dSPeter Zijlstra struct wake_q_head *wake_q)
297531ae4b0SThomas Gleixner {
298531ae4b0SThomas Gleixner int ret;
299531ae4b0SThomas Gleixner
300531ae4b0SThomas Gleixner lockdep_assert_held(&lock->wait_lock);
301531ae4b0SThomas Gleixner
302531ae4b0SThomas Gleixner if (try_to_take_rt_mutex(lock, task, NULL))
303531ae4b0SThomas Gleixner return 1;
304531ae4b0SThomas Gleixner
305531ae4b0SThomas Gleixner /* We enforce deadlock detection for futexes */
306add46132SPeter Zijlstra ret = task_blocks_on_rt_mutex(lock, waiter, task, NULL,
307894d1b3dSPeter Zijlstra RT_MUTEX_FULL_CHAINWALK, wake_q);
308531ae4b0SThomas Gleixner
309531ae4b0SThomas Gleixner if (ret && !rt_mutex_owner(lock)) {
310531ae4b0SThomas Gleixner /*
311531ae4b0SThomas Gleixner * Reset the return value. We might have
312531ae4b0SThomas Gleixner * returned with -EDEADLK and the owner
313531ae4b0SThomas Gleixner * released the lock while we were walking the
314531ae4b0SThomas Gleixner * pi chain. Let the waiter sort it out.
315531ae4b0SThomas Gleixner */
316531ae4b0SThomas Gleixner ret = 0;
317531ae4b0SThomas Gleixner }
318531ae4b0SThomas Gleixner
319531ae4b0SThomas Gleixner return ret;
320531ae4b0SThomas Gleixner }
321531ae4b0SThomas Gleixner
322531ae4b0SThomas Gleixner /**
323531ae4b0SThomas Gleixner * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
324531ae4b0SThomas Gleixner * @lock: the rt_mutex to take
325531ae4b0SThomas Gleixner * @waiter: the pre-initialized rt_mutex_waiter
326531ae4b0SThomas Gleixner * @task: the task to prepare
327531ae4b0SThomas Gleixner *
328531ae4b0SThomas Gleixner * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
329531ae4b0SThomas Gleixner * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
330531ae4b0SThomas Gleixner *
331531ae4b0SThomas Gleixner * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
332531ae4b0SThomas Gleixner * on failure.
333531ae4b0SThomas Gleixner *
334531ae4b0SThomas Gleixner * Returns:
335531ae4b0SThomas Gleixner * 0 - task blocked on lock
336531ae4b0SThomas Gleixner * 1 - acquired the lock for task, caller should wake it up
337531ae4b0SThomas Gleixner * <0 - error
338531ae4b0SThomas Gleixner *
339531ae4b0SThomas Gleixner * Special API call for PI-futex support.
340531ae4b0SThomas Gleixner */
rt_mutex_start_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task)341830e6accSPeter Zijlstra int __sched rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
342531ae4b0SThomas Gleixner struct rt_mutex_waiter *waiter,
343531ae4b0SThomas Gleixner struct task_struct *task)
344531ae4b0SThomas Gleixner {
345531ae4b0SThomas Gleixner int ret;
346894d1b3dSPeter Zijlstra DEFINE_WAKE_Q(wake_q);
347531ae4b0SThomas Gleixner
348531ae4b0SThomas Gleixner raw_spin_lock_irq(&lock->wait_lock);
349894d1b3dSPeter Zijlstra ret = __rt_mutex_start_proxy_lock(lock, waiter, task, &wake_q);
350531ae4b0SThomas Gleixner if (unlikely(ret))
351531ae4b0SThomas Gleixner remove_waiter(lock, waiter);
352894d1b3dSPeter Zijlstra preempt_disable();
353531ae4b0SThomas Gleixner raw_spin_unlock_irq(&lock->wait_lock);
354894d1b3dSPeter Zijlstra wake_up_q(&wake_q);
355894d1b3dSPeter Zijlstra preempt_enable();
356531ae4b0SThomas Gleixner
357531ae4b0SThomas Gleixner return ret;
358531ae4b0SThomas Gleixner }
359531ae4b0SThomas Gleixner
360531ae4b0SThomas Gleixner /**
361531ae4b0SThomas Gleixner * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
362531ae4b0SThomas Gleixner * @lock: the rt_mutex we were woken on
363531ae4b0SThomas Gleixner * @to: the timeout, null if none. hrtimer should already have
364531ae4b0SThomas Gleixner * been started.
365531ae4b0SThomas Gleixner * @waiter: the pre-initialized rt_mutex_waiter
366531ae4b0SThomas Gleixner *
367531ae4b0SThomas Gleixner * Wait for the lock acquisition started on our behalf by
368531ae4b0SThomas Gleixner * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
369531ae4b0SThomas Gleixner * rt_mutex_cleanup_proxy_lock().
370531ae4b0SThomas Gleixner *
371531ae4b0SThomas Gleixner * Returns:
372531ae4b0SThomas Gleixner * 0 - success
373531ae4b0SThomas Gleixner * <0 - error, one of -EINTR, -ETIMEDOUT
374531ae4b0SThomas Gleixner *
375531ae4b0SThomas Gleixner * Special API call for PI-futex support
376531ae4b0SThomas Gleixner */
rt_mutex_wait_proxy_lock(struct rt_mutex_base * lock,struct hrtimer_sleeper * to,struct rt_mutex_waiter * waiter)377830e6accSPeter Zijlstra int __sched rt_mutex_wait_proxy_lock(struct rt_mutex_base *lock,
378531ae4b0SThomas Gleixner struct hrtimer_sleeper *to,
379531ae4b0SThomas Gleixner struct rt_mutex_waiter *waiter)
380531ae4b0SThomas Gleixner {
381531ae4b0SThomas Gleixner int ret;
382531ae4b0SThomas Gleixner
383531ae4b0SThomas Gleixner raw_spin_lock_irq(&lock->wait_lock);
384531ae4b0SThomas Gleixner /* sleep on the mutex */
385531ae4b0SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE);
386*4a077914SJohn Stultz ret = rt_mutex_slowlock_block(lock, NULL, TASK_INTERRUPTIBLE, to, waiter, NULL);
387531ae4b0SThomas Gleixner /*
388531ae4b0SThomas Gleixner * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
389531ae4b0SThomas Gleixner * have to fix that up.
390531ae4b0SThomas Gleixner */
3911c0908d8SMel Gorman fixup_rt_mutex_waiters(lock, true);
392531ae4b0SThomas Gleixner raw_spin_unlock_irq(&lock->wait_lock);
393531ae4b0SThomas Gleixner
394531ae4b0SThomas Gleixner return ret;
395531ae4b0SThomas Gleixner }
396531ae4b0SThomas Gleixner
397531ae4b0SThomas Gleixner /**
398531ae4b0SThomas Gleixner * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
399531ae4b0SThomas Gleixner * @lock: the rt_mutex we were woken on
400531ae4b0SThomas Gleixner * @waiter: the pre-initialized rt_mutex_waiter
401531ae4b0SThomas Gleixner *
402531ae4b0SThomas Gleixner * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
403531ae4b0SThomas Gleixner * rt_mutex_wait_proxy_lock().
404531ae4b0SThomas Gleixner *
405531ae4b0SThomas Gleixner * Unless we acquired the lock; we're still enqueued on the wait-list and can
406531ae4b0SThomas Gleixner * in fact still be granted ownership until we're removed. Therefore we can
407531ae4b0SThomas Gleixner * find we are in fact the owner and must disregard the
408531ae4b0SThomas Gleixner * rt_mutex_wait_proxy_lock() failure.
409531ae4b0SThomas Gleixner *
410531ae4b0SThomas Gleixner * Returns:
411531ae4b0SThomas Gleixner * true - did the cleanup, we done.
412531ae4b0SThomas Gleixner * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
413531ae4b0SThomas Gleixner * caller should disregards its return value.
414531ae4b0SThomas Gleixner *
415531ae4b0SThomas Gleixner * Special API call for PI-futex support
416531ae4b0SThomas Gleixner */
rt_mutex_cleanup_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)417830e6accSPeter Zijlstra bool __sched rt_mutex_cleanup_proxy_lock(struct rt_mutex_base *lock,
418531ae4b0SThomas Gleixner struct rt_mutex_waiter *waiter)
419531ae4b0SThomas Gleixner {
420531ae4b0SThomas Gleixner bool cleanup = false;
421531ae4b0SThomas Gleixner
422531ae4b0SThomas Gleixner raw_spin_lock_irq(&lock->wait_lock);
423531ae4b0SThomas Gleixner /*
424531ae4b0SThomas Gleixner * Do an unconditional try-lock, this deals with the lock stealing
425531ae4b0SThomas Gleixner * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
426531ae4b0SThomas Gleixner * sets a NULL owner.
427531ae4b0SThomas Gleixner *
428531ae4b0SThomas Gleixner * We're not interested in the return value, because the subsequent
429531ae4b0SThomas Gleixner * test on rt_mutex_owner() will infer that. If the trylock succeeded,
430531ae4b0SThomas Gleixner * we will own the lock and it will have removed the waiter. If we
431531ae4b0SThomas Gleixner * failed the trylock, we're still not owner and we need to remove
432531ae4b0SThomas Gleixner * ourselves.
433531ae4b0SThomas Gleixner */
434531ae4b0SThomas Gleixner try_to_take_rt_mutex(lock, current, waiter);
435531ae4b0SThomas Gleixner /*
436531ae4b0SThomas Gleixner * Unless we're the owner; we're still enqueued on the wait_list.
437531ae4b0SThomas Gleixner * So check if we became owner, if not, take us off the wait_list.
438531ae4b0SThomas Gleixner */
439531ae4b0SThomas Gleixner if (rt_mutex_owner(lock) != current) {
440531ae4b0SThomas Gleixner remove_waiter(lock, waiter);
441531ae4b0SThomas Gleixner cleanup = true;
442531ae4b0SThomas Gleixner }
443531ae4b0SThomas Gleixner /*
444531ae4b0SThomas Gleixner * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
445531ae4b0SThomas Gleixner * have to fix that up.
446531ae4b0SThomas Gleixner */
4471c0908d8SMel Gorman fixup_rt_mutex_waiters(lock, false);
448531ae4b0SThomas Gleixner
449531ae4b0SThomas Gleixner raw_spin_unlock_irq(&lock->wait_lock);
450531ae4b0SThomas Gleixner
451531ae4b0SThomas Gleixner return cleanup;
452531ae4b0SThomas Gleixner }
453531ae4b0SThomas Gleixner
454531ae4b0SThomas Gleixner /*
455531ae4b0SThomas Gleixner * Recheck the pi chain, in case we got a priority setting
456531ae4b0SThomas Gleixner *
457531ae4b0SThomas Gleixner * Called from sched_setscheduler
458531ae4b0SThomas Gleixner */
rt_mutex_adjust_pi(struct task_struct * task)459531ae4b0SThomas Gleixner void __sched rt_mutex_adjust_pi(struct task_struct *task)
460531ae4b0SThomas Gleixner {
461531ae4b0SThomas Gleixner struct rt_mutex_waiter *waiter;
462830e6accSPeter Zijlstra struct rt_mutex_base *next_lock;
463531ae4b0SThomas Gleixner unsigned long flags;
464531ae4b0SThomas Gleixner
465531ae4b0SThomas Gleixner raw_spin_lock_irqsave(&task->pi_lock, flags);
466531ae4b0SThomas Gleixner
467531ae4b0SThomas Gleixner waiter = task->pi_blocked_on;
468f7853c34SPeter Zijlstra if (!waiter || rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
469531ae4b0SThomas Gleixner raw_spin_unlock_irqrestore(&task->pi_lock, flags);
470531ae4b0SThomas Gleixner return;
471531ae4b0SThomas Gleixner }
472531ae4b0SThomas Gleixner next_lock = waiter->lock;
473531ae4b0SThomas Gleixner raw_spin_unlock_irqrestore(&task->pi_lock, flags);
474531ae4b0SThomas Gleixner
475531ae4b0SThomas Gleixner /* gets dropped in rt_mutex_adjust_prio_chain()! */
476531ae4b0SThomas Gleixner get_task_struct(task);
477531ae4b0SThomas Gleixner
478531ae4b0SThomas Gleixner rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
479531ae4b0SThomas Gleixner next_lock, NULL, task);
480531ae4b0SThomas Gleixner }
481531ae4b0SThomas Gleixner
482531ae4b0SThomas Gleixner /*
483531ae4b0SThomas Gleixner * Performs the wakeup of the top-waiter and re-enables preemption.
484531ae4b0SThomas Gleixner */
rt_mutex_postunlock(struct rt_wake_q_head * wqh)4857980aa39SThomas Gleixner void __sched rt_mutex_postunlock(struct rt_wake_q_head *wqh)
486531ae4b0SThomas Gleixner {
4877980aa39SThomas Gleixner rt_mutex_wake_up_q(wqh);
488531ae4b0SThomas Gleixner }
489531ae4b0SThomas Gleixner
490531ae4b0SThomas Gleixner #ifdef CONFIG_DEBUG_RT_MUTEXES
rt_mutex_debug_task_free(struct task_struct * task)491531ae4b0SThomas Gleixner void rt_mutex_debug_task_free(struct task_struct *task)
492531ae4b0SThomas Gleixner {
493531ae4b0SThomas Gleixner DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
494531ae4b0SThomas Gleixner DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
495531ae4b0SThomas Gleixner }
496531ae4b0SThomas Gleixner #endif
497bb630f9fSThomas Gleixner
498bb630f9fSThomas Gleixner #ifdef CONFIG_PREEMPT_RT
499bb630f9fSThomas Gleixner /* Mutexes */
__mutex_rt_init(struct mutex * mutex,const char * name,struct lock_class_key * key)500bb630f9fSThomas Gleixner void __mutex_rt_init(struct mutex *mutex, const char *name,
501bb630f9fSThomas Gleixner struct lock_class_key *key)
502bb630f9fSThomas Gleixner {
503bb630f9fSThomas Gleixner debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
504bb630f9fSThomas Gleixner lockdep_init_map_wait(&mutex->dep_map, name, key, 0, LD_WAIT_SLEEP);
505bb630f9fSThomas Gleixner }
506bb630f9fSThomas Gleixner EXPORT_SYMBOL(__mutex_rt_init);
507bb630f9fSThomas Gleixner
__mutex_lock_common(struct mutex * lock,unsigned int state,unsigned int subclass,struct lockdep_map * nest_lock,unsigned long ip)508bb630f9fSThomas Gleixner static __always_inline int __mutex_lock_common(struct mutex *lock,
509bb630f9fSThomas Gleixner unsigned int state,
510bb630f9fSThomas Gleixner unsigned int subclass,
511bb630f9fSThomas Gleixner struct lockdep_map *nest_lock,
512bb630f9fSThomas Gleixner unsigned long ip)
513bb630f9fSThomas Gleixner {
514bb630f9fSThomas Gleixner int ret;
515bb630f9fSThomas Gleixner
516bb630f9fSThomas Gleixner might_sleep();
517bb630f9fSThomas Gleixner mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
518bb630f9fSThomas Gleixner ret = __rt_mutex_lock(&lock->rtmutex, state);
519bb630f9fSThomas Gleixner if (ret)
520bb630f9fSThomas Gleixner mutex_release(&lock->dep_map, ip);
521bb630f9fSThomas Gleixner else
522bb630f9fSThomas Gleixner lock_acquired(&lock->dep_map, ip);
523bb630f9fSThomas Gleixner return ret;
524bb630f9fSThomas Gleixner }
525bb630f9fSThomas Gleixner
526bb630f9fSThomas Gleixner #ifdef CONFIG_DEBUG_LOCK_ALLOC
mutex_lock_nested(struct mutex * lock,unsigned int subclass)527bb630f9fSThomas Gleixner void __sched mutex_lock_nested(struct mutex *lock, unsigned int subclass)
528bb630f9fSThomas Gleixner {
529bb630f9fSThomas Gleixner __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
530bb630f9fSThomas Gleixner }
531bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_nested);
532bb630f9fSThomas Gleixner
_mutex_lock_nest_lock(struct mutex * lock,struct lockdep_map * nest_lock)533bb630f9fSThomas Gleixner void __sched _mutex_lock_nest_lock(struct mutex *lock,
534bb630f9fSThomas Gleixner struct lockdep_map *nest_lock)
535bb630f9fSThomas Gleixner {
536bb630f9fSThomas Gleixner __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, nest_lock, _RET_IP_);
537bb630f9fSThomas Gleixner }
538bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
539bb630f9fSThomas Gleixner
mutex_lock_interruptible_nested(struct mutex * lock,unsigned int subclass)540bb630f9fSThomas Gleixner int __sched mutex_lock_interruptible_nested(struct mutex *lock,
541bb630f9fSThomas Gleixner unsigned int subclass)
542bb630f9fSThomas Gleixner {
543bb630f9fSThomas Gleixner return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
544bb630f9fSThomas Gleixner }
545bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
546bb630f9fSThomas Gleixner
mutex_lock_killable_nested(struct mutex * lock,unsigned int subclass)547bb630f9fSThomas Gleixner int __sched mutex_lock_killable_nested(struct mutex *lock,
548bb630f9fSThomas Gleixner unsigned int subclass)
549bb630f9fSThomas Gleixner {
550bb630f9fSThomas Gleixner return __mutex_lock_common(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
551bb630f9fSThomas Gleixner }
552bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
553bb630f9fSThomas Gleixner
mutex_lock_io_nested(struct mutex * lock,unsigned int subclass)554bb630f9fSThomas Gleixner void __sched mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
555bb630f9fSThomas Gleixner {
556bb630f9fSThomas Gleixner int token;
557bb630f9fSThomas Gleixner
558bb630f9fSThomas Gleixner might_sleep();
559bb630f9fSThomas Gleixner
560bb630f9fSThomas Gleixner token = io_schedule_prepare();
561bb630f9fSThomas Gleixner __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
562bb630f9fSThomas Gleixner io_schedule_finish(token);
563bb630f9fSThomas Gleixner }
564bb630f9fSThomas Gleixner EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
565bb630f9fSThomas Gleixner
566bb630f9fSThomas Gleixner #else /* CONFIG_DEBUG_LOCK_ALLOC */
567bb630f9fSThomas Gleixner
mutex_lock(struct mutex * lock)568bb630f9fSThomas Gleixner void __sched mutex_lock(struct mutex *lock)
569bb630f9fSThomas Gleixner {
570bb630f9fSThomas Gleixner __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
571bb630f9fSThomas Gleixner }
572bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock);
573bb630f9fSThomas Gleixner
mutex_lock_interruptible(struct mutex * lock)574bb630f9fSThomas Gleixner int __sched mutex_lock_interruptible(struct mutex *lock)
575bb630f9fSThomas Gleixner {
576bb630f9fSThomas Gleixner return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
577bb630f9fSThomas Gleixner }
578bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock_interruptible);
579bb630f9fSThomas Gleixner
mutex_lock_killable(struct mutex * lock)580bb630f9fSThomas Gleixner int __sched mutex_lock_killable(struct mutex *lock)
581bb630f9fSThomas Gleixner {
582bb630f9fSThomas Gleixner return __mutex_lock_common(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
583bb630f9fSThomas Gleixner }
584bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock_killable);
585bb630f9fSThomas Gleixner
mutex_lock_io(struct mutex * lock)586bb630f9fSThomas Gleixner void __sched mutex_lock_io(struct mutex *lock)
587bb630f9fSThomas Gleixner {
588bb630f9fSThomas Gleixner int token = io_schedule_prepare();
589bb630f9fSThomas Gleixner
590bb630f9fSThomas Gleixner __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
591bb630f9fSThomas Gleixner io_schedule_finish(token);
592bb630f9fSThomas Gleixner }
593bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_lock_io);
594bb630f9fSThomas Gleixner #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
595bb630f9fSThomas Gleixner
mutex_trylock(struct mutex * lock)596bb630f9fSThomas Gleixner int __sched mutex_trylock(struct mutex *lock)
597bb630f9fSThomas Gleixner {
598bb630f9fSThomas Gleixner int ret;
599bb630f9fSThomas Gleixner
600bb630f9fSThomas Gleixner if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
601bb630f9fSThomas Gleixner return 0;
602bb630f9fSThomas Gleixner
603bb630f9fSThomas Gleixner ret = __rt_mutex_trylock(&lock->rtmutex);
604bb630f9fSThomas Gleixner if (ret)
605bb630f9fSThomas Gleixner mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
606bb630f9fSThomas Gleixner
607bb630f9fSThomas Gleixner return ret;
608bb630f9fSThomas Gleixner }
609bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_trylock);
610bb630f9fSThomas Gleixner
mutex_unlock(struct mutex * lock)611bb630f9fSThomas Gleixner void __sched mutex_unlock(struct mutex *lock)
612bb630f9fSThomas Gleixner {
613bb630f9fSThomas Gleixner mutex_release(&lock->dep_map, _RET_IP_);
614bb630f9fSThomas Gleixner __rt_mutex_unlock(&lock->rtmutex);
615bb630f9fSThomas Gleixner }
616bb630f9fSThomas Gleixner EXPORT_SYMBOL(mutex_unlock);
617bb630f9fSThomas Gleixner
618bb630f9fSThomas Gleixner #endif /* CONFIG_PREEMPT_RT */
619