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/locking/rt-mutex-design.txt for details.
13 #include <linux/spinlock.h>
14 #include <linux/export.h>
15 #include <linux/sched.h>
16 #include <linux/sched/rt.h>
17 #include <linux/sched/deadline.h>
18 #include <linux/timer.h>
20 #include "rtmutex_common.h"
23 * lock->owner state tracking:
25 * lock->owner holds the task_struct pointer of the owner. Bit 0
26 * is used to keep track of the "lock has waiters" state.
29 * NULL 0 lock is free (fast acquire possible)
30 * NULL 1 lock is free and has waiters and the top waiter
31 * is going to take the lock*
32 * taskpointer 0 lock is held (fast release possible)
33 * taskpointer 1 lock is held and has waiters**
35 * The fast atomic compare exchange based acquire and release is only
36 * possible when bit 0 of lock->owner is 0.
38 * (*) It also can be a transitional state when grabbing the lock
39 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
40 * we need to set the bit0 before looking at the lock, and the owner may be
41 * NULL in this small time, hence this can be a transitional state.
43 * (**) There is a small time when bit 0 is set but there are no
44 * waiters. This can happen when grabbing the lock in the slow path.
45 * To prevent a cmpxchg of the owner releasing the lock, we need to
46 * set this bit before looking at the lock.
50 rt_mutex_set_owner(struct rt_mutex
*lock
, struct task_struct
*owner
)
52 unsigned long val
= (unsigned long)owner
;
54 if (rt_mutex_has_waiters(lock
))
55 val
|= RT_MUTEX_HAS_WAITERS
;
57 lock
->owner
= (struct task_struct
*)val
;
60 static inline void clear_rt_mutex_waiters(struct rt_mutex
*lock
)
62 lock
->owner
= (struct task_struct
*)
63 ((unsigned long)lock
->owner
& ~RT_MUTEX_HAS_WAITERS
);
66 static void fixup_rt_mutex_waiters(struct rt_mutex
*lock
)
68 if (!rt_mutex_has_waiters(lock
))
69 clear_rt_mutex_waiters(lock
);
73 * We can speed up the acquire/release, if there's no debugging state to be
76 #ifndef CONFIG_DEBUG_RT_MUTEXES
77 # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
78 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
79 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
82 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
83 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
84 * relaxed semantics suffice.
86 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
88 unsigned long owner
, *p
= (unsigned long *) &lock
->owner
;
92 } while (cmpxchg_relaxed(p
, owner
,
93 owner
| RT_MUTEX_HAS_WAITERS
) != owner
);
97 * Safe fastpath aware unlock:
98 * 1) Clear the waiters bit
99 * 2) Drop lock->wait_lock
100 * 3) Try to unlock the lock with cmpxchg
102 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
)
103 __releases(lock
->wait_lock
)
105 struct task_struct
*owner
= rt_mutex_owner(lock
);
107 clear_rt_mutex_waiters(lock
);
108 raw_spin_unlock(&lock
->wait_lock
);
110 * If a new waiter comes in between the unlock and the cmpxchg
111 * we have two situations:
115 * cmpxchg(p, owner, 0) == owner
116 * mark_rt_mutex_waiters(lock);
122 * mark_rt_mutex_waiters(lock);
124 * cmpxchg(p, owner, 0) != owner
133 return rt_mutex_cmpxchg_release(lock
, owner
, NULL
);
137 # define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
138 # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
139 # define rt_mutex_cmpxchg_release(l,c,n) (0)
141 static inline void mark_rt_mutex_waiters(struct rt_mutex
*lock
)
143 lock
->owner
= (struct task_struct
*)
144 ((unsigned long)lock
->owner
| RT_MUTEX_HAS_WAITERS
);
148 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
150 static inline bool unlock_rt_mutex_safe(struct rt_mutex
*lock
)
151 __releases(lock
->wait_lock
)
154 raw_spin_unlock(&lock
->wait_lock
);
160 rt_mutex_waiter_less(struct rt_mutex_waiter
*left
,
161 struct rt_mutex_waiter
*right
)
163 if (left
->prio
< right
->prio
)
167 * If both waiters have dl_prio(), we check the deadlines of the
169 * If left waiter has a dl_prio(), and we didn't return 1 above,
170 * then right waiter has a dl_prio() too.
172 if (dl_prio(left
->prio
))
173 return dl_time_before(left
->task
->dl
.deadline
,
174 right
->task
->dl
.deadline
);
180 rt_mutex_enqueue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
182 struct rb_node
**link
= &lock
->waiters
.rb_node
;
183 struct rb_node
*parent
= NULL
;
184 struct rt_mutex_waiter
*entry
;
189 entry
= rb_entry(parent
, struct rt_mutex_waiter
, tree_entry
);
190 if (rt_mutex_waiter_less(waiter
, entry
)) {
191 link
= &parent
->rb_left
;
193 link
= &parent
->rb_right
;
199 lock
->waiters_leftmost
= &waiter
->tree_entry
;
201 rb_link_node(&waiter
->tree_entry
, parent
, link
);
202 rb_insert_color(&waiter
->tree_entry
, &lock
->waiters
);
206 rt_mutex_dequeue(struct rt_mutex
*lock
, struct rt_mutex_waiter
*waiter
)
208 if (RB_EMPTY_NODE(&waiter
->tree_entry
))
211 if (lock
->waiters_leftmost
== &waiter
->tree_entry
)
212 lock
->waiters_leftmost
= rb_next(&waiter
->tree_entry
);
214 rb_erase(&waiter
->tree_entry
, &lock
->waiters
);
215 RB_CLEAR_NODE(&waiter
->tree_entry
);
219 rt_mutex_enqueue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
221 struct rb_node
**link
= &task
->pi_waiters
.rb_node
;
222 struct rb_node
*parent
= NULL
;
223 struct rt_mutex_waiter
*entry
;
228 entry
= rb_entry(parent
, struct rt_mutex_waiter
, pi_tree_entry
);
229 if (rt_mutex_waiter_less(waiter
, entry
)) {
230 link
= &parent
->rb_left
;
232 link
= &parent
->rb_right
;
238 task
->pi_waiters_leftmost
= &waiter
->pi_tree_entry
;
240 rb_link_node(&waiter
->pi_tree_entry
, parent
, link
);
241 rb_insert_color(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
245 rt_mutex_dequeue_pi(struct task_struct
*task
, struct rt_mutex_waiter
*waiter
)
247 if (RB_EMPTY_NODE(&waiter
->pi_tree_entry
))
250 if (task
->pi_waiters_leftmost
== &waiter
->pi_tree_entry
)
251 task
->pi_waiters_leftmost
= rb_next(&waiter
->pi_tree_entry
);
253 rb_erase(&waiter
->pi_tree_entry
, &task
->pi_waiters
);
254 RB_CLEAR_NODE(&waiter
->pi_tree_entry
);
258 * Calculate task priority from the waiter tree priority
260 * Return task->normal_prio when the waiter tree is empty or when
261 * the waiter is not allowed to do priority boosting
263 int rt_mutex_getprio(struct task_struct
*task
)
265 if (likely(!task_has_pi_waiters(task
)))
266 return task
->normal_prio
;
268 return min(task_top_pi_waiter(task
)->prio
,
272 struct task_struct
*rt_mutex_get_top_task(struct task_struct
*task
)
274 if (likely(!task_has_pi_waiters(task
)))
277 return task_top_pi_waiter(task
)->task
;
281 * Called by sched_setscheduler() to get the priority which will be
282 * effective after the change.
284 int rt_mutex_get_effective_prio(struct task_struct
*task
, int newprio
)
286 if (!task_has_pi_waiters(task
))
289 if (task_top_pi_waiter(task
)->task
->prio
<= newprio
)
290 return task_top_pi_waiter(task
)->task
->prio
;
295 * Adjust the priority of a task, after its pi_waiters got modified.
297 * This can be both boosting and unboosting. task->pi_lock must be held.
299 static void __rt_mutex_adjust_prio(struct task_struct
*task
)
301 int prio
= rt_mutex_getprio(task
);
303 if (task
->prio
!= prio
|| dl_prio(prio
))
304 rt_mutex_setprio(task
, prio
);
308 * Adjust task priority (undo boosting). Called from the exit path of
309 * rt_mutex_slowunlock() and rt_mutex_slowlock().
311 * (Note: We do this outside of the protection of lock->wait_lock to
312 * allow the lock to be taken while or before we readjust the priority
313 * of task. We do not use the spin_xx_mutex() variants here as we are
314 * outside of the debug path.)
316 void rt_mutex_adjust_prio(struct task_struct
*task
)
320 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
321 __rt_mutex_adjust_prio(task
);
322 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
326 * Deadlock detection is conditional:
328 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
329 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
331 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
332 * conducted independent of the detect argument.
334 * If the waiter argument is NULL this indicates the deboost path and
335 * deadlock detection is disabled independent of the detect argument
336 * and the config settings.
338 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter
*waiter
,
339 enum rtmutex_chainwalk chwalk
)
342 * This is just a wrapper function for the following call,
343 * because debug_rt_mutex_detect_deadlock() smells like a magic
344 * debug feature and I wanted to keep the cond function in the
345 * main source file along with the comments instead of having
346 * two of the same in the headers.
348 return debug_rt_mutex_detect_deadlock(waiter
, chwalk
);
352 * Max number of times we'll walk the boosting chain:
354 int max_lock_depth
= 1024;
356 static inline struct rt_mutex
*task_blocked_on_lock(struct task_struct
*p
)
358 return p
->pi_blocked_on
? p
->pi_blocked_on
->lock
: NULL
;
362 * Adjust the priority chain. Also used for deadlock detection.
363 * Decreases task's usage by one - may thus free the task.
365 * @task: the task owning the mutex (owner) for which a chain walk is
367 * @chwalk: do we have to carry out deadlock detection?
368 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
369 * things for a task that has just got its priority adjusted, and
370 * is waiting on a mutex)
371 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
372 * we dropped its pi_lock. Is never dereferenced, only used for
373 * comparison to detect lock chain changes.
374 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
375 * its priority to the mutex owner (can be NULL in the case
376 * depicted above or if the top waiter is gone away and we are
377 * actually deboosting the owner)
378 * @top_task: the current top waiter
380 * Returns 0 or -EDEADLK.
382 * Chain walk basics and protection scope
384 * [R] refcount on task
385 * [P] task->pi_lock held
386 * [L] rtmutex->wait_lock held
388 * Step Description Protected by
389 * function arguments:
391 * @orig_lock if != NULL @top_task is blocked on it
392 * @next_lock Unprotected. Cannot be
393 * dereferenced. Only used for
395 * @orig_waiter if != NULL @top_task is blocked on it
396 * @top_task current, or in case of proxy
397 * locking protected by calling
400 * loop_sanity_check();
402 * [1] lock(task->pi_lock); [R] acquire [P]
403 * [2] waiter = task->pi_blocked_on; [P]
404 * [3] check_exit_conditions_1(); [P]
405 * [4] lock = waiter->lock; [P]
406 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
407 * unlock(task->pi_lock); release [P]
410 * [6] check_exit_conditions_2(); [P] + [L]
411 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
412 * [8] unlock(task->pi_lock); release [P]
413 * put_task_struct(task); release [R]
414 * [9] check_exit_conditions_3(); [L]
415 * [10] task = owner(lock); [L]
416 * get_task_struct(task); [L] acquire [R]
417 * lock(task->pi_lock); [L] acquire [P]
418 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
419 * [12] check_exit_conditions_4(); [P] + [L]
420 * [13] unlock(task->pi_lock); release [P]
421 * unlock(lock->wait_lock); release [L]
424 static int rt_mutex_adjust_prio_chain(struct task_struct
*task
,
425 enum rtmutex_chainwalk chwalk
,
426 struct rt_mutex
*orig_lock
,
427 struct rt_mutex
*next_lock
,
428 struct rt_mutex_waiter
*orig_waiter
,
429 struct task_struct
*top_task
)
431 struct rt_mutex_waiter
*waiter
, *top_waiter
= orig_waiter
;
432 struct rt_mutex_waiter
*prerequeue_top_waiter
;
433 int ret
= 0, depth
= 0;
434 struct rt_mutex
*lock
;
435 bool detect_deadlock
;
439 detect_deadlock
= rt_mutex_cond_detect_deadlock(orig_waiter
, chwalk
);
442 * The (de)boosting is a step by step approach with a lot of
443 * pitfalls. We want this to be preemptible and we want hold a
444 * maximum of two locks per step. So we have to check
445 * carefully whether things change under us.
449 * We limit the lock chain length for each invocation.
451 if (++depth
> max_lock_depth
) {
455 * Print this only once. If the admin changes the limit,
456 * print a new message when reaching the limit again.
458 if (prev_max
!= max_lock_depth
) {
459 prev_max
= max_lock_depth
;
460 printk(KERN_WARNING
"Maximum lock depth %d reached "
461 "task: %s (%d)\n", max_lock_depth
,
462 top_task
->comm
, task_pid_nr(top_task
));
464 put_task_struct(task
);
470 * We are fully preemptible here and only hold the refcount on
471 * @task. So everything can have changed under us since the
472 * caller or our own code below (goto retry/again) dropped all
477 * [1] Task cannot go away as we did a get_task() before !
479 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
482 * [2] Get the waiter on which @task is blocked on.
484 waiter
= task
->pi_blocked_on
;
487 * [3] check_exit_conditions_1() protected by task->pi_lock.
491 * Check whether the end of the boosting chain has been
492 * reached or the state of the chain has changed while we
499 * Check the orig_waiter state. After we dropped the locks,
500 * the previous owner of the lock might have released the lock.
502 if (orig_waiter
&& !rt_mutex_owner(orig_lock
))
506 * We dropped all locks after taking a refcount on @task, so
507 * the task might have moved on in the lock chain or even left
508 * the chain completely and blocks now on an unrelated lock or
511 * We stored the lock on which @task was blocked in @next_lock,
512 * so we can detect the chain change.
514 if (next_lock
!= waiter
->lock
)
518 * Drop out, when the task has no waiters. Note,
519 * top_waiter can be NULL, when we are in the deboosting
523 if (!task_has_pi_waiters(task
))
526 * If deadlock detection is off, we stop here if we
527 * are not the top pi waiter of the task. If deadlock
528 * detection is enabled we continue, but stop the
529 * requeueing in the chain walk.
531 if (top_waiter
!= task_top_pi_waiter(task
)) {
532 if (!detect_deadlock
)
540 * If the waiter priority is the same as the task priority
541 * then there is no further priority adjustment necessary. If
542 * deadlock detection is off, we stop the chain walk. If its
543 * enabled we continue, but stop the requeueing in the chain
546 if (waiter
->prio
== task
->prio
) {
547 if (!detect_deadlock
)
554 * [4] Get the next lock
558 * [5] We need to trylock here as we are holding task->pi_lock,
559 * which is the reverse lock order versus the other rtmutex
562 if (!raw_spin_trylock(&lock
->wait_lock
)) {
563 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
569 * [6] check_exit_conditions_2() protected by task->pi_lock and
572 * Deadlock detection. If the lock is the same as the original
573 * lock which caused us to walk the lock chain or if the
574 * current lock is owned by the task which initiated the chain
575 * walk, we detected a deadlock.
577 if (lock
== orig_lock
|| rt_mutex_owner(lock
) == top_task
) {
578 debug_rt_mutex_deadlock(chwalk
, orig_waiter
, lock
);
579 raw_spin_unlock(&lock
->wait_lock
);
585 * If we just follow the lock chain for deadlock detection, no
586 * need to do all the requeue operations. To avoid a truckload
587 * of conditionals around the various places below, just do the
588 * minimum chain walk checks.
592 * No requeue[7] here. Just release @task [8]
594 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
595 put_task_struct(task
);
598 * [9] check_exit_conditions_3 protected by lock->wait_lock.
599 * If there is no owner of the lock, end of chain.
601 if (!rt_mutex_owner(lock
)) {
602 raw_spin_unlock(&lock
->wait_lock
);
606 /* [10] Grab the next task, i.e. owner of @lock */
607 task
= rt_mutex_owner(lock
);
608 get_task_struct(task
);
609 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
612 * No requeue [11] here. We just do deadlock detection.
614 * [12] Store whether owner is blocked
615 * itself. Decision is made after dropping the locks
617 next_lock
= task_blocked_on_lock(task
);
619 * Get the top waiter for the next iteration
621 top_waiter
= rt_mutex_top_waiter(lock
);
623 /* [13] Drop locks */
624 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
625 raw_spin_unlock(&lock
->wait_lock
);
627 /* If owner is not blocked, end of chain. */
634 * Store the current top waiter before doing the requeue
635 * operation on @lock. We need it for the boost/deboost
638 prerequeue_top_waiter
= rt_mutex_top_waiter(lock
);
640 /* [7] Requeue the waiter in the lock waiter tree. */
641 rt_mutex_dequeue(lock
, waiter
);
642 waiter
->prio
= task
->prio
;
643 rt_mutex_enqueue(lock
, waiter
);
645 /* [8] Release the task */
646 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
647 put_task_struct(task
);
650 * [9] check_exit_conditions_3 protected by lock->wait_lock.
652 * We must abort the chain walk if there is no lock owner even
653 * in the dead lock detection case, as we have nothing to
654 * follow here. This is the end of the chain we are walking.
656 if (!rt_mutex_owner(lock
)) {
658 * If the requeue [7] above changed the top waiter,
659 * then we need to wake the new top waiter up to try
662 if (prerequeue_top_waiter
!= rt_mutex_top_waiter(lock
))
663 wake_up_process(rt_mutex_top_waiter(lock
)->task
);
664 raw_spin_unlock(&lock
->wait_lock
);
668 /* [10] Grab the next task, i.e. the owner of @lock */
669 task
= rt_mutex_owner(lock
);
670 get_task_struct(task
);
671 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
673 /* [11] requeue the pi waiters if necessary */
674 if (waiter
== rt_mutex_top_waiter(lock
)) {
676 * The waiter became the new top (highest priority)
677 * waiter on the lock. Replace the previous top waiter
678 * in the owner tasks pi waiters tree with this waiter
679 * and adjust the priority of the owner.
681 rt_mutex_dequeue_pi(task
, prerequeue_top_waiter
);
682 rt_mutex_enqueue_pi(task
, waiter
);
683 __rt_mutex_adjust_prio(task
);
685 } else if (prerequeue_top_waiter
== waiter
) {
687 * The waiter was the top waiter on the lock, but is
688 * no longer the top prority waiter. Replace waiter in
689 * the owner tasks pi waiters tree with the new top
690 * (highest priority) waiter and adjust the priority
692 * The new top waiter is stored in @waiter so that
693 * @waiter == @top_waiter evaluates to true below and
694 * we continue to deboost the rest of the chain.
696 rt_mutex_dequeue_pi(task
, waiter
);
697 waiter
= rt_mutex_top_waiter(lock
);
698 rt_mutex_enqueue_pi(task
, waiter
);
699 __rt_mutex_adjust_prio(task
);
702 * Nothing changed. No need to do any priority
708 * [12] check_exit_conditions_4() protected by task->pi_lock
709 * and lock->wait_lock. The actual decisions are made after we
712 * Check whether the task which owns the current lock is pi
713 * blocked itself. If yes we store a pointer to the lock for
714 * the lock chain change detection above. After we dropped
715 * task->pi_lock next_lock cannot be dereferenced anymore.
717 next_lock
= task_blocked_on_lock(task
);
719 * Store the top waiter of @lock for the end of chain walk
722 top_waiter
= rt_mutex_top_waiter(lock
);
724 /* [13] Drop the locks */
725 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
726 raw_spin_unlock(&lock
->wait_lock
);
729 * Make the actual exit decisions [12], based on the stored
732 * We reached the end of the lock chain. Stop right here. No
733 * point to go back just to figure that out.
739 * If the current waiter is not the top waiter on the lock,
740 * then we can stop the chain walk here if we are not in full
741 * deadlock detection mode.
743 if (!detect_deadlock
&& waiter
!= top_waiter
)
749 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
751 put_task_struct(task
);
757 * Try to take an rt-mutex
759 * Must be called with lock->wait_lock held.
761 * @lock: The lock to be acquired.
762 * @task: The task which wants to acquire the lock
763 * @waiter: The waiter that is queued to the lock's wait tree if the
764 * callsite called task_blocked_on_lock(), otherwise NULL
766 static int try_to_take_rt_mutex(struct rt_mutex
*lock
, struct task_struct
*task
,
767 struct rt_mutex_waiter
*waiter
)
772 * Before testing whether we can acquire @lock, we set the
773 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
774 * other tasks which try to modify @lock into the slow path
775 * and they serialize on @lock->wait_lock.
777 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
778 * as explained at the top of this file if and only if:
780 * - There is a lock owner. The caller must fixup the
781 * transient state if it does a trylock or leaves the lock
782 * function due to a signal or timeout.
784 * - @task acquires the lock and there are no other
785 * waiters. This is undone in rt_mutex_set_owner(@task) at
786 * the end of this function.
788 mark_rt_mutex_waiters(lock
);
791 * If @lock has an owner, give up.
793 if (rt_mutex_owner(lock
))
797 * If @waiter != NULL, @task has already enqueued the waiter
798 * into @lock waiter tree. If @waiter == NULL then this is a
803 * If waiter is not the highest priority waiter of
806 if (waiter
!= rt_mutex_top_waiter(lock
))
810 * We can acquire the lock. Remove the waiter from the
813 rt_mutex_dequeue(lock
, waiter
);
817 * If the lock has waiters already we check whether @task is
818 * eligible to take over the lock.
820 * If there are no other waiters, @task can acquire
821 * the lock. @task->pi_blocked_on is NULL, so it does
822 * not need to be dequeued.
824 if (rt_mutex_has_waiters(lock
)) {
826 * If @task->prio is greater than or equal to
827 * the top waiter priority (kernel view),
830 if (task
->prio
>= rt_mutex_top_waiter(lock
)->prio
)
834 * The current top waiter stays enqueued. We
835 * don't have to change anything in the lock
840 * No waiters. Take the lock without the
841 * pi_lock dance.@task->pi_blocked_on is NULL
842 * and we have no waiters to enqueue in @task
850 * Clear @task->pi_blocked_on. Requires protection by
851 * @task->pi_lock. Redundant operation for the @waiter == NULL
852 * case, but conditionals are more expensive than a redundant
855 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
856 task
->pi_blocked_on
= NULL
;
858 * Finish the lock acquisition. @task is the new owner. If
859 * other waiters exist we have to insert the highest priority
860 * waiter into @task->pi_waiters tree.
862 if (rt_mutex_has_waiters(lock
))
863 rt_mutex_enqueue_pi(task
, rt_mutex_top_waiter(lock
));
864 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
867 /* We got the lock. */
868 debug_rt_mutex_lock(lock
);
871 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
872 * are still waiters or clears it.
874 rt_mutex_set_owner(lock
, task
);
876 rt_mutex_deadlock_account_lock(lock
, task
);
882 * Task blocks on lock.
884 * Prepare waiter and propagate pi chain
886 * This must be called with lock->wait_lock held.
888 static int task_blocks_on_rt_mutex(struct rt_mutex
*lock
,
889 struct rt_mutex_waiter
*waiter
,
890 struct task_struct
*task
,
891 enum rtmutex_chainwalk chwalk
)
893 struct task_struct
*owner
= rt_mutex_owner(lock
);
894 struct rt_mutex_waiter
*top_waiter
= waiter
;
895 struct rt_mutex
*next_lock
;
896 int chain_walk
= 0, res
;
900 * Early deadlock detection. We really don't want the task to
901 * enqueue on itself just to untangle the mess later. It's not
902 * only an optimization. We drop the locks, so another waiter
903 * can come in before the chain walk detects the deadlock. So
904 * the other will detect the deadlock and return -EDEADLOCK,
905 * which is wrong, as the other waiter is not in a deadlock
911 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
912 __rt_mutex_adjust_prio(task
);
915 waiter
->prio
= task
->prio
;
917 /* Get the top priority waiter on the lock */
918 if (rt_mutex_has_waiters(lock
))
919 top_waiter
= rt_mutex_top_waiter(lock
);
920 rt_mutex_enqueue(lock
, waiter
);
922 task
->pi_blocked_on
= waiter
;
924 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
929 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
930 if (waiter
== rt_mutex_top_waiter(lock
)) {
931 rt_mutex_dequeue_pi(owner
, top_waiter
);
932 rt_mutex_enqueue_pi(owner
, waiter
);
934 __rt_mutex_adjust_prio(owner
);
935 if (owner
->pi_blocked_on
)
937 } else if (rt_mutex_cond_detect_deadlock(waiter
, chwalk
)) {
941 /* Store the lock on which owner is blocked or NULL */
942 next_lock
= task_blocked_on_lock(owner
);
944 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
946 * Even if full deadlock detection is on, if the owner is not
947 * blocked itself, we can avoid finding this out in the chain
950 if (!chain_walk
|| !next_lock
)
954 * The owner can't disappear while holding a lock,
955 * so the owner struct is protected by wait_lock.
956 * Gets dropped in rt_mutex_adjust_prio_chain()!
958 get_task_struct(owner
);
960 raw_spin_unlock(&lock
->wait_lock
);
962 res
= rt_mutex_adjust_prio_chain(owner
, chwalk
, lock
,
963 next_lock
, waiter
, task
);
965 raw_spin_lock(&lock
->wait_lock
);
971 * Remove the top waiter from the current tasks pi waiter tree and
974 * Called with lock->wait_lock held.
976 static void mark_wakeup_next_waiter(struct wake_q_head
*wake_q
,
977 struct rt_mutex
*lock
)
979 struct rt_mutex_waiter
*waiter
;
982 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
984 waiter
= rt_mutex_top_waiter(lock
);
987 * Remove it from current->pi_waiters. We do not adjust a
988 * possible priority boost right now. We execute wakeup in the
989 * boosted mode and go back to normal after releasing
992 rt_mutex_dequeue_pi(current
, waiter
);
995 * As we are waking up the top waiter, and the waiter stays
996 * queued on the lock until it gets the lock, this lock
997 * obviously has waiters. Just set the bit here and this has
998 * the added benefit of forcing all new tasks into the
999 * slow path making sure no task of lower priority than
1000 * the top waiter can steal this lock.
1002 lock
->owner
= (void *) RT_MUTEX_HAS_WAITERS
;
1004 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
1006 wake_q_add(wake_q
, waiter
->task
);
1010 * Remove a waiter from a lock and give up
1012 * Must be called with lock->wait_lock held and
1013 * have just failed to try_to_take_rt_mutex().
1015 static void remove_waiter(struct rt_mutex
*lock
,
1016 struct rt_mutex_waiter
*waiter
)
1018 bool is_top_waiter
= (waiter
== rt_mutex_top_waiter(lock
));
1019 struct task_struct
*owner
= rt_mutex_owner(lock
);
1020 struct rt_mutex
*next_lock
;
1021 unsigned long flags
;
1023 raw_spin_lock_irqsave(¤t
->pi_lock
, flags
);
1024 rt_mutex_dequeue(lock
, waiter
);
1025 current
->pi_blocked_on
= NULL
;
1026 raw_spin_unlock_irqrestore(¤t
->pi_lock
, flags
);
1029 * Only update priority if the waiter was the highest priority
1030 * waiter of the lock and there is an owner to update.
1032 if (!owner
|| !is_top_waiter
)
1035 raw_spin_lock_irqsave(&owner
->pi_lock
, flags
);
1037 rt_mutex_dequeue_pi(owner
, waiter
);
1039 if (rt_mutex_has_waiters(lock
))
1040 rt_mutex_enqueue_pi(owner
, rt_mutex_top_waiter(lock
));
1042 __rt_mutex_adjust_prio(owner
);
1044 /* Store the lock on which owner is blocked or NULL */
1045 next_lock
= task_blocked_on_lock(owner
);
1047 raw_spin_unlock_irqrestore(&owner
->pi_lock
, flags
);
1050 * Don't walk the chain, if the owner task is not blocked
1056 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1057 get_task_struct(owner
);
1059 raw_spin_unlock(&lock
->wait_lock
);
1061 rt_mutex_adjust_prio_chain(owner
, RT_MUTEX_MIN_CHAINWALK
, lock
,
1062 next_lock
, NULL
, current
);
1064 raw_spin_lock(&lock
->wait_lock
);
1068 * Recheck the pi chain, in case we got a priority setting
1070 * Called from sched_setscheduler
1072 void rt_mutex_adjust_pi(struct task_struct
*task
)
1074 struct rt_mutex_waiter
*waiter
;
1075 struct rt_mutex
*next_lock
;
1076 unsigned long flags
;
1078 raw_spin_lock_irqsave(&task
->pi_lock
, flags
);
1080 waiter
= task
->pi_blocked_on
;
1081 if (!waiter
|| (waiter
->prio
== task
->prio
&&
1082 !dl_prio(task
->prio
))) {
1083 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1086 next_lock
= waiter
->lock
;
1087 raw_spin_unlock_irqrestore(&task
->pi_lock
, flags
);
1089 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1090 get_task_struct(task
);
1092 rt_mutex_adjust_prio_chain(task
, RT_MUTEX_MIN_CHAINWALK
, NULL
,
1093 next_lock
, NULL
, task
);
1097 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1098 * @lock: the rt_mutex to take
1099 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1100 * or TASK_UNINTERRUPTIBLE)
1101 * @timeout: the pre-initialized and started timer, or NULL for none
1102 * @waiter: the pre-initialized rt_mutex_waiter
1104 * lock->wait_lock must be held by the caller.
1107 __rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1108 struct hrtimer_sleeper
*timeout
,
1109 struct rt_mutex_waiter
*waiter
)
1114 /* Try to acquire the lock: */
1115 if (try_to_take_rt_mutex(lock
, current
, waiter
))
1119 * TASK_INTERRUPTIBLE checks for signals and
1120 * timeout. Ignored otherwise.
1122 if (unlikely(state
== TASK_INTERRUPTIBLE
)) {
1123 /* Signal pending? */
1124 if (signal_pending(current
))
1126 if (timeout
&& !timeout
->task
)
1132 raw_spin_unlock(&lock
->wait_lock
);
1134 debug_rt_mutex_print_deadlock(waiter
);
1138 raw_spin_lock(&lock
->wait_lock
);
1139 set_current_state(state
);
1142 __set_current_state(TASK_RUNNING
);
1146 static void rt_mutex_handle_deadlock(int res
, int detect_deadlock
,
1147 struct rt_mutex_waiter
*w
)
1150 * If the result is not -EDEADLOCK or the caller requested
1151 * deadlock detection, nothing to do here.
1153 if (res
!= -EDEADLOCK
|| detect_deadlock
)
1157 * Yell lowdly and stop the task right here.
1159 rt_mutex_print_deadlock(w
);
1161 set_current_state(TASK_INTERRUPTIBLE
);
1167 * Slow path lock function:
1170 rt_mutex_slowlock(struct rt_mutex
*lock
, int state
,
1171 struct hrtimer_sleeper
*timeout
,
1172 enum rtmutex_chainwalk chwalk
)
1174 struct rt_mutex_waiter waiter
;
1177 debug_rt_mutex_init_waiter(&waiter
);
1178 RB_CLEAR_NODE(&waiter
.pi_tree_entry
);
1179 RB_CLEAR_NODE(&waiter
.tree_entry
);
1181 raw_spin_lock(&lock
->wait_lock
);
1183 /* Try to acquire the lock again: */
1184 if (try_to_take_rt_mutex(lock
, current
, NULL
)) {
1185 raw_spin_unlock(&lock
->wait_lock
);
1189 set_current_state(state
);
1191 /* Setup the timer, when timeout != NULL */
1192 if (unlikely(timeout
))
1193 hrtimer_start_expires(&timeout
->timer
, HRTIMER_MODE_ABS
);
1195 ret
= task_blocks_on_rt_mutex(lock
, &waiter
, current
, chwalk
);
1198 /* sleep on the mutex */
1199 ret
= __rt_mutex_slowlock(lock
, state
, timeout
, &waiter
);
1201 if (unlikely(ret
)) {
1202 __set_current_state(TASK_RUNNING
);
1203 if (rt_mutex_has_waiters(lock
))
1204 remove_waiter(lock
, &waiter
);
1205 rt_mutex_handle_deadlock(ret
, chwalk
, &waiter
);
1209 * try_to_take_rt_mutex() sets the waiter bit
1210 * unconditionally. We might have to fix that up.
1212 fixup_rt_mutex_waiters(lock
);
1214 raw_spin_unlock(&lock
->wait_lock
);
1216 /* Remove pending timer: */
1217 if (unlikely(timeout
))
1218 hrtimer_cancel(&timeout
->timer
);
1220 debug_rt_mutex_free_waiter(&waiter
);
1226 * Slow path try-lock function:
1228 static inline int rt_mutex_slowtrylock(struct rt_mutex
*lock
)
1233 * If the lock already has an owner we fail to get the lock.
1234 * This can be done without taking the @lock->wait_lock as
1235 * it is only being read, and this is a trylock anyway.
1237 if (rt_mutex_owner(lock
))
1241 * The mutex has currently no owner. Lock the wait lock and
1242 * try to acquire the lock.
1244 raw_spin_lock(&lock
->wait_lock
);
1246 ret
= try_to_take_rt_mutex(lock
, current
, NULL
);
1249 * try_to_take_rt_mutex() sets the lock waiters bit
1250 * unconditionally. Clean this up.
1252 fixup_rt_mutex_waiters(lock
);
1254 raw_spin_unlock(&lock
->wait_lock
);
1260 * Slow path to release a rt-mutex.
1261 * Return whether the current task needs to undo a potential priority boosting.
1263 static bool __sched
rt_mutex_slowunlock(struct rt_mutex
*lock
,
1264 struct wake_q_head
*wake_q
)
1266 raw_spin_lock(&lock
->wait_lock
);
1268 debug_rt_mutex_unlock(lock
);
1270 rt_mutex_deadlock_account_unlock(current
);
1273 * We must be careful here if the fast path is enabled. If we
1274 * have no waiters queued we cannot set owner to NULL here
1277 * foo->lock->owner = NULL;
1278 * rtmutex_lock(foo->lock); <- fast path
1279 * free = atomic_dec_and_test(foo->refcnt);
1280 * rtmutex_unlock(foo->lock); <- fast path
1283 * raw_spin_unlock(foo->lock->wait_lock);
1285 * So for the fastpath enabled kernel:
1287 * Nothing can set the waiters bit as long as we hold
1288 * lock->wait_lock. So we do the following sequence:
1290 * owner = rt_mutex_owner(lock);
1291 * clear_rt_mutex_waiters(lock);
1292 * raw_spin_unlock(&lock->wait_lock);
1293 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1297 * The fastpath disabled variant is simple as all access to
1298 * lock->owner is serialized by lock->wait_lock:
1300 * lock->owner = NULL;
1301 * raw_spin_unlock(&lock->wait_lock);
1303 while (!rt_mutex_has_waiters(lock
)) {
1304 /* Drops lock->wait_lock ! */
1305 if (unlock_rt_mutex_safe(lock
) == true)
1307 /* Relock the rtmutex and try again */
1308 raw_spin_lock(&lock
->wait_lock
);
1312 * The wakeup next waiter path does not suffer from the above
1313 * race. See the comments there.
1315 * Queue the next waiter for wakeup once we release the wait_lock.
1317 mark_wakeup_next_waiter(wake_q
, lock
);
1319 raw_spin_unlock(&lock
->wait_lock
);
1321 /* check PI boosting */
1326 * debug aware fast / slowpath lock,trylock,unlock
1328 * The atomic acquire/release ops are compiled away, when either the
1329 * architecture does not support cmpxchg or when debugging is enabled.
1332 rt_mutex_fastlock(struct rt_mutex
*lock
, int state
,
1333 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1334 struct hrtimer_sleeper
*timeout
,
1335 enum rtmutex_chainwalk chwalk
))
1337 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1338 rt_mutex_deadlock_account_lock(lock
, current
);
1341 return slowfn(lock
, state
, NULL
, RT_MUTEX_MIN_CHAINWALK
);
1345 rt_mutex_timed_fastlock(struct rt_mutex
*lock
, int state
,
1346 struct hrtimer_sleeper
*timeout
,
1347 enum rtmutex_chainwalk chwalk
,
1348 int (*slowfn
)(struct rt_mutex
*lock
, int state
,
1349 struct hrtimer_sleeper
*timeout
,
1350 enum rtmutex_chainwalk chwalk
))
1352 if (chwalk
== RT_MUTEX_MIN_CHAINWALK
&&
1353 likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1354 rt_mutex_deadlock_account_lock(lock
, current
);
1357 return slowfn(lock
, state
, timeout
, chwalk
);
1361 rt_mutex_fasttrylock(struct rt_mutex
*lock
,
1362 int (*slowfn
)(struct rt_mutex
*lock
))
1364 if (likely(rt_mutex_cmpxchg_acquire(lock
, NULL
, current
))) {
1365 rt_mutex_deadlock_account_lock(lock
, current
);
1368 return slowfn(lock
);
1372 rt_mutex_fastunlock(struct rt_mutex
*lock
,
1373 bool (*slowfn
)(struct rt_mutex
*lock
,
1374 struct wake_q_head
*wqh
))
1378 if (likely(rt_mutex_cmpxchg_release(lock
, current
, NULL
))) {
1379 rt_mutex_deadlock_account_unlock(current
);
1382 bool deboost
= slowfn(lock
, &wake_q
);
1386 /* Undo pi boosting if necessary: */
1388 rt_mutex_adjust_prio(current
);
1393 * rt_mutex_lock - lock a rt_mutex
1395 * @lock: the rt_mutex to be locked
1397 void __sched
rt_mutex_lock(struct rt_mutex
*lock
)
1401 rt_mutex_fastlock(lock
, TASK_UNINTERRUPTIBLE
, rt_mutex_slowlock
);
1403 EXPORT_SYMBOL_GPL(rt_mutex_lock
);
1406 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1408 * @lock: the rt_mutex to be locked
1412 * -EINTR when interrupted by a signal
1414 int __sched
rt_mutex_lock_interruptible(struct rt_mutex
*lock
)
1418 return rt_mutex_fastlock(lock
, TASK_INTERRUPTIBLE
, rt_mutex_slowlock
);
1420 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible
);
1423 * Futex variant with full deadlock detection.
1425 int rt_mutex_timed_futex_lock(struct rt_mutex
*lock
,
1426 struct hrtimer_sleeper
*timeout
)
1430 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1431 RT_MUTEX_FULL_CHAINWALK
,
1436 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1437 * the timeout structure is provided
1440 * @lock: the rt_mutex to be locked
1441 * @timeout: timeout structure or NULL (no timeout)
1445 * -EINTR when interrupted by a signal
1446 * -ETIMEDOUT when the timeout expired
1449 rt_mutex_timed_lock(struct rt_mutex
*lock
, struct hrtimer_sleeper
*timeout
)
1453 return rt_mutex_timed_fastlock(lock
, TASK_INTERRUPTIBLE
, timeout
,
1454 RT_MUTEX_MIN_CHAINWALK
,
1457 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock
);
1460 * rt_mutex_trylock - try to lock a rt_mutex
1462 * @lock: the rt_mutex to be locked
1464 * This function can only be called in thread context. It's safe to
1465 * call it from atomic regions, but not from hard interrupt or soft
1466 * interrupt context.
1468 * Returns 1 on success and 0 on contention
1470 int __sched
rt_mutex_trylock(struct rt_mutex
*lock
)
1472 if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
1475 return rt_mutex_fasttrylock(lock
, rt_mutex_slowtrylock
);
1477 EXPORT_SYMBOL_GPL(rt_mutex_trylock
);
1480 * rt_mutex_unlock - unlock a rt_mutex
1482 * @lock: the rt_mutex to be unlocked
1484 void __sched
rt_mutex_unlock(struct rt_mutex
*lock
)
1486 rt_mutex_fastunlock(lock
, rt_mutex_slowunlock
);
1488 EXPORT_SYMBOL_GPL(rt_mutex_unlock
);
1491 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1492 * @lock: the rt_mutex to be unlocked
1494 * Returns: true/false indicating whether priority adjustment is
1497 bool __sched
rt_mutex_futex_unlock(struct rt_mutex
*lock
,
1498 struct wake_q_head
*wqh
)
1500 if (likely(rt_mutex_cmpxchg_release(lock
, current
, NULL
))) {
1501 rt_mutex_deadlock_account_unlock(current
);
1504 return rt_mutex_slowunlock(lock
, wqh
);
1508 * rt_mutex_destroy - mark a mutex unusable
1509 * @lock: the mutex to be destroyed
1511 * This function marks the mutex uninitialized, and any subsequent
1512 * use of the mutex is forbidden. The mutex must not be locked when
1513 * this function is called.
1515 void rt_mutex_destroy(struct rt_mutex
*lock
)
1517 WARN_ON(rt_mutex_is_locked(lock
));
1518 #ifdef CONFIG_DEBUG_RT_MUTEXES
1523 EXPORT_SYMBOL_GPL(rt_mutex_destroy
);
1526 * __rt_mutex_init - initialize the rt lock
1528 * @lock: the rt lock to be initialized
1530 * Initialize the rt lock to unlocked state.
1532 * Initializing of a locked rt lock is not allowed
1534 void __rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
1537 raw_spin_lock_init(&lock
->wait_lock
);
1538 lock
->waiters
= RB_ROOT
;
1539 lock
->waiters_leftmost
= NULL
;
1541 debug_rt_mutex_init(lock
, name
);
1543 EXPORT_SYMBOL_GPL(__rt_mutex_init
);
1546 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1549 * @lock: the rt_mutex to be locked
1550 * @proxy_owner:the task to set as owner
1552 * No locking. Caller has to do serializing itself
1553 * Special API call for PI-futex support
1555 void rt_mutex_init_proxy_locked(struct rt_mutex
*lock
,
1556 struct task_struct
*proxy_owner
)
1558 __rt_mutex_init(lock
, NULL
);
1559 debug_rt_mutex_proxy_lock(lock
, proxy_owner
);
1560 rt_mutex_set_owner(lock
, proxy_owner
);
1561 rt_mutex_deadlock_account_lock(lock
, proxy_owner
);
1565 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1567 * @lock: the rt_mutex to be locked
1569 * No locking. Caller has to do serializing itself
1570 * Special API call for PI-futex support
1572 void rt_mutex_proxy_unlock(struct rt_mutex
*lock
,
1573 struct task_struct
*proxy_owner
)
1575 debug_rt_mutex_proxy_unlock(lock
);
1576 rt_mutex_set_owner(lock
, NULL
);
1577 rt_mutex_deadlock_account_unlock(proxy_owner
);
1581 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1582 * @lock: the rt_mutex to take
1583 * @waiter: the pre-initialized rt_mutex_waiter
1584 * @task: the task to prepare
1587 * 0 - task blocked on lock
1588 * 1 - acquired the lock for task, caller should wake it up
1591 * Special API call for FUTEX_REQUEUE_PI support.
1593 int rt_mutex_start_proxy_lock(struct rt_mutex
*lock
,
1594 struct rt_mutex_waiter
*waiter
,
1595 struct task_struct
*task
)
1599 raw_spin_lock(&lock
->wait_lock
);
1601 if (try_to_take_rt_mutex(lock
, task
, NULL
)) {
1602 raw_spin_unlock(&lock
->wait_lock
);
1606 /* We enforce deadlock detection for futexes */
1607 ret
= task_blocks_on_rt_mutex(lock
, waiter
, task
,
1608 RT_MUTEX_FULL_CHAINWALK
);
1610 if (ret
&& !rt_mutex_owner(lock
)) {
1612 * Reset the return value. We might have
1613 * returned with -EDEADLK and the owner
1614 * released the lock while we were walking the
1615 * pi chain. Let the waiter sort it out.
1621 remove_waiter(lock
, waiter
);
1623 raw_spin_unlock(&lock
->wait_lock
);
1625 debug_rt_mutex_print_deadlock(waiter
);
1631 * rt_mutex_next_owner - return the next owner of the lock
1633 * @lock: the rt lock query
1635 * Returns the next owner of the lock or NULL
1637 * Caller has to serialize against other accessors to the lock
1640 * Special API call for PI-futex support
1642 struct task_struct
*rt_mutex_next_owner(struct rt_mutex
*lock
)
1644 if (!rt_mutex_has_waiters(lock
))
1647 return rt_mutex_top_waiter(lock
)->task
;
1651 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1652 * @lock: the rt_mutex we were woken on
1653 * @to: the timeout, null if none. hrtimer should already have
1655 * @waiter: the pre-initialized rt_mutex_waiter
1657 * Complete the lock acquisition started our behalf by another thread.
1661 * <0 - error, one of -EINTR, -ETIMEDOUT
1663 * Special API call for PI-futex requeue support
1665 int rt_mutex_finish_proxy_lock(struct rt_mutex
*lock
,
1666 struct hrtimer_sleeper
*to
,
1667 struct rt_mutex_waiter
*waiter
)
1671 raw_spin_lock(&lock
->wait_lock
);
1673 set_current_state(TASK_INTERRUPTIBLE
);
1675 /* sleep on the mutex */
1676 ret
= __rt_mutex_slowlock(lock
, TASK_INTERRUPTIBLE
, to
, waiter
);
1679 remove_waiter(lock
, waiter
);
1682 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1683 * have to fix that up.
1685 fixup_rt_mutex_waiters(lock
);
1687 raw_spin_unlock(&lock
->wait_lock
);