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);
314 return deadlock_detect ? -EDEADLK : 0;
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);
389 ret = deadlock_detect ? -EDEADLK : 0;
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
576 if (detect_deadlock && owner == task)
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);
802 * Slow path lock function:
805 rt_mutex_slowlock(struct rt_mutex *lock, int state,
806 struct hrtimer_sleeper *timeout,
809 struct rt_mutex_waiter waiter;
812 debug_rt_mutex_init_waiter(&waiter);
813 RB_CLEAR_NODE(&waiter.pi_tree_entry);
814 RB_CLEAR_NODE(&waiter.tree_entry);
816 raw_spin_lock(&lock->wait_lock);
818 /* Try to acquire the lock again: */
819 if (try_to_take_rt_mutex(lock, current, NULL)) {
820 raw_spin_unlock(&lock->wait_lock);
824 set_current_state(state);
826 /* Setup the timer, when timeout != NULL */
827 if (unlikely(timeout)) {
828 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
829 if (!hrtimer_active(&timeout->timer))
830 timeout->task = NULL;
833 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
836 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
838 set_current_state(TASK_RUNNING);
841 remove_waiter(lock, &waiter);
844 * try_to_take_rt_mutex() sets the waiter bit
845 * unconditionally. We might have to fix that up.
847 fixup_rt_mutex_waiters(lock);
849 raw_spin_unlock(&lock->wait_lock);
851 /* Remove pending timer: */
852 if (unlikely(timeout))
853 hrtimer_cancel(&timeout->timer);
855 debug_rt_mutex_free_waiter(&waiter);
861 * Slow path try-lock function:
864 rt_mutex_slowtrylock(struct rt_mutex *lock)
868 raw_spin_lock(&lock->wait_lock);
870 if (likely(rt_mutex_owner(lock) != current)) {
872 ret = try_to_take_rt_mutex(lock, current, NULL);
874 * try_to_take_rt_mutex() sets the lock waiters
875 * bit unconditionally. Clean this up.
877 fixup_rt_mutex_waiters(lock);
880 raw_spin_unlock(&lock->wait_lock);
886 * Slow path to release a rt-mutex:
889 rt_mutex_slowunlock(struct rt_mutex *lock)
891 raw_spin_lock(&lock->wait_lock);
893 debug_rt_mutex_unlock(lock);
895 rt_mutex_deadlock_account_unlock(current);
897 if (!rt_mutex_has_waiters(lock)) {
899 raw_spin_unlock(&lock->wait_lock);
903 wakeup_next_waiter(lock);
905 raw_spin_unlock(&lock->wait_lock);
907 /* Undo pi boosting if necessary: */
908 rt_mutex_adjust_prio(current);
912 * debug aware fast / slowpath lock,trylock,unlock
914 * The atomic acquire/release ops are compiled away, when either the
915 * architecture does not support cmpxchg or when debugging is enabled.
918 rt_mutex_fastlock(struct rt_mutex *lock, int state,
920 int (*slowfn)(struct rt_mutex *lock, int state,
921 struct hrtimer_sleeper *timeout,
922 int detect_deadlock))
924 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
925 rt_mutex_deadlock_account_lock(lock, current);
928 return slowfn(lock, state, NULL, detect_deadlock);
932 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
933 struct hrtimer_sleeper *timeout, int detect_deadlock,
934 int (*slowfn)(struct rt_mutex *lock, int state,
935 struct hrtimer_sleeper *timeout,
936 int detect_deadlock))
938 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
939 rt_mutex_deadlock_account_lock(lock, current);
942 return slowfn(lock, state, timeout, detect_deadlock);
946 rt_mutex_fasttrylock(struct rt_mutex *lock,
947 int (*slowfn)(struct rt_mutex *lock))
949 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
950 rt_mutex_deadlock_account_lock(lock, current);
957 rt_mutex_fastunlock(struct rt_mutex *lock,
958 void (*slowfn)(struct rt_mutex *lock))
960 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
961 rt_mutex_deadlock_account_unlock(current);
967 * rt_mutex_lock - lock a rt_mutex
969 * @lock: the rt_mutex to be locked
971 void __sched rt_mutex_lock(struct rt_mutex *lock)
975 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
977 EXPORT_SYMBOL_GPL(rt_mutex_lock);
980 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
982 * @lock: the rt_mutex to be locked
983 * @detect_deadlock: deadlock detection on/off
987 * -EINTR when interrupted by a signal
988 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
990 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
995 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
996 detect_deadlock, rt_mutex_slowlock);
998 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1001 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1002 * the timeout structure is provided
1005 * @lock: the rt_mutex to be locked
1006 * @timeout: timeout structure or NULL (no timeout)
1007 * @detect_deadlock: deadlock detection on/off
1011 * -EINTR when interrupted by a signal
1012 * -ETIMEDOUT when the timeout expired
1013 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1016 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1017 int detect_deadlock)
1021 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1022 detect_deadlock, rt_mutex_slowlock);
1024 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1027 * rt_mutex_trylock - try to lock a rt_mutex
1029 * @lock: the rt_mutex to be locked
1031 * Returns 1 on success and 0 on contention
1033 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1035 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1037 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1040 * rt_mutex_unlock - unlock a rt_mutex
1042 * @lock: the rt_mutex to be unlocked
1044 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1046 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1048 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1051 * rt_mutex_destroy - mark a mutex unusable
1052 * @lock: the mutex to be destroyed
1054 * This function marks the mutex uninitialized, and any subsequent
1055 * use of the mutex is forbidden. The mutex must not be locked when
1056 * this function is called.
1058 void rt_mutex_destroy(struct rt_mutex *lock)
1060 WARN_ON(rt_mutex_is_locked(lock));
1061 #ifdef CONFIG_DEBUG_RT_MUTEXES
1066 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1069 * __rt_mutex_init - initialize the rt lock
1071 * @lock: the rt lock to be initialized
1073 * Initialize the rt lock to unlocked state.
1075 * Initializing of a locked rt lock is not allowed
1077 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1080 raw_spin_lock_init(&lock->wait_lock);
1081 lock->waiters = RB_ROOT;
1082 lock->waiters_leftmost = NULL;
1084 debug_rt_mutex_init(lock, name);
1086 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1089 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1092 * @lock: the rt_mutex to be locked
1093 * @proxy_owner:the task to set as owner
1095 * No locking. Caller has to do serializing itself
1096 * Special API call for PI-futex support
1098 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1099 struct task_struct *proxy_owner)
1101 __rt_mutex_init(lock, NULL);
1102 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1103 rt_mutex_set_owner(lock, proxy_owner);
1104 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1108 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1110 * @lock: the rt_mutex to be locked
1112 * No locking. Caller has to do serializing itself
1113 * Special API call for PI-futex support
1115 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1116 struct task_struct *proxy_owner)
1118 debug_rt_mutex_proxy_unlock(lock);
1119 rt_mutex_set_owner(lock, NULL);
1120 rt_mutex_deadlock_account_unlock(proxy_owner);
1124 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1125 * @lock: the rt_mutex to take
1126 * @waiter: the pre-initialized rt_mutex_waiter
1127 * @task: the task to prepare
1128 * @detect_deadlock: perform deadlock detection (1) or not (0)
1131 * 0 - task blocked on lock
1132 * 1 - acquired the lock for task, caller should wake it up
1135 * Special API call for FUTEX_REQUEUE_PI support.
1137 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1138 struct rt_mutex_waiter *waiter,
1139 struct task_struct *task, int detect_deadlock)
1143 raw_spin_lock(&lock->wait_lock);
1145 if (try_to_take_rt_mutex(lock, task, NULL)) {
1146 raw_spin_unlock(&lock->wait_lock);
1150 ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
1152 if (ret && !rt_mutex_owner(lock)) {
1154 * Reset the return value. We might have
1155 * returned with -EDEADLK and the owner
1156 * released the lock while we were walking the
1157 * pi chain. Let the waiter sort it out.
1163 remove_waiter(lock, waiter);
1165 raw_spin_unlock(&lock->wait_lock);
1167 debug_rt_mutex_print_deadlock(waiter);
1173 * rt_mutex_next_owner - return the next owner of the lock
1175 * @lock: the rt lock query
1177 * Returns the next owner of the lock or NULL
1179 * Caller has to serialize against other accessors to the lock
1182 * Special API call for PI-futex support
1184 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1186 if (!rt_mutex_has_waiters(lock))
1189 return rt_mutex_top_waiter(lock)->task;
1193 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1194 * @lock: the rt_mutex we were woken on
1195 * @to: the timeout, null if none. hrtimer should already have
1197 * @waiter: the pre-initialized rt_mutex_waiter
1198 * @detect_deadlock: perform deadlock detection (1) or not (0)
1200 * Complete the lock acquisition started our behalf by another thread.
1204 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1206 * Special API call for PI-futex requeue support
1208 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1209 struct hrtimer_sleeper *to,
1210 struct rt_mutex_waiter *waiter,
1211 int detect_deadlock)
1215 raw_spin_lock(&lock->wait_lock);
1217 set_current_state(TASK_INTERRUPTIBLE);
1219 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1221 set_current_state(TASK_RUNNING);
1224 remove_waiter(lock, waiter);
1227 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1228 * have to fix that up.
1230 fixup_rt_mutex_waiters(lock);
1232 raw_spin_unlock(&lock->wait_lock);