blob: 5f0d0725ca32787c7527ccbba5a1356e8c5b9683 [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{
354 wake_q_add(&wqh->head, w->task);
355}
356
357static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
358{
359 wake_up_q(&wqh->head);
360
361 /* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
362 preempt_enable();
363}
364
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700365/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000366 * Deadlock detection is conditional:
367 *
368 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
369 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
370 *
371 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
372 * conducted independent of the detect argument.
373 *
374 * If the waiter argument is NULL this indicates the deboost path and
375 * deadlock detection is disabled independent of the detect argument
376 * and the config settings.
377 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100378static __always_inline bool
379rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
380 enum rtmutex_chainwalk chwalk)
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000381{
Zhen Lei07d25972021-07-31 20:30:11 +0800382 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
Thomas Gleixnerf7efc472021-03-26 16:29:36 +0100383 return waiter != NULL;
384 return chwalk == RT_MUTEX_FULL_CHAINWALK;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000385}
386
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200387static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p)
Thomas Gleixner82084982014-06-05 11:16:12 +0200388{
389 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
390}
391
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700392/*
393 * Adjust the priority chain. Also used for deadlock detection.
394 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200395 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200396 * @task: the task owning the mutex (owner) for which a chain walk is
397 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900398 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200399 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
400 * things for a task that has just got its priority adjusted, and
401 * is waiting on a mutex)
402 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
403 * we dropped its pi_lock. Is never dereferenced, only used for
404 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200405 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200406 * its priority to the mutex owner (can be NULL in the case
407 * depicted above or if the top waiter is gone away and we are
408 * actually deboosting the owner)
409 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200410 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700411 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200412 *
413 * Chain walk basics and protection scope
414 *
415 * [R] refcount on task
416 * [P] task->pi_lock held
417 * [L] rtmutex->wait_lock held
418 *
419 * Step Description Protected by
420 * function arguments:
421 * @task [R]
422 * @orig_lock if != NULL @top_task is blocked on it
423 * @next_lock Unprotected. Cannot be
424 * dereferenced. Only used for
425 * comparison.
426 * @orig_waiter if != NULL @top_task is blocked on it
427 * @top_task current, or in case of proxy
428 * locking protected by calling
429 * code
430 * again:
431 * loop_sanity_check();
432 * retry:
433 * [1] lock(task->pi_lock); [R] acquire [P]
434 * [2] waiter = task->pi_blocked_on; [P]
435 * [3] check_exit_conditions_1(); [P]
436 * [4] lock = waiter->lock; [P]
437 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
438 * unlock(task->pi_lock); release [P]
439 * goto retry;
440 * }
441 * [6] check_exit_conditions_2(); [P] + [L]
442 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
443 * [8] unlock(task->pi_lock); release [P]
444 * put_task_struct(task); release [R]
445 * [9] check_exit_conditions_3(); [L]
446 * [10] task = owner(lock); [L]
447 * get_task_struct(task); [L] acquire [R]
448 * lock(task->pi_lock); [L] acquire [P]
449 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
450 * [12] check_exit_conditions_4(); [P] + [L]
451 * [13] unlock(task->pi_lock); release [P]
452 * unlock(lock->wait_lock); release [L]
453 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700454 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100455static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
456 enum rtmutex_chainwalk chwalk,
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200457 struct rt_mutex_base *orig_lock,
458 struct rt_mutex_base *next_lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100459 struct rt_mutex_waiter *orig_waiter,
460 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700461{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700462 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000463 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000464 int ret = 0, depth = 0;
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200465 struct rt_mutex_base *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000466 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000467 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700468
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000469 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700470
471 /*
472 * The (de)boosting is a step by step approach with a lot of
473 * pitfalls. We want this to be preemptible and we want hold a
474 * maximum of two locks per step. So we have to check
475 * carefully whether things change under us.
476 */
477 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200478 /*
479 * We limit the lock chain length for each invocation.
480 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700481 if (++depth > max_lock_depth) {
482 static int prev_max;
483
484 /*
485 * Print this only once. If the admin changes the limit,
486 * print a new message when reaching the limit again.
487 */
488 if (prev_max != max_lock_depth) {
489 prev_max = max_lock_depth;
490 printk(KERN_WARNING "Maximum lock depth %d reached "
491 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700492 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700493 }
494 put_task_struct(task);
495
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200496 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700497 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200498
499 /*
500 * We are fully preemptible here and only hold the refcount on
501 * @task. So everything can have changed under us since the
502 * caller or our own code below (goto retry/again) dropped all
503 * locks.
504 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700505 retry:
506 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200507 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700508 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100509 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700510
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200511 /*
512 * [2] Get the waiter on which @task is blocked on.
513 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700514 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200515
516 /*
517 * [3] check_exit_conditions_1() protected by task->pi_lock.
518 */
519
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700520 /*
521 * Check whether the end of the boosting chain has been
522 * reached or the state of the chain has changed while we
523 * dropped the locks.
524 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800525 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700526 goto out_unlock_pi;
527
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700528 /*
529 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800530 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700531 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800532 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700533 goto out_unlock_pi;
534
535 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200536 * We dropped all locks after taking a refcount on @task, so
537 * the task might have moved on in the lock chain or even left
538 * the chain completely and blocks now on an unrelated lock or
539 * on @orig_lock.
540 *
541 * We stored the lock on which @task was blocked in @next_lock,
542 * so we can detect the chain change.
543 */
544 if (next_lock != waiter->lock)
545 goto out_unlock_pi;
546
547 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700548 * Drop out, when the task has no waiters. Note,
549 * top_waiter can be NULL, when we are in the deboosting
550 * mode!
551 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000552 if (top_waiter) {
553 if (!task_has_pi_waiters(task))
554 goto out_unlock_pi;
555 /*
556 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000557 * are not the top pi waiter of the task. If deadlock
558 * detection is enabled we continue, but stop the
559 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000560 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000561 if (top_waiter != task_top_pi_waiter(task)) {
562 if (!detect_deadlock)
563 goto out_unlock_pi;
564 else
565 requeue = false;
566 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000567 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700568
569 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000570 * If the waiter priority is the same as the task priority
571 * then there is no further priority adjustment necessary. If
572 * deadlock detection is off, we stop the chain walk. If its
573 * enabled we continue, but stop the requeueing in the chain
574 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700575 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100576 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000577 if (!detect_deadlock)
578 goto out_unlock_pi;
579 else
580 requeue = false;
581 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700582
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200583 /*
584 * [4] Get the next lock
585 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700586 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200587 /*
588 * [5] We need to trylock here as we are holding task->pi_lock,
589 * which is the reverse lock order versus the other rtmutex
590 * operations.
591 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100592 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100593 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700594 cpu_relax();
595 goto retry;
596 }
597
Thomas Gleixner397335f2014-05-22 03:25:39 +0000598 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200599 * [6] check_exit_conditions_2() protected by task->pi_lock and
600 * lock->wait_lock.
601 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000602 * Deadlock detection. If the lock is the same as the original
603 * lock which caused us to walk the lock chain or if the
604 * current lock is owned by the task which initiated the chain
605 * walk, we detected a deadlock.
606 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700607 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100608 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200609 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700610 goto out_unlock_pi;
611 }
612
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000613 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000614 * If we just follow the lock chain for deadlock detection, no
615 * need to do all the requeue operations. To avoid a truckload
616 * of conditionals around the various places below, just do the
617 * minimum chain walk checks.
618 */
619 if (!requeue) {
620 /*
621 * No requeue[7] here. Just release @task [8]
622 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100623 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000624 put_task_struct(task);
625
626 /*
627 * [9] check_exit_conditions_3 protected by lock->wait_lock.
628 * If there is no owner of the lock, end of chain.
629 */
630 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100631 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000632 return 0;
633 }
634
635 /* [10] Grab the next task, i.e. owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700636 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100637 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000638
639 /*
640 * No requeue [11] here. We just do deadlock detection.
641 *
642 * [12] Store whether owner is blocked
643 * itself. Decision is made after dropping the locks
644 */
645 next_lock = task_blocked_on_lock(task);
646 /*
647 * Get the top waiter for the next iteration
648 */
649 top_waiter = rt_mutex_top_waiter(lock);
650
651 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100652 raw_spin_unlock(&task->pi_lock);
653 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000654
655 /* If owner is not blocked, end of chain. */
656 if (!next_lock)
657 goto out_put_task;
658 goto again;
659 }
660
661 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000662 * Store the current top waiter before doing the requeue
663 * operation on @lock. We need it for the boost/deboost
664 * decision below.
665 */
666 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700667
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700668 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100669 rt_mutex_dequeue(lock, waiter);
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100670
671 /*
672 * Update the waiter prio fields now that we're dequeued.
673 *
674 * These values can have changed through either:
675 *
676 * sys_sched_set_scheduler() / sys_sched_setattr()
677 *
678 * or
679 *
680 * DL CBS enforcement advancing the effective deadline.
681 *
682 * Even though pi_waiters also uses these fields, and that tree is only
683 * updated in [11], we can do this here, since we hold [L], which
684 * serializes all pi_waiters access and rb_erase() does not care about
685 * the values of the node being removed.
686 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100687 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100688 waiter->deadline = task->dl.deadline;
689
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100690 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700691
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200692 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100693 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200694 put_task_struct(task);
695
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000696 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200697 * [9] check_exit_conditions_3 protected by lock->wait_lock.
698 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000699 * We must abort the chain walk if there is no lock owner even
700 * in the dead lock detection case, as we have nothing to
701 * follow here. This is the end of the chain we are walking.
702 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800703 if (!rt_mutex_owner(lock)) {
704 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200705 * If the requeue [7] above changed the top waiter,
706 * then we need to wake the new top waiter up to try
707 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800708 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000709 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Thomas Gleixnerc014ef62021-08-15 23:28:06 +0200710 wake_up_state(waiter->task, waiter->wake_state);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100711 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200712 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800713 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700714
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200715 /* [10] Grab the next task, i.e. the owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700716 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100717 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700718
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200719 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700720 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000721 /*
722 * The waiter became the new top (highest priority)
723 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700724 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000725 * and adjust the priority of the owner.
726 */
727 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100728 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100729 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700730
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000731 } else if (prerequeue_top_waiter == waiter) {
732 /*
733 * The waiter was the top waiter on the lock, but is
Ingo Molnare2db7592021-03-22 02:35:05 +0100734 * no longer the top priority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700735 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000736 * (highest priority) waiter and adjust the priority
737 * of the owner.
738 * The new top waiter is stored in @waiter so that
739 * @waiter == @top_waiter evaluates to true below and
740 * we continue to deboost the rest of the chain.
741 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100742 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700743 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100744 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100745 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000746 } else {
747 /*
748 * Nothing changed. No need to do any priority
749 * adjustment.
750 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700751 }
752
Thomas Gleixner82084982014-06-05 11:16:12 +0200753 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200754 * [12] check_exit_conditions_4() protected by task->pi_lock
755 * and lock->wait_lock. The actual decisions are made after we
756 * dropped the locks.
757 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200758 * Check whether the task which owns the current lock is pi
759 * blocked itself. If yes we store a pointer to the lock for
760 * the lock chain change detection above. After we dropped
761 * task->pi_lock next_lock cannot be dereferenced anymore.
762 */
763 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000764 /*
765 * Store the top waiter of @lock for the end of chain walk
766 * decision below.
767 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700768 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200769
770 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100771 raw_spin_unlock(&task->pi_lock);
772 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700773
Thomas Gleixner82084982014-06-05 11:16:12 +0200774 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200775 * Make the actual exit decisions [12], based on the stored
776 * values.
777 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200778 * We reached the end of the lock chain. Stop right here. No
779 * point to go back just to figure that out.
780 */
781 if (!next_lock)
782 goto out_put_task;
783
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000784 /*
785 * If the current waiter is not the top waiter on the lock,
786 * then we can stop the chain walk here if we are not in full
787 * deadlock detection mode.
788 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700789 if (!detect_deadlock && waiter != top_waiter)
790 goto out_put_task;
791
792 goto again;
793
794 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100795 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700796 out_put_task:
797 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700798
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700799 return ret;
800}
801
802/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700803 * Try to take an rt-mutex
804 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100805 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800806 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200807 * @lock: The lock to be acquired.
808 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700809 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200810 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700811 */
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100812static int __sched
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200813try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100814 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700815{
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100816 lockdep_assert_held(&lock->wait_lock);
817
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700818 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200819 * Before testing whether we can acquire @lock, we set the
820 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
821 * other tasks which try to modify @lock into the slow path
822 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700823 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200824 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
825 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700826 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200827 * - There is a lock owner. The caller must fixup the
828 * transient state if it does a trylock or leaves the lock
829 * function due to a signal or timeout.
830 *
831 * - @task acquires the lock and there are no other
832 * waiters. This is undone in rt_mutex_set_owner(@task) at
833 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700834 */
835 mark_rt_mutex_waiters(lock);
836
Thomas Gleixner358c3312014-06-11 01:01:13 +0200837 /*
838 * If @lock has an owner, give up.
839 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800840 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700841 return 0;
842
Lai Jiangshan81612392011-01-14 17:09:41 +0800843 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200844 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700845 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200846 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800847 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200848 if (waiter) {
849 /*
850 * If waiter is not the highest priority waiter of
851 * @lock, give up.
852 */
853 if (waiter != rt_mutex_top_waiter(lock))
854 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800855
856 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200857 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700858 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200859 */
860 rt_mutex_dequeue(lock, waiter);
861
862 } else {
863 /*
864 * If the lock has waiters already we check whether @task is
865 * eligible to take over the lock.
866 *
867 * If there are no other waiters, @task can acquire
868 * the lock. @task->pi_blocked_on is NULL, so it does
869 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800870 */
871 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200872 /*
873 * If @task->prio is greater than or equal to
874 * the top waiter priority (kernel view),
875 * @task lost.
876 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100877 if (!rt_mutex_waiter_less(task_to_waiter(task),
878 rt_mutex_top_waiter(lock)))
Thomas Gleixner358c3312014-06-11 01:01:13 +0200879 return 0;
880
881 /*
882 * The current top waiter stays enqueued. We
883 * don't have to change anything in the lock
884 * waiters order.
885 */
886 } else {
887 /*
888 * No waiters. Take the lock without the
889 * pi_lock dance.@task->pi_blocked_on is NULL
890 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700891 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200892 */
893 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800894 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800895 }
896
Thomas Gleixner358c3312014-06-11 01:01:13 +0200897 /*
898 * Clear @task->pi_blocked_on. Requires protection by
899 * @task->pi_lock. Redundant operation for the @waiter == NULL
900 * case, but conditionals are more expensive than a redundant
901 * store.
902 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100903 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200904 task->pi_blocked_on = NULL;
905 /*
906 * Finish the lock acquisition. @task is the new owner. If
907 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700908 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200909 */
910 if (rt_mutex_has_waiters(lock))
911 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100912 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200913
914takeit:
Thomas Gleixner358c3312014-06-11 01:01:13 +0200915 /*
916 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
917 * are still waiters or clears it.
918 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800919 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700920
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700921 return 1;
922}
923
924/*
925 * Task blocks on lock.
926 *
927 * Prepare waiter and propagate pi chain
928 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100929 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700930 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200931static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +0100932 struct rt_mutex_waiter *waiter,
933 struct task_struct *task,
934 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700935{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700936 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700937 struct rt_mutex_waiter *top_waiter = waiter;
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200938 struct rt_mutex_base *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700939 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700940
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100941 lockdep_assert_held(&lock->wait_lock);
942
Thomas Gleixner397335f2014-05-22 03:25:39 +0000943 /*
944 * Early deadlock detection. We really don't want the task to
945 * enqueue on itself just to untangle the mess later. It's not
946 * only an optimization. We drop the locks, so another waiter
947 * can come in before the chain walk detects the deadlock. So
948 * the other will detect the deadlock and return -EDEADLOCK,
949 * which is wrong, as the other waiter is not in a deadlock
950 * situation.
951 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200952 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000953 return -EDEADLK;
954
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100955 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700956 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700957 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100958 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100959 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700960
961 /* Get the top priority waiter on the lock */
962 if (rt_mutex_has_waiters(lock))
963 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100964 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700965
Darren Hart8dac4562009-04-03 13:40:12 -0700966 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700967
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100968 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700969
Lai Jiangshan81612392011-01-14 17:09:41 +0800970 if (!owner)
971 return 0;
972
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100973 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700974 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100975 rt_mutex_dequeue_pi(owner, top_waiter);
976 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700977
Peter Zijlstraacd58622017-03-23 15:56:11 +0100978 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700979 if (owner->pi_blocked_on)
980 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000981 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700982 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200983 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700984
Thomas Gleixner82084982014-06-05 11:16:12 +0200985 /* Store the lock on which owner is blocked or NULL */
986 next_lock = task_blocked_on_lock(owner);
987
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100988 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200989 /*
990 * Even if full deadlock detection is on, if the owner is not
991 * blocked itself, we can avoid finding this out in the chain
992 * walk.
993 */
994 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700995 return 0;
996
Steven Rostedtdb630632006-09-29 01:59:44 -0700997 /*
998 * The owner can't disappear while holding a lock,
999 * so the owner struct is protected by wait_lock.
1000 * Gets dropped in rt_mutex_adjust_prio_chain()!
1001 */
1002 get_task_struct(owner);
1003
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001004 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001005
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001006 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +02001007 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001008
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001009 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001010
1011 return res;
1012}
1013
1014/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001015 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001016 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001017 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001018 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001019 */
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001020static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh,
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001021 struct rt_mutex_base *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001022{
1023 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001024
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001025 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001026
1027 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001028
1029 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +01001030 * Remove it from current->pi_waiters and deboost.
1031 *
1032 * We must in fact deboost here in order to ensure we call
1033 * rt_mutex_setprio() to update p->pi_top_task before the
1034 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001035 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001036 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001037 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001038
Thomas Gleixner27e35712014-06-11 18:44:04 +00001039 /*
1040 * As we are waking up the top waiter, and the waiter stays
1041 * queued on the lock until it gets the lock, this lock
1042 * obviously has waiters. Just set the bit here and this has
1043 * the added benefit of forcing all new tasks into the
1044 * slow path making sure no task of lower priority than
1045 * the top waiter can steal this lock.
1046 */
1047 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001048
Peter Zijlstraacd58622017-03-23 15:56:11 +01001049 /*
1050 * We deboosted before waking the top waiter task such that we don't
1051 * run two tasks with the 'same' priority (and ensure the
1052 * p->pi_top_task pointer points to a blocked task). This however can
1053 * lead to priority inversion if we would get preempted after the
1054 * deboost but before waking our donor task, hence the preempt_disable()
1055 * before unlock.
1056 *
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001057 * Pairs with preempt_enable() in rt_mutex_wake_up_q();
Peter Zijlstraacd58622017-03-23 15:56:11 +01001058 */
1059 preempt_disable();
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001060 rt_mutex_wake_q_add(wqh, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001061 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001062}
1063
1064/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001065 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001066 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001067 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001068 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001069 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001070static void __sched remove_waiter(struct rt_mutex_base *lock,
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001071 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001072{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001073 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001074 struct task_struct *owner = rt_mutex_owner(lock);
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001075 struct rt_mutex_base *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001076
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +01001077 lockdep_assert_held(&lock->wait_lock);
1078
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001079 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001080 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001081 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001082 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001083
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001084 /*
1085 * Only update priority if the waiter was the highest priority
1086 * waiter of the lock and there is an owner to update.
1087 */
1088 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001089 return;
1090
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001091 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001092
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001093 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001094
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001095 if (rt_mutex_has_waiters(lock))
1096 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001097
Peter Zijlstraacd58622017-03-23 15:56:11 +01001098 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001099
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001100 /* Store the lock on which owner is blocked or NULL */
1101 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001102
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001103 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001104
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001105 /*
1106 * Don't walk the chain, if the owner task is not blocked
1107 * itself.
1108 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001109 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001110 return;
1111
Steven Rostedtdb630632006-09-29 01:59:44 -07001112 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1113 get_task_struct(owner);
1114
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001115 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001116
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001117 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1118 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001119
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001120 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001121}
1122
Darren Hart8dac4562009-04-03 13:40:12 -07001123/**
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001124 * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop
Darren Hart8dac4562009-04-03 13:40:12 -07001125 * @lock: the rt_mutex to take
1126 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001127 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001128 * @timeout: the pre-initialized and started timer, or NULL for none
1129 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001130 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001131 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001132 */
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001133static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
1134 unsigned int state,
1135 struct hrtimer_sleeper *timeout,
1136 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001137{
1138 int ret = 0;
1139
1140 for (;;) {
1141 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001142 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001143 break;
1144
Thomas Gleixnera51a3272021-03-26 16:29:44 +01001145 if (timeout && !timeout->task) {
1146 ret = -ETIMEDOUT;
1147 break;
1148 }
1149 if (signal_pending_state(state, current)) {
1150 ret = -EINTR;
1151 break;
Darren Hart8dac4562009-04-03 13:40:12 -07001152 }
1153
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001154 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001155
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001156 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001157
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001158 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001159 set_current_state(state);
1160 }
1161
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001162 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001163 return ret;
1164}
1165
Thomas Gleixnerd7a2edb2021-03-26 16:29:40 +01001166static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
1167 struct rt_mutex_waiter *w)
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001168{
1169 /*
1170 * If the result is not -EDEADLOCK or the caller requested
1171 * deadlock detection, nothing to do here.
1172 */
1173 if (res != -EDEADLOCK || detect_deadlock)
1174 return;
1175
1176 /*
Ingo Molnare2db7592021-03-22 02:35:05 +01001177 * Yell loudly and stop the task right here.
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001178 */
Sebastian Andrzej Siewior6d41c672021-03-26 16:29:32 +01001179 WARN(1, "rtmutex deadlock detected\n");
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001180 while (1) {
1181 set_current_state(TASK_INTERRUPTIBLE);
1182 schedule();
1183 }
1184}
1185
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001186/**
1187 * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held
1188 * @lock: The rtmutex to block lock
1189 * @state: The task state for sleeping
1190 * @chwalk: Indicator whether full or partial chainwalk is requested
1191 * @waiter: Initializer waiter for blocking
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001192 */
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001193static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,
1194 unsigned int state,
1195 enum rtmutex_chainwalk chwalk,
1196 struct rt_mutex_waiter *waiter)
1197{
1198 int ret;
1199
1200 lockdep_assert_held(&lock->wait_lock);
1201
1202 /* Try to acquire the lock again: */
1203 if (try_to_take_rt_mutex(lock, current, NULL))
1204 return 0;
1205
1206 set_current_state(state);
1207
1208 ret = task_blocks_on_rt_mutex(lock, waiter, current, chwalk);
1209
1210 if (likely(!ret))
1211 ret = rt_mutex_slowlock_block(lock, state, NULL, waiter);
1212
1213 if (unlikely(ret)) {
1214 __set_current_state(TASK_RUNNING);
1215 remove_waiter(lock, waiter);
1216 rt_mutex_handle_deadlock(ret, chwalk, waiter);
1217 }
1218
1219 /*
1220 * try_to_take_rt_mutex() sets the waiter bit
1221 * unconditionally. We might have to fix that up.
1222 */
1223 fixup_rt_mutex_waiters(lock);
1224 return ret;
1225}
1226
1227static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock,
1228 unsigned int state)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001229{
1230 struct rt_mutex_waiter waiter;
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001231 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001232
Peter Zijlstra50809352017-03-22 11:35:56 +01001233 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001234
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001235 ret = __rt_mutex_slowlock(lock, state, RT_MUTEX_MIN_CHAINWALK, &waiter);
1236
1237 debug_rt_mutex_free_waiter(&waiter);
1238 return ret;
1239}
1240
1241/*
1242 * rt_mutex_slowlock - Locking slowpath invoked when fast path fails
1243 * @lock: The rtmutex to block lock
1244 * @state: The task state for sleeping
1245 */
1246static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
1247 unsigned int state)
1248{
1249 unsigned long flags;
1250 int ret;
1251
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001252 /*
1253 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1254 * be called in early boot if the cmpxchg() fast path is disabled
1255 * (debug, no architecture support). In this case we will acquire the
1256 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1257 * enable interrupts in that early boot case. So we need to use the
1258 * irqsave/restore variants.
1259 */
1260 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001261 ret = __rt_mutex_slowlock_locked(lock, state);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001262 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001263
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001264 return ret;
1265}
1266
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001267static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
Thomas Gleixner531ae4b2021-08-15 23:27:57 +02001268 unsigned int state)
1269{
1270 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1271 return 0;
1272
Thomas Gleixnerebbdc412021-08-15 23:28:00 +02001273 return rt_mutex_slowlock(lock, state);
Thomas Gleixner531ae4b2021-08-15 23:27:57 +02001274}
1275
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001276static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock)
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001277{
1278 int ret = try_to_take_rt_mutex(lock, current, NULL);
1279
1280 /*
1281 * try_to_take_rt_mutex() sets the lock waiters bit
1282 * unconditionally. Clean this up.
1283 */
1284 fixup_rt_mutex_waiters(lock);
1285
1286 return ret;
1287}
1288
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001289/*
1290 * Slow path try-lock function:
1291 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001292static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001293{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001294 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001295 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001296
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001297 /*
1298 * If the lock already has an owner we fail to get the lock.
1299 * This can be done without taking the @lock->wait_lock as
1300 * it is only being read, and this is a trylock anyway.
1301 */
1302 if (rt_mutex_owner(lock))
1303 return 0;
1304
1305 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001306 * The mutex has currently no owner. Lock the wait lock and try to
1307 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001308 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001309 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001310
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001311 ret = __rt_mutex_slowtrylock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001312
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001313 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001314
1315 return ret;
1316}
1317
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001318static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock)
Thomas Gleixner70c80102021-03-26 16:29:41 +01001319{
Thomas Gleixner531ae4b2021-08-15 23:27:57 +02001320 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1321 return 1;
Thomas Gleixner70c80102021-03-26 16:29:41 +01001322
Thomas Gleixner531ae4b2021-08-15 23:27:57 +02001323 return rt_mutex_slowtrylock(lock);
Thomas Gleixner70c80102021-03-26 16:29:41 +01001324}
1325
1326/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001327 * Slow path to release a rt-mutex.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001328 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001329static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001330{
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001331 DEFINE_RT_WAKE_Q(wqh);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001332 unsigned long flags;
1333
1334 /* irqsave required to support early boot calls */
1335 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001336
1337 debug_rt_mutex_unlock(lock);
1338
Thomas Gleixner27e35712014-06-11 18:44:04 +00001339 /*
1340 * We must be careful here if the fast path is enabled. If we
1341 * have no waiters queued we cannot set owner to NULL here
1342 * because of:
1343 *
1344 * foo->lock->owner = NULL;
1345 * rtmutex_lock(foo->lock); <- fast path
1346 * free = atomic_dec_and_test(foo->refcnt);
1347 * rtmutex_unlock(foo->lock); <- fast path
1348 * if (free)
1349 * kfree(foo);
1350 * raw_spin_unlock(foo->lock->wait_lock);
1351 *
1352 * So for the fastpath enabled kernel:
1353 *
1354 * Nothing can set the waiters bit as long as we hold
1355 * lock->wait_lock. So we do the following sequence:
1356 *
1357 * owner = rt_mutex_owner(lock);
1358 * clear_rt_mutex_waiters(lock);
1359 * raw_spin_unlock(&lock->wait_lock);
1360 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1361 * return;
1362 * goto retry;
1363 *
1364 * The fastpath disabled variant is simple as all access to
1365 * lock->owner is serialized by lock->wait_lock:
1366 *
1367 * lock->owner = NULL;
1368 * raw_spin_unlock(&lock->wait_lock);
1369 */
1370 while (!rt_mutex_has_waiters(lock)) {
1371 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001372 if (unlock_rt_mutex_safe(lock, flags) == true)
Thomas Gleixner70c80102021-03-26 16:29:41 +01001373 return;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001374 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001375 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001376 }
1377
Thomas Gleixner27e35712014-06-11 18:44:04 +00001378 /*
1379 * The wakeup next waiter path does not suffer from the above
1380 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001381 *
1382 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001383 */
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001384 mark_wakeup_next_waiter(&wqh, lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001385 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001386
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001387 rt_mutex_wake_up_q(&wqh);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001388}
1389
Peter Zijlstra830e6ac2021-08-15 23:27:58 +02001390static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001391{
Thomas Gleixner70c80102021-03-26 16:29:41 +01001392 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1393 return;
1394
1395 rt_mutex_slowunlock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001396}