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