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;
145 * Adjust the priority chain. Also used for deadlock detection.
146 * Decreases task's usage by one - may thus free the task.
147 * Returns 0 or -EDEADLK.
149 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
151 struct rt_mutex *orig_lock,
152 struct rt_mutex_waiter *orig_waiter,
153 struct task_struct *top_task)
155 struct rt_mutex *lock;
156 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
157 int detect_deadlock, ret = 0, depth = 0;
160 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
164 * The (de)boosting is a step by step approach with a lot of
165 * pitfalls. We want this to be preemptible and we want hold a
166 * maximum of two locks per step. So we have to check
167 * carefully whether things change under us.
170 if (++depth > max_lock_depth) {
174 * Print this only once. If the admin changes the limit,
175 * print a new message when reaching the limit again.
177 if (prev_max != max_lock_depth) {
178 prev_max = max_lock_depth;
179 printk(KERN_WARNING "Maximum lock depth %d reached "
180 "task: %s (%d)\n", max_lock_depth,
181 top_task->comm, task_pid_nr(top_task));
183 put_task_struct(task);
185 return deadlock_detect ? -EDEADLK : 0;
189 * Task can not go away as we did a get_task() before !
191 raw_spin_lock_irqsave(&task->pi_lock, flags);
193 waiter = task->pi_blocked_on;
195 * Check whether the end of the boosting chain has been
196 * reached or the state of the chain has changed while we
203 * Check the orig_waiter state. After we dropped the locks,
204 * the previous owner of the lock might have released the lock.
206 if (orig_waiter && !rt_mutex_owner(orig_lock))
210 * Drop out, when the task has no waiters. Note,
211 * top_waiter can be NULL, when we are in the deboosting
215 if (!task_has_pi_waiters(task))
218 * If deadlock detection is off, we stop here if we
219 * are not the top pi waiter of the task.
221 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
226 * When deadlock detection is off then we check, if further
227 * priority adjustment is necessary.
229 if (!detect_deadlock && waiter->list_entry.prio == task->prio)
233 if (!raw_spin_trylock(&lock->wait_lock)) {
234 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
240 * Deadlock detection. If the lock is the same as the original
241 * lock which caused us to walk the lock chain or if the
242 * current lock is owned by the task which initiated the chain
243 * walk, we detected a deadlock.
245 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
246 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
247 raw_spin_unlock(&lock->wait_lock);
248 ret = deadlock_detect ? -EDEADLK : 0;
252 top_waiter = rt_mutex_top_waiter(lock);
254 /* Requeue the waiter */
255 plist_del(&waiter->list_entry, &lock->wait_list);
256 waiter->list_entry.prio = task->prio;
257 plist_add(&waiter->list_entry, &lock->wait_list);
259 /* Release the task */
260 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
261 if (!rt_mutex_owner(lock)) {
263 * If the requeue above changed the top waiter, then we need
264 * to wake the new top waiter up to try to get the lock.
267 if (top_waiter != rt_mutex_top_waiter(lock))
268 wake_up_process(rt_mutex_top_waiter(lock)->task);
269 raw_spin_unlock(&lock->wait_lock);
272 put_task_struct(task);
274 /* Grab the next task */
275 task = rt_mutex_owner(lock);
276 get_task_struct(task);
277 raw_spin_lock_irqsave(&task->pi_lock, flags);
279 if (waiter == rt_mutex_top_waiter(lock)) {
280 /* Boost the owner */
281 plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
282 waiter->pi_list_entry.prio = waiter->list_entry.prio;
283 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
284 __rt_mutex_adjust_prio(task);
286 } else if (top_waiter == waiter) {
287 /* Deboost the owner */
288 plist_del(&waiter->pi_list_entry, &task->pi_waiters);
289 waiter = rt_mutex_top_waiter(lock);
290 waiter->pi_list_entry.prio = waiter->list_entry.prio;
291 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
292 __rt_mutex_adjust_prio(task);
295 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
297 top_waiter = rt_mutex_top_waiter(lock);
298 raw_spin_unlock(&lock->wait_lock);
300 if (!detect_deadlock && waiter != top_waiter)
306 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
308 put_task_struct(task);
314 * Try to take an rt-mutex
316 * Must be called with lock->wait_lock held.
318 * @lock: the lock to be acquired.
319 * @task: the task which wants to acquire the lock
320 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
322 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
323 struct rt_mutex_waiter *waiter)
326 * We have to be careful here if the atomic speedups are
327 * enabled, such that, when
328 * - no other waiter is on the lock
329 * - the lock has been released since we did the cmpxchg
330 * the lock can be released or taken while we are doing the
331 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
333 * The atomic acquire/release aware variant of
334 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
335 * the WAITERS bit, the atomic release / acquire can not
336 * happen anymore and lock->wait_lock protects us from the
339 * Note, that this might set lock->owner =
340 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
341 * any more. This is fixed up when we take the ownership.
342 * This is the transitional state explained at the top of this file.
344 mark_rt_mutex_waiters(lock);
346 if (rt_mutex_owner(lock))
350 * It will get the lock because of one of these conditions:
351 * 1) there is no waiter
352 * 2) higher priority than waiters
353 * 3) it is top waiter
355 if (rt_mutex_has_waiters(lock)) {
356 if (task->prio >= rt_mutex_top_waiter(lock)->list_entry.prio) {
357 if (!waiter || waiter != rt_mutex_top_waiter(lock))
362 if (waiter || rt_mutex_has_waiters(lock)) {
364 struct rt_mutex_waiter *top;
366 raw_spin_lock_irqsave(&task->pi_lock, flags);
368 /* remove the queued waiter. */
370 plist_del(&waiter->list_entry, &lock->wait_list);
371 task->pi_blocked_on = NULL;
375 * We have to enqueue the top waiter(if it exists) into
376 * task->pi_waiters list.
378 if (rt_mutex_has_waiters(lock)) {
379 top = rt_mutex_top_waiter(lock);
380 top->pi_list_entry.prio = top->list_entry.prio;
381 plist_add(&top->pi_list_entry, &task->pi_waiters);
383 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
386 /* We got the lock. */
387 debug_rt_mutex_lock(lock);
389 rt_mutex_set_owner(lock, task);
391 rt_mutex_deadlock_account_lock(lock, task);
397 * Task blocks on lock.
399 * Prepare waiter and propagate pi chain
401 * This must be called with lock->wait_lock held.
403 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
404 struct rt_mutex_waiter *waiter,
405 struct task_struct *task,
408 struct task_struct *owner = rt_mutex_owner(lock);
409 struct rt_mutex_waiter *top_waiter = waiter;
411 int chain_walk = 0, res;
414 * Early deadlock detection. We really don't want the task to
415 * enqueue on itself just to untangle the mess later. It's not
416 * only an optimization. We drop the locks, so another waiter
417 * can come in before the chain walk detects the deadlock. So
418 * the other will detect the deadlock and return -EDEADLOCK,
419 * which is wrong, as the other waiter is not in a deadlock
422 if (detect_deadlock && owner == task)
425 raw_spin_lock_irqsave(&task->pi_lock, flags);
426 __rt_mutex_adjust_prio(task);
429 plist_node_init(&waiter->list_entry, task->prio);
430 plist_node_init(&waiter->pi_list_entry, task->prio);
432 /* Get the top priority waiter on the lock */
433 if (rt_mutex_has_waiters(lock))
434 top_waiter = rt_mutex_top_waiter(lock);
435 plist_add(&waiter->list_entry, &lock->wait_list);
437 task->pi_blocked_on = waiter;
439 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
444 if (waiter == rt_mutex_top_waiter(lock)) {
445 raw_spin_lock_irqsave(&owner->pi_lock, flags);
446 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
447 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
449 __rt_mutex_adjust_prio(owner);
450 if (owner->pi_blocked_on)
452 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
454 else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock))
461 * The owner can't disappear while holding a lock,
462 * so the owner struct is protected by wait_lock.
463 * Gets dropped in rt_mutex_adjust_prio_chain()!
465 get_task_struct(owner);
467 raw_spin_unlock(&lock->wait_lock);
469 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
472 raw_spin_lock(&lock->wait_lock);
478 * Wake up the next waiter on the lock.
480 * Remove the top waiter from the current tasks waiter list and wake it up.
482 * Called with lock->wait_lock held.
484 static void wakeup_next_waiter(struct rt_mutex *lock)
486 struct rt_mutex_waiter *waiter;
489 raw_spin_lock_irqsave(¤t->pi_lock, flags);
491 waiter = rt_mutex_top_waiter(lock);
494 * Remove it from current->pi_waiters. We do not adjust a
495 * possible priority boost right now. We execute wakeup in the
496 * boosted mode and go back to normal after releasing
499 plist_del(&waiter->pi_list_entry, ¤t->pi_waiters);
501 rt_mutex_set_owner(lock, NULL);
503 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
505 wake_up_process(waiter->task);
509 * Remove a waiter from a lock and give up
511 * Must be called with lock->wait_lock held and
512 * have just failed to try_to_take_rt_mutex().
514 static void remove_waiter(struct rt_mutex *lock,
515 struct rt_mutex_waiter *waiter)
517 int first = (waiter == rt_mutex_top_waiter(lock));
518 struct task_struct *owner = rt_mutex_owner(lock);
522 raw_spin_lock_irqsave(¤t->pi_lock, flags);
523 plist_del(&waiter->list_entry, &lock->wait_list);
524 current->pi_blocked_on = NULL;
525 raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
532 raw_spin_lock_irqsave(&owner->pi_lock, flags);
534 plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
536 if (rt_mutex_has_waiters(lock)) {
537 struct rt_mutex_waiter *next;
539 next = rt_mutex_top_waiter(lock);
540 plist_add(&next->pi_list_entry, &owner->pi_waiters);
542 __rt_mutex_adjust_prio(owner);
544 if (owner->pi_blocked_on)
547 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
550 WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
555 /* gets dropped in rt_mutex_adjust_prio_chain()! */
556 get_task_struct(owner);
558 raw_spin_unlock(&lock->wait_lock);
560 rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
562 raw_spin_lock(&lock->wait_lock);
566 * Recheck the pi chain, in case we got a priority setting
568 * Called from sched_setscheduler
570 void rt_mutex_adjust_pi(struct task_struct *task)
572 struct rt_mutex_waiter *waiter;
575 raw_spin_lock_irqsave(&task->pi_lock, flags);
577 waiter = task->pi_blocked_on;
578 if (!waiter || waiter->list_entry.prio == task->prio) {
579 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
583 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
585 /* gets dropped in rt_mutex_adjust_prio_chain()! */
586 get_task_struct(task);
587 rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
591 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
592 * @lock: the rt_mutex to take
593 * @state: the state the task should block in (TASK_INTERRUPTIBLE
594 * or TASK_UNINTERRUPTIBLE)
595 * @timeout: the pre-initialized and started timer, or NULL for none
596 * @waiter: the pre-initialized rt_mutex_waiter
598 * lock->wait_lock must be held by the caller.
601 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
602 struct hrtimer_sleeper *timeout,
603 struct rt_mutex_waiter *waiter)
609 /* Try to acquire the lock: */
610 if (try_to_take_rt_mutex(lock, current, waiter))
614 * TASK_INTERRUPTIBLE checks for signals and
615 * timeout. Ignored otherwise.
617 if (unlikely(state == TASK_INTERRUPTIBLE)) {
618 /* Signal pending? */
619 if (signal_pending(current))
621 if (timeout && !timeout->task)
627 raw_spin_unlock(&lock->wait_lock);
629 was_disabled = irqs_disabled();
633 debug_rt_mutex_print_deadlock(waiter);
635 schedule_rt_mutex(lock);
640 raw_spin_lock(&lock->wait_lock);
641 set_current_state(state);
648 * Slow path lock function:
651 rt_mutex_slowlock(struct rt_mutex *lock, int state,
652 struct hrtimer_sleeper *timeout,
655 struct rt_mutex_waiter waiter;
658 debug_rt_mutex_init_waiter(&waiter);
660 raw_spin_lock(&lock->wait_lock);
662 /* Try to acquire the lock again: */
663 if (try_to_take_rt_mutex(lock, current, NULL)) {
664 raw_spin_unlock(&lock->wait_lock);
668 set_current_state(state);
670 /* Setup the timer, when timeout != NULL */
671 if (unlikely(timeout)) {
672 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
673 if (!hrtimer_active(&timeout->timer))
674 timeout->task = NULL;
677 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
680 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
682 set_current_state(TASK_RUNNING);
685 remove_waiter(lock, &waiter);
688 * try_to_take_rt_mutex() sets the waiter bit
689 * unconditionally. We might have to fix that up.
691 fixup_rt_mutex_waiters(lock);
693 raw_spin_unlock(&lock->wait_lock);
695 /* Remove pending timer: */
696 if (unlikely(timeout))
697 hrtimer_cancel(&timeout->timer);
699 debug_rt_mutex_free_waiter(&waiter);
705 * Slow path try-lock function:
708 rt_mutex_slowtrylock(struct rt_mutex *lock)
712 raw_spin_lock(&lock->wait_lock);
714 if (likely(rt_mutex_owner(lock) != current)) {
716 ret = try_to_take_rt_mutex(lock, current, NULL);
718 * try_to_take_rt_mutex() sets the lock waiters
719 * bit unconditionally. Clean this up.
721 fixup_rt_mutex_waiters(lock);
724 raw_spin_unlock(&lock->wait_lock);
730 * Slow path to release a rt-mutex:
733 rt_mutex_slowunlock(struct rt_mutex *lock)
735 raw_spin_lock(&lock->wait_lock);
737 debug_rt_mutex_unlock(lock);
739 rt_mutex_deadlock_account_unlock(current);
741 if (!rt_mutex_has_waiters(lock)) {
743 raw_spin_unlock(&lock->wait_lock);
747 wakeup_next_waiter(lock);
749 raw_spin_unlock(&lock->wait_lock);
751 /* Undo pi boosting if necessary: */
752 rt_mutex_adjust_prio(current);
756 * debug aware fast / slowpath lock,trylock,unlock
758 * The atomic acquire/release ops are compiled away, when either the
759 * architecture does not support cmpxchg or when debugging is enabled.
762 rt_mutex_fastlock(struct rt_mutex *lock, int state,
764 int (*slowfn)(struct rt_mutex *lock, int state,
765 struct hrtimer_sleeper *timeout,
766 int detect_deadlock))
768 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
769 rt_mutex_deadlock_account_lock(lock, current);
772 return slowfn(lock, state, NULL, detect_deadlock);
776 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
777 struct hrtimer_sleeper *timeout, int detect_deadlock,
778 int (*slowfn)(struct rt_mutex *lock, int state,
779 struct hrtimer_sleeper *timeout,
780 int detect_deadlock))
782 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
783 rt_mutex_deadlock_account_lock(lock, current);
786 return slowfn(lock, state, timeout, detect_deadlock);
790 rt_mutex_fasttrylock(struct rt_mutex *lock,
791 int (*slowfn)(struct rt_mutex *lock))
793 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
794 rt_mutex_deadlock_account_lock(lock, current);
801 rt_mutex_fastunlock(struct rt_mutex *lock,
802 void (*slowfn)(struct rt_mutex *lock))
804 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
805 rt_mutex_deadlock_account_unlock(current);
811 * rt_mutex_lock - lock a rt_mutex
813 * @lock: the rt_mutex to be locked
815 void __sched rt_mutex_lock(struct rt_mutex *lock)
819 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
821 EXPORT_SYMBOL_GPL(rt_mutex_lock);
824 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
826 * @lock: the rt_mutex to be locked
827 * @detect_deadlock: deadlock detection on/off
831 * -EINTR when interrupted by a signal
832 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
834 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
839 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
840 detect_deadlock, rt_mutex_slowlock);
842 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
845 * rt_mutex_timed_lock - lock a rt_mutex interruptible
846 * the timeout structure is provided
849 * @lock: the rt_mutex to be locked
850 * @timeout: timeout structure or NULL (no timeout)
851 * @detect_deadlock: deadlock detection on/off
855 * -EINTR when interrupted by a signal
856 * -ETIMEDOUT when the timeout expired
857 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
860 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
865 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
866 detect_deadlock, rt_mutex_slowlock);
868 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
871 * rt_mutex_trylock - try to lock a rt_mutex
873 * @lock: the rt_mutex to be locked
875 * Returns 1 on success and 0 on contention
877 int __sched rt_mutex_trylock(struct rt_mutex *lock)
879 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
881 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
884 * rt_mutex_unlock - unlock a rt_mutex
886 * @lock: the rt_mutex to be unlocked
888 void __sched rt_mutex_unlock(struct rt_mutex *lock)
890 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
892 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
895 * rt_mutex_destroy - mark a mutex unusable
896 * @lock: the mutex to be destroyed
898 * This function marks the mutex uninitialized, and any subsequent
899 * use of the mutex is forbidden. The mutex must not be locked when
900 * this function is called.
902 void rt_mutex_destroy(struct rt_mutex *lock)
904 WARN_ON(rt_mutex_is_locked(lock));
905 #ifdef CONFIG_DEBUG_RT_MUTEXES
910 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
913 * __rt_mutex_init - initialize the rt lock
915 * @lock: the rt lock to be initialized
917 * Initialize the rt lock to unlocked state.
919 * Initializing of a locked rt lock is not allowed
921 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
924 raw_spin_lock_init(&lock->wait_lock);
925 plist_head_init(&lock->wait_list);
927 debug_rt_mutex_init(lock, name);
929 EXPORT_SYMBOL_GPL(__rt_mutex_init);
932 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
935 * @lock: the rt_mutex to be locked
936 * @proxy_owner:the task to set as owner
938 * No locking. Caller has to do serializing itself
939 * Special API call for PI-futex support
941 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
942 struct task_struct *proxy_owner)
944 __rt_mutex_init(lock, NULL);
945 debug_rt_mutex_proxy_lock(lock, proxy_owner);
946 rt_mutex_set_owner(lock, proxy_owner);
947 rt_mutex_deadlock_account_lock(lock, proxy_owner);
951 * rt_mutex_proxy_unlock - release a lock on behalf of owner
953 * @lock: the rt_mutex to be locked
955 * No locking. Caller has to do serializing itself
956 * Special API call for PI-futex support
958 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
959 struct task_struct *proxy_owner)
961 debug_rt_mutex_proxy_unlock(lock);
962 rt_mutex_set_owner(lock, NULL);
963 rt_mutex_deadlock_account_unlock(proxy_owner);
967 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
968 * @lock: the rt_mutex to take
969 * @waiter: the pre-initialized rt_mutex_waiter
970 * @task: the task to prepare
971 * @detect_deadlock: perform deadlock detection (1) or not (0)
974 * 0 - task blocked on lock
975 * 1 - acquired the lock for task, caller should wake it up
978 * Special API call for FUTEX_REQUEUE_PI support.
980 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
981 struct rt_mutex_waiter *waiter,
982 struct task_struct *task, int detect_deadlock)
986 raw_spin_lock(&lock->wait_lock);
988 if (try_to_take_rt_mutex(lock, task, NULL)) {
989 raw_spin_unlock(&lock->wait_lock);
993 ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
995 if (ret && !rt_mutex_owner(lock)) {
997 * Reset the return value. We might have
998 * returned with -EDEADLK and the owner
999 * released the lock while we were walking the
1000 * pi chain. Let the waiter sort it out.
1006 remove_waiter(lock, waiter);
1008 raw_spin_unlock(&lock->wait_lock);
1010 debug_rt_mutex_print_deadlock(waiter);
1016 * rt_mutex_next_owner - return the next owner of the lock
1018 * @lock: the rt lock query
1020 * Returns the next owner of the lock or NULL
1022 * Caller has to serialize against other accessors to the lock
1025 * Special API call for PI-futex support
1027 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1029 if (!rt_mutex_has_waiters(lock))
1032 return rt_mutex_top_waiter(lock)->task;
1036 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1037 * @lock: the rt_mutex we were woken on
1038 * @to: the timeout, null if none. hrtimer should already have
1040 * @waiter: the pre-initialized rt_mutex_waiter
1041 * @detect_deadlock: perform deadlock detection (1) or not (0)
1043 * Complete the lock acquisition started our behalf by another thread.
1047 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1049 * Special API call for PI-futex requeue support
1051 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1052 struct hrtimer_sleeper *to,
1053 struct rt_mutex_waiter *waiter,
1054 int detect_deadlock)
1058 raw_spin_lock(&lock->wait_lock);
1060 set_current_state(TASK_INTERRUPTIBLE);
1062 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1064 set_current_state(TASK_RUNNING);
1067 remove_waiter(lock, waiter);
1070 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1071 * have to fix that up.
1073 fixup_rt_mutex_waiters(lock);
1075 raw_spin_unlock(&lock->wait_lock);