1 // SPDX-License-Identifier: GPL-2.0-only
3 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
5 * started by Ingo Molnar and Thomas Gleixner.
7 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9 * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
10 * Copyright (C) 2006 Esben Nielsen
12 * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
14 * Adaptive Spinlocks simplification:
15 * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
17 * See Documentation/locking/rt-mutex-design.rst for details.
19 #include <linux/sched.h>
20 #include <linux/sched/debug.h>
21 #include <linux/sched/deadline.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/rt.h>
24 #include <linux/sched/wake_q.h>
25 #include <linux/ww_mutex.h>
27 #include "rtmutex_common.h"
30 # define build_ww_mutex() (false)
31 # define ww_container_of(rtm) NULL
33 static inline int __ww_mutex_add_waiter(struct rt_mutex_waiter *waiter,
34 struct rt_mutex *lock,
35 struct ww_acquire_ctx *ww_ctx)
40 static inline void __ww_mutex_check_waiters(struct rt_mutex *lock,
41 struct ww_acquire_ctx *ww_ctx)
45 static inline void ww_mutex_lock_acquired(struct ww_mutex *lock,
46 struct ww_acquire_ctx *ww_ctx)
50 static inline int __ww_mutex_check_kill(struct rt_mutex *lock,
51 struct rt_mutex_waiter *waiter,
52 struct ww_acquire_ctx *ww_ctx)
58 # define build_ww_mutex() (true)
59 # define ww_container_of(rtm) container_of(rtm, struct ww_mutex, base)
60 # include "ww_mutex.h"
64 * lock->owner state tracking:
66 * lock->owner holds the task_struct pointer of the owner. Bit 0
67 * is used to keep track of the "lock has waiters" state.
70 * NULL 0 lock is free (fast acquire possible)
71 * NULL 1 lock is free and has waiters and the top waiter
72 * is going to take the lock*
73 * taskpointer 0 lock is held (fast release possible)
74 * taskpointer 1 lock is held and has waiters**
76 * The fast atomic compare exchange based acquire and release is only
77 * possible when bit 0 of lock->owner is 0.
79 * (*) It also can be a transitional state when grabbing the lock
80 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
81 * we need to set the bit0 before looking at the lock, and the owner may be
82 * NULL in this small time, hence this can be a transitional state.
84 * (**) There is a small time when bit 0 is set but there are no
85 * waiters. This can happen when grabbing the lock in the slow path.
86 * To prevent a cmpxchg of the owner releasing the lock, we need to
87 * set this bit before looking at the lock.
90 static __always_inline void
91 rt_mutex_set_owner(struct rt_mutex_base *lock, struct task_struct *owner)
93 unsigned long val = (unsigned long)owner;
95 if (rt_mutex_has_waiters(lock))
96 val |= RT_MUTEX_HAS_WAITERS;
98 WRITE_ONCE(lock->owner, (struct task_struct *)val);
101 static __always_inline void clear_rt_mutex_waiters(struct rt_mutex_base *lock)
103 lock->owner = (struct task_struct *)
104 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
107 static __always_inline void fixup_rt_mutex_waiters(struct rt_mutex_base *lock)
109 unsigned long owner, *p = (unsigned long *) &lock->owner;
111 if (rt_mutex_has_waiters(lock))
115 * The rbtree has no waiters enqueued, now make sure that the
116 * lock->owner still has the waiters bit set, otherwise the
117 * following can happen:
123 * l->owner = T1 | HAS_WAITERS;
131 * l->owner = T1 | HAS_WAITERS;
136 * signal(->T2) signal(->T3)
143 * ==> wait list is empty
147 * fixup_rt_mutex_waiters()
148 * if (wait_list_empty(l) {
150 * owner = l->owner & ~HAS_WAITERS;
154 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
155 * if (wait_list_empty(l) {
156 * owner = l->owner & ~HAS_WAITERS;
157 * cmpxchg(l->owner, T1, NULL)
158 * ===> Success (l->owner = NULL)
164 * With the check for the waiter bit in place T3 on CPU2 will not
165 * overwrite. All tasks fiddling with the waiters bit are
166 * serialized by l->lock, so nothing else can modify the waiters
167 * bit. If the bit is set then nothing can change l->owner either
168 * so the simple RMW is safe. The cmpxchg() will simply fail if it
169 * happens in the middle of the RMW because the waiters bit is
172 owner = READ_ONCE(*p);
173 if (owner & RT_MUTEX_HAS_WAITERS)
174 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
178 * We can speed up the acquire/release, if there's no debugging state to be
181 #ifndef CONFIG_DEBUG_RT_MUTEXES
182 static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
183 struct task_struct *old,
184 struct task_struct *new)
186 return try_cmpxchg_acquire(&lock->owner, &old, new);
189 static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
190 struct task_struct *old,
191 struct task_struct *new)
193 return try_cmpxchg_release(&lock->owner, &old, new);
197 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
198 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
199 * relaxed semantics suffice.
201 static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
203 unsigned long owner, *p = (unsigned long *) &lock->owner;
207 } while (cmpxchg_relaxed(p, owner,
208 owner | RT_MUTEX_HAS_WAITERS) != owner);
212 * Safe fastpath aware unlock:
213 * 1) Clear the waiters bit
214 * 2) Drop lock->wait_lock
215 * 3) Try to unlock the lock with cmpxchg
217 static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
219 __releases(lock->wait_lock)
221 struct task_struct *owner = rt_mutex_owner(lock);
223 clear_rt_mutex_waiters(lock);
224 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
226 * If a new waiter comes in between the unlock and the cmpxchg
227 * we have two situations:
231 * cmpxchg(p, owner, 0) == owner
232 * mark_rt_mutex_waiters(lock);
238 * mark_rt_mutex_waiters(lock);
240 * cmpxchg(p, owner, 0) != owner
249 return rt_mutex_cmpxchg_release(lock, owner, NULL);
253 static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
254 struct task_struct *old,
255 struct task_struct *new)
261 static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
262 struct task_struct *old,
263 struct task_struct *new)
268 static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
270 lock->owner = (struct task_struct *)
271 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
275 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
277 static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
279 __releases(lock->wait_lock)
282 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
287 static __always_inline int __waiter_prio(struct task_struct *task)
289 int prio = task->prio;
297 static __always_inline void
298 waiter_update_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
300 waiter->prio = __waiter_prio(task);
301 waiter->deadline = task->dl.deadline;
305 * Only use with rt_mutex_waiter_{less,equal}()
307 #define task_to_waiter(p) \
308 &(struct rt_mutex_waiter){ .prio = __waiter_prio(p), .deadline = (p)->dl.deadline }
310 static __always_inline int rt_mutex_waiter_less(struct rt_mutex_waiter *left,
311 struct rt_mutex_waiter *right)
313 if (left->prio < right->prio)
317 * If both waiters have dl_prio(), we check the deadlines of the
319 * If left waiter has a dl_prio(), and we didn't return 1 above,
320 * then right waiter has a dl_prio() too.
322 if (dl_prio(left->prio))
323 return dl_time_before(left->deadline, right->deadline);
328 static __always_inline int rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
329 struct rt_mutex_waiter *right)
331 if (left->prio != right->prio)
335 * If both waiters have dl_prio(), we check the deadlines of the
337 * If left waiter has a dl_prio(), and we didn't return 0 above,
338 * then right waiter has a dl_prio() too.
340 if (dl_prio(left->prio))
341 return left->deadline == right->deadline;
346 static inline bool rt_mutex_steal(struct rt_mutex_waiter *waiter,
347 struct rt_mutex_waiter *top_waiter)
349 if (rt_mutex_waiter_less(waiter, top_waiter))
352 #ifdef RT_MUTEX_BUILD_SPINLOCKS
354 * Note that RT tasks are excluded from same priority (lateral)
355 * steals to prevent the introduction of an unbounded latency.
357 if (rt_prio(waiter->prio) || dl_prio(waiter->prio))
360 return rt_mutex_waiter_equal(waiter, top_waiter);
366 #define __node_2_waiter(node) \
367 rb_entry((node), struct rt_mutex_waiter, tree_entry)
369 static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
371 struct rt_mutex_waiter *aw = __node_2_waiter(a);
372 struct rt_mutex_waiter *bw = __node_2_waiter(b);
374 if (rt_mutex_waiter_less(aw, bw))
377 if (!build_ww_mutex())
380 if (rt_mutex_waiter_less(bw, aw))
383 /* NOTE: relies on waiter->ww_ctx being set before insertion */
388 return (signed long)(aw->ww_ctx->stamp -
389 bw->ww_ctx->stamp) < 0;
395 static __always_inline void
396 rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
398 rb_add_cached(&waiter->tree_entry, &lock->waiters, __waiter_less);
401 static __always_inline void
402 rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
404 if (RB_EMPTY_NODE(&waiter->tree_entry))
407 rb_erase_cached(&waiter->tree_entry, &lock->waiters);
408 RB_CLEAR_NODE(&waiter->tree_entry);
411 #define __node_2_pi_waiter(node) \
412 rb_entry((node), struct rt_mutex_waiter, pi_tree_entry)
414 static __always_inline bool
415 __pi_waiter_less(struct rb_node *a, const struct rb_node *b)
417 return rt_mutex_waiter_less(__node_2_pi_waiter(a), __node_2_pi_waiter(b));
420 static __always_inline void
421 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
423 rb_add_cached(&waiter->pi_tree_entry, &task->pi_waiters, __pi_waiter_less);
426 static __always_inline void
427 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
429 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
432 rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
433 RB_CLEAR_NODE(&waiter->pi_tree_entry);
436 static __always_inline void rt_mutex_adjust_prio(struct task_struct *p)
438 struct task_struct *pi_task = NULL;
440 lockdep_assert_held(&p->pi_lock);
442 if (task_has_pi_waiters(p))
443 pi_task = task_top_pi_waiter(p)->task;
445 rt_mutex_setprio(p, pi_task);
448 /* RT mutex specific wake_q wrappers */
449 static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh,
450 struct rt_mutex_waiter *w)
452 if (IS_ENABLED(CONFIG_PREEMPT_RT) && w->wake_state != TASK_NORMAL) {
453 if (IS_ENABLED(CONFIG_PROVE_LOCKING))
454 WARN_ON_ONCE(wqh->rtlock_task);
455 get_task_struct(w->task);
456 wqh->rtlock_task = w->task;
458 wake_q_add(&wqh->head, w->task);
462 static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
464 if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) {
465 wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT);
466 put_task_struct(wqh->rtlock_task);
467 wqh->rtlock_task = NULL;
470 if (!wake_q_empty(&wqh->head))
471 wake_up_q(&wqh->head);
473 /* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
478 * Deadlock detection is conditional:
480 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
481 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
483 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
484 * conducted independent of the detect argument.
486 * If the waiter argument is NULL this indicates the deboost path and
487 * deadlock detection is disabled independent of the detect argument
488 * and the config settings.
490 static __always_inline bool
491 rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
492 enum rtmutex_chainwalk chwalk)
494 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
495 return waiter != NULL;
496 return chwalk == RT_MUTEX_FULL_CHAINWALK;
499 static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p)
501 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
505 * Adjust the priority chain. Also used for deadlock detection.
506 * Decreases task's usage by one - may thus free the task.
508 * @task: the task owning the mutex (owner) for which a chain walk is
510 * @chwalk: do we have to carry out deadlock detection?
511 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
512 * things for a task that has just got its priority adjusted, and
513 * is waiting on a mutex)
514 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
515 * we dropped its pi_lock. Is never dereferenced, only used for
516 * comparison to detect lock chain changes.
517 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
518 * its priority to the mutex owner (can be NULL in the case
519 * depicted above or if the top waiter is gone away and we are
520 * actually deboosting the owner)
521 * @top_task: the current top waiter
523 * Returns 0 or -EDEADLK.
525 * Chain walk basics and protection scope
527 * [R] refcount on task
528 * [P] task->pi_lock held
529 * [L] rtmutex->wait_lock held
531 * Step Description Protected by
532 * function arguments:
534 * @orig_lock if != NULL @top_task is blocked on it
535 * @next_lock Unprotected. Cannot be
536 * dereferenced. Only used for
538 * @orig_waiter if != NULL @top_task is blocked on it
539 * @top_task current, or in case of proxy
540 * locking protected by calling
543 * loop_sanity_check();
545 * [1] lock(task->pi_lock); [R] acquire [P]
546 * [2] waiter = task->pi_blocked_on; [P]
547 * [3] check_exit_conditions_1(); [P]
548 * [4] lock = waiter->lock; [P]
549 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
550 * unlock(task->pi_lock); release [P]
553 * [6] check_exit_conditions_2(); [P] + [L]
554 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
555 * [8] unlock(task->pi_lock); release [P]
556 * put_task_struct(task); release [R]
557 * [9] check_exit_conditions_3(); [L]
558 * [10] task = owner(lock); [L]
559 * get_task_struct(task); [L] acquire [R]
560 * lock(task->pi_lock); [L] acquire [P]
561 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
562 * [12] check_exit_conditions_4(); [P] + [L]
563 * [13] unlock(task->pi_lock); release [P]
564 * unlock(lock->wait_lock); release [L]
567 static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
568 enum rtmutex_chainwalk chwalk,
569 struct rt_mutex_base *orig_lock,
570 struct rt_mutex_base *next_lock,
571 struct rt_mutex_waiter *orig_waiter,
572 struct task_struct *top_task)
574 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
575 struct rt_mutex_waiter *prerequeue_top_waiter;
576 int ret = 0, depth = 0;
577 struct rt_mutex_base *lock;
578 bool detect_deadlock;
581 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
584 * The (de)boosting is a step by step approach with a lot of
585 * pitfalls. We want this to be preemptible and we want hold a
586 * maximum of two locks per step. So we have to check
587 * carefully whether things change under us.
591 * We limit the lock chain length for each invocation.
593 if (++depth > max_lock_depth) {
597 * Print this only once. If the admin changes the limit,
598 * print a new message when reaching the limit again.
600 if (prev_max != max_lock_depth) {
601 prev_max = max_lock_depth;
602 printk(KERN_WARNING "Maximum lock depth %d reached "
603 "task: %s (%d)\n", max_lock_depth,
604 top_task->comm, task_pid_nr(top_task));
606 put_task_struct(task);
612 * We are fully preemptible here and only hold the refcount on
613 * @task. So everything can have changed under us since the
614 * caller or our own code below (goto retry/again) dropped all
619 * [1] Task cannot go away as we did a get_task() before !
621 raw_spin_lock_irq(&task->pi_lock);
624 * [2] Get the waiter on which @task is blocked on.
626 waiter = task->pi_blocked_on;
629 * [3] check_exit_conditions_1() protected by task->pi_lock.
633 * Check whether the end of the boosting chain has been
634 * reached or the state of the chain has changed while we
641 * Check the orig_waiter state. After we dropped the locks,
642 * the previous owner of the lock might have released the lock.
644 if (orig_waiter && !rt_mutex_owner(orig_lock))
648 * We dropped all locks after taking a refcount on @task, so
649 * the task might have moved on in the lock chain or even left
650 * the chain completely and blocks now on an unrelated lock or
653 * We stored the lock on which @task was blocked in @next_lock,
654 * so we can detect the chain change.
656 if (next_lock != waiter->lock)
660 * Drop out, when the task has no waiters. Note,
661 * top_waiter can be NULL, when we are in the deboosting
665 if (!task_has_pi_waiters(task))
668 * If deadlock detection is off, we stop here if we
669 * are not the top pi waiter of the task. If deadlock
670 * detection is enabled we continue, but stop the
671 * requeueing in the chain walk.
673 if (top_waiter != task_top_pi_waiter(task)) {
674 if (!detect_deadlock)
682 * If the waiter priority is the same as the task priority
683 * then there is no further priority adjustment necessary. If
684 * deadlock detection is off, we stop the chain walk. If its
685 * enabled we continue, but stop the requeueing in the chain
688 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
689 if (!detect_deadlock)
696 * [4] Get the next lock
700 * [5] We need to trylock here as we are holding task->pi_lock,
701 * which is the reverse lock order versus the other rtmutex
704 if (!raw_spin_trylock(&lock->wait_lock)) {
705 raw_spin_unlock_irq(&task->pi_lock);
711 * [6] check_exit_conditions_2() protected by task->pi_lock and
714 * Deadlock detection. If the lock is the same as the original
715 * lock which caused us to walk the lock chain or if the
716 * current lock is owned by the task which initiated the chain
717 * walk, we detected a deadlock.
719 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
720 raw_spin_unlock(&lock->wait_lock);
726 * If we just follow the lock chain for deadlock detection, no
727 * need to do all the requeue operations. To avoid a truckload
728 * of conditionals around the various places below, just do the
729 * minimum chain walk checks.
733 * No requeue[7] here. Just release @task [8]
735 raw_spin_unlock(&task->pi_lock);
736 put_task_struct(task);
739 * [9] check_exit_conditions_3 protected by lock->wait_lock.
740 * If there is no owner of the lock, end of chain.
742 if (!rt_mutex_owner(lock)) {
743 raw_spin_unlock_irq(&lock->wait_lock);
747 /* [10] Grab the next task, i.e. owner of @lock */
748 task = get_task_struct(rt_mutex_owner(lock));
749 raw_spin_lock(&task->pi_lock);
752 * No requeue [11] here. We just do deadlock detection.
754 * [12] Store whether owner is blocked
755 * itself. Decision is made after dropping the locks
757 next_lock = task_blocked_on_lock(task);
759 * Get the top waiter for the next iteration
761 top_waiter = rt_mutex_top_waiter(lock);
763 /* [13] Drop locks */
764 raw_spin_unlock(&task->pi_lock);
765 raw_spin_unlock_irq(&lock->wait_lock);
767 /* If owner is not blocked, end of chain. */
774 * Store the current top waiter before doing the requeue
775 * operation on @lock. We need it for the boost/deboost
778 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
780 /* [7] Requeue the waiter in the lock waiter tree. */
781 rt_mutex_dequeue(lock, waiter);
784 * Update the waiter prio fields now that we're dequeued.
786 * These values can have changed through either:
788 * sys_sched_set_scheduler() / sys_sched_setattr()
792 * DL CBS enforcement advancing the effective deadline.
794 * Even though pi_waiters also uses these fields, and that tree is only
795 * updated in [11], we can do this here, since we hold [L], which
796 * serializes all pi_waiters access and rb_erase() does not care about
797 * the values of the node being removed.
799 waiter_update_prio(waiter, task);
801 rt_mutex_enqueue(lock, waiter);
803 /* [8] Release the task */
804 raw_spin_unlock(&task->pi_lock);
805 put_task_struct(task);
808 * [9] check_exit_conditions_3 protected by lock->wait_lock.
810 * We must abort the chain walk if there is no lock owner even
811 * in the dead lock detection case, as we have nothing to
812 * follow here. This is the end of the chain we are walking.
814 if (!rt_mutex_owner(lock)) {
816 * If the requeue [7] above changed the top waiter,
817 * then we need to wake the new top waiter up to try
820 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
821 wake_up_state(waiter->task, waiter->wake_state);
822 raw_spin_unlock_irq(&lock->wait_lock);
826 /* [10] Grab the next task, i.e. the owner of @lock */
827 task = get_task_struct(rt_mutex_owner(lock));
828 raw_spin_lock(&task->pi_lock);
830 /* [11] requeue the pi waiters if necessary */
831 if (waiter == rt_mutex_top_waiter(lock)) {
833 * The waiter became the new top (highest priority)
834 * waiter on the lock. Replace the previous top waiter
835 * in the owner tasks pi waiters tree with this waiter
836 * and adjust the priority of the owner.
838 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
839 rt_mutex_enqueue_pi(task, waiter);
840 rt_mutex_adjust_prio(task);
842 } else if (prerequeue_top_waiter == waiter) {
844 * The waiter was the top waiter on the lock, but is
845 * no longer the top priority waiter. Replace waiter in
846 * the owner tasks pi waiters tree with the new top
847 * (highest priority) waiter and adjust the priority
849 * The new top waiter is stored in @waiter so that
850 * @waiter == @top_waiter evaluates to true below and
851 * we continue to deboost the rest of the chain.
853 rt_mutex_dequeue_pi(task, waiter);
854 waiter = rt_mutex_top_waiter(lock);
855 rt_mutex_enqueue_pi(task, waiter);
856 rt_mutex_adjust_prio(task);
859 * Nothing changed. No need to do any priority
865 * [12] check_exit_conditions_4() protected by task->pi_lock
866 * and lock->wait_lock. The actual decisions are made after we
869 * Check whether the task which owns the current lock is pi
870 * blocked itself. If yes we store a pointer to the lock for
871 * the lock chain change detection above. After we dropped
872 * task->pi_lock next_lock cannot be dereferenced anymore.
874 next_lock = task_blocked_on_lock(task);
876 * Store the top waiter of @lock for the end of chain walk
879 top_waiter = rt_mutex_top_waiter(lock);
881 /* [13] Drop the locks */
882 raw_spin_unlock(&task->pi_lock);
883 raw_spin_unlock_irq(&lock->wait_lock);
886 * Make the actual exit decisions [12], based on the stored
889 * We reached the end of the lock chain. Stop right here. No
890 * point to go back just to figure that out.
896 * If the current waiter is not the top waiter on the lock,
897 * then we can stop the chain walk here if we are not in full
898 * deadlock detection mode.
900 if (!detect_deadlock && waiter != top_waiter)
906 raw_spin_unlock_irq(&task->pi_lock);
908 put_task_struct(task);
914 * Try to take an rt-mutex
916 * Must be called with lock->wait_lock held and interrupts disabled
918 * @lock: The lock to be acquired.
919 * @task: The task which wants to acquire the lock
920 * @waiter: The waiter that is queued to the lock's wait tree if the
921 * callsite called task_blocked_on_lock(), otherwise NULL
924 try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
925 struct rt_mutex_waiter *waiter)
927 lockdep_assert_held(&lock->wait_lock);
930 * Before testing whether we can acquire @lock, we set the
931 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
932 * other tasks which try to modify @lock into the slow path
933 * and they serialize on @lock->wait_lock.
935 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
936 * as explained at the top of this file if and only if:
938 * - There is a lock owner. The caller must fixup the
939 * transient state if it does a trylock or leaves the lock
940 * function due to a signal or timeout.
942 * - @task acquires the lock and there are no other
943 * waiters. This is undone in rt_mutex_set_owner(@task) at
944 * the end of this function.
946 mark_rt_mutex_waiters(lock);
949 * If @lock has an owner, give up.
951 if (rt_mutex_owner(lock))
955 * If @waiter != NULL, @task has already enqueued the waiter
956 * into @lock waiter tree. If @waiter == NULL then this is a
960 struct rt_mutex_waiter *top_waiter = rt_mutex_top_waiter(lock);
963 * If waiter is the highest priority waiter of @lock,
964 * or allowed to steal it, take it over.
966 if (waiter == top_waiter || rt_mutex_steal(waiter, top_waiter)) {
968 * We can acquire the lock. Remove the waiter from the
971 rt_mutex_dequeue(lock, waiter);
977 * If the lock has waiters already we check whether @task is
978 * eligible to take over the lock.
980 * If there are no other waiters, @task can acquire
981 * the lock. @task->pi_blocked_on is NULL, so it does
982 * not need to be dequeued.
984 if (rt_mutex_has_waiters(lock)) {
985 /* Check whether the trylock can steal it. */
986 if (!rt_mutex_steal(task_to_waiter(task),
987 rt_mutex_top_waiter(lock)))
991 * The current top waiter stays enqueued. We
992 * don't have to change anything in the lock
997 * No waiters. Take the lock without the
998 * pi_lock dance.@task->pi_blocked_on is NULL
999 * and we have no waiters to enqueue in @task
1007 * Clear @task->pi_blocked_on. Requires protection by
1008 * @task->pi_lock. Redundant operation for the @waiter == NULL
1009 * case, but conditionals are more expensive than a redundant
1012 raw_spin_lock(&task->pi_lock);
1013 task->pi_blocked_on = NULL;
1015 * Finish the lock acquisition. @task is the new owner. If
1016 * other waiters exist we have to insert the highest priority
1017 * waiter into @task->pi_waiters tree.
1019 if (rt_mutex_has_waiters(lock))
1020 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
1021 raw_spin_unlock(&task->pi_lock);
1025 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
1026 * are still waiters or clears it.
1028 rt_mutex_set_owner(lock, task);
1034 * Task blocks on lock.
1036 * Prepare waiter and propagate pi chain
1038 * This must be called with lock->wait_lock held and interrupts disabled
1040 static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
1041 struct rt_mutex_waiter *waiter,
1042 struct task_struct *task,
1043 struct ww_acquire_ctx *ww_ctx,
1044 enum rtmutex_chainwalk chwalk)
1046 struct task_struct *owner = rt_mutex_owner(lock);
1047 struct rt_mutex_waiter *top_waiter = waiter;
1048 struct rt_mutex_base *next_lock;
1049 int chain_walk = 0, res;
1051 lockdep_assert_held(&lock->wait_lock);
1054 * Early deadlock detection. We really don't want the task to
1055 * enqueue on itself just to untangle the mess later. It's not
1056 * only an optimization. We drop the locks, so another waiter
1057 * can come in before the chain walk detects the deadlock. So
1058 * the other will detect the deadlock and return -EDEADLOCK,
1059 * which is wrong, as the other waiter is not in a deadlock
1065 raw_spin_lock(&task->pi_lock);
1066 waiter->task = task;
1067 waiter->lock = lock;
1068 waiter_update_prio(waiter, task);
1070 /* Get the top priority waiter on the lock */
1071 if (rt_mutex_has_waiters(lock))
1072 top_waiter = rt_mutex_top_waiter(lock);
1073 rt_mutex_enqueue(lock, waiter);
1075 task->pi_blocked_on = waiter;
1077 raw_spin_unlock(&task->pi_lock);
1079 if (build_ww_mutex() && ww_ctx) {
1080 struct rt_mutex *rtm;
1082 /* Check whether the waiter should back out immediately */
1083 rtm = container_of(lock, struct rt_mutex, rtmutex);
1084 res = __ww_mutex_add_waiter(waiter, rtm, ww_ctx);
1092 raw_spin_lock(&owner->pi_lock);
1093 if (waiter == rt_mutex_top_waiter(lock)) {
1094 rt_mutex_dequeue_pi(owner, top_waiter);
1095 rt_mutex_enqueue_pi(owner, waiter);
1097 rt_mutex_adjust_prio(owner);
1098 if (owner->pi_blocked_on)
1100 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
1104 /* Store the lock on which owner is blocked or NULL */
1105 next_lock = task_blocked_on_lock(owner);
1107 raw_spin_unlock(&owner->pi_lock);
1109 * Even if full deadlock detection is on, if the owner is not
1110 * blocked itself, we can avoid finding this out in the chain
1113 if (!chain_walk || !next_lock)
1117 * The owner can't disappear while holding a lock,
1118 * so the owner struct is protected by wait_lock.
1119 * Gets dropped in rt_mutex_adjust_prio_chain()!
1121 get_task_struct(owner);
1123 raw_spin_unlock_irq(&lock->wait_lock);
1125 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
1126 next_lock, waiter, task);
1128 raw_spin_lock_irq(&lock->wait_lock);
1134 * Remove the top waiter from the current tasks pi waiter tree and
1137 * Called with lock->wait_lock held and interrupts disabled.
1139 static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh,
1140 struct rt_mutex_base *lock)
1142 struct rt_mutex_waiter *waiter;
1144 raw_spin_lock(¤t->pi_lock);
1146 waiter = rt_mutex_top_waiter(lock);
1149 * Remove it from current->pi_waiters and deboost.
1151 * We must in fact deboost here in order to ensure we call
1152 * rt_mutex_setprio() to update p->pi_top_task before the
1155 rt_mutex_dequeue_pi(current, waiter);
1156 rt_mutex_adjust_prio(current);
1159 * As we are waking up the top waiter, and the waiter stays
1160 * queued on the lock until it gets the lock, this lock
1161 * obviously has waiters. Just set the bit here and this has
1162 * the added benefit of forcing all new tasks into the
1163 * slow path making sure no task of lower priority than
1164 * the top waiter can steal this lock.
1166 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
1169 * We deboosted before waking the top waiter task such that we don't
1170 * run two tasks with the 'same' priority (and ensure the
1171 * p->pi_top_task pointer points to a blocked task). This however can
1172 * lead to priority inversion if we would get preempted after the
1173 * deboost but before waking our donor task, hence the preempt_disable()
1176 * Pairs with preempt_enable() in rt_mutex_wake_up_q();
1179 rt_mutex_wake_q_add(wqh, waiter);
1180 raw_spin_unlock(¤t->pi_lock);
1183 static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1185 int ret = try_to_take_rt_mutex(lock, current, NULL);
1188 * try_to_take_rt_mutex() sets the lock waiters bit
1189 * unconditionally. Clean this up.
1191 fixup_rt_mutex_waiters(lock);
1197 * Slow path try-lock function:
1199 static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1201 unsigned long flags;
1205 * If the lock already has an owner we fail to get the lock.
1206 * This can be done without taking the @lock->wait_lock as
1207 * it is only being read, and this is a trylock anyway.
1209 if (rt_mutex_owner(lock))
1213 * The mutex has currently no owner. Lock the wait lock and try to
1214 * acquire the lock. We use irqsave here to support early boot calls.
1216 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1218 ret = __rt_mutex_slowtrylock(lock);
1220 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1225 static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock)
1227 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1230 return rt_mutex_slowtrylock(lock);
1234 * Slow path to release a rt-mutex.
1236 static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
1238 DEFINE_RT_WAKE_Q(wqh);
1239 unsigned long flags;
1241 /* irqsave required to support early boot calls */
1242 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1244 debug_rt_mutex_unlock(lock);
1247 * We must be careful here if the fast path is enabled. If we
1248 * have no waiters queued we cannot set owner to NULL here
1251 * foo->lock->owner = NULL;
1252 * rtmutex_lock(foo->lock); <- fast path
1253 * free = atomic_dec_and_test(foo->refcnt);
1254 * rtmutex_unlock(foo->lock); <- fast path
1257 * raw_spin_unlock(foo->lock->wait_lock);
1259 * So for the fastpath enabled kernel:
1261 * Nothing can set the waiters bit as long as we hold
1262 * lock->wait_lock. So we do the following sequence:
1264 * owner = rt_mutex_owner(lock);
1265 * clear_rt_mutex_waiters(lock);
1266 * raw_spin_unlock(&lock->wait_lock);
1267 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1271 * The fastpath disabled variant is simple as all access to
1272 * lock->owner is serialized by lock->wait_lock:
1274 * lock->owner = NULL;
1275 * raw_spin_unlock(&lock->wait_lock);
1277 while (!rt_mutex_has_waiters(lock)) {
1278 /* Drops lock->wait_lock ! */
1279 if (unlock_rt_mutex_safe(lock, flags) == true)
1281 /* Relock the rtmutex and try again */
1282 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1286 * The wakeup next waiter path does not suffer from the above
1287 * race. See the comments there.
1289 * Queue the next waiter for wakeup once we release the wait_lock.
1291 mark_wakeup_next_waiter(&wqh, lock);
1292 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1294 rt_mutex_wake_up_q(&wqh);
1297 static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
1299 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1302 rt_mutex_slowunlock(lock);
1306 static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1307 struct rt_mutex_waiter *waiter,
1308 struct task_struct *owner)
1314 /* If owner changed, trylock again. */
1315 if (owner != rt_mutex_owner(lock))
1318 * Ensure that @owner is dereferenced after checking that
1319 * the lock owner still matches @owner. If that fails,
1320 * @owner might point to freed memory. If it still matches,
1321 * the rcu_read_lock() ensures the memory stays valid.
1325 * Stop spinning when:
1326 * - the lock owner has been scheduled out
1327 * - current is not longer the top waiter
1328 * - current is requested to reschedule (redundant
1329 * for CONFIG_PREEMPT_RCU=y)
1330 * - the VCPU on which owner runs is preempted
1332 if (!owner->on_cpu || need_resched() ||
1333 rt_mutex_waiter_is_top_waiter(lock, waiter) ||
1334 vcpu_is_preempted(task_cpu(owner))) {
1344 static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1345 struct rt_mutex_waiter *waiter,
1346 struct task_struct *owner)
1352 #ifdef RT_MUTEX_BUILD_MUTEX
1354 * Functions required for:
1355 * - rtmutex, futex on all kernels
1356 * - mutex and rwsem substitutions on RT kernels
1360 * Remove a waiter from a lock and give up
1362 * Must be called with lock->wait_lock held and interrupts disabled. It must
1363 * have just failed to try_to_take_rt_mutex().
1365 static void __sched remove_waiter(struct rt_mutex_base *lock,
1366 struct rt_mutex_waiter *waiter)
1368 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
1369 struct task_struct *owner = rt_mutex_owner(lock);
1370 struct rt_mutex_base *next_lock;
1372 lockdep_assert_held(&lock->wait_lock);
1374 raw_spin_lock(¤t->pi_lock);
1375 rt_mutex_dequeue(lock, waiter);
1376 current->pi_blocked_on = NULL;
1377 raw_spin_unlock(¤t->pi_lock);
1380 * Only update priority if the waiter was the highest priority
1381 * waiter of the lock and there is an owner to update.
1383 if (!owner || !is_top_waiter)
1386 raw_spin_lock(&owner->pi_lock);
1388 rt_mutex_dequeue_pi(owner, waiter);
1390 if (rt_mutex_has_waiters(lock))
1391 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
1393 rt_mutex_adjust_prio(owner);
1395 /* Store the lock on which owner is blocked or NULL */
1396 next_lock = task_blocked_on_lock(owner);
1398 raw_spin_unlock(&owner->pi_lock);
1401 * Don't walk the chain, if the owner task is not blocked
1407 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1408 get_task_struct(owner);
1410 raw_spin_unlock_irq(&lock->wait_lock);
1412 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1413 next_lock, NULL, current);
1415 raw_spin_lock_irq(&lock->wait_lock);
1419 * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop
1420 * @lock: the rt_mutex to take
1421 * @ww_ctx: WW mutex context pointer
1422 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1423 * or TASK_UNINTERRUPTIBLE)
1424 * @timeout: the pre-initialized and started timer, or NULL for none
1425 * @waiter: the pre-initialized rt_mutex_waiter
1427 * Must be called with lock->wait_lock held and interrupts disabled
1429 static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
1430 struct ww_acquire_ctx *ww_ctx,
1432 struct hrtimer_sleeper *timeout,
1433 struct rt_mutex_waiter *waiter)
1435 struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1436 struct task_struct *owner;
1440 /* Try to acquire the lock: */
1441 if (try_to_take_rt_mutex(lock, current, waiter))
1444 if (timeout && !timeout->task) {
1448 if (signal_pending_state(state, current)) {
1453 if (build_ww_mutex() && ww_ctx) {
1454 ret = __ww_mutex_check_kill(rtm, waiter, ww_ctx);
1459 if (waiter == rt_mutex_top_waiter(lock))
1460 owner = rt_mutex_owner(lock);
1463 raw_spin_unlock_irq(&lock->wait_lock);
1465 if (!owner || !rtmutex_spin_on_owner(lock, waiter, owner))
1468 raw_spin_lock_irq(&lock->wait_lock);
1469 set_current_state(state);
1472 __set_current_state(TASK_RUNNING);
1476 static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
1477 struct rt_mutex_waiter *w)
1480 * If the result is not -EDEADLOCK or the caller requested
1481 * deadlock detection, nothing to do here.
1483 if (res != -EDEADLOCK || detect_deadlock)
1486 if (build_ww_mutex() && w->ww_ctx)
1490 * Yell loudly and stop the task right here.
1492 WARN(1, "rtmutex deadlock detected\n");
1494 set_current_state(TASK_INTERRUPTIBLE);
1500 * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held
1501 * @lock: The rtmutex to block lock
1502 * @ww_ctx: WW mutex context pointer
1503 * @state: The task state for sleeping
1504 * @chwalk: Indicator whether full or partial chainwalk is requested
1505 * @waiter: Initializer waiter for blocking
1507 static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,
1508 struct ww_acquire_ctx *ww_ctx,
1510 enum rtmutex_chainwalk chwalk,
1511 struct rt_mutex_waiter *waiter)
1513 struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1514 struct ww_mutex *ww = ww_container_of(rtm);
1517 lockdep_assert_held(&lock->wait_lock);
1519 /* Try to acquire the lock again: */
1520 if (try_to_take_rt_mutex(lock, current, NULL)) {
1521 if (build_ww_mutex() && ww_ctx) {
1522 __ww_mutex_check_waiters(rtm, ww_ctx);
1523 ww_mutex_lock_acquired(ww, ww_ctx);
1528 set_current_state(state);
1530 ret = task_blocks_on_rt_mutex(lock, waiter, current, ww_ctx, chwalk);
1532 ret = rt_mutex_slowlock_block(lock, ww_ctx, state, NULL, waiter);
1535 /* acquired the lock */
1536 if (build_ww_mutex() && ww_ctx) {
1537 if (!ww_ctx->is_wait_die)
1538 __ww_mutex_check_waiters(rtm, ww_ctx);
1539 ww_mutex_lock_acquired(ww, ww_ctx);
1542 __set_current_state(TASK_RUNNING);
1543 remove_waiter(lock, waiter);
1544 rt_mutex_handle_deadlock(ret, chwalk, waiter);
1548 * try_to_take_rt_mutex() sets the waiter bit
1549 * unconditionally. We might have to fix that up.
1551 fixup_rt_mutex_waiters(lock);
1555 static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock,
1556 struct ww_acquire_ctx *ww_ctx,
1559 struct rt_mutex_waiter waiter;
1562 rt_mutex_init_waiter(&waiter);
1563 waiter.ww_ctx = ww_ctx;
1565 ret = __rt_mutex_slowlock(lock, ww_ctx, state, RT_MUTEX_MIN_CHAINWALK,
1568 debug_rt_mutex_free_waiter(&waiter);
1573 * rt_mutex_slowlock - Locking slowpath invoked when fast path fails
1574 * @lock: The rtmutex to block lock
1575 * @ww_ctx: WW mutex context pointer
1576 * @state: The task state for sleeping
1578 static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
1579 struct ww_acquire_ctx *ww_ctx,
1582 unsigned long flags;
1586 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1587 * be called in early boot if the cmpxchg() fast path is disabled
1588 * (debug, no architecture support). In this case we will acquire the
1589 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1590 * enable interrupts in that early boot case. So we need to use the
1591 * irqsave/restore variants.
1593 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1594 ret = __rt_mutex_slowlock_locked(lock, ww_ctx, state);
1595 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1600 static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
1603 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1606 return rt_mutex_slowlock(lock, NULL, state);
1608 #endif /* RT_MUTEX_BUILD_MUTEX */
1610 #ifdef RT_MUTEX_BUILD_SPINLOCKS
1612 * Functions required for spin/rw_lock substitution on RT kernels
1616 * rtlock_slowlock_locked - Slow path lock acquisition for RT locks
1617 * @lock: The underlying RT mutex
1619 static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock)
1621 struct rt_mutex_waiter waiter;
1622 struct task_struct *owner;
1624 lockdep_assert_held(&lock->wait_lock);
1626 if (try_to_take_rt_mutex(lock, current, NULL))
1629 rt_mutex_init_rtlock_waiter(&waiter);
1631 /* Save current state and set state to TASK_RTLOCK_WAIT */
1632 current_save_and_set_rtlock_wait_state();
1634 task_blocks_on_rt_mutex(lock, &waiter, current, NULL, RT_MUTEX_MIN_CHAINWALK);
1637 /* Try to acquire the lock again */
1638 if (try_to_take_rt_mutex(lock, current, &waiter))
1641 if (&waiter == rt_mutex_top_waiter(lock))
1642 owner = rt_mutex_owner(lock);
1645 raw_spin_unlock_irq(&lock->wait_lock);
1647 if (!owner || !rtmutex_spin_on_owner(lock, &waiter, owner))
1650 raw_spin_lock_irq(&lock->wait_lock);
1651 set_current_state(TASK_RTLOCK_WAIT);
1654 /* Restore the task state */
1655 current_restore_rtlock_saved_state();
1658 * try_to_take_rt_mutex() sets the waiter bit unconditionally.
1659 * We might have to fix that up:
1661 fixup_rt_mutex_waiters(lock);
1662 debug_rt_mutex_free_waiter(&waiter);
1665 static __always_inline void __sched rtlock_slowlock(struct rt_mutex_base *lock)
1667 unsigned long flags;
1669 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1670 rtlock_slowlock_locked(lock);
1671 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1674 #endif /* RT_MUTEX_BUILD_SPINLOCKS */