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