blob: 4beca549aeebd4c785496021e18d8648be6ffc39 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07002/*
3 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
4 *
5 * started by Ingo Molnar and Thomas Gleixner.
6 *
7 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9 * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
10 * Copyright (C) 2006 Esben Nielsen
Steven Rostedtd07fe82c22006-07-30 03:04:03 -070011 *
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030012 * See Documentation/locking/rt-mutex-design.rst for details.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070013 */
14#include <linux/spinlock.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010016#include <linux/sched/signal.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060017#include <linux/sched/rt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010018#include <linux/sched/deadline.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010019#include <linux/sched/wake_q.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070021#include <linux/timer.h>
22
23#include "rtmutex_common.h"
24
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070025/*
26 * lock->owner state tracking:
27 *
Lai Jiangshan81612392011-01-14 17:09:41 +080028 * lock->owner holds the task_struct pointer of the owner. Bit 0
29 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070030 *
Lai Jiangshan81612392011-01-14 17:09:41 +080031 * owner bit0
32 * NULL 0 lock is free (fast acquire possible)
33 * NULL 1 lock is free and has waiters and the top waiter
34 * is going to take the lock*
35 * taskpointer 0 lock is held (fast release possible)
36 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070037 *
38 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080039 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070040 *
Lai Jiangshan81612392011-01-14 17:09:41 +080041 * (*) It also can be a transitional state when grabbing the lock
42 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
43 * we need to set the bit0 before looking at the lock, and the owner may be
44 * NULL in this small time, hence this can be a transitional state.
45 *
46 * (**) There is a small time when bit 0 is set but there are no
47 * waiters. This can happen when grabbing the lock in the slow path.
48 * To prevent a cmpxchg of the owner releasing the lock, we need to
49 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070050 */
51
Thomas Gleixnerbd197232007-06-17 21:11:10 +020052static void
Lai Jiangshan81612392011-01-14 17:09:41 +080053rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070054{
Lai Jiangshan81612392011-01-14 17:09:41 +080055 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070056
57 if (rt_mutex_has_waiters(lock))
58 val |= RT_MUTEX_HAS_WAITERS;
59
Paul E. McKenney0050c7b2020-01-03 15:59:12 -080060 WRITE_ONCE(lock->owner, (struct task_struct *)val);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070061}
62
63static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
64{
65 lock->owner = (struct task_struct *)
66 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
67}
68
69static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
70{
Thomas Gleixnerdbb26052016-11-30 21:04:41 +000071 unsigned long owner, *p = (unsigned long *) &lock->owner;
72
73 if (rt_mutex_has_waiters(lock))
74 return;
75
76 /*
77 * The rbtree has no waiters enqueued, now make sure that the
78 * lock->owner still has the waiters bit set, otherwise the
79 * following can happen:
80 *
81 * CPU 0 CPU 1 CPU2
82 * l->owner=T1
83 * rt_mutex_lock(l)
84 * lock(l->lock)
85 * l->owner = T1 | HAS_WAITERS;
86 * enqueue(T2)
87 * boost()
88 * unlock(l->lock)
89 * block()
90 *
91 * rt_mutex_lock(l)
92 * lock(l->lock)
93 * l->owner = T1 | HAS_WAITERS;
94 * enqueue(T3)
95 * boost()
96 * unlock(l->lock)
97 * block()
98 * signal(->T2) signal(->T3)
99 * lock(l->lock)
100 * dequeue(T2)
101 * deboost()
102 * unlock(l->lock)
103 * lock(l->lock)
104 * dequeue(T3)
105 * ==> wait list is empty
106 * deboost()
107 * unlock(l->lock)
108 * lock(l->lock)
109 * fixup_rt_mutex_waiters()
110 * if (wait_list_empty(l) {
111 * l->owner = owner
112 * owner = l->owner & ~HAS_WAITERS;
113 * ==> l->owner = T1
114 * }
115 * lock(l->lock)
116 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
117 * if (wait_list_empty(l) {
118 * owner = l->owner & ~HAS_WAITERS;
119 * cmpxchg(l->owner, T1, NULL)
120 * ===> Success (l->owner = NULL)
121 *
122 * l->owner = owner
123 * ==> l->owner = T1
124 * }
125 *
126 * With the check for the waiter bit in place T3 on CPU2 will not
127 * overwrite. All tasks fiddling with the waiters bit are
128 * serialized by l->lock, so nothing else can modify the waiters
129 * bit. If the bit is set then nothing can change l->owner either
130 * so the simple RMW is safe. The cmpxchg() will simply fail if it
131 * happens in the middle of the RMW because the waiters bit is
132 * still set.
133 */
134 owner = READ_ONCE(*p);
135 if (owner & RT_MUTEX_HAS_WAITERS)
136 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700137}
138
139/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100140 * We can speed up the acquire/release, if there's no debugging state to be
141 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200142 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100143#ifndef CONFIG_DEBUG_RT_MUTEXES
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700144# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
145# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
146
147/*
148 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
149 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
150 * relaxed semantics suffice.
151 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200152static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
153{
154 unsigned long owner, *p = (unsigned long *) &lock->owner;
155
156 do {
157 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700158 } while (cmpxchg_relaxed(p, owner,
159 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200160}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000161
162/*
163 * Safe fastpath aware unlock:
164 * 1) Clear the waiters bit
165 * 2) Drop lock->wait_lock
166 * 3) Try to unlock the lock with cmpxchg
167 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100168static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
169 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000170 __releases(lock->wait_lock)
171{
172 struct task_struct *owner = rt_mutex_owner(lock);
173
174 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100175 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000176 /*
177 * If a new waiter comes in between the unlock and the cmpxchg
178 * we have two situations:
179 *
180 * unlock(wait_lock);
181 * lock(wait_lock);
182 * cmpxchg(p, owner, 0) == owner
183 * mark_rt_mutex_waiters(lock);
184 * acquire(lock);
185 * or:
186 *
187 * unlock(wait_lock);
188 * lock(wait_lock);
189 * mark_rt_mutex_waiters(lock);
190 *
191 * cmpxchg(p, owner, 0) != owner
192 * enqueue_waiter();
193 * unlock(wait_lock);
194 * lock(wait_lock);
195 * wake waiter();
196 * unlock(wait_lock);
197 * lock(wait_lock);
198 * acquire(lock);
199 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700200 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000201}
202
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200203#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700204# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
205# define rt_mutex_cmpxchg_release(l,c,n) (0)
206
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200207static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
208{
209 lock->owner = (struct task_struct *)
210 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
211}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000212
213/*
214 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
215 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100216static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
217 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000218 __releases(lock->wait_lock)
219{
220 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100221 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000222 return true;
223}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200224#endif
225
Peter Zijlstra19830e52017-03-23 15:56:14 +0100226/*
227 * Only use with rt_mutex_waiter_{less,equal}()
228 */
229#define task_to_waiter(p) \
230 &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
231
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100232static inline int
233rt_mutex_waiter_less(struct rt_mutex_waiter *left,
234 struct rt_mutex_waiter *right)
235{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100236 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100237 return 1;
238
239 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100240 * If both waiters have dl_prio(), we check the deadlines of the
241 * associated tasks.
242 * If left waiter has a dl_prio(), and we didn't return 1 above,
243 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100244 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100245 if (dl_prio(left->prio))
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100246 return dl_time_before(left->deadline, right->deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100247
248 return 0;
249}
250
Peter Zijlstra19830e52017-03-23 15:56:14 +0100251static inline int
252rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
253 struct rt_mutex_waiter *right)
254{
255 if (left->prio != right->prio)
256 return 0;
257
258 /*
259 * If both waiters have dl_prio(), we check the deadlines of the
260 * associated tasks.
261 * If left waiter has a dl_prio(), and we didn't return 0 above,
262 * then right waiter has a dl_prio() too.
263 */
264 if (dl_prio(left->prio))
265 return left->deadline == right->deadline;
266
267 return 1;
268}
269
Peter Zijlstra5a798722020-04-29 17:29:58 +0200270#define __node_2_waiter(node) \
271 rb_entry((node), struct rt_mutex_waiter, tree_entry)
272
273static inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
274{
275 return rt_mutex_waiter_less(__node_2_waiter(a), __node_2_waiter(b));
276}
277
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100278static void
279rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
280{
Peter Zijlstra5a798722020-04-29 17:29:58 +0200281 rb_add_cached(&waiter->tree_entry, &lock->waiters, __waiter_less);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100282}
283
284static void
285rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
286{
287 if (RB_EMPTY_NODE(&waiter->tree_entry))
288 return;
289
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700290 rb_erase_cached(&waiter->tree_entry, &lock->waiters);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100291 RB_CLEAR_NODE(&waiter->tree_entry);
292}
293
Peter Zijlstra5a798722020-04-29 17:29:58 +0200294#define __node_2_pi_waiter(node) \
295 rb_entry((node), struct rt_mutex_waiter, pi_tree_entry)
296
297static inline bool __pi_waiter_less(struct rb_node *a, const struct rb_node *b)
298{
299 return rt_mutex_waiter_less(__node_2_pi_waiter(a), __node_2_pi_waiter(b));
300}
301
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100302static void
303rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
304{
Peter Zijlstra5a798722020-04-29 17:29:58 +0200305 rb_add_cached(&waiter->pi_tree_entry, &task->pi_waiters, __pi_waiter_less);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100306}
307
308static void
309rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
310{
311 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
312 return;
313
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700314 rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100315 RB_CLEAR_NODE(&waiter->pi_tree_entry);
316}
317
Peter Zijlstraacd58622017-03-23 15:56:11 +0100318static void rt_mutex_adjust_prio(struct task_struct *p)
Xunlei Pange96a77052017-03-23 15:56:08 +0100319{
Peter Zijlstraacd58622017-03-23 15:56:11 +0100320 struct task_struct *pi_task = NULL;
Xunlei Pange96a77052017-03-23 15:56:08 +0100321
Peter Zijlstraacd58622017-03-23 15:56:11 +0100322 lockdep_assert_held(&p->pi_lock);
Xunlei Pange96a77052017-03-23 15:56:08 +0100323
Peter Zijlstraacd58622017-03-23 15:56:11 +0100324 if (task_has_pi_waiters(p))
325 pi_task = task_top_pi_waiter(p)->task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700326
Peter Zijlstraacd58622017-03-23 15:56:11 +0100327 rt_mutex_setprio(p, pi_task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700328}
329
330/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000331 * Deadlock detection is conditional:
332 *
333 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
334 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
335 *
336 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
337 * conducted independent of the detect argument.
338 *
339 * If the waiter argument is NULL this indicates the deboost path and
340 * deadlock detection is disabled independent of the detect argument
341 * and the config settings.
342 */
343static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
344 enum rtmutex_chainwalk chwalk)
345{
346 /*
347 * This is just a wrapper function for the following call,
348 * because debug_rt_mutex_detect_deadlock() smells like a magic
349 * debug feature and I wanted to keep the cond function in the
350 * main source file along with the comments instead of having
351 * two of the same in the headers.
352 */
353 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
354}
355
356/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700357 * Max number of times we'll walk the boosting chain:
358 */
359int max_lock_depth = 1024;
360
Thomas Gleixner82084982014-06-05 11:16:12 +0200361static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
362{
363 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
364}
365
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700366/*
367 * Adjust the priority chain. Also used for deadlock detection.
368 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200369 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200370 * @task: the task owning the mutex (owner) for which a chain walk is
371 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900372 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200373 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
374 * things for a task that has just got its priority adjusted, and
375 * is waiting on a mutex)
376 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
377 * we dropped its pi_lock. Is never dereferenced, only used for
378 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200379 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200380 * its priority to the mutex owner (can be NULL in the case
381 * depicted above or if the top waiter is gone away and we are
382 * actually deboosting the owner)
383 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200384 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700385 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200386 *
387 * Chain walk basics and protection scope
388 *
389 * [R] refcount on task
390 * [P] task->pi_lock held
391 * [L] rtmutex->wait_lock held
392 *
393 * Step Description Protected by
394 * function arguments:
395 * @task [R]
396 * @orig_lock if != NULL @top_task is blocked on it
397 * @next_lock Unprotected. Cannot be
398 * dereferenced. Only used for
399 * comparison.
400 * @orig_waiter if != NULL @top_task is blocked on it
401 * @top_task current, or in case of proxy
402 * locking protected by calling
403 * code
404 * again:
405 * loop_sanity_check();
406 * retry:
407 * [1] lock(task->pi_lock); [R] acquire [P]
408 * [2] waiter = task->pi_blocked_on; [P]
409 * [3] check_exit_conditions_1(); [P]
410 * [4] lock = waiter->lock; [P]
411 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
412 * unlock(task->pi_lock); release [P]
413 * goto retry;
414 * }
415 * [6] check_exit_conditions_2(); [P] + [L]
416 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
417 * [8] unlock(task->pi_lock); release [P]
418 * put_task_struct(task); release [R]
419 * [9] check_exit_conditions_3(); [L]
420 * [10] task = owner(lock); [L]
421 * get_task_struct(task); [L] acquire [R]
422 * lock(task->pi_lock); [L] acquire [P]
423 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
424 * [12] check_exit_conditions_4(); [P] + [L]
425 * [13] unlock(task->pi_lock); release [P]
426 * unlock(lock->wait_lock); release [L]
427 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700428 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200429static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000430 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200431 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200432 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200433 struct rt_mutex_waiter *orig_waiter,
434 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700435{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700436 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000437 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000438 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000439 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000440 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000441 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700442
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000443 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700444
445 /*
446 * The (de)boosting is a step by step approach with a lot of
447 * pitfalls. We want this to be preemptible and we want hold a
448 * maximum of two locks per step. So we have to check
449 * carefully whether things change under us.
450 */
451 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200452 /*
453 * We limit the lock chain length for each invocation.
454 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700455 if (++depth > max_lock_depth) {
456 static int prev_max;
457
458 /*
459 * Print this only once. If the admin changes the limit,
460 * print a new message when reaching the limit again.
461 */
462 if (prev_max != max_lock_depth) {
463 prev_max = max_lock_depth;
464 printk(KERN_WARNING "Maximum lock depth %d reached "
465 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700466 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700467 }
468 put_task_struct(task);
469
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200470 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700471 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200472
473 /*
474 * We are fully preemptible here and only hold the refcount on
475 * @task. So everything can have changed under us since the
476 * caller or our own code below (goto retry/again) dropped all
477 * locks.
478 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700479 retry:
480 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200481 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700482 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100483 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700484
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200485 /*
486 * [2] Get the waiter on which @task is blocked on.
487 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700488 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200489
490 /*
491 * [3] check_exit_conditions_1() protected by task->pi_lock.
492 */
493
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700494 /*
495 * Check whether the end of the boosting chain has been
496 * reached or the state of the chain has changed while we
497 * dropped the locks.
498 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800499 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700500 goto out_unlock_pi;
501
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700502 /*
503 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800504 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700505 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800506 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700507 goto out_unlock_pi;
508
509 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200510 * We dropped all locks after taking a refcount on @task, so
511 * the task might have moved on in the lock chain or even left
512 * the chain completely and blocks now on an unrelated lock or
513 * on @orig_lock.
514 *
515 * We stored the lock on which @task was blocked in @next_lock,
516 * so we can detect the chain change.
517 */
518 if (next_lock != waiter->lock)
519 goto out_unlock_pi;
520
521 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700522 * Drop out, when the task has no waiters. Note,
523 * top_waiter can be NULL, when we are in the deboosting
524 * mode!
525 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000526 if (top_waiter) {
527 if (!task_has_pi_waiters(task))
528 goto out_unlock_pi;
529 /*
530 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000531 * are not the top pi waiter of the task. If deadlock
532 * detection is enabled we continue, but stop the
533 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000534 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000535 if (top_waiter != task_top_pi_waiter(task)) {
536 if (!detect_deadlock)
537 goto out_unlock_pi;
538 else
539 requeue = false;
540 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000541 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700542
543 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000544 * If the waiter priority is the same as the task priority
545 * then there is no further priority adjustment necessary. If
546 * deadlock detection is off, we stop the chain walk. If its
547 * enabled we continue, but stop the requeueing in the chain
548 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700549 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100550 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000551 if (!detect_deadlock)
552 goto out_unlock_pi;
553 else
554 requeue = false;
555 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700556
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200557 /*
558 * [4] Get the next lock
559 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700560 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200561 /*
562 * [5] We need to trylock here as we are holding task->pi_lock,
563 * which is the reverse lock order versus the other rtmutex
564 * operations.
565 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100566 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100567 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700568 cpu_relax();
569 goto retry;
570 }
571
Thomas Gleixner397335f2014-05-22 03:25:39 +0000572 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200573 * [6] check_exit_conditions_2() protected by task->pi_lock and
574 * lock->wait_lock.
575 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000576 * Deadlock detection. If the lock is the same as the original
577 * lock which caused us to walk the lock chain or if the
578 * current lock is owned by the task which initiated the chain
579 * walk, we detected a deadlock.
580 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700581 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100582 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200583 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700584 goto out_unlock_pi;
585 }
586
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000587 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000588 * If we just follow the lock chain for deadlock detection, no
589 * need to do all the requeue operations. To avoid a truckload
590 * of conditionals around the various places below, just do the
591 * minimum chain walk checks.
592 */
593 if (!requeue) {
594 /*
595 * No requeue[7] here. Just release @task [8]
596 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100597 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000598 put_task_struct(task);
599
600 /*
601 * [9] check_exit_conditions_3 protected by lock->wait_lock.
602 * If there is no owner of the lock, end of chain.
603 */
604 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100605 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000606 return 0;
607 }
608
609 /* [10] Grab the next task, i.e. owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700610 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100611 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000612
613 /*
614 * No requeue [11] here. We just do deadlock detection.
615 *
616 * [12] Store whether owner is blocked
617 * itself. Decision is made after dropping the locks
618 */
619 next_lock = task_blocked_on_lock(task);
620 /*
621 * Get the top waiter for the next iteration
622 */
623 top_waiter = rt_mutex_top_waiter(lock);
624
625 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100626 raw_spin_unlock(&task->pi_lock);
627 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000628
629 /* If owner is not blocked, end of chain. */
630 if (!next_lock)
631 goto out_put_task;
632 goto again;
633 }
634
635 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000636 * Store the current top waiter before doing the requeue
637 * operation on @lock. We need it for the boost/deboost
638 * decision below.
639 */
640 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700641
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700642 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100643 rt_mutex_dequeue(lock, waiter);
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100644
645 /*
646 * Update the waiter prio fields now that we're dequeued.
647 *
648 * These values can have changed through either:
649 *
650 * sys_sched_set_scheduler() / sys_sched_setattr()
651 *
652 * or
653 *
654 * DL CBS enforcement advancing the effective deadline.
655 *
656 * Even though pi_waiters also uses these fields, and that tree is only
657 * updated in [11], we can do this here, since we hold [L], which
658 * serializes all pi_waiters access and rb_erase() does not care about
659 * the values of the node being removed.
660 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100661 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100662 waiter->deadline = task->dl.deadline;
663
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100664 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700665
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200666 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100667 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200668 put_task_struct(task);
669
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000670 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200671 * [9] check_exit_conditions_3 protected by lock->wait_lock.
672 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000673 * We must abort the chain walk if there is no lock owner even
674 * in the dead lock detection case, as we have nothing to
675 * follow here. This is the end of the chain we are walking.
676 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800677 if (!rt_mutex_owner(lock)) {
678 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200679 * If the requeue [7] above changed the top waiter,
680 * then we need to wake the new top waiter up to try
681 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800682 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000683 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800684 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100685 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200686 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800687 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700688
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200689 /* [10] Grab the next task, i.e. the owner of @lock */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -0700690 task = get_task_struct(rt_mutex_owner(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100691 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700692
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200693 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700694 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000695 /*
696 * The waiter became the new top (highest priority)
697 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700698 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000699 * and adjust the priority of the owner.
700 */
701 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100702 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100703 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700704
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000705 } else if (prerequeue_top_waiter == waiter) {
706 /*
707 * The waiter was the top waiter on the lock, but is
Ingo Molnare2db7592021-03-22 02:35:05 +0100708 * no longer the top priority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700709 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000710 * (highest priority) waiter and adjust the priority
711 * of the owner.
712 * The new top waiter is stored in @waiter so that
713 * @waiter == @top_waiter evaluates to true below and
714 * we continue to deboost the rest of the chain.
715 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100716 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700717 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100718 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100719 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000720 } else {
721 /*
722 * Nothing changed. No need to do any priority
723 * adjustment.
724 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700725 }
726
Thomas Gleixner82084982014-06-05 11:16:12 +0200727 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200728 * [12] check_exit_conditions_4() protected by task->pi_lock
729 * and lock->wait_lock. The actual decisions are made after we
730 * dropped the locks.
731 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200732 * Check whether the task which owns the current lock is pi
733 * blocked itself. If yes we store a pointer to the lock for
734 * the lock chain change detection above. After we dropped
735 * task->pi_lock next_lock cannot be dereferenced anymore.
736 */
737 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000738 /*
739 * Store the top waiter of @lock for the end of chain walk
740 * decision below.
741 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700742 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200743
744 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100745 raw_spin_unlock(&task->pi_lock);
746 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700747
Thomas Gleixner82084982014-06-05 11:16:12 +0200748 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200749 * Make the actual exit decisions [12], based on the stored
750 * values.
751 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200752 * We reached the end of the lock chain. Stop right here. No
753 * point to go back just to figure that out.
754 */
755 if (!next_lock)
756 goto out_put_task;
757
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000758 /*
759 * If the current waiter is not the top waiter on the lock,
760 * then we can stop the chain walk here if we are not in full
761 * deadlock detection mode.
762 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700763 if (!detect_deadlock && waiter != top_waiter)
764 goto out_put_task;
765
766 goto again;
767
768 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100769 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700770 out_put_task:
771 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700772
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700773 return ret;
774}
775
776/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700777 * Try to take an rt-mutex
778 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100779 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800780 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200781 * @lock: The lock to be acquired.
782 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700783 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200784 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700785 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800786static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200787 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700788{
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100789 lockdep_assert_held(&lock->wait_lock);
790
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700791 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200792 * Before testing whether we can acquire @lock, we set the
793 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
794 * other tasks which try to modify @lock into the slow path
795 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700796 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200797 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
798 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700799 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200800 * - There is a lock owner. The caller must fixup the
801 * transient state if it does a trylock or leaves the lock
802 * function due to a signal or timeout.
803 *
804 * - @task acquires the lock and there are no other
805 * waiters. This is undone in rt_mutex_set_owner(@task) at
806 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700807 */
808 mark_rt_mutex_waiters(lock);
809
Thomas Gleixner358c3312014-06-11 01:01:13 +0200810 /*
811 * If @lock has an owner, give up.
812 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800813 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700814 return 0;
815
Lai Jiangshan81612392011-01-14 17:09:41 +0800816 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200817 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700818 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200819 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800820 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200821 if (waiter) {
822 /*
823 * If waiter is not the highest priority waiter of
824 * @lock, give up.
825 */
826 if (waiter != rt_mutex_top_waiter(lock))
827 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800828
829 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200830 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700831 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200832 */
833 rt_mutex_dequeue(lock, waiter);
834
835 } else {
836 /*
837 * If the lock has waiters already we check whether @task is
838 * eligible to take over the lock.
839 *
840 * If there are no other waiters, @task can acquire
841 * the lock. @task->pi_blocked_on is NULL, so it does
842 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800843 */
844 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200845 /*
846 * If @task->prio is greater than or equal to
847 * the top waiter priority (kernel view),
848 * @task lost.
849 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100850 if (!rt_mutex_waiter_less(task_to_waiter(task),
851 rt_mutex_top_waiter(lock)))
Thomas Gleixner358c3312014-06-11 01:01:13 +0200852 return 0;
853
854 /*
855 * The current top waiter stays enqueued. We
856 * don't have to change anything in the lock
857 * waiters order.
858 */
859 } else {
860 /*
861 * No waiters. Take the lock without the
862 * pi_lock dance.@task->pi_blocked_on is NULL
863 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700864 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200865 */
866 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800867 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800868 }
869
Thomas Gleixner358c3312014-06-11 01:01:13 +0200870 /*
871 * Clear @task->pi_blocked_on. Requires protection by
872 * @task->pi_lock. Redundant operation for the @waiter == NULL
873 * case, but conditionals are more expensive than a redundant
874 * store.
875 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100876 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200877 task->pi_blocked_on = NULL;
878 /*
879 * Finish the lock acquisition. @task is the new owner. If
880 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700881 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200882 */
883 if (rt_mutex_has_waiters(lock))
884 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100885 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200886
887takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700888 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700889 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700890
Thomas Gleixner358c3312014-06-11 01:01:13 +0200891 /*
892 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
893 * are still waiters or clears it.
894 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800895 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700896
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700897 return 1;
898}
899
900/*
901 * Task blocks on lock.
902 *
903 * Prepare waiter and propagate pi chain
904 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100905 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700906 */
907static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
908 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700909 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000910 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700911{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700912 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700913 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200914 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700915 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700916
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100917 lockdep_assert_held(&lock->wait_lock);
918
Thomas Gleixner397335f2014-05-22 03:25:39 +0000919 /*
920 * Early deadlock detection. We really don't want the task to
921 * enqueue on itself just to untangle the mess later. It's not
922 * only an optimization. We drop the locks, so another waiter
923 * can come in before the chain walk detects the deadlock. So
924 * the other will detect the deadlock and return -EDEADLOCK,
925 * which is wrong, as the other waiter is not in a deadlock
926 * situation.
927 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200928 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000929 return -EDEADLK;
930
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100931 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700932 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700933 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100934 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100935 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700936
937 /* Get the top priority waiter on the lock */
938 if (rt_mutex_has_waiters(lock))
939 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100940 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700941
Darren Hart8dac4562009-04-03 13:40:12 -0700942 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700943
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100944 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700945
Lai Jiangshan81612392011-01-14 17:09:41 +0800946 if (!owner)
947 return 0;
948
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100949 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700950 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100951 rt_mutex_dequeue_pi(owner, top_waiter);
952 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700953
Peter Zijlstraacd58622017-03-23 15:56:11 +0100954 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700955 if (owner->pi_blocked_on)
956 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000957 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700958 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200959 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700960
Thomas Gleixner82084982014-06-05 11:16:12 +0200961 /* Store the lock on which owner is blocked or NULL */
962 next_lock = task_blocked_on_lock(owner);
963
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100964 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200965 /*
966 * Even if full deadlock detection is on, if the owner is not
967 * blocked itself, we can avoid finding this out in the chain
968 * walk.
969 */
970 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700971 return 0;
972
Steven Rostedtdb630632006-09-29 01:59:44 -0700973 /*
974 * The owner can't disappear while holding a lock,
975 * so the owner struct is protected by wait_lock.
976 * Gets dropped in rt_mutex_adjust_prio_chain()!
977 */
978 get_task_struct(owner);
979
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100980 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700981
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000982 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200983 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700984
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100985 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700986
987 return res;
988}
989
990/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700991 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700992 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700993 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100994 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700995 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700996static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
997 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700998{
999 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001000
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001001 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001002
1003 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001004
1005 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +01001006 * Remove it from current->pi_waiters and deboost.
1007 *
1008 * We must in fact deboost here in order to ensure we call
1009 * rt_mutex_setprio() to update p->pi_top_task before the
1010 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001011 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001012 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001013 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001014
Thomas Gleixner27e35712014-06-11 18:44:04 +00001015 /*
1016 * As we are waking up the top waiter, and the waiter stays
1017 * queued on the lock until it gets the lock, this lock
1018 * obviously has waiters. Just set the bit here and this has
1019 * the added benefit of forcing all new tasks into the
1020 * slow path making sure no task of lower priority than
1021 * the top waiter can steal this lock.
1022 */
1023 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001024
Peter Zijlstraacd58622017-03-23 15:56:11 +01001025 /*
1026 * We deboosted before waking the top waiter task such that we don't
1027 * run two tasks with the 'same' priority (and ensure the
1028 * p->pi_top_task pointer points to a blocked task). This however can
1029 * lead to priority inversion if we would get preempted after the
1030 * deboost but before waking our donor task, hence the preempt_disable()
1031 * before unlock.
1032 *
1033 * Pairs with preempt_enable() in rt_mutex_postunlock();
1034 */
1035 preempt_disable();
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001036 wake_q_add(wake_q, waiter->task);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001037 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001038}
1039
1040/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001041 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001042 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001043 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001044 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001045 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001046static void remove_waiter(struct rt_mutex *lock,
1047 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001048{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001049 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001050 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001051 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001052
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +01001053 lockdep_assert_held(&lock->wait_lock);
1054
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001055 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001056 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001057 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001058 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001059
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001060 /*
1061 * Only update priority if the waiter was the highest priority
1062 * waiter of the lock and there is an owner to update.
1063 */
1064 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001065 return;
1066
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001067 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001068
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001069 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001070
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001071 if (rt_mutex_has_waiters(lock))
1072 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001073
Peter Zijlstraacd58622017-03-23 15:56:11 +01001074 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001075
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001076 /* Store the lock on which owner is blocked or NULL */
1077 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001078
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001079 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001080
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001081 /*
1082 * Don't walk the chain, if the owner task is not blocked
1083 * itself.
1084 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001085 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001086 return;
1087
Steven Rostedtdb630632006-09-29 01:59:44 -07001088 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1089 get_task_struct(owner);
1090
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001091 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001092
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001093 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1094 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001095
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001096 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001097}
1098
1099/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001100 * Recheck the pi chain, in case we got a priority setting
1101 *
1102 * Called from sched_setscheduler
1103 */
1104void rt_mutex_adjust_pi(struct task_struct *task)
1105{
1106 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001107 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001108 unsigned long flags;
1109
Thomas Gleixner1d615482009-11-17 14:54:03 +01001110 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001111
1112 waiter = task->pi_blocked_on;
Peter Zijlstra19830e52017-03-23 15:56:14 +01001113 if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001114 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001115 return;
1116 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001117 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001118 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001119
Steven Rostedtdb630632006-09-29 01:59:44 -07001120 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1121 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001122
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001123 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1124 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001125}
1126
Peter Zijlstra50809352017-03-22 11:35:56 +01001127void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1128{
1129 debug_rt_mutex_init_waiter(waiter);
1130 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1131 RB_CLEAR_NODE(&waiter->tree_entry);
1132 waiter->task = NULL;
1133}
1134
Darren Hart8dac4562009-04-03 13:40:12 -07001135/**
1136 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1137 * @lock: the rt_mutex to take
1138 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001139 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001140 * @timeout: the pre-initialized and started timer, or NULL for none
1141 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001142 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001143 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001144 */
1145static int __sched
1146__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1147 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001148 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001149{
1150 int ret = 0;
1151
1152 for (;;) {
1153 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001154 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001155 break;
1156
1157 /*
1158 * TASK_INTERRUPTIBLE checks for signals and
1159 * timeout. Ignored otherwise.
1160 */
Steven Rostedt (VMware)4009f4b2017-01-19 11:32:34 -05001161 if (likely(state == TASK_INTERRUPTIBLE)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001162 /* Signal pending? */
1163 if (signal_pending(current))
1164 ret = -EINTR;
1165 if (timeout && !timeout->task)
1166 ret = -ETIMEDOUT;
1167 if (ret)
1168 break;
1169 }
1170
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001171 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001172
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001173 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001174
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001175 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001176 set_current_state(state);
1177 }
1178
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001179 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001180 return ret;
1181}
1182
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001183static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1184 struct rt_mutex_waiter *w)
1185{
1186 /*
1187 * If the result is not -EDEADLOCK or the caller requested
1188 * deadlock detection, nothing to do here.
1189 */
1190 if (res != -EDEADLOCK || detect_deadlock)
1191 return;
1192
1193 /*
Ingo Molnare2db7592021-03-22 02:35:05 +01001194 * Yell loudly and stop the task right here.
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001195 */
Sebastian Andrzej Siewior6d41c672021-03-26 16:29:32 +01001196 WARN(1, "rtmutex deadlock detected\n");
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001197 while (1) {
1198 set_current_state(TASK_INTERRUPTIBLE);
1199 schedule();
1200 }
1201}
1202
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001203/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001204 * Slow path lock function:
1205 */
1206static int __sched
1207rt_mutex_slowlock(struct rt_mutex *lock, int state,
1208 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001209 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001210{
1211 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001212 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001213 int ret = 0;
1214
Peter Zijlstra50809352017-03-22 11:35:56 +01001215 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001216
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001217 /*
1218 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1219 * be called in early boot if the cmpxchg() fast path is disabled
1220 * (debug, no architecture support). In this case we will acquire the
1221 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1222 * enable interrupts in that early boot case. So we need to use the
1223 * irqsave/restore variants.
1224 */
1225 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001226
1227 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001228 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001229 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001230 return 0;
1231 }
1232
1233 set_current_state(state);
1234
1235 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001236 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001237 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001238
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001239 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001240
1241 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001242 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001243 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001244
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001245 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001246 __set_current_state(TASK_RUNNING);
Peter Zijlstrac28d62c2018-03-27 14:14:38 +02001247 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001248 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001249 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001250
1251 /*
1252 * try_to_take_rt_mutex() sets the waiter bit
1253 * unconditionally. We might have to fix that up.
1254 */
1255 fixup_rt_mutex_waiters(lock);
1256
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001257 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001258
1259 /* Remove pending timer: */
1260 if (unlikely(timeout))
1261 hrtimer_cancel(&timeout->timer);
1262
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001263 debug_rt_mutex_free_waiter(&waiter);
1264
1265 return ret;
1266}
1267
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001268static inline int __rt_mutex_slowtrylock(struct rt_mutex *lock)
1269{
1270 int ret = try_to_take_rt_mutex(lock, current, NULL);
1271
1272 /*
1273 * try_to_take_rt_mutex() sets the lock waiters bit
1274 * unconditionally. Clean this up.
1275 */
1276 fixup_rt_mutex_waiters(lock);
1277
1278 return ret;
1279}
1280
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001281/*
1282 * Slow path try-lock function:
1283 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001284static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001285{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001286 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001287 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001288
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001289 /*
1290 * If the lock already has an owner we fail to get the lock.
1291 * This can be done without taking the @lock->wait_lock as
1292 * it is only being read, and this is a trylock anyway.
1293 */
1294 if (rt_mutex_owner(lock))
1295 return 0;
1296
1297 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001298 * The mutex has currently no owner. Lock the wait lock and try to
1299 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001300 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001301 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001302
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001303 ret = __rt_mutex_slowtrylock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001304
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001305 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001306
1307 return ret;
1308}
1309
1310/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001311 * Slow path to release a rt-mutex.
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001312 *
1313 * Return whether the current task needs to call rt_mutex_postunlock().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001314 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001315static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1316 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001317{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001318 unsigned long flags;
1319
1320 /* irqsave required to support early boot calls */
1321 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001322
1323 debug_rt_mutex_unlock(lock);
1324
Thomas Gleixner27e35712014-06-11 18:44:04 +00001325 /*
1326 * We must be careful here if the fast path is enabled. If we
1327 * have no waiters queued we cannot set owner to NULL here
1328 * because of:
1329 *
1330 * foo->lock->owner = NULL;
1331 * rtmutex_lock(foo->lock); <- fast path
1332 * free = atomic_dec_and_test(foo->refcnt);
1333 * rtmutex_unlock(foo->lock); <- fast path
1334 * if (free)
1335 * kfree(foo);
1336 * raw_spin_unlock(foo->lock->wait_lock);
1337 *
1338 * So for the fastpath enabled kernel:
1339 *
1340 * Nothing can set the waiters bit as long as we hold
1341 * lock->wait_lock. So we do the following sequence:
1342 *
1343 * owner = rt_mutex_owner(lock);
1344 * clear_rt_mutex_waiters(lock);
1345 * raw_spin_unlock(&lock->wait_lock);
1346 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1347 * return;
1348 * goto retry;
1349 *
1350 * The fastpath disabled variant is simple as all access to
1351 * lock->owner is serialized by lock->wait_lock:
1352 *
1353 * lock->owner = NULL;
1354 * raw_spin_unlock(&lock->wait_lock);
1355 */
1356 while (!rt_mutex_has_waiters(lock)) {
1357 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001358 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001359 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001360 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001361 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001362 }
1363
Thomas Gleixner27e35712014-06-11 18:44:04 +00001364 /*
1365 * The wakeup next waiter path does not suffer from the above
1366 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001367 *
1368 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001369 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001370 mark_wakeup_next_waiter(wake_q, lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001371 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001372
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001373 return true; /* call rt_mutex_postunlock() */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001374}
1375
1376/*
1377 * debug aware fast / slowpath lock,trylock,unlock
1378 *
1379 * The atomic acquire/release ops are compiled away, when either the
1380 * architecture does not support cmpxchg or when debugging is enabled.
1381 */
1382static inline int
1383rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001384 int (*slowfn)(struct rt_mutex *lock, int state,
1385 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001386 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001387{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001388 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001389 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001390
1391 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001392}
1393
1394static inline int
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001395rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001396 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001397{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001398 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001399 return 1;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001400
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001401 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001402}
1403
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001404/*
Randy Dunlapc034f482021-02-25 17:21:10 -08001405 * Performs the wakeup of the top-waiter and re-enables preemption.
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001406 */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001407void rt_mutex_postunlock(struct wake_q_head *wake_q)
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001408{
1409 wake_up_q(wake_q);
1410
1411 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001412 preempt_enable();
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001413}
1414
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001415static inline void
1416rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001417 bool (*slowfn)(struct rt_mutex *lock,
1418 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001419{
Waiman Long194a6b52016-11-17 11:46:38 -05001420 DEFINE_WAKE_Q(wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001421
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001422 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1423 return;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001424
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001425 if (slowfn(lock, &wake_q))
1426 rt_mutex_postunlock(&wake_q);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001427}
1428
Peter Rosin62cedf32018-07-20 10:39:13 +02001429static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
1430{
1431 might_sleep();
1432
1433 mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
1434 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1435}
1436
1437#ifdef CONFIG_DEBUG_LOCK_ALLOC
1438/**
1439 * rt_mutex_lock_nested - lock a rt_mutex
1440 *
1441 * @lock: the rt_mutex to be locked
1442 * @subclass: the lockdep subclass
1443 */
1444void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
1445{
1446 __rt_mutex_lock(lock, subclass);
1447}
1448EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
Peter Rosin62cedf32018-07-20 10:39:13 +02001449
Steven Rostedt (VMware)84818af2018-09-10 21:46:38 -04001450#else /* !CONFIG_DEBUG_LOCK_ALLOC */
1451
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001452/**
1453 * rt_mutex_lock - lock a rt_mutex
1454 *
1455 * @lock: the rt_mutex to be locked
1456 */
1457void __sched rt_mutex_lock(struct rt_mutex *lock)
1458{
Peter Rosin62cedf32018-07-20 10:39:13 +02001459 __rt_mutex_lock(lock, 0);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001460}
1461EXPORT_SYMBOL_GPL(rt_mutex_lock);
Peter Rosin62cedf32018-07-20 10:39:13 +02001462#endif
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001463
1464/**
1465 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1466 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001467 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001468 *
1469 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001470 * 0 on success
1471 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001472 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001473int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001474{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001475 int ret;
1476
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001477 might_sleep();
1478
Peter Zijlstraf5694782016-09-19 12:15:37 +02001479 mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1480 ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1481 if (ret)
Qian Cai5facae42019-09-19 12:09:40 -04001482 mutex_release(&lock->dep_map, _RET_IP_);
Peter Zijlstraf5694782016-09-19 12:15:37 +02001483
1484 return ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001485}
1486EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1487
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001488/*
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001489 * Futex variant, must not use fastpath.
1490 */
1491int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1492{
1493 return rt_mutex_slowtrylock(lock);
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001494}
1495
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001496int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
1497{
1498 return __rt_mutex_slowtrylock(lock);
1499}
1500
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001501/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001502 * rt_mutex_trylock - try to lock a rt_mutex
1503 *
1504 * @lock: the rt_mutex to be locked
1505 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001506 * This function can only be called in thread context. It's safe to
1507 * call it from atomic regions, but not from hard interrupt or soft
1508 * interrupt context.
1509 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001510 * Returns 1 on success and 0 on contention
1511 */
1512int __sched rt_mutex_trylock(struct rt_mutex *lock)
1513{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001514 int ret;
1515
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001516 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001517 return 0;
1518
Peter Zijlstraf5694782016-09-19 12:15:37 +02001519 ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1520 if (ret)
1521 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1522
1523 return ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001524}
1525EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1526
1527/**
1528 * rt_mutex_unlock - unlock a rt_mutex
1529 *
1530 * @lock: the rt_mutex to be unlocked
1531 */
1532void __sched rt_mutex_unlock(struct rt_mutex *lock)
1533{
Qian Cai5facae42019-09-19 12:09:40 -04001534 mutex_release(&lock->dep_map, _RET_IP_);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001535 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1536}
1537EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1538
Luis Henriques23b94b92009-04-29 21:54:51 +01001539/**
Alex Shibf594bf2020-11-13 16:58:11 +08001540 * __rt_mutex_futex_unlock - Futex variant, that since futex variants
1541 * do not use the fast-path, can be simple and will not need to retry.
1542 *
1543 * @lock: The rt_mutex to be unlocked
1544 * @wake_q: The wake queue head from which to get the next lock waiter
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001545 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001546bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1547 struct wake_q_head *wake_q)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001548{
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001549 lockdep_assert_held(&lock->wait_lock);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001550
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001551 debug_rt_mutex_unlock(lock);
1552
1553 if (!rt_mutex_has_waiters(lock)) {
1554 lock->owner = NULL;
1555 return false; /* done */
1556 }
1557
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001558 /*
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001559 * We've already deboosted, mark_wakeup_next_waiter() will
1560 * retain preempt_disabled when we drop the wait_lock, to
1561 * avoid inversion prior to the wakeup. preempt_disable()
1562 * therein pairs with rt_mutex_postunlock().
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001563 */
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001564 mark_wakeup_next_waiter(wake_q, lock);
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001565
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001566 return true; /* call postunlock() */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001567}
1568
1569void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1570{
1571 DEFINE_WAKE_Q(wake_q);
Boqun Feng6b0ef922018-03-09 14:56:28 +08001572 unsigned long flags;
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001573 bool postunlock;
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001574
Boqun Feng6b0ef922018-03-09 14:56:28 +08001575 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001576 postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
Boqun Feng6b0ef922018-03-09 14:56:28 +08001577 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001578
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001579 if (postunlock)
1580 rt_mutex_postunlock(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001581}
1582
1583/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001584 * rt_mutex_destroy - mark a mutex unusable
1585 * @lock: the mutex to be destroyed
1586 *
1587 * This function marks the mutex uninitialized, and any subsequent
1588 * use of the mutex is forbidden. The mutex must not be locked when
1589 * this function is called.
1590 */
1591void rt_mutex_destroy(struct rt_mutex *lock)
1592{
1593 WARN_ON(rt_mutex_is_locked(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001594}
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001595EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1596
1597/**
Alex Shibf594bf2020-11-13 16:58:11 +08001598 * __rt_mutex_init - initialize the rt_mutex
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001599 *
Alex Shibf594bf2020-11-13 16:58:11 +08001600 * @lock: The rt_mutex to be initialized
1601 * @name: The lock name used for debugging
1602 * @key: The lock class key used for debugging
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001603 *
Alex Shibf594bf2020-11-13 16:58:11 +08001604 * Initialize the rt_mutex to unlocked state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001605 *
Alex Shibf594bf2020-11-13 16:58:11 +08001606 * Initializing of a locked rt_mutex is not allowed
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001607 */
Peter Zijlstraf5694782016-09-19 12:15:37 +02001608void __rt_mutex_init(struct rt_mutex *lock, const char *name,
1609 struct lock_class_key *key)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001610{
1611 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001612 raw_spin_lock_init(&lock->wait_lock);
Davidlohr Buesoa23ba902017-09-08 16:15:01 -07001613 lock->waiters = RB_ROOT_CACHED;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001614
Levin, Alexander (Sasha Levin)cde50a62017-06-18 14:06:01 +00001615 if (name && key)
1616 debug_rt_mutex_init(lock, name, key);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001617}
1618EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001619
1620/**
1621 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1622 * proxy owner
1623 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001624 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001625 * @proxy_owner:the task to set as owner
1626 *
1627 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001628 *
1629 * Special API call for PI-futex support. This initializes the rtmutex and
1630 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1631 * possible at this point because the pi_state which contains the rtmutex
1632 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001633 */
1634void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1635 struct task_struct *proxy_owner)
1636{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001637 __rt_mutex_init(lock, NULL, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001638 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001639 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001640}
1641
1642/**
1643 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1644 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001645 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001646 *
1647 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001648 *
1649 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1650 * (debugging) state. Concurrent operations on this rt_mutex are not
1651 * possible because it belongs to the pi_state which is about to be freed
1652 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001653 */
Thomas Gleixner2156ac12021-01-20 11:32:07 +01001654void rt_mutex_proxy_unlock(struct rt_mutex *lock)
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001655{
1656 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001657 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001658}
1659
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001660/**
1661 * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1662 * @lock: the rt_mutex to take
1663 * @waiter: the pre-initialized rt_mutex_waiter
1664 * @task: the task to prepare
1665 *
1666 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1667 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1668 *
1669 * NOTE: does _NOT_ remove the @waiter on failure; must either call
1670 * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
1671 *
1672 * Returns:
1673 * 0 - task blocked on lock
1674 * 1 - acquired the lock for task, caller should wake it up
1675 * <0 - error
1676 *
1677 * Special API call for PI-futex support.
1678 */
Peter Zijlstra56222b22017-03-22 11:36:00 +01001679int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1680 struct rt_mutex_waiter *waiter,
1681 struct task_struct *task)
1682{
1683 int ret;
1684
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001685 lockdep_assert_held(&lock->wait_lock);
1686
Peter Zijlstra56222b22017-03-22 11:36:00 +01001687 if (try_to_take_rt_mutex(lock, task, NULL))
1688 return 1;
1689
1690 /* We enforce deadlock detection for futexes */
1691 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1692 RT_MUTEX_FULL_CHAINWALK);
1693
1694 if (ret && !rt_mutex_owner(lock)) {
1695 /*
1696 * Reset the return value. We might have
1697 * returned with -EDEADLK and the owner
1698 * released the lock while we were walking the
1699 * pi chain. Let the waiter sort it out.
1700 */
1701 ret = 0;
1702 }
1703
Peter Zijlstra56222b22017-03-22 11:36:00 +01001704 return ret;
1705}
1706
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001707/**
Darren Hart8dac4562009-04-03 13:40:12 -07001708 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1709 * @lock: the rt_mutex to take
1710 * @waiter: the pre-initialized rt_mutex_waiter
1711 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001712 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001713 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1714 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1715 *
1716 * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
1717 * on failure.
1718 *
Darren Hart8dac4562009-04-03 13:40:12 -07001719 * Returns:
1720 * 0 - task blocked on lock
1721 * 1 - acquired the lock for task, caller should wake it up
1722 * <0 - error
1723 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001724 * Special API call for PI-futex support.
Darren Hart8dac4562009-04-03 13:40:12 -07001725 */
1726int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1727 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001728 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001729{
1730 int ret;
1731
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001732 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstra56222b22017-03-22 11:36:00 +01001733 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001734 if (unlikely(ret))
1735 remove_waiter(lock, waiter);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001736 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001737
Darren Hart8dac4562009-04-03 13:40:12 -07001738 return ret;
1739}
1740
1741/**
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001742 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
Darren Hart8dac4562009-04-03 13:40:12 -07001743 * @lock: the rt_mutex we were woken on
1744 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001745 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001746 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001747 *
Randy Dunlapc034f482021-02-25 17:21:10 -08001748 * Wait for the lock acquisition started on our behalf by
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001749 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1750 * rt_mutex_cleanup_proxy_lock().
Darren Hart8dac4562009-04-03 13:40:12 -07001751 *
1752 * Returns:
1753 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001754 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001755 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001756 * Special API call for PI-futex support
Darren Hart8dac4562009-04-03 13:40:12 -07001757 */
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001758int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
Darren Hart8dac4562009-04-03 13:40:12 -07001759 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001760 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001761{
1762 int ret;
1763
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001764 raw_spin_lock_irq(&lock->wait_lock);
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001765 /* sleep on the mutex */
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001766 set_current_state(TASK_INTERRUPTIBLE);
Lai Jiangshan81612392011-01-14 17:09:41 +08001767 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001768 /*
1769 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1770 * have to fix that up.
1771 */
1772 fixup_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001773 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001774
Darren Hart8dac4562009-04-03 13:40:12 -07001775 return ret;
1776}
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001777
1778/**
1779 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1780 * @lock: the rt_mutex we were woken on
1781 * @waiter: the pre-initialized rt_mutex_waiter
1782 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001783 * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
1784 * rt_mutex_wait_proxy_lock().
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001785 *
1786 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1787 * in fact still be granted ownership until we're removed. Therefore we can
1788 * find we are in fact the owner and must disregard the
1789 * rt_mutex_wait_proxy_lock() failure.
1790 *
1791 * Returns:
1792 * true - did the cleanup, we done.
1793 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1794 * caller should disregards its return value.
1795 *
1796 * Special API call for PI-futex support
1797 */
1798bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1799 struct rt_mutex_waiter *waiter)
1800{
1801 bool cleanup = false;
1802
1803 raw_spin_lock_irq(&lock->wait_lock);
1804 /*
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001805 * Do an unconditional try-lock, this deals with the lock stealing
1806 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
1807 * sets a NULL owner.
1808 *
1809 * We're not interested in the return value, because the subsequent
1810 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
1811 * we will own the lock and it will have removed the waiter. If we
1812 * failed the trylock, we're still not owner and we need to remove
1813 * ourselves.
1814 */
1815 try_to_take_rt_mutex(lock, current, waiter);
1816 /*
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001817 * Unless we're the owner; we're still enqueued on the wait_list.
1818 * So check if we became owner, if not, take us off the wait_list.
1819 */
1820 if (rt_mutex_owner(lock) != current) {
1821 remove_waiter(lock, waiter);
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001822 cleanup = true;
1823 }
Peter Zijlstracfafcd12017-03-22 11:35:58 +01001824 /*
1825 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1826 * have to fix that up.
1827 */
1828 fixup_rt_mutex_waiters(lock);
1829
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001830 raw_spin_unlock_irq(&lock->wait_lock);
1831
1832 return cleanup;
1833}