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