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);
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
93 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
94 __releases(lock->wait_lock)
96 struct task_struct *owner = rt_mutex_owner(lock);
98 clear_rt_mutex_waiters(lock);
99 raw_spin_unlock(&lock->wait_lock);
101 * If a new waiter comes in between the unlock and the cmpxchg
102 * we have two situations:
106 * cmpxchg(p, owner, 0) == owner
107 * mark_rt_mutex_waiters(lock);
113 * mark_rt_mutex_waiters(lock);
115 * cmpxchg(p, owner, 0) != owner
124 return rt_mutex_cmpxchg(lock, owner, NULL);
128 # define rt_mutex_cmpxchg(l,c,n) (0)
129 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
131 lock->owner = (struct task_struct *)
132 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
136 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
138 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
139 __releases(lock->wait_lock)
142 raw_spin_unlock(&lock->wait_lock);
148 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
149 struct rt_mutex_waiter *right)
151 if (left->prio < right->prio)
155 * If both waiters have dl_prio(), we check the deadlines of the
157 * If left waiter has a dl_prio(), and we didn't return 1 above,
158 * then right waiter has a dl_prio() too.
160 if (dl_prio(left->prio))
161 return (left->task->dl.deadline < right->task->dl.deadline);
167 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
169 struct rb_node **link = &lock->waiters.rb_node;
170 struct rb_node *parent = NULL;
171 struct rt_mutex_waiter *entry;
176 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
177 if (rt_mutex_waiter_less(waiter, entry)) {
178 link = &parent->rb_left;
180 link = &parent->rb_right;
186 lock->waiters_leftmost = &waiter->tree_entry;
188 rb_link_node(&waiter->tree_entry, parent, link);
189 rb_insert_color(&waiter->tree_entry, &lock->waiters);
193 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
195 if (RB_EMPTY_NODE(&waiter->tree_entry))
198 if (lock->waiters_leftmost == &waiter->tree_entry)
199 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
201 rb_erase(&waiter->tree_entry, &lock->waiters);
202 RB_CLEAR_NODE(&waiter->tree_entry);
206 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
208 struct rb_node **link = &task->pi_waiters.rb_node;
209 struct rb_node *parent = NULL;
210 struct rt_mutex_waiter *entry;
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;
219 link = &parent->rb_right;
225 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
227 rb_link_node(&waiter->pi_tree_entry, parent, link);
228 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
232 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
234 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
237 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
238 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
240 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
241 RB_CLEAR_NODE(&waiter->pi_tree_entry);
245 * Calculate task priority from the waiter tree priority
247 * Return task->normal_prio when the waiter tree is empty or when
248 * the waiter is not allowed to do priority boosting
250 int rt_mutex_getprio(struct task_struct *task)
252 if (likely(!task_has_pi_waiters(task)))
253 return task->normal_prio;
255 return min(task_top_pi_waiter(task)->prio,
259 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
261 if (likely(!task_has_pi_waiters(task)))
264 return task_top_pi_waiter(task)->task;
268 * Adjust the priority of a task, after its pi_waiters got modified.
270 * This can be both boosting and unboosting. task->pi_lock must be held.
272 static void __rt_mutex_adjust_prio(struct task_struct *task)
274 int prio = rt_mutex_getprio(task);
276 if (task->prio != prio || dl_prio(prio))
277 rt_mutex_setprio(task, prio);
281 * Adjust task priority (undo boosting). Called from the exit path of
282 * rt_mutex_slowunlock() and rt_mutex_slowlock().
284 * (Note: We do this outside of the protection of lock->wait_lock to
285 * allow the lock to be taken while or before we readjust the priority
286 * of task. We do not use the spin_xx_mutex() variants here as we are
287 * outside of the debug path.)
289 static void rt_mutex_adjust_prio(struct task_struct *task)
293 raw_spin_lock_irqsave(&task->pi_lock, flags);
294 __rt_mutex_adjust_prio(task);
295 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
299 * Max number of times we'll walk the boosting chain:
301 int max_lock_depth = 1024;
303 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
305 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
309 * Adjust the priority chain. Also used for deadlock detection.
310 * Decreases task's usage by one - may thus free the task.
312 * @task: the task owning the mutex (owner) for which a chain walk is
314 * @deadlock_detect: do we have to carry out deadlock detection?
315 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
316 * things for a task that has just got its priority adjusted, and
317 * is waiting on a mutex)
318 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
319 * we dropped its pi_lock. Is never dereferenced, only used for
320 * comparison to detect lock chain changes.
321 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
322 * its priority to the mutex owner (can be NULL in the case
323 * depicted above or if the top waiter is gone away and we are
324 * actually deboosting the owner)
325 * @top_task: the current top waiter
327 * Returns 0 or -EDEADLK.
329 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
331 struct rt_mutex *orig_lock,
332 struct rt_mutex *next_lock,
333 struct rt_mutex_waiter *orig_waiter,
334 struct task_struct *top_task)
336 struct rt_mutex *lock;
337 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
338 int detect_deadlock, ret = 0, depth = 0;
341 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
345 * The (de)boosting is a step by step approach with a lot of
346 * pitfalls. We want this to be preemptible and we want hold a
347 * maximum of two locks per step. So we have to check
348 * carefully whether things change under us.
351 if (++depth > max_lock_depth) {
355 * Print this only once. If the admin changes the limit,
356 * print a new message when reaching the limit again.
358 if (prev_max != max_lock_depth) {
359 prev_max = max_lock_depth;
360 printk(KERN_WARNING "Maximum lock depth %d reached "
361 "task: %s (%d)\n", max_lock_depth,
362 top_task->comm, task_pid_nr(top_task));
364 put_task_struct(task);
370 * Task can not go away as we did a get_task() before !
372 raw_spin_lock_irqsave(&task->pi_lock, flags);
374 waiter = task->pi_blocked_on;
376 * Check whether the end of the boosting chain has been
377 * reached or the state of the chain has changed while we
384 * Check the orig_waiter state. After we dropped the locks,
385 * the previous owner of the lock might have released the lock.
387 if (orig_waiter && !rt_mutex_owner(orig_lock))
391 * We dropped all locks after taking a refcount on @task, so
392 * the task might have moved on in the lock chain or even left
393 * the chain completely and blocks now on an unrelated lock or
396 * We stored the lock on which @task was blocked in @next_lock,
397 * so we can detect the chain change.
399 if (next_lock != waiter->lock)
403 * Drop out, when the task has no waiters. Note,
404 * top_waiter can be NULL, when we are in the deboosting
408 if (!task_has_pi_waiters(task))
411 * If deadlock detection is off, we stop here if we
412 * are not the top pi waiter of the task.
414 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
419 * When deadlock detection is off then we check, if further
420 * priority adjustment is necessary.
422 if (!detect_deadlock && waiter->prio == task->prio)
426 if (!raw_spin_trylock(&lock->wait_lock)) {
427 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
433 * Deadlock detection. If the lock is the same as the original
434 * lock which caused us to walk the lock chain or if the
435 * current lock is owned by the task which initiated the chain
436 * walk, we detected a deadlock.
438 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
439 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
440 raw_spin_unlock(&lock->wait_lock);
445 top_waiter = rt_mutex_top_waiter(lock);
447 /* Requeue the waiter */
448 rt_mutex_dequeue(lock, waiter);
449 waiter->prio = task->prio;
450 rt_mutex_enqueue(lock, waiter);
452 /* Release the task */
453 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
454 if (!rt_mutex_owner(lock)) {
456 * If the requeue above changed the top waiter, then we need
457 * to wake the new top waiter up to try to get the lock.
460 if (top_waiter != rt_mutex_top_waiter(lock))
461 wake_up_process(rt_mutex_top_waiter(lock)->task);
462 raw_spin_unlock(&lock->wait_lock);
465 put_task_struct(task);
467 /* Grab the next task */
468 task = rt_mutex_owner(lock);
469 get_task_struct(task);
470 raw_spin_lock_irqsave(&task->pi_lock, flags);
472 if (waiter == rt_mutex_top_waiter(lock)) {
473 /* Boost the owner */
474 rt_mutex_dequeue_pi(task, top_waiter);
475 rt_mutex_enqueue_pi(task, waiter);
476 __rt_mutex_adjust_prio(task);
478 } else if (top_waiter == waiter) {
479 /* Deboost the owner */
480 rt_mutex_dequeue_pi(task, waiter);
481 waiter = rt_mutex_top_waiter(lock);
482 rt_mutex_enqueue_pi(task, waiter);
483 __rt_mutex_adjust_prio(task);
487 * Check whether the task which owns the current lock is pi
488 * blocked itself. If yes we store a pointer to the lock for
489 * the lock chain change detection above. After we dropped
490 * task->pi_lock next_lock cannot be dereferenced anymore.
492 next_lock = task_blocked_on_lock(task);
494 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
496 top_waiter = rt_mutex_top_waiter(lock);
497 raw_spin_unlock(&lock->wait_lock);
500 * We reached the end of the lock chain. Stop right here. No
501 * point to go back just to figure that out.
506 if (!detect_deadlock && waiter != top_waiter)
512 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
514 put_task_struct(task);
520 * Try to take an rt-mutex
522 * Must be called with lock->wait_lock held.
524 * @lock: the lock to be acquired.
525 * @task: the task which wants to acquire the lock
526 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
528 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
529 struct rt_mutex_waiter *waiter)
532 * We have to be careful here if the atomic speedups are
533 * enabled, such that, when
534 * - no other waiter is on the lock
535 * - the lock has been released since we did the cmpxchg
536 * the lock can be released or taken while we are doing the
537 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
539 * The atomic acquire/release aware variant of
540 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
541 * the WAITERS bit, the atomic release / acquire can not
542 * happen anymore and lock->wait_lock protects us from the
545 * Note, that this might set lock->owner =
546 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
547 * any more. This is fixed up when we take the ownership.
548 * This is the transitional state explained at the top of this file.
550 mark_rt_mutex_waiters(lock);
552 if (rt_mutex_owner(lock))
556 * It will get the lock because of one of these conditions:
557 * 1) there is no waiter
558 * 2) higher priority than waiters
559 * 3) it is top waiter
561 if (rt_mutex_has_waiters(lock)) {
562 if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
563 if (!waiter || waiter != rt_mutex_top_waiter(lock))
568 if (waiter || rt_mutex_has_waiters(lock)) {
570 struct rt_mutex_waiter *top;
572 raw_spin_lock_irqsave(&task->pi_lock, flags);
574 /* remove the queued waiter. */
576 rt_mutex_dequeue(lock, waiter);
577 task->pi_blocked_on = NULL;
581 * We have to enqueue the top waiter(if it exists) into
582 * task->pi_waiters list.
584 if (rt_mutex_has_waiters(lock)) {
585 top = rt_mutex_top_waiter(lock);
586 rt_mutex_enqueue_pi(task, top);
588 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
591 /* We got the lock. */
592 debug_rt_mutex_lock(lock);
594 rt_mutex_set_owner(lock, task);
596 rt_mutex_deadlock_account_lock(lock, task);
602 * Task blocks on lock.
604 * Prepare waiter and propagate pi chain
606 * This must be called with lock->wait_lock held.
608 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
609 struct rt_mutex_waiter *waiter,
610 struct task_struct *task,
613 struct task_struct *owner = rt_mutex_owner(lock);
614 struct rt_mutex_waiter *top_waiter = waiter;
615 struct rt_mutex *next_lock;
616 int chain_walk = 0, res;
620 * Early deadlock detection. We really don't want the task to
621 * enqueue on itself just to untangle the mess later. It's not
622 * only an optimization. We drop the locks, so another waiter
623 * can come in before the chain walk detects the deadlock. So
624 * the other will detect the deadlock and return -EDEADLOCK,
625 * which is wrong, as the other waiter is not in a deadlock
631 raw_spin_lock_irqsave(&task->pi_lock, flags);
632 __rt_mutex_adjust_prio(task);
635 waiter->prio = task->prio;
637 /* Get the top priority waiter on the lock */
638 if (rt_mutex_has_waiters(lock))
639 top_waiter = rt_mutex_top_waiter(lock);
640 rt_mutex_enqueue(lock, waiter);
642 task->pi_blocked_on = waiter;
644 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
649 raw_spin_lock_irqsave(&owner->pi_lock, flags);
650 if (waiter == rt_mutex_top_waiter(lock)) {
651 rt_mutex_dequeue_pi(owner, top_waiter);
652 rt_mutex_enqueue_pi(owner, waiter);
654 __rt_mutex_adjust_prio(owner);
655 if (owner->pi_blocked_on)
657 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
661 /* Store the lock on which owner is blocked or NULL */
662 next_lock = task_blocked_on_lock(owner);
664 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
666 * Even if full deadlock detection is on, if the owner is not
667 * blocked itself, we can avoid finding this out in the chain
670 if (!chain_walk || !next_lock)
674 * The owner can't disappear while holding a lock,
675 * so the owner struct is protected by wait_lock.
676 * Gets dropped in rt_mutex_adjust_prio_chain()!
678 get_task_struct(owner);
680 raw_spin_unlock(&lock->wait_lock);
682 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
683 next_lock, waiter, task);
685 raw_spin_lock(&lock->wait_lock);
691 * Wake up the next waiter on the lock.
693 * Remove the top waiter from the current tasks pi waiter list and
696 * Called with lock->wait_lock held.
698 static void wakeup_next_waiter(struct rt_mutex *lock)
700 struct rt_mutex_waiter *waiter;
703 raw_spin_lock_irqsave(¤t->pi_lock, flags);
705 waiter = rt_mutex_top_waiter(lock);
708 * Remove it from current->pi_waiters. We do not adjust a
709 * possible priority boost right now. We execute wakeup in the
710 * boosted mode and go back to normal after releasing
713 rt_mutex_dequeue_pi(current, waiter);
716 * As we are waking up the top waiter, and the waiter stays
717 * queued on the lock until it gets the lock, this lock
718 * obviously has waiters. Just set the bit here and this has
719 * the added benefit of forcing all new tasks into the
720 * slow path making sure no task of lower priority than
721 * the top waiter can steal this lock.
723 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
725 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
728 * It's safe to dereference waiter as it cannot go away as
729 * long as we hold lock->wait_lock. The waiter task needs to
730 * acquire it in order to dequeue the waiter.
732 wake_up_process(waiter->task);
736 * Remove a waiter from a lock and give up
738 * Must be called with lock->wait_lock held and
739 * have just failed to try_to_take_rt_mutex().
741 static void remove_waiter(struct rt_mutex *lock,
742 struct rt_mutex_waiter *waiter)
744 int first = (waiter == rt_mutex_top_waiter(lock));
745 struct task_struct *owner = rt_mutex_owner(lock);
746 struct rt_mutex *next_lock = NULL;
749 raw_spin_lock_irqsave(¤t->pi_lock, flags);
750 rt_mutex_dequeue(lock, waiter);
751 current->pi_blocked_on = NULL;
752 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
759 raw_spin_lock_irqsave(&owner->pi_lock, flags);
761 rt_mutex_dequeue_pi(owner, waiter);
763 if (rt_mutex_has_waiters(lock)) {
764 struct rt_mutex_waiter *next;
766 next = rt_mutex_top_waiter(lock);
767 rt_mutex_enqueue_pi(owner, next);
769 __rt_mutex_adjust_prio(owner);
771 /* Store the lock on which owner is blocked or NULL */
772 next_lock = task_blocked_on_lock(owner);
774 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
780 /* gets dropped in rt_mutex_adjust_prio_chain()! */
781 get_task_struct(owner);
783 raw_spin_unlock(&lock->wait_lock);
785 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
787 raw_spin_lock(&lock->wait_lock);
791 * Recheck the pi chain, in case we got a priority setting
793 * Called from sched_setscheduler
795 void rt_mutex_adjust_pi(struct task_struct *task)
797 struct rt_mutex_waiter *waiter;
798 struct rt_mutex *next_lock;
801 raw_spin_lock_irqsave(&task->pi_lock, flags);
803 waiter = task->pi_blocked_on;
804 if (!waiter || (waiter->prio == task->prio &&
805 !dl_prio(task->prio))) {
806 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
809 next_lock = waiter->lock;
810 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
812 /* gets dropped in rt_mutex_adjust_prio_chain()! */
813 get_task_struct(task);
815 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
819 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
820 * @lock: the rt_mutex to take
821 * @state: the state the task should block in (TASK_INTERRUPTIBLE
822 * or TASK_UNINTERRUPTIBLE)
823 * @timeout: the pre-initialized and started timer, or NULL for none
824 * @waiter: the pre-initialized rt_mutex_waiter
826 * lock->wait_lock must be held by the caller.
829 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
830 struct hrtimer_sleeper *timeout,
831 struct rt_mutex_waiter *waiter)
836 /* Try to acquire the lock: */
837 if (try_to_take_rt_mutex(lock, current, waiter))
841 * TASK_INTERRUPTIBLE checks for signals and
842 * timeout. Ignored otherwise.
844 if (unlikely(state == TASK_INTERRUPTIBLE)) {
845 /* Signal pending? */
846 if (signal_pending(current))
848 if (timeout && !timeout->task)
854 raw_spin_unlock(&lock->wait_lock);
856 debug_rt_mutex_print_deadlock(waiter);
858 schedule_rt_mutex(lock);
860 raw_spin_lock(&lock->wait_lock);
861 set_current_state(state);
867 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
868 struct rt_mutex_waiter *w)
871 * If the result is not -EDEADLOCK or the caller requested
872 * deadlock detection, nothing to do here.
874 if (res != -EDEADLOCK || detect_deadlock)
878 * Yell lowdly and stop the task right here.
880 rt_mutex_print_deadlock(w);
882 set_current_state(TASK_INTERRUPTIBLE);
888 * Slow path lock function:
891 rt_mutex_slowlock(struct rt_mutex *lock, int state,
892 struct hrtimer_sleeper *timeout,
895 struct rt_mutex_waiter waiter;
898 debug_rt_mutex_init_waiter(&waiter);
899 RB_CLEAR_NODE(&waiter.pi_tree_entry);
900 RB_CLEAR_NODE(&waiter.tree_entry);
902 raw_spin_lock(&lock->wait_lock);
904 /* Try to acquire the lock again: */
905 if (try_to_take_rt_mutex(lock, current, NULL)) {
906 raw_spin_unlock(&lock->wait_lock);
910 set_current_state(state);
912 /* Setup the timer, when timeout != NULL */
913 if (unlikely(timeout)) {
914 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
915 if (!hrtimer_active(&timeout->timer))
916 timeout->task = NULL;
919 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
922 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
924 set_current_state(TASK_RUNNING);
927 remove_waiter(lock, &waiter);
928 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
932 * try_to_take_rt_mutex() sets the waiter bit
933 * unconditionally. We might have to fix that up.
935 fixup_rt_mutex_waiters(lock);
937 raw_spin_unlock(&lock->wait_lock);
939 /* Remove pending timer: */
940 if (unlikely(timeout))
941 hrtimer_cancel(&timeout->timer);
943 debug_rt_mutex_free_waiter(&waiter);
949 * Slow path try-lock function:
952 rt_mutex_slowtrylock(struct rt_mutex *lock)
956 raw_spin_lock(&lock->wait_lock);
958 if (likely(rt_mutex_owner(lock) != current)) {
960 ret = try_to_take_rt_mutex(lock, current, NULL);
962 * try_to_take_rt_mutex() sets the lock waiters
963 * bit unconditionally. Clean this up.
965 fixup_rt_mutex_waiters(lock);
968 raw_spin_unlock(&lock->wait_lock);
974 * Slow path to release a rt-mutex:
977 rt_mutex_slowunlock(struct rt_mutex *lock)
979 raw_spin_lock(&lock->wait_lock);
981 debug_rt_mutex_unlock(lock);
983 rt_mutex_deadlock_account_unlock(current);
986 * We must be careful here if the fast path is enabled. If we
987 * have no waiters queued we cannot set owner to NULL here
990 * foo->lock->owner = NULL;
991 * rtmutex_lock(foo->lock); <- fast path
992 * free = atomic_dec_and_test(foo->refcnt);
993 * rtmutex_unlock(foo->lock); <- fast path
996 * raw_spin_unlock(foo->lock->wait_lock);
998 * So for the fastpath enabled kernel:
1000 * Nothing can set the waiters bit as long as we hold
1001 * lock->wait_lock. So we do the following sequence:
1003 * owner = rt_mutex_owner(lock);
1004 * clear_rt_mutex_waiters(lock);
1005 * raw_spin_unlock(&lock->wait_lock);
1006 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1010 * The fastpath disabled variant is simple as all access to
1011 * lock->owner is serialized by lock->wait_lock:
1013 * lock->owner = NULL;
1014 * raw_spin_unlock(&lock->wait_lock);
1016 while (!rt_mutex_has_waiters(lock)) {
1017 /* Drops lock->wait_lock ! */
1018 if (unlock_rt_mutex_safe(lock) == true)
1020 /* Relock the rtmutex and try again */
1021 raw_spin_lock(&lock->wait_lock);
1025 * The wakeup next waiter path does not suffer from the above
1026 * race. See the comments there.
1028 wakeup_next_waiter(lock);
1030 raw_spin_unlock(&lock->wait_lock);
1032 /* Undo pi boosting if necessary: */
1033 rt_mutex_adjust_prio(current);
1037 * debug aware fast / slowpath lock,trylock,unlock
1039 * The atomic acquire/release ops are compiled away, when either the
1040 * architecture does not support cmpxchg or when debugging is enabled.
1043 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1044 int detect_deadlock,
1045 int (*slowfn)(struct rt_mutex *lock, int state,
1046 struct hrtimer_sleeper *timeout,
1047 int detect_deadlock))
1049 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1050 rt_mutex_deadlock_account_lock(lock, current);
1053 return slowfn(lock, state, NULL, detect_deadlock);
1057 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1058 struct hrtimer_sleeper *timeout, int detect_deadlock,
1059 int (*slowfn)(struct rt_mutex *lock, int state,
1060 struct hrtimer_sleeper *timeout,
1061 int detect_deadlock))
1063 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1064 rt_mutex_deadlock_account_lock(lock, current);
1067 return slowfn(lock, state, timeout, detect_deadlock);
1071 rt_mutex_fasttrylock(struct rt_mutex *lock,
1072 int (*slowfn)(struct rt_mutex *lock))
1074 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1075 rt_mutex_deadlock_account_lock(lock, current);
1078 return slowfn(lock);
1082 rt_mutex_fastunlock(struct rt_mutex *lock,
1083 void (*slowfn)(struct rt_mutex *lock))
1085 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1086 rt_mutex_deadlock_account_unlock(current);
1092 * rt_mutex_lock - lock a rt_mutex
1094 * @lock: the rt_mutex to be locked
1096 void __sched rt_mutex_lock(struct rt_mutex *lock)
1100 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1102 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1105 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1107 * @lock: the rt_mutex to be locked
1108 * @detect_deadlock: deadlock detection on/off
1112 * -EINTR when interrupted by a signal
1113 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1115 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1116 int detect_deadlock)
1120 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1121 detect_deadlock, rt_mutex_slowlock);
1123 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1126 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1127 * the timeout structure is provided
1130 * @lock: the rt_mutex to be locked
1131 * @timeout: timeout structure or NULL (no timeout)
1132 * @detect_deadlock: deadlock detection on/off
1136 * -EINTR when interrupted by a signal
1137 * -ETIMEDOUT when the timeout expired
1138 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1141 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1142 int detect_deadlock)
1146 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1147 detect_deadlock, rt_mutex_slowlock);
1149 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1152 * rt_mutex_trylock - try to lock a rt_mutex
1154 * @lock: the rt_mutex to be locked
1156 * Returns 1 on success and 0 on contention
1158 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1160 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1162 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1165 * rt_mutex_unlock - unlock a rt_mutex
1167 * @lock: the rt_mutex to be unlocked
1169 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1171 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1173 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1176 * rt_mutex_destroy - mark a mutex unusable
1177 * @lock: the mutex to be destroyed
1179 * This function marks the mutex uninitialized, and any subsequent
1180 * use of the mutex is forbidden. The mutex must not be locked when
1181 * this function is called.
1183 void rt_mutex_destroy(struct rt_mutex *lock)
1185 WARN_ON(rt_mutex_is_locked(lock));
1186 #ifdef CONFIG_DEBUG_RT_MUTEXES
1191 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1194 * __rt_mutex_init - initialize the rt lock
1196 * @lock: the rt lock to be initialized
1198 * Initialize the rt lock to unlocked state.
1200 * Initializing of a locked rt lock is not allowed
1202 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1205 raw_spin_lock_init(&lock->wait_lock);
1206 lock->waiters = RB_ROOT;
1207 lock->waiters_leftmost = NULL;
1209 debug_rt_mutex_init(lock, name);
1211 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1214 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1217 * @lock: the rt_mutex to be locked
1218 * @proxy_owner:the task to set as owner
1220 * No locking. Caller has to do serializing itself
1221 * Special API call for PI-futex support
1223 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1224 struct task_struct *proxy_owner)
1226 __rt_mutex_init(lock, NULL);
1227 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1228 rt_mutex_set_owner(lock, proxy_owner);
1229 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1233 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1235 * @lock: the rt_mutex to be locked
1237 * No locking. Caller has to do serializing itself
1238 * Special API call for PI-futex support
1240 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1241 struct task_struct *proxy_owner)
1243 debug_rt_mutex_proxy_unlock(lock);
1244 rt_mutex_set_owner(lock, NULL);
1245 rt_mutex_deadlock_account_unlock(proxy_owner);
1249 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1250 * @lock: the rt_mutex to take
1251 * @waiter: the pre-initialized rt_mutex_waiter
1252 * @task: the task to prepare
1253 * @detect_deadlock: perform deadlock detection (1) or not (0)
1256 * 0 - task blocked on lock
1257 * 1 - acquired the lock for task, caller should wake it up
1260 * Special API call for FUTEX_REQUEUE_PI support.
1262 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1263 struct rt_mutex_waiter *waiter,
1264 struct task_struct *task, int detect_deadlock)
1268 raw_spin_lock(&lock->wait_lock);
1270 if (try_to_take_rt_mutex(lock, task, NULL)) {
1271 raw_spin_unlock(&lock->wait_lock);
1275 /* We enforce deadlock detection for futexes */
1276 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1278 if (ret && !rt_mutex_owner(lock)) {
1280 * Reset the return value. We might have
1281 * returned with -EDEADLK and the owner
1282 * released the lock while we were walking the
1283 * pi chain. Let the waiter sort it out.
1289 remove_waiter(lock, waiter);
1291 raw_spin_unlock(&lock->wait_lock);
1293 debug_rt_mutex_print_deadlock(waiter);
1299 * rt_mutex_next_owner - return the next owner of the lock
1301 * @lock: the rt lock query
1303 * Returns the next owner of the lock or NULL
1305 * Caller has to serialize against other accessors to the lock
1308 * Special API call for PI-futex support
1310 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1312 if (!rt_mutex_has_waiters(lock))
1315 return rt_mutex_top_waiter(lock)->task;
1319 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1320 * @lock: the rt_mutex we were woken on
1321 * @to: the timeout, null if none. hrtimer should already have
1323 * @waiter: the pre-initialized rt_mutex_waiter
1324 * @detect_deadlock: perform deadlock detection (1) or not (0)
1326 * Complete the lock acquisition started our behalf by another thread.
1330 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1332 * Special API call for PI-futex requeue support
1334 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1335 struct hrtimer_sleeper *to,
1336 struct rt_mutex_waiter *waiter,
1337 int detect_deadlock)
1341 raw_spin_lock(&lock->wait_lock);
1343 set_current_state(TASK_INTERRUPTIBLE);
1345 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1347 set_current_state(TASK_RUNNING);
1350 remove_waiter(lock, waiter);
1353 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1354 * have to fix that up.
1356 fixup_rt_mutex_waiters(lock);
1358 raw_spin_unlock(&lock->wait_lock);