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/timer.h>
18 #include "rtmutex_common.h"
21 * lock->owner state tracking:
23 * lock->owner holds the task_struct pointer of the owner. Bit 0
24 * is used to keep track of the "lock has waiters" state.
27 * NULL 0 lock is free (fast acquire possible)
28 * NULL 1 lock is free and has waiters and the top waiter
29 * is going to take the lock*
30 * taskpointer 0 lock is held (fast release possible)
31 * taskpointer 1 lock is held and has waiters**
33 * The fast atomic compare exchange based acquire and release is only
34 * possible when bit 0 of lock->owner is 0.
36 * (*) It also can be a transitional state when grabbing the lock
37 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
38 * we need to set the bit0 before looking at the lock, and the owner may be
39 * NULL in this small time, hence this can be a transitional state.
41 * (**) There is a small time when bit 0 is set but there are no
42 * waiters. This can happen when grabbing the lock in the slow path.
43 * To prevent a cmpxchg of the owner releasing the lock, we need to
44 * set this bit before looking at the lock.
48 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
50 unsigned long val = (unsigned long)owner;
52 if (rt_mutex_has_waiters(lock))
53 val |= RT_MUTEX_HAS_WAITERS;
55 lock->owner = (struct task_struct *)val;
58 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
60 lock->owner = (struct task_struct *)
61 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
64 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
66 unsigned long owner, *p = (unsigned long *) &lock->owner;
68 if (rt_mutex_has_waiters(lock))
72 * The rbtree has no waiters enqueued, now make sure that the
73 * lock->owner still has the waiters bit set, otherwise the
74 * following can happen:
80 * l->owner = T1 | HAS_WAITERS;
88 * l->owner = T1 | HAS_WAITERS;
93 * signal(->T2) signal(->T3)
100 * ==> wait list is empty
104 * fixup_rt_mutex_waiters()
105 * if (wait_list_empty(l) {
107 * owner = l->owner & ~HAS_WAITERS;
111 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
112 * if (wait_list_empty(l) {
113 * owner = l->owner & ~HAS_WAITERS;
114 * cmpxchg(l->owner, T1, NULL)
115 * ===> Success (l->owner = NULL)
121 * With the check for the waiter bit in place T3 on CPU2 will not
122 * overwrite. All tasks fiddling with the waiters bit are
123 * serialized by l->lock, so nothing else can modify the waiters
124 * bit. If the bit is set then nothing can change l->owner either
125 * so the simple RMW is safe. The cmpxchg() will simply fail if it
126 * happens in the middle of the RMW because the waiters bit is
129 owner = ACCESS_ONCE(*p);
130 if (owner & RT_MUTEX_HAS_WAITERS)
131 ACCESS_ONCE(*p) = owner & ~RT_MUTEX_HAS_WAITERS;
135 * We can speed up the acquire/release, if the architecture
136 * supports cmpxchg and if there's no debugging state to be set up
138 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
139 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
140 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
142 unsigned long owner, *p = (unsigned long *) &lock->owner;
146 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
150 * Safe fastpath aware unlock:
151 * 1) Clear the waiters bit
152 * 2) Drop lock->wait_lock
153 * 3) Try to unlock the lock with cmpxchg
155 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
156 __releases(lock->wait_lock)
158 struct task_struct *owner = rt_mutex_owner(lock);
160 clear_rt_mutex_waiters(lock);
161 raw_spin_unlock(&lock->wait_lock);
163 * If a new waiter comes in between the unlock and the cmpxchg
164 * we have two situations:
168 * cmpxchg(p, owner, 0) == owner
169 * mark_rt_mutex_waiters(lock);
175 * mark_rt_mutex_waiters(lock);
177 * cmpxchg(p, owner, 0) != owner
186 return rt_mutex_cmpxchg(lock, owner, NULL);
190 # define rt_mutex_cmpxchg(l,c,n) (0)
191 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
193 lock->owner = (struct task_struct *)
194 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
198 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
200 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
201 __releases(lock->wait_lock)
204 raw_spin_unlock(&lock->wait_lock);
210 * Calculate task priority from the waiter list priority
212 * Return task->normal_prio when the waiter list is empty or when
213 * the waiter is not allowed to do priority boosting
215 int rt_mutex_getprio(struct task_struct *task)
217 if (likely(!task_has_pi_waiters(task)))
218 return task->normal_prio;
220 return min(task_top_pi_waiter(task)->pi_list_entry.prio,
225 * Adjust the priority of a task, after its pi_waiters got modified.
227 * This can be both boosting and unboosting. task->pi_lock must be held.
229 static void __rt_mutex_adjust_prio(struct task_struct *task)
231 int prio = rt_mutex_getprio(task);
233 if (task->prio != prio)
234 rt_mutex_setprio(task, prio);
238 * Adjust task priority (undo boosting). Called from the exit path of
239 * rt_mutex_slowunlock() and rt_mutex_slowlock().
241 * (Note: We do this outside of the protection of lock->wait_lock to
242 * allow the lock to be taken while or before we readjust the priority
243 * of task. We do not use the spin_xx_mutex() variants here as we are
244 * outside of the debug path.)
246 static void rt_mutex_adjust_prio(struct task_struct *task)
250 raw_spin_lock_irqsave(&task->pi_lock, flags);
251 __rt_mutex_adjust_prio(task);
252 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
256 * Max number of times we'll walk the boosting chain:
258 int max_lock_depth = 1024;
260 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
262 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
266 * Adjust the priority chain. Also used for deadlock detection.
267 * Decreases task's usage by one - may thus free the task.
269 * @task: the task owning the mutex (owner) for which a chain walk is
271 * @deadlock_detect: do we have to carry out deadlock detection?
272 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
273 * things for a task that has just got its priority adjusted, and
274 * is waiting on a mutex)
275 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
276 * we dropped its pi_lock. Is never dereferenced, only used for
277 * comparison to detect lock chain changes.
278 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
279 * its priority to the mutex owner (can be NULL in the case
280 * depicted above or if the top waiter is gone away and we are
281 * actually deboosting the owner)
282 * @top_task: the current top waiter
284 * Returns 0 or -EDEADLK.
286 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
288 struct rt_mutex *orig_lock,
289 struct rt_mutex *next_lock,
290 struct rt_mutex_waiter *orig_waiter,
291 struct task_struct *top_task)
293 struct rt_mutex *lock;
294 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
295 int detect_deadlock, ret = 0, depth = 0;
298 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
302 * The (de)boosting is a step by step approach with a lot of
303 * pitfalls. We want this to be preemptible and we want hold a
304 * maximum of two locks per step. So we have to check
305 * carefully whether things change under us.
308 if (++depth > max_lock_depth) {
312 * Print this only once. If the admin changes the limit,
313 * print a new message when reaching the limit again.
315 if (prev_max != max_lock_depth) {
316 prev_max = max_lock_depth;
317 printk(KERN_WARNING "Maximum lock depth %d reached "
318 "task: %s (%d)\n", max_lock_depth,
319 top_task->comm, task_pid_nr(top_task));
321 put_task_struct(task);
327 * Task can not go away as we did a get_task() before !
329 raw_spin_lock_irqsave(&task->pi_lock, flags);
331 waiter = task->pi_blocked_on;
333 * Check whether the end of the boosting chain has been
334 * reached or the state of the chain has changed while we
341 * Check the orig_waiter state. After we dropped the locks,
342 * the previous owner of the lock might have released the lock.
344 if (orig_waiter && !rt_mutex_owner(orig_lock))
348 * We dropped all locks after taking a refcount on @task, so
349 * the task might have moved on in the lock chain or even left
350 * the chain completely and blocks now on an unrelated lock or
353 * We stored the lock on which @task was blocked in @next_lock,
354 * so we can detect the chain change.
356 if (next_lock != waiter->lock)
360 * Drop out, when the task has no waiters. Note,
361 * top_waiter can be NULL, when we are in the deboosting
365 if (!task_has_pi_waiters(task))
368 * If deadlock detection is off, we stop here if we
369 * are not the top pi waiter of the task.
371 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
376 * When deadlock detection is off then we check, if further
377 * priority adjustment is necessary.
379 if (!detect_deadlock && waiter->list_entry.prio == task->prio)
383 if (!raw_spin_trylock(&lock->wait_lock)) {
384 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
390 * Deadlock detection. If the lock is the same as the original
391 * lock which caused us to walk the lock chain or if the
392 * current lock is owned by the task which initiated the chain
393 * walk, we detected a deadlock.
395 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
396 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
397 raw_spin_unlock(&lock->wait_lock);
402 top_waiter = rt_mutex_top_waiter(lock);
404 /* Requeue the waiter */
405 plist_del(&waiter->list_entry, &lock->wait_list);
406 waiter->list_entry.prio = task->prio;
407 plist_add(&waiter->list_entry, &lock->wait_list);
409 /* Release the task */
410 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
411 if (!rt_mutex_owner(lock)) {
413 * If the requeue above changed the top waiter, then we need
414 * to wake the new top waiter up to try to get the lock.
417 if (top_waiter != rt_mutex_top_waiter(lock))
418 wake_up_process(rt_mutex_top_waiter(lock)->task);
419 raw_spin_unlock(&lock->wait_lock);
422 put_task_struct(task);
424 /* Grab the next task */
425 task = rt_mutex_owner(lock);
426 get_task_struct(task);
427 raw_spin_lock_irqsave(&task->pi_lock, flags);
429 if (waiter == rt_mutex_top_waiter(lock)) {
430 /* Boost the owner */
431 plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
432 waiter->pi_list_entry.prio = waiter->list_entry.prio;
433 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
434 __rt_mutex_adjust_prio(task);
436 } else if (top_waiter == waiter) {
437 /* Deboost the owner */
438 plist_del(&waiter->pi_list_entry, &task->pi_waiters);
439 waiter = rt_mutex_top_waiter(lock);
440 waiter->pi_list_entry.prio = waiter->list_entry.prio;
441 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
442 __rt_mutex_adjust_prio(task);
446 * Check whether the task which owns the current lock is pi
447 * blocked itself. If yes we store a pointer to the lock for
448 * the lock chain change detection above. After we dropped
449 * task->pi_lock next_lock cannot be dereferenced anymore.
451 next_lock = task_blocked_on_lock(task);
453 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
455 top_waiter = rt_mutex_top_waiter(lock);
456 raw_spin_unlock(&lock->wait_lock);
459 * We reached the end of the lock chain. Stop right here. No
460 * point to go back just to figure that out.
465 if (!detect_deadlock && waiter != top_waiter)
471 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
473 put_task_struct(task);
479 * Try to take an rt-mutex
481 * Must be called with lock->wait_lock held.
483 * @lock: the lock to be acquired.
484 * @task: the task which wants to acquire the lock
485 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
487 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
488 struct rt_mutex_waiter *waiter)
491 * We have to be careful here if the atomic speedups are
492 * enabled, such that, when
493 * - no other waiter is on the lock
494 * - the lock has been released since we did the cmpxchg
495 * the lock can be released or taken while we are doing the
496 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
498 * The atomic acquire/release aware variant of
499 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
500 * the WAITERS bit, the atomic release / acquire can not
501 * happen anymore and lock->wait_lock protects us from the
504 * Note, that this might set lock->owner =
505 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
506 * any more. This is fixed up when we take the ownership.
507 * This is the transitional state explained at the top of this file.
509 mark_rt_mutex_waiters(lock);
511 if (rt_mutex_owner(lock))
515 * It will get the lock because of one of these conditions:
516 * 1) there is no waiter
517 * 2) higher priority than waiters
518 * 3) it is top waiter
520 if (rt_mutex_has_waiters(lock)) {
521 if (task->prio >= rt_mutex_top_waiter(lock)->list_entry.prio) {
522 if (!waiter || waiter != rt_mutex_top_waiter(lock))
527 if (waiter || rt_mutex_has_waiters(lock)) {
529 struct rt_mutex_waiter *top;
531 raw_spin_lock_irqsave(&task->pi_lock, flags);
533 /* remove the queued waiter. */
535 plist_del(&waiter->list_entry, &lock->wait_list);
536 task->pi_blocked_on = NULL;
540 * We have to enqueue the top waiter(if it exists) into
541 * task->pi_waiters list.
543 if (rt_mutex_has_waiters(lock)) {
544 top = rt_mutex_top_waiter(lock);
545 top->pi_list_entry.prio = top->list_entry.prio;
546 plist_add(&top->pi_list_entry, &task->pi_waiters);
548 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
551 /* We got the lock. */
552 debug_rt_mutex_lock(lock);
554 rt_mutex_set_owner(lock, task);
556 rt_mutex_deadlock_account_lock(lock, task);
562 * Task blocks on lock.
564 * Prepare waiter and propagate pi chain
566 * This must be called with lock->wait_lock held.
568 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
569 struct rt_mutex_waiter *waiter,
570 struct task_struct *task,
573 struct task_struct *owner = rt_mutex_owner(lock);
574 struct rt_mutex_waiter *top_waiter = waiter;
575 struct rt_mutex *next_lock;
576 int chain_walk = 0, res;
580 * Early deadlock detection. We really don't want the task to
581 * enqueue on itself just to untangle the mess later. It's not
582 * only an optimization. We drop the locks, so another waiter
583 * can come in before the chain walk detects the deadlock. So
584 * the other will detect the deadlock and return -EDEADLOCK,
585 * which is wrong, as the other waiter is not in a deadlock
591 raw_spin_lock_irqsave(&task->pi_lock, flags);
592 __rt_mutex_adjust_prio(task);
595 plist_node_init(&waiter->list_entry, task->prio);
596 plist_node_init(&waiter->pi_list_entry, task->prio);
598 /* Get the top priority waiter on the lock */
599 if (rt_mutex_has_waiters(lock))
600 top_waiter = rt_mutex_top_waiter(lock);
601 plist_add(&waiter->list_entry, &lock->wait_list);
603 task->pi_blocked_on = waiter;
605 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
610 raw_spin_lock_irqsave(&owner->pi_lock, flags);
611 if (waiter == rt_mutex_top_waiter(lock)) {
612 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
613 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
615 __rt_mutex_adjust_prio(owner);
616 if (owner->pi_blocked_on)
618 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
622 /* Store the lock on which owner is blocked or NULL */
623 next_lock = task_blocked_on_lock(owner);
625 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
627 * Even if full deadlock detection is on, if the owner is not
628 * blocked itself, we can avoid finding this out in the chain
631 if (!chain_walk || !next_lock)
635 * The owner can't disappear while holding a lock,
636 * so the owner struct is protected by wait_lock.
637 * Gets dropped in rt_mutex_adjust_prio_chain()!
639 get_task_struct(owner);
641 raw_spin_unlock(&lock->wait_lock);
643 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
644 next_lock, waiter, task);
646 raw_spin_lock(&lock->wait_lock);
652 * Wake up the next waiter on the lock.
654 * Remove the top waiter from the current tasks pi waiter list and
657 * Called with lock->wait_lock held.
659 static void wakeup_next_waiter(struct rt_mutex *lock)
661 struct rt_mutex_waiter *waiter;
664 raw_spin_lock_irqsave(¤t->pi_lock, flags);
666 waiter = rt_mutex_top_waiter(lock);
669 * Remove it from current->pi_waiters. We do not adjust a
670 * possible priority boost right now. We execute wakeup in the
671 * boosted mode and go back to normal after releasing
674 plist_del(&waiter->pi_list_entry, ¤t->pi_waiters);
677 * As we are waking up the top waiter, and the waiter stays
678 * queued on the lock until it gets the lock, this lock
679 * obviously has waiters. Just set the bit here and this has
680 * the added benefit of forcing all new tasks into the
681 * slow path making sure no task of lower priority than
682 * the top waiter can steal this lock.
684 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
686 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
689 * It's safe to dereference waiter as it cannot go away as
690 * long as we hold lock->wait_lock. The waiter task needs to
691 * acquire it in order to dequeue the waiter.
693 wake_up_process(waiter->task);
697 * Remove a waiter from a lock and give up
699 * Must be called with lock->wait_lock held and
700 * have just failed to try_to_take_rt_mutex().
702 static void remove_waiter(struct rt_mutex *lock,
703 struct rt_mutex_waiter *waiter)
705 int first = (waiter == rt_mutex_top_waiter(lock));
706 struct task_struct *owner = rt_mutex_owner(lock);
707 struct rt_mutex *next_lock = NULL;
710 raw_spin_lock_irqsave(¤t->pi_lock, flags);
711 plist_del(&waiter->list_entry, &lock->wait_list);
712 current->pi_blocked_on = NULL;
713 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
720 raw_spin_lock_irqsave(&owner->pi_lock, flags);
722 plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
724 if (rt_mutex_has_waiters(lock)) {
725 struct rt_mutex_waiter *next;
727 next = rt_mutex_top_waiter(lock);
728 plist_add(&next->pi_list_entry, &owner->pi_waiters);
730 __rt_mutex_adjust_prio(owner);
732 /* Store the lock on which owner is blocked or NULL */
733 next_lock = task_blocked_on_lock(owner);
735 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
738 WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
743 /* gets dropped in rt_mutex_adjust_prio_chain()! */
744 get_task_struct(owner);
746 raw_spin_unlock(&lock->wait_lock);
748 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
750 raw_spin_lock(&lock->wait_lock);
754 * Recheck the pi chain, in case we got a priority setting
756 * Called from sched_setscheduler
758 void rt_mutex_adjust_pi(struct task_struct *task)
760 struct rt_mutex_waiter *waiter;
761 struct rt_mutex *next_lock;
764 raw_spin_lock_irqsave(&task->pi_lock, flags);
766 waiter = task->pi_blocked_on;
767 if (!waiter || waiter->list_entry.prio == task->prio) {
768 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
771 next_lock = waiter->lock;
772 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
774 /* gets dropped in rt_mutex_adjust_prio_chain()! */
775 get_task_struct(task);
777 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
781 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
782 * @lock: the rt_mutex to take
783 * @state: the state the task should block in (TASK_INTERRUPTIBLE
784 * or TASK_UNINTERRUPTIBLE)
785 * @timeout: the pre-initialized and started timer, or NULL for none
786 * @waiter: the pre-initialized rt_mutex_waiter
788 * lock->wait_lock must be held by the caller.
791 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
792 struct hrtimer_sleeper *timeout,
793 struct rt_mutex_waiter *waiter)
799 /* Try to acquire the lock: */
800 if (try_to_take_rt_mutex(lock, current, waiter))
804 * TASK_INTERRUPTIBLE checks for signals and
805 * timeout. Ignored otherwise.
807 if (unlikely(state == TASK_INTERRUPTIBLE)) {
808 /* Signal pending? */
809 if (signal_pending(current))
811 if (timeout && !timeout->task)
817 raw_spin_unlock(&lock->wait_lock);
819 was_disabled = irqs_disabled();
823 debug_rt_mutex_print_deadlock(waiter);
825 schedule_rt_mutex(lock);
830 raw_spin_lock(&lock->wait_lock);
831 set_current_state(state);
837 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
838 struct rt_mutex_waiter *w)
841 * If the result is not -EDEADLOCK or the caller requested
842 * deadlock detection, nothing to do here.
844 if (res != -EDEADLOCK || detect_deadlock)
848 * Yell lowdly and stop the task right here.
850 rt_mutex_print_deadlock(w);
852 set_current_state(TASK_INTERRUPTIBLE);
858 * Slow path lock function:
861 rt_mutex_slowlock(struct rt_mutex *lock, int state,
862 struct hrtimer_sleeper *timeout,
865 struct rt_mutex_waiter waiter;
868 debug_rt_mutex_init_waiter(&waiter);
870 raw_spin_lock(&lock->wait_lock);
872 /* Try to acquire the lock again: */
873 if (try_to_take_rt_mutex(lock, current, NULL)) {
874 raw_spin_unlock(&lock->wait_lock);
878 set_current_state(state);
880 /* Setup the timer, when timeout != NULL */
881 if (unlikely(timeout)) {
882 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
883 if (!hrtimer_active(&timeout->timer))
884 timeout->task = NULL;
887 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
890 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
892 set_current_state(TASK_RUNNING);
895 remove_waiter(lock, &waiter);
896 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
900 * try_to_take_rt_mutex() sets the waiter bit
901 * unconditionally. We might have to fix that up.
903 fixup_rt_mutex_waiters(lock);
905 raw_spin_unlock(&lock->wait_lock);
907 /* Remove pending timer: */
908 if (unlikely(timeout))
909 hrtimer_cancel(&timeout->timer);
911 debug_rt_mutex_free_waiter(&waiter);
917 * Slow path try-lock function:
920 rt_mutex_slowtrylock(struct rt_mutex *lock)
924 raw_spin_lock(&lock->wait_lock);
926 if (likely(rt_mutex_owner(lock) != current)) {
928 ret = try_to_take_rt_mutex(lock, current, NULL);
930 * try_to_take_rt_mutex() sets the lock waiters
931 * bit unconditionally. Clean this up.
933 fixup_rt_mutex_waiters(lock);
936 raw_spin_unlock(&lock->wait_lock);
942 * Slow path to release a rt-mutex:
945 rt_mutex_slowunlock(struct rt_mutex *lock)
947 raw_spin_lock(&lock->wait_lock);
949 debug_rt_mutex_unlock(lock);
951 rt_mutex_deadlock_account_unlock(current);
954 * We must be careful here if the fast path is enabled. If we
955 * have no waiters queued we cannot set owner to NULL here
958 * foo->lock->owner = NULL;
959 * rtmutex_lock(foo->lock); <- fast path
960 * free = atomic_dec_and_test(foo->refcnt);
961 * rtmutex_unlock(foo->lock); <- fast path
964 * raw_spin_unlock(foo->lock->wait_lock);
966 * So for the fastpath enabled kernel:
968 * Nothing can set the waiters bit as long as we hold
969 * lock->wait_lock. So we do the following sequence:
971 * owner = rt_mutex_owner(lock);
972 * clear_rt_mutex_waiters(lock);
973 * raw_spin_unlock(&lock->wait_lock);
974 * if (cmpxchg(&lock->owner, owner, 0) == owner)
978 * The fastpath disabled variant is simple as all access to
979 * lock->owner is serialized by lock->wait_lock:
981 * lock->owner = NULL;
982 * raw_spin_unlock(&lock->wait_lock);
984 while (!rt_mutex_has_waiters(lock)) {
985 /* Drops lock->wait_lock ! */
986 if (unlock_rt_mutex_safe(lock) == true)
988 /* Relock the rtmutex and try again */
989 raw_spin_lock(&lock->wait_lock);
993 * The wakeup next waiter path does not suffer from the above
994 * race. See the comments there.
996 wakeup_next_waiter(lock);
998 raw_spin_unlock(&lock->wait_lock);
1000 /* Undo pi boosting if necessary: */
1001 rt_mutex_adjust_prio(current);
1005 * debug aware fast / slowpath lock,trylock,unlock
1007 * The atomic acquire/release ops are compiled away, when either the
1008 * architecture does not support cmpxchg or when debugging is enabled.
1011 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1012 int detect_deadlock,
1013 int (*slowfn)(struct rt_mutex *lock, int state,
1014 struct hrtimer_sleeper *timeout,
1015 int detect_deadlock))
1017 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1018 rt_mutex_deadlock_account_lock(lock, current);
1021 return slowfn(lock, state, NULL, detect_deadlock);
1025 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1026 struct hrtimer_sleeper *timeout, int detect_deadlock,
1027 int (*slowfn)(struct rt_mutex *lock, int state,
1028 struct hrtimer_sleeper *timeout,
1029 int detect_deadlock))
1031 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1032 rt_mutex_deadlock_account_lock(lock, current);
1035 return slowfn(lock, state, timeout, detect_deadlock);
1039 rt_mutex_fasttrylock(struct rt_mutex *lock,
1040 int (*slowfn)(struct rt_mutex *lock))
1042 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1043 rt_mutex_deadlock_account_lock(lock, current);
1046 return slowfn(lock);
1050 rt_mutex_fastunlock(struct rt_mutex *lock,
1051 void (*slowfn)(struct rt_mutex *lock))
1053 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1054 rt_mutex_deadlock_account_unlock(current);
1060 * rt_mutex_lock - lock a rt_mutex
1062 * @lock: the rt_mutex to be locked
1064 void __sched rt_mutex_lock(struct rt_mutex *lock)
1068 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1070 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1073 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1075 * @lock: the rt_mutex to be locked
1076 * @detect_deadlock: deadlock detection on/off
1080 * -EINTR when interrupted by a signal
1081 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1083 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1084 int detect_deadlock)
1088 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1089 detect_deadlock, rt_mutex_slowlock);
1091 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1094 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1095 * the timeout structure is provided
1098 * @lock: the rt_mutex to be locked
1099 * @timeout: timeout structure or NULL (no timeout)
1100 * @detect_deadlock: deadlock detection on/off
1104 * -EINTR when interrupted by a signal
1105 * -ETIMEDOUT when the timeout expired
1106 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1109 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1110 int detect_deadlock)
1114 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1115 detect_deadlock, rt_mutex_slowlock);
1117 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1120 * rt_mutex_trylock - try to lock a rt_mutex
1122 * @lock: the rt_mutex to be locked
1124 * Returns 1 on success and 0 on contention
1126 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1128 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1130 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1133 * rt_mutex_unlock - unlock a rt_mutex
1135 * @lock: the rt_mutex to be unlocked
1137 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1139 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1141 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1144 * rt_mutex_destroy - mark a mutex unusable
1145 * @lock: the mutex to be destroyed
1147 * This function marks the mutex uninitialized, and any subsequent
1148 * use of the mutex is forbidden. The mutex must not be locked when
1149 * this function is called.
1151 void rt_mutex_destroy(struct rt_mutex *lock)
1153 WARN_ON(rt_mutex_is_locked(lock));
1154 #ifdef CONFIG_DEBUG_RT_MUTEXES
1159 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1162 * __rt_mutex_init - initialize the rt lock
1164 * @lock: the rt lock to be initialized
1166 * Initialize the rt lock to unlocked state.
1168 * Initializing of a locked rt lock is not allowed
1170 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1173 raw_spin_lock_init(&lock->wait_lock);
1174 plist_head_init(&lock->wait_list);
1176 debug_rt_mutex_init(lock, name);
1178 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1181 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1184 * @lock: the rt_mutex to be locked
1185 * @proxy_owner:the task to set as owner
1187 * No locking. Caller has to do serializing itself
1188 * Special API call for PI-futex support
1190 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1191 struct task_struct *proxy_owner)
1193 __rt_mutex_init(lock, NULL);
1194 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1195 rt_mutex_set_owner(lock, proxy_owner);
1196 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1200 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1202 * @lock: the rt_mutex to be locked
1204 * No locking. Caller has to do serializing itself
1205 * Special API call for PI-futex support
1207 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1208 struct task_struct *proxy_owner)
1210 debug_rt_mutex_proxy_unlock(lock);
1211 rt_mutex_set_owner(lock, NULL);
1212 rt_mutex_deadlock_account_unlock(proxy_owner);
1216 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1217 * @lock: the rt_mutex to take
1218 * @waiter: the pre-initialized rt_mutex_waiter
1219 * @task: the task to prepare
1220 * @detect_deadlock: perform deadlock detection (1) or not (0)
1223 * 0 - task blocked on lock
1224 * 1 - acquired the lock for task, caller should wake it up
1227 * Special API call for FUTEX_REQUEUE_PI support.
1229 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1230 struct rt_mutex_waiter *waiter,
1231 struct task_struct *task, int detect_deadlock)
1235 raw_spin_lock(&lock->wait_lock);
1237 if (try_to_take_rt_mutex(lock, task, NULL)) {
1238 raw_spin_unlock(&lock->wait_lock);
1242 /* We enforce deadlock detection for futexes */
1243 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1245 if (ret && !rt_mutex_owner(lock)) {
1247 * Reset the return value. We might have
1248 * returned with -EDEADLK and the owner
1249 * released the lock while we were walking the
1250 * pi chain. Let the waiter sort it out.
1256 remove_waiter(lock, waiter);
1258 raw_spin_unlock(&lock->wait_lock);
1260 debug_rt_mutex_print_deadlock(waiter);
1266 * rt_mutex_next_owner - return the next owner of the lock
1268 * @lock: the rt lock query
1270 * Returns the next owner of the lock or NULL
1272 * Caller has to serialize against other accessors to the lock
1275 * Special API call for PI-futex support
1277 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1279 if (!rt_mutex_has_waiters(lock))
1282 return rt_mutex_top_waiter(lock)->task;
1286 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1287 * @lock: the rt_mutex we were woken on
1288 * @to: the timeout, null if none. hrtimer should already have
1290 * @waiter: the pre-initialized rt_mutex_waiter
1291 * @detect_deadlock: perform deadlock detection (1) or not (0)
1293 * Complete the lock acquisition started our behalf by another thread.
1297 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1299 * Special API call for PI-futex requeue support
1301 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1302 struct hrtimer_sleeper *to,
1303 struct rt_mutex_waiter *waiter,
1304 int detect_deadlock)
1308 raw_spin_lock(&lock->wait_lock);
1310 set_current_state(TASK_INTERRUPTIBLE);
1312 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1314 set_current_state(TASK_RUNNING);
1317 remove_waiter(lock, waiter);
1320 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1321 * have to fix that up.
1323 fixup_rt_mutex_waiters(lock);
1325 raw_spin_unlock(&lock->wait_lock);