blob: 38fbf9fa7f1be649b71aaa4a53091d777849bb38 [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 *
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -070012 * See Documentation/locking/rt-mutex-design.txt 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
60 lock->owner = (struct task_struct *)val;
61}
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_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
145# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
146# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
147
148/*
149 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
150 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
151 * relaxed semantics suffice.
152 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200153static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
154{
155 unsigned long owner, *p = (unsigned long *) &lock->owner;
156
157 do {
158 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700159 } while (cmpxchg_relaxed(p, owner,
160 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200161}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000162
163/*
164 * Safe fastpath aware unlock:
165 * 1) Clear the waiters bit
166 * 2) Drop lock->wait_lock
167 * 3) Try to unlock the lock with cmpxchg
168 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100169static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
170 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000171 __releases(lock->wait_lock)
172{
173 struct task_struct *owner = rt_mutex_owner(lock);
174
175 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100176 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000177 /*
178 * If a new waiter comes in between the unlock and the cmpxchg
179 * we have two situations:
180 *
181 * unlock(wait_lock);
182 * lock(wait_lock);
183 * cmpxchg(p, owner, 0) == owner
184 * mark_rt_mutex_waiters(lock);
185 * acquire(lock);
186 * or:
187 *
188 * unlock(wait_lock);
189 * lock(wait_lock);
190 * mark_rt_mutex_waiters(lock);
191 *
192 * cmpxchg(p, owner, 0) != owner
193 * enqueue_waiter();
194 * unlock(wait_lock);
195 * lock(wait_lock);
196 * wake waiter();
197 * unlock(wait_lock);
198 * lock(wait_lock);
199 * acquire(lock);
200 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700201 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000202}
203
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200204#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700205# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
206# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
207# define rt_mutex_cmpxchg_release(l,c,n) (0)
208
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200209static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
210{
211 lock->owner = (struct task_struct *)
212 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
213}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000214
215/*
216 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
217 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100218static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
219 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000220 __releases(lock->wait_lock)
221{
222 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100223 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000224 return true;
225}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200226#endif
227
Peter Zijlstra19830e52017-03-23 15:56:14 +0100228/*
229 * Only use with rt_mutex_waiter_{less,equal}()
230 */
231#define task_to_waiter(p) \
232 &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
233
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100234static inline int
235rt_mutex_waiter_less(struct rt_mutex_waiter *left,
236 struct rt_mutex_waiter *right)
237{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100238 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100239 return 1;
240
241 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100242 * If both waiters have dl_prio(), we check the deadlines of the
243 * associated tasks.
244 * If left waiter has a dl_prio(), and we didn't return 1 above,
245 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100246 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100247 if (dl_prio(left->prio))
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100248 return dl_time_before(left->deadline, right->deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100249
250 return 0;
251}
252
Peter Zijlstra19830e52017-03-23 15:56:14 +0100253static inline int
254rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
255 struct rt_mutex_waiter *right)
256{
257 if (left->prio != right->prio)
258 return 0;
259
260 /*
261 * If both waiters have dl_prio(), we check the deadlines of the
262 * associated tasks.
263 * If left waiter has a dl_prio(), and we didn't return 0 above,
264 * then right waiter has a dl_prio() too.
265 */
266 if (dl_prio(left->prio))
267 return left->deadline == right->deadline;
268
269 return 1;
270}
271
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100272static void
273rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
274{
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700275 struct rb_node **link = &lock->waiters.rb_root.rb_node;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100276 struct rb_node *parent = NULL;
277 struct rt_mutex_waiter *entry;
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700278 bool leftmost = true;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100279
280 while (*link) {
281 parent = *link;
282 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
283 if (rt_mutex_waiter_less(waiter, entry)) {
284 link = &parent->rb_left;
285 } else {
286 link = &parent->rb_right;
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700287 leftmost = false;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100288 }
289 }
290
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100291 rb_link_node(&waiter->tree_entry, parent, link);
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700292 rb_insert_color_cached(&waiter->tree_entry, &lock->waiters, leftmost);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100293}
294
295static void
296rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
297{
298 if (RB_EMPTY_NODE(&waiter->tree_entry))
299 return;
300
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700301 rb_erase_cached(&waiter->tree_entry, &lock->waiters);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100302 RB_CLEAR_NODE(&waiter->tree_entry);
303}
304
305static void
306rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
307{
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700308 struct rb_node **link = &task->pi_waiters.rb_root.rb_node;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100309 struct rb_node *parent = NULL;
310 struct rt_mutex_waiter *entry;
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700311 bool leftmost = true;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100312
313 while (*link) {
314 parent = *link;
315 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
316 if (rt_mutex_waiter_less(waiter, entry)) {
317 link = &parent->rb_left;
318 } else {
319 link = &parent->rb_right;
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700320 leftmost = false;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100321 }
322 }
323
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100324 rb_link_node(&waiter->pi_tree_entry, parent, link);
Davidlohr Buesoa23ba902017-09-08 16:15:01 -0700325 rb_insert_color_cached(&waiter->pi_tree_entry, &task->pi_waiters, leftmost);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100326}
327
328static void
329rt_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
Peter Zijlstraacd58622017-03-23 15:56:11 +0100338static 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
350/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000351 * Deadlock detection is conditional:
352 *
353 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
354 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
355 *
356 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
357 * conducted independent of the detect argument.
358 *
359 * If the waiter argument is NULL this indicates the deboost path and
360 * deadlock detection is disabled independent of the detect argument
361 * and the config settings.
362 */
363static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
364 enum rtmutex_chainwalk chwalk)
365{
366 /*
367 * This is just a wrapper function for the following call,
368 * because debug_rt_mutex_detect_deadlock() smells like a magic
369 * debug feature and I wanted to keep the cond function in the
370 * main source file along with the comments instead of having
371 * two of the same in the headers.
372 */
373 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
374}
375
376/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700377 * Max number of times we'll walk the boosting chain:
378 */
379int max_lock_depth = 1024;
380
Thomas Gleixner82084982014-06-05 11:16:12 +0200381static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
382{
383 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
384}
385
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700386/*
387 * Adjust the priority chain. Also used for deadlock detection.
388 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200389 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200390 * @task: the task owning the mutex (owner) for which a chain walk is
391 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900392 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200393 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
394 * things for a task that has just got its priority adjusted, and
395 * is waiting on a mutex)
396 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
397 * we dropped its pi_lock. Is never dereferenced, only used for
398 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200399 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200400 * its priority to the mutex owner (can be NULL in the case
401 * depicted above or if the top waiter is gone away and we are
402 * actually deboosting the owner)
403 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200404 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700405 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200406 *
407 * Chain walk basics and protection scope
408 *
409 * [R] refcount on task
410 * [P] task->pi_lock held
411 * [L] rtmutex->wait_lock held
412 *
413 * Step Description Protected by
414 * function arguments:
415 * @task [R]
416 * @orig_lock if != NULL @top_task is blocked on it
417 * @next_lock Unprotected. Cannot be
418 * dereferenced. Only used for
419 * comparison.
420 * @orig_waiter if != NULL @top_task is blocked on it
421 * @top_task current, or in case of proxy
422 * locking protected by calling
423 * code
424 * again:
425 * loop_sanity_check();
426 * retry:
427 * [1] lock(task->pi_lock); [R] acquire [P]
428 * [2] waiter = task->pi_blocked_on; [P]
429 * [3] check_exit_conditions_1(); [P]
430 * [4] lock = waiter->lock; [P]
431 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
432 * unlock(task->pi_lock); release [P]
433 * goto retry;
434 * }
435 * [6] check_exit_conditions_2(); [P] + [L]
436 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
437 * [8] unlock(task->pi_lock); release [P]
438 * put_task_struct(task); release [R]
439 * [9] check_exit_conditions_3(); [L]
440 * [10] task = owner(lock); [L]
441 * get_task_struct(task); [L] acquire [R]
442 * lock(task->pi_lock); [L] acquire [P]
443 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
444 * [12] check_exit_conditions_4(); [P] + [L]
445 * [13] unlock(task->pi_lock); release [P]
446 * unlock(lock->wait_lock); release [L]
447 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700448 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200449static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000450 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200451 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200452 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200453 struct rt_mutex_waiter *orig_waiter,
454 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700455{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700456 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000457 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000458 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000459 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000460 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000461 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700462
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000463 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700464
465 /*
466 * The (de)boosting is a step by step approach with a lot of
467 * pitfalls. We want this to be preemptible and we want hold a
468 * maximum of two locks per step. So we have to check
469 * carefully whether things change under us.
470 */
471 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200472 /*
473 * We limit the lock chain length for each invocation.
474 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700475 if (++depth > max_lock_depth) {
476 static int prev_max;
477
478 /*
479 * Print this only once. If the admin changes the limit,
480 * print a new message when reaching the limit again.
481 */
482 if (prev_max != max_lock_depth) {
483 prev_max = max_lock_depth;
484 printk(KERN_WARNING "Maximum lock depth %d reached "
485 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700486 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700487 }
488 put_task_struct(task);
489
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200490 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700491 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200492
493 /*
494 * We are fully preemptible here and only hold the refcount on
495 * @task. So everything can have changed under us since the
496 * caller or our own code below (goto retry/again) dropped all
497 * locks.
498 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700499 retry:
500 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200501 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700502 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100503 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700504
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200505 /*
506 * [2] Get the waiter on which @task is blocked on.
507 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700508 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200509
510 /*
511 * [3] check_exit_conditions_1() protected by task->pi_lock.
512 */
513
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700514 /*
515 * Check whether the end of the boosting chain has been
516 * reached or the state of the chain has changed while we
517 * dropped the locks.
518 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800519 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700520 goto out_unlock_pi;
521
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700522 /*
523 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800524 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700525 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800526 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700527 goto out_unlock_pi;
528
529 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200530 * We dropped all locks after taking a refcount on @task, so
531 * the task might have moved on in the lock chain or even left
532 * the chain completely and blocks now on an unrelated lock or
533 * on @orig_lock.
534 *
535 * We stored the lock on which @task was blocked in @next_lock,
536 * so we can detect the chain change.
537 */
538 if (next_lock != waiter->lock)
539 goto out_unlock_pi;
540
541 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700542 * Drop out, when the task has no waiters. Note,
543 * top_waiter can be NULL, when we are in the deboosting
544 * mode!
545 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000546 if (top_waiter) {
547 if (!task_has_pi_waiters(task))
548 goto out_unlock_pi;
549 /*
550 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000551 * are not the top pi waiter of the task. If deadlock
552 * detection is enabled we continue, but stop the
553 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000554 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000555 if (top_waiter != task_top_pi_waiter(task)) {
556 if (!detect_deadlock)
557 goto out_unlock_pi;
558 else
559 requeue = false;
560 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000561 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700562
563 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000564 * If the waiter priority is the same as the task priority
565 * then there is no further priority adjustment necessary. If
566 * deadlock detection is off, we stop the chain walk. If its
567 * enabled we continue, but stop the requeueing in the chain
568 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700569 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100570 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000571 if (!detect_deadlock)
572 goto out_unlock_pi;
573 else
574 requeue = false;
575 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700576
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200577 /*
578 * [4] Get the next lock
579 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700580 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200581 /*
582 * [5] We need to trylock here as we are holding task->pi_lock,
583 * which is the reverse lock order versus the other rtmutex
584 * operations.
585 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100586 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100587 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700588 cpu_relax();
589 goto retry;
590 }
591
Thomas Gleixner397335f2014-05-22 03:25:39 +0000592 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200593 * [6] check_exit_conditions_2() protected by task->pi_lock and
594 * lock->wait_lock.
595 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000596 * Deadlock detection. If the lock is the same as the original
597 * lock which caused us to walk the lock chain or if the
598 * current lock is owned by the task which initiated the chain
599 * walk, we detected a deadlock.
600 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700601 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000602 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100603 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200604 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700605 goto out_unlock_pi;
606 }
607
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000608 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000609 * If we just follow the lock chain for deadlock detection, no
610 * need to do all the requeue operations. To avoid a truckload
611 * of conditionals around the various places below, just do the
612 * minimum chain walk checks.
613 */
614 if (!requeue) {
615 /*
616 * No requeue[7] here. Just release @task [8]
617 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100618 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000619 put_task_struct(task);
620
621 /*
622 * [9] check_exit_conditions_3 protected by lock->wait_lock.
623 * If there is no owner of the lock, end of chain.
624 */
625 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100626 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000627 return 0;
628 }
629
630 /* [10] Grab the next task, i.e. owner of @lock */
631 task = rt_mutex_owner(lock);
632 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100633 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000634
635 /*
636 * No requeue [11] here. We just do deadlock detection.
637 *
638 * [12] Store whether owner is blocked
639 * itself. Decision is made after dropping the locks
640 */
641 next_lock = task_blocked_on_lock(task);
642 /*
643 * Get the top waiter for the next iteration
644 */
645 top_waiter = rt_mutex_top_waiter(lock);
646
647 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100648 raw_spin_unlock(&task->pi_lock);
649 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000650
651 /* If owner is not blocked, end of chain. */
652 if (!next_lock)
653 goto out_put_task;
654 goto again;
655 }
656
657 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000658 * Store the current top waiter before doing the requeue
659 * operation on @lock. We need it for the boost/deboost
660 * decision below.
661 */
662 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700663
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700664 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100665 rt_mutex_dequeue(lock, waiter);
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100666
667 /*
668 * Update the waiter prio fields now that we're dequeued.
669 *
670 * These values can have changed through either:
671 *
672 * sys_sched_set_scheduler() / sys_sched_setattr()
673 *
674 * or
675 *
676 * DL CBS enforcement advancing the effective deadline.
677 *
678 * Even though pi_waiters also uses these fields, and that tree is only
679 * updated in [11], we can do this here, since we hold [L], which
680 * serializes all pi_waiters access and rb_erase() does not care about
681 * the values of the node being removed.
682 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100683 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100684 waiter->deadline = task->dl.deadline;
685
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100686 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700687
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200688 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100689 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200690 put_task_struct(task);
691
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000692 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200693 * [9] check_exit_conditions_3 protected by lock->wait_lock.
694 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000695 * We must abort the chain walk if there is no lock owner even
696 * in the dead lock detection case, as we have nothing to
697 * follow here. This is the end of the chain we are walking.
698 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800699 if (!rt_mutex_owner(lock)) {
700 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200701 * If the requeue [7] above changed the top waiter,
702 * then we need to wake the new top waiter up to try
703 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800704 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000705 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800706 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100707 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200708 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800709 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700710
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200711 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700712 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700713 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100714 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700715
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200716 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700717 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000718 /*
719 * The waiter became the new top (highest priority)
720 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700721 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000722 * and adjust the priority of the owner.
723 */
724 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100725 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100726 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700727
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000728 } else if (prerequeue_top_waiter == waiter) {
729 /*
730 * The waiter was the top waiter on the lock, but is
731 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700732 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000733 * (highest priority) waiter and adjust the priority
734 * of the owner.
735 * The new top waiter is stored in @waiter so that
736 * @waiter == @top_waiter evaluates to true below and
737 * we continue to deboost the rest of the chain.
738 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100739 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700740 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100741 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100742 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000743 } else {
744 /*
745 * Nothing changed. No need to do any priority
746 * adjustment.
747 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700748 }
749
Thomas Gleixner82084982014-06-05 11:16:12 +0200750 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200751 * [12] check_exit_conditions_4() protected by task->pi_lock
752 * and lock->wait_lock. The actual decisions are made after we
753 * dropped the locks.
754 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200755 * Check whether the task which owns the current lock is pi
756 * blocked itself. If yes we store a pointer to the lock for
757 * the lock chain change detection above. After we dropped
758 * task->pi_lock next_lock cannot be dereferenced anymore.
759 */
760 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000761 /*
762 * Store the top waiter of @lock for the end of chain walk
763 * decision below.
764 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700765 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200766
767 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100768 raw_spin_unlock(&task->pi_lock);
769 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700770
Thomas Gleixner82084982014-06-05 11:16:12 +0200771 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200772 * Make the actual exit decisions [12], based on the stored
773 * values.
774 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200775 * We reached the end of the lock chain. Stop right here. No
776 * point to go back just to figure that out.
777 */
778 if (!next_lock)
779 goto out_put_task;
780
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000781 /*
782 * If the current waiter is not the top waiter on the lock,
783 * then we can stop the chain walk here if we are not in full
784 * deadlock detection mode.
785 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700786 if (!detect_deadlock && waiter != top_waiter)
787 goto out_put_task;
788
789 goto again;
790
791 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100792 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700793 out_put_task:
794 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700795
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700796 return ret;
797}
798
799/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700800 * Try to take an rt-mutex
801 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100802 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800803 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200804 * @lock: The lock to be acquired.
805 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700806 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200807 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700808 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800809static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200810 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700811{
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100812 lockdep_assert_held(&lock->wait_lock);
813
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700814 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200815 * Before testing whether we can acquire @lock, we set the
816 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
817 * other tasks which try to modify @lock into the slow path
818 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700819 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200820 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
821 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700822 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200823 * - There is a lock owner. The caller must fixup the
824 * transient state if it does a trylock or leaves the lock
825 * function due to a signal or timeout.
826 *
827 * - @task acquires the lock and there are no other
828 * waiters. This is undone in rt_mutex_set_owner(@task) at
829 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700830 */
831 mark_rt_mutex_waiters(lock);
832
Thomas Gleixner358c3312014-06-11 01:01:13 +0200833 /*
834 * If @lock has an owner, give up.
835 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800836 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700837 return 0;
838
Lai Jiangshan81612392011-01-14 17:09:41 +0800839 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200840 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700841 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200842 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800843 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200844 if (waiter) {
845 /*
846 * If waiter is not the highest priority waiter of
847 * @lock, give up.
848 */
849 if (waiter != rt_mutex_top_waiter(lock))
850 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800851
852 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200853 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700854 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200855 */
856 rt_mutex_dequeue(lock, waiter);
857
858 } else {
859 /*
860 * If the lock has waiters already we check whether @task is
861 * eligible to take over the lock.
862 *
863 * If there are no other waiters, @task can acquire
864 * the lock. @task->pi_blocked_on is NULL, so it does
865 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800866 */
867 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200868 /*
869 * If @task->prio is greater than or equal to
870 * the top waiter priority (kernel view),
871 * @task lost.
872 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100873 if (!rt_mutex_waiter_less(task_to_waiter(task),
874 rt_mutex_top_waiter(lock)))
Thomas Gleixner358c3312014-06-11 01:01:13 +0200875 return 0;
876
877 /*
878 * The current top waiter stays enqueued. We
879 * don't have to change anything in the lock
880 * waiters order.
881 */
882 } else {
883 /*
884 * No waiters. Take the lock without the
885 * pi_lock dance.@task->pi_blocked_on is NULL
886 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700887 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200888 */
889 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800890 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800891 }
892
Thomas Gleixner358c3312014-06-11 01:01:13 +0200893 /*
894 * Clear @task->pi_blocked_on. Requires protection by
895 * @task->pi_lock. Redundant operation for the @waiter == NULL
896 * case, but conditionals are more expensive than a redundant
897 * store.
898 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100899 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200900 task->pi_blocked_on = NULL;
901 /*
902 * Finish the lock acquisition. @task is the new owner. If
903 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700904 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200905 */
906 if (rt_mutex_has_waiters(lock))
907 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100908 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200909
910takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700911 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700912 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700913
Thomas Gleixner358c3312014-06-11 01:01:13 +0200914 /*
915 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
916 * are still waiters or clears it.
917 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800918 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700919
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700920 return 1;
921}
922
923/*
924 * Task blocks on lock.
925 *
926 * Prepare waiter and propagate pi chain
927 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100928 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700929 */
930static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
931 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700932 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000933 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700934{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700935 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700936 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200937 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700938 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700939
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100940 lockdep_assert_held(&lock->wait_lock);
941
Thomas Gleixner397335f2014-05-22 03:25:39 +0000942 /*
943 * Early deadlock detection. We really don't want the task to
944 * enqueue on itself just to untangle the mess later. It's not
945 * only an optimization. We drop the locks, so another waiter
946 * can come in before the chain walk detects the deadlock. So
947 * the other will detect the deadlock and return -EDEADLOCK,
948 * which is wrong, as the other waiter is not in a deadlock
949 * situation.
950 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200951 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000952 return -EDEADLK;
953
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100954 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700955 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700956 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100957 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100958 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700959
960 /* Get the top priority waiter on the lock */
961 if (rt_mutex_has_waiters(lock))
962 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100963 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700964
Darren Hart8dac4562009-04-03 13:40:12 -0700965 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700966
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100967 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700968
Lai Jiangshan81612392011-01-14 17:09:41 +0800969 if (!owner)
970 return 0;
971
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100972 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700973 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100974 rt_mutex_dequeue_pi(owner, top_waiter);
975 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700976
Peter Zijlstraacd58622017-03-23 15:56:11 +0100977 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700978 if (owner->pi_blocked_on)
979 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000980 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700981 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200982 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700983
Thomas Gleixner82084982014-06-05 11:16:12 +0200984 /* Store the lock on which owner is blocked or NULL */
985 next_lock = task_blocked_on_lock(owner);
986
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100987 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200988 /*
989 * Even if full deadlock detection is on, if the owner is not
990 * blocked itself, we can avoid finding this out in the chain
991 * walk.
992 */
993 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700994 return 0;
995
Steven Rostedtdb630632006-09-29 01:59:44 -0700996 /*
997 * The owner can't disappear while holding a lock,
998 * so the owner struct is protected by wait_lock.
999 * Gets dropped in rt_mutex_adjust_prio_chain()!
1000 */
1001 get_task_struct(owner);
1002
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001003 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001004
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001005 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +02001006 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001007
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001008 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001009
1010 return res;
1011}
1012
1013/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001014 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001015 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001016 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001017 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001018 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001019static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1020 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001021{
1022 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001023
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001024 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001025
1026 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001027
1028 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +01001029 * Remove it from current->pi_waiters and deboost.
1030 *
1031 * We must in fact deboost here in order to ensure we call
1032 * rt_mutex_setprio() to update p->pi_top_task before the
1033 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001034 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001035 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001036 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001037
Thomas Gleixner27e35712014-06-11 18:44:04 +00001038 /*
1039 * As we are waking up the top waiter, and the waiter stays
1040 * queued on the lock until it gets the lock, this lock
1041 * obviously has waiters. Just set the bit here and this has
1042 * the added benefit of forcing all new tasks into the
1043 * slow path making sure no task of lower priority than
1044 * the top waiter can steal this lock.
1045 */
1046 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001047
Peter Zijlstraacd58622017-03-23 15:56:11 +01001048 /*
1049 * We deboosted before waking the top waiter task such that we don't
1050 * run two tasks with the 'same' priority (and ensure the
1051 * p->pi_top_task pointer points to a blocked task). This however can
1052 * lead to priority inversion if we would get preempted after the
1053 * deboost but before waking our donor task, hence the preempt_disable()
1054 * before unlock.
1055 *
1056 * Pairs with preempt_enable() in rt_mutex_postunlock();
1057 */
1058 preempt_disable();
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001059 wake_q_add(wake_q, waiter->task);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001060 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001061}
1062
1063/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001064 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001065 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001066 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001067 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001068 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001069static void remove_waiter(struct rt_mutex *lock,
1070 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001071{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001072 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001073 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001074 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001075
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +01001076 lockdep_assert_held(&lock->wait_lock);
1077
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001078 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001079 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001080 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001081 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001082
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001083 /*
1084 * Only update priority if the waiter was the highest priority
1085 * waiter of the lock and there is an owner to update.
1086 */
1087 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001088 return;
1089
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001090 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001091
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001092 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001093
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001094 if (rt_mutex_has_waiters(lock))
1095 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001096
Peter Zijlstraacd58622017-03-23 15:56:11 +01001097 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001098
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001099 /* Store the lock on which owner is blocked or NULL */
1100 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001101
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001102 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001103
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001104 /*
1105 * Don't walk the chain, if the owner task is not blocked
1106 * itself.
1107 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001108 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001109 return;
1110
Steven Rostedtdb630632006-09-29 01:59:44 -07001111 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1112 get_task_struct(owner);
1113
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001114 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001115
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001116 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1117 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001118
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001119 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001120}
1121
1122/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001123 * Recheck the pi chain, in case we got a priority setting
1124 *
1125 * Called from sched_setscheduler
1126 */
1127void rt_mutex_adjust_pi(struct task_struct *task)
1128{
1129 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001130 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001131 unsigned long flags;
1132
Thomas Gleixner1d615482009-11-17 14:54:03 +01001133 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001134
1135 waiter = task->pi_blocked_on;
Peter Zijlstra19830e52017-03-23 15:56:14 +01001136 if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001137 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001138 return;
1139 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001140 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001141 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001142
Steven Rostedtdb630632006-09-29 01:59:44 -07001143 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1144 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001145
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001146 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1147 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001148}
1149
Peter Zijlstra50809352017-03-22 11:35:56 +01001150void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1151{
1152 debug_rt_mutex_init_waiter(waiter);
1153 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1154 RB_CLEAR_NODE(&waiter->tree_entry);
1155 waiter->task = NULL;
1156}
1157
Darren Hart8dac4562009-04-03 13:40:12 -07001158/**
1159 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1160 * @lock: the rt_mutex to take
1161 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001162 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001163 * @timeout: the pre-initialized and started timer, or NULL for none
1164 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001165 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001166 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001167 */
1168static int __sched
1169__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1170 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001171 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001172{
1173 int ret = 0;
1174
1175 for (;;) {
1176 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001177 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001178 break;
1179
1180 /*
1181 * TASK_INTERRUPTIBLE checks for signals and
1182 * timeout. Ignored otherwise.
1183 */
Steven Rostedt (VMware)4009f4b2017-01-19 11:32:34 -05001184 if (likely(state == TASK_INTERRUPTIBLE)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001185 /* Signal pending? */
1186 if (signal_pending(current))
1187 ret = -EINTR;
1188 if (timeout && !timeout->task)
1189 ret = -ETIMEDOUT;
1190 if (ret)
1191 break;
1192 }
1193
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001194 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001195
1196 debug_rt_mutex_print_deadlock(waiter);
1197
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001198 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001199
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001200 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001201 set_current_state(state);
1202 }
1203
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001204 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001205 return ret;
1206}
1207
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001208static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1209 struct rt_mutex_waiter *w)
1210{
1211 /*
1212 * If the result is not -EDEADLOCK or the caller requested
1213 * deadlock detection, nothing to do here.
1214 */
1215 if (res != -EDEADLOCK || detect_deadlock)
1216 return;
1217
1218 /*
1219 * Yell lowdly and stop the task right here.
1220 */
1221 rt_mutex_print_deadlock(w);
1222 while (1) {
1223 set_current_state(TASK_INTERRUPTIBLE);
1224 schedule();
1225 }
1226}
1227
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001228/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001229 * Slow path lock function:
1230 */
1231static int __sched
1232rt_mutex_slowlock(struct rt_mutex *lock, int state,
1233 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001234 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001235{
1236 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001237 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001238 int ret = 0;
1239
Peter Zijlstra50809352017-03-22 11:35:56 +01001240 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001241
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001242 /*
1243 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1244 * be called in early boot if the cmpxchg() fast path is disabled
1245 * (debug, no architecture support). In this case we will acquire the
1246 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1247 * enable interrupts in that early boot case. So we need to use the
1248 * irqsave/restore variants.
1249 */
1250 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001251
1252 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001253 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001254 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001255 return 0;
1256 }
1257
1258 set_current_state(state);
1259
1260 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001261 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001262 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001263
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001264 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001265
1266 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001267 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001268 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001269
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001270 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001271 __set_current_state(TASK_RUNNING);
Peter Zijlstrac28d62c2018-03-27 14:14:38 +02001272 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001273 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001274 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001275
1276 /*
1277 * try_to_take_rt_mutex() sets the waiter bit
1278 * unconditionally. We might have to fix that up.
1279 */
1280 fixup_rt_mutex_waiters(lock);
1281
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001282 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001283
1284 /* Remove pending timer: */
1285 if (unlikely(timeout))
1286 hrtimer_cancel(&timeout->timer);
1287
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001288 debug_rt_mutex_free_waiter(&waiter);
1289
1290 return ret;
1291}
1292
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001293static inline int __rt_mutex_slowtrylock(struct rt_mutex *lock)
1294{
1295 int ret = try_to_take_rt_mutex(lock, current, NULL);
1296
1297 /*
1298 * try_to_take_rt_mutex() sets the lock waiters bit
1299 * unconditionally. Clean this up.
1300 */
1301 fixup_rt_mutex_waiters(lock);
1302
1303 return ret;
1304}
1305
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001306/*
1307 * Slow path try-lock function:
1308 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001309static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001310{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001311 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001312 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001313
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001314 /*
1315 * If the lock already has an owner we fail to get the lock.
1316 * This can be done without taking the @lock->wait_lock as
1317 * it is only being read, and this is a trylock anyway.
1318 */
1319 if (rt_mutex_owner(lock))
1320 return 0;
1321
1322 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001323 * The mutex has currently no owner. Lock the wait lock and try to
1324 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001325 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001326 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001327
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001328 ret = __rt_mutex_slowtrylock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001329
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001330 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001331
1332 return ret;
1333}
1334
1335/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001336 * Slow path to release a rt-mutex.
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001337 *
1338 * Return whether the current task needs to call rt_mutex_postunlock().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001339 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001340static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1341 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001342{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001343 unsigned long flags;
1344
1345 /* irqsave required to support early boot calls */
1346 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001347
1348 debug_rt_mutex_unlock(lock);
1349
Thomas Gleixner27e35712014-06-11 18:44:04 +00001350 /*
1351 * We must be careful here if the fast path is enabled. If we
1352 * have no waiters queued we cannot set owner to NULL here
1353 * because of:
1354 *
1355 * foo->lock->owner = NULL;
1356 * rtmutex_lock(foo->lock); <- fast path
1357 * free = atomic_dec_and_test(foo->refcnt);
1358 * rtmutex_unlock(foo->lock); <- fast path
1359 * if (free)
1360 * kfree(foo);
1361 * raw_spin_unlock(foo->lock->wait_lock);
1362 *
1363 * So for the fastpath enabled kernel:
1364 *
1365 * Nothing can set the waiters bit as long as we hold
1366 * lock->wait_lock. So we do the following sequence:
1367 *
1368 * owner = rt_mutex_owner(lock);
1369 * clear_rt_mutex_waiters(lock);
1370 * raw_spin_unlock(&lock->wait_lock);
1371 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1372 * return;
1373 * goto retry;
1374 *
1375 * The fastpath disabled variant is simple as all access to
1376 * lock->owner is serialized by lock->wait_lock:
1377 *
1378 * lock->owner = NULL;
1379 * raw_spin_unlock(&lock->wait_lock);
1380 */
1381 while (!rt_mutex_has_waiters(lock)) {
1382 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001383 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001384 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001385 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001386 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001387 }
1388
Thomas Gleixner27e35712014-06-11 18:44:04 +00001389 /*
1390 * The wakeup next waiter path does not suffer from the above
1391 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001392 *
1393 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001394 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001395 mark_wakeup_next_waiter(wake_q, lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001396 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001397
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001398 return true; /* call rt_mutex_postunlock() */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001399}
1400
1401/*
1402 * debug aware fast / slowpath lock,trylock,unlock
1403 *
1404 * The atomic acquire/release ops are compiled away, when either the
1405 * architecture does not support cmpxchg or when debugging is enabled.
1406 */
1407static inline int
1408rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001409 int (*slowfn)(struct rt_mutex *lock, int state,
1410 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001411 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001412{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001413 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001414 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001415
1416 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001417}
1418
1419static inline int
1420rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001421 struct hrtimer_sleeper *timeout,
1422 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001423 int (*slowfn)(struct rt_mutex *lock, int state,
1424 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001425 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001426{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001427 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001428 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001429 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001430
1431 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001432}
1433
1434static inline int
1435rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001436 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001437{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001438 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001439 return 1;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001440
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001441 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001442}
1443
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001444/*
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001445 * Performs the wakeup of the the top-waiter and re-enables preemption.
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001446 */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001447void rt_mutex_postunlock(struct wake_q_head *wake_q)
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001448{
1449 wake_up_q(wake_q);
1450
1451 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001452 preempt_enable();
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001453}
1454
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001455static inline void
1456rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001457 bool (*slowfn)(struct rt_mutex *lock,
1458 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001459{
Waiman Long194a6b52016-11-17 11:46:38 -05001460 DEFINE_WAKE_Q(wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001461
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001462 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1463 return;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001464
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001465 if (slowfn(lock, &wake_q))
1466 rt_mutex_postunlock(&wake_q);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001467}
1468
Peter Rosin62cedf32018-07-20 10:39:13 +02001469static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
1470{
1471 might_sleep();
1472
1473 mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
1474 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1475}
1476
1477#ifdef CONFIG_DEBUG_LOCK_ALLOC
1478/**
1479 * rt_mutex_lock_nested - lock a rt_mutex
1480 *
1481 * @lock: the rt_mutex to be locked
1482 * @subclass: the lockdep subclass
1483 */
1484void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
1485{
1486 __rt_mutex_lock(lock, subclass);
1487}
1488EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
Peter Rosin62cedf32018-07-20 10:39:13 +02001489
Steven Rostedt (VMware)84818af2018-09-10 21:46:38 -04001490#else /* !CONFIG_DEBUG_LOCK_ALLOC */
1491
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001492/**
1493 * rt_mutex_lock - lock a rt_mutex
1494 *
1495 * @lock: the rt_mutex to be locked
1496 */
1497void __sched rt_mutex_lock(struct rt_mutex *lock)
1498{
Peter Rosin62cedf32018-07-20 10:39:13 +02001499 __rt_mutex_lock(lock, 0);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001500}
1501EXPORT_SYMBOL_GPL(rt_mutex_lock);
Peter Rosin62cedf32018-07-20 10:39:13 +02001502#endif
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001503
1504/**
1505 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1506 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001507 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001508 *
1509 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001510 * 0 on success
1511 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001512 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001513int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001514{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001515 int ret;
1516
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001517 might_sleep();
1518
Peter Zijlstraf5694782016-09-19 12:15:37 +02001519 mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1520 ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1521 if (ret)
1522 mutex_release(&lock->dep_map, 1, _RET_IP_);
1523
1524 return ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001525}
1526EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1527
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001528/*
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001529 * Futex variant, must not use fastpath.
1530 */
1531int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1532{
1533 return rt_mutex_slowtrylock(lock);
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001534}
1535
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01001536int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
1537{
1538 return __rt_mutex_slowtrylock(lock);
1539}
1540
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001541/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001542 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1543 * the timeout structure is provided
1544 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001545 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001546 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001547 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001548 *
1549 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001550 * 0 on success
1551 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001552 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001553 */
1554int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001555rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001556{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001557 int ret;
1558
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001559 might_sleep();
1560
Peter Zijlstraf5694782016-09-19 12:15:37 +02001561 mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1562 ret = rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001563 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001564 rt_mutex_slowlock);
Peter Zijlstraf5694782016-09-19 12:15:37 +02001565 if (ret)
1566 mutex_release(&lock->dep_map, 1, _RET_IP_);
1567
1568 return ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001569}
1570EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1571
1572/**
1573 * rt_mutex_trylock - try to lock a rt_mutex
1574 *
1575 * @lock: the rt_mutex to be locked
1576 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001577 * This function can only be called in thread context. It's safe to
1578 * call it from atomic regions, but not from hard interrupt or soft
1579 * interrupt context.
1580 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001581 * Returns 1 on success and 0 on contention
1582 */
1583int __sched rt_mutex_trylock(struct rt_mutex *lock)
1584{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001585 int ret;
1586
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001587 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001588 return 0;
1589
Peter Zijlstraf5694782016-09-19 12:15:37 +02001590 ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1591 if (ret)
1592 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1593
1594 return ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001595}
1596EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1597
1598/**
1599 * rt_mutex_unlock - unlock a rt_mutex
1600 *
1601 * @lock: the rt_mutex to be unlocked
1602 */
1603void __sched rt_mutex_unlock(struct rt_mutex *lock)
1604{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001605 mutex_release(&lock->dep_map, 1, _RET_IP_);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001606 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1607}
1608EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1609
Luis Henriques23b94b92009-04-29 21:54:51 +01001610/**
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001611 * Futex variant, that since futex variants do not use the fast-path, can be
1612 * simple and will not need to retry.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001613 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001614bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1615 struct wake_q_head *wake_q)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001616{
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001617 lockdep_assert_held(&lock->wait_lock);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001618
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001619 debug_rt_mutex_unlock(lock);
1620
1621 if (!rt_mutex_has_waiters(lock)) {
1622 lock->owner = NULL;
1623 return false; /* done */
1624 }
1625
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001626 /*
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001627 * We've already deboosted, mark_wakeup_next_waiter() will
1628 * retain preempt_disabled when we drop the wait_lock, to
1629 * avoid inversion prior to the wakeup. preempt_disable()
1630 * therein pairs with rt_mutex_postunlock().
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001631 */
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001632 mark_wakeup_next_waiter(wake_q, lock);
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001633
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001634 return true; /* call postunlock() */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001635}
1636
1637void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1638{
1639 DEFINE_WAKE_Q(wake_q);
Boqun Feng6b0ef922018-03-09 14:56:28 +08001640 unsigned long flags;
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001641 bool postunlock;
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001642
Boqun Feng6b0ef922018-03-09 14:56:28 +08001643 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001644 postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
Boqun Feng6b0ef922018-03-09 14:56:28 +08001645 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001646
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001647 if (postunlock)
1648 rt_mutex_postunlock(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001649}
1650
1651/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001652 * rt_mutex_destroy - mark a mutex unusable
1653 * @lock: the mutex to be destroyed
1654 *
1655 * This function marks the mutex uninitialized, and any subsequent
1656 * use of the mutex is forbidden. The mutex must not be locked when
1657 * this function is called.
1658 */
1659void rt_mutex_destroy(struct rt_mutex *lock)
1660{
1661 WARN_ON(rt_mutex_is_locked(lock));
1662#ifdef CONFIG_DEBUG_RT_MUTEXES
1663 lock->magic = NULL;
1664#endif
1665}
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001666EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1667
1668/**
1669 * __rt_mutex_init - initialize the rt lock
1670 *
1671 * @lock: the rt lock to be initialized
1672 *
1673 * Initialize the rt lock to unlocked state.
1674 *
1675 * Initializing of a locked rt lock is not allowed
1676 */
Peter Zijlstraf5694782016-09-19 12:15:37 +02001677void __rt_mutex_init(struct rt_mutex *lock, const char *name,
1678 struct lock_class_key *key)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001679{
1680 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001681 raw_spin_lock_init(&lock->wait_lock);
Davidlohr Buesoa23ba902017-09-08 16:15:01 -07001682 lock->waiters = RB_ROOT_CACHED;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001683
Levin, Alexander (Sasha Levin)cde50a62017-06-18 14:06:01 +00001684 if (name && key)
1685 debug_rt_mutex_init(lock, name, key);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001686}
1687EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001688
1689/**
1690 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1691 * proxy owner
1692 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001693 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001694 * @proxy_owner:the task to set as owner
1695 *
1696 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001697 *
1698 * Special API call for PI-futex support. This initializes the rtmutex and
1699 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1700 * possible at this point because the pi_state which contains the rtmutex
1701 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001702 */
1703void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1704 struct task_struct *proxy_owner)
1705{
Peter Zijlstraf5694782016-09-19 12:15:37 +02001706 __rt_mutex_init(lock, NULL, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001707 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001708 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001709}
1710
1711/**
1712 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1713 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001714 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001715 *
1716 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001717 *
1718 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1719 * (debugging) state. Concurrent operations on this rt_mutex are not
1720 * possible because it belongs to the pi_state which is about to be freed
1721 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001722 */
1723void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1724 struct task_struct *proxy_owner)
1725{
1726 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001727 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001728}
1729
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001730/**
1731 * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1732 * @lock: the rt_mutex to take
1733 * @waiter: the pre-initialized rt_mutex_waiter
1734 * @task: the task to prepare
1735 *
1736 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1737 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1738 *
1739 * NOTE: does _NOT_ remove the @waiter on failure; must either call
1740 * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
1741 *
1742 * Returns:
1743 * 0 - task blocked on lock
1744 * 1 - acquired the lock for task, caller should wake it up
1745 * <0 - error
1746 *
1747 * Special API call for PI-futex support.
1748 */
Peter Zijlstra56222b22017-03-22 11:36:00 +01001749int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1750 struct rt_mutex_waiter *waiter,
1751 struct task_struct *task)
1752{
1753 int ret;
1754
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001755 lockdep_assert_held(&lock->wait_lock);
1756
Peter Zijlstra56222b22017-03-22 11:36:00 +01001757 if (try_to_take_rt_mutex(lock, task, NULL))
1758 return 1;
1759
1760 /* We enforce deadlock detection for futexes */
1761 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1762 RT_MUTEX_FULL_CHAINWALK);
1763
1764 if (ret && !rt_mutex_owner(lock)) {
1765 /*
1766 * Reset the return value. We might have
1767 * returned with -EDEADLK and the owner
1768 * released the lock while we were walking the
1769 * pi chain. Let the waiter sort it out.
1770 */
1771 ret = 0;
1772 }
1773
Peter Zijlstra56222b22017-03-22 11:36:00 +01001774 debug_rt_mutex_print_deadlock(waiter);
1775
1776 return ret;
1777}
1778
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001779/**
Darren Hart8dac4562009-04-03 13:40:12 -07001780 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1781 * @lock: the rt_mutex to take
1782 * @waiter: the pre-initialized rt_mutex_waiter
1783 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001784 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001785 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1786 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1787 *
1788 * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
1789 * on failure.
1790 *
Darren Hart8dac4562009-04-03 13:40:12 -07001791 * Returns:
1792 * 0 - task blocked on lock
1793 * 1 - acquired the lock for task, caller should wake it up
1794 * <0 - error
1795 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001796 * Special API call for PI-futex support.
Darren Hart8dac4562009-04-03 13:40:12 -07001797 */
1798int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1799 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001800 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001801{
1802 int ret;
1803
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001804 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstra56222b22017-03-22 11:36:00 +01001805 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001806 if (unlikely(ret))
1807 remove_waiter(lock, waiter);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001808 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001809
Darren Hart8dac4562009-04-03 13:40:12 -07001810 return ret;
1811}
1812
1813/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001814 * rt_mutex_next_owner - return the next owner of the lock
1815 *
1816 * @lock: the rt lock query
1817 *
1818 * Returns the next owner of the lock or NULL
1819 *
1820 * Caller has to serialize against other accessors to the lock
1821 * itself.
1822 *
1823 * Special API call for PI-futex support
1824 */
1825struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1826{
1827 if (!rt_mutex_has_waiters(lock))
1828 return NULL;
1829
1830 return rt_mutex_top_waiter(lock)->task;
1831}
Darren Hart8dac4562009-04-03 13:40:12 -07001832
1833/**
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001834 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
Darren Hart8dac4562009-04-03 13:40:12 -07001835 * @lock: the rt_mutex we were woken on
1836 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001837 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001838 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001839 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001840 * Wait for the the lock acquisition started on our behalf by
1841 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1842 * rt_mutex_cleanup_proxy_lock().
Darren Hart8dac4562009-04-03 13:40:12 -07001843 *
1844 * Returns:
1845 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001846 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001847 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001848 * Special API call for PI-futex support
Darren Hart8dac4562009-04-03 13:40:12 -07001849 */
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001850int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
Darren Hart8dac4562009-04-03 13:40:12 -07001851 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001852 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001853{
1854 int ret;
1855
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001856 raw_spin_lock_irq(&lock->wait_lock);
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001857 /* sleep on the mutex */
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001858 set_current_state(TASK_INTERRUPTIBLE);
Lai Jiangshan81612392011-01-14 17:09:41 +08001859 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001860 /*
1861 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1862 * have to fix that up.
1863 */
1864 fixup_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001865 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001866
Darren Hart8dac4562009-04-03 13:40:12 -07001867 return ret;
1868}
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001869
1870/**
1871 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1872 * @lock: the rt_mutex we were woken on
1873 * @waiter: the pre-initialized rt_mutex_waiter
1874 *
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01001875 * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
1876 * rt_mutex_wait_proxy_lock().
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001877 *
1878 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1879 * in fact still be granted ownership until we're removed. Therefore we can
1880 * find we are in fact the owner and must disregard the
1881 * rt_mutex_wait_proxy_lock() failure.
1882 *
1883 * Returns:
1884 * true - did the cleanup, we done.
1885 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1886 * caller should disregards its return value.
1887 *
1888 * Special API call for PI-futex support
1889 */
1890bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1891 struct rt_mutex_waiter *waiter)
1892{
1893 bool cleanup = false;
1894
1895 raw_spin_lock_irq(&lock->wait_lock);
1896 /*
Peter Zijlstra04dc1b22017-05-19 17:48:50 +02001897 * Do an unconditional try-lock, this deals with the lock stealing
1898 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
1899 * sets a NULL owner.
1900 *
1901 * We're not interested in the return value, because the subsequent
1902 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
1903 * we will own the lock and it will have removed the waiter. If we
1904 * failed the trylock, we're still not owner and we need to remove
1905 * ourselves.
1906 */
1907 try_to_take_rt_mutex(lock, current, waiter);
1908 /*
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001909 * Unless we're the owner; we're still enqueued on the wait_list.
1910 * So check if we became owner, if not, take us off the wait_list.
1911 */
1912 if (rt_mutex_owner(lock) != current) {
1913 remove_waiter(lock, waiter);
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001914 cleanup = true;
1915 }
Peter Zijlstracfafcd12017-03-22 11:35:58 +01001916 /*
1917 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1918 * have to fix that up.
1919 */
1920 fixup_rt_mutex_waiters(lock);
1921
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001922 raw_spin_unlock_irq(&lock->wait_lock);
1923
1924 return cleanup;
1925}