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 if (!rt_mutex_has_waiters(lock))
67 clear_rt_mutex_waiters(lock);
71 * We can speed up the acquire/release, if the architecture
72 * supports cmpxchg and if there's no debugging state to be set up
74 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
75 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
76 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
78 unsigned long owner, *p = (unsigned long *) &lock->owner;
82 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
85 # define rt_mutex_cmpxchg(l,c,n) (0)
86 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
88 lock->owner = (struct task_struct *)
89 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
94 * Calculate task priority from the waiter list priority
96 * Return task->normal_prio when the waiter list is empty or when
97 * the waiter is not allowed to do priority boosting
99 int rt_mutex_getprio(struct task_struct *task)
101 if (likely(!task_has_pi_waiters(task)))
102 return task->normal_prio;
104 return min(task_top_pi_waiter(task)->pi_list_entry.prio,
109 * Adjust the priority of a task, after its pi_waiters got modified.
111 * This can be both boosting and unboosting. task->pi_lock must be held.
113 static void __rt_mutex_adjust_prio(struct task_struct *task)
115 int prio = rt_mutex_getprio(task);
117 if (task->prio != prio)
118 rt_mutex_setprio(task, prio);
122 * Adjust task priority (undo boosting). Called from the exit path of
123 * rt_mutex_slowunlock() and rt_mutex_slowlock().
125 * (Note: We do this outside of the protection of lock->wait_lock to
126 * allow the lock to be taken while or before we readjust the priority
127 * of task. We do not use the spin_xx_mutex() variants here as we are
128 * outside of the debug path.)
130 static void rt_mutex_adjust_prio(struct task_struct *task)
134 raw_spin_lock_irqsave(&task->pi_lock, flags);
135 __rt_mutex_adjust_prio(task);
136 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
140 * Max number of times we'll walk the boosting chain:
142 int max_lock_depth = 1024;
144 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
146 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
150 * Adjust the priority chain. Also used for deadlock detection.
151 * Decreases task's usage by one - may thus free the task.
153 * @task: the task owning the mutex (owner) for which a chain walk is
155 * @deadlock_detect: do we have to carry out deadlock detection?
156 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
157 * things for a task that has just got its priority adjusted, and
158 * is waiting on a mutex)
159 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
160 * we dropped its pi_lock. Is never dereferenced, only used for
161 * comparison to detect lock chain changes.
162 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
163 * its priority to the mutex owner (can be NULL in the case
164 * depicted above or if the top waiter is gone away and we are
165 * actually deboosting the owner)
166 * @top_task: the current top waiter
168 * Returns 0 or -EDEADLK.
170 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
172 struct rt_mutex *orig_lock,
173 struct rt_mutex *next_lock,
174 struct rt_mutex_waiter *orig_waiter,
175 struct task_struct *top_task)
177 struct rt_mutex *lock;
178 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
179 int detect_deadlock, ret = 0, depth = 0;
182 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
186 * The (de)boosting is a step by step approach with a lot of
187 * pitfalls. We want this to be preemptible and we want hold a
188 * maximum of two locks per step. So we have to check
189 * carefully whether things change under us.
192 if (++depth > max_lock_depth) {
196 * Print this only once. If the admin changes the limit,
197 * print a new message when reaching the limit again.
199 if (prev_max != max_lock_depth) {
200 prev_max = max_lock_depth;
201 printk(KERN_WARNING "Maximum lock depth %d reached "
202 "task: %s (%d)\n", max_lock_depth,
203 top_task->comm, task_pid_nr(top_task));
205 put_task_struct(task);
211 * Task can not go away as we did a get_task() before !
213 raw_spin_lock_irqsave(&task->pi_lock, flags);
215 waiter = task->pi_blocked_on;
217 * Check whether the end of the boosting chain has been
218 * reached or the state of the chain has changed while we
225 * Check the orig_waiter state. After we dropped the locks,
226 * the previous owner of the lock might have released the lock.
228 if (orig_waiter && !rt_mutex_owner(orig_lock))
232 * We dropped all locks after taking a refcount on @task, so
233 * the task might have moved on in the lock chain or even left
234 * the chain completely and blocks now on an unrelated lock or
237 * We stored the lock on which @task was blocked in @next_lock,
238 * so we can detect the chain change.
240 if (next_lock != waiter->lock)
244 * Drop out, when the task has no waiters. Note,
245 * top_waiter can be NULL, when we are in the deboosting
249 if (!task_has_pi_waiters(task))
252 * If deadlock detection is off, we stop here if we
253 * are not the top pi waiter of the task.
255 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
260 * When deadlock detection is off then we check, if further
261 * priority adjustment is necessary.
263 if (!detect_deadlock && waiter->list_entry.prio == task->prio)
267 if (!raw_spin_trylock(&lock->wait_lock)) {
268 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
274 * Deadlock detection. If the lock is the same as the original
275 * lock which caused us to walk the lock chain or if the
276 * current lock is owned by the task which initiated the chain
277 * walk, we detected a deadlock.
279 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
280 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
281 raw_spin_unlock(&lock->wait_lock);
286 top_waiter = rt_mutex_top_waiter(lock);
288 /* Requeue the waiter */
289 plist_del(&waiter->list_entry, &lock->wait_list);
290 waiter->list_entry.prio = task->prio;
291 plist_add(&waiter->list_entry, &lock->wait_list);
293 /* Release the task */
294 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
295 if (!rt_mutex_owner(lock)) {
297 * If the requeue above changed the top waiter, then we need
298 * to wake the new top waiter up to try to get the lock.
301 if (top_waiter != rt_mutex_top_waiter(lock))
302 wake_up_process(rt_mutex_top_waiter(lock)->task);
303 raw_spin_unlock(&lock->wait_lock);
306 put_task_struct(task);
308 /* Grab the next task */
309 task = rt_mutex_owner(lock);
310 get_task_struct(task);
311 raw_spin_lock_irqsave(&task->pi_lock, flags);
313 if (waiter == rt_mutex_top_waiter(lock)) {
314 /* Boost the owner */
315 plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
316 waiter->pi_list_entry.prio = waiter->list_entry.prio;
317 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
318 __rt_mutex_adjust_prio(task);
320 } else if (top_waiter == waiter) {
321 /* Deboost the owner */
322 plist_del(&waiter->pi_list_entry, &task->pi_waiters);
323 waiter = rt_mutex_top_waiter(lock);
324 waiter->pi_list_entry.prio = waiter->list_entry.prio;
325 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
326 __rt_mutex_adjust_prio(task);
330 * Check whether the task which owns the current lock is pi
331 * blocked itself. If yes we store a pointer to the lock for
332 * the lock chain change detection above. After we dropped
333 * task->pi_lock next_lock cannot be dereferenced anymore.
335 next_lock = task_blocked_on_lock(task);
337 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
339 top_waiter = rt_mutex_top_waiter(lock);
340 raw_spin_unlock(&lock->wait_lock);
343 * We reached the end of the lock chain. Stop right here. No
344 * point to go back just to figure that out.
349 if (!detect_deadlock && waiter != top_waiter)
355 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
357 put_task_struct(task);
363 * Try to take an rt-mutex
365 * Must be called with lock->wait_lock held.
367 * @lock: the lock to be acquired.
368 * @task: the task which wants to acquire the lock
369 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
371 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
372 struct rt_mutex_waiter *waiter)
375 * We have to be careful here if the atomic speedups are
376 * enabled, such that, when
377 * - no other waiter is on the lock
378 * - the lock has been released since we did the cmpxchg
379 * the lock can be released or taken while we are doing the
380 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
382 * The atomic acquire/release aware variant of
383 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
384 * the WAITERS bit, the atomic release / acquire can not
385 * happen anymore and lock->wait_lock protects us from the
388 * Note, that this might set lock->owner =
389 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
390 * any more. This is fixed up when we take the ownership.
391 * This is the transitional state explained at the top of this file.
393 mark_rt_mutex_waiters(lock);
395 if (rt_mutex_owner(lock))
399 * It will get the lock because of one of these conditions:
400 * 1) there is no waiter
401 * 2) higher priority than waiters
402 * 3) it is top waiter
404 if (rt_mutex_has_waiters(lock)) {
405 if (task->prio >= rt_mutex_top_waiter(lock)->list_entry.prio) {
406 if (!waiter || waiter != rt_mutex_top_waiter(lock))
411 if (waiter || rt_mutex_has_waiters(lock)) {
413 struct rt_mutex_waiter *top;
415 raw_spin_lock_irqsave(&task->pi_lock, flags);
417 /* remove the queued waiter. */
419 plist_del(&waiter->list_entry, &lock->wait_list);
420 task->pi_blocked_on = NULL;
424 * We have to enqueue the top waiter(if it exists) into
425 * task->pi_waiters list.
427 if (rt_mutex_has_waiters(lock)) {
428 top = rt_mutex_top_waiter(lock);
429 top->pi_list_entry.prio = top->list_entry.prio;
430 plist_add(&top->pi_list_entry, &task->pi_waiters);
432 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
435 /* We got the lock. */
436 debug_rt_mutex_lock(lock);
438 rt_mutex_set_owner(lock, task);
440 rt_mutex_deadlock_account_lock(lock, task);
446 * Task blocks on lock.
448 * Prepare waiter and propagate pi chain
450 * This must be called with lock->wait_lock held.
452 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
453 struct rt_mutex_waiter *waiter,
454 struct task_struct *task,
457 struct task_struct *owner = rt_mutex_owner(lock);
458 struct rt_mutex_waiter *top_waiter = waiter;
459 struct rt_mutex *next_lock;
460 int chain_walk = 0, res;
464 * Early deadlock detection. We really don't want the task to
465 * enqueue on itself just to untangle the mess later. It's not
466 * only an optimization. We drop the locks, so another waiter
467 * can come in before the chain walk detects the deadlock. So
468 * the other will detect the deadlock and return -EDEADLOCK,
469 * which is wrong, as the other waiter is not in a deadlock
475 raw_spin_lock_irqsave(&task->pi_lock, flags);
476 __rt_mutex_adjust_prio(task);
479 plist_node_init(&waiter->list_entry, task->prio);
480 plist_node_init(&waiter->pi_list_entry, task->prio);
482 /* Get the top priority waiter on the lock */
483 if (rt_mutex_has_waiters(lock))
484 top_waiter = rt_mutex_top_waiter(lock);
485 plist_add(&waiter->list_entry, &lock->wait_list);
487 task->pi_blocked_on = waiter;
489 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
494 raw_spin_lock_irqsave(&owner->pi_lock, flags);
495 if (waiter == rt_mutex_top_waiter(lock)) {
496 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
497 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
499 __rt_mutex_adjust_prio(owner);
500 if (owner->pi_blocked_on)
502 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
506 /* Store the lock on which owner is blocked or NULL */
507 next_lock = task_blocked_on_lock(owner);
509 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
511 * Even if full deadlock detection is on, if the owner is not
512 * blocked itself, we can avoid finding this out in the chain
515 if (!chain_walk || !next_lock)
519 * The owner can't disappear while holding a lock,
520 * so the owner struct is protected by wait_lock.
521 * Gets dropped in rt_mutex_adjust_prio_chain()!
523 get_task_struct(owner);
525 raw_spin_unlock(&lock->wait_lock);
527 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
528 next_lock, waiter, task);
530 raw_spin_lock(&lock->wait_lock);
536 * Wake up the next waiter on the lock.
538 * Remove the top waiter from the current tasks waiter list and wake it up.
540 * Called with lock->wait_lock held.
542 static void wakeup_next_waiter(struct rt_mutex *lock)
544 struct rt_mutex_waiter *waiter;
547 raw_spin_lock_irqsave(¤t->pi_lock, flags);
549 waiter = rt_mutex_top_waiter(lock);
552 * Remove it from current->pi_waiters. We do not adjust a
553 * possible priority boost right now. We execute wakeup in the
554 * boosted mode and go back to normal after releasing
557 plist_del(&waiter->pi_list_entry, ¤t->pi_waiters);
559 rt_mutex_set_owner(lock, NULL);
561 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
563 wake_up_process(waiter->task);
567 * Remove a waiter from a lock and give up
569 * Must be called with lock->wait_lock held and
570 * have just failed to try_to_take_rt_mutex().
572 static void remove_waiter(struct rt_mutex *lock,
573 struct rt_mutex_waiter *waiter)
575 int first = (waiter == rt_mutex_top_waiter(lock));
576 struct task_struct *owner = rt_mutex_owner(lock);
577 struct rt_mutex *next_lock = NULL;
580 raw_spin_lock_irqsave(¤t->pi_lock, flags);
581 plist_del(&waiter->list_entry, &lock->wait_list);
582 current->pi_blocked_on = NULL;
583 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
590 raw_spin_lock_irqsave(&owner->pi_lock, flags);
592 plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
594 if (rt_mutex_has_waiters(lock)) {
595 struct rt_mutex_waiter *next;
597 next = rt_mutex_top_waiter(lock);
598 plist_add(&next->pi_list_entry, &owner->pi_waiters);
600 __rt_mutex_adjust_prio(owner);
602 /* Store the lock on which owner is blocked or NULL */
603 next_lock = task_blocked_on_lock(owner);
605 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
608 WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
613 /* gets dropped in rt_mutex_adjust_prio_chain()! */
614 get_task_struct(owner);
616 raw_spin_unlock(&lock->wait_lock);
618 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
620 raw_spin_lock(&lock->wait_lock);
624 * Recheck the pi chain, in case we got a priority setting
626 * Called from sched_setscheduler
628 void rt_mutex_adjust_pi(struct task_struct *task)
630 struct rt_mutex_waiter *waiter;
631 struct rt_mutex *next_lock;
634 raw_spin_lock_irqsave(&task->pi_lock, flags);
636 waiter = task->pi_blocked_on;
637 if (!waiter || waiter->list_entry.prio == task->prio) {
638 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
641 next_lock = waiter->lock;
642 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
644 /* gets dropped in rt_mutex_adjust_prio_chain()! */
645 get_task_struct(task);
647 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
651 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
652 * @lock: the rt_mutex to take
653 * @state: the state the task should block in (TASK_INTERRUPTIBLE
654 * or TASK_UNINTERRUPTIBLE)
655 * @timeout: the pre-initialized and started timer, or NULL for none
656 * @waiter: the pre-initialized rt_mutex_waiter
658 * lock->wait_lock must be held by the caller.
661 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
662 struct hrtimer_sleeper *timeout,
663 struct rt_mutex_waiter *waiter)
669 /* Try to acquire the lock: */
670 if (try_to_take_rt_mutex(lock, current, waiter))
674 * TASK_INTERRUPTIBLE checks for signals and
675 * timeout. Ignored otherwise.
677 if (unlikely(state == TASK_INTERRUPTIBLE)) {
678 /* Signal pending? */
679 if (signal_pending(current))
681 if (timeout && !timeout->task)
687 raw_spin_unlock(&lock->wait_lock);
689 was_disabled = irqs_disabled();
693 debug_rt_mutex_print_deadlock(waiter);
695 schedule_rt_mutex(lock);
700 raw_spin_lock(&lock->wait_lock);
701 set_current_state(state);
707 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
708 struct rt_mutex_waiter *w)
711 * If the result is not -EDEADLOCK or the caller requested
712 * deadlock detection, nothing to do here.
714 if (res != -EDEADLOCK || detect_deadlock)
718 * Yell lowdly and stop the task right here.
720 rt_mutex_print_deadlock(w);
722 set_current_state(TASK_INTERRUPTIBLE);
728 * Slow path lock function:
731 rt_mutex_slowlock(struct rt_mutex *lock, int state,
732 struct hrtimer_sleeper *timeout,
735 struct rt_mutex_waiter waiter;
738 debug_rt_mutex_init_waiter(&waiter);
740 raw_spin_lock(&lock->wait_lock);
742 /* Try to acquire the lock again: */
743 if (try_to_take_rt_mutex(lock, current, NULL)) {
744 raw_spin_unlock(&lock->wait_lock);
748 set_current_state(state);
750 /* Setup the timer, when timeout != NULL */
751 if (unlikely(timeout)) {
752 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
753 if (!hrtimer_active(&timeout->timer))
754 timeout->task = NULL;
757 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
760 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
762 set_current_state(TASK_RUNNING);
765 remove_waiter(lock, &waiter);
766 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
770 * try_to_take_rt_mutex() sets the waiter bit
771 * unconditionally. We might have to fix that up.
773 fixup_rt_mutex_waiters(lock);
775 raw_spin_unlock(&lock->wait_lock);
777 /* Remove pending timer: */
778 if (unlikely(timeout))
779 hrtimer_cancel(&timeout->timer);
781 debug_rt_mutex_free_waiter(&waiter);
787 * Slow path try-lock function:
790 rt_mutex_slowtrylock(struct rt_mutex *lock)
794 raw_spin_lock(&lock->wait_lock);
796 if (likely(rt_mutex_owner(lock) != current)) {
798 ret = try_to_take_rt_mutex(lock, current, NULL);
800 * try_to_take_rt_mutex() sets the lock waiters
801 * bit unconditionally. Clean this up.
803 fixup_rt_mutex_waiters(lock);
806 raw_spin_unlock(&lock->wait_lock);
812 * Slow path to release a rt-mutex:
815 rt_mutex_slowunlock(struct rt_mutex *lock)
817 raw_spin_lock(&lock->wait_lock);
819 debug_rt_mutex_unlock(lock);
821 rt_mutex_deadlock_account_unlock(current);
823 if (!rt_mutex_has_waiters(lock)) {
825 raw_spin_unlock(&lock->wait_lock);
829 wakeup_next_waiter(lock);
831 raw_spin_unlock(&lock->wait_lock);
833 /* Undo pi boosting if necessary: */
834 rt_mutex_adjust_prio(current);
838 * debug aware fast / slowpath lock,trylock,unlock
840 * The atomic acquire/release ops are compiled away, when either the
841 * architecture does not support cmpxchg or when debugging is enabled.
844 rt_mutex_fastlock(struct rt_mutex *lock, int state,
846 int (*slowfn)(struct rt_mutex *lock, int state,
847 struct hrtimer_sleeper *timeout,
848 int detect_deadlock))
850 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
851 rt_mutex_deadlock_account_lock(lock, current);
854 return slowfn(lock, state, NULL, detect_deadlock);
858 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
859 struct hrtimer_sleeper *timeout, int detect_deadlock,
860 int (*slowfn)(struct rt_mutex *lock, int state,
861 struct hrtimer_sleeper *timeout,
862 int detect_deadlock))
864 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
865 rt_mutex_deadlock_account_lock(lock, current);
868 return slowfn(lock, state, timeout, detect_deadlock);
872 rt_mutex_fasttrylock(struct rt_mutex *lock,
873 int (*slowfn)(struct rt_mutex *lock))
875 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
876 rt_mutex_deadlock_account_lock(lock, current);
883 rt_mutex_fastunlock(struct rt_mutex *lock,
884 void (*slowfn)(struct rt_mutex *lock))
886 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
887 rt_mutex_deadlock_account_unlock(current);
893 * rt_mutex_lock - lock a rt_mutex
895 * @lock: the rt_mutex to be locked
897 void __sched rt_mutex_lock(struct rt_mutex *lock)
901 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
903 EXPORT_SYMBOL_GPL(rt_mutex_lock);
906 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
908 * @lock: the rt_mutex to be locked
909 * @detect_deadlock: deadlock detection on/off
913 * -EINTR when interrupted by a signal
914 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
916 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
921 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
922 detect_deadlock, rt_mutex_slowlock);
924 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
927 * rt_mutex_timed_lock - lock a rt_mutex interruptible
928 * the timeout structure is provided
931 * @lock: the rt_mutex to be locked
932 * @timeout: timeout structure or NULL (no timeout)
933 * @detect_deadlock: deadlock detection on/off
937 * -EINTR when interrupted by a signal
938 * -ETIMEDOUT when the timeout expired
939 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
942 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
947 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
948 detect_deadlock, rt_mutex_slowlock);
950 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
953 * rt_mutex_trylock - try to lock a rt_mutex
955 * @lock: the rt_mutex to be locked
957 * Returns 1 on success and 0 on contention
959 int __sched rt_mutex_trylock(struct rt_mutex *lock)
961 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
963 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
966 * rt_mutex_unlock - unlock a rt_mutex
968 * @lock: the rt_mutex to be unlocked
970 void __sched rt_mutex_unlock(struct rt_mutex *lock)
972 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
974 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
977 * rt_mutex_destroy - mark a mutex unusable
978 * @lock: the mutex to be destroyed
980 * This function marks the mutex uninitialized, and any subsequent
981 * use of the mutex is forbidden. The mutex must not be locked when
982 * this function is called.
984 void rt_mutex_destroy(struct rt_mutex *lock)
986 WARN_ON(rt_mutex_is_locked(lock));
987 #ifdef CONFIG_DEBUG_RT_MUTEXES
992 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
995 * __rt_mutex_init - initialize the rt lock
997 * @lock: the rt lock to be initialized
999 * Initialize the rt lock to unlocked state.
1001 * Initializing of a locked rt lock is not allowed
1003 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1006 raw_spin_lock_init(&lock->wait_lock);
1007 plist_head_init(&lock->wait_list);
1009 debug_rt_mutex_init(lock, name);
1011 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1014 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1017 * @lock: the rt_mutex to be locked
1018 * @proxy_owner:the task to set as owner
1020 * No locking. Caller has to do serializing itself
1021 * Special API call for PI-futex support
1023 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1024 struct task_struct *proxy_owner)
1026 __rt_mutex_init(lock, NULL);
1027 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1028 rt_mutex_set_owner(lock, proxy_owner);
1029 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1033 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1035 * @lock: the rt_mutex to be locked
1037 * No locking. Caller has to do serializing itself
1038 * Special API call for PI-futex support
1040 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1041 struct task_struct *proxy_owner)
1043 debug_rt_mutex_proxy_unlock(lock);
1044 rt_mutex_set_owner(lock, NULL);
1045 rt_mutex_deadlock_account_unlock(proxy_owner);
1049 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1050 * @lock: the rt_mutex to take
1051 * @waiter: the pre-initialized rt_mutex_waiter
1052 * @task: the task to prepare
1053 * @detect_deadlock: perform deadlock detection (1) or not (0)
1056 * 0 - task blocked on lock
1057 * 1 - acquired the lock for task, caller should wake it up
1060 * Special API call for FUTEX_REQUEUE_PI support.
1062 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1063 struct rt_mutex_waiter *waiter,
1064 struct task_struct *task, int detect_deadlock)
1068 raw_spin_lock(&lock->wait_lock);
1070 if (try_to_take_rt_mutex(lock, task, NULL)) {
1071 raw_spin_unlock(&lock->wait_lock);
1075 /* We enforce deadlock detection for futexes */
1076 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1078 if (ret && !rt_mutex_owner(lock)) {
1080 * Reset the return value. We might have
1081 * returned with -EDEADLK and the owner
1082 * released the lock while we were walking the
1083 * pi chain. Let the waiter sort it out.
1089 remove_waiter(lock, waiter);
1091 raw_spin_unlock(&lock->wait_lock);
1093 debug_rt_mutex_print_deadlock(waiter);
1099 * rt_mutex_next_owner - return the next owner of the lock
1101 * @lock: the rt lock query
1103 * Returns the next owner of the lock or NULL
1105 * Caller has to serialize against other accessors to the lock
1108 * Special API call for PI-futex support
1110 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1112 if (!rt_mutex_has_waiters(lock))
1115 return rt_mutex_top_waiter(lock)->task;
1119 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1120 * @lock: the rt_mutex we were woken on
1121 * @to: the timeout, null if none. hrtimer should already have
1123 * @waiter: the pre-initialized rt_mutex_waiter
1124 * @detect_deadlock: perform deadlock detection (1) or not (0)
1126 * Complete the lock acquisition started our behalf by another thread.
1130 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1132 * Special API call for PI-futex requeue support
1134 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1135 struct hrtimer_sleeper *to,
1136 struct rt_mutex_waiter *waiter,
1137 int detect_deadlock)
1141 raw_spin_lock(&lock->wait_lock);
1143 set_current_state(TASK_INTERRUPTIBLE);
1145 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1147 set_current_state(TASK_RUNNING);
1150 remove_waiter(lock, waiter);
1153 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1154 * have to fix that up.
1156 fixup_rt_mutex_waiters(lock);
1158 raw_spin_unlock(&lock->wait_lock);