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 * Called by sched_setscheduler() to check whether the priority change
269 * is overruled by a possible priority boosting.
271 int rt_mutex_check_prio(struct task_struct *task, int newprio)
273 if (!task_has_pi_waiters(task))
276 return task_top_pi_waiter(task)->task->prio <= newprio;
280 * Adjust the priority of a task, after its pi_waiters got modified.
282 * This can be both boosting and unboosting. task->pi_lock must be held.
284 static void __rt_mutex_adjust_prio(struct task_struct *task)
286 int prio = rt_mutex_getprio(task);
288 if (task->prio != prio || dl_prio(prio))
289 rt_mutex_setprio(task, prio);
293 * Adjust task priority (undo boosting). Called from the exit path of
294 * rt_mutex_slowunlock() and rt_mutex_slowlock().
296 * (Note: We do this outside of the protection of lock->wait_lock to
297 * allow the lock to be taken while or before we readjust the priority
298 * of task. We do not use the spin_xx_mutex() variants here as we are
299 * outside of the debug path.)
301 static void rt_mutex_adjust_prio(struct task_struct *task)
305 raw_spin_lock_irqsave(&task->pi_lock, flags);
306 __rt_mutex_adjust_prio(task);
307 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
311 * Max number of times we'll walk the boosting chain:
313 int max_lock_depth = 1024;
315 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
317 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
321 * Adjust the priority chain. Also used for deadlock detection.
322 * Decreases task's usage by one - may thus free the task.
324 * @task: the task owning the mutex (owner) for which a chain walk is
326 * @deadlock_detect: do we have to carry out deadlock detection?
327 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
328 * things for a task that has just got its priority adjusted, and
329 * is waiting on a mutex)
330 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
331 * we dropped its pi_lock. Is never dereferenced, only used for
332 * comparison to detect lock chain changes.
333 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
334 * its priority to the mutex owner (can be NULL in the case
335 * depicted above or if the top waiter is gone away and we are
336 * actually deboosting the owner)
337 * @top_task: the current top waiter
339 * Returns 0 or -EDEADLK.
341 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
343 struct rt_mutex *orig_lock,
344 struct rt_mutex *next_lock,
345 struct rt_mutex_waiter *orig_waiter,
346 struct task_struct *top_task)
348 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
349 struct rt_mutex_waiter *prerequeue_top_waiter;
350 int detect_deadlock, ret = 0, depth = 0;
351 struct rt_mutex *lock;
354 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
358 * The (de)boosting is a step by step approach with a lot of
359 * pitfalls. We want this to be preemptible and we want hold a
360 * maximum of two locks per step. So we have to check
361 * carefully whether things change under us.
364 if (++depth > max_lock_depth) {
368 * Print this only once. If the admin changes the limit,
369 * print a new message when reaching the limit again.
371 if (prev_max != max_lock_depth) {
372 prev_max = max_lock_depth;
373 printk(KERN_WARNING "Maximum lock depth %d reached "
374 "task: %s (%d)\n", max_lock_depth,
375 top_task->comm, task_pid_nr(top_task));
377 put_task_struct(task);
383 * Task can not go away as we did a get_task() before !
385 raw_spin_lock_irqsave(&task->pi_lock, flags);
387 waiter = task->pi_blocked_on;
389 * Check whether the end of the boosting chain has been
390 * reached or the state of the chain has changed while we
397 * Check the orig_waiter state. After we dropped the locks,
398 * the previous owner of the lock might have released the lock.
400 if (orig_waiter && !rt_mutex_owner(orig_lock))
404 * We dropped all locks after taking a refcount on @task, so
405 * the task might have moved on in the lock chain or even left
406 * the chain completely and blocks now on an unrelated lock or
409 * We stored the lock on which @task was blocked in @next_lock,
410 * so we can detect the chain change.
412 if (next_lock != waiter->lock)
416 * Drop out, when the task has no waiters. Note,
417 * top_waiter can be NULL, when we are in the deboosting
421 if (!task_has_pi_waiters(task))
424 * If deadlock detection is off, we stop here if we
425 * are not the top pi waiter of the task.
427 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
432 * When deadlock detection is off then we check, if further
433 * priority adjustment is necessary.
435 if (!detect_deadlock && waiter->prio == task->prio)
439 if (!raw_spin_trylock(&lock->wait_lock)) {
440 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
446 * Deadlock detection. If the lock is the same as the original
447 * lock which caused us to walk the lock chain or if the
448 * current lock is owned by the task which initiated the chain
449 * walk, we detected a deadlock.
451 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
452 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
453 raw_spin_unlock(&lock->wait_lock);
459 * Store the current top waiter before doing the requeue
460 * operation on @lock. We need it for the boost/deboost
463 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
465 /* Requeue the waiter in the lock waiter list. */
466 rt_mutex_dequeue(lock, waiter);
467 waiter->prio = task->prio;
468 rt_mutex_enqueue(lock, waiter);
470 /* Release the task */
471 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
472 put_task_struct(task);
475 * We must abort the chain walk if there is no lock owner even
476 * in the dead lock detection case, as we have nothing to
477 * follow here. This is the end of the chain we are walking.
479 if (!rt_mutex_owner(lock)) {
481 * If the requeue above changed the top waiter, then we need
482 * to wake the new top waiter up to try to get the lock.
484 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
485 wake_up_process(rt_mutex_top_waiter(lock)->task);
486 raw_spin_unlock(&lock->wait_lock);
490 /* Grab the next task, i.e. the owner of @lock */
491 task = rt_mutex_owner(lock);
492 get_task_struct(task);
493 raw_spin_lock_irqsave(&task->pi_lock, flags);
495 if (waiter == rt_mutex_top_waiter(lock)) {
497 * The waiter became the new top (highest priority)
498 * waiter on the lock. Replace the previous top waiter
499 * in the owner tasks pi waiters list with this waiter
500 * and adjust the priority of the owner.
502 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
503 rt_mutex_enqueue_pi(task, waiter);
504 __rt_mutex_adjust_prio(task);
506 } else if (prerequeue_top_waiter == waiter) {
508 * The waiter was the top waiter on the lock, but is
509 * no longer the top prority waiter. Replace waiter in
510 * the owner tasks pi waiters list with the new top
511 * (highest priority) waiter and adjust the priority
513 * The new top waiter is stored in @waiter so that
514 * @waiter == @top_waiter evaluates to true below and
515 * we continue to deboost the rest of the chain.
517 rt_mutex_dequeue_pi(task, waiter);
518 waiter = rt_mutex_top_waiter(lock);
519 rt_mutex_enqueue_pi(task, waiter);
520 __rt_mutex_adjust_prio(task);
523 * Nothing changed. No need to do any priority
529 * Check whether the task which owns the current lock is pi
530 * blocked itself. If yes we store a pointer to the lock for
531 * the lock chain change detection above. After we dropped
532 * task->pi_lock next_lock cannot be dereferenced anymore.
534 next_lock = task_blocked_on_lock(task);
536 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
539 * Store the top waiter of @lock for the end of chain walk
542 top_waiter = rt_mutex_top_waiter(lock);
543 raw_spin_unlock(&lock->wait_lock);
546 * We reached the end of the lock chain. Stop right here. No
547 * point to go back just to figure that out.
553 * If the current waiter is not the top waiter on the lock,
554 * then we can stop the chain walk here if we are not in full
555 * deadlock detection mode.
557 if (!detect_deadlock && waiter != top_waiter)
563 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
565 put_task_struct(task);
571 * Try to take an rt-mutex
573 * Must be called with lock->wait_lock held.
575 * @lock: The lock to be acquired.
576 * @task: The task which wants to acquire the lock
577 * @waiter: The waiter that is queued to the lock's wait list if the
578 * callsite called task_blocked_on_lock(), otherwise NULL
580 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
581 struct rt_mutex_waiter *waiter)
586 * Before testing whether we can acquire @lock, we set the
587 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
588 * other tasks which try to modify @lock into the slow path
589 * and they serialize on @lock->wait_lock.
591 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
592 * as explained at the top of this file if and only if:
594 * - There is a lock owner. The caller must fixup the
595 * transient state if it does a trylock or leaves the lock
596 * function due to a signal or timeout.
598 * - @task acquires the lock and there are no other
599 * waiters. This is undone in rt_mutex_set_owner(@task) at
600 * the end of this function.
602 mark_rt_mutex_waiters(lock);
605 * If @lock has an owner, give up.
607 if (rt_mutex_owner(lock))
611 * If @waiter != NULL, @task has already enqueued the waiter
612 * into @lock waiter list. If @waiter == NULL then this is a
617 * If waiter is not the highest priority waiter of
620 if (waiter != rt_mutex_top_waiter(lock))
624 * We can acquire the lock. Remove the waiter from the
627 rt_mutex_dequeue(lock, waiter);
631 * If the lock has waiters already we check whether @task is
632 * eligible to take over the lock.
634 * If there are no other waiters, @task can acquire
635 * the lock. @task->pi_blocked_on is NULL, so it does
636 * not need to be dequeued.
638 if (rt_mutex_has_waiters(lock)) {
640 * If @task->prio is greater than or equal to
641 * the top waiter priority (kernel view),
644 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
648 * The current top waiter stays enqueued. We
649 * don't have to change anything in the lock
654 * No waiters. Take the lock without the
655 * pi_lock dance.@task->pi_blocked_on is NULL
656 * and we have no waiters to enqueue in @task
664 * Clear @task->pi_blocked_on. Requires protection by
665 * @task->pi_lock. Redundant operation for the @waiter == NULL
666 * case, but conditionals are more expensive than a redundant
669 raw_spin_lock_irqsave(&task->pi_lock, flags);
670 task->pi_blocked_on = NULL;
672 * Finish the lock acquisition. @task is the new owner. If
673 * other waiters exist we have to insert the highest priority
674 * waiter into @task->pi_waiters list.
676 if (rt_mutex_has_waiters(lock))
677 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
678 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
681 /* We got the lock. */
682 debug_rt_mutex_lock(lock);
685 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
686 * are still waiters or clears it.
688 rt_mutex_set_owner(lock, task);
690 rt_mutex_deadlock_account_lock(lock, task);
696 * Task blocks on lock.
698 * Prepare waiter and propagate pi chain
700 * This must be called with lock->wait_lock held.
702 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
703 struct rt_mutex_waiter *waiter,
704 struct task_struct *task,
707 struct task_struct *owner = rt_mutex_owner(lock);
708 struct rt_mutex_waiter *top_waiter = waiter;
709 struct rt_mutex *next_lock;
710 int chain_walk = 0, res;
714 * Early deadlock detection. We really don't want the task to
715 * enqueue on itself just to untangle the mess later. It's not
716 * only an optimization. We drop the locks, so another waiter
717 * can come in before the chain walk detects the deadlock. So
718 * the other will detect the deadlock and return -EDEADLOCK,
719 * which is wrong, as the other waiter is not in a deadlock
725 raw_spin_lock_irqsave(&task->pi_lock, flags);
726 __rt_mutex_adjust_prio(task);
729 waiter->prio = task->prio;
731 /* Get the top priority waiter on the lock */
732 if (rt_mutex_has_waiters(lock))
733 top_waiter = rt_mutex_top_waiter(lock);
734 rt_mutex_enqueue(lock, waiter);
736 task->pi_blocked_on = waiter;
738 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
743 raw_spin_lock_irqsave(&owner->pi_lock, flags);
744 if (waiter == rt_mutex_top_waiter(lock)) {
745 rt_mutex_dequeue_pi(owner, top_waiter);
746 rt_mutex_enqueue_pi(owner, waiter);
748 __rt_mutex_adjust_prio(owner);
749 if (owner->pi_blocked_on)
751 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
755 /* Store the lock on which owner is blocked or NULL */
756 next_lock = task_blocked_on_lock(owner);
758 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
760 * Even if full deadlock detection is on, if the owner is not
761 * blocked itself, we can avoid finding this out in the chain
764 if (!chain_walk || !next_lock)
768 * The owner can't disappear while holding a lock,
769 * so the owner struct is protected by wait_lock.
770 * Gets dropped in rt_mutex_adjust_prio_chain()!
772 get_task_struct(owner);
774 raw_spin_unlock(&lock->wait_lock);
776 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
777 next_lock, waiter, task);
779 raw_spin_lock(&lock->wait_lock);
785 * Wake up the next waiter on the lock.
787 * Remove the top waiter from the current tasks pi waiter list and
790 * Called with lock->wait_lock held.
792 static void wakeup_next_waiter(struct rt_mutex *lock)
794 struct rt_mutex_waiter *waiter;
797 raw_spin_lock_irqsave(¤t->pi_lock, flags);
799 waiter = rt_mutex_top_waiter(lock);
802 * Remove it from current->pi_waiters. We do not adjust a
803 * possible priority boost right now. We execute wakeup in the
804 * boosted mode and go back to normal after releasing
807 rt_mutex_dequeue_pi(current, waiter);
810 * As we are waking up the top waiter, and the waiter stays
811 * queued on the lock until it gets the lock, this lock
812 * obviously has waiters. Just set the bit here and this has
813 * the added benefit of forcing all new tasks into the
814 * slow path making sure no task of lower priority than
815 * the top waiter can steal this lock.
817 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
819 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
822 * It's safe to dereference waiter as it cannot go away as
823 * long as we hold lock->wait_lock. The waiter task needs to
824 * acquire it in order to dequeue the waiter.
826 wake_up_process(waiter->task);
830 * Remove a waiter from a lock and give up
832 * Must be called with lock->wait_lock held and
833 * have just failed to try_to_take_rt_mutex().
835 static void remove_waiter(struct rt_mutex *lock,
836 struct rt_mutex_waiter *waiter)
838 int first = (waiter == rt_mutex_top_waiter(lock));
839 struct task_struct *owner = rt_mutex_owner(lock);
840 struct rt_mutex *next_lock = NULL;
843 raw_spin_lock_irqsave(¤t->pi_lock, flags);
844 rt_mutex_dequeue(lock, waiter);
845 current->pi_blocked_on = NULL;
846 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
853 raw_spin_lock_irqsave(&owner->pi_lock, flags);
855 rt_mutex_dequeue_pi(owner, waiter);
857 if (rt_mutex_has_waiters(lock)) {
858 struct rt_mutex_waiter *next;
860 next = rt_mutex_top_waiter(lock);
861 rt_mutex_enqueue_pi(owner, next);
863 __rt_mutex_adjust_prio(owner);
865 /* Store the lock on which owner is blocked or NULL */
866 next_lock = task_blocked_on_lock(owner);
868 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
874 /* gets dropped in rt_mutex_adjust_prio_chain()! */
875 get_task_struct(owner);
877 raw_spin_unlock(&lock->wait_lock);
879 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
881 raw_spin_lock(&lock->wait_lock);
885 * Recheck the pi chain, in case we got a priority setting
887 * Called from sched_setscheduler
889 void rt_mutex_adjust_pi(struct task_struct *task)
891 struct rt_mutex_waiter *waiter;
892 struct rt_mutex *next_lock;
895 raw_spin_lock_irqsave(&task->pi_lock, flags);
897 waiter = task->pi_blocked_on;
898 if (!waiter || (waiter->prio == task->prio &&
899 !dl_prio(task->prio))) {
900 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
903 next_lock = waiter->lock;
904 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
906 /* gets dropped in rt_mutex_adjust_prio_chain()! */
907 get_task_struct(task);
909 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
913 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
914 * @lock: the rt_mutex to take
915 * @state: the state the task should block in (TASK_INTERRUPTIBLE
916 * or TASK_UNINTERRUPTIBLE)
917 * @timeout: the pre-initialized and started timer, or NULL for none
918 * @waiter: the pre-initialized rt_mutex_waiter
920 * lock->wait_lock must be held by the caller.
923 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
924 struct hrtimer_sleeper *timeout,
925 struct rt_mutex_waiter *waiter)
930 /* Try to acquire the lock: */
931 if (try_to_take_rt_mutex(lock, current, waiter))
935 * TASK_INTERRUPTIBLE checks for signals and
936 * timeout. Ignored otherwise.
938 if (unlikely(state == TASK_INTERRUPTIBLE)) {
939 /* Signal pending? */
940 if (signal_pending(current))
942 if (timeout && !timeout->task)
948 raw_spin_unlock(&lock->wait_lock);
950 debug_rt_mutex_print_deadlock(waiter);
952 schedule_rt_mutex(lock);
954 raw_spin_lock(&lock->wait_lock);
955 set_current_state(state);
961 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
962 struct rt_mutex_waiter *w)
965 * If the result is not -EDEADLOCK or the caller requested
966 * deadlock detection, nothing to do here.
968 if (res != -EDEADLOCK || detect_deadlock)
972 * Yell lowdly and stop the task right here.
974 rt_mutex_print_deadlock(w);
976 set_current_state(TASK_INTERRUPTIBLE);
982 * Slow path lock function:
985 rt_mutex_slowlock(struct rt_mutex *lock, int state,
986 struct hrtimer_sleeper *timeout,
989 struct rt_mutex_waiter waiter;
992 debug_rt_mutex_init_waiter(&waiter);
993 RB_CLEAR_NODE(&waiter.pi_tree_entry);
994 RB_CLEAR_NODE(&waiter.tree_entry);
996 raw_spin_lock(&lock->wait_lock);
998 /* Try to acquire the lock again: */
999 if (try_to_take_rt_mutex(lock, current, NULL)) {
1000 raw_spin_unlock(&lock->wait_lock);
1004 set_current_state(state);
1006 /* Setup the timer, when timeout != NULL */
1007 if (unlikely(timeout)) {
1008 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1009 if (!hrtimer_active(&timeout->timer))
1010 timeout->task = NULL;
1013 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
1016 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1018 set_current_state(TASK_RUNNING);
1020 if (unlikely(ret)) {
1021 remove_waiter(lock, &waiter);
1022 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
1026 * try_to_take_rt_mutex() sets the waiter bit
1027 * unconditionally. We might have to fix that up.
1029 fixup_rt_mutex_waiters(lock);
1031 raw_spin_unlock(&lock->wait_lock);
1033 /* Remove pending timer: */
1034 if (unlikely(timeout))
1035 hrtimer_cancel(&timeout->timer);
1037 debug_rt_mutex_free_waiter(&waiter);
1043 * Slow path try-lock function:
1045 static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
1050 * If the lock already has an owner we fail to get the lock.
1051 * This can be done without taking the @lock->wait_lock as
1052 * it is only being read, and this is a trylock anyway.
1054 if (rt_mutex_owner(lock))
1058 * The mutex has currently no owner. Lock the wait lock and
1059 * try to acquire the lock.
1061 raw_spin_lock(&lock->wait_lock);
1063 ret = try_to_take_rt_mutex(lock, current, NULL);
1066 * try_to_take_rt_mutex() sets the lock waiters bit
1067 * unconditionally. Clean this up.
1069 fixup_rt_mutex_waiters(lock);
1071 raw_spin_unlock(&lock->wait_lock);
1077 * Slow path to release a rt-mutex:
1080 rt_mutex_slowunlock(struct rt_mutex *lock)
1082 raw_spin_lock(&lock->wait_lock);
1084 debug_rt_mutex_unlock(lock);
1086 rt_mutex_deadlock_account_unlock(current);
1089 * We must be careful here if the fast path is enabled. If we
1090 * have no waiters queued we cannot set owner to NULL here
1093 * foo->lock->owner = NULL;
1094 * rtmutex_lock(foo->lock); <- fast path
1095 * free = atomic_dec_and_test(foo->refcnt);
1096 * rtmutex_unlock(foo->lock); <- fast path
1099 * raw_spin_unlock(foo->lock->wait_lock);
1101 * So for the fastpath enabled kernel:
1103 * Nothing can set the waiters bit as long as we hold
1104 * lock->wait_lock. So we do the following sequence:
1106 * owner = rt_mutex_owner(lock);
1107 * clear_rt_mutex_waiters(lock);
1108 * raw_spin_unlock(&lock->wait_lock);
1109 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1113 * The fastpath disabled variant is simple as all access to
1114 * lock->owner is serialized by lock->wait_lock:
1116 * lock->owner = NULL;
1117 * raw_spin_unlock(&lock->wait_lock);
1119 while (!rt_mutex_has_waiters(lock)) {
1120 /* Drops lock->wait_lock ! */
1121 if (unlock_rt_mutex_safe(lock) == true)
1123 /* Relock the rtmutex and try again */
1124 raw_spin_lock(&lock->wait_lock);
1128 * The wakeup next waiter path does not suffer from the above
1129 * race. See the comments there.
1131 wakeup_next_waiter(lock);
1133 raw_spin_unlock(&lock->wait_lock);
1135 /* Undo pi boosting if necessary: */
1136 rt_mutex_adjust_prio(current);
1140 * debug aware fast / slowpath lock,trylock,unlock
1142 * The atomic acquire/release ops are compiled away, when either the
1143 * architecture does not support cmpxchg or when debugging is enabled.
1146 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1147 int detect_deadlock,
1148 int (*slowfn)(struct rt_mutex *lock, int state,
1149 struct hrtimer_sleeper *timeout,
1150 int detect_deadlock))
1152 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1153 rt_mutex_deadlock_account_lock(lock, current);
1156 return slowfn(lock, state, NULL, detect_deadlock);
1160 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1161 struct hrtimer_sleeper *timeout, int detect_deadlock,
1162 int (*slowfn)(struct rt_mutex *lock, int state,
1163 struct hrtimer_sleeper *timeout,
1164 int detect_deadlock))
1166 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1167 rt_mutex_deadlock_account_lock(lock, current);
1170 return slowfn(lock, state, timeout, detect_deadlock);
1174 rt_mutex_fasttrylock(struct rt_mutex *lock,
1175 int (*slowfn)(struct rt_mutex *lock))
1177 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1178 rt_mutex_deadlock_account_lock(lock, current);
1181 return slowfn(lock);
1185 rt_mutex_fastunlock(struct rt_mutex *lock,
1186 void (*slowfn)(struct rt_mutex *lock))
1188 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1189 rt_mutex_deadlock_account_unlock(current);
1195 * rt_mutex_lock - lock a rt_mutex
1197 * @lock: the rt_mutex to be locked
1199 void __sched rt_mutex_lock(struct rt_mutex *lock)
1203 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1205 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1208 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1210 * @lock: the rt_mutex to be locked
1211 * @detect_deadlock: deadlock detection on/off
1215 * -EINTR when interrupted by a signal
1216 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1218 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1219 int detect_deadlock)
1223 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1224 detect_deadlock, rt_mutex_slowlock);
1226 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1229 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1230 * the timeout structure is provided
1233 * @lock: the rt_mutex to be locked
1234 * @timeout: timeout structure or NULL (no timeout)
1235 * @detect_deadlock: deadlock detection on/off
1239 * -EINTR when interrupted by a signal
1240 * -ETIMEDOUT when the timeout expired
1241 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1244 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1245 int detect_deadlock)
1249 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1250 detect_deadlock, rt_mutex_slowlock);
1252 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1255 * rt_mutex_trylock - try to lock a rt_mutex
1257 * @lock: the rt_mutex to be locked
1259 * Returns 1 on success and 0 on contention
1261 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1263 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1265 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1268 * rt_mutex_unlock - unlock a rt_mutex
1270 * @lock: the rt_mutex to be unlocked
1272 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1274 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1276 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1279 * rt_mutex_destroy - mark a mutex unusable
1280 * @lock: the mutex to be destroyed
1282 * This function marks the mutex uninitialized, and any subsequent
1283 * use of the mutex is forbidden. The mutex must not be locked when
1284 * this function is called.
1286 void rt_mutex_destroy(struct rt_mutex *lock)
1288 WARN_ON(rt_mutex_is_locked(lock));
1289 #ifdef CONFIG_DEBUG_RT_MUTEXES
1294 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1297 * __rt_mutex_init - initialize the rt lock
1299 * @lock: the rt lock to be initialized
1301 * Initialize the rt lock to unlocked state.
1303 * Initializing of a locked rt lock is not allowed
1305 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1308 raw_spin_lock_init(&lock->wait_lock);
1309 lock->waiters = RB_ROOT;
1310 lock->waiters_leftmost = NULL;
1312 debug_rt_mutex_init(lock, name);
1314 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1317 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1320 * @lock: the rt_mutex to be locked
1321 * @proxy_owner:the task to set as owner
1323 * No locking. Caller has to do serializing itself
1324 * Special API call for PI-futex support
1326 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1327 struct task_struct *proxy_owner)
1329 __rt_mutex_init(lock, NULL);
1330 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1331 rt_mutex_set_owner(lock, proxy_owner);
1332 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1336 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1338 * @lock: the rt_mutex to be locked
1340 * No locking. Caller has to do serializing itself
1341 * Special API call for PI-futex support
1343 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1344 struct task_struct *proxy_owner)
1346 debug_rt_mutex_proxy_unlock(lock);
1347 rt_mutex_set_owner(lock, NULL);
1348 rt_mutex_deadlock_account_unlock(proxy_owner);
1352 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1353 * @lock: the rt_mutex to take
1354 * @waiter: the pre-initialized rt_mutex_waiter
1355 * @task: the task to prepare
1356 * @detect_deadlock: perform deadlock detection (1) or not (0)
1359 * 0 - task blocked on lock
1360 * 1 - acquired the lock for task, caller should wake it up
1363 * Special API call for FUTEX_REQUEUE_PI support.
1365 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1366 struct rt_mutex_waiter *waiter,
1367 struct task_struct *task, int detect_deadlock)
1371 raw_spin_lock(&lock->wait_lock);
1373 if (try_to_take_rt_mutex(lock, task, NULL)) {
1374 raw_spin_unlock(&lock->wait_lock);
1378 /* We enforce deadlock detection for futexes */
1379 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1381 if (ret && !rt_mutex_owner(lock)) {
1383 * Reset the return value. We might have
1384 * returned with -EDEADLK and the owner
1385 * released the lock while we were walking the
1386 * pi chain. Let the waiter sort it out.
1392 remove_waiter(lock, waiter);
1394 raw_spin_unlock(&lock->wait_lock);
1396 debug_rt_mutex_print_deadlock(waiter);
1402 * rt_mutex_next_owner - return the next owner of the lock
1404 * @lock: the rt lock query
1406 * Returns the next owner of the lock or NULL
1408 * Caller has to serialize against other accessors to the lock
1411 * Special API call for PI-futex support
1413 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1415 if (!rt_mutex_has_waiters(lock))
1418 return rt_mutex_top_waiter(lock)->task;
1422 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1423 * @lock: the rt_mutex we were woken on
1424 * @to: the timeout, null if none. hrtimer should already have
1426 * @waiter: the pre-initialized rt_mutex_waiter
1427 * @detect_deadlock: perform deadlock detection (1) or not (0)
1429 * Complete the lock acquisition started our behalf by another thread.
1433 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1435 * Special API call for PI-futex requeue support
1437 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1438 struct hrtimer_sleeper *to,
1439 struct rt_mutex_waiter *waiter,
1440 int detect_deadlock)
1444 raw_spin_lock(&lock->wait_lock);
1446 set_current_state(TASK_INTERRUPTIBLE);
1448 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1450 set_current_state(TASK_RUNNING);
1453 remove_waiter(lock, waiter);
1456 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1457 * have to fix that up.
1459 fixup_rt_mutex_waiters(lock);
1461 raw_spin_unlock(&lock->wait_lock);