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/sched/deadline.h>
18 #include <linux/timer.h>
20 #include "rtmutex_common.h"
23 * lock->owner state tracking:
25 * lock->owner holds the task_struct pointer of the owner. Bit 0
26 * is used to keep track of the "lock has waiters" state.
29 * NULL 0 lock is free (fast acquire possible)
30 * NULL 1 lock is free and has waiters and the top waiter
31 * is going to take the lock*
32 * taskpointer 0 lock is held (fast release possible)
33 * taskpointer 1 lock is held and has waiters**
35 * The fast atomic compare exchange based acquire and release is only
36 * possible when bit 0 of lock->owner is 0.
38 * (*) It also can be a transitional state when grabbing the lock
39 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
40 * we need to set the bit0 before looking at the lock, and the owner may be
41 * NULL in this small time, hence this can be a transitional state.
43 * (**) There is a small time when bit 0 is set but there are no
44 * waiters. This can happen when grabbing the lock in the slow path.
45 * To prevent a cmpxchg of the owner releasing the lock, we need to
46 * set this bit before looking at the lock.
50 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
52 unsigned long val = (unsigned long)owner;
54 if (rt_mutex_has_waiters(lock))
55 val |= RT_MUTEX_HAS_WAITERS;
57 lock->owner = (struct task_struct *)val;
60 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
62 lock->owner = (struct task_struct *)
63 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
66 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
68 if (!rt_mutex_has_waiters(lock))
69 clear_rt_mutex_waiters(lock);
73 * We can speed up the acquire/release, if the architecture
74 * supports cmpxchg and if there's no debugging state to be set up
76 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
77 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
78 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
80 unsigned long owner, *p = (unsigned long *) &lock->owner;
84 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
87 # define rt_mutex_cmpxchg(l,c,n) (0)
88 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
90 lock->owner = (struct task_struct *)
91 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
96 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
97 struct rt_mutex_waiter *right)
99 if (left->prio < right->prio)
103 * If both waiters have dl_prio(), we check the deadlines of the
105 * If left waiter has a dl_prio(), and we didn't return 1 above,
106 * then right waiter has a dl_prio() too.
108 if (dl_prio(left->prio))
109 return (left->task->dl.deadline < right->task->dl.deadline);
115 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
117 struct rb_node **link = &lock->waiters.rb_node;
118 struct rb_node *parent = NULL;
119 struct rt_mutex_waiter *entry;
124 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
125 if (rt_mutex_waiter_less(waiter, entry)) {
126 link = &parent->rb_left;
128 link = &parent->rb_right;
134 lock->waiters_leftmost = &waiter->tree_entry;
136 rb_link_node(&waiter->tree_entry, parent, link);
137 rb_insert_color(&waiter->tree_entry, &lock->waiters);
141 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
143 if (RB_EMPTY_NODE(&waiter->tree_entry))
146 if (lock->waiters_leftmost == &waiter->tree_entry)
147 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
149 rb_erase(&waiter->tree_entry, &lock->waiters);
150 RB_CLEAR_NODE(&waiter->tree_entry);
154 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
156 struct rb_node **link = &task->pi_waiters.rb_node;
157 struct rb_node *parent = NULL;
158 struct rt_mutex_waiter *entry;
163 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
164 if (rt_mutex_waiter_less(waiter, entry)) {
165 link = &parent->rb_left;
167 link = &parent->rb_right;
173 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
175 rb_link_node(&waiter->pi_tree_entry, parent, link);
176 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
180 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
182 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
185 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
186 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
188 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
189 RB_CLEAR_NODE(&waiter->pi_tree_entry);
193 * Calculate task priority from the waiter tree priority
195 * Return task->normal_prio when the waiter tree is empty or when
196 * the waiter is not allowed to do priority boosting
198 int rt_mutex_getprio(struct task_struct *task)
200 if (likely(!task_has_pi_waiters(task)))
201 return task->normal_prio;
203 return min(task_top_pi_waiter(task)->prio,
207 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
209 if (likely(!task_has_pi_waiters(task)))
212 return task_top_pi_waiter(task)->task;
216 * Adjust the priority of a task, after its pi_waiters got modified.
218 * This can be both boosting and unboosting. task->pi_lock must be held.
220 static void __rt_mutex_adjust_prio(struct task_struct *task)
222 int prio = rt_mutex_getprio(task);
224 if (task->prio != prio || dl_prio(prio))
225 rt_mutex_setprio(task, prio);
229 * Adjust task priority (undo boosting). Called from the exit path of
230 * rt_mutex_slowunlock() and rt_mutex_slowlock().
232 * (Note: We do this outside of the protection of lock->wait_lock to
233 * allow the lock to be taken while or before we readjust the priority
234 * of task. We do not use the spin_xx_mutex() variants here as we are
235 * outside of the debug path.)
237 static void rt_mutex_adjust_prio(struct task_struct *task)
241 raw_spin_lock_irqsave(&task->pi_lock, flags);
242 __rt_mutex_adjust_prio(task);
243 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
247 * Max number of times we'll walk the boosting chain:
249 int max_lock_depth = 1024;
252 * Adjust the priority chain. Also used for deadlock detection.
253 * Decreases task's usage by one - may thus free the task.
255 * @task: the task owning the mutex (owner) for which a chain walk is probably
257 * @deadlock_detect: do we have to carry out deadlock detection?
258 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
259 * things for a task that has just got its priority adjusted, and
260 * is waiting on a mutex)
261 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
262 * its priority to the mutex owner (can be NULL in the case
263 * depicted above or if the top waiter is gone away and we are
264 * actually deboosting the owner)
265 * @top_task: the current top waiter
267 * Returns 0 or -EDEADLK.
269 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
271 struct rt_mutex *orig_lock,
272 struct rt_mutex_waiter *orig_waiter,
273 struct task_struct *top_task)
275 struct rt_mutex *lock;
276 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
277 int detect_deadlock, ret = 0, depth = 0;
280 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
284 * The (de)boosting is a step by step approach with a lot of
285 * pitfalls. We want this to be preemptible and we want hold a
286 * maximum of two locks per step. So we have to check
287 * carefully whether things change under us.
290 if (++depth > max_lock_depth) {
294 * Print this only once. If the admin changes the limit,
295 * print a new message when reaching the limit again.
297 if (prev_max != max_lock_depth) {
298 prev_max = max_lock_depth;
299 printk(KERN_WARNING "Maximum lock depth %d reached "
300 "task: %s (%d)\n", max_lock_depth,
301 top_task->comm, task_pid_nr(top_task));
303 put_task_struct(task);
305 return deadlock_detect ? -EDEADLK : 0;
309 * Task can not go away as we did a get_task() before !
311 raw_spin_lock_irqsave(&task->pi_lock, flags);
313 waiter = task->pi_blocked_on;
315 * Check whether the end of the boosting chain has been
316 * reached or the state of the chain has changed while we
323 * Check the orig_waiter state. After we dropped the locks,
324 * the previous owner of the lock might have released the lock.
326 if (orig_waiter && !rt_mutex_owner(orig_lock))
330 * Drop out, when the task has no waiters. Note,
331 * top_waiter can be NULL, when we are in the deboosting
335 if (!task_has_pi_waiters(task))
338 * If deadlock detection is off, we stop here if we
339 * are not the top pi waiter of the task.
341 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
346 * When deadlock detection is off then we check, if further
347 * priority adjustment is necessary.
349 if (!detect_deadlock && waiter->prio == task->prio)
353 if (!raw_spin_trylock(&lock->wait_lock)) {
354 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
360 * Deadlock detection. If the lock is the same as the original
361 * lock which caused us to walk the lock chain or if the
362 * current lock is owned by the task which initiated the chain
363 * walk, we detected a deadlock.
365 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
366 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
367 raw_spin_unlock(&lock->wait_lock);
368 ret = deadlock_detect ? -EDEADLK : 0;
372 top_waiter = rt_mutex_top_waiter(lock);
374 /* Requeue the waiter */
375 rt_mutex_dequeue(lock, waiter);
376 waiter->prio = task->prio;
377 rt_mutex_enqueue(lock, waiter);
379 /* Release the task */
380 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
381 if (!rt_mutex_owner(lock)) {
383 * If the requeue above changed the top waiter, then we need
384 * to wake the new top waiter up to try to get the lock.
387 if (top_waiter != rt_mutex_top_waiter(lock))
388 wake_up_process(rt_mutex_top_waiter(lock)->task);
389 raw_spin_unlock(&lock->wait_lock);
392 put_task_struct(task);
394 /* Grab the next task */
395 task = rt_mutex_owner(lock);
396 get_task_struct(task);
397 raw_spin_lock_irqsave(&task->pi_lock, flags);
399 if (waiter == rt_mutex_top_waiter(lock)) {
400 /* Boost the owner */
401 rt_mutex_dequeue_pi(task, top_waiter);
402 rt_mutex_enqueue_pi(task, waiter);
403 __rt_mutex_adjust_prio(task);
405 } else if (top_waiter == waiter) {
406 /* Deboost the owner */
407 rt_mutex_dequeue_pi(task, waiter);
408 waiter = rt_mutex_top_waiter(lock);
409 rt_mutex_enqueue_pi(task, waiter);
410 __rt_mutex_adjust_prio(task);
413 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
415 top_waiter = rt_mutex_top_waiter(lock);
416 raw_spin_unlock(&lock->wait_lock);
418 if (!detect_deadlock && waiter != top_waiter)
424 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
426 put_task_struct(task);
432 * Try to take an rt-mutex
434 * Must be called with lock->wait_lock held.
436 * @lock: the lock to be acquired.
437 * @task: the task which wants to acquire the lock
438 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
440 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
441 struct rt_mutex_waiter *waiter)
444 * We have to be careful here if the atomic speedups are
445 * enabled, such that, when
446 * - no other waiter is on the lock
447 * - the lock has been released since we did the cmpxchg
448 * the lock can be released or taken while we are doing the
449 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
451 * The atomic acquire/release aware variant of
452 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
453 * the WAITERS bit, the atomic release / acquire can not
454 * happen anymore and lock->wait_lock protects us from the
457 * Note, that this might set lock->owner =
458 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
459 * any more. This is fixed up when we take the ownership.
460 * This is the transitional state explained at the top of this file.
462 mark_rt_mutex_waiters(lock);
464 if (rt_mutex_owner(lock))
468 * It will get the lock because of one of these conditions:
469 * 1) there is no waiter
470 * 2) higher priority than waiters
471 * 3) it is top waiter
473 if (rt_mutex_has_waiters(lock)) {
474 if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
475 if (!waiter || waiter != rt_mutex_top_waiter(lock))
480 if (waiter || rt_mutex_has_waiters(lock)) {
482 struct rt_mutex_waiter *top;
484 raw_spin_lock_irqsave(&task->pi_lock, flags);
486 /* remove the queued waiter. */
488 rt_mutex_dequeue(lock, waiter);
489 task->pi_blocked_on = NULL;
493 * We have to enqueue the top waiter(if it exists) into
494 * task->pi_waiters list.
496 if (rt_mutex_has_waiters(lock)) {
497 top = rt_mutex_top_waiter(lock);
498 rt_mutex_enqueue_pi(task, top);
500 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
503 /* We got the lock. */
504 debug_rt_mutex_lock(lock);
506 rt_mutex_set_owner(lock, task);
508 rt_mutex_deadlock_account_lock(lock, task);
514 * Task blocks on lock.
516 * Prepare waiter and propagate pi chain
518 * This must be called with lock->wait_lock held.
520 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
521 struct rt_mutex_waiter *waiter,
522 struct task_struct *task,
525 struct task_struct *owner = rt_mutex_owner(lock);
526 struct rt_mutex_waiter *top_waiter = waiter;
528 int chain_walk = 0, res;
531 * Early deadlock detection. We really don't want the task to
532 * enqueue on itself just to untangle the mess later. It's not
533 * only an optimization. We drop the locks, so another waiter
534 * can come in before the chain walk detects the deadlock. So
535 * the other will detect the deadlock and return -EDEADLOCK,
536 * which is wrong, as the other waiter is not in a deadlock
539 if (detect_deadlock && owner == task)
542 raw_spin_lock_irqsave(&task->pi_lock, flags);
543 __rt_mutex_adjust_prio(task);
546 waiter->prio = task->prio;
548 /* Get the top priority waiter on the lock */
549 if (rt_mutex_has_waiters(lock))
550 top_waiter = rt_mutex_top_waiter(lock);
551 rt_mutex_enqueue(lock, waiter);
553 task->pi_blocked_on = waiter;
555 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
560 if (waiter == rt_mutex_top_waiter(lock)) {
561 raw_spin_lock_irqsave(&owner->pi_lock, flags);
562 rt_mutex_dequeue_pi(owner, top_waiter);
563 rt_mutex_enqueue_pi(owner, waiter);
565 __rt_mutex_adjust_prio(owner);
566 if (owner->pi_blocked_on)
568 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
570 else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock))
577 * The owner can't disappear while holding a lock,
578 * so the owner struct is protected by wait_lock.
579 * Gets dropped in rt_mutex_adjust_prio_chain()!
581 get_task_struct(owner);
583 raw_spin_unlock(&lock->wait_lock);
585 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
588 raw_spin_lock(&lock->wait_lock);
594 * Wake up the next waiter on the lock.
596 * Remove the top waiter from the current tasks waiter list and wake it up.
598 * Called with lock->wait_lock held.
600 static void wakeup_next_waiter(struct rt_mutex *lock)
602 struct rt_mutex_waiter *waiter;
605 raw_spin_lock_irqsave(¤t->pi_lock, flags);
607 waiter = rt_mutex_top_waiter(lock);
610 * Remove it from current->pi_waiters. We do not adjust a
611 * possible priority boost right now. We execute wakeup in the
612 * boosted mode and go back to normal after releasing
615 rt_mutex_dequeue_pi(current, waiter);
617 rt_mutex_set_owner(lock, NULL);
619 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
621 wake_up_process(waiter->task);
625 * Remove a waiter from a lock and give up
627 * Must be called with lock->wait_lock held and
628 * have just failed to try_to_take_rt_mutex().
630 static void remove_waiter(struct rt_mutex *lock,
631 struct rt_mutex_waiter *waiter)
633 int first = (waiter == rt_mutex_top_waiter(lock));
634 struct task_struct *owner = rt_mutex_owner(lock);
638 raw_spin_lock_irqsave(¤t->pi_lock, flags);
639 rt_mutex_dequeue(lock, waiter);
640 current->pi_blocked_on = NULL;
641 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
648 raw_spin_lock_irqsave(&owner->pi_lock, flags);
650 rt_mutex_dequeue_pi(owner, waiter);
652 if (rt_mutex_has_waiters(lock)) {
653 struct rt_mutex_waiter *next;
655 next = rt_mutex_top_waiter(lock);
656 rt_mutex_enqueue_pi(owner, next);
658 __rt_mutex_adjust_prio(owner);
660 if (owner->pi_blocked_on)
663 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
669 /* gets dropped in rt_mutex_adjust_prio_chain()! */
670 get_task_struct(owner);
672 raw_spin_unlock(&lock->wait_lock);
674 rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
676 raw_spin_lock(&lock->wait_lock);
680 * Recheck the pi chain, in case we got a priority setting
682 * Called from sched_setscheduler
684 void rt_mutex_adjust_pi(struct task_struct *task)
686 struct rt_mutex_waiter *waiter;
689 raw_spin_lock_irqsave(&task->pi_lock, flags);
691 waiter = task->pi_blocked_on;
692 if (!waiter || (waiter->prio == task->prio &&
693 !dl_prio(task->prio))) {
694 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
698 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
700 /* gets dropped in rt_mutex_adjust_prio_chain()! */
701 get_task_struct(task);
702 rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
706 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
707 * @lock: the rt_mutex to take
708 * @state: the state the task should block in (TASK_INTERRUPTIBLE
709 * or TASK_UNINTERRUPTIBLE)
710 * @timeout: the pre-initialized and started timer, or NULL for none
711 * @waiter: the pre-initialized rt_mutex_waiter
713 * lock->wait_lock must be held by the caller.
716 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
717 struct hrtimer_sleeper *timeout,
718 struct rt_mutex_waiter *waiter)
723 /* Try to acquire the lock: */
724 if (try_to_take_rt_mutex(lock, current, waiter))
728 * TASK_INTERRUPTIBLE checks for signals and
729 * timeout. Ignored otherwise.
731 if (unlikely(state == TASK_INTERRUPTIBLE)) {
732 /* Signal pending? */
733 if (signal_pending(current))
735 if (timeout && !timeout->task)
741 raw_spin_unlock(&lock->wait_lock);
743 debug_rt_mutex_print_deadlock(waiter);
745 schedule_rt_mutex(lock);
747 raw_spin_lock(&lock->wait_lock);
748 set_current_state(state);
755 * Slow path lock function:
758 rt_mutex_slowlock(struct rt_mutex *lock, int state,
759 struct hrtimer_sleeper *timeout,
762 struct rt_mutex_waiter waiter;
765 debug_rt_mutex_init_waiter(&waiter);
766 RB_CLEAR_NODE(&waiter.pi_tree_entry);
767 RB_CLEAR_NODE(&waiter.tree_entry);
769 raw_spin_lock(&lock->wait_lock);
771 /* Try to acquire the lock again: */
772 if (try_to_take_rt_mutex(lock, current, NULL)) {
773 raw_spin_unlock(&lock->wait_lock);
777 set_current_state(state);
779 /* Setup the timer, when timeout != NULL */
780 if (unlikely(timeout)) {
781 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
782 if (!hrtimer_active(&timeout->timer))
783 timeout->task = NULL;
786 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
789 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
791 set_current_state(TASK_RUNNING);
794 remove_waiter(lock, &waiter);
797 * try_to_take_rt_mutex() sets the waiter bit
798 * unconditionally. We might have to fix that up.
800 fixup_rt_mutex_waiters(lock);
802 raw_spin_unlock(&lock->wait_lock);
804 /* Remove pending timer: */
805 if (unlikely(timeout))
806 hrtimer_cancel(&timeout->timer);
808 debug_rt_mutex_free_waiter(&waiter);
814 * Slow path try-lock function:
817 rt_mutex_slowtrylock(struct rt_mutex *lock)
821 raw_spin_lock(&lock->wait_lock);
823 if (likely(rt_mutex_owner(lock) != current)) {
825 ret = try_to_take_rt_mutex(lock, current, NULL);
827 * try_to_take_rt_mutex() sets the lock waiters
828 * bit unconditionally. Clean this up.
830 fixup_rt_mutex_waiters(lock);
833 raw_spin_unlock(&lock->wait_lock);
839 * Slow path to release a rt-mutex:
842 rt_mutex_slowunlock(struct rt_mutex *lock)
844 raw_spin_lock(&lock->wait_lock);
846 debug_rt_mutex_unlock(lock);
848 rt_mutex_deadlock_account_unlock(current);
850 if (!rt_mutex_has_waiters(lock)) {
852 raw_spin_unlock(&lock->wait_lock);
856 wakeup_next_waiter(lock);
858 raw_spin_unlock(&lock->wait_lock);
860 /* Undo pi boosting if necessary: */
861 rt_mutex_adjust_prio(current);
865 * debug aware fast / slowpath lock,trylock,unlock
867 * The atomic acquire/release ops are compiled away, when either the
868 * architecture does not support cmpxchg or when debugging is enabled.
871 rt_mutex_fastlock(struct rt_mutex *lock, int state,
873 int (*slowfn)(struct rt_mutex *lock, int state,
874 struct hrtimer_sleeper *timeout,
875 int detect_deadlock))
877 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
878 rt_mutex_deadlock_account_lock(lock, current);
881 return slowfn(lock, state, NULL, detect_deadlock);
885 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
886 struct hrtimer_sleeper *timeout, int detect_deadlock,
887 int (*slowfn)(struct rt_mutex *lock, int state,
888 struct hrtimer_sleeper *timeout,
889 int detect_deadlock))
891 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
892 rt_mutex_deadlock_account_lock(lock, current);
895 return slowfn(lock, state, timeout, detect_deadlock);
899 rt_mutex_fasttrylock(struct rt_mutex *lock,
900 int (*slowfn)(struct rt_mutex *lock))
902 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
903 rt_mutex_deadlock_account_lock(lock, current);
910 rt_mutex_fastunlock(struct rt_mutex *lock,
911 void (*slowfn)(struct rt_mutex *lock))
913 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
914 rt_mutex_deadlock_account_unlock(current);
920 * rt_mutex_lock - lock a rt_mutex
922 * @lock: the rt_mutex to be locked
924 void __sched rt_mutex_lock(struct rt_mutex *lock)
928 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
930 EXPORT_SYMBOL_GPL(rt_mutex_lock);
933 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
935 * @lock: the rt_mutex to be locked
936 * @detect_deadlock: deadlock detection on/off
940 * -EINTR when interrupted by a signal
941 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
943 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
948 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
949 detect_deadlock, rt_mutex_slowlock);
951 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
954 * rt_mutex_timed_lock - lock a rt_mutex interruptible
955 * the timeout structure is provided
958 * @lock: the rt_mutex to be locked
959 * @timeout: timeout structure or NULL (no timeout)
960 * @detect_deadlock: deadlock detection on/off
964 * -EINTR when interrupted by a signal
965 * -ETIMEDOUT when the timeout expired
966 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
969 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
974 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
975 detect_deadlock, rt_mutex_slowlock);
977 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
980 * rt_mutex_trylock - try to lock a rt_mutex
982 * @lock: the rt_mutex to be locked
984 * Returns 1 on success and 0 on contention
986 int __sched rt_mutex_trylock(struct rt_mutex *lock)
988 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
990 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
993 * rt_mutex_unlock - unlock a rt_mutex
995 * @lock: the rt_mutex to be unlocked
997 void __sched rt_mutex_unlock(struct rt_mutex *lock)
999 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1001 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1004 * rt_mutex_destroy - mark a mutex unusable
1005 * @lock: the mutex to be destroyed
1007 * This function marks the mutex uninitialized, and any subsequent
1008 * use of the mutex is forbidden. The mutex must not be locked when
1009 * this function is called.
1011 void rt_mutex_destroy(struct rt_mutex *lock)
1013 WARN_ON(rt_mutex_is_locked(lock));
1014 #ifdef CONFIG_DEBUG_RT_MUTEXES
1019 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1022 * __rt_mutex_init - initialize the rt lock
1024 * @lock: the rt lock to be initialized
1026 * Initialize the rt lock to unlocked state.
1028 * Initializing of a locked rt lock is not allowed
1030 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1033 raw_spin_lock_init(&lock->wait_lock);
1034 lock->waiters = RB_ROOT;
1035 lock->waiters_leftmost = NULL;
1037 debug_rt_mutex_init(lock, name);
1039 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1042 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1045 * @lock: the rt_mutex to be locked
1046 * @proxy_owner:the task to set as owner
1048 * No locking. Caller has to do serializing itself
1049 * Special API call for PI-futex support
1051 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1052 struct task_struct *proxy_owner)
1054 __rt_mutex_init(lock, NULL);
1055 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1056 rt_mutex_set_owner(lock, proxy_owner);
1057 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1061 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1063 * @lock: the rt_mutex to be locked
1065 * No locking. Caller has to do serializing itself
1066 * Special API call for PI-futex support
1068 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1069 struct task_struct *proxy_owner)
1071 debug_rt_mutex_proxy_unlock(lock);
1072 rt_mutex_set_owner(lock, NULL);
1073 rt_mutex_deadlock_account_unlock(proxy_owner);
1077 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1078 * @lock: the rt_mutex to take
1079 * @waiter: the pre-initialized rt_mutex_waiter
1080 * @task: the task to prepare
1081 * @detect_deadlock: perform deadlock detection (1) or not (0)
1084 * 0 - task blocked on lock
1085 * 1 - acquired the lock for task, caller should wake it up
1088 * Special API call for FUTEX_REQUEUE_PI support.
1090 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1091 struct rt_mutex_waiter *waiter,
1092 struct task_struct *task, int detect_deadlock)
1096 raw_spin_lock(&lock->wait_lock);
1098 if (try_to_take_rt_mutex(lock, task, NULL)) {
1099 raw_spin_unlock(&lock->wait_lock);
1103 ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
1105 if (ret && !rt_mutex_owner(lock)) {
1107 * Reset the return value. We might have
1108 * returned with -EDEADLK and the owner
1109 * released the lock while we were walking the
1110 * pi chain. Let the waiter sort it out.
1116 remove_waiter(lock, waiter);
1118 raw_spin_unlock(&lock->wait_lock);
1120 debug_rt_mutex_print_deadlock(waiter);
1126 * rt_mutex_next_owner - return the next owner of the lock
1128 * @lock: the rt lock query
1130 * Returns the next owner of the lock or NULL
1132 * Caller has to serialize against other accessors to the lock
1135 * Special API call for PI-futex support
1137 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1139 if (!rt_mutex_has_waiters(lock))
1142 return rt_mutex_top_waiter(lock)->task;
1146 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1147 * @lock: the rt_mutex we were woken on
1148 * @to: the timeout, null if none. hrtimer should already have
1150 * @waiter: the pre-initialized rt_mutex_waiter
1151 * @detect_deadlock: perform deadlock detection (1) or not (0)
1153 * Complete the lock acquisition started our behalf by another thread.
1157 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1159 * Special API call for PI-futex requeue support
1161 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1162 struct hrtimer_sleeper *to,
1163 struct rt_mutex_waiter *waiter,
1164 int detect_deadlock)
1168 raw_spin_lock(&lock->wait_lock);
1170 set_current_state(TASK_INTERRUPTIBLE);
1172 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1174 set_current_state(TASK_RUNNING);
1177 remove_waiter(lock, waiter);
1180 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1181 * have to fix that up.
1183 fixup_rt_mutex_waiters(lock);
1185 raw_spin_unlock(&lock->wait_lock);