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