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/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/timer.h>
19 #include "rtmutex_common.h"
22 * lock->owner state tracking:
24 * lock->owner holds the task_struct pointer of the owner. Bit 0
25 * is used to keep track of the "lock has waiters" state.
28 * NULL 0 lock is free (fast acquire possible)
29 * NULL 1 lock is free and has waiters and the top waiter
30 * is going to take the lock*
31 * taskpointer 0 lock is held (fast release possible)
32 * taskpointer 1 lock is held and has waiters**
34 * The fast atomic compare exchange based acquire and release is only
35 * possible when bit 0 of lock->owner is 0.
37 * (*) It also can be a transitional state when grabbing the lock
38 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
39 * we need to set the bit0 before looking at the lock, and the owner may be
40 * NULL in this small time, hence this can be a transitional state.
42 * (**) There is a small time when bit 0 is set but there are no
43 * waiters. This can happen when grabbing the lock in the slow path.
44 * To prevent a cmpxchg of the owner releasing the lock, we need to
45 * set this bit before looking at the lock.
49 rt_mutex_set_owner(struct rt_mutex
*lock
, struct task_struct
*owner
)
51 unsigned long val
= (unsigned long)owner
;
53 if (rt_mutex_has_waiters(lock
))
54 val
|= RT_MUTEX_HAS_WAITERS
;
56 lock
->owner
= (struct task_struct
*)val
;
59 static inline void clear_rt_mutex_waiters(struct rt_mutex
*lock
)
61 lock
->owner
= (struct task_struct
*)
62 ((unsigned long)lock
->owner
& ~RT_MUTEX_HAS_WAITERS
);
65 static void fixup_rt_mutex_waiters(struct rt_mutex
*lock
)
67 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
69 if (rt_mutex_has_waiters(lock
))
73 * The rbtree has no waiters enqueued, now make sure that the
74 * lock->owner still has the waiters bit set, otherwise the
75 * following can happen:
81 * l->owner = T1 | HAS_WAITERS;
89 * l->owner = T1 | HAS_WAITERS;
94 * signal(->T2) signal(->T3)
101 * ==> wait list is empty
105 * fixup_rt_mutex_waiters()
106 * if (wait_list_empty(l) {
108 * owner = l->owner & ~HAS_WAITERS;
112 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
113 * if (wait_list_empty(l) {
114 * owner = l->owner & ~HAS_WAITERS;
115 * cmpxchg(l->owner, T1, NULL)
116 * ===> Success (l->owner = NULL)
122 * With the check for the waiter bit in place T3 on CPU2 will not
123 * overwrite. All tasks fiddling with the waiters bit are
124 * serialized by l->lock, so nothing else can modify the waiters
125 * bit. If the bit is set then nothing can change l->owner either
126 * so the simple RMW is safe. The cmpxchg() will simply fail if it
127 * happens in the middle of the RMW because the waiters bit is
130 owner
= ACCESS_ONCE(*p
);
131 if (owner
& RT_MUTEX_HAS_WAITERS
)
132 ACCESS_ONCE(*p
) = owner
& ~RT_MUTEX_HAS_WAITERS
;
136 * We can speed up the acquire/release, if the architecture
137 * supports cmpxchg and if there's no debugging state to be set up
139 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
140 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
141 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
143 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
147 } while (cmpxchg(p
, owner
, owner
| RT_MUTEX_HAS_WAITERS
) != owner
);
151 * Safe fastpath aware unlock:
152 * 1) Clear the waiters bit
153 * 2) Drop lock->wait_lock
154 * 3) Try to unlock the lock with cmpxchg
156 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
)
157 __releases(lock
->wait_lock
)
159 struct task_struct
*owner
= rt_mutex_owner(lock
);
161 clear_rt_mutex_waiters(lock
);
162 raw_spin_unlock(&lock
->wait_lock
);
164 * If a new waiter comes in between the unlock and the cmpxchg
165 * we have two situations:
169 * cmpxchg(p, owner, 0) == owner
170 * mark_rt_mutex_waiters(lock);
176 * mark_rt_mutex_waiters(lock);
178 * cmpxchg(p, owner, 0) != owner
187 return rt_mutex_cmpxchg(lock
, owner
, NULL
);
191 # define rt_mutex_cmpxchg(l,c,n) (0)
192 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
194 lock
->owner
= (struct task_struct
*)
195 ((unsigned long)lock
->owner
| RT_MUTEX_HAS_WAITERS
);
199 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
201 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
)
202 __releases(lock
->wait_lock
)
205 raw_spin_unlock(&lock
->wait_lock
);
211 * Calculate task priority from the waiter list priority
213 * Return task->normal_prio when the waiter list is empty or when
214 * the waiter is not allowed to do priority boosting
216 int rt_mutex_getprio(struct task_struct
*task
)
218 if (likely(!task_has_pi_waiters(task
)))
219 return task
->normal_prio
;
221 return min(task_top_pi_waiter(task
)->pi_list_entry
.prio
,
226 * Adjust the priority of a task, after its pi_waiters got modified.
228 * This can be both boosting and unboosting. task->pi_lock must be held.
230 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
232 int prio
= rt_mutex_getprio(task
);
234 if (task
->prio
!= prio
)
235 rt_mutex_setprio(task
, prio
);
239 * Adjust task priority (undo boosting). Called from the exit path of
240 * rt_mutex_slowunlock() and rt_mutex_slowlock().
242 * (Note: We do this outside of the protection of lock->wait_lock to
243 * allow the lock to be taken while or before we readjust the priority
244 * of task. We do not use the spin_xx_mutex() variants here as we are
245 * outside of the debug path.)
247 static void rt_mutex_adjust_prio(struct task_struct
*task
)
251 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
252 __rt_mutex_adjust_prio(task
);
253 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
257 * Max number of times we'll walk the boosting chain:
259 int max_lock_depth
= 1024;
261 static inline struct rt_mutex
*task_blocked_on_lock(struct task_struct
*p
)
263 return p
->pi_blocked_on
? p
->pi_blocked_on
->lock
: NULL
;
267 * Adjust the priority chain. Also used for deadlock detection.
268 * Decreases task's usage by one - may thus free the task.
269 * Returns 0 or -EDEADLK.
271 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
273 struct rt_mutex
*orig_lock
,
274 struct rt_mutex
*next_lock
,
275 struct rt_mutex_waiter
*orig_waiter
,
276 struct task_struct
*top_task
)
278 struct rt_mutex
*lock
;
279 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
280 int detect_deadlock
, ret
= 0, depth
= 0;
283 detect_deadlock
= debug_rt_mutex_detect_deadlock(orig_waiter
,
287 * The (de)boosting is a step by step approach with a lot of
288 * pitfalls. We want this to be preemptible and we want hold a
289 * maximum of two locks per step. So we have to check
290 * carefully whether things change under us.
293 if (++depth
> max_lock_depth
) {
297 * Print this only once. If the admin changes the limit,
298 * print a new message when reaching the limit again.
300 if (prev_max
!= max_lock_depth
) {
301 prev_max
= max_lock_depth
;
302 printk(KERN_WARNING
"Maximum lock depth %d reached "
303 "task: %s (%d)\n", max_lock_depth
,
304 top_task
->comm
, task_pid_nr(top_task
));
306 put_task_struct(task
);
312 * Task can not go away as we did a get_task() before !
314 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
316 waiter
= task
->pi_blocked_on
;
318 * Check whether the end of the boosting chain has been
319 * reached or the state of the chain has changed while we
326 * Check the orig_waiter state. After we dropped the locks,
327 * the previous owner of the lock might have released the lock.
329 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
333 * We dropped all locks after taking a refcount on @task, so
334 * the task might have moved on in the lock chain or even left
335 * the chain completely and blocks now on an unrelated lock or
338 * We stored the lock on which @task was blocked in @next_lock,
339 * so we can detect the chain change.
341 if (next_lock
!= waiter
->lock
)
345 * Drop out, when the task has no waiters. Note,
346 * top_waiter can be NULL, when we are in the deboosting
350 if (!task_has_pi_waiters(task
))
353 * If deadlock detection is off, we stop here if we
354 * are not the top pi waiter of the task.
356 if (!detect_deadlock
&& top_waiter
!= task_top_pi_waiter(task
))
361 * When deadlock detection is off then we check, if further
362 * priority adjustment is necessary.
364 if (!detect_deadlock
&& waiter
->list_entry
.prio
== task
->prio
)
368 if (!raw_spin_trylock(&lock
->wait_lock
)) {
369 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
375 * Deadlock detection. If the lock is the same as the original
376 * lock which caused us to walk the lock chain or if the
377 * current lock is owned by the task which initiated the chain
378 * walk, we detected a deadlock.
380 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
381 debug_rt_mutex_deadlock(deadlock_detect
, orig_waiter
, lock
);
382 raw_spin_unlock(&lock
->wait_lock
);
387 top_waiter
= rt_mutex_top_waiter(lock
);
389 /* Requeue the waiter */
390 plist_del(&waiter
->list_entry
, &lock
->wait_list
);
391 waiter
->list_entry
.prio
= task
->prio
;
392 plist_add(&waiter
->list_entry
, &lock
->wait_list
);
394 /* Release the task */
395 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
396 if (!rt_mutex_owner(lock
)) {
398 * If the requeue above changed the top waiter, then we need
399 * to wake the new top waiter up to try to get the lock.
402 if (top_waiter
!= rt_mutex_top_waiter(lock
))
403 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
404 raw_spin_unlock(&lock
->wait_lock
);
407 put_task_struct(task
);
409 /* Grab the next task */
410 task
= rt_mutex_owner(lock
);
411 get_task_struct(task
);
412 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
414 if (waiter
== rt_mutex_top_waiter(lock
)) {
415 /* Boost the owner */
416 plist_del(&top_waiter
->pi_list_entry
, &task
->pi_waiters
);
417 waiter
->pi_list_entry
.prio
= waiter
->list_entry
.prio
;
418 plist_add(&waiter
->pi_list_entry
, &task
->pi_waiters
);
419 __rt_mutex_adjust_prio(task
);
421 } else if (top_waiter
== waiter
) {
422 /* Deboost the owner */
423 plist_del(&waiter
->pi_list_entry
, &task
->pi_waiters
);
424 waiter
= rt_mutex_top_waiter(lock
);
425 waiter
->pi_list_entry
.prio
= waiter
->list_entry
.prio
;
426 plist_add(&waiter
->pi_list_entry
, &task
->pi_waiters
);
427 __rt_mutex_adjust_prio(task
);
431 * Check whether the task which owns the current lock is pi
432 * blocked itself. If yes we store a pointer to the lock for
433 * the lock chain change detection above. After we dropped
434 * task->pi_lock next_lock cannot be dereferenced anymore.
436 next_lock
= task_blocked_on_lock(task
);
438 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
440 top_waiter
= rt_mutex_top_waiter(lock
);
441 raw_spin_unlock(&lock
->wait_lock
);
444 * We reached the end of the lock chain. Stop right here. No
445 * point to go back just to figure that out.
450 if (!detect_deadlock
&& waiter
!= top_waiter
)
456 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
458 put_task_struct(task
);
464 * Try to take an rt-mutex
466 * Must be called with lock->wait_lock held.
468 * @lock: the lock to be acquired.
469 * @task: the task which wants to acquire the lock
470 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
472 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
473 struct rt_mutex_waiter
*waiter
)
476 * We have to be careful here if the atomic speedups are
477 * enabled, such that, when
478 * - no other waiter is on the lock
479 * - the lock has been released since we did the cmpxchg
480 * the lock can be released or taken while we are doing the
481 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
483 * The atomic acquire/release aware variant of
484 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
485 * the WAITERS bit, the atomic release / acquire can not
486 * happen anymore and lock->wait_lock protects us from the
489 * Note, that this might set lock->owner =
490 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
491 * any more. This is fixed up when we take the ownership.
492 * This is the transitional state explained at the top of this file.
494 mark_rt_mutex_waiters(lock
);
496 if (rt_mutex_owner(lock
))
500 * It will get the lock because of one of these conditions:
501 * 1) there is no waiter
502 * 2) higher priority than waiters
503 * 3) it is top waiter
505 if (rt_mutex_has_waiters(lock
)) {
506 if (task
->prio
>= rt_mutex_top_waiter(lock
)->list_entry
.prio
) {
507 if (!waiter
|| waiter
!= rt_mutex_top_waiter(lock
))
512 if (waiter
|| rt_mutex_has_waiters(lock
)) {
514 struct rt_mutex_waiter
*top
;
516 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
518 /* remove the queued waiter. */
520 plist_del(&waiter
->list_entry
, &lock
->wait_list
);
521 task
->pi_blocked_on
= NULL
;
525 * We have to enqueue the top waiter(if it exists) into
526 * task->pi_waiters list.
528 if (rt_mutex_has_waiters(lock
)) {
529 top
= rt_mutex_top_waiter(lock
);
530 top
->pi_list_entry
.prio
= top
->list_entry
.prio
;
531 plist_add(&top
->pi_list_entry
, &task
->pi_waiters
);
533 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
536 /* We got the lock. */
537 debug_rt_mutex_lock(lock
);
539 rt_mutex_set_owner(lock
, task
);
541 rt_mutex_deadlock_account_lock(lock
, task
);
547 * Task blocks on lock.
549 * Prepare waiter and propagate pi chain
551 * This must be called with lock->wait_lock held.
553 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
554 struct rt_mutex_waiter
*waiter
,
555 struct task_struct
*task
,
558 struct task_struct
*owner
= rt_mutex_owner(lock
);
559 struct rt_mutex_waiter
*top_waiter
= waiter
;
560 struct rt_mutex
*next_lock
;
561 int chain_walk
= 0, res
;
565 * Early deadlock detection. We really don't want the task to
566 * enqueue on itself just to untangle the mess later. It's not
567 * only an optimization. We drop the locks, so another waiter
568 * can come in before the chain walk detects the deadlock. So
569 * the other will detect the deadlock and return -EDEADLOCK,
570 * which is wrong, as the other waiter is not in a deadlock
576 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
577 __rt_mutex_adjust_prio(task
);
580 plist_node_init(&waiter
->list_entry
, task
->prio
);
581 plist_node_init(&waiter
->pi_list_entry
, task
->prio
);
583 /* Get the top priority waiter on the lock */
584 if (rt_mutex_has_waiters(lock
))
585 top_waiter
= rt_mutex_top_waiter(lock
);
586 plist_add(&waiter
->list_entry
, &lock
->wait_list
);
588 task
->pi_blocked_on
= waiter
;
590 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
595 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
596 if (waiter
== rt_mutex_top_waiter(lock
)) {
597 plist_del(&top_waiter
->pi_list_entry
, &owner
->pi_waiters
);
598 plist_add(&waiter
->pi_list_entry
, &owner
->pi_waiters
);
600 __rt_mutex_adjust_prio(owner
);
601 if (owner
->pi_blocked_on
)
603 } else if (debug_rt_mutex_detect_deadlock(waiter
, detect_deadlock
)) {
607 /* Store the lock on which owner is blocked or NULL */
608 next_lock
= task_blocked_on_lock(owner
);
610 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
612 * Even if full deadlock detection is on, if the owner is not
613 * blocked itself, we can avoid finding this out in the chain
616 if (!chain_walk
|| !next_lock
)
620 * The owner can't disappear while holding a lock,
621 * so the owner struct is protected by wait_lock.
622 * Gets dropped in rt_mutex_adjust_prio_chain()!
624 get_task_struct(owner
);
626 raw_spin_unlock(&lock
->wait_lock
);
628 res
= rt_mutex_adjust_prio_chain(owner
, detect_deadlock
, lock
,
629 next_lock
, waiter
, task
);
631 raw_spin_lock(&lock
->wait_lock
);
637 * Wake up the next waiter on the lock.
639 * Remove the top waiter from the current tasks pi waiter list and
642 * Called with lock->wait_lock held.
644 static void wakeup_next_waiter(struct rt_mutex
*lock
)
646 struct rt_mutex_waiter
*waiter
;
649 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
651 waiter
= rt_mutex_top_waiter(lock
);
654 * Remove it from current->pi_waiters. We do not adjust a
655 * possible priority boost right now. We execute wakeup in the
656 * boosted mode and go back to normal after releasing
659 plist_del(&waiter
->pi_list_entry
, ¤t
->pi_waiters
);
662 * As we are waking up the top waiter, and the waiter stays
663 * queued on the lock until it gets the lock, this lock
664 * obviously has waiters. Just set the bit here and this has
665 * the added benefit of forcing all new tasks into the
666 * slow path making sure no task of lower priority than
667 * the top waiter can steal this lock.
669 lock
->owner
= (void *) RT_MUTEX_HAS_WAITERS
;
671 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
674 * It's safe to dereference waiter as it cannot go away as
675 * long as we hold lock->wait_lock. The waiter task needs to
676 * acquire it in order to dequeue the waiter.
678 wake_up_process(waiter
->task
);
682 * Remove a waiter from a lock and give up
684 * Must be called with lock->wait_lock held and
685 * have just failed to try_to_take_rt_mutex().
687 static void remove_waiter(struct rt_mutex
*lock
,
688 struct rt_mutex_waiter
*waiter
)
690 int first
= (waiter
== rt_mutex_top_waiter(lock
));
691 struct task_struct
*owner
= rt_mutex_owner(lock
);
692 struct rt_mutex
*next_lock
= NULL
;
695 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
696 plist_del(&waiter
->list_entry
, &lock
->wait_list
);
697 current
->pi_blocked_on
= NULL
;
698 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
705 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
707 plist_del(&waiter
->pi_list_entry
, &owner
->pi_waiters
);
709 if (rt_mutex_has_waiters(lock
)) {
710 struct rt_mutex_waiter
*next
;
712 next
= rt_mutex_top_waiter(lock
);
713 plist_add(&next
->pi_list_entry
, &owner
->pi_waiters
);
715 __rt_mutex_adjust_prio(owner
);
717 /* Store the lock on which owner is blocked or NULL */
718 next_lock
= task_blocked_on_lock(owner
);
720 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
723 WARN_ON(!plist_node_empty(&waiter
->pi_list_entry
));
728 /* gets dropped in rt_mutex_adjust_prio_chain()! */
729 get_task_struct(owner
);
731 raw_spin_unlock(&lock
->wait_lock
);
733 rt_mutex_adjust_prio_chain(owner
, 0, lock
, next_lock
, NULL
, current
);
735 raw_spin_lock(&lock
->wait_lock
);
739 * Recheck the pi chain, in case we got a priority setting
741 * Called from sched_setscheduler
743 void rt_mutex_adjust_pi(struct task_struct
*task
)
745 struct rt_mutex_waiter
*waiter
;
746 struct rt_mutex
*next_lock
;
749 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
751 waiter
= task
->pi_blocked_on
;
752 if (!waiter
|| waiter
->list_entry
.prio
== task
->prio
) {
753 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
756 next_lock
= waiter
->lock
;
757 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
759 /* gets dropped in rt_mutex_adjust_prio_chain()! */
760 get_task_struct(task
);
762 rt_mutex_adjust_prio_chain(task
, 0, NULL
, next_lock
, NULL
, task
);
766 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
767 * @lock: the rt_mutex to take
768 * @state: the state the task should block in (TASK_INTERRUPTIBLE
769 * or TASK_UNINTERRUPTIBLE)
770 * @timeout: the pre-initialized and started timer, or NULL for none
771 * @waiter: the pre-initialized rt_mutex_waiter
773 * lock->wait_lock must be held by the caller.
776 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
777 struct hrtimer_sleeper
*timeout
,
778 struct rt_mutex_waiter
*waiter
)
783 /* Try to acquire the lock: */
784 if (try_to_take_rt_mutex(lock
, current
, waiter
))
788 * TASK_INTERRUPTIBLE checks for signals and
789 * timeout. Ignored otherwise.
791 if (unlikely(state
== TASK_INTERRUPTIBLE
)) {
792 /* Signal pending? */
793 if (signal_pending(current
))
795 if (timeout
&& !timeout
->task
)
801 raw_spin_unlock(&lock
->wait_lock
);
803 debug_rt_mutex_print_deadlock(waiter
);
805 schedule_rt_mutex(lock
);
807 raw_spin_lock(&lock
->wait_lock
);
808 set_current_state(state
);
814 static void rt_mutex_handle_deadlock(int res
, int detect_deadlock
,
815 struct rt_mutex_waiter
*w
)
818 * If the result is not -EDEADLOCK or the caller requested
819 * deadlock detection, nothing to do here.
821 if (res
!= -EDEADLOCK
|| detect_deadlock
)
825 * Yell lowdly and stop the task right here.
827 rt_mutex_print_deadlock(w
);
829 set_current_state(TASK_INTERRUPTIBLE
);
835 * Slow path lock function:
838 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
839 struct hrtimer_sleeper
*timeout
,
842 struct rt_mutex_waiter waiter
;
845 debug_rt_mutex_init_waiter(&waiter
);
847 raw_spin_lock(&lock
->wait_lock
);
849 /* Try to acquire the lock again: */
850 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
851 raw_spin_unlock(&lock
->wait_lock
);
855 set_current_state(state
);
857 /* Setup the timer, when timeout != NULL */
858 if (unlikely(timeout
)) {
859 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
860 if (!hrtimer_active(&timeout
->timer
))
861 timeout
->task
= NULL
;
864 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, detect_deadlock
);
867 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
869 set_current_state(TASK_RUNNING
);
872 remove_waiter(lock
, &waiter
);
873 rt_mutex_handle_deadlock(ret
, detect_deadlock
, &waiter
);
877 * try_to_take_rt_mutex() sets the waiter bit
878 * unconditionally. We might have to fix that up.
880 fixup_rt_mutex_waiters(lock
);
882 raw_spin_unlock(&lock
->wait_lock
);
884 /* Remove pending timer: */
885 if (unlikely(timeout
))
886 hrtimer_cancel(&timeout
->timer
);
888 debug_rt_mutex_free_waiter(&waiter
);
894 * Slow path try-lock function:
897 rt_mutex_slowtrylock(struct rt_mutex
*lock
)
901 raw_spin_lock(&lock
->wait_lock
);
903 if (likely(rt_mutex_owner(lock
) != current
)) {
905 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
907 * try_to_take_rt_mutex() sets the lock waiters
908 * bit unconditionally. Clean this up.
910 fixup_rt_mutex_waiters(lock
);
913 raw_spin_unlock(&lock
->wait_lock
);
919 * Slow path to release a rt-mutex:
922 rt_mutex_slowunlock(struct rt_mutex
*lock
)
924 raw_spin_lock(&lock
->wait_lock
);
926 debug_rt_mutex_unlock(lock
);
928 rt_mutex_deadlock_account_unlock(current
);
931 * We must be careful here if the fast path is enabled. If we
932 * have no waiters queued we cannot set owner to NULL here
935 * foo->lock->owner = NULL;
936 * rtmutex_lock(foo->lock); <- fast path
937 * free = atomic_dec_and_test(foo->refcnt);
938 * rtmutex_unlock(foo->lock); <- fast path
941 * raw_spin_unlock(foo->lock->wait_lock);
943 * So for the fastpath enabled kernel:
945 * Nothing can set the waiters bit as long as we hold
946 * lock->wait_lock. So we do the following sequence:
948 * owner = rt_mutex_owner(lock);
949 * clear_rt_mutex_waiters(lock);
950 * raw_spin_unlock(&lock->wait_lock);
951 * if (cmpxchg(&lock->owner, owner, 0) == owner)
955 * The fastpath disabled variant is simple as all access to
956 * lock->owner is serialized by lock->wait_lock:
958 * lock->owner = NULL;
959 * raw_spin_unlock(&lock->wait_lock);
961 while (!rt_mutex_has_waiters(lock
)) {
962 /* Drops lock->wait_lock ! */
963 if (unlock_rt_mutex_safe(lock
) == true)
965 /* Relock the rtmutex and try again */
966 raw_spin_lock(&lock
->wait_lock
);
970 * The wakeup next waiter path does not suffer from the above
971 * race. See the comments there.
973 wakeup_next_waiter(lock
);
975 raw_spin_unlock(&lock
->wait_lock
);
977 /* Undo pi boosting if necessary: */
978 rt_mutex_adjust_prio(current
);
982 * debug aware fast / slowpath lock,trylock,unlock
984 * The atomic acquire/release ops are compiled away, when either the
985 * architecture does not support cmpxchg or when debugging is enabled.
988 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
990 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
991 struct hrtimer_sleeper
*timeout
,
992 int detect_deadlock
))
994 if (!detect_deadlock
&& likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
995 rt_mutex_deadlock_account_lock(lock
, current
);
998 return slowfn(lock
, state
, NULL
, detect_deadlock
);
1002 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
1003 struct hrtimer_sleeper
*timeout
, int detect_deadlock
,
1004 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1005 struct hrtimer_sleeper
*timeout
,
1006 int detect_deadlock
))
1008 if (!detect_deadlock
&& likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1009 rt_mutex_deadlock_account_lock(lock
, current
);
1012 return slowfn(lock
, state
, timeout
, detect_deadlock
);
1016 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
1017 int (*slowfn
)(struct rt_mutex
*lock
))
1019 if (likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
1020 rt_mutex_deadlock_account_lock(lock
, current
);
1023 return slowfn(lock
);
1027 rt_mutex_fastunlock(struct rt_mutex
*lock
,
1028 void (*slowfn
)(struct rt_mutex
*lock
))
1030 if (likely(rt_mutex_cmpxchg(lock
, current
, NULL
)))
1031 rt_mutex_deadlock_account_unlock(current
);
1037 * rt_mutex_lock - lock a rt_mutex
1039 * @lock: the rt_mutex to be locked
1041 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
1045 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, 0, rt_mutex_slowlock
);
1047 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
1050 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1052 * @lock: the rt_mutex to be locked
1053 * @detect_deadlock: deadlock detection on/off
1057 * -EINTR when interrupted by a signal
1058 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1060 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
,
1061 int detect_deadlock
)
1065 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
,
1066 detect_deadlock
, rt_mutex_slowlock
);
1068 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible
);
1071 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1072 * the timeout structure is provided
1075 * @lock: the rt_mutex to be locked
1076 * @timeout: timeout structure or NULL (no timeout)
1077 * @detect_deadlock: deadlock detection on/off
1081 * -EINTR when interrupted by a signal
1082 * -ETIMEDOUT when the timeout expired
1083 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1086 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
,
1087 int detect_deadlock
)
1091 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1092 detect_deadlock
, rt_mutex_slowlock
);
1094 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
1097 * rt_mutex_trylock - try to lock a rt_mutex
1099 * @lock: the rt_mutex to be locked
1101 * Returns 1 on success and 0 on contention
1103 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
1105 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
1107 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
1110 * rt_mutex_unlock - unlock a rt_mutex
1112 * @lock: the rt_mutex to be unlocked
1114 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
1116 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
1118 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
1121 * rt_mutex_destroy - mark a mutex unusable
1122 * @lock: the mutex to be destroyed
1124 * This function marks the mutex uninitialized, and any subsequent
1125 * use of the mutex is forbidden. The mutex must not be locked when
1126 * this function is called.
1128 void rt_mutex_destroy(struct rt_mutex
*lock
)
1130 WARN_ON(rt_mutex_is_locked(lock
));
1131 #ifdef CONFIG_DEBUG_RT_MUTEXES
1136 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
1139 * __rt_mutex_init - initialize the rt lock
1141 * @lock: the rt lock to be initialized
1143 * Initialize the rt lock to unlocked state.
1145 * Initializing of a locked rt lock is not allowed
1147 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
1150 raw_spin_lock_init(&lock
->wait_lock
);
1151 plist_head_init(&lock
->wait_list
);
1153 debug_rt_mutex_init(lock
, name
);
1155 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
1158 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1161 * @lock: the rt_mutex to be locked
1162 * @proxy_owner:the task to set as owner
1164 * No locking. Caller has to do serializing itself
1165 * Special API call for PI-futex support
1167 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
1168 struct task_struct
*proxy_owner
)
1170 __rt_mutex_init(lock
, NULL
);
1171 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
1172 rt_mutex_set_owner(lock
, proxy_owner
);
1173 rt_mutex_deadlock_account_lock(lock
, proxy_owner
);
1177 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1179 * @lock: the rt_mutex to be locked
1181 * No locking. Caller has to do serializing itself
1182 * Special API call for PI-futex support
1184 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
1185 struct task_struct
*proxy_owner
)
1187 debug_rt_mutex_proxy_unlock(lock
);
1188 rt_mutex_set_owner(lock
, NULL
);
1189 rt_mutex_deadlock_account_unlock(proxy_owner
);
1193 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1194 * @lock: the rt_mutex to take
1195 * @waiter: the pre-initialized rt_mutex_waiter
1196 * @task: the task to prepare
1197 * @detect_deadlock: perform deadlock detection (1) or not (0)
1200 * 0 - task blocked on lock
1201 * 1 - acquired the lock for task, caller should wake it up
1204 * Special API call for FUTEX_REQUEUE_PI support.
1206 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1207 struct rt_mutex_waiter
*waiter
,
1208 struct task_struct
*task
, int detect_deadlock
)
1212 raw_spin_lock(&lock
->wait_lock
);
1214 if (try_to_take_rt_mutex(lock
, task
, NULL
)) {
1215 raw_spin_unlock(&lock
->wait_lock
);
1219 /* We enforce deadlock detection for futexes */
1220 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
, 1);
1222 if (ret
&& !rt_mutex_owner(lock
)) {
1224 * Reset the return value. We might have
1225 * returned with -EDEADLK and the owner
1226 * released the lock while we were walking the
1227 * pi chain. Let the waiter sort it out.
1233 remove_waiter(lock
, waiter
);
1235 raw_spin_unlock(&lock
->wait_lock
);
1237 debug_rt_mutex_print_deadlock(waiter
);
1243 * rt_mutex_next_owner - return the next owner of the lock
1245 * @lock: the rt lock query
1247 * Returns the next owner of the lock or NULL
1249 * Caller has to serialize against other accessors to the lock
1252 * Special API call for PI-futex support
1254 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
1256 if (!rt_mutex_has_waiters(lock
))
1259 return rt_mutex_top_waiter(lock
)->task
;
1263 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1264 * @lock: the rt_mutex we were woken on
1265 * @to: the timeout, null if none. hrtimer should already have
1267 * @waiter: the pre-initialized rt_mutex_waiter
1268 * @detect_deadlock: perform deadlock detection (1) or not (0)
1270 * Complete the lock acquisition started our behalf by another thread.
1274 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1276 * Special API call for PI-futex requeue support
1278 int rt_mutex_finish_proxy_lock(struct rt_mutex
*lock
,
1279 struct hrtimer_sleeper
*to
,
1280 struct rt_mutex_waiter
*waiter
,
1281 int detect_deadlock
)
1285 raw_spin_lock(&lock
->wait_lock
);
1287 set_current_state(TASK_INTERRUPTIBLE
);
1289 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1291 set_current_state(TASK_RUNNING
);
1294 remove_waiter(lock
, waiter
);
1297 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1298 * have to fix that up.
1300 fixup_rt_mutex_waiters(lock
);
1302 raw_spin_unlock(&lock
->wait_lock
);