blob: 5187added8bcee462be016c3865e86cb513d74c9 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07002/*
3 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
4 *
5 * started by Ingo Molnar and Thomas Gleixner.
6 *
7 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9 * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
10 * Copyright (C) 2006 Esben Nielsen
Steven Rostedtd07fe82c22006-07-30 03:04:03 -070011 *
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030012 * See Documentation/locking/rt-mutex-design.rst for details.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070013 */
14#include <linux/spinlock.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010016#include <linux/sched/signal.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060017#include <linux/sched/rt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010018#include <linux/sched/deadline.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010019#include <linux/sched/wake_q.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070021#include <linux/timer.h>
22
23#include "rtmutex_common.h"
24
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070025/*
26 * lock->owner state tracking:
27 *
Lai Jiangshan81612392011-01-14 17:09:41 +080028 * lock->owner holds the task_struct pointer of the owner. Bit 0
29 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070030 *
Lai Jiangshan81612392011-01-14 17:09:41 +080031 * owner bit0
32 * NULL 0 lock is free (fast acquire possible)
33 * NULL 1 lock is free and has waiters and the top waiter
34 * is going to take the lock*
35 * taskpointer 0 lock is held (fast release possible)
36 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070037 *
38 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080039 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070040 *
Lai Jiangshan81612392011-01-14 17:09:41 +080041 * (*) It also can be a transitional state when grabbing the lock
42 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
43 * we need to set the bit0 before looking at the lock, and the owner may be
44 * NULL in this small time, hence this can be a transitional state.
45 *
46 * (**) There is a small time when bit 0 is set but there are no
47 * waiters. This can happen when grabbing the lock in the slow path.
48 * To prevent a cmpxchg of the owner releasing the lock, we need to
49 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070050 */
51
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +010052static __always_inline void
Lai Jiangshan81612392011-01-14 17:09:41 +080053rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070054{
Lai Jiangshan81612392011-01-14 17:09:41 +080055 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070056
57 if (rt_mutex_has_waiters(lock))
58 val |= RT_MUTEX_HAS_WAITERS;
59
Paul E. McKenney0050c7b2020-01-03 15:59:12 -080060 WRITE_ONCE(lock->owner, (struct task_struct *)val);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070061}
62
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +010063static __always_inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070064{
65 lock->owner = (struct task_struct *)
66 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
67}
68
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +010069static __always_inline void fixup_rt_mutex_waiters(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070070{
Thomas Gleixnerdbb26052016-11-30 21:04:41 +000071 unsigned long owner, *p = (unsigned long *) &lock->owner;
72
73 if (rt_mutex_has_waiters(lock))
74 return;
75
76 /*
77 * The rbtree has no waiters enqueued, now make sure that the
78 * lock->owner still has the waiters bit set, otherwise the
79 * following can happen:
80 *
81 * CPU 0 CPU 1 CPU2
82 * l->owner=T1
83 * rt_mutex_lock(l)
84 * lock(l->lock)
85 * l->owner = T1 | HAS_WAITERS;
86 * enqueue(T2)
87 * boost()
88 * unlock(l->lock)
89 * block()
90 *
91 * rt_mutex_lock(l)
92 * lock(l->lock)
93 * l->owner = T1 | HAS_WAITERS;
94 * enqueue(T3)
95 * boost()
96 * unlock(l->lock)
97 * block()
98 * signal(->T2) signal(->T3)
99 * lock(l->lock)
100 * dequeue(T2)
101 * deboost()
102 * unlock(l->lock)
103 * lock(l->lock)
104 * dequeue(T3)
105 * ==> wait list is empty
106 * deboost()
107 * unlock(l->lock)
108 * lock(l->lock)
109 * fixup_rt_mutex_waiters()
110 * if (wait_list_empty(l) {
111 * l->owner = owner
112 * owner = l->owner & ~HAS_WAITERS;
113 * ==> l->owner = T1
114 * }
115 * lock(l->lock)
116 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
117 * if (wait_list_empty(l) {
118 * owner = l->owner & ~HAS_WAITERS;
119 * cmpxchg(l->owner, T1, NULL)
120 * ===> Success (l->owner = NULL)
121 *
122 * l->owner = owner
123 * ==> l->owner = T1
124 * }
125 *
126 * With the check for the waiter bit in place T3 on CPU2 will not
127 * overwrite. All tasks fiddling with the waiters bit are
128 * serialized by l->lock, so nothing else can modify the waiters
129 * bit. If the bit is set then nothing can change l->owner either
130 * so the simple RMW is safe. The cmpxchg() will simply fail if it
131 * happens in the middle of the RMW because the waiters bit is
132 * still set.
133 */
134 owner = READ_ONCE(*p);
135 if (owner & RT_MUTEX_HAS_WAITERS)
136 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700137}
138
139/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100140 * We can speed up the acquire/release, if there's no debugging state to be
141 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200142 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100143#ifndef CONFIG_DEBUG_RT_MUTEXES
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200144static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex *lock,
145 struct task_struct *old,
146 struct task_struct *new)
147{
148 return cmpxchg_acquire(&lock->owner, old, new) == old;
149}
150
151static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex *lock,
152 struct task_struct *old,
153 struct task_struct *new)
154{
155 return cmpxchg_release(&lock->owner, old, new) == old;
156}
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700157
158/*
159 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
160 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
161 * relaxed semantics suffice.
162 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100163static __always_inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200164{
165 unsigned long owner, *p = (unsigned long *) &lock->owner;
166
167 do {
168 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700169 } while (cmpxchg_relaxed(p, owner,
170 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200171}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000172
173/*
174 * Safe fastpath aware unlock:
175 * 1) Clear the waiters bit
176 * 2) Drop lock->wait_lock
177 * 3) Try to unlock the lock with cmpxchg
178 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100179static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
180 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000181 __releases(lock->wait_lock)
182{
183 struct task_struct *owner = rt_mutex_owner(lock);
184
185 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100186 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000187 /*
188 * If a new waiter comes in between the unlock and the cmpxchg
189 * we have two situations:
190 *
191 * unlock(wait_lock);
192 * lock(wait_lock);
193 * cmpxchg(p, owner, 0) == owner
194 * mark_rt_mutex_waiters(lock);
195 * acquire(lock);
196 * or:
197 *
198 * unlock(wait_lock);
199 * lock(wait_lock);
200 * mark_rt_mutex_waiters(lock);
201 *
202 * cmpxchg(p, owner, 0) != owner
203 * enqueue_waiter();
204 * unlock(wait_lock);
205 * lock(wait_lock);
206 * wake waiter();
207 * unlock(wait_lock);
208 * lock(wait_lock);
209 * acquire(lock);
210 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700211 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000212}
213
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200214#else
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200215static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex *lock,
216 struct task_struct *old,
217 struct task_struct *new)
218{
219 return false;
220
221}
222
223static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex *lock,
224 struct task_struct *old,
225 struct task_struct *new)
226{
227 return false;
228}
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700229
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100230static __always_inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200231{
232 lock->owner = (struct task_struct *)
233 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
234}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000235
236/*
237 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
238 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100239static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
240 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000241 __releases(lock->wait_lock)
242{
243 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100244 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000245 return true;
246}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200247#endif
248
Peter Zijlstra19830e52017-03-23 15:56:14 +0100249/*
250 * Only use with rt_mutex_waiter_{less,equal}()
251 */
252#define task_to_waiter(p) \
253 &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
254
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100255static __always_inline int rt_mutex_waiter_less(struct rt_mutex_waiter *left,
256 struct rt_mutex_waiter *right)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100257{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100258 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100259 return 1;
260
261 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100262 * If both waiters have dl_prio(), we check the deadlines of the
263 * associated tasks.
264 * If left waiter has a dl_prio(), and we didn't return 1 above,
265 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100266 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100267 if (dl_prio(left->prio))
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100268 return dl_time_before(left->deadline, right->deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100269
270 return 0;
271}
272
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100273static __always_inline int rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
274 struct rt_mutex_waiter *right)
Peter Zijlstra19830e52017-03-23 15:56:14 +0100275{
276 if (left->prio != right->prio)
277 return 0;
278
279 /*
280 * If both waiters have dl_prio(), we check the deadlines of the
281 * associated tasks.
282 * If left waiter has a dl_prio(), and we didn't return 0 above,
283 * then right waiter has a dl_prio() too.
284 */
285 if (dl_prio(left->prio))
286 return left->deadline == right->deadline;
287
288 return 1;
289}
290
Peter Zijlstra5a798722020-04-29 17:29:58 +0200291#define __node_2_waiter(node) \
292 rb_entry((node), struct rt_mutex_waiter, tree_entry)
293
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100294static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
Peter Zijlstra5a798722020-04-29 17:29:58 +0200295{
296 return rt_mutex_waiter_less(__node_2_waiter(a), __node_2_waiter(b));
297}
298
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100299static __always_inline void
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100300rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
301{
Peter Zijlstra5a798722020-04-29 17:29:58 +0200302 rb_add_cached(&waiter->tree_entry, &lock->waiters, __waiter_less);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100303}
304
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100305static __always_inline void
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100306rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
307{
308 if (RB_EMPTY_NODE(&waiter->tree_entry))
309 return;
310
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700311 rb_erase_cached(&waiter->tree_entry, &lock->waiters);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100312 RB_CLEAR_NODE(&waiter->tree_entry);
313}
314
Peter Zijlstra5a798722020-04-29 17:29:58 +0200315#define __node_2_pi_waiter(node) \
316 rb_entry((node), struct rt_mutex_waiter, pi_tree_entry)
317
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100318static __always_inline bool
319__pi_waiter_less(struct rb_node *a, const struct rb_node *b)
Peter Zijlstra5a798722020-04-29 17:29:58 +0200320{
321 return rt_mutex_waiter_less(__node_2_pi_waiter(a), __node_2_pi_waiter(b));
322}
323
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100324static __always_inline void
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100325rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
326{
Peter Zijlstra5a798722020-04-29 17:29:58 +0200327 rb_add_cached(&waiter->pi_tree_entry, &task->pi_waiters, __pi_waiter_less);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100328}
329
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100330static __always_inline void
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100331rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
332{
333 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
334 return;
335
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700336 rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100337 RB_CLEAR_NODE(&waiter->pi_tree_entry);
338}
339
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100340static __always_inline void rt_mutex_adjust_prio(struct task_struct *p)
Xunlei Pange96a77052017-03-23 15:56:08 +0100341{
Peter Zijlstraacd58622017-03-23 15:56:11 +0100342 struct task_struct *pi_task = NULL;
Xunlei Pange96a77052017-03-23 15:56:08 +0100343
Peter Zijlstraacd58622017-03-23 15:56:11 +0100344 lockdep_assert_held(&p->pi_lock);
Xunlei Pange96a77052017-03-23 15:56:08 +0100345
Peter Zijlstraacd58622017-03-23 15:56:11 +0100346 if (task_has_pi_waiters(p))
347 pi_task = task_top_pi_waiter(p)->task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700348
Peter Zijlstraacd58622017-03-23 15:56:11 +0100349 rt_mutex_setprio(p, pi_task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700350}
351
352/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000353 * Deadlock detection is conditional:
354 *
355 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
356 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
357 *
358 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
359 * conducted independent of the detect argument.
360 *
361 * If the waiter argument is NULL this indicates the deboost path and
362 * deadlock detection is disabled independent of the detect argument
363 * and the config settings.
364 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100365static __always_inline bool
366rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
367 enum rtmutex_chainwalk chwalk)
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000368{
Zhen Lei07d25972021-07-31 20:30:11 +0800369 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
Thomas Gleixnerf7efc472021-03-26 16:29:36 +0100370 return waiter != NULL;
371 return chwalk == RT_MUTEX_FULL_CHAINWALK;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000372}
373
374/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700375 * Max number of times we'll walk the boosting chain:
376 */
377int max_lock_depth = 1024;
378
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100379static __always_inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
Thomas Gleixner82084982014-06-05 11:16:12 +0200380{
381 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
382}
383
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700384/*
385 * Adjust the priority chain. Also used for deadlock detection.
386 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200387 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200388 * @task: the task owning the mutex (owner) for which a chain walk is
389 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900390 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200391 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
392 * things for a task that has just got its priority adjusted, and
393 * is waiting on a mutex)
394 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
395 * we dropped its pi_lock. Is never dereferenced, only used for
396 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200397 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200398 * its priority to the mutex owner (can be NULL in the case
399 * depicted above or if the top waiter is gone away and we are
400 * actually deboosting the owner)
401 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200402 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700403 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200404 *
405 * Chain walk basics and protection scope
406 *
407 * [R] refcount on task
408 * [P] task->pi_lock held
409 * [L] rtmutex->wait_lock held
410 *
411 * Step Description Protected by
412 * function arguments:
413 * @task [R]
414 * @orig_lock if != NULL @top_task is blocked on it
415 * @next_lock Unprotected. Cannot be
416 * dereferenced. Only used for
417 * comparison.
418 * @orig_waiter if != NULL @top_task is blocked on it
419 * @top_task current, or in case of proxy
420 * locking protected by calling
421 * code
422 * again:
423 * loop_sanity_check();
424 * retry:
425 * [1] lock(task->pi_lock); [R] acquire [P]
426 * [2] waiter = task->pi_blocked_on; [P]
427 * [3] check_exit_conditions_1(); [P]
428 * [4] lock = waiter->lock; [P]
429 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
430 * unlock(task->pi_lock); release [P]
431 * goto retry;
432 * }
433 * [6] check_exit_conditions_2(); [P] + [L]
434 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
435 * [8] unlock(task->pi_lock); release [P]
436 * put_task_struct(task); release [R]
437 * [9] check_exit_conditions_3(); [L]
438 * [10] task = owner(lock); [L]
439 * get_task_struct(task); [L] acquire [R]
440 * lock(task->pi_lock); [L] acquire [P]
441 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
442 * [12] check_exit_conditions_4(); [P] + [L]
443 * [13] unlock(task->pi_lock); release [P]
444 * unlock(lock->wait_lock); release [L]
445 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700446 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100447static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
448 enum rtmutex_chainwalk chwalk,
449 struct rt_mutex *orig_lock,
450 struct rt_mutex *next_lock,
451 struct rt_mutex_waiter *orig_waiter,
452 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700453{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700454 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000455 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000456 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000457 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000458 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000459 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700460
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000461 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700462
463 /*
464 * The (de)boosting is a step by step approach with a lot of
465 * pitfalls. We want this to be preemptible and we want hold a
466 * maximum of two locks per step. So we have to check
467 * carefully whether things change under us.
468 */
469 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200470 /*
471 * We limit the lock chain length for each invocation.
472 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700473 if (++depth > max_lock_depth) {
474 static int prev_max;
475
476 /*
477 * Print this only once. If the admin changes the limit,
478 * print a new message when reaching the limit again.
479 */
480 if (prev_max != max_lock_depth) {
481 prev_max = max_lock_depth;
482 printk(KERN_WARNING "Maximum lock depth %d reached "
483 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700484 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700485 }
486 put_task_struct(task);
487
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200488 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700489 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200490
491 /*
492 * We are fully preemptible here and only hold the refcount on
493 * @task. So everything can have changed under us since the
494 * caller or our own code below (goto retry/again) dropped all
495 * locks.
496 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700497 retry:
498 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200499 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700500 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100501 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700502
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200503 /*
504 * [2] Get the waiter on which @task is blocked on.
505 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700506 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200507
508 /*
509 * [3] check_exit_conditions_1() protected by task->pi_lock.
510 */
511
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700512 /*
513 * Check whether the end of the boosting chain has been
514 * reached or the state of the chain has changed while we
515 * dropped the locks.
516 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800517 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700518 goto out_unlock_pi;
519
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700520 /*
521 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800522 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700523 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800524 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700525 goto out_unlock_pi;
526
527 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200528 * We dropped all locks after taking a refcount on @task, so
529 * the task might have moved on in the lock chain or even left
530 * the chain completely and blocks now on an unrelated lock or
531 * on @orig_lock.
532 *
533 * We stored the lock on which @task was blocked in @next_lock,
534 * so we can detect the chain change.
535 */
536 if (next_lock != waiter->lock)
537 goto out_unlock_pi;
538
539 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700540 * Drop out, when the task has no waiters. Note,
541 * top_waiter can be NULL, when we are in the deboosting
542 * mode!
543 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000544 if (top_waiter) {
545 if (!task_has_pi_waiters(task))
546 goto out_unlock_pi;
547 /*
548 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000549 * are not the top pi waiter of the task. If deadlock
550 * detection is enabled we continue, but stop the
551 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000552 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000553 if (top_waiter != task_top_pi_waiter(task)) {
554 if (!detect_deadlock)
555 goto out_unlock_pi;
556 else
557 requeue = false;
558 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000559 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700560
561 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000562 * If the waiter priority is the same as the task priority
563 * then there is no further priority adjustment necessary. If
564 * deadlock detection is off, we stop the chain walk. If its
565 * enabled we continue, but stop the requeueing in the chain
566 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700567 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100568 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000569 if (!detect_deadlock)
570 goto out_unlock_pi;
571 else
572 requeue = false;
573 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700574
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200575 /*
576 * [4] Get the next lock
577 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700578 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200579 /*
580 * [5] We need to trylock here as we are holding task->pi_lock,
581 * which is the reverse lock order versus the other rtmutex
582 * operations.
583 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100584 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100585 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700586 cpu_relax();
587 goto retry;
588 }
589
Thomas Gleixner397335f2014-05-22 03:25:39 +0000590 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200591 * [6] check_exit_conditions_2() protected by task->pi_lock and
592 * lock->wait_lock.
593 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000594 * Deadlock detection. If the lock is the same as the original
595 * lock which caused us to walk the lock chain or if the
596 * current lock is owned by the task which initiated the chain
597 * walk, we detected a deadlock.
598 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700599 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100600 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200601 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700602 goto out_unlock_pi;
603 }
604
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000605 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000606 * If we just follow the lock chain for deadlock detection, no
607 * need to do all the requeue operations. To avoid a truckload
608 * of conditionals around the various places below, just do the
609 * minimum chain walk checks.
610 */
611 if (!requeue) {
612 /*
613 * No requeue[7] here. Just release @task [8]
614 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100615 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000616 put_task_struct(task);
617
618 /*
619 * [9] check_exit_conditions_3 protected by lock->wait_lock.
620 * If there is no owner of the lock, end of chain.
621 */
622 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100623 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000624 return 0;
625 }
626
627 /* [10] Grab the next task, i.e. owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700628 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100629 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000630
631 /*
632 * No requeue [11] here. We just do deadlock detection.
633 *
634 * [12] Store whether owner is blocked
635 * itself. Decision is made after dropping the locks
636 */
637 next_lock = task_blocked_on_lock(task);
638 /*
639 * Get the top waiter for the next iteration
640 */
641 top_waiter = rt_mutex_top_waiter(lock);
642
643 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100644 raw_spin_unlock(&task->pi_lock);
645 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000646
647 /* If owner is not blocked, end of chain. */
648 if (!next_lock)
649 goto out_put_task;
650 goto again;
651 }
652
653 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000654 * Store the current top waiter before doing the requeue
655 * operation on @lock. We need it for the boost/deboost
656 * decision below.
657 */
658 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700659
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700660 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100661 rt_mutex_dequeue(lock, waiter);
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100662
663 /*
664 * Update the waiter prio fields now that we're dequeued.
665 *
666 * These values can have changed through either:
667 *
668 * sys_sched_set_scheduler() / sys_sched_setattr()
669 *
670 * or
671 *
672 * DL CBS enforcement advancing the effective deadline.
673 *
674 * Even though pi_waiters also uses these fields, and that tree is only
675 * updated in [11], we can do this here, since we hold [L], which
676 * serializes all pi_waiters access and rb_erase() does not care about
677 * the values of the node being removed.
678 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100679 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100680 waiter->deadline = task->dl.deadline;
681
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100682 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700683
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200684 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100685 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200686 put_task_struct(task);
687
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000688 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200689 * [9] check_exit_conditions_3 protected by lock->wait_lock.
690 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000691 * We must abort the chain walk if there is no lock owner even
692 * in the dead lock detection case, as we have nothing to
693 * follow here. This is the end of the chain we are walking.
694 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800695 if (!rt_mutex_owner(lock)) {
696 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200697 * If the requeue [7] above changed the top waiter,
698 * then we need to wake the new top waiter up to try
699 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800700 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000701 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800702 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100703 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200704 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800705 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700706
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200707 /* [10] Grab the next task, i.e. the owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700708 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100709 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700710
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200711 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700712 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000713 /*
714 * The waiter became the new top (highest priority)
715 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700716 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000717 * and adjust the priority of the owner.
718 */
719 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100720 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100721 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700722
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000723 } else if (prerequeue_top_waiter == waiter) {
724 /*
725 * The waiter was the top waiter on the lock, but is
Ingo Molnare2db7592021-03-22 02:35:05 +0100726 * no longer the top priority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700727 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000728 * (highest priority) waiter and adjust the priority
729 * of the owner.
730 * The new top waiter is stored in @waiter so that
731 * @waiter == @top_waiter evaluates to true below and
732 * we continue to deboost the rest of the chain.
733 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100734 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700735 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100736 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100737 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000738 } else {
739 /*
740 * Nothing changed. No need to do any priority
741 * adjustment.
742 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700743 }
744
Thomas Gleixner82084982014-06-05 11:16:12 +0200745 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200746 * [12] check_exit_conditions_4() protected by task->pi_lock
747 * and lock->wait_lock. The actual decisions are made after we
748 * dropped the locks.
749 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200750 * Check whether the task which owns the current lock is pi
751 * blocked itself. If yes we store a pointer to the lock for
752 * the lock chain change detection above. After we dropped
753 * task->pi_lock next_lock cannot be dereferenced anymore.
754 */
755 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000756 /*
757 * Store the top waiter of @lock for the end of chain walk
758 * decision below.
759 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700760 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200761
762 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100763 raw_spin_unlock(&task->pi_lock);
764 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700765
Thomas Gleixner82084982014-06-05 11:16:12 +0200766 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200767 * Make the actual exit decisions [12], based on the stored
768 * values.
769 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200770 * We reached the end of the lock chain. Stop right here. No
771 * point to go back just to figure that out.
772 */
773 if (!next_lock)
774 goto out_put_task;
775
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000776 /*
777 * If the current waiter is not the top waiter on the lock,
778 * then we can stop the chain walk here if we are not in full
779 * deadlock detection mode.
780 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700781 if (!detect_deadlock && waiter != top_waiter)
782 goto out_put_task;
783
784 goto again;
785
786 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100787 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700788 out_put_task:
789 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700790
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700791 return ret;
792}
793
794/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700795 * Try to take an rt-mutex
796 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100797 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800798 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200799 * @lock: The lock to be acquired.
800 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700801 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200802 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700803 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100804static int __sched
805try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
806 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700807{
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100808 lockdep_assert_held(&lock->wait_lock);
809
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700810 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200811 * Before testing whether we can acquire @lock, we set the
812 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
813 * other tasks which try to modify @lock into the slow path
814 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700815 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200816 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
817 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700818 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200819 * - There is a lock owner. The caller must fixup the
820 * transient state if it does a trylock or leaves the lock
821 * function due to a signal or timeout.
822 *
823 * - @task acquires the lock and there are no other
824 * waiters. This is undone in rt_mutex_set_owner(@task) at
825 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700826 */
827 mark_rt_mutex_waiters(lock);
828
Thomas Gleixner358c3312014-06-11 01:01:13 +0200829 /*
830 * If @lock has an owner, give up.
831 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800832 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700833 return 0;
834
Lai Jiangshan81612392011-01-14 17:09:41 +0800835 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200836 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700837 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200838 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800839 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200840 if (waiter) {
841 /*
842 * If waiter is not the highest priority waiter of
843 * @lock, give up.
844 */
845 if (waiter != rt_mutex_top_waiter(lock))
846 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800847
848 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200849 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700850 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200851 */
852 rt_mutex_dequeue(lock, waiter);
853
854 } else {
855 /*
856 * If the lock has waiters already we check whether @task is
857 * eligible to take over the lock.
858 *
859 * If there are no other waiters, @task can acquire
860 * the lock. @task->pi_blocked_on is NULL, so it does
861 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800862 */
863 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200864 /*
865 * If @task->prio is greater than or equal to
866 * the top waiter priority (kernel view),
867 * @task lost.
868 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100869 if (!rt_mutex_waiter_less(task_to_waiter(task),
870 rt_mutex_top_waiter(lock)))
Thomas Gleixner358c3312014-06-11 01:01:13 +0200871 return 0;
872
873 /*
874 * The current top waiter stays enqueued. We
875 * don't have to change anything in the lock
876 * waiters order.
877 */
878 } else {
879 /*
880 * No waiters. Take the lock without the
881 * pi_lock dance.@task->pi_blocked_on is NULL
882 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700883 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200884 */
885 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800886 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800887 }
888
Thomas Gleixner358c3312014-06-11 01:01:13 +0200889 /*
890 * Clear @task->pi_blocked_on. Requires protection by
891 * @task->pi_lock. Redundant operation for the @waiter == NULL
892 * case, but conditionals are more expensive than a redundant
893 * store.
894 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100895 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200896 task->pi_blocked_on = NULL;
897 /*
898 * Finish the lock acquisition. @task is the new owner. If
899 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700900 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200901 */
902 if (rt_mutex_has_waiters(lock))
903 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100904 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200905
906takeit:
Thomas Gleixner358c3312014-06-11 01:01:13 +0200907 /*
908 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
909 * are still waiters or clears it.
910 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800911 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700912
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700913 return 1;
914}
915
916/*
917 * Task blocks on lock.
918 *
919 * Prepare waiter and propagate pi chain
920 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100921 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700922 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100923static int __sched task_blocks_on_rt_mutex(struct rt_mutex *lock,
924 struct rt_mutex_waiter *waiter,
925 struct task_struct *task,
926 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700927{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700928 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700929 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200930 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700931 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700932
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100933 lockdep_assert_held(&lock->wait_lock);
934
Thomas Gleixner397335f2014-05-22 03:25:39 +0000935 /*
936 * Early deadlock detection. We really don't want the task to
937 * enqueue on itself just to untangle the mess later. It's not
938 * only an optimization. We drop the locks, so another waiter
939 * can come in before the chain walk detects the deadlock. So
940 * the other will detect the deadlock and return -EDEADLOCK,
941 * which is wrong, as the other waiter is not in a deadlock
942 * situation.
943 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200944 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000945 return -EDEADLK;
946
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100947 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700948 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700949 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100950 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100951 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700952
953 /* Get the top priority waiter on the lock */
954 if (rt_mutex_has_waiters(lock))
955 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100956 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700957
Darren Hart8dac4562009-04-03 13:40:12 -0700958 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700959
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100960 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700961
Lai Jiangshan81612392011-01-14 17:09:41 +0800962 if (!owner)
963 return 0;
964
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100965 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700966 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100967 rt_mutex_dequeue_pi(owner, top_waiter);
968 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700969
Peter Zijlstraacd58622017-03-23 15:56:11 +0100970 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700971 if (owner->pi_blocked_on)
972 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000973 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700974 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200975 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700976
Thomas Gleixner82084982014-06-05 11:16:12 +0200977 /* Store the lock on which owner is blocked or NULL */
978 next_lock = task_blocked_on_lock(owner);
979
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100980 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200981 /*
982 * Even if full deadlock detection is on, if the owner is not
983 * blocked itself, we can avoid finding this out in the chain
984 * walk.
985 */
986 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700987 return 0;
988
Steven Rostedtdb630632006-09-29 01:59:44 -0700989 /*
990 * The owner can't disappear while holding a lock,
991 * so the owner struct is protected by wait_lock.
992 * Gets dropped in rt_mutex_adjust_prio_chain()!
993 */
994 get_task_struct(owner);
995
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100996 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700997
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000998 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200999 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001000
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001001 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001002
1003 return res;
1004}
1005
1006/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001007 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001008 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001009 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001010 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001011 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001012static void __sched mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1013 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001014{
1015 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001016
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001017 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001018
1019 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001020
1021 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +01001022 * Remove it from current->pi_waiters and deboost.
1023 *
1024 * We must in fact deboost here in order to ensure we call
1025 * rt_mutex_setprio() to update p->pi_top_task before the
1026 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001027 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001028 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001029 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001030
Thomas Gleixner27e35712014-06-11 18:44:04 +00001031 /*
1032 * As we are waking up the top waiter, and the waiter stays
1033 * queued on the lock until it gets the lock, this lock
1034 * obviously has waiters. Just set the bit here and this has
1035 * the added benefit of forcing all new tasks into the
1036 * slow path making sure no task of lower priority than
1037 * the top waiter can steal this lock.
1038 */
1039 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001040
Peter Zijlstraacd58622017-03-23 15:56:11 +01001041 /*
1042 * We deboosted before waking the top waiter task such that we don't
1043 * run two tasks with the 'same' priority (and ensure the
1044 * p->pi_top_task pointer points to a blocked task). This however can
1045 * lead to priority inversion if we would get preempted after the
1046 * deboost but before waking our donor task, hence the preempt_disable()
1047 * before unlock.
1048 *
1049 * Pairs with preempt_enable() in rt_mutex_postunlock();
1050 */
1051 preempt_disable();
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001052 wake_q_add(wake_q, waiter->task);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001053 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001054}
1055
1056/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001057 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001058 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001059 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001060 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001061 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001062static void __sched remove_waiter(struct rt_mutex *lock,
1063 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001064{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001065 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001066 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001067 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001068
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +01001069 lockdep_assert_held(&lock->wait_lock);
1070
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001071 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001072 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001073 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001074 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001075
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001076 /*
1077 * Only update priority if the waiter was the highest priority
1078 * waiter of the lock and there is an owner to update.
1079 */
1080 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001081 return;
1082
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001083 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001084
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001085 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001086
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001087 if (rt_mutex_has_waiters(lock))
1088 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001089
Peter Zijlstraacd58622017-03-23 15:56:11 +01001090 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001091
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001092 /* Store the lock on which owner is blocked or NULL */
1093 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001094
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001095 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001096
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001097 /*
1098 * Don't walk the chain, if the owner task is not blocked
1099 * itself.
1100 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001101 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001102 return;
1103
Steven Rostedtdb630632006-09-29 01:59:44 -07001104 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1105 get_task_struct(owner);
1106
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001107 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001108
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001109 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1110 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001111
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001112 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001113}
1114
1115/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001116 * Recheck the pi chain, in case we got a priority setting
1117 *
1118 * Called from sched_setscheduler
1119 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001120void __sched rt_mutex_adjust_pi(struct task_struct *task)
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001121{
1122 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001123 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001124 unsigned long flags;
1125
Thomas Gleixner1d615482009-11-17 14:54:03 +01001126 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001127
1128 waiter = task->pi_blocked_on;
Peter Zijlstra19830e52017-03-23 15:56:14 +01001129 if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001130 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001131 return;
1132 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001133 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001134 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001135
Steven Rostedtdb630632006-09-29 01:59:44 -07001136 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1137 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001138
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001139 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1140 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001141}
1142
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001143void __sched rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
Peter Zijlstra50809352017-03-22 11:35:56 +01001144{
1145 debug_rt_mutex_init_waiter(waiter);
1146 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1147 RB_CLEAR_NODE(&waiter->tree_entry);
1148 waiter->task = NULL;
1149}
1150
Darren Hart8dac4562009-04-03 13:40:12 -07001151/**
1152 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1153 * @lock: the rt_mutex to take
1154 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001155 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001156 * @timeout: the pre-initialized and started timer, or NULL for none
1157 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001158 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001159 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001160 */
Peter Zijlstra2f064a52021-06-11 10:28:17 +02001161static int __sched __rt_mutex_slowlock(struct rt_mutex *lock, unsigned int state,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001162 struct hrtimer_sleeper *timeout,
1163 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001164{
1165 int ret = 0;
1166
1167 for (;;) {
1168 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001169 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001170 break;
1171
Thomas Gleixnera51a3272021-03-26 16:29:44 +01001172 if (timeout && !timeout->task) {
1173 ret = -ETIMEDOUT;
1174 break;
1175 }
1176 if (signal_pending_state(state, current)) {
1177 ret = -EINTR;
1178 break;
Darren Hart8dac4562009-04-03 13:40:12 -07001179 }
1180
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001181 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001182
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001183 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001184
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001185 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001186 set_current_state(state);
1187 }
1188
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001189 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001190 return ret;
1191}
1192
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001193static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
1194 struct rt_mutex_waiter *w)
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001195{
1196 /*
1197 * If the result is not -EDEADLOCK or the caller requested
1198 * deadlock detection, nothing to do here.
1199 */
1200 if (res != -EDEADLOCK || detect_deadlock)
1201 return;
1202
1203 /*
Ingo Molnare2db7592021-03-22 02:35:05 +01001204 * Yell loudly and stop the task right here.
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001205 */
Sebastian Andrzej Siewior6d41c672021-03-26 16:29:32 +01001206 WARN(1, "rtmutex deadlock detected\n");
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001207 while (1) {
1208 set_current_state(TASK_INTERRUPTIBLE);
1209 schedule();
1210 }
1211}
1212
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001213/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001214 * Slow path lock function:
1215 */
Peter Zijlstra2f064a52021-06-11 10:28:17 +02001216static int __sched rt_mutex_slowlock(struct rt_mutex *lock, unsigned int state,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001217 struct hrtimer_sleeper *timeout,
1218 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001219{
1220 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001221 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001222 int ret = 0;
1223
Peter Zijlstra50809352017-03-22 11:35:56 +01001224 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001225
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001226 /*
1227 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1228 * be called in early boot if the cmpxchg() fast path is disabled
1229 * (debug, no architecture support). In this case we will acquire the
1230 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1231 * enable interrupts in that early boot case. So we need to use the
1232 * irqsave/restore variants.
1233 */
1234 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001235
1236 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001237 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001238 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001239 return 0;
1240 }
1241
1242 set_current_state(state);
1243
1244 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001245 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001246 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001247
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001248 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001249
1250 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001251 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001252 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001253
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001254 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001255 __set_current_state(TASK_RUNNING);
Peter Zijlstrac28d62c2018-03-27 14:14:38 +02001256 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001257 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001258 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001259
1260 /*
1261 * try_to_take_rt_mutex() sets the waiter bit
1262 * unconditionally. We might have to fix that up.
1263 */
1264 fixup_rt_mutex_waiters(lock);
1265
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001266 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001267
1268 /* Remove pending timer: */
1269 if (unlikely(timeout))
1270 hrtimer_cancel(&timeout->timer);
1271
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001272 debug_rt_mutex_free_waiter(&waiter);
1273
1274 return ret;
1275}
1276
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001277static int __sched __rt_mutex_slowtrylock(struct rt_mutex *lock)
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001278{
1279 int ret = try_to_take_rt_mutex(lock, current, NULL);
1280
1281 /*
1282 * try_to_take_rt_mutex() sets the lock waiters bit
1283 * unconditionally. Clean this up.
1284 */
1285 fixup_rt_mutex_waiters(lock);
1286
1287 return ret;
1288}
1289
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001290/*
1291 * Slow path try-lock function:
1292 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001293static int __sched rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001294{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001295 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001296 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001297
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001298 /*
1299 * If the lock already has an owner we fail to get the lock.
1300 * This can be done without taking the @lock->wait_lock as
1301 * it is only being read, and this is a trylock anyway.
1302 */
1303 if (rt_mutex_owner(lock))
1304 return 0;
1305
1306 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001307 * The mutex has currently no owner. Lock the wait lock and try to
1308 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001309 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001310 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001311
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001312 ret = __rt_mutex_slowtrylock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001313
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001314 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001315
1316 return ret;
1317}
1318
1319/*
Thomas Gleixner70c80102021-03-26 16:29:41 +01001320 * Performs the wakeup of the top-waiter and re-enables preemption.
1321 */
1322void __sched rt_mutex_postunlock(struct wake_q_head *wake_q)
1323{
1324 wake_up_q(wake_q);
1325
Thomas Gleixner82cd5b12021-03-26 16:29:42 +01001326 /* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
Thomas Gleixner70c80102021-03-26 16:29:41 +01001327 preempt_enable();
1328}
1329
1330/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001331 * Slow path to release a rt-mutex.
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001332 *
1333 * Return whether the current task needs to call rt_mutex_postunlock().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001334 */
Thomas Gleixner70c80102021-03-26 16:29:41 +01001335static void __sched rt_mutex_slowunlock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001336{
Thomas Gleixner70c80102021-03-26 16:29:41 +01001337 DEFINE_WAKE_Q(wake_q);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001338 unsigned long flags;
1339
1340 /* irqsave required to support early boot calls */
1341 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001342
1343 debug_rt_mutex_unlock(lock);
1344
Thomas Gleixner27e35712014-06-11 18:44:04 +00001345 /*
1346 * We must be careful here if the fast path is enabled. If we
1347 * have no waiters queued we cannot set owner to NULL here
1348 * because of:
1349 *
1350 * foo->lock->owner = NULL;
1351 * rtmutex_lock(foo->lock); <- fast path
1352 * free = atomic_dec_and_test(foo->refcnt);
1353 * rtmutex_unlock(foo->lock); <- fast path
1354 * if (free)
1355 * kfree(foo);
1356 * raw_spin_unlock(foo->lock->wait_lock);
1357 *
1358 * So for the fastpath enabled kernel:
1359 *
1360 * Nothing can set the waiters bit as long as we hold
1361 * lock->wait_lock. So we do the following sequence:
1362 *
1363 * owner = rt_mutex_owner(lock);
1364 * clear_rt_mutex_waiters(lock);
1365 * raw_spin_unlock(&lock->wait_lock);
1366 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1367 * return;
1368 * goto retry;
1369 *
1370 * The fastpath disabled variant is simple as all access to
1371 * lock->owner is serialized by lock->wait_lock:
1372 *
1373 * lock->owner = NULL;
1374 * raw_spin_unlock(&lock->wait_lock);
1375 */
1376 while (!rt_mutex_has_waiters(lock)) {
1377 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001378 if (unlock_rt_mutex_safe(lock, flags) == true)
Thomas Gleixner70c80102021-03-26 16:29:41 +01001379 return;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001380 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001381 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001382 }
1383
Thomas Gleixner27e35712014-06-11 18:44:04 +00001384 /*
1385 * The wakeup next waiter path does not suffer from the above
1386 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001387 *
1388 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001389 */
Thomas Gleixner70c80102021-03-26 16:29:41 +01001390 mark_wakeup_next_waiter(&wake_q, lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001391 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001392
Thomas Gleixner70c80102021-03-26 16:29:41 +01001393 rt_mutex_postunlock(&wake_q);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001394}
1395
1396/*
1397 * debug aware fast / slowpath lock,trylock,unlock
1398 *
1399 * The atomic acquire/release ops are compiled away, when either the
1400 * architecture does not support cmpxchg or when debugging is enabled.
1401 */
Thomas Gleixner70c80102021-03-26 16:29:41 +01001402static __always_inline int __rt_mutex_lock(struct rt_mutex *lock, long state,
1403 unsigned int subclass)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001404{
Thomas Gleixner70c80102021-03-26 16:29:41 +01001405 int ret;
1406
1407 might_sleep();
1408 mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
1409
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001410 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001411 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001412
Thomas Gleixner70c80102021-03-26 16:29:41 +01001413 ret = rt_mutex_slowlock(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
1414 if (ret)
1415 mutex_release(&lock->dep_map, _RET_IP_);
1416 return ret;
Peter Rosin62cedf32018-07-20 10:39:13 +02001417}
1418
1419#ifdef CONFIG_DEBUG_LOCK_ALLOC
1420/**
1421 * rt_mutex_lock_nested - lock a rt_mutex
1422 *
1423 * @lock: the rt_mutex to be locked
1424 * @subclass: the lockdep subclass
1425 */
1426void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
1427{
Thomas Gleixner70c80102021-03-26 16:29:41 +01001428 __rt_mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass);
Peter Rosin62cedf32018-07-20 10:39:13 +02001429}
1430EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
Peter Rosin62cedf32018-07-20 10:39:13 +02001431
Steven Rostedt (VMware)84818af2018-09-10 21:46:38 -04001432#else /* !CONFIG_DEBUG_LOCK_ALLOC */
1433
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001434/**
1435 * rt_mutex_lock - lock a rt_mutex
1436 *
1437 * @lock: the rt_mutex to be locked
1438 */
1439void __sched rt_mutex_lock(struct rt_mutex *lock)
1440{
Thomas Gleixner70c80102021-03-26 16:29:41 +01001441 __rt_mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001442}
1443EXPORT_SYMBOL_GPL(rt_mutex_lock);
Peter Rosin62cedf32018-07-20 10:39:13 +02001444#endif
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001445
1446/**
1447 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1448 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001449 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001450 *
1451 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001452 * 0 on success
1453 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001454 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001455int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001456{
Thomas Gleixner70c80102021-03-26 16:29:41 +01001457 return __rt_mutex_lock(lock, TASK_INTERRUPTIBLE, 0);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001458}
1459EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1460
1461/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001462 * rt_mutex_trylock - try to lock a rt_mutex
1463 *
1464 * @lock: the rt_mutex to be locked
1465 *
Thomas Gleixner70c80102021-03-26 16:29:41 +01001466 * This function can only be called in thread context. It's safe to call it
1467 * from atomic regions, but not from hard or soft interrupt context.
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001468 *
Thomas Gleixner70c80102021-03-26 16:29:41 +01001469 * Returns:
1470 * 1 on success
1471 * 0 on contention
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001472 */
1473int __sched rt_mutex_trylock(struct rt_mutex *lock)
1474{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001475 int ret;
1476
Thomas Gleixnerc2c360e2021-03-26 16:29:43 +01001477 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001478 return 0;
1479
Thomas Gleixner70c80102021-03-26 16:29:41 +01001480 /*
1481 * No lockdep annotation required because lockdep disables the fast
1482 * path.
1483 */
1484 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1485 return 1;
1486
1487 ret = rt_mutex_slowtrylock(lock);
Peter Zijlstraf5694782016-09-19 12:15:37 +02001488 if (ret)
1489 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1490
1491 return ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001492}
1493EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1494
1495/**
1496 * rt_mutex_unlock - unlock a rt_mutex
1497 *
1498 * @lock: the rt_mutex to be unlocked
1499 */
1500void __sched rt_mutex_unlock(struct rt_mutex *lock)
1501{
Qian Cai5facae42019-09-19 12:09:40 -04001502 mutex_release(&lock->dep_map, _RET_IP_);
Thomas Gleixner70c80102021-03-26 16:29:41 +01001503 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1504 return;
1505
1506 rt_mutex_slowunlock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001507}
1508EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1509
Thomas Gleixner70c80102021-03-26 16:29:41 +01001510/*
1511 * Futex variants, must not use fastpath.
1512 */
1513int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1514{
1515 return rt_mutex_slowtrylock(lock);
1516}
1517
1518int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
1519{
1520 return __rt_mutex_slowtrylock(lock);
1521}
1522
Luis Henriques23b94b92009-04-29 21:54:51 +01001523/**
Alex Shibf594bf2020-11-13 16:58:11 +08001524 * __rt_mutex_futex_unlock - Futex variant, that since futex variants
1525 * do not use the fast-path, can be simple and will not need to retry.
1526 *
1527 * @lock: The rt_mutex to be unlocked
1528 * @wake_q: The wake queue head from which to get the next lock waiter
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001529 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001530bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001531 struct wake_q_head *wake_q)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001532{
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001533 lockdep_assert_held(&lock->wait_lock);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001534
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001535 debug_rt_mutex_unlock(lock);
1536
1537 if (!rt_mutex_has_waiters(lock)) {
1538 lock->owner = NULL;
1539 return false; /* done */
1540 }
1541
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001542 /*
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001543 * We've already deboosted, mark_wakeup_next_waiter() will
1544 * retain preempt_disabled when we drop the wait_lock, to
1545 * avoid inversion prior to the wakeup. preempt_disable()
1546 * therein pairs with rt_mutex_postunlock().
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001547 */
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001548 mark_wakeup_next_waiter(wake_q, lock);
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001549
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001550 return true; /* call postunlock() */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001551}
1552
1553void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1554{
1555 DEFINE_WAKE_Q(wake_q);
Boqun Feng6b0ef922018-03-09 14:56:28 +08001556 unsigned long flags;
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001557 bool postunlock;
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001558
Boqun Feng6b0ef922018-03-09 14:56:28 +08001559 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001560 postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
Boqun Feng6b0ef922018-03-09 14:56:28 +08001561 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001562
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001563 if (postunlock)
1564 rt_mutex_postunlock(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001565}
1566
1567/**
Alex Shibf594bf2020-11-13 16:58:11 +08001568 * __rt_mutex_init - initialize the rt_mutex
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001569 *
Alex Shibf594bf2020-11-13 16:58:11 +08001570 * @lock: The rt_mutex to be initialized
1571 * @name: The lock name used for debugging
1572 * @key: The lock class key used for debugging
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001573 *
Alex Shibf594bf2020-11-13 16:58:11 +08001574 * Initialize the rt_mutex to unlocked state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001575 *
Alex Shibf594bf2020-11-13 16:58:11 +08001576 * Initializing of a locked rt_mutex is not allowed
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001577 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001578void __sched __rt_mutex_init(struct rt_mutex *lock, const char *name,
Peter Zijlstraf5694782016-09-19 12:15:37 +02001579 struct lock_class_key *key)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001580{
Thomas Gleixnerf5a98862021-03-26 16:29:38 +01001581 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
Thomas Gleixnerb41cda02021-08-15 23:27:38 +02001582 lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001583
Thomas Gleixnerf5a98862021-03-26 16:29:38 +01001584 __rt_mutex_basic_init(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001585}
1586EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001587
1588/**
1589 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1590 * proxy owner
1591 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001592 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001593 * @proxy_owner:the task to set as owner
1594 *
1595 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001596 *
1597 * Special API call for PI-futex support. This initializes the rtmutex and
1598 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1599 * possible at this point because the pi_state which contains the rtmutex
1600 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001601 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001602void __sched rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1603 struct task_struct *proxy_owner)
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001604{
Thomas Gleixnerf5a98862021-03-26 16:29:38 +01001605 __rt_mutex_basic_init(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001606 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001607}
1608
1609/**
1610 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1611 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001612 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001613 *
1614 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001615 *
1616 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1617 * (debugging) state. Concurrent operations on this rt_mutex are not
1618 * possible because it belongs to the pi_state which is about to be freed
1619 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001620 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001621void __sched rt_mutex_proxy_unlock(struct rt_mutex *lock)
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001622{
1623 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001624 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001625}
1626
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001627/**
1628 * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1629 * @lock: the rt_mutex to take
1630 * @waiter: the pre-initialized rt_mutex_waiter
1631 * @task: the task to prepare
1632 *
1633 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1634 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1635 *
1636 * NOTE: does _NOT_ remove the @waiter on failure; must either call
1637 * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
1638 *
1639 * Returns:
1640 * 0 - task blocked on lock
1641 * 1 - acquired the lock for task, caller should wake it up
1642 * <0 - error
1643 *
1644 * Special API call for PI-futex support.
1645 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001646int __sched __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1647 struct rt_mutex_waiter *waiter,
1648 struct task_struct *task)
Peter Zijlstra56222b22017-03-22 11:36:00 +01001649{
1650 int ret;
1651
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001652 lockdep_assert_held(&lock->wait_lock);
1653
Peter Zijlstra56222b22017-03-22 11:36:00 +01001654 if (try_to_take_rt_mutex(lock, task, NULL))
1655 return 1;
1656
1657 /* We enforce deadlock detection for futexes */
1658 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1659 RT_MUTEX_FULL_CHAINWALK);
1660
1661 if (ret && !rt_mutex_owner(lock)) {
1662 /*
1663 * Reset the return value. We might have
1664 * returned with -EDEADLK and the owner
1665 * released the lock while we were walking the
1666 * pi chain. Let the waiter sort it out.
1667 */
1668 ret = 0;
1669 }
1670
Peter Zijlstra56222b22017-03-22 11:36:00 +01001671 return ret;
1672}
1673
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001674/**
Darren Hart8dac4562009-04-03 13:40:12 -07001675 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1676 * @lock: the rt_mutex to take
1677 * @waiter: the pre-initialized rt_mutex_waiter
1678 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001679 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001680 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1681 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1682 *
1683 * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
1684 * on failure.
1685 *
Darren Hart8dac4562009-04-03 13:40:12 -07001686 * Returns:
1687 * 0 - task blocked on lock
1688 * 1 - acquired the lock for task, caller should wake it up
1689 * <0 - error
1690 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001691 * Special API call for PI-futex support.
Darren Hart8dac4562009-04-03 13:40:12 -07001692 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001693int __sched rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1694 struct rt_mutex_waiter *waiter,
1695 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001696{
1697 int ret;
1698
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001699 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstra56222b22017-03-22 11:36:00 +01001700 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001701 if (unlikely(ret))
1702 remove_waiter(lock, waiter);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001703 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001704
Darren Hart8dac4562009-04-03 13:40:12 -07001705 return ret;
1706}
1707
1708/**
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001709 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
Darren Hart8dac4562009-04-03 13:40:12 -07001710 * @lock: the rt_mutex we were woken on
1711 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001712 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001713 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001714 *
Randy Dunlapc034f482021-02-25 17:21:10 -08001715 * Wait for the lock acquisition started on our behalf by
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001716 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1717 * rt_mutex_cleanup_proxy_lock().
Darren Hart8dac4562009-04-03 13:40:12 -07001718 *
1719 * Returns:
1720 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001721 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001722 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001723 * Special API call for PI-futex support
Darren Hart8dac4562009-04-03 13:40:12 -07001724 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001725int __sched rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
1726 struct hrtimer_sleeper *to,
1727 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001728{
1729 int ret;
1730
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001731 raw_spin_lock_irq(&lock->wait_lock);
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001732 /* sleep on the mutex */
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001733 set_current_state(TASK_INTERRUPTIBLE);
Lai Jiangshan81612392011-01-14 17:09:41 +08001734 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001735 /*
1736 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1737 * have to fix that up.
1738 */
1739 fixup_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001740 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001741
Darren Hart8dac4562009-04-03 13:40:12 -07001742 return ret;
1743}
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001744
1745/**
1746 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1747 * @lock: the rt_mutex we were woken on
1748 * @waiter: the pre-initialized rt_mutex_waiter
1749 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001750 * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
1751 * rt_mutex_wait_proxy_lock().
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001752 *
1753 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1754 * in fact still be granted ownership until we're removed. Therefore we can
1755 * find we are in fact the owner and must disregard the
1756 * rt_mutex_wait_proxy_lock() failure.
1757 *
1758 * Returns:
1759 * true - did the cleanup, we done.
1760 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1761 * caller should disregards its return value.
1762 *
1763 * Special API call for PI-futex support
1764 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001765bool __sched rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1766 struct rt_mutex_waiter *waiter)
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001767{
1768 bool cleanup = false;
1769
1770 raw_spin_lock_irq(&lock->wait_lock);
1771 /*
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001772 * Do an unconditional try-lock, this deals with the lock stealing
1773 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
1774 * sets a NULL owner.
1775 *
1776 * We're not interested in the return value, because the subsequent
1777 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
1778 * we will own the lock and it will have removed the waiter. If we
1779 * failed the trylock, we're still not owner and we need to remove
1780 * ourselves.
1781 */
1782 try_to_take_rt_mutex(lock, current, waiter);
1783 /*
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001784 * Unless we're the owner; we're still enqueued on the wait_list.
1785 * So check if we became owner, if not, take us off the wait_list.
1786 */
1787 if (rt_mutex_owner(lock) != current) {
1788 remove_waiter(lock, waiter);
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001789 cleanup = true;
1790 }
Peter Zijlstracfafcd12017-03-22 11:35:58 +01001791 /*
1792 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1793 * have to fix that up.
1794 */
1795 fixup_rt_mutex_waiters(lock);
1796
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001797 raw_spin_unlock_irq(&lock->wait_lock);
1798
1799 return cleanup;
1800}
Thomas Gleixnerfae37fe2021-03-26 16:29:35 +01001801
1802#ifdef CONFIG_DEBUG_RT_MUTEXES
1803void rt_mutex_debug_task_free(struct task_struct *task)
1804{
1805 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
1806 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
1807}
1808#endif