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/signal.h>
16 #include <linux/sched/rt.h>
17 #include <linux/sched/deadline.h>
18 #include <linux/sched/wake_q.h>
19 #include <linux/timer.h>
21 #include "rtmutex_common.h"
24 * lock->owner state tracking:
26 * lock->owner holds the task_struct pointer of the owner. Bit 0
27 * is used to keep track of the "lock has waiters" state.
30 * NULL 0 lock is free (fast acquire possible)
31 * NULL 1 lock is free and has waiters and the top waiter
32 * is going to take the lock*
33 * taskpointer 0 lock is held (fast release possible)
34 * taskpointer 1 lock is held and has waiters**
36 * The fast atomic compare exchange based acquire and release is only
37 * possible when bit 0 of lock->owner is 0.
39 * (*) It also can be a transitional state when grabbing the lock
40 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
41 * we need to set the bit0 before looking at the lock, and the owner may be
42 * NULL in this small time, hence this can be a transitional state.
44 * (**) There is a small time when bit 0 is set but there are no
45 * waiters. This can happen when grabbing the lock in the slow path.
46 * To prevent a cmpxchg of the owner releasing the lock, we need to
47 * set this bit before looking at the lock.
51 rt_mutex_set_owner(struct rt_mutex
*lock
, struct task_struct
*owner
)
53 unsigned long val
= (unsigned long)owner
;
55 if (rt_mutex_has_waiters(lock
))
56 val
|= RT_MUTEX_HAS_WAITERS
;
58 lock
->owner
= (struct task_struct
*)val
;
61 static inline void clear_rt_mutex_waiters(struct rt_mutex
*lock
)
63 lock
->owner
= (struct task_struct
*)
64 ((unsigned long)lock
->owner
& ~RT_MUTEX_HAS_WAITERS
);
67 static void fixup_rt_mutex_waiters(struct rt_mutex
*lock
)
69 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
71 if (rt_mutex_has_waiters(lock
))
75 * The rbtree has no waiters enqueued, now make sure that the
76 * lock->owner still has the waiters bit set, otherwise the
77 * following can happen:
83 * l->owner = T1 | HAS_WAITERS;
91 * l->owner = T1 | HAS_WAITERS;
96 * signal(->T2) signal(->T3)
103 * ==> wait list is empty
107 * fixup_rt_mutex_waiters()
108 * if (wait_list_empty(l) {
110 * owner = l->owner & ~HAS_WAITERS;
114 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
115 * if (wait_list_empty(l) {
116 * owner = l->owner & ~HAS_WAITERS;
117 * cmpxchg(l->owner, T1, NULL)
118 * ===> Success (l->owner = NULL)
124 * With the check for the waiter bit in place T3 on CPU2 will not
125 * overwrite. All tasks fiddling with the waiters bit are
126 * serialized by l->lock, so nothing else can modify the waiters
127 * bit. If the bit is set then nothing can change l->owner either
128 * so the simple RMW is safe. The cmpxchg() will simply fail if it
129 * happens in the middle of the RMW because the waiters bit is
132 owner
= READ_ONCE(*p
);
133 if (owner
& RT_MUTEX_HAS_WAITERS
)
134 WRITE_ONCE(*p
, owner
& ~RT_MUTEX_HAS_WAITERS
);
138 * We can speed up the acquire/release, if there's no debugging state to be
141 #ifndef CONFIG_DEBUG_RT_MUTEXES
142 # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
143 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
144 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
147 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
148 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
149 * relaxed semantics suffice.
151 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
153 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
157 } while (cmpxchg_relaxed(p
, owner
,
158 owner
| RT_MUTEX_HAS_WAITERS
) != owner
);
162 * Safe fastpath aware unlock:
163 * 1) Clear the waiters bit
164 * 2) Drop lock->wait_lock
165 * 3) Try to unlock the lock with cmpxchg
167 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
,
169 __releases(lock
->wait_lock
)
171 struct task_struct
*owner
= rt_mutex_owner(lock
);
173 clear_rt_mutex_waiters(lock
);
174 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
176 * If a new waiter comes in between the unlock and the cmpxchg
177 * we have two situations:
181 * cmpxchg(p, owner, 0) == owner
182 * mark_rt_mutex_waiters(lock);
188 * mark_rt_mutex_waiters(lock);
190 * cmpxchg(p, owner, 0) != owner
199 return rt_mutex_cmpxchg_release(lock
, owner
, NULL
);
203 # define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
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 rt_mutex_waiter_less(struct rt_mutex_waiter
*left
,
228 struct rt_mutex_waiter
*right
)
230 if (left
->prio
< right
->prio
)
234 * If both waiters have dl_prio(), we check the deadlines of the
236 * If left waiter has a dl_prio(), and we didn't return 1 above,
237 * then right waiter has a dl_prio() too.
239 if (dl_prio(left
->prio
))
240 return dl_time_before(left
->task
->dl
.deadline
,
241 right
->task
->dl
.deadline
);
247 rt_mutex_enqueue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
249 struct rb_node
**link
= &lock
->waiters
.rb_node
;
250 struct rb_node
*parent
= NULL
;
251 struct rt_mutex_waiter
*entry
;
256 entry
= rb_entry(parent
, struct rt_mutex_waiter
, tree_entry
);
257 if (rt_mutex_waiter_less(waiter
, entry
)) {
258 link
= &parent
->rb_left
;
260 link
= &parent
->rb_right
;
266 lock
->waiters_leftmost
= &waiter
->tree_entry
;
268 rb_link_node(&waiter
->tree_entry
, parent
, link
);
269 rb_insert_color(&waiter
->tree_entry
, &lock
->waiters
);
273 rt_mutex_dequeue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
275 if (RB_EMPTY_NODE(&waiter
->tree_entry
))
278 if (lock
->waiters_leftmost
== &waiter
->tree_entry
)
279 lock
->waiters_leftmost
= rb_next(&waiter
->tree_entry
);
281 rb_erase(&waiter
->tree_entry
, &lock
->waiters
);
282 RB_CLEAR_NODE(&waiter
->tree_entry
);
286 rt_mutex_enqueue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
288 struct rb_node
**link
= &task
->pi_waiters
.rb_node
;
289 struct rb_node
*parent
= NULL
;
290 struct rt_mutex_waiter
*entry
;
295 entry
= rb_entry(parent
, struct rt_mutex_waiter
, pi_tree_entry
);
296 if (rt_mutex_waiter_less(waiter
, entry
)) {
297 link
= &parent
->rb_left
;
299 link
= &parent
->rb_right
;
305 task
->pi_waiters_leftmost
= &waiter
->pi_tree_entry
;
307 rb_link_node(&waiter
->pi_tree_entry
, parent
, link
);
308 rb_insert_color(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
312 rt_mutex_dequeue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
314 if (RB_EMPTY_NODE(&waiter
->pi_tree_entry
))
317 if (task
->pi_waiters_leftmost
== &waiter
->pi_tree_entry
)
318 task
->pi_waiters_leftmost
= rb_next(&waiter
->pi_tree_entry
);
320 rb_erase(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
321 RB_CLEAR_NODE(&waiter
->pi_tree_entry
);
325 * Calculate task priority from the waiter tree priority
327 * Return task->normal_prio when the waiter tree is empty or when
328 * the waiter is not allowed to do priority boosting
330 int rt_mutex_getprio(struct task_struct
*task
)
332 if (likely(!task_has_pi_waiters(task
)))
333 return task
->normal_prio
;
335 return min(task_top_pi_waiter(task
)->prio
,
339 struct task_struct
*rt_mutex_get_top_task(struct task_struct
*task
)
341 if (likely(!task_has_pi_waiters(task
)))
344 return task_top_pi_waiter(task
)->task
;
348 * Called by sched_setscheduler() to get the priority which will be
349 * effective after the change.
351 int rt_mutex_get_effective_prio(struct task_struct
*task
, int newprio
)
353 if (!task_has_pi_waiters(task
))
356 if (task_top_pi_waiter(task
)->task
->prio
<= newprio
)
357 return task_top_pi_waiter(task
)->task
->prio
;
362 * Adjust the priority of a task, after its pi_waiters got modified.
364 * This can be both boosting and unboosting. task->pi_lock must be held.
366 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
368 int prio
= rt_mutex_getprio(task
);
370 if (task
->prio
!= prio
|| dl_prio(prio
))
371 rt_mutex_setprio(task
, prio
);
375 * Adjust task priority (undo boosting). Called from the exit path of
376 * rt_mutex_slowunlock() and rt_mutex_slowlock().
378 * (Note: We do this outside of the protection of lock->wait_lock to
379 * allow the lock to be taken while or before we readjust the priority
380 * of task. We do not use the spin_xx_mutex() variants here as we are
381 * outside of the debug path.)
383 void rt_mutex_adjust_prio(struct task_struct
*task
)
387 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
388 __rt_mutex_adjust_prio(task
);
389 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
393 * Deadlock detection is conditional:
395 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
396 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
398 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
399 * conducted independent of the detect argument.
401 * If the waiter argument is NULL this indicates the deboost path and
402 * deadlock detection is disabled independent of the detect argument
403 * and the config settings.
405 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter
*waiter
,
406 enum rtmutex_chainwalk chwalk
)
409 * This is just a wrapper function for the following call,
410 * because debug_rt_mutex_detect_deadlock() smells like a magic
411 * debug feature and I wanted to keep the cond function in the
412 * main source file along with the comments instead of having
413 * two of the same in the headers.
415 return debug_rt_mutex_detect_deadlock(waiter
, chwalk
);
419 * Max number of times we'll walk the boosting chain:
421 int max_lock_depth
= 1024;
423 static inline struct rt_mutex
*task_blocked_on_lock(struct task_struct
*p
)
425 return p
->pi_blocked_on
? p
->pi_blocked_on
->lock
: NULL
;
429 * Adjust the priority chain. Also used for deadlock detection.
430 * Decreases task's usage by one - may thus free the task.
432 * @task: the task owning the mutex (owner) for which a chain walk is
434 * @chwalk: do we have to carry out deadlock detection?
435 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
436 * things for a task that has just got its priority adjusted, and
437 * is waiting on a mutex)
438 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
439 * we dropped its pi_lock. Is never dereferenced, only used for
440 * comparison to detect lock chain changes.
441 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
442 * its priority to the mutex owner (can be NULL in the case
443 * depicted above or if the top waiter is gone away and we are
444 * actually deboosting the owner)
445 * @top_task: the current top waiter
447 * Returns 0 or -EDEADLK.
449 * Chain walk basics and protection scope
451 * [R] refcount on task
452 * [P] task->pi_lock held
453 * [L] rtmutex->wait_lock held
455 * Step Description Protected by
456 * function arguments:
458 * @orig_lock if != NULL @top_task is blocked on it
459 * @next_lock Unprotected. Cannot be
460 * dereferenced. Only used for
462 * @orig_waiter if != NULL @top_task is blocked on it
463 * @top_task current, or in case of proxy
464 * locking protected by calling
467 * loop_sanity_check();
469 * [1] lock(task->pi_lock); [R] acquire [P]
470 * [2] waiter = task->pi_blocked_on; [P]
471 * [3] check_exit_conditions_1(); [P]
472 * [4] lock = waiter->lock; [P]
473 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
474 * unlock(task->pi_lock); release [P]
477 * [6] check_exit_conditions_2(); [P] + [L]
478 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
479 * [8] unlock(task->pi_lock); release [P]
480 * put_task_struct(task); release [R]
481 * [9] check_exit_conditions_3(); [L]
482 * [10] task = owner(lock); [L]
483 * get_task_struct(task); [L] acquire [R]
484 * lock(task->pi_lock); [L] acquire [P]
485 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
486 * [12] check_exit_conditions_4(); [P] + [L]
487 * [13] unlock(task->pi_lock); release [P]
488 * unlock(lock->wait_lock); release [L]
491 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
492 enum rtmutex_chainwalk chwalk
,
493 struct rt_mutex
*orig_lock
,
494 struct rt_mutex
*next_lock
,
495 struct rt_mutex_waiter
*orig_waiter
,
496 struct task_struct
*top_task
)
498 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
499 struct rt_mutex_waiter
*prerequeue_top_waiter
;
500 int ret
= 0, depth
= 0;
501 struct rt_mutex
*lock
;
502 bool detect_deadlock
;
505 detect_deadlock
= rt_mutex_cond_detect_deadlock(orig_waiter
, chwalk
);
508 * The (de)boosting is a step by step approach with a lot of
509 * pitfalls. We want this to be preemptible and we want hold a
510 * maximum of two locks per step. So we have to check
511 * carefully whether things change under us.
515 * We limit the lock chain length for each invocation.
517 if (++depth
> max_lock_depth
) {
521 * Print this only once. If the admin changes the limit,
522 * print a new message when reaching the limit again.
524 if (prev_max
!= max_lock_depth
) {
525 prev_max
= max_lock_depth
;
526 printk(KERN_WARNING
"Maximum lock depth %d reached "
527 "task: %s (%d)\n", max_lock_depth
,
528 top_task
->comm
, task_pid_nr(top_task
));
530 put_task_struct(task
);
536 * We are fully preemptible here and only hold the refcount on
537 * @task. So everything can have changed under us since the
538 * caller or our own code below (goto retry/again) dropped all
543 * [1] Task cannot go away as we did a get_task() before !
545 raw_spin_lock_irq(&task
->pi_lock
);
548 * [2] Get the waiter on which @task is blocked on.
550 waiter
= task
->pi_blocked_on
;
553 * [3] check_exit_conditions_1() protected by task->pi_lock.
557 * Check whether the end of the boosting chain has been
558 * reached or the state of the chain has changed while we
565 * Check the orig_waiter state. After we dropped the locks,
566 * the previous owner of the lock might have released the lock.
568 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
572 * We dropped all locks after taking a refcount on @task, so
573 * the task might have moved on in the lock chain or even left
574 * the chain completely and blocks now on an unrelated lock or
577 * We stored the lock on which @task was blocked in @next_lock,
578 * so we can detect the chain change.
580 if (next_lock
!= waiter
->lock
)
584 * Drop out, when the task has no waiters. Note,
585 * top_waiter can be NULL, when we are in the deboosting
589 if (!task_has_pi_waiters(task
))
592 * If deadlock detection is off, we stop here if we
593 * are not the top pi waiter of the task. If deadlock
594 * detection is enabled we continue, but stop the
595 * requeueing in the chain walk.
597 if (top_waiter
!= task_top_pi_waiter(task
)) {
598 if (!detect_deadlock
)
606 * If the waiter priority is the same as the task priority
607 * then there is no further priority adjustment necessary. If
608 * deadlock detection is off, we stop the chain walk. If its
609 * enabled we continue, but stop the requeueing in the chain
612 if (waiter
->prio
== task
->prio
) {
613 if (!detect_deadlock
)
620 * [4] Get the next lock
624 * [5] We need to trylock here as we are holding task->pi_lock,
625 * which is the reverse lock order versus the other rtmutex
628 if (!raw_spin_trylock(&lock
->wait_lock
)) {
629 raw_spin_unlock_irq(&task
->pi_lock
);
635 * [6] check_exit_conditions_2() protected by task->pi_lock and
638 * Deadlock detection. If the lock is the same as the original
639 * lock which caused us to walk the lock chain or if the
640 * current lock is owned by the task which initiated the chain
641 * walk, we detected a deadlock.
643 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
644 debug_rt_mutex_deadlock(chwalk
, orig_waiter
, lock
);
645 raw_spin_unlock(&lock
->wait_lock
);
651 * If we just follow the lock chain for deadlock detection, no
652 * need to do all the requeue operations. To avoid a truckload
653 * of conditionals around the various places below, just do the
654 * minimum chain walk checks.
658 * No requeue[7] here. Just release @task [8]
660 raw_spin_unlock(&task
->pi_lock
);
661 put_task_struct(task
);
664 * [9] check_exit_conditions_3 protected by lock->wait_lock.
665 * If there is no owner of the lock, end of chain.
667 if (!rt_mutex_owner(lock
)) {
668 raw_spin_unlock_irq(&lock
->wait_lock
);
672 /* [10] Grab the next task, i.e. owner of @lock */
673 task
= rt_mutex_owner(lock
);
674 get_task_struct(task
);
675 raw_spin_lock(&task
->pi_lock
);
678 * No requeue [11] here. We just do deadlock detection.
680 * [12] Store whether owner is blocked
681 * itself. Decision is made after dropping the locks
683 next_lock
= task_blocked_on_lock(task
);
685 * Get the top waiter for the next iteration
687 top_waiter
= rt_mutex_top_waiter(lock
);
689 /* [13] Drop locks */
690 raw_spin_unlock(&task
->pi_lock
);
691 raw_spin_unlock_irq(&lock
->wait_lock
);
693 /* If owner is not blocked, end of chain. */
700 * Store the current top waiter before doing the requeue
701 * operation on @lock. We need it for the boost/deboost
704 prerequeue_top_waiter
= rt_mutex_top_waiter(lock
);
706 /* [7] Requeue the waiter in the lock waiter tree. */
707 rt_mutex_dequeue(lock
, waiter
);
708 waiter
->prio
= task
->prio
;
709 rt_mutex_enqueue(lock
, waiter
);
711 /* [8] Release the task */
712 raw_spin_unlock(&task
->pi_lock
);
713 put_task_struct(task
);
716 * [9] check_exit_conditions_3 protected by lock->wait_lock.
718 * We must abort the chain walk if there is no lock owner even
719 * in the dead lock detection case, as we have nothing to
720 * follow here. This is the end of the chain we are walking.
722 if (!rt_mutex_owner(lock
)) {
724 * If the requeue [7] above changed the top waiter,
725 * then we need to wake the new top waiter up to try
728 if (prerequeue_top_waiter
!= rt_mutex_top_waiter(lock
))
729 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
730 raw_spin_unlock_irq(&lock
->wait_lock
);
734 /* [10] Grab the next task, i.e. the owner of @lock */
735 task
= rt_mutex_owner(lock
);
736 get_task_struct(task
);
737 raw_spin_lock(&task
->pi_lock
);
739 /* [11] requeue the pi waiters if necessary */
740 if (waiter
== rt_mutex_top_waiter(lock
)) {
742 * The waiter became the new top (highest priority)
743 * waiter on the lock. Replace the previous top waiter
744 * in the owner tasks pi waiters tree with this waiter
745 * and adjust the priority of the owner.
747 rt_mutex_dequeue_pi(task
, prerequeue_top_waiter
);
748 rt_mutex_enqueue_pi(task
, waiter
);
749 __rt_mutex_adjust_prio(task
);
751 } else if (prerequeue_top_waiter
== waiter
) {
753 * The waiter was the top waiter on the lock, but is
754 * no longer the top prority waiter. Replace waiter in
755 * the owner tasks pi waiters tree with the new top
756 * (highest priority) waiter and adjust the priority
758 * The new top waiter is stored in @waiter so that
759 * @waiter == @top_waiter evaluates to true below and
760 * we continue to deboost the rest of the chain.
762 rt_mutex_dequeue_pi(task
, waiter
);
763 waiter
= rt_mutex_top_waiter(lock
);
764 rt_mutex_enqueue_pi(task
, waiter
);
765 __rt_mutex_adjust_prio(task
);
768 * Nothing changed. No need to do any priority
774 * [12] check_exit_conditions_4() protected by task->pi_lock
775 * and lock->wait_lock. The actual decisions are made after we
778 * Check whether the task which owns the current lock is pi
779 * blocked itself. If yes we store a pointer to the lock for
780 * the lock chain change detection above. After we dropped
781 * task->pi_lock next_lock cannot be dereferenced anymore.
783 next_lock
= task_blocked_on_lock(task
);
785 * Store the top waiter of @lock for the end of chain walk
788 top_waiter
= rt_mutex_top_waiter(lock
);
790 /* [13] Drop the locks */
791 raw_spin_unlock(&task
->pi_lock
);
792 raw_spin_unlock_irq(&lock
->wait_lock
);
795 * Make the actual exit decisions [12], based on the stored
798 * We reached the end of the lock chain. Stop right here. No
799 * point to go back just to figure that out.
805 * If the current waiter is not the top waiter on the lock,
806 * then we can stop the chain walk here if we are not in full
807 * deadlock detection mode.
809 if (!detect_deadlock
&& waiter
!= top_waiter
)
815 raw_spin_unlock_irq(&task
->pi_lock
);
817 put_task_struct(task
);
823 * Try to take an rt-mutex
825 * Must be called with lock->wait_lock held and interrupts disabled
827 * @lock: The lock to be acquired.
828 * @task: The task which wants to acquire the lock
829 * @waiter: The waiter that is queued to the lock's wait tree if the
830 * callsite called task_blocked_on_lock(), otherwise NULL
832 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
833 struct rt_mutex_waiter
*waiter
)
836 * Before testing whether we can acquire @lock, we set the
837 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
838 * other tasks which try to modify @lock into the slow path
839 * and they serialize on @lock->wait_lock.
841 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
842 * as explained at the top of this file if and only if:
844 * - There is a lock owner. The caller must fixup the
845 * transient state if it does a trylock or leaves the lock
846 * function due to a signal or timeout.
848 * - @task acquires the lock and there are no other
849 * waiters. This is undone in rt_mutex_set_owner(@task) at
850 * the end of this function.
852 mark_rt_mutex_waiters(lock
);
855 * If @lock has an owner, give up.
857 if (rt_mutex_owner(lock
))
861 * If @waiter != NULL, @task has already enqueued the waiter
862 * into @lock waiter tree. If @waiter == NULL then this is a
867 * If waiter is not the highest priority waiter of
870 if (waiter
!= rt_mutex_top_waiter(lock
))
874 * We can acquire the lock. Remove the waiter from the
877 rt_mutex_dequeue(lock
, waiter
);
881 * If the lock has waiters already we check whether @task is
882 * eligible to take over the lock.
884 * If there are no other waiters, @task can acquire
885 * the lock. @task->pi_blocked_on is NULL, so it does
886 * not need to be dequeued.
888 if (rt_mutex_has_waiters(lock
)) {
890 * If @task->prio is greater than or equal to
891 * the top waiter priority (kernel view),
894 if (task
->prio
>= rt_mutex_top_waiter(lock
)->prio
)
898 * The current top waiter stays enqueued. We
899 * don't have to change anything in the lock
904 * No waiters. Take the lock without the
905 * pi_lock dance.@task->pi_blocked_on is NULL
906 * and we have no waiters to enqueue in @task
914 * Clear @task->pi_blocked_on. Requires protection by
915 * @task->pi_lock. Redundant operation for the @waiter == NULL
916 * case, but conditionals are more expensive than a redundant
919 raw_spin_lock(&task
->pi_lock
);
920 task
->pi_blocked_on
= NULL
;
922 * Finish the lock acquisition. @task is the new owner. If
923 * other waiters exist we have to insert the highest priority
924 * waiter into @task->pi_waiters tree.
926 if (rt_mutex_has_waiters(lock
))
927 rt_mutex_enqueue_pi(task
, rt_mutex_top_waiter(lock
));
928 raw_spin_unlock(&task
->pi_lock
);
931 /* We got the lock. */
932 debug_rt_mutex_lock(lock
);
935 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
936 * are still waiters or clears it.
938 rt_mutex_set_owner(lock
, task
);
940 rt_mutex_deadlock_account_lock(lock
, task
);
946 * Task blocks on lock.
948 * Prepare waiter and propagate pi chain
950 * This must be called with lock->wait_lock held and interrupts disabled
952 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
953 struct rt_mutex_waiter
*waiter
,
954 struct task_struct
*task
,
955 enum rtmutex_chainwalk chwalk
)
957 struct task_struct
*owner
= rt_mutex_owner(lock
);
958 struct rt_mutex_waiter
*top_waiter
= waiter
;
959 struct rt_mutex
*next_lock
;
960 int chain_walk
= 0, res
;
963 * Early deadlock detection. We really don't want the task to
964 * enqueue on itself just to untangle the mess later. It's not
965 * only an optimization. We drop the locks, so another waiter
966 * can come in before the chain walk detects the deadlock. So
967 * the other will detect the deadlock and return -EDEADLOCK,
968 * which is wrong, as the other waiter is not in a deadlock
974 raw_spin_lock(&task
->pi_lock
);
975 __rt_mutex_adjust_prio(task
);
978 waiter
->prio
= task
->prio
;
980 /* Get the top priority waiter on the lock */
981 if (rt_mutex_has_waiters(lock
))
982 top_waiter
= rt_mutex_top_waiter(lock
);
983 rt_mutex_enqueue(lock
, waiter
);
985 task
->pi_blocked_on
= waiter
;
987 raw_spin_unlock(&task
->pi_lock
);
992 raw_spin_lock(&owner
->pi_lock
);
993 if (waiter
== rt_mutex_top_waiter(lock
)) {
994 rt_mutex_dequeue_pi(owner
, top_waiter
);
995 rt_mutex_enqueue_pi(owner
, waiter
);
997 __rt_mutex_adjust_prio(owner
);
998 if (owner
->pi_blocked_on
)
1000 } else if (rt_mutex_cond_detect_deadlock(waiter
, chwalk
)) {
1004 /* Store the lock on which owner is blocked or NULL */
1005 next_lock
= task_blocked_on_lock(owner
);
1007 raw_spin_unlock(&owner
->pi_lock
);
1009 * Even if full deadlock detection is on, if the owner is not
1010 * blocked itself, we can avoid finding this out in the chain
1013 if (!chain_walk
|| !next_lock
)
1017 * The owner can't disappear while holding a lock,
1018 * so the owner struct is protected by wait_lock.
1019 * Gets dropped in rt_mutex_adjust_prio_chain()!
1021 get_task_struct(owner
);
1023 raw_spin_unlock_irq(&lock
->wait_lock
);
1025 res
= rt_mutex_adjust_prio_chain(owner
, chwalk
, lock
,
1026 next_lock
, waiter
, task
);
1028 raw_spin_lock_irq(&lock
->wait_lock
);
1034 * Remove the top waiter from the current tasks pi waiter tree and
1037 * Called with lock->wait_lock held and interrupts disabled.
1039 static void mark_wakeup_next_waiter(struct wake_q_head
*wake_q
,
1040 struct rt_mutex
*lock
)
1042 struct rt_mutex_waiter
*waiter
;
1044 raw_spin_lock(¤t
->pi_lock
);
1046 waiter
= rt_mutex_top_waiter(lock
);
1049 * Remove it from current->pi_waiters. We do not adjust a
1050 * possible priority boost right now. We execute wakeup in the
1051 * boosted mode and go back to normal after releasing
1054 rt_mutex_dequeue_pi(current
, waiter
);
1057 * As we are waking up the top waiter, and the waiter stays
1058 * queued on the lock until it gets the lock, this lock
1059 * obviously has waiters. Just set the bit here and this has
1060 * the added benefit of forcing all new tasks into the
1061 * slow path making sure no task of lower priority than
1062 * the top waiter can steal this lock.
1064 lock
->owner
= (void *) RT_MUTEX_HAS_WAITERS
;
1066 raw_spin_unlock(¤t
->pi_lock
);
1068 wake_q_add(wake_q
, waiter
->task
);
1072 * Remove a waiter from a lock and give up
1074 * Must be called with lock->wait_lock held and interrupts disabled. I must
1075 * have just failed to try_to_take_rt_mutex().
1077 static void remove_waiter(struct rt_mutex
*lock
,
1078 struct rt_mutex_waiter
*waiter
)
1080 bool is_top_waiter
= (waiter
== rt_mutex_top_waiter(lock
));
1081 struct task_struct
*owner
= rt_mutex_owner(lock
);
1082 struct rt_mutex
*next_lock
;
1084 raw_spin_lock(¤t
->pi_lock
);
1085 rt_mutex_dequeue(lock
, waiter
);
1086 current
->pi_blocked_on
= NULL
;
1087 raw_spin_unlock(¤t
->pi_lock
);
1090 * Only update priority if the waiter was the highest priority
1091 * waiter of the lock and there is an owner to update.
1093 if (!owner
|| !is_top_waiter
)
1096 raw_spin_lock(&owner
->pi_lock
);
1098 rt_mutex_dequeue_pi(owner
, waiter
);
1100 if (rt_mutex_has_waiters(lock
))
1101 rt_mutex_enqueue_pi(owner
, rt_mutex_top_waiter(lock
));
1103 __rt_mutex_adjust_prio(owner
);
1105 /* Store the lock on which owner is blocked or NULL */
1106 next_lock
= task_blocked_on_lock(owner
);
1108 raw_spin_unlock(&owner
->pi_lock
);
1111 * Don't walk the chain, if the owner task is not blocked
1117 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1118 get_task_struct(owner
);
1120 raw_spin_unlock_irq(&lock
->wait_lock
);
1122 rt_mutex_adjust_prio_chain(owner
, RT_MUTEX_MIN_CHAINWALK
, lock
,
1123 next_lock
, NULL
, current
);
1125 raw_spin_lock_irq(&lock
->wait_lock
);
1129 * Recheck the pi chain, in case we got a priority setting
1131 * Called from sched_setscheduler
1133 void rt_mutex_adjust_pi(struct task_struct
*task
)
1135 struct rt_mutex_waiter
*waiter
;
1136 struct rt_mutex
*next_lock
;
1137 unsigned long flags
;
1139 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
1141 waiter
= task
->pi_blocked_on
;
1142 if (!waiter
|| (waiter
->prio
== task
->prio
&&
1143 !dl_prio(task
->prio
))) {
1144 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1147 next_lock
= waiter
->lock
;
1148 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1150 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1151 get_task_struct(task
);
1153 rt_mutex_adjust_prio_chain(task
, RT_MUTEX_MIN_CHAINWALK
, NULL
,
1154 next_lock
, NULL
, task
);
1158 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1159 * @lock: the rt_mutex to take
1160 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1161 * or TASK_UNINTERRUPTIBLE)
1162 * @timeout: the pre-initialized and started timer, or NULL for none
1163 * @waiter: the pre-initialized rt_mutex_waiter
1165 * Must be called with lock->wait_lock held and interrupts disabled
1168 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1169 struct hrtimer_sleeper
*timeout
,
1170 struct rt_mutex_waiter
*waiter
)
1175 /* Try to acquire the lock: */
1176 if (try_to_take_rt_mutex(lock
, current
, waiter
))
1180 * TASK_INTERRUPTIBLE checks for signals and
1181 * timeout. Ignored otherwise.
1183 if (likely(state
== TASK_INTERRUPTIBLE
)) {
1184 /* Signal pending? */
1185 if (signal_pending(current
))
1187 if (timeout
&& !timeout
->task
)
1193 raw_spin_unlock_irq(&lock
->wait_lock
);
1195 debug_rt_mutex_print_deadlock(waiter
);
1199 raw_spin_lock_irq(&lock
->wait_lock
);
1200 set_current_state(state
);
1203 __set_current_state(TASK_RUNNING
);
1207 static void rt_mutex_handle_deadlock(int res
, int detect_deadlock
,
1208 struct rt_mutex_waiter
*w
)
1211 * If the result is not -EDEADLOCK or the caller requested
1212 * deadlock detection, nothing to do here.
1214 if (res
!= -EDEADLOCK
|| detect_deadlock
)
1218 * Yell lowdly and stop the task right here.
1220 rt_mutex_print_deadlock(w
);
1222 set_current_state(TASK_INTERRUPTIBLE
);
1228 * Slow path lock function:
1231 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1232 struct hrtimer_sleeper
*timeout
,
1233 enum rtmutex_chainwalk chwalk
)
1235 struct rt_mutex_waiter waiter
;
1236 unsigned long flags
;
1239 debug_rt_mutex_init_waiter(&waiter
);
1240 RB_CLEAR_NODE(&waiter
.pi_tree_entry
);
1241 RB_CLEAR_NODE(&waiter
.tree_entry
);
1244 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1245 * be called in early boot if the cmpxchg() fast path is disabled
1246 * (debug, no architecture support). In this case we will acquire the
1247 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1248 * enable interrupts in that early boot case. So we need to use the
1249 * irqsave/restore variants.
1251 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1253 /* Try to acquire the lock again: */
1254 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
1255 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1259 set_current_state(state
);
1261 /* Setup the timer, when timeout != NULL */
1262 if (unlikely(timeout
))
1263 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
1265 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, chwalk
);
1268 /* sleep on the mutex */
1269 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
1271 if (unlikely(ret
)) {
1272 __set_current_state(TASK_RUNNING
);
1273 if (rt_mutex_has_waiters(lock
))
1274 remove_waiter(lock
, &waiter
);
1275 rt_mutex_handle_deadlock(ret
, chwalk
, &waiter
);
1279 * try_to_take_rt_mutex() sets the waiter bit
1280 * unconditionally. We might have to fix that up.
1282 fixup_rt_mutex_waiters(lock
);
1284 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1286 /* Remove pending timer: */
1287 if (unlikely(timeout
))
1288 hrtimer_cancel(&timeout
->timer
);
1290 debug_rt_mutex_free_waiter(&waiter
);
1296 * Slow path try-lock function:
1298 static inline int rt_mutex_slowtrylock(struct rt_mutex
*lock
)
1300 unsigned long flags
;
1304 * If the lock already has an owner we fail to get the lock.
1305 * This can be done without taking the @lock->wait_lock as
1306 * it is only being read, and this is a trylock anyway.
1308 if (rt_mutex_owner(lock
))
1312 * The mutex has currently no owner. Lock the wait lock and try to
1313 * acquire the lock. We use irqsave here to support early boot calls.
1315 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1317 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
1320 * try_to_take_rt_mutex() sets the lock waiters bit
1321 * unconditionally. Clean this up.
1323 fixup_rt_mutex_waiters(lock
);
1325 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1331 * Slow path to release a rt-mutex.
1332 * Return whether the current task needs to undo a potential priority boosting.
1334 static bool __sched
rt_mutex_slowunlock(struct rt_mutex
*lock
,
1335 struct wake_q_head
*wake_q
)
1337 unsigned long flags
;
1339 /* irqsave required to support early boot calls */
1340 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1342 debug_rt_mutex_unlock(lock
);
1344 rt_mutex_deadlock_account_unlock(current
);
1347 * We must be careful here if the fast path is enabled. If we
1348 * have no waiters queued we cannot set owner to NULL here
1351 * foo->lock->owner = NULL;
1352 * rtmutex_lock(foo->lock); <- fast path
1353 * free = atomic_dec_and_test(foo->refcnt);
1354 * rtmutex_unlock(foo->lock); <- fast path
1357 * raw_spin_unlock(foo->lock->wait_lock);
1359 * So for the fastpath enabled kernel:
1361 * Nothing can set the waiters bit as long as we hold
1362 * lock->wait_lock. So we do the following sequence:
1364 * owner = rt_mutex_owner(lock);
1365 * clear_rt_mutex_waiters(lock);
1366 * raw_spin_unlock(&lock->wait_lock);
1367 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1371 * The fastpath disabled variant is simple as all access to
1372 * lock->owner is serialized by lock->wait_lock:
1374 * lock->owner = NULL;
1375 * raw_spin_unlock(&lock->wait_lock);
1377 while (!rt_mutex_has_waiters(lock
)) {
1378 /* Drops lock->wait_lock ! */
1379 if (unlock_rt_mutex_safe(lock
, flags
) == true)
1381 /* Relock the rtmutex and try again */
1382 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1386 * The wakeup next waiter path does not suffer from the above
1387 * race. See the comments there.
1389 * Queue the next waiter for wakeup once we release the wait_lock.
1391 mark_wakeup_next_waiter(wake_q
, lock
);
1393 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1395 /* check PI boosting */
1400 * debug aware fast / slowpath lock,trylock,unlock
1402 * The atomic acquire/release ops are compiled away, when either the
1403 * architecture does not support cmpxchg or when debugging is enabled.
1406 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
1407 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1408 struct hrtimer_sleeper
*timeout
,
1409 enum rtmutex_chainwalk chwalk
))
1411 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1412 rt_mutex_deadlock_account_lock(lock
, current
);
1415 return slowfn(lock
, state
, NULL
, RT_MUTEX_MIN_CHAINWALK
);
1419 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
1420 struct hrtimer_sleeper
*timeout
,
1421 enum rtmutex_chainwalk chwalk
,
1422 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1423 struct hrtimer_sleeper
*timeout
,
1424 enum rtmutex_chainwalk chwalk
))
1426 if (chwalk
== RT_MUTEX_MIN_CHAINWALK
&&
1427 likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1428 rt_mutex_deadlock_account_lock(lock
, current
);
1431 return slowfn(lock
, state
, timeout
, chwalk
);
1435 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
1436 int (*slowfn
)(struct rt_mutex
*lock
))
1438 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1439 rt_mutex_deadlock_account_lock(lock
, current
);
1442 return slowfn(lock
);
1446 rt_mutex_fastunlock(struct rt_mutex
*lock
,
1447 bool (*slowfn
)(struct rt_mutex
*lock
,
1448 struct wake_q_head
*wqh
))
1450 DEFINE_WAKE_Q(wake_q
);
1452 if (likely(rt_mutex_cmpxchg_release(lock
, current
, NULL
))) {
1453 rt_mutex_deadlock_account_unlock(current
);
1456 bool deboost
= slowfn(lock
, &wake_q
);
1460 /* Undo pi boosting if necessary: */
1462 rt_mutex_adjust_prio(current
);
1467 * rt_mutex_lock - lock a rt_mutex
1469 * @lock: the rt_mutex to be locked
1471 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
1475 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, rt_mutex_slowlock
);
1477 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
1480 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1482 * @lock: the rt_mutex to be locked
1486 * -EINTR when interrupted by a signal
1488 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
)
1492 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
, rt_mutex_slowlock
);
1494 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible
);
1497 * Futex variant with full deadlock detection.
1499 int rt_mutex_timed_futex_lock(struct rt_mutex
*lock
,
1500 struct hrtimer_sleeper
*timeout
)
1504 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1505 RT_MUTEX_FULL_CHAINWALK
,
1510 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1511 * the timeout structure is provided
1514 * @lock: the rt_mutex to be locked
1515 * @timeout: timeout structure or NULL (no timeout)
1519 * -EINTR when interrupted by a signal
1520 * -ETIMEDOUT when the timeout expired
1523 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
)
1527 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1528 RT_MUTEX_MIN_CHAINWALK
,
1531 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
1534 * rt_mutex_trylock - try to lock a rt_mutex
1536 * @lock: the rt_mutex to be locked
1538 * This function can only be called in thread context. It's safe to
1539 * call it from atomic regions, but not from hard interrupt or soft
1540 * interrupt context.
1542 * Returns 1 on success and 0 on contention
1544 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
1546 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
1549 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
1551 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
1554 * rt_mutex_unlock - unlock a rt_mutex
1556 * @lock: the rt_mutex to be unlocked
1558 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
1560 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
1562 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
1565 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1566 * @lock: the rt_mutex to be unlocked
1568 * Returns: true/false indicating whether priority adjustment is
1571 bool __sched
rt_mutex_futex_unlock(struct rt_mutex
*lock
,
1572 struct wake_q_head
*wqh
)
1574 if (likely(rt_mutex_cmpxchg_release(lock
, current
, NULL
))) {
1575 rt_mutex_deadlock_account_unlock(current
);
1578 return rt_mutex_slowunlock(lock
, wqh
);
1582 * rt_mutex_destroy - mark a mutex unusable
1583 * @lock: the mutex to be destroyed
1585 * This function marks the mutex uninitialized, and any subsequent
1586 * use of the mutex is forbidden. The mutex must not be locked when
1587 * this function is called.
1589 void rt_mutex_destroy(struct rt_mutex
*lock
)
1591 WARN_ON(rt_mutex_is_locked(lock
));
1592 #ifdef CONFIG_DEBUG_RT_MUTEXES
1597 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
1600 * __rt_mutex_init - initialize the rt lock
1602 * @lock: the rt lock to be initialized
1604 * Initialize the rt lock to unlocked state.
1606 * Initializing of a locked rt lock is not allowed
1608 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
1611 raw_spin_lock_init(&lock
->wait_lock
);
1612 lock
->waiters
= RB_ROOT
;
1613 lock
->waiters_leftmost
= NULL
;
1615 debug_rt_mutex_init(lock
, name
);
1617 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
1620 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1623 * @lock: the rt_mutex to be locked
1624 * @proxy_owner:the task to set as owner
1626 * No locking. Caller has to do serializing itself
1628 * Special API call for PI-futex support. This initializes the rtmutex and
1629 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1630 * possible at this point because the pi_state which contains the rtmutex
1631 * is not yet visible to other tasks.
1633 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
1634 struct task_struct
*proxy_owner
)
1636 __rt_mutex_init(lock
, NULL
);
1637 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
1638 rt_mutex_set_owner(lock
, proxy_owner
);
1639 rt_mutex_deadlock_account_lock(lock
, proxy_owner
);
1643 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1645 * @lock: the rt_mutex to be locked
1647 * No locking. Caller has to do serializing itself
1649 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1650 * (debugging) state. Concurrent operations on this rt_mutex are not
1651 * possible because it belongs to the pi_state which is about to be freed
1652 * and it is not longer visible to other tasks.
1654 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
1655 struct task_struct
*proxy_owner
)
1657 debug_rt_mutex_proxy_unlock(lock
);
1658 rt_mutex_set_owner(lock
, NULL
);
1659 rt_mutex_deadlock_account_unlock(proxy_owner
);
1663 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1664 * @lock: the rt_mutex to take
1665 * @waiter: the pre-initialized rt_mutex_waiter
1666 * @task: the task to prepare
1669 * 0 - task blocked on lock
1670 * 1 - acquired the lock for task, caller should wake it up
1673 * Special API call for FUTEX_REQUEUE_PI support.
1675 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1676 struct rt_mutex_waiter
*waiter
,
1677 struct task_struct
*task
)
1681 raw_spin_lock_irq(&lock
->wait_lock
);
1683 if (try_to_take_rt_mutex(lock
, task
, NULL
)) {
1684 raw_spin_unlock_irq(&lock
->wait_lock
);
1688 /* We enforce deadlock detection for futexes */
1689 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
,
1690 RT_MUTEX_FULL_CHAINWALK
);
1692 if (ret
&& !rt_mutex_owner(lock
)) {
1694 * Reset the return value. We might have
1695 * returned with -EDEADLK and the owner
1696 * released the lock while we were walking the
1697 * pi chain. Let the waiter sort it out.
1703 remove_waiter(lock
, waiter
);
1705 raw_spin_unlock_irq(&lock
->wait_lock
);
1707 debug_rt_mutex_print_deadlock(waiter
);
1713 * rt_mutex_next_owner - return the next owner of the lock
1715 * @lock: the rt lock query
1717 * Returns the next owner of the lock or NULL
1719 * Caller has to serialize against other accessors to the lock
1722 * Special API call for PI-futex support
1724 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
1726 if (!rt_mutex_has_waiters(lock
))
1729 return rt_mutex_top_waiter(lock
)->task
;
1733 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1734 * @lock: the rt_mutex we were woken on
1735 * @to: the timeout, null if none. hrtimer should already have
1737 * @waiter: the pre-initialized rt_mutex_waiter
1739 * Complete the lock acquisition started our behalf by another thread.
1743 * <0 - error, one of -EINTR, -ETIMEDOUT
1745 * Special API call for PI-futex requeue support
1747 int rt_mutex_finish_proxy_lock(struct rt_mutex
*lock
,
1748 struct hrtimer_sleeper
*to
,
1749 struct rt_mutex_waiter
*waiter
)
1753 raw_spin_lock_irq(&lock
->wait_lock
);
1755 set_current_state(TASK_INTERRUPTIBLE
);
1757 /* sleep on the mutex */
1758 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1761 remove_waiter(lock
, waiter
);
1764 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1765 * have to fix that up.
1767 fixup_rt_mutex_waiters(lock
);
1769 raw_spin_unlock_irq(&lock
->wait_lock
);