rtmutex: Simplify remove_waiter()
[deliverable/linux.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 * Chain walk basics and protection scope
342 *
343 * [R] refcount on task
344 * [P] task->pi_lock held
345 * [L] rtmutex->wait_lock held
346 *
347 * Step Description Protected by
348 * function arguments:
349 * @task [R]
350 * @orig_lock if != NULL @top_task is blocked on it
351 * @next_lock Unprotected. Cannot be
352 * dereferenced. Only used for
353 * comparison.
354 * @orig_waiter if != NULL @top_task is blocked on it
355 * @top_task current, or in case of proxy
356 * locking protected by calling
357 * code
358 * again:
359 * loop_sanity_check();
360 * retry:
361 * [1] lock(task->pi_lock); [R] acquire [P]
362 * [2] waiter = task->pi_blocked_on; [P]
363 * [3] check_exit_conditions_1(); [P]
364 * [4] lock = waiter->lock; [P]
365 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
366 * unlock(task->pi_lock); release [P]
367 * goto retry;
368 * }
369 * [6] check_exit_conditions_2(); [P] + [L]
370 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
371 * [8] unlock(task->pi_lock); release [P]
372 * put_task_struct(task); release [R]
373 * [9] check_exit_conditions_3(); [L]
374 * [10] task = owner(lock); [L]
375 * get_task_struct(task); [L] acquire [R]
376 * lock(task->pi_lock); [L] acquire [P]
377 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
378 * [12] check_exit_conditions_4(); [P] + [L]
379 * [13] unlock(task->pi_lock); release [P]
380 * unlock(lock->wait_lock); release [L]
381 * goto again;
382 */
383 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
384 int deadlock_detect,
385 struct rt_mutex *orig_lock,
386 struct rt_mutex *next_lock,
387 struct rt_mutex_waiter *orig_waiter,
388 struct task_struct *top_task)
389 {
390 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
391 struct rt_mutex_waiter *prerequeue_top_waiter;
392 int detect_deadlock, ret = 0, depth = 0;
393 struct rt_mutex *lock;
394 unsigned long flags;
395
396 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
397 deadlock_detect);
398
399 /*
400 * The (de)boosting is a step by step approach with a lot of
401 * pitfalls. We want this to be preemptible and we want hold a
402 * maximum of two locks per step. So we have to check
403 * carefully whether things change under us.
404 */
405 again:
406 /*
407 * We limit the lock chain length for each invocation.
408 */
409 if (++depth > max_lock_depth) {
410 static int prev_max;
411
412 /*
413 * Print this only once. If the admin changes the limit,
414 * print a new message when reaching the limit again.
415 */
416 if (prev_max != max_lock_depth) {
417 prev_max = max_lock_depth;
418 printk(KERN_WARNING "Maximum lock depth %d reached "
419 "task: %s (%d)\n", max_lock_depth,
420 top_task->comm, task_pid_nr(top_task));
421 }
422 put_task_struct(task);
423
424 return -EDEADLK;
425 }
426
427 /*
428 * We are fully preemptible here and only hold the refcount on
429 * @task. So everything can have changed under us since the
430 * caller or our own code below (goto retry/again) dropped all
431 * locks.
432 */
433 retry:
434 /*
435 * [1] Task cannot go away as we did a get_task() before !
436 */
437 raw_spin_lock_irqsave(&task->pi_lock, flags);
438
439 /*
440 * [2] Get the waiter on which @task is blocked on.
441 */
442 waiter = task->pi_blocked_on;
443
444 /*
445 * [3] check_exit_conditions_1() protected by task->pi_lock.
446 */
447
448 /*
449 * Check whether the end of the boosting chain has been
450 * reached or the state of the chain has changed while we
451 * dropped the locks.
452 */
453 if (!waiter)
454 goto out_unlock_pi;
455
456 /*
457 * Check the orig_waiter state. After we dropped the locks,
458 * the previous owner of the lock might have released the lock.
459 */
460 if (orig_waiter && !rt_mutex_owner(orig_lock))
461 goto out_unlock_pi;
462
463 /*
464 * We dropped all locks after taking a refcount on @task, so
465 * the task might have moved on in the lock chain or even left
466 * the chain completely and blocks now on an unrelated lock or
467 * on @orig_lock.
468 *
469 * We stored the lock on which @task was blocked in @next_lock,
470 * so we can detect the chain change.
471 */
472 if (next_lock != waiter->lock)
473 goto out_unlock_pi;
474
475 /*
476 * Drop out, when the task has no waiters. Note,
477 * top_waiter can be NULL, when we are in the deboosting
478 * mode!
479 */
480 if (top_waiter) {
481 if (!task_has_pi_waiters(task))
482 goto out_unlock_pi;
483 /*
484 * If deadlock detection is off, we stop here if we
485 * are not the top pi waiter of the task.
486 */
487 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
488 goto out_unlock_pi;
489 }
490
491 /*
492 * When deadlock detection is off then we check, if further
493 * priority adjustment is necessary.
494 */
495 if (!detect_deadlock && waiter->prio == task->prio)
496 goto out_unlock_pi;
497
498 /*
499 * [4] Get the next lock
500 */
501 lock = waiter->lock;
502 /*
503 * [5] We need to trylock here as we are holding task->pi_lock,
504 * which is the reverse lock order versus the other rtmutex
505 * operations.
506 */
507 if (!raw_spin_trylock(&lock->wait_lock)) {
508 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
509 cpu_relax();
510 goto retry;
511 }
512
513 /*
514 * [6] check_exit_conditions_2() protected by task->pi_lock and
515 * lock->wait_lock.
516 *
517 * Deadlock detection. If the lock is the same as the original
518 * lock which caused us to walk the lock chain or if the
519 * current lock is owned by the task which initiated the chain
520 * walk, we detected a deadlock.
521 */
522 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
523 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
524 raw_spin_unlock(&lock->wait_lock);
525 ret = -EDEADLK;
526 goto out_unlock_pi;
527 }
528
529 /*
530 * Store the current top waiter before doing the requeue
531 * operation on @lock. We need it for the boost/deboost
532 * decision below.
533 */
534 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
535
536 /* [7] Requeue the waiter in the lock waiter list. */
537 rt_mutex_dequeue(lock, waiter);
538 waiter->prio = task->prio;
539 rt_mutex_enqueue(lock, waiter);
540
541 /* [8] Release the task */
542 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
543 put_task_struct(task);
544
545 /*
546 * [9] check_exit_conditions_3 protected by lock->wait_lock.
547 *
548 * We must abort the chain walk if there is no lock owner even
549 * in the dead lock detection case, as we have nothing to
550 * follow here. This is the end of the chain we are walking.
551 */
552 if (!rt_mutex_owner(lock)) {
553 /*
554 * If the requeue [7] above changed the top waiter,
555 * then we need to wake the new top waiter up to try
556 * to get the lock.
557 */
558 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
559 wake_up_process(rt_mutex_top_waiter(lock)->task);
560 raw_spin_unlock(&lock->wait_lock);
561 return 0;
562 }
563
564 /* [10] Grab the next task, i.e. the owner of @lock */
565 task = rt_mutex_owner(lock);
566 get_task_struct(task);
567 raw_spin_lock_irqsave(&task->pi_lock, flags);
568
569 /* [11] requeue the pi waiters if necessary */
570 if (waiter == rt_mutex_top_waiter(lock)) {
571 /*
572 * The waiter became the new top (highest priority)
573 * waiter on the lock. Replace the previous top waiter
574 * in the owner tasks pi waiters list with this waiter
575 * and adjust the priority of the owner.
576 */
577 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
578 rt_mutex_enqueue_pi(task, waiter);
579 __rt_mutex_adjust_prio(task);
580
581 } else if (prerequeue_top_waiter == waiter) {
582 /*
583 * The waiter was the top waiter on the lock, but is
584 * no longer the top prority waiter. Replace waiter in
585 * the owner tasks pi waiters list with the new top
586 * (highest priority) waiter and adjust the priority
587 * of the owner.
588 * The new top waiter is stored in @waiter so that
589 * @waiter == @top_waiter evaluates to true below and
590 * we continue to deboost the rest of the chain.
591 */
592 rt_mutex_dequeue_pi(task, waiter);
593 waiter = rt_mutex_top_waiter(lock);
594 rt_mutex_enqueue_pi(task, waiter);
595 __rt_mutex_adjust_prio(task);
596 } else {
597 /*
598 * Nothing changed. No need to do any priority
599 * adjustment.
600 */
601 }
602
603 /*
604 * [12] check_exit_conditions_4() protected by task->pi_lock
605 * and lock->wait_lock. The actual decisions are made after we
606 * dropped the locks.
607 *
608 * Check whether the task which owns the current lock is pi
609 * blocked itself. If yes we store a pointer to the lock for
610 * the lock chain change detection above. After we dropped
611 * task->pi_lock next_lock cannot be dereferenced anymore.
612 */
613 next_lock = task_blocked_on_lock(task);
614 /*
615 * Store the top waiter of @lock for the end of chain walk
616 * decision below.
617 */
618 top_waiter = rt_mutex_top_waiter(lock);
619
620 /* [13] Drop the locks */
621 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
622 raw_spin_unlock(&lock->wait_lock);
623
624 /*
625 * Make the actual exit decisions [12], based on the stored
626 * values.
627 *
628 * We reached the end of the lock chain. Stop right here. No
629 * point to go back just to figure that out.
630 */
631 if (!next_lock)
632 goto out_put_task;
633
634 /*
635 * If the current waiter is not the top waiter on the lock,
636 * then we can stop the chain walk here if we are not in full
637 * deadlock detection mode.
638 */
639 if (!detect_deadlock && waiter != top_waiter)
640 goto out_put_task;
641
642 goto again;
643
644 out_unlock_pi:
645 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
646 out_put_task:
647 put_task_struct(task);
648
649 return ret;
650 }
651
652 /*
653 * Try to take an rt-mutex
654 *
655 * Must be called with lock->wait_lock held.
656 *
657 * @lock: The lock to be acquired.
658 * @task: The task which wants to acquire the lock
659 * @waiter: The waiter that is queued to the lock's wait list if the
660 * callsite called task_blocked_on_lock(), otherwise NULL
661 */
662 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
663 struct rt_mutex_waiter *waiter)
664 {
665 unsigned long flags;
666
667 /*
668 * Before testing whether we can acquire @lock, we set the
669 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
670 * other tasks which try to modify @lock into the slow path
671 * and they serialize on @lock->wait_lock.
672 *
673 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
674 * as explained at the top of this file if and only if:
675 *
676 * - There is a lock owner. The caller must fixup the
677 * transient state if it does a trylock or leaves the lock
678 * function due to a signal or timeout.
679 *
680 * - @task acquires the lock and there are no other
681 * waiters. This is undone in rt_mutex_set_owner(@task) at
682 * the end of this function.
683 */
684 mark_rt_mutex_waiters(lock);
685
686 /*
687 * If @lock has an owner, give up.
688 */
689 if (rt_mutex_owner(lock))
690 return 0;
691
692 /*
693 * If @waiter != NULL, @task has already enqueued the waiter
694 * into @lock waiter list. If @waiter == NULL then this is a
695 * trylock attempt.
696 */
697 if (waiter) {
698 /*
699 * If waiter is not the highest priority waiter of
700 * @lock, give up.
701 */
702 if (waiter != rt_mutex_top_waiter(lock))
703 return 0;
704
705 /*
706 * We can acquire the lock. Remove the waiter from the
707 * lock waiters list.
708 */
709 rt_mutex_dequeue(lock, waiter);
710
711 } else {
712 /*
713 * If the lock has waiters already we check whether @task is
714 * eligible to take over the lock.
715 *
716 * If there are no other waiters, @task can acquire
717 * the lock. @task->pi_blocked_on is NULL, so it does
718 * not need to be dequeued.
719 */
720 if (rt_mutex_has_waiters(lock)) {
721 /*
722 * If @task->prio is greater than or equal to
723 * the top waiter priority (kernel view),
724 * @task lost.
725 */
726 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
727 return 0;
728
729 /*
730 * The current top waiter stays enqueued. We
731 * don't have to change anything in the lock
732 * waiters order.
733 */
734 } else {
735 /*
736 * No waiters. Take the lock without the
737 * pi_lock dance.@task->pi_blocked_on is NULL
738 * and we have no waiters to enqueue in @task
739 * pi waiters list.
740 */
741 goto takeit;
742 }
743 }
744
745 /*
746 * Clear @task->pi_blocked_on. Requires protection by
747 * @task->pi_lock. Redundant operation for the @waiter == NULL
748 * case, but conditionals are more expensive than a redundant
749 * store.
750 */
751 raw_spin_lock_irqsave(&task->pi_lock, flags);
752 task->pi_blocked_on = NULL;
753 /*
754 * Finish the lock acquisition. @task is the new owner. If
755 * other waiters exist we have to insert the highest priority
756 * waiter into @task->pi_waiters list.
757 */
758 if (rt_mutex_has_waiters(lock))
759 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
760 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
761
762 takeit:
763 /* We got the lock. */
764 debug_rt_mutex_lock(lock);
765
766 /*
767 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
768 * are still waiters or clears it.
769 */
770 rt_mutex_set_owner(lock, task);
771
772 rt_mutex_deadlock_account_lock(lock, task);
773
774 return 1;
775 }
776
777 /*
778 * Task blocks on lock.
779 *
780 * Prepare waiter and propagate pi chain
781 *
782 * This must be called with lock->wait_lock held.
783 */
784 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
785 struct rt_mutex_waiter *waiter,
786 struct task_struct *task,
787 int detect_deadlock)
788 {
789 struct task_struct *owner = rt_mutex_owner(lock);
790 struct rt_mutex_waiter *top_waiter = waiter;
791 struct rt_mutex *next_lock;
792 int chain_walk = 0, res;
793 unsigned long flags;
794
795 /*
796 * Early deadlock detection. We really don't want the task to
797 * enqueue on itself just to untangle the mess later. It's not
798 * only an optimization. We drop the locks, so another waiter
799 * can come in before the chain walk detects the deadlock. So
800 * the other will detect the deadlock and return -EDEADLOCK,
801 * which is wrong, as the other waiter is not in a deadlock
802 * situation.
803 */
804 if (owner == task)
805 return -EDEADLK;
806
807 raw_spin_lock_irqsave(&task->pi_lock, flags);
808 __rt_mutex_adjust_prio(task);
809 waiter->task = task;
810 waiter->lock = lock;
811 waiter->prio = task->prio;
812
813 /* Get the top priority waiter on the lock */
814 if (rt_mutex_has_waiters(lock))
815 top_waiter = rt_mutex_top_waiter(lock);
816 rt_mutex_enqueue(lock, waiter);
817
818 task->pi_blocked_on = waiter;
819
820 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
821
822 if (!owner)
823 return 0;
824
825 raw_spin_lock_irqsave(&owner->pi_lock, flags);
826 if (waiter == rt_mutex_top_waiter(lock)) {
827 rt_mutex_dequeue_pi(owner, top_waiter);
828 rt_mutex_enqueue_pi(owner, waiter);
829
830 __rt_mutex_adjust_prio(owner);
831 if (owner->pi_blocked_on)
832 chain_walk = 1;
833 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
834 chain_walk = 1;
835 }
836
837 /* Store the lock on which owner is blocked or NULL */
838 next_lock = task_blocked_on_lock(owner);
839
840 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
841 /*
842 * Even if full deadlock detection is on, if the owner is not
843 * blocked itself, we can avoid finding this out in the chain
844 * walk.
845 */
846 if (!chain_walk || !next_lock)
847 return 0;
848
849 /*
850 * The owner can't disappear while holding a lock,
851 * so the owner struct is protected by wait_lock.
852 * Gets dropped in rt_mutex_adjust_prio_chain()!
853 */
854 get_task_struct(owner);
855
856 raw_spin_unlock(&lock->wait_lock);
857
858 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
859 next_lock, waiter, task);
860
861 raw_spin_lock(&lock->wait_lock);
862
863 return res;
864 }
865
866 /*
867 * Wake up the next waiter on the lock.
868 *
869 * Remove the top waiter from the current tasks pi waiter list and
870 * wake it up.
871 *
872 * Called with lock->wait_lock held.
873 */
874 static void wakeup_next_waiter(struct rt_mutex *lock)
875 {
876 struct rt_mutex_waiter *waiter;
877 unsigned long flags;
878
879 raw_spin_lock_irqsave(&current->pi_lock, flags);
880
881 waiter = rt_mutex_top_waiter(lock);
882
883 /*
884 * Remove it from current->pi_waiters. We do not adjust a
885 * possible priority boost right now. We execute wakeup in the
886 * boosted mode and go back to normal after releasing
887 * lock->wait_lock.
888 */
889 rt_mutex_dequeue_pi(current, waiter);
890
891 /*
892 * As we are waking up the top waiter, and the waiter stays
893 * queued on the lock until it gets the lock, this lock
894 * obviously has waiters. Just set the bit here and this has
895 * the added benefit of forcing all new tasks into the
896 * slow path making sure no task of lower priority than
897 * the top waiter can steal this lock.
898 */
899 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
900
901 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
902
903 /*
904 * It's safe to dereference waiter as it cannot go away as
905 * long as we hold lock->wait_lock. The waiter task needs to
906 * acquire it in order to dequeue the waiter.
907 */
908 wake_up_process(waiter->task);
909 }
910
911 /*
912 * Remove a waiter from a lock and give up
913 *
914 * Must be called with lock->wait_lock held and
915 * have just failed to try_to_take_rt_mutex().
916 */
917 static void remove_waiter(struct rt_mutex *lock,
918 struct rt_mutex_waiter *waiter)
919 {
920 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
921 struct task_struct *owner = rt_mutex_owner(lock);
922 struct rt_mutex *next_lock;
923 unsigned long flags;
924
925 raw_spin_lock_irqsave(&current->pi_lock, flags);
926 rt_mutex_dequeue(lock, waiter);
927 current->pi_blocked_on = NULL;
928 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
929
930 /*
931 * Only update priority if the waiter was the highest priority
932 * waiter of the lock and there is an owner to update.
933 */
934 if (!owner || !is_top_waiter)
935 return;
936
937 raw_spin_lock_irqsave(&owner->pi_lock, flags);
938
939 rt_mutex_dequeue_pi(owner, waiter);
940
941 if (rt_mutex_has_waiters(lock))
942 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
943
944 __rt_mutex_adjust_prio(owner);
945
946 /* Store the lock on which owner is blocked or NULL */
947 next_lock = task_blocked_on_lock(owner);
948
949 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
950
951 /*
952 * Don't walk the chain, if the owner task is not blocked
953 * itself.
954 */
955 if (!next_lock)
956 return;
957
958 /* gets dropped in rt_mutex_adjust_prio_chain()! */
959 get_task_struct(owner);
960
961 raw_spin_unlock(&lock->wait_lock);
962
963 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
964
965 raw_spin_lock(&lock->wait_lock);
966 }
967
968 /*
969 * Recheck the pi chain, in case we got a priority setting
970 *
971 * Called from sched_setscheduler
972 */
973 void rt_mutex_adjust_pi(struct task_struct *task)
974 {
975 struct rt_mutex_waiter *waiter;
976 struct rt_mutex *next_lock;
977 unsigned long flags;
978
979 raw_spin_lock_irqsave(&task->pi_lock, flags);
980
981 waiter = task->pi_blocked_on;
982 if (!waiter || (waiter->prio == task->prio &&
983 !dl_prio(task->prio))) {
984 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
985 return;
986 }
987 next_lock = waiter->lock;
988 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
989
990 /* gets dropped in rt_mutex_adjust_prio_chain()! */
991 get_task_struct(task);
992
993 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
994 }
995
996 /**
997 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
998 * @lock: the rt_mutex to take
999 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1000 * or TASK_UNINTERRUPTIBLE)
1001 * @timeout: the pre-initialized and started timer, or NULL for none
1002 * @waiter: the pre-initialized rt_mutex_waiter
1003 *
1004 * lock->wait_lock must be held by the caller.
1005 */
1006 static int __sched
1007 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
1008 struct hrtimer_sleeper *timeout,
1009 struct rt_mutex_waiter *waiter)
1010 {
1011 int ret = 0;
1012
1013 for (;;) {
1014 /* Try to acquire the lock: */
1015 if (try_to_take_rt_mutex(lock, current, waiter))
1016 break;
1017
1018 /*
1019 * TASK_INTERRUPTIBLE checks for signals and
1020 * timeout. Ignored otherwise.
1021 */
1022 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1023 /* Signal pending? */
1024 if (signal_pending(current))
1025 ret = -EINTR;
1026 if (timeout && !timeout->task)
1027 ret = -ETIMEDOUT;
1028 if (ret)
1029 break;
1030 }
1031
1032 raw_spin_unlock(&lock->wait_lock);
1033
1034 debug_rt_mutex_print_deadlock(waiter);
1035
1036 schedule_rt_mutex(lock);
1037
1038 raw_spin_lock(&lock->wait_lock);
1039 set_current_state(state);
1040 }
1041
1042 return ret;
1043 }
1044
1045 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1046 struct rt_mutex_waiter *w)
1047 {
1048 /*
1049 * If the result is not -EDEADLOCK or the caller requested
1050 * deadlock detection, nothing to do here.
1051 */
1052 if (res != -EDEADLOCK || detect_deadlock)
1053 return;
1054
1055 /*
1056 * Yell lowdly and stop the task right here.
1057 */
1058 rt_mutex_print_deadlock(w);
1059 while (1) {
1060 set_current_state(TASK_INTERRUPTIBLE);
1061 schedule();
1062 }
1063 }
1064
1065 /*
1066 * Slow path lock function:
1067 */
1068 static int __sched
1069 rt_mutex_slowlock(struct rt_mutex *lock, int state,
1070 struct hrtimer_sleeper *timeout,
1071 int detect_deadlock)
1072 {
1073 struct rt_mutex_waiter waiter;
1074 int ret = 0;
1075
1076 debug_rt_mutex_init_waiter(&waiter);
1077 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1078 RB_CLEAR_NODE(&waiter.tree_entry);
1079
1080 raw_spin_lock(&lock->wait_lock);
1081
1082 /* Try to acquire the lock again: */
1083 if (try_to_take_rt_mutex(lock, current, NULL)) {
1084 raw_spin_unlock(&lock->wait_lock);
1085 return 0;
1086 }
1087
1088 set_current_state(state);
1089
1090 /* Setup the timer, when timeout != NULL */
1091 if (unlikely(timeout)) {
1092 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1093 if (!hrtimer_active(&timeout->timer))
1094 timeout->task = NULL;
1095 }
1096
1097 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
1098
1099 if (likely(!ret))
1100 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1101
1102 set_current_state(TASK_RUNNING);
1103
1104 if (unlikely(ret)) {
1105 remove_waiter(lock, &waiter);
1106 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
1107 }
1108
1109 /*
1110 * try_to_take_rt_mutex() sets the waiter bit
1111 * unconditionally. We might have to fix that up.
1112 */
1113 fixup_rt_mutex_waiters(lock);
1114
1115 raw_spin_unlock(&lock->wait_lock);
1116
1117 /* Remove pending timer: */
1118 if (unlikely(timeout))
1119 hrtimer_cancel(&timeout->timer);
1120
1121 debug_rt_mutex_free_waiter(&waiter);
1122
1123 return ret;
1124 }
1125
1126 /*
1127 * Slow path try-lock function:
1128 */
1129 static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
1130 {
1131 int ret;
1132
1133 /*
1134 * If the lock already has an owner we fail to get the lock.
1135 * This can be done without taking the @lock->wait_lock as
1136 * it is only being read, and this is a trylock anyway.
1137 */
1138 if (rt_mutex_owner(lock))
1139 return 0;
1140
1141 /*
1142 * The mutex has currently no owner. Lock the wait lock and
1143 * try to acquire the lock.
1144 */
1145 raw_spin_lock(&lock->wait_lock);
1146
1147 ret = try_to_take_rt_mutex(lock, current, NULL);
1148
1149 /*
1150 * try_to_take_rt_mutex() sets the lock waiters bit
1151 * unconditionally. Clean this up.
1152 */
1153 fixup_rt_mutex_waiters(lock);
1154
1155 raw_spin_unlock(&lock->wait_lock);
1156
1157 return ret;
1158 }
1159
1160 /*
1161 * Slow path to release a rt-mutex:
1162 */
1163 static void __sched
1164 rt_mutex_slowunlock(struct rt_mutex *lock)
1165 {
1166 raw_spin_lock(&lock->wait_lock);
1167
1168 debug_rt_mutex_unlock(lock);
1169
1170 rt_mutex_deadlock_account_unlock(current);
1171
1172 /*
1173 * We must be careful here if the fast path is enabled. If we
1174 * have no waiters queued we cannot set owner to NULL here
1175 * because of:
1176 *
1177 * foo->lock->owner = NULL;
1178 * rtmutex_lock(foo->lock); <- fast path
1179 * free = atomic_dec_and_test(foo->refcnt);
1180 * rtmutex_unlock(foo->lock); <- fast path
1181 * if (free)
1182 * kfree(foo);
1183 * raw_spin_unlock(foo->lock->wait_lock);
1184 *
1185 * So for the fastpath enabled kernel:
1186 *
1187 * Nothing can set the waiters bit as long as we hold
1188 * lock->wait_lock. So we do the following sequence:
1189 *
1190 * owner = rt_mutex_owner(lock);
1191 * clear_rt_mutex_waiters(lock);
1192 * raw_spin_unlock(&lock->wait_lock);
1193 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1194 * return;
1195 * goto retry;
1196 *
1197 * The fastpath disabled variant is simple as all access to
1198 * lock->owner is serialized by lock->wait_lock:
1199 *
1200 * lock->owner = NULL;
1201 * raw_spin_unlock(&lock->wait_lock);
1202 */
1203 while (!rt_mutex_has_waiters(lock)) {
1204 /* Drops lock->wait_lock ! */
1205 if (unlock_rt_mutex_safe(lock) == true)
1206 return;
1207 /* Relock the rtmutex and try again */
1208 raw_spin_lock(&lock->wait_lock);
1209 }
1210
1211 /*
1212 * The wakeup next waiter path does not suffer from the above
1213 * race. See the comments there.
1214 */
1215 wakeup_next_waiter(lock);
1216
1217 raw_spin_unlock(&lock->wait_lock);
1218
1219 /* Undo pi boosting if necessary: */
1220 rt_mutex_adjust_prio(current);
1221 }
1222
1223 /*
1224 * debug aware fast / slowpath lock,trylock,unlock
1225 *
1226 * The atomic acquire/release ops are compiled away, when either the
1227 * architecture does not support cmpxchg or when debugging is enabled.
1228 */
1229 static inline int
1230 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1231 int detect_deadlock,
1232 int (*slowfn)(struct rt_mutex *lock, int state,
1233 struct hrtimer_sleeper *timeout,
1234 int detect_deadlock))
1235 {
1236 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1237 rt_mutex_deadlock_account_lock(lock, current);
1238 return 0;
1239 } else
1240 return slowfn(lock, state, NULL, detect_deadlock);
1241 }
1242
1243 static inline int
1244 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1245 struct hrtimer_sleeper *timeout, int detect_deadlock,
1246 int (*slowfn)(struct rt_mutex *lock, int state,
1247 struct hrtimer_sleeper *timeout,
1248 int detect_deadlock))
1249 {
1250 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1251 rt_mutex_deadlock_account_lock(lock, current);
1252 return 0;
1253 } else
1254 return slowfn(lock, state, timeout, detect_deadlock);
1255 }
1256
1257 static inline int
1258 rt_mutex_fasttrylock(struct rt_mutex *lock,
1259 int (*slowfn)(struct rt_mutex *lock))
1260 {
1261 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1262 rt_mutex_deadlock_account_lock(lock, current);
1263 return 1;
1264 }
1265 return slowfn(lock);
1266 }
1267
1268 static inline void
1269 rt_mutex_fastunlock(struct rt_mutex *lock,
1270 void (*slowfn)(struct rt_mutex *lock))
1271 {
1272 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1273 rt_mutex_deadlock_account_unlock(current);
1274 else
1275 slowfn(lock);
1276 }
1277
1278 /**
1279 * rt_mutex_lock - lock a rt_mutex
1280 *
1281 * @lock: the rt_mutex to be locked
1282 */
1283 void __sched rt_mutex_lock(struct rt_mutex *lock)
1284 {
1285 might_sleep();
1286
1287 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1288 }
1289 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1290
1291 /**
1292 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1293 *
1294 * @lock: the rt_mutex to be locked
1295 * @detect_deadlock: deadlock detection on/off
1296 *
1297 * Returns:
1298 * 0 on success
1299 * -EINTR when interrupted by a signal
1300 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1301 */
1302 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1303 int detect_deadlock)
1304 {
1305 might_sleep();
1306
1307 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1308 detect_deadlock, rt_mutex_slowlock);
1309 }
1310 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1311
1312 /**
1313 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1314 * the timeout structure is provided
1315 * by the caller
1316 *
1317 * @lock: the rt_mutex to be locked
1318 * @timeout: timeout structure or NULL (no timeout)
1319 * @detect_deadlock: deadlock detection on/off
1320 *
1321 * Returns:
1322 * 0 on success
1323 * -EINTR when interrupted by a signal
1324 * -ETIMEDOUT when the timeout expired
1325 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1326 */
1327 int
1328 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1329 int detect_deadlock)
1330 {
1331 might_sleep();
1332
1333 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1334 detect_deadlock, rt_mutex_slowlock);
1335 }
1336 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1337
1338 /**
1339 * rt_mutex_trylock - try to lock a rt_mutex
1340 *
1341 * @lock: the rt_mutex to be locked
1342 *
1343 * Returns 1 on success and 0 on contention
1344 */
1345 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1346 {
1347 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1348 }
1349 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1350
1351 /**
1352 * rt_mutex_unlock - unlock a rt_mutex
1353 *
1354 * @lock: the rt_mutex to be unlocked
1355 */
1356 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1357 {
1358 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1359 }
1360 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1361
1362 /**
1363 * rt_mutex_destroy - mark a mutex unusable
1364 * @lock: the mutex to be destroyed
1365 *
1366 * This function marks the mutex uninitialized, and any subsequent
1367 * use of the mutex is forbidden. The mutex must not be locked when
1368 * this function is called.
1369 */
1370 void rt_mutex_destroy(struct rt_mutex *lock)
1371 {
1372 WARN_ON(rt_mutex_is_locked(lock));
1373 #ifdef CONFIG_DEBUG_RT_MUTEXES
1374 lock->magic = NULL;
1375 #endif
1376 }
1377
1378 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1379
1380 /**
1381 * __rt_mutex_init - initialize the rt lock
1382 *
1383 * @lock: the rt lock to be initialized
1384 *
1385 * Initialize the rt lock to unlocked state.
1386 *
1387 * Initializing of a locked rt lock is not allowed
1388 */
1389 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1390 {
1391 lock->owner = NULL;
1392 raw_spin_lock_init(&lock->wait_lock);
1393 lock->waiters = RB_ROOT;
1394 lock->waiters_leftmost = NULL;
1395
1396 debug_rt_mutex_init(lock, name);
1397 }
1398 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1399
1400 /**
1401 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1402 * proxy owner
1403 *
1404 * @lock: the rt_mutex to be locked
1405 * @proxy_owner:the task to set as owner
1406 *
1407 * No locking. Caller has to do serializing itself
1408 * Special API call for PI-futex support
1409 */
1410 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1411 struct task_struct *proxy_owner)
1412 {
1413 __rt_mutex_init(lock, NULL);
1414 debug_rt_mutex_proxy_lock(lock, proxy_owner);
1415 rt_mutex_set_owner(lock, proxy_owner);
1416 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1417 }
1418
1419 /**
1420 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1421 *
1422 * @lock: the rt_mutex to be locked
1423 *
1424 * No locking. Caller has to do serializing itself
1425 * Special API call for PI-futex support
1426 */
1427 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1428 struct task_struct *proxy_owner)
1429 {
1430 debug_rt_mutex_proxy_unlock(lock);
1431 rt_mutex_set_owner(lock, NULL);
1432 rt_mutex_deadlock_account_unlock(proxy_owner);
1433 }
1434
1435 /**
1436 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1437 * @lock: the rt_mutex to take
1438 * @waiter: the pre-initialized rt_mutex_waiter
1439 * @task: the task to prepare
1440 * @detect_deadlock: perform deadlock detection (1) or not (0)
1441 *
1442 * Returns:
1443 * 0 - task blocked on lock
1444 * 1 - acquired the lock for task, caller should wake it up
1445 * <0 - error
1446 *
1447 * Special API call for FUTEX_REQUEUE_PI support.
1448 */
1449 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1450 struct rt_mutex_waiter *waiter,
1451 struct task_struct *task, int detect_deadlock)
1452 {
1453 int ret;
1454
1455 raw_spin_lock(&lock->wait_lock);
1456
1457 if (try_to_take_rt_mutex(lock, task, NULL)) {
1458 raw_spin_unlock(&lock->wait_lock);
1459 return 1;
1460 }
1461
1462 /* We enforce deadlock detection for futexes */
1463 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
1464
1465 if (ret && !rt_mutex_owner(lock)) {
1466 /*
1467 * Reset the return value. We might have
1468 * returned with -EDEADLK and the owner
1469 * released the lock while we were walking the
1470 * pi chain. Let the waiter sort it out.
1471 */
1472 ret = 0;
1473 }
1474
1475 if (unlikely(ret))
1476 remove_waiter(lock, waiter);
1477
1478 raw_spin_unlock(&lock->wait_lock);
1479
1480 debug_rt_mutex_print_deadlock(waiter);
1481
1482 return ret;
1483 }
1484
1485 /**
1486 * rt_mutex_next_owner - return the next owner of the lock
1487 *
1488 * @lock: the rt lock query
1489 *
1490 * Returns the next owner of the lock or NULL
1491 *
1492 * Caller has to serialize against other accessors to the lock
1493 * itself.
1494 *
1495 * Special API call for PI-futex support
1496 */
1497 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1498 {
1499 if (!rt_mutex_has_waiters(lock))
1500 return NULL;
1501
1502 return rt_mutex_top_waiter(lock)->task;
1503 }
1504
1505 /**
1506 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1507 * @lock: the rt_mutex we were woken on
1508 * @to: the timeout, null if none. hrtimer should already have
1509 * been started.
1510 * @waiter: the pre-initialized rt_mutex_waiter
1511 * @detect_deadlock: perform deadlock detection (1) or not (0)
1512 *
1513 * Complete the lock acquisition started our behalf by another thread.
1514 *
1515 * Returns:
1516 * 0 - success
1517 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1518 *
1519 * Special API call for PI-futex requeue support
1520 */
1521 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1522 struct hrtimer_sleeper *to,
1523 struct rt_mutex_waiter *waiter,
1524 int detect_deadlock)
1525 {
1526 int ret;
1527
1528 raw_spin_lock(&lock->wait_lock);
1529
1530 set_current_state(TASK_INTERRUPTIBLE);
1531
1532 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1533
1534 set_current_state(TASK_RUNNING);
1535
1536 if (unlikely(ret))
1537 remove_waiter(lock, waiter);
1538
1539 /*
1540 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1541 * have to fix that up.
1542 */
1543 fixup_rt_mutex_waiters(lock);
1544
1545 raw_spin_unlock(&lock->wait_lock);
1546
1547 return ret;
1548 }
This page took 0.078158 seconds and 6 git commands to generate.