blob: 1e8fdabb19dedb894dc454fe1c2806f917a737b9 [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.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200340 *
341 * Chain walk basics and protection scope
342 *
343 * [R] refcount on task
344 * [P] task->pi_lock held
345 * [L] rtmutex->wait_lock held
346 *
347 * Step Description Protected by
348 * function arguments:
349 * @task [R]
350 * @orig_lock if != NULL @top_task is blocked on it
351 * @next_lock Unprotected. Cannot be
352 * dereferenced. Only used for
353 * comparison.
354 * @orig_waiter if != NULL @top_task is blocked on it
355 * @top_task current, or in case of proxy
356 * locking protected by calling
357 * code
358 * again:
359 * loop_sanity_check();
360 * retry:
361 * [1] lock(task->pi_lock); [R] acquire [P]
362 * [2] waiter = task->pi_blocked_on; [P]
363 * [3] check_exit_conditions_1(); [P]
364 * [4] lock = waiter->lock; [P]
365 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
366 * unlock(task->pi_lock); release [P]
367 * goto retry;
368 * }
369 * [6] check_exit_conditions_2(); [P] + [L]
370 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
371 * [8] unlock(task->pi_lock); release [P]
372 * put_task_struct(task); release [R]
373 * [9] check_exit_conditions_3(); [L]
374 * [10] task = owner(lock); [L]
375 * get_task_struct(task); [L] acquire [R]
376 * lock(task->pi_lock); [L] acquire [P]
377 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
378 * [12] check_exit_conditions_4(); [P] + [L]
379 * [13] unlock(task->pi_lock); release [P]
380 * unlock(lock->wait_lock); release [L]
381 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700382 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200383static int rt_mutex_adjust_prio_chain(struct task_struct *task,
384 int deadlock_detect,
385 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200386 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200387 struct rt_mutex_waiter *orig_waiter,
388 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700389{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700390 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000391 struct rt_mutex_waiter *prerequeue_top_waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700392 int detect_deadlock, ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000393 struct rt_mutex *lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700394 unsigned long flags;
395
396 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
397 deadlock_detect);
398
399 /*
400 * The (de)boosting is a step by step approach with a lot of
401 * pitfalls. We want this to be preemptible and we want hold a
402 * maximum of two locks per step. So we have to check
403 * carefully whether things change under us.
404 */
405 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200406 /*
407 * We limit the lock chain length for each invocation.
408 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700409 if (++depth > max_lock_depth) {
410 static int prev_max;
411
412 /*
413 * Print this only once. If the admin changes the limit,
414 * print a new message when reaching the limit again.
415 */
416 if (prev_max != max_lock_depth) {
417 prev_max = max_lock_depth;
418 printk(KERN_WARNING "Maximum lock depth %d reached "
419 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700420 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700421 }
422 put_task_struct(task);
423
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200424 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700425 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200426
427 /*
428 * We are fully preemptible here and only hold the refcount on
429 * @task. So everything can have changed under us since the
430 * caller or our own code below (goto retry/again) dropped all
431 * locks.
432 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700433 retry:
434 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200435 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700436 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100437 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700438
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200439 /*
440 * [2] Get the waiter on which @task is blocked on.
441 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700442 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200443
444 /*
445 * [3] check_exit_conditions_1() protected by task->pi_lock.
446 */
447
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700448 /*
449 * Check whether the end of the boosting chain has been
450 * reached or the state of the chain has changed while we
451 * dropped the locks.
452 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800453 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700454 goto out_unlock_pi;
455
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700456 /*
457 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800458 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700459 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800460 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700461 goto out_unlock_pi;
462
463 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200464 * We dropped all locks after taking a refcount on @task, so
465 * the task might have moved on in the lock chain or even left
466 * the chain completely and blocks now on an unrelated lock or
467 * on @orig_lock.
468 *
469 * We stored the lock on which @task was blocked in @next_lock,
470 * so we can detect the chain change.
471 */
472 if (next_lock != waiter->lock)
473 goto out_unlock_pi;
474
475 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700476 * Drop out, when the task has no waiters. Note,
477 * top_waiter can be NULL, when we are in the deboosting
478 * mode!
479 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000480 if (top_waiter) {
481 if (!task_has_pi_waiters(task))
482 goto out_unlock_pi;
483 /*
484 * If deadlock detection is off, we stop here if we
485 * are not the top pi waiter of the task.
486 */
487 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
488 goto out_unlock_pi;
489 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700490
491 /*
492 * When deadlock detection is off then we check, if further
493 * priority adjustment is necessary.
494 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100495 if (!detect_deadlock && waiter->prio == task->prio)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700496 goto out_unlock_pi;
497
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200498 /*
499 * [4] Get the next lock
500 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700501 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200502 /*
503 * [5] We need to trylock here as we are holding task->pi_lock,
504 * which is the reverse lock order versus the other rtmutex
505 * operations.
506 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100507 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100508 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700509 cpu_relax();
510 goto retry;
511 }
512
Thomas Gleixner397335f2014-05-22 03:25:39 +0000513 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200514 * [6] check_exit_conditions_2() protected by task->pi_lock and
515 * lock->wait_lock.
516 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000517 * Deadlock detection. If the lock is the same as the original
518 * lock which caused us to walk the lock chain or if the
519 * current lock is owned by the task which initiated the chain
520 * walk, we detected a deadlock.
521 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700522 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700523 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100524 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200525 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700526 goto out_unlock_pi;
527 }
528
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000529 /*
530 * Store the current top waiter before doing the requeue
531 * operation on @lock. We need it for the boost/deboost
532 * decision below.
533 */
534 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700535
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200536 /* [7] Requeue the waiter in the lock waiter list. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100537 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100538 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100539 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700540
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200541 /* [8] Release the task */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100542 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200543 put_task_struct(task);
544
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000545 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200546 * [9] check_exit_conditions_3 protected by lock->wait_lock.
547 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000548 * We must abort the chain walk if there is no lock owner even
549 * in the dead lock detection case, as we have nothing to
550 * follow here. This is the end of the chain we are walking.
551 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800552 if (!rt_mutex_owner(lock)) {
553 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200554 * If the requeue [7] above changed the top waiter,
555 * then we need to wake the new top waiter up to try
556 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800557 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000558 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800559 wake_up_process(rt_mutex_top_waiter(lock)->task);
560 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200561 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800562 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700563
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200564 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700565 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700566 get_task_struct(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100567 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700568
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200569 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700570 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000571 /*
572 * The waiter became the new top (highest priority)
573 * waiter on the lock. Replace the previous top waiter
574 * in the owner tasks pi waiters list with this waiter
575 * and adjust the priority of the owner.
576 */
577 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100578 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700579 __rt_mutex_adjust_prio(task);
580
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000581 } else if (prerequeue_top_waiter == waiter) {
582 /*
583 * The waiter was the top waiter on the lock, but is
584 * no longer the top prority waiter. Replace waiter in
585 * the owner tasks pi waiters list with the new top
586 * (highest priority) waiter and adjust the priority
587 * of the owner.
588 * The new top waiter is stored in @waiter so that
589 * @waiter == @top_waiter evaluates to true below and
590 * we continue to deboost the rest of the chain.
591 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100592 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700593 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100594 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700595 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000596 } else {
597 /*
598 * Nothing changed. No need to do any priority
599 * adjustment.
600 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700601 }
602
Thomas Gleixner82084982014-06-05 11:16:12 +0200603 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200604 * [12] check_exit_conditions_4() protected by task->pi_lock
605 * and lock->wait_lock. The actual decisions are made after we
606 * dropped the locks.
607 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200608 * Check whether the task which owns the current lock is pi
609 * blocked itself. If yes we store a pointer to the lock for
610 * the lock chain change detection above. After we dropped
611 * task->pi_lock next_lock cannot be dereferenced anymore.
612 */
613 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000614 /*
615 * Store the top waiter of @lock for the end of chain walk
616 * decision below.
617 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700618 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200619
620 /* [13] Drop the locks */
621 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100622 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700623
Thomas Gleixner82084982014-06-05 11:16:12 +0200624 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200625 * Make the actual exit decisions [12], based on the stored
626 * values.
627 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200628 * We reached the end of the lock chain. Stop right here. No
629 * point to go back just to figure that out.
630 */
631 if (!next_lock)
632 goto out_put_task;
633
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000634 /*
635 * If the current waiter is not the top waiter on the lock,
636 * then we can stop the chain walk here if we are not in full
637 * deadlock detection mode.
638 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700639 if (!detect_deadlock && waiter != top_waiter)
640 goto out_put_task;
641
642 goto again;
643
644 out_unlock_pi:
Thomas Gleixner1d615482009-11-17 14:54:03 +0100645 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700646 out_put_task:
647 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700648
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700649 return ret;
650}
651
652/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700653 * Try to take an rt-mutex
654 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700655 * Must be called with lock->wait_lock held.
Lai Jiangshan81612392011-01-14 17:09:41 +0800656 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200657 * @lock: The lock to be acquired.
658 * @task: The task which wants to acquire the lock
659 * @waiter: The waiter that is queued to the lock's wait list if the
660 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700661 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800662static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200663 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700664{
Thomas Gleixner358c3312014-06-11 01:01:13 +0200665 unsigned long flags;
666
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700667 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200668 * Before testing whether we can acquire @lock, we set the
669 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
670 * other tasks which try to modify @lock into the slow path
671 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700672 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200673 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
674 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700675 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200676 * - There is a lock owner. The caller must fixup the
677 * transient state if it does a trylock or leaves the lock
678 * function due to a signal or timeout.
679 *
680 * - @task acquires the lock and there are no other
681 * waiters. This is undone in rt_mutex_set_owner(@task) at
682 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700683 */
684 mark_rt_mutex_waiters(lock);
685
Thomas Gleixner358c3312014-06-11 01:01:13 +0200686 /*
687 * If @lock has an owner, give up.
688 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800689 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700690 return 0;
691
Lai Jiangshan81612392011-01-14 17:09:41 +0800692 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200693 * If @waiter != NULL, @task has already enqueued the waiter
694 * into @lock waiter list. If @waiter == NULL then this is a
695 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800696 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200697 if (waiter) {
698 /*
699 * If waiter is not the highest priority waiter of
700 * @lock, give up.
701 */
702 if (waiter != rt_mutex_top_waiter(lock))
703 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800704
705 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200706 * We can acquire the lock. Remove the waiter from the
707 * lock waiters list.
708 */
709 rt_mutex_dequeue(lock, waiter);
710
711 } else {
712 /*
713 * If the lock has waiters already we check whether @task is
714 * eligible to take over the lock.
715 *
716 * If there are no other waiters, @task can acquire
717 * the lock. @task->pi_blocked_on is NULL, so it does
718 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800719 */
720 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200721 /*
722 * If @task->prio is greater than or equal to
723 * the top waiter priority (kernel view),
724 * @task lost.
725 */
726 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
727 return 0;
728
729 /*
730 * The current top waiter stays enqueued. We
731 * don't have to change anything in the lock
732 * waiters order.
733 */
734 } else {
735 /*
736 * No waiters. Take the lock without the
737 * pi_lock dance.@task->pi_blocked_on is NULL
738 * and we have no waiters to enqueue in @task
739 * pi waiters list.
740 */
741 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800742 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800743 }
744
Thomas Gleixner358c3312014-06-11 01:01:13 +0200745 /*
746 * Clear @task->pi_blocked_on. Requires protection by
747 * @task->pi_lock. Redundant operation for the @waiter == NULL
748 * case, but conditionals are more expensive than a redundant
749 * store.
750 */
751 raw_spin_lock_irqsave(&task->pi_lock, flags);
752 task->pi_blocked_on = NULL;
753 /*
754 * Finish the lock acquisition. @task is the new owner. If
755 * other waiters exist we have to insert the highest priority
756 * waiter into @task->pi_waiters list.
757 */
758 if (rt_mutex_has_waiters(lock))
759 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
760 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
761
762takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700763 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700764 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700765
Thomas Gleixner358c3312014-06-11 01:01:13 +0200766 /*
767 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
768 * are still waiters or clears it.
769 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800770 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700771
Lai Jiangshan81612392011-01-14 17:09:41 +0800772 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700773
774 return 1;
775}
776
777/*
778 * Task blocks on lock.
779 *
780 * Prepare waiter and propagate pi chain
781 *
782 * This must be called with lock->wait_lock held.
783 */
784static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
785 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700786 struct task_struct *task,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700787 int detect_deadlock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700788{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700789 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700790 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200791 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700792 int chain_walk = 0, res;
Thomas Gleixner82084982014-06-05 11:16:12 +0200793 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700794
Thomas Gleixner397335f2014-05-22 03:25:39 +0000795 /*
796 * Early deadlock detection. We really don't want the task to
797 * enqueue on itself just to untangle the mess later. It's not
798 * only an optimization. We drop the locks, so another waiter
799 * can come in before the chain walk detects the deadlock. So
800 * the other will detect the deadlock and return -EDEADLOCK,
801 * which is wrong, as the other waiter is not in a deadlock
802 * situation.
803 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200804 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000805 return -EDEADLK;
806
Thomas Gleixner1d615482009-11-17 14:54:03 +0100807 raw_spin_lock_irqsave(&task->pi_lock, flags);
Darren Hart8dac4562009-04-03 13:40:12 -0700808 __rt_mutex_adjust_prio(task);
809 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700810 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100811 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700812
813 /* Get the top priority waiter on the lock */
814 if (rt_mutex_has_waiters(lock))
815 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100816 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700817
Darren Hart8dac4562009-04-03 13:40:12 -0700818 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700819
Thomas Gleixner1d615482009-11-17 14:54:03 +0100820 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700821
Lai Jiangshan81612392011-01-14 17:09:41 +0800822 if (!owner)
823 return 0;
824
Thomas Gleixner82084982014-06-05 11:16:12 +0200825 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700826 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100827 rt_mutex_dequeue_pi(owner, top_waiter);
828 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700829
830 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700831 if (owner->pi_blocked_on)
832 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200833 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700834 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200835 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700836
Thomas Gleixner82084982014-06-05 11:16:12 +0200837 /* Store the lock on which owner is blocked or NULL */
838 next_lock = task_blocked_on_lock(owner);
839
840 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
841 /*
842 * Even if full deadlock detection is on, if the owner is not
843 * blocked itself, we can avoid finding this out in the chain
844 * walk.
845 */
846 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700847 return 0;
848
Steven Rostedtdb630632006-09-29 01:59:44 -0700849 /*
850 * The owner can't disappear while holding a lock,
851 * so the owner struct is protected by wait_lock.
852 * Gets dropped in rt_mutex_adjust_prio_chain()!
853 */
854 get_task_struct(owner);
855
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100856 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700857
Thomas Gleixner82084982014-06-05 11:16:12 +0200858 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
859 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700860
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100861 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700862
863 return res;
864}
865
866/*
867 * Wake up the next waiter on the lock.
868 *
Thomas Gleixner27e35712014-06-11 18:44:04 +0000869 * Remove the top waiter from the current tasks pi waiter list and
870 * wake it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700871 *
872 * Called with lock->wait_lock held.
873 */
874static void wakeup_next_waiter(struct rt_mutex *lock)
875{
876 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700877 unsigned long flags;
878
Thomas Gleixner1d615482009-11-17 14:54:03 +0100879 raw_spin_lock_irqsave(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700880
881 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700882
883 /*
884 * Remove it from current->pi_waiters. We do not adjust a
885 * possible priority boost right now. We execute wakeup in the
886 * boosted mode and go back to normal after releasing
887 * lock->wait_lock.
888 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100889 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700890
Thomas Gleixner27e35712014-06-11 18:44:04 +0000891 /*
892 * As we are waking up the top waiter, and the waiter stays
893 * queued on the lock until it gets the lock, this lock
894 * obviously has waiters. Just set the bit here and this has
895 * the added benefit of forcing all new tasks into the
896 * slow path making sure no task of lower priority than
897 * the top waiter can steal this lock.
898 */
899 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700900
Thomas Gleixner1d615482009-11-17 14:54:03 +0100901 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700902
Thomas Gleixner27e35712014-06-11 18:44:04 +0000903 /*
904 * It's safe to dereference waiter as it cannot go away as
905 * long as we hold lock->wait_lock. The waiter task needs to
906 * acquire it in order to dequeue the waiter.
907 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800908 wake_up_process(waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700909}
910
911/*
Lai Jiangshan81612392011-01-14 17:09:41 +0800912 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700913 *
Lai Jiangshan81612392011-01-14 17:09:41 +0800914 * Must be called with lock->wait_lock held and
915 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700916 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200917static void remove_waiter(struct rt_mutex *lock,
918 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700919{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200920 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -0700921 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200922 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700923 unsigned long flags;
924
Thomas Gleixner1d615482009-11-17 14:54:03 +0100925 raw_spin_lock_irqsave(&current->pi_lock, flags);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100926 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700927 current->pi_blocked_on = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100928 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700929
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200930 /*
931 * Only update priority if the waiter was the highest priority
932 * waiter of the lock and there is an owner to update.
933 */
934 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +0800935 return;
936
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200937 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700938
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200939 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700940
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200941 if (rt_mutex_has_waiters(lock))
942 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700943
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200944 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700945
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200946 /* Store the lock on which owner is blocked or NULL */
947 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700948
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200949 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
Steven Rostedtdb630632006-09-29 01:59:44 -0700950
Thomas Gleixner1ca7b862014-06-07 09:36:13 +0200951 /*
952 * Don't walk the chain, if the owner task is not blocked
953 * itself.
954 */
Thomas Gleixner82084982014-06-05 11:16:12 +0200955 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700956 return;
957
Steven Rostedtdb630632006-09-29 01:59:44 -0700958 /* gets dropped in rt_mutex_adjust_prio_chain()! */
959 get_task_struct(owner);
960
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100961 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700962
Thomas Gleixner82084982014-06-05 11:16:12 +0200963 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700964
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100965 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700966}
967
968/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700969 * Recheck the pi chain, in case we got a priority setting
970 *
971 * Called from sched_setscheduler
972 */
973void rt_mutex_adjust_pi(struct task_struct *task)
974{
975 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200976 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700977 unsigned long flags;
978
Thomas Gleixner1d615482009-11-17 14:54:03 +0100979 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700980
981 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100982 if (!waiter || (waiter->prio == task->prio &&
983 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100984 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700985 return;
986 }
Thomas Gleixner82084982014-06-05 11:16:12 +0200987 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100988 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700989
Steven Rostedtdb630632006-09-29 01:59:44 -0700990 /* gets dropped in rt_mutex_adjust_prio_chain()! */
991 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +0200992
993 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700994}
995
Darren Hart8dac4562009-04-03 13:40:12 -0700996/**
997 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
998 * @lock: the rt_mutex to take
999 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1000 * or TASK_UNINTERRUPTIBLE)
1001 * @timeout: the pre-initialized and started timer, or NULL for none
1002 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001003 *
1004 * lock->wait_lock must be held by the caller.
1005 */
1006static int __sched
1007__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1008 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001009 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001010{
1011 int ret = 0;
1012
1013 for (;;) {
1014 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001015 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001016 break;
1017
1018 /*
1019 * TASK_INTERRUPTIBLE checks for signals and
1020 * timeout. Ignored otherwise.
1021 */
1022 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1023 /* Signal pending? */
1024 if (signal_pending(current))
1025 ret = -EINTR;
1026 if (timeout && !timeout->task)
1027 ret = -ETIMEDOUT;
1028 if (ret)
1029 break;
1030 }
1031
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001032 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001033
1034 debug_rt_mutex_print_deadlock(waiter);
1035
Lai Jiangshan81612392011-01-14 17:09:41 +08001036 schedule_rt_mutex(lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001037
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001038 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001039 set_current_state(state);
1040 }
1041
1042 return ret;
1043}
1044
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001045static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1046 struct rt_mutex_waiter *w)
1047{
1048 /*
1049 * If the result is not -EDEADLOCK or the caller requested
1050 * deadlock detection, nothing to do here.
1051 */
1052 if (res != -EDEADLOCK || detect_deadlock)
1053 return;
1054
1055 /*
1056 * Yell lowdly and stop the task right here.
1057 */
1058 rt_mutex_print_deadlock(w);
1059 while (1) {
1060 set_current_state(TASK_INTERRUPTIBLE);
1061 schedule();
1062 }
1063}
1064
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001065/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001066 * Slow path lock function:
1067 */
1068static int __sched
1069rt_mutex_slowlock(struct rt_mutex *lock, int state,
1070 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001071 int detect_deadlock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001072{
1073 struct rt_mutex_waiter waiter;
1074 int ret = 0;
1075
1076 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001077 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1078 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001079
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001080 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001081
1082 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001083 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001084 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001085 return 0;
1086 }
1087
1088 set_current_state(state);
1089
1090 /* Setup the timer, when timeout != NULL */
Peter Zijlstra720a2592008-02-13 15:45:36 +01001091 if (unlikely(timeout)) {
Arjan van de Vencc584b22008-09-01 15:02:30 -07001092 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Peter Zijlstra720a2592008-02-13 15:45:36 +01001093 if (!hrtimer_active(&timeout->timer))
1094 timeout->task = NULL;
1095 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001096
Lai Jiangshan81612392011-01-14 17:09:41 +08001097 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
1098
1099 if (likely(!ret))
1100 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001101
1102 set_current_state(TASK_RUNNING);
1103
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001104 if (unlikely(ret)) {
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001105 remove_waiter(lock, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001106 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
1107 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001108
1109 /*
1110 * try_to_take_rt_mutex() sets the waiter bit
1111 * unconditionally. We might have to fix that up.
1112 */
1113 fixup_rt_mutex_waiters(lock);
1114
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001115 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001116
1117 /* Remove pending timer: */
1118 if (unlikely(timeout))
1119 hrtimer_cancel(&timeout->timer);
1120
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001121 debug_rt_mutex_free_waiter(&waiter);
1122
1123 return ret;
1124}
1125
1126/*
1127 * Slow path try-lock function:
1128 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001129static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001130{
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001131 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001132
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001133 /*
1134 * If the lock already has an owner we fail to get the lock.
1135 * This can be done without taking the @lock->wait_lock as
1136 * it is only being read, and this is a trylock anyway.
1137 */
1138 if (rt_mutex_owner(lock))
1139 return 0;
1140
1141 /*
1142 * The mutex has currently no owner. Lock the wait lock and
1143 * try to acquire the lock.
1144 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001145 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001146
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001147 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001148
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001149 /*
1150 * try_to_take_rt_mutex() sets the lock waiters bit
1151 * unconditionally. Clean this up.
1152 */
1153 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001154
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001155 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001156
1157 return ret;
1158}
1159
1160/*
1161 * Slow path to release a rt-mutex:
1162 */
1163static void __sched
1164rt_mutex_slowunlock(struct rt_mutex *lock)
1165{
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001166 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001167
1168 debug_rt_mutex_unlock(lock);
1169
1170 rt_mutex_deadlock_account_unlock(current);
1171
Thomas Gleixner27e35712014-06-11 18:44:04 +00001172 /*
1173 * We must be careful here if the fast path is enabled. If we
1174 * have no waiters queued we cannot set owner to NULL here
1175 * because of:
1176 *
1177 * foo->lock->owner = NULL;
1178 * rtmutex_lock(foo->lock); <- fast path
1179 * free = atomic_dec_and_test(foo->refcnt);
1180 * rtmutex_unlock(foo->lock); <- fast path
1181 * if (free)
1182 * kfree(foo);
1183 * raw_spin_unlock(foo->lock->wait_lock);
1184 *
1185 * So for the fastpath enabled kernel:
1186 *
1187 * Nothing can set the waiters bit as long as we hold
1188 * lock->wait_lock. So we do the following sequence:
1189 *
1190 * owner = rt_mutex_owner(lock);
1191 * clear_rt_mutex_waiters(lock);
1192 * raw_spin_unlock(&lock->wait_lock);
1193 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1194 * return;
1195 * goto retry;
1196 *
1197 * The fastpath disabled variant is simple as all access to
1198 * lock->owner is serialized by lock->wait_lock:
1199 *
1200 * lock->owner = NULL;
1201 * raw_spin_unlock(&lock->wait_lock);
1202 */
1203 while (!rt_mutex_has_waiters(lock)) {
1204 /* Drops lock->wait_lock ! */
1205 if (unlock_rt_mutex_safe(lock) == true)
1206 return;
1207 /* Relock the rtmutex and try again */
1208 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001209 }
1210
Thomas Gleixner27e35712014-06-11 18:44:04 +00001211 /*
1212 * The wakeup next waiter path does not suffer from the above
1213 * race. See the comments there.
1214 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001215 wakeup_next_waiter(lock);
1216
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001217 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001218
1219 /* Undo pi boosting if necessary: */
1220 rt_mutex_adjust_prio(current);
1221}
1222
1223/*
1224 * debug aware fast / slowpath lock,trylock,unlock
1225 *
1226 * The atomic acquire/release ops are compiled away, when either the
1227 * architecture does not support cmpxchg or when debugging is enabled.
1228 */
1229static inline int
1230rt_mutex_fastlock(struct rt_mutex *lock, int state,
1231 int detect_deadlock,
1232 int (*slowfn)(struct rt_mutex *lock, int state,
1233 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001234 int detect_deadlock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001235{
1236 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1237 rt_mutex_deadlock_account_lock(lock, current);
1238 return 0;
1239 } else
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001240 return slowfn(lock, state, NULL, detect_deadlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001241}
1242
1243static inline int
1244rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1245 struct hrtimer_sleeper *timeout, int detect_deadlock,
1246 int (*slowfn)(struct rt_mutex *lock, int state,
1247 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001248 int detect_deadlock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001249{
1250 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1251 rt_mutex_deadlock_account_lock(lock, current);
1252 return 0;
1253 } else
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001254 return slowfn(lock, state, timeout, detect_deadlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001255}
1256
1257static inline int
1258rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001259 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001260{
1261 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1262 rt_mutex_deadlock_account_lock(lock, current);
1263 return 1;
1264 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001265 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001266}
1267
1268static inline void
1269rt_mutex_fastunlock(struct rt_mutex *lock,
1270 void (*slowfn)(struct rt_mutex *lock))
1271{
1272 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1273 rt_mutex_deadlock_account_unlock(current);
1274 else
1275 slowfn(lock);
1276}
1277
1278/**
1279 * rt_mutex_lock - lock a rt_mutex
1280 *
1281 * @lock: the rt_mutex to be locked
1282 */
1283void __sched rt_mutex_lock(struct rt_mutex *lock)
1284{
1285 might_sleep();
1286
1287 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
1288}
1289EXPORT_SYMBOL_GPL(rt_mutex_lock);
1290
1291/**
1292 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1293 *
1294 * @lock: the rt_mutex to be locked
1295 * @detect_deadlock: deadlock detection on/off
1296 *
1297 * Returns:
1298 * 0 on success
1299 * -EINTR when interrupted by a signal
1300 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1301 */
1302int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
1303 int detect_deadlock)
1304{
1305 might_sleep();
1306
1307 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1308 detect_deadlock, rt_mutex_slowlock);
1309}
1310EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1311
1312/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001313 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1314 * the timeout structure is provided
1315 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001316 *
1317 * @lock: the rt_mutex to be locked
1318 * @timeout: timeout structure or NULL (no timeout)
1319 * @detect_deadlock: deadlock detection on/off
1320 *
1321 * Returns:
1322 * 0 on success
1323 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001324 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001325 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1326 */
1327int
1328rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1329 int detect_deadlock)
1330{
1331 might_sleep();
1332
1333 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1334 detect_deadlock, rt_mutex_slowlock);
1335}
1336EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1337
1338/**
1339 * rt_mutex_trylock - try to lock a rt_mutex
1340 *
1341 * @lock: the rt_mutex to be locked
1342 *
1343 * Returns 1 on success and 0 on contention
1344 */
1345int __sched rt_mutex_trylock(struct rt_mutex *lock)
1346{
1347 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1348}
1349EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1350
1351/**
1352 * rt_mutex_unlock - unlock a rt_mutex
1353 *
1354 * @lock: the rt_mutex to be unlocked
1355 */
1356void __sched rt_mutex_unlock(struct rt_mutex *lock)
1357{
1358 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1359}
1360EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1361
Luis Henriques23b94b92009-04-29 21:54:51 +01001362/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001363 * rt_mutex_destroy - mark a mutex unusable
1364 * @lock: the mutex to be destroyed
1365 *
1366 * This function marks the mutex uninitialized, and any subsequent
1367 * use of the mutex is forbidden. The mutex must not be locked when
1368 * this function is called.
1369 */
1370void rt_mutex_destroy(struct rt_mutex *lock)
1371{
1372 WARN_ON(rt_mutex_is_locked(lock));
1373#ifdef CONFIG_DEBUG_RT_MUTEXES
1374 lock->magic = NULL;
1375#endif
1376}
1377
1378EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1379
1380/**
1381 * __rt_mutex_init - initialize the rt lock
1382 *
1383 * @lock: the rt lock to be initialized
1384 *
1385 * Initialize the rt lock to unlocked state.
1386 *
1387 * Initializing of a locked rt lock is not allowed
1388 */
1389void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1390{
1391 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001392 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001393 lock->waiters = RB_ROOT;
1394 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001395
1396 debug_rt_mutex_init(lock, name);
1397}
1398EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001399
1400/**
1401 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1402 * proxy owner
1403 *
1404 * @lock: the rt_mutex to be locked
1405 * @proxy_owner:the task to set as owner
1406 *
1407 * No locking. Caller has to do serializing itself
1408 * Special API call for PI-futex support
1409 */
1410void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1411 struct task_struct *proxy_owner)
1412{
1413 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001414 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001415 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001416 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1417}
1418
1419/**
1420 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1421 *
1422 * @lock: the rt_mutex to be locked
1423 *
1424 * No locking. Caller has to do serializing itself
1425 * Special API call for PI-futex support
1426 */
1427void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1428 struct task_struct *proxy_owner)
1429{
1430 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001431 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001432 rt_mutex_deadlock_account_unlock(proxy_owner);
1433}
1434
1435/**
Darren Hart8dac4562009-04-03 13:40:12 -07001436 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1437 * @lock: the rt_mutex to take
1438 * @waiter: the pre-initialized rt_mutex_waiter
1439 * @task: the task to prepare
1440 * @detect_deadlock: perform deadlock detection (1) or not (0)
1441 *
1442 * Returns:
1443 * 0 - task blocked on lock
1444 * 1 - acquired the lock for task, caller should wake it up
1445 * <0 - error
1446 *
1447 * Special API call for FUTEX_REQUEUE_PI support.
1448 */
1449int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1450 struct rt_mutex_waiter *waiter,
1451 struct task_struct *task, int detect_deadlock)
1452{
1453 int ret;
1454
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001455 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001456
Lai Jiangshan81612392011-01-14 17:09:41 +08001457 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001458 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001459 return 1;
1460 }
1461
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001462 /* We enforce deadlock detection for futexes */
1463 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
Darren Hart8dac4562009-04-03 13:40:12 -07001464
Lai Jiangshan81612392011-01-14 17:09:41 +08001465 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001466 /*
1467 * Reset the return value. We might have
1468 * returned with -EDEADLK and the owner
1469 * released the lock while we were walking the
1470 * pi chain. Let the waiter sort it out.
1471 */
1472 ret = 0;
1473 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001474
1475 if (unlikely(ret))
1476 remove_waiter(lock, waiter);
1477
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001478 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001479
1480 debug_rt_mutex_print_deadlock(waiter);
1481
1482 return ret;
1483}
1484
1485/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001486 * rt_mutex_next_owner - return the next owner of the lock
1487 *
1488 * @lock: the rt lock query
1489 *
1490 * Returns the next owner of the lock or NULL
1491 *
1492 * Caller has to serialize against other accessors to the lock
1493 * itself.
1494 *
1495 * Special API call for PI-futex support
1496 */
1497struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1498{
1499 if (!rt_mutex_has_waiters(lock))
1500 return NULL;
1501
1502 return rt_mutex_top_waiter(lock)->task;
1503}
Darren Hart8dac4562009-04-03 13:40:12 -07001504
1505/**
1506 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1507 * @lock: the rt_mutex we were woken on
1508 * @to: the timeout, null if none. hrtimer should already have
1509 * been started.
1510 * @waiter: the pre-initialized rt_mutex_waiter
1511 * @detect_deadlock: perform deadlock detection (1) or not (0)
1512 *
1513 * Complete the lock acquisition started our behalf by another thread.
1514 *
1515 * Returns:
1516 * 0 - success
1517 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1518 *
1519 * Special API call for PI-futex requeue support
1520 */
1521int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1522 struct hrtimer_sleeper *to,
1523 struct rt_mutex_waiter *waiter,
1524 int detect_deadlock)
1525{
1526 int ret;
1527
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001528 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001529
1530 set_current_state(TASK_INTERRUPTIBLE);
1531
Lai Jiangshan81612392011-01-14 17:09:41 +08001532 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001533
1534 set_current_state(TASK_RUNNING);
1535
Lai Jiangshan81612392011-01-14 17:09:41 +08001536 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001537 remove_waiter(lock, waiter);
1538
1539 /*
1540 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1541 * have to fix that up.
1542 */
1543 fixup_rt_mutex_waiters(lock);
1544
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001545 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001546
Darren Hart8dac4562009-04-03 13:40:12 -07001547 return ret;
1548}