blob: 5fa042be94d5dc958e1668befb272172001bd276 [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 *
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -070011 * See Documentation/locking/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/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +010073 * We can speed up the acquire/release, if there's no debugging state to be
74 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +020075 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +010076#ifndef CONFIG_DEBUG_RT_MUTEXES
Thomas Gleixnerbd197232007-06-17 21:11:10 +020077# 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/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000311 * Deadlock detection is conditional:
312 *
313 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
314 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
315 *
316 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
317 * conducted independent of the detect argument.
318 *
319 * If the waiter argument is NULL this indicates the deboost path and
320 * deadlock detection is disabled independent of the detect argument
321 * and the config settings.
322 */
323static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
324 enum rtmutex_chainwalk chwalk)
325{
326 /*
327 * This is just a wrapper function for the following call,
328 * because debug_rt_mutex_detect_deadlock() smells like a magic
329 * debug feature and I wanted to keep the cond function in the
330 * main source file along with the comments instead of having
331 * two of the same in the headers.
332 */
333 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
334}
335
336/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700337 * Max number of times we'll walk the boosting chain:
338 */
339int max_lock_depth = 1024;
340
Thomas Gleixner82084982014-06-05 11:16:12 +0200341static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
342{
343 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
344}
345
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700346/*
347 * Adjust the priority chain. Also used for deadlock detection.
348 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200349 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200350 * @task: the task owning the mutex (owner) for which a chain walk is
351 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900352 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200353 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
354 * things for a task that has just got its priority adjusted, and
355 * is waiting on a mutex)
356 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
357 * we dropped its pi_lock. Is never dereferenced, only used for
358 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200359 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200360 * its priority to the mutex owner (can be NULL in the case
361 * depicted above or if the top waiter is gone away and we are
362 * actually deboosting the owner)
363 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200364 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700365 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200366 *
367 * Chain walk basics and protection scope
368 *
369 * [R] refcount on task
370 * [P] task->pi_lock held
371 * [L] rtmutex->wait_lock held
372 *
373 * Step Description Protected by
374 * function arguments:
375 * @task [R]
376 * @orig_lock if != NULL @top_task is blocked on it
377 * @next_lock Unprotected. Cannot be
378 * dereferenced. Only used for
379 * comparison.
380 * @orig_waiter if != NULL @top_task is blocked on it
381 * @top_task current, or in case of proxy
382 * locking protected by calling
383 * code
384 * again:
385 * loop_sanity_check();
386 * retry:
387 * [1] lock(task->pi_lock); [R] acquire [P]
388 * [2] waiter = task->pi_blocked_on; [P]
389 * [3] check_exit_conditions_1(); [P]
390 * [4] lock = waiter->lock; [P]
391 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
392 * unlock(task->pi_lock); release [P]
393 * goto retry;
394 * }
395 * [6] check_exit_conditions_2(); [P] + [L]
396 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
397 * [8] unlock(task->pi_lock); release [P]
398 * put_task_struct(task); release [R]
399 * [9] check_exit_conditions_3(); [L]
400 * [10] task = owner(lock); [L]
401 * get_task_struct(task); [L] acquire [R]
402 * lock(task->pi_lock); [L] acquire [P]
403 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
404 * [12] check_exit_conditions_4(); [P] + [L]
405 * [13] unlock(task->pi_lock); release [P]
406 * unlock(lock->wait_lock); release [L]
407 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700408 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200409static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000410 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200411 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200412 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200413 struct rt_mutex_waiter *orig_waiter,
414 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700415{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700416 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000417 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000418 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000419 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000420 bool detect_deadlock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700421 unsigned long flags;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000422 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700423
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000424 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700425
426 /*
427 * The (de)boosting is a step by step approach with a lot of
428 * pitfalls. We want this to be preemptible and we want hold a
429 * maximum of two locks per step. So we have to check
430 * carefully whether things change under us.
431 */
432 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200433 /*
434 * We limit the lock chain length for each invocation.
435 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700436 if (++depth > max_lock_depth) {
437 static int prev_max;
438
439 /*
440 * Print this only once. If the admin changes the limit,
441 * print a new message when reaching the limit again.
442 */
443 if (prev_max != max_lock_depth) {
444 prev_max = max_lock_depth;
445 printk(KERN_WARNING "Maximum lock depth %d reached "
446 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700447 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700448 }
449 put_task_struct(task);
450
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200451 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700452 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200453
454 /*
455 * We are fully preemptible here and only hold the refcount on
456 * @task. So everything can have changed under us since the
457 * caller or our own code below (goto retry/again) dropped all
458 * locks.
459 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700460 retry:
461 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200462 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700463 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100464 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700465
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200466 /*
467 * [2] Get the waiter on which @task is blocked on.
468 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700469 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200470
471 /*
472 * [3] check_exit_conditions_1() protected by task->pi_lock.
473 */
474
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700475 /*
476 * Check whether the end of the boosting chain has been
477 * reached or the state of the chain has changed while we
478 * dropped the locks.
479 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800480 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700481 goto out_unlock_pi;
482
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700483 /*
484 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800485 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700486 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800487 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700488 goto out_unlock_pi;
489
490 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200491 * We dropped all locks after taking a refcount on @task, so
492 * the task might have moved on in the lock chain or even left
493 * the chain completely and blocks now on an unrelated lock or
494 * on @orig_lock.
495 *
496 * We stored the lock on which @task was blocked in @next_lock,
497 * so we can detect the chain change.
498 */
499 if (next_lock != waiter->lock)
500 goto out_unlock_pi;
501
502 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700503 * Drop out, when the task has no waiters. Note,
504 * top_waiter can be NULL, when we are in the deboosting
505 * mode!
506 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000507 if (top_waiter) {
508 if (!task_has_pi_waiters(task))
509 goto out_unlock_pi;
510 /*
511 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000512 * are not the top pi waiter of the task. If deadlock
513 * detection is enabled we continue, but stop the
514 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000515 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000516 if (top_waiter != task_top_pi_waiter(task)) {
517 if (!detect_deadlock)
518 goto out_unlock_pi;
519 else
520 requeue = false;
521 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000522 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700523
524 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000525 * If the waiter priority is the same as the task priority
526 * then there is no further priority adjustment necessary. If
527 * deadlock detection is off, we stop the chain walk. If its
528 * enabled we continue, but stop the requeueing in the chain
529 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700530 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000531 if (waiter->prio == task->prio) {
532 if (!detect_deadlock)
533 goto out_unlock_pi;
534 else
535 requeue = false;
536 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700537
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200538 /*
539 * [4] Get the next lock
540 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700541 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200542 /*
543 * [5] We need to trylock here as we are holding task->pi_lock,
544 * which is the reverse lock order versus the other rtmutex
545 * operations.
546 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100547 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100548 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700549 cpu_relax();
550 goto retry;
551 }
552
Thomas Gleixner397335f2014-05-22 03:25:39 +0000553 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200554 * [6] check_exit_conditions_2() protected by task->pi_lock and
555 * lock->wait_lock.
556 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000557 * Deadlock detection. If the lock is the same as the original
558 * lock which caused us to walk the lock chain or if the
559 * current lock is owned by the task which initiated the chain
560 * walk, we detected a deadlock.
561 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700562 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000563 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100564 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200565 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700566 goto out_unlock_pi;
567 }
568
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000569 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000570 * If we just follow the lock chain for deadlock detection, no
571 * need to do all the requeue operations. To avoid a truckload
572 * of conditionals around the various places below, just do the
573 * minimum chain walk checks.
574 */
575 if (!requeue) {
576 /*
577 * No requeue[7] here. Just release @task [8]
578 */
579 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
580 put_task_struct(task);
581
582 /*
583 * [9] check_exit_conditions_3 protected by lock->wait_lock.
584 * If there is no owner of the lock, end of chain.
585 */
586 if (!rt_mutex_owner(lock)) {
587 raw_spin_unlock(&lock->wait_lock);
588 return 0;
589 }
590
591 /* [10] Grab the next task, i.e. owner of @lock */
592 task = rt_mutex_owner(lock);
593 get_task_struct(task);
594 raw_spin_lock_irqsave(&task->pi_lock, flags);
595
596 /*
597 * No requeue [11] here. We just do deadlock detection.
598 *
599 * [12] Store whether owner is blocked
600 * itself. Decision is made after dropping the locks
601 */
602 next_lock = task_blocked_on_lock(task);
603 /*
604 * Get the top waiter for the next iteration
605 */
606 top_waiter = rt_mutex_top_waiter(lock);
607
608 /* [13] Drop locks */
609 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
610 raw_spin_unlock(&lock->wait_lock);
611
612 /* If owner is not blocked, end of chain. */
613 if (!next_lock)
614 goto out_put_task;
615 goto again;
616 }
617
618 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000619 * Store the current top waiter before doing the requeue
620 * operation on @lock. We need it for the boost/deboost
621 * decision below.
622 */
623 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700624
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200625 /* [7] Requeue the waiter in the lock waiter list. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100626 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100627 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100628 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700629
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200630 /* [8] Release the task */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100631 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200632 put_task_struct(task);
633
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000634 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200635 * [9] check_exit_conditions_3 protected by lock->wait_lock.
636 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000637 * We must abort the chain walk if there is no lock owner even
638 * in the dead lock detection case, as we have nothing to
639 * follow here. This is the end of the chain we are walking.
640 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800641 if (!rt_mutex_owner(lock)) {
642 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200643 * If the requeue [7] above changed the top waiter,
644 * then we need to wake the new top waiter up to try
645 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800646 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000647 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800648 wake_up_process(rt_mutex_top_waiter(lock)->task);
649 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200650 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800651 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700652
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200653 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700654 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700655 get_task_struct(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100656 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700657
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200658 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700659 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000660 /*
661 * The waiter became the new top (highest priority)
662 * waiter on the lock. Replace the previous top waiter
663 * in the owner tasks pi waiters list with this waiter
664 * and adjust the priority of the owner.
665 */
666 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100667 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700668 __rt_mutex_adjust_prio(task);
669
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000670 } else if (prerequeue_top_waiter == waiter) {
671 /*
672 * The waiter was the top waiter on the lock, but is
673 * no longer the top prority waiter. Replace waiter in
674 * the owner tasks pi waiters list with the new top
675 * (highest priority) waiter and adjust the priority
676 * of the owner.
677 * The new top waiter is stored in @waiter so that
678 * @waiter == @top_waiter evaluates to true below and
679 * we continue to deboost the rest of the chain.
680 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100681 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700682 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100683 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700684 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000685 } else {
686 /*
687 * Nothing changed. No need to do any priority
688 * adjustment.
689 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700690 }
691
Thomas Gleixner82084982014-06-05 11:16:12 +0200692 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200693 * [12] check_exit_conditions_4() protected by task->pi_lock
694 * and lock->wait_lock. The actual decisions are made after we
695 * dropped the locks.
696 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200697 * Check whether the task which owns the current lock is pi
698 * blocked itself. If yes we store a pointer to the lock for
699 * the lock chain change detection above. After we dropped
700 * task->pi_lock next_lock cannot be dereferenced anymore.
701 */
702 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000703 /*
704 * Store the top waiter of @lock for the end of chain walk
705 * decision below.
706 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700707 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200708
709 /* [13] Drop the locks */
710 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100711 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700712
Thomas Gleixner82084982014-06-05 11:16:12 +0200713 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200714 * Make the actual exit decisions [12], based on the stored
715 * values.
716 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200717 * We reached the end of the lock chain. Stop right here. No
718 * point to go back just to figure that out.
719 */
720 if (!next_lock)
721 goto out_put_task;
722
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000723 /*
724 * If the current waiter is not the top waiter on the lock,
725 * then we can stop the chain walk here if we are not in full
726 * deadlock detection mode.
727 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700728 if (!detect_deadlock && waiter != top_waiter)
729 goto out_put_task;
730
731 goto again;
732
733 out_unlock_pi:
Thomas Gleixner1d615482009-11-17 14:54:03 +0100734 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700735 out_put_task:
736 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700737
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700738 return ret;
739}
740
741/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700742 * Try to take an rt-mutex
743 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700744 * Must be called with lock->wait_lock held.
Lai Jiangshan81612392011-01-14 17:09:41 +0800745 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200746 * @lock: The lock to be acquired.
747 * @task: The task which wants to acquire the lock
748 * @waiter: The waiter that is queued to the lock's wait list if the
749 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700750 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800751static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200752 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700753{
Thomas Gleixner358c3312014-06-11 01:01:13 +0200754 unsigned long flags;
755
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700756 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200757 * Before testing whether we can acquire @lock, we set the
758 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
759 * other tasks which try to modify @lock into the slow path
760 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700761 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200762 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
763 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700764 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200765 * - There is a lock owner. The caller must fixup the
766 * transient state if it does a trylock or leaves the lock
767 * function due to a signal or timeout.
768 *
769 * - @task acquires the lock and there are no other
770 * waiters. This is undone in rt_mutex_set_owner(@task) at
771 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700772 */
773 mark_rt_mutex_waiters(lock);
774
Thomas Gleixner358c3312014-06-11 01:01:13 +0200775 /*
776 * If @lock has an owner, give up.
777 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800778 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700779 return 0;
780
Lai Jiangshan81612392011-01-14 17:09:41 +0800781 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200782 * If @waiter != NULL, @task has already enqueued the waiter
783 * into @lock waiter list. If @waiter == NULL then this is a
784 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800785 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200786 if (waiter) {
787 /*
788 * If waiter is not the highest priority waiter of
789 * @lock, give up.
790 */
791 if (waiter != rt_mutex_top_waiter(lock))
792 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800793
794 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200795 * We can acquire the lock. Remove the waiter from the
796 * lock waiters list.
797 */
798 rt_mutex_dequeue(lock, waiter);
799
800 } else {
801 /*
802 * If the lock has waiters already we check whether @task is
803 * eligible to take over the lock.
804 *
805 * If there are no other waiters, @task can acquire
806 * the lock. @task->pi_blocked_on is NULL, so it does
807 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800808 */
809 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200810 /*
811 * If @task->prio is greater than or equal to
812 * the top waiter priority (kernel view),
813 * @task lost.
814 */
815 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
816 return 0;
817
818 /*
819 * The current top waiter stays enqueued. We
820 * don't have to change anything in the lock
821 * waiters order.
822 */
823 } else {
824 /*
825 * No waiters. Take the lock without the
826 * pi_lock dance.@task->pi_blocked_on is NULL
827 * and we have no waiters to enqueue in @task
828 * pi waiters list.
829 */
830 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800831 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800832 }
833
Thomas Gleixner358c3312014-06-11 01:01:13 +0200834 /*
835 * Clear @task->pi_blocked_on. Requires protection by
836 * @task->pi_lock. Redundant operation for the @waiter == NULL
837 * case, but conditionals are more expensive than a redundant
838 * store.
839 */
840 raw_spin_lock_irqsave(&task->pi_lock, flags);
841 task->pi_blocked_on = NULL;
842 /*
843 * Finish the lock acquisition. @task is the new owner. If
844 * other waiters exist we have to insert the highest priority
845 * waiter into @task->pi_waiters list.
846 */
847 if (rt_mutex_has_waiters(lock))
848 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
849 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
850
851takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700852 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700853 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700854
Thomas Gleixner358c3312014-06-11 01:01:13 +0200855 /*
856 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
857 * are still waiters or clears it.
858 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800859 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700860
Lai Jiangshan81612392011-01-14 17:09:41 +0800861 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700862
863 return 1;
864}
865
866/*
867 * Task blocks on lock.
868 *
869 * Prepare waiter and propagate pi chain
870 *
871 * This must be called with lock->wait_lock held.
872 */
873static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
874 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700875 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000876 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700877{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700878 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700879 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200880 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700881 int chain_walk = 0, res;
Thomas Gleixner82084982014-06-05 11:16:12 +0200882 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700883
Thomas Gleixner397335f2014-05-22 03:25:39 +0000884 /*
885 * Early deadlock detection. We really don't want the task to
886 * enqueue on itself just to untangle the mess later. It's not
887 * only an optimization. We drop the locks, so another waiter
888 * can come in before the chain walk detects the deadlock. So
889 * the other will detect the deadlock and return -EDEADLOCK,
890 * which is wrong, as the other waiter is not in a deadlock
891 * situation.
892 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200893 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000894 return -EDEADLK;
895
Thomas Gleixner1d615482009-11-17 14:54:03 +0100896 raw_spin_lock_irqsave(&task->pi_lock, flags);
Darren Hart8dac4562009-04-03 13:40:12 -0700897 __rt_mutex_adjust_prio(task);
898 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700899 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100900 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700901
902 /* Get the top priority waiter on the lock */
903 if (rt_mutex_has_waiters(lock))
904 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100905 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700906
Darren Hart8dac4562009-04-03 13:40:12 -0700907 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700908
Thomas Gleixner1d615482009-11-17 14:54:03 +0100909 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700910
Lai Jiangshan81612392011-01-14 17:09:41 +0800911 if (!owner)
912 return 0;
913
Thomas Gleixner82084982014-06-05 11:16:12 +0200914 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700915 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100916 rt_mutex_dequeue_pi(owner, top_waiter);
917 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700918
919 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700920 if (owner->pi_blocked_on)
921 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000922 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700923 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200924 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700925
Thomas Gleixner82084982014-06-05 11:16:12 +0200926 /* Store the lock on which owner is blocked or NULL */
927 next_lock = task_blocked_on_lock(owner);
928
929 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
930 /*
931 * Even if full deadlock detection is on, if the owner is not
932 * blocked itself, we can avoid finding this out in the chain
933 * walk.
934 */
935 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700936 return 0;
937
Steven Rostedtdb630632006-09-29 01:59:44 -0700938 /*
939 * The owner can't disappear while holding a lock,
940 * so the owner struct is protected by wait_lock.
941 * Gets dropped in rt_mutex_adjust_prio_chain()!
942 */
943 get_task_struct(owner);
944
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100945 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700946
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000947 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200948 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700949
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100950 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700951
952 return res;
953}
954
955/*
956 * Wake up the next waiter on the lock.
957 *
Thomas Gleixner27e35712014-06-11 18:44:04 +0000958 * Remove the top waiter from the current tasks pi waiter list and
959 * wake it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700960 *
961 * Called with lock->wait_lock held.
962 */
963static void wakeup_next_waiter(struct rt_mutex *lock)
964{
965 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700966 unsigned long flags;
967
Thomas Gleixner1d615482009-11-17 14:54:03 +0100968 raw_spin_lock_irqsave(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700969
970 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700971
972 /*
973 * Remove it from current->pi_waiters. We do not adjust a
974 * possible priority boost right now. We execute wakeup in the
975 * boosted mode and go back to normal after releasing
976 * lock->wait_lock.
977 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100978 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700979
Thomas Gleixner27e35712014-06-11 18:44:04 +0000980 /*
981 * As we are waking up the top waiter, and the waiter stays
982 * queued on the lock until it gets the lock, this lock
983 * obviously has waiters. Just set the bit here and this has
984 * the added benefit of forcing all new tasks into the
985 * slow path making sure no task of lower priority than
986 * the top waiter can steal this lock.
987 */
988 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700989
Thomas Gleixner1d615482009-11-17 14:54:03 +0100990 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700991
Thomas Gleixner27e35712014-06-11 18:44:04 +0000992 /*
993 * It's safe to dereference waiter as it cannot go away as
994 * long as we hold lock->wait_lock. The waiter task needs to
995 * acquire it in order to dequeue the waiter.
996 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800997 wake_up_process(waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700998}
999
1000/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001001 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001002 *
Lai Jiangshan81612392011-01-14 17:09:41 +08001003 * Must be called with lock->wait_lock held and
1004 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001005 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001006static void remove_waiter(struct rt_mutex *lock,
1007 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001008{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001009 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001010 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001011 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001012 unsigned long flags;
1013
Thomas Gleixner1d615482009-11-17 14:54:03 +01001014 raw_spin_lock_irqsave(&current->pi_lock, flags);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001015 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001016 current->pi_blocked_on = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001017 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001018
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001019 /*
1020 * Only update priority if the waiter was the highest priority
1021 * waiter of the lock and there is an owner to update.
1022 */
1023 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001024 return;
1025
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001026 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001027
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001028 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001029
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001030 if (rt_mutex_has_waiters(lock))
1031 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001032
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001033 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001034
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001035 /* Store the lock on which owner is blocked or NULL */
1036 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001037
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001038 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
Steven Rostedtdb630632006-09-29 01:59:44 -07001039
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001040 /*
1041 * Don't walk the chain, if the owner task is not blocked
1042 * itself.
1043 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001044 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001045 return;
1046
Steven Rostedtdb630632006-09-29 01:59:44 -07001047 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1048 get_task_struct(owner);
1049
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001050 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001051
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001052 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1053 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001054
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001055 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001056}
1057
1058/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001059 * Recheck the pi chain, in case we got a priority setting
1060 *
1061 * Called from sched_setscheduler
1062 */
1063void rt_mutex_adjust_pi(struct task_struct *task)
1064{
1065 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001066 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001067 unsigned long flags;
1068
Thomas Gleixner1d615482009-11-17 14:54:03 +01001069 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001070
1071 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001072 if (!waiter || (waiter->prio == task->prio &&
1073 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001074 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001075 return;
1076 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001077 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001078 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001079
Steven Rostedtdb630632006-09-29 01:59:44 -07001080 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1081 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001082
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001083 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1084 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001085}
1086
Darren Hart8dac4562009-04-03 13:40:12 -07001087/**
1088 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1089 * @lock: the rt_mutex to take
1090 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1091 * or TASK_UNINTERRUPTIBLE)
1092 * @timeout: the pre-initialized and started timer, or NULL for none
1093 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001094 *
1095 * lock->wait_lock must be held by the caller.
1096 */
1097static int __sched
1098__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1099 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001100 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001101{
1102 int ret = 0;
1103
1104 for (;;) {
1105 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001106 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001107 break;
1108
1109 /*
1110 * TASK_INTERRUPTIBLE checks for signals and
1111 * timeout. Ignored otherwise.
1112 */
1113 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1114 /* Signal pending? */
1115 if (signal_pending(current))
1116 ret = -EINTR;
1117 if (timeout && !timeout->task)
1118 ret = -ETIMEDOUT;
1119 if (ret)
1120 break;
1121 }
1122
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001123 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001124
1125 debug_rt_mutex_print_deadlock(waiter);
1126
Lai Jiangshan81612392011-01-14 17:09:41 +08001127 schedule_rt_mutex(lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001128
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001129 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001130 set_current_state(state);
1131 }
1132
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001133 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001134 return ret;
1135}
1136
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001137static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1138 struct rt_mutex_waiter *w)
1139{
1140 /*
1141 * If the result is not -EDEADLOCK or the caller requested
1142 * deadlock detection, nothing to do here.
1143 */
1144 if (res != -EDEADLOCK || detect_deadlock)
1145 return;
1146
1147 /*
1148 * Yell lowdly and stop the task right here.
1149 */
1150 rt_mutex_print_deadlock(w);
1151 while (1) {
1152 set_current_state(TASK_INTERRUPTIBLE);
1153 schedule();
1154 }
1155}
1156
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001157/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001158 * Slow path lock function:
1159 */
1160static int __sched
1161rt_mutex_slowlock(struct rt_mutex *lock, int state,
1162 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001163 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001164{
1165 struct rt_mutex_waiter waiter;
1166 int ret = 0;
1167
1168 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001169 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1170 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001171
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001172 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001173
1174 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001175 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001176 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001177 return 0;
1178 }
1179
1180 set_current_state(state);
1181
1182 /* Setup the timer, when timeout != NULL */
Peter Zijlstra720a2592008-02-13 15:45:36 +01001183 if (unlikely(timeout)) {
Arjan van de Vencc584b22008-09-01 15:02:30 -07001184 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Peter Zijlstra720a2592008-02-13 15:45:36 +01001185 if (!hrtimer_active(&timeout->timer))
1186 timeout->task = NULL;
1187 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001188
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001189 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001190
1191 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001192 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001193 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001194
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001195 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001196 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001197 if (rt_mutex_has_waiters(lock))
1198 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001199 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001200 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001201
1202 /*
1203 * try_to_take_rt_mutex() sets the waiter bit
1204 * unconditionally. We might have to fix that up.
1205 */
1206 fixup_rt_mutex_waiters(lock);
1207
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001208 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001209
1210 /* Remove pending timer: */
1211 if (unlikely(timeout))
1212 hrtimer_cancel(&timeout->timer);
1213
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001214 debug_rt_mutex_free_waiter(&waiter);
1215
1216 return ret;
1217}
1218
1219/*
1220 * Slow path try-lock function:
1221 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001222static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001223{
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001224 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001225
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001226 /*
1227 * If the lock already has an owner we fail to get the lock.
1228 * This can be done without taking the @lock->wait_lock as
1229 * it is only being read, and this is a trylock anyway.
1230 */
1231 if (rt_mutex_owner(lock))
1232 return 0;
1233
1234 /*
1235 * The mutex has currently no owner. Lock the wait lock and
1236 * try to acquire the lock.
1237 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001238 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001239
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001240 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001241
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001242 /*
1243 * try_to_take_rt_mutex() sets the lock waiters bit
1244 * unconditionally. Clean this up.
1245 */
1246 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001247
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001248 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001249
1250 return ret;
1251}
1252
1253/*
1254 * Slow path to release a rt-mutex:
1255 */
1256static void __sched
1257rt_mutex_slowunlock(struct rt_mutex *lock)
1258{
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001259 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001260
1261 debug_rt_mutex_unlock(lock);
1262
1263 rt_mutex_deadlock_account_unlock(current);
1264
Thomas Gleixner27e35712014-06-11 18:44:04 +00001265 /*
1266 * We must be careful here if the fast path is enabled. If we
1267 * have no waiters queued we cannot set owner to NULL here
1268 * because of:
1269 *
1270 * foo->lock->owner = NULL;
1271 * rtmutex_lock(foo->lock); <- fast path
1272 * free = atomic_dec_and_test(foo->refcnt);
1273 * rtmutex_unlock(foo->lock); <- fast path
1274 * if (free)
1275 * kfree(foo);
1276 * raw_spin_unlock(foo->lock->wait_lock);
1277 *
1278 * So for the fastpath enabled kernel:
1279 *
1280 * Nothing can set the waiters bit as long as we hold
1281 * lock->wait_lock. So we do the following sequence:
1282 *
1283 * owner = rt_mutex_owner(lock);
1284 * clear_rt_mutex_waiters(lock);
1285 * raw_spin_unlock(&lock->wait_lock);
1286 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1287 * return;
1288 * goto retry;
1289 *
1290 * The fastpath disabled variant is simple as all access to
1291 * lock->owner is serialized by lock->wait_lock:
1292 *
1293 * lock->owner = NULL;
1294 * raw_spin_unlock(&lock->wait_lock);
1295 */
1296 while (!rt_mutex_has_waiters(lock)) {
1297 /* Drops lock->wait_lock ! */
1298 if (unlock_rt_mutex_safe(lock) == true)
1299 return;
1300 /* Relock the rtmutex and try again */
1301 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001302 }
1303
Thomas Gleixner27e35712014-06-11 18:44:04 +00001304 /*
1305 * The wakeup next waiter path does not suffer from the above
1306 * race. See the comments there.
1307 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001308 wakeup_next_waiter(lock);
1309
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001310 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001311
1312 /* Undo pi boosting if necessary: */
1313 rt_mutex_adjust_prio(current);
1314}
1315
1316/*
1317 * debug aware fast / slowpath lock,trylock,unlock
1318 *
1319 * The atomic acquire/release ops are compiled away, when either the
1320 * architecture does not support cmpxchg or when debugging is enabled.
1321 */
1322static inline int
1323rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001324 int (*slowfn)(struct rt_mutex *lock, int state,
1325 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001326 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001327{
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001328 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001329 rt_mutex_deadlock_account_lock(lock, current);
1330 return 0;
1331 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001332 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001333}
1334
1335static inline int
1336rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001337 struct hrtimer_sleeper *timeout,
1338 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001339 int (*slowfn)(struct rt_mutex *lock, int state,
1340 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001341 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001342{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001343 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1344 likely(rt_mutex_cmpxchg(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001345 rt_mutex_deadlock_account_lock(lock, current);
1346 return 0;
1347 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001348 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001349}
1350
1351static inline int
1352rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001353 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001354{
1355 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1356 rt_mutex_deadlock_account_lock(lock, current);
1357 return 1;
1358 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001359 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001360}
1361
1362static inline void
1363rt_mutex_fastunlock(struct rt_mutex *lock,
1364 void (*slowfn)(struct rt_mutex *lock))
1365{
1366 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
1367 rt_mutex_deadlock_account_unlock(current);
1368 else
1369 slowfn(lock);
1370}
1371
1372/**
1373 * rt_mutex_lock - lock a rt_mutex
1374 *
1375 * @lock: the rt_mutex to be locked
1376 */
1377void __sched rt_mutex_lock(struct rt_mutex *lock)
1378{
1379 might_sleep();
1380
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001381 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001382}
1383EXPORT_SYMBOL_GPL(rt_mutex_lock);
1384
1385/**
1386 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1387 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001388 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001389 *
1390 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001391 * 0 on success
1392 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001393 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001394int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001395{
1396 might_sleep();
1397
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001398 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001399}
1400EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1401
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001402/*
1403 * Futex variant with full deadlock detection.
1404 */
1405int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1406 struct hrtimer_sleeper *timeout)
1407{
1408 might_sleep();
1409
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001410 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1411 RT_MUTEX_FULL_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001412 rt_mutex_slowlock);
1413}
1414
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001415/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001416 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1417 * the timeout structure is provided
1418 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001419 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001420 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001421 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001422 *
1423 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001424 * 0 on success
1425 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001426 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001427 */
1428int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001429rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001430{
1431 might_sleep();
1432
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001433 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1434 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001435 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001436}
1437EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1438
1439/**
1440 * rt_mutex_trylock - try to lock a rt_mutex
1441 *
1442 * @lock: the rt_mutex to be locked
1443 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001444 * This function can only be called in thread context. It's safe to
1445 * call it from atomic regions, but not from hard interrupt or soft
1446 * interrupt context.
1447 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001448 * Returns 1 on success and 0 on contention
1449 */
1450int __sched rt_mutex_trylock(struct rt_mutex *lock)
1451{
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001452 if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
1453 return 0;
1454
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001455 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1456}
1457EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1458
1459/**
1460 * rt_mutex_unlock - unlock a rt_mutex
1461 *
1462 * @lock: the rt_mutex to be unlocked
1463 */
1464void __sched rt_mutex_unlock(struct rt_mutex *lock)
1465{
1466 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1467}
1468EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1469
Luis Henriques23b94b92009-04-29 21:54:51 +01001470/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001471 * rt_mutex_destroy - mark a mutex unusable
1472 * @lock: the mutex to be destroyed
1473 *
1474 * This function marks the mutex uninitialized, and any subsequent
1475 * use of the mutex is forbidden. The mutex must not be locked when
1476 * this function is called.
1477 */
1478void rt_mutex_destroy(struct rt_mutex *lock)
1479{
1480 WARN_ON(rt_mutex_is_locked(lock));
1481#ifdef CONFIG_DEBUG_RT_MUTEXES
1482 lock->magic = NULL;
1483#endif
1484}
1485
1486EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1487
1488/**
1489 * __rt_mutex_init - initialize the rt lock
1490 *
1491 * @lock: the rt lock to be initialized
1492 *
1493 * Initialize the rt lock to unlocked state.
1494 *
1495 * Initializing of a locked rt lock is not allowed
1496 */
1497void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1498{
1499 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001500 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001501 lock->waiters = RB_ROOT;
1502 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001503
1504 debug_rt_mutex_init(lock, name);
1505}
1506EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001507
1508/**
1509 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1510 * proxy owner
1511 *
1512 * @lock: the rt_mutex to be locked
1513 * @proxy_owner:the task to set as owner
1514 *
1515 * No locking. Caller has to do serializing itself
1516 * Special API call for PI-futex support
1517 */
1518void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1519 struct task_struct *proxy_owner)
1520{
1521 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001522 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001523 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001524 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1525}
1526
1527/**
1528 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1529 *
1530 * @lock: the rt_mutex to be locked
1531 *
1532 * No locking. Caller has to do serializing itself
1533 * Special API call for PI-futex support
1534 */
1535void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1536 struct task_struct *proxy_owner)
1537{
1538 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001539 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001540 rt_mutex_deadlock_account_unlock(proxy_owner);
1541}
1542
1543/**
Darren Hart8dac4562009-04-03 13:40:12 -07001544 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1545 * @lock: the rt_mutex to take
1546 * @waiter: the pre-initialized rt_mutex_waiter
1547 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001548 *
1549 * Returns:
1550 * 0 - task blocked on lock
1551 * 1 - acquired the lock for task, caller should wake it up
1552 * <0 - error
1553 *
1554 * Special API call for FUTEX_REQUEUE_PI support.
1555 */
1556int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1557 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001558 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001559{
1560 int ret;
1561
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001562 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001563
Lai Jiangshan81612392011-01-14 17:09:41 +08001564 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001565 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001566 return 1;
1567 }
1568
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001569 /* We enforce deadlock detection for futexes */
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001570 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1571 RT_MUTEX_FULL_CHAINWALK);
Darren Hart8dac4562009-04-03 13:40:12 -07001572
Lai Jiangshan81612392011-01-14 17:09:41 +08001573 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001574 /*
1575 * Reset the return value. We might have
1576 * returned with -EDEADLK and the owner
1577 * released the lock while we were walking the
1578 * pi chain. Let the waiter sort it out.
1579 */
1580 ret = 0;
1581 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001582
1583 if (unlikely(ret))
1584 remove_waiter(lock, waiter);
1585
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001586 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001587
1588 debug_rt_mutex_print_deadlock(waiter);
1589
1590 return ret;
1591}
1592
1593/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001594 * rt_mutex_next_owner - return the next owner of the lock
1595 *
1596 * @lock: the rt lock query
1597 *
1598 * Returns the next owner of the lock or NULL
1599 *
1600 * Caller has to serialize against other accessors to the lock
1601 * itself.
1602 *
1603 * Special API call for PI-futex support
1604 */
1605struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1606{
1607 if (!rt_mutex_has_waiters(lock))
1608 return NULL;
1609
1610 return rt_mutex_top_waiter(lock)->task;
1611}
Darren Hart8dac4562009-04-03 13:40:12 -07001612
1613/**
1614 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1615 * @lock: the rt_mutex we were woken on
1616 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001617 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001618 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001619 *
1620 * Complete the lock acquisition started our behalf by another thread.
1621 *
1622 * Returns:
1623 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001624 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001625 *
1626 * Special API call for PI-futex requeue support
1627 */
1628int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1629 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001630 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001631{
1632 int ret;
1633
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001634 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001635
1636 set_current_state(TASK_INTERRUPTIBLE);
1637
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001638 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001639 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001640
Lai Jiangshan81612392011-01-14 17:09:41 +08001641 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001642 remove_waiter(lock, waiter);
1643
1644 /*
1645 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1646 * have to fix that up.
1647 */
1648 fixup_rt_mutex_waiters(lock);
1649
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001650 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001651
Darren Hart8dac4562009-04-03 13:40:12 -07001652 return ret;
1653}