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