blob: 8251e75dd9c0bd67337754baf6f03fb2cf1f956f [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
Davidlohr Bueso700318d2015-09-30 13:03:13 -070077# define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
78# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
79# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
80
81/*
82 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
83 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
84 * relaxed semantics suffice.
85 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +020086static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
87{
88 unsigned long owner, *p = (unsigned long *) &lock->owner;
89
90 do {
91 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -070092 } while (cmpxchg_relaxed(p, owner,
93 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +020094}
Thomas Gleixner27e35712014-06-11 18:44:04 +000095
96/*
97 * Safe fastpath aware unlock:
98 * 1) Clear the waiters bit
99 * 2) Drop lock->wait_lock
100 * 3) Try to unlock the lock with cmpxchg
101 */
102static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
103 __releases(lock->wait_lock)
104{
105 struct task_struct *owner = rt_mutex_owner(lock);
106
107 clear_rt_mutex_waiters(lock);
108 raw_spin_unlock(&lock->wait_lock);
109 /*
110 * If a new waiter comes in between the unlock and the cmpxchg
111 * we have two situations:
112 *
113 * unlock(wait_lock);
114 * lock(wait_lock);
115 * cmpxchg(p, owner, 0) == owner
116 * mark_rt_mutex_waiters(lock);
117 * acquire(lock);
118 * or:
119 *
120 * unlock(wait_lock);
121 * lock(wait_lock);
122 * mark_rt_mutex_waiters(lock);
123 *
124 * cmpxchg(p, owner, 0) != owner
125 * enqueue_waiter();
126 * unlock(wait_lock);
127 * lock(wait_lock);
128 * wake waiter();
129 * unlock(wait_lock);
130 * lock(wait_lock);
131 * acquire(lock);
132 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700133 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000134}
135
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200136#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700137# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
138# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
139# define rt_mutex_cmpxchg_release(l,c,n) (0)
140
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200141static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
142{
143 lock->owner = (struct task_struct *)
144 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
145}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000146
147/*
148 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
149 */
150static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
151 __releases(lock->wait_lock)
152{
153 lock->owner = NULL;
154 raw_spin_unlock(&lock->wait_lock);
155 return true;
156}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200157#endif
158
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100159static inline int
160rt_mutex_waiter_less(struct rt_mutex_waiter *left,
161 struct rt_mutex_waiter *right)
162{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100163 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100164 return 1;
165
166 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100167 * If both waiters have dl_prio(), we check the deadlines of the
168 * associated tasks.
169 * If left waiter has a dl_prio(), and we didn't return 1 above,
170 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100171 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100172 if (dl_prio(left->prio))
Juri Lellif5240572015-09-02 11:01:35 +0100173 return dl_time_before(left->task->dl.deadline,
174 right->task->dl.deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100175
176 return 0;
177}
178
179static void
180rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
181{
182 struct rb_node **link = &lock->waiters.rb_node;
183 struct rb_node *parent = NULL;
184 struct rt_mutex_waiter *entry;
185 int leftmost = 1;
186
187 while (*link) {
188 parent = *link;
189 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
190 if (rt_mutex_waiter_less(waiter, entry)) {
191 link = &parent->rb_left;
192 } else {
193 link = &parent->rb_right;
194 leftmost = 0;
195 }
196 }
197
198 if (leftmost)
199 lock->waiters_leftmost = &waiter->tree_entry;
200
201 rb_link_node(&waiter->tree_entry, parent, link);
202 rb_insert_color(&waiter->tree_entry, &lock->waiters);
203}
204
205static void
206rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
207{
208 if (RB_EMPTY_NODE(&waiter->tree_entry))
209 return;
210
211 if (lock->waiters_leftmost == &waiter->tree_entry)
212 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
213
214 rb_erase(&waiter->tree_entry, &lock->waiters);
215 RB_CLEAR_NODE(&waiter->tree_entry);
216}
217
218static void
219rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
220{
221 struct rb_node **link = &task->pi_waiters.rb_node;
222 struct rb_node *parent = NULL;
223 struct rt_mutex_waiter *entry;
224 int leftmost = 1;
225
226 while (*link) {
227 parent = *link;
228 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
229 if (rt_mutex_waiter_less(waiter, entry)) {
230 link = &parent->rb_left;
231 } else {
232 link = &parent->rb_right;
233 leftmost = 0;
234 }
235 }
236
237 if (leftmost)
238 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
239
240 rb_link_node(&waiter->pi_tree_entry, parent, link);
241 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
242}
243
244static void
245rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
246{
247 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
248 return;
249
250 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
251 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
252
253 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
254 RB_CLEAR_NODE(&waiter->pi_tree_entry);
255}
256
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200257/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100258 * Calculate task priority from the waiter tree priority
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700259 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100260 * Return task->normal_prio when the waiter tree is empty or when
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700261 * the waiter is not allowed to do priority boosting
262 */
263int rt_mutex_getprio(struct task_struct *task)
264{
265 if (likely(!task_has_pi_waiters(task)))
266 return task->normal_prio;
267
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100268 return min(task_top_pi_waiter(task)->prio,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700269 task->normal_prio);
270}
271
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100272struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
273{
274 if (likely(!task_has_pi_waiters(task)))
275 return NULL;
276
277 return task_top_pi_waiter(task)->task;
278}
279
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700280/*
Thomas Gleixner0782e632015-05-05 19:49:49 +0200281 * Called by sched_setscheduler() to get the priority which will be
282 * effective after the change.
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100283 */
Thomas Gleixner0782e632015-05-05 19:49:49 +0200284int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100285{
286 if (!task_has_pi_waiters(task))
Thomas Gleixner0782e632015-05-05 19:49:49 +0200287 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100288
Thomas Gleixner0782e632015-05-05 19:49:49 +0200289 if (task_top_pi_waiter(task)->task->prio <= newprio)
290 return task_top_pi_waiter(task)->task->prio;
291 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100292}
293
294/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700295 * Adjust the priority of a task, after its pi_waiters got modified.
296 *
297 * This can be both boosting and unboosting. task->pi_lock must be held.
298 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200299static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700300{
301 int prio = rt_mutex_getprio(task);
302
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100303 if (task->prio != prio || dl_prio(prio))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700304 rt_mutex_setprio(task, prio);
305}
306
307/*
308 * Adjust task priority (undo boosting). Called from the exit path of
309 * rt_mutex_slowunlock() and rt_mutex_slowlock().
310 *
311 * (Note: We do this outside of the protection of lock->wait_lock to
312 * allow the lock to be taken while or before we readjust the priority
313 * of task. We do not use the spin_xx_mutex() variants here as we are
314 * outside of the debug path.)
315 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +0200316void rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700317{
318 unsigned long flags;
319
Thomas Gleixner1d615482009-11-17 14:54:03 +0100320 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700321 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100322 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700323}
324
325/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000326 * Deadlock detection is conditional:
327 *
328 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
329 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
330 *
331 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
332 * conducted independent of the detect argument.
333 *
334 * If the waiter argument is NULL this indicates the deboost path and
335 * deadlock detection is disabled independent of the detect argument
336 * and the config settings.
337 */
338static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
339 enum rtmutex_chainwalk chwalk)
340{
341 /*
342 * This is just a wrapper function for the following call,
343 * because debug_rt_mutex_detect_deadlock() smells like a magic
344 * debug feature and I wanted to keep the cond function in the
345 * main source file along with the comments instead of having
346 * two of the same in the headers.
347 */
348 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
349}
350
351/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700352 * Max number of times we'll walk the boosting chain:
353 */
354int max_lock_depth = 1024;
355
Thomas Gleixner82084982014-06-05 11:16:12 +0200356static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
357{
358 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
359}
360
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700361/*
362 * Adjust the priority chain. Also used for deadlock detection.
363 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200364 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200365 * @task: the task owning the mutex (owner) for which a chain walk is
366 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900367 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200368 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
369 * things for a task that has just got its priority adjusted, and
370 * is waiting on a mutex)
371 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
372 * we dropped its pi_lock. Is never dereferenced, only used for
373 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200374 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200375 * its priority to the mutex owner (can be NULL in the case
376 * depicted above or if the top waiter is gone away and we are
377 * actually deboosting the owner)
378 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200379 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700380 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200381 *
382 * Chain walk basics and protection scope
383 *
384 * [R] refcount on task
385 * [P] task->pi_lock held
386 * [L] rtmutex->wait_lock held
387 *
388 * Step Description Protected by
389 * function arguments:
390 * @task [R]
391 * @orig_lock if != NULL @top_task is blocked on it
392 * @next_lock Unprotected. Cannot be
393 * dereferenced. Only used for
394 * comparison.
395 * @orig_waiter if != NULL @top_task is blocked on it
396 * @top_task current, or in case of proxy
397 * locking protected by calling
398 * code
399 * again:
400 * loop_sanity_check();
401 * retry:
402 * [1] lock(task->pi_lock); [R] acquire [P]
403 * [2] waiter = task->pi_blocked_on; [P]
404 * [3] check_exit_conditions_1(); [P]
405 * [4] lock = waiter->lock; [P]
406 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
407 * unlock(task->pi_lock); release [P]
408 * goto retry;
409 * }
410 * [6] check_exit_conditions_2(); [P] + [L]
411 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
412 * [8] unlock(task->pi_lock); release [P]
413 * put_task_struct(task); release [R]
414 * [9] check_exit_conditions_3(); [L]
415 * [10] task = owner(lock); [L]
416 * get_task_struct(task); [L] acquire [R]
417 * lock(task->pi_lock); [L] acquire [P]
418 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
419 * [12] check_exit_conditions_4(); [P] + [L]
420 * [13] unlock(task->pi_lock); release [P]
421 * unlock(lock->wait_lock); release [L]
422 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700423 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200424static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000425 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200426 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200427 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200428 struct rt_mutex_waiter *orig_waiter,
429 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700430{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700431 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000432 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000433 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000434 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000435 bool detect_deadlock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700436 unsigned long flags;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000437 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700438
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000439 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700440
441 /*
442 * The (de)boosting is a step by step approach with a lot of
443 * pitfalls. We want this to be preemptible and we want hold a
444 * maximum of two locks per step. So we have to check
445 * carefully whether things change under us.
446 */
447 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200448 /*
449 * We limit the lock chain length for each invocation.
450 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700451 if (++depth > max_lock_depth) {
452 static int prev_max;
453
454 /*
455 * Print this only once. If the admin changes the limit,
456 * print a new message when reaching the limit again.
457 */
458 if (prev_max != max_lock_depth) {
459 prev_max = max_lock_depth;
460 printk(KERN_WARNING "Maximum lock depth %d reached "
461 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700462 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700463 }
464 put_task_struct(task);
465
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200466 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700467 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200468
469 /*
470 * We are fully preemptible here and only hold the refcount on
471 * @task. So everything can have changed under us since the
472 * caller or our own code below (goto retry/again) dropped all
473 * locks.
474 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700475 retry:
476 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200477 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700478 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100479 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700480
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200481 /*
482 * [2] Get the waiter on which @task is blocked on.
483 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700484 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200485
486 /*
487 * [3] check_exit_conditions_1() protected by task->pi_lock.
488 */
489
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700490 /*
491 * Check whether the end of the boosting chain has been
492 * reached or the state of the chain has changed while we
493 * dropped the locks.
494 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800495 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700496 goto out_unlock_pi;
497
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700498 /*
499 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800500 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700501 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800502 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700503 goto out_unlock_pi;
504
505 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200506 * We dropped all locks after taking a refcount on @task, so
507 * the task might have moved on in the lock chain or even left
508 * the chain completely and blocks now on an unrelated lock or
509 * on @orig_lock.
510 *
511 * We stored the lock on which @task was blocked in @next_lock,
512 * so we can detect the chain change.
513 */
514 if (next_lock != waiter->lock)
515 goto out_unlock_pi;
516
517 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700518 * Drop out, when the task has no waiters. Note,
519 * top_waiter can be NULL, when we are in the deboosting
520 * mode!
521 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000522 if (top_waiter) {
523 if (!task_has_pi_waiters(task))
524 goto out_unlock_pi;
525 /*
526 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000527 * are not the top pi waiter of the task. If deadlock
528 * detection is enabled we continue, but stop the
529 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000530 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000531 if (top_waiter != task_top_pi_waiter(task)) {
532 if (!detect_deadlock)
533 goto out_unlock_pi;
534 else
535 requeue = false;
536 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000537 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700538
539 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000540 * If the waiter priority is the same as the task priority
541 * then there is no further priority adjustment necessary. If
542 * deadlock detection is off, we stop the chain walk. If its
543 * enabled we continue, but stop the requeueing in the chain
544 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700545 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000546 if (waiter->prio == task->prio) {
547 if (!detect_deadlock)
548 goto out_unlock_pi;
549 else
550 requeue = false;
551 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700552
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200553 /*
554 * [4] Get the next lock
555 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700556 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200557 /*
558 * [5] We need to trylock here as we are holding task->pi_lock,
559 * which is the reverse lock order versus the other rtmutex
560 * operations.
561 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100562 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100563 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700564 cpu_relax();
565 goto retry;
566 }
567
Thomas Gleixner397335f2014-05-22 03:25:39 +0000568 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200569 * [6] check_exit_conditions_2() protected by task->pi_lock and
570 * lock->wait_lock.
571 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000572 * Deadlock detection. If the lock is the same as the original
573 * lock which caused us to walk the lock chain or if the
574 * current lock is owned by the task which initiated the chain
575 * walk, we detected a deadlock.
576 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700577 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000578 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100579 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200580 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700581 goto out_unlock_pi;
582 }
583
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000584 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000585 * If we just follow the lock chain for deadlock detection, no
586 * need to do all the requeue operations. To avoid a truckload
587 * of conditionals around the various places below, just do the
588 * minimum chain walk checks.
589 */
590 if (!requeue) {
591 /*
592 * No requeue[7] here. Just release @task [8]
593 */
594 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
595 put_task_struct(task);
596
597 /*
598 * [9] check_exit_conditions_3 protected by lock->wait_lock.
599 * If there is no owner of the lock, end of chain.
600 */
601 if (!rt_mutex_owner(lock)) {
602 raw_spin_unlock(&lock->wait_lock);
603 return 0;
604 }
605
606 /* [10] Grab the next task, i.e. owner of @lock */
607 task = rt_mutex_owner(lock);
608 get_task_struct(task);
609 raw_spin_lock_irqsave(&task->pi_lock, flags);
610
611 /*
612 * No requeue [11] here. We just do deadlock detection.
613 *
614 * [12] Store whether owner is blocked
615 * itself. Decision is made after dropping the locks
616 */
617 next_lock = task_blocked_on_lock(task);
618 /*
619 * Get the top waiter for the next iteration
620 */
621 top_waiter = rt_mutex_top_waiter(lock);
622
623 /* [13] Drop locks */
624 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
625 raw_spin_unlock(&lock->wait_lock);
626
627 /* If owner is not blocked, end of chain. */
628 if (!next_lock)
629 goto out_put_task;
630 goto again;
631 }
632
633 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000634 * Store the current top waiter before doing the requeue
635 * operation on @lock. We need it for the boost/deboost
636 * decision below.
637 */
638 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700639
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700640 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100641 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100642 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100643 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700644
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200645 /* [8] Release the task */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100646 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200647 put_task_struct(task);
648
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000649 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200650 * [9] check_exit_conditions_3 protected by lock->wait_lock.
651 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000652 * We must abort the chain walk if there is no lock owner even
653 * in the dead lock detection case, as we have nothing to
654 * follow here. This is the end of the chain we are walking.
655 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800656 if (!rt_mutex_owner(lock)) {
657 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200658 * If the requeue [7] above changed the top waiter,
659 * then we need to wake the new top waiter up to try
660 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800661 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000662 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800663 wake_up_process(rt_mutex_top_waiter(lock)->task);
664 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200665 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800666 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700667
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200668 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700669 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700670 get_task_struct(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100671 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700672
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200673 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700674 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000675 /*
676 * The waiter became the new top (highest priority)
677 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700678 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000679 * and adjust the priority of the owner.
680 */
681 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100682 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700683 __rt_mutex_adjust_prio(task);
684
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000685 } else if (prerequeue_top_waiter == waiter) {
686 /*
687 * The waiter was the top waiter on the lock, but is
688 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700689 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000690 * (highest priority) waiter and adjust the priority
691 * of the owner.
692 * The new top waiter is stored in @waiter so that
693 * @waiter == @top_waiter evaluates to true below and
694 * we continue to deboost the rest of the chain.
695 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100696 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700697 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100698 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700699 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000700 } else {
701 /*
702 * Nothing changed. No need to do any priority
703 * adjustment.
704 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700705 }
706
Thomas Gleixner82084982014-06-05 11:16:12 +0200707 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200708 * [12] check_exit_conditions_4() protected by task->pi_lock
709 * and lock->wait_lock. The actual decisions are made after we
710 * dropped the locks.
711 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200712 * Check whether the task which owns the current lock is pi
713 * blocked itself. If yes we store a pointer to the lock for
714 * the lock chain change detection above. After we dropped
715 * task->pi_lock next_lock cannot be dereferenced anymore.
716 */
717 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000718 /*
719 * Store the top waiter of @lock for the end of chain walk
720 * decision below.
721 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700722 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200723
724 /* [13] Drop the locks */
725 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100726 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700727
Thomas Gleixner82084982014-06-05 11:16:12 +0200728 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200729 * Make the actual exit decisions [12], based on the stored
730 * values.
731 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200732 * We reached the end of the lock chain. Stop right here. No
733 * point to go back just to figure that out.
734 */
735 if (!next_lock)
736 goto out_put_task;
737
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000738 /*
739 * If the current waiter is not the top waiter on the lock,
740 * then we can stop the chain walk here if we are not in full
741 * deadlock detection mode.
742 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700743 if (!detect_deadlock && waiter != top_waiter)
744 goto out_put_task;
745
746 goto again;
747
748 out_unlock_pi:
Thomas Gleixner1d615482009-11-17 14:54:03 +0100749 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700750 out_put_task:
751 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700752
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700753 return ret;
754}
755
756/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700757 * Try to take an rt-mutex
758 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700759 * Must be called with lock->wait_lock held.
Lai Jiangshan81612392011-01-14 17:09:41 +0800760 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200761 * @lock: The lock to be acquired.
762 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700763 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200764 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700765 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800766static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200767 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700768{
Thomas Gleixner358c3312014-06-11 01:01:13 +0200769 unsigned long flags;
770
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700771 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200772 * Before testing whether we can acquire @lock, we set the
773 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
774 * other tasks which try to modify @lock into the slow path
775 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700776 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200777 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
778 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700779 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200780 * - There is a lock owner. The caller must fixup the
781 * transient state if it does a trylock or leaves the lock
782 * function due to a signal or timeout.
783 *
784 * - @task acquires the lock and there are no other
785 * waiters. This is undone in rt_mutex_set_owner(@task) at
786 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700787 */
788 mark_rt_mutex_waiters(lock);
789
Thomas Gleixner358c3312014-06-11 01:01:13 +0200790 /*
791 * If @lock has an owner, give up.
792 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800793 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700794 return 0;
795
Lai Jiangshan81612392011-01-14 17:09:41 +0800796 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200797 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700798 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200799 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800800 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200801 if (waiter) {
802 /*
803 * If waiter is not the highest priority waiter of
804 * @lock, give up.
805 */
806 if (waiter != rt_mutex_top_waiter(lock))
807 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800808
809 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200810 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700811 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200812 */
813 rt_mutex_dequeue(lock, waiter);
814
815 } else {
816 /*
817 * If the lock has waiters already we check whether @task is
818 * eligible to take over the lock.
819 *
820 * If there are no other waiters, @task can acquire
821 * the lock. @task->pi_blocked_on is NULL, so it does
822 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800823 */
824 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200825 /*
826 * If @task->prio is greater than or equal to
827 * the top waiter priority (kernel view),
828 * @task lost.
829 */
830 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
831 return 0;
832
833 /*
834 * The current top waiter stays enqueued. We
835 * don't have to change anything in the lock
836 * waiters order.
837 */
838 } else {
839 /*
840 * No waiters. Take the lock without the
841 * pi_lock dance.@task->pi_blocked_on is NULL
842 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700843 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200844 */
845 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800846 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800847 }
848
Thomas Gleixner358c3312014-06-11 01:01:13 +0200849 /*
850 * Clear @task->pi_blocked_on. Requires protection by
851 * @task->pi_lock. Redundant operation for the @waiter == NULL
852 * case, but conditionals are more expensive than a redundant
853 * store.
854 */
855 raw_spin_lock_irqsave(&task->pi_lock, flags);
856 task->pi_blocked_on = NULL;
857 /*
858 * Finish the lock acquisition. @task is the new owner. If
859 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700860 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200861 */
862 if (rt_mutex_has_waiters(lock))
863 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
864 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
865
866takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700867 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700868 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700869
Thomas Gleixner358c3312014-06-11 01:01:13 +0200870 /*
871 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
872 * are still waiters or clears it.
873 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800874 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700875
Lai Jiangshan81612392011-01-14 17:09:41 +0800876 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700877
878 return 1;
879}
880
881/*
882 * Task blocks on lock.
883 *
884 * Prepare waiter and propagate pi chain
885 *
886 * This must be called with lock->wait_lock held.
887 */
888static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
889 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700890 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000891 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700892{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700893 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700894 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200895 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700896 int chain_walk = 0, res;
Thomas Gleixner82084982014-06-05 11:16:12 +0200897 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700898
Thomas Gleixner397335f2014-05-22 03:25:39 +0000899 /*
900 * Early deadlock detection. We really don't want the task to
901 * enqueue on itself just to untangle the mess later. It's not
902 * only an optimization. We drop the locks, so another waiter
903 * can come in before the chain walk detects the deadlock. So
904 * the other will detect the deadlock and return -EDEADLOCK,
905 * which is wrong, as the other waiter is not in a deadlock
906 * situation.
907 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200908 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000909 return -EDEADLK;
910
Thomas Gleixner1d615482009-11-17 14:54:03 +0100911 raw_spin_lock_irqsave(&task->pi_lock, flags);
Darren Hart8dac4562009-04-03 13:40:12 -0700912 __rt_mutex_adjust_prio(task);
913 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700914 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100915 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700916
917 /* Get the top priority waiter on the lock */
918 if (rt_mutex_has_waiters(lock))
919 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100920 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700921
Darren Hart8dac4562009-04-03 13:40:12 -0700922 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700923
Thomas Gleixner1d615482009-11-17 14:54:03 +0100924 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700925
Lai Jiangshan81612392011-01-14 17:09:41 +0800926 if (!owner)
927 return 0;
928
Thomas Gleixner82084982014-06-05 11:16:12 +0200929 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700930 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100931 rt_mutex_dequeue_pi(owner, top_waiter);
932 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700933
934 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700935 if (owner->pi_blocked_on)
936 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000937 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700938 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200939 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700940
Thomas Gleixner82084982014-06-05 11:16:12 +0200941 /* Store the lock on which owner is blocked or NULL */
942 next_lock = task_blocked_on_lock(owner);
943
944 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
945 /*
946 * Even if full deadlock detection is on, if the owner is not
947 * blocked itself, we can avoid finding this out in the chain
948 * walk.
949 */
950 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700951 return 0;
952
Steven Rostedtdb630632006-09-29 01:59:44 -0700953 /*
954 * The owner can't disappear while holding a lock,
955 * so the owner struct is protected by wait_lock.
956 * Gets dropped in rt_mutex_adjust_prio_chain()!
957 */
958 get_task_struct(owner);
959
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100960 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700961
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000962 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200963 next_lock, waiter, task);
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 return res;
968}
969
970/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700971 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700972 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700973 *
974 * Called with lock->wait_lock held.
975 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700976static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
977 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700978{
979 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700980 unsigned long flags;
981
Thomas Gleixner1d615482009-11-17 14:54:03 +0100982 raw_spin_lock_irqsave(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700983
984 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700985
986 /*
987 * Remove it from current->pi_waiters. We do not adjust a
988 * possible priority boost right now. We execute wakeup in the
989 * boosted mode and go back to normal after releasing
990 * lock->wait_lock.
991 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100992 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700993
Thomas Gleixner27e35712014-06-11 18:44:04 +0000994 /*
995 * As we are waking up the top waiter, and the waiter stays
996 * queued on the lock until it gets the lock, this lock
997 * obviously has waiters. Just set the bit here and this has
998 * the added benefit of forcing all new tasks into the
999 * slow path making sure no task of lower priority than
1000 * the top waiter can steal this lock.
1001 */
1002 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001003
Thomas Gleixner1d615482009-11-17 14:54:03 +01001004 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001005
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001006 wake_q_add(wake_q, waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001007}
1008
1009/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001010 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001011 *
Lai Jiangshan81612392011-01-14 17:09:41 +08001012 * Must be called with lock->wait_lock held and
1013 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001014 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001015static void remove_waiter(struct rt_mutex *lock,
1016 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001017{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001018 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001019 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001020 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001021 unsigned long flags;
1022
Thomas Gleixner1d615482009-11-17 14:54:03 +01001023 raw_spin_lock_irqsave(&current->pi_lock, flags);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001024 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001025 current->pi_blocked_on = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001026 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001027
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001028 /*
1029 * Only update priority if the waiter was the highest priority
1030 * waiter of the lock and there is an owner to update.
1031 */
1032 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001033 return;
1034
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001035 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001036
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001037 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001038
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001039 if (rt_mutex_has_waiters(lock))
1040 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001041
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001042 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001043
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001044 /* Store the lock on which owner is blocked or NULL */
1045 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001046
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001047 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
Steven Rostedtdb630632006-09-29 01:59:44 -07001048
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001049 /*
1050 * Don't walk the chain, if the owner task is not blocked
1051 * itself.
1052 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001053 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001054 return;
1055
Steven Rostedtdb630632006-09-29 01:59:44 -07001056 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1057 get_task_struct(owner);
1058
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001059 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001060
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001061 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1062 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001063
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001064 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001065}
1066
1067/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001068 * Recheck the pi chain, in case we got a priority setting
1069 *
1070 * Called from sched_setscheduler
1071 */
1072void rt_mutex_adjust_pi(struct task_struct *task)
1073{
1074 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001075 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001076 unsigned long flags;
1077
Thomas Gleixner1d615482009-11-17 14:54:03 +01001078 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001079
1080 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001081 if (!waiter || (waiter->prio == task->prio &&
1082 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001083 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001084 return;
1085 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001086 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001087 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001088
Steven Rostedtdb630632006-09-29 01:59:44 -07001089 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1090 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001091
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001092 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1093 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001094}
1095
Darren Hart8dac4562009-04-03 13:40:12 -07001096/**
1097 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1098 * @lock: the rt_mutex to take
1099 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1100 * or TASK_UNINTERRUPTIBLE)
1101 * @timeout: the pre-initialized and started timer, or NULL for none
1102 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001103 *
1104 * lock->wait_lock must be held by the caller.
1105 */
1106static int __sched
1107__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1108 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001109 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001110{
1111 int ret = 0;
1112
1113 for (;;) {
1114 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001115 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001116 break;
1117
1118 /*
1119 * TASK_INTERRUPTIBLE checks for signals and
1120 * timeout. Ignored otherwise.
1121 */
1122 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1123 /* Signal pending? */
1124 if (signal_pending(current))
1125 ret = -EINTR;
1126 if (timeout && !timeout->task)
1127 ret = -ETIMEDOUT;
1128 if (ret)
1129 break;
1130 }
1131
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001132 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001133
1134 debug_rt_mutex_print_deadlock(waiter);
1135
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001136 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001137
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001138 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001139 set_current_state(state);
1140 }
1141
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001142 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001143 return ret;
1144}
1145
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001146static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1147 struct rt_mutex_waiter *w)
1148{
1149 /*
1150 * If the result is not -EDEADLOCK or the caller requested
1151 * deadlock detection, nothing to do here.
1152 */
1153 if (res != -EDEADLOCK || detect_deadlock)
1154 return;
1155
1156 /*
1157 * Yell lowdly and stop the task right here.
1158 */
1159 rt_mutex_print_deadlock(w);
1160 while (1) {
1161 set_current_state(TASK_INTERRUPTIBLE);
1162 schedule();
1163 }
1164}
1165
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001166/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001167 * Slow path lock function:
1168 */
1169static int __sched
1170rt_mutex_slowlock(struct rt_mutex *lock, int state,
1171 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001172 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001173{
1174 struct rt_mutex_waiter waiter;
1175 int ret = 0;
1176
1177 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001178 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1179 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001180
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001181 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001182
1183 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001184 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001185 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001186 return 0;
1187 }
1188
1189 set_current_state(state);
1190
1191 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001192 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001193 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001194
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001195 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001196
1197 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001198 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001199 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001200
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001201 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001202 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001203 if (rt_mutex_has_waiters(lock))
1204 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001205 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001206 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001207
1208 /*
1209 * try_to_take_rt_mutex() sets the waiter bit
1210 * unconditionally. We might have to fix that up.
1211 */
1212 fixup_rt_mutex_waiters(lock);
1213
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001214 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001215
1216 /* Remove pending timer: */
1217 if (unlikely(timeout))
1218 hrtimer_cancel(&timeout->timer);
1219
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001220 debug_rt_mutex_free_waiter(&waiter);
1221
1222 return ret;
1223}
1224
1225/*
1226 * Slow path try-lock function:
1227 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001228static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001229{
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001230 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001231
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001232 /*
1233 * If the lock already has an owner we fail to get the lock.
1234 * This can be done without taking the @lock->wait_lock as
1235 * it is only being read, and this is a trylock anyway.
1236 */
1237 if (rt_mutex_owner(lock))
1238 return 0;
1239
1240 /*
1241 * The mutex has currently no owner. Lock the wait lock and
1242 * try to acquire the lock.
1243 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001244 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001245
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001246 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001247
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001248 /*
1249 * try_to_take_rt_mutex() sets the lock waiters bit
1250 * unconditionally. Clean this up.
1251 */
1252 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001253
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001254 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001255
1256 return ret;
1257}
1258
1259/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001260 * Slow path to release a rt-mutex.
1261 * Return whether the current task needs to undo a potential priority boosting.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001262 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001263static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1264 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001265{
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001266 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001267
1268 debug_rt_mutex_unlock(lock);
1269
1270 rt_mutex_deadlock_account_unlock(current);
1271
Thomas Gleixner27e35712014-06-11 18:44:04 +00001272 /*
1273 * We must be careful here if the fast path is enabled. If we
1274 * have no waiters queued we cannot set owner to NULL here
1275 * because of:
1276 *
1277 * foo->lock->owner = NULL;
1278 * rtmutex_lock(foo->lock); <- fast path
1279 * free = atomic_dec_and_test(foo->refcnt);
1280 * rtmutex_unlock(foo->lock); <- fast path
1281 * if (free)
1282 * kfree(foo);
1283 * raw_spin_unlock(foo->lock->wait_lock);
1284 *
1285 * So for the fastpath enabled kernel:
1286 *
1287 * Nothing can set the waiters bit as long as we hold
1288 * lock->wait_lock. So we do the following sequence:
1289 *
1290 * owner = rt_mutex_owner(lock);
1291 * clear_rt_mutex_waiters(lock);
1292 * raw_spin_unlock(&lock->wait_lock);
1293 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1294 * return;
1295 * goto retry;
1296 *
1297 * The fastpath disabled variant is simple as all access to
1298 * lock->owner is serialized by lock->wait_lock:
1299 *
1300 * lock->owner = NULL;
1301 * raw_spin_unlock(&lock->wait_lock);
1302 */
1303 while (!rt_mutex_has_waiters(lock)) {
1304 /* Drops lock->wait_lock ! */
1305 if (unlock_rt_mutex_safe(lock) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001306 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001307 /* Relock the rtmutex and try again */
1308 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001309 }
1310
Thomas Gleixner27e35712014-06-11 18:44:04 +00001311 /*
1312 * The wakeup next waiter path does not suffer from the above
1313 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001314 *
1315 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001316 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001317 mark_wakeup_next_waiter(wake_q, lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001318
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001319 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001320
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001321 /* check PI boosting */
1322 return true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001323}
1324
1325/*
1326 * debug aware fast / slowpath lock,trylock,unlock
1327 *
1328 * The atomic acquire/release ops are compiled away, when either the
1329 * architecture does not support cmpxchg or when debugging is enabled.
1330 */
1331static inline int
1332rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001333 int (*slowfn)(struct rt_mutex *lock, int state,
1334 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001335 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001336{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001337 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001338 rt_mutex_deadlock_account_lock(lock, current);
1339 return 0;
1340 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001341 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001342}
1343
1344static inline int
1345rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001346 struct hrtimer_sleeper *timeout,
1347 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001348 int (*slowfn)(struct rt_mutex *lock, int state,
1349 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001350 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001351{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001352 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001353 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001354 rt_mutex_deadlock_account_lock(lock, current);
1355 return 0;
1356 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001357 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001358}
1359
1360static inline int
1361rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001362 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001363{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001364 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001365 rt_mutex_deadlock_account_lock(lock, current);
1366 return 1;
1367 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001368 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001369}
1370
1371static inline void
1372rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001373 bool (*slowfn)(struct rt_mutex *lock,
1374 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001375{
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001376 WAKE_Q(wake_q);
1377
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001378 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001379 rt_mutex_deadlock_account_unlock(current);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001380
1381 } else {
1382 bool deboost = slowfn(lock, &wake_q);
1383
1384 wake_up_q(&wake_q);
1385
1386 /* Undo pi boosting if necessary: */
1387 if (deboost)
1388 rt_mutex_adjust_prio(current);
1389 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001390}
1391
1392/**
1393 * rt_mutex_lock - lock a rt_mutex
1394 *
1395 * @lock: the rt_mutex to be locked
1396 */
1397void __sched rt_mutex_lock(struct rt_mutex *lock)
1398{
1399 might_sleep();
1400
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001401 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001402}
1403EXPORT_SYMBOL_GPL(rt_mutex_lock);
1404
1405/**
1406 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1407 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001408 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001409 *
1410 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001411 * 0 on success
1412 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001413 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001414int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001415{
1416 might_sleep();
1417
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001418 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001419}
1420EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1421
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001422/*
1423 * Futex variant with full deadlock detection.
1424 */
1425int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1426 struct hrtimer_sleeper *timeout)
1427{
1428 might_sleep();
1429
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001430 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1431 RT_MUTEX_FULL_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001432 rt_mutex_slowlock);
1433}
1434
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001435/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001436 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1437 * the timeout structure is provided
1438 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001439 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001440 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001441 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001442 *
1443 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001444 * 0 on success
1445 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001446 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001447 */
1448int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001449rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001450{
1451 might_sleep();
1452
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001453 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1454 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001455 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001456}
1457EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1458
1459/**
1460 * rt_mutex_trylock - try to lock a rt_mutex
1461 *
1462 * @lock: the rt_mutex to be locked
1463 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001464 * This function can only be called in thread context. It's safe to
1465 * call it from atomic regions, but not from hard interrupt or soft
1466 * interrupt context.
1467 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001468 * Returns 1 on success and 0 on contention
1469 */
1470int __sched rt_mutex_trylock(struct rt_mutex *lock)
1471{
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001472 if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
1473 return 0;
1474
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001475 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1476}
1477EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1478
1479/**
1480 * rt_mutex_unlock - unlock a rt_mutex
1481 *
1482 * @lock: the rt_mutex to be unlocked
1483 */
1484void __sched rt_mutex_unlock(struct rt_mutex *lock)
1485{
1486 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1487}
1488EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1489
Luis Henriques23b94b92009-04-29 21:54:51 +01001490/**
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001491 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1492 * @lock: the rt_mutex to be unlocked
1493 *
1494 * Returns: true/false indicating whether priority adjustment is
1495 * required or not.
1496 */
1497bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1498 struct wake_q_head *wqh)
1499{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001500 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001501 rt_mutex_deadlock_account_unlock(current);
1502 return false;
1503 }
1504 return rt_mutex_slowunlock(lock, wqh);
1505}
1506
1507/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001508 * rt_mutex_destroy - mark a mutex unusable
1509 * @lock: the mutex to be destroyed
1510 *
1511 * This function marks the mutex uninitialized, and any subsequent
1512 * use of the mutex is forbidden. The mutex must not be locked when
1513 * this function is called.
1514 */
1515void rt_mutex_destroy(struct rt_mutex *lock)
1516{
1517 WARN_ON(rt_mutex_is_locked(lock));
1518#ifdef CONFIG_DEBUG_RT_MUTEXES
1519 lock->magic = NULL;
1520#endif
1521}
1522
1523EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1524
1525/**
1526 * __rt_mutex_init - initialize the rt lock
1527 *
1528 * @lock: the rt lock to be initialized
1529 *
1530 * Initialize the rt lock to unlocked state.
1531 *
1532 * Initializing of a locked rt lock is not allowed
1533 */
1534void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1535{
1536 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001537 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001538 lock->waiters = RB_ROOT;
1539 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001540
1541 debug_rt_mutex_init(lock, name);
1542}
1543EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001544
1545/**
1546 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1547 * proxy owner
1548 *
1549 * @lock: the rt_mutex to be locked
1550 * @proxy_owner:the task to set as owner
1551 *
1552 * No locking. Caller has to do serializing itself
1553 * Special API call for PI-futex support
1554 */
1555void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1556 struct task_struct *proxy_owner)
1557{
1558 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001559 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001560 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001561 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1562}
1563
1564/**
1565 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1566 *
1567 * @lock: the rt_mutex to be locked
1568 *
1569 * No locking. Caller has to do serializing itself
1570 * Special API call for PI-futex support
1571 */
1572void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1573 struct task_struct *proxy_owner)
1574{
1575 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001576 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001577 rt_mutex_deadlock_account_unlock(proxy_owner);
1578}
1579
1580/**
Darren Hart8dac4562009-04-03 13:40:12 -07001581 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1582 * @lock: the rt_mutex to take
1583 * @waiter: the pre-initialized rt_mutex_waiter
1584 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001585 *
1586 * Returns:
1587 * 0 - task blocked on lock
1588 * 1 - acquired the lock for task, caller should wake it up
1589 * <0 - error
1590 *
1591 * Special API call for FUTEX_REQUEUE_PI support.
1592 */
1593int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1594 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001595 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001596{
1597 int ret;
1598
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001599 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001600
Lai Jiangshan81612392011-01-14 17:09:41 +08001601 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001602 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001603 return 1;
1604 }
1605
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001606 /* We enforce deadlock detection for futexes */
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001607 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1608 RT_MUTEX_FULL_CHAINWALK);
Darren Hart8dac4562009-04-03 13:40:12 -07001609
Lai Jiangshan81612392011-01-14 17:09:41 +08001610 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001611 /*
1612 * Reset the return value. We might have
1613 * returned with -EDEADLK and the owner
1614 * released the lock while we were walking the
1615 * pi chain. Let the waiter sort it out.
1616 */
1617 ret = 0;
1618 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001619
1620 if (unlikely(ret))
1621 remove_waiter(lock, waiter);
1622
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001623 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001624
1625 debug_rt_mutex_print_deadlock(waiter);
1626
1627 return ret;
1628}
1629
1630/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001631 * rt_mutex_next_owner - return the next owner of the lock
1632 *
1633 * @lock: the rt lock query
1634 *
1635 * Returns the next owner of the lock or NULL
1636 *
1637 * Caller has to serialize against other accessors to the lock
1638 * itself.
1639 *
1640 * Special API call for PI-futex support
1641 */
1642struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1643{
1644 if (!rt_mutex_has_waiters(lock))
1645 return NULL;
1646
1647 return rt_mutex_top_waiter(lock)->task;
1648}
Darren Hart8dac4562009-04-03 13:40:12 -07001649
1650/**
1651 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1652 * @lock: the rt_mutex we were woken on
1653 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001654 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001655 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001656 *
1657 * Complete the lock acquisition started our behalf by another thread.
1658 *
1659 * Returns:
1660 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001661 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001662 *
1663 * Special API call for PI-futex requeue support
1664 */
1665int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1666 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001667 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001668{
1669 int ret;
1670
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001671 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001672
1673 set_current_state(TASK_INTERRUPTIBLE);
1674
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001675 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001676 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001677
Lai Jiangshan81612392011-01-14 17:09:41 +08001678 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001679 remove_waiter(lock, waiter);
1680
1681 /*
1682 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1683 * have to fix that up.
1684 */
1685 fixup_rt_mutex_waiters(lock);
1686
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001687 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001688
Darren Hart8dac4562009-04-03 13:40:12 -07001689 return ret;
1690}