blob: 50bc93b3552f79132eadcaa125e9595f61fb6297 [file] [log] [blame]
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001/*
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
Steven Rostedtd07fe822006-07-30 03:04:03 -070010 *
11 * See Documentation/rt-mutex-design.txt for details.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070012 */
13#include <linux/spinlock.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070015#include <linux/sched.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060016#include <linux/sched/rt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010017#include <linux/sched/deadline.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070018#include <linux/timer.h>
19
20#include "rtmutex_common.h"
21
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070022/*
23 * lock->owner state tracking:
24 *
Lai Jiangshan81612392011-01-14 17:09:41 +080025 * lock->owner holds the task_struct pointer of the owner. Bit 0
26 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070027 *
Lai Jiangshan81612392011-01-14 17:09:41 +080028 * 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**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070034 *
35 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080036 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070037 *
Lai Jiangshan81612392011-01-14 17:09:41 +080038 * (*) 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.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070047 */
48
Thomas Gleixnerbd197232007-06-17 21:11:10 +020049static void
Lai Jiangshan81612392011-01-14 17:09:41 +080050rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070051{
Lai Jiangshan81612392011-01-14 17:09:41 +080052 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070053
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
72/*
Thomas Gleixnerbd197232007-06-17 21:11:10 +020073 * 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}
Thomas Gleixner27e35712014-06-11 18:44:04 +000086
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 */
93static 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
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200127#else
128# define rt_mutex_cmpxchg(l,c,n) (0)
129static 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}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000134
135/*
136 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
137 */
138static 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}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200145#endif
146
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100147static inline int
148rt_mutex_waiter_less(struct rt_mutex_waiter *left,
149 struct rt_mutex_waiter *right)
150{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100151 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100152 return 1;
153
154 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100155 * 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.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100159 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100160 if (dl_prio(left->prio))
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100161 return (left->task->dl.deadline < right->task->dl.deadline);
162
163 return 0;
164}
165
166static void
167rt_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
192static void
193rt_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
205static void
206rt_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
231static void
232rt_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
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200244/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100245 * Calculate task priority from the waiter tree priority
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700246 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100247 * Return task->normal_prio when the waiter tree is empty or when
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700248 * the waiter is not allowed to do priority boosting
249 */
250int rt_mutex_getprio(struct task_struct *task)
251{
252 if (likely(!task_has_pi_waiters(task)))
253 return task->normal_prio;
254
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100255 return min(task_top_pi_waiter(task)->prio,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700256 task->normal_prio);
257}
258
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100259struct 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
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700267/*
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100268 * Called by sched_setscheduler() to check whether the priority change
269 * is overruled by a possible priority boosting.
270 */
271int 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/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700280 * 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 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200284static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700285{
286 int prio = rt_mutex_getprio(task);
287
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100288 if (task->prio != prio || dl_prio(prio))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700289 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 */
301static void rt_mutex_adjust_prio(struct task_struct *task)
302{
303 unsigned long flags;
304
Thomas Gleixner1d615482009-11-17 14:54:03 +0100305 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700306 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100307 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700308}
309
310/*
311 * Max number of times we'll walk the boosting chain:
312 */
313int max_lock_depth = 1024;
314
Thomas Gleixner82084982014-06-05 11:16:12 +0200315static 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
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700320/*
321 * Adjust the priority chain. Also used for deadlock detection.
322 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200323 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200324 * @task: the task owning the mutex (owner) for which a chain walk is
325 * probably needed
Juri Lelli0c106172013-05-15 11:04:10 +0200326 * @deadlock_detect: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200327 * @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.
Juri Lelli0c106172013-05-15 11:04:10 +0200333 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200334 * 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
Juri Lelli0c106172013-05-15 11:04:10 +0200338 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700339 * Returns 0 or -EDEADLK.
340 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200341static int rt_mutex_adjust_prio_chain(struct task_struct *task,
342 int deadlock_detect,
343 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200344 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200345 struct rt_mutex_waiter *orig_waiter,
346 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700347{
348 struct rt_mutex *lock;
349 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
350 int detect_deadlock, ret = 0, depth = 0;
351 unsigned long flags;
352
353 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
354 deadlock_detect);
355
356 /*
357 * The (de)boosting is a step by step approach with a lot of
358 * pitfalls. We want this to be preemptible and we want hold a
359 * maximum of two locks per step. So we have to check
360 * carefully whether things change under us.
361 */
362 again:
363 if (++depth > max_lock_depth) {
364 static int prev_max;
365
366 /*
367 * Print this only once. If the admin changes the limit,
368 * print a new message when reaching the limit again.
369 */
370 if (prev_max != max_lock_depth) {
371 prev_max = max_lock_depth;
372 printk(KERN_WARNING "Maximum lock depth %d reached "
373 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700374 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700375 }
376 put_task_struct(task);
377
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200378 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700379 }
380 retry:
381 /*
382 * Task can not go away as we did a get_task() before !
383 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100384 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700385
386 waiter = task->pi_blocked_on;
387 /*
388 * Check whether the end of the boosting chain has been
389 * reached or the state of the chain has changed while we
390 * dropped the locks.
391 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800392 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700393 goto out_unlock_pi;
394
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700395 /*
396 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800397 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700398 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800399 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700400 goto out_unlock_pi;
401
402 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200403 * We dropped all locks after taking a refcount on @task, so
404 * the task might have moved on in the lock chain or even left
405 * the chain completely and blocks now on an unrelated lock or
406 * on @orig_lock.
407 *
408 * We stored the lock on which @task was blocked in @next_lock,
409 * so we can detect the chain change.
410 */
411 if (next_lock != waiter->lock)
412 goto out_unlock_pi;
413
414 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700415 * Drop out, when the task has no waiters. Note,
416 * top_waiter can be NULL, when we are in the deboosting
417 * mode!
418 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000419 if (top_waiter) {
420 if (!task_has_pi_waiters(task))
421 goto out_unlock_pi;
422 /*
423 * If deadlock detection is off, we stop here if we
424 * are not the top pi waiter of the task.
425 */
426 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
427 goto out_unlock_pi;
428 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700429
430 /*
431 * When deadlock detection is off then we check, if further
432 * priority adjustment is necessary.
433 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100434 if (!detect_deadlock && waiter->prio == task->prio)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700435 goto out_unlock_pi;
436
437 lock = waiter->lock;
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100438 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100439 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700440 cpu_relax();
441 goto retry;
442 }
443
Thomas Gleixner397335f2014-05-22 03:25:39 +0000444 /*
445 * Deadlock detection. If the lock is the same as the original
446 * lock which caused us to walk the lock chain or if the
447 * current lock is owned by the task which initiated the chain
448 * walk, we detected a deadlock.
449 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700450 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700451 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100452 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200453 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700454 goto out_unlock_pi;
455 }
456
457 top_waiter = rt_mutex_top_waiter(lock);
458
459 /* Requeue the waiter */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100460 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100461 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100462 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700463
464 /* Release the task */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100465 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Lai Jiangshan81612392011-01-14 17:09:41 +0800466 if (!rt_mutex_owner(lock)) {
467 /*
468 * If the requeue above changed the top waiter, then we need
469 * to wake the new top waiter up to try to get the lock.
470 */
471
472 if (top_waiter != rt_mutex_top_waiter(lock))
473 wake_up_process(rt_mutex_top_waiter(lock)->task);
474 raw_spin_unlock(&lock->wait_lock);
475 goto out_put_task;
476 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700477 put_task_struct(task);
478
479 /* Grab the next task */
480 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700481 get_task_struct(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100482 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700483
484 if (waiter == rt_mutex_top_waiter(lock)) {
485 /* Boost the owner */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100486 rt_mutex_dequeue_pi(task, top_waiter);
487 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700488 __rt_mutex_adjust_prio(task);
489
490 } else if (top_waiter == waiter) {
491 /* Deboost the owner */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100492 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700493 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100494 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700495 __rt_mutex_adjust_prio(task);
496 }
497
Thomas Gleixner82084982014-06-05 11:16:12 +0200498 /*
499 * Check whether the task which owns the current lock is pi
500 * blocked itself. If yes we store a pointer to the lock for
501 * the lock chain change detection above. After we dropped
502 * task->pi_lock next_lock cannot be dereferenced anymore.
503 */
504 next_lock = task_blocked_on_lock(task);
505
Thomas Gleixner1d615482009-11-17 14:54:03 +0100506 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700507
508 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100509 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700510
Thomas Gleixner82084982014-06-05 11:16:12 +0200511 /*
512 * We reached the end of the lock chain. Stop right here. No
513 * point to go back just to figure that out.
514 */
515 if (!next_lock)
516 goto out_put_task;
517
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700518 if (!detect_deadlock && waiter != top_waiter)
519 goto out_put_task;
520
521 goto again;
522
523 out_unlock_pi:
Thomas Gleixner1d615482009-11-17 14:54:03 +0100524 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700525 out_put_task:
526 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700527
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700528 return ret;
529}
530
531/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700532 * Try to take an rt-mutex
533 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700534 * Must be called with lock->wait_lock held.
Lai Jiangshan81612392011-01-14 17:09:41 +0800535 *
536 * @lock: the lock to be acquired.
537 * @task: the task which wants to acquire the lock
538 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700539 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800540static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
541 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700542{
543 /*
544 * We have to be careful here if the atomic speedups are
545 * enabled, such that, when
546 * - no other waiter is on the lock
547 * - the lock has been released since we did the cmpxchg
548 * the lock can be released or taken while we are doing the
549 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
550 *
551 * The atomic acquire/release aware variant of
552 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
553 * the WAITERS bit, the atomic release / acquire can not
554 * happen anymore and lock->wait_lock protects us from the
555 * non-atomic case.
556 *
557 * Note, that this might set lock->owner =
558 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
559 * any more. This is fixed up when we take the ownership.
560 * This is the transitional state explained at the top of this file.
561 */
562 mark_rt_mutex_waiters(lock);
563
Lai Jiangshan81612392011-01-14 17:09:41 +0800564 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700565 return 0;
566
Lai Jiangshan81612392011-01-14 17:09:41 +0800567 /*
568 * It will get the lock because of one of these conditions:
569 * 1) there is no waiter
570 * 2) higher priority than waiters
571 * 3) it is top waiter
572 */
573 if (rt_mutex_has_waiters(lock)) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100574 if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
Lai Jiangshan81612392011-01-14 17:09:41 +0800575 if (!waiter || waiter != rt_mutex_top_waiter(lock))
576 return 0;
577 }
578 }
579
580 if (waiter || rt_mutex_has_waiters(lock)) {
581 unsigned long flags;
582 struct rt_mutex_waiter *top;
583
584 raw_spin_lock_irqsave(&task->pi_lock, flags);
585
586 /* remove the queued waiter. */
587 if (waiter) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100588 rt_mutex_dequeue(lock, waiter);
Lai Jiangshan81612392011-01-14 17:09:41 +0800589 task->pi_blocked_on = NULL;
590 }
591
592 /*
593 * We have to enqueue the top waiter(if it exists) into
594 * task->pi_waiters list.
595 */
596 if (rt_mutex_has_waiters(lock)) {
597 top = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100598 rt_mutex_enqueue_pi(task, top);
Lai Jiangshan81612392011-01-14 17:09:41 +0800599 }
600 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
601 }
602
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700603 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700604 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700605
Lai Jiangshan81612392011-01-14 17:09:41 +0800606 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700607
Lai Jiangshan81612392011-01-14 17:09:41 +0800608 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700609
610 return 1;
611}
612
613/*
614 * Task blocks on lock.
615 *
616 * Prepare waiter and propagate pi chain
617 *
618 * This must be called with lock->wait_lock held.
619 */
620static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
621 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700622 struct task_struct *task,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700623 int detect_deadlock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700624{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700625 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700626 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200627 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700628 int chain_walk = 0, res;
Thomas Gleixner82084982014-06-05 11:16:12 +0200629 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700630
Thomas Gleixner397335f2014-05-22 03:25:39 +0000631 /*
632 * Early deadlock detection. We really don't want the task to
633 * enqueue on itself just to untangle the mess later. It's not
634 * only an optimization. We drop the locks, so another waiter
635 * can come in before the chain walk detects the deadlock. So
636 * the other will detect the deadlock and return -EDEADLOCK,
637 * which is wrong, as the other waiter is not in a deadlock
638 * situation.
639 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200640 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000641 return -EDEADLK;
642
Thomas Gleixner1d615482009-11-17 14:54:03 +0100643 raw_spin_lock_irqsave(&task->pi_lock, flags);
Darren Hart8dac4562009-04-03 13:40:12 -0700644 __rt_mutex_adjust_prio(task);
645 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700646 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100647 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700648
649 /* Get the top priority waiter on the lock */
650 if (rt_mutex_has_waiters(lock))
651 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100652 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700653
Darren Hart8dac4562009-04-03 13:40:12 -0700654 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700655
Thomas Gleixner1d615482009-11-17 14:54:03 +0100656 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700657
Lai Jiangshan81612392011-01-14 17:09:41 +0800658 if (!owner)
659 return 0;
660
Thomas Gleixner82084982014-06-05 11:16:12 +0200661 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700662 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100663 rt_mutex_dequeue_pi(owner, top_waiter);
664 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700665
666 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700667 if (owner->pi_blocked_on)
668 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200669 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700670 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200671 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700672
Thomas Gleixner82084982014-06-05 11:16:12 +0200673 /* Store the lock on which owner is blocked or NULL */
674 next_lock = task_blocked_on_lock(owner);
675
676 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
677 /*
678 * Even if full deadlock detection is on, if the owner is not
679 * blocked itself, we can avoid finding this out in the chain
680 * walk.
681 */
682 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700683 return 0;
684
Steven Rostedtdb630632006-09-29 01:59:44 -0700685 /*
686 * The owner can't disappear while holding a lock,
687 * so the owner struct is protected by wait_lock.
688 * Gets dropped in rt_mutex_adjust_prio_chain()!
689 */
690 get_task_struct(owner);
691
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100692 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700693
Thomas Gleixner82084982014-06-05 11:16:12 +0200694 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
695 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700696
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100697 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700698
699 return res;
700}
701
702/*
703 * Wake up the next waiter on the lock.
704 *
Thomas Gleixner27e35712014-06-11 18:44:04 +0000705 * Remove the top waiter from the current tasks pi waiter list and
706 * wake it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700707 *
708 * Called with lock->wait_lock held.
709 */
710static void wakeup_next_waiter(struct rt_mutex *lock)
711{
712 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700713 unsigned long flags;
714
Thomas Gleixner1d615482009-11-17 14:54:03 +0100715 raw_spin_lock_irqsave(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700716
717 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700718
719 /*
720 * Remove it from current->pi_waiters. We do not adjust a
721 * possible priority boost right now. We execute wakeup in the
722 * boosted mode and go back to normal after releasing
723 * lock->wait_lock.
724 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100725 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700726
Thomas Gleixner27e35712014-06-11 18:44:04 +0000727 /*
728 * As we are waking up the top waiter, and the waiter stays
729 * queued on the lock until it gets the lock, this lock
730 * obviously has waiters. Just set the bit here and this has
731 * the added benefit of forcing all new tasks into the
732 * slow path making sure no task of lower priority than
733 * the top waiter can steal this lock.
734 */
735 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700736
Thomas Gleixner1d615482009-11-17 14:54:03 +0100737 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700738
Thomas Gleixner27e35712014-06-11 18:44:04 +0000739 /*
740 * It's safe to dereference waiter as it cannot go away as
741 * long as we hold lock->wait_lock. The waiter task needs to
742 * acquire it in order to dequeue the waiter.
743 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800744 wake_up_process(waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700745}
746
747/*
Lai Jiangshan81612392011-01-14 17:09:41 +0800748 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700749 *
Lai Jiangshan81612392011-01-14 17:09:41 +0800750 * Must be called with lock->wait_lock held and
751 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700752 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200753static void remove_waiter(struct rt_mutex *lock,
754 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700755{
756 int first = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -0700757 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200758 struct rt_mutex *next_lock = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700759 unsigned long flags;
760
Thomas Gleixner1d615482009-11-17 14:54:03 +0100761 raw_spin_lock_irqsave(&current->pi_lock, flags);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100762 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700763 current->pi_blocked_on = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100764 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700765
Lai Jiangshan81612392011-01-14 17:09:41 +0800766 if (!owner)
767 return;
768
769 if (first) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700770
Thomas Gleixner1d615482009-11-17 14:54:03 +0100771 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700772
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100773 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700774
775 if (rt_mutex_has_waiters(lock)) {
776 struct rt_mutex_waiter *next;
777
778 next = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100779 rt_mutex_enqueue_pi(owner, next);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700780 }
781 __rt_mutex_adjust_prio(owner);
782
Thomas Gleixner82084982014-06-05 11:16:12 +0200783 /* Store the lock on which owner is blocked or NULL */
784 next_lock = task_blocked_on_lock(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700785
Thomas Gleixner1d615482009-11-17 14:54:03 +0100786 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700787 }
788
Thomas Gleixner82084982014-06-05 11:16:12 +0200789 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700790 return;
791
Steven Rostedtdb630632006-09-29 01:59:44 -0700792 /* gets dropped in rt_mutex_adjust_prio_chain()! */
793 get_task_struct(owner);
794
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100795 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700796
Thomas Gleixner82084982014-06-05 11:16:12 +0200797 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700798
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100799 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700800}
801
802/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700803 * Recheck the pi chain, in case we got a priority setting
804 *
805 * Called from sched_setscheduler
806 */
807void rt_mutex_adjust_pi(struct task_struct *task)
808{
809 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200810 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700811 unsigned long flags;
812
Thomas Gleixner1d615482009-11-17 14:54:03 +0100813 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700814
815 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100816 if (!waiter || (waiter->prio == task->prio &&
817 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100818 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700819 return;
820 }
Thomas Gleixner82084982014-06-05 11:16:12 +0200821 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100822 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700823
Steven Rostedtdb630632006-09-29 01:59:44 -0700824 /* gets dropped in rt_mutex_adjust_prio_chain()! */
825 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +0200826
827 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700828}
829
Darren Hart8dac4562009-04-03 13:40:12 -0700830/**
831 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
832 * @lock: the rt_mutex to take
833 * @state: the state the task should block in (TASK_INTERRUPTIBLE
834 * or TASK_UNINTERRUPTIBLE)
835 * @timeout: the pre-initialized and started timer, or NULL for none
836 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -0700837 *
838 * lock->wait_lock must be held by the caller.
839 */
840static int __sched
841__rt_mutex_slowlock(struct rt_mutex *lock, int state,
842 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +0800843 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -0700844{
845 int ret = 0;
846
847 for (;;) {
848 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +0800849 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -0700850 break;
851
852 /*
853 * TASK_INTERRUPTIBLE checks for signals and
854 * timeout. Ignored otherwise.
855 */
856 if (unlikely(state == TASK_INTERRUPTIBLE)) {
857 /* Signal pending? */
858 if (signal_pending(current))
859 ret = -EINTR;
860 if (timeout && !timeout->task)
861 ret = -ETIMEDOUT;
862 if (ret)
863 break;
864 }
865
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100866 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700867
868 debug_rt_mutex_print_deadlock(waiter);
869
Lai Jiangshan81612392011-01-14 17:09:41 +0800870 schedule_rt_mutex(lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700871
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100872 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700873 set_current_state(state);
874 }
875
876 return ret;
877}
878
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200879static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
880 struct rt_mutex_waiter *w)
881{
882 /*
883 * If the result is not -EDEADLOCK or the caller requested
884 * deadlock detection, nothing to do here.
885 */
886 if (res != -EDEADLOCK || detect_deadlock)
887 return;
888
889 /*
890 * Yell lowdly and stop the task right here.
891 */
892 rt_mutex_print_deadlock(w);
893 while (1) {
894 set_current_state(TASK_INTERRUPTIBLE);
895 schedule();
896 }
897}
898
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700899/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700900 * Slow path lock function:
901 */
902static int __sched
903rt_mutex_slowlock(struct rt_mutex *lock, int state,
904 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700905 int detect_deadlock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700906{
907 struct rt_mutex_waiter waiter;
908 int ret = 0;
909
910 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100911 RB_CLEAR_NODE(&waiter.pi_tree_entry);
912 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700913
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100914 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700915
916 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +0800917 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100918 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700919 return 0;
920 }
921
922 set_current_state(state);
923
924 /* Setup the timer, when timeout != NULL */
Peter Zijlstra720a2592008-02-13 15:45:36 +0100925 if (unlikely(timeout)) {
Arjan van de Vencc584b22008-09-01 15:02:30 -0700926 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Peter Zijlstra720a2592008-02-13 15:45:36 +0100927 if (!hrtimer_active(&timeout->timer))
928 timeout->task = NULL;
929 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700930
Lai Jiangshan81612392011-01-14 17:09:41 +0800931 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
932
933 if (likely(!ret))
934 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700935
936 set_current_state(TASK_RUNNING);
937
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200938 if (unlikely(ret)) {
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700939 remove_waiter(lock, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200940 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
941 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700942
943 /*
944 * try_to_take_rt_mutex() sets the waiter bit
945 * unconditionally. We might have to fix that up.
946 */
947 fixup_rt_mutex_waiters(lock);
948
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100949 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700950
951 /* Remove pending timer: */
952 if (unlikely(timeout))
953 hrtimer_cancel(&timeout->timer);
954
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700955 debug_rt_mutex_free_waiter(&waiter);
956
957 return ret;
958}
959
960/*
961 * Slow path try-lock function:
962 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +0200963static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700964{
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +0200965 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700966
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +0200967 /*
968 * If the lock already has an owner we fail to get the lock.
969 * This can be done without taking the @lock->wait_lock as
970 * it is only being read, and this is a trylock anyway.
971 */
972 if (rt_mutex_owner(lock))
973 return 0;
974
975 /*
976 * The mutex has currently no owner. Lock the wait lock and
977 * try to acquire the lock.
978 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100979 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700980
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +0200981 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700982
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +0200983 /*
984 * try_to_take_rt_mutex() sets the lock waiters bit
985 * unconditionally. Clean this up.
986 */
987 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700988
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100989 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700990
991 return ret;
992}
993
994/*
995 * Slow path to release a rt-mutex:
996 */
997static void __sched
998rt_mutex_slowunlock(struct rt_mutex *lock)
999{
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001000 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001001
1002 debug_rt_mutex_unlock(lock);
1003
1004 rt_mutex_deadlock_account_unlock(current);
1005
Thomas Gleixner27e35712014-06-11 18:44:04 +00001006 /*
1007 * We must be careful here if the fast path is enabled. If we
1008 * have no waiters queued we cannot set owner to NULL here
1009 * because of:
1010 *
1011 * foo->lock->owner = NULL;
1012 * rtmutex_lock(foo->lock); <- fast path
1013 * free = atomic_dec_and_test(foo->refcnt);
1014 * rtmutex_unlock(foo->lock); <- fast path
1015 * if (free)
1016 * kfree(foo);
1017 * raw_spin_unlock(foo->lock->wait_lock);
1018 *
1019 * So for the fastpath enabled kernel:
1020 *
1021 * Nothing can set the waiters bit as long as we hold
1022 * lock->wait_lock. So we do the following sequence:
1023 *
1024 * owner = rt_mutex_owner(lock);
1025 * clear_rt_mutex_waiters(lock);
1026 * raw_spin_unlock(&lock->wait_lock);
1027 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1028 * return;
1029 * goto retry;
1030 *
1031 * The fastpath disabled variant is simple as all access to
1032 * lock->owner is serialized by lock->wait_lock:
1033 *
1034 * lock->owner = NULL;
1035 * raw_spin_unlock(&lock->wait_lock);
1036 */
1037 while (!rt_mutex_has_waiters(lock)) {
1038 /* Drops lock->wait_lock ! */
1039 if (unlock_rt_mutex_safe(lock) == true)
1040 return;
1041 /* Relock the rtmutex and try again */
1042 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001043 }
1044
Thomas Gleixner27e35712014-06-11 18:44:04 +00001045 /*
1046 * The wakeup next waiter path does not suffer from the above
1047 * race. See the comments there.
1048 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001049 wakeup_next_waiter(lock);
1050
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001051 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001052
1053 /* Undo pi boosting if necessary: */
1054 rt_mutex_adjust_prio(current);
1055}
1056
1057/*
1058 * debug aware fast / slowpath lock,trylock,unlock
1059 *
1060 * The atomic acquire/release ops are compiled away, when either the
1061 * architecture does not support cmpxchg or when debugging is enabled.
1062 */
1063static inline int
1064rt_mutex_fastlock(struct rt_mutex *lock, int state,
1065 int detect_deadlock,
1066 int (*slowfn)(struct rt_mutex *lock, int state,
1067 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001068 int detect_deadlock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001069{
1070 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1071 rt_mutex_deadlock_account_lock(lock, current);
1072 return 0;
1073 } else
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001074 return slowfn(lock, state, NULL, detect_deadlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001075}
1076
1077static inline int
1078rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1079 struct hrtimer_sleeper *timeout, int detect_deadlock,
1080 int (*slowfn)(struct rt_mutex *lock, int state,
1081 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001082 int detect_deadlock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001083{
1084 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1085 rt_mutex_deadlock_account_lock(lock, current);
1086 return 0;
1087 } else
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001088 return slowfn(lock, state, timeout, detect_deadlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001089}
1090
1091static inline int
1092rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001093 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001094{
1095 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1096 rt_mutex_deadlock_account_lock(lock, current);
1097 return 1;
1098 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001099 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001100}
1101
1102static inline void
1103rt_mutex_fastunlock(struct rt_mutex *lock,
1104 void (*slowfn)(struct rt_mutex *lock))
1105{
1106 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1107 rt_mutex_deadlock_account_unlock(current);
1108 else
1109 slowfn(lock);
1110}
1111
1112/**
1113 * rt_mutex_lock - lock a rt_mutex
1114 *
1115 * @lock: the rt_mutex to be locked
1116 */
1117void __sched rt_mutex_lock(struct rt_mutex *lock)
1118{
1119 might_sleep();
1120
1121 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1122}
1123EXPORT_SYMBOL_GPL(rt_mutex_lock);
1124
1125/**
1126 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1127 *
1128 * @lock: the rt_mutex to be locked
1129 * @detect_deadlock: deadlock detection on/off
1130 *
1131 * Returns:
1132 * 0 on success
1133 * -EINTR when interrupted by a signal
1134 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1135 */
1136int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1137 int detect_deadlock)
1138{
1139 might_sleep();
1140
1141 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1142 detect_deadlock, rt_mutex_slowlock);
1143}
1144EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1145
1146/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001147 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1148 * the timeout structure is provided
1149 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001150 *
1151 * @lock: the rt_mutex to be locked
1152 * @timeout: timeout structure or NULL (no timeout)
1153 * @detect_deadlock: deadlock detection on/off
1154 *
1155 * Returns:
1156 * 0 on success
1157 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001158 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001159 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1160 */
1161int
1162rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1163 int detect_deadlock)
1164{
1165 might_sleep();
1166
1167 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1168 detect_deadlock, rt_mutex_slowlock);
1169}
1170EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1171
1172/**
1173 * rt_mutex_trylock - try to lock a rt_mutex
1174 *
1175 * @lock: the rt_mutex to be locked
1176 *
1177 * Returns 1 on success and 0 on contention
1178 */
1179int __sched rt_mutex_trylock(struct rt_mutex *lock)
1180{
1181 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1182}
1183EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1184
1185/**
1186 * rt_mutex_unlock - unlock a rt_mutex
1187 *
1188 * @lock: the rt_mutex to be unlocked
1189 */
1190void __sched rt_mutex_unlock(struct rt_mutex *lock)
1191{
1192 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1193}
1194EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1195
Luis Henriques23b94b92009-04-29 21:54:51 +01001196/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001197 * rt_mutex_destroy - mark a mutex unusable
1198 * @lock: the mutex to be destroyed
1199 *
1200 * This function marks the mutex uninitialized, and any subsequent
1201 * use of the mutex is forbidden. The mutex must not be locked when
1202 * this function is called.
1203 */
1204void rt_mutex_destroy(struct rt_mutex *lock)
1205{
1206 WARN_ON(rt_mutex_is_locked(lock));
1207#ifdef CONFIG_DEBUG_RT_MUTEXES
1208 lock->magic = NULL;
1209#endif
1210}
1211
1212EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1213
1214/**
1215 * __rt_mutex_init - initialize the rt lock
1216 *
1217 * @lock: the rt lock to be initialized
1218 *
1219 * Initialize the rt lock to unlocked state.
1220 *
1221 * Initializing of a locked rt lock is not allowed
1222 */
1223void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1224{
1225 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001226 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001227 lock->waiters = RB_ROOT;
1228 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001229
1230 debug_rt_mutex_init(lock, name);
1231}
1232EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001233
1234/**
1235 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1236 * proxy owner
1237 *
1238 * @lock: the rt_mutex to be locked
1239 * @proxy_owner:the task to set as owner
1240 *
1241 * No locking. Caller has to do serializing itself
1242 * Special API call for PI-futex support
1243 */
1244void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1245 struct task_struct *proxy_owner)
1246{
1247 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001248 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001249 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001250 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1251}
1252
1253/**
1254 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1255 *
1256 * @lock: the rt_mutex to be locked
1257 *
1258 * No locking. Caller has to do serializing itself
1259 * Special API call for PI-futex support
1260 */
1261void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1262 struct task_struct *proxy_owner)
1263{
1264 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001265 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001266 rt_mutex_deadlock_account_unlock(proxy_owner);
1267}
1268
1269/**
Darren Hart8dac4562009-04-03 13:40:12 -07001270 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1271 * @lock: the rt_mutex to take
1272 * @waiter: the pre-initialized rt_mutex_waiter
1273 * @task: the task to prepare
1274 * @detect_deadlock: perform deadlock detection (1) or not (0)
1275 *
1276 * Returns:
1277 * 0 - task blocked on lock
1278 * 1 - acquired the lock for task, caller should wake it up
1279 * <0 - error
1280 *
1281 * Special API call for FUTEX_REQUEUE_PI support.
1282 */
1283int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1284 struct rt_mutex_waiter *waiter,
1285 struct task_struct *task, int detect_deadlock)
1286{
1287 int ret;
1288
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001289 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001290
Lai Jiangshan81612392011-01-14 17:09:41 +08001291 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001292 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001293 return 1;
1294 }
1295
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001296 /* We enforce deadlock detection for futexes */
1297 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
Darren Hart8dac4562009-04-03 13:40:12 -07001298
Lai Jiangshan81612392011-01-14 17:09:41 +08001299 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001300 /*
1301 * Reset the return value. We might have
1302 * returned with -EDEADLK and the owner
1303 * released the lock while we were walking the
1304 * pi chain. Let the waiter sort it out.
1305 */
1306 ret = 0;
1307 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001308
1309 if (unlikely(ret))
1310 remove_waiter(lock, waiter);
1311
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001312 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001313
1314 debug_rt_mutex_print_deadlock(waiter);
1315
1316 return ret;
1317}
1318
1319/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001320 * rt_mutex_next_owner - return the next owner of the lock
1321 *
1322 * @lock: the rt lock query
1323 *
1324 * Returns the next owner of the lock or NULL
1325 *
1326 * Caller has to serialize against other accessors to the lock
1327 * itself.
1328 *
1329 * Special API call for PI-futex support
1330 */
1331struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1332{
1333 if (!rt_mutex_has_waiters(lock))
1334 return NULL;
1335
1336 return rt_mutex_top_waiter(lock)->task;
1337}
Darren Hart8dac4562009-04-03 13:40:12 -07001338
1339/**
1340 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1341 * @lock: the rt_mutex we were woken on
1342 * @to: the timeout, null if none. hrtimer should already have
1343 * been started.
1344 * @waiter: the pre-initialized rt_mutex_waiter
1345 * @detect_deadlock: perform deadlock detection (1) or not (0)
1346 *
1347 * Complete the lock acquisition started our behalf by another thread.
1348 *
1349 * Returns:
1350 * 0 - success
1351 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1352 *
1353 * Special API call for PI-futex requeue support
1354 */
1355int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1356 struct hrtimer_sleeper *to,
1357 struct rt_mutex_waiter *waiter,
1358 int detect_deadlock)
1359{
1360 int ret;
1361
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001362 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001363
1364 set_current_state(TASK_INTERRUPTIBLE);
1365
Lai Jiangshan81612392011-01-14 17:09:41 +08001366 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001367
1368 set_current_state(TASK_RUNNING);
1369
Lai Jiangshan81612392011-01-14 17:09:41 +08001370 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001371 remove_waiter(lock, waiter);
1372
1373 /*
1374 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1375 * have to fix that up.
1376 */
1377 fixup_rt_mutex_waiters(lock);
1378
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001379 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001380
Darren Hart8dac4562009-04-03 13:40:12 -07001381 return ret;
1382}