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 the architecture
74 * supports cmpxchg and if there's no debugging state to be set up
76 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
77 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
78 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
80 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
84 } while (cmpxchg(p
, owner
, owner
| RT_MUTEX_HAS_WAITERS
) != owner
);
88 * Safe fastpath aware unlock:
89 * 1) Clear the waiters bit
90 * 2) Drop lock->wait_lock
91 * 3) Try to unlock the lock with cmpxchg
93 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
)
94 __releases(lock
->wait_lock
)
96 struct task_struct
*owner
= rt_mutex_owner(lock
);
98 clear_rt_mutex_waiters(lock
);
99 raw_spin_unlock(&lock
->wait_lock
);
101 * If a new waiter comes in between the unlock and the cmpxchg
102 * we have two situations:
106 * cmpxchg(p, owner, 0) == owner
107 * mark_rt_mutex_waiters(lock);
113 * mark_rt_mutex_waiters(lock);
115 * cmpxchg(p, owner, 0) != owner
124 return rt_mutex_cmpxchg(lock
, owner
, NULL
);
128 # define rt_mutex_cmpxchg(l,c,n) (0)
129 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
131 lock
->owner
= (struct task_struct
*)
132 ((unsigned long)lock
->owner
| RT_MUTEX_HAS_WAITERS
);
136 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
138 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
)
139 __releases(lock
->wait_lock
)
142 raw_spin_unlock(&lock
->wait_lock
);
148 rt_mutex_waiter_less(struct rt_mutex_waiter
*left
,
149 struct rt_mutex_waiter
*right
)
151 if (left
->prio
< right
->prio
)
155 * If both waiters have dl_prio(), we check the deadlines of the
157 * If left waiter has a dl_prio(), and we didn't return 1 above,
158 * then right waiter has a dl_prio() too.
160 if (dl_prio(left
->prio
))
161 return (left
->task
->dl
.deadline
< right
->task
->dl
.deadline
);
167 rt_mutex_enqueue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
169 struct rb_node
**link
= &lock
->waiters
.rb_node
;
170 struct rb_node
*parent
= NULL
;
171 struct rt_mutex_waiter
*entry
;
176 entry
= rb_entry(parent
, struct rt_mutex_waiter
, tree_entry
);
177 if (rt_mutex_waiter_less(waiter
, entry
)) {
178 link
= &parent
->rb_left
;
180 link
= &parent
->rb_right
;
186 lock
->waiters_leftmost
= &waiter
->tree_entry
;
188 rb_link_node(&waiter
->tree_entry
, parent
, link
);
189 rb_insert_color(&waiter
->tree_entry
, &lock
->waiters
);
193 rt_mutex_dequeue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
195 if (RB_EMPTY_NODE(&waiter
->tree_entry
))
198 if (lock
->waiters_leftmost
== &waiter
->tree_entry
)
199 lock
->waiters_leftmost
= rb_next(&waiter
->tree_entry
);
201 rb_erase(&waiter
->tree_entry
, &lock
->waiters
);
202 RB_CLEAR_NODE(&waiter
->tree_entry
);
206 rt_mutex_enqueue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
208 struct rb_node
**link
= &task
->pi_waiters
.rb_node
;
209 struct rb_node
*parent
= NULL
;
210 struct rt_mutex_waiter
*entry
;
215 entry
= rb_entry(parent
, struct rt_mutex_waiter
, pi_tree_entry
);
216 if (rt_mutex_waiter_less(waiter
, entry
)) {
217 link
= &parent
->rb_left
;
219 link
= &parent
->rb_right
;
225 task
->pi_waiters_leftmost
= &waiter
->pi_tree_entry
;
227 rb_link_node(&waiter
->pi_tree_entry
, parent
, link
);
228 rb_insert_color(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
232 rt_mutex_dequeue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
234 if (RB_EMPTY_NODE(&waiter
->pi_tree_entry
))
237 if (task
->pi_waiters_leftmost
== &waiter
->pi_tree_entry
)
238 task
->pi_waiters_leftmost
= rb_next(&waiter
->pi_tree_entry
);
240 rb_erase(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
241 RB_CLEAR_NODE(&waiter
->pi_tree_entry
);
245 * Calculate task priority from the waiter tree priority
247 * Return task->normal_prio when the waiter tree is empty or when
248 * the waiter is not allowed to do priority boosting
250 int rt_mutex_getprio(struct task_struct
*task
)
252 if (likely(!task_has_pi_waiters(task
)))
253 return task
->normal_prio
;
255 return min(task_top_pi_waiter(task
)->prio
,
259 struct task_struct
*rt_mutex_get_top_task(struct task_struct
*task
)
261 if (likely(!task_has_pi_waiters(task
)))
264 return task_top_pi_waiter(task
)->task
;
268 * Called by sched_setscheduler() to get the priority which will be
269 * effective after the change.
271 int rt_mutex_get_effective_prio(struct task_struct
*task
, int newprio
)
273 if (!task_has_pi_waiters(task
))
276 if (task_top_pi_waiter(task
)->task
->prio
<= newprio
)
277 return task_top_pi_waiter(task
)->task
->prio
;
282 * Adjust the priority of a task, after its pi_waiters got modified.
284 * This can be both boosting and unboosting. task->pi_lock must be held.
286 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
288 int prio
= rt_mutex_getprio(task
);
290 if (task
->prio
!= prio
|| dl_prio(prio
))
291 rt_mutex_setprio(task
, prio
);
295 * Adjust task priority (undo boosting). Called from the exit path of
296 * rt_mutex_slowunlock() and rt_mutex_slowlock().
298 * (Note: We do this outside of the protection of lock->wait_lock to
299 * allow the lock to be taken while or before we readjust the priority
300 * of task. We do not use the spin_xx_mutex() variants here as we are
301 * outside of the debug path.)
303 static void rt_mutex_adjust_prio(struct task_struct
*task
)
307 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
308 __rt_mutex_adjust_prio(task
);
309 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
313 * Deadlock detection is conditional:
315 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
316 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
318 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
319 * conducted independent of the detect argument.
321 * If the waiter argument is NULL this indicates the deboost path and
322 * deadlock detection is disabled independent of the detect argument
323 * and the config settings.
325 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter
*waiter
,
326 enum rtmutex_chainwalk chwalk
)
329 * This is just a wrapper function for the following call,
330 * because debug_rt_mutex_detect_deadlock() smells like a magic
331 * debug feature and I wanted to keep the cond function in the
332 * main source file along with the comments instead of having
333 * two of the same in the headers.
335 return debug_rt_mutex_detect_deadlock(waiter
, chwalk
);
339 * Max number of times we'll walk the boosting chain:
341 int max_lock_depth
= 1024;
343 static inline struct rt_mutex
*task_blocked_on_lock(struct task_struct
*p
)
345 return p
->pi_blocked_on
? p
->pi_blocked_on
->lock
: NULL
;
349 * Adjust the priority chain. Also used for deadlock detection.
350 * Decreases task's usage by one - may thus free the task.
352 * @task: the task owning the mutex (owner) for which a chain walk is
354 * @chwalk: do we have to carry out deadlock detection?
355 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
356 * things for a task that has just got its priority adjusted, and
357 * is waiting on a mutex)
358 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
359 * we dropped its pi_lock. Is never dereferenced, only used for
360 * comparison to detect lock chain changes.
361 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
362 * its priority to the mutex owner (can be NULL in the case
363 * depicted above or if the top waiter is gone away and we are
364 * actually deboosting the owner)
365 * @top_task: the current top waiter
367 * Returns 0 or -EDEADLK.
369 * Chain walk basics and protection scope
371 * [R] refcount on task
372 * [P] task->pi_lock held
373 * [L] rtmutex->wait_lock held
375 * Step Description Protected by
376 * function arguments:
378 * @orig_lock if != NULL @top_task is blocked on it
379 * @next_lock Unprotected. Cannot be
380 * dereferenced. Only used for
382 * @orig_waiter if != NULL @top_task is blocked on it
383 * @top_task current, or in case of proxy
384 * locking protected by calling
387 * loop_sanity_check();
389 * [1] lock(task->pi_lock); [R] acquire [P]
390 * [2] waiter = task->pi_blocked_on; [P]
391 * [3] check_exit_conditions_1(); [P]
392 * [4] lock = waiter->lock; [P]
393 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
394 * unlock(task->pi_lock); release [P]
397 * [6] check_exit_conditions_2(); [P] + [L]
398 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
399 * [8] unlock(task->pi_lock); release [P]
400 * put_task_struct(task); release [R]
401 * [9] check_exit_conditions_3(); [L]
402 * [10] task = owner(lock); [L]
403 * get_task_struct(task); [L] acquire [R]
404 * lock(task->pi_lock); [L] acquire [P]
405 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
406 * [12] check_exit_conditions_4(); [P] + [L]
407 * [13] unlock(task->pi_lock); release [P]
408 * unlock(lock->wait_lock); release [L]
411 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
412 enum rtmutex_chainwalk chwalk
,
413 struct rt_mutex
*orig_lock
,
414 struct rt_mutex
*next_lock
,
415 struct rt_mutex_waiter
*orig_waiter
,
416 struct task_struct
*top_task
)
418 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
419 struct rt_mutex_waiter
*prerequeue_top_waiter
;
420 int ret
= 0, depth
= 0;
421 struct rt_mutex
*lock
;
422 bool detect_deadlock
;
426 detect_deadlock
= rt_mutex_cond_detect_deadlock(orig_waiter
, chwalk
);
429 * The (de)boosting is a step by step approach with a lot of
430 * pitfalls. We want this to be preemptible and we want hold a
431 * maximum of two locks per step. So we have to check
432 * carefully whether things change under us.
436 * We limit the lock chain length for each invocation.
438 if (++depth
> max_lock_depth
) {
442 * Print this only once. If the admin changes the limit,
443 * print a new message when reaching the limit again.
445 if (prev_max
!= max_lock_depth
) {
446 prev_max
= max_lock_depth
;
447 printk(KERN_WARNING
"Maximum lock depth %d reached "
448 "task: %s (%d)\n", max_lock_depth
,
449 top_task
->comm
, task_pid_nr(top_task
));
451 put_task_struct(task
);
457 * We are fully preemptible here and only hold the refcount on
458 * @task. So everything can have changed under us since the
459 * caller or our own code below (goto retry/again) dropped all
464 * [1] Task cannot go away as we did a get_task() before !
466 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
469 * [2] Get the waiter on which @task is blocked on.
471 waiter
= task
->pi_blocked_on
;
474 * [3] check_exit_conditions_1() protected by task->pi_lock.
478 * Check whether the end of the boosting chain has been
479 * reached or the state of the chain has changed while we
486 * Check the orig_waiter state. After we dropped the locks,
487 * the previous owner of the lock might have released the lock.
489 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
493 * We dropped all locks after taking a refcount on @task, so
494 * the task might have moved on in the lock chain or even left
495 * the chain completely and blocks now on an unrelated lock or
498 * We stored the lock on which @task was blocked in @next_lock,
499 * so we can detect the chain change.
501 if (next_lock
!= waiter
->lock
)
505 * Drop out, when the task has no waiters. Note,
506 * top_waiter can be NULL, when we are in the deboosting
510 if (!task_has_pi_waiters(task
))
513 * If deadlock detection is off, we stop here if we
514 * are not the top pi waiter of the task. If deadlock
515 * detection is enabled we continue, but stop the
516 * requeueing in the chain walk.
518 if (top_waiter
!= task_top_pi_waiter(task
)) {
519 if (!detect_deadlock
)
527 * If the waiter priority is the same as the task priority
528 * then there is no further priority adjustment necessary. If
529 * deadlock detection is off, we stop the chain walk. If its
530 * enabled we continue, but stop the requeueing in the chain
533 if (waiter
->prio
== task
->prio
) {
534 if (!detect_deadlock
)
541 * [4] Get the next lock
545 * [5] We need to trylock here as we are holding task->pi_lock,
546 * which is the reverse lock order versus the other rtmutex
549 if (!raw_spin_trylock(&lock
->wait_lock
)) {
550 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
556 * [6] check_exit_conditions_2() protected by task->pi_lock and
559 * Deadlock detection. If the lock is the same as the original
560 * lock which caused us to walk the lock chain or if the
561 * current lock is owned by the task which initiated the chain
562 * walk, we detected a deadlock.
564 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
565 debug_rt_mutex_deadlock(chwalk
, orig_waiter
, lock
);
566 raw_spin_unlock(&lock
->wait_lock
);
572 * If we just follow the lock chain for deadlock detection, no
573 * need to do all the requeue operations. To avoid a truckload
574 * of conditionals around the various places below, just do the
575 * minimum chain walk checks.
579 * No requeue[7] here. Just release @task [8]
581 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
582 put_task_struct(task
);
585 * [9] check_exit_conditions_3 protected by lock->wait_lock.
586 * If there is no owner of the lock, end of chain.
588 if (!rt_mutex_owner(lock
)) {
589 raw_spin_unlock(&lock
->wait_lock
);
593 /* [10] Grab the next task, i.e. owner of @lock */
594 task
= rt_mutex_owner(lock
);
595 get_task_struct(task
);
596 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
599 * No requeue [11] here. We just do deadlock detection.
601 * [12] Store whether owner is blocked
602 * itself. Decision is made after dropping the locks
604 next_lock
= task_blocked_on_lock(task
);
606 * Get the top waiter for the next iteration
608 top_waiter
= rt_mutex_top_waiter(lock
);
610 /* [13] Drop locks */
611 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
612 raw_spin_unlock(&lock
->wait_lock
);
614 /* If owner is not blocked, end of chain. */
621 * Store the current top waiter before doing the requeue
622 * operation on @lock. We need it for the boost/deboost
625 prerequeue_top_waiter
= rt_mutex_top_waiter(lock
);
627 /* [7] Requeue the waiter in the lock waiter list. */
628 rt_mutex_dequeue(lock
, waiter
);
629 waiter
->prio
= task
->prio
;
630 rt_mutex_enqueue(lock
, waiter
);
632 /* [8] Release the task */
633 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
634 put_task_struct(task
);
637 * [9] check_exit_conditions_3 protected by lock->wait_lock.
639 * We must abort the chain walk if there is no lock owner even
640 * in the dead lock detection case, as we have nothing to
641 * follow here. This is the end of the chain we are walking.
643 if (!rt_mutex_owner(lock
)) {
645 * If the requeue [7] above changed the top waiter,
646 * then we need to wake the new top waiter up to try
649 if (prerequeue_top_waiter
!= rt_mutex_top_waiter(lock
))
650 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
651 raw_spin_unlock(&lock
->wait_lock
);
655 /* [10] Grab the next task, i.e. the owner of @lock */
656 task
= rt_mutex_owner(lock
);
657 get_task_struct(task
);
658 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
660 /* [11] requeue the pi waiters if necessary */
661 if (waiter
== rt_mutex_top_waiter(lock
)) {
663 * The waiter became the new top (highest priority)
664 * waiter on the lock. Replace the previous top waiter
665 * in the owner tasks pi waiters list with this waiter
666 * and adjust the priority of the owner.
668 rt_mutex_dequeue_pi(task
, prerequeue_top_waiter
);
669 rt_mutex_enqueue_pi(task
, waiter
);
670 __rt_mutex_adjust_prio(task
);
672 } else if (prerequeue_top_waiter
== waiter
) {
674 * The waiter was the top waiter on the lock, but is
675 * no longer the top prority waiter. Replace waiter in
676 * the owner tasks pi waiters list with the new top
677 * (highest priority) waiter and adjust the priority
679 * The new top waiter is stored in @waiter so that
680 * @waiter == @top_waiter evaluates to true below and
681 * we continue to deboost the rest of the chain.
683 rt_mutex_dequeue_pi(task
, waiter
);
684 waiter
= rt_mutex_top_waiter(lock
);
685 rt_mutex_enqueue_pi(task
, waiter
);
686 __rt_mutex_adjust_prio(task
);
689 * Nothing changed. No need to do any priority
695 * [12] check_exit_conditions_4() protected by task->pi_lock
696 * and lock->wait_lock. The actual decisions are made after we
699 * Check whether the task which owns the current lock is pi
700 * blocked itself. If yes we store a pointer to the lock for
701 * the lock chain change detection above. After we dropped
702 * task->pi_lock next_lock cannot be dereferenced anymore.
704 next_lock
= task_blocked_on_lock(task
);
706 * Store the top waiter of @lock for the end of chain walk
709 top_waiter
= rt_mutex_top_waiter(lock
);
711 /* [13] Drop the locks */
712 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
713 raw_spin_unlock(&lock
->wait_lock
);
716 * Make the actual exit decisions [12], based on the stored
719 * We reached the end of the lock chain. Stop right here. No
720 * point to go back just to figure that out.
726 * If the current waiter is not the top waiter on the lock,
727 * then we can stop the chain walk here if we are not in full
728 * deadlock detection mode.
730 if (!detect_deadlock
&& waiter
!= top_waiter
)
736 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
738 put_task_struct(task
);
744 * Try to take an rt-mutex
746 * Must be called with lock->wait_lock held.
748 * @lock: The lock to be acquired.
749 * @task: The task which wants to acquire the lock
750 * @waiter: The waiter that is queued to the lock's wait list if the
751 * callsite called task_blocked_on_lock(), otherwise NULL
753 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
754 struct rt_mutex_waiter
*waiter
)
759 * Before testing whether we can acquire @lock, we set the
760 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
761 * other tasks which try to modify @lock into the slow path
762 * and they serialize on @lock->wait_lock.
764 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
765 * as explained at the top of this file if and only if:
767 * - There is a lock owner. The caller must fixup the
768 * transient state if it does a trylock or leaves the lock
769 * function due to a signal or timeout.
771 * - @task acquires the lock and there are no other
772 * waiters. This is undone in rt_mutex_set_owner(@task) at
773 * the end of this function.
775 mark_rt_mutex_waiters(lock
);
778 * If @lock has an owner, give up.
780 if (rt_mutex_owner(lock
))
784 * If @waiter != NULL, @task has already enqueued the waiter
785 * into @lock waiter list. If @waiter == NULL then this is a
790 * If waiter is not the highest priority waiter of
793 if (waiter
!= rt_mutex_top_waiter(lock
))
797 * We can acquire the lock. Remove the waiter from the
800 rt_mutex_dequeue(lock
, waiter
);
804 * If the lock has waiters already we check whether @task is
805 * eligible to take over the lock.
807 * If there are no other waiters, @task can acquire
808 * the lock. @task->pi_blocked_on is NULL, so it does
809 * not need to be dequeued.
811 if (rt_mutex_has_waiters(lock
)) {
813 * If @task->prio is greater than or equal to
814 * the top waiter priority (kernel view),
817 if (task
->prio
>= rt_mutex_top_waiter(lock
)->prio
)
821 * The current top waiter stays enqueued. We
822 * don't have to change anything in the lock
827 * No waiters. Take the lock without the
828 * pi_lock dance.@task->pi_blocked_on is NULL
829 * and we have no waiters to enqueue in @task
837 * Clear @task->pi_blocked_on. Requires protection by
838 * @task->pi_lock. Redundant operation for the @waiter == NULL
839 * case, but conditionals are more expensive than a redundant
842 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
843 task
->pi_blocked_on
= NULL
;
845 * Finish the lock acquisition. @task is the new owner. If
846 * other waiters exist we have to insert the highest priority
847 * waiter into @task->pi_waiters list.
849 if (rt_mutex_has_waiters(lock
))
850 rt_mutex_enqueue_pi(task
, rt_mutex_top_waiter(lock
));
851 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
854 /* We got the lock. */
855 debug_rt_mutex_lock(lock
);
858 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
859 * are still waiters or clears it.
861 rt_mutex_set_owner(lock
, task
);
863 rt_mutex_deadlock_account_lock(lock
, task
);
869 * Task blocks on lock.
871 * Prepare waiter and propagate pi chain
873 * This must be called with lock->wait_lock held.
875 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
876 struct rt_mutex_waiter
*waiter
,
877 struct task_struct
*task
,
878 enum rtmutex_chainwalk chwalk
)
880 struct task_struct
*owner
= rt_mutex_owner(lock
);
881 struct rt_mutex_waiter
*top_waiter
= waiter
;
882 struct rt_mutex
*next_lock
;
883 int chain_walk
= 0, res
;
887 * Early deadlock detection. We really don't want the task to
888 * enqueue on itself just to untangle the mess later. It's not
889 * only an optimization. We drop the locks, so another waiter
890 * can come in before the chain walk detects the deadlock. So
891 * the other will detect the deadlock and return -EDEADLOCK,
892 * which is wrong, as the other waiter is not in a deadlock
898 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
899 __rt_mutex_adjust_prio(task
);
902 waiter
->prio
= task
->prio
;
904 /* Get the top priority waiter on the lock */
905 if (rt_mutex_has_waiters(lock
))
906 top_waiter
= rt_mutex_top_waiter(lock
);
907 rt_mutex_enqueue(lock
, waiter
);
909 task
->pi_blocked_on
= waiter
;
911 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
916 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
917 if (waiter
== rt_mutex_top_waiter(lock
)) {
918 rt_mutex_dequeue_pi(owner
, top_waiter
);
919 rt_mutex_enqueue_pi(owner
, waiter
);
921 __rt_mutex_adjust_prio(owner
);
922 if (owner
->pi_blocked_on
)
924 } else if (rt_mutex_cond_detect_deadlock(waiter
, chwalk
)) {
928 /* Store the lock on which owner is blocked or NULL */
929 next_lock
= task_blocked_on_lock(owner
);
931 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
933 * Even if full deadlock detection is on, if the owner is not
934 * blocked itself, we can avoid finding this out in the chain
937 if (!chain_walk
|| !next_lock
)
941 * The owner can't disappear while holding a lock,
942 * so the owner struct is protected by wait_lock.
943 * Gets dropped in rt_mutex_adjust_prio_chain()!
945 get_task_struct(owner
);
947 raw_spin_unlock(&lock
->wait_lock
);
949 res
= rt_mutex_adjust_prio_chain(owner
, chwalk
, lock
,
950 next_lock
, waiter
, task
);
952 raw_spin_lock(&lock
->wait_lock
);
958 * Wake up the next waiter on the lock.
960 * Remove the top waiter from the current tasks pi waiter list and
963 * Called with lock->wait_lock held.
965 static void wakeup_next_waiter(struct rt_mutex
*lock
)
967 struct rt_mutex_waiter
*waiter
;
970 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
972 waiter
= rt_mutex_top_waiter(lock
);
975 * Remove it from current->pi_waiters. We do not adjust a
976 * possible priority boost right now. We execute wakeup in the
977 * boosted mode and go back to normal after releasing
980 rt_mutex_dequeue_pi(current
, waiter
);
983 * As we are waking up the top waiter, and the waiter stays
984 * queued on the lock until it gets the lock, this lock
985 * obviously has waiters. Just set the bit here and this has
986 * the added benefit of forcing all new tasks into the
987 * slow path making sure no task of lower priority than
988 * the top waiter can steal this lock.
990 lock
->owner
= (void *) RT_MUTEX_HAS_WAITERS
;
992 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
995 * It's safe to dereference waiter as it cannot go away as
996 * long as we hold lock->wait_lock. The waiter task needs to
997 * acquire it in order to dequeue the waiter.
999 wake_up_process(waiter
->task
);
1003 * Remove a waiter from a lock and give up
1005 * Must be called with lock->wait_lock held and
1006 * have just failed to try_to_take_rt_mutex().
1008 static void remove_waiter(struct rt_mutex
*lock
,
1009 struct rt_mutex_waiter
*waiter
)
1011 bool is_top_waiter
= (waiter
== rt_mutex_top_waiter(lock
));
1012 struct task_struct
*owner
= rt_mutex_owner(lock
);
1013 struct rt_mutex
*next_lock
;
1014 unsigned long flags
;
1016 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
1017 rt_mutex_dequeue(lock
, waiter
);
1018 current
->pi_blocked_on
= NULL
;
1019 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
1022 * Only update priority if the waiter was the highest priority
1023 * waiter of the lock and there is an owner to update.
1025 if (!owner
|| !is_top_waiter
)
1028 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
1030 rt_mutex_dequeue_pi(owner
, waiter
);
1032 if (rt_mutex_has_waiters(lock
))
1033 rt_mutex_enqueue_pi(owner
, rt_mutex_top_waiter(lock
));
1035 __rt_mutex_adjust_prio(owner
);
1037 /* Store the lock on which owner is blocked or NULL */
1038 next_lock
= task_blocked_on_lock(owner
);
1040 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
1043 * Don't walk the chain, if the owner task is not blocked
1049 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1050 get_task_struct(owner
);
1052 raw_spin_unlock(&lock
->wait_lock
);
1054 rt_mutex_adjust_prio_chain(owner
, RT_MUTEX_MIN_CHAINWALK
, lock
,
1055 next_lock
, NULL
, current
);
1057 raw_spin_lock(&lock
->wait_lock
);
1061 * Recheck the pi chain, in case we got a priority setting
1063 * Called from sched_setscheduler
1065 void rt_mutex_adjust_pi(struct task_struct
*task
)
1067 struct rt_mutex_waiter
*waiter
;
1068 struct rt_mutex
*next_lock
;
1069 unsigned long flags
;
1071 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
1073 waiter
= task
->pi_blocked_on
;
1074 if (!waiter
|| (waiter
->prio
== task
->prio
&&
1075 !dl_prio(task
->prio
))) {
1076 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1079 next_lock
= waiter
->lock
;
1080 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1082 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1083 get_task_struct(task
);
1085 rt_mutex_adjust_prio_chain(task
, RT_MUTEX_MIN_CHAINWALK
, NULL
,
1086 next_lock
, NULL
, task
);
1090 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1091 * @lock: the rt_mutex to take
1092 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1093 * or TASK_UNINTERRUPTIBLE)
1094 * @timeout: the pre-initialized and started timer, or NULL for none
1095 * @waiter: the pre-initialized rt_mutex_waiter
1097 * lock->wait_lock must be held by the caller.
1100 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1101 struct hrtimer_sleeper
*timeout
,
1102 struct rt_mutex_waiter
*waiter
)
1107 /* Try to acquire the lock: */
1108 if (try_to_take_rt_mutex(lock
, current
, waiter
))
1112 * TASK_INTERRUPTIBLE checks for signals and
1113 * timeout. Ignored otherwise.
1115 if (unlikely(state
== TASK_INTERRUPTIBLE
)) {
1116 /* Signal pending? */
1117 if (signal_pending(current
))
1119 if (timeout
&& !timeout
->task
)
1125 raw_spin_unlock(&lock
->wait_lock
);
1127 debug_rt_mutex_print_deadlock(waiter
);
1129 schedule_rt_mutex(lock
);
1131 raw_spin_lock(&lock
->wait_lock
);
1132 set_current_state(state
);
1135 __set_current_state(TASK_RUNNING
);
1139 static void rt_mutex_handle_deadlock(int res
, int detect_deadlock
,
1140 struct rt_mutex_waiter
*w
)
1143 * If the result is not -EDEADLOCK or the caller requested
1144 * deadlock detection, nothing to do here.
1146 if (res
!= -EDEADLOCK
|| detect_deadlock
)
1150 * Yell lowdly and stop the task right here.
1152 rt_mutex_print_deadlock(w
);
1154 set_current_state(TASK_INTERRUPTIBLE
);
1160 * Slow path lock function:
1163 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1164 struct hrtimer_sleeper
*timeout
,
1165 enum rtmutex_chainwalk chwalk
)
1167 struct rt_mutex_waiter waiter
;
1170 debug_rt_mutex_init_waiter(&waiter
);
1171 RB_CLEAR_NODE(&waiter
.pi_tree_entry
);
1172 RB_CLEAR_NODE(&waiter
.tree_entry
);
1174 raw_spin_lock(&lock
->wait_lock
);
1176 /* Try to acquire the lock again: */
1177 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
1178 raw_spin_unlock(&lock
->wait_lock
);
1182 set_current_state(state
);
1184 /* Setup the timer, when timeout != NULL */
1185 if (unlikely(timeout
)) {
1186 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
1187 if (!hrtimer_active(&timeout
->timer
))
1188 timeout
->task
= NULL
;
1191 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, chwalk
);
1194 /* sleep on the mutex */
1195 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
1197 if (unlikely(ret
)) {
1198 __set_current_state(TASK_RUNNING
);
1199 if (rt_mutex_has_waiters(lock
))
1200 remove_waiter(lock
, &waiter
);
1201 rt_mutex_handle_deadlock(ret
, chwalk
, &waiter
);
1205 * try_to_take_rt_mutex() sets the waiter bit
1206 * unconditionally. We might have to fix that up.
1208 fixup_rt_mutex_waiters(lock
);
1210 raw_spin_unlock(&lock
->wait_lock
);
1212 /* Remove pending timer: */
1213 if (unlikely(timeout
))
1214 hrtimer_cancel(&timeout
->timer
);
1216 debug_rt_mutex_free_waiter(&waiter
);
1222 * Slow path try-lock function:
1224 static inline int rt_mutex_slowtrylock(struct rt_mutex
*lock
)
1229 * If the lock already has an owner we fail to get the lock.
1230 * This can be done without taking the @lock->wait_lock as
1231 * it is only being read, and this is a trylock anyway.
1233 if (rt_mutex_owner(lock
))
1237 * The mutex has currently no owner. Lock the wait lock and
1238 * try to acquire the lock.
1240 raw_spin_lock(&lock
->wait_lock
);
1242 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
1245 * try_to_take_rt_mutex() sets the lock waiters bit
1246 * unconditionally. Clean this up.
1248 fixup_rt_mutex_waiters(lock
);
1250 raw_spin_unlock(&lock
->wait_lock
);
1256 * Slow path to release a rt-mutex:
1259 rt_mutex_slowunlock(struct rt_mutex
*lock
)
1261 raw_spin_lock(&lock
->wait_lock
);
1263 debug_rt_mutex_unlock(lock
);
1265 rt_mutex_deadlock_account_unlock(current
);
1268 * We must be careful here if the fast path is enabled. If we
1269 * have no waiters queued we cannot set owner to NULL here
1272 * foo->lock->owner = NULL;
1273 * rtmutex_lock(foo->lock); <- fast path
1274 * free = atomic_dec_and_test(foo->refcnt);
1275 * rtmutex_unlock(foo->lock); <- fast path
1278 * raw_spin_unlock(foo->lock->wait_lock);
1280 * So for the fastpath enabled kernel:
1282 * Nothing can set the waiters bit as long as we hold
1283 * lock->wait_lock. So we do the following sequence:
1285 * owner = rt_mutex_owner(lock);
1286 * clear_rt_mutex_waiters(lock);
1287 * raw_spin_unlock(&lock->wait_lock);
1288 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1292 * The fastpath disabled variant is simple as all access to
1293 * lock->owner is serialized by lock->wait_lock:
1295 * lock->owner = NULL;
1296 * raw_spin_unlock(&lock->wait_lock);
1298 while (!rt_mutex_has_waiters(lock
)) {
1299 /* Drops lock->wait_lock ! */
1300 if (unlock_rt_mutex_safe(lock
) == true)
1302 /* Relock the rtmutex and try again */
1303 raw_spin_lock(&lock
->wait_lock
);
1307 * The wakeup next waiter path does not suffer from the above
1308 * race. See the comments there.
1310 wakeup_next_waiter(lock
);
1312 raw_spin_unlock(&lock
->wait_lock
);
1314 /* Undo pi boosting if necessary: */
1315 rt_mutex_adjust_prio(current
);
1319 * debug aware fast / slowpath lock,trylock,unlock
1321 * The atomic acquire/release ops are compiled away, when either the
1322 * architecture does not support cmpxchg or when debugging is enabled.
1325 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
1326 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1327 struct hrtimer_sleeper
*timeout
,
1328 enum rtmutex_chainwalk chwalk
))
1330 if (likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1331 rt_mutex_deadlock_account_lock(lock
, current
);
1334 return slowfn(lock
, state
, NULL
, RT_MUTEX_MIN_CHAINWALK
);
1338 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
1339 struct hrtimer_sleeper
*timeout
,
1340 enum rtmutex_chainwalk chwalk
,
1341 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1342 struct hrtimer_sleeper
*timeout
,
1343 enum rtmutex_chainwalk chwalk
))
1345 if (chwalk
== RT_MUTEX_MIN_CHAINWALK
&&
1346 likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1347 rt_mutex_deadlock_account_lock(lock
, current
);
1350 return slowfn(lock
, state
, timeout
, chwalk
);
1354 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
1355 int (*slowfn
)(struct rt_mutex
*lock
))
1357 if (likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1358 rt_mutex_deadlock_account_lock(lock
, current
);
1361 return slowfn(lock
);
1365 rt_mutex_fastunlock(struct rt_mutex
*lock
,
1366 void (*slowfn
)(struct rt_mutex
*lock
))
1368 if (likely(rt_mutex_cmpxchg(lock
, current
, NULL
)))
1369 rt_mutex_deadlock_account_unlock(current
);
1375 * rt_mutex_lock - lock a rt_mutex
1377 * @lock: the rt_mutex to be locked
1379 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
1383 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, rt_mutex_slowlock
);
1385 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
1388 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1390 * @lock: the rt_mutex to be locked
1394 * -EINTR when interrupted by a signal
1396 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
)
1400 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
, rt_mutex_slowlock
);
1402 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible
);
1405 * Futex variant with full deadlock detection.
1407 int rt_mutex_timed_futex_lock(struct rt_mutex
*lock
,
1408 struct hrtimer_sleeper
*timeout
)
1412 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1413 RT_MUTEX_FULL_CHAINWALK
,
1418 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1419 * the timeout structure is provided
1422 * @lock: the rt_mutex to be locked
1423 * @timeout: timeout structure or NULL (no timeout)
1427 * -EINTR when interrupted by a signal
1428 * -ETIMEDOUT when the timeout expired
1431 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
)
1435 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1436 RT_MUTEX_MIN_CHAINWALK
,
1439 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
1442 * rt_mutex_trylock - try to lock a rt_mutex
1444 * @lock: the rt_mutex to be locked
1446 * Returns 1 on success and 0 on contention
1448 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
1450 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
1452 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
1455 * rt_mutex_unlock - unlock a rt_mutex
1457 * @lock: the rt_mutex to be unlocked
1459 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
1461 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
1463 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
1466 * rt_mutex_destroy - mark a mutex unusable
1467 * @lock: the mutex to be destroyed
1469 * This function marks the mutex uninitialized, and any subsequent
1470 * use of the mutex is forbidden. The mutex must not be locked when
1471 * this function is called.
1473 void rt_mutex_destroy(struct rt_mutex
*lock
)
1475 WARN_ON(rt_mutex_is_locked(lock
));
1476 #ifdef CONFIG_DEBUG_RT_MUTEXES
1481 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
1484 * __rt_mutex_init - initialize the rt lock
1486 * @lock: the rt lock to be initialized
1488 * Initialize the rt lock to unlocked state.
1490 * Initializing of a locked rt lock is not allowed
1492 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
1495 raw_spin_lock_init(&lock
->wait_lock
);
1496 lock
->waiters
= RB_ROOT
;
1497 lock
->waiters_leftmost
= NULL
;
1499 debug_rt_mutex_init(lock
, name
);
1501 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
1504 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1507 * @lock: the rt_mutex to be locked
1508 * @proxy_owner:the task to set as owner
1510 * No locking. Caller has to do serializing itself
1511 * Special API call for PI-futex support
1513 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
1514 struct task_struct
*proxy_owner
)
1516 __rt_mutex_init(lock
, NULL
);
1517 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
1518 rt_mutex_set_owner(lock
, proxy_owner
);
1519 rt_mutex_deadlock_account_lock(lock
, proxy_owner
);
1523 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1525 * @lock: the rt_mutex to be locked
1527 * No locking. Caller has to do serializing itself
1528 * Special API call for PI-futex support
1530 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
1531 struct task_struct
*proxy_owner
)
1533 debug_rt_mutex_proxy_unlock(lock
);
1534 rt_mutex_set_owner(lock
, NULL
);
1535 rt_mutex_deadlock_account_unlock(proxy_owner
);
1539 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1540 * @lock: the rt_mutex to take
1541 * @waiter: the pre-initialized rt_mutex_waiter
1542 * @task: the task to prepare
1545 * 0 - task blocked on lock
1546 * 1 - acquired the lock for task, caller should wake it up
1549 * Special API call for FUTEX_REQUEUE_PI support.
1551 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1552 struct rt_mutex_waiter
*waiter
,
1553 struct task_struct
*task
)
1557 raw_spin_lock(&lock
->wait_lock
);
1559 if (try_to_take_rt_mutex(lock
, task
, NULL
)) {
1560 raw_spin_unlock(&lock
->wait_lock
);
1564 /* We enforce deadlock detection for futexes */
1565 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
,
1566 RT_MUTEX_FULL_CHAINWALK
);
1568 if (ret
&& !rt_mutex_owner(lock
)) {
1570 * Reset the return value. We might have
1571 * returned with -EDEADLK and the owner
1572 * released the lock while we were walking the
1573 * pi chain. Let the waiter sort it out.
1579 remove_waiter(lock
, waiter
);
1581 raw_spin_unlock(&lock
->wait_lock
);
1583 debug_rt_mutex_print_deadlock(waiter
);
1589 * rt_mutex_next_owner - return the next owner of the lock
1591 * @lock: the rt lock query
1593 * Returns the next owner of the lock or NULL
1595 * Caller has to serialize against other accessors to the lock
1598 * Special API call for PI-futex support
1600 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
1602 if (!rt_mutex_has_waiters(lock
))
1605 return rt_mutex_top_waiter(lock
)->task
;
1609 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1610 * @lock: the rt_mutex we were woken on
1611 * @to: the timeout, null if none. hrtimer should already have
1613 * @waiter: the pre-initialized rt_mutex_waiter
1615 * Complete the lock acquisition started our behalf by another thread.
1619 * <0 - error, one of -EINTR, -ETIMEDOUT
1621 * Special API call for PI-futex requeue support
1623 int rt_mutex_finish_proxy_lock(struct rt_mutex
*lock
,
1624 struct hrtimer_sleeper
*to
,
1625 struct rt_mutex_waiter
*waiter
)
1629 raw_spin_lock(&lock
->wait_lock
);
1631 set_current_state(TASK_INTERRUPTIBLE
);
1633 /* sleep on the mutex */
1634 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1637 remove_waiter(lock
, waiter
);
1640 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1641 * have to fix that up.
1643 fixup_rt_mutex_waiters(lock
);
1645 raw_spin_unlock(&lock
->wait_lock
);