rtmutex: Detect changes in the pi lock chain
[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 deadlock_detect ? -EDEADLK : 0;
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 = deadlock_detect ? -EDEADLK : 0;
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 (detect_deadlock && 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 /*
802  * Slow path lock function:
803  */
804 static int __sched
805 rt_mutex_slowlock(struct rt_mutex *lock, int state,
806                   struct hrtimer_sleeper *timeout,
807                   int detect_deadlock)
808 {
809         struct rt_mutex_waiter waiter;
810         int ret = 0;
811
812         debug_rt_mutex_init_waiter(&waiter);
813         RB_CLEAR_NODE(&waiter.pi_tree_entry);
814         RB_CLEAR_NODE(&waiter.tree_entry);
815
816         raw_spin_lock(&lock->wait_lock);
817
818         /* Try to acquire the lock again: */
819         if (try_to_take_rt_mutex(lock, current, NULL)) {
820                 raw_spin_unlock(&lock->wait_lock);
821                 return 0;
822         }
823
824         set_current_state(state);
825
826         /* Setup the timer, when timeout != NULL */
827         if (unlikely(timeout)) {
828                 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
829                 if (!hrtimer_active(&timeout->timer))
830                         timeout->task = NULL;
831         }
832
833         ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
834
835         if (likely(!ret))
836                 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
837
838         set_current_state(TASK_RUNNING);
839
840         if (unlikely(ret))
841                 remove_waiter(lock, &waiter);
842
843         /*
844          * try_to_take_rt_mutex() sets the waiter bit
845          * unconditionally. We might have to fix that up.
846          */
847         fixup_rt_mutex_waiters(lock);
848
849         raw_spin_unlock(&lock->wait_lock);
850
851         /* Remove pending timer: */
852         if (unlikely(timeout))
853                 hrtimer_cancel(&timeout->timer);
854
855         debug_rt_mutex_free_waiter(&waiter);
856
857         return ret;
858 }
859
860 /*
861  * Slow path try-lock function:
862  */
863 static inline int
864 rt_mutex_slowtrylock(struct rt_mutex *lock)
865 {
866         int ret = 0;
867
868         raw_spin_lock(&lock->wait_lock);
869
870         if (likely(rt_mutex_owner(lock) != current)) {
871
872                 ret = try_to_take_rt_mutex(lock, current, NULL);
873                 /*
874                  * try_to_take_rt_mutex() sets the lock waiters
875                  * bit unconditionally. Clean this up.
876                  */
877                 fixup_rt_mutex_waiters(lock);
878         }
879
880         raw_spin_unlock(&lock->wait_lock);
881
882         return ret;
883 }
884
885 /*
886  * Slow path to release a rt-mutex:
887  */
888 static void __sched
889 rt_mutex_slowunlock(struct rt_mutex *lock)
890 {
891         raw_spin_lock(&lock->wait_lock);
892
893         debug_rt_mutex_unlock(lock);
894
895         rt_mutex_deadlock_account_unlock(current);
896
897         if (!rt_mutex_has_waiters(lock)) {
898                 lock->owner = NULL;
899                 raw_spin_unlock(&lock->wait_lock);
900                 return;
901         }
902
903         wakeup_next_waiter(lock);
904
905         raw_spin_unlock(&lock->wait_lock);
906
907         /* Undo pi boosting if necessary: */
908         rt_mutex_adjust_prio(current);
909 }
910
911 /*
912  * debug aware fast / slowpath lock,trylock,unlock
913  *
914  * The atomic acquire/release ops are compiled away, when either the
915  * architecture does not support cmpxchg or when debugging is enabled.
916  */
917 static inline int
918 rt_mutex_fastlock(struct rt_mutex *lock, int state,
919                   int detect_deadlock,
920                   int (*slowfn)(struct rt_mutex *lock, int state,
921                                 struct hrtimer_sleeper *timeout,
922                                 int detect_deadlock))
923 {
924         if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
925                 rt_mutex_deadlock_account_lock(lock, current);
926                 return 0;
927         } else
928                 return slowfn(lock, state, NULL, detect_deadlock);
929 }
930
931 static inline int
932 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
933                         struct hrtimer_sleeper *timeout, int detect_deadlock,
934                         int (*slowfn)(struct rt_mutex *lock, int state,
935                                       struct hrtimer_sleeper *timeout,
936                                       int detect_deadlock))
937 {
938         if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
939                 rt_mutex_deadlock_account_lock(lock, current);
940                 return 0;
941         } else
942                 return slowfn(lock, state, timeout, detect_deadlock);
943 }
944
945 static inline int
946 rt_mutex_fasttrylock(struct rt_mutex *lock,
947                      int (*slowfn)(struct rt_mutex *lock))
948 {
949         if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
950                 rt_mutex_deadlock_account_lock(lock, current);
951                 return 1;
952         }
953         return slowfn(lock);
954 }
955
956 static inline void
957 rt_mutex_fastunlock(struct rt_mutex *lock,
958                     void (*slowfn)(struct rt_mutex *lock))
959 {
960         if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
961                 rt_mutex_deadlock_account_unlock(current);
962         else
963                 slowfn(lock);
964 }
965
966 /**
967  * rt_mutex_lock - lock a rt_mutex
968  *
969  * @lock: the rt_mutex to be locked
970  */
971 void __sched rt_mutex_lock(struct rt_mutex *lock)
972 {
973         might_sleep();
974
975         rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
976 }
977 EXPORT_SYMBOL_GPL(rt_mutex_lock);
978
979 /**
980  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
981  *
982  * @lock:               the rt_mutex to be locked
983  * @detect_deadlock:    deadlock detection on/off
984  *
985  * Returns:
986  *  0           on success
987  * -EINTR       when interrupted by a signal
988  * -EDEADLK     when the lock would deadlock (when deadlock detection is on)
989  */
990 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
991                                                  int detect_deadlock)
992 {
993         might_sleep();
994
995         return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
996                                  detect_deadlock, rt_mutex_slowlock);
997 }
998 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
999
1000 /**
1001  * rt_mutex_timed_lock - lock a rt_mutex interruptible
1002  *                      the timeout structure is provided
1003  *                      by the caller
1004  *
1005  * @lock:               the rt_mutex to be locked
1006  * @timeout:            timeout structure or NULL (no timeout)
1007  * @detect_deadlock:    deadlock detection on/off
1008  *
1009  * Returns:
1010  *  0           on success
1011  * -EINTR       when interrupted by a signal
1012  * -ETIMEDOUT   when the timeout expired
1013  * -EDEADLK     when the lock would deadlock (when deadlock detection is on)
1014  */
1015 int
1016 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1017                     int detect_deadlock)
1018 {
1019         might_sleep();
1020
1021         return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1022                                        detect_deadlock, rt_mutex_slowlock);
1023 }
1024 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1025
1026 /**
1027  * rt_mutex_trylock - try to lock a rt_mutex
1028  *
1029  * @lock:       the rt_mutex to be locked
1030  *
1031  * Returns 1 on success and 0 on contention
1032  */
1033 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1034 {
1035         return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1036 }
1037 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1038
1039 /**
1040  * rt_mutex_unlock - unlock a rt_mutex
1041  *
1042  * @lock: the rt_mutex to be unlocked
1043  */
1044 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1045 {
1046         rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1047 }
1048 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1049
1050 /**
1051  * rt_mutex_destroy - mark a mutex unusable
1052  * @lock: the mutex to be destroyed
1053  *
1054  * This function marks the mutex uninitialized, and any subsequent
1055  * use of the mutex is forbidden. The mutex must not be locked when
1056  * this function is called.
1057  */
1058 void rt_mutex_destroy(struct rt_mutex *lock)
1059 {
1060         WARN_ON(rt_mutex_is_locked(lock));
1061 #ifdef CONFIG_DEBUG_RT_MUTEXES
1062         lock->magic = NULL;
1063 #endif
1064 }
1065
1066 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1067
1068 /**
1069  * __rt_mutex_init - initialize the rt lock
1070  *
1071  * @lock: the rt lock to be initialized
1072  *
1073  * Initialize the rt lock to unlocked state.
1074  *
1075  * Initializing of a locked rt lock is not allowed
1076  */
1077 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1078 {
1079         lock->owner = NULL;
1080         raw_spin_lock_init(&lock->wait_lock);
1081         lock->waiters = RB_ROOT;
1082         lock->waiters_leftmost = NULL;
1083
1084         debug_rt_mutex_init(lock, name);
1085 }
1086 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1087
1088 /**
1089  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1090  *                              proxy owner
1091  *
1092  * @lock:       the rt_mutex to be locked
1093  * @proxy_owner:the task to set as owner
1094  *
1095  * No locking. Caller has to do serializing itself
1096  * Special API call for PI-futex support
1097  */
1098 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1099                                 struct task_struct *proxy_owner)
1100 {
1101         __rt_mutex_init(lock, NULL);
1102         debug_rt_mutex_proxy_lock(lock, proxy_owner);
1103         rt_mutex_set_owner(lock, proxy_owner);
1104         rt_mutex_deadlock_account_lock(lock, proxy_owner);
1105 }
1106
1107 /**
1108  * rt_mutex_proxy_unlock - release a lock on behalf of owner
1109  *
1110  * @lock:       the rt_mutex to be locked
1111  *
1112  * No locking. Caller has to do serializing itself
1113  * Special API call for PI-futex support
1114  */
1115 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1116                            struct task_struct *proxy_owner)
1117 {
1118         debug_rt_mutex_proxy_unlock(lock);
1119         rt_mutex_set_owner(lock, NULL);
1120         rt_mutex_deadlock_account_unlock(proxy_owner);
1121 }
1122
1123 /**
1124  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1125  * @lock:               the rt_mutex to take
1126  * @waiter:             the pre-initialized rt_mutex_waiter
1127  * @task:               the task to prepare
1128  * @detect_deadlock:    perform deadlock detection (1) or not (0)
1129  *
1130  * Returns:
1131  *  0 - task blocked on lock
1132  *  1 - acquired the lock for task, caller should wake it up
1133  * <0 - error
1134  *
1135  * Special API call for FUTEX_REQUEUE_PI support.
1136  */
1137 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1138                               struct rt_mutex_waiter *waiter,
1139                               struct task_struct *task, int detect_deadlock)
1140 {
1141         int ret;
1142
1143         raw_spin_lock(&lock->wait_lock);
1144
1145         if (try_to_take_rt_mutex(lock, task, NULL)) {
1146                 raw_spin_unlock(&lock->wait_lock);
1147                 return 1;
1148         }
1149
1150         ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
1151
1152         if (ret && !rt_mutex_owner(lock)) {
1153                 /*
1154                  * Reset the return value. We might have
1155                  * returned with -EDEADLK and the owner
1156                  * released the lock while we were walking the
1157                  * pi chain.  Let the waiter sort it out.
1158                  */
1159                 ret = 0;
1160         }
1161
1162         if (unlikely(ret))
1163                 remove_waiter(lock, waiter);
1164
1165         raw_spin_unlock(&lock->wait_lock);
1166
1167         debug_rt_mutex_print_deadlock(waiter);
1168
1169         return ret;
1170 }
1171
1172 /**
1173  * rt_mutex_next_owner - return the next owner of the lock
1174  *
1175  * @lock: the rt lock query
1176  *
1177  * Returns the next owner of the lock or NULL
1178  *
1179  * Caller has to serialize against other accessors to the lock
1180  * itself.
1181  *
1182  * Special API call for PI-futex support
1183  */
1184 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1185 {
1186         if (!rt_mutex_has_waiters(lock))
1187                 return NULL;
1188
1189         return rt_mutex_top_waiter(lock)->task;
1190 }
1191
1192 /**
1193  * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1194  * @lock:               the rt_mutex we were woken on
1195  * @to:                 the timeout, null if none. hrtimer should already have
1196  *                      been started.
1197  * @waiter:             the pre-initialized rt_mutex_waiter
1198  * @detect_deadlock:    perform deadlock detection (1) or not (0)
1199  *
1200  * Complete the lock acquisition started our behalf by another thread.
1201  *
1202  * Returns:
1203  *  0 - success
1204  * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1205  *
1206  * Special API call for PI-futex requeue support
1207  */
1208 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1209                                struct hrtimer_sleeper *to,
1210                                struct rt_mutex_waiter *waiter,
1211                                int detect_deadlock)
1212 {
1213         int ret;
1214
1215         raw_spin_lock(&lock->wait_lock);
1216
1217         set_current_state(TASK_INTERRUPTIBLE);
1218
1219         ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1220
1221         set_current_state(TASK_RUNNING);
1222
1223         if (unlikely(ret))
1224                 remove_waiter(lock, waiter);
1225
1226         /*
1227          * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1228          * have to fix that up.
1229          */
1230         fixup_rt_mutex_waiters(lock);
1231
1232         raw_spin_unlock(&lock->wait_lock);
1233
1234         return ret;
1235 }