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 if (!rt_mutex_has_waiters(lock
))
69 clear_rt_mutex_waiters(lock
);
73 * We can speed up the acquire/release, if there's no debugging state to be
76 #ifndef CONFIG_DEBUG_RT_MUTEXES
77 # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
78 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
79 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
82 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
83 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
84 * relaxed semantics suffice.
86 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
88 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
92 } while (cmpxchg_relaxed(p
, owner
,
93 owner
| RT_MUTEX_HAS_WAITERS
) != owner
);
97 * Safe fastpath aware unlock:
98 * 1) Clear the waiters bit
99 * 2) Drop lock->wait_lock
100 * 3) Try to unlock the lock with cmpxchg
102 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
,
104 __releases(lock
->wait_lock
)
106 struct task_struct
*owner
= rt_mutex_owner(lock
);
108 clear_rt_mutex_waiters(lock
);
109 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
111 * If a new waiter comes in between the unlock and the cmpxchg
112 * we have two situations:
116 * cmpxchg(p, owner, 0) == owner
117 * mark_rt_mutex_waiters(lock);
123 * mark_rt_mutex_waiters(lock);
125 * cmpxchg(p, owner, 0) != owner
134 return rt_mutex_cmpxchg_release(lock
, owner
, NULL
);
138 # define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
139 # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
140 # define rt_mutex_cmpxchg_release(l,c,n) (0)
142 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
144 lock
->owner
= (struct task_struct
*)
145 ((unsigned long)lock
->owner
| RT_MUTEX_HAS_WAITERS
);
149 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
151 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
,
153 __releases(lock
->wait_lock
)
156 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
162 rt_mutex_waiter_less(struct rt_mutex_waiter
*left
,
163 struct rt_mutex_waiter
*right
)
165 if (left
->prio
< right
->prio
)
169 * If both waiters have dl_prio(), we check the deadlines of the
171 * If left waiter has a dl_prio(), and we didn't return 1 above,
172 * then right waiter has a dl_prio() too.
174 if (dl_prio(left
->prio
))
175 return dl_time_before(left
->task
->dl
.deadline
,
176 right
->task
->dl
.deadline
);
182 rt_mutex_enqueue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
184 struct rb_node
**link
= &lock
->waiters
.rb_node
;
185 struct rb_node
*parent
= NULL
;
186 struct rt_mutex_waiter
*entry
;
191 entry
= rb_entry(parent
, struct rt_mutex_waiter
, tree_entry
);
192 if (rt_mutex_waiter_less(waiter
, entry
)) {
193 link
= &parent
->rb_left
;
195 link
= &parent
->rb_right
;
201 lock
->waiters_leftmost
= &waiter
->tree_entry
;
203 rb_link_node(&waiter
->tree_entry
, parent
, link
);
204 rb_insert_color(&waiter
->tree_entry
, &lock
->waiters
);
208 rt_mutex_dequeue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
210 if (RB_EMPTY_NODE(&waiter
->tree_entry
))
213 if (lock
->waiters_leftmost
== &waiter
->tree_entry
)
214 lock
->waiters_leftmost
= rb_next(&waiter
->tree_entry
);
216 rb_erase(&waiter
->tree_entry
, &lock
->waiters
);
217 RB_CLEAR_NODE(&waiter
->tree_entry
);
221 rt_mutex_enqueue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
223 struct rb_node
**link
= &task
->pi_waiters
.rb_node
;
224 struct rb_node
*parent
= NULL
;
225 struct rt_mutex_waiter
*entry
;
230 entry
= rb_entry(parent
, struct rt_mutex_waiter
, pi_tree_entry
);
231 if (rt_mutex_waiter_less(waiter
, entry
)) {
232 link
= &parent
->rb_left
;
234 link
= &parent
->rb_right
;
240 task
->pi_waiters_leftmost
= &waiter
->pi_tree_entry
;
242 rb_link_node(&waiter
->pi_tree_entry
, parent
, link
);
243 rb_insert_color(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
247 rt_mutex_dequeue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
249 if (RB_EMPTY_NODE(&waiter
->pi_tree_entry
))
252 if (task
->pi_waiters_leftmost
== &waiter
->pi_tree_entry
)
253 task
->pi_waiters_leftmost
= rb_next(&waiter
->pi_tree_entry
);
255 rb_erase(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
256 RB_CLEAR_NODE(&waiter
->pi_tree_entry
);
260 * Calculate task priority from the waiter tree priority
262 * Return task->normal_prio when the waiter tree is empty or when
263 * the waiter is not allowed to do priority boosting
265 int rt_mutex_getprio(struct task_struct
*task
)
267 if (likely(!task_has_pi_waiters(task
)))
268 return task
->normal_prio
;
270 return min(task_top_pi_waiter(task
)->prio
,
274 struct task_struct
*rt_mutex_get_top_task(struct task_struct
*task
)
276 if (likely(!task_has_pi_waiters(task
)))
279 return task_top_pi_waiter(task
)->task
;
283 * Called by sched_setscheduler() to get the priority which will be
284 * effective after the change.
286 int rt_mutex_get_effective_prio(struct task_struct
*task
, int newprio
)
288 if (!task_has_pi_waiters(task
))
291 if (task_top_pi_waiter(task
)->task
->prio
<= newprio
)
292 return task_top_pi_waiter(task
)->task
->prio
;
297 * Get the effective policy based on the current prio value.
299 int rt_mutex_get_effective_policy(int policy
, int prio
)
302 return SCHED_DEADLINE
;
304 /* With RT, the default class is SCHED_FIFO. */
306 if (policy
== SCHED_RR
)
311 /* With fair, the default class is SCHED_NORMAL. */
322 * Get the effective rt priority based on the current prio value.
324 int rt_mutex_get_effective_rt_prio(int prio
)
329 return MAX_RT_PRIO
- 1 - prio
;
333 * Adjust the priority of a task, after its pi_waiters got modified.
335 * This can be both boosting and unboosting. task->pi_lock must be held.
337 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
339 int prio
= rt_mutex_getprio(task
);
341 if (task
->prio
!= prio
|| dl_prio(prio
))
342 rt_mutex_setprio(task
, prio
);
346 * Adjust task priority (undo boosting). Called from the exit path of
347 * rt_mutex_slowunlock() and rt_mutex_slowlock().
349 * (Note: We do this outside of the protection of lock->wait_lock to
350 * allow the lock to be taken while or before we readjust the priority
351 * of task. We do not use the spin_xx_mutex() variants here as we are
352 * outside of the debug path.)
354 void rt_mutex_adjust_prio(struct task_struct
*task
)
358 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
359 __rt_mutex_adjust_prio(task
);
360 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
364 * Deadlock detection is conditional:
366 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
367 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
369 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
370 * conducted independent of the detect argument.
372 * If the waiter argument is NULL this indicates the deboost path and
373 * deadlock detection is disabled independent of the detect argument
374 * and the config settings.
376 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter
*waiter
,
377 enum rtmutex_chainwalk chwalk
)
380 * This is just a wrapper function for the following call,
381 * because debug_rt_mutex_detect_deadlock() smells like a magic
382 * debug feature and I wanted to keep the cond function in the
383 * main source file along with the comments instead of having
384 * two of the same in the headers.
386 return debug_rt_mutex_detect_deadlock(waiter
, chwalk
);
390 * Max number of times we'll walk the boosting chain:
392 int max_lock_depth
= 1024;
394 static inline struct rt_mutex
*task_blocked_on_lock(struct task_struct
*p
)
396 return p
->pi_blocked_on
? p
->pi_blocked_on
->lock
: NULL
;
400 * Adjust the priority chain. Also used for deadlock detection.
401 * Decreases task's usage by one - may thus free the task.
403 * @task: the task owning the mutex (owner) for which a chain walk is
405 * @chwalk: do we have to carry out deadlock detection?
406 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
407 * things for a task that has just got its priority adjusted, and
408 * is waiting on a mutex)
409 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
410 * we dropped its pi_lock. Is never dereferenced, only used for
411 * comparison to detect lock chain changes.
412 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
413 * its priority to the mutex owner (can be NULL in the case
414 * depicted above or if the top waiter is gone away and we are
415 * actually deboosting the owner)
416 * @top_task: the current top waiter
418 * Returns 0 or -EDEADLK.
420 * Chain walk basics and protection scope
422 * [R] refcount on task
423 * [P] task->pi_lock held
424 * [L] rtmutex->wait_lock held
426 * Step Description Protected by
427 * function arguments:
429 * @orig_lock if != NULL @top_task is blocked on it
430 * @next_lock Unprotected. Cannot be
431 * dereferenced. Only used for
433 * @orig_waiter if != NULL @top_task is blocked on it
434 * @top_task current, or in case of proxy
435 * locking protected by calling
438 * loop_sanity_check();
440 * [1] lock(task->pi_lock); [R] acquire [P]
441 * [2] waiter = task->pi_blocked_on; [P]
442 * [3] check_exit_conditions_1(); [P]
443 * [4] lock = waiter->lock; [P]
444 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
445 * unlock(task->pi_lock); release [P]
448 * [6] check_exit_conditions_2(); [P] + [L]
449 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
450 * [8] unlock(task->pi_lock); release [P]
451 * put_task_struct(task); release [R]
452 * [9] check_exit_conditions_3(); [L]
453 * [10] task = owner(lock); [L]
454 * get_task_struct(task); [L] acquire [R]
455 * lock(task->pi_lock); [L] acquire [P]
456 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
457 * [12] check_exit_conditions_4(); [P] + [L]
458 * [13] unlock(task->pi_lock); release [P]
459 * unlock(lock->wait_lock); release [L]
462 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
463 enum rtmutex_chainwalk chwalk
,
464 struct rt_mutex
*orig_lock
,
465 struct rt_mutex
*next_lock
,
466 struct rt_mutex_waiter
*orig_waiter
,
467 struct task_struct
*top_task
)
469 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
470 struct rt_mutex_waiter
*prerequeue_top_waiter
;
471 int ret
= 0, depth
= 0;
472 struct rt_mutex
*lock
;
473 bool detect_deadlock
;
476 detect_deadlock
= rt_mutex_cond_detect_deadlock(orig_waiter
, chwalk
);
479 * The (de)boosting is a step by step approach with a lot of
480 * pitfalls. We want this to be preemptible and we want hold a
481 * maximum of two locks per step. So we have to check
482 * carefully whether things change under us.
486 * We limit the lock chain length for each invocation.
488 if (++depth
> max_lock_depth
) {
492 * Print this only once. If the admin changes the limit,
493 * print a new message when reaching the limit again.
495 if (prev_max
!= max_lock_depth
) {
496 prev_max
= max_lock_depth
;
497 printk(KERN_WARNING
"Maximum lock depth %d reached "
498 "task: %s (%d)\n", max_lock_depth
,
499 top_task
->comm
, task_pid_nr(top_task
));
501 put_task_struct(task
);
507 * We are fully preemptible here and only hold the refcount on
508 * @task. So everything can have changed under us since the
509 * caller or our own code below (goto retry/again) dropped all
514 * [1] Task cannot go away as we did a get_task() before !
516 raw_spin_lock_irq(&task
->pi_lock
);
519 * [2] Get the waiter on which @task is blocked on.
521 waiter
= task
->pi_blocked_on
;
524 * [3] check_exit_conditions_1() protected by task->pi_lock.
528 * Check whether the end of the boosting chain has been
529 * reached or the state of the chain has changed while we
536 * Check the orig_waiter state. After we dropped the locks,
537 * the previous owner of the lock might have released the lock.
539 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
543 * We dropped all locks after taking a refcount on @task, so
544 * the task might have moved on in the lock chain or even left
545 * the chain completely and blocks now on an unrelated lock or
548 * We stored the lock on which @task was blocked in @next_lock,
549 * so we can detect the chain change.
551 if (next_lock
!= waiter
->lock
)
555 * Drop out, when the task has no waiters. Note,
556 * top_waiter can be NULL, when we are in the deboosting
560 if (!task_has_pi_waiters(task
))
563 * If deadlock detection is off, we stop here if we
564 * are not the top pi waiter of the task. If deadlock
565 * detection is enabled we continue, but stop the
566 * requeueing in the chain walk.
568 if (top_waiter
!= task_top_pi_waiter(task
)) {
569 if (!detect_deadlock
)
577 * If the waiter priority is the same as the task priority
578 * then there is no further priority adjustment necessary. If
579 * deadlock detection is off, we stop the chain walk. If its
580 * enabled we continue, but stop the requeueing in the chain
583 if (waiter
->prio
== task
->prio
) {
584 if (!detect_deadlock
)
591 * [4] Get the next lock
595 * [5] We need to trylock here as we are holding task->pi_lock,
596 * which is the reverse lock order versus the other rtmutex
599 if (!raw_spin_trylock(&lock
->wait_lock
)) {
600 raw_spin_unlock_irq(&task
->pi_lock
);
606 * [6] check_exit_conditions_2() protected by task->pi_lock and
609 * Deadlock detection. If the lock is the same as the original
610 * lock which caused us to walk the lock chain or if the
611 * current lock is owned by the task which initiated the chain
612 * walk, we detected a deadlock.
614 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
615 debug_rt_mutex_deadlock(chwalk
, orig_waiter
, lock
);
616 raw_spin_unlock(&lock
->wait_lock
);
622 * If we just follow the lock chain for deadlock detection, no
623 * need to do all the requeue operations. To avoid a truckload
624 * of conditionals around the various places below, just do the
625 * minimum chain walk checks.
629 * No requeue[7] here. Just release @task [8]
631 raw_spin_unlock(&task
->pi_lock
);
632 put_task_struct(task
);
635 * [9] check_exit_conditions_3 protected by lock->wait_lock.
636 * If there is no owner of the lock, end of chain.
638 if (!rt_mutex_owner(lock
)) {
639 raw_spin_unlock_irq(&lock
->wait_lock
);
643 /* [10] Grab the next task, i.e. owner of @lock */
644 task
= rt_mutex_owner(lock
);
645 get_task_struct(task
);
646 raw_spin_lock(&task
->pi_lock
);
649 * No requeue [11] here. We just do deadlock detection.
651 * [12] Store whether owner is blocked
652 * itself. Decision is made after dropping the locks
654 next_lock
= task_blocked_on_lock(task
);
656 * Get the top waiter for the next iteration
658 top_waiter
= rt_mutex_top_waiter(lock
);
660 /* [13] Drop locks */
661 raw_spin_unlock(&task
->pi_lock
);
662 raw_spin_unlock_irq(&lock
->wait_lock
);
664 /* If owner is not blocked, end of chain. */
671 * Store the current top waiter before doing the requeue
672 * operation on @lock. We need it for the boost/deboost
675 prerequeue_top_waiter
= rt_mutex_top_waiter(lock
);
677 /* [7] Requeue the waiter in the lock waiter tree. */
678 rt_mutex_dequeue(lock
, waiter
);
679 waiter
->prio
= task
->prio
;
680 rt_mutex_enqueue(lock
, waiter
);
682 /* [8] Release the task */
683 raw_spin_unlock(&task
->pi_lock
);
684 put_task_struct(task
);
687 * [9] check_exit_conditions_3 protected by lock->wait_lock.
689 * We must abort the chain walk if there is no lock owner even
690 * in the dead lock detection case, as we have nothing to
691 * follow here. This is the end of the chain we are walking.
693 if (!rt_mutex_owner(lock
)) {
695 * If the requeue [7] above changed the top waiter,
696 * then we need to wake the new top waiter up to try
699 if (prerequeue_top_waiter
!= rt_mutex_top_waiter(lock
))
700 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
701 raw_spin_unlock_irq(&lock
->wait_lock
);
705 /* [10] Grab the next task, i.e. the owner of @lock */
706 task
= rt_mutex_owner(lock
);
707 get_task_struct(task
);
708 raw_spin_lock(&task
->pi_lock
);
710 /* [11] requeue the pi waiters if necessary */
711 if (waiter
== rt_mutex_top_waiter(lock
)) {
713 * The waiter became the new top (highest priority)
714 * waiter on the lock. Replace the previous top waiter
715 * in the owner tasks pi waiters tree with this waiter
716 * and adjust the priority of the owner.
718 rt_mutex_dequeue_pi(task
, prerequeue_top_waiter
);
719 rt_mutex_enqueue_pi(task
, waiter
);
720 __rt_mutex_adjust_prio(task
);
722 } else if (prerequeue_top_waiter
== waiter
) {
724 * The waiter was the top waiter on the lock, but is
725 * no longer the top prority waiter. Replace waiter in
726 * the owner tasks pi waiters tree with the new top
727 * (highest priority) waiter and adjust the priority
729 * The new top waiter is stored in @waiter so that
730 * @waiter == @top_waiter evaluates to true below and
731 * we continue to deboost the rest of the chain.
733 rt_mutex_dequeue_pi(task
, waiter
);
734 waiter
= rt_mutex_top_waiter(lock
);
735 rt_mutex_enqueue_pi(task
, waiter
);
736 __rt_mutex_adjust_prio(task
);
739 * Nothing changed. No need to do any priority
745 * [12] check_exit_conditions_4() protected by task->pi_lock
746 * and lock->wait_lock. The actual decisions are made after we
749 * Check whether the task which owns the current lock is pi
750 * blocked itself. If yes we store a pointer to the lock for
751 * the lock chain change detection above. After we dropped
752 * task->pi_lock next_lock cannot be dereferenced anymore.
754 next_lock
= task_blocked_on_lock(task
);
756 * Store the top waiter of @lock for the end of chain walk
759 top_waiter
= rt_mutex_top_waiter(lock
);
761 /* [13] Drop the locks */
762 raw_spin_unlock(&task
->pi_lock
);
763 raw_spin_unlock_irq(&lock
->wait_lock
);
766 * Make the actual exit decisions [12], based on the stored
769 * We reached the end of the lock chain. Stop right here. No
770 * point to go back just to figure that out.
776 * If the current waiter is not the top waiter on the lock,
777 * then we can stop the chain walk here if we are not in full
778 * deadlock detection mode.
780 if (!detect_deadlock
&& waiter
!= top_waiter
)
786 raw_spin_unlock_irq(&task
->pi_lock
);
788 put_task_struct(task
);
794 * Try to take an rt-mutex
796 * Must be called with lock->wait_lock held and interrupts disabled
798 * @lock: The lock to be acquired.
799 * @task: The task which wants to acquire the lock
800 * @waiter: The waiter that is queued to the lock's wait tree if the
801 * callsite called task_blocked_on_lock(), otherwise NULL
803 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
804 struct rt_mutex_waiter
*waiter
)
807 * Before testing whether we can acquire @lock, we set the
808 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
809 * other tasks which try to modify @lock into the slow path
810 * and they serialize on @lock->wait_lock.
812 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
813 * as explained at the top of this file if and only if:
815 * - There is a lock owner. The caller must fixup the
816 * transient state if it does a trylock or leaves the lock
817 * function due to a signal or timeout.
819 * - @task acquires the lock and there are no other
820 * waiters. This is undone in rt_mutex_set_owner(@task) at
821 * the end of this function.
823 mark_rt_mutex_waiters(lock
);
826 * If @lock has an owner, give up.
828 if (rt_mutex_owner(lock
))
832 * If @waiter != NULL, @task has already enqueued the waiter
833 * into @lock waiter tree. If @waiter == NULL then this is a
838 * If waiter is not the highest priority waiter of
841 if (waiter
!= rt_mutex_top_waiter(lock
))
845 * We can acquire the lock. Remove the waiter from the
848 rt_mutex_dequeue(lock
, waiter
);
852 * If the lock has waiters already we check whether @task is
853 * eligible to take over the lock.
855 * If there are no other waiters, @task can acquire
856 * the lock. @task->pi_blocked_on is NULL, so it does
857 * not need to be dequeued.
859 if (rt_mutex_has_waiters(lock
)) {
861 * If @task->prio is greater than or equal to
862 * the top waiter priority (kernel view),
865 if (task
->prio
>= rt_mutex_top_waiter(lock
)->prio
)
869 * The current top waiter stays enqueued. We
870 * don't have to change anything in the lock
875 * No waiters. Take the lock without the
876 * pi_lock dance.@task->pi_blocked_on is NULL
877 * and we have no waiters to enqueue in @task
885 * Clear @task->pi_blocked_on. Requires protection by
886 * @task->pi_lock. Redundant operation for the @waiter == NULL
887 * case, but conditionals are more expensive than a redundant
890 raw_spin_lock(&task
->pi_lock
);
891 task
->pi_blocked_on
= NULL
;
893 * Finish the lock acquisition. @task is the new owner. If
894 * other waiters exist we have to insert the highest priority
895 * waiter into @task->pi_waiters tree.
897 if (rt_mutex_has_waiters(lock
))
898 rt_mutex_enqueue_pi(task
, rt_mutex_top_waiter(lock
));
899 raw_spin_unlock(&task
->pi_lock
);
902 /* We got the lock. */
903 debug_rt_mutex_lock(lock
);
906 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
907 * are still waiters or clears it.
909 rt_mutex_set_owner(lock
, task
);
911 rt_mutex_deadlock_account_lock(lock
, task
);
917 * Task blocks on lock.
919 * Prepare waiter and propagate pi chain
921 * This must be called with lock->wait_lock held and interrupts disabled
923 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
924 struct rt_mutex_waiter
*waiter
,
925 struct task_struct
*task
,
926 enum rtmutex_chainwalk chwalk
)
928 struct task_struct
*owner
= rt_mutex_owner(lock
);
929 struct rt_mutex_waiter
*top_waiter
= waiter
;
930 struct rt_mutex
*next_lock
;
931 int chain_walk
= 0, res
;
934 * Early deadlock detection. We really don't want the task to
935 * enqueue on itself just to untangle the mess later. It's not
936 * only an optimization. We drop the locks, so another waiter
937 * can come in before the chain walk detects the deadlock. So
938 * the other will detect the deadlock and return -EDEADLOCK,
939 * which is wrong, as the other waiter is not in a deadlock
945 raw_spin_lock(&task
->pi_lock
);
946 __rt_mutex_adjust_prio(task
);
949 waiter
->prio
= task
->prio
;
951 /* Get the top priority waiter on the lock */
952 if (rt_mutex_has_waiters(lock
))
953 top_waiter
= rt_mutex_top_waiter(lock
);
954 rt_mutex_enqueue(lock
, waiter
);
956 task
->pi_blocked_on
= waiter
;
958 raw_spin_unlock(&task
->pi_lock
);
963 raw_spin_lock(&owner
->pi_lock
);
964 if (waiter
== rt_mutex_top_waiter(lock
)) {
965 rt_mutex_dequeue_pi(owner
, top_waiter
);
966 rt_mutex_enqueue_pi(owner
, waiter
);
968 __rt_mutex_adjust_prio(owner
);
969 if (owner
->pi_blocked_on
)
971 } else if (rt_mutex_cond_detect_deadlock(waiter
, chwalk
)) {
975 /* Store the lock on which owner is blocked or NULL */
976 next_lock
= task_blocked_on_lock(owner
);
978 raw_spin_unlock(&owner
->pi_lock
);
980 * Even if full deadlock detection is on, if the owner is not
981 * blocked itself, we can avoid finding this out in the chain
984 if (!chain_walk
|| !next_lock
)
988 * The owner can't disappear while holding a lock,
989 * so the owner struct is protected by wait_lock.
990 * Gets dropped in rt_mutex_adjust_prio_chain()!
992 get_task_struct(owner
);
994 raw_spin_unlock_irq(&lock
->wait_lock
);
996 res
= rt_mutex_adjust_prio_chain(owner
, chwalk
, lock
,
997 next_lock
, waiter
, task
);
999 raw_spin_lock_irq(&lock
->wait_lock
);
1005 * Remove the top waiter from the current tasks pi waiter tree and
1008 * Called with lock->wait_lock held and interrupts disabled.
1010 static void mark_wakeup_next_waiter(struct wake_q_head
*wake_q
,
1011 struct rt_mutex
*lock
)
1013 struct rt_mutex_waiter
*waiter
;
1015 raw_spin_lock(¤t
->pi_lock
);
1017 waiter
= rt_mutex_top_waiter(lock
);
1020 * Remove it from current->pi_waiters. We do not adjust a
1021 * possible priority boost right now. We execute wakeup in the
1022 * boosted mode and go back to normal after releasing
1025 rt_mutex_dequeue_pi(current
, waiter
);
1028 * As we are waking up the top waiter, and the waiter stays
1029 * queued on the lock until it gets the lock, this lock
1030 * obviously has waiters. Just set the bit here and this has
1031 * the added benefit of forcing all new tasks into the
1032 * slow path making sure no task of lower priority than
1033 * the top waiter can steal this lock.
1035 lock
->owner
= (void *) RT_MUTEX_HAS_WAITERS
;
1037 raw_spin_unlock(¤t
->pi_lock
);
1039 wake_q_add(wake_q
, waiter
->task
);
1043 * Remove a waiter from a lock and give up
1045 * Must be called with lock->wait_lock held and interrupts disabled. I must
1046 * have just failed to try_to_take_rt_mutex().
1048 static void remove_waiter(struct rt_mutex
*lock
,
1049 struct rt_mutex_waiter
*waiter
)
1051 bool is_top_waiter
= (waiter
== rt_mutex_top_waiter(lock
));
1052 struct task_struct
*owner
= rt_mutex_owner(lock
);
1053 struct rt_mutex
*next_lock
;
1055 raw_spin_lock(¤t
->pi_lock
);
1056 rt_mutex_dequeue(lock
, waiter
);
1057 current
->pi_blocked_on
= NULL
;
1058 raw_spin_unlock(¤t
->pi_lock
);
1061 * Only update priority if the waiter was the highest priority
1062 * waiter of the lock and there is an owner to update.
1064 if (!owner
|| !is_top_waiter
)
1067 raw_spin_lock(&owner
->pi_lock
);
1069 rt_mutex_dequeue_pi(owner
, waiter
);
1071 if (rt_mutex_has_waiters(lock
))
1072 rt_mutex_enqueue_pi(owner
, rt_mutex_top_waiter(lock
));
1074 __rt_mutex_adjust_prio(owner
);
1076 /* Store the lock on which owner is blocked or NULL */
1077 next_lock
= task_blocked_on_lock(owner
);
1079 raw_spin_unlock(&owner
->pi_lock
);
1082 * Don't walk the chain, if the owner task is not blocked
1088 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1089 get_task_struct(owner
);
1091 raw_spin_unlock_irq(&lock
->wait_lock
);
1093 rt_mutex_adjust_prio_chain(owner
, RT_MUTEX_MIN_CHAINWALK
, lock
,
1094 next_lock
, NULL
, current
);
1096 raw_spin_lock_irq(&lock
->wait_lock
);
1100 * Recheck the pi chain, in case we got a priority setting
1102 * Called from sched_setscheduler
1104 void rt_mutex_adjust_pi(struct task_struct
*task
)
1106 struct rt_mutex_waiter
*waiter
;
1107 struct rt_mutex
*next_lock
;
1108 unsigned long flags
;
1110 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
1112 waiter
= task
->pi_blocked_on
;
1113 if (!waiter
|| (waiter
->prio
== task
->prio
&&
1114 !dl_prio(task
->prio
))) {
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
);
1129 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1130 * @lock: the rt_mutex to take
1131 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1132 * or TASK_UNINTERRUPTIBLE)
1133 * @timeout: the pre-initialized and started timer, or NULL for none
1134 * @waiter: the pre-initialized rt_mutex_waiter
1136 * Must be called with lock->wait_lock held and interrupts disabled
1139 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1140 struct hrtimer_sleeper
*timeout
,
1141 struct rt_mutex_waiter
*waiter
)
1146 /* Try to acquire the lock: */
1147 if (try_to_take_rt_mutex(lock
, current
, waiter
))
1151 * TASK_INTERRUPTIBLE checks for signals and
1152 * timeout. Ignored otherwise.
1154 if (unlikely(state
== TASK_INTERRUPTIBLE
)) {
1155 /* Signal pending? */
1156 if (signal_pending(current
))
1158 if (timeout
&& !timeout
->task
)
1164 raw_spin_unlock_irq(&lock
->wait_lock
);
1166 debug_rt_mutex_print_deadlock(waiter
);
1170 raw_spin_lock_irq(&lock
->wait_lock
);
1171 set_current_state(state
);
1174 __set_current_state(TASK_RUNNING
);
1178 static void rt_mutex_handle_deadlock(int res
, int detect_deadlock
,
1179 struct rt_mutex_waiter
*w
)
1182 * If the result is not -EDEADLOCK or the caller requested
1183 * deadlock detection, nothing to do here.
1185 if (res
!= -EDEADLOCK
|| detect_deadlock
)
1189 * Yell lowdly and stop the task right here.
1191 rt_mutex_print_deadlock(w
);
1193 set_current_state(TASK_INTERRUPTIBLE
);
1199 * Slow path lock function:
1202 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1203 struct hrtimer_sleeper
*timeout
,
1204 enum rtmutex_chainwalk chwalk
)
1206 struct rt_mutex_waiter waiter
;
1207 unsigned long flags
;
1210 debug_rt_mutex_init_waiter(&waiter
);
1211 RB_CLEAR_NODE(&waiter
.pi_tree_entry
);
1212 RB_CLEAR_NODE(&waiter
.tree_entry
);
1215 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1216 * be called in early boot if the cmpxchg() fast path is disabled
1217 * (debug, no architecture support). In this case we will acquire the
1218 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1219 * enable interrupts in that early boot case. So we need to use the
1220 * irqsave/restore variants.
1222 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1224 /* Try to acquire the lock again: */
1225 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
1226 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1230 set_current_state(state
);
1232 /* Setup the timer, when timeout != NULL */
1233 if (unlikely(timeout
))
1234 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
1236 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, chwalk
);
1239 /* sleep on the mutex */
1240 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
1242 if (unlikely(ret
)) {
1243 __set_current_state(TASK_RUNNING
);
1244 if (rt_mutex_has_waiters(lock
))
1245 remove_waiter(lock
, &waiter
);
1246 rt_mutex_handle_deadlock(ret
, chwalk
, &waiter
);
1250 * try_to_take_rt_mutex() sets the waiter bit
1251 * unconditionally. We might have to fix that up.
1253 fixup_rt_mutex_waiters(lock
);
1255 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1257 /* Remove pending timer: */
1258 if (unlikely(timeout
))
1259 hrtimer_cancel(&timeout
->timer
);
1261 debug_rt_mutex_free_waiter(&waiter
);
1267 * Slow path try-lock function:
1269 static inline int rt_mutex_slowtrylock(struct rt_mutex
*lock
)
1271 unsigned long flags
;
1275 * If the lock already has an owner we fail to get the lock.
1276 * This can be done without taking the @lock->wait_lock as
1277 * it is only being read, and this is a trylock anyway.
1279 if (rt_mutex_owner(lock
))
1283 * The mutex has currently no owner. Lock the wait lock and try to
1284 * acquire the lock. We use irqsave here to support early boot calls.
1286 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1288 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
1291 * try_to_take_rt_mutex() sets the lock waiters bit
1292 * unconditionally. Clean this up.
1294 fixup_rt_mutex_waiters(lock
);
1296 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1302 * Slow path to release a rt-mutex.
1303 * Return whether the current task needs to undo a potential priority boosting.
1305 static bool __sched
rt_mutex_slowunlock(struct rt_mutex
*lock
,
1306 struct wake_q_head
*wake_q
)
1308 unsigned long flags
;
1310 /* irqsave required to support early boot calls */
1311 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1313 debug_rt_mutex_unlock(lock
);
1315 rt_mutex_deadlock_account_unlock(current
);
1318 * We must be careful here if the fast path is enabled. If we
1319 * have no waiters queued we cannot set owner to NULL here
1322 * foo->lock->owner = NULL;
1323 * rtmutex_lock(foo->lock); <- fast path
1324 * free = atomic_dec_and_test(foo->refcnt);
1325 * rtmutex_unlock(foo->lock); <- fast path
1328 * raw_spin_unlock(foo->lock->wait_lock);
1330 * So for the fastpath enabled kernel:
1332 * Nothing can set the waiters bit as long as we hold
1333 * lock->wait_lock. So we do the following sequence:
1335 * owner = rt_mutex_owner(lock);
1336 * clear_rt_mutex_waiters(lock);
1337 * raw_spin_unlock(&lock->wait_lock);
1338 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1342 * The fastpath disabled variant is simple as all access to
1343 * lock->owner is serialized by lock->wait_lock:
1345 * lock->owner = NULL;
1346 * raw_spin_unlock(&lock->wait_lock);
1348 while (!rt_mutex_has_waiters(lock
)) {
1349 /* Drops lock->wait_lock ! */
1350 if (unlock_rt_mutex_safe(lock
, flags
) == true)
1352 /* Relock the rtmutex and try again */
1353 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1357 * The wakeup next waiter path does not suffer from the above
1358 * race. See the comments there.
1360 * Queue the next waiter for wakeup once we release the wait_lock.
1362 mark_wakeup_next_waiter(wake_q
, lock
);
1364 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1366 /* check PI boosting */
1371 * debug aware fast / slowpath lock,trylock,unlock
1373 * The atomic acquire/release ops are compiled away, when either the
1374 * architecture does not support cmpxchg or when debugging is enabled.
1377 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
1378 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1379 struct hrtimer_sleeper
*timeout
,
1380 enum rtmutex_chainwalk chwalk
))
1382 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1383 rt_mutex_deadlock_account_lock(lock
, current
);
1386 return slowfn(lock
, state
, NULL
, RT_MUTEX_MIN_CHAINWALK
);
1390 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
1391 struct hrtimer_sleeper
*timeout
,
1392 enum rtmutex_chainwalk chwalk
,
1393 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1394 struct hrtimer_sleeper
*timeout
,
1395 enum rtmutex_chainwalk chwalk
))
1397 if (chwalk
== RT_MUTEX_MIN_CHAINWALK
&&
1398 likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1399 rt_mutex_deadlock_account_lock(lock
, current
);
1402 return slowfn(lock
, state
, timeout
, chwalk
);
1406 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
1407 int (*slowfn
)(struct rt_mutex
*lock
))
1409 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1410 rt_mutex_deadlock_account_lock(lock
, current
);
1413 return slowfn(lock
);
1417 rt_mutex_fastunlock(struct rt_mutex
*lock
,
1418 bool (*slowfn
)(struct rt_mutex
*lock
,
1419 struct wake_q_head
*wqh
))
1423 if (likely(rt_mutex_cmpxchg_release(lock
, current
, NULL
))) {
1424 rt_mutex_deadlock_account_unlock(current
);
1427 bool deboost
= slowfn(lock
, &wake_q
);
1431 /* Undo pi boosting if necessary: */
1433 rt_mutex_adjust_prio(current
);
1438 * rt_mutex_lock - lock a rt_mutex
1440 * @lock: the rt_mutex to be locked
1442 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
1446 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, rt_mutex_slowlock
);
1448 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
1451 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1453 * @lock: the rt_mutex to be locked
1457 * -EINTR when interrupted by a signal
1459 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
)
1463 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
, rt_mutex_slowlock
);
1465 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible
);
1468 * Futex variant with full deadlock detection.
1470 int rt_mutex_timed_futex_lock(struct rt_mutex
*lock
,
1471 struct hrtimer_sleeper
*timeout
)
1475 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1476 RT_MUTEX_FULL_CHAINWALK
,
1481 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1482 * the timeout structure is provided
1485 * @lock: the rt_mutex to be locked
1486 * @timeout: timeout structure or NULL (no timeout)
1490 * -EINTR when interrupted by a signal
1491 * -ETIMEDOUT when the timeout expired
1494 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
)
1498 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1499 RT_MUTEX_MIN_CHAINWALK
,
1502 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
1505 * rt_mutex_trylock - try to lock a rt_mutex
1507 * @lock: the rt_mutex to be locked
1509 * This function can only be called in thread context. It's safe to
1510 * call it from atomic regions, but not from hard interrupt or soft
1511 * interrupt context.
1513 * Returns 1 on success and 0 on contention
1515 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
1517 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
1520 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
1522 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
1525 * rt_mutex_unlock - unlock a rt_mutex
1527 * @lock: the rt_mutex to be unlocked
1529 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
1531 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
1533 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
1536 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1537 * @lock: the rt_mutex to be unlocked
1539 * Returns: true/false indicating whether priority adjustment is
1542 bool __sched
rt_mutex_futex_unlock(struct rt_mutex
*lock
,
1543 struct wake_q_head
*wqh
)
1545 if (likely(rt_mutex_cmpxchg_release(lock
, current
, NULL
))) {
1546 rt_mutex_deadlock_account_unlock(current
);
1549 return rt_mutex_slowunlock(lock
, wqh
);
1553 * rt_mutex_destroy - mark a mutex unusable
1554 * @lock: the mutex to be destroyed
1556 * This function marks the mutex uninitialized, and any subsequent
1557 * use of the mutex is forbidden. The mutex must not be locked when
1558 * this function is called.
1560 void rt_mutex_destroy(struct rt_mutex
*lock
)
1562 WARN_ON(rt_mutex_is_locked(lock
));
1563 #ifdef CONFIG_DEBUG_RT_MUTEXES
1568 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
1571 * __rt_mutex_init - initialize the rt lock
1573 * @lock: the rt lock to be initialized
1575 * Initialize the rt lock to unlocked state.
1577 * Initializing of a locked rt lock is not allowed
1579 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
1582 raw_spin_lock_init(&lock
->wait_lock
);
1583 lock
->waiters
= RB_ROOT
;
1584 lock
->waiters_leftmost
= NULL
;
1586 debug_rt_mutex_init(lock
, name
);
1588 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
1591 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1594 * @lock: the rt_mutex to be locked
1595 * @proxy_owner:the task to set as owner
1597 * No locking. Caller has to do serializing itself
1598 * Special API call for PI-futex support
1600 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
1601 struct task_struct
*proxy_owner
)
1603 __rt_mutex_init(lock
, NULL
);
1604 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
1605 rt_mutex_set_owner(lock
, proxy_owner
);
1606 rt_mutex_deadlock_account_lock(lock
, proxy_owner
);
1610 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1612 * @lock: the rt_mutex to be locked
1614 * No locking. Caller has to do serializing itself
1615 * Special API call for PI-futex support
1617 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
1618 struct task_struct
*proxy_owner
)
1620 debug_rt_mutex_proxy_unlock(lock
);
1621 rt_mutex_set_owner(lock
, NULL
);
1622 rt_mutex_deadlock_account_unlock(proxy_owner
);
1626 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1627 * @lock: the rt_mutex to take
1628 * @waiter: the pre-initialized rt_mutex_waiter
1629 * @task: the task to prepare
1632 * 0 - task blocked on lock
1633 * 1 - acquired the lock for task, caller should wake it up
1636 * Special API call for FUTEX_REQUEUE_PI support.
1638 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1639 struct rt_mutex_waiter
*waiter
,
1640 struct task_struct
*task
)
1644 raw_spin_lock_irq(&lock
->wait_lock
);
1646 if (try_to_take_rt_mutex(lock
, task
, NULL
)) {
1647 raw_spin_unlock_irq(&lock
->wait_lock
);
1651 /* We enforce deadlock detection for futexes */
1652 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
,
1653 RT_MUTEX_FULL_CHAINWALK
);
1655 if (ret
&& !rt_mutex_owner(lock
)) {
1657 * Reset the return value. We might have
1658 * returned with -EDEADLK and the owner
1659 * released the lock while we were walking the
1660 * pi chain. Let the waiter sort it out.
1666 remove_waiter(lock
, waiter
);
1668 raw_spin_unlock_irq(&lock
->wait_lock
);
1670 debug_rt_mutex_print_deadlock(waiter
);
1676 * rt_mutex_next_owner - return the next owner of the lock
1678 * @lock: the rt lock query
1680 * Returns the next owner of the lock or NULL
1682 * Caller has to serialize against other accessors to the lock
1685 * Special API call for PI-futex support
1687 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
1689 if (!rt_mutex_has_waiters(lock
))
1692 return rt_mutex_top_waiter(lock
)->task
;
1696 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1697 * @lock: the rt_mutex we were woken on
1698 * @to: the timeout, null if none. hrtimer should already have
1700 * @waiter: the pre-initialized rt_mutex_waiter
1702 * Complete the lock acquisition started our behalf by another thread.
1706 * <0 - error, one of -EINTR, -ETIMEDOUT
1708 * Special API call for PI-futex requeue support
1710 int rt_mutex_finish_proxy_lock(struct rt_mutex
*lock
,
1711 struct hrtimer_sleeper
*to
,
1712 struct rt_mutex_waiter
*waiter
)
1716 raw_spin_lock_irq(&lock
->wait_lock
);
1718 set_current_state(TASK_INTERRUPTIBLE
);
1720 /* sleep on the mutex */
1721 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1724 remove_waiter(lock
, waiter
);
1727 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1728 * have to fix that up.
1730 fixup_rt_mutex_waiters(lock
);
1732 raw_spin_unlock_irq(&lock
->wait_lock
);