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