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/locking/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 unsigned long owner, *p = (unsigned long *) &lock->owner;
70 if (rt_mutex_has_waiters(lock))
74 * The rbtree has no waiters enqueued, now make sure that the
75 * lock->owner still has the waiters bit set, otherwise the
76 * following can happen:
82 * l->owner = T1 | HAS_WAITERS;
90 * l->owner = T1 | HAS_WAITERS;
95 * signal(->T2) signal(->T3)
102 * ==> wait list is empty
106 * fixup_rt_mutex_waiters()
107 * if (wait_list_empty(l) {
109 * owner = l->owner & ~HAS_WAITERS;
113 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
114 * if (wait_list_empty(l) {
115 * owner = l->owner & ~HAS_WAITERS;
116 * cmpxchg(l->owner, T1, NULL)
117 * ===> Success (l->owner = NULL)
123 * With the check for the waiter bit in place T3 on CPU2 will not
124 * overwrite. All tasks fiddling with the waiters bit are
125 * serialized by l->lock, so nothing else can modify the waiters
126 * bit. If the bit is set then nothing can change l->owner either
127 * so the simple RMW is safe. The cmpxchg() will simply fail if it
128 * happens in the middle of the RMW because the waiters bit is
131 owner = READ_ONCE(*p);
132 if (owner & RT_MUTEX_HAS_WAITERS)
133 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
137 * We can speed up the acquire/release, if there's no debugging state to be
140 #ifndef CONFIG_DEBUG_RT_MUTEXES
141 # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
142 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
143 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
146 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
147 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
148 * relaxed semantics suffice.
150 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
152 unsigned long owner, *p = (unsigned long *) &lock->owner;
156 } while (cmpxchg_relaxed(p, owner,
157 owner | RT_MUTEX_HAS_WAITERS) != owner);
161 * Safe fastpath aware unlock:
162 * 1) Clear the waiters bit
163 * 2) Drop lock->wait_lock
164 * 3) Try to unlock the lock with cmpxchg
166 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
168 __releases(lock->wait_lock)
170 struct task_struct *owner = rt_mutex_owner(lock);
172 clear_rt_mutex_waiters(lock);
173 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
175 * If a new waiter comes in between the unlock and the cmpxchg
176 * we have two situations:
180 * cmpxchg(p, owner, 0) == owner
181 * mark_rt_mutex_waiters(lock);
187 * mark_rt_mutex_waiters(lock);
189 * cmpxchg(p, owner, 0) != owner
198 return rt_mutex_cmpxchg_release(lock, owner, NULL);
202 # define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
203 # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
204 # define rt_mutex_cmpxchg_release(l,c,n) (0)
206 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
208 lock->owner = (struct task_struct *)
209 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
213 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
215 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
217 __releases(lock->wait_lock)
220 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
226 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
227 struct rt_mutex_waiter *right)
229 if (left->prio < right->prio)
233 * If both waiters have dl_prio(), we check the deadlines of the
235 * If left waiter has a dl_prio(), and we didn't return 1 above,
236 * then right waiter has a dl_prio() too.
238 if (dl_prio(left->prio))
239 return dl_time_before(left->deadline, right->deadline);
245 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
247 struct rb_node **link = &lock->waiters.rb_node;
248 struct rb_node *parent = NULL;
249 struct rt_mutex_waiter *entry;
254 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
255 if (rt_mutex_waiter_less(waiter, entry)) {
256 link = &parent->rb_left;
258 link = &parent->rb_right;
264 lock->waiters_leftmost = &waiter->tree_entry;
266 rb_link_node(&waiter->tree_entry, parent, link);
267 rb_insert_color(&waiter->tree_entry, &lock->waiters);
271 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
273 if (RB_EMPTY_NODE(&waiter->tree_entry))
276 if (lock->waiters_leftmost == &waiter->tree_entry)
277 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
279 rb_erase(&waiter->tree_entry, &lock->waiters);
280 RB_CLEAR_NODE(&waiter->tree_entry);
284 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
286 struct rb_node **link = &task->pi_waiters.rb_node;
287 struct rb_node *parent = NULL;
288 struct rt_mutex_waiter *entry;
293 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
294 if (rt_mutex_waiter_less(waiter, entry)) {
295 link = &parent->rb_left;
297 link = &parent->rb_right;
303 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
305 rb_link_node(&waiter->pi_tree_entry, parent, link);
306 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
310 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
312 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
315 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
316 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
318 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
319 RB_CLEAR_NODE(&waiter->pi_tree_entry);
323 * Calculate task priority from the waiter tree priority
325 * Return task->normal_prio when the waiter tree is empty or when
326 * the waiter is not allowed to do priority boosting
328 int rt_mutex_getprio(struct task_struct *task)
330 if (likely(!task_has_pi_waiters(task)))
331 return task->normal_prio;
333 return min(task_top_pi_waiter(task)->prio,
337 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
339 if (likely(!task_has_pi_waiters(task)))
342 return task_top_pi_waiter(task)->task;
346 * Called by sched_setscheduler() to get the priority which will be
347 * effective after the change.
349 int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
351 if (!task_has_pi_waiters(task))
354 if (task_top_pi_waiter(task)->task->prio <= newprio)
355 return task_top_pi_waiter(task)->task->prio;
360 * Adjust the priority of a task, after its pi_waiters got modified.
362 * This can be both boosting and unboosting. task->pi_lock must be held.
364 static void __rt_mutex_adjust_prio(struct task_struct *task)
366 int prio = rt_mutex_getprio(task);
368 if (task->prio != prio || dl_prio(prio))
369 rt_mutex_setprio(task, prio);
373 * Adjust task priority (undo boosting). Called from the exit path of
374 * rt_mutex_slowunlock() and rt_mutex_slowlock().
376 * (Note: We do this outside of the protection of lock->wait_lock to
377 * allow the lock to be taken while or before we readjust the priority
378 * of task. We do not use the spin_xx_mutex() variants here as we are
379 * outside of the debug path.)
381 void rt_mutex_adjust_prio(struct task_struct *task)
385 raw_spin_lock_irqsave(&task->pi_lock, flags);
386 __rt_mutex_adjust_prio(task);
387 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
391 * Deadlock detection is conditional:
393 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
394 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
396 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
397 * conducted independent of the detect argument.
399 * If the waiter argument is NULL this indicates the deboost path and
400 * deadlock detection is disabled independent of the detect argument
401 * and the config settings.
403 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
404 enum rtmutex_chainwalk chwalk)
407 * This is just a wrapper function for the following call,
408 * because debug_rt_mutex_detect_deadlock() smells like a magic
409 * debug feature and I wanted to keep the cond function in the
410 * main source file along with the comments instead of having
411 * two of the same in the headers.
413 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
417 * Max number of times we'll walk the boosting chain:
419 int max_lock_depth = 1024;
421 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
423 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
427 * Adjust the priority chain. Also used for deadlock detection.
428 * Decreases task's usage by one - may thus free the task.
430 * @task: the task owning the mutex (owner) for which a chain walk is
432 * @chwalk: do we have to carry out deadlock detection?
433 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
434 * things for a task that has just got its priority adjusted, and
435 * is waiting on a mutex)
436 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
437 * we dropped its pi_lock. Is never dereferenced, only used for
438 * comparison to detect lock chain changes.
439 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
440 * its priority to the mutex owner (can be NULL in the case
441 * depicted above or if the top waiter is gone away and we are
442 * actually deboosting the owner)
443 * @top_task: the current top waiter
445 * Returns 0 or -EDEADLK.
447 * Chain walk basics and protection scope
449 * [R] refcount on task
450 * [P] task->pi_lock held
451 * [L] rtmutex->wait_lock held
453 * Step Description Protected by
454 * function arguments:
456 * @orig_lock if != NULL @top_task is blocked on it
457 * @next_lock Unprotected. Cannot be
458 * dereferenced. Only used for
460 * @orig_waiter if != NULL @top_task is blocked on it
461 * @top_task current, or in case of proxy
462 * locking protected by calling
465 * loop_sanity_check();
467 * [1] lock(task->pi_lock); [R] acquire [P]
468 * [2] waiter = task->pi_blocked_on; [P]
469 * [3] check_exit_conditions_1(); [P]
470 * [4] lock = waiter->lock; [P]
471 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
472 * unlock(task->pi_lock); release [P]
475 * [6] check_exit_conditions_2(); [P] + [L]
476 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
477 * [8] unlock(task->pi_lock); release [P]
478 * put_task_struct(task); release [R]
479 * [9] check_exit_conditions_3(); [L]
480 * [10] task = owner(lock); [L]
481 * get_task_struct(task); [L] acquire [R]
482 * lock(task->pi_lock); [L] acquire [P]
483 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
484 * [12] check_exit_conditions_4(); [P] + [L]
485 * [13] unlock(task->pi_lock); release [P]
486 * unlock(lock->wait_lock); release [L]
489 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
490 enum rtmutex_chainwalk chwalk,
491 struct rt_mutex *orig_lock,
492 struct rt_mutex *next_lock,
493 struct rt_mutex_waiter *orig_waiter,
494 struct task_struct *top_task)
496 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
497 struct rt_mutex_waiter *prerequeue_top_waiter;
498 int ret = 0, depth = 0;
499 struct rt_mutex *lock;
500 bool detect_deadlock;
503 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
506 * The (de)boosting is a step by step approach with a lot of
507 * pitfalls. We want this to be preemptible and we want hold a
508 * maximum of two locks per step. So we have to check
509 * carefully whether things change under us.
513 * We limit the lock chain length for each invocation.
515 if (++depth > max_lock_depth) {
519 * Print this only once. If the admin changes the limit,
520 * print a new message when reaching the limit again.
522 if (prev_max != max_lock_depth) {
523 prev_max = max_lock_depth;
524 printk(KERN_WARNING "Maximum lock depth %d reached "
525 "task: %s (%d)\n", max_lock_depth,
526 top_task->comm, task_pid_nr(top_task));
528 put_task_struct(task);
534 * We are fully preemptible here and only hold the refcount on
535 * @task. So everything can have changed under us since the
536 * caller or our own code below (goto retry/again) dropped all
541 * [1] Task cannot go away as we did a get_task() before !
543 raw_spin_lock_irq(&task->pi_lock);
546 * [2] Get the waiter on which @task is blocked on.
548 waiter = task->pi_blocked_on;
551 * [3] check_exit_conditions_1() protected by task->pi_lock.
555 * Check whether the end of the boosting chain has been
556 * reached or the state of the chain has changed while we
563 * Check the orig_waiter state. After we dropped the locks,
564 * the previous owner of the lock might have released the lock.
566 if (orig_waiter && !rt_mutex_owner(orig_lock))
570 * We dropped all locks after taking a refcount on @task, so
571 * the task might have moved on in the lock chain or even left
572 * the chain completely and blocks now on an unrelated lock or
575 * We stored the lock on which @task was blocked in @next_lock,
576 * so we can detect the chain change.
578 if (next_lock != waiter->lock)
582 * Drop out, when the task has no waiters. Note,
583 * top_waiter can be NULL, when we are in the deboosting
587 if (!task_has_pi_waiters(task))
590 * If deadlock detection is off, we stop here if we
591 * are not the top pi waiter of the task. If deadlock
592 * detection is enabled we continue, but stop the
593 * requeueing in the chain walk.
595 if (top_waiter != task_top_pi_waiter(task)) {
596 if (!detect_deadlock)
604 * If the waiter priority is the same as the task priority
605 * then there is no further priority adjustment necessary. If
606 * deadlock detection is off, we stop the chain walk. If its
607 * enabled we continue, but stop the requeueing in the chain
610 if (waiter->prio == task->prio) {
611 if (!detect_deadlock)
618 * [4] Get the next lock
622 * [5] We need to trylock here as we are holding task->pi_lock,
623 * which is the reverse lock order versus the other rtmutex
626 if (!raw_spin_trylock(&lock->wait_lock)) {
627 raw_spin_unlock_irq(&task->pi_lock);
633 * [6] check_exit_conditions_2() protected by task->pi_lock and
636 * Deadlock detection. If the lock is the same as the original
637 * lock which caused us to walk the lock chain or if the
638 * current lock is owned by the task which initiated the chain
639 * walk, we detected a deadlock.
641 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
642 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
643 raw_spin_unlock(&lock->wait_lock);
649 * If we just follow the lock chain for deadlock detection, no
650 * need to do all the requeue operations. To avoid a truckload
651 * of conditionals around the various places below, just do the
652 * minimum chain walk checks.
656 * No requeue[7] here. Just release @task [8]
658 raw_spin_unlock(&task->pi_lock);
659 put_task_struct(task);
662 * [9] check_exit_conditions_3 protected by lock->wait_lock.
663 * If there is no owner of the lock, end of chain.
665 if (!rt_mutex_owner(lock)) {
666 raw_spin_unlock_irq(&lock->wait_lock);
670 /* [10] Grab the next task, i.e. owner of @lock */
671 task = rt_mutex_owner(lock);
672 get_task_struct(task);
673 raw_spin_lock(&task->pi_lock);
676 * No requeue [11] here. We just do deadlock detection.
678 * [12] Store whether owner is blocked
679 * itself. Decision is made after dropping the locks
681 next_lock = task_blocked_on_lock(task);
683 * Get the top waiter for the next iteration
685 top_waiter = rt_mutex_top_waiter(lock);
687 /* [13] Drop locks */
688 raw_spin_unlock(&task->pi_lock);
689 raw_spin_unlock_irq(&lock->wait_lock);
691 /* If owner is not blocked, end of chain. */
698 * Store the current top waiter before doing the requeue
699 * operation on @lock. We need it for the boost/deboost
702 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
704 /* [7] Requeue the waiter in the lock waiter tree. */
705 rt_mutex_dequeue(lock, waiter);
708 * Update the waiter prio fields now that we're dequeued.
710 * These values can have changed through either:
712 * sys_sched_set_scheduler() / sys_sched_setattr()
716 * DL CBS enforcement advancing the effective deadline.
718 * Even though pi_waiters also uses these fields, and that tree is only
719 * updated in [11], we can do this here, since we hold [L], which
720 * serializes all pi_waiters access and rb_erase() does not care about
721 * the values of the node being removed.
723 waiter->prio = task->prio;
724 waiter->deadline = task->dl.deadline;
726 rt_mutex_enqueue(lock, waiter);
728 /* [8] Release the task */
729 raw_spin_unlock(&task->pi_lock);
730 put_task_struct(task);
733 * [9] check_exit_conditions_3 protected by lock->wait_lock.
735 * We must abort the chain walk if there is no lock owner even
736 * in the dead lock detection case, as we have nothing to
737 * follow here. This is the end of the chain we are walking.
739 if (!rt_mutex_owner(lock)) {
741 * If the requeue [7] above changed the top waiter,
742 * then we need to wake the new top waiter up to try
745 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
746 wake_up_process(rt_mutex_top_waiter(lock)->task);
747 raw_spin_unlock_irq(&lock->wait_lock);
751 /* [10] Grab the next task, i.e. the owner of @lock */
752 task = rt_mutex_owner(lock);
753 get_task_struct(task);
754 raw_spin_lock(&task->pi_lock);
756 /* [11] requeue the pi waiters if necessary */
757 if (waiter == rt_mutex_top_waiter(lock)) {
759 * The waiter became the new top (highest priority)
760 * waiter on the lock. Replace the previous top waiter
761 * in the owner tasks pi waiters tree with this waiter
762 * and adjust the priority of the owner.
764 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
765 rt_mutex_enqueue_pi(task, waiter);
766 __rt_mutex_adjust_prio(task);
768 } else if (prerequeue_top_waiter == waiter) {
770 * The waiter was the top waiter on the lock, but is
771 * no longer the top prority waiter. Replace waiter in
772 * the owner tasks pi waiters tree with the new top
773 * (highest priority) waiter and adjust the priority
775 * The new top waiter is stored in @waiter so that
776 * @waiter == @top_waiter evaluates to true below and
777 * we continue to deboost the rest of the chain.
779 rt_mutex_dequeue_pi(task, waiter);
780 waiter = rt_mutex_top_waiter(lock);
781 rt_mutex_enqueue_pi(task, waiter);
782 __rt_mutex_adjust_prio(task);
785 * Nothing changed. No need to do any priority
791 * [12] check_exit_conditions_4() protected by task->pi_lock
792 * and lock->wait_lock. The actual decisions are made after we
795 * Check whether the task which owns the current lock is pi
796 * blocked itself. If yes we store a pointer to the lock for
797 * the lock chain change detection above. After we dropped
798 * task->pi_lock next_lock cannot be dereferenced anymore.
800 next_lock = task_blocked_on_lock(task);
802 * Store the top waiter of @lock for the end of chain walk
805 top_waiter = rt_mutex_top_waiter(lock);
807 /* [13] Drop the locks */
808 raw_spin_unlock(&task->pi_lock);
809 raw_spin_unlock_irq(&lock->wait_lock);
812 * Make the actual exit decisions [12], based on the stored
815 * We reached the end of the lock chain. Stop right here. No
816 * point to go back just to figure that out.
822 * If the current waiter is not the top waiter on the lock,
823 * then we can stop the chain walk here if we are not in full
824 * deadlock detection mode.
826 if (!detect_deadlock && waiter != top_waiter)
832 raw_spin_unlock_irq(&task->pi_lock);
834 put_task_struct(task);
840 * Try to take an rt-mutex
842 * Must be called with lock->wait_lock held and interrupts disabled
844 * @lock: The lock to be acquired.
845 * @task: The task which wants to acquire the lock
846 * @waiter: The waiter that is queued to the lock's wait tree if the
847 * callsite called task_blocked_on_lock(), otherwise NULL
849 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
850 struct rt_mutex_waiter *waiter)
852 lockdep_assert_held(&lock->wait_lock);
855 * Before testing whether we can acquire @lock, we set the
856 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
857 * other tasks which try to modify @lock into the slow path
858 * and they serialize on @lock->wait_lock.
860 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
861 * as explained at the top of this file if and only if:
863 * - There is a lock owner. The caller must fixup the
864 * transient state if it does a trylock or leaves the lock
865 * function due to a signal or timeout.
867 * - @task acquires the lock and there are no other
868 * waiters. This is undone in rt_mutex_set_owner(@task) at
869 * the end of this function.
871 mark_rt_mutex_waiters(lock);
874 * If @lock has an owner, give up.
876 if (rt_mutex_owner(lock))
880 * If @waiter != NULL, @task has already enqueued the waiter
881 * into @lock waiter tree. If @waiter == NULL then this is a
886 * If waiter is not the highest priority waiter of
889 if (waiter != rt_mutex_top_waiter(lock))
893 * We can acquire the lock. Remove the waiter from the
896 rt_mutex_dequeue(lock, waiter);
900 * If the lock has waiters already we check whether @task is
901 * eligible to take over the lock.
903 * If there are no other waiters, @task can acquire
904 * the lock. @task->pi_blocked_on is NULL, so it does
905 * not need to be dequeued.
907 if (rt_mutex_has_waiters(lock)) {
909 * If @task->prio is greater than or equal to
910 * the top waiter priority (kernel view),
913 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
917 * The current top waiter stays enqueued. We
918 * don't have to change anything in the lock
923 * No waiters. Take the lock without the
924 * pi_lock dance.@task->pi_blocked_on is NULL
925 * and we have no waiters to enqueue in @task
933 * Clear @task->pi_blocked_on. Requires protection by
934 * @task->pi_lock. Redundant operation for the @waiter == NULL
935 * case, but conditionals are more expensive than a redundant
938 raw_spin_lock(&task->pi_lock);
939 task->pi_blocked_on = NULL;
941 * Finish the lock acquisition. @task is the new owner. If
942 * other waiters exist we have to insert the highest priority
943 * waiter into @task->pi_waiters tree.
945 if (rt_mutex_has_waiters(lock))
946 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
947 raw_spin_unlock(&task->pi_lock);
950 /* We got the lock. */
951 debug_rt_mutex_lock(lock);
954 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
955 * are still waiters or clears it.
957 rt_mutex_set_owner(lock, task);
963 * Task blocks on lock.
965 * Prepare waiter and propagate pi chain
967 * This must be called with lock->wait_lock held and interrupts disabled
969 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
970 struct rt_mutex_waiter *waiter,
971 struct task_struct *task,
972 enum rtmutex_chainwalk chwalk)
974 struct task_struct *owner = rt_mutex_owner(lock);
975 struct rt_mutex_waiter *top_waiter = waiter;
976 struct rt_mutex *next_lock;
977 int chain_walk = 0, res;
979 lockdep_assert_held(&lock->wait_lock);
982 * Early deadlock detection. We really don't want the task to
983 * enqueue on itself just to untangle the mess later. It's not
984 * only an optimization. We drop the locks, so another waiter
985 * can come in before the chain walk detects the deadlock. So
986 * the other will detect the deadlock and return -EDEADLOCK,
987 * which is wrong, as the other waiter is not in a deadlock
993 raw_spin_lock(&task->pi_lock);
994 __rt_mutex_adjust_prio(task);
997 waiter->prio = task->prio;
998 waiter->deadline = task->dl.deadline;
1000 /* Get the top priority waiter on the lock */
1001 if (rt_mutex_has_waiters(lock))
1002 top_waiter = rt_mutex_top_waiter(lock);
1003 rt_mutex_enqueue(lock, waiter);
1005 task->pi_blocked_on = waiter;
1007 raw_spin_unlock(&task->pi_lock);
1012 raw_spin_lock(&owner->pi_lock);
1013 if (waiter == rt_mutex_top_waiter(lock)) {
1014 rt_mutex_dequeue_pi(owner, top_waiter);
1015 rt_mutex_enqueue_pi(owner, waiter);
1017 __rt_mutex_adjust_prio(owner);
1018 if (owner->pi_blocked_on)
1020 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
1024 /* Store the lock on which owner is blocked or NULL */
1025 next_lock = task_blocked_on_lock(owner);
1027 raw_spin_unlock(&owner->pi_lock);
1029 * Even if full deadlock detection is on, if the owner is not
1030 * blocked itself, we can avoid finding this out in the chain
1033 if (!chain_walk || !next_lock)
1037 * The owner can't disappear while holding a lock,
1038 * so the owner struct is protected by wait_lock.
1039 * Gets dropped in rt_mutex_adjust_prio_chain()!
1041 get_task_struct(owner);
1043 raw_spin_unlock_irq(&lock->wait_lock);
1045 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
1046 next_lock, waiter, task);
1048 raw_spin_lock_irq(&lock->wait_lock);
1054 * Remove the top waiter from the current tasks pi waiter tree and
1057 * Called with lock->wait_lock held and interrupts disabled.
1059 static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1060 struct rt_mutex *lock)
1062 struct rt_mutex_waiter *waiter;
1064 raw_spin_lock(¤t->pi_lock);
1066 waiter = rt_mutex_top_waiter(lock);
1069 * Remove it from current->pi_waiters. We do not adjust a
1070 * possible priority boost right now. We execute wakeup in the
1071 * boosted mode and go back to normal after releasing
1074 rt_mutex_dequeue_pi(current, waiter);
1077 * As we are waking up the top waiter, and the waiter stays
1078 * queued on the lock until it gets the lock, this lock
1079 * obviously has waiters. Just set the bit here and this has
1080 * the added benefit of forcing all new tasks into the
1081 * slow path making sure no task of lower priority than
1082 * the top waiter can steal this lock.
1084 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
1086 raw_spin_unlock(¤t->pi_lock);
1088 wake_q_add(wake_q, waiter->task);
1092 * Remove a waiter from a lock and give up
1094 * Must be called with lock->wait_lock held and interrupts disabled. I must
1095 * have just failed to try_to_take_rt_mutex().
1097 static void remove_waiter(struct rt_mutex *lock,
1098 struct rt_mutex_waiter *waiter)
1100 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
1101 struct task_struct *owner = rt_mutex_owner(lock);
1102 struct rt_mutex *next_lock;
1104 lockdep_assert_held(&lock->wait_lock);
1106 raw_spin_lock(¤t->pi_lock);
1107 rt_mutex_dequeue(lock, waiter);
1108 current->pi_blocked_on = NULL;
1109 raw_spin_unlock(¤t->pi_lock);
1112 * Only update priority if the waiter was the highest priority
1113 * waiter of the lock and there is an owner to update.
1115 if (!owner || !is_top_waiter)
1118 raw_spin_lock(&owner->pi_lock);
1120 rt_mutex_dequeue_pi(owner, waiter);
1122 if (rt_mutex_has_waiters(lock))
1123 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
1125 __rt_mutex_adjust_prio(owner);
1127 /* Store the lock on which owner is blocked or NULL */
1128 next_lock = task_blocked_on_lock(owner);
1130 raw_spin_unlock(&owner->pi_lock);
1133 * Don't walk the chain, if the owner task is not blocked
1139 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1140 get_task_struct(owner);
1142 raw_spin_unlock_irq(&lock->wait_lock);
1144 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1145 next_lock, NULL, current);
1147 raw_spin_lock_irq(&lock->wait_lock);
1151 * Recheck the pi chain, in case we got a priority setting
1153 * Called from sched_setscheduler
1155 void rt_mutex_adjust_pi(struct task_struct *task)
1157 struct rt_mutex_waiter *waiter;
1158 struct rt_mutex *next_lock;
1159 unsigned long flags;
1161 raw_spin_lock_irqsave(&task->pi_lock, flags);
1163 waiter = task->pi_blocked_on;
1164 if (!waiter || (waiter->prio == task->prio &&
1165 !dl_prio(task->prio))) {
1166 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1169 next_lock = waiter->lock;
1170 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1172 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1173 get_task_struct(task);
1175 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1176 next_lock, NULL, task);
1179 void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1181 debug_rt_mutex_init_waiter(waiter);
1182 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1183 RB_CLEAR_NODE(&waiter->tree_entry);
1184 waiter->task = NULL;
1188 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1189 * @lock: the rt_mutex to take
1190 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1191 * or TASK_UNINTERRUPTIBLE)
1192 * @timeout: the pre-initialized and started timer, or NULL for none
1193 * @waiter: the pre-initialized rt_mutex_waiter
1195 * Must be called with lock->wait_lock held and interrupts disabled
1198 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
1199 struct hrtimer_sleeper *timeout,
1200 struct rt_mutex_waiter *waiter)
1205 /* Try to acquire the lock: */
1206 if (try_to_take_rt_mutex(lock, current, waiter))
1210 * TASK_INTERRUPTIBLE checks for signals and
1211 * timeout. Ignored otherwise.
1213 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1214 /* Signal pending? */
1215 if (signal_pending(current))
1217 if (timeout && !timeout->task)
1223 raw_spin_unlock_irq(&lock->wait_lock);
1225 debug_rt_mutex_print_deadlock(waiter);
1229 raw_spin_lock_irq(&lock->wait_lock);
1230 set_current_state(state);
1233 __set_current_state(TASK_RUNNING);
1237 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1238 struct rt_mutex_waiter *w)
1241 * If the result is not -EDEADLOCK or the caller requested
1242 * deadlock detection, nothing to do here.
1244 if (res != -EDEADLOCK || detect_deadlock)
1248 * Yell lowdly and stop the task right here.
1250 rt_mutex_print_deadlock(w);
1252 set_current_state(TASK_INTERRUPTIBLE);
1258 * Slow path lock function:
1261 rt_mutex_slowlock(struct rt_mutex *lock, int state,
1262 struct hrtimer_sleeper *timeout,
1263 enum rtmutex_chainwalk chwalk)
1265 struct rt_mutex_waiter waiter;
1266 unsigned long flags;
1269 rt_mutex_init_waiter(&waiter);
1272 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1273 * be called in early boot if the cmpxchg() fast path is disabled
1274 * (debug, no architecture support). In this case we will acquire the
1275 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1276 * enable interrupts in that early boot case. So we need to use the
1277 * irqsave/restore variants.
1279 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1281 /* Try to acquire the lock again: */
1282 if (try_to_take_rt_mutex(lock, current, NULL)) {
1283 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1287 set_current_state(state);
1289 /* Setup the timer, when timeout != NULL */
1290 if (unlikely(timeout))
1291 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1293 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
1296 /* sleep on the mutex */
1297 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1299 if (unlikely(ret)) {
1300 __set_current_state(TASK_RUNNING);
1301 if (rt_mutex_has_waiters(lock))
1302 remove_waiter(lock, &waiter);
1303 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
1307 * try_to_take_rt_mutex() sets the waiter bit
1308 * unconditionally. We might have to fix that up.
1310 fixup_rt_mutex_waiters(lock);
1312 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1314 /* Remove pending timer: */
1315 if (unlikely(timeout))
1316 hrtimer_cancel(&timeout->timer);
1318 debug_rt_mutex_free_waiter(&waiter);
1323 static inline int __rt_mutex_slowtrylock(struct rt_mutex *lock)
1325 int ret = try_to_take_rt_mutex(lock, current, NULL);
1328 * try_to_take_rt_mutex() sets the lock waiters bit
1329 * unconditionally. Clean this up.
1331 fixup_rt_mutex_waiters(lock);
1337 * Slow path try-lock function:
1339 static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
1341 unsigned long flags;
1345 * If the lock already has an owner we fail to get the lock.
1346 * This can be done without taking the @lock->wait_lock as
1347 * it is only being read, and this is a trylock anyway.
1349 if (rt_mutex_owner(lock))
1353 * The mutex has currently no owner. Lock the wait lock and try to
1354 * acquire the lock. We use irqsave here to support early boot calls.
1356 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1358 ret = __rt_mutex_slowtrylock(lock);
1360 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1366 * Slow path to release a rt-mutex.
1367 * Return whether the current task needs to undo a potential priority boosting.
1369 static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1370 struct wake_q_head *wake_q)
1372 unsigned long flags;
1374 /* irqsave required to support early boot calls */
1375 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1377 debug_rt_mutex_unlock(lock);
1380 * We must be careful here if the fast path is enabled. If we
1381 * have no waiters queued we cannot set owner to NULL here
1384 * foo->lock->owner = NULL;
1385 * rtmutex_lock(foo->lock); <- fast path
1386 * free = atomic_dec_and_test(foo->refcnt);
1387 * rtmutex_unlock(foo->lock); <- fast path
1390 * raw_spin_unlock(foo->lock->wait_lock);
1392 * So for the fastpath enabled kernel:
1394 * Nothing can set the waiters bit as long as we hold
1395 * lock->wait_lock. So we do the following sequence:
1397 * owner = rt_mutex_owner(lock);
1398 * clear_rt_mutex_waiters(lock);
1399 * raw_spin_unlock(&lock->wait_lock);
1400 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1404 * The fastpath disabled variant is simple as all access to
1405 * lock->owner is serialized by lock->wait_lock:
1407 * lock->owner = NULL;
1408 * raw_spin_unlock(&lock->wait_lock);
1410 while (!rt_mutex_has_waiters(lock)) {
1411 /* Drops lock->wait_lock ! */
1412 if (unlock_rt_mutex_safe(lock, flags) == true)
1414 /* Relock the rtmutex and try again */
1415 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1419 * The wakeup next waiter path does not suffer from the above
1420 * race. See the comments there.
1422 * Queue the next waiter for wakeup once we release the wait_lock.
1424 mark_wakeup_next_waiter(wake_q, lock);
1426 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1428 /* check PI boosting */
1433 * debug aware fast / slowpath lock,trylock,unlock
1435 * The atomic acquire/release ops are compiled away, when either the
1436 * architecture does not support cmpxchg or when debugging is enabled.
1439 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1440 int (*slowfn)(struct rt_mutex *lock, int state,
1441 struct hrtimer_sleeper *timeout,
1442 enum rtmutex_chainwalk chwalk))
1444 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1447 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
1451 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1452 struct hrtimer_sleeper *timeout,
1453 enum rtmutex_chainwalk chwalk,
1454 int (*slowfn)(struct rt_mutex *lock, int state,
1455 struct hrtimer_sleeper *timeout,
1456 enum rtmutex_chainwalk chwalk))
1458 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1459 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1462 return slowfn(lock, state, timeout, chwalk);
1466 rt_mutex_fasttrylock(struct rt_mutex *lock,
1467 int (*slowfn)(struct rt_mutex *lock))
1469 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1472 return slowfn(lock);
1476 rt_mutex_fastunlock(struct rt_mutex *lock,
1477 bool (*slowfn)(struct rt_mutex *lock,
1478 struct wake_q_head *wqh))
1483 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1486 deboost = slowfn(lock, &wake_q);
1490 /* Undo pi boosting if necessary: */
1492 rt_mutex_adjust_prio(current);
1496 * rt_mutex_lock - lock a rt_mutex
1498 * @lock: the rt_mutex to be locked
1500 void __sched rt_mutex_lock(struct rt_mutex *lock)
1504 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1506 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1509 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1511 * @lock: the rt_mutex to be locked
1515 * -EINTR when interrupted by a signal
1517 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
1521 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1523 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1526 * Futex variant, must not use fastpath.
1528 int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1530 return rt_mutex_slowtrylock(lock);
1533 int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
1535 return __rt_mutex_slowtrylock(lock);
1539 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1540 * the timeout structure is provided
1543 * @lock: the rt_mutex to be locked
1544 * @timeout: timeout structure or NULL (no timeout)
1548 * -EINTR when interrupted by a signal
1549 * -ETIMEDOUT when the timeout expired
1552 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
1556 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1557 RT_MUTEX_MIN_CHAINWALK,
1560 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1563 * rt_mutex_trylock - try to lock a rt_mutex
1565 * @lock: the rt_mutex to be locked
1567 * This function can only be called in thread context. It's safe to
1568 * call it from atomic regions, but not from hard interrupt or soft
1569 * interrupt context.
1571 * Returns 1 on success and 0 on contention
1573 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1575 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
1578 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1580 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1583 * rt_mutex_unlock - unlock a rt_mutex
1585 * @lock: the rt_mutex to be unlocked
1587 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1589 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1591 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1594 * Futex variant, that since futex variants do not use the fast-path, can be
1595 * simple and will not need to retry.
1597 bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1598 struct wake_q_head *wake_q)
1600 lockdep_assert_held(&lock->wait_lock);
1602 debug_rt_mutex_unlock(lock);
1604 if (!rt_mutex_has_waiters(lock)) {
1606 return false; /* done */
1609 mark_wakeup_next_waiter(wake_q, lock);
1610 return true; /* deboost and wakeups */
1613 void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1618 raw_spin_lock_irq(&lock->wait_lock);
1619 deboost = __rt_mutex_futex_unlock(lock, &wake_q);
1620 raw_spin_unlock_irq(&lock->wait_lock);
1624 rt_mutex_adjust_prio(current);
1629 * rt_mutex_destroy - mark a mutex unusable
1630 * @lock: the mutex to be destroyed
1632 * This function marks the mutex uninitialized, and any subsequent
1633 * use of the mutex is forbidden. The mutex must not be locked when
1634 * this function is called.
1636 void rt_mutex_destroy(struct rt_mutex *lock)
1638 WARN_ON(rt_mutex_is_locked(lock));
1639 #ifdef CONFIG_DEBUG_RT_MUTEXES
1644 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1647 * __rt_mutex_init - initialize the rt lock
1649 * @lock: the rt lock to be initialized
1651 * Initialize the rt lock to unlocked state.
1653 * Initializing of a locked rt lock is not allowed
1655 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1658 raw_spin_lock_init(&lock->wait_lock);
1659 lock->waiters = RB_ROOT;
1660 lock->waiters_leftmost = NULL;
1662 debug_rt_mutex_init(lock, name);
1664 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1667 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1670 * @lock: the rt_mutex to be locked
1671 * @proxy_owner:the task to set as owner
1673 * No locking. Caller has to do serializing itself
1674 * Special API call for PI-futex support
1676 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1677 struct task_struct *proxy_owner)
1679 __rt_mutex_init(lock, NULL);
1680 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1681 rt_mutex_set_owner(lock, proxy_owner);
1685 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1687 * @lock: the rt_mutex to be locked
1689 * No locking. Caller has to do serializing itself
1690 * Special API call for PI-futex support
1692 void rt_mutex_proxy_unlock(struct rt_mutex *lock)
1694 debug_rt_mutex_proxy_unlock(lock);
1695 rt_mutex_set_owner(lock, NULL);
1699 * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1700 * @lock: the rt_mutex to take
1701 * @waiter: the pre-initialized rt_mutex_waiter
1702 * @task: the task to prepare
1704 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1705 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1707 * NOTE: does _NOT_ remove the @waiter on failure; must either call
1708 * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
1711 * 0 - task blocked on lock
1712 * 1 - acquired the lock for task, caller should wake it up
1715 * Special API call for PI-futex support.
1717 int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1718 struct rt_mutex_waiter *waiter,
1719 struct task_struct *task)
1723 lockdep_assert_held(&lock->wait_lock);
1725 if (try_to_take_rt_mutex(lock, task, NULL))
1728 /* We enforce deadlock detection for futexes */
1729 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1730 RT_MUTEX_FULL_CHAINWALK);
1732 if (ret && !rt_mutex_owner(lock)) {
1734 * Reset the return value. We might have
1735 * returned with -EDEADLK and the owner
1736 * released the lock while we were walking the
1737 * pi chain. Let the waiter sort it out.
1742 debug_rt_mutex_print_deadlock(waiter);
1748 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1749 * @lock: the rt_mutex to take
1750 * @waiter: the pre-initialized rt_mutex_waiter
1751 * @task: the task to prepare
1753 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1754 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1756 * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
1760 * 0 - task blocked on lock
1761 * 1 - acquired the lock for task, caller should wake it up
1764 * Special API call for PI-futex support.
1766 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1767 struct rt_mutex_waiter *waiter,
1768 struct task_struct *task)
1772 raw_spin_lock_irq(&lock->wait_lock);
1773 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
1775 remove_waiter(lock, waiter);
1776 raw_spin_unlock_irq(&lock->wait_lock);
1782 * rt_mutex_next_owner - return the next owner of the lock
1784 * @lock: the rt lock query
1786 * Returns the next owner of the lock or NULL
1788 * Caller has to serialize against other accessors to the lock
1791 * Special API call for PI-futex support
1793 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1795 if (!rt_mutex_has_waiters(lock))
1798 return rt_mutex_top_waiter(lock)->task;
1802 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
1803 * @lock: the rt_mutex we were woken on
1804 * @to: the timeout, null if none. hrtimer should already have
1806 * @waiter: the pre-initialized rt_mutex_waiter
1808 * Wait for the the lock acquisition started on our behalf by
1809 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1810 * rt_mutex_cleanup_proxy_lock().
1814 * <0 - error, one of -EINTR, -ETIMEDOUT
1816 * Special API call for PI-futex support
1818 int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
1819 struct hrtimer_sleeper *to,
1820 struct rt_mutex_waiter *waiter)
1824 raw_spin_lock_irq(&lock->wait_lock);
1825 /* sleep on the mutex */
1826 set_current_state(TASK_INTERRUPTIBLE);
1827 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1829 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1830 * have to fix that up.
1832 fixup_rt_mutex_waiters(lock);
1833 raw_spin_unlock_irq(&lock->wait_lock);
1839 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1840 * @lock: the rt_mutex we were woken on
1841 * @waiter: the pre-initialized rt_mutex_waiter
1843 * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
1844 * rt_mutex_wait_proxy_lock().
1846 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1847 * in fact still be granted ownership until we're removed. Therefore we can
1848 * find we are in fact the owner and must disregard the
1849 * rt_mutex_wait_proxy_lock() failure.
1852 * true - did the cleanup, we done.
1853 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1854 * caller should disregards its return value.
1856 * Special API call for PI-futex support
1858 bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1859 struct rt_mutex_waiter *waiter)
1861 bool cleanup = false;
1863 raw_spin_lock_irq(&lock->wait_lock);
1865 * Do an unconditional try-lock, this deals with the lock stealing
1866 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
1867 * sets a NULL owner.
1869 * We're not interested in the return value, because the subsequent
1870 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
1871 * we will own the lock and it will have removed the waiter. If we
1872 * failed the trylock, we're still not owner and we need to remove
1875 try_to_take_rt_mutex(lock, current, waiter);
1877 * Unless we're the owner; we're still enqueued on the wait_list.
1878 * So check if we became owner, if not, take us off the wait_list.
1880 if (rt_mutex_owner(lock) != current) {
1881 remove_waiter(lock, waiter);
1885 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1886 * have to fix that up.
1888 fixup_rt_mutex_waiters(lock);
1890 raw_spin_unlock_irq(&lock->wait_lock);