KVM: Enable 32bit dirty log pointers on 64bit host
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / rtmutex.c
blob29bd4baf9e756d848895a868bd25c103f6ee1f85
1 /*
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/module.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 and 1
24 * are used to keep track of the "owner is pending" and "lock has
25 * waiters" state.
27 * owner bit1 bit0
28 * NULL 0 0 lock is free (fast acquire possible)
29 * NULL 0 1 invalid state
30 * NULL 1 0 Transitional State*
31 * NULL 1 1 invalid state
32 * taskpointer 0 0 lock is held (fast release possible)
33 * taskpointer 0 1 task is pending owner
34 * taskpointer 1 0 lock is held and has waiters
35 * taskpointer 1 1 task is pending owner and lock has more waiters
37 * Pending ownership is assigned to the top (highest priority)
38 * waiter of the lock, when the lock is released. The thread is woken
39 * up and can now take the lock. Until the lock is taken (bit 0
40 * cleared) a competing higher priority thread can steal the lock
41 * which puts the woken up thread back on the waiters list.
43 * The fast atomic compare exchange based acquire and release is only
44 * possible when bit 0 and 1 of lock->owner are 0.
46 * (*) There's a small time where the owner can be NULL and the
47 * "lock has waiters" bit is set. This can happen when grabbing the lock.
48 * To prevent a cmpxchg of the owner releasing the lock, we need to set this
49 * bit before looking at the lock, hence the reason this is a transitional
50 * state.
53 static void
54 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner,
55 unsigned long mask)
57 unsigned long val = (unsigned long)owner | mask;
59 if (rt_mutex_has_waiters(lock))
60 val |= RT_MUTEX_HAS_WAITERS;
62 lock->owner = (struct task_struct *)val;
65 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
67 lock->owner = (struct task_struct *)
68 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
71 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
73 if (!rt_mutex_has_waiters(lock))
74 clear_rt_mutex_waiters(lock);
78 * We can speed up the acquire/release, if the architecture
79 * supports cmpxchg and if there's no debugging state to be set up
81 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
82 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
83 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
85 unsigned long owner, *p = (unsigned long *) &lock->owner;
87 do {
88 owner = *p;
89 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
91 #else
92 # define rt_mutex_cmpxchg(l,c,n) (0)
93 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
95 lock->owner = (struct task_struct *)
96 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
98 #endif
101 * Calculate task priority from the waiter list priority
103 * Return task->normal_prio when the waiter list is empty or when
104 * the waiter is not allowed to do priority boosting
106 int rt_mutex_getprio(struct task_struct *task)
108 if (likely(!task_has_pi_waiters(task)))
109 return task->normal_prio;
111 return min(task_top_pi_waiter(task)->pi_list_entry.prio,
112 task->normal_prio);
116 * Adjust the priority of a task, after its pi_waiters got modified.
118 * This can be both boosting and unboosting. task->pi_lock must be held.
120 static void __rt_mutex_adjust_prio(struct task_struct *task)
122 int prio = rt_mutex_getprio(task);
124 if (task->prio != prio)
125 rt_mutex_setprio(task, prio);
129 * Adjust task priority (undo boosting). Called from the exit path of
130 * rt_mutex_slowunlock() and rt_mutex_slowlock().
132 * (Note: We do this outside of the protection of lock->wait_lock to
133 * allow the lock to be taken while or before we readjust the priority
134 * of task. We do not use the spin_xx_mutex() variants here as we are
135 * outside of the debug path.)
137 static void rt_mutex_adjust_prio(struct task_struct *task)
139 unsigned long flags;
141 spin_lock_irqsave(&task->pi_lock, flags);
142 __rt_mutex_adjust_prio(task);
143 spin_unlock_irqrestore(&task->pi_lock, flags);
147 * Max number of times we'll walk the boosting chain:
149 int max_lock_depth = 1024;
152 * Adjust the priority chain. Also used for deadlock detection.
153 * Decreases task's usage by one - may thus free the task.
154 * Returns 0 or -EDEADLK.
156 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
157 int deadlock_detect,
158 struct rt_mutex *orig_lock,
159 struct rt_mutex_waiter *orig_waiter,
160 struct task_struct *top_task)
162 struct rt_mutex *lock;
163 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
164 int detect_deadlock, ret = 0, depth = 0;
165 unsigned long flags;
167 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
168 deadlock_detect);
171 * The (de)boosting is a step by step approach with a lot of
172 * pitfalls. We want this to be preemptible and we want hold a
173 * maximum of two locks per step. So we have to check
174 * carefully whether things change under us.
176 again:
177 if (++depth > max_lock_depth) {
178 static int prev_max;
181 * Print this only once. If the admin changes the limit,
182 * print a new message when reaching the limit again.
184 if (prev_max != max_lock_depth) {
185 prev_max = max_lock_depth;
186 printk(KERN_WARNING "Maximum lock depth %d reached "
187 "task: %s (%d)\n", max_lock_depth,
188 top_task->comm, task_pid_nr(top_task));
190 put_task_struct(task);
192 return deadlock_detect ? -EDEADLK : 0;
194 retry:
196 * Task can not go away as we did a get_task() before !
198 spin_lock_irqsave(&task->pi_lock, flags);
200 waiter = task->pi_blocked_on;
202 * Check whether the end of the boosting chain has been
203 * reached or the state of the chain has changed while we
204 * dropped the locks.
206 if (!waiter || !waiter->task)
207 goto out_unlock_pi;
210 * Check the orig_waiter state. After we dropped the locks,
211 * the previous owner of the lock might have released the lock
212 * and made us the pending owner:
214 if (orig_waiter && !orig_waiter->task)
215 goto out_unlock_pi;
218 * Drop out, when the task has no waiters. Note,
219 * top_waiter can be NULL, when we are in the deboosting
220 * mode!
222 if (top_waiter && (!task_has_pi_waiters(task) ||
223 top_waiter != task_top_pi_waiter(task)))
224 goto out_unlock_pi;
227 * When deadlock detection is off then we check, if further
228 * priority adjustment is necessary.
230 if (!detect_deadlock && waiter->list_entry.prio == task->prio)
231 goto out_unlock_pi;
233 lock = waiter->lock;
234 if (!spin_trylock(&lock->wait_lock)) {
235 spin_unlock_irqrestore(&task->pi_lock, flags);
236 cpu_relax();
237 goto retry;
240 /* Deadlock detection */
241 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
242 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
243 spin_unlock(&lock->wait_lock);
244 ret = deadlock_detect ? -EDEADLK : 0;
245 goto out_unlock_pi;
248 top_waiter = rt_mutex_top_waiter(lock);
250 /* Requeue the waiter */
251 plist_del(&waiter->list_entry, &lock->wait_list);
252 waiter->list_entry.prio = task->prio;
253 plist_add(&waiter->list_entry, &lock->wait_list);
255 /* Release the task */
256 spin_unlock_irqrestore(&task->pi_lock, flags);
257 put_task_struct(task);
259 /* Grab the next task */
260 task = rt_mutex_owner(lock);
261 get_task_struct(task);
262 spin_lock_irqsave(&task->pi_lock, flags);
264 if (waiter == rt_mutex_top_waiter(lock)) {
265 /* Boost the owner */
266 plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
267 waiter->pi_list_entry.prio = waiter->list_entry.prio;
268 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
269 __rt_mutex_adjust_prio(task);
271 } else if (top_waiter == waiter) {
272 /* Deboost the owner */
273 plist_del(&waiter->pi_list_entry, &task->pi_waiters);
274 waiter = rt_mutex_top_waiter(lock);
275 waiter->pi_list_entry.prio = waiter->list_entry.prio;
276 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
277 __rt_mutex_adjust_prio(task);
280 spin_unlock_irqrestore(&task->pi_lock, flags);
282 top_waiter = rt_mutex_top_waiter(lock);
283 spin_unlock(&lock->wait_lock);
285 if (!detect_deadlock && waiter != top_waiter)
286 goto out_put_task;
288 goto again;
290 out_unlock_pi:
291 spin_unlock_irqrestore(&task->pi_lock, flags);
292 out_put_task:
293 put_task_struct(task);
295 return ret;
299 * Optimization: check if we can steal the lock from the
300 * assigned pending owner [which might not have taken the
301 * lock yet]:
303 static inline int try_to_steal_lock(struct rt_mutex *lock,
304 struct task_struct *task)
306 struct task_struct *pendowner = rt_mutex_owner(lock);
307 struct rt_mutex_waiter *next;
308 unsigned long flags;
310 if (!rt_mutex_owner_pending(lock))
311 return 0;
313 if (pendowner == task)
314 return 1;
316 spin_lock_irqsave(&pendowner->pi_lock, flags);
317 if (task->prio >= pendowner->prio) {
318 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
319 return 0;
323 * Check if a waiter is enqueued on the pending owners
324 * pi_waiters list. Remove it and readjust pending owners
325 * priority.
327 if (likely(!rt_mutex_has_waiters(lock))) {
328 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
329 return 1;
332 /* No chain handling, pending owner is not blocked on anything: */
333 next = rt_mutex_top_waiter(lock);
334 plist_del(&next->pi_list_entry, &pendowner->pi_waiters);
335 __rt_mutex_adjust_prio(pendowner);
336 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
339 * We are going to steal the lock and a waiter was
340 * enqueued on the pending owners pi_waiters queue. So
341 * we have to enqueue this waiter into
342 * task->pi_waiters list. This covers the case,
343 * where task is boosted because it holds another
344 * lock and gets unboosted because the booster is
345 * interrupted, so we would delay a waiter with higher
346 * priority as task->normal_prio.
348 * Note: in the rare case of a SCHED_OTHER task changing
349 * its priority and thus stealing the lock, next->task
350 * might be task:
352 if (likely(next->task != task)) {
353 spin_lock_irqsave(&task->pi_lock, flags);
354 plist_add(&next->pi_list_entry, &task->pi_waiters);
355 __rt_mutex_adjust_prio(task);
356 spin_unlock_irqrestore(&task->pi_lock, flags);
358 return 1;
362 * Try to take an rt-mutex
364 * This fails
365 * - when the lock has a real owner
366 * - when a different pending owner exists and has higher priority than current
368 * Must be called with lock->wait_lock held.
370 static int try_to_take_rt_mutex(struct rt_mutex *lock)
373 * We have to be careful here if the atomic speedups are
374 * enabled, such that, when
375 * - no other waiter is on the lock
376 * - the lock has been released since we did the cmpxchg
377 * the lock can be released or taken while we are doing the
378 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
380 * The atomic acquire/release aware variant of
381 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
382 * the WAITERS bit, the atomic release / acquire can not
383 * happen anymore and lock->wait_lock protects us from the
384 * non-atomic case.
386 * Note, that this might set lock->owner =
387 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
388 * any more. This is fixed up when we take the ownership.
389 * This is the transitional state explained at the top of this file.
391 mark_rt_mutex_waiters(lock);
393 if (rt_mutex_owner(lock) && !try_to_steal_lock(lock, current))
394 return 0;
396 /* We got the lock. */
397 debug_rt_mutex_lock(lock);
399 rt_mutex_set_owner(lock, current, 0);
401 rt_mutex_deadlock_account_lock(lock, current);
403 return 1;
407 * Task blocks on lock.
409 * Prepare waiter and propagate pi chain
411 * This must be called with lock->wait_lock held.
413 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
414 struct rt_mutex_waiter *waiter,
415 struct task_struct *task,
416 int detect_deadlock)
418 struct task_struct *owner = rt_mutex_owner(lock);
419 struct rt_mutex_waiter *top_waiter = waiter;
420 unsigned long flags;
421 int chain_walk = 0, res;
423 spin_lock_irqsave(&task->pi_lock, flags);
424 __rt_mutex_adjust_prio(task);
425 waiter->task = task;
426 waiter->lock = lock;
427 plist_node_init(&waiter->list_entry, task->prio);
428 plist_node_init(&waiter->pi_list_entry, task->prio);
430 /* Get the top priority waiter on the lock */
431 if (rt_mutex_has_waiters(lock))
432 top_waiter = rt_mutex_top_waiter(lock);
433 plist_add(&waiter->list_entry, &lock->wait_list);
435 task->pi_blocked_on = waiter;
437 spin_unlock_irqrestore(&task->pi_lock, flags);
439 if (waiter == rt_mutex_top_waiter(lock)) {
440 spin_lock_irqsave(&owner->pi_lock, flags);
441 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
442 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
444 __rt_mutex_adjust_prio(owner);
445 if (owner->pi_blocked_on)
446 chain_walk = 1;
447 spin_unlock_irqrestore(&owner->pi_lock, flags);
449 else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock))
450 chain_walk = 1;
452 if (!chain_walk)
453 return 0;
456 * The owner can't disappear while holding a lock,
457 * so the owner struct is protected by wait_lock.
458 * Gets dropped in rt_mutex_adjust_prio_chain()!
460 get_task_struct(owner);
462 spin_unlock(&lock->wait_lock);
464 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
465 task);
467 spin_lock(&lock->wait_lock);
469 return res;
473 * Wake up the next waiter on the lock.
475 * Remove the top waiter from the current tasks waiter list and from
476 * the lock waiter list. Set it as pending owner. Then wake it up.
478 * Called with lock->wait_lock held.
480 static void wakeup_next_waiter(struct rt_mutex *lock)
482 struct rt_mutex_waiter *waiter;
483 struct task_struct *pendowner;
484 unsigned long flags;
486 spin_lock_irqsave(&current->pi_lock, flags);
488 waiter = rt_mutex_top_waiter(lock);
489 plist_del(&waiter->list_entry, &lock->wait_list);
492 * Remove it from current->pi_waiters. We do not adjust a
493 * possible priority boost right now. We execute wakeup in the
494 * boosted mode and go back to normal after releasing
495 * lock->wait_lock.
497 plist_del(&waiter->pi_list_entry, &current->pi_waiters);
498 pendowner = waiter->task;
499 waiter->task = NULL;
501 rt_mutex_set_owner(lock, pendowner, RT_MUTEX_OWNER_PENDING);
503 spin_unlock_irqrestore(&current->pi_lock, flags);
506 * Clear the pi_blocked_on variable and enqueue a possible
507 * waiter into the pi_waiters list of the pending owner. This
508 * prevents that in case the pending owner gets unboosted a
509 * waiter with higher priority than pending-owner->normal_prio
510 * is blocked on the unboosted (pending) owner.
512 spin_lock_irqsave(&pendowner->pi_lock, flags);
514 WARN_ON(!pendowner->pi_blocked_on);
515 WARN_ON(pendowner->pi_blocked_on != waiter);
516 WARN_ON(pendowner->pi_blocked_on->lock != lock);
518 pendowner->pi_blocked_on = NULL;
520 if (rt_mutex_has_waiters(lock)) {
521 struct rt_mutex_waiter *next;
523 next = rt_mutex_top_waiter(lock);
524 plist_add(&next->pi_list_entry, &pendowner->pi_waiters);
526 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
528 wake_up_process(pendowner);
532 * Remove a waiter from a lock
534 * Must be called with lock->wait_lock held
536 static void remove_waiter(struct rt_mutex *lock,
537 struct rt_mutex_waiter *waiter)
539 int first = (waiter == rt_mutex_top_waiter(lock));
540 struct task_struct *owner = rt_mutex_owner(lock);
541 unsigned long flags;
542 int chain_walk = 0;
544 spin_lock_irqsave(&current->pi_lock, flags);
545 plist_del(&waiter->list_entry, &lock->wait_list);
546 waiter->task = NULL;
547 current->pi_blocked_on = NULL;
548 spin_unlock_irqrestore(&current->pi_lock, flags);
550 if (first && owner != current) {
552 spin_lock_irqsave(&owner->pi_lock, flags);
554 plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
556 if (rt_mutex_has_waiters(lock)) {
557 struct rt_mutex_waiter *next;
559 next = rt_mutex_top_waiter(lock);
560 plist_add(&next->pi_list_entry, &owner->pi_waiters);
562 __rt_mutex_adjust_prio(owner);
564 if (owner->pi_blocked_on)
565 chain_walk = 1;
567 spin_unlock_irqrestore(&owner->pi_lock, flags);
570 WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
572 if (!chain_walk)
573 return;
575 /* gets dropped in rt_mutex_adjust_prio_chain()! */
576 get_task_struct(owner);
578 spin_unlock(&lock->wait_lock);
580 rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
582 spin_lock(&lock->wait_lock);
586 * Recheck the pi chain, in case we got a priority setting
588 * Called from sched_setscheduler
590 void rt_mutex_adjust_pi(struct task_struct *task)
592 struct rt_mutex_waiter *waiter;
593 unsigned long flags;
595 spin_lock_irqsave(&task->pi_lock, flags);
597 waiter = task->pi_blocked_on;
598 if (!waiter || waiter->list_entry.prio == task->prio) {
599 spin_unlock_irqrestore(&task->pi_lock, flags);
600 return;
603 spin_unlock_irqrestore(&task->pi_lock, flags);
605 /* gets dropped in rt_mutex_adjust_prio_chain()! */
606 get_task_struct(task);
607 rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
611 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
612 * @lock: the rt_mutex to take
613 * @state: the state the task should block in (TASK_INTERRUPTIBLE
614 * or TASK_UNINTERRUPTIBLE)
615 * @timeout: the pre-initialized and started timer, or NULL for none
616 * @waiter: the pre-initialized rt_mutex_waiter
617 * @detect_deadlock: passed to task_blocks_on_rt_mutex
619 * lock->wait_lock must be held by the caller.
621 static int __sched
622 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
623 struct hrtimer_sleeper *timeout,
624 struct rt_mutex_waiter *waiter,
625 int detect_deadlock)
627 int ret = 0;
629 for (;;) {
630 /* Try to acquire the lock: */
631 if (try_to_take_rt_mutex(lock))
632 break;
635 * TASK_INTERRUPTIBLE checks for signals and
636 * timeout. Ignored otherwise.
638 if (unlikely(state == TASK_INTERRUPTIBLE)) {
639 /* Signal pending? */
640 if (signal_pending(current))
641 ret = -EINTR;
642 if (timeout && !timeout->task)
643 ret = -ETIMEDOUT;
644 if (ret)
645 break;
649 * waiter->task is NULL the first time we come here and
650 * when we have been woken up by the previous owner
651 * but the lock got stolen by a higher prio task.
653 if (!waiter->task) {
654 ret = task_blocks_on_rt_mutex(lock, waiter, current,
655 detect_deadlock);
657 * If we got woken up by the owner then start loop
658 * all over without going into schedule to try
659 * to get the lock now:
661 if (unlikely(!waiter->task)) {
663 * Reset the return value. We might
664 * have returned with -EDEADLK and the
665 * owner released the lock while we
666 * were walking the pi chain.
668 ret = 0;
669 continue;
671 if (unlikely(ret))
672 break;
675 spin_unlock(&lock->wait_lock);
677 debug_rt_mutex_print_deadlock(waiter);
679 if (waiter->task)
680 schedule_rt_mutex(lock);
682 spin_lock(&lock->wait_lock);
683 set_current_state(state);
686 return ret;
690 * Slow path lock function:
692 static int __sched
693 rt_mutex_slowlock(struct rt_mutex *lock, int state,
694 struct hrtimer_sleeper *timeout,
695 int detect_deadlock)
697 struct rt_mutex_waiter waiter;
698 int ret = 0;
700 debug_rt_mutex_init_waiter(&waiter);
701 waiter.task = NULL;
703 spin_lock(&lock->wait_lock);
705 /* Try to acquire the lock again: */
706 if (try_to_take_rt_mutex(lock)) {
707 spin_unlock(&lock->wait_lock);
708 return 0;
711 set_current_state(state);
713 /* Setup the timer, when timeout != NULL */
714 if (unlikely(timeout)) {
715 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
716 if (!hrtimer_active(&timeout->timer))
717 timeout->task = NULL;
720 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
721 detect_deadlock);
723 set_current_state(TASK_RUNNING);
725 if (unlikely(waiter.task))
726 remove_waiter(lock, &waiter);
729 * try_to_take_rt_mutex() sets the waiter bit
730 * unconditionally. We might have to fix that up.
732 fixup_rt_mutex_waiters(lock);
734 spin_unlock(&lock->wait_lock);
736 /* Remove pending timer: */
737 if (unlikely(timeout))
738 hrtimer_cancel(&timeout->timer);
741 * Readjust priority, when we did not get the lock. We might
742 * have been the pending owner and boosted. Since we did not
743 * take the lock, the PI boost has to go.
745 if (unlikely(ret))
746 rt_mutex_adjust_prio(current);
748 debug_rt_mutex_free_waiter(&waiter);
750 return ret;
754 * Slow path try-lock function:
756 static inline int
757 rt_mutex_slowtrylock(struct rt_mutex *lock)
759 int ret = 0;
761 spin_lock(&lock->wait_lock);
763 if (likely(rt_mutex_owner(lock) != current)) {
765 ret = try_to_take_rt_mutex(lock);
767 * try_to_take_rt_mutex() sets the lock waiters
768 * bit unconditionally. Clean this up.
770 fixup_rt_mutex_waiters(lock);
773 spin_unlock(&lock->wait_lock);
775 return ret;
779 * Slow path to release a rt-mutex:
781 static void __sched
782 rt_mutex_slowunlock(struct rt_mutex *lock)
784 spin_lock(&lock->wait_lock);
786 debug_rt_mutex_unlock(lock);
788 rt_mutex_deadlock_account_unlock(current);
790 if (!rt_mutex_has_waiters(lock)) {
791 lock->owner = NULL;
792 spin_unlock(&lock->wait_lock);
793 return;
796 wakeup_next_waiter(lock);
798 spin_unlock(&lock->wait_lock);
800 /* Undo pi boosting if necessary: */
801 rt_mutex_adjust_prio(current);
805 * debug aware fast / slowpath lock,trylock,unlock
807 * The atomic acquire/release ops are compiled away, when either the
808 * architecture does not support cmpxchg or when debugging is enabled.
810 static inline int
811 rt_mutex_fastlock(struct rt_mutex *lock, int state,
812 int detect_deadlock,
813 int (*slowfn)(struct rt_mutex *lock, int state,
814 struct hrtimer_sleeper *timeout,
815 int detect_deadlock))
817 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
818 rt_mutex_deadlock_account_lock(lock, current);
819 return 0;
820 } else
821 return slowfn(lock, state, NULL, detect_deadlock);
824 static inline int
825 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
826 struct hrtimer_sleeper *timeout, int detect_deadlock,
827 int (*slowfn)(struct rt_mutex *lock, int state,
828 struct hrtimer_sleeper *timeout,
829 int detect_deadlock))
831 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
832 rt_mutex_deadlock_account_lock(lock, current);
833 return 0;
834 } else
835 return slowfn(lock, state, timeout, detect_deadlock);
838 static inline int
839 rt_mutex_fasttrylock(struct rt_mutex *lock,
840 int (*slowfn)(struct rt_mutex *lock))
842 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
843 rt_mutex_deadlock_account_lock(lock, current);
844 return 1;
846 return slowfn(lock);
849 static inline void
850 rt_mutex_fastunlock(struct rt_mutex *lock,
851 void (*slowfn)(struct rt_mutex *lock))
853 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
854 rt_mutex_deadlock_account_unlock(current);
855 else
856 slowfn(lock);
860 * rt_mutex_lock - lock a rt_mutex
862 * @lock: the rt_mutex to be locked
864 void __sched rt_mutex_lock(struct rt_mutex *lock)
866 might_sleep();
868 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
870 EXPORT_SYMBOL_GPL(rt_mutex_lock);
873 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
875 * @lock: the rt_mutex to be locked
876 * @detect_deadlock: deadlock detection on/off
878 * Returns:
879 * 0 on success
880 * -EINTR when interrupted by a signal
881 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
883 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
884 int detect_deadlock)
886 might_sleep();
888 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
889 detect_deadlock, rt_mutex_slowlock);
891 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
894 * rt_mutex_timed_lock - lock a rt_mutex interruptible
895 * the timeout structure is provided
896 * by the caller
898 * @lock: the rt_mutex to be locked
899 * @timeout: timeout structure or NULL (no timeout)
900 * @detect_deadlock: deadlock detection on/off
902 * Returns:
903 * 0 on success
904 * -EINTR when interrupted by a signal
905 * -ETIMEDOUT when the timeout expired
906 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
909 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
910 int detect_deadlock)
912 might_sleep();
914 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
915 detect_deadlock, rt_mutex_slowlock);
917 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
920 * rt_mutex_trylock - try to lock a rt_mutex
922 * @lock: the rt_mutex to be locked
924 * Returns 1 on success and 0 on contention
926 int __sched rt_mutex_trylock(struct rt_mutex *lock)
928 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
930 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
933 * rt_mutex_unlock - unlock a rt_mutex
935 * @lock: the rt_mutex to be unlocked
937 void __sched rt_mutex_unlock(struct rt_mutex *lock)
939 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
941 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
944 * rt_mutex_destroy - mark a mutex unusable
945 * @lock: the mutex to be destroyed
947 * This function marks the mutex uninitialized, and any subsequent
948 * use of the mutex is forbidden. The mutex must not be locked when
949 * this function is called.
951 void rt_mutex_destroy(struct rt_mutex *lock)
953 WARN_ON(rt_mutex_is_locked(lock));
954 #ifdef CONFIG_DEBUG_RT_MUTEXES
955 lock->magic = NULL;
956 #endif
959 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
962 * __rt_mutex_init - initialize the rt lock
964 * @lock: the rt lock to be initialized
966 * Initialize the rt lock to unlocked state.
968 * Initializing of a locked rt lock is not allowed
970 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
972 lock->owner = NULL;
973 spin_lock_init(&lock->wait_lock);
974 plist_head_init(&lock->wait_list, &lock->wait_lock);
976 debug_rt_mutex_init(lock, name);
978 EXPORT_SYMBOL_GPL(__rt_mutex_init);
981 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
982 * proxy owner
984 * @lock: the rt_mutex to be locked
985 * @proxy_owner:the task to set as owner
987 * No locking. Caller has to do serializing itself
988 * Special API call for PI-futex support
990 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
991 struct task_struct *proxy_owner)
993 __rt_mutex_init(lock, NULL);
994 debug_rt_mutex_proxy_lock(lock, proxy_owner);
995 rt_mutex_set_owner(lock, proxy_owner, 0);
996 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1000 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1002 * @lock: the rt_mutex to be locked
1004 * No locking. Caller has to do serializing itself
1005 * Special API call for PI-futex support
1007 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1008 struct task_struct *proxy_owner)
1010 debug_rt_mutex_proxy_unlock(lock);
1011 rt_mutex_set_owner(lock, NULL, 0);
1012 rt_mutex_deadlock_account_unlock(proxy_owner);
1016 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1017 * @lock: the rt_mutex to take
1018 * @waiter: the pre-initialized rt_mutex_waiter
1019 * @task: the task to prepare
1020 * @detect_deadlock: perform deadlock detection (1) or not (0)
1022 * Returns:
1023 * 0 - task blocked on lock
1024 * 1 - acquired the lock for task, caller should wake it up
1025 * <0 - error
1027 * Special API call for FUTEX_REQUEUE_PI support.
1029 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1030 struct rt_mutex_waiter *waiter,
1031 struct task_struct *task, int detect_deadlock)
1033 int ret;
1035 spin_lock(&lock->wait_lock);
1037 mark_rt_mutex_waiters(lock);
1039 if (!rt_mutex_owner(lock) || try_to_steal_lock(lock, task)) {
1040 /* We got the lock for task. */
1041 debug_rt_mutex_lock(lock);
1042 rt_mutex_set_owner(lock, task, 0);
1043 spin_unlock(&lock->wait_lock);
1044 rt_mutex_deadlock_account_lock(lock, task);
1045 return 1;
1048 ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
1050 if (ret && !waiter->task) {
1052 * Reset the return value. We might have
1053 * returned with -EDEADLK and the owner
1054 * released the lock while we were walking the
1055 * pi chain. Let the waiter sort it out.
1057 ret = 0;
1059 spin_unlock(&lock->wait_lock);
1061 debug_rt_mutex_print_deadlock(waiter);
1063 return ret;
1067 * rt_mutex_next_owner - return the next owner of the lock
1069 * @lock: the rt lock query
1071 * Returns the next owner of the lock or NULL
1073 * Caller has to serialize against other accessors to the lock
1074 * itself.
1076 * Special API call for PI-futex support
1078 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1080 if (!rt_mutex_has_waiters(lock))
1081 return NULL;
1083 return rt_mutex_top_waiter(lock)->task;
1087 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1088 * @lock: the rt_mutex we were woken on
1089 * @to: the timeout, null if none. hrtimer should already have
1090 * been started.
1091 * @waiter: the pre-initialized rt_mutex_waiter
1092 * @detect_deadlock: perform deadlock detection (1) or not (0)
1094 * Complete the lock acquisition started our behalf by another thread.
1096 * Returns:
1097 * 0 - success
1098 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1100 * Special API call for PI-futex requeue support
1102 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1103 struct hrtimer_sleeper *to,
1104 struct rt_mutex_waiter *waiter,
1105 int detect_deadlock)
1107 int ret;
1109 spin_lock(&lock->wait_lock);
1111 set_current_state(TASK_INTERRUPTIBLE);
1113 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter,
1114 detect_deadlock);
1116 set_current_state(TASK_RUNNING);
1118 if (unlikely(waiter->task))
1119 remove_waiter(lock, waiter);
1122 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1123 * have to fix that up.
1125 fixup_rt_mutex_waiters(lock);
1127 spin_unlock(&lock->wait_lock);
1130 * Readjust priority, when we did not get the lock. We might have been
1131 * the pending owner and boosted. Since we did not take the lock, the
1132 * PI boost has to go.
1134 if (unlikely(ret))
1135 rt_mutex_adjust_prio(current);
1137 return ret;