packaging: ivi: Do Not Use profile macro
[profile/ivi/kernel-x86-ivi.git] / kernel / locking / rtmutex.c
1 /*
2  * RT-Mutexes: simple blocking mutual exclusion locks with PI support
3  *
4  * started by Ingo Molnar and Thomas Gleixner.
5  *
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
10  *
11  *  See Documentation/rt-mutex-design.txt for details.
12  */
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>
19
20 #include "rtmutex_common.h"
21
22 /*
23  * lock->owner state tracking:
24  *
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.
27  *
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**
34  *
35  * The fast atomic compare exchange based acquire and release is only
36  * possible when bit 0 of lock->owner is 0.
37  *
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.
42  *
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.
47  */
48
49 static void
50 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
51 {
52         unsigned long val = (unsigned long)owner;
53
54         if (rt_mutex_has_waiters(lock))
55                 val |= RT_MUTEX_HAS_WAITERS;
56
57         lock->owner = (struct task_struct *)val;
58 }
59
60 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
61 {
62         lock->owner = (struct task_struct *)
63                         ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
64 }
65
66 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
67 {
68         if (!rt_mutex_has_waiters(lock))
69                 clear_rt_mutex_waiters(lock);
70 }
71
72 /*
73  * We can speed up the acquire/release, if the architecture
74  * supports cmpxchg and if there's no debugging state to be set up
75  */
76 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
77 # define rt_mutex_cmpxchg(l,c,n)        (cmpxchg(&l->owner, c, n) == c)
78 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
79 {
80         unsigned long owner, *p = (unsigned long *) &lock->owner;
81
82         do {
83                 owner = *p;
84         } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
85 }
86
87 /*
88  * Safe fastpath aware unlock:
89  * 1) Clear the waiters bit
90  * 2) Drop lock->wait_lock
91  * 3) Try to unlock the lock with cmpxchg
92  */
93 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
94         __releases(lock->wait_lock)
95 {
96         struct task_struct *owner = rt_mutex_owner(lock);
97
98         clear_rt_mutex_waiters(lock);
99         raw_spin_unlock(&lock->wait_lock);
100         /*
101          * If a new waiter comes in between the unlock and the cmpxchg
102          * we have two situations:
103          *
104          * unlock(wait_lock);
105          *                                      lock(wait_lock);
106          * cmpxchg(p, owner, 0) == owner
107          *                                      mark_rt_mutex_waiters(lock);
108          *                                      acquire(lock);
109          * or:
110          *
111          * unlock(wait_lock);
112          *                                      lock(wait_lock);
113          *                                      mark_rt_mutex_waiters(lock);
114          *
115          * cmpxchg(p, owner, 0) != owner
116          *                                      enqueue_waiter();
117          *                                      unlock(wait_lock);
118          * lock(wait_lock);
119          * wake waiter();
120          * unlock(wait_lock);
121          *                                      lock(wait_lock);
122          *                                      acquire(lock);
123          */
124         return rt_mutex_cmpxchg(lock, owner, NULL);
125 }
126
127 #else
128 # define rt_mutex_cmpxchg(l,c,n)        (0)
129 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
130 {
131         lock->owner = (struct task_struct *)
132                         ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
133 }
134
135 /*
136  * Simple slow path only version: lock->owner is protected by lock->wait_lock.
137  */
138 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
139         __releases(lock->wait_lock)
140 {
141         lock->owner = NULL;
142         raw_spin_unlock(&lock->wait_lock);
143         return true;
144 }
145 #endif
146
147 static inline int
148 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
149                      struct rt_mutex_waiter *right)
150 {
151         if (left->prio < right->prio)
152                 return 1;
153
154         /*
155          * If both waiters have dl_prio(), we check the deadlines of the
156          * associated tasks.
157          * If left waiter has a dl_prio(), and we didn't return 1 above,
158          * then right waiter has a dl_prio() too.
159          */
160         if (dl_prio(left->prio))
161                 return (left->task->dl.deadline < right->task->dl.deadline);
162
163         return 0;
164 }
165
166 static void
167 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
168 {
169         struct rb_node **link = &lock->waiters.rb_node;
170         struct rb_node *parent = NULL;
171         struct rt_mutex_waiter *entry;
172         int leftmost = 1;
173
174         while (*link) {
175                 parent = *link;
176                 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
177                 if (rt_mutex_waiter_less(waiter, entry)) {
178                         link = &parent->rb_left;
179                 } else {
180                         link = &parent->rb_right;
181                         leftmost = 0;
182                 }
183         }
184
185         if (leftmost)
186                 lock->waiters_leftmost = &waiter->tree_entry;
187
188         rb_link_node(&waiter->tree_entry, parent, link);
189         rb_insert_color(&waiter->tree_entry, &lock->waiters);
190 }
191
192 static void
193 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
194 {
195         if (RB_EMPTY_NODE(&waiter->tree_entry))
196                 return;
197
198         if (lock->waiters_leftmost == &waiter->tree_entry)
199                 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
200
201         rb_erase(&waiter->tree_entry, &lock->waiters);
202         RB_CLEAR_NODE(&waiter->tree_entry);
203 }
204
205 static void
206 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
207 {
208         struct rb_node **link = &task->pi_waiters.rb_node;
209         struct rb_node *parent = NULL;
210         struct rt_mutex_waiter *entry;
211         int leftmost = 1;
212
213         while (*link) {
214                 parent = *link;
215                 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
216                 if (rt_mutex_waiter_less(waiter, entry)) {
217                         link = &parent->rb_left;
218                 } else {
219                         link = &parent->rb_right;
220                         leftmost = 0;
221                 }
222         }
223
224         if (leftmost)
225                 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
226
227         rb_link_node(&waiter->pi_tree_entry, parent, link);
228         rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
229 }
230
231 static void
232 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
233 {
234         if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
235                 return;
236
237         if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
238                 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
239
240         rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
241         RB_CLEAR_NODE(&waiter->pi_tree_entry);
242 }
243
244 /*
245  * Calculate task priority from the waiter tree priority
246  *
247  * Return task->normal_prio when the waiter tree is empty or when
248  * the waiter is not allowed to do priority boosting
249  */
250 int rt_mutex_getprio(struct task_struct *task)
251 {
252         if (likely(!task_has_pi_waiters(task)))
253                 return task->normal_prio;
254
255         return min(task_top_pi_waiter(task)->prio,
256                    task->normal_prio);
257 }
258
259 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
260 {
261         if (likely(!task_has_pi_waiters(task)))
262                 return NULL;
263
264         return task_top_pi_waiter(task)->task;
265 }
266
267 /*
268  * Adjust the priority of a task, after its pi_waiters got modified.
269  *
270  * This can be both boosting and unboosting. task->pi_lock must be held.
271  */
272 static void __rt_mutex_adjust_prio(struct task_struct *task)
273 {
274         int prio = rt_mutex_getprio(task);
275
276         if (task->prio != prio || dl_prio(prio))
277                 rt_mutex_setprio(task, prio);
278 }
279
280 /*
281  * Adjust task priority (undo boosting). Called from the exit path of
282  * rt_mutex_slowunlock() and rt_mutex_slowlock().
283  *
284  * (Note: We do this outside of the protection of lock->wait_lock to
285  * allow the lock to be taken while or before we readjust the priority
286  * of task. We do not use the spin_xx_mutex() variants here as we are
287  * outside of the debug path.)
288  */
289 static void rt_mutex_adjust_prio(struct task_struct *task)
290 {
291         unsigned long flags;
292
293         raw_spin_lock_irqsave(&task->pi_lock, flags);
294         __rt_mutex_adjust_prio(task);
295         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
296 }
297
298 /*
299  * Max number of times we'll walk the boosting chain:
300  */
301 int max_lock_depth = 1024;
302
303 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
304 {
305         return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
306 }
307
308 /*
309  * Adjust the priority chain. Also used for deadlock detection.
310  * Decreases task's usage by one - may thus free the task.
311  *
312  * @task:       the task owning the mutex (owner) for which a chain walk is
313  *              probably needed
314  * @deadlock_detect: do we have to carry out deadlock detection?
315  * @orig_lock:  the mutex (can be NULL if we are walking the chain to recheck
316  *              things for a task that has just got its priority adjusted, and
317  *              is waiting on a mutex)
318  * @next_lock:  the mutex on which the owner of @orig_lock was blocked before
319  *              we dropped its pi_lock. Is never dereferenced, only used for
320  *              comparison to detect lock chain changes.
321  * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
322  *              its priority to the mutex owner (can be NULL in the case
323  *              depicted above or if the top waiter is gone away and we are
324  *              actually deboosting the owner)
325  * @top_task:   the current top waiter
326  *
327  * Returns 0 or -EDEADLK.
328  */
329 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
330                                       int deadlock_detect,
331                                       struct rt_mutex *orig_lock,
332                                       struct rt_mutex *next_lock,
333                                       struct rt_mutex_waiter *orig_waiter,
334                                       struct task_struct *top_task)
335 {
336         struct rt_mutex *lock;
337         struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
338         int detect_deadlock, ret = 0, depth = 0;
339         unsigned long flags;
340
341         detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
342                                                          deadlock_detect);
343
344         /*
345          * The (de)boosting is a step by step approach with a lot of
346          * pitfalls. We want this to be preemptible and we want hold a
347          * maximum of two locks per step. So we have to check
348          * carefully whether things change under us.
349          */
350  again:
351         if (++depth > max_lock_depth) {
352                 static int prev_max;
353
354                 /*
355                  * Print this only once. If the admin changes the limit,
356                  * print a new message when reaching the limit again.
357                  */
358                 if (prev_max != max_lock_depth) {
359                         prev_max = max_lock_depth;
360                         printk(KERN_WARNING "Maximum lock depth %d reached "
361                                "task: %s (%d)\n", max_lock_depth,
362                                top_task->comm, task_pid_nr(top_task));
363                 }
364                 put_task_struct(task);
365
366                 return -EDEADLK;
367         }
368  retry:
369         /*
370          * Task can not go away as we did a get_task() before !
371          */
372         raw_spin_lock_irqsave(&task->pi_lock, flags);
373
374         waiter = task->pi_blocked_on;
375         /*
376          * Check whether the end of the boosting chain has been
377          * reached or the state of the chain has changed while we
378          * dropped the locks.
379          */
380         if (!waiter)
381                 goto out_unlock_pi;
382
383         /*
384          * Check the orig_waiter state. After we dropped the locks,
385          * the previous owner of the lock might have released the lock.
386          */
387         if (orig_waiter && !rt_mutex_owner(orig_lock))
388                 goto out_unlock_pi;
389
390         /*
391          * We dropped all locks after taking a refcount on @task, so
392          * the task might have moved on in the lock chain or even left
393          * the chain completely and blocks now on an unrelated lock or
394          * on @orig_lock.
395          *
396          * We stored the lock on which @task was blocked in @next_lock,
397          * so we can detect the chain change.
398          */
399         if (next_lock != waiter->lock)
400                 goto out_unlock_pi;
401
402         /*
403          * Drop out, when the task has no waiters. Note,
404          * top_waiter can be NULL, when we are in the deboosting
405          * mode!
406          */
407         if (top_waiter) {
408                 if (!task_has_pi_waiters(task))
409                         goto out_unlock_pi;
410                 /*
411                  * If deadlock detection is off, we stop here if we
412                  * are not the top pi waiter of the task.
413                  */
414                 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
415                         goto out_unlock_pi;
416         }
417
418         /*
419          * When deadlock detection is off then we check, if further
420          * priority adjustment is necessary.
421          */
422         if (!detect_deadlock && waiter->prio == task->prio)
423                 goto out_unlock_pi;
424
425         lock = waiter->lock;
426         if (!raw_spin_trylock(&lock->wait_lock)) {
427                 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
428                 cpu_relax();
429                 goto retry;
430         }
431
432         /*
433          * Deadlock detection. If the lock is the same as the original
434          * lock which caused us to walk the lock chain or if the
435          * current lock is owned by the task which initiated the chain
436          * walk, we detected a deadlock.
437          */
438         if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
439                 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
440                 raw_spin_unlock(&lock->wait_lock);
441                 ret = -EDEADLK;
442                 goto out_unlock_pi;
443         }
444
445         top_waiter = rt_mutex_top_waiter(lock);
446
447         /* Requeue the waiter */
448         rt_mutex_dequeue(lock, waiter);
449         waiter->prio = task->prio;
450         rt_mutex_enqueue(lock, waiter);
451
452         /* Release the task */
453         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
454         if (!rt_mutex_owner(lock)) {
455                 /*
456                  * If the requeue above changed the top waiter, then we need
457                  * to wake the new top waiter up to try to get the lock.
458                  */
459
460                 if (top_waiter != rt_mutex_top_waiter(lock))
461                         wake_up_process(rt_mutex_top_waiter(lock)->task);
462                 raw_spin_unlock(&lock->wait_lock);
463                 goto out_put_task;
464         }
465         put_task_struct(task);
466
467         /* Grab the next task */
468         task = rt_mutex_owner(lock);
469         get_task_struct(task);
470         raw_spin_lock_irqsave(&task->pi_lock, flags);
471
472         if (waiter == rt_mutex_top_waiter(lock)) {
473                 /* Boost the owner */
474                 rt_mutex_dequeue_pi(task, top_waiter);
475                 rt_mutex_enqueue_pi(task, waiter);
476                 __rt_mutex_adjust_prio(task);
477
478         } else if (top_waiter == waiter) {
479                 /* Deboost the owner */
480                 rt_mutex_dequeue_pi(task, waiter);
481                 waiter = rt_mutex_top_waiter(lock);
482                 rt_mutex_enqueue_pi(task, waiter);
483                 __rt_mutex_adjust_prio(task);
484         }
485
486         /*
487          * Check whether the task which owns the current lock is pi
488          * blocked itself. If yes we store a pointer to the lock for
489          * the lock chain change detection above. After we dropped
490          * task->pi_lock next_lock cannot be dereferenced anymore.
491          */
492         next_lock = task_blocked_on_lock(task);
493
494         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
495
496         top_waiter = rt_mutex_top_waiter(lock);
497         raw_spin_unlock(&lock->wait_lock);
498
499         /*
500          * We reached the end of the lock chain. Stop right here. No
501          * point to go back just to figure that out.
502          */
503         if (!next_lock)
504                 goto out_put_task;
505
506         if (!detect_deadlock && waiter != top_waiter)
507                 goto out_put_task;
508
509         goto again;
510
511  out_unlock_pi:
512         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
513  out_put_task:
514         put_task_struct(task);
515
516         return ret;
517 }
518
519 /*
520  * Try to take an rt-mutex
521  *
522  * Must be called with lock->wait_lock held.
523  *
524  * @lock:   the lock to be acquired.
525  * @task:   the task which wants to acquire the lock
526  * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
527  */
528 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
529                 struct rt_mutex_waiter *waiter)
530 {
531         /*
532          * We have to be careful here if the atomic speedups are
533          * enabled, such that, when
534          *  - no other waiter is on the lock
535          *  - the lock has been released since we did the cmpxchg
536          * the lock can be released or taken while we are doing the
537          * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
538          *
539          * The atomic acquire/release aware variant of
540          * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
541          * the WAITERS bit, the atomic release / acquire can not
542          * happen anymore and lock->wait_lock protects us from the
543          * non-atomic case.
544          *
545          * Note, that this might set lock->owner =
546          * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
547          * any more. This is fixed up when we take the ownership.
548          * This is the transitional state explained at the top of this file.
549          */
550         mark_rt_mutex_waiters(lock);
551
552         if (rt_mutex_owner(lock))
553                 return 0;
554
555         /*
556          * It will get the lock because of one of these conditions:
557          * 1) there is no waiter
558          * 2) higher priority than waiters
559          * 3) it is top waiter
560          */
561         if (rt_mutex_has_waiters(lock)) {
562                 if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
563                         if (!waiter || waiter != rt_mutex_top_waiter(lock))
564                                 return 0;
565                 }
566         }
567
568         if (waiter || rt_mutex_has_waiters(lock)) {
569                 unsigned long flags;
570                 struct rt_mutex_waiter *top;
571
572                 raw_spin_lock_irqsave(&task->pi_lock, flags);
573
574                 /* remove the queued waiter. */
575                 if (waiter) {
576                         rt_mutex_dequeue(lock, waiter);
577                         task->pi_blocked_on = NULL;
578                 }
579
580                 /*
581                  * We have to enqueue the top waiter(if it exists) into
582                  * task->pi_waiters list.
583                  */
584                 if (rt_mutex_has_waiters(lock)) {
585                         top = rt_mutex_top_waiter(lock);
586                         rt_mutex_enqueue_pi(task, top);
587                 }
588                 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
589         }
590
591         /* We got the lock. */
592         debug_rt_mutex_lock(lock);
593
594         rt_mutex_set_owner(lock, task);
595
596         rt_mutex_deadlock_account_lock(lock, task);
597
598         return 1;
599 }
600
601 /*
602  * Task blocks on lock.
603  *
604  * Prepare waiter and propagate pi chain
605  *
606  * This must be called with lock->wait_lock held.
607  */
608 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
609                                    struct rt_mutex_waiter *waiter,
610                                    struct task_struct *task,
611                                    int detect_deadlock)
612 {
613         struct task_struct *owner = rt_mutex_owner(lock);
614         struct rt_mutex_waiter *top_waiter = waiter;
615         struct rt_mutex *next_lock;
616         int chain_walk = 0, res;
617         unsigned long flags;
618
619         /*
620          * Early deadlock detection. We really don't want the task to
621          * enqueue on itself just to untangle the mess later. It's not
622          * only an optimization. We drop the locks, so another waiter
623          * can come in before the chain walk detects the deadlock. So
624          * the other will detect the deadlock and return -EDEADLOCK,
625          * which is wrong, as the other waiter is not in a deadlock
626          * situation.
627          */
628         if (owner == task)
629                 return -EDEADLK;
630
631         raw_spin_lock_irqsave(&task->pi_lock, flags);
632         __rt_mutex_adjust_prio(task);
633         waiter->task = task;
634         waiter->lock = lock;
635         waiter->prio = task->prio;
636
637         /* Get the top priority waiter on the lock */
638         if (rt_mutex_has_waiters(lock))
639                 top_waiter = rt_mutex_top_waiter(lock);
640         rt_mutex_enqueue(lock, waiter);
641
642         task->pi_blocked_on = waiter;
643
644         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
645
646         if (!owner)
647                 return 0;
648
649         raw_spin_lock_irqsave(&owner->pi_lock, flags);
650         if (waiter == rt_mutex_top_waiter(lock)) {
651                 rt_mutex_dequeue_pi(owner, top_waiter);
652                 rt_mutex_enqueue_pi(owner, waiter);
653
654                 __rt_mutex_adjust_prio(owner);
655                 if (owner->pi_blocked_on)
656                         chain_walk = 1;
657         } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
658                 chain_walk = 1;
659         }
660
661         /* Store the lock on which owner is blocked or NULL */
662         next_lock = task_blocked_on_lock(owner);
663
664         raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
665         /*
666          * Even if full deadlock detection is on, if the owner is not
667          * blocked itself, we can avoid finding this out in the chain
668          * walk.
669          */
670         if (!chain_walk || !next_lock)
671                 return 0;
672
673         /*
674          * The owner can't disappear while holding a lock,
675          * so the owner struct is protected by wait_lock.
676          * Gets dropped in rt_mutex_adjust_prio_chain()!
677          */
678         get_task_struct(owner);
679
680         raw_spin_unlock(&lock->wait_lock);
681
682         res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
683                                          next_lock, waiter, task);
684
685         raw_spin_lock(&lock->wait_lock);
686
687         return res;
688 }
689
690 /*
691  * Wake up the next waiter on the lock.
692  *
693  * Remove the top waiter from the current tasks pi waiter list and
694  * wake it up.
695  *
696  * Called with lock->wait_lock held.
697  */
698 static void wakeup_next_waiter(struct rt_mutex *lock)
699 {
700         struct rt_mutex_waiter *waiter;
701         unsigned long flags;
702
703         raw_spin_lock_irqsave(&current->pi_lock, flags);
704
705         waiter = rt_mutex_top_waiter(lock);
706
707         /*
708          * Remove it from current->pi_waiters. We do not adjust a
709          * possible priority boost right now. We execute wakeup in the
710          * boosted mode and go back to normal after releasing
711          * lock->wait_lock.
712          */
713         rt_mutex_dequeue_pi(current, waiter);
714
715         /*
716          * As we are waking up the top waiter, and the waiter stays
717          * queued on the lock until it gets the lock, this lock
718          * obviously has waiters. Just set the bit here and this has
719          * the added benefit of forcing all new tasks into the
720          * slow path making sure no task of lower priority than
721          * the top waiter can steal this lock.
722          */
723         lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
724
725         raw_spin_unlock_irqrestore(&current->pi_lock, flags);
726
727         /*
728          * It's safe to dereference waiter as it cannot go away as
729          * long as we hold lock->wait_lock. The waiter task needs to
730          * acquire it in order to dequeue the waiter.
731          */
732         wake_up_process(waiter->task);
733 }
734
735 /*
736  * Remove a waiter from a lock and give up
737  *
738  * Must be called with lock->wait_lock held and
739  * have just failed to try_to_take_rt_mutex().
740  */
741 static void remove_waiter(struct rt_mutex *lock,
742                           struct rt_mutex_waiter *waiter)
743 {
744         int first = (waiter == rt_mutex_top_waiter(lock));
745         struct task_struct *owner = rt_mutex_owner(lock);
746         struct rt_mutex *next_lock = NULL;
747         unsigned long flags;
748
749         raw_spin_lock_irqsave(&current->pi_lock, flags);
750         rt_mutex_dequeue(lock, waiter);
751         current->pi_blocked_on = NULL;
752         raw_spin_unlock_irqrestore(&current->pi_lock, flags);
753
754         if (!owner)
755                 return;
756
757         if (first) {
758
759                 raw_spin_lock_irqsave(&owner->pi_lock, flags);
760
761                 rt_mutex_dequeue_pi(owner, waiter);
762
763                 if (rt_mutex_has_waiters(lock)) {
764                         struct rt_mutex_waiter *next;
765
766                         next = rt_mutex_top_waiter(lock);
767                         rt_mutex_enqueue_pi(owner, next);
768                 }
769                 __rt_mutex_adjust_prio(owner);
770
771                 /* Store the lock on which owner is blocked or NULL */
772                 next_lock = task_blocked_on_lock(owner);
773
774                 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
775         }
776
777         if (!next_lock)
778                 return;
779
780         /* gets dropped in rt_mutex_adjust_prio_chain()! */
781         get_task_struct(owner);
782
783         raw_spin_unlock(&lock->wait_lock);
784
785         rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
786
787         raw_spin_lock(&lock->wait_lock);
788 }
789
790 /*
791  * Recheck the pi chain, in case we got a priority setting
792  *
793  * Called from sched_setscheduler
794  */
795 void rt_mutex_adjust_pi(struct task_struct *task)
796 {
797         struct rt_mutex_waiter *waiter;
798         struct rt_mutex *next_lock;
799         unsigned long flags;
800
801         raw_spin_lock_irqsave(&task->pi_lock, flags);
802
803         waiter = task->pi_blocked_on;
804         if (!waiter || (waiter->prio == task->prio &&
805                         !dl_prio(task->prio))) {
806                 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
807                 return;
808         }
809         next_lock = waiter->lock;
810         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
811
812         /* gets dropped in rt_mutex_adjust_prio_chain()! */
813         get_task_struct(task);
814
815         rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
816 }
817
818 /**
819  * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
820  * @lock:                the rt_mutex to take
821  * @state:               the state the task should block in (TASK_INTERRUPTIBLE
822  *                       or TASK_UNINTERRUPTIBLE)
823  * @timeout:             the pre-initialized and started timer, or NULL for none
824  * @waiter:              the pre-initialized rt_mutex_waiter
825  *
826  * lock->wait_lock must be held by the caller.
827  */
828 static int __sched
829 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
830                     struct hrtimer_sleeper *timeout,
831                     struct rt_mutex_waiter *waiter)
832 {
833         int ret = 0;
834
835         for (;;) {
836                 /* Try to acquire the lock: */
837                 if (try_to_take_rt_mutex(lock, current, waiter))
838                         break;
839
840                 /*
841                  * TASK_INTERRUPTIBLE checks for signals and
842                  * timeout. Ignored otherwise.
843                  */
844                 if (unlikely(state == TASK_INTERRUPTIBLE)) {
845                         /* Signal pending? */
846                         if (signal_pending(current))
847                                 ret = -EINTR;
848                         if (timeout && !timeout->task)
849                                 ret = -ETIMEDOUT;
850                         if (ret)
851                                 break;
852                 }
853
854                 raw_spin_unlock(&lock->wait_lock);
855
856                 debug_rt_mutex_print_deadlock(waiter);
857
858                 schedule_rt_mutex(lock);
859
860                 raw_spin_lock(&lock->wait_lock);
861                 set_current_state(state);
862         }
863
864         return ret;
865 }
866
867 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
868                                      struct rt_mutex_waiter *w)
869 {
870         /*
871          * If the result is not -EDEADLOCK or the caller requested
872          * deadlock detection, nothing to do here.
873          */
874         if (res != -EDEADLOCK || detect_deadlock)
875                 return;
876
877         /*
878          * Yell lowdly and stop the task right here.
879          */
880         rt_mutex_print_deadlock(w);
881         while (1) {
882                 set_current_state(TASK_INTERRUPTIBLE);
883                 schedule();
884         }
885 }
886
887 /*
888  * Slow path lock function:
889  */
890 static int __sched
891 rt_mutex_slowlock(struct rt_mutex *lock, int state,
892                   struct hrtimer_sleeper *timeout,
893                   int detect_deadlock)
894 {
895         struct rt_mutex_waiter waiter;
896         int ret = 0;
897
898         debug_rt_mutex_init_waiter(&waiter);
899         RB_CLEAR_NODE(&waiter.pi_tree_entry);
900         RB_CLEAR_NODE(&waiter.tree_entry);
901
902         raw_spin_lock(&lock->wait_lock);
903
904         /* Try to acquire the lock again: */
905         if (try_to_take_rt_mutex(lock, current, NULL)) {
906                 raw_spin_unlock(&lock->wait_lock);
907                 return 0;
908         }
909
910         set_current_state(state);
911
912         /* Setup the timer, when timeout != NULL */
913         if (unlikely(timeout)) {
914                 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
915                 if (!hrtimer_active(&timeout->timer))
916                         timeout->task = NULL;
917         }
918
919         ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
920
921         if (likely(!ret))
922                 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
923
924         set_current_state(TASK_RUNNING);
925
926         if (unlikely(ret)) {
927                 remove_waiter(lock, &waiter);
928                 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
929         }
930
931         /*
932          * try_to_take_rt_mutex() sets the waiter bit
933          * unconditionally. We might have to fix that up.
934          */
935         fixup_rt_mutex_waiters(lock);
936
937         raw_spin_unlock(&lock->wait_lock);
938
939         /* Remove pending timer: */
940         if (unlikely(timeout))
941                 hrtimer_cancel(&timeout->timer);
942
943         debug_rt_mutex_free_waiter(&waiter);
944
945         return ret;
946 }
947
948 /*
949  * Slow path try-lock function:
950  */
951 static inline int
952 rt_mutex_slowtrylock(struct rt_mutex *lock)
953 {
954         int ret = 0;
955
956         raw_spin_lock(&lock->wait_lock);
957
958         if (likely(rt_mutex_owner(lock) != current)) {
959
960                 ret = try_to_take_rt_mutex(lock, current, NULL);
961                 /*
962                  * try_to_take_rt_mutex() sets the lock waiters
963                  * bit unconditionally. Clean this up.
964                  */
965                 fixup_rt_mutex_waiters(lock);
966         }
967
968         raw_spin_unlock(&lock->wait_lock);
969
970         return ret;
971 }
972
973 /*
974  * Slow path to release a rt-mutex:
975  */
976 static void __sched
977 rt_mutex_slowunlock(struct rt_mutex *lock)
978 {
979         raw_spin_lock(&lock->wait_lock);
980
981         debug_rt_mutex_unlock(lock);
982
983         rt_mutex_deadlock_account_unlock(current);
984
985         /*
986          * We must be careful here if the fast path is enabled. If we
987          * have no waiters queued we cannot set owner to NULL here
988          * because of:
989          *
990          * foo->lock->owner = NULL;
991          *                      rtmutex_lock(foo->lock);   <- fast path
992          *                      free = atomic_dec_and_test(foo->refcnt);
993          *                      rtmutex_unlock(foo->lock); <- fast path
994          *                      if (free)
995          *                              kfree(foo);
996          * raw_spin_unlock(foo->lock->wait_lock);
997          *
998          * So for the fastpath enabled kernel:
999          *
1000          * Nothing can set the waiters bit as long as we hold
1001          * lock->wait_lock. So we do the following sequence:
1002          *
1003          *      owner = rt_mutex_owner(lock);
1004          *      clear_rt_mutex_waiters(lock);
1005          *      raw_spin_unlock(&lock->wait_lock);
1006          *      if (cmpxchg(&lock->owner, owner, 0) == owner)
1007          *              return;
1008          *      goto retry;
1009          *
1010          * The fastpath disabled variant is simple as all access to
1011          * lock->owner is serialized by lock->wait_lock:
1012          *
1013          *      lock->owner = NULL;
1014          *      raw_spin_unlock(&lock->wait_lock);
1015          */
1016         while (!rt_mutex_has_waiters(lock)) {
1017                 /* Drops lock->wait_lock ! */
1018                 if (unlock_rt_mutex_safe(lock) == true)
1019                         return;
1020                 /* Relock the rtmutex and try again */
1021                 raw_spin_lock(&lock->wait_lock);
1022         }
1023
1024         /*
1025          * The wakeup next waiter path does not suffer from the above
1026          * race. See the comments there.
1027          */
1028         wakeup_next_waiter(lock);
1029
1030         raw_spin_unlock(&lock->wait_lock);
1031
1032         /* Undo pi boosting if necessary: */
1033         rt_mutex_adjust_prio(current);
1034 }
1035
1036 /*
1037  * debug aware fast / slowpath lock,trylock,unlock
1038  *
1039  * The atomic acquire/release ops are compiled away, when either the
1040  * architecture does not support cmpxchg or when debugging is enabled.
1041  */
1042 static inline int
1043 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1044                   int detect_deadlock,
1045                   int (*slowfn)(struct rt_mutex *lock, int state,
1046                                 struct hrtimer_sleeper *timeout,
1047                                 int detect_deadlock))
1048 {
1049         if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1050                 rt_mutex_deadlock_account_lock(lock, current);
1051                 return 0;
1052         } else
1053                 return slowfn(lock, state, NULL, detect_deadlock);
1054 }
1055
1056 static inline int
1057 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1058                         struct hrtimer_sleeper *timeout, int detect_deadlock,
1059                         int (*slowfn)(struct rt_mutex *lock, int state,
1060                                       struct hrtimer_sleeper *timeout,
1061                                       int detect_deadlock))
1062 {
1063         if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1064                 rt_mutex_deadlock_account_lock(lock, current);
1065                 return 0;
1066         } else
1067                 return slowfn(lock, state, timeout, detect_deadlock);
1068 }
1069
1070 static inline int
1071 rt_mutex_fasttrylock(struct rt_mutex *lock,
1072                      int (*slowfn)(struct rt_mutex *lock))
1073 {
1074         if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1075                 rt_mutex_deadlock_account_lock(lock, current);
1076                 return 1;
1077         }
1078         return slowfn(lock);
1079 }
1080
1081 static inline void
1082 rt_mutex_fastunlock(struct rt_mutex *lock,
1083                     void (*slowfn)(struct rt_mutex *lock))
1084 {
1085         if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1086                 rt_mutex_deadlock_account_unlock(current);
1087         else
1088                 slowfn(lock);
1089 }
1090
1091 /**
1092  * rt_mutex_lock - lock a rt_mutex
1093  *
1094  * @lock: the rt_mutex to be locked
1095  */
1096 void __sched rt_mutex_lock(struct rt_mutex *lock)
1097 {
1098         might_sleep();
1099
1100         rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1101 }
1102 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1103
1104 /**
1105  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1106  *
1107  * @lock:               the rt_mutex to be locked
1108  * @detect_deadlock:    deadlock detection on/off
1109  *
1110  * Returns:
1111  *  0           on success
1112  * -EINTR       when interrupted by a signal
1113  * -EDEADLK     when the lock would deadlock (when deadlock detection is on)
1114  */
1115 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1116                                                  int detect_deadlock)
1117 {
1118         might_sleep();
1119
1120         return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1121                                  detect_deadlock, rt_mutex_slowlock);
1122 }
1123 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1124
1125 /**
1126  * rt_mutex_timed_lock - lock a rt_mutex interruptible
1127  *                      the timeout structure is provided
1128  *                      by the caller
1129  *
1130  * @lock:               the rt_mutex to be locked
1131  * @timeout:            timeout structure or NULL (no timeout)
1132  * @detect_deadlock:    deadlock detection on/off
1133  *
1134  * Returns:
1135  *  0           on success
1136  * -EINTR       when interrupted by a signal
1137  * -ETIMEDOUT   when the timeout expired
1138  * -EDEADLK     when the lock would deadlock (when deadlock detection is on)
1139  */
1140 int
1141 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1142                     int detect_deadlock)
1143 {
1144         might_sleep();
1145
1146         return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1147                                        detect_deadlock, rt_mutex_slowlock);
1148 }
1149 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1150
1151 /**
1152  * rt_mutex_trylock - try to lock a rt_mutex
1153  *
1154  * @lock:       the rt_mutex to be locked
1155  *
1156  * Returns 1 on success and 0 on contention
1157  */
1158 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1159 {
1160         return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1161 }
1162 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1163
1164 /**
1165  * rt_mutex_unlock - unlock a rt_mutex
1166  *
1167  * @lock: the rt_mutex to be unlocked
1168  */
1169 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1170 {
1171         rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1172 }
1173 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1174
1175 /**
1176  * rt_mutex_destroy - mark a mutex unusable
1177  * @lock: the mutex to be destroyed
1178  *
1179  * This function marks the mutex uninitialized, and any subsequent
1180  * use of the mutex is forbidden. The mutex must not be locked when
1181  * this function is called.
1182  */
1183 void rt_mutex_destroy(struct rt_mutex *lock)
1184 {
1185         WARN_ON(rt_mutex_is_locked(lock));
1186 #ifdef CONFIG_DEBUG_RT_MUTEXES
1187         lock->magic = NULL;
1188 #endif
1189 }
1190
1191 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1192
1193 /**
1194  * __rt_mutex_init - initialize the rt lock
1195  *
1196  * @lock: the rt lock to be initialized
1197  *
1198  * Initialize the rt lock to unlocked state.
1199  *
1200  * Initializing of a locked rt lock is not allowed
1201  */
1202 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1203 {
1204         lock->owner = NULL;
1205         raw_spin_lock_init(&lock->wait_lock);
1206         lock->waiters = RB_ROOT;
1207         lock->waiters_leftmost = NULL;
1208
1209         debug_rt_mutex_init(lock, name);
1210 }
1211 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1212
1213 /**
1214  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1215  *                              proxy owner
1216  *
1217  * @lock:       the rt_mutex to be locked
1218  * @proxy_owner:the task to set as owner
1219  *
1220  * No locking. Caller has to do serializing itself
1221  * Special API call for PI-futex support
1222  */
1223 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1224                                 struct task_struct *proxy_owner)
1225 {
1226         __rt_mutex_init(lock, NULL);
1227         debug_rt_mutex_proxy_lock(lock, proxy_owner);
1228         rt_mutex_set_owner(lock, proxy_owner);
1229         rt_mutex_deadlock_account_lock(lock, proxy_owner);
1230 }
1231
1232 /**
1233  * rt_mutex_proxy_unlock - release a lock on behalf of owner
1234  *
1235  * @lock:       the rt_mutex to be locked
1236  *
1237  * No locking. Caller has to do serializing itself
1238  * Special API call for PI-futex support
1239  */
1240 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1241                            struct task_struct *proxy_owner)
1242 {
1243         debug_rt_mutex_proxy_unlock(lock);
1244         rt_mutex_set_owner(lock, NULL);
1245         rt_mutex_deadlock_account_unlock(proxy_owner);
1246 }
1247
1248 /**
1249  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1250  * @lock:               the rt_mutex to take
1251  * @waiter:             the pre-initialized rt_mutex_waiter
1252  * @task:               the task to prepare
1253  * @detect_deadlock:    perform deadlock detection (1) or not (0)
1254  *
1255  * Returns:
1256  *  0 - task blocked on lock
1257  *  1 - acquired the lock for task, caller should wake it up
1258  * <0 - error
1259  *
1260  * Special API call for FUTEX_REQUEUE_PI support.
1261  */
1262 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1263                               struct rt_mutex_waiter *waiter,
1264                               struct task_struct *task, int detect_deadlock)
1265 {
1266         int ret;
1267
1268         raw_spin_lock(&lock->wait_lock);
1269
1270         if (try_to_take_rt_mutex(lock, task, NULL)) {
1271                 raw_spin_unlock(&lock->wait_lock);
1272                 return 1;
1273         }
1274
1275         /* We enforce deadlock detection for futexes */
1276         ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1277
1278         if (ret && !rt_mutex_owner(lock)) {
1279                 /*
1280                  * Reset the return value. We might have
1281                  * returned with -EDEADLK and the owner
1282                  * released the lock while we were walking the
1283                  * pi chain.  Let the waiter sort it out.
1284                  */
1285                 ret = 0;
1286         }
1287
1288         if (unlikely(ret))
1289                 remove_waiter(lock, waiter);
1290
1291         raw_spin_unlock(&lock->wait_lock);
1292
1293         debug_rt_mutex_print_deadlock(waiter);
1294
1295         return ret;
1296 }
1297
1298 /**
1299  * rt_mutex_next_owner - return the next owner of the lock
1300  *
1301  * @lock: the rt lock query
1302  *
1303  * Returns the next owner of the lock or NULL
1304  *
1305  * Caller has to serialize against other accessors to the lock
1306  * itself.
1307  *
1308  * Special API call for PI-futex support
1309  */
1310 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1311 {
1312         if (!rt_mutex_has_waiters(lock))
1313                 return NULL;
1314
1315         return rt_mutex_top_waiter(lock)->task;
1316 }
1317
1318 /**
1319  * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1320  * @lock:               the rt_mutex we were woken on
1321  * @to:                 the timeout, null if none. hrtimer should already have
1322  *                      been started.
1323  * @waiter:             the pre-initialized rt_mutex_waiter
1324  * @detect_deadlock:    perform deadlock detection (1) or not (0)
1325  *
1326  * Complete the lock acquisition started our behalf by another thread.
1327  *
1328  * Returns:
1329  *  0 - success
1330  * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1331  *
1332  * Special API call for PI-futex requeue support
1333  */
1334 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1335                                struct hrtimer_sleeper *to,
1336                                struct rt_mutex_waiter *waiter,
1337                                int detect_deadlock)
1338 {
1339         int ret;
1340
1341         raw_spin_lock(&lock->wait_lock);
1342
1343         set_current_state(TASK_INTERRUPTIBLE);
1344
1345         ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1346
1347         set_current_state(TASK_RUNNING);
1348
1349         if (unlikely(ret))
1350                 remove_waiter(lock, waiter);
1351
1352         /*
1353          * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1354          * have to fix that up.
1355          */
1356         fixup_rt_mutex_waiters(lock);
1357
1358         raw_spin_unlock(&lock->wait_lock);
1359
1360         return ret;
1361 }