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