[ARM] Remove yucky ifdefs to print "id(wb)BRR" suffix on CPU name
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / rtmutex.c
blob45d61016da57612ec7690568abaef95a4515c547
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 #include <linux/spinlock.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/timer.h>
16 #include "rtmutex_common.h"
18 #ifdef CONFIG_DEBUG_RT_MUTEXES
19 # include "rtmutex-debug.h"
20 #else
21 # include "rtmutex.h"
22 #endif
25 * lock->owner state tracking:
27 * lock->owner holds the task_struct pointer of the owner. Bit 0 and 1
28 * are used to keep track of the "owner is pending" and "lock has
29 * waiters" state.
31 * owner bit1 bit0
32 * NULL 0 0 lock is free (fast acquire possible)
33 * NULL 0 1 invalid state
34 * NULL 1 0 Transitional State*
35 * NULL 1 1 invalid state
36 * taskpointer 0 0 lock is held (fast release possible)
37 * taskpointer 0 1 task is pending owner
38 * taskpointer 1 0 lock is held and has waiters
39 * taskpointer 1 1 task is pending owner and lock has more waiters
41 * Pending ownership is assigned to the top (highest priority)
42 * waiter of the lock, when the lock is released. The thread is woken
43 * up and can now take the lock. Until the lock is taken (bit 0
44 * cleared) a competing higher priority thread can steal the lock
45 * which puts the woken up thread back on the waiters list.
47 * The fast atomic compare exchange based acquire and release is only
48 * possible when bit 0 and 1 of lock->owner are 0.
50 * (*) There's a small time where the owner can be NULL and the
51 * "lock has waiters" bit is set. This can happen when grabbing the lock.
52 * To prevent a cmpxchg of the owner releasing the lock, we need to set this
53 * bit before looking at the lock, hence the reason this is a transitional
54 * state.
57 static void
58 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner,
59 unsigned long mask)
61 unsigned long val = (unsigned long)owner | mask;
63 if (rt_mutex_has_waiters(lock))
64 val |= RT_MUTEX_HAS_WAITERS;
66 lock->owner = (struct task_struct *)val;
69 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
71 lock->owner = (struct task_struct *)
72 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
75 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
77 if (!rt_mutex_has_waiters(lock))
78 clear_rt_mutex_waiters(lock);
82 * We can speed up the acquire/release, if the architecture
83 * supports cmpxchg and if there's no debugging state to be set up
85 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
86 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
87 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
89 unsigned long owner, *p = (unsigned long *) &lock->owner;
91 do {
92 owner = *p;
93 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
95 #else
96 # define rt_mutex_cmpxchg(l,c,n) (0)
97 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
99 lock->owner = (struct task_struct *)
100 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
102 #endif
105 * Calculate task priority from the waiter list priority
107 * Return task->normal_prio when the waiter list is empty or when
108 * the waiter is not allowed to do priority boosting
110 int rt_mutex_getprio(struct task_struct *task)
112 if (likely(!task_has_pi_waiters(task)))
113 return task->normal_prio;
115 return min(task_top_pi_waiter(task)->pi_list_entry.prio,
116 task->normal_prio);
120 * Adjust the priority of a task, after its pi_waiters got modified.
122 * This can be both boosting and unboosting. task->pi_lock must be held.
124 static void __rt_mutex_adjust_prio(struct task_struct *task)
126 int prio = rt_mutex_getprio(task);
128 if (task->prio != prio)
129 rt_mutex_setprio(task, prio);
133 * Adjust task priority (undo boosting). Called from the exit path of
134 * rt_mutex_slowunlock() and rt_mutex_slowlock().
136 * (Note: We do this outside of the protection of lock->wait_lock to
137 * allow the lock to be taken while or before we readjust the priority
138 * of task. We do not use the spin_xx_mutex() variants here as we are
139 * outside of the debug path.)
141 static void rt_mutex_adjust_prio(struct task_struct *task)
143 unsigned long flags;
145 spin_lock_irqsave(&task->pi_lock, flags);
146 __rt_mutex_adjust_prio(task);
147 spin_unlock_irqrestore(&task->pi_lock, flags);
151 * Max number of times we'll walk the boosting chain:
153 int max_lock_depth = 1024;
156 * Adjust the priority chain. Also used for deadlock detection.
157 * Decreases task's usage by one - may thus free the task.
158 * Returns 0 or -EDEADLK.
160 static int rt_mutex_adjust_prio_chain(task_t *task,
161 int deadlock_detect,
162 struct rt_mutex *orig_lock,
163 struct rt_mutex_waiter *orig_waiter,
164 struct task_struct *top_task
165 __IP_DECL__)
167 struct rt_mutex *lock;
168 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
169 int detect_deadlock, ret = 0, depth = 0;
170 unsigned long flags;
172 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
173 deadlock_detect);
176 * The (de)boosting is a step by step approach with a lot of
177 * pitfalls. We want this to be preemptible and we want hold a
178 * maximum of two locks per step. So we have to check
179 * carefully whether things change under us.
181 again:
182 if (++depth > max_lock_depth) {
183 static int prev_max;
186 * Print this only once. If the admin changes the limit,
187 * print a new message when reaching the limit again.
189 if (prev_max != max_lock_depth) {
190 prev_max = max_lock_depth;
191 printk(KERN_WARNING "Maximum lock depth %d reached "
192 "task: %s (%d)\n", max_lock_depth,
193 top_task->comm, top_task->pid);
195 put_task_struct(task);
197 return deadlock_detect ? -EDEADLK : 0;
199 retry:
201 * Task can not go away as we did a get_task() before !
203 spin_lock_irqsave(&task->pi_lock, flags);
205 waiter = task->pi_blocked_on;
207 * Check whether the end of the boosting chain has been
208 * reached or the state of the chain has changed while we
209 * dropped the locks.
211 if (!waiter || !waiter->task)
212 goto out_unlock_pi;
214 if (top_waiter && (!task_has_pi_waiters(task) ||
215 top_waiter != task_top_pi_waiter(task)))
216 goto out_unlock_pi;
219 * When deadlock detection is off then we check, if further
220 * priority adjustment is necessary.
222 if (!detect_deadlock && waiter->list_entry.prio == task->prio)
223 goto out_unlock_pi;
225 lock = waiter->lock;
226 if (!spin_trylock(&lock->wait_lock)) {
227 spin_unlock_irqrestore(&task->pi_lock, flags);
228 cpu_relax();
229 goto retry;
232 /* Deadlock detection */
233 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
234 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
235 spin_unlock(&lock->wait_lock);
236 ret = deadlock_detect ? -EDEADLK : 0;
237 goto out_unlock_pi;
240 top_waiter = rt_mutex_top_waiter(lock);
242 /* Requeue the waiter */
243 plist_del(&waiter->list_entry, &lock->wait_list);
244 waiter->list_entry.prio = task->prio;
245 plist_add(&waiter->list_entry, &lock->wait_list);
247 /* Release the task */
248 spin_unlock_irqrestore(&task->pi_lock, flags);
249 put_task_struct(task);
251 /* Grab the next task */
252 task = rt_mutex_owner(lock);
253 spin_lock_irqsave(&task->pi_lock, flags);
255 if (waiter == rt_mutex_top_waiter(lock)) {
256 /* Boost the owner */
257 plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
258 waiter->pi_list_entry.prio = waiter->list_entry.prio;
259 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
260 __rt_mutex_adjust_prio(task);
262 } else if (top_waiter == waiter) {
263 /* Deboost the owner */
264 plist_del(&waiter->pi_list_entry, &task->pi_waiters);
265 waiter = rt_mutex_top_waiter(lock);
266 waiter->pi_list_entry.prio = waiter->list_entry.prio;
267 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
268 __rt_mutex_adjust_prio(task);
271 get_task_struct(task);
272 spin_unlock_irqrestore(&task->pi_lock, flags);
274 top_waiter = rt_mutex_top_waiter(lock);
275 spin_unlock(&lock->wait_lock);
277 if (!detect_deadlock && waiter != top_waiter)
278 goto out_put_task;
280 goto again;
282 out_unlock_pi:
283 spin_unlock_irqrestore(&task->pi_lock, flags);
284 out_put_task:
285 put_task_struct(task);
286 return ret;
290 * Optimization: check if we can steal the lock from the
291 * assigned pending owner [which might not have taken the
292 * lock yet]:
294 static inline int try_to_steal_lock(struct rt_mutex *lock)
296 struct task_struct *pendowner = rt_mutex_owner(lock);
297 struct rt_mutex_waiter *next;
298 unsigned long flags;
300 if (!rt_mutex_owner_pending(lock))
301 return 0;
303 if (pendowner == current)
304 return 1;
306 spin_lock_irqsave(&pendowner->pi_lock, flags);
307 if (current->prio >= pendowner->prio) {
308 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
309 return 0;
313 * Check if a waiter is enqueued on the pending owners
314 * pi_waiters list. Remove it and readjust pending owners
315 * priority.
317 if (likely(!rt_mutex_has_waiters(lock))) {
318 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
319 return 1;
322 /* No chain handling, pending owner is not blocked on anything: */
323 next = rt_mutex_top_waiter(lock);
324 plist_del(&next->pi_list_entry, &pendowner->pi_waiters);
325 __rt_mutex_adjust_prio(pendowner);
326 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
329 * We are going to steal the lock and a waiter was
330 * enqueued on the pending owners pi_waiters queue. So
331 * we have to enqueue this waiter into
332 * current->pi_waiters list. This covers the case,
333 * where current is boosted because it holds another
334 * lock and gets unboosted because the booster is
335 * interrupted, so we would delay a waiter with higher
336 * priority as current->normal_prio.
338 * Note: in the rare case of a SCHED_OTHER task changing
339 * its priority and thus stealing the lock, next->task
340 * might be current:
342 if (likely(next->task != current)) {
343 spin_lock_irqsave(&current->pi_lock, flags);
344 plist_add(&next->pi_list_entry, &current->pi_waiters);
345 __rt_mutex_adjust_prio(current);
346 spin_unlock_irqrestore(&current->pi_lock, flags);
348 return 1;
352 * Try to take an rt-mutex
354 * This fails
355 * - when the lock has a real owner
356 * - when a different pending owner exists and has higher priority than current
358 * Must be called with lock->wait_lock held.
360 static int try_to_take_rt_mutex(struct rt_mutex *lock __IP_DECL__)
363 * We have to be careful here if the atomic speedups are
364 * enabled, such that, when
365 * - no other waiter is on the lock
366 * - the lock has been released since we did the cmpxchg
367 * the lock can be released or taken while we are doing the
368 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
370 * The atomic acquire/release aware variant of
371 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
372 * the WAITERS bit, the atomic release / acquire can not
373 * happen anymore and lock->wait_lock protects us from the
374 * non-atomic case.
376 * Note, that this might set lock->owner =
377 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
378 * any more. This is fixed up when we take the ownership.
379 * This is the transitional state explained at the top of this file.
381 mark_rt_mutex_waiters(lock);
383 if (rt_mutex_owner(lock) && !try_to_steal_lock(lock))
384 return 0;
386 /* We got the lock. */
387 debug_rt_mutex_lock(lock __IP__);
389 rt_mutex_set_owner(lock, current, 0);
391 rt_mutex_deadlock_account_lock(lock, current);
393 return 1;
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 int detect_deadlock
406 __IP_DECL__)
408 struct rt_mutex_waiter *top_waiter = waiter;
409 task_t *owner = rt_mutex_owner(lock);
410 int boost = 0, res;
411 unsigned long flags;
413 spin_lock_irqsave(&current->pi_lock, flags);
414 __rt_mutex_adjust_prio(current);
415 waiter->task = current;
416 waiter->lock = lock;
417 plist_node_init(&waiter->list_entry, current->prio);
418 plist_node_init(&waiter->pi_list_entry, current->prio);
420 /* Get the top priority waiter on the lock */
421 if (rt_mutex_has_waiters(lock))
422 top_waiter = rt_mutex_top_waiter(lock);
423 plist_add(&waiter->list_entry, &lock->wait_list);
425 current->pi_blocked_on = waiter;
427 spin_unlock_irqrestore(&current->pi_lock, flags);
429 if (waiter == rt_mutex_top_waiter(lock)) {
430 spin_lock_irqsave(&owner->pi_lock, flags);
431 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
432 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
434 __rt_mutex_adjust_prio(owner);
435 if (owner->pi_blocked_on) {
436 boost = 1;
437 /* gets dropped in rt_mutex_adjust_prio_chain()! */
438 get_task_struct(owner);
440 spin_unlock_irqrestore(&owner->pi_lock, flags);
442 else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
443 spin_lock_irqsave(&owner->pi_lock, flags);
444 if (owner->pi_blocked_on) {
445 boost = 1;
446 /* gets dropped in rt_mutex_adjust_prio_chain()! */
447 get_task_struct(owner);
449 spin_unlock_irqrestore(&owner->pi_lock, flags);
451 if (!boost)
452 return 0;
454 spin_unlock(&lock->wait_lock);
456 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
457 current __IP__);
459 spin_lock(&lock->wait_lock);
461 return res;
465 * Wake up the next waiter on the lock.
467 * Remove the top waiter from the current tasks waiter list and from
468 * the lock waiter list. Set it as pending owner. Then wake it up.
470 * Called with lock->wait_lock held.
472 static void wakeup_next_waiter(struct rt_mutex *lock)
474 struct rt_mutex_waiter *waiter;
475 struct task_struct *pendowner;
476 unsigned long flags;
478 spin_lock_irqsave(&current->pi_lock, flags);
480 waiter = rt_mutex_top_waiter(lock);
481 plist_del(&waiter->list_entry, &lock->wait_list);
484 * Remove it from current->pi_waiters. We do not adjust a
485 * possible priority boost right now. We execute wakeup in the
486 * boosted mode and go back to normal after releasing
487 * lock->wait_lock.
489 plist_del(&waiter->pi_list_entry, &current->pi_waiters);
490 pendowner = waiter->task;
491 waiter->task = NULL;
493 rt_mutex_set_owner(lock, pendowner, RT_MUTEX_OWNER_PENDING);
495 spin_unlock_irqrestore(&current->pi_lock, flags);
498 * Clear the pi_blocked_on variable and enqueue a possible
499 * waiter into the pi_waiters list of the pending owner. This
500 * prevents that in case the pending owner gets unboosted a
501 * waiter with higher priority than pending-owner->normal_prio
502 * is blocked on the unboosted (pending) owner.
504 spin_lock_irqsave(&pendowner->pi_lock, flags);
506 WARN_ON(!pendowner->pi_blocked_on);
507 WARN_ON(pendowner->pi_blocked_on != waiter);
508 WARN_ON(pendowner->pi_blocked_on->lock != lock);
510 pendowner->pi_blocked_on = NULL;
512 if (rt_mutex_has_waiters(lock)) {
513 struct rt_mutex_waiter *next;
515 next = rt_mutex_top_waiter(lock);
516 plist_add(&next->pi_list_entry, &pendowner->pi_waiters);
518 spin_unlock_irqrestore(&pendowner->pi_lock, flags);
520 wake_up_process(pendowner);
524 * Remove a waiter from a lock
526 * Must be called with lock->wait_lock held
528 static void remove_waiter(struct rt_mutex *lock,
529 struct rt_mutex_waiter *waiter __IP_DECL__)
531 int first = (waiter == rt_mutex_top_waiter(lock));
532 int boost = 0;
533 task_t *owner = rt_mutex_owner(lock);
534 unsigned long flags;
536 spin_lock_irqsave(&current->pi_lock, flags);
537 plist_del(&waiter->list_entry, &lock->wait_list);
538 waiter->task = NULL;
539 current->pi_blocked_on = NULL;
540 spin_unlock_irqrestore(&current->pi_lock, flags);
542 if (first && owner != current) {
544 spin_lock_irqsave(&owner->pi_lock, flags);
546 plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
548 if (rt_mutex_has_waiters(lock)) {
549 struct rt_mutex_waiter *next;
551 next = rt_mutex_top_waiter(lock);
552 plist_add(&next->pi_list_entry, &owner->pi_waiters);
554 __rt_mutex_adjust_prio(owner);
556 if (owner->pi_blocked_on) {
557 boost = 1;
558 /* gets dropped in rt_mutex_adjust_prio_chain()! */
559 get_task_struct(owner);
561 spin_unlock_irqrestore(&owner->pi_lock, flags);
564 WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
566 if (!boost)
567 return;
569 spin_unlock(&lock->wait_lock);
571 rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current __IP__);
573 spin_lock(&lock->wait_lock);
577 * Recheck the pi chain, in case we got a priority setting
579 * Called from sched_setscheduler
581 void rt_mutex_adjust_pi(struct task_struct *task)
583 struct rt_mutex_waiter *waiter;
584 unsigned long flags;
586 spin_lock_irqsave(&task->pi_lock, flags);
588 waiter = task->pi_blocked_on;
589 if (!waiter || waiter->list_entry.prio == task->prio) {
590 spin_unlock_irqrestore(&task->pi_lock, flags);
591 return;
594 /* gets dropped in rt_mutex_adjust_prio_chain()! */
595 get_task_struct(task);
596 spin_unlock_irqrestore(&task->pi_lock, flags);
598 rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task __RET_IP__);
602 * Slow path lock function:
604 static int __sched
605 rt_mutex_slowlock(struct rt_mutex *lock, int state,
606 struct hrtimer_sleeper *timeout,
607 int detect_deadlock __IP_DECL__)
609 struct rt_mutex_waiter waiter;
610 int ret = 0;
612 debug_rt_mutex_init_waiter(&waiter);
613 waiter.task = NULL;
615 spin_lock(&lock->wait_lock);
617 /* Try to acquire the lock again: */
618 if (try_to_take_rt_mutex(lock __IP__)) {
619 spin_unlock(&lock->wait_lock);
620 return 0;
623 set_current_state(state);
625 /* Setup the timer, when timeout != NULL */
626 if (unlikely(timeout))
627 hrtimer_start(&timeout->timer, timeout->timer.expires,
628 HRTIMER_ABS);
630 for (;;) {
631 /* Try to acquire the lock: */
632 if (try_to_take_rt_mutex(lock __IP__))
633 break;
636 * TASK_INTERRUPTIBLE checks for signals and
637 * timeout. Ignored otherwise.
639 if (unlikely(state == TASK_INTERRUPTIBLE)) {
640 /* Signal pending? */
641 if (signal_pending(current))
642 ret = -EINTR;
643 if (timeout && !timeout->task)
644 ret = -ETIMEDOUT;
645 if (ret)
646 break;
650 * waiter.task is NULL the first time we come here and
651 * when we have been woken up by the previous owner
652 * but the lock got stolen by a higher prio task.
654 if (!waiter.task) {
655 ret = task_blocks_on_rt_mutex(lock, &waiter,
656 detect_deadlock __IP__);
658 * If we got woken up by the owner then start loop
659 * all over without going into schedule to try
660 * to get the lock now:
662 if (unlikely(!waiter.task))
663 continue;
665 if (unlikely(ret))
666 break;
669 spin_unlock(&lock->wait_lock);
671 debug_rt_mutex_print_deadlock(&waiter);
673 if (waiter.task)
674 schedule_rt_mutex(lock);
676 spin_lock(&lock->wait_lock);
677 set_current_state(state);
680 set_current_state(TASK_RUNNING);
682 if (unlikely(waiter.task))
683 remove_waiter(lock, &waiter __IP__);
686 * try_to_take_rt_mutex() sets the waiter bit
687 * unconditionally. We might have to fix that up.
689 fixup_rt_mutex_waiters(lock);
691 spin_unlock(&lock->wait_lock);
693 /* Remove pending timer: */
694 if (unlikely(timeout))
695 hrtimer_cancel(&timeout->timer);
698 * Readjust priority, when we did not get the lock. We might
699 * have been the pending owner and boosted. Since we did not
700 * take the lock, the PI boost has to go.
702 if (unlikely(ret))
703 rt_mutex_adjust_prio(current);
705 debug_rt_mutex_free_waiter(&waiter);
707 return ret;
711 * Slow path try-lock function:
713 static inline int
714 rt_mutex_slowtrylock(struct rt_mutex *lock __IP_DECL__)
716 int ret = 0;
718 spin_lock(&lock->wait_lock);
720 if (likely(rt_mutex_owner(lock) != current)) {
722 ret = try_to_take_rt_mutex(lock __IP__);
724 * try_to_take_rt_mutex() sets the lock waiters
725 * bit unconditionally. Clean this up.
727 fixup_rt_mutex_waiters(lock);
730 spin_unlock(&lock->wait_lock);
732 return ret;
736 * Slow path to release a rt-mutex:
738 static void __sched
739 rt_mutex_slowunlock(struct rt_mutex *lock)
741 spin_lock(&lock->wait_lock);
743 debug_rt_mutex_unlock(lock);
745 rt_mutex_deadlock_account_unlock(current);
747 if (!rt_mutex_has_waiters(lock)) {
748 lock->owner = NULL;
749 spin_unlock(&lock->wait_lock);
750 return;
753 wakeup_next_waiter(lock);
755 spin_unlock(&lock->wait_lock);
757 /* Undo pi boosting if necessary: */
758 rt_mutex_adjust_prio(current);
762 * debug aware fast / slowpath lock,trylock,unlock
764 * The atomic acquire/release ops are compiled away, when either the
765 * architecture does not support cmpxchg or when debugging is enabled.
767 static inline int
768 rt_mutex_fastlock(struct rt_mutex *lock, int state,
769 int detect_deadlock,
770 int (*slowfn)(struct rt_mutex *lock, int state,
771 struct hrtimer_sleeper *timeout,
772 int detect_deadlock __IP_DECL__))
774 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
775 rt_mutex_deadlock_account_lock(lock, current);
776 return 0;
777 } else
778 return slowfn(lock, state, NULL, detect_deadlock __RET_IP__);
781 static inline int
782 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
783 struct hrtimer_sleeper *timeout, int detect_deadlock,
784 int (*slowfn)(struct rt_mutex *lock, int state,
785 struct hrtimer_sleeper *timeout,
786 int detect_deadlock __IP_DECL__))
788 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
789 rt_mutex_deadlock_account_lock(lock, current);
790 return 0;
791 } else
792 return slowfn(lock, state, timeout, detect_deadlock __RET_IP__);
795 static inline int
796 rt_mutex_fasttrylock(struct rt_mutex *lock,
797 int (*slowfn)(struct rt_mutex *lock __IP_DECL__))
799 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
800 rt_mutex_deadlock_account_lock(lock, current);
801 return 1;
803 return slowfn(lock __RET_IP__);
806 static inline void
807 rt_mutex_fastunlock(struct rt_mutex *lock,
808 void (*slowfn)(struct rt_mutex *lock))
810 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
811 rt_mutex_deadlock_account_unlock(current);
812 else
813 slowfn(lock);
817 * rt_mutex_lock - lock a rt_mutex
819 * @lock: the rt_mutex to be locked
821 void __sched rt_mutex_lock(struct rt_mutex *lock)
823 might_sleep();
825 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
827 EXPORT_SYMBOL_GPL(rt_mutex_lock);
830 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
832 * @lock: the rt_mutex to be locked
833 * @detect_deadlock: deadlock detection on/off
835 * Returns:
836 * 0 on success
837 * -EINTR when interrupted by a signal
838 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
840 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
841 int detect_deadlock)
843 might_sleep();
845 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
846 detect_deadlock, rt_mutex_slowlock);
848 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
851 * rt_mutex_lock_interruptible_ktime - lock a rt_mutex interruptible
852 * the timeout structure is provided
853 * by the caller
855 * @lock: the rt_mutex to be locked
856 * @timeout: timeout structure or NULL (no timeout)
857 * @detect_deadlock: deadlock detection on/off
859 * Returns:
860 * 0 on success
861 * -EINTR when interrupted by a signal
862 * -ETIMEOUT when the timeout expired
863 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
866 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
867 int detect_deadlock)
869 might_sleep();
871 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
872 detect_deadlock, rt_mutex_slowlock);
874 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
877 * rt_mutex_trylock - try to lock a rt_mutex
879 * @lock: the rt_mutex to be locked
881 * Returns 1 on success and 0 on contention
883 int __sched rt_mutex_trylock(struct rt_mutex *lock)
885 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
887 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
890 * rt_mutex_unlock - unlock a rt_mutex
892 * @lock: the rt_mutex to be unlocked
894 void __sched rt_mutex_unlock(struct rt_mutex *lock)
896 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
898 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
900 /***
901 * rt_mutex_destroy - mark a mutex unusable
902 * @lock: the mutex to be destroyed
904 * This function marks the mutex uninitialized, and any subsequent
905 * use of the mutex is forbidden. The mutex must not be locked when
906 * this function is called.
908 void rt_mutex_destroy(struct rt_mutex *lock)
910 WARN_ON(rt_mutex_is_locked(lock));
911 #ifdef CONFIG_DEBUG_RT_MUTEXES
912 lock->magic = NULL;
913 #endif
916 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
919 * __rt_mutex_init - initialize the rt lock
921 * @lock: the rt lock to be initialized
923 * Initialize the rt lock to unlocked state.
925 * Initializing of a locked rt lock is not allowed
927 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
929 lock->owner = NULL;
930 spin_lock_init(&lock->wait_lock);
931 plist_head_init(&lock->wait_list, &lock->wait_lock);
933 debug_rt_mutex_init(lock, name);
935 EXPORT_SYMBOL_GPL(__rt_mutex_init);
938 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
939 * proxy owner
941 * @lock: the rt_mutex to be locked
942 * @proxy_owner:the task to set as owner
944 * No locking. Caller has to do serializing itself
945 * Special API call for PI-futex support
947 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
948 struct task_struct *proxy_owner)
950 __rt_mutex_init(lock, NULL);
951 debug_rt_mutex_proxy_lock(lock, proxy_owner __RET_IP__);
952 rt_mutex_set_owner(lock, proxy_owner, 0);
953 rt_mutex_deadlock_account_lock(lock, proxy_owner);
957 * rt_mutex_proxy_unlock - release a lock on behalf of owner
959 * @lock: the rt_mutex to be locked
961 * No locking. Caller has to do serializing itself
962 * Special API call for PI-futex support
964 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
965 struct task_struct *proxy_owner)
967 debug_rt_mutex_proxy_unlock(lock);
968 rt_mutex_set_owner(lock, NULL, 0);
969 rt_mutex_deadlock_account_unlock(proxy_owner);
973 * rt_mutex_next_owner - return the next owner of the lock
975 * @lock: the rt lock query
977 * Returns the next owner of the lock or NULL
979 * Caller has to serialize against other accessors to the lock
980 * itself.
982 * Special API call for PI-futex support
984 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
986 if (!rt_mutex_has_waiters(lock))
987 return NULL;
989 return rt_mutex_top_waiter(lock)->task;