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 * See Documentation/locking/rt-mutex-design.rst for details.
14 #include <linux/spinlock.h>
15 #include <linux/export.h>
16 #include <linux/sched/signal.h>
17 #include <linux/sched/rt.h>
18 #include <linux/sched/deadline.h>
19 #include <linux/sched/wake_q.h>
20 #include <linux/sched/debug.h>
21 #include <linux/timer.h>
23 #include "rtmutex_common.h"
26 * lock->owner state tracking:
28 * lock->owner holds the task_struct pointer of the owner. Bit 0
29 * is used to keep track of the "lock has waiters" state.
32 * NULL 0 lock is free (fast acquire possible)
33 * NULL 1 lock is free and has waiters and the top waiter
34 * is going to take the lock*
35 * taskpointer 0 lock is held (fast release possible)
36 * taskpointer 1 lock is held and has waiters**
38 * The fast atomic compare exchange based acquire and release is only
39 * possible when bit 0 of lock->owner is 0.
41 * (*) It also can be a transitional state when grabbing the lock
42 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
43 * we need to set the bit0 before looking at the lock, and the owner may be
44 * NULL in this small time, hence this can be a transitional state.
46 * (**) There is a small time when bit 0 is set but there are no
47 * waiters. This can happen when grabbing the lock in the slow path.
48 * To prevent a cmpxchg of the owner releasing the lock, we need to
49 * set this bit before looking at the lock.
53 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
55 unsigned long val = (unsigned long)owner;
57 if (rt_mutex_has_waiters(lock))
58 val |= RT_MUTEX_HAS_WAITERS;
60 WRITE_ONCE(lock->owner, (struct task_struct *)val);
63 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
65 lock->owner = (struct task_struct *)
66 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
69 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
71 unsigned long owner, *p = (unsigned long *) &lock->owner;
73 if (rt_mutex_has_waiters(lock))
77 * The rbtree has no waiters enqueued, now make sure that the
78 * lock->owner still has the waiters bit set, otherwise the
79 * following can happen:
85 * l->owner = T1 | HAS_WAITERS;
93 * l->owner = T1 | HAS_WAITERS;
98 * signal(->T2) signal(->T3)
105 * ==> wait list is empty
109 * fixup_rt_mutex_waiters()
110 * if (wait_list_empty(l) {
112 * owner = l->owner & ~HAS_WAITERS;
116 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
117 * if (wait_list_empty(l) {
118 * owner = l->owner & ~HAS_WAITERS;
119 * cmpxchg(l->owner, T1, NULL)
120 * ===> Success (l->owner = NULL)
126 * With the check for the waiter bit in place T3 on CPU2 will not
127 * overwrite. All tasks fiddling with the waiters bit are
128 * serialized by l->lock, so nothing else can modify the waiters
129 * bit. If the bit is set then nothing can change l->owner either
130 * so the simple RMW is safe. The cmpxchg() will simply fail if it
131 * happens in the middle of the RMW because the waiters bit is
134 owner = READ_ONCE(*p);
135 if (owner & RT_MUTEX_HAS_WAITERS)
136 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
140 * We can speed up the acquire/release, if there's no debugging state to be
143 #ifndef CONFIG_DEBUG_RT_MUTEXES
144 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
145 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
148 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
149 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
150 * relaxed semantics suffice.
152 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
154 unsigned long owner, *p = (unsigned long *) &lock->owner;
158 } while (cmpxchg_relaxed(p, owner,
159 owner | RT_MUTEX_HAS_WAITERS) != owner);
163 * Safe fastpath aware unlock:
164 * 1) Clear the waiters bit
165 * 2) Drop lock->wait_lock
166 * 3) Try to unlock the lock with cmpxchg
168 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
170 __releases(lock->wait_lock)
172 struct task_struct *owner = rt_mutex_owner(lock);
174 clear_rt_mutex_waiters(lock);
175 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
177 * If a new waiter comes in between the unlock and the cmpxchg
178 * we have two situations:
182 * cmpxchg(p, owner, 0) == owner
183 * mark_rt_mutex_waiters(lock);
189 * mark_rt_mutex_waiters(lock);
191 * cmpxchg(p, owner, 0) != owner
200 return rt_mutex_cmpxchg_release(lock, owner, NULL);
204 # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
205 # define rt_mutex_cmpxchg_release(l,c,n) (0)
207 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
209 lock->owner = (struct task_struct *)
210 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
214 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
216 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
218 __releases(lock->wait_lock)
221 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
227 * Only use with rt_mutex_waiter_{less,equal}()
229 #define task_to_waiter(p) \
230 &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
233 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
234 struct rt_mutex_waiter *right)
236 if (left->prio < right->prio)
240 * If both waiters have dl_prio(), we check the deadlines of the
242 * If left waiter has a dl_prio(), and we didn't return 1 above,
243 * then right waiter has a dl_prio() too.
245 if (dl_prio(left->prio))
246 return dl_time_before(left->deadline, right->deadline);
252 rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
253 struct rt_mutex_waiter *right)
255 if (left->prio != right->prio)
259 * If both waiters have dl_prio(), we check the deadlines of the
261 * If left waiter has a dl_prio(), and we didn't return 0 above,
262 * then right waiter has a dl_prio() too.
264 if (dl_prio(left->prio))
265 return left->deadline == right->deadline;
270 #define __node_2_waiter(node) \
271 rb_entry((node), struct rt_mutex_waiter, tree_entry)
273 static inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
275 return rt_mutex_waiter_less(__node_2_waiter(a), __node_2_waiter(b));
279 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
281 rb_add_cached(&waiter->tree_entry, &lock->waiters, __waiter_less);
285 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
287 if (RB_EMPTY_NODE(&waiter->tree_entry))
290 rb_erase_cached(&waiter->tree_entry, &lock->waiters);
291 RB_CLEAR_NODE(&waiter->tree_entry);
294 #define __node_2_pi_waiter(node) \
295 rb_entry((node), struct rt_mutex_waiter, pi_tree_entry)
297 static inline bool __pi_waiter_less(struct rb_node *a, const struct rb_node *b)
299 return rt_mutex_waiter_less(__node_2_pi_waiter(a), __node_2_pi_waiter(b));
303 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
305 rb_add_cached(&waiter->pi_tree_entry, &task->pi_waiters, __pi_waiter_less);
309 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
311 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
314 rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
315 RB_CLEAR_NODE(&waiter->pi_tree_entry);
318 static void rt_mutex_adjust_prio(struct task_struct *p)
320 struct task_struct *pi_task = NULL;
322 lockdep_assert_held(&p->pi_lock);
324 if (task_has_pi_waiters(p))
325 pi_task = task_top_pi_waiter(p)->task;
327 rt_mutex_setprio(p, pi_task);
331 * Deadlock detection is conditional:
333 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
334 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
336 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
337 * conducted independent of the detect argument.
339 * If the waiter argument is NULL this indicates the deboost path and
340 * deadlock detection is disabled independent of the detect argument
341 * and the config settings.
343 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
344 enum rtmutex_chainwalk chwalk)
347 * This is just a wrapper function for the following call,
348 * because debug_rt_mutex_detect_deadlock() smells like a magic
349 * debug feature and I wanted to keep the cond function in the
350 * main source file along with the comments instead of having
351 * two of the same in the headers.
353 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
357 * Max number of times we'll walk the boosting chain:
359 int max_lock_depth = 1024;
361 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
363 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
367 * Adjust the priority chain. Also used for deadlock detection.
368 * Decreases task's usage by one - may thus free the task.
370 * @task: the task owning the mutex (owner) for which a chain walk is
372 * @chwalk: do we have to carry out deadlock detection?
373 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
374 * things for a task that has just got its priority adjusted, and
375 * is waiting on a mutex)
376 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
377 * we dropped its pi_lock. Is never dereferenced, only used for
378 * comparison to detect lock chain changes.
379 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
380 * its priority to the mutex owner (can be NULL in the case
381 * depicted above or if the top waiter is gone away and we are
382 * actually deboosting the owner)
383 * @top_task: the current top waiter
385 * Returns 0 or -EDEADLK.
387 * Chain walk basics and protection scope
389 * [R] refcount on task
390 * [P] task->pi_lock held
391 * [L] rtmutex->wait_lock held
393 * Step Description Protected by
394 * function arguments:
396 * @orig_lock if != NULL @top_task is blocked on it
397 * @next_lock Unprotected. Cannot be
398 * dereferenced. Only used for
400 * @orig_waiter if != NULL @top_task is blocked on it
401 * @top_task current, or in case of proxy
402 * locking protected by calling
405 * loop_sanity_check();
407 * [1] lock(task->pi_lock); [R] acquire [P]
408 * [2] waiter = task->pi_blocked_on; [P]
409 * [3] check_exit_conditions_1(); [P]
410 * [4] lock = waiter->lock; [P]
411 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
412 * unlock(task->pi_lock); release [P]
415 * [6] check_exit_conditions_2(); [P] + [L]
416 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
417 * [8] unlock(task->pi_lock); release [P]
418 * put_task_struct(task); release [R]
419 * [9] check_exit_conditions_3(); [L]
420 * [10] task = owner(lock); [L]
421 * get_task_struct(task); [L] acquire [R]
422 * lock(task->pi_lock); [L] acquire [P]
423 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
424 * [12] check_exit_conditions_4(); [P] + [L]
425 * [13] unlock(task->pi_lock); release [P]
426 * unlock(lock->wait_lock); release [L]
429 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
430 enum rtmutex_chainwalk chwalk,
431 struct rt_mutex *orig_lock,
432 struct rt_mutex *next_lock,
433 struct rt_mutex_waiter *orig_waiter,
434 struct task_struct *top_task)
436 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
437 struct rt_mutex_waiter *prerequeue_top_waiter;
438 int ret = 0, depth = 0;
439 struct rt_mutex *lock;
440 bool detect_deadlock;
443 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
446 * The (de)boosting is a step by step approach with a lot of
447 * pitfalls. We want this to be preemptible and we want hold a
448 * maximum of two locks per step. So we have to check
449 * carefully whether things change under us.
453 * We limit the lock chain length for each invocation.
455 if (++depth > max_lock_depth) {
459 * Print this only once. If the admin changes the limit,
460 * print a new message when reaching the limit again.
462 if (prev_max != max_lock_depth) {
463 prev_max = max_lock_depth;
464 printk(KERN_WARNING "Maximum lock depth %d reached "
465 "task: %s (%d)\n", max_lock_depth,
466 top_task->comm, task_pid_nr(top_task));
468 put_task_struct(task);
474 * We are fully preemptible here and only hold the refcount on
475 * @task. So everything can have changed under us since the
476 * caller or our own code below (goto retry/again) dropped all
481 * [1] Task cannot go away as we did a get_task() before !
483 raw_spin_lock_irq(&task->pi_lock);
486 * [2] Get the waiter on which @task is blocked on.
488 waiter = task->pi_blocked_on;
491 * [3] check_exit_conditions_1() protected by task->pi_lock.
495 * Check whether the end of the boosting chain has been
496 * reached or the state of the chain has changed while we
503 * Check the orig_waiter state. After we dropped the locks,
504 * the previous owner of the lock might have released the lock.
506 if (orig_waiter && !rt_mutex_owner(orig_lock))
510 * We dropped all locks after taking a refcount on @task, so
511 * the task might have moved on in the lock chain or even left
512 * the chain completely and blocks now on an unrelated lock or
515 * We stored the lock on which @task was blocked in @next_lock,
516 * so we can detect the chain change.
518 if (next_lock != waiter->lock)
522 * Drop out, when the task has no waiters. Note,
523 * top_waiter can be NULL, when we are in the deboosting
527 if (!task_has_pi_waiters(task))
530 * If deadlock detection is off, we stop here if we
531 * are not the top pi waiter of the task. If deadlock
532 * detection is enabled we continue, but stop the
533 * requeueing in the chain walk.
535 if (top_waiter != task_top_pi_waiter(task)) {
536 if (!detect_deadlock)
544 * If the waiter priority is the same as the task priority
545 * then there is no further priority adjustment necessary. If
546 * deadlock detection is off, we stop the chain walk. If its
547 * enabled we continue, but stop the requeueing in the chain
550 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
551 if (!detect_deadlock)
558 * [4] Get the next lock
562 * [5] We need to trylock here as we are holding task->pi_lock,
563 * which is the reverse lock order versus the other rtmutex
566 if (!raw_spin_trylock(&lock->wait_lock)) {
567 raw_spin_unlock_irq(&task->pi_lock);
573 * [6] check_exit_conditions_2() protected by task->pi_lock and
576 * Deadlock detection. If the lock is the same as the original
577 * lock which caused us to walk the lock chain or if the
578 * current lock is owned by the task which initiated the chain
579 * walk, we detected a deadlock.
581 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
582 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
583 raw_spin_unlock(&lock->wait_lock);
589 * If we just follow the lock chain for deadlock detection, no
590 * need to do all the requeue operations. To avoid a truckload
591 * of conditionals around the various places below, just do the
592 * minimum chain walk checks.
596 * No requeue[7] here. Just release @task [8]
598 raw_spin_unlock(&task->pi_lock);
599 put_task_struct(task);
602 * [9] check_exit_conditions_3 protected by lock->wait_lock.
603 * If there is no owner of the lock, end of chain.
605 if (!rt_mutex_owner(lock)) {
606 raw_spin_unlock_irq(&lock->wait_lock);
610 /* [10] Grab the next task, i.e. owner of @lock */
611 task = get_task_struct(rt_mutex_owner(lock));
612 raw_spin_lock(&task->pi_lock);
615 * No requeue [11] here. We just do deadlock detection.
617 * [12] Store whether owner is blocked
618 * itself. Decision is made after dropping the locks
620 next_lock = task_blocked_on_lock(task);
622 * Get the top waiter for the next iteration
624 top_waiter = rt_mutex_top_waiter(lock);
626 /* [13] Drop locks */
627 raw_spin_unlock(&task->pi_lock);
628 raw_spin_unlock_irq(&lock->wait_lock);
630 /* If owner is not blocked, end of chain. */
637 * Store the current top waiter before doing the requeue
638 * operation on @lock. We need it for the boost/deboost
641 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
643 /* [7] Requeue the waiter in the lock waiter tree. */
644 rt_mutex_dequeue(lock, waiter);
647 * Update the waiter prio fields now that we're dequeued.
649 * These values can have changed through either:
651 * sys_sched_set_scheduler() / sys_sched_setattr()
655 * DL CBS enforcement advancing the effective deadline.
657 * Even though pi_waiters also uses these fields, and that tree is only
658 * updated in [11], we can do this here, since we hold [L], which
659 * serializes all pi_waiters access and rb_erase() does not care about
660 * the values of the node being removed.
662 waiter->prio = task->prio;
663 waiter->deadline = task->dl.deadline;
665 rt_mutex_enqueue(lock, waiter);
667 /* [8] Release the task */
668 raw_spin_unlock(&task->pi_lock);
669 put_task_struct(task);
672 * [9] check_exit_conditions_3 protected by lock->wait_lock.
674 * We must abort the chain walk if there is no lock owner even
675 * in the dead lock detection case, as we have nothing to
676 * follow here. This is the end of the chain we are walking.
678 if (!rt_mutex_owner(lock)) {
680 * If the requeue [7] above changed the top waiter,
681 * then we need to wake the new top waiter up to try
684 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
685 wake_up_process(rt_mutex_top_waiter(lock)->task);
686 raw_spin_unlock_irq(&lock->wait_lock);
690 /* [10] Grab the next task, i.e. the owner of @lock */
691 task = get_task_struct(rt_mutex_owner(lock));
692 raw_spin_lock(&task->pi_lock);
694 /* [11] requeue the pi waiters if necessary */
695 if (waiter == rt_mutex_top_waiter(lock)) {
697 * The waiter became the new top (highest priority)
698 * waiter on the lock. Replace the previous top waiter
699 * in the owner tasks pi waiters tree with this waiter
700 * and adjust the priority of the owner.
702 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
703 rt_mutex_enqueue_pi(task, waiter);
704 rt_mutex_adjust_prio(task);
706 } else if (prerequeue_top_waiter == waiter) {
708 * The waiter was the top waiter on the lock, but is
709 * no longer the top prority waiter. Replace waiter in
710 * the owner tasks pi waiters tree with the new top
711 * (highest priority) waiter and adjust the priority
713 * The new top waiter is stored in @waiter so that
714 * @waiter == @top_waiter evaluates to true below and
715 * we continue to deboost the rest of the chain.
717 rt_mutex_dequeue_pi(task, waiter);
718 waiter = rt_mutex_top_waiter(lock);
719 rt_mutex_enqueue_pi(task, waiter);
720 rt_mutex_adjust_prio(task);
723 * Nothing changed. No need to do any priority
729 * [12] check_exit_conditions_4() protected by task->pi_lock
730 * and lock->wait_lock. The actual decisions are made after we
733 * Check whether the task which owns the current lock is pi
734 * blocked itself. If yes we store a pointer to the lock for
735 * the lock chain change detection above. After we dropped
736 * task->pi_lock next_lock cannot be dereferenced anymore.
738 next_lock = task_blocked_on_lock(task);
740 * Store the top waiter of @lock for the end of chain walk
743 top_waiter = rt_mutex_top_waiter(lock);
745 /* [13] Drop the locks */
746 raw_spin_unlock(&task->pi_lock);
747 raw_spin_unlock_irq(&lock->wait_lock);
750 * Make the actual exit decisions [12], based on the stored
753 * We reached the end of the lock chain. Stop right here. No
754 * point to go back just to figure that out.
760 * If the current waiter is not the top waiter on the lock,
761 * then we can stop the chain walk here if we are not in full
762 * deadlock detection mode.
764 if (!detect_deadlock && waiter != top_waiter)
770 raw_spin_unlock_irq(&task->pi_lock);
772 put_task_struct(task);
778 * Try to take an rt-mutex
780 * Must be called with lock->wait_lock held and interrupts disabled
782 * @lock: The lock to be acquired.
783 * @task: The task which wants to acquire the lock
784 * @waiter: The waiter that is queued to the lock's wait tree if the
785 * callsite called task_blocked_on_lock(), otherwise NULL
787 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
788 struct rt_mutex_waiter *waiter)
790 lockdep_assert_held(&lock->wait_lock);
793 * Before testing whether we can acquire @lock, we set the
794 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
795 * other tasks which try to modify @lock into the slow path
796 * and they serialize on @lock->wait_lock.
798 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
799 * as explained at the top of this file if and only if:
801 * - There is a lock owner. The caller must fixup the
802 * transient state if it does a trylock or leaves the lock
803 * function due to a signal or timeout.
805 * - @task acquires the lock and there are no other
806 * waiters. This is undone in rt_mutex_set_owner(@task) at
807 * the end of this function.
809 mark_rt_mutex_waiters(lock);
812 * If @lock has an owner, give up.
814 if (rt_mutex_owner(lock))
818 * If @waiter != NULL, @task has already enqueued the waiter
819 * into @lock waiter tree. If @waiter == NULL then this is a
824 * If waiter is not the highest priority waiter of
827 if (waiter != rt_mutex_top_waiter(lock))
831 * We can acquire the lock. Remove the waiter from the
834 rt_mutex_dequeue(lock, waiter);
838 * If the lock has waiters already we check whether @task is
839 * eligible to take over the lock.
841 * If there are no other waiters, @task can acquire
842 * the lock. @task->pi_blocked_on is NULL, so it does
843 * not need to be dequeued.
845 if (rt_mutex_has_waiters(lock)) {
847 * If @task->prio is greater than or equal to
848 * the top waiter priority (kernel view),
851 if (!rt_mutex_waiter_less(task_to_waiter(task),
852 rt_mutex_top_waiter(lock)))
856 * The current top waiter stays enqueued. We
857 * don't have to change anything in the lock
862 * No waiters. Take the lock without the
863 * pi_lock dance.@task->pi_blocked_on is NULL
864 * and we have no waiters to enqueue in @task
872 * Clear @task->pi_blocked_on. Requires protection by
873 * @task->pi_lock. Redundant operation for the @waiter == NULL
874 * case, but conditionals are more expensive than a redundant
877 raw_spin_lock(&task->pi_lock);
878 task->pi_blocked_on = NULL;
880 * Finish the lock acquisition. @task is the new owner. If
881 * other waiters exist we have to insert the highest priority
882 * waiter into @task->pi_waiters tree.
884 if (rt_mutex_has_waiters(lock))
885 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
886 raw_spin_unlock(&task->pi_lock);
889 /* We got the lock. */
890 debug_rt_mutex_lock(lock);
893 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
894 * are still waiters or clears it.
896 rt_mutex_set_owner(lock, task);
902 * Task blocks on lock.
904 * Prepare waiter and propagate pi chain
906 * This must be called with lock->wait_lock held and interrupts disabled
908 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
909 struct rt_mutex_waiter *waiter,
910 struct task_struct *task,
911 enum rtmutex_chainwalk chwalk)
913 struct task_struct *owner = rt_mutex_owner(lock);
914 struct rt_mutex_waiter *top_waiter = waiter;
915 struct rt_mutex *next_lock;
916 int chain_walk = 0, res;
918 lockdep_assert_held(&lock->wait_lock);
921 * Early deadlock detection. We really don't want the task to
922 * enqueue on itself just to untangle the mess later. It's not
923 * only an optimization. We drop the locks, so another waiter
924 * can come in before the chain walk detects the deadlock. So
925 * the other will detect the deadlock and return -EDEADLOCK,
926 * which is wrong, as the other waiter is not in a deadlock
932 raw_spin_lock(&task->pi_lock);
935 waiter->prio = task->prio;
936 waiter->deadline = task->dl.deadline;
938 /* Get the top priority waiter on the lock */
939 if (rt_mutex_has_waiters(lock))
940 top_waiter = rt_mutex_top_waiter(lock);
941 rt_mutex_enqueue(lock, waiter);
943 task->pi_blocked_on = waiter;
945 raw_spin_unlock(&task->pi_lock);
950 raw_spin_lock(&owner->pi_lock);
951 if (waiter == rt_mutex_top_waiter(lock)) {
952 rt_mutex_dequeue_pi(owner, top_waiter);
953 rt_mutex_enqueue_pi(owner, waiter);
955 rt_mutex_adjust_prio(owner);
956 if (owner->pi_blocked_on)
958 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
962 /* Store the lock on which owner is blocked or NULL */
963 next_lock = task_blocked_on_lock(owner);
965 raw_spin_unlock(&owner->pi_lock);
967 * Even if full deadlock detection is on, if the owner is not
968 * blocked itself, we can avoid finding this out in the chain
971 if (!chain_walk || !next_lock)
975 * The owner can't disappear while holding a lock,
976 * so the owner struct is protected by wait_lock.
977 * Gets dropped in rt_mutex_adjust_prio_chain()!
979 get_task_struct(owner);
981 raw_spin_unlock_irq(&lock->wait_lock);
983 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
984 next_lock, waiter, task);
986 raw_spin_lock_irq(&lock->wait_lock);
992 * Remove the top waiter from the current tasks pi waiter tree and
995 * Called with lock->wait_lock held and interrupts disabled.
997 static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
998 struct rt_mutex *lock)
1000 struct rt_mutex_waiter *waiter;
1002 raw_spin_lock(¤t->pi_lock);
1004 waiter = rt_mutex_top_waiter(lock);
1007 * Remove it from current->pi_waiters and deboost.
1009 * We must in fact deboost here in order to ensure we call
1010 * rt_mutex_setprio() to update p->pi_top_task before the
1013 rt_mutex_dequeue_pi(current, waiter);
1014 rt_mutex_adjust_prio(current);
1017 * As we are waking up the top waiter, and the waiter stays
1018 * queued on the lock until it gets the lock, this lock
1019 * obviously has waiters. Just set the bit here and this has
1020 * the added benefit of forcing all new tasks into the
1021 * slow path making sure no task of lower priority than
1022 * the top waiter can steal this lock.
1024 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
1027 * We deboosted before waking the top waiter task such that we don't
1028 * run two tasks with the 'same' priority (and ensure the
1029 * p->pi_top_task pointer points to a blocked task). This however can
1030 * lead to priority inversion if we would get preempted after the
1031 * deboost but before waking our donor task, hence the preempt_disable()
1034 * Pairs with preempt_enable() in rt_mutex_postunlock();
1037 wake_q_add(wake_q, waiter->task);
1038 raw_spin_unlock(¤t->pi_lock);
1042 * Remove a waiter from a lock and give up
1044 * Must be called with lock->wait_lock held and interrupts disabled. I must
1045 * have just failed to try_to_take_rt_mutex().
1047 static void remove_waiter(struct rt_mutex *lock,
1048 struct rt_mutex_waiter *waiter)
1050 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
1051 struct task_struct *owner = rt_mutex_owner(lock);
1052 struct rt_mutex *next_lock;
1054 lockdep_assert_held(&lock->wait_lock);
1056 raw_spin_lock(¤t->pi_lock);
1057 rt_mutex_dequeue(lock, waiter);
1058 current->pi_blocked_on = NULL;
1059 raw_spin_unlock(¤t->pi_lock);
1062 * Only update priority if the waiter was the highest priority
1063 * waiter of the lock and there is an owner to update.
1065 if (!owner || !is_top_waiter)
1068 raw_spin_lock(&owner->pi_lock);
1070 rt_mutex_dequeue_pi(owner, waiter);
1072 if (rt_mutex_has_waiters(lock))
1073 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
1075 rt_mutex_adjust_prio(owner);
1077 /* Store the lock on which owner is blocked or NULL */
1078 next_lock = task_blocked_on_lock(owner);
1080 raw_spin_unlock(&owner->pi_lock);
1083 * Don't walk the chain, if the owner task is not blocked
1089 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1090 get_task_struct(owner);
1092 raw_spin_unlock_irq(&lock->wait_lock);
1094 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1095 next_lock, NULL, current);
1097 raw_spin_lock_irq(&lock->wait_lock);
1101 * Recheck the pi chain, in case we got a priority setting
1103 * Called from sched_setscheduler
1105 void rt_mutex_adjust_pi(struct task_struct *task)
1107 struct rt_mutex_waiter *waiter;
1108 struct rt_mutex *next_lock;
1109 unsigned long flags;
1111 raw_spin_lock_irqsave(&task->pi_lock, flags);
1113 waiter = task->pi_blocked_on;
1114 if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
1115 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1118 next_lock = waiter->lock;
1119 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1121 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1122 get_task_struct(task);
1124 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1125 next_lock, NULL, task);
1128 void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1130 debug_rt_mutex_init_waiter(waiter);
1131 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1132 RB_CLEAR_NODE(&waiter->tree_entry);
1133 waiter->task = NULL;
1137 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1138 * @lock: the rt_mutex to take
1139 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1140 * or TASK_UNINTERRUPTIBLE)
1141 * @timeout: the pre-initialized and started timer, or NULL for none
1142 * @waiter: the pre-initialized rt_mutex_waiter
1144 * Must be called with lock->wait_lock held and interrupts disabled
1147 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
1148 struct hrtimer_sleeper *timeout,
1149 struct rt_mutex_waiter *waiter)
1154 /* Try to acquire the lock: */
1155 if (try_to_take_rt_mutex(lock, current, waiter))
1159 * TASK_INTERRUPTIBLE checks for signals and
1160 * timeout. Ignored otherwise.
1162 if (likely(state == TASK_INTERRUPTIBLE)) {
1163 /* Signal pending? */
1164 if (signal_pending(current))
1166 if (timeout && !timeout->task)
1172 raw_spin_unlock_irq(&lock->wait_lock);
1174 debug_rt_mutex_print_deadlock(waiter);
1178 raw_spin_lock_irq(&lock->wait_lock);
1179 set_current_state(state);
1182 __set_current_state(TASK_RUNNING);
1186 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1187 struct rt_mutex_waiter *w)
1190 * If the result is not -EDEADLOCK or the caller requested
1191 * deadlock detection, nothing to do here.
1193 if (res != -EDEADLOCK || detect_deadlock)
1197 * Yell lowdly and stop the task right here.
1199 rt_mutex_print_deadlock(w);
1201 set_current_state(TASK_INTERRUPTIBLE);
1207 * Slow path lock function:
1210 rt_mutex_slowlock(struct rt_mutex *lock, int state,
1211 struct hrtimer_sleeper *timeout,
1212 enum rtmutex_chainwalk chwalk)
1214 struct rt_mutex_waiter waiter;
1215 unsigned long flags;
1218 rt_mutex_init_waiter(&waiter);
1221 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1222 * be called in early boot if the cmpxchg() fast path is disabled
1223 * (debug, no architecture support). In this case we will acquire the
1224 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1225 * enable interrupts in that early boot case. So we need to use the
1226 * irqsave/restore variants.
1228 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1230 /* Try to acquire the lock again: */
1231 if (try_to_take_rt_mutex(lock, current, NULL)) {
1232 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1236 set_current_state(state);
1238 /* Setup the timer, when timeout != NULL */
1239 if (unlikely(timeout))
1240 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1242 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
1245 /* sleep on the mutex */
1246 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1248 if (unlikely(ret)) {
1249 __set_current_state(TASK_RUNNING);
1250 remove_waiter(lock, &waiter);
1251 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
1255 * try_to_take_rt_mutex() sets the waiter bit
1256 * unconditionally. We might have to fix that up.
1258 fixup_rt_mutex_waiters(lock);
1260 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1262 /* Remove pending timer: */
1263 if (unlikely(timeout))
1264 hrtimer_cancel(&timeout->timer);
1266 debug_rt_mutex_free_waiter(&waiter);
1271 static inline int __rt_mutex_slowtrylock(struct rt_mutex *lock)
1273 int ret = try_to_take_rt_mutex(lock, current, NULL);
1276 * try_to_take_rt_mutex() sets the lock waiters bit
1277 * unconditionally. Clean this up.
1279 fixup_rt_mutex_waiters(lock);
1285 * Slow path try-lock function:
1287 static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
1289 unsigned long flags;
1293 * If the lock already has an owner we fail to get the lock.
1294 * This can be done without taking the @lock->wait_lock as
1295 * it is only being read, and this is a trylock anyway.
1297 if (rt_mutex_owner(lock))
1301 * The mutex has currently no owner. Lock the wait lock and try to
1302 * acquire the lock. We use irqsave here to support early boot calls.
1304 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1306 ret = __rt_mutex_slowtrylock(lock);
1308 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1314 * Slow path to release a rt-mutex.
1316 * Return whether the current task needs to call rt_mutex_postunlock().
1318 static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1319 struct wake_q_head *wake_q)
1321 unsigned long flags;
1323 /* irqsave required to support early boot calls */
1324 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1326 debug_rt_mutex_unlock(lock);
1329 * We must be careful here if the fast path is enabled. If we
1330 * have no waiters queued we cannot set owner to NULL here
1333 * foo->lock->owner = NULL;
1334 * rtmutex_lock(foo->lock); <- fast path
1335 * free = atomic_dec_and_test(foo->refcnt);
1336 * rtmutex_unlock(foo->lock); <- fast path
1339 * raw_spin_unlock(foo->lock->wait_lock);
1341 * So for the fastpath enabled kernel:
1343 * Nothing can set the waiters bit as long as we hold
1344 * lock->wait_lock. So we do the following sequence:
1346 * owner = rt_mutex_owner(lock);
1347 * clear_rt_mutex_waiters(lock);
1348 * raw_spin_unlock(&lock->wait_lock);
1349 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1353 * The fastpath disabled variant is simple as all access to
1354 * lock->owner is serialized by lock->wait_lock:
1356 * lock->owner = NULL;
1357 * raw_spin_unlock(&lock->wait_lock);
1359 while (!rt_mutex_has_waiters(lock)) {
1360 /* Drops lock->wait_lock ! */
1361 if (unlock_rt_mutex_safe(lock, flags) == true)
1363 /* Relock the rtmutex and try again */
1364 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1368 * The wakeup next waiter path does not suffer from the above
1369 * race. See the comments there.
1371 * Queue the next waiter for wakeup once we release the wait_lock.
1373 mark_wakeup_next_waiter(wake_q, lock);
1374 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1376 return true; /* call rt_mutex_postunlock() */
1380 * debug aware fast / slowpath lock,trylock,unlock
1382 * The atomic acquire/release ops are compiled away, when either the
1383 * architecture does not support cmpxchg or when debugging is enabled.
1386 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1387 int (*slowfn)(struct rt_mutex *lock, int state,
1388 struct hrtimer_sleeper *timeout,
1389 enum rtmutex_chainwalk chwalk))
1391 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1394 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
1398 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1399 struct hrtimer_sleeper *timeout,
1400 enum rtmutex_chainwalk chwalk,
1401 int (*slowfn)(struct rt_mutex *lock, int state,
1402 struct hrtimer_sleeper *timeout,
1403 enum rtmutex_chainwalk chwalk))
1405 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1406 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1409 return slowfn(lock, state, timeout, chwalk);
1413 rt_mutex_fasttrylock(struct rt_mutex *lock,
1414 int (*slowfn)(struct rt_mutex *lock))
1416 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1419 return slowfn(lock);
1423 * Performs the wakeup of the top-waiter and re-enables preemption.
1425 void rt_mutex_postunlock(struct wake_q_head *wake_q)
1429 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
1434 rt_mutex_fastunlock(struct rt_mutex *lock,
1435 bool (*slowfn)(struct rt_mutex *lock,
1436 struct wake_q_head *wqh))
1438 DEFINE_WAKE_Q(wake_q);
1440 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1443 if (slowfn(lock, &wake_q))
1444 rt_mutex_postunlock(&wake_q);
1447 static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
1451 mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
1452 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1455 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1457 * rt_mutex_lock_nested - lock a rt_mutex
1459 * @lock: the rt_mutex to be locked
1460 * @subclass: the lockdep subclass
1462 void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
1464 __rt_mutex_lock(lock, subclass);
1466 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
1468 #else /* !CONFIG_DEBUG_LOCK_ALLOC */
1471 * rt_mutex_lock - lock a rt_mutex
1473 * @lock: the rt_mutex to be locked
1475 void __sched rt_mutex_lock(struct rt_mutex *lock)
1477 __rt_mutex_lock(lock, 0);
1479 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1483 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1485 * @lock: the rt_mutex to be locked
1489 * -EINTR when interrupted by a signal
1491 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
1497 mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1498 ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1500 mutex_release(&lock->dep_map, _RET_IP_);
1504 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1507 * Futex variant, must not use fastpath.
1509 int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1511 return rt_mutex_slowtrylock(lock);
1514 int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
1516 return __rt_mutex_slowtrylock(lock);
1520 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1521 * the timeout structure is provided
1524 * @lock: the rt_mutex to be locked
1525 * @timeout: timeout structure or NULL (no timeout)
1529 * -EINTR when interrupted by a signal
1530 * -ETIMEDOUT when the timeout expired
1533 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
1539 mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1540 ret = rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1541 RT_MUTEX_MIN_CHAINWALK,
1544 mutex_release(&lock->dep_map, _RET_IP_);
1548 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1551 * rt_mutex_trylock - try to lock a rt_mutex
1553 * @lock: the rt_mutex to be locked
1555 * This function can only be called in thread context. It's safe to
1556 * call it from atomic regions, but not from hard interrupt or soft
1557 * interrupt context.
1559 * Returns 1 on success and 0 on contention
1561 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1565 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
1568 ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1570 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1574 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1577 * rt_mutex_unlock - unlock a rt_mutex
1579 * @lock: the rt_mutex to be unlocked
1581 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1583 mutex_release(&lock->dep_map, _RET_IP_);
1584 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1586 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1589 * __rt_mutex_futex_unlock - Futex variant, that since futex variants
1590 * do not use the fast-path, can be simple and will not need to retry.
1592 * @lock: The rt_mutex to be unlocked
1593 * @wake_q: The wake queue head from which to get the next lock waiter
1595 bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1596 struct wake_q_head *wake_q)
1598 lockdep_assert_held(&lock->wait_lock);
1600 debug_rt_mutex_unlock(lock);
1602 if (!rt_mutex_has_waiters(lock)) {
1604 return false; /* done */
1608 * We've already deboosted, mark_wakeup_next_waiter() will
1609 * retain preempt_disabled when we drop the wait_lock, to
1610 * avoid inversion prior to the wakeup. preempt_disable()
1611 * therein pairs with rt_mutex_postunlock().
1613 mark_wakeup_next_waiter(wake_q, lock);
1615 return true; /* call postunlock() */
1618 void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1620 DEFINE_WAKE_Q(wake_q);
1621 unsigned long flags;
1624 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1625 postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
1626 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1629 rt_mutex_postunlock(&wake_q);
1633 * rt_mutex_destroy - mark a mutex unusable
1634 * @lock: the mutex to be destroyed
1636 * This function marks the mutex uninitialized, and any subsequent
1637 * use of the mutex is forbidden. The mutex must not be locked when
1638 * this function is called.
1640 void rt_mutex_destroy(struct rt_mutex *lock)
1642 WARN_ON(rt_mutex_is_locked(lock));
1643 #ifdef CONFIG_DEBUG_RT_MUTEXES
1647 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1650 * __rt_mutex_init - initialize the rt_mutex
1652 * @lock: The rt_mutex to be initialized
1653 * @name: The lock name used for debugging
1654 * @key: The lock class key used for debugging
1656 * Initialize the rt_mutex to unlocked state.
1658 * Initializing of a locked rt_mutex is not allowed
1660 void __rt_mutex_init(struct rt_mutex *lock, const char *name,
1661 struct lock_class_key *key)
1664 raw_spin_lock_init(&lock->wait_lock);
1665 lock->waiters = RB_ROOT_CACHED;
1668 debug_rt_mutex_init(lock, name, key);
1670 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1673 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1676 * @lock: the rt_mutex to be locked
1677 * @proxy_owner:the task to set as owner
1679 * No locking. Caller has to do serializing itself
1681 * Special API call for PI-futex support. This initializes the rtmutex and
1682 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1683 * possible at this point because the pi_state which contains the rtmutex
1684 * is not yet visible to other tasks.
1686 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1687 struct task_struct *proxy_owner)
1689 __rt_mutex_init(lock, NULL, NULL);
1690 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1691 rt_mutex_set_owner(lock, proxy_owner);
1695 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1697 * @lock: the rt_mutex to be locked
1699 * No locking. Caller has to do serializing itself
1701 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1702 * (debugging) state. Concurrent operations on this rt_mutex are not
1703 * possible because it belongs to the pi_state which is about to be freed
1704 * and it is not longer visible to other tasks.
1706 void rt_mutex_proxy_unlock(struct rt_mutex *lock)
1708 debug_rt_mutex_proxy_unlock(lock);
1709 rt_mutex_set_owner(lock, NULL);
1713 * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1714 * @lock: the rt_mutex to take
1715 * @waiter: the pre-initialized rt_mutex_waiter
1716 * @task: the task to prepare
1718 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1719 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1721 * NOTE: does _NOT_ remove the @waiter on failure; must either call
1722 * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
1725 * 0 - task blocked on lock
1726 * 1 - acquired the lock for task, caller should wake it up
1729 * Special API call for PI-futex support.
1731 int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1732 struct rt_mutex_waiter *waiter,
1733 struct task_struct *task)
1737 lockdep_assert_held(&lock->wait_lock);
1739 if (try_to_take_rt_mutex(lock, task, NULL))
1742 /* We enforce deadlock detection for futexes */
1743 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1744 RT_MUTEX_FULL_CHAINWALK);
1746 if (ret && !rt_mutex_owner(lock)) {
1748 * Reset the return value. We might have
1749 * returned with -EDEADLK and the owner
1750 * released the lock while we were walking the
1751 * pi chain. Let the waiter sort it out.
1756 debug_rt_mutex_print_deadlock(waiter);
1762 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1763 * @lock: the rt_mutex to take
1764 * @waiter: the pre-initialized rt_mutex_waiter
1765 * @task: the task to prepare
1767 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
1768 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
1770 * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
1774 * 0 - task blocked on lock
1775 * 1 - acquired the lock for task, caller should wake it up
1778 * Special API call for PI-futex support.
1780 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1781 struct rt_mutex_waiter *waiter,
1782 struct task_struct *task)
1786 raw_spin_lock_irq(&lock->wait_lock);
1787 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
1789 remove_waiter(lock, waiter);
1790 raw_spin_unlock_irq(&lock->wait_lock);
1796 * rt_mutex_next_owner - return the next owner of the lock
1798 * @lock: the rt lock query
1800 * Returns the next owner of the lock or NULL
1802 * Caller has to serialize against other accessors to the lock
1805 * Special API call for PI-futex support
1807 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1809 if (!rt_mutex_has_waiters(lock))
1812 return rt_mutex_top_waiter(lock)->task;
1816 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
1817 * @lock: the rt_mutex we were woken on
1818 * @to: the timeout, null if none. hrtimer should already have
1820 * @waiter: the pre-initialized rt_mutex_waiter
1822 * Wait for the lock acquisition started on our behalf by
1823 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1824 * rt_mutex_cleanup_proxy_lock().
1828 * <0 - error, one of -EINTR, -ETIMEDOUT
1830 * Special API call for PI-futex support
1832 int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
1833 struct hrtimer_sleeper *to,
1834 struct rt_mutex_waiter *waiter)
1838 raw_spin_lock_irq(&lock->wait_lock);
1839 /* sleep on the mutex */
1840 set_current_state(TASK_INTERRUPTIBLE);
1841 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1843 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1844 * have to fix that up.
1846 fixup_rt_mutex_waiters(lock);
1847 raw_spin_unlock_irq(&lock->wait_lock);
1853 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1854 * @lock: the rt_mutex we were woken on
1855 * @waiter: the pre-initialized rt_mutex_waiter
1857 * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
1858 * rt_mutex_wait_proxy_lock().
1860 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1861 * in fact still be granted ownership until we're removed. Therefore we can
1862 * find we are in fact the owner and must disregard the
1863 * rt_mutex_wait_proxy_lock() failure.
1866 * true - did the cleanup, we done.
1867 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1868 * caller should disregards its return value.
1870 * Special API call for PI-futex support
1872 bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1873 struct rt_mutex_waiter *waiter)
1875 bool cleanup = false;
1877 raw_spin_lock_irq(&lock->wait_lock);
1879 * Do an unconditional try-lock, this deals with the lock stealing
1880 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
1881 * sets a NULL owner.
1883 * We're not interested in the return value, because the subsequent
1884 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
1885 * we will own the lock and it will have removed the waiter. If we
1886 * failed the trylock, we're still not owner and we need to remove
1889 try_to_take_rt_mutex(lock, current, waiter);
1891 * Unless we're the owner; we're still enqueued on the wait_list.
1892 * So check if we became owner, if not, take us off the wait_list.
1894 if (rt_mutex_owner(lock) != current) {
1895 remove_waiter(lock, waiter);
1899 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1900 * have to fix that up.
1902 fixup_rt_mutex_waiters(lock);
1904 raw_spin_unlock_irq(&lock->wait_lock);