2 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
4 * started by Ingo Molnar and Thomas Gleixner.
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
11 * See Documentation/rt-mutex-design.txt for details.
13 #include <linux/spinlock.h>
14 #include <linux/export.h>
15 #include <linux/sched.h>
16 #include <linux/sched/rt.h>
17 #include <linux/sched/deadline.h>
18 #include <linux/timer.h>
20 #include "rtmutex_common.h"
23 * lock->owner state tracking:
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.
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**
35 * The fast atomic compare exchange based acquire and release is only
36 * possible when bit 0 of lock->owner is 0.
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.
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.
50 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
52 unsigned long val = (unsigned long)owner;
54 if (rt_mutex_has_waiters(lock))
55 val |= RT_MUTEX_HAS_WAITERS;
57 lock->owner = (struct task_struct *)val;
60 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
62 lock->owner = (struct task_struct *)
63 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
66 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
68 if (!rt_mutex_has_waiters(lock))
69 clear_rt_mutex_waiters(lock);
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
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)
78 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
80 unsigned long owner, *p = (unsigned long *) &lock->owner;
84 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
87 # define rt_mutex_cmpxchg(l,c,n) (0)
88 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
90 lock->owner = (struct task_struct *)
91 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
96 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
97 struct rt_mutex_waiter *right)
99 if (left->prio < right->prio)
103 * If both waiters have dl_prio(), we check the deadlines of the
105 * If left waiter has a dl_prio(), and we didn't return 1 above,
106 * then right waiter has a dl_prio() too.
108 if (dl_prio(left->prio))
109 return (left->task->dl.deadline < right->task->dl.deadline);
115 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
117 struct rb_node **link = &lock->waiters.rb_node;
118 struct rb_node *parent = NULL;
119 struct rt_mutex_waiter *entry;
124 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
125 if (rt_mutex_waiter_less(waiter, entry)) {
126 link = &parent->rb_left;
128 link = &parent->rb_right;
134 lock->waiters_leftmost = &waiter->tree_entry;
136 rb_link_node(&waiter->tree_entry, parent, link);
137 rb_insert_color(&waiter->tree_entry, &lock->waiters);
141 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
143 if (RB_EMPTY_NODE(&waiter->tree_entry))
146 if (lock->waiters_leftmost == &waiter->tree_entry)
147 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
149 rb_erase(&waiter->tree_entry, &lock->waiters);
150 RB_CLEAR_NODE(&waiter->tree_entry);
154 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
156 struct rb_node **link = &task->pi_waiters.rb_node;
157 struct rb_node *parent = NULL;
158 struct rt_mutex_waiter *entry;
163 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
164 if (rt_mutex_waiter_less(waiter, entry)) {
165 link = &parent->rb_left;
167 link = &parent->rb_right;
173 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
175 rb_link_node(&waiter->pi_tree_entry, parent, link);
176 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
180 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
182 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
185 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
186 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
188 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
189 RB_CLEAR_NODE(&waiter->pi_tree_entry);
193 * Calculate task priority from the waiter tree priority
195 * Return task->normal_prio when the waiter tree is empty or when
196 * the waiter is not allowed to do priority boosting
198 int rt_mutex_getprio(struct task_struct *task)
200 if (likely(!task_has_pi_waiters(task)))
201 return task->normal_prio;
203 return min(task_top_pi_waiter(task)->prio,
207 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
209 if (likely(!task_has_pi_waiters(task)))
212 return task_top_pi_waiter(task)->task;
216 * Adjust the priority of a task, after its pi_waiters got modified.
218 * This can be both boosting and unboosting. task->pi_lock must be held.
220 static void __rt_mutex_adjust_prio(struct task_struct *task)
222 int prio = rt_mutex_getprio(task);
224 if (task->prio != prio || dl_prio(prio))
225 rt_mutex_setprio(task, prio);
229 * Adjust task priority (undo boosting). Called from the exit path of
230 * rt_mutex_slowunlock() and rt_mutex_slowlock().
232 * (Note: We do this outside of the protection of lock->wait_lock to
233 * allow the lock to be taken while or before we readjust the priority
234 * of task. We do not use the spin_xx_mutex() variants here as we are
235 * outside of the debug path.)
237 static void rt_mutex_adjust_prio(struct task_struct *task)
241 raw_spin_lock_irqsave(&task->pi_lock, flags);
242 __rt_mutex_adjust_prio(task);
243 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
247 * Max number of times we'll walk the boosting chain:
249 int max_lock_depth = 1024;
251 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
253 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
257 * Adjust the priority chain. Also used for deadlock detection.
258 * Decreases task's usage by one - may thus free the task.
260 * @task: the task owning the mutex (owner) for which a chain walk is
262 * @deadlock_detect: do we have to carry out deadlock detection?
263 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
264 * things for a task that has just got its priority adjusted, and
265 * is waiting on a mutex)
266 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
267 * we dropped its pi_lock. Is never dereferenced, only used for
268 * comparison to detect lock chain changes.
269 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
270 * its priority to the mutex owner (can be NULL in the case
271 * depicted above or if the top waiter is gone away and we are
272 * actually deboosting the owner)
273 * @top_task: the current top waiter
275 * Returns 0 or -EDEADLK.
277 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
279 struct rt_mutex *orig_lock,
280 struct rt_mutex *next_lock,
281 struct rt_mutex_waiter *orig_waiter,
282 struct task_struct *top_task)
284 struct rt_mutex *lock;
285 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
286 int detect_deadlock, ret = 0, depth = 0;
289 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
293 * The (de)boosting is a step by step approach with a lot of
294 * pitfalls. We want this to be preemptible and we want hold a
295 * maximum of two locks per step. So we have to check
296 * carefully whether things change under us.
299 if (++depth > max_lock_depth) {
303 * Print this only once. If the admin changes the limit,
304 * print a new message when reaching the limit again.
306 if (prev_max != max_lock_depth) {
307 prev_max = max_lock_depth;
308 printk(KERN_WARNING "Maximum lock depth %d reached "
309 "task: %s (%d)\n", max_lock_depth,
310 top_task->comm, task_pid_nr(top_task));
312 put_task_struct(task);
318 * Task can not go away as we did a get_task() before !
320 raw_spin_lock_irqsave(&task->pi_lock, flags);
322 waiter = task->pi_blocked_on;
324 * Check whether the end of the boosting chain has been
325 * reached or the state of the chain has changed while we
332 * Check the orig_waiter state. After we dropped the locks,
333 * the previous owner of the lock might have released the lock.
335 if (orig_waiter && !rt_mutex_owner(orig_lock))
339 * We dropped all locks after taking a refcount on @task, so
340 * the task might have moved on in the lock chain or even left
341 * the chain completely and blocks now on an unrelated lock or
344 * We stored the lock on which @task was blocked in @next_lock,
345 * so we can detect the chain change.
347 if (next_lock != waiter->lock)
351 * Drop out, when the task has no waiters. Note,
352 * top_waiter can be NULL, when we are in the deboosting
356 if (!task_has_pi_waiters(task))
359 * If deadlock detection is off, we stop here if we
360 * are not the top pi waiter of the task.
362 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
367 * When deadlock detection is off then we check, if further
368 * priority adjustment is necessary.
370 if (!detect_deadlock && waiter->prio == task->prio)
374 if (!raw_spin_trylock(&lock->wait_lock)) {
375 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
381 * Deadlock detection. If the lock is the same as the original
382 * lock which caused us to walk the lock chain or if the
383 * current lock is owned by the task which initiated the chain
384 * walk, we detected a deadlock.
386 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
387 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
388 raw_spin_unlock(&lock->wait_lock);
393 top_waiter = rt_mutex_top_waiter(lock);
395 /* Requeue the waiter */
396 rt_mutex_dequeue(lock, waiter);
397 waiter->prio = task->prio;
398 rt_mutex_enqueue(lock, waiter);
400 /* Release the task */
401 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
402 if (!rt_mutex_owner(lock)) {
404 * If the requeue above changed the top waiter, then we need
405 * to wake the new top waiter up to try to get the lock.
408 if (top_waiter != rt_mutex_top_waiter(lock))
409 wake_up_process(rt_mutex_top_waiter(lock)->task);
410 raw_spin_unlock(&lock->wait_lock);
413 put_task_struct(task);
415 /* Grab the next task */
416 task = rt_mutex_owner(lock);
417 get_task_struct(task);
418 raw_spin_lock_irqsave(&task->pi_lock, flags);
420 if (waiter == rt_mutex_top_waiter(lock)) {
421 /* Boost the owner */
422 rt_mutex_dequeue_pi(task, top_waiter);
423 rt_mutex_enqueue_pi(task, waiter);
424 __rt_mutex_adjust_prio(task);
426 } else if (top_waiter == waiter) {
427 /* Deboost the owner */
428 rt_mutex_dequeue_pi(task, waiter);
429 waiter = rt_mutex_top_waiter(lock);
430 rt_mutex_enqueue_pi(task, waiter);
431 __rt_mutex_adjust_prio(task);
435 * Check whether the task which owns the current lock is pi
436 * blocked itself. If yes we store a pointer to the lock for
437 * the lock chain change detection above. After we dropped
438 * task->pi_lock next_lock cannot be dereferenced anymore.
440 next_lock = task_blocked_on_lock(task);
442 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
444 top_waiter = rt_mutex_top_waiter(lock);
445 raw_spin_unlock(&lock->wait_lock);
448 * We reached the end of the lock chain. Stop right here. No
449 * point to go back just to figure that out.
454 if (!detect_deadlock && waiter != top_waiter)
460 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
462 put_task_struct(task);
468 * Try to take an rt-mutex
470 * Must be called with lock->wait_lock held.
472 * @lock: the lock to be acquired.
473 * @task: the task which wants to acquire the lock
474 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
476 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
477 struct rt_mutex_waiter *waiter)
480 * We have to be careful here if the atomic speedups are
481 * enabled, such that, when
482 * - no other waiter is on the lock
483 * - the lock has been released since we did the cmpxchg
484 * the lock can be released or taken while we are doing the
485 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
487 * The atomic acquire/release aware variant of
488 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
489 * the WAITERS bit, the atomic release / acquire can not
490 * happen anymore and lock->wait_lock protects us from the
493 * Note, that this might set lock->owner =
494 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
495 * any more. This is fixed up when we take the ownership.
496 * This is the transitional state explained at the top of this file.
498 mark_rt_mutex_waiters(lock);
500 if (rt_mutex_owner(lock))
504 * It will get the lock because of one of these conditions:
505 * 1) there is no waiter
506 * 2) higher priority than waiters
507 * 3) it is top waiter
509 if (rt_mutex_has_waiters(lock)) {
510 if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
511 if (!waiter || waiter != rt_mutex_top_waiter(lock))
516 if (waiter || rt_mutex_has_waiters(lock)) {
518 struct rt_mutex_waiter *top;
520 raw_spin_lock_irqsave(&task->pi_lock, flags);
522 /* remove the queued waiter. */
524 rt_mutex_dequeue(lock, waiter);
525 task->pi_blocked_on = NULL;
529 * We have to enqueue the top waiter(if it exists) into
530 * task->pi_waiters list.
532 if (rt_mutex_has_waiters(lock)) {
533 top = rt_mutex_top_waiter(lock);
534 rt_mutex_enqueue_pi(task, top);
536 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
539 /* We got the lock. */
540 debug_rt_mutex_lock(lock);
542 rt_mutex_set_owner(lock, task);
544 rt_mutex_deadlock_account_lock(lock, task);
550 * Task blocks on lock.
552 * Prepare waiter and propagate pi chain
554 * This must be called with lock->wait_lock held.
556 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
557 struct rt_mutex_waiter *waiter,
558 struct task_struct *task,
561 struct task_struct *owner = rt_mutex_owner(lock);
562 struct rt_mutex_waiter *top_waiter = waiter;
563 struct rt_mutex *next_lock;
564 int chain_walk = 0, res;
568 * Early deadlock detection. We really don't want the task to
569 * enqueue on itself just to untangle the mess later. It's not
570 * only an optimization. We drop the locks, so another waiter
571 * can come in before the chain walk detects the deadlock. So
572 * the other will detect the deadlock and return -EDEADLOCK,
573 * which is wrong, as the other waiter is not in a deadlock
579 raw_spin_lock_irqsave(&task->pi_lock, flags);
580 __rt_mutex_adjust_prio(task);
583 waiter->prio = task->prio;
585 /* Get the top priority waiter on the lock */
586 if (rt_mutex_has_waiters(lock))
587 top_waiter = rt_mutex_top_waiter(lock);
588 rt_mutex_enqueue(lock, waiter);
590 task->pi_blocked_on = waiter;
592 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
597 raw_spin_lock_irqsave(&owner->pi_lock, flags);
598 if (waiter == rt_mutex_top_waiter(lock)) {
599 rt_mutex_dequeue_pi(owner, top_waiter);
600 rt_mutex_enqueue_pi(owner, waiter);
602 __rt_mutex_adjust_prio(owner);
603 if (owner->pi_blocked_on)
605 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
609 /* Store the lock on which owner is blocked or NULL */
610 next_lock = task_blocked_on_lock(owner);
612 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
614 * Even if full deadlock detection is on, if the owner is not
615 * blocked itself, we can avoid finding this out in the chain
618 if (!chain_walk || !next_lock)
622 * The owner can't disappear while holding a lock,
623 * so the owner struct is protected by wait_lock.
624 * Gets dropped in rt_mutex_adjust_prio_chain()!
626 get_task_struct(owner);
628 raw_spin_unlock(&lock->wait_lock);
630 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
631 next_lock, waiter, task);
633 raw_spin_lock(&lock->wait_lock);
639 * Wake up the next waiter on the lock.
641 * Remove the top waiter from the current tasks waiter list and wake it up.
643 * Called with lock->wait_lock held.
645 static void wakeup_next_waiter(struct rt_mutex *lock)
647 struct rt_mutex_waiter *waiter;
650 raw_spin_lock_irqsave(¤t->pi_lock, flags);
652 waiter = rt_mutex_top_waiter(lock);
655 * Remove it from current->pi_waiters. We do not adjust a
656 * possible priority boost right now. We execute wakeup in the
657 * boosted mode and go back to normal after releasing
660 rt_mutex_dequeue_pi(current, waiter);
662 rt_mutex_set_owner(lock, NULL);
664 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
666 wake_up_process(waiter->task);
670 * Remove a waiter from a lock and give up
672 * Must be called with lock->wait_lock held and
673 * have just failed to try_to_take_rt_mutex().
675 static void remove_waiter(struct rt_mutex *lock,
676 struct rt_mutex_waiter *waiter)
678 int first = (waiter == rt_mutex_top_waiter(lock));
679 struct task_struct *owner = rt_mutex_owner(lock);
680 struct rt_mutex *next_lock = NULL;
683 raw_spin_lock_irqsave(¤t->pi_lock, flags);
684 rt_mutex_dequeue(lock, waiter);
685 current->pi_blocked_on = NULL;
686 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
693 raw_spin_lock_irqsave(&owner->pi_lock, flags);
695 rt_mutex_dequeue_pi(owner, waiter);
697 if (rt_mutex_has_waiters(lock)) {
698 struct rt_mutex_waiter *next;
700 next = rt_mutex_top_waiter(lock);
701 rt_mutex_enqueue_pi(owner, next);
703 __rt_mutex_adjust_prio(owner);
705 /* Store the lock on which owner is blocked or NULL */
706 next_lock = task_blocked_on_lock(owner);
708 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
714 /* gets dropped in rt_mutex_adjust_prio_chain()! */
715 get_task_struct(owner);
717 raw_spin_unlock(&lock->wait_lock);
719 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
721 raw_spin_lock(&lock->wait_lock);
725 * Recheck the pi chain, in case we got a priority setting
727 * Called from sched_setscheduler
729 void rt_mutex_adjust_pi(struct task_struct *task)
731 struct rt_mutex_waiter *waiter;
732 struct rt_mutex *next_lock;
735 raw_spin_lock_irqsave(&task->pi_lock, flags);
737 waiter = task->pi_blocked_on;
738 if (!waiter || (waiter->prio == task->prio &&
739 !dl_prio(task->prio))) {
740 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
743 next_lock = waiter->lock;
744 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
746 /* gets dropped in rt_mutex_adjust_prio_chain()! */
747 get_task_struct(task);
749 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
753 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
754 * @lock: the rt_mutex to take
755 * @state: the state the task should block in (TASK_INTERRUPTIBLE
756 * or TASK_UNINTERRUPTIBLE)
757 * @timeout: the pre-initialized and started timer, or NULL for none
758 * @waiter: the pre-initialized rt_mutex_waiter
760 * lock->wait_lock must be held by the caller.
763 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
764 struct hrtimer_sleeper *timeout,
765 struct rt_mutex_waiter *waiter)
770 /* Try to acquire the lock: */
771 if (try_to_take_rt_mutex(lock, current, waiter))
775 * TASK_INTERRUPTIBLE checks for signals and
776 * timeout. Ignored otherwise.
778 if (unlikely(state == TASK_INTERRUPTIBLE)) {
779 /* Signal pending? */
780 if (signal_pending(current))
782 if (timeout && !timeout->task)
788 raw_spin_unlock(&lock->wait_lock);
790 debug_rt_mutex_print_deadlock(waiter);
792 schedule_rt_mutex(lock);
794 raw_spin_lock(&lock->wait_lock);
795 set_current_state(state);
801 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
802 struct rt_mutex_waiter *w)
805 * If the result is not -EDEADLOCK or the caller requested
806 * deadlock detection, nothing to do here.
808 if (res != -EDEADLOCK || detect_deadlock)
812 * Yell lowdly and stop the task right here.
814 rt_mutex_print_deadlock(w);
816 set_current_state(TASK_INTERRUPTIBLE);
822 * Slow path lock function:
825 rt_mutex_slowlock(struct rt_mutex *lock, int state,
826 struct hrtimer_sleeper *timeout,
829 struct rt_mutex_waiter waiter;
832 debug_rt_mutex_init_waiter(&waiter);
833 RB_CLEAR_NODE(&waiter.pi_tree_entry);
834 RB_CLEAR_NODE(&waiter.tree_entry);
836 raw_spin_lock(&lock->wait_lock);
838 /* Try to acquire the lock again: */
839 if (try_to_take_rt_mutex(lock, current, NULL)) {
840 raw_spin_unlock(&lock->wait_lock);
844 set_current_state(state);
846 /* Setup the timer, when timeout != NULL */
847 if (unlikely(timeout)) {
848 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
849 if (!hrtimer_active(&timeout->timer))
850 timeout->task = NULL;
853 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
856 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
858 set_current_state(TASK_RUNNING);
861 remove_waiter(lock, &waiter);
862 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
866 * try_to_take_rt_mutex() sets the waiter bit
867 * unconditionally. We might have to fix that up.
869 fixup_rt_mutex_waiters(lock);
871 raw_spin_unlock(&lock->wait_lock);
873 /* Remove pending timer: */
874 if (unlikely(timeout))
875 hrtimer_cancel(&timeout->timer);
877 debug_rt_mutex_free_waiter(&waiter);
883 * Slow path try-lock function:
886 rt_mutex_slowtrylock(struct rt_mutex *lock)
890 raw_spin_lock(&lock->wait_lock);
892 if (likely(rt_mutex_owner(lock) != current)) {
894 ret = try_to_take_rt_mutex(lock, current, NULL);
896 * try_to_take_rt_mutex() sets the lock waiters
897 * bit unconditionally. Clean this up.
899 fixup_rt_mutex_waiters(lock);
902 raw_spin_unlock(&lock->wait_lock);
908 * Slow path to release a rt-mutex:
911 rt_mutex_slowunlock(struct rt_mutex *lock)
913 raw_spin_lock(&lock->wait_lock);
915 debug_rt_mutex_unlock(lock);
917 rt_mutex_deadlock_account_unlock(current);
919 if (!rt_mutex_has_waiters(lock)) {
921 raw_spin_unlock(&lock->wait_lock);
925 wakeup_next_waiter(lock);
927 raw_spin_unlock(&lock->wait_lock);
929 /* Undo pi boosting if necessary: */
930 rt_mutex_adjust_prio(current);
934 * debug aware fast / slowpath lock,trylock,unlock
936 * The atomic acquire/release ops are compiled away, when either the
937 * architecture does not support cmpxchg or when debugging is enabled.
940 rt_mutex_fastlock(struct rt_mutex *lock, int state,
942 int (*slowfn)(struct rt_mutex *lock, int state,
943 struct hrtimer_sleeper *timeout,
944 int detect_deadlock))
946 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
947 rt_mutex_deadlock_account_lock(lock, current);
950 return slowfn(lock, state, NULL, detect_deadlock);
954 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
955 struct hrtimer_sleeper *timeout, int detect_deadlock,
956 int (*slowfn)(struct rt_mutex *lock, int state,
957 struct hrtimer_sleeper *timeout,
958 int detect_deadlock))
960 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
961 rt_mutex_deadlock_account_lock(lock, current);
964 return slowfn(lock, state, timeout, detect_deadlock);
968 rt_mutex_fasttrylock(struct rt_mutex *lock,
969 int (*slowfn)(struct rt_mutex *lock))
971 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
972 rt_mutex_deadlock_account_lock(lock, current);
979 rt_mutex_fastunlock(struct rt_mutex *lock,
980 void (*slowfn)(struct rt_mutex *lock))
982 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
983 rt_mutex_deadlock_account_unlock(current);
989 * rt_mutex_lock - lock a rt_mutex
991 * @lock: the rt_mutex to be locked
993 void __sched rt_mutex_lock(struct rt_mutex *lock)
997 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
999 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1002 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1004 * @lock: the rt_mutex to be locked
1005 * @detect_deadlock: deadlock detection on/off
1009 * -EINTR when interrupted by a signal
1010 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1012 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1013 int detect_deadlock)
1017 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1018 detect_deadlock, rt_mutex_slowlock);
1020 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1023 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1024 * the timeout structure is provided
1027 * @lock: the rt_mutex to be locked
1028 * @timeout: timeout structure or NULL (no timeout)
1029 * @detect_deadlock: deadlock detection on/off
1033 * -EINTR when interrupted by a signal
1034 * -ETIMEDOUT when the timeout expired
1035 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1038 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1039 int detect_deadlock)
1043 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1044 detect_deadlock, rt_mutex_slowlock);
1046 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1049 * rt_mutex_trylock - try to lock a rt_mutex
1051 * @lock: the rt_mutex to be locked
1053 * Returns 1 on success and 0 on contention
1055 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1057 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1059 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1062 * rt_mutex_unlock - unlock a rt_mutex
1064 * @lock: the rt_mutex to be unlocked
1066 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1068 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1070 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1073 * rt_mutex_destroy - mark a mutex unusable
1074 * @lock: the mutex to be destroyed
1076 * This function marks the mutex uninitialized, and any subsequent
1077 * use of the mutex is forbidden. The mutex must not be locked when
1078 * this function is called.
1080 void rt_mutex_destroy(struct rt_mutex *lock)
1082 WARN_ON(rt_mutex_is_locked(lock));
1083 #ifdef CONFIG_DEBUG_RT_MUTEXES
1088 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1091 * __rt_mutex_init - initialize the rt lock
1093 * @lock: the rt lock to be initialized
1095 * Initialize the rt lock to unlocked state.
1097 * Initializing of a locked rt lock is not allowed
1099 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1102 raw_spin_lock_init(&lock->wait_lock);
1103 lock->waiters = RB_ROOT;
1104 lock->waiters_leftmost = NULL;
1106 debug_rt_mutex_init(lock, name);
1108 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1111 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1114 * @lock: the rt_mutex to be locked
1115 * @proxy_owner:the task to set as owner
1117 * No locking. Caller has to do serializing itself
1118 * Special API call for PI-futex support
1120 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1121 struct task_struct *proxy_owner)
1123 __rt_mutex_init(lock, NULL);
1124 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1125 rt_mutex_set_owner(lock, proxy_owner);
1126 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1130 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1132 * @lock: the rt_mutex to be locked
1134 * No locking. Caller has to do serializing itself
1135 * Special API call for PI-futex support
1137 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1138 struct task_struct *proxy_owner)
1140 debug_rt_mutex_proxy_unlock(lock);
1141 rt_mutex_set_owner(lock, NULL);
1142 rt_mutex_deadlock_account_unlock(proxy_owner);
1146 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1147 * @lock: the rt_mutex to take
1148 * @waiter: the pre-initialized rt_mutex_waiter
1149 * @task: the task to prepare
1150 * @detect_deadlock: perform deadlock detection (1) or not (0)
1153 * 0 - task blocked on lock
1154 * 1 - acquired the lock for task, caller should wake it up
1157 * Special API call for FUTEX_REQUEUE_PI support.
1159 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1160 struct rt_mutex_waiter *waiter,
1161 struct task_struct *task, int detect_deadlock)
1165 raw_spin_lock(&lock->wait_lock);
1167 if (try_to_take_rt_mutex(lock, task, NULL)) {
1168 raw_spin_unlock(&lock->wait_lock);
1172 /* We enforce deadlock detection for futexes */
1173 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1175 if (ret && !rt_mutex_owner(lock)) {
1177 * Reset the return value. We might have
1178 * returned with -EDEADLK and the owner
1179 * released the lock while we were walking the
1180 * pi chain. Let the waiter sort it out.
1186 remove_waiter(lock, waiter);
1188 raw_spin_unlock(&lock->wait_lock);
1190 debug_rt_mutex_print_deadlock(waiter);
1196 * rt_mutex_next_owner - return the next owner of the lock
1198 * @lock: the rt lock query
1200 * Returns the next owner of the lock or NULL
1202 * Caller has to serialize against other accessors to the lock
1205 * Special API call for PI-futex support
1207 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1209 if (!rt_mutex_has_waiters(lock))
1212 return rt_mutex_top_waiter(lock)->task;
1216 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1217 * @lock: the rt_mutex we were woken on
1218 * @to: the timeout, null if none. hrtimer should already have
1220 * @waiter: the pre-initialized rt_mutex_waiter
1221 * @detect_deadlock: perform deadlock detection (1) or not (0)
1223 * Complete the lock acquisition started our behalf by another thread.
1227 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1229 * Special API call for PI-futex requeue support
1231 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1232 struct hrtimer_sleeper *to,
1233 struct rt_mutex_waiter *waiter,
1234 int detect_deadlock)
1238 raw_spin_lock(&lock->wait_lock);
1240 set_current_state(TASK_INTERRUPTIBLE);
1242 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1244 set_current_state(TASK_RUNNING);
1247 remove_waiter(lock, waiter);
1250 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1251 * have to fix that up.
1253 fixup_rt_mutex_waiters(lock);
1255 raw_spin_unlock(&lock->wait_lock);