blob: 86d4853d7b405c2ed53fcbbe4d42a4b0167802ab [file] [log] [blame]
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001/*
2 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
3 *
4 * started by Ingo Molnar and Thomas Gleixner.
5 *
6 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
8 * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
9 * Copyright (C) 2006 Esben Nielsen
Steven Rostedtd07fe822006-07-30 03:04:03 -070010 *
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -070011 * See Documentation/locking/rt-mutex-design.txt for details.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070012 */
13#include <linux/spinlock.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070015#include <linux/sched.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060016#include <linux/sched/rt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010017#include <linux/sched/deadline.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070018#include <linux/timer.h>
19
20#include "rtmutex_common.h"
21
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070022/*
23 * lock->owner state tracking:
24 *
Lai Jiangshan81612392011-01-14 17:09:41 +080025 * lock->owner holds the task_struct pointer of the owner. Bit 0
26 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070027 *
Lai Jiangshan81612392011-01-14 17:09:41 +080028 * owner bit0
29 * NULL 0 lock is free (fast acquire possible)
30 * NULL 1 lock is free and has waiters and the top waiter
31 * is going to take the lock*
32 * taskpointer 0 lock is held (fast release possible)
33 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070034 *
35 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080036 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070037 *
Lai Jiangshan81612392011-01-14 17:09:41 +080038 * (*) It also can be a transitional state when grabbing the lock
39 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
40 * we need to set the bit0 before looking at the lock, and the owner may be
41 * NULL in this small time, hence this can be a transitional state.
42 *
43 * (**) There is a small time when bit 0 is set but there are no
44 * waiters. This can happen when grabbing the lock in the slow path.
45 * To prevent a cmpxchg of the owner releasing the lock, we need to
46 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070047 */
48
Thomas Gleixnerbd197232007-06-17 21:11:10 +020049static void
Lai Jiangshan81612392011-01-14 17:09:41 +080050rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070051{
Lai Jiangshan81612392011-01-14 17:09:41 +080052 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070053
54 if (rt_mutex_has_waiters(lock))
55 val |= RT_MUTEX_HAS_WAITERS;
56
57 lock->owner = (struct task_struct *)val;
58}
59
60static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
61{
62 lock->owner = (struct task_struct *)
63 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
64}
65
66static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
67{
68 if (!rt_mutex_has_waiters(lock))
69 clear_rt_mutex_waiters(lock);
70}
71
72/*
Thomas Gleixnerbd197232007-06-17 21:11:10 +020073 * We can speed up the acquire/release, if the architecture
74 * supports cmpxchg and if there's no debugging state to be set up
75 */
76#if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
77# define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
78static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
79{
80 unsigned long owner, *p = (unsigned long *) &lock->owner;
81
82 do {
83 owner = *p;
84 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
85}
Thomas Gleixner27e35712014-06-11 18:44:04 +000086
87/*
88 * Safe fastpath aware unlock:
89 * 1) Clear the waiters bit
90 * 2) Drop lock->wait_lock
91 * 3) Try to unlock the lock with cmpxchg
92 */
93static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
94 __releases(lock->wait_lock)
95{
96 struct task_struct *owner = rt_mutex_owner(lock);
97
98 clear_rt_mutex_waiters(lock);
99 raw_spin_unlock(&lock->wait_lock);
100 /*
101 * If a new waiter comes in between the unlock and the cmpxchg
102 * we have two situations:
103 *
104 * unlock(wait_lock);
105 * lock(wait_lock);
106 * cmpxchg(p, owner, 0) == owner
107 * mark_rt_mutex_waiters(lock);
108 * acquire(lock);
109 * or:
110 *
111 * unlock(wait_lock);
112 * lock(wait_lock);
113 * mark_rt_mutex_waiters(lock);
114 *
115 * cmpxchg(p, owner, 0) != owner
116 * enqueue_waiter();
117 * unlock(wait_lock);
118 * lock(wait_lock);
119 * wake waiter();
120 * unlock(wait_lock);
121 * lock(wait_lock);
122 * acquire(lock);
123 */
124 return rt_mutex_cmpxchg(lock, owner, NULL);
125}
126
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200127#else
128# define rt_mutex_cmpxchg(l,c,n) (0)
129static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
130{
131 lock->owner = (struct task_struct *)
132 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
133}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000134
135/*
136 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
137 */
138static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
139 __releases(lock->wait_lock)
140{
141 lock->owner = NULL;
142 raw_spin_unlock(&lock->wait_lock);
143 return true;
144}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200145#endif
146
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100147static inline int
148rt_mutex_waiter_less(struct rt_mutex_waiter *left,
149 struct rt_mutex_waiter *right)
150{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100151 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100152 return 1;
153
154 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100155 * If both waiters have dl_prio(), we check the deadlines of the
156 * associated tasks.
157 * If left waiter has a dl_prio(), and we didn't return 1 above,
158 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100159 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100160 if (dl_prio(left->prio))
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100161 return (left->task->dl.deadline < right->task->dl.deadline);
162
163 return 0;
164}
165
166static void
167rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
168{
169 struct rb_node **link = &lock->waiters.rb_node;
170 struct rb_node *parent = NULL;
171 struct rt_mutex_waiter *entry;
172 int leftmost = 1;
173
174 while (*link) {
175 parent = *link;
176 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
177 if (rt_mutex_waiter_less(waiter, entry)) {
178 link = &parent->rb_left;
179 } else {
180 link = &parent->rb_right;
181 leftmost = 0;
182 }
183 }
184
185 if (leftmost)
186 lock->waiters_leftmost = &waiter->tree_entry;
187
188 rb_link_node(&waiter->tree_entry, parent, link);
189 rb_insert_color(&waiter->tree_entry, &lock->waiters);
190}
191
192static void
193rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
194{
195 if (RB_EMPTY_NODE(&waiter->tree_entry))
196 return;
197
198 if (lock->waiters_leftmost == &waiter->tree_entry)
199 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
200
201 rb_erase(&waiter->tree_entry, &lock->waiters);
202 RB_CLEAR_NODE(&waiter->tree_entry);
203}
204
205static void
206rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
207{
208 struct rb_node **link = &task->pi_waiters.rb_node;
209 struct rb_node *parent = NULL;
210 struct rt_mutex_waiter *entry;
211 int leftmost = 1;
212
213 while (*link) {
214 parent = *link;
215 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
216 if (rt_mutex_waiter_less(waiter, entry)) {
217 link = &parent->rb_left;
218 } else {
219 link = &parent->rb_right;
220 leftmost = 0;
221 }
222 }
223
224 if (leftmost)
225 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
226
227 rb_link_node(&waiter->pi_tree_entry, parent, link);
228 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
229}
230
231static void
232rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
233{
234 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
235 return;
236
237 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
238 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
239
240 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
241 RB_CLEAR_NODE(&waiter->pi_tree_entry);
242}
243
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200244/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100245 * Calculate task priority from the waiter tree priority
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700246 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100247 * Return task->normal_prio when the waiter tree is empty or when
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700248 * the waiter is not allowed to do priority boosting
249 */
250int rt_mutex_getprio(struct task_struct *task)
251{
252 if (likely(!task_has_pi_waiters(task)))
253 return task->normal_prio;
254
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100255 return min(task_top_pi_waiter(task)->prio,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700256 task->normal_prio);
257}
258
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100259struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
260{
261 if (likely(!task_has_pi_waiters(task)))
262 return NULL;
263
264 return task_top_pi_waiter(task)->task;
265}
266
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700267/*
Thomas Gleixner0782e632015-05-05 19:49:49 +0200268 * Called by sched_setscheduler() to get the priority which will be
269 * effective after the change.
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100270 */
Thomas Gleixner0782e632015-05-05 19:49:49 +0200271int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100272{
273 if (!task_has_pi_waiters(task))
Thomas Gleixner0782e632015-05-05 19:49:49 +0200274 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100275
Thomas Gleixner0782e632015-05-05 19:49:49 +0200276 if (task_top_pi_waiter(task)->task->prio <= newprio)
277 return task_top_pi_waiter(task)->task->prio;
278 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100279}
280
281/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700282 * Adjust the priority of a task, after its pi_waiters got modified.
283 *
284 * This can be both boosting and unboosting. task->pi_lock must be held.
285 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200286static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700287{
288 int prio = rt_mutex_getprio(task);
289
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100290 if (task->prio != prio || dl_prio(prio))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700291 rt_mutex_setprio(task, prio);
292}
293
294/*
295 * Adjust task priority (undo boosting). Called from the exit path of
296 * rt_mutex_slowunlock() and rt_mutex_slowlock().
297 *
298 * (Note: We do this outside of the protection of lock->wait_lock to
299 * allow the lock to be taken while or before we readjust the priority
300 * of task. We do not use the spin_xx_mutex() variants here as we are
301 * outside of the debug path.)
302 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +0200303void rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700304{
305 unsigned long flags;
306
Thomas Gleixner1d615482009-11-17 14:54:03 +0100307 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700308 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100309 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700310}
311
312/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000313 * Deadlock detection is conditional:
314 *
315 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
316 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
317 *
318 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
319 * conducted independent of the detect argument.
320 *
321 * If the waiter argument is NULL this indicates the deboost path and
322 * deadlock detection is disabled independent of the detect argument
323 * and the config settings.
324 */
325static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
326 enum rtmutex_chainwalk chwalk)
327{
328 /*
329 * This is just a wrapper function for the following call,
330 * because debug_rt_mutex_detect_deadlock() smells like a magic
331 * debug feature and I wanted to keep the cond function in the
332 * main source file along with the comments instead of having
333 * two of the same in the headers.
334 */
335 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
336}
337
338/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700339 * Max number of times we'll walk the boosting chain:
340 */
341int max_lock_depth = 1024;
342
Thomas Gleixner82084982014-06-05 11:16:12 +0200343static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
344{
345 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
346}
347
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700348/*
349 * Adjust the priority chain. Also used for deadlock detection.
350 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200351 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200352 * @task: the task owning the mutex (owner) for which a chain walk is
353 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900354 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200355 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
356 * things for a task that has just got its priority adjusted, and
357 * is waiting on a mutex)
358 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
359 * we dropped its pi_lock. Is never dereferenced, only used for
360 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200361 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200362 * its priority to the mutex owner (can be NULL in the case
363 * depicted above or if the top waiter is gone away and we are
364 * actually deboosting the owner)
365 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200366 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700367 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200368 *
369 * Chain walk basics and protection scope
370 *
371 * [R] refcount on task
372 * [P] task->pi_lock held
373 * [L] rtmutex->wait_lock held
374 *
375 * Step Description Protected by
376 * function arguments:
377 * @task [R]
378 * @orig_lock if != NULL @top_task is blocked on it
379 * @next_lock Unprotected. Cannot be
380 * dereferenced. Only used for
381 * comparison.
382 * @orig_waiter if != NULL @top_task is blocked on it
383 * @top_task current, or in case of proxy
384 * locking protected by calling
385 * code
386 * again:
387 * loop_sanity_check();
388 * retry:
389 * [1] lock(task->pi_lock); [R] acquire [P]
390 * [2] waiter = task->pi_blocked_on; [P]
391 * [3] check_exit_conditions_1(); [P]
392 * [4] lock = waiter->lock; [P]
393 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
394 * unlock(task->pi_lock); release [P]
395 * goto retry;
396 * }
397 * [6] check_exit_conditions_2(); [P] + [L]
398 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
399 * [8] unlock(task->pi_lock); release [P]
400 * put_task_struct(task); release [R]
401 * [9] check_exit_conditions_3(); [L]
402 * [10] task = owner(lock); [L]
403 * get_task_struct(task); [L] acquire [R]
404 * lock(task->pi_lock); [L] acquire [P]
405 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
406 * [12] check_exit_conditions_4(); [P] + [L]
407 * [13] unlock(task->pi_lock); release [P]
408 * unlock(lock->wait_lock); release [L]
409 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700410 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200411static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000412 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200413 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200414 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200415 struct rt_mutex_waiter *orig_waiter,
416 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700417{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700418 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000419 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000420 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000421 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000422 bool detect_deadlock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700423 unsigned long flags;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000424 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700425
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000426 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700427
428 /*
429 * The (de)boosting is a step by step approach with a lot of
430 * pitfalls. We want this to be preemptible and we want hold a
431 * maximum of two locks per step. So we have to check
432 * carefully whether things change under us.
433 */
434 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200435 /*
436 * We limit the lock chain length for each invocation.
437 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700438 if (++depth > max_lock_depth) {
439 static int prev_max;
440
441 /*
442 * Print this only once. If the admin changes the limit,
443 * print a new message when reaching the limit again.
444 */
445 if (prev_max != max_lock_depth) {
446 prev_max = max_lock_depth;
447 printk(KERN_WARNING "Maximum lock depth %d reached "
448 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700449 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700450 }
451 put_task_struct(task);
452
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200453 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700454 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200455
456 /*
457 * We are fully preemptible here and only hold the refcount on
458 * @task. So everything can have changed under us since the
459 * caller or our own code below (goto retry/again) dropped all
460 * locks.
461 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700462 retry:
463 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200464 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700465 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100466 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700467
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200468 /*
469 * [2] Get the waiter on which @task is blocked on.
470 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700471 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200472
473 /*
474 * [3] check_exit_conditions_1() protected by task->pi_lock.
475 */
476
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700477 /*
478 * Check whether the end of the boosting chain has been
479 * reached or the state of the chain has changed while we
480 * dropped the locks.
481 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800482 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700483 goto out_unlock_pi;
484
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700485 /*
486 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800487 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700488 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800489 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700490 goto out_unlock_pi;
491
492 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200493 * We dropped all locks after taking a refcount on @task, so
494 * the task might have moved on in the lock chain or even left
495 * the chain completely and blocks now on an unrelated lock or
496 * on @orig_lock.
497 *
498 * We stored the lock on which @task was blocked in @next_lock,
499 * so we can detect the chain change.
500 */
501 if (next_lock != waiter->lock)
502 goto out_unlock_pi;
503
504 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700505 * Drop out, when the task has no waiters. Note,
506 * top_waiter can be NULL, when we are in the deboosting
507 * mode!
508 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000509 if (top_waiter) {
510 if (!task_has_pi_waiters(task))
511 goto out_unlock_pi;
512 /*
513 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000514 * are not the top pi waiter of the task. If deadlock
515 * detection is enabled we continue, but stop the
516 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000517 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000518 if (top_waiter != task_top_pi_waiter(task)) {
519 if (!detect_deadlock)
520 goto out_unlock_pi;
521 else
522 requeue = false;
523 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000524 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700525
526 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000527 * If the waiter priority is the same as the task priority
528 * then there is no further priority adjustment necessary. If
529 * deadlock detection is off, we stop the chain walk. If its
530 * enabled we continue, but stop the requeueing in the chain
531 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700532 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000533 if (waiter->prio == task->prio) {
534 if (!detect_deadlock)
535 goto out_unlock_pi;
536 else
537 requeue = false;
538 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700539
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200540 /*
541 * [4] Get the next lock
542 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700543 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200544 /*
545 * [5] We need to trylock here as we are holding task->pi_lock,
546 * which is the reverse lock order versus the other rtmutex
547 * operations.
548 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100549 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100550 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700551 cpu_relax();
552 goto retry;
553 }
554
Thomas Gleixner397335f2014-05-22 03:25:39 +0000555 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200556 * [6] check_exit_conditions_2() protected by task->pi_lock and
557 * lock->wait_lock.
558 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000559 * Deadlock detection. If the lock is the same as the original
560 * lock which caused us to walk the lock chain or if the
561 * current lock is owned by the task which initiated the chain
562 * walk, we detected a deadlock.
563 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700564 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000565 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100566 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200567 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700568 goto out_unlock_pi;
569 }
570
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000571 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000572 * If we just follow the lock chain for deadlock detection, no
573 * need to do all the requeue operations. To avoid a truckload
574 * of conditionals around the various places below, just do the
575 * minimum chain walk checks.
576 */
577 if (!requeue) {
578 /*
579 * No requeue[7] here. Just release @task [8]
580 */
581 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
582 put_task_struct(task);
583
584 /*
585 * [9] check_exit_conditions_3 protected by lock->wait_lock.
586 * If there is no owner of the lock, end of chain.
587 */
588 if (!rt_mutex_owner(lock)) {
589 raw_spin_unlock(&lock->wait_lock);
590 return 0;
591 }
592
593 /* [10] Grab the next task, i.e. owner of @lock */
594 task = rt_mutex_owner(lock);
595 get_task_struct(task);
596 raw_spin_lock_irqsave(&task->pi_lock, flags);
597
598 /*
599 * No requeue [11] here. We just do deadlock detection.
600 *
601 * [12] Store whether owner is blocked
602 * itself. Decision is made after dropping the locks
603 */
604 next_lock = task_blocked_on_lock(task);
605 /*
606 * Get the top waiter for the next iteration
607 */
608 top_waiter = rt_mutex_top_waiter(lock);
609
610 /* [13] Drop locks */
611 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
612 raw_spin_unlock(&lock->wait_lock);
613
614 /* If owner is not blocked, end of chain. */
615 if (!next_lock)
616 goto out_put_task;
617 goto again;
618 }
619
620 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000621 * Store the current top waiter before doing the requeue
622 * operation on @lock. We need it for the boost/deboost
623 * decision below.
624 */
625 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700626
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700627 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100628 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100629 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100630 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700631
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200632 /* [8] Release the task */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100633 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200634 put_task_struct(task);
635
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000636 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200637 * [9] check_exit_conditions_3 protected by lock->wait_lock.
638 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000639 * We must abort the chain walk if there is no lock owner even
640 * in the dead lock detection case, as we have nothing to
641 * follow here. This is the end of the chain we are walking.
642 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800643 if (!rt_mutex_owner(lock)) {
644 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200645 * If the requeue [7] above changed the top waiter,
646 * then we need to wake the new top waiter up to try
647 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800648 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000649 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800650 wake_up_process(rt_mutex_top_waiter(lock)->task);
651 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200652 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800653 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700654
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200655 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700656 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700657 get_task_struct(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100658 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700659
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200660 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700661 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000662 /*
663 * The waiter became the new top (highest priority)
664 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700665 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000666 * and adjust the priority of the owner.
667 */
668 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100669 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700670 __rt_mutex_adjust_prio(task);
671
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000672 } else if (prerequeue_top_waiter == waiter) {
673 /*
674 * The waiter was the top waiter on the lock, but is
675 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700676 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000677 * (highest priority) waiter and adjust the priority
678 * of the owner.
679 * The new top waiter is stored in @waiter so that
680 * @waiter == @top_waiter evaluates to true below and
681 * we continue to deboost the rest of the chain.
682 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100683 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700684 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100685 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700686 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000687 } else {
688 /*
689 * Nothing changed. No need to do any priority
690 * adjustment.
691 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700692 }
693
Thomas Gleixner82084982014-06-05 11:16:12 +0200694 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200695 * [12] check_exit_conditions_4() protected by task->pi_lock
696 * and lock->wait_lock. The actual decisions are made after we
697 * dropped the locks.
698 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200699 * Check whether the task which owns the current lock is pi
700 * blocked itself. If yes we store a pointer to the lock for
701 * the lock chain change detection above. After we dropped
702 * task->pi_lock next_lock cannot be dereferenced anymore.
703 */
704 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000705 /*
706 * Store the top waiter of @lock for the end of chain walk
707 * decision below.
708 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700709 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200710
711 /* [13] Drop the locks */
712 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100713 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700714
Thomas Gleixner82084982014-06-05 11:16:12 +0200715 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200716 * Make the actual exit decisions [12], based on the stored
717 * values.
718 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200719 * We reached the end of the lock chain. Stop right here. No
720 * point to go back just to figure that out.
721 */
722 if (!next_lock)
723 goto out_put_task;
724
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000725 /*
726 * If the current waiter is not the top waiter on the lock,
727 * then we can stop the chain walk here if we are not in full
728 * deadlock detection mode.
729 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700730 if (!detect_deadlock && waiter != top_waiter)
731 goto out_put_task;
732
733 goto again;
734
735 out_unlock_pi:
Thomas Gleixner1d615482009-11-17 14:54:03 +0100736 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700737 out_put_task:
738 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700739
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700740 return ret;
741}
742
743/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700744 * Try to take an rt-mutex
745 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700746 * Must be called with lock->wait_lock held.
Lai Jiangshan81612392011-01-14 17:09:41 +0800747 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200748 * @lock: The lock to be acquired.
749 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700750 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200751 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700752 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800753static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200754 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700755{
Thomas Gleixner358c3312014-06-11 01:01:13 +0200756 unsigned long flags;
757
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700758 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200759 * Before testing whether we can acquire @lock, we set the
760 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
761 * other tasks which try to modify @lock into the slow path
762 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700763 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200764 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
765 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700766 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200767 * - There is a lock owner. The caller must fixup the
768 * transient state if it does a trylock or leaves the lock
769 * function due to a signal or timeout.
770 *
771 * - @task acquires the lock and there are no other
772 * waiters. This is undone in rt_mutex_set_owner(@task) at
773 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700774 */
775 mark_rt_mutex_waiters(lock);
776
Thomas Gleixner358c3312014-06-11 01:01:13 +0200777 /*
778 * If @lock has an owner, give up.
779 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800780 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700781 return 0;
782
Lai Jiangshan81612392011-01-14 17:09:41 +0800783 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200784 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700785 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200786 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800787 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200788 if (waiter) {
789 /*
790 * If waiter is not the highest priority waiter of
791 * @lock, give up.
792 */
793 if (waiter != rt_mutex_top_waiter(lock))
794 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800795
796 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200797 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700798 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200799 */
800 rt_mutex_dequeue(lock, waiter);
801
802 } else {
803 /*
804 * If the lock has waiters already we check whether @task is
805 * eligible to take over the lock.
806 *
807 * If there are no other waiters, @task can acquire
808 * the lock. @task->pi_blocked_on is NULL, so it does
809 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800810 */
811 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200812 /*
813 * If @task->prio is greater than or equal to
814 * the top waiter priority (kernel view),
815 * @task lost.
816 */
817 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
818 return 0;
819
820 /*
821 * The current top waiter stays enqueued. We
822 * don't have to change anything in the lock
823 * waiters order.
824 */
825 } else {
826 /*
827 * No waiters. Take the lock without the
828 * pi_lock dance.@task->pi_blocked_on is NULL
829 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700830 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200831 */
832 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800833 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800834 }
835
Thomas Gleixner358c3312014-06-11 01:01:13 +0200836 /*
837 * Clear @task->pi_blocked_on. Requires protection by
838 * @task->pi_lock. Redundant operation for the @waiter == NULL
839 * case, but conditionals are more expensive than a redundant
840 * store.
841 */
842 raw_spin_lock_irqsave(&task->pi_lock, flags);
843 task->pi_blocked_on = NULL;
844 /*
845 * Finish the lock acquisition. @task is the new owner. If
846 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700847 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200848 */
849 if (rt_mutex_has_waiters(lock))
850 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
851 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
852
853takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700854 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700855 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700856
Thomas Gleixner358c3312014-06-11 01:01:13 +0200857 /*
858 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
859 * are still waiters or clears it.
860 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800861 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700862
Lai Jiangshan81612392011-01-14 17:09:41 +0800863 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700864
865 return 1;
866}
867
868/*
869 * Task blocks on lock.
870 *
871 * Prepare waiter and propagate pi chain
872 *
873 * This must be called with lock->wait_lock held.
874 */
875static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
876 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700877 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000878 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700879{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700880 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700881 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200882 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700883 int chain_walk = 0, res;
Thomas Gleixner82084982014-06-05 11:16:12 +0200884 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700885
Thomas Gleixner397335f2014-05-22 03:25:39 +0000886 /*
887 * Early deadlock detection. We really don't want the task to
888 * enqueue on itself just to untangle the mess later. It's not
889 * only an optimization. We drop the locks, so another waiter
890 * can come in before the chain walk detects the deadlock. So
891 * the other will detect the deadlock and return -EDEADLOCK,
892 * which is wrong, as the other waiter is not in a deadlock
893 * situation.
894 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200895 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000896 return -EDEADLK;
897
Thomas Gleixner1d615482009-11-17 14:54:03 +0100898 raw_spin_lock_irqsave(&task->pi_lock, flags);
Darren Hart8dac4562009-04-03 13:40:12 -0700899 __rt_mutex_adjust_prio(task);
900 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700901 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100902 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700903
904 /* Get the top priority waiter on the lock */
905 if (rt_mutex_has_waiters(lock))
906 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100907 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700908
Darren Hart8dac4562009-04-03 13:40:12 -0700909 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700910
Thomas Gleixner1d615482009-11-17 14:54:03 +0100911 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700912
Lai Jiangshan81612392011-01-14 17:09:41 +0800913 if (!owner)
914 return 0;
915
Thomas Gleixner82084982014-06-05 11:16:12 +0200916 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700917 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100918 rt_mutex_dequeue_pi(owner, top_waiter);
919 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700920
921 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700922 if (owner->pi_blocked_on)
923 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000924 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700925 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200926 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700927
Thomas Gleixner82084982014-06-05 11:16:12 +0200928 /* Store the lock on which owner is blocked or NULL */
929 next_lock = task_blocked_on_lock(owner);
930
931 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
932 /*
933 * Even if full deadlock detection is on, if the owner is not
934 * blocked itself, we can avoid finding this out in the chain
935 * walk.
936 */
937 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700938 return 0;
939
Steven Rostedtdb630632006-09-29 01:59:44 -0700940 /*
941 * The owner can't disappear while holding a lock,
942 * so the owner struct is protected by wait_lock.
943 * Gets dropped in rt_mutex_adjust_prio_chain()!
944 */
945 get_task_struct(owner);
946
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100947 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700948
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000949 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200950 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700951
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100952 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700953
954 return res;
955}
956
957/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700958 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700959 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700960 *
961 * Called with lock->wait_lock held.
962 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700963static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
964 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700965{
966 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700967 unsigned long flags;
968
Thomas Gleixner1d615482009-11-17 14:54:03 +0100969 raw_spin_lock_irqsave(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700970
971 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700972
973 /*
974 * Remove it from current->pi_waiters. We do not adjust a
975 * possible priority boost right now. We execute wakeup in the
976 * boosted mode and go back to normal after releasing
977 * lock->wait_lock.
978 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100979 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700980
Thomas Gleixner27e35712014-06-11 18:44:04 +0000981 /*
982 * As we are waking up the top waiter, and the waiter stays
983 * queued on the lock until it gets the lock, this lock
984 * obviously has waiters. Just set the bit here and this has
985 * the added benefit of forcing all new tasks into the
986 * slow path making sure no task of lower priority than
987 * the top waiter can steal this lock.
988 */
989 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700990
Thomas Gleixner1d615482009-11-17 14:54:03 +0100991 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700992
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700993 wake_q_add(wake_q, waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700994}
995
996/*
Lai Jiangshan81612392011-01-14 17:09:41 +0800997 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700998 *
Lai Jiangshan81612392011-01-14 17:09:41 +0800999 * Must be called with lock->wait_lock held and
1000 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001001 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001002static void remove_waiter(struct rt_mutex *lock,
1003 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001004{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001005 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001006 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001007 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001008 unsigned long flags;
1009
Thomas Gleixner1d615482009-11-17 14:54:03 +01001010 raw_spin_lock_irqsave(&current->pi_lock, flags);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001011 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001012 current->pi_blocked_on = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001013 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001014
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001015 /*
1016 * Only update priority if the waiter was the highest priority
1017 * waiter of the lock and there is an owner to update.
1018 */
1019 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001020 return;
1021
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001022 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001023
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001024 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001025
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001026 if (rt_mutex_has_waiters(lock))
1027 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001028
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001029 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001030
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001031 /* Store the lock on which owner is blocked or NULL */
1032 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001033
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001034 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
Steven Rostedtdb630632006-09-29 01:59:44 -07001035
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001036 /*
1037 * Don't walk the chain, if the owner task is not blocked
1038 * itself.
1039 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001040 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001041 return;
1042
Steven Rostedtdb630632006-09-29 01:59:44 -07001043 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1044 get_task_struct(owner);
1045
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001046 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001047
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001048 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1049 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001050
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001051 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001052}
1053
1054/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001055 * Recheck the pi chain, in case we got a priority setting
1056 *
1057 * Called from sched_setscheduler
1058 */
1059void rt_mutex_adjust_pi(struct task_struct *task)
1060{
1061 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001062 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001063 unsigned long flags;
1064
Thomas Gleixner1d615482009-11-17 14:54:03 +01001065 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001066
1067 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001068 if (!waiter || (waiter->prio == task->prio &&
1069 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001070 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001071 return;
1072 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001073 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001074 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001075
Steven Rostedtdb630632006-09-29 01:59:44 -07001076 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1077 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001078
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001079 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1080 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001081}
1082
Darren Hart8dac4562009-04-03 13:40:12 -07001083/**
1084 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1085 * @lock: the rt_mutex to take
1086 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1087 * or TASK_UNINTERRUPTIBLE)
1088 * @timeout: the pre-initialized and started timer, or NULL for none
1089 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001090 *
1091 * lock->wait_lock must be held by the caller.
1092 */
1093static int __sched
1094__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1095 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001096 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001097{
1098 int ret = 0;
1099
1100 for (;;) {
1101 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001102 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001103 break;
1104
1105 /*
1106 * TASK_INTERRUPTIBLE checks for signals and
1107 * timeout. Ignored otherwise.
1108 */
1109 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1110 /* Signal pending? */
1111 if (signal_pending(current))
1112 ret = -EINTR;
1113 if (timeout && !timeout->task)
1114 ret = -ETIMEDOUT;
1115 if (ret)
1116 break;
1117 }
1118
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001119 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001120
1121 debug_rt_mutex_print_deadlock(waiter);
1122
Lai Jiangshan81612392011-01-14 17:09:41 +08001123 schedule_rt_mutex(lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001124
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001125 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001126 set_current_state(state);
1127 }
1128
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001129 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001130 return ret;
1131}
1132
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001133static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1134 struct rt_mutex_waiter *w)
1135{
1136 /*
1137 * If the result is not -EDEADLOCK or the caller requested
1138 * deadlock detection, nothing to do here.
1139 */
1140 if (res != -EDEADLOCK || detect_deadlock)
1141 return;
1142
1143 /*
1144 * Yell lowdly and stop the task right here.
1145 */
1146 rt_mutex_print_deadlock(w);
1147 while (1) {
1148 set_current_state(TASK_INTERRUPTIBLE);
1149 schedule();
1150 }
1151}
1152
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001153/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001154 * Slow path lock function:
1155 */
1156static int __sched
1157rt_mutex_slowlock(struct rt_mutex *lock, int state,
1158 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001159 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001160{
1161 struct rt_mutex_waiter waiter;
1162 int ret = 0;
1163
1164 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001165 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1166 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001167
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001168 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001169
1170 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001171 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001172 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001173 return 0;
1174 }
1175
1176 set_current_state(state);
1177
1178 /* Setup the timer, when timeout != NULL */
Peter Zijlstra720a2592008-02-13 15:45:36 +01001179 if (unlikely(timeout)) {
Arjan van de Vencc584b22008-09-01 15:02:30 -07001180 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Peter Zijlstra720a2592008-02-13 15:45:36 +01001181 if (!hrtimer_active(&timeout->timer))
1182 timeout->task = NULL;
1183 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001184
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001185 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001186
1187 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001188 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001189 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001190
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001191 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001192 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001193 if (rt_mutex_has_waiters(lock))
1194 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001195 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001196 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001197
1198 /*
1199 * try_to_take_rt_mutex() sets the waiter bit
1200 * unconditionally. We might have to fix that up.
1201 */
1202 fixup_rt_mutex_waiters(lock);
1203
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001204 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001205
1206 /* Remove pending timer: */
1207 if (unlikely(timeout))
1208 hrtimer_cancel(&timeout->timer);
1209
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001210 debug_rt_mutex_free_waiter(&waiter);
1211
1212 return ret;
1213}
1214
1215/*
1216 * Slow path try-lock function:
1217 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001218static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001219{
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001220 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001221
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001222 /*
1223 * If the lock already has an owner we fail to get the lock.
1224 * This can be done without taking the @lock->wait_lock as
1225 * it is only being read, and this is a trylock anyway.
1226 */
1227 if (rt_mutex_owner(lock))
1228 return 0;
1229
1230 /*
1231 * The mutex has currently no owner. Lock the wait lock and
1232 * try to acquire the lock.
1233 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001234 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001235
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001236 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001237
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001238 /*
1239 * try_to_take_rt_mutex() sets the lock waiters bit
1240 * unconditionally. Clean this up.
1241 */
1242 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001243
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001244 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001245
1246 return ret;
1247}
1248
1249/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001250 * Slow path to release a rt-mutex.
1251 * Return whether the current task needs to undo a potential priority boosting.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001252 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001253static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1254 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001255{
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001256 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001257
1258 debug_rt_mutex_unlock(lock);
1259
1260 rt_mutex_deadlock_account_unlock(current);
1261
Thomas Gleixner27e35712014-06-11 18:44:04 +00001262 /*
1263 * We must be careful here if the fast path is enabled. If we
1264 * have no waiters queued we cannot set owner to NULL here
1265 * because of:
1266 *
1267 * foo->lock->owner = NULL;
1268 * rtmutex_lock(foo->lock); <- fast path
1269 * free = atomic_dec_and_test(foo->refcnt);
1270 * rtmutex_unlock(foo->lock); <- fast path
1271 * if (free)
1272 * kfree(foo);
1273 * raw_spin_unlock(foo->lock->wait_lock);
1274 *
1275 * So for the fastpath enabled kernel:
1276 *
1277 * Nothing can set the waiters bit as long as we hold
1278 * lock->wait_lock. So we do the following sequence:
1279 *
1280 * owner = rt_mutex_owner(lock);
1281 * clear_rt_mutex_waiters(lock);
1282 * raw_spin_unlock(&lock->wait_lock);
1283 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1284 * return;
1285 * goto retry;
1286 *
1287 * The fastpath disabled variant is simple as all access to
1288 * lock->owner is serialized by lock->wait_lock:
1289 *
1290 * lock->owner = NULL;
1291 * raw_spin_unlock(&lock->wait_lock);
1292 */
1293 while (!rt_mutex_has_waiters(lock)) {
1294 /* Drops lock->wait_lock ! */
1295 if (unlock_rt_mutex_safe(lock) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001296 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001297 /* Relock the rtmutex and try again */
1298 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001299 }
1300
Thomas Gleixner27e35712014-06-11 18:44:04 +00001301 /*
1302 * The wakeup next waiter path does not suffer from the above
1303 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001304 *
1305 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001306 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001307 mark_wakeup_next_waiter(wake_q, lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001308
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001309 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001310
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001311 /* check PI boosting */
1312 return true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001313}
1314
1315/*
1316 * debug aware fast / slowpath lock,trylock,unlock
1317 *
1318 * The atomic acquire/release ops are compiled away, when either the
1319 * architecture does not support cmpxchg or when debugging is enabled.
1320 */
1321static inline int
1322rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001323 int (*slowfn)(struct rt_mutex *lock, int state,
1324 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001325 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001326{
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001327 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001328 rt_mutex_deadlock_account_lock(lock, current);
1329 return 0;
1330 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001331 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001332}
1333
1334static inline int
1335rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001336 struct hrtimer_sleeper *timeout,
1337 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001338 int (*slowfn)(struct rt_mutex *lock, int state,
1339 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001340 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001341{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001342 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1343 likely(rt_mutex_cmpxchg(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001344 rt_mutex_deadlock_account_lock(lock, current);
1345 return 0;
1346 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001347 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001348}
1349
1350static inline int
1351rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001352 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001353{
1354 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1355 rt_mutex_deadlock_account_lock(lock, current);
1356 return 1;
1357 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001358 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001359}
1360
1361static inline void
1362rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001363 bool (*slowfn)(struct rt_mutex *lock,
1364 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001365{
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001366 WAKE_Q(wake_q);
1367
1368 if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001369 rt_mutex_deadlock_account_unlock(current);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001370
1371 } else {
1372 bool deboost = slowfn(lock, &wake_q);
1373
1374 wake_up_q(&wake_q);
1375
1376 /* Undo pi boosting if necessary: */
1377 if (deboost)
1378 rt_mutex_adjust_prio(current);
1379 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001380}
1381
1382/**
1383 * rt_mutex_lock - lock a rt_mutex
1384 *
1385 * @lock: the rt_mutex to be locked
1386 */
1387void __sched rt_mutex_lock(struct rt_mutex *lock)
1388{
1389 might_sleep();
1390
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001391 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001392}
1393EXPORT_SYMBOL_GPL(rt_mutex_lock);
1394
1395/**
1396 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1397 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001398 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001399 *
1400 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001401 * 0 on success
1402 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001403 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001404int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001405{
1406 might_sleep();
1407
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001408 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001409}
1410EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1411
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001412/*
1413 * Futex variant with full deadlock detection.
1414 */
1415int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1416 struct hrtimer_sleeper *timeout)
1417{
1418 might_sleep();
1419
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001420 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1421 RT_MUTEX_FULL_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001422 rt_mutex_slowlock);
1423}
1424
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001425/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001426 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1427 * the timeout structure is provided
1428 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001429 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001430 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001431 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001432 *
1433 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001434 * 0 on success
1435 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001436 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001437 */
1438int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001439rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001440{
1441 might_sleep();
1442
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001443 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1444 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001445 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001446}
1447EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1448
1449/**
1450 * rt_mutex_trylock - try to lock a rt_mutex
1451 *
1452 * @lock: the rt_mutex to be locked
1453 *
1454 * Returns 1 on success and 0 on contention
1455 */
1456int __sched rt_mutex_trylock(struct rt_mutex *lock)
1457{
1458 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1459}
1460EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1461
1462/**
1463 * rt_mutex_unlock - unlock a rt_mutex
1464 *
1465 * @lock: the rt_mutex to be unlocked
1466 */
1467void __sched rt_mutex_unlock(struct rt_mutex *lock)
1468{
1469 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1470}
1471EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1472
Luis Henriques23b94b92009-04-29 21:54:51 +01001473/**
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001474 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1475 * @lock: the rt_mutex to be unlocked
1476 *
1477 * Returns: true/false indicating whether priority adjustment is
1478 * required or not.
1479 */
1480bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1481 struct wake_q_head *wqh)
1482{
1483 if (likely(rt_mutex_cmpxchg(lock, current, NULL))) {
1484 rt_mutex_deadlock_account_unlock(current);
1485 return false;
1486 }
1487 return rt_mutex_slowunlock(lock, wqh);
1488}
1489
1490/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001491 * rt_mutex_destroy - mark a mutex unusable
1492 * @lock: the mutex to be destroyed
1493 *
1494 * This function marks the mutex uninitialized, and any subsequent
1495 * use of the mutex is forbidden. The mutex must not be locked when
1496 * this function is called.
1497 */
1498void rt_mutex_destroy(struct rt_mutex *lock)
1499{
1500 WARN_ON(rt_mutex_is_locked(lock));
1501#ifdef CONFIG_DEBUG_RT_MUTEXES
1502 lock->magic = NULL;
1503#endif
1504}
1505
1506EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1507
1508/**
1509 * __rt_mutex_init - initialize the rt lock
1510 *
1511 * @lock: the rt lock to be initialized
1512 *
1513 * Initialize the rt lock to unlocked state.
1514 *
1515 * Initializing of a locked rt lock is not allowed
1516 */
1517void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1518{
1519 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001520 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001521 lock->waiters = RB_ROOT;
1522 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001523
1524 debug_rt_mutex_init(lock, name);
1525}
1526EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001527
1528/**
1529 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1530 * proxy owner
1531 *
1532 * @lock: the rt_mutex to be locked
1533 * @proxy_owner:the task to set as owner
1534 *
1535 * No locking. Caller has to do serializing itself
1536 * Special API call for PI-futex support
1537 */
1538void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1539 struct task_struct *proxy_owner)
1540{
1541 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001542 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001543 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001544 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1545}
1546
1547/**
1548 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1549 *
1550 * @lock: the rt_mutex to be locked
1551 *
1552 * No locking. Caller has to do serializing itself
1553 * Special API call for PI-futex support
1554 */
1555void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1556 struct task_struct *proxy_owner)
1557{
1558 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001559 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001560 rt_mutex_deadlock_account_unlock(proxy_owner);
1561}
1562
1563/**
Darren Hart8dac4562009-04-03 13:40:12 -07001564 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1565 * @lock: the rt_mutex to take
1566 * @waiter: the pre-initialized rt_mutex_waiter
1567 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001568 *
1569 * Returns:
1570 * 0 - task blocked on lock
1571 * 1 - acquired the lock for task, caller should wake it up
1572 * <0 - error
1573 *
1574 * Special API call for FUTEX_REQUEUE_PI support.
1575 */
1576int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1577 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001578 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001579{
1580 int ret;
1581
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001582 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001583
Lai Jiangshan81612392011-01-14 17:09:41 +08001584 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001585 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001586 return 1;
1587 }
1588
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001589 /* We enforce deadlock detection for futexes */
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001590 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1591 RT_MUTEX_FULL_CHAINWALK);
Darren Hart8dac4562009-04-03 13:40:12 -07001592
Lai Jiangshan81612392011-01-14 17:09:41 +08001593 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001594 /*
1595 * Reset the return value. We might have
1596 * returned with -EDEADLK and the owner
1597 * released the lock while we were walking the
1598 * pi chain. Let the waiter sort it out.
1599 */
1600 ret = 0;
1601 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001602
1603 if (unlikely(ret))
1604 remove_waiter(lock, waiter);
1605
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001606 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001607
1608 debug_rt_mutex_print_deadlock(waiter);
1609
1610 return ret;
1611}
1612
1613/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001614 * rt_mutex_next_owner - return the next owner of the lock
1615 *
1616 * @lock: the rt lock query
1617 *
1618 * Returns the next owner of the lock or NULL
1619 *
1620 * Caller has to serialize against other accessors to the lock
1621 * itself.
1622 *
1623 * Special API call for PI-futex support
1624 */
1625struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1626{
1627 if (!rt_mutex_has_waiters(lock))
1628 return NULL;
1629
1630 return rt_mutex_top_waiter(lock)->task;
1631}
Darren Hart8dac4562009-04-03 13:40:12 -07001632
1633/**
1634 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1635 * @lock: the rt_mutex we were woken on
1636 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001637 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001638 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001639 *
1640 * Complete the lock acquisition started our behalf by another thread.
1641 *
1642 * Returns:
1643 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001644 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001645 *
1646 * Special API call for PI-futex requeue support
1647 */
1648int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1649 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001650 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001651{
1652 int ret;
1653
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001654 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001655
1656 set_current_state(TASK_INTERRUPTIBLE);
1657
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001658 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001659 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001660
Lai Jiangshan81612392011-01-14 17:09:41 +08001661 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001662 remove_waiter(lock, waiter);
1663
1664 /*
1665 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1666 * have to fix that up.
1667 */
1668 fixup_rt_mutex_waiters(lock);
1669
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001670 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001671
Darren Hart8dac4562009-04-03 13:40:12 -07001672 return ret;
1673}