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