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 if (!rt_mutex_has_waiters(lock))
68 clear_rt_mutex_waiters(lock);
72 * We can speed up the acquire/release, if the architecture
73 * supports cmpxchg and if there's no debugging state to be set up
75 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
76 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
77 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
79 unsigned long owner, *p = (unsigned long *) &lock->owner;
83 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
87 * Safe fastpath aware unlock:
88 * 1) Clear the waiters bit
89 * 2) Drop lock->wait_lock
90 * 3) Try to unlock the lock with cmpxchg
92 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
93 __releases(lock->wait_lock)
95 struct task_struct *owner = rt_mutex_owner(lock);
97 clear_rt_mutex_waiters(lock);
98 raw_spin_unlock(&lock->wait_lock);
100 * If a new waiter comes in between the unlock and the cmpxchg
101 * we have two situations:
105 * cmpxchg(p, owner, 0) == owner
106 * mark_rt_mutex_waiters(lock);
112 * mark_rt_mutex_waiters(lock);
114 * cmpxchg(p, owner, 0) != owner
123 return rt_mutex_cmpxchg(lock, owner, NULL);
127 # define rt_mutex_cmpxchg(l,c,n) (0)
128 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
130 lock->owner = (struct task_struct *)
131 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
135 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
137 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
138 __releases(lock->wait_lock)
141 raw_spin_unlock(&lock->wait_lock);
147 * Calculate task priority from the waiter list priority
149 * Return task->normal_prio when the waiter list is empty or when
150 * the waiter is not allowed to do priority boosting
152 int rt_mutex_getprio(struct task_struct *task)
154 if (likely(!task_has_pi_waiters(task)))
155 return task->normal_prio;
157 return min(task_top_pi_waiter(task)->pi_list_entry.prio,
162 * Adjust the priority of a task, after its pi_waiters got modified.
164 * This can be both boosting and unboosting. task->pi_lock must be held.
166 static void __rt_mutex_adjust_prio(struct task_struct *task)
168 int prio = rt_mutex_getprio(task);
170 if (task->prio != prio)
171 rt_mutex_setprio(task, prio);
175 * Adjust task priority (undo boosting). Called from the exit path of
176 * rt_mutex_slowunlock() and rt_mutex_slowlock().
178 * (Note: We do this outside of the protection of lock->wait_lock to
179 * allow the lock to be taken while or before we readjust the priority
180 * of task. We do not use the spin_xx_mutex() variants here as we are
181 * outside of the debug path.)
183 static void rt_mutex_adjust_prio(struct task_struct *task)
187 raw_spin_lock_irqsave(&task->pi_lock, flags);
188 __rt_mutex_adjust_prio(task);
189 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
193 * Max number of times we'll walk the boosting chain:
195 int max_lock_depth = 1024;
197 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
199 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
203 * Adjust the priority chain. Also used for deadlock detection.
204 * Decreases task's usage by one - may thus free the task.
205 * Returns 0 or -EDEADLK.
207 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
209 struct rt_mutex *orig_lock,
210 struct rt_mutex *next_lock,
211 struct rt_mutex_waiter *orig_waiter,
212 struct task_struct *top_task)
214 struct rt_mutex *lock;
215 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
216 int detect_deadlock, ret = 0, depth = 0;
219 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
223 * The (de)boosting is a step by step approach with a lot of
224 * pitfalls. We want this to be preemptible and we want hold a
225 * maximum of two locks per step. So we have to check
226 * carefully whether things change under us.
229 if (++depth > max_lock_depth) {
233 * Print this only once. If the admin changes the limit,
234 * print a new message when reaching the limit again.
236 if (prev_max != max_lock_depth) {
237 prev_max = max_lock_depth;
238 printk(KERN_WARNING "Maximum lock depth %d reached "
239 "task: %s (%d)\n", max_lock_depth,
240 top_task->comm, task_pid_nr(top_task));
242 put_task_struct(task);
248 * Task can not go away as we did a get_task() before !
250 raw_spin_lock_irqsave(&task->pi_lock, flags);
252 waiter = task->pi_blocked_on;
254 * Check whether the end of the boosting chain has been
255 * reached or the state of the chain has changed while we
262 * Check the orig_waiter state. After we dropped the locks,
263 * the previous owner of the lock might have released the lock.
265 if (orig_waiter && !rt_mutex_owner(orig_lock))
269 * We dropped all locks after taking a refcount on @task, so
270 * the task might have moved on in the lock chain or even left
271 * the chain completely and blocks now on an unrelated lock or
274 * We stored the lock on which @task was blocked in @next_lock,
275 * so we can detect the chain change.
277 if (next_lock != waiter->lock)
281 * Drop out, when the task has no waiters. Note,
282 * top_waiter can be NULL, when we are in the deboosting
286 if (!task_has_pi_waiters(task))
289 * If deadlock detection is off, we stop here if we
290 * are not the top pi waiter of the task.
292 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
297 * When deadlock detection is off then we check, if further
298 * priority adjustment is necessary.
300 if (!detect_deadlock && waiter->list_entry.prio == task->prio)
304 if (!raw_spin_trylock(&lock->wait_lock)) {
305 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
311 * Deadlock detection. If the lock is the same as the original
312 * lock which caused us to walk the lock chain or if the
313 * current lock is owned by the task which initiated the chain
314 * walk, we detected a deadlock.
316 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
317 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
318 raw_spin_unlock(&lock->wait_lock);
323 top_waiter = rt_mutex_top_waiter(lock);
325 /* Requeue the waiter */
326 plist_del(&waiter->list_entry, &lock->wait_list);
327 waiter->list_entry.prio = task->prio;
328 plist_add(&waiter->list_entry, &lock->wait_list);
330 /* Release the task */
331 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
332 if (!rt_mutex_owner(lock)) {
334 * If the requeue above changed the top waiter, then we need
335 * to wake the new top waiter up to try to get the lock.
338 if (top_waiter != rt_mutex_top_waiter(lock))
339 wake_up_process(rt_mutex_top_waiter(lock)->task);
340 raw_spin_unlock(&lock->wait_lock);
343 put_task_struct(task);
345 /* Grab the next task */
346 task = rt_mutex_owner(lock);
347 get_task_struct(task);
348 raw_spin_lock_irqsave(&task->pi_lock, flags);
350 if (waiter == rt_mutex_top_waiter(lock)) {
351 /* Boost the owner */
352 plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
353 waiter->pi_list_entry.prio = waiter->list_entry.prio;
354 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
355 __rt_mutex_adjust_prio(task);
357 } else if (top_waiter == waiter) {
358 /* Deboost the owner */
359 plist_del(&waiter->pi_list_entry, &task->pi_waiters);
360 waiter = rt_mutex_top_waiter(lock);
361 waiter->pi_list_entry.prio = waiter->list_entry.prio;
362 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
363 __rt_mutex_adjust_prio(task);
367 * Check whether the task which owns the current lock is pi
368 * blocked itself. If yes we store a pointer to the lock for
369 * the lock chain change detection above. After we dropped
370 * task->pi_lock next_lock cannot be dereferenced anymore.
372 next_lock = task_blocked_on_lock(task);
374 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
376 top_waiter = rt_mutex_top_waiter(lock);
377 raw_spin_unlock(&lock->wait_lock);
380 * We reached the end of the lock chain. Stop right here. No
381 * point to go back just to figure that out.
386 if (!detect_deadlock && waiter != top_waiter)
392 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
394 put_task_struct(task);
400 * Try to take an rt-mutex
402 * Must be called with lock->wait_lock held.
404 * @lock: the lock to be acquired.
405 * @task: the task which wants to acquire the lock
406 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
408 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
409 struct rt_mutex_waiter *waiter)
412 * We have to be careful here if the atomic speedups are
413 * enabled, such that, when
414 * - no other waiter is on the lock
415 * - the lock has been released since we did the cmpxchg
416 * the lock can be released or taken while we are doing the
417 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
419 * The atomic acquire/release aware variant of
420 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
421 * the WAITERS bit, the atomic release / acquire can not
422 * happen anymore and lock->wait_lock protects us from the
425 * Note, that this might set lock->owner =
426 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
427 * any more. This is fixed up when we take the ownership.
428 * This is the transitional state explained at the top of this file.
430 mark_rt_mutex_waiters(lock);
432 if (rt_mutex_owner(lock))
436 * It will get the lock because of one of these conditions:
437 * 1) there is no waiter
438 * 2) higher priority than waiters
439 * 3) it is top waiter
441 if (rt_mutex_has_waiters(lock)) {
442 if (task->prio >= rt_mutex_top_waiter(lock)->list_entry.prio) {
443 if (!waiter || waiter != rt_mutex_top_waiter(lock))
448 if (waiter || rt_mutex_has_waiters(lock)) {
450 struct rt_mutex_waiter *top;
452 raw_spin_lock_irqsave(&task->pi_lock, flags);
454 /* remove the queued waiter. */
456 plist_del(&waiter->list_entry, &lock->wait_list);
457 task->pi_blocked_on = NULL;
461 * We have to enqueue the top waiter(if it exists) into
462 * task->pi_waiters list.
464 if (rt_mutex_has_waiters(lock)) {
465 top = rt_mutex_top_waiter(lock);
466 top->pi_list_entry.prio = top->list_entry.prio;
467 plist_add(&top->pi_list_entry, &task->pi_waiters);
469 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
472 /* We got the lock. */
473 debug_rt_mutex_lock(lock);
475 rt_mutex_set_owner(lock, task);
477 rt_mutex_deadlock_account_lock(lock, task);
483 * Task blocks on lock.
485 * Prepare waiter and propagate pi chain
487 * This must be called with lock->wait_lock held.
489 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
490 struct rt_mutex_waiter *waiter,
491 struct task_struct *task,
494 struct task_struct *owner = rt_mutex_owner(lock);
495 struct rt_mutex_waiter *top_waiter = waiter;
496 struct rt_mutex *next_lock;
497 int chain_walk = 0, res;
501 * Early deadlock detection. We really don't want the task to
502 * enqueue on itself just to untangle the mess later. It's not
503 * only an optimization. We drop the locks, so another waiter
504 * can come in before the chain walk detects the deadlock. So
505 * the other will detect the deadlock and return -EDEADLOCK,
506 * which is wrong, as the other waiter is not in a deadlock
512 raw_spin_lock_irqsave(&task->pi_lock, flags);
513 __rt_mutex_adjust_prio(task);
516 plist_node_init(&waiter->list_entry, task->prio);
517 plist_node_init(&waiter->pi_list_entry, task->prio);
519 /* Get the top priority waiter on the lock */
520 if (rt_mutex_has_waiters(lock))
521 top_waiter = rt_mutex_top_waiter(lock);
522 plist_add(&waiter->list_entry, &lock->wait_list);
524 task->pi_blocked_on = waiter;
526 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
531 raw_spin_lock_irqsave(&owner->pi_lock, flags);
532 if (waiter == rt_mutex_top_waiter(lock)) {
533 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
534 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
536 __rt_mutex_adjust_prio(owner);
537 if (owner->pi_blocked_on)
539 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
543 /* Store the lock on which owner is blocked or NULL */
544 next_lock = task_blocked_on_lock(owner);
546 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
548 * Even if full deadlock detection is on, if the owner is not
549 * blocked itself, we can avoid finding this out in the chain
552 if (!chain_walk || !next_lock)
556 * The owner can't disappear while holding a lock,
557 * so the owner struct is protected by wait_lock.
558 * Gets dropped in rt_mutex_adjust_prio_chain()!
560 get_task_struct(owner);
562 raw_spin_unlock(&lock->wait_lock);
564 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
565 next_lock, waiter, task);
567 raw_spin_lock(&lock->wait_lock);
573 * Wake up the next waiter on the lock.
575 * Remove the top waiter from the current tasks pi waiter list and
578 * Called with lock->wait_lock held.
580 static void wakeup_next_waiter(struct rt_mutex *lock)
582 struct rt_mutex_waiter *waiter;
585 raw_spin_lock_irqsave(¤t->pi_lock, flags);
587 waiter = rt_mutex_top_waiter(lock);
590 * Remove it from current->pi_waiters. We do not adjust a
591 * possible priority boost right now. We execute wakeup in the
592 * boosted mode and go back to normal after releasing
595 plist_del(&waiter->pi_list_entry, ¤t->pi_waiters);
598 * As we are waking up the top waiter, and the waiter stays
599 * queued on the lock until it gets the lock, this lock
600 * obviously has waiters. Just set the bit here and this has
601 * the added benefit of forcing all new tasks into the
602 * slow path making sure no task of lower priority than
603 * the top waiter can steal this lock.
605 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
607 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
610 * It's safe to dereference waiter as it cannot go away as
611 * long as we hold lock->wait_lock. The waiter task needs to
612 * acquire it in order to dequeue the waiter.
614 wake_up_process(waiter->task);
618 * Remove a waiter from a lock and give up
620 * Must be called with lock->wait_lock held and
621 * have just failed to try_to_take_rt_mutex().
623 static void remove_waiter(struct rt_mutex *lock,
624 struct rt_mutex_waiter *waiter)
626 int first = (waiter == rt_mutex_top_waiter(lock));
627 struct task_struct *owner = rt_mutex_owner(lock);
628 struct rt_mutex *next_lock = NULL;
631 raw_spin_lock_irqsave(¤t->pi_lock, flags);
632 plist_del(&waiter->list_entry, &lock->wait_list);
633 current->pi_blocked_on = NULL;
634 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
641 raw_spin_lock_irqsave(&owner->pi_lock, flags);
643 plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
645 if (rt_mutex_has_waiters(lock)) {
646 struct rt_mutex_waiter *next;
648 next = rt_mutex_top_waiter(lock);
649 plist_add(&next->pi_list_entry, &owner->pi_waiters);
651 __rt_mutex_adjust_prio(owner);
653 /* Store the lock on which owner is blocked or NULL */
654 next_lock = task_blocked_on_lock(owner);
656 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
659 WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
664 /* gets dropped in rt_mutex_adjust_prio_chain()! */
665 get_task_struct(owner);
667 raw_spin_unlock(&lock->wait_lock);
669 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
671 raw_spin_lock(&lock->wait_lock);
675 * Recheck the pi chain, in case we got a priority setting
677 * Called from sched_setscheduler
679 void rt_mutex_adjust_pi(struct task_struct *task)
681 struct rt_mutex_waiter *waiter;
682 struct rt_mutex *next_lock;
685 raw_spin_lock_irqsave(&task->pi_lock, flags);
687 waiter = task->pi_blocked_on;
688 if (!waiter || waiter->list_entry.prio == task->prio) {
689 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
692 next_lock = waiter->lock;
693 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
695 /* gets dropped in rt_mutex_adjust_prio_chain()! */
696 get_task_struct(task);
698 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
702 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
703 * @lock: the rt_mutex to take
704 * @state: the state the task should block in (TASK_INTERRUPTIBLE
705 * or TASK_UNINTERRUPTIBLE)
706 * @timeout: the pre-initialized and started timer, or NULL for none
707 * @waiter: the pre-initialized rt_mutex_waiter
709 * lock->wait_lock must be held by the caller.
712 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
713 struct hrtimer_sleeper *timeout,
714 struct rt_mutex_waiter *waiter)
719 /* Try to acquire the lock: */
720 if (try_to_take_rt_mutex(lock, current, waiter))
724 * TASK_INTERRUPTIBLE checks for signals and
725 * timeout. Ignored otherwise.
727 if (unlikely(state == TASK_INTERRUPTIBLE)) {
728 /* Signal pending? */
729 if (signal_pending(current))
731 if (timeout && !timeout->task)
737 raw_spin_unlock(&lock->wait_lock);
739 debug_rt_mutex_print_deadlock(waiter);
741 schedule_rt_mutex(lock);
743 raw_spin_lock(&lock->wait_lock);
744 set_current_state(state);
750 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
751 struct rt_mutex_waiter *w)
754 * If the result is not -EDEADLOCK or the caller requested
755 * deadlock detection, nothing to do here.
757 if (res != -EDEADLOCK || detect_deadlock)
761 * Yell lowdly and stop the task right here.
763 rt_mutex_print_deadlock(w);
765 set_current_state(TASK_INTERRUPTIBLE);
771 * Slow path lock function:
774 rt_mutex_slowlock(struct rt_mutex *lock, int state,
775 struct hrtimer_sleeper *timeout,
778 struct rt_mutex_waiter waiter;
781 debug_rt_mutex_init_waiter(&waiter);
783 raw_spin_lock(&lock->wait_lock);
785 /* Try to acquire the lock again: */
786 if (try_to_take_rt_mutex(lock, current, NULL)) {
787 raw_spin_unlock(&lock->wait_lock);
791 set_current_state(state);
793 /* Setup the timer, when timeout != NULL */
794 if (unlikely(timeout)) {
795 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
796 if (!hrtimer_active(&timeout->timer))
797 timeout->task = NULL;
800 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
803 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
805 set_current_state(TASK_RUNNING);
808 remove_waiter(lock, &waiter);
809 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
813 * try_to_take_rt_mutex() sets the waiter bit
814 * unconditionally. We might have to fix that up.
816 fixup_rt_mutex_waiters(lock);
818 raw_spin_unlock(&lock->wait_lock);
820 /* Remove pending timer: */
821 if (unlikely(timeout))
822 hrtimer_cancel(&timeout->timer);
824 debug_rt_mutex_free_waiter(&waiter);
830 * Slow path try-lock function:
833 rt_mutex_slowtrylock(struct rt_mutex *lock)
837 raw_spin_lock(&lock->wait_lock);
839 if (likely(rt_mutex_owner(lock) != current)) {
841 ret = try_to_take_rt_mutex(lock, current, NULL);
843 * try_to_take_rt_mutex() sets the lock waiters
844 * bit unconditionally. Clean this up.
846 fixup_rt_mutex_waiters(lock);
849 raw_spin_unlock(&lock->wait_lock);
855 * Slow path to release a rt-mutex:
858 rt_mutex_slowunlock(struct rt_mutex *lock)
860 raw_spin_lock(&lock->wait_lock);
862 debug_rt_mutex_unlock(lock);
864 rt_mutex_deadlock_account_unlock(current);
867 * We must be careful here if the fast path is enabled. If we
868 * have no waiters queued we cannot set owner to NULL here
871 * foo->lock->owner = NULL;
872 * rtmutex_lock(foo->lock); <- fast path
873 * free = atomic_dec_and_test(foo->refcnt);
874 * rtmutex_unlock(foo->lock); <- fast path
877 * raw_spin_unlock(foo->lock->wait_lock);
879 * So for the fastpath enabled kernel:
881 * Nothing can set the waiters bit as long as we hold
882 * lock->wait_lock. So we do the following sequence:
884 * owner = rt_mutex_owner(lock);
885 * clear_rt_mutex_waiters(lock);
886 * raw_spin_unlock(&lock->wait_lock);
887 * if (cmpxchg(&lock->owner, owner, 0) == owner)
891 * The fastpath disabled variant is simple as all access to
892 * lock->owner is serialized by lock->wait_lock:
894 * lock->owner = NULL;
895 * raw_spin_unlock(&lock->wait_lock);
897 while (!rt_mutex_has_waiters(lock)) {
898 /* Drops lock->wait_lock ! */
899 if (unlock_rt_mutex_safe(lock) == true)
901 /* Relock the rtmutex and try again */
902 raw_spin_lock(&lock->wait_lock);
906 * The wakeup next waiter path does not suffer from the above
907 * race. See the comments there.
909 wakeup_next_waiter(lock);
911 raw_spin_unlock(&lock->wait_lock);
913 /* Undo pi boosting if necessary: */
914 rt_mutex_adjust_prio(current);
918 * debug aware fast / slowpath lock,trylock,unlock
920 * The atomic acquire/release ops are compiled away, when either the
921 * architecture does not support cmpxchg or when debugging is enabled.
924 rt_mutex_fastlock(struct rt_mutex *lock, int state,
926 int (*slowfn)(struct rt_mutex *lock, int state,
927 struct hrtimer_sleeper *timeout,
928 int detect_deadlock))
930 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
931 rt_mutex_deadlock_account_lock(lock, current);
934 return slowfn(lock, state, NULL, detect_deadlock);
938 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
939 struct hrtimer_sleeper *timeout, int detect_deadlock,
940 int (*slowfn)(struct rt_mutex *lock, int state,
941 struct hrtimer_sleeper *timeout,
942 int detect_deadlock))
944 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
945 rt_mutex_deadlock_account_lock(lock, current);
948 return slowfn(lock, state, timeout, detect_deadlock);
952 rt_mutex_fasttrylock(struct rt_mutex *lock,
953 int (*slowfn)(struct rt_mutex *lock))
955 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
956 rt_mutex_deadlock_account_lock(lock, current);
963 rt_mutex_fastunlock(struct rt_mutex *lock,
964 void (*slowfn)(struct rt_mutex *lock))
966 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
967 rt_mutex_deadlock_account_unlock(current);
973 * rt_mutex_lock - lock a rt_mutex
975 * @lock: the rt_mutex to be locked
977 void __sched rt_mutex_lock(struct rt_mutex *lock)
981 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
983 EXPORT_SYMBOL_GPL(rt_mutex_lock);
986 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
988 * @lock: the rt_mutex to be locked
989 * @detect_deadlock: deadlock detection on/off
993 * -EINTR when interrupted by a signal
994 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
996 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1001 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1002 detect_deadlock, rt_mutex_slowlock);
1004 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1007 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1008 * the timeout structure is provided
1011 * @lock: the rt_mutex to be locked
1012 * @timeout: timeout structure or NULL (no timeout)
1013 * @detect_deadlock: deadlock detection on/off
1017 * -EINTR when interrupted by a signal
1018 * -ETIMEDOUT when the timeout expired
1019 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1022 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1023 int detect_deadlock)
1027 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1028 detect_deadlock, rt_mutex_slowlock);
1030 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1033 * rt_mutex_trylock - try to lock a rt_mutex
1035 * @lock: the rt_mutex to be locked
1037 * Returns 1 on success and 0 on contention
1039 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1041 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1043 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1046 * rt_mutex_unlock - unlock a rt_mutex
1048 * @lock: the rt_mutex to be unlocked
1050 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1052 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1054 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1057 * rt_mutex_destroy - mark a mutex unusable
1058 * @lock: the mutex to be destroyed
1060 * This function marks the mutex uninitialized, and any subsequent
1061 * use of the mutex is forbidden. The mutex must not be locked when
1062 * this function is called.
1064 void rt_mutex_destroy(struct rt_mutex *lock)
1066 WARN_ON(rt_mutex_is_locked(lock));
1067 #ifdef CONFIG_DEBUG_RT_MUTEXES
1072 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1075 * __rt_mutex_init - initialize the rt lock
1077 * @lock: the rt lock to be initialized
1079 * Initialize the rt lock to unlocked state.
1081 * Initializing of a locked rt lock is not allowed
1083 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1086 raw_spin_lock_init(&lock->wait_lock);
1087 plist_head_init(&lock->wait_list);
1089 debug_rt_mutex_init(lock, name);
1091 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1094 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1097 * @lock: the rt_mutex to be locked
1098 * @proxy_owner:the task to set as owner
1100 * No locking. Caller has to do serializing itself
1101 * Special API call for PI-futex support
1103 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1104 struct task_struct *proxy_owner)
1106 __rt_mutex_init(lock, NULL);
1107 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1108 rt_mutex_set_owner(lock, proxy_owner);
1109 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1113 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1115 * @lock: the rt_mutex to be locked
1117 * No locking. Caller has to do serializing itself
1118 * Special API call for PI-futex support
1120 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1121 struct task_struct *proxy_owner)
1123 debug_rt_mutex_proxy_unlock(lock);
1124 rt_mutex_set_owner(lock, NULL);
1125 rt_mutex_deadlock_account_unlock(proxy_owner);
1129 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1130 * @lock: the rt_mutex to take
1131 * @waiter: the pre-initialized rt_mutex_waiter
1132 * @task: the task to prepare
1133 * @detect_deadlock: perform deadlock detection (1) or not (0)
1136 * 0 - task blocked on lock
1137 * 1 - acquired the lock for task, caller should wake it up
1140 * Special API call for FUTEX_REQUEUE_PI support.
1142 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1143 struct rt_mutex_waiter *waiter,
1144 struct task_struct *task, int detect_deadlock)
1148 raw_spin_lock(&lock->wait_lock);
1150 if (try_to_take_rt_mutex(lock, task, NULL)) {
1151 raw_spin_unlock(&lock->wait_lock);
1155 /* We enforce deadlock detection for futexes */
1156 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1158 if (ret && !rt_mutex_owner(lock)) {
1160 * Reset the return value. We might have
1161 * returned with -EDEADLK and the owner
1162 * released the lock while we were walking the
1163 * pi chain. Let the waiter sort it out.
1169 remove_waiter(lock, waiter);
1171 raw_spin_unlock(&lock->wait_lock);
1173 debug_rt_mutex_print_deadlock(waiter);
1179 * rt_mutex_next_owner - return the next owner of the lock
1181 * @lock: the rt lock query
1183 * Returns the next owner of the lock or NULL
1185 * Caller has to serialize against other accessors to the lock
1188 * Special API call for PI-futex support
1190 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1192 if (!rt_mutex_has_waiters(lock))
1195 return rt_mutex_top_waiter(lock)->task;
1199 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1200 * @lock: the rt_mutex we were woken on
1201 * @to: the timeout, null if none. hrtimer should already have
1203 * @waiter: the pre-initialized rt_mutex_waiter
1204 * @detect_deadlock: perform deadlock detection (1) or not (0)
1206 * Complete the lock acquisition started our behalf by another thread.
1210 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1212 * Special API call for PI-futex requeue support
1214 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1215 struct hrtimer_sleeper *to,
1216 struct rt_mutex_waiter *waiter,
1217 int detect_deadlock)
1221 raw_spin_lock(&lock->wait_lock);
1223 set_current_state(TASK_INTERRUPTIBLE);
1225 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1227 set_current_state(TASK_RUNNING);
1230 remove_waiter(lock, waiter);
1233 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1234 * have to fix that up.
1236 fixup_rt_mutex_waiters(lock);
1238 raw_spin_unlock(&lock->wait_lock);