blob: 949781aa54b1137e561936fc9ed0087aa8343b6f [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 */
Thomas Gleixner531ae4b2021-08-15 23:27:57 +020014#include <linux/sched.h>
15#include <linux/sched/debug.h>
16#include <linux/sched/deadline.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060018#include <linux/sched/rt.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010019#include <linux/sched/wake_q.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070020
21#include "rtmutex_common.h"
22
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070023/*
24 * lock->owner state tracking:
25 *
Lai Jiangshan81612392011-01-14 17:09:41 +080026 * lock->owner holds the task_struct pointer of the owner. Bit 0
27 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070028 *
Lai Jiangshan81612392011-01-14 17:09:41 +080029 * owner bit0
30 * NULL 0 lock is free (fast acquire possible)
31 * NULL 1 lock is free and has waiters and the top waiter
32 * is going to take the lock*
33 * taskpointer 0 lock is held (fast release possible)
34 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070035 *
36 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080037 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070038 *
Lai Jiangshan81612392011-01-14 17:09:41 +080039 * (*) It also can be a transitional state when grabbing the lock
40 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
41 * we need to set the bit0 before looking at the lock, and the owner may be
42 * NULL in this small time, hence this can be a transitional state.
43 *
44 * (**) There is a small time when bit 0 is set but there are no
45 * waiters. This can happen when grabbing the lock in the slow path.
46 * To prevent a cmpxchg of the owner releasing the lock, we need to
47 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070048 */
49
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +010050static __always_inline void
Peter Zijlstra830e6ac2021-08-15 23:27:58 +020051rt_mutex_set_owner(struct rt_mutex_base *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070052{
Lai Jiangshan81612392011-01-14 17:09:41 +080053 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070054
55 if (rt_mutex_has_waiters(lock))
56 val |= RT_MUTEX_HAS_WAITERS;
57
Paul E. McKenney0050c7b2020-01-03 15:59:12 -080058 WRITE_ONCE(lock->owner, (struct task_struct *)val);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070059}
60
Peter Zijlstra830e6ac2021-08-15 23:27:58 +020061static __always_inline void clear_rt_mutex_waiters(struct rt_mutex_base *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070062{
63 lock->owner = (struct task_struct *)
64 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
65}
66
Peter Zijlstra830e6ac2021-08-15 23:27:58 +020067static __always_inline void fixup_rt_mutex_waiters(struct rt_mutex_base *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070068{
Thomas Gleixnerdbb26052016-11-30 21:04:41 +000069 unsigned long owner, *p = (unsigned long *) &lock->owner;
70
71 if (rt_mutex_has_waiters(lock))
72 return;
73
74 /*
75 * The rbtree has no waiters enqueued, now make sure that the
76 * lock->owner still has the waiters bit set, otherwise the
77 * following can happen:
78 *
79 * CPU 0 CPU 1 CPU2
80 * l->owner=T1
81 * rt_mutex_lock(l)
82 * lock(l->lock)
83 * l->owner = T1 | HAS_WAITERS;
84 * enqueue(T2)
85 * boost()
86 * unlock(l->lock)
87 * block()
88 *
89 * rt_mutex_lock(l)
90 * lock(l->lock)
91 * l->owner = T1 | HAS_WAITERS;
92 * enqueue(T3)
93 * boost()
94 * unlock(l->lock)
95 * block()
96 * signal(->T2) signal(->T3)
97 * lock(l->lock)
98 * dequeue(T2)
99 * deboost()
100 * unlock(l->lock)
101 * lock(l->lock)
102 * dequeue(T3)
103 * ==> wait list is empty
104 * deboost()
105 * unlock(l->lock)
106 * lock(l->lock)
107 * fixup_rt_mutex_waiters()
108 * if (wait_list_empty(l) {
109 * l->owner = owner
110 * owner = l->owner & ~HAS_WAITERS;
111 * ==> l->owner = T1
112 * }
113 * lock(l->lock)
114 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
115 * if (wait_list_empty(l) {
116 * owner = l->owner & ~HAS_WAITERS;
117 * cmpxchg(l->owner, T1, NULL)
118 * ===> Success (l->owner = NULL)
119 *
120 * l->owner = owner
121 * ==> l->owner = T1
122 * }
123 *
124 * With the check for the waiter bit in place T3 on CPU2 will not
125 * overwrite. All tasks fiddling with the waiters bit are
126 * serialized by l->lock, so nothing else can modify the waiters
127 * bit. If the bit is set then nothing can change l->owner either
128 * so the simple RMW is safe. The cmpxchg() will simply fail if it
129 * happens in the middle of the RMW because the waiters bit is
130 * still set.
131 */
132 owner = READ_ONCE(*p);
133 if (owner & RT_MUTEX_HAS_WAITERS)
134 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700135}
136
137/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100138 * We can speed up the acquire/release, if there's no debugging state to be
139 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200140 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100141#ifndef CONFIG_DEBUG_RT_MUTEXES
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200142static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200143 struct task_struct *old,
144 struct task_struct *new)
145{
Thomas Gleixner709e0b62021-08-15 23:27:55 +0200146 return try_cmpxchg_acquire(&lock->owner, &old, new);
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200147}
148
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200149static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200150 struct task_struct *old,
151 struct task_struct *new)
152{
Thomas Gleixner709e0b62021-08-15 23:27:55 +0200153 return try_cmpxchg_release(&lock->owner, &old, new);
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200154}
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700155
156/*
157 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
158 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
159 * relaxed semantics suffice.
160 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200161static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200162{
163 unsigned long owner, *p = (unsigned long *) &lock->owner;
164
165 do {
166 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700167 } while (cmpxchg_relaxed(p, owner,
168 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200169}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000170
171/*
172 * Safe fastpath aware unlock:
173 * 1) Clear the waiters bit
174 * 2) Drop lock->wait_lock
175 * 3) Try to unlock the lock with cmpxchg
176 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200177static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100178 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000179 __releases(lock->wait_lock)
180{
181 struct task_struct *owner = rt_mutex_owner(lock);
182
183 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100184 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000185 /*
186 * If a new waiter comes in between the unlock and the cmpxchg
187 * we have two situations:
188 *
189 * unlock(wait_lock);
190 * lock(wait_lock);
191 * cmpxchg(p, owner, 0) == owner
192 * mark_rt_mutex_waiters(lock);
193 * acquire(lock);
194 * or:
195 *
196 * unlock(wait_lock);
197 * lock(wait_lock);
198 * mark_rt_mutex_waiters(lock);
199 *
200 * cmpxchg(p, owner, 0) != owner
201 * enqueue_waiter();
202 * unlock(wait_lock);
203 * lock(wait_lock);
204 * wake waiter();
205 * unlock(wait_lock);
206 * lock(wait_lock);
207 * acquire(lock);
208 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700209 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000210}
211
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200212#else
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200213static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200214 struct task_struct *old,
215 struct task_struct *new)
216{
217 return false;
218
219}
220
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200221static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
Sebastian Andrzej Siewior78515932021-08-15 23:27:54 +0200222 struct task_struct *old,
223 struct task_struct *new)
224{
225 return false;
226}
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700227
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200228static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200229{
230 lock->owner = (struct task_struct *)
231 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
232}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000233
234/*
235 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
236 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200237static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100238 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000239 __releases(lock->wait_lock)
240{
241 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100242 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000243 return true;
244}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200245#endif
246
Peter Zijlstra19830e52017-03-23 15:56:14 +0100247/*
248 * Only use with rt_mutex_waiter_{less,equal}()
249 */
250#define task_to_waiter(p) \
251 &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
252
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100253static __always_inline int rt_mutex_waiter_less(struct rt_mutex_waiter *left,
254 struct rt_mutex_waiter *right)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100255{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100256 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100257 return 1;
258
259 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100260 * If both waiters have dl_prio(), we check the deadlines of the
261 * associated tasks.
262 * If left waiter has a dl_prio(), and we didn't return 1 above,
263 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100264 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100265 if (dl_prio(left->prio))
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100266 return dl_time_before(left->deadline, right->deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100267
268 return 0;
269}
270
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100271static __always_inline int rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
272 struct rt_mutex_waiter *right)
Peter Zijlstra19830e52017-03-23 15:56:14 +0100273{
274 if (left->prio != right->prio)
275 return 0;
276
277 /*
278 * If both waiters have dl_prio(), we check the deadlines of the
279 * associated tasks.
280 * If left waiter has a dl_prio(), and we didn't return 0 above,
281 * then right waiter has a dl_prio() too.
282 */
283 if (dl_prio(left->prio))
284 return left->deadline == right->deadline;
285
286 return 1;
287}
288
Peter Zijlstra5a798722020-04-29 17:29:58 +0200289#define __node_2_waiter(node) \
290 rb_entry((node), struct rt_mutex_waiter, tree_entry)
291
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100292static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
Peter Zijlstra5a798722020-04-29 17:29:58 +0200293{
294 return rt_mutex_waiter_less(__node_2_waiter(a), __node_2_waiter(b));
295}
296
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100297static __always_inline void
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200298rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100299{
Peter Zijlstra5a798722020-04-29 17:29:58 +0200300 rb_add_cached(&waiter->tree_entry, &lock->waiters, __waiter_less);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100301}
302
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100303static __always_inline void
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200304rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100305{
306 if (RB_EMPTY_NODE(&waiter->tree_entry))
307 return;
308
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700309 rb_erase_cached(&waiter->tree_entry, &lock->waiters);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100310 RB_CLEAR_NODE(&waiter->tree_entry);
311}
312
Peter Zijlstra5a798722020-04-29 17:29:58 +0200313#define __node_2_pi_waiter(node) \
314 rb_entry((node), struct rt_mutex_waiter, pi_tree_entry)
315
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100316static __always_inline bool
317__pi_waiter_less(struct rb_node *a, const struct rb_node *b)
Peter Zijlstra5a798722020-04-29 17:29:58 +0200318{
319 return rt_mutex_waiter_less(__node_2_pi_waiter(a), __node_2_pi_waiter(b));
320}
321
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100322static __always_inline void
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100323rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
324{
Peter Zijlstra5a798722020-04-29 17:29:58 +0200325 rb_add_cached(&waiter->pi_tree_entry, &task->pi_waiters, __pi_waiter_less);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100326}
327
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100328static __always_inline void
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100329rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
330{
331 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
332 return;
333
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700334 rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100335 RB_CLEAR_NODE(&waiter->pi_tree_entry);
336}
337
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100338static __always_inline void rt_mutex_adjust_prio(struct task_struct *p)
Xunlei Pange96a77052017-03-23 15:56:08 +0100339{
Peter Zijlstraacd58622017-03-23 15:56:11 +0100340 struct task_struct *pi_task = NULL;
Xunlei Pange96a77052017-03-23 15:56:08 +0100341
Peter Zijlstraacd58622017-03-23 15:56:11 +0100342 lockdep_assert_held(&p->pi_lock);
Xunlei Pange96a77052017-03-23 15:56:08 +0100343
Peter Zijlstraacd58622017-03-23 15:56:11 +0100344 if (task_has_pi_waiters(p))
345 pi_task = task_top_pi_waiter(p)->task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700346
Peter Zijlstraacd58622017-03-23 15:56:11 +0100347 rt_mutex_setprio(p, pi_task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700348}
349
Thomas Gleixnerb576e642021-08-15 23:28:08 +0200350/* RT mutex specific wake_q wrappers */
351static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh,
352 struct rt_mutex_waiter *w)
353{
Thomas Gleixner456cfbc2021-08-15 23:28:11 +0200354 if (IS_ENABLED(CONFIG_PREEMPT_RT) && w->wake_state != TASK_NORMAL) {
355 if (IS_ENABLED(CONFIG_PROVE_LOCKING))
356 WARN_ON_ONCE(wqh->rtlock_task);
357 get_task_struct(w->task);
358 wqh->rtlock_task = w->task;
359 } else {
360 wake_q_add(&wqh->head, w->task);
361 }
Thomas Gleixnerb576e642021-08-15 23:28:08 +0200362}
363
364static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
365{
Thomas Gleixner456cfbc2021-08-15 23:28:11 +0200366 if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) {
367 wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT);
368 put_task_struct(wqh->rtlock_task);
369 wqh->rtlock_task = NULL;
370 }
371
372 if (!wake_q_empty(&wqh->head))
373 wake_up_q(&wqh->head);
Thomas Gleixnerb576e642021-08-15 23:28:08 +0200374
375 /* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
376 preempt_enable();
377}
378
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700379/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000380 * Deadlock detection is conditional:
381 *
382 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
383 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
384 *
385 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
386 * conducted independent of the detect argument.
387 *
388 * If the waiter argument is NULL this indicates the deboost path and
389 * deadlock detection is disabled independent of the detect argument
390 * and the config settings.
391 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100392static __always_inline bool
393rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
394 enum rtmutex_chainwalk chwalk)
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000395{
Zhen Lei07d25972021-07-31 20:30:11 +0800396 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
Thomas Gleixnerf7efc472021-03-26 16:29:36 +0100397 return waiter != NULL;
398 return chwalk == RT_MUTEX_FULL_CHAINWALK;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000399}
400
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200401static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p)
Thomas Gleixner82084982014-06-05 11:16:12 +0200402{
403 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
404}
405
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700406/*
407 * Adjust the priority chain. Also used for deadlock detection.
408 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200409 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200410 * @task: the task owning the mutex (owner) for which a chain walk is
411 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900412 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200413 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
414 * things for a task that has just got its priority adjusted, and
415 * is waiting on a mutex)
416 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
417 * we dropped its pi_lock. Is never dereferenced, only used for
418 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200419 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200420 * its priority to the mutex owner (can be NULL in the case
421 * depicted above or if the top waiter is gone away and we are
422 * actually deboosting the owner)
423 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200424 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700425 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200426 *
427 * Chain walk basics and protection scope
428 *
429 * [R] refcount on task
430 * [P] task->pi_lock held
431 * [L] rtmutex->wait_lock held
432 *
433 * Step Description Protected by
434 * function arguments:
435 * @task [R]
436 * @orig_lock if != NULL @top_task is blocked on it
437 * @next_lock Unprotected. Cannot be
438 * dereferenced. Only used for
439 * comparison.
440 * @orig_waiter if != NULL @top_task is blocked on it
441 * @top_task current, or in case of proxy
442 * locking protected by calling
443 * code
444 * again:
445 * loop_sanity_check();
446 * retry:
447 * [1] lock(task->pi_lock); [R] acquire [P]
448 * [2] waiter = task->pi_blocked_on; [P]
449 * [3] check_exit_conditions_1(); [P]
450 * [4] lock = waiter->lock; [P]
451 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
452 * unlock(task->pi_lock); release [P]
453 * goto retry;
454 * }
455 * [6] check_exit_conditions_2(); [P] + [L]
456 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
457 * [8] unlock(task->pi_lock); release [P]
458 * put_task_struct(task); release [R]
459 * [9] check_exit_conditions_3(); [L]
460 * [10] task = owner(lock); [L]
461 * get_task_struct(task); [L] acquire [R]
462 * lock(task->pi_lock); [L] acquire [P]
463 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
464 * [12] check_exit_conditions_4(); [P] + [L]
465 * [13] unlock(task->pi_lock); release [P]
466 * unlock(lock->wait_lock); release [L]
467 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700468 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100469static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
470 enum rtmutex_chainwalk chwalk,
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200471 struct rt_mutex_base *orig_lock,
472 struct rt_mutex_base *next_lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100473 struct rt_mutex_waiter *orig_waiter,
474 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700475{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700476 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000477 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000478 int ret = 0, depth = 0;
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200479 struct rt_mutex_base *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000480 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000481 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700482
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000483 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700484
485 /*
486 * The (de)boosting is a step by step approach with a lot of
487 * pitfalls. We want this to be preemptible and we want hold a
488 * maximum of two locks per step. So we have to check
489 * carefully whether things change under us.
490 */
491 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200492 /*
493 * We limit the lock chain length for each invocation.
494 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700495 if (++depth > max_lock_depth) {
496 static int prev_max;
497
498 /*
499 * Print this only once. If the admin changes the limit,
500 * print a new message when reaching the limit again.
501 */
502 if (prev_max != max_lock_depth) {
503 prev_max = max_lock_depth;
504 printk(KERN_WARNING "Maximum lock depth %d reached "
505 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700506 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700507 }
508 put_task_struct(task);
509
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200510 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700511 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200512
513 /*
514 * We are fully preemptible here and only hold the refcount on
515 * @task. So everything can have changed under us since the
516 * caller or our own code below (goto retry/again) dropped all
517 * locks.
518 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700519 retry:
520 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200521 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700522 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100523 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700524
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200525 /*
526 * [2] Get the waiter on which @task is blocked on.
527 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700528 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200529
530 /*
531 * [3] check_exit_conditions_1() protected by task->pi_lock.
532 */
533
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700534 /*
535 * Check whether the end of the boosting chain has been
536 * reached or the state of the chain has changed while we
537 * dropped the locks.
538 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800539 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700540 goto out_unlock_pi;
541
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700542 /*
543 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800544 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700545 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800546 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700547 goto out_unlock_pi;
548
549 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200550 * We dropped all locks after taking a refcount on @task, so
551 * the task might have moved on in the lock chain or even left
552 * the chain completely and blocks now on an unrelated lock or
553 * on @orig_lock.
554 *
555 * We stored the lock on which @task was blocked in @next_lock,
556 * so we can detect the chain change.
557 */
558 if (next_lock != waiter->lock)
559 goto out_unlock_pi;
560
561 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700562 * Drop out, when the task has no waiters. Note,
563 * top_waiter can be NULL, when we are in the deboosting
564 * mode!
565 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000566 if (top_waiter) {
567 if (!task_has_pi_waiters(task))
568 goto out_unlock_pi;
569 /*
570 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000571 * are not the top pi waiter of the task. If deadlock
572 * detection is enabled we continue, but stop the
573 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000574 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000575 if (top_waiter != task_top_pi_waiter(task)) {
576 if (!detect_deadlock)
577 goto out_unlock_pi;
578 else
579 requeue = false;
580 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000581 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700582
583 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000584 * If the waiter priority is the same as the task priority
585 * then there is no further priority adjustment necessary. If
586 * deadlock detection is off, we stop the chain walk. If its
587 * enabled we continue, but stop the requeueing in the chain
588 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700589 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100590 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000591 if (!detect_deadlock)
592 goto out_unlock_pi;
593 else
594 requeue = false;
595 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700596
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200597 /*
598 * [4] Get the next lock
599 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700600 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200601 /*
602 * [5] We need to trylock here as we are holding task->pi_lock,
603 * which is the reverse lock order versus the other rtmutex
604 * operations.
605 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100606 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100607 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700608 cpu_relax();
609 goto retry;
610 }
611
Thomas Gleixner397335f2014-05-22 03:25:39 +0000612 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200613 * [6] check_exit_conditions_2() protected by task->pi_lock and
614 * lock->wait_lock.
615 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000616 * Deadlock detection. If the lock is the same as the original
617 * lock which caused us to walk the lock chain or if the
618 * current lock is owned by the task which initiated the chain
619 * walk, we detected a deadlock.
620 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700621 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100622 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200623 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700624 goto out_unlock_pi;
625 }
626
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000627 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000628 * If we just follow the lock chain for deadlock detection, no
629 * need to do all the requeue operations. To avoid a truckload
630 * of conditionals around the various places below, just do the
631 * minimum chain walk checks.
632 */
633 if (!requeue) {
634 /*
635 * No requeue[7] here. Just release @task [8]
636 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100637 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000638 put_task_struct(task);
639
640 /*
641 * [9] check_exit_conditions_3 protected by lock->wait_lock.
642 * If there is no owner of the lock, end of chain.
643 */
644 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100645 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000646 return 0;
647 }
648
649 /* [10] Grab the next task, i.e. owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700650 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100651 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000652
653 /*
654 * No requeue [11] here. We just do deadlock detection.
655 *
656 * [12] Store whether owner is blocked
657 * itself. Decision is made after dropping the locks
658 */
659 next_lock = task_blocked_on_lock(task);
660 /*
661 * Get the top waiter for the next iteration
662 */
663 top_waiter = rt_mutex_top_waiter(lock);
664
665 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100666 raw_spin_unlock(&task->pi_lock);
667 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000668
669 /* If owner is not blocked, end of chain. */
670 if (!next_lock)
671 goto out_put_task;
672 goto again;
673 }
674
675 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000676 * Store the current top waiter before doing the requeue
677 * operation on @lock. We need it for the boost/deboost
678 * decision below.
679 */
680 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700681
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700682 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100683 rt_mutex_dequeue(lock, waiter);
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100684
685 /*
686 * Update the waiter prio fields now that we're dequeued.
687 *
688 * These values can have changed through either:
689 *
690 * sys_sched_set_scheduler() / sys_sched_setattr()
691 *
692 * or
693 *
694 * DL CBS enforcement advancing the effective deadline.
695 *
696 * Even though pi_waiters also uses these fields, and that tree is only
697 * updated in [11], we can do this here, since we hold [L], which
698 * serializes all pi_waiters access and rb_erase() does not care about
699 * the values of the node being removed.
700 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100701 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100702 waiter->deadline = task->dl.deadline;
703
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100704 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700705
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200706 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100707 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200708 put_task_struct(task);
709
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000710 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200711 * [9] check_exit_conditions_3 protected by lock->wait_lock.
712 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000713 * We must abort the chain walk if there is no lock owner even
714 * in the dead lock detection case, as we have nothing to
715 * follow here. This is the end of the chain we are walking.
716 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800717 if (!rt_mutex_owner(lock)) {
718 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200719 * If the requeue [7] above changed the top waiter,
720 * then we need to wake the new top waiter up to try
721 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800722 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000723 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Thomas Gleixnerc014ef62021-08-15 23:28:06 +0200724 wake_up_state(waiter->task, waiter->wake_state);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100725 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200726 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800727 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700728
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200729 /* [10] Grab the next task, i.e. the owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700730 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100731 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700732
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200733 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700734 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000735 /*
736 * The waiter became the new top (highest priority)
737 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700738 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000739 * and adjust the priority of the owner.
740 */
741 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100742 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100743 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700744
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000745 } else if (prerequeue_top_waiter == waiter) {
746 /*
747 * The waiter was the top waiter on the lock, but is
Ingo Molnare2db7592021-03-22 02:35:05 +0100748 * no longer the top priority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700749 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000750 * (highest priority) waiter and adjust the priority
751 * of the owner.
752 * The new top waiter is stored in @waiter so that
753 * @waiter == @top_waiter evaluates to true below and
754 * we continue to deboost the rest of the chain.
755 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100756 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700757 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100758 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100759 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000760 } else {
761 /*
762 * Nothing changed. No need to do any priority
763 * adjustment.
764 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700765 }
766
Thomas Gleixner82084982014-06-05 11:16:12 +0200767 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200768 * [12] check_exit_conditions_4() protected by task->pi_lock
769 * and lock->wait_lock. The actual decisions are made after we
770 * dropped the locks.
771 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200772 * Check whether the task which owns the current lock is pi
773 * blocked itself. If yes we store a pointer to the lock for
774 * the lock chain change detection above. After we dropped
775 * task->pi_lock next_lock cannot be dereferenced anymore.
776 */
777 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000778 /*
779 * Store the top waiter of @lock for the end of chain walk
780 * decision below.
781 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700782 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200783
784 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100785 raw_spin_unlock(&task->pi_lock);
786 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700787
Thomas Gleixner82084982014-06-05 11:16:12 +0200788 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200789 * Make the actual exit decisions [12], based on the stored
790 * values.
791 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200792 * We reached the end of the lock chain. Stop right here. No
793 * point to go back just to figure that out.
794 */
795 if (!next_lock)
796 goto out_put_task;
797
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000798 /*
799 * If the current waiter is not the top waiter on the lock,
800 * then we can stop the chain walk here if we are not in full
801 * deadlock detection mode.
802 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700803 if (!detect_deadlock && waiter != top_waiter)
804 goto out_put_task;
805
806 goto again;
807
808 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100809 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700810 out_put_task:
811 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700812
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700813 return ret;
814}
815
816/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700817 * Try to take an rt-mutex
818 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100819 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800820 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200821 * @lock: The lock to be acquired.
822 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700823 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200824 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700825 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100826static int __sched
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200827try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100828 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700829{
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100830 lockdep_assert_held(&lock->wait_lock);
831
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700832 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200833 * Before testing whether we can acquire @lock, we set the
834 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
835 * other tasks which try to modify @lock into the slow path
836 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700837 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200838 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
839 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700840 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200841 * - There is a lock owner. The caller must fixup the
842 * transient state if it does a trylock or leaves the lock
843 * function due to a signal or timeout.
844 *
845 * - @task acquires the lock and there are no other
846 * waiters. This is undone in rt_mutex_set_owner(@task) at
847 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700848 */
849 mark_rt_mutex_waiters(lock);
850
Thomas Gleixner358c3312014-06-11 01:01:13 +0200851 /*
852 * If @lock has an owner, give up.
853 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800854 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700855 return 0;
856
Lai Jiangshan81612392011-01-14 17:09:41 +0800857 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200858 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700859 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200860 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800861 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200862 if (waiter) {
863 /*
864 * If waiter is not the highest priority waiter of
865 * @lock, give up.
866 */
867 if (waiter != rt_mutex_top_waiter(lock))
868 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800869
870 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200871 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700872 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200873 */
874 rt_mutex_dequeue(lock, waiter);
875
876 } else {
877 /*
878 * If the lock has waiters already we check whether @task is
879 * eligible to take over the lock.
880 *
881 * If there are no other waiters, @task can acquire
882 * the lock. @task->pi_blocked_on is NULL, so it does
883 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800884 */
885 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200886 /*
887 * If @task->prio is greater than or equal to
888 * the top waiter priority (kernel view),
889 * @task lost.
890 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100891 if (!rt_mutex_waiter_less(task_to_waiter(task),
892 rt_mutex_top_waiter(lock)))
Thomas Gleixner358c3312014-06-11 01:01:13 +0200893 return 0;
894
895 /*
896 * The current top waiter stays enqueued. We
897 * don't have to change anything in the lock
898 * waiters order.
899 */
900 } else {
901 /*
902 * No waiters. Take the lock without the
903 * pi_lock dance.@task->pi_blocked_on is NULL
904 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700905 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200906 */
907 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800908 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800909 }
910
Thomas Gleixner358c3312014-06-11 01:01:13 +0200911 /*
912 * Clear @task->pi_blocked_on. Requires protection by
913 * @task->pi_lock. Redundant operation for the @waiter == NULL
914 * case, but conditionals are more expensive than a redundant
915 * store.
916 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100917 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200918 task->pi_blocked_on = NULL;
919 /*
920 * Finish the lock acquisition. @task is the new owner. If
921 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700922 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200923 */
924 if (rt_mutex_has_waiters(lock))
925 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100926 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200927
928takeit:
Thomas Gleixner358c3312014-06-11 01:01:13 +0200929 /*
930 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
931 * are still waiters or clears it.
932 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800933 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700934
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700935 return 1;
936}
937
938/*
939 * Task blocks on lock.
940 *
941 * Prepare waiter and propagate pi chain
942 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100943 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700944 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200945static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100946 struct rt_mutex_waiter *waiter,
947 struct task_struct *task,
948 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700949{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700950 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700951 struct rt_mutex_waiter *top_waiter = waiter;
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200952 struct rt_mutex_base *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700953 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700954
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100955 lockdep_assert_held(&lock->wait_lock);
956
Thomas Gleixner397335f2014-05-22 03:25:39 +0000957 /*
958 * Early deadlock detection. We really don't want the task to
959 * enqueue on itself just to untangle the mess later. It's not
960 * only an optimization. We drop the locks, so another waiter
961 * can come in before the chain walk detects the deadlock. So
962 * the other will detect the deadlock and return -EDEADLOCK,
963 * which is wrong, as the other waiter is not in a deadlock
964 * situation.
965 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200966 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000967 return -EDEADLK;
968
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100969 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700970 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700971 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100972 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100973 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700974
975 /* Get the top priority waiter on the lock */
976 if (rt_mutex_has_waiters(lock))
977 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100978 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700979
Darren Hart8dac4562009-04-03 13:40:12 -0700980 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700981
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100982 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700983
Lai Jiangshan81612392011-01-14 17:09:41 +0800984 if (!owner)
985 return 0;
986
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100987 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700988 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100989 rt_mutex_dequeue_pi(owner, top_waiter);
990 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700991
Peter Zijlstraacd58622017-03-23 15:56:11 +0100992 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700993 if (owner->pi_blocked_on)
994 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000995 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700996 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200997 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700998
Thomas Gleixner82084982014-06-05 11:16:12 +0200999 /* Store the lock on which owner is blocked or NULL */
1000 next_lock = task_blocked_on_lock(owner);
1001
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001002 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +02001003 /*
1004 * Even if full deadlock detection is on, if the owner is not
1005 * blocked itself, we can avoid finding this out in the chain
1006 * walk.
1007 */
1008 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001009 return 0;
1010
Steven Rostedtdb630632006-09-29 01:59:44 -07001011 /*
1012 * The owner can't disappear while holding a lock,
1013 * so the owner struct is protected by wait_lock.
1014 * Gets dropped in rt_mutex_adjust_prio_chain()!
1015 */
1016 get_task_struct(owner);
1017
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001018 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001019
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001020 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +02001021 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001022
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001023 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001024
1025 return res;
1026}
1027
1028/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001029 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001030 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001031 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001032 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001033 */
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001034static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh,
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001035 struct rt_mutex_base *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001036{
1037 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001038
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001039 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001040
1041 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001042
1043 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +01001044 * Remove it from current->pi_waiters and deboost.
1045 *
1046 * We must in fact deboost here in order to ensure we call
1047 * rt_mutex_setprio() to update p->pi_top_task before the
1048 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001049 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001050 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001051 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001052
Thomas Gleixner27e35712014-06-11 18:44:04 +00001053 /*
1054 * As we are waking up the top waiter, and the waiter stays
1055 * queued on the lock until it gets the lock, this lock
1056 * obviously has waiters. Just set the bit here and this has
1057 * the added benefit of forcing all new tasks into the
1058 * slow path making sure no task of lower priority than
1059 * the top waiter can steal this lock.
1060 */
1061 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001062
Peter Zijlstraacd58622017-03-23 15:56:11 +01001063 /*
1064 * We deboosted before waking the top waiter task such that we don't
1065 * run two tasks with the 'same' priority (and ensure the
1066 * p->pi_top_task pointer points to a blocked task). This however can
1067 * lead to priority inversion if we would get preempted after the
1068 * deboost but before waking our donor task, hence the preempt_disable()
1069 * before unlock.
1070 *
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001071 * Pairs with preempt_enable() in rt_mutex_wake_up_q();
Peter Zijlstraacd58622017-03-23 15:56:11 +01001072 */
1073 preempt_disable();
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001074 rt_mutex_wake_q_add(wqh, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001075 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001076}
1077
Thomas Gleixnere17ba59b2021-08-15 23:28:12 +02001078static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1079{
1080 int ret = try_to_take_rt_mutex(lock, current, NULL);
1081
1082 /*
1083 * try_to_take_rt_mutex() sets the lock waiters bit
1084 * unconditionally. Clean this up.
1085 */
1086 fixup_rt_mutex_waiters(lock);
1087
1088 return ret;
1089}
1090
1091/*
1092 * Slow path try-lock function:
1093 */
1094static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1095{
1096 unsigned long flags;
1097 int ret;
1098
1099 /*
1100 * If the lock already has an owner we fail to get the lock.
1101 * This can be done without taking the @lock->wait_lock as
1102 * it is only being read, and this is a trylock anyway.
1103 */
1104 if (rt_mutex_owner(lock))
1105 return 0;
1106
1107 /*
1108 * The mutex has currently no owner. Lock the wait lock and try to
1109 * acquire the lock. We use irqsave here to support early boot calls.
1110 */
1111 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1112
1113 ret = __rt_mutex_slowtrylock(lock);
1114
1115 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1116
1117 return ret;
1118}
1119
1120static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock)
1121{
1122 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1123 return 1;
1124
1125 return rt_mutex_slowtrylock(lock);
1126}
1127
1128/*
1129 * Slow path to release a rt-mutex.
1130 */
1131static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
1132{
1133 DEFINE_RT_WAKE_Q(wqh);
1134 unsigned long flags;
1135
1136 /* irqsave required to support early boot calls */
1137 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1138
1139 debug_rt_mutex_unlock(lock);
1140
1141 /*
1142 * We must be careful here if the fast path is enabled. If we
1143 * have no waiters queued we cannot set owner to NULL here
1144 * because of:
1145 *
1146 * foo->lock->owner = NULL;
1147 * rtmutex_lock(foo->lock); <- fast path
1148 * free = atomic_dec_and_test(foo->refcnt);
1149 * rtmutex_unlock(foo->lock); <- fast path
1150 * if (free)
1151 * kfree(foo);
1152 * raw_spin_unlock(foo->lock->wait_lock);
1153 *
1154 * So for the fastpath enabled kernel:
1155 *
1156 * Nothing can set the waiters bit as long as we hold
1157 * lock->wait_lock. So we do the following sequence:
1158 *
1159 * owner = rt_mutex_owner(lock);
1160 * clear_rt_mutex_waiters(lock);
1161 * raw_spin_unlock(&lock->wait_lock);
1162 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1163 * return;
1164 * goto retry;
1165 *
1166 * The fastpath disabled variant is simple as all access to
1167 * lock->owner is serialized by lock->wait_lock:
1168 *
1169 * lock->owner = NULL;
1170 * raw_spin_unlock(&lock->wait_lock);
1171 */
1172 while (!rt_mutex_has_waiters(lock)) {
1173 /* Drops lock->wait_lock ! */
1174 if (unlock_rt_mutex_safe(lock, flags) == true)
1175 return;
1176 /* Relock the rtmutex and try again */
1177 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1178 }
1179
1180 /*
1181 * The wakeup next waiter path does not suffer from the above
1182 * race. See the comments there.
1183 *
1184 * Queue the next waiter for wakeup once we release the wait_lock.
1185 */
1186 mark_wakeup_next_waiter(&wqh, lock);
1187 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1188
1189 rt_mutex_wake_up_q(&wqh);
1190}
1191
1192static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
1193{
1194 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1195 return;
1196
1197 rt_mutex_slowunlock(lock);
1198}
1199
1200#ifdef RT_MUTEX_BUILD_MUTEX
1201/*
1202 * Functions required for:
1203 * - rtmutex, futex on all kernels
1204 * - mutex and rwsem substitutions on RT kernels
1205 */
1206
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001207/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001208 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001209 *
Thomas Gleixnere17ba59b2021-08-15 23:28:12 +02001210 * Must be called with lock->wait_lock held and interrupts disabled. It must
Lai Jiangshan81612392011-01-14 17:09:41 +08001211 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001212 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001213static void __sched remove_waiter(struct rt_mutex_base *lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001214 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001215{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001216 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001217 struct task_struct *owner = rt_mutex_owner(lock);
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001218 struct rt_mutex_base *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001219
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +01001220 lockdep_assert_held(&lock->wait_lock);
1221
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001222 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001223 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001224 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001225 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001226
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001227 /*
1228 * Only update priority if the waiter was the highest priority
1229 * waiter of the lock and there is an owner to update.
1230 */
1231 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001232 return;
1233
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001234 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001235
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001236 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001237
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001238 if (rt_mutex_has_waiters(lock))
1239 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001240
Peter Zijlstraacd58622017-03-23 15:56:11 +01001241 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001242
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001243 /* Store the lock on which owner is blocked or NULL */
1244 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001245
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001246 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001247
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001248 /*
1249 * Don't walk the chain, if the owner task is not blocked
1250 * itself.
1251 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001252 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001253 return;
1254
Steven Rostedtdb630632006-09-29 01:59:44 -07001255 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1256 get_task_struct(owner);
1257
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001258 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001259
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001260 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1261 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001262
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001263 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001264}
1265
Darren Hart8dac4562009-04-03 13:40:12 -07001266/**
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001267 * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop
Darren Hart8dac4562009-04-03 13:40:12 -07001268 * @lock: the rt_mutex to take
1269 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001270 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001271 * @timeout: the pre-initialized and started timer, or NULL for none
1272 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001273 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001274 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001275 */
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001276static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
1277 unsigned int state,
1278 struct hrtimer_sleeper *timeout,
1279 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001280{
1281 int ret = 0;
1282
1283 for (;;) {
1284 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001285 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001286 break;
1287
Thomas Gleixnera51a3272021-03-26 16:29:44 +01001288 if (timeout && !timeout->task) {
1289 ret = -ETIMEDOUT;
1290 break;
1291 }
1292 if (signal_pending_state(state, current)) {
1293 ret = -EINTR;
1294 break;
Darren Hart8dac4562009-04-03 13:40:12 -07001295 }
1296
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001297 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001298
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001299 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001300
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001301 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001302 set_current_state(state);
1303 }
1304
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001305 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001306 return ret;
1307}
1308
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001309static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
1310 struct rt_mutex_waiter *w)
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001311{
1312 /*
1313 * If the result is not -EDEADLOCK or the caller requested
1314 * deadlock detection, nothing to do here.
1315 */
1316 if (res != -EDEADLOCK || detect_deadlock)
1317 return;
1318
1319 /*
Ingo Molnare2db7592021-03-22 02:35:05 +01001320 * Yell loudly and stop the task right here.
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001321 */
Sebastian Andrzej Siewior6d41c672021-03-26 16:29:32 +01001322 WARN(1, "rtmutex deadlock detected\n");
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001323 while (1) {
1324 set_current_state(TASK_INTERRUPTIBLE);
1325 schedule();
1326 }
1327}
1328
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001329/**
1330 * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held
1331 * @lock: The rtmutex to block lock
1332 * @state: The task state for sleeping
1333 * @chwalk: Indicator whether full or partial chainwalk is requested
1334 * @waiter: Initializer waiter for blocking
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001335 */
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001336static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,
1337 unsigned int state,
1338 enum rtmutex_chainwalk chwalk,
1339 struct rt_mutex_waiter *waiter)
1340{
1341 int ret;
1342
1343 lockdep_assert_held(&lock->wait_lock);
1344
1345 /* Try to acquire the lock again: */
1346 if (try_to_take_rt_mutex(lock, current, NULL))
1347 return 0;
1348
1349 set_current_state(state);
1350
1351 ret = task_blocks_on_rt_mutex(lock, waiter, current, chwalk);
1352
1353 if (likely(!ret))
1354 ret = rt_mutex_slowlock_block(lock, state, NULL, waiter);
1355
1356 if (unlikely(ret)) {
1357 __set_current_state(TASK_RUNNING);
1358 remove_waiter(lock, waiter);
1359 rt_mutex_handle_deadlock(ret, chwalk, waiter);
1360 }
1361
1362 /*
1363 * try_to_take_rt_mutex() sets the waiter bit
1364 * unconditionally. We might have to fix that up.
1365 */
1366 fixup_rt_mutex_waiters(lock);
1367 return ret;
1368}
1369
1370static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock,
1371 unsigned int state)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001372{
1373 struct rt_mutex_waiter waiter;
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001374 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001375
Peter Zijlstra50809352017-03-22 11:35:56 +01001376 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001377
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001378 ret = __rt_mutex_slowlock(lock, state, RT_MUTEX_MIN_CHAINWALK, &waiter);
1379
1380 debug_rt_mutex_free_waiter(&waiter);
1381 return ret;
1382}
1383
1384/*
1385 * rt_mutex_slowlock - Locking slowpath invoked when fast path fails
1386 * @lock: The rtmutex to block lock
1387 * @state: The task state for sleeping
1388 */
1389static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
1390 unsigned int state)
1391{
1392 unsigned long flags;
1393 int ret;
1394
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001395 /*
1396 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1397 * be called in early boot if the cmpxchg() fast path is disabled
1398 * (debug, no architecture support). In this case we will acquire the
1399 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1400 * enable interrupts in that early boot case. So we need to use the
1401 * irqsave/restore variants.
1402 */
1403 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001404 ret = __rt_mutex_slowlock_locked(lock, state);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001405 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001406
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001407 return ret;
1408}
1409
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001410static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
Thomas Gleixner531ae4b2021-08-15 23:27:57 +02001411 unsigned int state)
1412{
1413 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1414 return 0;
1415
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001416 return rt_mutex_slowlock(lock, state);
Thomas Gleixner531ae4b2021-08-15 23:27:57 +02001417}
Thomas Gleixnere17ba59b2021-08-15 23:28:12 +02001418#endif /* RT_MUTEX_BUILD_MUTEX */