arm64: dts: rk3368: hdmi disabled default and remove hdmi node from p9 board
[firefly-linux-kernel-4.4.55.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/locking/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         unsigned long owner, *p = (unsigned long *) &lock->owner;
69
70         if (rt_mutex_has_waiters(lock))
71                 return;
72
73         /*
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:
77          *
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()
87          *
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
111          *                }
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)
118          *
119          *                                  l->owner = owner
120          *                                    ==> l->owner = T1
121          *                                }
122          *
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.
130          */
131         owner = READ_ONCE(*p);
132         if (owner & RT_MUTEX_HAS_WAITERS)
133                 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
134 }
135
136 /*
137  * We can speed up the acquire/release, if there's no debugging state to be
138  * set up.
139  */
140 #ifndef CONFIG_DEBUG_RT_MUTEXES
141 # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
142 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
143 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
144
145 /*
146  * Callers must hold the ->wait_lock -- which is the whole purpose as we force
147  * all future threads that attempt to [Rmw] the lock to the slowpath. As such
148  * relaxed semantics suffice.
149  */
150 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
151 {
152         unsigned long owner, *p = (unsigned long *) &lock->owner;
153
154         do {
155                 owner = *p;
156         } while (cmpxchg_relaxed(p, owner,
157                                  owner | RT_MUTEX_HAS_WAITERS) != owner);
158 }
159
160 /*
161  * Safe fastpath aware unlock:
162  * 1) Clear the waiters bit
163  * 2) Drop lock->wait_lock
164  * 3) Try to unlock the lock with cmpxchg
165  */
166 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
167         __releases(lock->wait_lock)
168 {
169         struct task_struct *owner = rt_mutex_owner(lock);
170
171         clear_rt_mutex_waiters(lock);
172         raw_spin_unlock(&lock->wait_lock);
173         /*
174          * If a new waiter comes in between the unlock and the cmpxchg
175          * we have two situations:
176          *
177          * unlock(wait_lock);
178          *                                      lock(wait_lock);
179          * cmpxchg(p, owner, 0) == owner
180          *                                      mark_rt_mutex_waiters(lock);
181          *                                      acquire(lock);
182          * or:
183          *
184          * unlock(wait_lock);
185          *                                      lock(wait_lock);
186          *                                      mark_rt_mutex_waiters(lock);
187          *
188          * cmpxchg(p, owner, 0) != owner
189          *                                      enqueue_waiter();
190          *                                      unlock(wait_lock);
191          * lock(wait_lock);
192          * wake waiter();
193          * unlock(wait_lock);
194          *                                      lock(wait_lock);
195          *                                      acquire(lock);
196          */
197         return rt_mutex_cmpxchg_release(lock, owner, NULL);
198 }
199
200 #else
201 # define rt_mutex_cmpxchg_relaxed(l,c,n)        (0)
202 # define rt_mutex_cmpxchg_acquire(l,c,n)        (0)
203 # define rt_mutex_cmpxchg_release(l,c,n)        (0)
204
205 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
206 {
207         lock->owner = (struct task_struct *)
208                         ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
209 }
210
211 /*
212  * Simple slow path only version: lock->owner is protected by lock->wait_lock.
213  */
214 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
215         __releases(lock->wait_lock)
216 {
217         lock->owner = NULL;
218         raw_spin_unlock(&lock->wait_lock);
219         return true;
220 }
221 #endif
222
223 static inline int
224 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
225                      struct rt_mutex_waiter *right)
226 {
227         if (left->prio < right->prio)
228                 return 1;
229
230         /*
231          * If both waiters have dl_prio(), we check the deadlines of the
232          * associated tasks.
233          * If left waiter has a dl_prio(), and we didn't return 1 above,
234          * then right waiter has a dl_prio() too.
235          */
236         if (dl_prio(left->prio))
237                 return dl_time_before(left->task->dl.deadline,
238                                       right->task->dl.deadline);
239
240         return 0;
241 }
242
243 static void
244 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
245 {
246         struct rb_node **link = &lock->waiters.rb_node;
247         struct rb_node *parent = NULL;
248         struct rt_mutex_waiter *entry;
249         int leftmost = 1;
250
251         while (*link) {
252                 parent = *link;
253                 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
254                 if (rt_mutex_waiter_less(waiter, entry)) {
255                         link = &parent->rb_left;
256                 } else {
257                         link = &parent->rb_right;
258                         leftmost = 0;
259                 }
260         }
261
262         if (leftmost)
263                 lock->waiters_leftmost = &waiter->tree_entry;
264
265         rb_link_node(&waiter->tree_entry, parent, link);
266         rb_insert_color(&waiter->tree_entry, &lock->waiters);
267 }
268
269 static void
270 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
271 {
272         if (RB_EMPTY_NODE(&waiter->tree_entry))
273                 return;
274
275         if (lock->waiters_leftmost == &waiter->tree_entry)
276                 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
277
278         rb_erase(&waiter->tree_entry, &lock->waiters);
279         RB_CLEAR_NODE(&waiter->tree_entry);
280 }
281
282 static void
283 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
284 {
285         struct rb_node **link = &task->pi_waiters.rb_node;
286         struct rb_node *parent = NULL;
287         struct rt_mutex_waiter *entry;
288         int leftmost = 1;
289
290         while (*link) {
291                 parent = *link;
292                 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
293                 if (rt_mutex_waiter_less(waiter, entry)) {
294                         link = &parent->rb_left;
295                 } else {
296                         link = &parent->rb_right;
297                         leftmost = 0;
298                 }
299         }
300
301         if (leftmost)
302                 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
303
304         rb_link_node(&waiter->pi_tree_entry, parent, link);
305         rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
306 }
307
308 static void
309 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
310 {
311         if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
312                 return;
313
314         if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
315                 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
316
317         rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
318         RB_CLEAR_NODE(&waiter->pi_tree_entry);
319 }
320
321 /*
322  * Calculate task priority from the waiter tree priority
323  *
324  * Return task->normal_prio when the waiter tree is empty or when
325  * the waiter is not allowed to do priority boosting
326  */
327 int rt_mutex_getprio(struct task_struct *task)
328 {
329         if (likely(!task_has_pi_waiters(task)))
330                 return task->normal_prio;
331
332         return min(task_top_pi_waiter(task)->prio,
333                    task->normal_prio);
334 }
335
336 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
337 {
338         if (likely(!task_has_pi_waiters(task)))
339                 return NULL;
340
341         return task_top_pi_waiter(task)->task;
342 }
343
344 /*
345  * Called by sched_setscheduler() to get the priority which will be
346  * effective after the change.
347  */
348 int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
349 {
350         if (!task_has_pi_waiters(task))
351                 return newprio;
352
353         if (task_top_pi_waiter(task)->task->prio <= newprio)
354                 return task_top_pi_waiter(task)->task->prio;
355         return newprio;
356 }
357
358 /*
359  * Adjust the priority of a task, after its pi_waiters got modified.
360  *
361  * This can be both boosting and unboosting. task->pi_lock must be held.
362  */
363 static void __rt_mutex_adjust_prio(struct task_struct *task)
364 {
365         int prio = rt_mutex_getprio(task);
366
367         if (task->prio != prio || dl_prio(prio))
368                 rt_mutex_setprio(task, prio);
369 }
370
371 /*
372  * Adjust task priority (undo boosting). Called from the exit path of
373  * rt_mutex_slowunlock() and rt_mutex_slowlock().
374  *
375  * (Note: We do this outside of the protection of lock->wait_lock to
376  * allow the lock to be taken while or before we readjust the priority
377  * of task. We do not use the spin_xx_mutex() variants here as we are
378  * outside of the debug path.)
379  */
380 void rt_mutex_adjust_prio(struct task_struct *task)
381 {
382         unsigned long flags;
383
384         raw_spin_lock_irqsave(&task->pi_lock, flags);
385         __rt_mutex_adjust_prio(task);
386         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
387 }
388
389 /*
390  * Deadlock detection is conditional:
391  *
392  * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
393  * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
394  *
395  * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
396  * conducted independent of the detect argument.
397  *
398  * If the waiter argument is NULL this indicates the deboost path and
399  * deadlock detection is disabled independent of the detect argument
400  * and the config settings.
401  */
402 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
403                                           enum rtmutex_chainwalk chwalk)
404 {
405         /*
406          * This is just a wrapper function for the following call,
407          * because debug_rt_mutex_detect_deadlock() smells like a magic
408          * debug feature and I wanted to keep the cond function in the
409          * main source file along with the comments instead of having
410          * two of the same in the headers.
411          */
412         return debug_rt_mutex_detect_deadlock(waiter, chwalk);
413 }
414
415 /*
416  * Max number of times we'll walk the boosting chain:
417  */
418 int max_lock_depth = 1024;
419
420 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
421 {
422         return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
423 }
424
425 /*
426  * Adjust the priority chain. Also used for deadlock detection.
427  * Decreases task's usage by one - may thus free the task.
428  *
429  * @task:       the task owning the mutex (owner) for which a chain walk is
430  *              probably needed
431  * @chwalk:     do we have to carry out deadlock detection?
432  * @orig_lock:  the mutex (can be NULL if we are walking the chain to recheck
433  *              things for a task that has just got its priority adjusted, and
434  *              is waiting on a mutex)
435  * @next_lock:  the mutex on which the owner of @orig_lock was blocked before
436  *              we dropped its pi_lock. Is never dereferenced, only used for
437  *              comparison to detect lock chain changes.
438  * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
439  *              its priority to the mutex owner (can be NULL in the case
440  *              depicted above or if the top waiter is gone away and we are
441  *              actually deboosting the owner)
442  * @top_task:   the current top waiter
443  *
444  * Returns 0 or -EDEADLK.
445  *
446  * Chain walk basics and protection scope
447  *
448  * [R] refcount on task
449  * [P] task->pi_lock held
450  * [L] rtmutex->wait_lock held
451  *
452  * Step Description                             Protected by
453  *      function arguments:
454  *      @task                                   [R]
455  *      @orig_lock if != NULL                   @top_task is blocked on it
456  *      @next_lock                              Unprotected. Cannot be
457  *                                              dereferenced. Only used for
458  *                                              comparison.
459  *      @orig_waiter if != NULL                 @top_task is blocked on it
460  *      @top_task                               current, or in case of proxy
461  *                                              locking protected by calling
462  *                                              code
463  *      again:
464  *        loop_sanity_check();
465  *      retry:
466  * [1]    lock(task->pi_lock);                  [R] acquire [P]
467  * [2]    waiter = task->pi_blocked_on;         [P]
468  * [3]    check_exit_conditions_1();            [P]
469  * [4]    lock = waiter->lock;                  [P]
470  * [5]    if (!try_lock(lock->wait_lock)) {     [P] try to acquire [L]
471  *          unlock(task->pi_lock);              release [P]
472  *          goto retry;
473  *        }
474  * [6]    check_exit_conditions_2();            [P] + [L]
475  * [7]    requeue_lock_waiter(lock, waiter);    [P] + [L]
476  * [8]    unlock(task->pi_lock);                release [P]
477  *        put_task_struct(task);                release [R]
478  * [9]    check_exit_conditions_3();            [L]
479  * [10]   task = owner(lock);                   [L]
480  *        get_task_struct(task);                [L] acquire [R]
481  *        lock(task->pi_lock);                  [L] acquire [P]
482  * [11]   requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
483  * [12]   check_exit_conditions_4();            [P] + [L]
484  * [13]   unlock(task->pi_lock);                release [P]
485  *        unlock(lock->wait_lock);              release [L]
486  *        goto again;
487  */
488 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
489                                       enum rtmutex_chainwalk chwalk,
490                                       struct rt_mutex *orig_lock,
491                                       struct rt_mutex *next_lock,
492                                       struct rt_mutex_waiter *orig_waiter,
493                                       struct task_struct *top_task)
494 {
495         struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
496         struct rt_mutex_waiter *prerequeue_top_waiter;
497         int ret = 0, depth = 0;
498         struct rt_mutex *lock;
499         bool detect_deadlock;
500         unsigned long flags;
501         bool requeue = true;
502
503         detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
504
505         /*
506          * The (de)boosting is a step by step approach with a lot of
507          * pitfalls. We want this to be preemptible and we want hold a
508          * maximum of two locks per step. So we have to check
509          * carefully whether things change under us.
510          */
511  again:
512         /*
513          * We limit the lock chain length for each invocation.
514          */
515         if (++depth > max_lock_depth) {
516                 static int prev_max;
517
518                 /*
519                  * Print this only once. If the admin changes the limit,
520                  * print a new message when reaching the limit again.
521                  */
522                 if (prev_max != max_lock_depth) {
523                         prev_max = max_lock_depth;
524                         printk(KERN_WARNING "Maximum lock depth %d reached "
525                                "task: %s (%d)\n", max_lock_depth,
526                                top_task->comm, task_pid_nr(top_task));
527                 }
528                 put_task_struct(task);
529
530                 return -EDEADLK;
531         }
532
533         /*
534          * We are fully preemptible here and only hold the refcount on
535          * @task. So everything can have changed under us since the
536          * caller or our own code below (goto retry/again) dropped all
537          * locks.
538          */
539  retry:
540         /*
541          * [1] Task cannot go away as we did a get_task() before !
542          */
543         raw_spin_lock_irqsave(&task->pi_lock, flags);
544
545         /*
546          * [2] Get the waiter on which @task is blocked on.
547          */
548         waiter = task->pi_blocked_on;
549
550         /*
551          * [3] check_exit_conditions_1() protected by task->pi_lock.
552          */
553
554         /*
555          * Check whether the end of the boosting chain has been
556          * reached or the state of the chain has changed while we
557          * dropped the locks.
558          */
559         if (!waiter)
560                 goto out_unlock_pi;
561
562         /*
563          * Check the orig_waiter state. After we dropped the locks,
564          * the previous owner of the lock might have released the lock.
565          */
566         if (orig_waiter && !rt_mutex_owner(orig_lock))
567                 goto out_unlock_pi;
568
569         /*
570          * We dropped all locks after taking a refcount on @task, so
571          * the task might have moved on in the lock chain or even left
572          * the chain completely and blocks now on an unrelated lock or
573          * on @orig_lock.
574          *
575          * We stored the lock on which @task was blocked in @next_lock,
576          * so we can detect the chain change.
577          */
578         if (next_lock != waiter->lock)
579                 goto out_unlock_pi;
580
581         /*
582          * Drop out, when the task has no waiters. Note,
583          * top_waiter can be NULL, when we are in the deboosting
584          * mode!
585          */
586         if (top_waiter) {
587                 if (!task_has_pi_waiters(task))
588                         goto out_unlock_pi;
589                 /*
590                  * If deadlock detection is off, we stop here if we
591                  * are not the top pi waiter of the task. If deadlock
592                  * detection is enabled we continue, but stop the
593                  * requeueing in the chain walk.
594                  */
595                 if (top_waiter != task_top_pi_waiter(task)) {
596                         if (!detect_deadlock)
597                                 goto out_unlock_pi;
598                         else
599                                 requeue = false;
600                 }
601         }
602
603         /*
604          * If the waiter priority is the same as the task priority
605          * then there is no further priority adjustment necessary.  If
606          * deadlock detection is off, we stop the chain walk. If its
607          * enabled we continue, but stop the requeueing in the chain
608          * walk.
609          */
610         if (waiter->prio == task->prio) {
611                 if (!detect_deadlock)
612                         goto out_unlock_pi;
613                 else
614                         requeue = false;
615         }
616
617         /*
618          * [4] Get the next lock
619          */
620         lock = waiter->lock;
621         /*
622          * [5] We need to trylock here as we are holding task->pi_lock,
623          * which is the reverse lock order versus the other rtmutex
624          * operations.
625          */
626         if (!raw_spin_trylock(&lock->wait_lock)) {
627                 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
628                 cpu_relax();
629                 goto retry;
630         }
631
632         /*
633          * [6] check_exit_conditions_2() protected by task->pi_lock and
634          * lock->wait_lock.
635          *
636          * Deadlock detection. If the lock is the same as the original
637          * lock which caused us to walk the lock chain or if the
638          * current lock is owned by the task which initiated the chain
639          * walk, we detected a deadlock.
640          */
641         if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
642                 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
643                 raw_spin_unlock(&lock->wait_lock);
644                 ret = -EDEADLK;
645                 goto out_unlock_pi;
646         }
647
648         /*
649          * If we just follow the lock chain for deadlock detection, no
650          * need to do all the requeue operations. To avoid a truckload
651          * of conditionals around the various places below, just do the
652          * minimum chain walk checks.
653          */
654         if (!requeue) {
655                 /*
656                  * No requeue[7] here. Just release @task [8]
657                  */
658                 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
659                 put_task_struct(task);
660
661                 /*
662                  * [9] check_exit_conditions_3 protected by lock->wait_lock.
663                  * If there is no owner of the lock, end of chain.
664                  */
665                 if (!rt_mutex_owner(lock)) {
666                         raw_spin_unlock(&lock->wait_lock);
667                         return 0;
668                 }
669
670                 /* [10] Grab the next task, i.e. owner of @lock */
671                 task = rt_mutex_owner(lock);
672                 get_task_struct(task);
673                 raw_spin_lock_irqsave(&task->pi_lock, flags);
674
675                 /*
676                  * No requeue [11] here. We just do deadlock detection.
677                  *
678                  * [12] Store whether owner is blocked
679                  * itself. Decision is made after dropping the locks
680                  */
681                 next_lock = task_blocked_on_lock(task);
682                 /*
683                  * Get the top waiter for the next iteration
684                  */
685                 top_waiter = rt_mutex_top_waiter(lock);
686
687                 /* [13] Drop locks */
688                 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
689                 raw_spin_unlock(&lock->wait_lock);
690
691                 /* If owner is not blocked, end of chain. */
692                 if (!next_lock)
693                         goto out_put_task;
694                 goto again;
695         }
696
697         /*
698          * Store the current top waiter before doing the requeue
699          * operation on @lock. We need it for the boost/deboost
700          * decision below.
701          */
702         prerequeue_top_waiter = rt_mutex_top_waiter(lock);
703
704         /* [7] Requeue the waiter in the lock waiter tree. */
705         rt_mutex_dequeue(lock, waiter);
706         waiter->prio = task->prio;
707         rt_mutex_enqueue(lock, waiter);
708
709         /* [8] Release the task */
710         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
711         put_task_struct(task);
712
713         /*
714          * [9] check_exit_conditions_3 protected by lock->wait_lock.
715          *
716          * We must abort the chain walk if there is no lock owner even
717          * in the dead lock detection case, as we have nothing to
718          * follow here. This is the end of the chain we are walking.
719          */
720         if (!rt_mutex_owner(lock)) {
721                 /*
722                  * If the requeue [7] above changed the top waiter,
723                  * then we need to wake the new top waiter up to try
724                  * to get the lock.
725                  */
726                 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
727                         wake_up_process(rt_mutex_top_waiter(lock)->task);
728                 raw_spin_unlock(&lock->wait_lock);
729                 return 0;
730         }
731
732         /* [10] Grab the next task, i.e. the owner of @lock */
733         task = rt_mutex_owner(lock);
734         get_task_struct(task);
735         raw_spin_lock_irqsave(&task->pi_lock, flags);
736
737         /* [11] requeue the pi waiters if necessary */
738         if (waiter == rt_mutex_top_waiter(lock)) {
739                 /*
740                  * The waiter became the new top (highest priority)
741                  * waiter on the lock. Replace the previous top waiter
742                  * in the owner tasks pi waiters tree with this waiter
743                  * and adjust the priority of the owner.
744                  */
745                 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
746                 rt_mutex_enqueue_pi(task, waiter);
747                 __rt_mutex_adjust_prio(task);
748
749         } else if (prerequeue_top_waiter == waiter) {
750                 /*
751                  * The waiter was the top waiter on the lock, but is
752                  * no longer the top prority waiter. Replace waiter in
753                  * the owner tasks pi waiters tree with the new top
754                  * (highest priority) waiter and adjust the priority
755                  * of the owner.
756                  * The new top waiter is stored in @waiter so that
757                  * @waiter == @top_waiter evaluates to true below and
758                  * we continue to deboost the rest of the chain.
759                  */
760                 rt_mutex_dequeue_pi(task, waiter);
761                 waiter = rt_mutex_top_waiter(lock);
762                 rt_mutex_enqueue_pi(task, waiter);
763                 __rt_mutex_adjust_prio(task);
764         } else {
765                 /*
766                  * Nothing changed. No need to do any priority
767                  * adjustment.
768                  */
769         }
770
771         /*
772          * [12] check_exit_conditions_4() protected by task->pi_lock
773          * and lock->wait_lock. The actual decisions are made after we
774          * dropped the locks.
775          *
776          * Check whether the task which owns the current lock is pi
777          * blocked itself. If yes we store a pointer to the lock for
778          * the lock chain change detection above. After we dropped
779          * task->pi_lock next_lock cannot be dereferenced anymore.
780          */
781         next_lock = task_blocked_on_lock(task);
782         /*
783          * Store the top waiter of @lock for the end of chain walk
784          * decision below.
785          */
786         top_waiter = rt_mutex_top_waiter(lock);
787
788         /* [13] Drop the locks */
789         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
790         raw_spin_unlock(&lock->wait_lock);
791
792         /*
793          * Make the actual exit decisions [12], based on the stored
794          * values.
795          *
796          * We reached the end of the lock chain. Stop right here. No
797          * point to go back just to figure that out.
798          */
799         if (!next_lock)
800                 goto out_put_task;
801
802         /*
803          * If the current waiter is not the top waiter on the lock,
804          * then we can stop the chain walk here if we are not in full
805          * deadlock detection mode.
806          */
807         if (!detect_deadlock && waiter != top_waiter)
808                 goto out_put_task;
809
810         goto again;
811
812  out_unlock_pi:
813         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
814  out_put_task:
815         put_task_struct(task);
816
817         return ret;
818 }
819
820 /*
821  * Try to take an rt-mutex
822  *
823  * Must be called with lock->wait_lock held.
824  *
825  * @lock:   The lock to be acquired.
826  * @task:   The task which wants to acquire the lock
827  * @waiter: The waiter that is queued to the lock's wait tree if the
828  *          callsite called task_blocked_on_lock(), otherwise NULL
829  */
830 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
831                                 struct rt_mutex_waiter *waiter)
832 {
833         unsigned long flags;
834
835         /*
836          * Before testing whether we can acquire @lock, we set the
837          * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
838          * other tasks which try to modify @lock into the slow path
839          * and they serialize on @lock->wait_lock.
840          *
841          * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
842          * as explained at the top of this file if and only if:
843          *
844          * - There is a lock owner. The caller must fixup the
845          *   transient state if it does a trylock or leaves the lock
846          *   function due to a signal or timeout.
847          *
848          * - @task acquires the lock and there are no other
849          *   waiters. This is undone in rt_mutex_set_owner(@task) at
850          *   the end of this function.
851          */
852         mark_rt_mutex_waiters(lock);
853
854         /*
855          * If @lock has an owner, give up.
856          */
857         if (rt_mutex_owner(lock))
858                 return 0;
859
860         /*
861          * If @waiter != NULL, @task has already enqueued the waiter
862          * into @lock waiter tree. If @waiter == NULL then this is a
863          * trylock attempt.
864          */
865         if (waiter) {
866                 /*
867                  * If waiter is not the highest priority waiter of
868                  * @lock, give up.
869                  */
870                 if (waiter != rt_mutex_top_waiter(lock))
871                         return 0;
872
873                 /*
874                  * We can acquire the lock. Remove the waiter from the
875                  * lock waiters tree.
876                  */
877                 rt_mutex_dequeue(lock, waiter);
878
879         } else {
880                 /*
881                  * If the lock has waiters already we check whether @task is
882                  * eligible to take over the lock.
883                  *
884                  * If there are no other waiters, @task can acquire
885                  * the lock.  @task->pi_blocked_on is NULL, so it does
886                  * not need to be dequeued.
887                  */
888                 if (rt_mutex_has_waiters(lock)) {
889                         /*
890                          * If @task->prio is greater than or equal to
891                          * the top waiter priority (kernel view),
892                          * @task lost.
893                          */
894                         if (task->prio >= rt_mutex_top_waiter(lock)->prio)
895                                 return 0;
896
897                         /*
898                          * The current top waiter stays enqueued. We
899                          * don't have to change anything in the lock
900                          * waiters order.
901                          */
902                 } else {
903                         /*
904                          * No waiters. Take the lock without the
905                          * pi_lock dance.@task->pi_blocked_on is NULL
906                          * and we have no waiters to enqueue in @task
907                          * pi waiters tree.
908                          */
909                         goto takeit;
910                 }
911         }
912
913         /*
914          * Clear @task->pi_blocked_on. Requires protection by
915          * @task->pi_lock. Redundant operation for the @waiter == NULL
916          * case, but conditionals are more expensive than a redundant
917          * store.
918          */
919         raw_spin_lock_irqsave(&task->pi_lock, flags);
920         task->pi_blocked_on = NULL;
921         /*
922          * Finish the lock acquisition. @task is the new owner. If
923          * other waiters exist we have to insert the highest priority
924          * waiter into @task->pi_waiters tree.
925          */
926         if (rt_mutex_has_waiters(lock))
927                 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
928         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
929
930 takeit:
931         /* We got the lock. */
932         debug_rt_mutex_lock(lock);
933
934         /*
935          * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
936          * are still waiters or clears it.
937          */
938         rt_mutex_set_owner(lock, task);
939
940         rt_mutex_deadlock_account_lock(lock, task);
941
942         return 1;
943 }
944
945 /*
946  * Task blocks on lock.
947  *
948  * Prepare waiter and propagate pi chain
949  *
950  * This must be called with lock->wait_lock held.
951  */
952 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
953                                    struct rt_mutex_waiter *waiter,
954                                    struct task_struct *task,
955                                    enum rtmutex_chainwalk chwalk)
956 {
957         struct task_struct *owner = rt_mutex_owner(lock);
958         struct rt_mutex_waiter *top_waiter = waiter;
959         struct rt_mutex *next_lock;
960         int chain_walk = 0, res;
961         unsigned long flags;
962
963         /*
964          * Early deadlock detection. We really don't want the task to
965          * enqueue on itself just to untangle the mess later. It's not
966          * only an optimization. We drop the locks, so another waiter
967          * can come in before the chain walk detects the deadlock. So
968          * the other will detect the deadlock and return -EDEADLOCK,
969          * which is wrong, as the other waiter is not in a deadlock
970          * situation.
971          */
972         if (owner == task)
973                 return -EDEADLK;
974
975         raw_spin_lock_irqsave(&task->pi_lock, flags);
976         __rt_mutex_adjust_prio(task);
977         waiter->task = task;
978         waiter->lock = lock;
979         waiter->prio = task->prio;
980
981         /* Get the top priority waiter on the lock */
982         if (rt_mutex_has_waiters(lock))
983                 top_waiter = rt_mutex_top_waiter(lock);
984         rt_mutex_enqueue(lock, waiter);
985
986         task->pi_blocked_on = waiter;
987
988         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
989
990         if (!owner)
991                 return 0;
992
993         raw_spin_lock_irqsave(&owner->pi_lock, flags);
994         if (waiter == rt_mutex_top_waiter(lock)) {
995                 rt_mutex_dequeue_pi(owner, top_waiter);
996                 rt_mutex_enqueue_pi(owner, waiter);
997
998                 __rt_mutex_adjust_prio(owner);
999                 if (owner->pi_blocked_on)
1000                         chain_walk = 1;
1001         } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
1002                 chain_walk = 1;
1003         }
1004
1005         /* Store the lock on which owner is blocked or NULL */
1006         next_lock = task_blocked_on_lock(owner);
1007
1008         raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
1009         /*
1010          * Even if full deadlock detection is on, if the owner is not
1011          * blocked itself, we can avoid finding this out in the chain
1012          * walk.
1013          */
1014         if (!chain_walk || !next_lock)
1015                 return 0;
1016
1017         /*
1018          * The owner can't disappear while holding a lock,
1019          * so the owner struct is protected by wait_lock.
1020          * Gets dropped in rt_mutex_adjust_prio_chain()!
1021          */
1022         get_task_struct(owner);
1023
1024         raw_spin_unlock(&lock->wait_lock);
1025
1026         res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
1027                                          next_lock, waiter, task);
1028
1029         raw_spin_lock(&lock->wait_lock);
1030
1031         return res;
1032 }
1033
1034 /*
1035  * Remove the top waiter from the current tasks pi waiter tree and
1036  * queue it up.
1037  *
1038  * Called with lock->wait_lock held.
1039  */
1040 static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1041                                     struct rt_mutex *lock)
1042 {
1043         struct rt_mutex_waiter *waiter;
1044         unsigned long flags;
1045
1046         raw_spin_lock_irqsave(&current->pi_lock, flags);
1047
1048         waiter = rt_mutex_top_waiter(lock);
1049
1050         /*
1051          * Remove it from current->pi_waiters. We do not adjust a
1052          * possible priority boost right now. We execute wakeup in the
1053          * boosted mode and go back to normal after releasing
1054          * lock->wait_lock.
1055          */
1056         rt_mutex_dequeue_pi(current, waiter);
1057
1058         /*
1059          * As we are waking up the top waiter, and the waiter stays
1060          * queued on the lock until it gets the lock, this lock
1061          * obviously has waiters. Just set the bit here and this has
1062          * the added benefit of forcing all new tasks into the
1063          * slow path making sure no task of lower priority than
1064          * the top waiter can steal this lock.
1065          */
1066         lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
1067
1068         raw_spin_unlock_irqrestore(&current->pi_lock, flags);
1069
1070         wake_q_add(wake_q, waiter->task);
1071 }
1072
1073 /*
1074  * Remove a waiter from a lock and give up
1075  *
1076  * Must be called with lock->wait_lock held and
1077  * have just failed to try_to_take_rt_mutex().
1078  */
1079 static void remove_waiter(struct rt_mutex *lock,
1080                           struct rt_mutex_waiter *waiter)
1081 {
1082         bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
1083         struct task_struct *owner = rt_mutex_owner(lock);
1084         struct rt_mutex *next_lock;
1085         unsigned long flags;
1086
1087         raw_spin_lock_irqsave(&current->pi_lock, flags);
1088         rt_mutex_dequeue(lock, waiter);
1089         current->pi_blocked_on = NULL;
1090         raw_spin_unlock_irqrestore(&current->pi_lock, flags);
1091
1092         /*
1093          * Only update priority if the waiter was the highest priority
1094          * waiter of the lock and there is an owner to update.
1095          */
1096         if (!owner || !is_top_waiter)
1097                 return;
1098
1099         raw_spin_lock_irqsave(&owner->pi_lock, flags);
1100
1101         rt_mutex_dequeue_pi(owner, waiter);
1102
1103         if (rt_mutex_has_waiters(lock))
1104                 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
1105
1106         __rt_mutex_adjust_prio(owner);
1107
1108         /* Store the lock on which owner is blocked or NULL */
1109         next_lock = task_blocked_on_lock(owner);
1110
1111         raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
1112
1113         /*
1114          * Don't walk the chain, if the owner task is not blocked
1115          * itself.
1116          */
1117         if (!next_lock)
1118                 return;
1119
1120         /* gets dropped in rt_mutex_adjust_prio_chain()! */
1121         get_task_struct(owner);
1122
1123         raw_spin_unlock(&lock->wait_lock);
1124
1125         rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1126                                    next_lock, NULL, current);
1127
1128         raw_spin_lock(&lock->wait_lock);
1129 }
1130
1131 /*
1132  * Recheck the pi chain, in case we got a priority setting
1133  *
1134  * Called from sched_setscheduler
1135  */
1136 void rt_mutex_adjust_pi(struct task_struct *task)
1137 {
1138         struct rt_mutex_waiter *waiter;
1139         struct rt_mutex *next_lock;
1140         unsigned long flags;
1141
1142         raw_spin_lock_irqsave(&task->pi_lock, flags);
1143
1144         waiter = task->pi_blocked_on;
1145         if (!waiter || (waiter->prio == task->prio &&
1146                         !dl_prio(task->prio))) {
1147                 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1148                 return;
1149         }
1150         next_lock = waiter->lock;
1151         raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1152
1153         /* gets dropped in rt_mutex_adjust_prio_chain()! */
1154         get_task_struct(task);
1155
1156         rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1157                                    next_lock, NULL, task);
1158 }
1159
1160 /**
1161  * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1162  * @lock:                the rt_mutex to take
1163  * @state:               the state the task should block in (TASK_INTERRUPTIBLE
1164  *                       or TASK_UNINTERRUPTIBLE)
1165  * @timeout:             the pre-initialized and started timer, or NULL for none
1166  * @waiter:              the pre-initialized rt_mutex_waiter
1167  *
1168  * lock->wait_lock must be held by the caller.
1169  */
1170 static int __sched
1171 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
1172                     struct hrtimer_sleeper *timeout,
1173                     struct rt_mutex_waiter *waiter)
1174 {
1175         int ret = 0;
1176
1177         for (;;) {
1178                 /* Try to acquire the lock: */
1179                 if (try_to_take_rt_mutex(lock, current, waiter))
1180                         break;
1181
1182                 /*
1183                  * TASK_INTERRUPTIBLE checks for signals and
1184                  * timeout. Ignored otherwise.
1185                  */
1186                 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1187                         /* Signal pending? */
1188                         if (signal_pending(current))
1189                                 ret = -EINTR;
1190                         if (timeout && !timeout->task)
1191                                 ret = -ETIMEDOUT;
1192                         if (ret)
1193                                 break;
1194                 }
1195
1196                 raw_spin_unlock(&lock->wait_lock);
1197
1198                 debug_rt_mutex_print_deadlock(waiter);
1199
1200                 schedule();
1201
1202                 raw_spin_lock(&lock->wait_lock);
1203                 set_current_state(state);
1204         }
1205
1206         __set_current_state(TASK_RUNNING);
1207         return ret;
1208 }
1209
1210 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1211                                      struct rt_mutex_waiter *w)
1212 {
1213         /*
1214          * If the result is not -EDEADLOCK or the caller requested
1215          * deadlock detection, nothing to do here.
1216          */
1217         if (res != -EDEADLOCK || detect_deadlock)
1218                 return;
1219
1220         /*
1221          * Yell lowdly and stop the task right here.
1222          */
1223         rt_mutex_print_deadlock(w);
1224         while (1) {
1225                 set_current_state(TASK_INTERRUPTIBLE);
1226                 schedule();
1227         }
1228 }
1229
1230 /*
1231  * Slow path lock function:
1232  */
1233 static int __sched
1234 rt_mutex_slowlock(struct rt_mutex *lock, int state,
1235                   struct hrtimer_sleeper *timeout,
1236                   enum rtmutex_chainwalk chwalk)
1237 {
1238         struct rt_mutex_waiter waiter;
1239         int ret = 0;
1240
1241         debug_rt_mutex_init_waiter(&waiter);
1242         RB_CLEAR_NODE(&waiter.pi_tree_entry);
1243         RB_CLEAR_NODE(&waiter.tree_entry);
1244
1245         raw_spin_lock(&lock->wait_lock);
1246
1247         /* Try to acquire the lock again: */
1248         if (try_to_take_rt_mutex(lock, current, NULL)) {
1249                 raw_spin_unlock(&lock->wait_lock);
1250                 return 0;
1251         }
1252
1253         set_current_state(state);
1254
1255         /* Setup the timer, when timeout != NULL */
1256         if (unlikely(timeout))
1257                 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1258
1259         ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
1260
1261         if (likely(!ret))
1262                 /* sleep on the mutex */
1263                 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1264
1265         if (unlikely(ret)) {
1266                 __set_current_state(TASK_RUNNING);
1267                 if (rt_mutex_has_waiters(lock))
1268                         remove_waiter(lock, &waiter);
1269                 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
1270         }
1271
1272         /*
1273          * try_to_take_rt_mutex() sets the waiter bit
1274          * unconditionally. We might have to fix that up.
1275          */
1276         fixup_rt_mutex_waiters(lock);
1277
1278         raw_spin_unlock(&lock->wait_lock);
1279
1280         /* Remove pending timer: */
1281         if (unlikely(timeout))
1282                 hrtimer_cancel(&timeout->timer);
1283
1284         debug_rt_mutex_free_waiter(&waiter);
1285
1286         return ret;
1287 }
1288
1289 /*
1290  * Slow path try-lock function:
1291  */
1292 static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
1293 {
1294         int ret;
1295
1296         /*
1297          * If the lock already has an owner we fail to get the lock.
1298          * This can be done without taking the @lock->wait_lock as
1299          * it is only being read, and this is a trylock anyway.
1300          */
1301         if (rt_mutex_owner(lock))
1302                 return 0;
1303
1304         /*
1305          * The mutex has currently no owner. Lock the wait lock and
1306          * try to acquire the lock.
1307          */
1308         raw_spin_lock(&lock->wait_lock);
1309
1310         ret = try_to_take_rt_mutex(lock, current, NULL);
1311
1312         /*
1313          * try_to_take_rt_mutex() sets the lock waiters bit
1314          * unconditionally. Clean this up.
1315          */
1316         fixup_rt_mutex_waiters(lock);
1317
1318         raw_spin_unlock(&lock->wait_lock);
1319
1320         return ret;
1321 }
1322
1323 /*
1324  * Slow path to release a rt-mutex.
1325  * Return whether the current task needs to undo a potential priority boosting.
1326  */
1327 static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1328                                         struct wake_q_head *wake_q)
1329 {
1330         raw_spin_lock(&lock->wait_lock);
1331
1332         debug_rt_mutex_unlock(lock);
1333
1334         rt_mutex_deadlock_account_unlock(current);
1335
1336         /*
1337          * We must be careful here if the fast path is enabled. If we
1338          * have no waiters queued we cannot set owner to NULL here
1339          * because of:
1340          *
1341          * foo->lock->owner = NULL;
1342          *                      rtmutex_lock(foo->lock);   <- fast path
1343          *                      free = atomic_dec_and_test(foo->refcnt);
1344          *                      rtmutex_unlock(foo->lock); <- fast path
1345          *                      if (free)
1346          *                              kfree(foo);
1347          * raw_spin_unlock(foo->lock->wait_lock);
1348          *
1349          * So for the fastpath enabled kernel:
1350          *
1351          * Nothing can set the waiters bit as long as we hold
1352          * lock->wait_lock. So we do the following sequence:
1353          *
1354          *      owner = rt_mutex_owner(lock);
1355          *      clear_rt_mutex_waiters(lock);
1356          *      raw_spin_unlock(&lock->wait_lock);
1357          *      if (cmpxchg(&lock->owner, owner, 0) == owner)
1358          *              return;
1359          *      goto retry;
1360          *
1361          * The fastpath disabled variant is simple as all access to
1362          * lock->owner is serialized by lock->wait_lock:
1363          *
1364          *      lock->owner = NULL;
1365          *      raw_spin_unlock(&lock->wait_lock);
1366          */
1367         while (!rt_mutex_has_waiters(lock)) {
1368                 /* Drops lock->wait_lock ! */
1369                 if (unlock_rt_mutex_safe(lock) == true)
1370                         return false;
1371                 /* Relock the rtmutex and try again */
1372                 raw_spin_lock(&lock->wait_lock);
1373         }
1374
1375         /*
1376          * The wakeup next waiter path does not suffer from the above
1377          * race. See the comments there.
1378          *
1379          * Queue the next waiter for wakeup once we release the wait_lock.
1380          */
1381         mark_wakeup_next_waiter(wake_q, lock);
1382
1383         raw_spin_unlock(&lock->wait_lock);
1384
1385         /* check PI boosting */
1386         return true;
1387 }
1388
1389 /*
1390  * debug aware fast / slowpath lock,trylock,unlock
1391  *
1392  * The atomic acquire/release ops are compiled away, when either the
1393  * architecture does not support cmpxchg or when debugging is enabled.
1394  */
1395 static inline int
1396 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1397                   int (*slowfn)(struct rt_mutex *lock, int state,
1398                                 struct hrtimer_sleeper *timeout,
1399                                 enum rtmutex_chainwalk chwalk))
1400 {
1401         if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
1402                 rt_mutex_deadlock_account_lock(lock, current);
1403                 return 0;
1404         } else
1405                 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
1406 }
1407
1408 static inline int
1409 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1410                         struct hrtimer_sleeper *timeout,
1411                         enum rtmutex_chainwalk chwalk,
1412                         int (*slowfn)(struct rt_mutex *lock, int state,
1413                                       struct hrtimer_sleeper *timeout,
1414                                       enum rtmutex_chainwalk chwalk))
1415 {
1416         if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1417             likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
1418                 rt_mutex_deadlock_account_lock(lock, current);
1419                 return 0;
1420         } else
1421                 return slowfn(lock, state, timeout, chwalk);
1422 }
1423
1424 static inline int
1425 rt_mutex_fasttrylock(struct rt_mutex *lock,
1426                      int (*slowfn)(struct rt_mutex *lock))
1427 {
1428         if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
1429                 rt_mutex_deadlock_account_lock(lock, current);
1430                 return 1;
1431         }
1432         return slowfn(lock);
1433 }
1434
1435 static inline void
1436 rt_mutex_fastunlock(struct rt_mutex *lock,
1437                     bool (*slowfn)(struct rt_mutex *lock,
1438                                    struct wake_q_head *wqh))
1439 {
1440         WAKE_Q(wake_q);
1441
1442         if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
1443                 rt_mutex_deadlock_account_unlock(current);
1444
1445         } else {
1446                 bool deboost = slowfn(lock, &wake_q);
1447
1448                 wake_up_q(&wake_q);
1449
1450                 /* Undo pi boosting if necessary: */
1451                 if (deboost)
1452                         rt_mutex_adjust_prio(current);
1453         }
1454 }
1455
1456 /**
1457  * rt_mutex_lock - lock a rt_mutex
1458  *
1459  * @lock: the rt_mutex to be locked
1460  */
1461 void __sched rt_mutex_lock(struct rt_mutex *lock)
1462 {
1463         might_sleep();
1464
1465         rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1466 }
1467 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1468
1469 /**
1470  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1471  *
1472  * @lock:               the rt_mutex to be locked
1473  *
1474  * Returns:
1475  *  0           on success
1476  * -EINTR       when interrupted by a signal
1477  */
1478 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
1479 {
1480         might_sleep();
1481
1482         return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1483 }
1484 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1485
1486 /*
1487  * Futex variant with full deadlock detection.
1488  */
1489 int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1490                               struct hrtimer_sleeper *timeout)
1491 {
1492         might_sleep();
1493
1494         return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1495                                        RT_MUTEX_FULL_CHAINWALK,
1496                                        rt_mutex_slowlock);
1497 }
1498
1499 /**
1500  * rt_mutex_timed_lock - lock a rt_mutex interruptible
1501  *                      the timeout structure is provided
1502  *                      by the caller
1503  *
1504  * @lock:               the rt_mutex to be locked
1505  * @timeout:            timeout structure or NULL (no timeout)
1506  *
1507  * Returns:
1508  *  0           on success
1509  * -EINTR       when interrupted by a signal
1510  * -ETIMEDOUT   when the timeout expired
1511  */
1512 int
1513 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
1514 {
1515         might_sleep();
1516
1517         return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1518                                        RT_MUTEX_MIN_CHAINWALK,
1519                                        rt_mutex_slowlock);
1520 }
1521 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1522
1523 /**
1524  * rt_mutex_trylock - try to lock a rt_mutex
1525  *
1526  * @lock:       the rt_mutex to be locked
1527  *
1528  * This function can only be called in thread context. It's safe to
1529  * call it from atomic regions, but not from hard interrupt or soft
1530  * interrupt context.
1531  *
1532  * Returns 1 on success and 0 on contention
1533  */
1534 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1535 {
1536         if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
1537                 return 0;
1538
1539         return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1540 }
1541 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1542
1543 /**
1544  * rt_mutex_unlock - unlock a rt_mutex
1545  *
1546  * @lock: the rt_mutex to be unlocked
1547  */
1548 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1549 {
1550         rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1551 }
1552 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1553
1554 /**
1555  * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1556  * @lock: the rt_mutex to be unlocked
1557  *
1558  * Returns: true/false indicating whether priority adjustment is
1559  * required or not.
1560  */
1561 bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1562                                    struct wake_q_head *wqh)
1563 {
1564         if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
1565                 rt_mutex_deadlock_account_unlock(current);
1566                 return false;
1567         }
1568         return rt_mutex_slowunlock(lock, wqh);
1569 }
1570
1571 /**
1572  * rt_mutex_destroy - mark a mutex unusable
1573  * @lock: the mutex to be destroyed
1574  *
1575  * This function marks the mutex uninitialized, and any subsequent
1576  * use of the mutex is forbidden. The mutex must not be locked when
1577  * this function is called.
1578  */
1579 void rt_mutex_destroy(struct rt_mutex *lock)
1580 {
1581         WARN_ON(rt_mutex_is_locked(lock));
1582 #ifdef CONFIG_DEBUG_RT_MUTEXES
1583         lock->magic = NULL;
1584 #endif
1585 }
1586
1587 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1588
1589 /**
1590  * __rt_mutex_init - initialize the rt lock
1591  *
1592  * @lock: the rt lock to be initialized
1593  *
1594  * Initialize the rt lock to unlocked state.
1595  *
1596  * Initializing of a locked rt lock is not allowed
1597  */
1598 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1599 {
1600         lock->owner = NULL;
1601         raw_spin_lock_init(&lock->wait_lock);
1602         lock->waiters = RB_ROOT;
1603         lock->waiters_leftmost = NULL;
1604
1605         debug_rt_mutex_init(lock, name);
1606 }
1607 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1608
1609 /**
1610  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1611  *                              proxy owner
1612  *
1613  * @lock:       the rt_mutex to be locked
1614  * @proxy_owner:the task to set as owner
1615  *
1616  * No locking. Caller has to do serializing itself
1617  * Special API call for PI-futex support
1618  */
1619 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1620                                 struct task_struct *proxy_owner)
1621 {
1622         __rt_mutex_init(lock, NULL);
1623         debug_rt_mutex_proxy_lock(lock, proxy_owner);
1624         rt_mutex_set_owner(lock, proxy_owner);
1625         rt_mutex_deadlock_account_lock(lock, proxy_owner);
1626 }
1627
1628 /**
1629  * rt_mutex_proxy_unlock - release a lock on behalf of owner
1630  *
1631  * @lock:       the rt_mutex to be locked
1632  *
1633  * No locking. Caller has to do serializing itself
1634  * Special API call for PI-futex support
1635  */
1636 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1637                            struct task_struct *proxy_owner)
1638 {
1639         debug_rt_mutex_proxy_unlock(lock);
1640         rt_mutex_set_owner(lock, NULL);
1641         rt_mutex_deadlock_account_unlock(proxy_owner);
1642 }
1643
1644 /**
1645  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1646  * @lock:               the rt_mutex to take
1647  * @waiter:             the pre-initialized rt_mutex_waiter
1648  * @task:               the task to prepare
1649  *
1650  * Returns:
1651  *  0 - task blocked on lock
1652  *  1 - acquired the lock for task, caller should wake it up
1653  * <0 - error
1654  *
1655  * Special API call for FUTEX_REQUEUE_PI support.
1656  */
1657 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1658                               struct rt_mutex_waiter *waiter,
1659                               struct task_struct *task)
1660 {
1661         int ret;
1662
1663         raw_spin_lock(&lock->wait_lock);
1664
1665         if (try_to_take_rt_mutex(lock, task, NULL)) {
1666                 raw_spin_unlock(&lock->wait_lock);
1667                 return 1;
1668         }
1669
1670         /* We enforce deadlock detection for futexes */
1671         ret = task_blocks_on_rt_mutex(lock, waiter, task,
1672                                       RT_MUTEX_FULL_CHAINWALK);
1673
1674         if (ret && !rt_mutex_owner(lock)) {
1675                 /*
1676                  * Reset the return value. We might have
1677                  * returned with -EDEADLK and the owner
1678                  * released the lock while we were walking the
1679                  * pi chain.  Let the waiter sort it out.
1680                  */
1681                 ret = 0;
1682         }
1683
1684         if (unlikely(ret))
1685                 remove_waiter(lock, waiter);
1686
1687         raw_spin_unlock(&lock->wait_lock);
1688
1689         debug_rt_mutex_print_deadlock(waiter);
1690
1691         return ret;
1692 }
1693
1694 /**
1695  * rt_mutex_next_owner - return the next owner of the lock
1696  *
1697  * @lock: the rt lock query
1698  *
1699  * Returns the next owner of the lock or NULL
1700  *
1701  * Caller has to serialize against other accessors to the lock
1702  * itself.
1703  *
1704  * Special API call for PI-futex support
1705  */
1706 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1707 {
1708         if (!rt_mutex_has_waiters(lock))
1709                 return NULL;
1710
1711         return rt_mutex_top_waiter(lock)->task;
1712 }
1713
1714 /**
1715  * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1716  * @lock:               the rt_mutex we were woken on
1717  * @to:                 the timeout, null if none. hrtimer should already have
1718  *                      been started.
1719  * @waiter:             the pre-initialized rt_mutex_waiter
1720  *
1721  * Complete the lock acquisition started our behalf by another thread.
1722  *
1723  * Returns:
1724  *  0 - success
1725  * <0 - error, one of -EINTR, -ETIMEDOUT
1726  *
1727  * Special API call for PI-futex requeue support
1728  */
1729 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1730                                struct hrtimer_sleeper *to,
1731                                struct rt_mutex_waiter *waiter)
1732 {
1733         int ret;
1734
1735         raw_spin_lock(&lock->wait_lock);
1736
1737         set_current_state(TASK_INTERRUPTIBLE);
1738
1739         /* sleep on the mutex */
1740         ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1741
1742         if (unlikely(ret))
1743                 remove_waiter(lock, waiter);
1744
1745         /*
1746          * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1747          * have to fix that up.
1748          */
1749         fixup_rt_mutex_waiters(lock);
1750
1751         raw_spin_unlock(&lock->wait_lock);
1752
1753         return ret;
1754 }