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