Linux 3.16.66
[linux/fpc-iii.git] / kernel / locking / rtmutex.c
blob5b28de11e4c944c05e627ee9c6d8869c960d00f2
1 /*
2 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
4 * started by Ingo Molnar and Thomas Gleixner.
6 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
8 * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
9 * Copyright (C) 2006 Esben Nielsen
11 * See Documentation/rt-mutex-design.txt for details.
13 #include <linux/spinlock.h>
14 #include <linux/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.
28 * owner bit0
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.
49 static void
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 unsigned long owner, *p = (unsigned long *) &lock->owner;
70 if (rt_mutex_has_waiters(lock))
71 return;
74 * The rbtree has no waiters enqueued, now make sure that the
75 * lock->owner still has the waiters bit set, otherwise the
76 * following can happen:
78 * CPU 0 CPU 1 CPU2
79 * l->owner=T1
80 * rt_mutex_lock(l)
81 * lock(l->lock)
82 * l->owner = T1 | HAS_WAITERS;
83 * enqueue(T2)
84 * boost()
85 * unlock(l->lock)
86 * block()
88 * rt_mutex_lock(l)
89 * lock(l->lock)
90 * l->owner = T1 | HAS_WAITERS;
91 * enqueue(T3)
92 * boost()
93 * unlock(l->lock)
94 * block()
95 * signal(->T2) signal(->T3)
96 * lock(l->lock)
97 * dequeue(T2)
98 * deboost()
99 * unlock(l->lock)
100 * lock(l->lock)
101 * dequeue(T3)
102 * ==> wait list is empty
103 * deboost()
104 * unlock(l->lock)
105 * lock(l->lock)
106 * fixup_rt_mutex_waiters()
107 * if (wait_list_empty(l) {
108 * l->owner = owner
109 * owner = l->owner & ~HAS_WAITERS;
110 * ==> l->owner = T1
112 * lock(l->lock)
113 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
114 * if (wait_list_empty(l) {
115 * owner = l->owner & ~HAS_WAITERS;
116 * cmpxchg(l->owner, T1, NULL)
117 * ===> Success (l->owner = NULL)
119 * l->owner = owner
120 * ==> l->owner = T1
123 * With the check for the waiter bit in place T3 on CPU2 will not
124 * overwrite. All tasks fiddling with the waiters bit are
125 * serialized by l->lock, so nothing else can modify the waiters
126 * bit. If the bit is set then nothing can change l->owner either
127 * so the simple RMW is safe. The cmpxchg() will simply fail if it
128 * happens in the middle of the RMW because the waiters bit is
129 * still set.
131 owner = ACCESS_ONCE(*p);
132 if (owner & RT_MUTEX_HAS_WAITERS)
133 ACCESS_ONCE(*p) = owner & ~RT_MUTEX_HAS_WAITERS;
137 * We can speed up the acquire/release, if the architecture
138 * supports cmpxchg and if there's no debugging state to be set up
140 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
141 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
142 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
144 unsigned long owner, *p = (unsigned long *) &lock->owner;
146 do {
147 owner = *p;
148 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
152 * Safe fastpath aware unlock:
153 * 1) Clear the waiters bit
154 * 2) Drop lock->wait_lock
155 * 3) Try to unlock the lock with cmpxchg
157 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
158 __releases(lock->wait_lock)
160 struct task_struct *owner = rt_mutex_owner(lock);
162 clear_rt_mutex_waiters(lock);
163 raw_spin_unlock(&lock->wait_lock);
165 * If a new waiter comes in between the unlock and the cmpxchg
166 * we have two situations:
168 * unlock(wait_lock);
169 * lock(wait_lock);
170 * cmpxchg(p, owner, 0) == owner
171 * mark_rt_mutex_waiters(lock);
172 * acquire(lock);
173 * or:
175 * unlock(wait_lock);
176 * lock(wait_lock);
177 * mark_rt_mutex_waiters(lock);
179 * cmpxchg(p, owner, 0) != owner
180 * enqueue_waiter();
181 * unlock(wait_lock);
182 * lock(wait_lock);
183 * wake waiter();
184 * unlock(wait_lock);
185 * lock(wait_lock);
186 * acquire(lock);
188 return rt_mutex_cmpxchg(lock, owner, NULL);
191 #else
192 # define rt_mutex_cmpxchg(l,c,n) (0)
193 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
195 lock->owner = (struct task_struct *)
196 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
200 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
202 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
203 __releases(lock->wait_lock)
205 lock->owner = NULL;
206 raw_spin_unlock(&lock->wait_lock);
207 return true;
209 #endif
211 static inline int
212 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
213 struct rt_mutex_waiter *right)
215 if (left->prio < right->prio)
216 return 1;
219 * If both waiters have dl_prio(), we check the deadlines of the
220 * associated tasks.
221 * If left waiter has a dl_prio(), and we didn't return 1 above,
222 * then right waiter has a dl_prio() too.
224 if (dl_prio(left->prio))
225 return (left->task->dl.deadline < right->task->dl.deadline);
227 return 0;
230 static void
231 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
233 struct rb_node **link = &lock->waiters.rb_node;
234 struct rb_node *parent = NULL;
235 struct rt_mutex_waiter *entry;
236 int leftmost = 1;
238 while (*link) {
239 parent = *link;
240 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
241 if (rt_mutex_waiter_less(waiter, entry)) {
242 link = &parent->rb_left;
243 } else {
244 link = &parent->rb_right;
245 leftmost = 0;
249 if (leftmost)
250 lock->waiters_leftmost = &waiter->tree_entry;
252 rb_link_node(&waiter->tree_entry, parent, link);
253 rb_insert_color(&waiter->tree_entry, &lock->waiters);
256 static void
257 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
259 if (RB_EMPTY_NODE(&waiter->tree_entry))
260 return;
262 if (lock->waiters_leftmost == &waiter->tree_entry)
263 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
265 rb_erase(&waiter->tree_entry, &lock->waiters);
266 RB_CLEAR_NODE(&waiter->tree_entry);
269 static void
270 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
272 struct rb_node **link = &task->pi_waiters.rb_node;
273 struct rb_node *parent = NULL;
274 struct rt_mutex_waiter *entry;
275 int leftmost = 1;
277 while (*link) {
278 parent = *link;
279 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
280 if (rt_mutex_waiter_less(waiter, entry)) {
281 link = &parent->rb_left;
282 } else {
283 link = &parent->rb_right;
284 leftmost = 0;
288 if (leftmost)
289 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
291 rb_link_node(&waiter->pi_tree_entry, parent, link);
292 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
295 static void
296 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
298 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
299 return;
301 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
302 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
304 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
305 RB_CLEAR_NODE(&waiter->pi_tree_entry);
309 * Calculate task priority from the waiter tree priority
311 * Return task->normal_prio when the waiter tree is empty or when
312 * the waiter is not allowed to do priority boosting
314 int rt_mutex_getprio(struct task_struct *task)
316 if (likely(!task_has_pi_waiters(task)))
317 return task->normal_prio;
319 return min(task_top_pi_waiter(task)->prio,
320 task->normal_prio);
323 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
325 if (likely(!task_has_pi_waiters(task)))
326 return NULL;
328 return task_top_pi_waiter(task)->task;
332 * Called by sched_setscheduler() to get the priority which will be
333 * effective after the change.
335 int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
337 if (!task_has_pi_waiters(task))
338 return newprio;
340 if (task_top_pi_waiter(task)->task->prio <= newprio)
341 return task_top_pi_waiter(task)->task->prio;
342 return newprio;
346 * Adjust the priority of a task, after its pi_waiters got modified.
348 * This can be both boosting and unboosting. task->pi_lock must be held.
350 static void __rt_mutex_adjust_prio(struct task_struct *task)
352 int prio = rt_mutex_getprio(task);
354 if (task->prio != prio || dl_prio(prio))
355 rt_mutex_setprio(task, prio);
359 * Adjust task priority (undo boosting). Called from the exit path of
360 * rt_mutex_slowunlock() and rt_mutex_slowlock().
362 * (Note: We do this outside of the protection of lock->wait_lock to
363 * allow the lock to be taken while or before we readjust the priority
364 * of task. We do not use the spin_xx_mutex() variants here as we are
365 * outside of the debug path.)
367 static void rt_mutex_adjust_prio(struct task_struct *task)
369 unsigned long flags;
371 raw_spin_lock_irqsave(&task->pi_lock, flags);
372 __rt_mutex_adjust_prio(task);
373 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
377 * Max number of times we'll walk the boosting chain:
379 int max_lock_depth = 1024;
381 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
383 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
387 * Adjust the priority chain. Also used for deadlock detection.
388 * Decreases task's usage by one - may thus free the task.
390 * @task: the task owning the mutex (owner) for which a chain walk is
391 * probably needed
392 * @deadlock_detect: do we have to carry out deadlock detection?
393 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
394 * things for a task that has just got its priority adjusted, and
395 * is waiting on a mutex)
396 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
397 * we dropped its pi_lock. Is never dereferenced, only used for
398 * comparison to detect lock chain changes.
399 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
400 * its priority to the mutex owner (can be NULL in the case
401 * depicted above or if the top waiter is gone away and we are
402 * actually deboosting the owner)
403 * @top_task: the current top waiter
405 * Returns 0 or -EDEADLK.
407 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
408 int deadlock_detect,
409 struct rt_mutex *orig_lock,
410 struct rt_mutex *next_lock,
411 struct rt_mutex_waiter *orig_waiter,
412 struct task_struct *top_task)
414 struct rt_mutex *lock;
415 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
416 int detect_deadlock, ret = 0, depth = 0;
417 unsigned long flags;
419 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
420 deadlock_detect);
423 * The (de)boosting is a step by step approach with a lot of
424 * pitfalls. We want this to be preemptible and we want hold a
425 * maximum of two locks per step. So we have to check
426 * carefully whether things change under us.
428 again:
429 if (++depth > max_lock_depth) {
430 static int prev_max;
433 * Print this only once. If the admin changes the limit,
434 * print a new message when reaching the limit again.
436 if (prev_max != max_lock_depth) {
437 prev_max = max_lock_depth;
438 printk(KERN_WARNING "Maximum lock depth %d reached "
439 "task: %s (%d)\n", max_lock_depth,
440 top_task->comm, task_pid_nr(top_task));
442 put_task_struct(task);
444 return -EDEADLK;
446 retry:
448 * Task can not go away as we did a get_task() before !
450 raw_spin_lock_irqsave(&task->pi_lock, flags);
452 waiter = task->pi_blocked_on;
454 * Check whether the end of the boosting chain has been
455 * reached or the state of the chain has changed while we
456 * dropped the locks.
458 if (!waiter)
459 goto out_unlock_pi;
462 * Check the orig_waiter state. After we dropped the locks,
463 * the previous owner of the lock might have released the lock.
465 if (orig_waiter && !rt_mutex_owner(orig_lock))
466 goto out_unlock_pi;
469 * We dropped all locks after taking a refcount on @task, so
470 * the task might have moved on in the lock chain or even left
471 * the chain completely and blocks now on an unrelated lock or
472 * on @orig_lock.
474 * We stored the lock on which @task was blocked in @next_lock,
475 * so we can detect the chain change.
477 if (next_lock != waiter->lock)
478 goto out_unlock_pi;
481 * Drop out, when the task has no waiters. Note,
482 * top_waiter can be NULL, when we are in the deboosting
483 * mode!
485 if (top_waiter) {
486 if (!task_has_pi_waiters(task))
487 goto out_unlock_pi;
489 * If deadlock detection is off, we stop here if we
490 * are not the top pi waiter of the task.
492 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
493 goto out_unlock_pi;
497 * When deadlock detection is off then we check, if further
498 * priority adjustment is necessary.
500 if (!detect_deadlock && waiter->prio == task->prio)
501 goto out_unlock_pi;
503 lock = waiter->lock;
504 if (!raw_spin_trylock(&lock->wait_lock)) {
505 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
506 cpu_relax();
507 goto retry;
511 * Deadlock detection. If the lock is the same as the original
512 * lock which caused us to walk the lock chain or if the
513 * current lock is owned by the task which initiated the chain
514 * walk, we detected a deadlock.
516 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
517 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
518 raw_spin_unlock(&lock->wait_lock);
519 ret = -EDEADLK;
520 goto out_unlock_pi;
523 top_waiter = rt_mutex_top_waiter(lock);
525 /* Requeue the waiter */
526 rt_mutex_dequeue(lock, waiter);
527 waiter->prio = task->prio;
528 rt_mutex_enqueue(lock, waiter);
530 /* Release the task */
531 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
532 if (!rt_mutex_owner(lock)) {
534 * If the requeue above changed the top waiter, then we need
535 * to wake the new top waiter up to try to get the lock.
538 if (top_waiter != rt_mutex_top_waiter(lock))
539 wake_up_process(rt_mutex_top_waiter(lock)->task);
540 raw_spin_unlock(&lock->wait_lock);
541 goto out_put_task;
543 put_task_struct(task);
545 /* Grab the next task */
546 task = rt_mutex_owner(lock);
547 get_task_struct(task);
548 raw_spin_lock_irqsave(&task->pi_lock, flags);
550 if (waiter == rt_mutex_top_waiter(lock)) {
551 /* Boost the owner */
552 rt_mutex_dequeue_pi(task, top_waiter);
553 rt_mutex_enqueue_pi(task, waiter);
554 __rt_mutex_adjust_prio(task);
556 } else if (top_waiter == waiter) {
557 /* Deboost the owner */
558 rt_mutex_dequeue_pi(task, waiter);
559 waiter = rt_mutex_top_waiter(lock);
560 rt_mutex_enqueue_pi(task, waiter);
561 __rt_mutex_adjust_prio(task);
565 * Check whether the task which owns the current lock is pi
566 * blocked itself. If yes we store a pointer to the lock for
567 * the lock chain change detection above. After we dropped
568 * task->pi_lock next_lock cannot be dereferenced anymore.
570 next_lock = task_blocked_on_lock(task);
572 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
574 top_waiter = rt_mutex_top_waiter(lock);
575 raw_spin_unlock(&lock->wait_lock);
578 * We reached the end of the lock chain. Stop right here. No
579 * point to go back just to figure that out.
581 if (!next_lock)
582 goto out_put_task;
584 if (!detect_deadlock && waiter != top_waiter)
585 goto out_put_task;
587 goto again;
589 out_unlock_pi:
590 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
591 out_put_task:
592 put_task_struct(task);
594 return ret;
598 * Try to take an rt-mutex
600 * Must be called with lock->wait_lock held.
602 * @lock: the lock to be acquired.
603 * @task: the task which wants to acquire the lock
604 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
606 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
607 struct rt_mutex_waiter *waiter)
610 * We have to be careful here if the atomic speedups are
611 * enabled, such that, when
612 * - no other waiter is on the lock
613 * - the lock has been released since we did the cmpxchg
614 * the lock can be released or taken while we are doing the
615 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
617 * The atomic acquire/release aware variant of
618 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
619 * the WAITERS bit, the atomic release / acquire can not
620 * happen anymore and lock->wait_lock protects us from the
621 * non-atomic case.
623 * Note, that this might set lock->owner =
624 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
625 * any more. This is fixed up when we take the ownership.
626 * This is the transitional state explained at the top of this file.
628 mark_rt_mutex_waiters(lock);
630 if (rt_mutex_owner(lock))
631 return 0;
634 * It will get the lock because of one of these conditions:
635 * 1) there is no waiter
636 * 2) higher priority than waiters
637 * 3) it is top waiter
639 if (rt_mutex_has_waiters(lock)) {
640 if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
641 if (!waiter || waiter != rt_mutex_top_waiter(lock))
642 return 0;
646 if (waiter || rt_mutex_has_waiters(lock)) {
647 unsigned long flags;
648 struct rt_mutex_waiter *top;
650 raw_spin_lock_irqsave(&task->pi_lock, flags);
652 /* remove the queued waiter. */
653 if (waiter) {
654 rt_mutex_dequeue(lock, waiter);
655 task->pi_blocked_on = NULL;
659 * We have to enqueue the top waiter(if it exists) into
660 * task->pi_waiters list.
662 if (rt_mutex_has_waiters(lock)) {
663 top = rt_mutex_top_waiter(lock);
664 rt_mutex_enqueue_pi(task, top);
666 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
669 /* We got the lock. */
670 debug_rt_mutex_lock(lock);
672 rt_mutex_set_owner(lock, task);
674 rt_mutex_deadlock_account_lock(lock, task);
676 return 1;
680 * Task blocks on lock.
682 * Prepare waiter and propagate pi chain
684 * This must be called with lock->wait_lock held.
686 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
687 struct rt_mutex_waiter *waiter,
688 struct task_struct *task,
689 int detect_deadlock)
691 struct task_struct *owner = rt_mutex_owner(lock);
692 struct rt_mutex_waiter *top_waiter = waiter;
693 struct rt_mutex *next_lock;
694 int chain_walk = 0, res;
695 unsigned long flags;
698 * Early deadlock detection. We really don't want the task to
699 * enqueue on itself just to untangle the mess later. It's not
700 * only an optimization. We drop the locks, so another waiter
701 * can come in before the chain walk detects the deadlock. So
702 * the other will detect the deadlock and return -EDEADLOCK,
703 * which is wrong, as the other waiter is not in a deadlock
704 * situation.
706 if (owner == task)
707 return -EDEADLK;
709 raw_spin_lock_irqsave(&task->pi_lock, flags);
710 __rt_mutex_adjust_prio(task);
711 waiter->task = task;
712 waiter->lock = lock;
713 waiter->prio = task->prio;
715 /* Get the top priority waiter on the lock */
716 if (rt_mutex_has_waiters(lock))
717 top_waiter = rt_mutex_top_waiter(lock);
718 rt_mutex_enqueue(lock, waiter);
720 task->pi_blocked_on = waiter;
722 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
724 if (!owner)
725 return 0;
727 raw_spin_lock_irqsave(&owner->pi_lock, flags);
728 if (waiter == rt_mutex_top_waiter(lock)) {
729 rt_mutex_dequeue_pi(owner, top_waiter);
730 rt_mutex_enqueue_pi(owner, waiter);
732 __rt_mutex_adjust_prio(owner);
733 if (owner->pi_blocked_on)
734 chain_walk = 1;
735 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
736 chain_walk = 1;
739 /* Store the lock on which owner is blocked or NULL */
740 next_lock = task_blocked_on_lock(owner);
742 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
744 * Even if full deadlock detection is on, if the owner is not
745 * blocked itself, we can avoid finding this out in the chain
746 * walk.
748 if (!chain_walk || !next_lock)
749 return 0;
752 * The owner can't disappear while holding a lock,
753 * so the owner struct is protected by wait_lock.
754 * Gets dropped in rt_mutex_adjust_prio_chain()!
756 get_task_struct(owner);
758 raw_spin_unlock(&lock->wait_lock);
760 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
761 next_lock, waiter, task);
763 raw_spin_lock(&lock->wait_lock);
765 return res;
769 * Wake up the next waiter on the lock.
771 * Remove the top waiter from the current tasks pi waiter list and
772 * wake it up.
774 * Called with lock->wait_lock held.
776 static void wakeup_next_waiter(struct rt_mutex *lock)
778 struct rt_mutex_waiter *waiter;
779 unsigned long flags;
781 raw_spin_lock_irqsave(&current->pi_lock, flags);
783 waiter = rt_mutex_top_waiter(lock);
786 * Remove it from current->pi_waiters. We do not adjust a
787 * possible priority boost right now. We execute wakeup in the
788 * boosted mode and go back to normal after releasing
789 * lock->wait_lock.
791 rt_mutex_dequeue_pi(current, waiter);
794 * As we are waking up the top waiter, and the waiter stays
795 * queued on the lock until it gets the lock, this lock
796 * obviously has waiters. Just set the bit here and this has
797 * the added benefit of forcing all new tasks into the
798 * slow path making sure no task of lower priority than
799 * the top waiter can steal this lock.
801 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
803 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
806 * It's safe to dereference waiter as it cannot go away as
807 * long as we hold lock->wait_lock. The waiter task needs to
808 * acquire it in order to dequeue the waiter.
810 wake_up_process(waiter->task);
814 * Remove a waiter from a lock and give up
816 * Must be called with lock->wait_lock held and
817 * have just failed to try_to_take_rt_mutex().
819 static void remove_waiter(struct rt_mutex *lock,
820 struct rt_mutex_waiter *waiter)
822 int first = (waiter == rt_mutex_top_waiter(lock));
823 struct task_struct *owner = rt_mutex_owner(lock);
824 struct rt_mutex *next_lock = NULL;
825 unsigned long flags;
827 raw_spin_lock_irqsave(&current->pi_lock, flags);
828 rt_mutex_dequeue(lock, waiter);
829 current->pi_blocked_on = NULL;
830 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
832 if (!owner)
833 return;
835 if (first) {
837 raw_spin_lock_irqsave(&owner->pi_lock, flags);
839 rt_mutex_dequeue_pi(owner, waiter);
841 if (rt_mutex_has_waiters(lock)) {
842 struct rt_mutex_waiter *next;
844 next = rt_mutex_top_waiter(lock);
845 rt_mutex_enqueue_pi(owner, next);
847 __rt_mutex_adjust_prio(owner);
849 /* Store the lock on which owner is blocked or NULL */
850 next_lock = task_blocked_on_lock(owner);
852 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
855 if (!next_lock)
856 return;
858 /* gets dropped in rt_mutex_adjust_prio_chain()! */
859 get_task_struct(owner);
861 raw_spin_unlock(&lock->wait_lock);
863 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
865 raw_spin_lock(&lock->wait_lock);
869 * Recheck the pi chain, in case we got a priority setting
871 * Called from sched_setscheduler
873 void rt_mutex_adjust_pi(struct task_struct *task)
875 struct rt_mutex_waiter *waiter;
876 struct rt_mutex *next_lock;
877 unsigned long flags;
879 raw_spin_lock_irqsave(&task->pi_lock, flags);
881 waiter = task->pi_blocked_on;
882 if (!waiter || (waiter->prio == task->prio &&
883 !dl_prio(task->prio))) {
884 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
885 return;
887 next_lock = waiter->lock;
888 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
890 /* gets dropped in rt_mutex_adjust_prio_chain()! */
891 get_task_struct(task);
893 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
897 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
898 * @lock: the rt_mutex to take
899 * @state: the state the task should block in (TASK_INTERRUPTIBLE
900 * or TASK_UNINTERRUPTIBLE)
901 * @timeout: the pre-initialized and started timer, or NULL for none
902 * @waiter: the pre-initialized rt_mutex_waiter
904 * lock->wait_lock must be held by the caller.
906 static int __sched
907 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
908 struct hrtimer_sleeper *timeout,
909 struct rt_mutex_waiter *waiter)
911 int ret = 0;
913 for (;;) {
914 /* Try to acquire the lock: */
915 if (try_to_take_rt_mutex(lock, current, waiter))
916 break;
919 * TASK_INTERRUPTIBLE checks for signals and
920 * timeout. Ignored otherwise.
922 if (unlikely(state == TASK_INTERRUPTIBLE)) {
923 /* Signal pending? */
924 if (signal_pending(current))
925 ret = -EINTR;
926 if (timeout && !timeout->task)
927 ret = -ETIMEDOUT;
928 if (ret)
929 break;
932 raw_spin_unlock(&lock->wait_lock);
934 debug_rt_mutex_print_deadlock(waiter);
936 schedule_rt_mutex(lock);
938 raw_spin_lock(&lock->wait_lock);
939 set_current_state(state);
942 return ret;
945 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
946 struct rt_mutex_waiter *w)
949 * If the result is not -EDEADLOCK or the caller requested
950 * deadlock detection, nothing to do here.
952 if (res != -EDEADLOCK || detect_deadlock)
953 return;
956 * Yell lowdly and stop the task right here.
958 rt_mutex_print_deadlock(w);
959 while (1) {
960 set_current_state(TASK_INTERRUPTIBLE);
961 schedule();
966 * Slow path lock function:
968 static int __sched
969 rt_mutex_slowlock(struct rt_mutex *lock, int state,
970 struct hrtimer_sleeper *timeout,
971 int detect_deadlock)
973 struct rt_mutex_waiter waiter;
974 int ret = 0;
976 debug_rt_mutex_init_waiter(&waiter);
977 RB_CLEAR_NODE(&waiter.pi_tree_entry);
978 RB_CLEAR_NODE(&waiter.tree_entry);
980 raw_spin_lock(&lock->wait_lock);
982 /* Try to acquire the lock again: */
983 if (try_to_take_rt_mutex(lock, current, NULL)) {
984 raw_spin_unlock(&lock->wait_lock);
985 return 0;
988 set_current_state(state);
990 /* Setup the timer, when timeout != NULL */
991 if (unlikely(timeout)) {
992 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
993 if (!hrtimer_active(&timeout->timer))
994 timeout->task = NULL;
997 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
999 if (likely(!ret))
1000 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1002 set_current_state(TASK_RUNNING);
1004 if (unlikely(ret)) {
1005 if (rt_mutex_has_waiters(lock))
1006 remove_waiter(lock, &waiter);
1007 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
1011 * try_to_take_rt_mutex() sets the waiter bit
1012 * unconditionally. We might have to fix that up.
1014 fixup_rt_mutex_waiters(lock);
1016 raw_spin_unlock(&lock->wait_lock);
1018 /* Remove pending timer: */
1019 if (unlikely(timeout))
1020 hrtimer_cancel(&timeout->timer);
1022 debug_rt_mutex_free_waiter(&waiter);
1024 return ret;
1028 * Slow path try-lock function:
1030 static inline int
1031 rt_mutex_slowtrylock(struct rt_mutex *lock)
1033 int ret = 0;
1035 raw_spin_lock(&lock->wait_lock);
1037 if (likely(rt_mutex_owner(lock) != current)) {
1039 ret = try_to_take_rt_mutex(lock, current, NULL);
1041 * try_to_take_rt_mutex() sets the lock waiters
1042 * bit unconditionally. Clean this up.
1044 fixup_rt_mutex_waiters(lock);
1047 raw_spin_unlock(&lock->wait_lock);
1049 return ret;
1053 * Slow path to release a rt-mutex:
1055 static void __sched
1056 rt_mutex_slowunlock(struct rt_mutex *lock)
1058 raw_spin_lock(&lock->wait_lock);
1060 debug_rt_mutex_unlock(lock);
1062 rt_mutex_deadlock_account_unlock(current);
1065 * We must be careful here if the fast path is enabled. If we
1066 * have no waiters queued we cannot set owner to NULL here
1067 * because of:
1069 * foo->lock->owner = NULL;
1070 * rtmutex_lock(foo->lock); <- fast path
1071 * free = atomic_dec_and_test(foo->refcnt);
1072 * rtmutex_unlock(foo->lock); <- fast path
1073 * if (free)
1074 * kfree(foo);
1075 * raw_spin_unlock(foo->lock->wait_lock);
1077 * So for the fastpath enabled kernel:
1079 * Nothing can set the waiters bit as long as we hold
1080 * lock->wait_lock. So we do the following sequence:
1082 * owner = rt_mutex_owner(lock);
1083 * clear_rt_mutex_waiters(lock);
1084 * raw_spin_unlock(&lock->wait_lock);
1085 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1086 * return;
1087 * goto retry;
1089 * The fastpath disabled variant is simple as all access to
1090 * lock->owner is serialized by lock->wait_lock:
1092 * lock->owner = NULL;
1093 * raw_spin_unlock(&lock->wait_lock);
1095 while (!rt_mutex_has_waiters(lock)) {
1096 /* Drops lock->wait_lock ! */
1097 if (unlock_rt_mutex_safe(lock) == true)
1098 return;
1099 /* Relock the rtmutex and try again */
1100 raw_spin_lock(&lock->wait_lock);
1104 * The wakeup next waiter path does not suffer from the above
1105 * race. See the comments there.
1107 wakeup_next_waiter(lock);
1109 raw_spin_unlock(&lock->wait_lock);
1111 /* Undo pi boosting if necessary: */
1112 rt_mutex_adjust_prio(current);
1116 * debug aware fast / slowpath lock,trylock,unlock
1118 * The atomic acquire/release ops are compiled away, when either the
1119 * architecture does not support cmpxchg or when debugging is enabled.
1121 static inline int
1122 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1123 int detect_deadlock,
1124 int (*slowfn)(struct rt_mutex *lock, int state,
1125 struct hrtimer_sleeper *timeout,
1126 int detect_deadlock))
1128 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1129 rt_mutex_deadlock_account_lock(lock, current);
1130 return 0;
1131 } else
1132 return slowfn(lock, state, NULL, detect_deadlock);
1135 static inline int
1136 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1137 struct hrtimer_sleeper *timeout, int detect_deadlock,
1138 int (*slowfn)(struct rt_mutex *lock, int state,
1139 struct hrtimer_sleeper *timeout,
1140 int detect_deadlock))
1142 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1143 rt_mutex_deadlock_account_lock(lock, current);
1144 return 0;
1145 } else
1146 return slowfn(lock, state, timeout, detect_deadlock);
1149 static inline int
1150 rt_mutex_fasttrylock(struct rt_mutex *lock,
1151 int (*slowfn)(struct rt_mutex *lock))
1153 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1154 rt_mutex_deadlock_account_lock(lock, current);
1155 return 1;
1157 return slowfn(lock);
1160 static inline void
1161 rt_mutex_fastunlock(struct rt_mutex *lock,
1162 void (*slowfn)(struct rt_mutex *lock))
1164 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1165 rt_mutex_deadlock_account_unlock(current);
1166 else
1167 slowfn(lock);
1171 * rt_mutex_lock - lock a rt_mutex
1173 * @lock: the rt_mutex to be locked
1175 void __sched rt_mutex_lock(struct rt_mutex *lock)
1177 might_sleep();
1179 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1181 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1184 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1186 * @lock: the rt_mutex to be locked
1187 * @detect_deadlock: deadlock detection on/off
1189 * Returns:
1190 * 0 on success
1191 * -EINTR when interrupted by a signal
1192 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1194 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1195 int detect_deadlock)
1197 might_sleep();
1199 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1200 detect_deadlock, rt_mutex_slowlock);
1202 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1205 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1206 * the timeout structure is provided
1207 * by the caller
1209 * @lock: the rt_mutex to be locked
1210 * @timeout: timeout structure or NULL (no timeout)
1211 * @detect_deadlock: deadlock detection on/off
1213 * Returns:
1214 * 0 on success
1215 * -EINTR when interrupted by a signal
1216 * -ETIMEDOUT when the timeout expired
1217 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1220 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1221 int detect_deadlock)
1223 might_sleep();
1225 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1226 detect_deadlock, rt_mutex_slowlock);
1228 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1231 * rt_mutex_trylock - try to lock a rt_mutex
1233 * @lock: the rt_mutex to be locked
1235 * Returns 1 on success and 0 on contention
1237 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1239 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1241 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1244 * rt_mutex_unlock - unlock a rt_mutex
1246 * @lock: the rt_mutex to be unlocked
1248 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1250 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1252 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1255 * rt_mutex_destroy - mark a mutex unusable
1256 * @lock: the mutex to be destroyed
1258 * This function marks the mutex uninitialized, and any subsequent
1259 * use of the mutex is forbidden. The mutex must not be locked when
1260 * this function is called.
1262 void rt_mutex_destroy(struct rt_mutex *lock)
1264 WARN_ON(rt_mutex_is_locked(lock));
1265 #ifdef CONFIG_DEBUG_RT_MUTEXES
1266 lock->magic = NULL;
1267 #endif
1270 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1273 * __rt_mutex_init - initialize the rt lock
1275 * @lock: the rt lock to be initialized
1277 * Initialize the rt lock to unlocked state.
1279 * Initializing of a locked rt lock is not allowed
1281 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1283 lock->owner = NULL;
1284 raw_spin_lock_init(&lock->wait_lock);
1285 lock->waiters = RB_ROOT;
1286 lock->waiters_leftmost = NULL;
1288 debug_rt_mutex_init(lock, name);
1290 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1293 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1294 * proxy owner
1296 * @lock: the rt_mutex to be locked
1297 * @proxy_owner:the task to set as owner
1299 * No locking. Caller has to do serializing itself
1300 * Special API call for PI-futex support
1302 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1303 struct task_struct *proxy_owner)
1305 __rt_mutex_init(lock, NULL);
1306 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1307 rt_mutex_set_owner(lock, proxy_owner);
1308 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1312 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1314 * @lock: the rt_mutex to be locked
1316 * No locking. Caller has to do serializing itself
1317 * Special API call for PI-futex support
1319 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1320 struct task_struct *proxy_owner)
1322 debug_rt_mutex_proxy_unlock(lock);
1323 rt_mutex_set_owner(lock, NULL);
1324 rt_mutex_deadlock_account_unlock(proxy_owner);
1328 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1329 * @lock: the rt_mutex to take
1330 * @waiter: the pre-initialized rt_mutex_waiter
1331 * @task: the task to prepare
1332 * @detect_deadlock: perform deadlock detection (1) or not (0)
1334 * Returns:
1335 * 0 - task blocked on lock
1336 * 1 - acquired the lock for task, caller should wake it up
1337 * <0 - error
1339 * Special API call for FUTEX_REQUEUE_PI support.
1341 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1342 struct rt_mutex_waiter *waiter,
1343 struct task_struct *task, int detect_deadlock)
1345 int ret;
1347 raw_spin_lock(&lock->wait_lock);
1349 if (try_to_take_rt_mutex(lock, task, NULL)) {
1350 raw_spin_unlock(&lock->wait_lock);
1351 return 1;
1354 /* We enforce deadlock detection for futexes */
1355 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1357 if (ret && !rt_mutex_owner(lock)) {
1359 * Reset the return value. We might have
1360 * returned with -EDEADLK and the owner
1361 * released the lock while we were walking the
1362 * pi chain. Let the waiter sort it out.
1364 ret = 0;
1367 if (unlikely(ret))
1368 remove_waiter(lock, waiter);
1370 raw_spin_unlock(&lock->wait_lock);
1372 debug_rt_mutex_print_deadlock(waiter);
1374 return ret;
1378 * rt_mutex_next_owner - return the next owner of the lock
1380 * @lock: the rt lock query
1382 * Returns the next owner of the lock or NULL
1384 * Caller has to serialize against other accessors to the lock
1385 * itself.
1387 * Special API call for PI-futex support
1389 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1391 if (!rt_mutex_has_waiters(lock))
1392 return NULL;
1394 return rt_mutex_top_waiter(lock)->task;
1398 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1399 * @lock: the rt_mutex we were woken on
1400 * @to: the timeout, null if none. hrtimer should already have
1401 * been started.
1402 * @waiter: the pre-initialized rt_mutex_waiter
1403 * @detect_deadlock: perform deadlock detection (1) or not (0)
1405 * Complete the lock acquisition started our behalf by another thread.
1407 * Returns:
1408 * 0 - success
1409 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1411 * Special API call for PI-futex requeue support
1413 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1414 struct hrtimer_sleeper *to,
1415 struct rt_mutex_waiter *waiter,
1416 int detect_deadlock)
1418 int ret;
1420 raw_spin_lock(&lock->wait_lock);
1422 set_current_state(TASK_INTERRUPTIBLE);
1424 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1426 set_current_state(TASK_RUNNING);
1428 if (unlikely(ret))
1429 remove_waiter(lock, waiter);
1432 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1433 * have to fix that up.
1435 fixup_rt_mutex_waiters(lock);
1437 raw_spin_unlock(&lock->wait_lock);
1439 return ret;