blob: d4f79849136192c00b1aa20833446602e05fed7f [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 Rostedtd07fe82c22006-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 Molnar84f001e2017-02-01 16:36:40 +010018#include <linux/sched/wake_q.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070019#include <linux/timer.h>
20
21#include "rtmutex_common.h"
22
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070023/*
24 * lock->owner state tracking:
25 *
Lai Jiangshan81612392011-01-14 17:09:41 +080026 * lock->owner holds the task_struct pointer of the owner. Bit 0
27 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070028 *
Lai Jiangshan81612392011-01-14 17:09:41 +080029 * owner bit0
30 * NULL 0 lock is free (fast acquire possible)
31 * NULL 1 lock is free and has waiters and the top waiter
32 * is going to take the lock*
33 * taskpointer 0 lock is held (fast release possible)
34 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070035 *
36 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080037 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070038 *
Lai Jiangshan81612392011-01-14 17:09:41 +080039 * (*) It also can be a transitional state when grabbing the lock
40 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
41 * we need to set the bit0 before looking at the lock, and the owner may be
42 * NULL in this small time, hence this can be a transitional state.
43 *
44 * (**) There is a small time when bit 0 is set but there are no
45 * waiters. This can happen when grabbing the lock in the slow path.
46 * To prevent a cmpxchg of the owner releasing the lock, we need to
47 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070048 */
49
Thomas Gleixnerbd197232007-06-17 21:11:10 +020050static void
Lai Jiangshan81612392011-01-14 17:09:41 +080051rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070052{
Lai Jiangshan81612392011-01-14 17:09:41 +080053 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070054
55 if (rt_mutex_has_waiters(lock))
56 val |= RT_MUTEX_HAS_WAITERS;
57
58 lock->owner = (struct task_struct *)val;
59}
60
61static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
62{
63 lock->owner = (struct task_struct *)
64 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
65}
66
67static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
68{
Thomas Gleixnerdbb26052016-11-30 21:04:41 +000069 unsigned long owner, *p = (unsigned long *) &lock->owner;
70
71 if (rt_mutex_has_waiters(lock))
72 return;
73
74 /*
75 * The rbtree has no waiters enqueued, now make sure that the
76 * lock->owner still has the waiters bit set, otherwise the
77 * following can happen:
78 *
79 * CPU 0 CPU 1 CPU2
80 * l->owner=T1
81 * rt_mutex_lock(l)
82 * lock(l->lock)
83 * l->owner = T1 | HAS_WAITERS;
84 * enqueue(T2)
85 * boost()
86 * unlock(l->lock)
87 * block()
88 *
89 * rt_mutex_lock(l)
90 * lock(l->lock)
91 * l->owner = T1 | HAS_WAITERS;
92 * enqueue(T3)
93 * boost()
94 * unlock(l->lock)
95 * block()
96 * signal(->T2) signal(->T3)
97 * lock(l->lock)
98 * dequeue(T2)
99 * deboost()
100 * unlock(l->lock)
101 * lock(l->lock)
102 * dequeue(T3)
103 * ==> wait list is empty
104 * deboost()
105 * unlock(l->lock)
106 * lock(l->lock)
107 * fixup_rt_mutex_waiters()
108 * if (wait_list_empty(l) {
109 * l->owner = owner
110 * owner = l->owner & ~HAS_WAITERS;
111 * ==> l->owner = T1
112 * }
113 * lock(l->lock)
114 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
115 * if (wait_list_empty(l) {
116 * owner = l->owner & ~HAS_WAITERS;
117 * cmpxchg(l->owner, T1, NULL)
118 * ===> Success (l->owner = NULL)
119 *
120 * l->owner = owner
121 * ==> l->owner = T1
122 * }
123 *
124 * With the check for the waiter bit in place T3 on CPU2 will not
125 * overwrite. All tasks fiddling with the waiters bit are
126 * serialized by l->lock, so nothing else can modify the waiters
127 * bit. If the bit is set then nothing can change l->owner either
128 * so the simple RMW is safe. The cmpxchg() will simply fail if it
129 * happens in the middle of the RMW because the waiters bit is
130 * still set.
131 */
132 owner = READ_ONCE(*p);
133 if (owner & RT_MUTEX_HAS_WAITERS)
134 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700135}
136
137/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100138 * We can speed up the acquire/release, if there's no debugging state to be
139 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200140 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100141#ifndef CONFIG_DEBUG_RT_MUTEXES
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700142# define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
143# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
144# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
145
146/*
147 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
148 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
149 * relaxed semantics suffice.
150 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200151static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
152{
153 unsigned long owner, *p = (unsigned long *) &lock->owner;
154
155 do {
156 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700157 } while (cmpxchg_relaxed(p, owner,
158 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200159}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000160
161/*
162 * Safe fastpath aware unlock:
163 * 1) Clear the waiters bit
164 * 2) Drop lock->wait_lock
165 * 3) Try to unlock the lock with cmpxchg
166 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100167static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
168 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000169 __releases(lock->wait_lock)
170{
171 struct task_struct *owner = rt_mutex_owner(lock);
172
173 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100174 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000175 /*
176 * If a new waiter comes in between the unlock and the cmpxchg
177 * we have two situations:
178 *
179 * unlock(wait_lock);
180 * lock(wait_lock);
181 * cmpxchg(p, owner, 0) == owner
182 * mark_rt_mutex_waiters(lock);
183 * acquire(lock);
184 * or:
185 *
186 * unlock(wait_lock);
187 * lock(wait_lock);
188 * mark_rt_mutex_waiters(lock);
189 *
190 * cmpxchg(p, owner, 0) != owner
191 * enqueue_waiter();
192 * unlock(wait_lock);
193 * lock(wait_lock);
194 * wake waiter();
195 * unlock(wait_lock);
196 * lock(wait_lock);
197 * acquire(lock);
198 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700199 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000200}
201
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200202#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700203# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
204# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
205# define rt_mutex_cmpxchg_release(l,c,n) (0)
206
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200207static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
208{
209 lock->owner = (struct task_struct *)
210 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
211}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000212
213/*
214 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
215 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100216static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
217 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000218 __releases(lock->wait_lock)
219{
220 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100221 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000222 return true;
223}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200224#endif
225
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100226static inline int
227rt_mutex_waiter_less(struct rt_mutex_waiter *left,
228 struct rt_mutex_waiter *right)
229{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100230 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100231 return 1;
232
233 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100234 * If both waiters have dl_prio(), we check the deadlines of the
235 * associated tasks.
236 * If left waiter has a dl_prio(), and we didn't return 1 above,
237 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100238 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100239 if (dl_prio(left->prio))
Juri Lellif5240572015-09-02 11:01:35 +0100240 return dl_time_before(left->task->dl.deadline,
241 right->task->dl.deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100242
243 return 0;
244}
245
246static void
247rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
248{
249 struct rb_node **link = &lock->waiters.rb_node;
250 struct rb_node *parent = NULL;
251 struct rt_mutex_waiter *entry;
252 int leftmost = 1;
253
254 while (*link) {
255 parent = *link;
256 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
257 if (rt_mutex_waiter_less(waiter, entry)) {
258 link = &parent->rb_left;
259 } else {
260 link = &parent->rb_right;
261 leftmost = 0;
262 }
263 }
264
265 if (leftmost)
266 lock->waiters_leftmost = &waiter->tree_entry;
267
268 rb_link_node(&waiter->tree_entry, parent, link);
269 rb_insert_color(&waiter->tree_entry, &lock->waiters);
270}
271
272static void
273rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
274{
275 if (RB_EMPTY_NODE(&waiter->tree_entry))
276 return;
277
278 if (lock->waiters_leftmost == &waiter->tree_entry)
279 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
280
281 rb_erase(&waiter->tree_entry, &lock->waiters);
282 RB_CLEAR_NODE(&waiter->tree_entry);
283}
284
285static void
286rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
287{
288 struct rb_node **link = &task->pi_waiters.rb_node;
289 struct rb_node *parent = NULL;
290 struct rt_mutex_waiter *entry;
291 int leftmost = 1;
292
293 while (*link) {
294 parent = *link;
295 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
296 if (rt_mutex_waiter_less(waiter, entry)) {
297 link = &parent->rb_left;
298 } else {
299 link = &parent->rb_right;
300 leftmost = 0;
301 }
302 }
303
304 if (leftmost)
305 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
306
307 rb_link_node(&waiter->pi_tree_entry, parent, link);
308 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
309}
310
311static void
312rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
313{
314 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
315 return;
316
317 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
318 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
319
320 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
321 RB_CLEAR_NODE(&waiter->pi_tree_entry);
322}
323
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200324/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100325 * Calculate task priority from the waiter tree priority
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700326 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100327 * Return task->normal_prio when the waiter tree is empty or when
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700328 * the waiter is not allowed to do priority boosting
329 */
330int rt_mutex_getprio(struct task_struct *task)
331{
332 if (likely(!task_has_pi_waiters(task)))
333 return task->normal_prio;
334
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100335 return min(task_top_pi_waiter(task)->prio,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700336 task->normal_prio);
337}
338
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100339struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
340{
341 if (likely(!task_has_pi_waiters(task)))
342 return NULL;
343
344 return task_top_pi_waiter(task)->task;
345}
346
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700347/*
Thomas Gleixner0782e632015-05-05 19:49:49 +0200348 * Called by sched_setscheduler() to get the priority which will be
349 * effective after the change.
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100350 */
Thomas Gleixner0782e632015-05-05 19:49:49 +0200351int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100352{
353 if (!task_has_pi_waiters(task))
Thomas Gleixner0782e632015-05-05 19:49:49 +0200354 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100355
Thomas Gleixner0782e632015-05-05 19:49:49 +0200356 if (task_top_pi_waiter(task)->task->prio <= newprio)
357 return task_top_pi_waiter(task)->task->prio;
358 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100359}
360
361/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700362 * Adjust the priority of a task, after its pi_waiters got modified.
363 *
364 * This can be both boosting and unboosting. task->pi_lock must be held.
365 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200366static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700367{
368 int prio = rt_mutex_getprio(task);
369
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100370 if (task->prio != prio || dl_prio(prio))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700371 rt_mutex_setprio(task, prio);
372}
373
374/*
375 * Adjust task priority (undo boosting). Called from the exit path of
376 * rt_mutex_slowunlock() and rt_mutex_slowlock().
377 *
378 * (Note: We do this outside of the protection of lock->wait_lock to
379 * allow the lock to be taken while or before we readjust the priority
380 * of task. We do not use the spin_xx_mutex() variants here as we are
381 * outside of the debug path.)
382 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +0200383void rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700384{
385 unsigned long flags;
386
Thomas Gleixner1d615482009-11-17 14:54:03 +0100387 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700388 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100389 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700390}
391
392/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000393 * Deadlock detection is conditional:
394 *
395 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
396 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
397 *
398 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
399 * conducted independent of the detect argument.
400 *
401 * If the waiter argument is NULL this indicates the deboost path and
402 * deadlock detection is disabled independent of the detect argument
403 * and the config settings.
404 */
405static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
406 enum rtmutex_chainwalk chwalk)
407{
408 /*
409 * This is just a wrapper function for the following call,
410 * because debug_rt_mutex_detect_deadlock() smells like a magic
411 * debug feature and I wanted to keep the cond function in the
412 * main source file along with the comments instead of having
413 * two of the same in the headers.
414 */
415 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
416}
417
418/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700419 * Max number of times we'll walk the boosting chain:
420 */
421int max_lock_depth = 1024;
422
Thomas Gleixner82084982014-06-05 11:16:12 +0200423static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
424{
425 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
426}
427
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700428/*
429 * Adjust the priority chain. Also used for deadlock detection.
430 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200431 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200432 * @task: the task owning the mutex (owner) for which a chain walk is
433 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900434 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200435 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
436 * things for a task that has just got its priority adjusted, and
437 * is waiting on a mutex)
438 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
439 * we dropped its pi_lock. Is never dereferenced, only used for
440 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200441 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200442 * its priority to the mutex owner (can be NULL in the case
443 * depicted above or if the top waiter is gone away and we are
444 * actually deboosting the owner)
445 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200446 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700447 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200448 *
449 * Chain walk basics and protection scope
450 *
451 * [R] refcount on task
452 * [P] task->pi_lock held
453 * [L] rtmutex->wait_lock held
454 *
455 * Step Description Protected by
456 * function arguments:
457 * @task [R]
458 * @orig_lock if != NULL @top_task is blocked on it
459 * @next_lock Unprotected. Cannot be
460 * dereferenced. Only used for
461 * comparison.
462 * @orig_waiter if != NULL @top_task is blocked on it
463 * @top_task current, or in case of proxy
464 * locking protected by calling
465 * code
466 * again:
467 * loop_sanity_check();
468 * retry:
469 * [1] lock(task->pi_lock); [R] acquire [P]
470 * [2] waiter = task->pi_blocked_on; [P]
471 * [3] check_exit_conditions_1(); [P]
472 * [4] lock = waiter->lock; [P]
473 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
474 * unlock(task->pi_lock); release [P]
475 * goto retry;
476 * }
477 * [6] check_exit_conditions_2(); [P] + [L]
478 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
479 * [8] unlock(task->pi_lock); release [P]
480 * put_task_struct(task); release [R]
481 * [9] check_exit_conditions_3(); [L]
482 * [10] task = owner(lock); [L]
483 * get_task_struct(task); [L] acquire [R]
484 * lock(task->pi_lock); [L] acquire [P]
485 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
486 * [12] check_exit_conditions_4(); [P] + [L]
487 * [13] unlock(task->pi_lock); release [P]
488 * unlock(lock->wait_lock); release [L]
489 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700490 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200491static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000492 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200493 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200494 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200495 struct rt_mutex_waiter *orig_waiter,
496 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700497{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700498 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000499 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000500 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000501 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000502 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000503 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700504
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000505 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700506
507 /*
508 * The (de)boosting is a step by step approach with a lot of
509 * pitfalls. We want this to be preemptible and we want hold a
510 * maximum of two locks per step. So we have to check
511 * carefully whether things change under us.
512 */
513 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200514 /*
515 * We limit the lock chain length for each invocation.
516 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700517 if (++depth > max_lock_depth) {
518 static int prev_max;
519
520 /*
521 * Print this only once. If the admin changes the limit,
522 * print a new message when reaching the limit again.
523 */
524 if (prev_max != max_lock_depth) {
525 prev_max = max_lock_depth;
526 printk(KERN_WARNING "Maximum lock depth %d reached "
527 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700528 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700529 }
530 put_task_struct(task);
531
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200532 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700533 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200534
535 /*
536 * We are fully preemptible here and only hold the refcount on
537 * @task. So everything can have changed under us since the
538 * caller or our own code below (goto retry/again) dropped all
539 * locks.
540 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700541 retry:
542 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200543 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700544 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100545 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700546
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200547 /*
548 * [2] Get the waiter on which @task is blocked on.
549 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700550 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200551
552 /*
553 * [3] check_exit_conditions_1() protected by task->pi_lock.
554 */
555
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700556 /*
557 * Check whether the end of the boosting chain has been
558 * reached or the state of the chain has changed while we
559 * dropped the locks.
560 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800561 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700562 goto out_unlock_pi;
563
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700564 /*
565 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800566 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700567 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800568 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700569 goto out_unlock_pi;
570
571 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200572 * We dropped all locks after taking a refcount on @task, so
573 * the task might have moved on in the lock chain or even left
574 * the chain completely and blocks now on an unrelated lock or
575 * on @orig_lock.
576 *
577 * We stored the lock on which @task was blocked in @next_lock,
578 * so we can detect the chain change.
579 */
580 if (next_lock != waiter->lock)
581 goto out_unlock_pi;
582
583 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700584 * Drop out, when the task has no waiters. Note,
585 * top_waiter can be NULL, when we are in the deboosting
586 * mode!
587 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000588 if (top_waiter) {
589 if (!task_has_pi_waiters(task))
590 goto out_unlock_pi;
591 /*
592 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000593 * are not the top pi waiter of the task. If deadlock
594 * detection is enabled we continue, but stop the
595 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000596 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000597 if (top_waiter != task_top_pi_waiter(task)) {
598 if (!detect_deadlock)
599 goto out_unlock_pi;
600 else
601 requeue = false;
602 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000603 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700604
605 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000606 * If the waiter priority is the same as the task priority
607 * then there is no further priority adjustment necessary. If
608 * deadlock detection is off, we stop the chain walk. If its
609 * enabled we continue, but stop the requeueing in the chain
610 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700611 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000612 if (waiter->prio == task->prio) {
613 if (!detect_deadlock)
614 goto out_unlock_pi;
615 else
616 requeue = false;
617 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700618
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200619 /*
620 * [4] Get the next lock
621 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700622 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200623 /*
624 * [5] We need to trylock here as we are holding task->pi_lock,
625 * which is the reverse lock order versus the other rtmutex
626 * operations.
627 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100628 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100629 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700630 cpu_relax();
631 goto retry;
632 }
633
Thomas Gleixner397335f2014-05-22 03:25:39 +0000634 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200635 * [6] check_exit_conditions_2() protected by task->pi_lock and
636 * lock->wait_lock.
637 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000638 * Deadlock detection. If the lock is the same as the original
639 * lock which caused us to walk the lock chain or if the
640 * current lock is owned by the task which initiated the chain
641 * walk, we detected a deadlock.
642 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700643 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000644 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100645 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200646 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700647 goto out_unlock_pi;
648 }
649
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000650 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000651 * If we just follow the lock chain for deadlock detection, no
652 * need to do all the requeue operations. To avoid a truckload
653 * of conditionals around the various places below, just do the
654 * minimum chain walk checks.
655 */
656 if (!requeue) {
657 /*
658 * No requeue[7] here. Just release @task [8]
659 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100660 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000661 put_task_struct(task);
662
663 /*
664 * [9] check_exit_conditions_3 protected by lock->wait_lock.
665 * If there is no owner of the lock, end of chain.
666 */
667 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100668 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000669 return 0;
670 }
671
672 /* [10] Grab the next task, i.e. owner of @lock */
673 task = rt_mutex_owner(lock);
674 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100675 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000676
677 /*
678 * No requeue [11] here. We just do deadlock detection.
679 *
680 * [12] Store whether owner is blocked
681 * itself. Decision is made after dropping the locks
682 */
683 next_lock = task_blocked_on_lock(task);
684 /*
685 * Get the top waiter for the next iteration
686 */
687 top_waiter = rt_mutex_top_waiter(lock);
688
689 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100690 raw_spin_unlock(&task->pi_lock);
691 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000692
693 /* If owner is not blocked, end of chain. */
694 if (!next_lock)
695 goto out_put_task;
696 goto again;
697 }
698
699 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000700 * Store the current top waiter before doing the requeue
701 * operation on @lock. We need it for the boost/deboost
702 * decision below.
703 */
704 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700705
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700706 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100707 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100708 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100709 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700710
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200711 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100712 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200713 put_task_struct(task);
714
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000715 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200716 * [9] check_exit_conditions_3 protected by lock->wait_lock.
717 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000718 * We must abort the chain walk if there is no lock owner even
719 * in the dead lock detection case, as we have nothing to
720 * follow here. This is the end of the chain we are walking.
721 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800722 if (!rt_mutex_owner(lock)) {
723 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200724 * If the requeue [7] above changed the top waiter,
725 * then we need to wake the new top waiter up to try
726 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800727 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000728 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800729 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100730 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200731 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800732 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700733
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200734 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700735 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700736 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100737 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700738
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200739 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700740 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000741 /*
742 * The waiter became the new top (highest priority)
743 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700744 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000745 * and adjust the priority of the owner.
746 */
747 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100748 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700749 __rt_mutex_adjust_prio(task);
750
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000751 } else if (prerequeue_top_waiter == waiter) {
752 /*
753 * The waiter was the top waiter on the lock, but is
754 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700755 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000756 * (highest priority) waiter and adjust the priority
757 * of the owner.
758 * The new top waiter is stored in @waiter so that
759 * @waiter == @top_waiter evaluates to true below and
760 * we continue to deboost the rest of the chain.
761 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100762 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700763 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100764 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700765 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000766 } else {
767 /*
768 * Nothing changed. No need to do any priority
769 * adjustment.
770 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700771 }
772
Thomas Gleixner82084982014-06-05 11:16:12 +0200773 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200774 * [12] check_exit_conditions_4() protected by task->pi_lock
775 * and lock->wait_lock. The actual decisions are made after we
776 * dropped the locks.
777 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200778 * Check whether the task which owns the current lock is pi
779 * blocked itself. If yes we store a pointer to the lock for
780 * the lock chain change detection above. After we dropped
781 * task->pi_lock next_lock cannot be dereferenced anymore.
782 */
783 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000784 /*
785 * Store the top waiter of @lock for the end of chain walk
786 * decision below.
787 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700788 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200789
790 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100791 raw_spin_unlock(&task->pi_lock);
792 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700793
Thomas Gleixner82084982014-06-05 11:16:12 +0200794 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200795 * Make the actual exit decisions [12], based on the stored
796 * values.
797 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200798 * We reached the end of the lock chain. Stop right here. No
799 * point to go back just to figure that out.
800 */
801 if (!next_lock)
802 goto out_put_task;
803
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000804 /*
805 * If the current waiter is not the top waiter on the lock,
806 * then we can stop the chain walk here if we are not in full
807 * deadlock detection mode.
808 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700809 if (!detect_deadlock && waiter != top_waiter)
810 goto out_put_task;
811
812 goto again;
813
814 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100815 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700816 out_put_task:
817 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700818
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700819 return ret;
820}
821
822/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700823 * Try to take an rt-mutex
824 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100825 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800826 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200827 * @lock: The lock to be acquired.
828 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700829 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200830 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700831 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800832static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200833 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700834{
835 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200836 * Before testing whether we can acquire @lock, we set the
837 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
838 * other tasks which try to modify @lock into the slow path
839 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700840 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200841 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
842 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700843 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200844 * - There is a lock owner. The caller must fixup the
845 * transient state if it does a trylock or leaves the lock
846 * function due to a signal or timeout.
847 *
848 * - @task acquires the lock and there are no other
849 * waiters. This is undone in rt_mutex_set_owner(@task) at
850 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700851 */
852 mark_rt_mutex_waiters(lock);
853
Thomas Gleixner358c3312014-06-11 01:01:13 +0200854 /*
855 * If @lock has an owner, give up.
856 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800857 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700858 return 0;
859
Lai Jiangshan81612392011-01-14 17:09:41 +0800860 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200861 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700862 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200863 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800864 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200865 if (waiter) {
866 /*
867 * If waiter is not the highest priority waiter of
868 * @lock, give up.
869 */
870 if (waiter != rt_mutex_top_waiter(lock))
871 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800872
873 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200874 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700875 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200876 */
877 rt_mutex_dequeue(lock, waiter);
878
879 } else {
880 /*
881 * If the lock has waiters already we check whether @task is
882 * eligible to take over the lock.
883 *
884 * If there are no other waiters, @task can acquire
885 * the lock. @task->pi_blocked_on is NULL, so it does
886 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800887 */
888 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200889 /*
890 * If @task->prio is greater than or equal to
891 * the top waiter priority (kernel view),
892 * @task lost.
893 */
894 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
895 return 0;
896
897 /*
898 * The current top waiter stays enqueued. We
899 * don't have to change anything in the lock
900 * waiters order.
901 */
902 } else {
903 /*
904 * No waiters. Take the lock without the
905 * pi_lock dance.@task->pi_blocked_on is NULL
906 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700907 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200908 */
909 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800910 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800911 }
912
Thomas Gleixner358c3312014-06-11 01:01:13 +0200913 /*
914 * Clear @task->pi_blocked_on. Requires protection by
915 * @task->pi_lock. Redundant operation for the @waiter == NULL
916 * case, but conditionals are more expensive than a redundant
917 * store.
918 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100919 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200920 task->pi_blocked_on = NULL;
921 /*
922 * Finish the lock acquisition. @task is the new owner. If
923 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700924 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200925 */
926 if (rt_mutex_has_waiters(lock))
927 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100928 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200929
930takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700931 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700932 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700933
Thomas Gleixner358c3312014-06-11 01:01:13 +0200934 /*
935 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
936 * are still waiters or clears it.
937 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800938 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700939
Lai Jiangshan81612392011-01-14 17:09:41 +0800940 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700941
942 return 1;
943}
944
945/*
946 * Task blocks on lock.
947 *
948 * Prepare waiter and propagate pi chain
949 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100950 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700951 */
952static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
953 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700954 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000955 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700956{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700957 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700958 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200959 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700960 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700961
Thomas Gleixner397335f2014-05-22 03:25:39 +0000962 /*
963 * Early deadlock detection. We really don't want the task to
964 * enqueue on itself just to untangle the mess later. It's not
965 * only an optimization. We drop the locks, so another waiter
966 * can come in before the chain walk detects the deadlock. So
967 * the other will detect the deadlock and return -EDEADLOCK,
968 * which is wrong, as the other waiter is not in a deadlock
969 * situation.
970 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200971 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000972 return -EDEADLK;
973
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100974 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700975 __rt_mutex_adjust_prio(task);
976 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700977 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100978 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700979
980 /* Get the top priority waiter on the lock */
981 if (rt_mutex_has_waiters(lock))
982 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100983 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700984
Darren Hart8dac4562009-04-03 13:40:12 -0700985 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700986
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100987 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700988
Lai Jiangshan81612392011-01-14 17:09:41 +0800989 if (!owner)
990 return 0;
991
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100992 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700993 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100994 rt_mutex_dequeue_pi(owner, top_waiter);
995 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700996
997 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700998 if (owner->pi_blocked_on)
999 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001000 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -07001001 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +02001002 }
Steven Rostedtdb630632006-09-29 01:59:44 -07001003
Thomas Gleixner82084982014-06-05 11:16:12 +02001004 /* Store the lock on which owner is blocked or NULL */
1005 next_lock = task_blocked_on_lock(owner);
1006
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001007 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +02001008 /*
1009 * Even if full deadlock detection is on, if the owner is not
1010 * blocked itself, we can avoid finding this out in the chain
1011 * walk.
1012 */
1013 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001014 return 0;
1015
Steven Rostedtdb630632006-09-29 01:59:44 -07001016 /*
1017 * The owner can't disappear while holding a lock,
1018 * so the owner struct is protected by wait_lock.
1019 * Gets dropped in rt_mutex_adjust_prio_chain()!
1020 */
1021 get_task_struct(owner);
1022
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001023 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001024
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001025 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +02001026 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001027
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001028 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001029
1030 return res;
1031}
1032
1033/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001034 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001035 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001036 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001037 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001038 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001039static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1040 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001041{
1042 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001043
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001044 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001045
1046 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001047
1048 /*
1049 * Remove it from current->pi_waiters. We do not adjust a
1050 * possible priority boost right now. We execute wakeup in the
1051 * boosted mode and go back to normal after releasing
1052 * lock->wait_lock.
1053 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001054 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001055
Thomas Gleixner27e35712014-06-11 18:44:04 +00001056 /*
1057 * As we are waking up the top waiter, and the waiter stays
1058 * queued on the lock until it gets the lock, this lock
1059 * obviously has waiters. Just set the bit here and this has
1060 * the added benefit of forcing all new tasks into the
1061 * slow path making sure no task of lower priority than
1062 * the top waiter can steal this lock.
1063 */
1064 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001065
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001066 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001067
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001068 wake_q_add(wake_q, waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001069}
1070
1071/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001072 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001073 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001074 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001075 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001076 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001077static void remove_waiter(struct rt_mutex *lock,
1078 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001079{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001080 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001081 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001082 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001083
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001084 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001085 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001086 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001087 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001088
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001089 /*
1090 * Only update priority if the waiter was the highest priority
1091 * waiter of the lock and there is an owner to update.
1092 */
1093 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001094 return;
1095
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001096 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001097
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001098 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001099
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001100 if (rt_mutex_has_waiters(lock))
1101 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001102
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001103 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001104
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001105 /* Store the lock on which owner is blocked or NULL */
1106 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001107
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001108 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001109
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001110 /*
1111 * Don't walk the chain, if the owner task is not blocked
1112 * itself.
1113 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001114 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001115 return;
1116
Steven Rostedtdb630632006-09-29 01:59:44 -07001117 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1118 get_task_struct(owner);
1119
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001120 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001121
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001122 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1123 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001124
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001125 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001126}
1127
1128/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001129 * Recheck the pi chain, in case we got a priority setting
1130 *
1131 * Called from sched_setscheduler
1132 */
1133void rt_mutex_adjust_pi(struct task_struct *task)
1134{
1135 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001136 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001137 unsigned long flags;
1138
Thomas Gleixner1d615482009-11-17 14:54:03 +01001139 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001140
1141 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001142 if (!waiter || (waiter->prio == task->prio &&
1143 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001144 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001145 return;
1146 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001147 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001148 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001149
Steven Rostedtdb630632006-09-29 01:59:44 -07001150 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1151 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001152
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001153 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1154 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001155}
1156
Darren Hart8dac4562009-04-03 13:40:12 -07001157/**
1158 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1159 * @lock: the rt_mutex to take
1160 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001161 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001162 * @timeout: the pre-initialized and started timer, or NULL for none
1163 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001164 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001165 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001166 */
1167static int __sched
1168__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1169 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001170 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001171{
1172 int ret = 0;
1173
1174 for (;;) {
1175 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001176 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001177 break;
1178
1179 /*
1180 * TASK_INTERRUPTIBLE checks for signals and
1181 * timeout. Ignored otherwise.
1182 */
Steven Rostedt (VMware)4009f4b2017-01-19 11:32:34 -05001183 if (likely(state == TASK_INTERRUPTIBLE)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001184 /* Signal pending? */
1185 if (signal_pending(current))
1186 ret = -EINTR;
1187 if (timeout && !timeout->task)
1188 ret = -ETIMEDOUT;
1189 if (ret)
1190 break;
1191 }
1192
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001193 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001194
1195 debug_rt_mutex_print_deadlock(waiter);
1196
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001197 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001198
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001199 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001200 set_current_state(state);
1201 }
1202
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001203 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001204 return ret;
1205}
1206
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001207static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1208 struct rt_mutex_waiter *w)
1209{
1210 /*
1211 * If the result is not -EDEADLOCK or the caller requested
1212 * deadlock detection, nothing to do here.
1213 */
1214 if (res != -EDEADLOCK || detect_deadlock)
1215 return;
1216
1217 /*
1218 * Yell lowdly and stop the task right here.
1219 */
1220 rt_mutex_print_deadlock(w);
1221 while (1) {
1222 set_current_state(TASK_INTERRUPTIBLE);
1223 schedule();
1224 }
1225}
1226
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001227/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001228 * Slow path lock function:
1229 */
1230static int __sched
1231rt_mutex_slowlock(struct rt_mutex *lock, int state,
1232 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001233 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001234{
1235 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001236 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001237 int ret = 0;
1238
1239 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001240 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1241 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001242
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001243 /*
1244 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1245 * be called in early boot if the cmpxchg() fast path is disabled
1246 * (debug, no architecture support). In this case we will acquire the
1247 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1248 * enable interrupts in that early boot case. So we need to use the
1249 * irqsave/restore variants.
1250 */
1251 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001252
1253 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001254 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001255 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001256 return 0;
1257 }
1258
1259 set_current_state(state);
1260
1261 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001262 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001263 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001264
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001265 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001266
1267 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001268 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001269 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001270
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001271 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001272 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001273 if (rt_mutex_has_waiters(lock))
1274 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001275 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001276 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001277
1278 /*
1279 * try_to_take_rt_mutex() sets the waiter bit
1280 * unconditionally. We might have to fix that up.
1281 */
1282 fixup_rt_mutex_waiters(lock);
1283
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001284 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001285
1286 /* Remove pending timer: */
1287 if (unlikely(timeout))
1288 hrtimer_cancel(&timeout->timer);
1289
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001290 debug_rt_mutex_free_waiter(&waiter);
1291
1292 return ret;
1293}
1294
1295/*
1296 * Slow path try-lock function:
1297 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001298static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001299{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001300 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001301 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001302
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001303 /*
1304 * If the lock already has an owner we fail to get the lock.
1305 * This can be done without taking the @lock->wait_lock as
1306 * it is only being read, and this is a trylock anyway.
1307 */
1308 if (rt_mutex_owner(lock))
1309 return 0;
1310
1311 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001312 * The mutex has currently no owner. Lock the wait lock and try to
1313 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001314 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001315 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001316
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001317 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001318
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001319 /*
1320 * try_to_take_rt_mutex() sets the lock waiters bit
1321 * unconditionally. Clean this up.
1322 */
1323 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001324
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001325 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001326
1327 return ret;
1328}
1329
1330/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001331 * Slow path to release a rt-mutex.
1332 * Return whether the current task needs to undo a potential priority boosting.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001333 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001334static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1335 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001336{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001337 unsigned long flags;
1338
1339 /* irqsave required to support early boot calls */
1340 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001341
1342 debug_rt_mutex_unlock(lock);
1343
1344 rt_mutex_deadlock_account_unlock(current);
1345
Thomas Gleixner27e35712014-06-11 18:44:04 +00001346 /*
1347 * We must be careful here if the fast path is enabled. If we
1348 * have no waiters queued we cannot set owner to NULL here
1349 * because of:
1350 *
1351 * foo->lock->owner = NULL;
1352 * rtmutex_lock(foo->lock); <- fast path
1353 * free = atomic_dec_and_test(foo->refcnt);
1354 * rtmutex_unlock(foo->lock); <- fast path
1355 * if (free)
1356 * kfree(foo);
1357 * raw_spin_unlock(foo->lock->wait_lock);
1358 *
1359 * So for the fastpath enabled kernel:
1360 *
1361 * Nothing can set the waiters bit as long as we hold
1362 * lock->wait_lock. So we do the following sequence:
1363 *
1364 * owner = rt_mutex_owner(lock);
1365 * clear_rt_mutex_waiters(lock);
1366 * raw_spin_unlock(&lock->wait_lock);
1367 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1368 * return;
1369 * goto retry;
1370 *
1371 * The fastpath disabled variant is simple as all access to
1372 * lock->owner is serialized by lock->wait_lock:
1373 *
1374 * lock->owner = NULL;
1375 * raw_spin_unlock(&lock->wait_lock);
1376 */
1377 while (!rt_mutex_has_waiters(lock)) {
1378 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001379 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001380 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001381 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001382 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001383 }
1384
Thomas Gleixner27e35712014-06-11 18:44:04 +00001385 /*
1386 * The wakeup next waiter path does not suffer from the above
1387 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001388 *
1389 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001390 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001391 mark_wakeup_next_waiter(wake_q, lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001392
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001393 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001394
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001395 /* check PI boosting */
1396 return true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001397}
1398
1399/*
1400 * debug aware fast / slowpath lock,trylock,unlock
1401 *
1402 * The atomic acquire/release ops are compiled away, when either the
1403 * architecture does not support cmpxchg or when debugging is enabled.
1404 */
1405static inline int
1406rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001407 int (*slowfn)(struct rt_mutex *lock, int state,
1408 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001409 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001410{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001411 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001412 rt_mutex_deadlock_account_lock(lock, current);
1413 return 0;
1414 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001415 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001416}
1417
1418static inline int
1419rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001420 struct hrtimer_sleeper *timeout,
1421 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001422 int (*slowfn)(struct rt_mutex *lock, int state,
1423 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001424 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001425{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001426 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001427 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001428 rt_mutex_deadlock_account_lock(lock, current);
1429 return 0;
1430 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001431 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001432}
1433
1434static inline int
1435rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001436 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001437{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001438 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001439 rt_mutex_deadlock_account_lock(lock, current);
1440 return 1;
1441 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001442 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001443}
1444
1445static inline void
1446rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001447 bool (*slowfn)(struct rt_mutex *lock,
1448 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001449{
Waiman Long194a6b52016-11-17 11:46:38 -05001450 DEFINE_WAKE_Q(wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001451
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001452 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001453 rt_mutex_deadlock_account_unlock(current);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001454
1455 } else {
1456 bool deboost = slowfn(lock, &wake_q);
1457
1458 wake_up_q(&wake_q);
1459
1460 /* Undo pi boosting if necessary: */
1461 if (deboost)
1462 rt_mutex_adjust_prio(current);
1463 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001464}
1465
1466/**
1467 * rt_mutex_lock - lock a rt_mutex
1468 *
1469 * @lock: the rt_mutex to be locked
1470 */
1471void __sched rt_mutex_lock(struct rt_mutex *lock)
1472{
1473 might_sleep();
1474
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001475 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001476}
1477EXPORT_SYMBOL_GPL(rt_mutex_lock);
1478
1479/**
1480 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1481 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001482 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001483 *
1484 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001485 * 0 on success
1486 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001487 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001488int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001489{
1490 might_sleep();
1491
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001492 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001493}
1494EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1495
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001496/*
1497 * Futex variant with full deadlock detection.
1498 */
1499int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1500 struct hrtimer_sleeper *timeout)
1501{
1502 might_sleep();
1503
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001504 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1505 RT_MUTEX_FULL_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001506 rt_mutex_slowlock);
1507}
1508
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001509/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001510 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1511 * the timeout structure is provided
1512 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001513 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001514 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001515 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001516 *
1517 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001518 * 0 on success
1519 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001520 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001521 */
1522int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001523rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001524{
1525 might_sleep();
1526
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001527 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1528 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001529 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001530}
1531EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1532
1533/**
1534 * rt_mutex_trylock - try to lock a rt_mutex
1535 *
1536 * @lock: the rt_mutex to be locked
1537 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001538 * This function can only be called in thread context. It's safe to
1539 * call it from atomic regions, but not from hard interrupt or soft
1540 * interrupt context.
1541 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001542 * Returns 1 on success and 0 on contention
1543 */
1544int __sched rt_mutex_trylock(struct rt_mutex *lock)
1545{
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001546 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001547 return 0;
1548
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001549 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1550}
1551EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1552
1553/**
1554 * rt_mutex_unlock - unlock a rt_mutex
1555 *
1556 * @lock: the rt_mutex to be unlocked
1557 */
1558void __sched rt_mutex_unlock(struct rt_mutex *lock)
1559{
1560 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1561}
1562EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1563
Luis Henriques23b94b92009-04-29 21:54:51 +01001564/**
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001565 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1566 * @lock: the rt_mutex to be unlocked
1567 *
1568 * Returns: true/false indicating whether priority adjustment is
1569 * required or not.
1570 */
1571bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1572 struct wake_q_head *wqh)
1573{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001574 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001575 rt_mutex_deadlock_account_unlock(current);
1576 return false;
1577 }
1578 return rt_mutex_slowunlock(lock, wqh);
1579}
1580
1581/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001582 * rt_mutex_destroy - mark a mutex unusable
1583 * @lock: the mutex to be destroyed
1584 *
1585 * This function marks the mutex uninitialized, and any subsequent
1586 * use of the mutex is forbidden. The mutex must not be locked when
1587 * this function is called.
1588 */
1589void rt_mutex_destroy(struct rt_mutex *lock)
1590{
1591 WARN_ON(rt_mutex_is_locked(lock));
1592#ifdef CONFIG_DEBUG_RT_MUTEXES
1593 lock->magic = NULL;
1594#endif
1595}
1596
1597EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1598
1599/**
1600 * __rt_mutex_init - initialize the rt lock
1601 *
1602 * @lock: the rt lock to be initialized
1603 *
1604 * Initialize the rt lock to unlocked state.
1605 *
1606 * Initializing of a locked rt lock is not allowed
1607 */
1608void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1609{
1610 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001611 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001612 lock->waiters = RB_ROOT;
1613 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001614
1615 debug_rt_mutex_init(lock, name);
1616}
1617EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001618
1619/**
1620 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1621 * proxy owner
1622 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001623 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001624 * @proxy_owner:the task to set as owner
1625 *
1626 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001627 *
1628 * Special API call for PI-futex support. This initializes the rtmutex and
1629 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1630 * possible at this point because the pi_state which contains the rtmutex
1631 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001632 */
1633void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1634 struct task_struct *proxy_owner)
1635{
1636 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001637 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001638 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001639 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1640}
1641
1642/**
1643 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1644 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001645 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001646 *
1647 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001648 *
1649 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1650 * (debugging) state. Concurrent operations on this rt_mutex are not
1651 * possible because it belongs to the pi_state which is about to be freed
1652 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001653 */
1654void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1655 struct task_struct *proxy_owner)
1656{
1657 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001658 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001659 rt_mutex_deadlock_account_unlock(proxy_owner);
1660}
1661
1662/**
Darren Hart8dac4562009-04-03 13:40:12 -07001663 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1664 * @lock: the rt_mutex to take
1665 * @waiter: the pre-initialized rt_mutex_waiter
1666 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001667 *
1668 * Returns:
1669 * 0 - task blocked on lock
1670 * 1 - acquired the lock for task, caller should wake it up
1671 * <0 - error
1672 *
1673 * Special API call for FUTEX_REQUEUE_PI support.
1674 */
1675int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1676 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001677 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001678{
1679 int ret;
1680
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001681 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001682
Lai Jiangshan81612392011-01-14 17:09:41 +08001683 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001684 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001685 return 1;
1686 }
1687
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001688 /* We enforce deadlock detection for futexes */
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001689 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1690 RT_MUTEX_FULL_CHAINWALK);
Darren Hart8dac4562009-04-03 13:40:12 -07001691
Lai Jiangshan81612392011-01-14 17:09:41 +08001692 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001693 /*
1694 * Reset the return value. We might have
1695 * returned with -EDEADLK and the owner
1696 * released the lock while we were walking the
1697 * pi chain. Let the waiter sort it out.
1698 */
1699 ret = 0;
1700 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001701
1702 if (unlikely(ret))
1703 remove_waiter(lock, waiter);
1704
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001705 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001706
1707 debug_rt_mutex_print_deadlock(waiter);
1708
1709 return ret;
1710}
1711
1712/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001713 * rt_mutex_next_owner - return the next owner of the lock
1714 *
1715 * @lock: the rt lock query
1716 *
1717 * Returns the next owner of the lock or NULL
1718 *
1719 * Caller has to serialize against other accessors to the lock
1720 * itself.
1721 *
1722 * Special API call for PI-futex support
1723 */
1724struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1725{
1726 if (!rt_mutex_has_waiters(lock))
1727 return NULL;
1728
1729 return rt_mutex_top_waiter(lock)->task;
1730}
Darren Hart8dac4562009-04-03 13:40:12 -07001731
1732/**
1733 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1734 * @lock: the rt_mutex we were woken on
1735 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001736 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001737 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001738 *
1739 * Complete the lock acquisition started our behalf by another thread.
1740 *
1741 * Returns:
1742 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001743 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001744 *
1745 * Special API call for PI-futex requeue support
1746 */
1747int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1748 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001749 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001750{
1751 int ret;
1752
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001753 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001754
1755 set_current_state(TASK_INTERRUPTIBLE);
1756
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001757 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001758 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001759
Lai Jiangshan81612392011-01-14 17:09:41 +08001760 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001761 remove_waiter(lock, waiter);
1762
1763 /*
1764 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1765 * have to fix that up.
1766 */
1767 fixup_rt_mutex_waiters(lock);
1768
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001769 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001770
Darren Hart8dac4562009-04-03 13:40:12 -07001771 return ret;
1772}