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 check whether the priority change
269 * is overruled by a possible priority boosting.
271 int rt_mutex_check_prio(struct task_struct
*task
, int newprio
)
273 if (!task_has_pi_waiters(task
))
276 return task_top_pi_waiter(task
)->task
->prio
<= newprio
;
280 * Adjust the priority of a task, after its pi_waiters got modified.
282 * This can be both boosting and unboosting. task->pi_lock must be held.
284 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
286 int prio
= rt_mutex_getprio(task
);
288 if (task
->prio
!= prio
|| dl_prio(prio
))
289 rt_mutex_setprio(task
, prio
);
293 * Adjust task priority (undo boosting). Called from the exit path of
294 * rt_mutex_slowunlock() and rt_mutex_slowlock().
296 * (Note: We do this outside of the protection of lock->wait_lock to
297 * allow the lock to be taken while or before we readjust the priority
298 * of task. We do not use the spin_xx_mutex() variants here as we are
299 * outside of the debug path.)
301 static void rt_mutex_adjust_prio(struct task_struct
*task
)
305 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
306 __rt_mutex_adjust_prio(task
);
307 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
311 * Deadlock detection is conditional:
313 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
314 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
316 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
317 * conducted independent of the detect argument.
319 * If the waiter argument is NULL this indicates the deboost path and
320 * deadlock detection is disabled independent of the detect argument
321 * and the config settings.
323 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter
*waiter
,
324 enum rtmutex_chainwalk chwalk
)
327 * This is just a wrapper function for the following call,
328 * because debug_rt_mutex_detect_deadlock() smells like a magic
329 * debug feature and I wanted to keep the cond function in the
330 * main source file along with the comments instead of having
331 * two of the same in the headers.
333 return debug_rt_mutex_detect_deadlock(waiter
, chwalk
);
337 * Max number of times we'll walk the boosting chain:
339 int max_lock_depth
= 1024;
341 static inline struct rt_mutex
*task_blocked_on_lock(struct task_struct
*p
)
343 return p
->pi_blocked_on
? p
->pi_blocked_on
->lock
: NULL
;
347 * Adjust the priority chain. Also used for deadlock detection.
348 * Decreases task's usage by one - may thus free the task.
350 * @task: the task owning the mutex (owner) for which a chain walk is
352 * @deadlock_detect: do we have to carry out deadlock detection?
353 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
354 * things for a task that has just got its priority adjusted, and
355 * is waiting on a mutex)
356 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
357 * we dropped its pi_lock. Is never dereferenced, only used for
358 * comparison to detect lock chain changes.
359 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
360 * its priority to the mutex owner (can be NULL in the case
361 * depicted above or if the top waiter is gone away and we are
362 * actually deboosting the owner)
363 * @top_task: the current top waiter
365 * Returns 0 or -EDEADLK.
367 * Chain walk basics and protection scope
369 * [R] refcount on task
370 * [P] task->pi_lock held
371 * [L] rtmutex->wait_lock held
373 * Step Description Protected by
374 * function arguments:
376 * @orig_lock if != NULL @top_task is blocked on it
377 * @next_lock Unprotected. Cannot be
378 * dereferenced. Only used for
380 * @orig_waiter if != NULL @top_task is blocked on it
381 * @top_task current, or in case of proxy
382 * locking protected by calling
385 * loop_sanity_check();
387 * [1] lock(task->pi_lock); [R] acquire [P]
388 * [2] waiter = task->pi_blocked_on; [P]
389 * [3] check_exit_conditions_1(); [P]
390 * [4] lock = waiter->lock; [P]
391 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
392 * unlock(task->pi_lock); release [P]
395 * [6] check_exit_conditions_2(); [P] + [L]
396 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
397 * [8] unlock(task->pi_lock); release [P]
398 * put_task_struct(task); release [R]
399 * [9] check_exit_conditions_3(); [L]
400 * [10] task = owner(lock); [L]
401 * get_task_struct(task); [L] acquire [R]
402 * lock(task->pi_lock); [L] acquire [P]
403 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
404 * [12] check_exit_conditions_4(); [P] + [L]
405 * [13] unlock(task->pi_lock); release [P]
406 * unlock(lock->wait_lock); release [L]
409 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
410 enum rtmutex_chainwalk chwalk
,
411 struct rt_mutex
*orig_lock
,
412 struct rt_mutex
*next_lock
,
413 struct rt_mutex_waiter
*orig_waiter
,
414 struct task_struct
*top_task
)
416 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
417 struct rt_mutex_waiter
*prerequeue_top_waiter
;
418 int ret
= 0, depth
= 0;
419 struct rt_mutex
*lock
;
420 bool detect_deadlock
;
424 detect_deadlock
= rt_mutex_cond_detect_deadlock(orig_waiter
, chwalk
);
427 * The (de)boosting is a step by step approach with a lot of
428 * pitfalls. We want this to be preemptible and we want hold a
429 * maximum of two locks per step. So we have to check
430 * carefully whether things change under us.
434 * We limit the lock chain length for each invocation.
436 if (++depth
> max_lock_depth
) {
440 * Print this only once. If the admin changes the limit,
441 * print a new message when reaching the limit again.
443 if (prev_max
!= max_lock_depth
) {
444 prev_max
= max_lock_depth
;
445 printk(KERN_WARNING
"Maximum lock depth %d reached "
446 "task: %s (%d)\n", max_lock_depth
,
447 top_task
->comm
, task_pid_nr(top_task
));
449 put_task_struct(task
);
455 * We are fully preemptible here and only hold the refcount on
456 * @task. So everything can have changed under us since the
457 * caller or our own code below (goto retry/again) dropped all
462 * [1] Task cannot go away as we did a get_task() before !
464 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
467 * [2] Get the waiter on which @task is blocked on.
469 waiter
= task
->pi_blocked_on
;
472 * [3] check_exit_conditions_1() protected by task->pi_lock.
476 * Check whether the end of the boosting chain has been
477 * reached or the state of the chain has changed while we
484 * Check the orig_waiter state. After we dropped the locks,
485 * the previous owner of the lock might have released the lock.
487 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
491 * We dropped all locks after taking a refcount on @task, so
492 * the task might have moved on in the lock chain or even left
493 * the chain completely and blocks now on an unrelated lock or
496 * We stored the lock on which @task was blocked in @next_lock,
497 * so we can detect the chain change.
499 if (next_lock
!= waiter
->lock
)
503 * Drop out, when the task has no waiters. Note,
504 * top_waiter can be NULL, when we are in the deboosting
508 if (!task_has_pi_waiters(task
))
511 * If deadlock detection is off, we stop here if we
512 * are not the top pi waiter of the task. If deadlock
513 * detection is enabled we continue, but stop the
514 * requeueing in the chain walk.
516 if (top_waiter
!= task_top_pi_waiter(task
)) {
517 if (!detect_deadlock
)
525 * If the waiter priority is the same as the task priority
526 * then there is no further priority adjustment necessary. If
527 * deadlock detection is off, we stop the chain walk. If its
528 * enabled we continue, but stop the requeueing in the chain
531 if (waiter
->prio
== task
->prio
) {
532 if (!detect_deadlock
)
539 * [4] Get the next lock
543 * [5] We need to trylock here as we are holding task->pi_lock,
544 * which is the reverse lock order versus the other rtmutex
547 if (!raw_spin_trylock(&lock
->wait_lock
)) {
548 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
554 * [6] check_exit_conditions_2() protected by task->pi_lock and
557 * Deadlock detection. If the lock is the same as the original
558 * lock which caused us to walk the lock chain or if the
559 * current lock is owned by the task which initiated the chain
560 * walk, we detected a deadlock.
562 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
563 debug_rt_mutex_deadlock(chwalk
, orig_waiter
, lock
);
564 raw_spin_unlock(&lock
->wait_lock
);
570 * If we just follow the lock chain for deadlock detection, no
571 * need to do all the requeue operations. To avoid a truckload
572 * of conditionals around the various places below, just do the
573 * minimum chain walk checks.
577 * No requeue[7] here. Just release @task [8]
579 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
580 put_task_struct(task
);
583 * [9] check_exit_conditions_3 protected by lock->wait_lock.
584 * If there is no owner of the lock, end of chain.
586 if (!rt_mutex_owner(lock
)) {
587 raw_spin_unlock(&lock
->wait_lock
);
591 /* [10] Grab the next task, i.e. owner of @lock */
592 task
= rt_mutex_owner(lock
);
593 get_task_struct(task
);
594 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
597 * No requeue [11] here. We just do deadlock detection.
599 * [12] Store whether owner is blocked
600 * itself. Decision is made after dropping the locks
602 next_lock
= task_blocked_on_lock(task
);
604 * Get the top waiter for the next iteration
606 top_waiter
= rt_mutex_top_waiter(lock
);
608 /* [13] Drop locks */
609 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
610 raw_spin_unlock(&lock
->wait_lock
);
612 /* If owner is not blocked, end of chain. */
619 * Store the current top waiter before doing the requeue
620 * operation on @lock. We need it for the boost/deboost
623 prerequeue_top_waiter
= rt_mutex_top_waiter(lock
);
625 /* [7] Requeue the waiter in the lock waiter list. */
626 rt_mutex_dequeue(lock
, waiter
);
627 waiter
->prio
= task
->prio
;
628 rt_mutex_enqueue(lock
, waiter
);
630 /* [8] Release the task */
631 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
632 put_task_struct(task
);
635 * [9] check_exit_conditions_3 protected by lock->wait_lock.
637 * We must abort the chain walk if there is no lock owner even
638 * in the dead lock detection case, as we have nothing to
639 * follow here. This is the end of the chain we are walking.
641 if (!rt_mutex_owner(lock
)) {
643 * If the requeue [7] above changed the top waiter,
644 * then we need to wake the new top waiter up to try
647 if (prerequeue_top_waiter
!= rt_mutex_top_waiter(lock
))
648 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
649 raw_spin_unlock(&lock
->wait_lock
);
653 /* [10] Grab the next task, i.e. the owner of @lock */
654 task
= rt_mutex_owner(lock
);
655 get_task_struct(task
);
656 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
658 /* [11] requeue the pi waiters if necessary */
659 if (waiter
== rt_mutex_top_waiter(lock
)) {
661 * The waiter became the new top (highest priority)
662 * waiter on the lock. Replace the previous top waiter
663 * in the owner tasks pi waiters list with this waiter
664 * and adjust the priority of the owner.
666 rt_mutex_dequeue_pi(task
, prerequeue_top_waiter
);
667 rt_mutex_enqueue_pi(task
, waiter
);
668 __rt_mutex_adjust_prio(task
);
670 } else if (prerequeue_top_waiter
== waiter
) {
672 * The waiter was the top waiter on the lock, but is
673 * no longer the top prority waiter. Replace waiter in
674 * the owner tasks pi waiters list with the new top
675 * (highest priority) waiter and adjust the priority
677 * The new top waiter is stored in @waiter so that
678 * @waiter == @top_waiter evaluates to true below and
679 * we continue to deboost the rest of the chain.
681 rt_mutex_dequeue_pi(task
, waiter
);
682 waiter
= rt_mutex_top_waiter(lock
);
683 rt_mutex_enqueue_pi(task
, waiter
);
684 __rt_mutex_adjust_prio(task
);
687 * Nothing changed. No need to do any priority
693 * [12] check_exit_conditions_4() protected by task->pi_lock
694 * and lock->wait_lock. The actual decisions are made after we
697 * Check whether the task which owns the current lock is pi
698 * blocked itself. If yes we store a pointer to the lock for
699 * the lock chain change detection above. After we dropped
700 * task->pi_lock next_lock cannot be dereferenced anymore.
702 next_lock
= task_blocked_on_lock(task
);
704 * Store the top waiter of @lock for the end of chain walk
707 top_waiter
= rt_mutex_top_waiter(lock
);
709 /* [13] Drop the locks */
710 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
711 raw_spin_unlock(&lock
->wait_lock
);
714 * Make the actual exit decisions [12], based on the stored
717 * We reached the end of the lock chain. Stop right here. No
718 * point to go back just to figure that out.
724 * If the current waiter is not the top waiter on the lock,
725 * then we can stop the chain walk here if we are not in full
726 * deadlock detection mode.
728 if (!detect_deadlock
&& waiter
!= top_waiter
)
734 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
736 put_task_struct(task
);
742 * Try to take an rt-mutex
744 * Must be called with lock->wait_lock held.
746 * @lock: The lock to be acquired.
747 * @task: The task which wants to acquire the lock
748 * @waiter: The waiter that is queued to the lock's wait list if the
749 * callsite called task_blocked_on_lock(), otherwise NULL
751 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
752 struct rt_mutex_waiter
*waiter
)
757 * Before testing whether we can acquire @lock, we set the
758 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
759 * other tasks which try to modify @lock into the slow path
760 * and they serialize on @lock->wait_lock.
762 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
763 * as explained at the top of this file if and only if:
765 * - There is a lock owner. The caller must fixup the
766 * transient state if it does a trylock or leaves the lock
767 * function due to a signal or timeout.
769 * - @task acquires the lock and there are no other
770 * waiters. This is undone in rt_mutex_set_owner(@task) at
771 * the end of this function.
773 mark_rt_mutex_waiters(lock
);
776 * If @lock has an owner, give up.
778 if (rt_mutex_owner(lock
))
782 * If @waiter != NULL, @task has already enqueued the waiter
783 * into @lock waiter list. If @waiter == NULL then this is a
788 * If waiter is not the highest priority waiter of
791 if (waiter
!= rt_mutex_top_waiter(lock
))
795 * We can acquire the lock. Remove the waiter from the
798 rt_mutex_dequeue(lock
, waiter
);
802 * If the lock has waiters already we check whether @task is
803 * eligible to take over the lock.
805 * If there are no other waiters, @task can acquire
806 * the lock. @task->pi_blocked_on is NULL, so it does
807 * not need to be dequeued.
809 if (rt_mutex_has_waiters(lock
)) {
811 * If @task->prio is greater than or equal to
812 * the top waiter priority (kernel view),
815 if (task
->prio
>= rt_mutex_top_waiter(lock
)->prio
)
819 * The current top waiter stays enqueued. We
820 * don't have to change anything in the lock
825 * No waiters. Take the lock without the
826 * pi_lock dance.@task->pi_blocked_on is NULL
827 * and we have no waiters to enqueue in @task
835 * Clear @task->pi_blocked_on. Requires protection by
836 * @task->pi_lock. Redundant operation for the @waiter == NULL
837 * case, but conditionals are more expensive than a redundant
840 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
841 task
->pi_blocked_on
= NULL
;
843 * Finish the lock acquisition. @task is the new owner. If
844 * other waiters exist we have to insert the highest priority
845 * waiter into @task->pi_waiters list.
847 if (rt_mutex_has_waiters(lock
))
848 rt_mutex_enqueue_pi(task
, rt_mutex_top_waiter(lock
));
849 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
852 /* We got the lock. */
853 debug_rt_mutex_lock(lock
);
856 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
857 * are still waiters or clears it.
859 rt_mutex_set_owner(lock
, task
);
861 rt_mutex_deadlock_account_lock(lock
, task
);
867 * Task blocks on lock.
869 * Prepare waiter and propagate pi chain
871 * This must be called with lock->wait_lock held.
873 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
874 struct rt_mutex_waiter
*waiter
,
875 struct task_struct
*task
,
876 enum rtmutex_chainwalk chwalk
)
878 struct task_struct
*owner
= rt_mutex_owner(lock
);
879 struct rt_mutex_waiter
*top_waiter
= waiter
;
880 struct rt_mutex
*next_lock
;
881 int chain_walk
= 0, res
;
885 * Early deadlock detection. We really don't want the task to
886 * enqueue on itself just to untangle the mess later. It's not
887 * only an optimization. We drop the locks, so another waiter
888 * can come in before the chain walk detects the deadlock. So
889 * the other will detect the deadlock and return -EDEADLOCK,
890 * which is wrong, as the other waiter is not in a deadlock
896 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
897 __rt_mutex_adjust_prio(task
);
900 waiter
->prio
= task
->prio
;
902 /* Get the top priority waiter on the lock */
903 if (rt_mutex_has_waiters(lock
))
904 top_waiter
= rt_mutex_top_waiter(lock
);
905 rt_mutex_enqueue(lock
, waiter
);
907 task
->pi_blocked_on
= waiter
;
909 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
914 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
915 if (waiter
== rt_mutex_top_waiter(lock
)) {
916 rt_mutex_dequeue_pi(owner
, top_waiter
);
917 rt_mutex_enqueue_pi(owner
, waiter
);
919 __rt_mutex_adjust_prio(owner
);
920 if (owner
->pi_blocked_on
)
922 } else if (rt_mutex_cond_detect_deadlock(waiter
, chwalk
)) {
926 /* Store the lock on which owner is blocked or NULL */
927 next_lock
= task_blocked_on_lock(owner
);
929 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
931 * Even if full deadlock detection is on, if the owner is not
932 * blocked itself, we can avoid finding this out in the chain
935 if (!chain_walk
|| !next_lock
)
939 * The owner can't disappear while holding a lock,
940 * so the owner struct is protected by wait_lock.
941 * Gets dropped in rt_mutex_adjust_prio_chain()!
943 get_task_struct(owner
);
945 raw_spin_unlock(&lock
->wait_lock
);
947 res
= rt_mutex_adjust_prio_chain(owner
, chwalk
, lock
,
948 next_lock
, waiter
, task
);
950 raw_spin_lock(&lock
->wait_lock
);
956 * Wake up the next waiter on the lock.
958 * Remove the top waiter from the current tasks pi waiter list and
961 * Called with lock->wait_lock held.
963 static void wakeup_next_waiter(struct rt_mutex
*lock
)
965 struct rt_mutex_waiter
*waiter
;
968 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
970 waiter
= rt_mutex_top_waiter(lock
);
973 * Remove it from current->pi_waiters. We do not adjust a
974 * possible priority boost right now. We execute wakeup in the
975 * boosted mode and go back to normal after releasing
978 rt_mutex_dequeue_pi(current
, waiter
);
981 * As we are waking up the top waiter, and the waiter stays
982 * queued on the lock until it gets the lock, this lock
983 * obviously has waiters. Just set the bit here and this has
984 * the added benefit of forcing all new tasks into the
985 * slow path making sure no task of lower priority than
986 * the top waiter can steal this lock.
988 lock
->owner
= (void *) RT_MUTEX_HAS_WAITERS
;
990 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
993 * It's safe to dereference waiter as it cannot go away as
994 * long as we hold lock->wait_lock. The waiter task needs to
995 * acquire it in order to dequeue the waiter.
997 wake_up_process(waiter
->task
);
1001 * Remove a waiter from a lock and give up
1003 * Must be called with lock->wait_lock held and
1004 * have just failed to try_to_take_rt_mutex().
1006 static void remove_waiter(struct rt_mutex
*lock
,
1007 struct rt_mutex_waiter
*waiter
)
1009 bool is_top_waiter
= (waiter
== rt_mutex_top_waiter(lock
));
1010 struct task_struct
*owner
= rt_mutex_owner(lock
);
1011 struct rt_mutex
*next_lock
;
1012 unsigned long flags
;
1014 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
1015 rt_mutex_dequeue(lock
, waiter
);
1016 current
->pi_blocked_on
= NULL
;
1017 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
1020 * Only update priority if the waiter was the highest priority
1021 * waiter of the lock and there is an owner to update.
1023 if (!owner
|| !is_top_waiter
)
1026 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
1028 rt_mutex_dequeue_pi(owner
, waiter
);
1030 if (rt_mutex_has_waiters(lock
))
1031 rt_mutex_enqueue_pi(owner
, rt_mutex_top_waiter(lock
));
1033 __rt_mutex_adjust_prio(owner
);
1035 /* Store the lock on which owner is blocked or NULL */
1036 next_lock
= task_blocked_on_lock(owner
);
1038 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
1041 * Don't walk the chain, if the owner task is not blocked
1047 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1048 get_task_struct(owner
);
1050 raw_spin_unlock(&lock
->wait_lock
);
1052 rt_mutex_adjust_prio_chain(owner
, RT_MUTEX_MIN_CHAINWALK
, lock
,
1053 next_lock
, NULL
, current
);
1055 raw_spin_lock(&lock
->wait_lock
);
1059 * Recheck the pi chain, in case we got a priority setting
1061 * Called from sched_setscheduler
1063 void rt_mutex_adjust_pi(struct task_struct
*task
)
1065 struct rt_mutex_waiter
*waiter
;
1066 struct rt_mutex
*next_lock
;
1067 unsigned long flags
;
1069 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
1071 waiter
= task
->pi_blocked_on
;
1072 if (!waiter
|| (waiter
->prio
== task
->prio
&&
1073 !dl_prio(task
->prio
))) {
1074 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1077 next_lock
= waiter
->lock
;
1078 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1080 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1081 get_task_struct(task
);
1083 rt_mutex_adjust_prio_chain(task
, RT_MUTEX_MIN_CHAINWALK
, NULL
,
1084 next_lock
, NULL
, task
);
1088 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1089 * @lock: the rt_mutex to take
1090 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1091 * or TASK_UNINTERRUPTIBLE)
1092 * @timeout: the pre-initialized and started timer, or NULL for none
1093 * @waiter: the pre-initialized rt_mutex_waiter
1095 * lock->wait_lock must be held by the caller.
1098 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1099 struct hrtimer_sleeper
*timeout
,
1100 struct rt_mutex_waiter
*waiter
)
1105 /* Try to acquire the lock: */
1106 if (try_to_take_rt_mutex(lock
, current
, waiter
))
1110 * TASK_INTERRUPTIBLE checks for signals and
1111 * timeout. Ignored otherwise.
1113 if (unlikely(state
== TASK_INTERRUPTIBLE
)) {
1114 /* Signal pending? */
1115 if (signal_pending(current
))
1117 if (timeout
&& !timeout
->task
)
1123 raw_spin_unlock(&lock
->wait_lock
);
1125 debug_rt_mutex_print_deadlock(waiter
);
1127 schedule_rt_mutex(lock
);
1129 raw_spin_lock(&lock
->wait_lock
);
1130 set_current_state(state
);
1133 __set_current_state(TASK_RUNNING
);
1137 static void rt_mutex_handle_deadlock(int res
, int detect_deadlock
,
1138 struct rt_mutex_waiter
*w
)
1141 * If the result is not -EDEADLOCK or the caller requested
1142 * deadlock detection, nothing to do here.
1144 if (res
!= -EDEADLOCK
|| detect_deadlock
)
1148 * Yell lowdly and stop the task right here.
1150 rt_mutex_print_deadlock(w
);
1152 set_current_state(TASK_INTERRUPTIBLE
);
1158 * Slow path lock function:
1161 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1162 struct hrtimer_sleeper
*timeout
,
1163 enum rtmutex_chainwalk chwalk
)
1165 struct rt_mutex_waiter waiter
;
1168 debug_rt_mutex_init_waiter(&waiter
);
1169 RB_CLEAR_NODE(&waiter
.pi_tree_entry
);
1170 RB_CLEAR_NODE(&waiter
.tree_entry
);
1172 raw_spin_lock(&lock
->wait_lock
);
1174 /* Try to acquire the lock again: */
1175 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
1176 raw_spin_unlock(&lock
->wait_lock
);
1180 set_current_state(state
);
1182 /* Setup the timer, when timeout != NULL */
1183 if (unlikely(timeout
)) {
1184 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
1185 if (!hrtimer_active(&timeout
->timer
))
1186 timeout
->task
= NULL
;
1189 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, chwalk
);
1192 /* sleep on the mutex */
1193 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
1195 if (unlikely(ret
)) {
1196 __set_current_state(TASK_RUNNING
);
1197 if (rt_mutex_has_waiters(lock
))
1198 remove_waiter(lock
, &waiter
);
1199 rt_mutex_handle_deadlock(ret
, chwalk
, &waiter
);
1203 * try_to_take_rt_mutex() sets the waiter bit
1204 * unconditionally. We might have to fix that up.
1206 fixup_rt_mutex_waiters(lock
);
1208 raw_spin_unlock(&lock
->wait_lock
);
1210 /* Remove pending timer: */
1211 if (unlikely(timeout
))
1212 hrtimer_cancel(&timeout
->timer
);
1214 debug_rt_mutex_free_waiter(&waiter
);
1220 * Slow path try-lock function:
1222 static inline int rt_mutex_slowtrylock(struct rt_mutex
*lock
)
1227 * If the lock already has an owner we fail to get the lock.
1228 * This can be done without taking the @lock->wait_lock as
1229 * it is only being read, and this is a trylock anyway.
1231 if (rt_mutex_owner(lock
))
1235 * The mutex has currently no owner. Lock the wait lock and
1236 * try to acquire the lock.
1238 raw_spin_lock(&lock
->wait_lock
);
1240 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
1243 * try_to_take_rt_mutex() sets the lock waiters bit
1244 * unconditionally. Clean this up.
1246 fixup_rt_mutex_waiters(lock
);
1248 raw_spin_unlock(&lock
->wait_lock
);
1254 * Slow path to release a rt-mutex:
1257 rt_mutex_slowunlock(struct rt_mutex
*lock
)
1259 raw_spin_lock(&lock
->wait_lock
);
1261 debug_rt_mutex_unlock(lock
);
1263 rt_mutex_deadlock_account_unlock(current
);
1266 * We must be careful here if the fast path is enabled. If we
1267 * have no waiters queued we cannot set owner to NULL here
1270 * foo->lock->owner = NULL;
1271 * rtmutex_lock(foo->lock); <- fast path
1272 * free = atomic_dec_and_test(foo->refcnt);
1273 * rtmutex_unlock(foo->lock); <- fast path
1276 * raw_spin_unlock(foo->lock->wait_lock);
1278 * So for the fastpath enabled kernel:
1280 * Nothing can set the waiters bit as long as we hold
1281 * lock->wait_lock. So we do the following sequence:
1283 * owner = rt_mutex_owner(lock);
1284 * clear_rt_mutex_waiters(lock);
1285 * raw_spin_unlock(&lock->wait_lock);
1286 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1290 * The fastpath disabled variant is simple as all access to
1291 * lock->owner is serialized by lock->wait_lock:
1293 * lock->owner = NULL;
1294 * raw_spin_unlock(&lock->wait_lock);
1296 while (!rt_mutex_has_waiters(lock
)) {
1297 /* Drops lock->wait_lock ! */
1298 if (unlock_rt_mutex_safe(lock
) == true)
1300 /* Relock the rtmutex and try again */
1301 raw_spin_lock(&lock
->wait_lock
);
1305 * The wakeup next waiter path does not suffer from the above
1306 * race. See the comments there.
1308 wakeup_next_waiter(lock
);
1310 raw_spin_unlock(&lock
->wait_lock
);
1312 /* Undo pi boosting if necessary: */
1313 rt_mutex_adjust_prio(current
);
1317 * debug aware fast / slowpath lock,trylock,unlock
1319 * The atomic acquire/release ops are compiled away, when either the
1320 * architecture does not support cmpxchg or when debugging is enabled.
1323 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
1324 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1325 struct hrtimer_sleeper
*timeout
,
1326 enum rtmutex_chainwalk chwalk
))
1328 if (likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1329 rt_mutex_deadlock_account_lock(lock
, current
);
1332 return slowfn(lock
, state
, NULL
, RT_MUTEX_MIN_CHAINWALK
);
1336 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
1337 struct hrtimer_sleeper
*timeout
,
1338 enum rtmutex_chainwalk chwalk
,
1339 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1340 struct hrtimer_sleeper
*timeout
,
1341 enum rtmutex_chainwalk chwalk
))
1343 if (chwalk
== RT_MUTEX_MIN_CHAINWALK
&&
1344 likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1345 rt_mutex_deadlock_account_lock(lock
, current
);
1348 return slowfn(lock
, state
, timeout
, chwalk
);
1352 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
1353 int (*slowfn
)(struct rt_mutex
*lock
))
1355 if (likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1356 rt_mutex_deadlock_account_lock(lock
, current
);
1359 return slowfn(lock
);
1363 rt_mutex_fastunlock(struct rt_mutex
*lock
,
1364 void (*slowfn
)(struct rt_mutex
*lock
))
1366 if (likely(rt_mutex_cmpxchg(lock
, current
, NULL
)))
1367 rt_mutex_deadlock_account_unlock(current
);
1373 * rt_mutex_lock - lock a rt_mutex
1375 * @lock: the rt_mutex to be locked
1377 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
1381 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, rt_mutex_slowlock
);
1383 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
1386 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1388 * @lock: the rt_mutex to be locked
1392 * -EINTR when interrupted by a signal
1394 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
)
1398 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
, rt_mutex_slowlock
);
1400 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible
);
1403 * Futex variant with full deadlock detection.
1405 int rt_mutex_timed_futex_lock(struct rt_mutex
*lock
,
1406 struct hrtimer_sleeper
*timeout
)
1410 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1411 RT_MUTEX_FULL_CHAINWALK
,
1416 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1417 * the timeout structure is provided
1420 * @lock: the rt_mutex to be locked
1421 * @timeout: timeout structure or NULL (no timeout)
1425 * -EINTR when interrupted by a signal
1426 * -ETIMEDOUT when the timeout expired
1429 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
)
1433 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1434 RT_MUTEX_MIN_CHAINWALK
,
1437 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
1440 * rt_mutex_trylock - try to lock a rt_mutex
1442 * @lock: the rt_mutex to be locked
1444 * Returns 1 on success and 0 on contention
1446 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
1448 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
1450 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
1453 * rt_mutex_unlock - unlock a rt_mutex
1455 * @lock: the rt_mutex to be unlocked
1457 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
1459 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
1461 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
1464 * rt_mutex_destroy - mark a mutex unusable
1465 * @lock: the mutex to be destroyed
1467 * This function marks the mutex uninitialized, and any subsequent
1468 * use of the mutex is forbidden. The mutex must not be locked when
1469 * this function is called.
1471 void rt_mutex_destroy(struct rt_mutex
*lock
)
1473 WARN_ON(rt_mutex_is_locked(lock
));
1474 #ifdef CONFIG_DEBUG_RT_MUTEXES
1479 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
1482 * __rt_mutex_init - initialize the rt lock
1484 * @lock: the rt lock to be initialized
1486 * Initialize the rt lock to unlocked state.
1488 * Initializing of a locked rt lock is not allowed
1490 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
1493 raw_spin_lock_init(&lock
->wait_lock
);
1494 lock
->waiters
= RB_ROOT
;
1495 lock
->waiters_leftmost
= NULL
;
1497 debug_rt_mutex_init(lock
, name
);
1499 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
1502 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1505 * @lock: the rt_mutex to be locked
1506 * @proxy_owner:the task to set as owner
1508 * No locking. Caller has to do serializing itself
1509 * Special API call for PI-futex support
1511 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
1512 struct task_struct
*proxy_owner
)
1514 __rt_mutex_init(lock
, NULL
);
1515 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
1516 rt_mutex_set_owner(lock
, proxy_owner
);
1517 rt_mutex_deadlock_account_lock(lock
, proxy_owner
);
1521 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1523 * @lock: the rt_mutex to be locked
1525 * No locking. Caller has to do serializing itself
1526 * Special API call for PI-futex support
1528 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
1529 struct task_struct
*proxy_owner
)
1531 debug_rt_mutex_proxy_unlock(lock
);
1532 rt_mutex_set_owner(lock
, NULL
);
1533 rt_mutex_deadlock_account_unlock(proxy_owner
);
1537 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1538 * @lock: the rt_mutex to take
1539 * @waiter: the pre-initialized rt_mutex_waiter
1540 * @task: the task to prepare
1543 * 0 - task blocked on lock
1544 * 1 - acquired the lock for task, caller should wake it up
1547 * Special API call for FUTEX_REQUEUE_PI support.
1549 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1550 struct rt_mutex_waiter
*waiter
,
1551 struct task_struct
*task
)
1555 raw_spin_lock(&lock
->wait_lock
);
1557 if (try_to_take_rt_mutex(lock
, task
, NULL
)) {
1558 raw_spin_unlock(&lock
->wait_lock
);
1562 /* We enforce deadlock detection for futexes */
1563 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
,
1564 RT_MUTEX_FULL_CHAINWALK
);
1566 if (ret
&& !rt_mutex_owner(lock
)) {
1568 * Reset the return value. We might have
1569 * returned with -EDEADLK and the owner
1570 * released the lock while we were walking the
1571 * pi chain. Let the waiter sort it out.
1577 remove_waiter(lock
, waiter
);
1579 raw_spin_unlock(&lock
->wait_lock
);
1581 debug_rt_mutex_print_deadlock(waiter
);
1587 * rt_mutex_next_owner - return the next owner of the lock
1589 * @lock: the rt lock query
1591 * Returns the next owner of the lock or NULL
1593 * Caller has to serialize against other accessors to the lock
1596 * Special API call for PI-futex support
1598 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
1600 if (!rt_mutex_has_waiters(lock
))
1603 return rt_mutex_top_waiter(lock
)->task
;
1607 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1608 * @lock: the rt_mutex we were woken on
1609 * @to: the timeout, null if none. hrtimer should already have
1611 * @waiter: the pre-initialized rt_mutex_waiter
1613 * Complete the lock acquisition started our behalf by another thread.
1617 * <0 - error, one of -EINTR, -ETIMEDOUT
1619 * Special API call for PI-futex requeue support
1621 int rt_mutex_finish_proxy_lock(struct rt_mutex
*lock
,
1622 struct hrtimer_sleeper
*to
,
1623 struct rt_mutex_waiter
*waiter
)
1627 raw_spin_lock(&lock
->wait_lock
);
1629 set_current_state(TASK_INTERRUPTIBLE
);
1631 /* sleep on the mutex */
1632 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1635 remove_waiter(lock
, waiter
);
1638 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1639 * have to fix that up.
1641 fixup_rt_mutex_waiters(lock
);
1643 raw_spin_unlock(&lock
->wait_lock
);