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