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/sched/debug.h>
20 #include <linux/timer.h>
22 #include "rtmutex_common.h"
25 * lock->owner state tracking:
27 * lock->owner holds the task_struct pointer of the owner. Bit 0
28 * is used to keep track of the "lock has waiters" state.
31 * NULL 0 lock is free (fast acquire possible)
32 * NULL 1 lock is free and has waiters and the top waiter
33 * is going to take the lock*
34 * taskpointer 0 lock is held (fast release possible)
35 * taskpointer 1 lock is held and has waiters**
37 * The fast atomic compare exchange based acquire and release is only
38 * possible when bit 0 of lock->owner is 0.
40 * (*) It also can be a transitional state when grabbing the lock
41 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
42 * we need to set the bit0 before looking at the lock, and the owner may be
43 * NULL in this small time, hence this can be a transitional state.
45 * (**) There is a small time when bit 0 is set but there are no
46 * waiters. This can happen when grabbing the lock in the slow path.
47 * To prevent a cmpxchg of the owner releasing the lock, we need to
48 * set this bit before looking at the lock.
52 rt_mutex_set_owner(struct rt_mutex
*lock
, struct task_struct
*owner
)
54 unsigned long val
= (unsigned long)owner
;
56 if (rt_mutex_has_waiters(lock
))
57 val
|= RT_MUTEX_HAS_WAITERS
;
59 lock
->owner
= (struct task_struct
*)val
;
62 static inline void clear_rt_mutex_waiters(struct rt_mutex
*lock
)
64 lock
->owner
= (struct task_struct
*)
65 ((unsigned long)lock
->owner
& ~RT_MUTEX_HAS_WAITERS
);
68 static void fixup_rt_mutex_waiters(struct rt_mutex
*lock
)
70 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
72 if (rt_mutex_has_waiters(lock
))
76 * The rbtree has no waiters enqueued, now make sure that the
77 * lock->owner still has the waiters bit set, otherwise the
78 * following can happen:
84 * l->owner = T1 | HAS_WAITERS;
92 * l->owner = T1 | HAS_WAITERS;
97 * signal(->T2) signal(->T3)
104 * ==> wait list is empty
108 * fixup_rt_mutex_waiters()
109 * if (wait_list_empty(l) {
111 * owner = l->owner & ~HAS_WAITERS;
115 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
116 * if (wait_list_empty(l) {
117 * owner = l->owner & ~HAS_WAITERS;
118 * cmpxchg(l->owner, T1, NULL)
119 * ===> Success (l->owner = NULL)
125 * With the check for the waiter bit in place T3 on CPU2 will not
126 * overwrite. All tasks fiddling with the waiters bit are
127 * serialized by l->lock, so nothing else can modify the waiters
128 * bit. If the bit is set then nothing can change l->owner either
129 * so the simple RMW is safe. The cmpxchg() will simply fail if it
130 * happens in the middle of the RMW because the waiters bit is
133 owner
= READ_ONCE(*p
);
134 if (owner
& RT_MUTEX_HAS_WAITERS
)
135 WRITE_ONCE(*p
, owner
& ~RT_MUTEX_HAS_WAITERS
);
139 * We can speed up the acquire/release, if there's no debugging state to be
142 #ifndef CONFIG_DEBUG_RT_MUTEXES
143 # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
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_relaxed(l,c,n) (0)
205 # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
206 # define rt_mutex_cmpxchg_release(l,c,n) (0)
208 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
210 lock
->owner
= (struct task_struct
*)
211 ((unsigned long)lock
->owner
| RT_MUTEX_HAS_WAITERS
);
215 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
217 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
,
219 __releases(lock
->wait_lock
)
222 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
228 rt_mutex_waiter_less(struct rt_mutex_waiter
*left
,
229 struct rt_mutex_waiter
*right
)
231 if (left
->prio
< right
->prio
)
235 * If both waiters have dl_prio(), we check the deadlines of the
237 * If left waiter has a dl_prio(), and we didn't return 1 above,
238 * then right waiter has a dl_prio() too.
240 if (dl_prio(left
->prio
))
241 return dl_time_before(left
->task
->dl
.deadline
,
242 right
->task
->dl
.deadline
);
248 rt_mutex_enqueue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
250 struct rb_node
**link
= &lock
->waiters
.rb_node
;
251 struct rb_node
*parent
= NULL
;
252 struct rt_mutex_waiter
*entry
;
257 entry
= rb_entry(parent
, struct rt_mutex_waiter
, tree_entry
);
258 if (rt_mutex_waiter_less(waiter
, entry
)) {
259 link
= &parent
->rb_left
;
261 link
= &parent
->rb_right
;
267 lock
->waiters_leftmost
= &waiter
->tree_entry
;
269 rb_link_node(&waiter
->tree_entry
, parent
, link
);
270 rb_insert_color(&waiter
->tree_entry
, &lock
->waiters
);
274 rt_mutex_dequeue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
276 if (RB_EMPTY_NODE(&waiter
->tree_entry
))
279 if (lock
->waiters_leftmost
== &waiter
->tree_entry
)
280 lock
->waiters_leftmost
= rb_next(&waiter
->tree_entry
);
282 rb_erase(&waiter
->tree_entry
, &lock
->waiters
);
283 RB_CLEAR_NODE(&waiter
->tree_entry
);
287 rt_mutex_enqueue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
289 struct rb_node
**link
= &task
->pi_waiters
.rb_node
;
290 struct rb_node
*parent
= NULL
;
291 struct rt_mutex_waiter
*entry
;
296 entry
= rb_entry(parent
, struct rt_mutex_waiter
, pi_tree_entry
);
297 if (rt_mutex_waiter_less(waiter
, entry
)) {
298 link
= &parent
->rb_left
;
300 link
= &parent
->rb_right
;
306 task
->pi_waiters_leftmost
= &waiter
->pi_tree_entry
;
308 rb_link_node(&waiter
->pi_tree_entry
, parent
, link
);
309 rb_insert_color(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
313 rt_mutex_dequeue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
315 if (RB_EMPTY_NODE(&waiter
->pi_tree_entry
))
318 if (task
->pi_waiters_leftmost
== &waiter
->pi_tree_entry
)
319 task
->pi_waiters_leftmost
= rb_next(&waiter
->pi_tree_entry
);
321 rb_erase(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
322 RB_CLEAR_NODE(&waiter
->pi_tree_entry
);
326 * Must hold both p->pi_lock and task_rq(p)->lock.
328 void rt_mutex_update_top_task(struct task_struct
*p
)
330 if (!task_has_pi_waiters(p
)) {
331 p
->pi_top_task
= NULL
;
335 p
->pi_top_task
= task_top_pi_waiter(p
)->task
;
339 * Calculate task priority from the waiter tree priority
341 * Return task->normal_prio when the waiter tree is empty or when
342 * the waiter is not allowed to do priority boosting
344 int rt_mutex_getprio(struct task_struct
*task
)
346 if (likely(!task_has_pi_waiters(task
)))
347 return task
->normal_prio
;
349 return min(task_top_pi_waiter(task
)->prio
,
354 * Must hold either p->pi_lock or task_rq(p)->lock.
356 struct task_struct
*rt_mutex_get_top_task(struct task_struct
*task
)
358 return task
->pi_top_task
;
362 * Called by sched_setscheduler() to get the priority which will be
363 * effective after the change.
365 int rt_mutex_get_effective_prio(struct task_struct
*task
, int newprio
)
367 struct task_struct
*top_task
= rt_mutex_get_top_task(task
);
372 return min(top_task
->prio
, newprio
);
376 * Adjust the priority of a task, after its pi_waiters got modified.
378 * This can be both boosting and unboosting. task->pi_lock must be held.
380 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
382 int prio
= rt_mutex_getprio(task
);
384 if (task
->prio
!= prio
|| dl_prio(prio
))
385 rt_mutex_setprio(task
, prio
);
389 * Deadlock detection is conditional:
391 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
392 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
394 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
395 * conducted independent of the detect argument.
397 * If the waiter argument is NULL this indicates the deboost path and
398 * deadlock detection is disabled independent of the detect argument
399 * and the config settings.
401 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter
*waiter
,
402 enum rtmutex_chainwalk chwalk
)
405 * This is just a wrapper function for the following call,
406 * because debug_rt_mutex_detect_deadlock() smells like a magic
407 * debug feature and I wanted to keep the cond function in the
408 * main source file along with the comments instead of having
409 * two of the same in the headers.
411 return debug_rt_mutex_detect_deadlock(waiter
, chwalk
);
415 * Max number of times we'll walk the boosting chain:
417 int max_lock_depth
= 1024;
419 static inline struct rt_mutex
*task_blocked_on_lock(struct task_struct
*p
)
421 return p
->pi_blocked_on
? p
->pi_blocked_on
->lock
: NULL
;
425 * Adjust the priority chain. Also used for deadlock detection.
426 * Decreases task's usage by one - may thus free the task.
428 * @task: the task owning the mutex (owner) for which a chain walk is
430 * @chwalk: do we have to carry out deadlock detection?
431 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
432 * things for a task that has just got its priority adjusted, and
433 * is waiting on a mutex)
434 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
435 * we dropped its pi_lock. Is never dereferenced, only used for
436 * comparison to detect lock chain changes.
437 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
438 * its priority to the mutex owner (can be NULL in the case
439 * depicted above or if the top waiter is gone away and we are
440 * actually deboosting the owner)
441 * @top_task: the current top waiter
443 * Returns 0 or -EDEADLK.
445 * Chain walk basics and protection scope
447 * [R] refcount on task
448 * [P] task->pi_lock held
449 * [L] rtmutex->wait_lock held
451 * Step Description Protected by
452 * function arguments:
454 * @orig_lock if != NULL @top_task is blocked on it
455 * @next_lock Unprotected. Cannot be
456 * dereferenced. Only used for
458 * @orig_waiter if != NULL @top_task is blocked on it
459 * @top_task current, or in case of proxy
460 * locking protected by calling
463 * loop_sanity_check();
465 * [1] lock(task->pi_lock); [R] acquire [P]
466 * [2] waiter = task->pi_blocked_on; [P]
467 * [3] check_exit_conditions_1(); [P]
468 * [4] lock = waiter->lock; [P]
469 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
470 * unlock(task->pi_lock); release [P]
473 * [6] check_exit_conditions_2(); [P] + [L]
474 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
475 * [8] unlock(task->pi_lock); release [P]
476 * put_task_struct(task); release [R]
477 * [9] check_exit_conditions_3(); [L]
478 * [10] task = owner(lock); [L]
479 * get_task_struct(task); [L] acquire [R]
480 * lock(task->pi_lock); [L] acquire [P]
481 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
482 * [12] check_exit_conditions_4(); [P] + [L]
483 * [13] unlock(task->pi_lock); release [P]
484 * unlock(lock->wait_lock); release [L]
487 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
488 enum rtmutex_chainwalk chwalk
,
489 struct rt_mutex
*orig_lock
,
490 struct rt_mutex
*next_lock
,
491 struct rt_mutex_waiter
*orig_waiter
,
492 struct task_struct
*top_task
)
494 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
495 struct rt_mutex_waiter
*prerequeue_top_waiter
;
496 int ret
= 0, depth
= 0;
497 struct rt_mutex
*lock
;
498 bool detect_deadlock
;
501 detect_deadlock
= rt_mutex_cond_detect_deadlock(orig_waiter
, chwalk
);
504 * The (de)boosting is a step by step approach with a lot of
505 * pitfalls. We want this to be preemptible and we want hold a
506 * maximum of two locks per step. So we have to check
507 * carefully whether things change under us.
511 * We limit the lock chain length for each invocation.
513 if (++depth
> max_lock_depth
) {
517 * Print this only once. If the admin changes the limit,
518 * print a new message when reaching the limit again.
520 if (prev_max
!= max_lock_depth
) {
521 prev_max
= max_lock_depth
;
522 printk(KERN_WARNING
"Maximum lock depth %d reached "
523 "task: %s (%d)\n", max_lock_depth
,
524 top_task
->comm
, task_pid_nr(top_task
));
526 put_task_struct(task
);
532 * We are fully preemptible here and only hold the refcount on
533 * @task. So everything can have changed under us since the
534 * caller or our own code below (goto retry/again) dropped all
539 * [1] Task cannot go away as we did a get_task() before !
541 raw_spin_lock_irq(&task
->pi_lock
);
544 * [2] Get the waiter on which @task is blocked on.
546 waiter
= task
->pi_blocked_on
;
549 * [3] check_exit_conditions_1() protected by task->pi_lock.
553 * Check whether the end of the boosting chain has been
554 * reached or the state of the chain has changed while we
561 * Check the orig_waiter state. After we dropped the locks,
562 * the previous owner of the lock might have released the lock.
564 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
568 * We dropped all locks after taking a refcount on @task, so
569 * the task might have moved on in the lock chain or even left
570 * the chain completely and blocks now on an unrelated lock or
573 * We stored the lock on which @task was blocked in @next_lock,
574 * so we can detect the chain change.
576 if (next_lock
!= waiter
->lock
)
580 * Drop out, when the task has no waiters. Note,
581 * top_waiter can be NULL, when we are in the deboosting
585 if (!task_has_pi_waiters(task
))
588 * If deadlock detection is off, we stop here if we
589 * are not the top pi waiter of the task. If deadlock
590 * detection is enabled we continue, but stop the
591 * requeueing in the chain walk.
593 if (top_waiter
!= task_top_pi_waiter(task
)) {
594 if (!detect_deadlock
)
602 * If the waiter priority is the same as the task priority
603 * then there is no further priority adjustment necessary. If
604 * deadlock detection is off, we stop the chain walk. If its
605 * enabled we continue, but stop the requeueing in the chain
608 if (waiter
->prio
== task
->prio
&& !dl_task(task
)) {
609 if (!detect_deadlock
)
616 * [4] Get the next lock
620 * [5] We need to trylock here as we are holding task->pi_lock,
621 * which is the reverse lock order versus the other rtmutex
624 if (!raw_spin_trylock(&lock
->wait_lock
)) {
625 raw_spin_unlock_irq(&task
->pi_lock
);
631 * [6] check_exit_conditions_2() protected by task->pi_lock and
634 * Deadlock detection. If the lock is the same as the original
635 * lock which caused us to walk the lock chain or if the
636 * current lock is owned by the task which initiated the chain
637 * walk, we detected a deadlock.
639 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
640 debug_rt_mutex_deadlock(chwalk
, orig_waiter
, lock
);
641 raw_spin_unlock(&lock
->wait_lock
);
647 * If we just follow the lock chain for deadlock detection, no
648 * need to do all the requeue operations. To avoid a truckload
649 * of conditionals around the various places below, just do the
650 * minimum chain walk checks.
654 * No requeue[7] here. Just release @task [8]
656 raw_spin_unlock(&task
->pi_lock
);
657 put_task_struct(task
);
660 * [9] check_exit_conditions_3 protected by lock->wait_lock.
661 * If there is no owner of the lock, end of chain.
663 if (!rt_mutex_owner(lock
)) {
664 raw_spin_unlock_irq(&lock
->wait_lock
);
668 /* [10] Grab the next task, i.e. owner of @lock */
669 task
= rt_mutex_owner(lock
);
670 get_task_struct(task
);
671 raw_spin_lock(&task
->pi_lock
);
674 * No requeue [11] here. We just do deadlock detection.
676 * [12] Store whether owner is blocked
677 * itself. Decision is made after dropping the locks
679 next_lock
= task_blocked_on_lock(task
);
681 * Get the top waiter for the next iteration
683 top_waiter
= rt_mutex_top_waiter(lock
);
685 /* [13] Drop locks */
686 raw_spin_unlock(&task
->pi_lock
);
687 raw_spin_unlock_irq(&lock
->wait_lock
);
689 /* If owner is not blocked, end of chain. */
696 * Store the current top waiter before doing the requeue
697 * operation on @lock. We need it for the boost/deboost
700 prerequeue_top_waiter
= rt_mutex_top_waiter(lock
);
702 /* [7] Requeue the waiter in the lock waiter tree. */
703 rt_mutex_dequeue(lock
, waiter
);
704 waiter
->prio
= task
->prio
;
705 rt_mutex_enqueue(lock
, waiter
);
707 /* [8] Release the task */
708 raw_spin_unlock(&task
->pi_lock
);
709 put_task_struct(task
);
712 * [9] check_exit_conditions_3 protected by lock->wait_lock.
714 * We must abort the chain walk if there is no lock owner even
715 * in the dead lock detection case, as we have nothing to
716 * follow here. This is the end of the chain we are walking.
718 if (!rt_mutex_owner(lock
)) {
720 * If the requeue [7] above changed the top waiter,
721 * then we need to wake the new top waiter up to try
724 if (prerequeue_top_waiter
!= rt_mutex_top_waiter(lock
))
725 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
726 raw_spin_unlock_irq(&lock
->wait_lock
);
730 /* [10] Grab the next task, i.e. the owner of @lock */
731 task
= rt_mutex_owner(lock
);
732 get_task_struct(task
);
733 raw_spin_lock(&task
->pi_lock
);
735 /* [11] requeue the pi waiters if necessary */
736 if (waiter
== rt_mutex_top_waiter(lock
)) {
738 * The waiter became the new top (highest priority)
739 * waiter on the lock. Replace the previous top waiter
740 * in the owner tasks pi waiters tree with this waiter
741 * and adjust the priority of the owner.
743 rt_mutex_dequeue_pi(task
, prerequeue_top_waiter
);
744 rt_mutex_enqueue_pi(task
, waiter
);
745 __rt_mutex_adjust_prio(task
);
747 } else if (prerequeue_top_waiter
== waiter
) {
749 * The waiter was the top waiter on the lock, but is
750 * no longer the top prority waiter. Replace waiter in
751 * the owner tasks pi waiters tree with the new top
752 * (highest priority) waiter and adjust the priority
754 * The new top waiter is stored in @waiter so that
755 * @waiter == @top_waiter evaluates to true below and
756 * we continue to deboost the rest of the chain.
758 rt_mutex_dequeue_pi(task
, waiter
);
759 waiter
= rt_mutex_top_waiter(lock
);
760 rt_mutex_enqueue_pi(task
, waiter
);
761 __rt_mutex_adjust_prio(task
);
764 * Nothing changed. No need to do any priority
770 * [12] check_exit_conditions_4() protected by task->pi_lock
771 * and lock->wait_lock. The actual decisions are made after we
774 * Check whether the task which owns the current lock is pi
775 * blocked itself. If yes we store a pointer to the lock for
776 * the lock chain change detection above. After we dropped
777 * task->pi_lock next_lock cannot be dereferenced anymore.
779 next_lock
= task_blocked_on_lock(task
);
781 * Store the top waiter of @lock for the end of chain walk
784 top_waiter
= rt_mutex_top_waiter(lock
);
786 /* [13] Drop the locks */
787 raw_spin_unlock(&task
->pi_lock
);
788 raw_spin_unlock_irq(&lock
->wait_lock
);
791 * Make the actual exit decisions [12], based on the stored
794 * We reached the end of the lock chain. Stop right here. No
795 * point to go back just to figure that out.
801 * If the current waiter is not the top waiter on the lock,
802 * then we can stop the chain walk here if we are not in full
803 * deadlock detection mode.
805 if (!detect_deadlock
&& waiter
!= top_waiter
)
811 raw_spin_unlock_irq(&task
->pi_lock
);
813 put_task_struct(task
);
819 * Try to take an rt-mutex
821 * Must be called with lock->wait_lock held and interrupts disabled
823 * @lock: The lock to be acquired.
824 * @task: The task which wants to acquire the lock
825 * @waiter: The waiter that is queued to the lock's wait tree if the
826 * callsite called task_blocked_on_lock(), otherwise NULL
828 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
829 struct rt_mutex_waiter
*waiter
)
832 * Before testing whether we can acquire @lock, we set the
833 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
834 * other tasks which try to modify @lock into the slow path
835 * and they serialize on @lock->wait_lock.
837 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
838 * as explained at the top of this file if and only if:
840 * - There is a lock owner. The caller must fixup the
841 * transient state if it does a trylock or leaves the lock
842 * function due to a signal or timeout.
844 * - @task acquires the lock and there are no other
845 * waiters. This is undone in rt_mutex_set_owner(@task) at
846 * the end of this function.
848 mark_rt_mutex_waiters(lock
);
851 * If @lock has an owner, give up.
853 if (rt_mutex_owner(lock
))
857 * If @waiter != NULL, @task has already enqueued the waiter
858 * into @lock waiter tree. If @waiter == NULL then this is a
863 * If waiter is not the highest priority waiter of
866 if (waiter
!= rt_mutex_top_waiter(lock
))
870 * We can acquire the lock. Remove the waiter from the
873 rt_mutex_dequeue(lock
, waiter
);
877 * If the lock has waiters already we check whether @task is
878 * eligible to take over the lock.
880 * If there are no other waiters, @task can acquire
881 * the lock. @task->pi_blocked_on is NULL, so it does
882 * not need to be dequeued.
884 if (rt_mutex_has_waiters(lock
)) {
886 * If @task->prio is greater than or equal to
887 * the top waiter priority (kernel view),
890 if (task
->prio
>= rt_mutex_top_waiter(lock
)->prio
)
894 * The current top waiter stays enqueued. We
895 * don't have to change anything in the lock
900 * No waiters. Take the lock without the
901 * pi_lock dance.@task->pi_blocked_on is NULL
902 * and we have no waiters to enqueue in @task
910 * Clear @task->pi_blocked_on. Requires protection by
911 * @task->pi_lock. Redundant operation for the @waiter == NULL
912 * case, but conditionals are more expensive than a redundant
915 raw_spin_lock(&task
->pi_lock
);
916 task
->pi_blocked_on
= NULL
;
918 * Finish the lock acquisition. @task is the new owner. If
919 * other waiters exist we have to insert the highest priority
920 * waiter into @task->pi_waiters tree.
922 if (rt_mutex_has_waiters(lock
))
923 rt_mutex_enqueue_pi(task
, rt_mutex_top_waiter(lock
));
924 raw_spin_unlock(&task
->pi_lock
);
927 /* We got the lock. */
928 debug_rt_mutex_lock(lock
);
931 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
932 * are still waiters or clears it.
934 rt_mutex_set_owner(lock
, task
);
940 * Task blocks on lock.
942 * Prepare waiter and propagate pi chain
944 * This must be called with lock->wait_lock held and interrupts disabled
946 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
947 struct rt_mutex_waiter
*waiter
,
948 struct task_struct
*task
,
949 enum rtmutex_chainwalk chwalk
)
951 struct task_struct
*owner
= rt_mutex_owner(lock
);
952 struct rt_mutex_waiter
*top_waiter
= waiter
;
953 struct rt_mutex
*next_lock
;
954 int chain_walk
= 0, res
;
957 * Early deadlock detection. We really don't want the task to
958 * enqueue on itself just to untangle the mess later. It's not
959 * only an optimization. We drop the locks, so another waiter
960 * can come in before the chain walk detects the deadlock. So
961 * the other will detect the deadlock and return -EDEADLOCK,
962 * which is wrong, as the other waiter is not in a deadlock
968 raw_spin_lock(&task
->pi_lock
);
969 __rt_mutex_adjust_prio(task
);
972 waiter
->prio
= task
->prio
;
974 /* Get the top priority waiter on the lock */
975 if (rt_mutex_has_waiters(lock
))
976 top_waiter
= rt_mutex_top_waiter(lock
);
977 rt_mutex_enqueue(lock
, waiter
);
979 task
->pi_blocked_on
= waiter
;
981 raw_spin_unlock(&task
->pi_lock
);
986 raw_spin_lock(&owner
->pi_lock
);
987 if (waiter
== rt_mutex_top_waiter(lock
)) {
988 rt_mutex_dequeue_pi(owner
, top_waiter
);
989 rt_mutex_enqueue_pi(owner
, waiter
);
991 __rt_mutex_adjust_prio(owner
);
992 if (owner
->pi_blocked_on
)
994 } else if (rt_mutex_cond_detect_deadlock(waiter
, chwalk
)) {
998 /* Store the lock on which owner is blocked or NULL */
999 next_lock
= task_blocked_on_lock(owner
);
1001 raw_spin_unlock(&owner
->pi_lock
);
1003 * Even if full deadlock detection is on, if the owner is not
1004 * blocked itself, we can avoid finding this out in the chain
1007 if (!chain_walk
|| !next_lock
)
1011 * The owner can't disappear while holding a lock,
1012 * so the owner struct is protected by wait_lock.
1013 * Gets dropped in rt_mutex_adjust_prio_chain()!
1015 get_task_struct(owner
);
1017 raw_spin_unlock_irq(&lock
->wait_lock
);
1019 res
= rt_mutex_adjust_prio_chain(owner
, chwalk
, lock
,
1020 next_lock
, waiter
, task
);
1022 raw_spin_lock_irq(&lock
->wait_lock
);
1028 * Remove the top waiter from the current tasks pi waiter tree and
1031 * Called with lock->wait_lock held and interrupts disabled.
1033 static void mark_wakeup_next_waiter(struct wake_q_head
*wake_q
,
1034 struct rt_mutex
*lock
)
1036 struct rt_mutex_waiter
*waiter
;
1038 raw_spin_lock(¤t
->pi_lock
);
1040 waiter
= rt_mutex_top_waiter(lock
);
1043 * Remove it from current->pi_waiters. We do not adjust a
1044 * possible priority boost right now. We execute wakeup in the
1045 * boosted mode and go back to normal after releasing
1048 rt_mutex_dequeue_pi(current
, waiter
);
1049 __rt_mutex_adjust_prio(current
);
1052 * As we are waking up the top waiter, and the waiter stays
1053 * queued on the lock until it gets the lock, this lock
1054 * obviously has waiters. Just set the bit here and this has
1055 * the added benefit of forcing all new tasks into the
1056 * slow path making sure no task of lower priority than
1057 * the top waiter can steal this lock.
1059 lock
->owner
= (void *) RT_MUTEX_HAS_WAITERS
;
1061 raw_spin_unlock(¤t
->pi_lock
);
1063 wake_q_add(wake_q
, waiter
->task
);
1067 * Remove a waiter from a lock and give up
1069 * Must be called with lock->wait_lock held and interrupts disabled. I must
1070 * have just failed to try_to_take_rt_mutex().
1072 static void remove_waiter(struct rt_mutex
*lock
,
1073 struct rt_mutex_waiter
*waiter
)
1075 bool is_top_waiter
= (waiter
== rt_mutex_top_waiter(lock
));
1076 struct task_struct
*owner
= rt_mutex_owner(lock
);
1077 struct rt_mutex
*next_lock
;
1079 raw_spin_lock(¤t
->pi_lock
);
1080 rt_mutex_dequeue(lock
, waiter
);
1081 current
->pi_blocked_on
= NULL
;
1082 raw_spin_unlock(¤t
->pi_lock
);
1085 * Only update priority if the waiter was the highest priority
1086 * waiter of the lock and there is an owner to update.
1088 if (!owner
|| !is_top_waiter
)
1091 raw_spin_lock(&owner
->pi_lock
);
1093 rt_mutex_dequeue_pi(owner
, waiter
);
1095 if (rt_mutex_has_waiters(lock
))
1096 rt_mutex_enqueue_pi(owner
, rt_mutex_top_waiter(lock
));
1098 __rt_mutex_adjust_prio(owner
);
1100 /* Store the lock on which owner is blocked or NULL */
1101 next_lock
= task_blocked_on_lock(owner
);
1103 raw_spin_unlock(&owner
->pi_lock
);
1106 * Don't walk the chain, if the owner task is not blocked
1112 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1113 get_task_struct(owner
);
1115 raw_spin_unlock_irq(&lock
->wait_lock
);
1117 rt_mutex_adjust_prio_chain(owner
, RT_MUTEX_MIN_CHAINWALK
, lock
,
1118 next_lock
, NULL
, current
);
1120 raw_spin_lock_irq(&lock
->wait_lock
);
1124 * Recheck the pi chain, in case we got a priority setting
1126 * Called from sched_setscheduler
1128 void rt_mutex_adjust_pi(struct task_struct
*task
)
1130 struct rt_mutex_waiter
*waiter
;
1131 struct rt_mutex
*next_lock
;
1132 unsigned long flags
;
1134 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
1136 waiter
= task
->pi_blocked_on
;
1137 if (!waiter
|| (waiter
->prio
== task
->prio
&&
1138 !dl_prio(task
->prio
))) {
1139 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1142 next_lock
= waiter
->lock
;
1143 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1145 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1146 get_task_struct(task
);
1148 rt_mutex_adjust_prio_chain(task
, RT_MUTEX_MIN_CHAINWALK
, NULL
,
1149 next_lock
, NULL
, task
);
1152 void rt_mutex_init_waiter(struct rt_mutex_waiter
*waiter
)
1154 debug_rt_mutex_init_waiter(waiter
);
1155 RB_CLEAR_NODE(&waiter
->pi_tree_entry
);
1156 RB_CLEAR_NODE(&waiter
->tree_entry
);
1157 waiter
->task
= NULL
;
1161 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1162 * @lock: the rt_mutex to take
1163 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1164 * or TASK_UNINTERRUPTIBLE)
1165 * @timeout: the pre-initialized and started timer, or NULL for none
1166 * @waiter: the pre-initialized rt_mutex_waiter
1168 * Must be called with lock->wait_lock held and interrupts disabled
1171 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1172 struct hrtimer_sleeper
*timeout
,
1173 struct rt_mutex_waiter
*waiter
)
1178 /* Try to acquire the lock: */
1179 if (try_to_take_rt_mutex(lock
, current
, waiter
))
1183 * TASK_INTERRUPTIBLE checks for signals and
1184 * timeout. Ignored otherwise.
1186 if (likely(state
== TASK_INTERRUPTIBLE
)) {
1187 /* Signal pending? */
1188 if (signal_pending(current
))
1190 if (timeout
&& !timeout
->task
)
1196 raw_spin_unlock_irq(&lock
->wait_lock
);
1198 debug_rt_mutex_print_deadlock(waiter
);
1202 raw_spin_lock_irq(&lock
->wait_lock
);
1203 set_current_state(state
);
1206 __set_current_state(TASK_RUNNING
);
1210 static void rt_mutex_handle_deadlock(int res
, int detect_deadlock
,
1211 struct rt_mutex_waiter
*w
)
1214 * If the result is not -EDEADLOCK or the caller requested
1215 * deadlock detection, nothing to do here.
1217 if (res
!= -EDEADLOCK
|| detect_deadlock
)
1221 * Yell lowdly and stop the task right here.
1223 rt_mutex_print_deadlock(w
);
1225 set_current_state(TASK_INTERRUPTIBLE
);
1231 * Slow path lock function:
1234 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1235 struct hrtimer_sleeper
*timeout
,
1236 enum rtmutex_chainwalk chwalk
)
1238 struct rt_mutex_waiter waiter
;
1239 unsigned long flags
;
1242 rt_mutex_init_waiter(&waiter
);
1245 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1246 * be called in early boot if the cmpxchg() fast path is disabled
1247 * (debug, no architecture support). In this case we will acquire the
1248 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1249 * enable interrupts in that early boot case. So we need to use the
1250 * irqsave/restore variants.
1252 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1254 /* Try to acquire the lock again: */
1255 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
1256 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1260 set_current_state(state
);
1262 /* Setup the timer, when timeout != NULL */
1263 if (unlikely(timeout
))
1264 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
1266 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, chwalk
);
1269 /* sleep on the mutex */
1270 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
1272 if (unlikely(ret
)) {
1273 __set_current_state(TASK_RUNNING
);
1274 if (rt_mutex_has_waiters(lock
))
1275 remove_waiter(lock
, &waiter
);
1276 rt_mutex_handle_deadlock(ret
, chwalk
, &waiter
);
1280 * try_to_take_rt_mutex() sets the waiter bit
1281 * unconditionally. We might have to fix that up.
1283 fixup_rt_mutex_waiters(lock
);
1285 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1287 /* Remove pending timer: */
1288 if (unlikely(timeout
))
1289 hrtimer_cancel(&timeout
->timer
);
1291 debug_rt_mutex_free_waiter(&waiter
);
1297 * Slow path try-lock function:
1299 static inline int rt_mutex_slowtrylock(struct rt_mutex
*lock
)
1301 unsigned long flags
;
1305 * If the lock already has an owner we fail to get the lock.
1306 * This can be done without taking the @lock->wait_lock as
1307 * it is only being read, and this is a trylock anyway.
1309 if (rt_mutex_owner(lock
))
1313 * The mutex has currently no owner. Lock the wait lock and try to
1314 * acquire the lock. We use irqsave here to support early boot calls.
1316 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1318 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
1321 * try_to_take_rt_mutex() sets the lock waiters bit
1322 * unconditionally. Clean this up.
1324 fixup_rt_mutex_waiters(lock
);
1326 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1332 * Slow path to release a rt-mutex.
1334 * Return whether the current task needs to call rt_mutex_postunlock().
1336 static bool __sched
rt_mutex_slowunlock(struct rt_mutex
*lock
,
1337 struct wake_q_head
*wake_q
)
1339 unsigned long flags
;
1341 /* irqsave required to support early boot calls */
1342 raw_spin_lock_irqsave(&lock
->wait_lock
, flags
);
1344 debug_rt_mutex_unlock(lock
);
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
);
1394 * We should deboost before waking the top waiter task such that
1395 * we don't run two tasks with the 'same' priority. This however
1396 * can lead to prio-inversion if we would get preempted after
1397 * the deboost but before waking our high-prio task, hence the
1398 * preempt_disable before unlock. Pairs with preempt_enable() in
1399 * rt_mutex_postunlock();
1403 raw_spin_unlock_irqrestore(&lock
->wait_lock
, flags
);
1405 return true; /* call rt_mutex_postunlock() */
1409 * debug aware fast / slowpath lock,trylock,unlock
1411 * The atomic acquire/release ops are compiled away, when either the
1412 * architecture does not support cmpxchg or when debugging is enabled.
1415 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
1416 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1417 struct hrtimer_sleeper
*timeout
,
1418 enum rtmutex_chainwalk chwalk
))
1420 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
)))
1423 return slowfn(lock
, state
, NULL
, RT_MUTEX_MIN_CHAINWALK
);
1427 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
1428 struct hrtimer_sleeper
*timeout
,
1429 enum rtmutex_chainwalk chwalk
,
1430 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1431 struct hrtimer_sleeper
*timeout
,
1432 enum rtmutex_chainwalk chwalk
))
1434 if (chwalk
== RT_MUTEX_MIN_CHAINWALK
&&
1435 likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
)))
1438 return slowfn(lock
, state
, timeout
, chwalk
);
1442 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
1443 int (*slowfn
)(struct rt_mutex
*lock
))
1445 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
)))
1448 return slowfn(lock
);
1452 * Performs the wakeup of the the top-waiter and re-enables preemption.
1454 void rt_mutex_postunlock(struct wake_q_head
*wake_q
)
1458 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
1463 rt_mutex_fastunlock(struct rt_mutex
*lock
,
1464 bool (*slowfn
)(struct rt_mutex
*lock
,
1465 struct wake_q_head
*wqh
))
1467 DEFINE_WAKE_Q(wake_q
);
1469 if (likely(rt_mutex_cmpxchg_release(lock
, current
, NULL
)))
1472 if (slowfn(lock
, &wake_q
))
1473 rt_mutex_postunlock(&wake_q
);
1477 * rt_mutex_lock - lock a rt_mutex
1479 * @lock: the rt_mutex to be locked
1481 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
1485 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, rt_mutex_slowlock
);
1487 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
1490 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1492 * @lock: the rt_mutex to be locked
1496 * -EINTR when interrupted by a signal
1498 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
)
1502 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
, rt_mutex_slowlock
);
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
);
1515 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1516 * the timeout structure is provided
1519 * @lock: the rt_mutex to be locked
1520 * @timeout: timeout structure or NULL (no timeout)
1524 * -EINTR when interrupted by a signal
1525 * -ETIMEDOUT when the timeout expired
1528 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
)
1532 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1533 RT_MUTEX_MIN_CHAINWALK
,
1536 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
1539 * rt_mutex_trylock - try to lock a rt_mutex
1541 * @lock: the rt_mutex to be locked
1543 * This function can only be called in thread context. It's safe to
1544 * call it from atomic regions, but not from hard interrupt or soft
1545 * interrupt context.
1547 * Returns 1 on success and 0 on contention
1549 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
1551 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
1554 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
1556 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
1559 * rt_mutex_unlock - unlock a rt_mutex
1561 * @lock: the rt_mutex to be unlocked
1563 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
1565 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
1567 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
1570 * Futex variant, that since futex variants do not use the fast-path, can be
1571 * simple and will not need to retry.
1573 bool __sched
__rt_mutex_futex_unlock(struct rt_mutex
*lock
,
1574 struct wake_q_head
*wake_q
)
1576 lockdep_assert_held(&lock
->wait_lock
);
1578 debug_rt_mutex_unlock(lock
);
1580 if (!rt_mutex_has_waiters(lock
)) {
1582 return false; /* done */
1585 mark_wakeup_next_waiter(wake_q
, lock
);
1587 * We've already deboosted, retain preempt_disabled when dropping
1588 * the wait_lock to avoid inversion until the wakeup. Matched
1589 * by rt_mutex_postunlock();
1593 return true; /* call postunlock() */
1596 void __sched
rt_mutex_futex_unlock(struct rt_mutex
*lock
)
1598 DEFINE_WAKE_Q(wake_q
);
1601 raw_spin_lock_irq(&lock
->wait_lock
);
1602 postunlock
= __rt_mutex_futex_unlock(lock
, &wake_q
);
1603 raw_spin_unlock_irq(&lock
->wait_lock
);
1606 rt_mutex_postunlock(&wake_q
);
1610 * rt_mutex_destroy - mark a mutex unusable
1611 * @lock: the mutex to be destroyed
1613 * This function marks the mutex uninitialized, and any subsequent
1614 * use of the mutex is forbidden. The mutex must not be locked when
1615 * this function is called.
1617 void rt_mutex_destroy(struct rt_mutex
*lock
)
1619 WARN_ON(rt_mutex_is_locked(lock
));
1620 #ifdef CONFIG_DEBUG_RT_MUTEXES
1625 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
1628 * __rt_mutex_init - initialize the rt lock
1630 * @lock: the rt lock to be initialized
1632 * Initialize the rt lock to unlocked state.
1634 * Initializing of a locked rt lock is not allowed
1636 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
1639 raw_spin_lock_init(&lock
->wait_lock
);
1640 lock
->waiters
= RB_ROOT
;
1641 lock
->waiters_leftmost
= NULL
;
1643 debug_rt_mutex_init(lock
, name
);
1645 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
1648 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1651 * @lock: the rt_mutex to be locked
1652 * @proxy_owner:the task to set as owner
1654 * No locking. Caller has to do serializing itself
1656 * Special API call for PI-futex support. This initializes the rtmutex and
1657 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1658 * possible at this point because the pi_state which contains the rtmutex
1659 * is not yet visible to other tasks.
1661 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
1662 struct task_struct
*proxy_owner
)
1664 __rt_mutex_init(lock
, NULL
);
1665 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
1666 rt_mutex_set_owner(lock
, proxy_owner
);
1670 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1672 * @lock: the rt_mutex to be locked
1674 * No locking. Caller has to do serializing itself
1676 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1677 * (debugging) state. Concurrent operations on this rt_mutex are not
1678 * possible because it belongs to the pi_state which is about to be freed
1679 * and it is not longer visible to other tasks.
1681 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
1682 struct task_struct
*proxy_owner
)
1684 debug_rt_mutex_proxy_unlock(lock
);
1685 rt_mutex_set_owner(lock
, NULL
);
1688 int __rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1689 struct rt_mutex_waiter
*waiter
,
1690 struct task_struct
*task
)
1694 if (try_to_take_rt_mutex(lock
, task
, NULL
))
1697 /* We enforce deadlock detection for futexes */
1698 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
,
1699 RT_MUTEX_FULL_CHAINWALK
);
1701 if (ret
&& !rt_mutex_owner(lock
)) {
1703 * Reset the return value. We might have
1704 * returned with -EDEADLK and the owner
1705 * released the lock while we were walking the
1706 * pi chain. Let the waiter sort it out.
1712 remove_waiter(lock
, waiter
);
1714 debug_rt_mutex_print_deadlock(waiter
);
1720 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1721 * @lock: the rt_mutex to take
1722 * @waiter: the pre-initialized rt_mutex_waiter
1723 * @task: the task to prepare
1726 * 0 - task blocked on lock
1727 * 1 - acquired the lock for task, caller should wake it up
1730 * Special API call for FUTEX_REQUEUE_PI support.
1732 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1733 struct rt_mutex_waiter
*waiter
,
1734 struct task_struct
*task
)
1738 raw_spin_lock_irq(&lock
->wait_lock
);
1739 ret
= __rt_mutex_start_proxy_lock(lock
, waiter
, task
);
1740 raw_spin_unlock_irq(&lock
->wait_lock
);
1746 * rt_mutex_next_owner - return the next owner of the lock
1748 * @lock: the rt lock query
1750 * Returns the next owner of the lock or NULL
1752 * Caller has to serialize against other accessors to the lock
1755 * Special API call for PI-futex support
1757 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
1759 if (!rt_mutex_has_waiters(lock
))
1762 return rt_mutex_top_waiter(lock
)->task
;
1766 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
1767 * @lock: the rt_mutex we were woken on
1768 * @to: the timeout, null if none. hrtimer should already have
1770 * @waiter: the pre-initialized rt_mutex_waiter
1772 * Wait for the the lock acquisition started on our behalf by
1773 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1774 * rt_mutex_cleanup_proxy_lock().
1778 * <0 - error, one of -EINTR, -ETIMEDOUT
1780 * Special API call for PI-futex support
1782 int rt_mutex_wait_proxy_lock(struct rt_mutex
*lock
,
1783 struct hrtimer_sleeper
*to
,
1784 struct rt_mutex_waiter
*waiter
)
1788 raw_spin_lock_irq(&lock
->wait_lock
);
1790 set_current_state(TASK_INTERRUPTIBLE
);
1792 /* sleep on the mutex */
1793 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1795 raw_spin_unlock_irq(&lock
->wait_lock
);
1801 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1802 * @lock: the rt_mutex we were woken on
1803 * @waiter: the pre-initialized rt_mutex_waiter
1805 * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
1807 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1808 * in fact still be granted ownership until we're removed. Therefore we can
1809 * find we are in fact the owner and must disregard the
1810 * rt_mutex_wait_proxy_lock() failure.
1813 * true - did the cleanup, we done.
1814 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1815 * caller should disregards its return value.
1817 * Special API call for PI-futex support
1819 bool rt_mutex_cleanup_proxy_lock(struct rt_mutex
*lock
,
1820 struct rt_mutex_waiter
*waiter
)
1822 bool cleanup
= false;
1824 raw_spin_lock_irq(&lock
->wait_lock
);
1826 * Unless we're the owner; we're still enqueued on the wait_list.
1827 * So check if we became owner, if not, take us off the wait_list.
1829 if (rt_mutex_owner(lock
) != current
) {
1830 remove_waiter(lock
, waiter
);
1831 fixup_rt_mutex_waiters(lock
);
1836 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1837 * have to fix that up.
1839 fixup_rt_mutex_waiters(lock
);
1841 raw_spin_unlock_irq(&lock
->wait_lock
);