sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[linux-2.6-block.git] / kernel / locking / rtmutex.c
CommitLineData
23f78d4a
IM
1/*
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
d07fe82c 10 *
214e0aed 11 * See Documentation/locking/rt-mutex-design.txt for details.
23f78d4a
IM
12 */
13#include <linux/spinlock.h>
9984de1a 14#include <linux/export.h>
23f78d4a 15#include <linux/sched.h>
8bd75c77 16#include <linux/sched/rt.h>
fb00aca4 17#include <linux/sched/deadline.h>
84f001e1 18#include <linux/sched/wake_q.h>
23f78d4a
IM
19#include <linux/timer.h>
20
21#include "rtmutex_common.h"
22
23f78d4a
IM
23/*
24 * lock->owner state tracking:
25 *
8161239a
LJ
26 * lock->owner holds the task_struct pointer of the owner. Bit 0
27 * is used to keep track of the "lock has waiters" state.
23f78d4a 28 *
8161239a
LJ
29 * owner bit0
30 * NULL 0 lock is free (fast acquire possible)
31 * NULL 1 lock is free and has waiters and the top waiter
32 * is going to take the lock*
33 * taskpointer 0 lock is held (fast release possible)
34 * taskpointer 1 lock is held and has waiters**
23f78d4a
IM
35 *
36 * The fast atomic compare exchange based acquire and release is only
8161239a
LJ
37 * possible when bit 0 of lock->owner is 0.
38 *
39 * (*) It also can be a transitional state when grabbing the lock
40 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
41 * we need to set the bit0 before looking at the lock, and the owner may be
42 * NULL in this small time, hence this can be a transitional state.
23f78d4a 43 *
8161239a
LJ
44 * (**) There is a small time when bit 0 is set but there are no
45 * waiters. This can happen when grabbing the lock in the slow path.
46 * To prevent a cmpxchg of the owner releasing the lock, we need to
47 * set this bit before looking at the lock.
23f78d4a
IM
48 */
49
bd197234 50static void
8161239a 51rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
23f78d4a 52{
8161239a 53 unsigned long val = (unsigned long)owner;
23f78d4a
IM
54
55 if (rt_mutex_has_waiters(lock))
56 val |= RT_MUTEX_HAS_WAITERS;
57
58 lock->owner = (struct task_struct *)val;
59}
60
61static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
62{
63 lock->owner = (struct task_struct *)
64 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
65}
66
67static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
68{
dbb26055
TG
69 unsigned long owner, *p = (unsigned long *) &lock->owner;
70
71 if (rt_mutex_has_waiters(lock))
72 return;
73
74 /*
75 * The rbtree has no waiters enqueued, now make sure that the
76 * lock->owner still has the waiters bit set, otherwise the
77 * following can happen:
78 *
79 * CPU 0 CPU 1 CPU2
80 * l->owner=T1
81 * rt_mutex_lock(l)
82 * lock(l->lock)
83 * l->owner = T1 | HAS_WAITERS;
84 * enqueue(T2)
85 * boost()
86 * unlock(l->lock)
87 * block()
88 *
89 * rt_mutex_lock(l)
90 * lock(l->lock)
91 * l->owner = T1 | HAS_WAITERS;
92 * enqueue(T3)
93 * boost()
94 * unlock(l->lock)
95 * block()
96 * signal(->T2) signal(->T3)
97 * lock(l->lock)
98 * dequeue(T2)
99 * deboost()
100 * unlock(l->lock)
101 * lock(l->lock)
102 * dequeue(T3)
103 * ==> wait list is empty
104 * deboost()
105 * unlock(l->lock)
106 * lock(l->lock)
107 * fixup_rt_mutex_waiters()
108 * if (wait_list_empty(l) {
109 * l->owner = owner
110 * owner = l->owner & ~HAS_WAITERS;
111 * ==> l->owner = T1
112 * }
113 * lock(l->lock)
114 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
115 * if (wait_list_empty(l) {
116 * owner = l->owner & ~HAS_WAITERS;
117 * cmpxchg(l->owner, T1, NULL)
118 * ===> Success (l->owner = NULL)
119 *
120 * l->owner = owner
121 * ==> l->owner = T1
122 * }
123 *
124 * With the check for the waiter bit in place T3 on CPU2 will not
125 * overwrite. All tasks fiddling with the waiters bit are
126 * serialized by l->lock, so nothing else can modify the waiters
127 * bit. If the bit is set then nothing can change l->owner either
128 * so the simple RMW is safe. The cmpxchg() will simply fail if it
129 * happens in the middle of the RMW because the waiters bit is
130 * still set.
131 */
132 owner = READ_ONCE(*p);
133 if (owner & RT_MUTEX_HAS_WAITERS)
134 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
23f78d4a
IM
135}
136
bd197234 137/*
cede8841
SAS
138 * We can speed up the acquire/release, if there's no debugging state to be
139 * set up.
bd197234 140 */
cede8841 141#ifndef CONFIG_DEBUG_RT_MUTEXES
700318d1
DB
142# define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
143# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
144# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
145
146/*
147 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
148 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
149 * relaxed semantics suffice.
150 */
bd197234
TG
151static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
152{
153 unsigned long owner, *p = (unsigned long *) &lock->owner;
154
155 do {
156 owner = *p;
700318d1
DB
157 } while (cmpxchg_relaxed(p, owner,
158 owner | RT_MUTEX_HAS_WAITERS) != owner);
bd197234 159}
27e35715
TG
160
161/*
162 * Safe fastpath aware unlock:
163 * 1) Clear the waiters bit
164 * 2) Drop lock->wait_lock
165 * 3) Try to unlock the lock with cmpxchg
166 */
b4abf910
TG
167static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
168 unsigned long flags)
27e35715
TG
169 __releases(lock->wait_lock)
170{
171 struct task_struct *owner = rt_mutex_owner(lock);
172
173 clear_rt_mutex_waiters(lock);
b4abf910 174 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
27e35715
TG
175 /*
176 * If a new waiter comes in between the unlock and the cmpxchg
177 * we have two situations:
178 *
179 * unlock(wait_lock);
180 * lock(wait_lock);
181 * cmpxchg(p, owner, 0) == owner
182 * mark_rt_mutex_waiters(lock);
183 * acquire(lock);
184 * or:
185 *
186 * unlock(wait_lock);
187 * lock(wait_lock);
188 * mark_rt_mutex_waiters(lock);
189 *
190 * cmpxchg(p, owner, 0) != owner
191 * enqueue_waiter();
192 * unlock(wait_lock);
193 * lock(wait_lock);
194 * wake waiter();
195 * unlock(wait_lock);
196 * lock(wait_lock);
197 * acquire(lock);
198 */
700318d1 199 return rt_mutex_cmpxchg_release(lock, owner, NULL);
27e35715
TG
200}
201
bd197234 202#else
700318d1
DB
203# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
204# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
205# define rt_mutex_cmpxchg_release(l,c,n) (0)
206
bd197234
TG
207static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
208{
209 lock->owner = (struct task_struct *)
210 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
211}
27e35715
TG
212
213/*
214 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
215 */
b4abf910
TG
216static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
217 unsigned long flags)
27e35715
TG
218 __releases(lock->wait_lock)
219{
220 lock->owner = NULL;
b4abf910 221 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
27e35715
TG
222 return true;
223}
bd197234
TG
224#endif
225
fb00aca4
PZ
226static inline int
227rt_mutex_waiter_less(struct rt_mutex_waiter *left,
228 struct rt_mutex_waiter *right)
229{
2d3d891d 230 if (left->prio < right->prio)
fb00aca4
PZ
231 return 1;
232
233 /*
2d3d891d
DF
234 * If both waiters have dl_prio(), we check the deadlines of the
235 * associated tasks.
236 * If left waiter has a dl_prio(), and we didn't return 1 above,
237 * then right waiter has a dl_prio() too.
fb00aca4 238 */
2d3d891d 239 if (dl_prio(left->prio))
f5240575
JL
240 return dl_time_before(left->task->dl.deadline,
241 right->task->dl.deadline);
fb00aca4
PZ
242
243 return 0;
244}
245
246static void
247rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
248{
249 struct rb_node **link = &lock->waiters.rb_node;
250 struct rb_node *parent = NULL;
251 struct rt_mutex_waiter *entry;
252 int leftmost = 1;
253
254 while (*link) {
255 parent = *link;
256 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
257 if (rt_mutex_waiter_less(waiter, entry)) {
258 link = &parent->rb_left;
259 } else {
260 link = &parent->rb_right;
261 leftmost = 0;
262 }
263 }
264
265 if (leftmost)
266 lock->waiters_leftmost = &waiter->tree_entry;
267
268 rb_link_node(&waiter->tree_entry, parent, link);
269 rb_insert_color(&waiter->tree_entry, &lock->waiters);
270}
271
272static void
273rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
274{
275 if (RB_EMPTY_NODE(&waiter->tree_entry))
276 return;
277
278 if (lock->waiters_leftmost == &waiter->tree_entry)
279 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
280
281 rb_erase(&waiter->tree_entry, &lock->waiters);
282 RB_CLEAR_NODE(&waiter->tree_entry);
283}
284
285static void
286rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
287{
288 struct rb_node **link = &task->pi_waiters.rb_node;
289 struct rb_node *parent = NULL;
290 struct rt_mutex_waiter *entry;
291 int leftmost = 1;
292
293 while (*link) {
294 parent = *link;
295 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
296 if (rt_mutex_waiter_less(waiter, entry)) {
297 link = &parent->rb_left;
298 } else {
299 link = &parent->rb_right;
300 leftmost = 0;
301 }
302 }
303
304 if (leftmost)
305 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
306
307 rb_link_node(&waiter->pi_tree_entry, parent, link);
308 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
309}
310
311static void
312rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
313{
314 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
315 return;
316
317 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
318 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
319
320 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
321 RB_CLEAR_NODE(&waiter->pi_tree_entry);
322}
323
23f78d4a 324/*
fb00aca4 325 * Calculate task priority from the waiter tree priority
23f78d4a 326 *
fb00aca4 327 * Return task->normal_prio when the waiter tree is empty or when
23f78d4a
IM
328 * the waiter is not allowed to do priority boosting
329 */
330int rt_mutex_getprio(struct task_struct *task)
331{
332 if (likely(!task_has_pi_waiters(task)))
333 return task->normal_prio;
334
2d3d891d 335 return min(task_top_pi_waiter(task)->prio,
23f78d4a
IM
336 task->normal_prio);
337}
338
2d3d891d
DF
339struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
340{
341 if (likely(!task_has_pi_waiters(task)))
342 return NULL;
343
344 return task_top_pi_waiter(task)->task;
345}
346
c365c292 347/*
0782e63b
TG
348 * Called by sched_setscheduler() to get the priority which will be
349 * effective after the change.
c365c292 350 */
0782e63b 351int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
c365c292
TG
352{
353 if (!task_has_pi_waiters(task))
0782e63b 354 return newprio;
c365c292 355
0782e63b
TG
356 if (task_top_pi_waiter(task)->task->prio <= newprio)
357 return task_top_pi_waiter(task)->task->prio;
358 return newprio;
c365c292
TG
359}
360
23f78d4a
IM
361/*
362 * Adjust the priority of a task, after its pi_waiters got modified.
363 *
364 * This can be both boosting and unboosting. task->pi_lock must be held.
365 */
bd197234 366static void __rt_mutex_adjust_prio(struct task_struct *task)
23f78d4a
IM
367{
368 int prio = rt_mutex_getprio(task);
369
2d3d891d 370 if (task->prio != prio || dl_prio(prio))
23f78d4a
IM
371 rt_mutex_setprio(task, prio);
372}
373
374/*
375 * Adjust task priority (undo boosting). Called from the exit path of
376 * rt_mutex_slowunlock() and rt_mutex_slowlock().
377 *
378 * (Note: We do this outside of the protection of lock->wait_lock to
379 * allow the lock to be taken while or before we readjust the priority
380 * of task. We do not use the spin_xx_mutex() variants here as we are
381 * outside of the debug path.)
382 */
802ab58d 383void rt_mutex_adjust_prio(struct task_struct *task)
23f78d4a
IM
384{
385 unsigned long flags;
386
1d615482 387 raw_spin_lock_irqsave(&task->pi_lock, flags);
23f78d4a 388 __rt_mutex_adjust_prio(task);
1d615482 389 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
23f78d4a
IM
390}
391
8930ed80
TG
392/*
393 * Deadlock detection is conditional:
394 *
395 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
396 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
397 *
398 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
399 * conducted independent of the detect argument.
400 *
401 * If the waiter argument is NULL this indicates the deboost path and
402 * deadlock detection is disabled independent of the detect argument
403 * and the config settings.
404 */
405static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
406 enum rtmutex_chainwalk chwalk)
407{
408 /*
409 * This is just a wrapper function for the following call,
410 * because debug_rt_mutex_detect_deadlock() smells like a magic
411 * debug feature and I wanted to keep the cond function in the
412 * main source file along with the comments instead of having
413 * two of the same in the headers.
414 */
415 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
416}
417
23f78d4a
IM
418/*
419 * Max number of times we'll walk the boosting chain:
420 */
421int max_lock_depth = 1024;
422
82084984
TG
423static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
424{
425 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
426}
427
23f78d4a
IM
428/*
429 * Adjust the priority chain. Also used for deadlock detection.
430 * Decreases task's usage by one - may thus free the task.
0c106173 431 *
82084984
TG
432 * @task: the task owning the mutex (owner) for which a chain walk is
433 * probably needed
e6beaa36 434 * @chwalk: do we have to carry out deadlock detection?
82084984
TG
435 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
436 * things for a task that has just got its priority adjusted, and
437 * is waiting on a mutex)
438 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
439 * we dropped its pi_lock. Is never dereferenced, only used for
440 * comparison to detect lock chain changes.
0c106173 441 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
82084984
TG
442 * its priority to the mutex owner (can be NULL in the case
443 * depicted above or if the top waiter is gone away and we are
444 * actually deboosting the owner)
445 * @top_task: the current top waiter
0c106173 446 *
23f78d4a 447 * Returns 0 or -EDEADLK.
3eb65aea
TG
448 *
449 * Chain walk basics and protection scope
450 *
451 * [R] refcount on task
452 * [P] task->pi_lock held
453 * [L] rtmutex->wait_lock held
454 *
455 * Step Description Protected by
456 * function arguments:
457 * @task [R]
458 * @orig_lock if != NULL @top_task is blocked on it
459 * @next_lock Unprotected. Cannot be
460 * dereferenced. Only used for
461 * comparison.
462 * @orig_waiter if != NULL @top_task is blocked on it
463 * @top_task current, or in case of proxy
464 * locking protected by calling
465 * code
466 * again:
467 * loop_sanity_check();
468 * retry:
469 * [1] lock(task->pi_lock); [R] acquire [P]
470 * [2] waiter = task->pi_blocked_on; [P]
471 * [3] check_exit_conditions_1(); [P]
472 * [4] lock = waiter->lock; [P]
473 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
474 * unlock(task->pi_lock); release [P]
475 * goto retry;
476 * }
477 * [6] check_exit_conditions_2(); [P] + [L]
478 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
479 * [8] unlock(task->pi_lock); release [P]
480 * put_task_struct(task); release [R]
481 * [9] check_exit_conditions_3(); [L]
482 * [10] task = owner(lock); [L]
483 * get_task_struct(task); [L] acquire [R]
484 * lock(task->pi_lock); [L] acquire [P]
485 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
486 * [12] check_exit_conditions_4(); [P] + [L]
487 * [13] unlock(task->pi_lock); release [P]
488 * unlock(lock->wait_lock); release [L]
489 * goto again;
23f78d4a 490 */
bd197234 491static int rt_mutex_adjust_prio_chain(struct task_struct *task,
8930ed80 492 enum rtmutex_chainwalk chwalk,
bd197234 493 struct rt_mutex *orig_lock,
82084984 494 struct rt_mutex *next_lock,
bd197234
TG
495 struct rt_mutex_waiter *orig_waiter,
496 struct task_struct *top_task)
23f78d4a 497{
23f78d4a 498 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
a57594a1 499 struct rt_mutex_waiter *prerequeue_top_waiter;
8930ed80 500 int ret = 0, depth = 0;
a57594a1 501 struct rt_mutex *lock;
8930ed80 502 bool detect_deadlock;
67792e2c 503 bool requeue = true;
23f78d4a 504
8930ed80 505 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
23f78d4a
IM
506
507 /*
508 * The (de)boosting is a step by step approach with a lot of
509 * pitfalls. We want this to be preemptible and we want hold a
510 * maximum of two locks per step. So we have to check
511 * carefully whether things change under us.
512 */
513 again:
3eb65aea
TG
514 /*
515 * We limit the lock chain length for each invocation.
516 */
23f78d4a
IM
517 if (++depth > max_lock_depth) {
518 static int prev_max;
519
520 /*
521 * Print this only once. If the admin changes the limit,
522 * print a new message when reaching the limit again.
523 */
524 if (prev_max != max_lock_depth) {
525 prev_max = max_lock_depth;
526 printk(KERN_WARNING "Maximum lock depth %d reached "
527 "task: %s (%d)\n", max_lock_depth,
ba25f9dc 528 top_task->comm, task_pid_nr(top_task));
23f78d4a
IM
529 }
530 put_task_struct(task);
531
3d5c9340 532 return -EDEADLK;
23f78d4a 533 }
3eb65aea
TG
534
535 /*
536 * We are fully preemptible here and only hold the refcount on
537 * @task. So everything can have changed under us since the
538 * caller or our own code below (goto retry/again) dropped all
539 * locks.
540 */
23f78d4a
IM
541 retry:
542 /*
3eb65aea 543 * [1] Task cannot go away as we did a get_task() before !
23f78d4a 544 */
b4abf910 545 raw_spin_lock_irq(&task->pi_lock);
23f78d4a 546
3eb65aea
TG
547 /*
548 * [2] Get the waiter on which @task is blocked on.
549 */
23f78d4a 550 waiter = task->pi_blocked_on;
3eb65aea
TG
551
552 /*
553 * [3] check_exit_conditions_1() protected by task->pi_lock.
554 */
555
23f78d4a
IM
556 /*
557 * Check whether the end of the boosting chain has been
558 * reached or the state of the chain has changed while we
559 * dropped the locks.
560 */
8161239a 561 if (!waiter)
23f78d4a
IM
562 goto out_unlock_pi;
563
1a539a87
TG
564 /*
565 * Check the orig_waiter state. After we dropped the locks,
8161239a 566 * the previous owner of the lock might have released the lock.
1a539a87 567 */
8161239a 568 if (orig_waiter && !rt_mutex_owner(orig_lock))
1a539a87
TG
569 goto out_unlock_pi;
570
82084984
TG
571 /*
572 * We dropped all locks after taking a refcount on @task, so
573 * the task might have moved on in the lock chain or even left
574 * the chain completely and blocks now on an unrelated lock or
575 * on @orig_lock.
576 *
577 * We stored the lock on which @task was blocked in @next_lock,
578 * so we can detect the chain change.
579 */
580 if (next_lock != waiter->lock)
581 goto out_unlock_pi;
582
1a539a87
TG
583 /*
584 * Drop out, when the task has no waiters. Note,
585 * top_waiter can be NULL, when we are in the deboosting
586 * mode!
587 */
397335f0
TG
588 if (top_waiter) {
589 if (!task_has_pi_waiters(task))
590 goto out_unlock_pi;
591 /*
592 * If deadlock detection is off, we stop here if we
67792e2c
TG
593 * are not the top pi waiter of the task. If deadlock
594 * detection is enabled we continue, but stop the
595 * requeueing in the chain walk.
397335f0 596 */
67792e2c
TG
597 if (top_waiter != task_top_pi_waiter(task)) {
598 if (!detect_deadlock)
599 goto out_unlock_pi;
600 else
601 requeue = false;
602 }
397335f0 603 }
23f78d4a
IM
604
605 /*
67792e2c
TG
606 * If the waiter priority is the same as the task priority
607 * then there is no further priority adjustment necessary. If
608 * deadlock detection is off, we stop the chain walk. If its
609 * enabled we continue, but stop the requeueing in the chain
610 * walk.
23f78d4a 611 */
67792e2c
TG
612 if (waiter->prio == task->prio) {
613 if (!detect_deadlock)
614 goto out_unlock_pi;
615 else
616 requeue = false;
617 }
23f78d4a 618
3eb65aea
TG
619 /*
620 * [4] Get the next lock
621 */
23f78d4a 622 lock = waiter->lock;
3eb65aea
TG
623 /*
624 * [5] We need to trylock here as we are holding task->pi_lock,
625 * which is the reverse lock order versus the other rtmutex
626 * operations.
627 */
d209d74d 628 if (!raw_spin_trylock(&lock->wait_lock)) {
b4abf910 629 raw_spin_unlock_irq(&task->pi_lock);
23f78d4a
IM
630 cpu_relax();
631 goto retry;
632 }
633
397335f0 634 /*
3eb65aea
TG
635 * [6] check_exit_conditions_2() protected by task->pi_lock and
636 * lock->wait_lock.
637 *
397335f0
TG
638 * Deadlock detection. If the lock is the same as the original
639 * lock which caused us to walk the lock chain or if the
640 * current lock is owned by the task which initiated the chain
641 * walk, we detected a deadlock.
642 */
95e02ca9 643 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
8930ed80 644 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
d209d74d 645 raw_spin_unlock(&lock->wait_lock);
3d5c9340 646 ret = -EDEADLK;
23f78d4a
IM
647 goto out_unlock_pi;
648 }
649
67792e2c
TG
650 /*
651 * If we just follow the lock chain for deadlock detection, no
652 * need to do all the requeue operations. To avoid a truckload
653 * of conditionals around the various places below, just do the
654 * minimum chain walk checks.
655 */
656 if (!requeue) {
657 /*
658 * No requeue[7] here. Just release @task [8]
659 */
b4abf910 660 raw_spin_unlock(&task->pi_lock);
67792e2c
TG
661 put_task_struct(task);
662
663 /*
664 * [9] check_exit_conditions_3 protected by lock->wait_lock.
665 * If there is no owner of the lock, end of chain.
666 */
667 if (!rt_mutex_owner(lock)) {
b4abf910 668 raw_spin_unlock_irq(&lock->wait_lock);
67792e2c
TG
669 return 0;
670 }
671
672 /* [10] Grab the next task, i.e. owner of @lock */
673 task = rt_mutex_owner(lock);
674 get_task_struct(task);
b4abf910 675 raw_spin_lock(&task->pi_lock);
67792e2c
TG
676
677 /*
678 * No requeue [11] here. We just do deadlock detection.
679 *
680 * [12] Store whether owner is blocked
681 * itself. Decision is made after dropping the locks
682 */
683 next_lock = task_blocked_on_lock(task);
684 /*
685 * Get the top waiter for the next iteration
686 */
687 top_waiter = rt_mutex_top_waiter(lock);
688
689 /* [13] Drop locks */
b4abf910
TG
690 raw_spin_unlock(&task->pi_lock);
691 raw_spin_unlock_irq(&lock->wait_lock);
67792e2c
TG
692
693 /* If owner is not blocked, end of chain. */
694 if (!next_lock)
695 goto out_put_task;
696 goto again;
697 }
698
a57594a1
TG
699 /*
700 * Store the current top waiter before doing the requeue
701 * operation on @lock. We need it for the boost/deboost
702 * decision below.
703 */
704 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
23f78d4a 705
9f40a51a 706 /* [7] Requeue the waiter in the lock waiter tree. */
fb00aca4 707 rt_mutex_dequeue(lock, waiter);
2d3d891d 708 waiter->prio = task->prio;
fb00aca4 709 rt_mutex_enqueue(lock, waiter);
23f78d4a 710
3eb65aea 711 /* [8] Release the task */
b4abf910 712 raw_spin_unlock(&task->pi_lock);
2ffa5a5c
TG
713 put_task_struct(task);
714
a57594a1 715 /*
3eb65aea
TG
716 * [9] check_exit_conditions_3 protected by lock->wait_lock.
717 *
a57594a1
TG
718 * We must abort the chain walk if there is no lock owner even
719 * in the dead lock detection case, as we have nothing to
720 * follow here. This is the end of the chain we are walking.
721 */
8161239a
LJ
722 if (!rt_mutex_owner(lock)) {
723 /*
3eb65aea
TG
724 * If the requeue [7] above changed the top waiter,
725 * then we need to wake the new top waiter up to try
726 * to get the lock.
8161239a 727 */
a57594a1 728 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
8161239a 729 wake_up_process(rt_mutex_top_waiter(lock)->task);
b4abf910 730 raw_spin_unlock_irq(&lock->wait_lock);
2ffa5a5c 731 return 0;
8161239a 732 }
23f78d4a 733
3eb65aea 734 /* [10] Grab the next task, i.e. the owner of @lock */
23f78d4a 735 task = rt_mutex_owner(lock);
db630637 736 get_task_struct(task);
b4abf910 737 raw_spin_lock(&task->pi_lock);
23f78d4a 738
3eb65aea 739 /* [11] requeue the pi waiters if necessary */
23f78d4a 740 if (waiter == rt_mutex_top_waiter(lock)) {
a57594a1
TG
741 /*
742 * The waiter became the new top (highest priority)
743 * waiter on the lock. Replace the previous top waiter
9f40a51a 744 * in the owner tasks pi waiters tree with this waiter
a57594a1
TG
745 * and adjust the priority of the owner.
746 */
747 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
fb00aca4 748 rt_mutex_enqueue_pi(task, waiter);
23f78d4a
IM
749 __rt_mutex_adjust_prio(task);
750
a57594a1
TG
751 } else if (prerequeue_top_waiter == waiter) {
752 /*
753 * The waiter was the top waiter on the lock, but is
754 * no longer the top prority waiter. Replace waiter in
9f40a51a 755 * the owner tasks pi waiters tree with the new top
a57594a1
TG
756 * (highest priority) waiter and adjust the priority
757 * of the owner.
758 * The new top waiter is stored in @waiter so that
759 * @waiter == @top_waiter evaluates to true below and
760 * we continue to deboost the rest of the chain.
761 */
fb00aca4 762 rt_mutex_dequeue_pi(task, waiter);
23f78d4a 763 waiter = rt_mutex_top_waiter(lock);
fb00aca4 764 rt_mutex_enqueue_pi(task, waiter);
23f78d4a 765 __rt_mutex_adjust_prio(task);
a57594a1
TG
766 } else {
767 /*
768 * Nothing changed. No need to do any priority
769 * adjustment.
770 */
23f78d4a
IM
771 }
772
82084984 773 /*
3eb65aea
TG
774 * [12] check_exit_conditions_4() protected by task->pi_lock
775 * and lock->wait_lock. The actual decisions are made after we
776 * dropped the locks.
777 *
82084984
TG
778 * Check whether the task which owns the current lock is pi
779 * blocked itself. If yes we store a pointer to the lock for
780 * the lock chain change detection above. After we dropped
781 * task->pi_lock next_lock cannot be dereferenced anymore.
782 */
783 next_lock = task_blocked_on_lock(task);
a57594a1
TG
784 /*
785 * Store the top waiter of @lock for the end of chain walk
786 * decision below.
787 */
23f78d4a 788 top_waiter = rt_mutex_top_waiter(lock);
3eb65aea
TG
789
790 /* [13] Drop the locks */
b4abf910
TG
791 raw_spin_unlock(&task->pi_lock);
792 raw_spin_unlock_irq(&lock->wait_lock);
23f78d4a 793
82084984 794 /*
3eb65aea
TG
795 * Make the actual exit decisions [12], based on the stored
796 * values.
797 *
82084984
TG
798 * We reached the end of the lock chain. Stop right here. No
799 * point to go back just to figure that out.
800 */
801 if (!next_lock)
802 goto out_put_task;
803
a57594a1
TG
804 /*
805 * If the current waiter is not the top waiter on the lock,
806 * then we can stop the chain walk here if we are not in full
807 * deadlock detection mode.
808 */
23f78d4a
IM
809 if (!detect_deadlock && waiter != top_waiter)
810 goto out_put_task;
811
812 goto again;
813
814 out_unlock_pi:
b4abf910 815 raw_spin_unlock_irq(&task->pi_lock);
23f78d4a
IM
816 out_put_task:
817 put_task_struct(task);
36c8b586 818
23f78d4a
IM
819 return ret;
820}
821
23f78d4a
IM
822/*
823 * Try to take an rt-mutex
824 *
b4abf910 825 * Must be called with lock->wait_lock held and interrupts disabled
8161239a 826 *
358c331f
TG
827 * @lock: The lock to be acquired.
828 * @task: The task which wants to acquire the lock
9f40a51a 829 * @waiter: The waiter that is queued to the lock's wait tree if the
358c331f 830 * callsite called task_blocked_on_lock(), otherwise NULL
23f78d4a 831 */
8161239a 832static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
358c331f 833 struct rt_mutex_waiter *waiter)
23f78d4a
IM
834{
835 /*
358c331f
TG
836 * Before testing whether we can acquire @lock, we set the
837 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
838 * other tasks which try to modify @lock into the slow path
839 * and they serialize on @lock->wait_lock.
23f78d4a 840 *
358c331f
TG
841 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
842 * as explained at the top of this file if and only if:
23f78d4a 843 *
358c331f
TG
844 * - There is a lock owner. The caller must fixup the
845 * transient state if it does a trylock or leaves the lock
846 * function due to a signal or timeout.
847 *
848 * - @task acquires the lock and there are no other
849 * waiters. This is undone in rt_mutex_set_owner(@task) at
850 * the end of this function.
23f78d4a
IM
851 */
852 mark_rt_mutex_waiters(lock);
853
358c331f
TG
854 /*
855 * If @lock has an owner, give up.
856 */
8161239a 857 if (rt_mutex_owner(lock))
23f78d4a
IM
858 return 0;
859
8161239a 860 /*
358c331f 861 * If @waiter != NULL, @task has already enqueued the waiter
9f40a51a 862 * into @lock waiter tree. If @waiter == NULL then this is a
358c331f 863 * trylock attempt.
8161239a 864 */
358c331f
TG
865 if (waiter) {
866 /*
867 * If waiter is not the highest priority waiter of
868 * @lock, give up.
869 */
870 if (waiter != rt_mutex_top_waiter(lock))
871 return 0;
8161239a 872
358c331f
TG
873 /*
874 * We can acquire the lock. Remove the waiter from the
9f40a51a 875 * lock waiters tree.
358c331f
TG
876 */
877 rt_mutex_dequeue(lock, waiter);
8161239a 878
358c331f 879 } else {
8161239a 880 /*
358c331f
TG
881 * If the lock has waiters already we check whether @task is
882 * eligible to take over the lock.
883 *
884 * If there are no other waiters, @task can acquire
885 * the lock. @task->pi_blocked_on is NULL, so it does
886 * not need to be dequeued.
8161239a
LJ
887 */
888 if (rt_mutex_has_waiters(lock)) {
358c331f
TG
889 /*
890 * If @task->prio is greater than or equal to
891 * the top waiter priority (kernel view),
892 * @task lost.
893 */
894 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
895 return 0;
896
897 /*
898 * The current top waiter stays enqueued. We
899 * don't have to change anything in the lock
900 * waiters order.
901 */
902 } else {
903 /*
904 * No waiters. Take the lock without the
905 * pi_lock dance.@task->pi_blocked_on is NULL
906 * and we have no waiters to enqueue in @task
9f40a51a 907 * pi waiters tree.
358c331f
TG
908 */
909 goto takeit;
8161239a 910 }
8161239a
LJ
911 }
912
358c331f
TG
913 /*
914 * Clear @task->pi_blocked_on. Requires protection by
915 * @task->pi_lock. Redundant operation for the @waiter == NULL
916 * case, but conditionals are more expensive than a redundant
917 * store.
918 */
b4abf910 919 raw_spin_lock(&task->pi_lock);
358c331f
TG
920 task->pi_blocked_on = NULL;
921 /*
922 * Finish the lock acquisition. @task is the new owner. If
923 * other waiters exist we have to insert the highest priority
9f40a51a 924 * waiter into @task->pi_waiters tree.
358c331f
TG
925 */
926 if (rt_mutex_has_waiters(lock))
927 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
b4abf910 928 raw_spin_unlock(&task->pi_lock);
358c331f
TG
929
930takeit:
23f78d4a 931 /* We got the lock. */
9a11b49a 932 debug_rt_mutex_lock(lock);
23f78d4a 933
358c331f
TG
934 /*
935 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
936 * are still waiters or clears it.
937 */
8161239a 938 rt_mutex_set_owner(lock, task);
23f78d4a 939
8161239a 940 rt_mutex_deadlock_account_lock(lock, task);
23f78d4a
IM
941
942 return 1;
943}
944
945/*
946 * Task blocks on lock.
947 *
948 * Prepare waiter and propagate pi chain
949 *
b4abf910 950 * This must be called with lock->wait_lock held and interrupts disabled
23f78d4a
IM
951 */
952static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
953 struct rt_mutex_waiter *waiter,
8dac456a 954 struct task_struct *task,
8930ed80 955 enum rtmutex_chainwalk chwalk)
23f78d4a 956{
36c8b586 957 struct task_struct *owner = rt_mutex_owner(lock);
23f78d4a 958 struct rt_mutex_waiter *top_waiter = waiter;
82084984 959 struct rt_mutex *next_lock;
db630637 960 int chain_walk = 0, res;
23f78d4a 961
397335f0
TG
962 /*
963 * Early deadlock detection. We really don't want the task to
964 * enqueue on itself just to untangle the mess later. It's not
965 * only an optimization. We drop the locks, so another waiter
966 * can come in before the chain walk detects the deadlock. So
967 * the other will detect the deadlock and return -EDEADLOCK,
968 * which is wrong, as the other waiter is not in a deadlock
969 * situation.
970 */
3d5c9340 971 if (owner == task)
397335f0
TG
972 return -EDEADLK;
973
b4abf910 974 raw_spin_lock(&task->pi_lock);
8dac456a
DH
975 __rt_mutex_adjust_prio(task);
976 waiter->task = task;
23f78d4a 977 waiter->lock = lock;
2d3d891d 978 waiter->prio = task->prio;
23f78d4a
IM
979
980 /* Get the top priority waiter on the lock */
981 if (rt_mutex_has_waiters(lock))
982 top_waiter = rt_mutex_top_waiter(lock);
fb00aca4 983 rt_mutex_enqueue(lock, waiter);
23f78d4a 984
8dac456a 985 task->pi_blocked_on = waiter;
23f78d4a 986
b4abf910 987 raw_spin_unlock(&task->pi_lock);
23f78d4a 988
8161239a
LJ
989 if (!owner)
990 return 0;
991
b4abf910 992 raw_spin_lock(&owner->pi_lock);
23f78d4a 993 if (waiter == rt_mutex_top_waiter(lock)) {
fb00aca4
PZ
994 rt_mutex_dequeue_pi(owner, top_waiter);
995 rt_mutex_enqueue_pi(owner, waiter);
23f78d4a
IM
996
997 __rt_mutex_adjust_prio(owner);
db630637
SR
998 if (owner->pi_blocked_on)
999 chain_walk = 1;
8930ed80 1000 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
db630637 1001 chain_walk = 1;
82084984 1002 }
db630637 1003
82084984
TG
1004 /* Store the lock on which owner is blocked or NULL */
1005 next_lock = task_blocked_on_lock(owner);
1006
b4abf910 1007 raw_spin_unlock(&owner->pi_lock);
82084984
TG
1008 /*
1009 * Even if full deadlock detection is on, if the owner is not
1010 * blocked itself, we can avoid finding this out in the chain
1011 * walk.
1012 */
1013 if (!chain_walk || !next_lock)
23f78d4a
IM
1014 return 0;
1015
db630637
SR
1016 /*
1017 * The owner can't disappear while holding a lock,
1018 * so the owner struct is protected by wait_lock.
1019 * Gets dropped in rt_mutex_adjust_prio_chain()!
1020 */
1021 get_task_struct(owner);
1022
b4abf910 1023 raw_spin_unlock_irq(&lock->wait_lock);
23f78d4a 1024
8930ed80 1025 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
82084984 1026 next_lock, waiter, task);
23f78d4a 1027
b4abf910 1028 raw_spin_lock_irq(&lock->wait_lock);
23f78d4a
IM
1029
1030 return res;
1031}
1032
1033/*
9f40a51a 1034 * Remove the top waiter from the current tasks pi waiter tree and
45ab4eff 1035 * queue it up.
23f78d4a 1036 *
b4abf910 1037 * Called with lock->wait_lock held and interrupts disabled.
23f78d4a 1038 */
45ab4eff
DB
1039static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1040 struct rt_mutex *lock)
23f78d4a
IM
1041{
1042 struct rt_mutex_waiter *waiter;
23f78d4a 1043
b4abf910 1044 raw_spin_lock(&current->pi_lock);
23f78d4a
IM
1045
1046 waiter = rt_mutex_top_waiter(lock);
23f78d4a
IM
1047
1048 /*
1049 * Remove it from current->pi_waiters. We do not adjust a
1050 * possible priority boost right now. We execute wakeup in the
1051 * boosted mode and go back to normal after releasing
1052 * lock->wait_lock.
1053 */
fb00aca4 1054 rt_mutex_dequeue_pi(current, waiter);
23f78d4a 1055
27e35715
TG
1056 /*
1057 * As we are waking up the top waiter, and the waiter stays
1058 * queued on the lock until it gets the lock, this lock
1059 * obviously has waiters. Just set the bit here and this has
1060 * the added benefit of forcing all new tasks into the
1061 * slow path making sure no task of lower priority than
1062 * the top waiter can steal this lock.
1063 */
1064 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
23f78d4a 1065
b4abf910 1066 raw_spin_unlock(&current->pi_lock);
23f78d4a 1067
45ab4eff 1068 wake_q_add(wake_q, waiter->task);
23f78d4a
IM
1069}
1070
1071/*
8161239a 1072 * Remove a waiter from a lock and give up
23f78d4a 1073 *
b4abf910 1074 * Must be called with lock->wait_lock held and interrupts disabled. I must
8161239a 1075 * have just failed to try_to_take_rt_mutex().
23f78d4a 1076 */
bd197234
TG
1077static void remove_waiter(struct rt_mutex *lock,
1078 struct rt_mutex_waiter *waiter)
23f78d4a 1079{
1ca7b860 1080 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
36c8b586 1081 struct task_struct *owner = rt_mutex_owner(lock);
1ca7b860 1082 struct rt_mutex *next_lock;
23f78d4a 1083
b4abf910 1084 raw_spin_lock(&current->pi_lock);
fb00aca4 1085 rt_mutex_dequeue(lock, waiter);
23f78d4a 1086 current->pi_blocked_on = NULL;
b4abf910 1087 raw_spin_unlock(&current->pi_lock);
23f78d4a 1088
1ca7b860
TG
1089 /*
1090 * Only update priority if the waiter was the highest priority
1091 * waiter of the lock and there is an owner to update.
1092 */
1093 if (!owner || !is_top_waiter)
8161239a
LJ
1094 return;
1095
b4abf910 1096 raw_spin_lock(&owner->pi_lock);
23f78d4a 1097
1ca7b860 1098 rt_mutex_dequeue_pi(owner, waiter);
23f78d4a 1099
1ca7b860
TG
1100 if (rt_mutex_has_waiters(lock))
1101 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
23f78d4a 1102
1ca7b860 1103 __rt_mutex_adjust_prio(owner);
23f78d4a 1104
1ca7b860
TG
1105 /* Store the lock on which owner is blocked or NULL */
1106 next_lock = task_blocked_on_lock(owner);
db630637 1107
b4abf910 1108 raw_spin_unlock(&owner->pi_lock);
23f78d4a 1109
1ca7b860
TG
1110 /*
1111 * Don't walk the chain, if the owner task is not blocked
1112 * itself.
1113 */
82084984 1114 if (!next_lock)
23f78d4a
IM
1115 return;
1116
db630637
SR
1117 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1118 get_task_struct(owner);
1119
b4abf910 1120 raw_spin_unlock_irq(&lock->wait_lock);
23f78d4a 1121
8930ed80
TG
1122 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1123 next_lock, NULL, current);
23f78d4a 1124
b4abf910 1125 raw_spin_lock_irq(&lock->wait_lock);
23f78d4a
IM
1126}
1127
95e02ca9
TG
1128/*
1129 * Recheck the pi chain, in case we got a priority setting
1130 *
1131 * Called from sched_setscheduler
1132 */
1133void rt_mutex_adjust_pi(struct task_struct *task)
1134{
1135 struct rt_mutex_waiter *waiter;
82084984 1136 struct rt_mutex *next_lock;
95e02ca9
TG
1137 unsigned long flags;
1138
1d615482 1139 raw_spin_lock_irqsave(&task->pi_lock, flags);
95e02ca9
TG
1140
1141 waiter = task->pi_blocked_on;
2d3d891d
DF
1142 if (!waiter || (waiter->prio == task->prio &&
1143 !dl_prio(task->prio))) {
1d615482 1144 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
95e02ca9
TG
1145 return;
1146 }
82084984 1147 next_lock = waiter->lock;
1d615482 1148 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
95e02ca9 1149
db630637
SR
1150 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1151 get_task_struct(task);
82084984 1152
8930ed80
TG
1153 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1154 next_lock, NULL, task);
95e02ca9
TG
1155}
1156
8dac456a
DH
1157/**
1158 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1159 * @lock: the rt_mutex to take
1160 * @state: the state the task should block in (TASK_INTERRUPTIBLE
b4abf910 1161 * or TASK_UNINTERRUPTIBLE)
8dac456a
DH
1162 * @timeout: the pre-initialized and started timer, or NULL for none
1163 * @waiter: the pre-initialized rt_mutex_waiter
8dac456a 1164 *
b4abf910 1165 * Must be called with lock->wait_lock held and interrupts disabled
23f78d4a
IM
1166 */
1167static int __sched
8dac456a
DH
1168__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1169 struct hrtimer_sleeper *timeout,
8161239a 1170 struct rt_mutex_waiter *waiter)
23f78d4a 1171{
23f78d4a
IM
1172 int ret = 0;
1173
23f78d4a
IM
1174 for (;;) {
1175 /* Try to acquire the lock: */
8161239a 1176 if (try_to_take_rt_mutex(lock, current, waiter))
23f78d4a
IM
1177 break;
1178
1179 /*
1180 * TASK_INTERRUPTIBLE checks for signals and
1181 * timeout. Ignored otherwise.
1182 */
4009f4b3 1183 if (likely(state == TASK_INTERRUPTIBLE)) {
23f78d4a
IM
1184 /* Signal pending? */
1185 if (signal_pending(current))
1186 ret = -EINTR;
1187 if (timeout && !timeout->task)
1188 ret = -ETIMEDOUT;
1189 if (ret)
1190 break;
1191 }
1192
b4abf910 1193 raw_spin_unlock_irq(&lock->wait_lock);
23f78d4a 1194
8dac456a 1195 debug_rt_mutex_print_deadlock(waiter);
23f78d4a 1196
1b0b7c17 1197 schedule();
23f78d4a 1198
b4abf910 1199 raw_spin_lock_irq(&lock->wait_lock);
23f78d4a
IM
1200 set_current_state(state);
1201 }
1202
afffc6c1 1203 __set_current_state(TASK_RUNNING);
8dac456a
DH
1204 return ret;
1205}
1206
3d5c9340
TG
1207static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1208 struct rt_mutex_waiter *w)
1209{
1210 /*
1211 * If the result is not -EDEADLOCK or the caller requested
1212 * deadlock detection, nothing to do here.
1213 */
1214 if (res != -EDEADLOCK || detect_deadlock)
1215 return;
1216
1217 /*
1218 * Yell lowdly and stop the task right here.
1219 */
1220 rt_mutex_print_deadlock(w);
1221 while (1) {
1222 set_current_state(TASK_INTERRUPTIBLE);
1223 schedule();
1224 }
1225}
1226
8dac456a
DH
1227/*
1228 * Slow path lock function:
1229 */
1230static int __sched
1231rt_mutex_slowlock(struct rt_mutex *lock, int state,
1232 struct hrtimer_sleeper *timeout,
8930ed80 1233 enum rtmutex_chainwalk chwalk)
8dac456a
DH
1234{
1235 struct rt_mutex_waiter waiter;
b4abf910 1236 unsigned long flags;
8dac456a
DH
1237 int ret = 0;
1238
1239 debug_rt_mutex_init_waiter(&waiter);
fb00aca4
PZ
1240 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1241 RB_CLEAR_NODE(&waiter.tree_entry);
8dac456a 1242
b4abf910
TG
1243 /*
1244 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1245 * be called in early boot if the cmpxchg() fast path is disabled
1246 * (debug, no architecture support). In this case we will acquire the
1247 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1248 * enable interrupts in that early boot case. So we need to use the
1249 * irqsave/restore variants.
1250 */
1251 raw_spin_lock_irqsave(&lock->wait_lock, flags);
8dac456a
DH
1252
1253 /* Try to acquire the lock again: */
8161239a 1254 if (try_to_take_rt_mutex(lock, current, NULL)) {
b4abf910 1255 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
8dac456a
DH
1256 return 0;
1257 }
1258
1259 set_current_state(state);
1260
1261 /* Setup the timer, when timeout != NULL */
ccdd92c1 1262 if (unlikely(timeout))
8dac456a 1263 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
8dac456a 1264
8930ed80 1265 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
8161239a
LJ
1266
1267 if (likely(!ret))
afffc6c1 1268 /* sleep on the mutex */
8161239a 1269 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
8dac456a 1270
3d5c9340 1271 if (unlikely(ret)) {
9d3e2d02 1272 __set_current_state(TASK_RUNNING);
8d1e5a1a
SAS
1273 if (rt_mutex_has_waiters(lock))
1274 remove_waiter(lock, &waiter);
8930ed80 1275 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
3d5c9340 1276 }
23f78d4a
IM
1277
1278 /*
1279 * try_to_take_rt_mutex() sets the waiter bit
1280 * unconditionally. We might have to fix that up.
1281 */
1282 fixup_rt_mutex_waiters(lock);
1283
b4abf910 1284 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
23f78d4a
IM
1285
1286 /* Remove pending timer: */
1287 if (unlikely(timeout))
1288 hrtimer_cancel(&timeout->timer);
1289
23f78d4a
IM
1290 debug_rt_mutex_free_waiter(&waiter);
1291
1292 return ret;
1293}
1294
1295/*
1296 * Slow path try-lock function:
1297 */
88f2b4c1 1298static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
23f78d4a 1299{
b4abf910 1300 unsigned long flags;
88f2b4c1
TG
1301 int ret;
1302
1303 /*
1304 * If the lock already has an owner we fail to get the lock.
1305 * This can be done without taking the @lock->wait_lock as
1306 * it is only being read, and this is a trylock anyway.
1307 */
1308 if (rt_mutex_owner(lock))
1309 return 0;
23f78d4a 1310
88f2b4c1 1311 /*
b4abf910
TG
1312 * The mutex has currently no owner. Lock the wait lock and try to
1313 * acquire the lock. We use irqsave here to support early boot calls.
88f2b4c1 1314 */
b4abf910 1315 raw_spin_lock_irqsave(&lock->wait_lock, flags);
23f78d4a 1316
88f2b4c1 1317 ret = try_to_take_rt_mutex(lock, current, NULL);
23f78d4a 1318
88f2b4c1
TG
1319 /*
1320 * try_to_take_rt_mutex() sets the lock waiters bit
1321 * unconditionally. Clean this up.
1322 */
1323 fixup_rt_mutex_waiters(lock);
23f78d4a 1324
b4abf910 1325 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
23f78d4a
IM
1326
1327 return ret;
1328}
1329
1330/*
802ab58d
SAS
1331 * Slow path to release a rt-mutex.
1332 * Return whether the current task needs to undo a potential priority boosting.
23f78d4a 1333 */
802ab58d
SAS
1334static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1335 struct wake_q_head *wake_q)
23f78d4a 1336{
b4abf910
TG
1337 unsigned long flags;
1338
1339 /* irqsave required to support early boot calls */
1340 raw_spin_lock_irqsave(&lock->wait_lock, flags);
23f78d4a
IM
1341
1342 debug_rt_mutex_unlock(lock);
1343
1344 rt_mutex_deadlock_account_unlock(current);
1345
27e35715
TG
1346 /*
1347 * We must be careful here if the fast path is enabled. If we
1348 * have no waiters queued we cannot set owner to NULL here
1349 * because of:
1350 *
1351 * foo->lock->owner = NULL;
1352 * rtmutex_lock(foo->lock); <- fast path
1353 * free = atomic_dec_and_test(foo->refcnt);
1354 * rtmutex_unlock(foo->lock); <- fast path
1355 * if (free)
1356 * kfree(foo);
1357 * raw_spin_unlock(foo->lock->wait_lock);
1358 *
1359 * So for the fastpath enabled kernel:
1360 *
1361 * Nothing can set the waiters bit as long as we hold
1362 * lock->wait_lock. So we do the following sequence:
1363 *
1364 * owner = rt_mutex_owner(lock);
1365 * clear_rt_mutex_waiters(lock);
1366 * raw_spin_unlock(&lock->wait_lock);
1367 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1368 * return;
1369 * goto retry;
1370 *
1371 * The fastpath disabled variant is simple as all access to
1372 * lock->owner is serialized by lock->wait_lock:
1373 *
1374 * lock->owner = NULL;
1375 * raw_spin_unlock(&lock->wait_lock);
1376 */
1377 while (!rt_mutex_has_waiters(lock)) {
1378 /* Drops lock->wait_lock ! */
b4abf910 1379 if (unlock_rt_mutex_safe(lock, flags) == true)
802ab58d 1380 return false;
27e35715 1381 /* Relock the rtmutex and try again */
b4abf910 1382 raw_spin_lock_irqsave(&lock->wait_lock, flags);
23f78d4a
IM
1383 }
1384
27e35715
TG
1385 /*
1386 * The wakeup next waiter path does not suffer from the above
1387 * race. See the comments there.
45ab4eff
DB
1388 *
1389 * Queue the next waiter for wakeup once we release the wait_lock.
27e35715 1390 */
802ab58d 1391 mark_wakeup_next_waiter(wake_q, lock);
23f78d4a 1392
b4abf910 1393 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
23f78d4a 1394
802ab58d
SAS
1395 /* check PI boosting */
1396 return true;
23f78d4a
IM
1397}
1398
1399/*
1400 * debug aware fast / slowpath lock,trylock,unlock
1401 *
1402 * The atomic acquire/release ops are compiled away, when either the
1403 * architecture does not support cmpxchg or when debugging is enabled.
1404 */
1405static inline int
1406rt_mutex_fastlock(struct rt_mutex *lock, int state,
23f78d4a
IM
1407 int (*slowfn)(struct rt_mutex *lock, int state,
1408 struct hrtimer_sleeper *timeout,
8930ed80 1409 enum rtmutex_chainwalk chwalk))
23f78d4a 1410{
700318d1 1411 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
23f78d4a
IM
1412 rt_mutex_deadlock_account_lock(lock, current);
1413 return 0;
1414 } else
8930ed80 1415 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
23f78d4a
IM
1416}
1417
1418static inline int
1419rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
8930ed80
TG
1420 struct hrtimer_sleeper *timeout,
1421 enum rtmutex_chainwalk chwalk,
23f78d4a
IM
1422 int (*slowfn)(struct rt_mutex *lock, int state,
1423 struct hrtimer_sleeper *timeout,
8930ed80 1424 enum rtmutex_chainwalk chwalk))
23f78d4a 1425{
8930ed80 1426 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
700318d1 1427 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
23f78d4a
IM
1428 rt_mutex_deadlock_account_lock(lock, current);
1429 return 0;
1430 } else
8930ed80 1431 return slowfn(lock, state, timeout, chwalk);
23f78d4a
IM
1432}
1433
1434static inline int
1435rt_mutex_fasttrylock(struct rt_mutex *lock,
9a11b49a 1436 int (*slowfn)(struct rt_mutex *lock))
23f78d4a 1437{
700318d1 1438 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
23f78d4a
IM
1439 rt_mutex_deadlock_account_lock(lock, current);
1440 return 1;
1441 }
9a11b49a 1442 return slowfn(lock);
23f78d4a
IM
1443}
1444
1445static inline void
1446rt_mutex_fastunlock(struct rt_mutex *lock,
802ab58d
SAS
1447 bool (*slowfn)(struct rt_mutex *lock,
1448 struct wake_q_head *wqh))
23f78d4a 1449{
194a6b5b 1450 DEFINE_WAKE_Q(wake_q);
802ab58d 1451
700318d1 1452 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
23f78d4a 1453 rt_mutex_deadlock_account_unlock(current);
802ab58d
SAS
1454
1455 } else {
1456 bool deboost = slowfn(lock, &wake_q);
1457
1458 wake_up_q(&wake_q);
1459
1460 /* Undo pi boosting if necessary: */
1461 if (deboost)
1462 rt_mutex_adjust_prio(current);
1463 }
23f78d4a
IM
1464}
1465
1466/**
1467 * rt_mutex_lock - lock a rt_mutex
1468 *
1469 * @lock: the rt_mutex to be locked
1470 */
1471void __sched rt_mutex_lock(struct rt_mutex *lock)
1472{
1473 might_sleep();
1474
c051b21f 1475 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
23f78d4a
IM
1476}
1477EXPORT_SYMBOL_GPL(rt_mutex_lock);
1478
1479/**
1480 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1481 *
c051b21f 1482 * @lock: the rt_mutex to be locked
23f78d4a
IM
1483 *
1484 * Returns:
c051b21f
TG
1485 * 0 on success
1486 * -EINTR when interrupted by a signal
23f78d4a 1487 */
c051b21f 1488int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
23f78d4a
IM
1489{
1490 might_sleep();
1491
c051b21f 1492 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
23f78d4a
IM
1493}
1494EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1495
c051b21f
TG
1496/*
1497 * Futex variant with full deadlock detection.
1498 */
1499int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1500 struct hrtimer_sleeper *timeout)
1501{
1502 might_sleep();
1503
8930ed80
TG
1504 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1505 RT_MUTEX_FULL_CHAINWALK,
c051b21f
TG
1506 rt_mutex_slowlock);
1507}
1508
23f78d4a 1509/**
23b94b96
LH
1510 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1511 * the timeout structure is provided
1512 * by the caller
23f78d4a 1513 *
c051b21f 1514 * @lock: the rt_mutex to be locked
23f78d4a 1515 * @timeout: timeout structure or NULL (no timeout)
23f78d4a
IM
1516 *
1517 * Returns:
c051b21f
TG
1518 * 0 on success
1519 * -EINTR when interrupted by a signal
3ac49a1c 1520 * -ETIMEDOUT when the timeout expired
23f78d4a
IM
1521 */
1522int
c051b21f 1523rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
23f78d4a
IM
1524{
1525 might_sleep();
1526
8930ed80
TG
1527 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1528 RT_MUTEX_MIN_CHAINWALK,
c051b21f 1529 rt_mutex_slowlock);
23f78d4a
IM
1530}
1531EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1532
1533/**
1534 * rt_mutex_trylock - try to lock a rt_mutex
1535 *
1536 * @lock: the rt_mutex to be locked
1537 *
6ce47fd9
TG
1538 * This function can only be called in thread context. It's safe to
1539 * call it from atomic regions, but not from hard interrupt or soft
1540 * interrupt context.
1541 *
23f78d4a
IM
1542 * Returns 1 on success and 0 on contention
1543 */
1544int __sched rt_mutex_trylock(struct rt_mutex *lock)
1545{
a461d587 1546 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
6ce47fd9
TG
1547 return 0;
1548
23f78d4a
IM
1549 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1550}
1551EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1552
1553/**
1554 * rt_mutex_unlock - unlock a rt_mutex
1555 *
1556 * @lock: the rt_mutex to be unlocked
1557 */
1558void __sched rt_mutex_unlock(struct rt_mutex *lock)
1559{
1560 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1561}
1562EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1563
802ab58d
SAS
1564/**
1565 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1566 * @lock: the rt_mutex to be unlocked
1567 *
1568 * Returns: true/false indicating whether priority adjustment is
1569 * required or not.
1570 */
1571bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1572 struct wake_q_head *wqh)
1573{
700318d1 1574 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
802ab58d
SAS
1575 rt_mutex_deadlock_account_unlock(current);
1576 return false;
1577 }
1578 return rt_mutex_slowunlock(lock, wqh);
1579}
1580
23b94b96 1581/**
23f78d4a
IM
1582 * rt_mutex_destroy - mark a mutex unusable
1583 * @lock: the mutex to be destroyed
1584 *
1585 * This function marks the mutex uninitialized, and any subsequent
1586 * use of the mutex is forbidden. The mutex must not be locked when
1587 * this function is called.
1588 */
1589void rt_mutex_destroy(struct rt_mutex *lock)
1590{
1591 WARN_ON(rt_mutex_is_locked(lock));
1592#ifdef CONFIG_DEBUG_RT_MUTEXES
1593 lock->magic = NULL;
1594#endif
1595}
1596
1597EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1598
1599/**
1600 * __rt_mutex_init - initialize the rt lock
1601 *
1602 * @lock: the rt lock to be initialized
1603 *
1604 * Initialize the rt lock to unlocked state.
1605 *
1606 * Initializing of a locked rt lock is not allowed
1607 */
1608void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1609{
1610 lock->owner = NULL;
d209d74d 1611 raw_spin_lock_init(&lock->wait_lock);
fb00aca4
PZ
1612 lock->waiters = RB_ROOT;
1613 lock->waiters_leftmost = NULL;
23f78d4a
IM
1614
1615 debug_rt_mutex_init(lock, name);
1616}
1617EXPORT_SYMBOL_GPL(__rt_mutex_init);
0cdbee99
IM
1618
1619/**
1620 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1621 * proxy owner
1622 *
84d82ec5 1623 * @lock: the rt_mutex to be locked
0cdbee99
IM
1624 * @proxy_owner:the task to set as owner
1625 *
1626 * No locking. Caller has to do serializing itself
84d82ec5
TG
1627 *
1628 * Special API call for PI-futex support. This initializes the rtmutex and
1629 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1630 * possible at this point because the pi_state which contains the rtmutex
1631 * is not yet visible to other tasks.
0cdbee99
IM
1632 */
1633void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1634 struct task_struct *proxy_owner)
1635{
1636 __rt_mutex_init(lock, NULL);
9a11b49a 1637 debug_rt_mutex_proxy_lock(lock, proxy_owner);
8161239a 1638 rt_mutex_set_owner(lock, proxy_owner);
0cdbee99
IM
1639 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1640}
1641
1642/**
1643 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1644 *
84d82ec5 1645 * @lock: the rt_mutex to be locked
0cdbee99
IM
1646 *
1647 * No locking. Caller has to do serializing itself
84d82ec5
TG
1648 *
1649 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1650 * (debugging) state. Concurrent operations on this rt_mutex are not
1651 * possible because it belongs to the pi_state which is about to be freed
1652 * and it is not longer visible to other tasks.
0cdbee99
IM
1653 */
1654void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1655 struct task_struct *proxy_owner)
1656{
1657 debug_rt_mutex_proxy_unlock(lock);
8161239a 1658 rt_mutex_set_owner(lock, NULL);
0cdbee99
IM
1659 rt_mutex_deadlock_account_unlock(proxy_owner);
1660}
1661
8dac456a
DH
1662/**
1663 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1664 * @lock: the rt_mutex to take
1665 * @waiter: the pre-initialized rt_mutex_waiter
1666 * @task: the task to prepare
8dac456a
DH
1667 *
1668 * Returns:
1669 * 0 - task blocked on lock
1670 * 1 - acquired the lock for task, caller should wake it up
1671 * <0 - error
1672 *
1673 * Special API call for FUTEX_REQUEUE_PI support.
1674 */
1675int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1676 struct rt_mutex_waiter *waiter,
c051b21f 1677 struct task_struct *task)
8dac456a
DH
1678{
1679 int ret;
1680
b4abf910 1681 raw_spin_lock_irq(&lock->wait_lock);
8dac456a 1682
8161239a 1683 if (try_to_take_rt_mutex(lock, task, NULL)) {
b4abf910 1684 raw_spin_unlock_irq(&lock->wait_lock);
8dac456a
DH
1685 return 1;
1686 }
1687
3d5c9340 1688 /* We enforce deadlock detection for futexes */
8930ed80
TG
1689 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1690 RT_MUTEX_FULL_CHAINWALK);
8dac456a 1691
8161239a 1692 if (ret && !rt_mutex_owner(lock)) {
8dac456a
DH
1693 /*
1694 * Reset the return value. We might have
1695 * returned with -EDEADLK and the owner
1696 * released the lock while we were walking the
1697 * pi chain. Let the waiter sort it out.
1698 */
1699 ret = 0;
1700 }
8161239a
LJ
1701
1702 if (unlikely(ret))
1703 remove_waiter(lock, waiter);
1704
b4abf910 1705 raw_spin_unlock_irq(&lock->wait_lock);
8dac456a
DH
1706
1707 debug_rt_mutex_print_deadlock(waiter);
1708
1709 return ret;
1710}
1711
0cdbee99
IM
1712/**
1713 * rt_mutex_next_owner - return the next owner of the lock
1714 *
1715 * @lock: the rt lock query
1716 *
1717 * Returns the next owner of the lock or NULL
1718 *
1719 * Caller has to serialize against other accessors to the lock
1720 * itself.
1721 *
1722 * Special API call for PI-futex support
1723 */
1724struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1725{
1726 if (!rt_mutex_has_waiters(lock))
1727 return NULL;
1728
1729 return rt_mutex_top_waiter(lock)->task;
1730}
8dac456a
DH
1731
1732/**
1733 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1734 * @lock: the rt_mutex we were woken on
1735 * @to: the timeout, null if none. hrtimer should already have
c051b21f 1736 * been started.
8dac456a 1737 * @waiter: the pre-initialized rt_mutex_waiter
8dac456a
DH
1738 *
1739 * Complete the lock acquisition started our behalf by another thread.
1740 *
1741 * Returns:
1742 * 0 - success
c051b21f 1743 * <0 - error, one of -EINTR, -ETIMEDOUT
8dac456a
DH
1744 *
1745 * Special API call for PI-futex requeue support
1746 */
1747int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1748 struct hrtimer_sleeper *to,
c051b21f 1749 struct rt_mutex_waiter *waiter)
8dac456a
DH
1750{
1751 int ret;
1752
b4abf910 1753 raw_spin_lock_irq(&lock->wait_lock);
8dac456a
DH
1754
1755 set_current_state(TASK_INTERRUPTIBLE);
1756
afffc6c1 1757 /* sleep on the mutex */
8161239a 1758 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
8dac456a 1759
8161239a 1760 if (unlikely(ret))
8dac456a
DH
1761 remove_waiter(lock, waiter);
1762
1763 /*
1764 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1765 * have to fix that up.
1766 */
1767 fixup_rt_mutex_waiters(lock);
1768
b4abf910 1769 raw_spin_unlock_irq(&lock->wait_lock);
8dac456a 1770
8dac456a
DH
1771 return ret;
1772}