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/timer.h>
19 #include "rtmutex_common.h"
22 * lock->owner state tracking:
24 * lock->owner holds the task_struct pointer of the owner. Bit 0
25 * is used to keep track of the "lock has waiters" state.
28 * NULL 0 lock is free (fast acquire possible)
29 * NULL 1 lock is free and has waiters and the top waiter
30 * is going to take the lock*
31 * taskpointer 0 lock is held (fast release possible)
32 * taskpointer 1 lock is held and has waiters**
34 * The fast atomic compare exchange based acquire and release is only
35 * possible when bit 0 of lock->owner is 0.
37 * (*) It also can be a transitional state when grabbing the lock
38 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
39 * we need to set the bit0 before looking at the lock, and the owner may be
40 * NULL in this small time, hence this can be a transitional state.
42 * (**) There is a small time when bit 0 is set but there are no
43 * waiters. This can happen when grabbing the lock in the slow path.
44 * To prevent a cmpxchg of the owner releasing the lock, we need to
45 * set this bit before looking at the lock.
49 rt_mutex_set_owner(struct rt_mutex
*lock
, struct task_struct
*owner
)
51 unsigned long val
= (unsigned long)owner
;
53 if (rt_mutex_has_waiters(lock
))
54 val
|= RT_MUTEX_HAS_WAITERS
;
56 lock
->owner
= (struct task_struct
*)val
;
59 static inline void clear_rt_mutex_waiters(struct rt_mutex
*lock
)
61 lock
->owner
= (struct task_struct
*)
62 ((unsigned long)lock
->owner
& ~RT_MUTEX_HAS_WAITERS
);
65 static void fixup_rt_mutex_waiters(struct rt_mutex
*lock
)
67 if (!rt_mutex_has_waiters(lock
))
68 clear_rt_mutex_waiters(lock
);
72 * We can speed up the acquire/release, if the architecture
73 * supports cmpxchg and if there's no debugging state to be set up
75 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
76 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
77 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
79 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
83 } while (cmpxchg(p
, owner
, owner
| RT_MUTEX_HAS_WAITERS
) != owner
);
86 # define rt_mutex_cmpxchg(l,c,n) (0)
87 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
89 lock
->owner
= (struct task_struct
*)
90 ((unsigned long)lock
->owner
| RT_MUTEX_HAS_WAITERS
);
95 * Calculate task priority from the waiter list priority
97 * Return task->normal_prio when the waiter list is empty or when
98 * the waiter is not allowed to do priority boosting
100 int rt_mutex_getprio(struct task_struct
*task
)
102 if (likely(!task_has_pi_waiters(task
)))
103 return task
->normal_prio
;
105 return min(task_top_pi_waiter(task
)->pi_list_entry
.prio
,
110 * Adjust the priority of a task, after its pi_waiters got modified.
112 * This can be both boosting and unboosting. task->pi_lock must be held.
114 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
116 int prio
= rt_mutex_getprio(task
);
118 if (task
->prio
!= prio
)
119 rt_mutex_setprio(task
, prio
);
123 * Adjust task priority (undo boosting). Called from the exit path of
124 * rt_mutex_slowunlock() and rt_mutex_slowlock().
126 * (Note: We do this outside of the protection of lock->wait_lock to
127 * allow the lock to be taken while or before we readjust the priority
128 * of task. We do not use the spin_xx_mutex() variants here as we are
129 * outside of the debug path.)
131 static void rt_mutex_adjust_prio(struct task_struct
*task
)
135 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
136 __rt_mutex_adjust_prio(task
);
137 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
141 * Max number of times we'll walk the boosting chain:
143 int max_lock_depth
= 1024;
146 * Adjust the priority chain. Also used for deadlock detection.
147 * Decreases task's usage by one - may thus free the task.
148 * Returns 0 or -EDEADLK.
150 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
152 struct rt_mutex
*orig_lock
,
153 struct rt_mutex_waiter
*orig_waiter
,
154 struct task_struct
*top_task
)
156 struct rt_mutex
*lock
;
157 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
158 int detect_deadlock
, ret
= 0, depth
= 0;
161 detect_deadlock
= debug_rt_mutex_detect_deadlock(orig_waiter
,
165 * The (de)boosting is a step by step approach with a lot of
166 * pitfalls. We want this to be preemptible and we want hold a
167 * maximum of two locks per step. So we have to check
168 * carefully whether things change under us.
171 if (++depth
> max_lock_depth
) {
175 * Print this only once. If the admin changes the limit,
176 * print a new message when reaching the limit again.
178 if (prev_max
!= max_lock_depth
) {
179 prev_max
= max_lock_depth
;
180 printk(KERN_WARNING
"Maximum lock depth %d reached "
181 "task: %s (%d)\n", max_lock_depth
,
182 top_task
->comm
, task_pid_nr(top_task
));
184 put_task_struct(task
);
186 return deadlock_detect
? -EDEADLK
: 0;
190 * Task can not go away as we did a get_task() before !
192 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
194 waiter
= task
->pi_blocked_on
;
196 * Check whether the end of the boosting chain has been
197 * reached or the state of the chain has changed while we
204 * Check the orig_waiter state. After we dropped the locks,
205 * the previous owner of the lock might have released the lock.
207 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
211 * Drop out, when the task has no waiters. Note,
212 * top_waiter can be NULL, when we are in the deboosting
215 if (top_waiter
&& (!task_has_pi_waiters(task
) ||
216 top_waiter
!= task_top_pi_waiter(task
)))
220 * When deadlock detection is off then we check, if further
221 * priority adjustment is necessary.
223 if (!detect_deadlock
&& waiter
->list_entry
.prio
== task
->prio
)
227 if (!raw_spin_trylock(&lock
->wait_lock
)) {
228 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
233 /* Deadlock detection */
234 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
235 debug_rt_mutex_deadlock(deadlock_detect
, orig_waiter
, lock
);
236 raw_spin_unlock(&lock
->wait_lock
);
237 ret
= deadlock_detect
? -EDEADLK
: 0;
241 top_waiter
= rt_mutex_top_waiter(lock
);
243 /* Requeue the waiter */
244 plist_del(&waiter
->list_entry
, &lock
->wait_list
);
245 waiter
->list_entry
.prio
= task
->prio
;
246 plist_add(&waiter
->list_entry
, &lock
->wait_list
);
248 /* Release the task */
249 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
250 if (!rt_mutex_owner(lock
)) {
252 * If the requeue above changed the top waiter, then we need
253 * to wake the new top waiter up to try to get the lock.
256 if (top_waiter
!= rt_mutex_top_waiter(lock
))
257 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
258 raw_spin_unlock(&lock
->wait_lock
);
261 put_task_struct(task
);
263 /* Grab the next task */
264 task
= rt_mutex_owner(lock
);
265 get_task_struct(task
);
266 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
268 if (waiter
== rt_mutex_top_waiter(lock
)) {
269 /* Boost the owner */
270 plist_del(&top_waiter
->pi_list_entry
, &task
->pi_waiters
);
271 waiter
->pi_list_entry
.prio
= waiter
->list_entry
.prio
;
272 plist_add(&waiter
->pi_list_entry
, &task
->pi_waiters
);
273 __rt_mutex_adjust_prio(task
);
275 } else if (top_waiter
== waiter
) {
276 /* Deboost the owner */
277 plist_del(&waiter
->pi_list_entry
, &task
->pi_waiters
);
278 waiter
= rt_mutex_top_waiter(lock
);
279 waiter
->pi_list_entry
.prio
= waiter
->list_entry
.prio
;
280 plist_add(&waiter
->pi_list_entry
, &task
->pi_waiters
);
281 __rt_mutex_adjust_prio(task
);
284 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
286 top_waiter
= rt_mutex_top_waiter(lock
);
287 raw_spin_unlock(&lock
->wait_lock
);
289 if (!detect_deadlock
&& waiter
!= top_waiter
)
295 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
297 put_task_struct(task
);
303 * Try to take an rt-mutex
305 * Must be called with lock->wait_lock held.
307 * @lock: the lock to be acquired.
308 * @task: the task which wants to acquire the lock
309 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
311 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
312 struct rt_mutex_waiter
*waiter
)
315 * We have to be careful here if the atomic speedups are
316 * enabled, such that, when
317 * - no other waiter is on the lock
318 * - the lock has been released since we did the cmpxchg
319 * the lock can be released or taken while we are doing the
320 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
322 * The atomic acquire/release aware variant of
323 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
324 * the WAITERS bit, the atomic release / acquire can not
325 * happen anymore and lock->wait_lock protects us from the
328 * Note, that this might set lock->owner =
329 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
330 * any more. This is fixed up when we take the ownership.
331 * This is the transitional state explained at the top of this file.
333 mark_rt_mutex_waiters(lock
);
335 if (rt_mutex_owner(lock
))
339 * It will get the lock because of one of these conditions:
340 * 1) there is no waiter
341 * 2) higher priority than waiters
342 * 3) it is top waiter
344 if (rt_mutex_has_waiters(lock
)) {
345 if (task
->prio
>= rt_mutex_top_waiter(lock
)->list_entry
.prio
) {
346 if (!waiter
|| waiter
!= rt_mutex_top_waiter(lock
))
351 if (waiter
|| rt_mutex_has_waiters(lock
)) {
353 struct rt_mutex_waiter
*top
;
355 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
357 /* remove the queued waiter. */
359 plist_del(&waiter
->list_entry
, &lock
->wait_list
);
360 task
->pi_blocked_on
= NULL
;
364 * We have to enqueue the top waiter(if it exists) into
365 * task->pi_waiters list.
367 if (rt_mutex_has_waiters(lock
)) {
368 top
= rt_mutex_top_waiter(lock
);
369 top
->pi_list_entry
.prio
= top
->list_entry
.prio
;
370 plist_add(&top
->pi_list_entry
, &task
->pi_waiters
);
372 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
375 /* We got the lock. */
376 debug_rt_mutex_lock(lock
);
378 rt_mutex_set_owner(lock
, task
);
380 rt_mutex_deadlock_account_lock(lock
, task
);
386 * Task blocks on lock.
388 * Prepare waiter and propagate pi chain
390 * This must be called with lock->wait_lock held.
392 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
393 struct rt_mutex_waiter
*waiter
,
394 struct task_struct
*task
,
397 struct task_struct
*owner
= rt_mutex_owner(lock
);
398 struct rt_mutex_waiter
*top_waiter
= waiter
;
400 int chain_walk
= 0, res
;
402 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
403 __rt_mutex_adjust_prio(task
);
406 plist_node_init(&waiter
->list_entry
, task
->prio
);
407 plist_node_init(&waiter
->pi_list_entry
, task
->prio
);
409 /* Get the top priority waiter on the lock */
410 if (rt_mutex_has_waiters(lock
))
411 top_waiter
= rt_mutex_top_waiter(lock
);
412 plist_add(&waiter
->list_entry
, &lock
->wait_list
);
414 task
->pi_blocked_on
= waiter
;
416 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
421 if (waiter
== rt_mutex_top_waiter(lock
)) {
422 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
423 plist_del(&top_waiter
->pi_list_entry
, &owner
->pi_waiters
);
424 plist_add(&waiter
->pi_list_entry
, &owner
->pi_waiters
);
426 __rt_mutex_adjust_prio(owner
);
427 if (owner
->pi_blocked_on
)
429 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
431 else if (debug_rt_mutex_detect_deadlock(waiter
, detect_deadlock
))
438 * The owner can't disappear while holding a lock,
439 * so the owner struct is protected by wait_lock.
440 * Gets dropped in rt_mutex_adjust_prio_chain()!
442 get_task_struct(owner
);
444 raw_spin_unlock(&lock
->wait_lock
);
446 res
= rt_mutex_adjust_prio_chain(owner
, detect_deadlock
, lock
, waiter
,
449 raw_spin_lock(&lock
->wait_lock
);
455 * Wake up the next waiter on the lock.
457 * Remove the top waiter from the current tasks waiter list and wake it up.
459 * Called with lock->wait_lock held.
461 static void wakeup_next_waiter(struct rt_mutex
*lock
)
463 struct rt_mutex_waiter
*waiter
;
466 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
468 waiter
= rt_mutex_top_waiter(lock
);
471 * Remove it from current->pi_waiters. We do not adjust a
472 * possible priority boost right now. We execute wakeup in the
473 * boosted mode and go back to normal after releasing
476 plist_del(&waiter
->pi_list_entry
, ¤t
->pi_waiters
);
478 rt_mutex_set_owner(lock
, NULL
);
480 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
482 wake_up_process(waiter
->task
);
486 * Remove a waiter from a lock and give up
488 * Must be called with lock->wait_lock held and
489 * have just failed to try_to_take_rt_mutex().
491 static void remove_waiter(struct rt_mutex
*lock
,
492 struct rt_mutex_waiter
*waiter
)
494 int first
= (waiter
== rt_mutex_top_waiter(lock
));
495 struct task_struct
*owner
= rt_mutex_owner(lock
);
499 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
500 plist_del(&waiter
->list_entry
, &lock
->wait_list
);
501 current
->pi_blocked_on
= NULL
;
502 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
509 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
511 plist_del(&waiter
->pi_list_entry
, &owner
->pi_waiters
);
513 if (rt_mutex_has_waiters(lock
)) {
514 struct rt_mutex_waiter
*next
;
516 next
= rt_mutex_top_waiter(lock
);
517 plist_add(&next
->pi_list_entry
, &owner
->pi_waiters
);
519 __rt_mutex_adjust_prio(owner
);
521 if (owner
->pi_blocked_on
)
524 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
527 WARN_ON(!plist_node_empty(&waiter
->pi_list_entry
));
532 /* gets dropped in rt_mutex_adjust_prio_chain()! */
533 get_task_struct(owner
);
535 raw_spin_unlock(&lock
->wait_lock
);
537 rt_mutex_adjust_prio_chain(owner
, 0, lock
, NULL
, current
);
539 raw_spin_lock(&lock
->wait_lock
);
543 * Recheck the pi chain, in case we got a priority setting
545 * Called from sched_setscheduler
547 void rt_mutex_adjust_pi(struct task_struct
*task
)
549 struct rt_mutex_waiter
*waiter
;
552 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
554 waiter
= task
->pi_blocked_on
;
555 if (!waiter
|| waiter
->list_entry
.prio
== task
->prio
) {
556 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
560 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
562 /* gets dropped in rt_mutex_adjust_prio_chain()! */
563 get_task_struct(task
);
564 rt_mutex_adjust_prio_chain(task
, 0, NULL
, NULL
, task
);
568 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
569 * @lock: the rt_mutex to take
570 * @state: the state the task should block in (TASK_INTERRUPTIBLE
571 * or TASK_UNINTERRUPTIBLE)
572 * @timeout: the pre-initialized and started timer, or NULL for none
573 * @waiter: the pre-initialized rt_mutex_waiter
575 * lock->wait_lock must be held by the caller.
578 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
579 struct hrtimer_sleeper
*timeout
,
580 struct rt_mutex_waiter
*waiter
)
585 /* Try to acquire the lock: */
586 if (try_to_take_rt_mutex(lock
, current
, waiter
))
590 * TASK_INTERRUPTIBLE checks for signals and
591 * timeout. Ignored otherwise.
593 if (unlikely(state
== TASK_INTERRUPTIBLE
)) {
594 /* Signal pending? */
595 if (signal_pending(current
))
597 if (timeout
&& !timeout
->task
)
603 raw_spin_unlock(&lock
->wait_lock
);
605 debug_rt_mutex_print_deadlock(waiter
);
607 schedule_rt_mutex(lock
);
609 raw_spin_lock(&lock
->wait_lock
);
610 set_current_state(state
);
617 * Slow path lock function:
620 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
621 struct hrtimer_sleeper
*timeout
,
624 struct rt_mutex_waiter waiter
;
627 debug_rt_mutex_init_waiter(&waiter
);
629 raw_spin_lock(&lock
->wait_lock
);
631 /* Try to acquire the lock again: */
632 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
633 raw_spin_unlock(&lock
->wait_lock
);
637 set_current_state(state
);
639 /* Setup the timer, when timeout != NULL */
640 if (unlikely(timeout
)) {
641 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
642 if (!hrtimer_active(&timeout
->timer
))
643 timeout
->task
= NULL
;
646 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, detect_deadlock
);
649 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
651 set_current_state(TASK_RUNNING
);
654 remove_waiter(lock
, &waiter
);
657 * try_to_take_rt_mutex() sets the waiter bit
658 * unconditionally. We might have to fix that up.
660 fixup_rt_mutex_waiters(lock
);
662 raw_spin_unlock(&lock
->wait_lock
);
664 /* Remove pending timer: */
665 if (unlikely(timeout
))
666 hrtimer_cancel(&timeout
->timer
);
668 debug_rt_mutex_free_waiter(&waiter
);
674 * Slow path try-lock function:
677 rt_mutex_slowtrylock(struct rt_mutex
*lock
)
681 raw_spin_lock(&lock
->wait_lock
);
683 if (likely(rt_mutex_owner(lock
) != current
)) {
685 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
687 * try_to_take_rt_mutex() sets the lock waiters
688 * bit unconditionally. Clean this up.
690 fixup_rt_mutex_waiters(lock
);
693 raw_spin_unlock(&lock
->wait_lock
);
699 * Slow path to release a rt-mutex:
702 rt_mutex_slowunlock(struct rt_mutex
*lock
)
704 raw_spin_lock(&lock
->wait_lock
);
706 debug_rt_mutex_unlock(lock
);
708 rt_mutex_deadlock_account_unlock(current
);
710 if (!rt_mutex_has_waiters(lock
)) {
712 raw_spin_unlock(&lock
->wait_lock
);
716 wakeup_next_waiter(lock
);
718 raw_spin_unlock(&lock
->wait_lock
);
720 /* Undo pi boosting if necessary: */
721 rt_mutex_adjust_prio(current
);
725 * debug aware fast / slowpath lock,trylock,unlock
727 * The atomic acquire/release ops are compiled away, when either the
728 * architecture does not support cmpxchg or when debugging is enabled.
731 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
733 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
734 struct hrtimer_sleeper
*timeout
,
735 int detect_deadlock
))
737 if (!detect_deadlock
&& likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
738 rt_mutex_deadlock_account_lock(lock
, current
);
741 return slowfn(lock
, state
, NULL
, detect_deadlock
);
745 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
746 struct hrtimer_sleeper
*timeout
, int detect_deadlock
,
747 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
748 struct hrtimer_sleeper
*timeout
,
749 int detect_deadlock
))
751 if (!detect_deadlock
&& likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
752 rt_mutex_deadlock_account_lock(lock
, current
);
755 return slowfn(lock
, state
, timeout
, detect_deadlock
);
759 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
760 int (*slowfn
)(struct rt_mutex
*lock
))
762 if (likely(rt_mutex_cmpxchg(lock
, NULL
, current
))) {
763 rt_mutex_deadlock_account_lock(lock
, current
);
770 rt_mutex_fastunlock(struct rt_mutex
*lock
,
771 void (*slowfn
)(struct rt_mutex
*lock
))
773 if (likely(rt_mutex_cmpxchg(lock
, current
, NULL
)))
774 rt_mutex_deadlock_account_unlock(current
);
780 * rt_mutex_lock - lock a rt_mutex
782 * @lock: the rt_mutex to be locked
784 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
788 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, 0, rt_mutex_slowlock
);
790 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
793 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
795 * @lock: the rt_mutex to be locked
796 * @detect_deadlock: deadlock detection on/off
800 * -EINTR when interrupted by a signal
801 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
803 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
,
808 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
,
809 detect_deadlock
, rt_mutex_slowlock
);
811 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible
);
814 * rt_mutex_timed_lock - lock a rt_mutex interruptible
815 * the timeout structure is provided
818 * @lock: the rt_mutex to be locked
819 * @timeout: timeout structure or NULL (no timeout)
820 * @detect_deadlock: deadlock detection on/off
824 * -EINTR when interrupted by a signal
825 * -ETIMEDOUT when the timeout expired
826 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
829 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
,
834 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
835 detect_deadlock
, rt_mutex_slowlock
);
837 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
840 * rt_mutex_trylock - try to lock a rt_mutex
842 * @lock: the rt_mutex to be locked
844 * Returns 1 on success and 0 on contention
846 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
848 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
850 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
853 * rt_mutex_unlock - unlock a rt_mutex
855 * @lock: the rt_mutex to be unlocked
857 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
859 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
861 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
864 * rt_mutex_destroy - mark a mutex unusable
865 * @lock: the mutex to be destroyed
867 * This function marks the mutex uninitialized, and any subsequent
868 * use of the mutex is forbidden. The mutex must not be locked when
869 * this function is called.
871 void rt_mutex_destroy(struct rt_mutex
*lock
)
873 WARN_ON(rt_mutex_is_locked(lock
));
874 #ifdef CONFIG_DEBUG_RT_MUTEXES
879 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
882 * __rt_mutex_init - initialize the rt lock
884 * @lock: the rt lock to be initialized
886 * Initialize the rt lock to unlocked state.
888 * Initializing of a locked rt lock is not allowed
890 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
893 raw_spin_lock_init(&lock
->wait_lock
);
894 plist_head_init(&lock
->wait_list
);
896 debug_rt_mutex_init(lock
, name
);
898 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
901 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
904 * @lock: the rt_mutex to be locked
905 * @proxy_owner:the task to set as owner
907 * No locking. Caller has to do serializing itself
908 * Special API call for PI-futex support
910 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
911 struct task_struct
*proxy_owner
)
913 __rt_mutex_init(lock
, NULL
);
914 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
915 rt_mutex_set_owner(lock
, proxy_owner
);
916 rt_mutex_deadlock_account_lock(lock
, proxy_owner
);
920 * rt_mutex_proxy_unlock - release a lock on behalf of owner
922 * @lock: the rt_mutex to be locked
924 * No locking. Caller has to do serializing itself
925 * Special API call for PI-futex support
927 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
928 struct task_struct
*proxy_owner
)
930 debug_rt_mutex_proxy_unlock(lock
);
931 rt_mutex_set_owner(lock
, NULL
);
932 rt_mutex_deadlock_account_unlock(proxy_owner
);
936 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
937 * @lock: the rt_mutex to take
938 * @waiter: the pre-initialized rt_mutex_waiter
939 * @task: the task to prepare
940 * @detect_deadlock: perform deadlock detection (1) or not (0)
943 * 0 - task blocked on lock
944 * 1 - acquired the lock for task, caller should wake it up
947 * Special API call for FUTEX_REQUEUE_PI support.
949 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
950 struct rt_mutex_waiter
*waiter
,
951 struct task_struct
*task
, int detect_deadlock
)
955 raw_spin_lock(&lock
->wait_lock
);
957 if (try_to_take_rt_mutex(lock
, task
, NULL
)) {
958 raw_spin_unlock(&lock
->wait_lock
);
962 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
, detect_deadlock
);
964 if (ret
&& !rt_mutex_owner(lock
)) {
966 * Reset the return value. We might have
967 * returned with -EDEADLK and the owner
968 * released the lock while we were walking the
969 * pi chain. Let the waiter sort it out.
975 remove_waiter(lock
, waiter
);
977 raw_spin_unlock(&lock
->wait_lock
);
979 debug_rt_mutex_print_deadlock(waiter
);
985 * rt_mutex_next_owner - return the next owner of the lock
987 * @lock: the rt lock query
989 * Returns the next owner of the lock or NULL
991 * Caller has to serialize against other accessors to the lock
994 * Special API call for PI-futex support
996 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
998 if (!rt_mutex_has_waiters(lock
))
1001 return rt_mutex_top_waiter(lock
)->task
;
1005 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1006 * @lock: the rt_mutex we were woken on
1007 * @to: the timeout, null if none. hrtimer should already have
1009 * @waiter: the pre-initialized rt_mutex_waiter
1010 * @detect_deadlock: perform deadlock detection (1) or not (0)
1012 * Complete the lock acquisition started our behalf by another thread.
1016 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1018 * Special API call for PI-futex requeue support
1020 int rt_mutex_finish_proxy_lock(struct rt_mutex
*lock
,
1021 struct hrtimer_sleeper
*to
,
1022 struct rt_mutex_waiter
*waiter
,
1023 int detect_deadlock
)
1027 raw_spin_lock(&lock
->wait_lock
);
1029 set_current_state(TASK_INTERRUPTIBLE
);
1031 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1033 set_current_state(TASK_RUNNING
);
1036 remove_waiter(lock
, waiter
);
1039 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1040 * have to fix that up.
1042 fixup_rt_mutex_waiters(lock
);
1044 raw_spin_unlock(&lock
->wait_lock
);