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 * Called by sched_setscheduler() to check whether the priority change
217 * is overruled by a possible priority boosting.
219 int rt_mutex_check_prio(struct task_struct *task, int newprio)
221 if (!task_has_pi_waiters(task))
224 return task_top_pi_waiter(task)->task->prio <= newprio;
228 * Adjust the priority of a task, after its pi_waiters got modified.
230 * This can be both boosting and unboosting. task->pi_lock must be held.
232 static void __rt_mutex_adjust_prio(struct task_struct *task)
234 int prio = rt_mutex_getprio(task);
236 if (task->prio != prio || dl_prio(prio))
237 rt_mutex_setprio(task, prio);
241 * Adjust task priority (undo boosting). Called from the exit path of
242 * rt_mutex_slowunlock() and rt_mutex_slowlock().
244 * (Note: We do this outside of the protection of lock->wait_lock to
245 * allow the lock to be taken while or before we readjust the priority
246 * of task. We do not use the spin_xx_mutex() variants here as we are
247 * outside of the debug path.)
249 static void rt_mutex_adjust_prio(struct task_struct *task)
253 raw_spin_lock_irqsave(&task->pi_lock, flags);
254 __rt_mutex_adjust_prio(task);
255 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
259 * Max number of times we'll walk the boosting chain:
261 int max_lock_depth = 1024;
264 * Adjust the priority chain. Also used for deadlock detection.
265 * Decreases task's usage by one - may thus free the task.
267 * @task: the task owning the mutex (owner) for which a chain walk is probably
269 * @deadlock_detect: do we have to carry out deadlock detection?
270 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
271 * things for a task that has just got its priority adjusted, and
272 * is waiting on a mutex)
273 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
274 * its priority to the mutex owner (can be NULL in the case
275 * depicted above or if the top waiter is gone away and we are
276 * actually deboosting the owner)
277 * @top_task: the current top waiter
279 * Returns 0 or -EDEADLK.
281 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
283 struct rt_mutex *orig_lock,
284 struct rt_mutex_waiter *orig_waiter,
285 struct task_struct *top_task)
287 struct rt_mutex *lock;
288 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
289 int detect_deadlock, ret = 0, depth = 0;
292 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
296 * The (de)boosting is a step by step approach with a lot of
297 * pitfalls. We want this to be preemptible and we want hold a
298 * maximum of two locks per step. So we have to check
299 * carefully whether things change under us.
302 if (++depth > max_lock_depth) {
306 * Print this only once. If the admin changes the limit,
307 * print a new message when reaching the limit again.
309 if (prev_max != max_lock_depth) {
310 prev_max = max_lock_depth;
311 printk(KERN_WARNING "Maximum lock depth %d reached "
312 "task: %s (%d)\n", max_lock_depth,
313 top_task->comm, task_pid_nr(top_task));
315 put_task_struct(task);
317 return deadlock_detect ? -EDEADLK : 0;
321 * Task can not go away as we did a get_task() before !
323 raw_spin_lock_irqsave(&task->pi_lock, flags);
325 waiter = task->pi_blocked_on;
327 * Check whether the end of the boosting chain has been
328 * reached or the state of the chain has changed while we
335 * Check the orig_waiter state. After we dropped the locks,
336 * the previous owner of the lock might have released the lock.
338 if (orig_waiter && !rt_mutex_owner(orig_lock))
342 * Drop out, when the task has no waiters. Note,
343 * top_waiter can be NULL, when we are in the deboosting
346 if (top_waiter && (!task_has_pi_waiters(task) ||
347 top_waiter != task_top_pi_waiter(task)))
351 * When deadlock detection is off then we check, if further
352 * priority adjustment is necessary.
354 if (!detect_deadlock && waiter->prio == task->prio)
358 if (!raw_spin_trylock(&lock->wait_lock)) {
359 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
364 /* Deadlock detection */
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;
530 raw_spin_lock_irqsave(&task->pi_lock, flags);
531 __rt_mutex_adjust_prio(task);
534 waiter->prio = task->prio;
536 /* Get the top priority waiter on the lock */
537 if (rt_mutex_has_waiters(lock))
538 top_waiter = rt_mutex_top_waiter(lock);
539 rt_mutex_enqueue(lock, waiter);
541 task->pi_blocked_on = waiter;
543 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
548 if (waiter == rt_mutex_top_waiter(lock)) {
549 raw_spin_lock_irqsave(&owner->pi_lock, flags);
550 rt_mutex_dequeue_pi(owner, top_waiter);
551 rt_mutex_enqueue_pi(owner, waiter);
553 __rt_mutex_adjust_prio(owner);
554 if (owner->pi_blocked_on)
556 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
558 else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock))
565 * The owner can't disappear while holding a lock,
566 * so the owner struct is protected by wait_lock.
567 * Gets dropped in rt_mutex_adjust_prio_chain()!
569 get_task_struct(owner);
571 raw_spin_unlock(&lock->wait_lock);
573 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
576 raw_spin_lock(&lock->wait_lock);
582 * Wake up the next waiter on the lock.
584 * Remove the top waiter from the current tasks waiter list and wake it up.
586 * Called with lock->wait_lock held.
588 static void wakeup_next_waiter(struct rt_mutex *lock)
590 struct rt_mutex_waiter *waiter;
593 raw_spin_lock_irqsave(¤t->pi_lock, flags);
595 waiter = rt_mutex_top_waiter(lock);
598 * Remove it from current->pi_waiters. We do not adjust a
599 * possible priority boost right now. We execute wakeup in the
600 * boosted mode and go back to normal after releasing
603 rt_mutex_dequeue_pi(current, waiter);
605 rt_mutex_set_owner(lock, NULL);
607 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
609 wake_up_process(waiter->task);
613 * Remove a waiter from a lock and give up
615 * Must be called with lock->wait_lock held and
616 * have just failed to try_to_take_rt_mutex().
618 static void remove_waiter(struct rt_mutex *lock,
619 struct rt_mutex_waiter *waiter)
621 int first = (waiter == rt_mutex_top_waiter(lock));
622 struct task_struct *owner = rt_mutex_owner(lock);
626 raw_spin_lock_irqsave(¤t->pi_lock, flags);
627 rt_mutex_dequeue(lock, waiter);
628 current->pi_blocked_on = NULL;
629 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
636 raw_spin_lock_irqsave(&owner->pi_lock, flags);
638 rt_mutex_dequeue_pi(owner, waiter);
640 if (rt_mutex_has_waiters(lock)) {
641 struct rt_mutex_waiter *next;
643 next = rt_mutex_top_waiter(lock);
644 rt_mutex_enqueue_pi(owner, next);
646 __rt_mutex_adjust_prio(owner);
648 if (owner->pi_blocked_on)
651 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
657 /* gets dropped in rt_mutex_adjust_prio_chain()! */
658 get_task_struct(owner);
660 raw_spin_unlock(&lock->wait_lock);
662 rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
664 raw_spin_lock(&lock->wait_lock);
668 * Recheck the pi chain, in case we got a priority setting
670 * Called from sched_setscheduler
672 void rt_mutex_adjust_pi(struct task_struct *task)
674 struct rt_mutex_waiter *waiter;
677 raw_spin_lock_irqsave(&task->pi_lock, flags);
679 waiter = task->pi_blocked_on;
680 if (!waiter || (waiter->prio == task->prio &&
681 !dl_prio(task->prio))) {
682 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
686 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
688 /* gets dropped in rt_mutex_adjust_prio_chain()! */
689 get_task_struct(task);
690 rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
694 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
695 * @lock: the rt_mutex to take
696 * @state: the state the task should block in (TASK_INTERRUPTIBLE
697 * or TASK_UNINTERRUPTIBLE)
698 * @timeout: the pre-initialized and started timer, or NULL for none
699 * @waiter: the pre-initialized rt_mutex_waiter
701 * lock->wait_lock must be held by the caller.
704 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
705 struct hrtimer_sleeper *timeout,
706 struct rt_mutex_waiter *waiter)
711 /* Try to acquire the lock: */
712 if (try_to_take_rt_mutex(lock, current, waiter))
716 * TASK_INTERRUPTIBLE checks for signals and
717 * timeout. Ignored otherwise.
719 if (unlikely(state == TASK_INTERRUPTIBLE)) {
720 /* Signal pending? */
721 if (signal_pending(current))
723 if (timeout && !timeout->task)
729 raw_spin_unlock(&lock->wait_lock);
731 debug_rt_mutex_print_deadlock(waiter);
733 schedule_rt_mutex(lock);
735 raw_spin_lock(&lock->wait_lock);
736 set_current_state(state);
743 * Slow path lock function:
746 rt_mutex_slowlock(struct rt_mutex *lock, int state,
747 struct hrtimer_sleeper *timeout,
750 struct rt_mutex_waiter waiter;
753 debug_rt_mutex_init_waiter(&waiter);
754 RB_CLEAR_NODE(&waiter.pi_tree_entry);
755 RB_CLEAR_NODE(&waiter.tree_entry);
757 raw_spin_lock(&lock->wait_lock);
759 /* Try to acquire the lock again: */
760 if (try_to_take_rt_mutex(lock, current, NULL)) {
761 raw_spin_unlock(&lock->wait_lock);
765 set_current_state(state);
767 /* Setup the timer, when timeout != NULL */
768 if (unlikely(timeout)) {
769 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
770 if (!hrtimer_active(&timeout->timer))
771 timeout->task = NULL;
774 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
777 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
779 set_current_state(TASK_RUNNING);
782 remove_waiter(lock, &waiter);
785 * try_to_take_rt_mutex() sets the waiter bit
786 * unconditionally. We might have to fix that up.
788 fixup_rt_mutex_waiters(lock);
790 raw_spin_unlock(&lock->wait_lock);
792 /* Remove pending timer: */
793 if (unlikely(timeout))
794 hrtimer_cancel(&timeout->timer);
796 debug_rt_mutex_free_waiter(&waiter);
802 * Slow path try-lock function:
805 rt_mutex_slowtrylock(struct rt_mutex *lock)
809 raw_spin_lock(&lock->wait_lock);
811 if (likely(rt_mutex_owner(lock) != current)) {
813 ret = try_to_take_rt_mutex(lock, current, NULL);
815 * try_to_take_rt_mutex() sets the lock waiters
816 * bit unconditionally. Clean this up.
818 fixup_rt_mutex_waiters(lock);
821 raw_spin_unlock(&lock->wait_lock);
827 * Slow path to release a rt-mutex:
830 rt_mutex_slowunlock(struct rt_mutex *lock)
832 raw_spin_lock(&lock->wait_lock);
834 debug_rt_mutex_unlock(lock);
836 rt_mutex_deadlock_account_unlock(current);
838 if (!rt_mutex_has_waiters(lock)) {
840 raw_spin_unlock(&lock->wait_lock);
844 wakeup_next_waiter(lock);
846 raw_spin_unlock(&lock->wait_lock);
848 /* Undo pi boosting if necessary: */
849 rt_mutex_adjust_prio(current);
853 * debug aware fast / slowpath lock,trylock,unlock
855 * The atomic acquire/release ops are compiled away, when either the
856 * architecture does not support cmpxchg or when debugging is enabled.
859 rt_mutex_fastlock(struct rt_mutex *lock, int state,
861 int (*slowfn)(struct rt_mutex *lock, int state,
862 struct hrtimer_sleeper *timeout,
863 int detect_deadlock))
865 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
866 rt_mutex_deadlock_account_lock(lock, current);
869 return slowfn(lock, state, NULL, detect_deadlock);
873 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
874 struct hrtimer_sleeper *timeout, int detect_deadlock,
875 int (*slowfn)(struct rt_mutex *lock, int state,
876 struct hrtimer_sleeper *timeout,
877 int detect_deadlock))
879 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
880 rt_mutex_deadlock_account_lock(lock, current);
883 return slowfn(lock, state, timeout, detect_deadlock);
887 rt_mutex_fasttrylock(struct rt_mutex *lock,
888 int (*slowfn)(struct rt_mutex *lock))
890 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
891 rt_mutex_deadlock_account_lock(lock, current);
898 rt_mutex_fastunlock(struct rt_mutex *lock,
899 void (*slowfn)(struct rt_mutex *lock))
901 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
902 rt_mutex_deadlock_account_unlock(current);
908 * rt_mutex_lock - lock a rt_mutex
910 * @lock: the rt_mutex to be locked
912 void __sched rt_mutex_lock(struct rt_mutex *lock)
916 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
918 EXPORT_SYMBOL_GPL(rt_mutex_lock);
921 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
923 * @lock: the rt_mutex to be locked
924 * @detect_deadlock: deadlock detection on/off
928 * -EINTR when interrupted by a signal
929 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
931 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
936 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
937 detect_deadlock, rt_mutex_slowlock);
939 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
942 * rt_mutex_timed_lock - lock a rt_mutex interruptible
943 * the timeout structure is provided
946 * @lock: the rt_mutex to be locked
947 * @timeout: timeout structure or NULL (no timeout)
948 * @detect_deadlock: deadlock detection on/off
952 * -EINTR when interrupted by a signal
953 * -ETIMEDOUT when the timeout expired
954 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
957 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
962 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
963 detect_deadlock, rt_mutex_slowlock);
965 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
968 * rt_mutex_trylock - try to lock a rt_mutex
970 * @lock: the rt_mutex to be locked
972 * Returns 1 on success and 0 on contention
974 int __sched rt_mutex_trylock(struct rt_mutex *lock)
976 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
978 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
981 * rt_mutex_unlock - unlock a rt_mutex
983 * @lock: the rt_mutex to be unlocked
985 void __sched rt_mutex_unlock(struct rt_mutex *lock)
987 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
989 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
992 * rt_mutex_destroy - mark a mutex unusable
993 * @lock: the mutex to be destroyed
995 * This function marks the mutex uninitialized, and any subsequent
996 * use of the mutex is forbidden. The mutex must not be locked when
997 * this function is called.
999 void rt_mutex_destroy(struct rt_mutex *lock)
1001 WARN_ON(rt_mutex_is_locked(lock));
1002 #ifdef CONFIG_DEBUG_RT_MUTEXES
1007 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1010 * __rt_mutex_init - initialize the rt lock
1012 * @lock: the rt lock to be initialized
1014 * Initialize the rt lock to unlocked state.
1016 * Initializing of a locked rt lock is not allowed
1018 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1021 raw_spin_lock_init(&lock->wait_lock);
1022 lock->waiters = RB_ROOT;
1023 lock->waiters_leftmost = NULL;
1025 debug_rt_mutex_init(lock, name);
1027 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1030 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1033 * @lock: the rt_mutex to be locked
1034 * @proxy_owner:the task to set as owner
1036 * No locking. Caller has to do serializing itself
1037 * Special API call for PI-futex support
1039 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1040 struct task_struct *proxy_owner)
1042 __rt_mutex_init(lock, NULL);
1043 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1044 rt_mutex_set_owner(lock, proxy_owner);
1045 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1049 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1051 * @lock: the rt_mutex to be locked
1053 * No locking. Caller has to do serializing itself
1054 * Special API call for PI-futex support
1056 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1057 struct task_struct *proxy_owner)
1059 debug_rt_mutex_proxy_unlock(lock);
1060 rt_mutex_set_owner(lock, NULL);
1061 rt_mutex_deadlock_account_unlock(proxy_owner);
1065 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1066 * @lock: the rt_mutex to take
1067 * @waiter: the pre-initialized rt_mutex_waiter
1068 * @task: the task to prepare
1069 * @detect_deadlock: perform deadlock detection (1) or not (0)
1072 * 0 - task blocked on lock
1073 * 1 - acquired the lock for task, caller should wake it up
1076 * Special API call for FUTEX_REQUEUE_PI support.
1078 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1079 struct rt_mutex_waiter *waiter,
1080 struct task_struct *task, int detect_deadlock)
1084 raw_spin_lock(&lock->wait_lock);
1086 if (try_to_take_rt_mutex(lock, task, NULL)) {
1087 raw_spin_unlock(&lock->wait_lock);
1091 ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
1093 if (ret && !rt_mutex_owner(lock)) {
1095 * Reset the return value. We might have
1096 * returned with -EDEADLK and the owner
1097 * released the lock while we were walking the
1098 * pi chain. Let the waiter sort it out.
1104 remove_waiter(lock, waiter);
1106 raw_spin_unlock(&lock->wait_lock);
1108 debug_rt_mutex_print_deadlock(waiter);
1114 * rt_mutex_next_owner - return the next owner of the lock
1116 * @lock: the rt lock query
1118 * Returns the next owner of the lock or NULL
1120 * Caller has to serialize against other accessors to the lock
1123 * Special API call for PI-futex support
1125 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1127 if (!rt_mutex_has_waiters(lock))
1130 return rt_mutex_top_waiter(lock)->task;
1134 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1135 * @lock: the rt_mutex we were woken on
1136 * @to: the timeout, null if none. hrtimer should already have
1138 * @waiter: the pre-initialized rt_mutex_waiter
1139 * @detect_deadlock: perform deadlock detection (1) or not (0)
1141 * Complete the lock acquisition started our behalf by another thread.
1145 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1147 * Special API call for PI-futex requeue support
1149 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1150 struct hrtimer_sleeper *to,
1151 struct rt_mutex_waiter *waiter,
1152 int detect_deadlock)
1156 raw_spin_lock(&lock->wait_lock);
1158 set_current_state(TASK_INTERRUPTIBLE);
1160 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1162 set_current_state(TASK_RUNNING);
1165 remove_waiter(lock, waiter);
1168 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1169 * have to fix that up.
1171 fixup_rt_mutex_waiters(lock);
1173 raw_spin_unlock(&lock->wait_lock);