blob: d8d7a153b7117339455e923061a94b72178abc5d [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 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 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100102static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
103 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000104 __releases(lock->wait_lock)
105{
106 struct task_struct *owner = rt_mutex_owner(lock);
107
108 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100109 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000110 /*
111 * If a new waiter comes in between the unlock and the cmpxchg
112 * we have two situations:
113 *
114 * unlock(wait_lock);
115 * lock(wait_lock);
116 * cmpxchg(p, owner, 0) == owner
117 * mark_rt_mutex_waiters(lock);
118 * acquire(lock);
119 * or:
120 *
121 * unlock(wait_lock);
122 * lock(wait_lock);
123 * mark_rt_mutex_waiters(lock);
124 *
125 * cmpxchg(p, owner, 0) != owner
126 * enqueue_waiter();
127 * unlock(wait_lock);
128 * lock(wait_lock);
129 * wake waiter();
130 * unlock(wait_lock);
131 * lock(wait_lock);
132 * acquire(lock);
133 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700134 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000135}
136
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200137#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700138# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
139# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
140# define rt_mutex_cmpxchg_release(l,c,n) (0)
141
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200142static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
143{
144 lock->owner = (struct task_struct *)
145 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
146}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000147
148/*
149 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
150 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100151static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
152 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000153 __releases(lock->wait_lock)
154{
155 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100156 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000157 return true;
158}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200159#endif
160
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100161static inline int
162rt_mutex_waiter_less(struct rt_mutex_waiter *left,
163 struct rt_mutex_waiter *right)
164{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100165 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100166 return 1;
167
168 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100169 * If both waiters have dl_prio(), we check the deadlines of the
170 * associated tasks.
171 * If left waiter has a dl_prio(), and we didn't return 1 above,
172 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100173 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100174 if (dl_prio(left->prio))
Juri Lellif5240572015-09-02 11:01:35 +0100175 return dl_time_before(left->task->dl.deadline,
176 right->task->dl.deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100177
178 return 0;
179}
180
181static void
182rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
183{
184 struct rb_node **link = &lock->waiters.rb_node;
185 struct rb_node *parent = NULL;
186 struct rt_mutex_waiter *entry;
187 int leftmost = 1;
188
189 while (*link) {
190 parent = *link;
191 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
192 if (rt_mutex_waiter_less(waiter, entry)) {
193 link = &parent->rb_left;
194 } else {
195 link = &parent->rb_right;
196 leftmost = 0;
197 }
198 }
199
200 if (leftmost)
201 lock->waiters_leftmost = &waiter->tree_entry;
202
203 rb_link_node(&waiter->tree_entry, parent, link);
204 rb_insert_color(&waiter->tree_entry, &lock->waiters);
205}
206
207static void
208rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
209{
210 if (RB_EMPTY_NODE(&waiter->tree_entry))
211 return;
212
213 if (lock->waiters_leftmost == &waiter->tree_entry)
214 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
215
216 rb_erase(&waiter->tree_entry, &lock->waiters);
217 RB_CLEAR_NODE(&waiter->tree_entry);
218}
219
220static void
221rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
222{
223 struct rb_node **link = &task->pi_waiters.rb_node;
224 struct rb_node *parent = NULL;
225 struct rt_mutex_waiter *entry;
226 int leftmost = 1;
227
228 while (*link) {
229 parent = *link;
230 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
231 if (rt_mutex_waiter_less(waiter, entry)) {
232 link = &parent->rb_left;
233 } else {
234 link = &parent->rb_right;
235 leftmost = 0;
236 }
237 }
238
239 if (leftmost)
240 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
241
242 rb_link_node(&waiter->pi_tree_entry, parent, link);
243 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
244}
245
246static void
247rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
248{
249 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
250 return;
251
252 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
253 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
254
255 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
256 RB_CLEAR_NODE(&waiter->pi_tree_entry);
257}
258
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200259/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100260 * Calculate task priority from the waiter tree priority
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700261 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100262 * Return task->normal_prio when the waiter tree is empty or when
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700263 * the waiter is not allowed to do priority boosting
264 */
265int rt_mutex_getprio(struct task_struct *task)
266{
267 if (likely(!task_has_pi_waiters(task)))
268 return task->normal_prio;
269
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100270 return min(task_top_pi_waiter(task)->prio,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700271 task->normal_prio);
272}
273
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100274struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
275{
276 if (likely(!task_has_pi_waiters(task)))
277 return NULL;
278
279 return task_top_pi_waiter(task)->task;
280}
281
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700282/*
Thomas Gleixner0782e632015-05-05 19:49:49 +0200283 * Called by sched_setscheduler() to get the priority which will be
284 * effective after the change.
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100285 */
Thomas Gleixner0782e632015-05-05 19:49:49 +0200286int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100287{
288 if (!task_has_pi_waiters(task))
Thomas Gleixner0782e632015-05-05 19:49:49 +0200289 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100290
Thomas Gleixner0782e632015-05-05 19:49:49 +0200291 if (task_top_pi_waiter(task)->task->prio <= newprio)
292 return task_top_pi_waiter(task)->task->prio;
293 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100294}
295
296/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700297 * Adjust the priority of a task, after its pi_waiters got modified.
298 *
299 * This can be both boosting and unboosting. task->pi_lock must be held.
300 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200301static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700302{
303 int prio = rt_mutex_getprio(task);
304
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100305 if (task->prio != prio || dl_prio(prio))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700306 rt_mutex_setprio(task, prio);
307}
308
309/*
310 * Adjust task priority (undo boosting). Called from the exit path of
311 * rt_mutex_slowunlock() and rt_mutex_slowlock().
312 *
313 * (Note: We do this outside of the protection of lock->wait_lock to
314 * allow the lock to be taken while or before we readjust the priority
315 * of task. We do not use the spin_xx_mutex() variants here as we are
316 * outside of the debug path.)
317 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +0200318void rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700319{
320 unsigned long flags;
321
Thomas Gleixner1d615482009-11-17 14:54:03 +0100322 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700323 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100324 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700325}
326
327/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000328 * Deadlock detection is conditional:
329 *
330 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
331 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
332 *
333 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
334 * conducted independent of the detect argument.
335 *
336 * If the waiter argument is NULL this indicates the deboost path and
337 * deadlock detection is disabled independent of the detect argument
338 * and the config settings.
339 */
340static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
341 enum rtmutex_chainwalk chwalk)
342{
343 /*
344 * This is just a wrapper function for the following call,
345 * because debug_rt_mutex_detect_deadlock() smells like a magic
346 * debug feature and I wanted to keep the cond function in the
347 * main source file along with the comments instead of having
348 * two of the same in the headers.
349 */
350 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
351}
352
353/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700354 * Max number of times we'll walk the boosting chain:
355 */
356int max_lock_depth = 1024;
357
Thomas Gleixner82084982014-06-05 11:16:12 +0200358static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
359{
360 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
361}
362
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700363/*
364 * Adjust the priority chain. Also used for deadlock detection.
365 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200366 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200367 * @task: the task owning the mutex (owner) for which a chain walk is
368 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900369 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200370 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
371 * things for a task that has just got its priority adjusted, and
372 * is waiting on a mutex)
373 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
374 * we dropped its pi_lock. Is never dereferenced, only used for
375 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200376 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200377 * its priority to the mutex owner (can be NULL in the case
378 * depicted above or if the top waiter is gone away and we are
379 * actually deboosting the owner)
380 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200381 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700382 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200383 *
384 * Chain walk basics and protection scope
385 *
386 * [R] refcount on task
387 * [P] task->pi_lock held
388 * [L] rtmutex->wait_lock held
389 *
390 * Step Description Protected by
391 * function arguments:
392 * @task [R]
393 * @orig_lock if != NULL @top_task is blocked on it
394 * @next_lock Unprotected. Cannot be
395 * dereferenced. Only used for
396 * comparison.
397 * @orig_waiter if != NULL @top_task is blocked on it
398 * @top_task current, or in case of proxy
399 * locking protected by calling
400 * code
401 * again:
402 * loop_sanity_check();
403 * retry:
404 * [1] lock(task->pi_lock); [R] acquire [P]
405 * [2] waiter = task->pi_blocked_on; [P]
406 * [3] check_exit_conditions_1(); [P]
407 * [4] lock = waiter->lock; [P]
408 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
409 * unlock(task->pi_lock); release [P]
410 * goto retry;
411 * }
412 * [6] check_exit_conditions_2(); [P] + [L]
413 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
414 * [8] unlock(task->pi_lock); release [P]
415 * put_task_struct(task); release [R]
416 * [9] check_exit_conditions_3(); [L]
417 * [10] task = owner(lock); [L]
418 * get_task_struct(task); [L] acquire [R]
419 * lock(task->pi_lock); [L] acquire [P]
420 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
421 * [12] check_exit_conditions_4(); [P] + [L]
422 * [13] unlock(task->pi_lock); release [P]
423 * unlock(lock->wait_lock); release [L]
424 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700425 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200426static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000427 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200428 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200429 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200430 struct rt_mutex_waiter *orig_waiter,
431 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700432{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700433 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000434 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000435 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000436 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000437 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000438 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700439
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000440 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700441
442 /*
443 * The (de)boosting is a step by step approach with a lot of
444 * pitfalls. We want this to be preemptible and we want hold a
445 * maximum of two locks per step. So we have to check
446 * carefully whether things change under us.
447 */
448 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200449 /*
450 * We limit the lock chain length for each invocation.
451 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700452 if (++depth > max_lock_depth) {
453 static int prev_max;
454
455 /*
456 * Print this only once. If the admin changes the limit,
457 * print a new message when reaching the limit again.
458 */
459 if (prev_max != max_lock_depth) {
460 prev_max = max_lock_depth;
461 printk(KERN_WARNING "Maximum lock depth %d reached "
462 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700463 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700464 }
465 put_task_struct(task);
466
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200467 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700468 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200469
470 /*
471 * We are fully preemptible here and only hold the refcount on
472 * @task. So everything can have changed under us since the
473 * caller or our own code below (goto retry/again) dropped all
474 * locks.
475 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700476 retry:
477 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200478 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700479 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100480 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700481
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200482 /*
483 * [2] Get the waiter on which @task is blocked on.
484 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700485 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200486
487 /*
488 * [3] check_exit_conditions_1() protected by task->pi_lock.
489 */
490
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700491 /*
492 * Check whether the end of the boosting chain has been
493 * reached or the state of the chain has changed while we
494 * dropped the locks.
495 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800496 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700497 goto out_unlock_pi;
498
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700499 /*
500 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800501 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700502 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800503 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700504 goto out_unlock_pi;
505
506 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200507 * We dropped all locks after taking a refcount on @task, so
508 * the task might have moved on in the lock chain or even left
509 * the chain completely and blocks now on an unrelated lock or
510 * on @orig_lock.
511 *
512 * We stored the lock on which @task was blocked in @next_lock,
513 * so we can detect the chain change.
514 */
515 if (next_lock != waiter->lock)
516 goto out_unlock_pi;
517
518 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700519 * Drop out, when the task has no waiters. Note,
520 * top_waiter can be NULL, when we are in the deboosting
521 * mode!
522 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000523 if (top_waiter) {
524 if (!task_has_pi_waiters(task))
525 goto out_unlock_pi;
526 /*
527 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000528 * are not the top pi waiter of the task. If deadlock
529 * detection is enabled we continue, but stop the
530 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000531 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000532 if (top_waiter != task_top_pi_waiter(task)) {
533 if (!detect_deadlock)
534 goto out_unlock_pi;
535 else
536 requeue = false;
537 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000538 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700539
540 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000541 * If the waiter priority is the same as the task priority
542 * then there is no further priority adjustment necessary. If
543 * deadlock detection is off, we stop the chain walk. If its
544 * enabled we continue, but stop the requeueing in the chain
545 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700546 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000547 if (waiter->prio == task->prio) {
548 if (!detect_deadlock)
549 goto out_unlock_pi;
550 else
551 requeue = false;
552 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700553
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200554 /*
555 * [4] Get the next lock
556 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700557 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200558 /*
559 * [5] We need to trylock here as we are holding task->pi_lock,
560 * which is the reverse lock order versus the other rtmutex
561 * operations.
562 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100563 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100564 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700565 cpu_relax();
566 goto retry;
567 }
568
Thomas Gleixner397335f2014-05-22 03:25:39 +0000569 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200570 * [6] check_exit_conditions_2() protected by task->pi_lock and
571 * lock->wait_lock.
572 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000573 * Deadlock detection. If the lock is the same as the original
574 * lock which caused us to walk the lock chain or if the
575 * current lock is owned by the task which initiated the chain
576 * walk, we detected a deadlock.
577 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700578 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000579 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100580 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200581 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700582 goto out_unlock_pi;
583 }
584
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000585 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000586 * If we just follow the lock chain for deadlock detection, no
587 * need to do all the requeue operations. To avoid a truckload
588 * of conditionals around the various places below, just do the
589 * minimum chain walk checks.
590 */
591 if (!requeue) {
592 /*
593 * No requeue[7] here. Just release @task [8]
594 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100595 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000596 put_task_struct(task);
597
598 /*
599 * [9] check_exit_conditions_3 protected by lock->wait_lock.
600 * If there is no owner of the lock, end of chain.
601 */
602 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100603 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000604 return 0;
605 }
606
607 /* [10] Grab the next task, i.e. owner of @lock */
608 task = rt_mutex_owner(lock);
609 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100610 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000611
612 /*
613 * No requeue [11] here. We just do deadlock detection.
614 *
615 * [12] Store whether owner is blocked
616 * itself. Decision is made after dropping the locks
617 */
618 next_lock = task_blocked_on_lock(task);
619 /*
620 * Get the top waiter for the next iteration
621 */
622 top_waiter = rt_mutex_top_waiter(lock);
623
624 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100625 raw_spin_unlock(&task->pi_lock);
626 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000627
628 /* If owner is not blocked, end of chain. */
629 if (!next_lock)
630 goto out_put_task;
631 goto again;
632 }
633
634 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000635 * Store the current top waiter before doing the requeue
636 * operation on @lock. We need it for the boost/deboost
637 * decision below.
638 */
639 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700640
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700641 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100642 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100643 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100644 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700645
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200646 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100647 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200648 put_task_struct(task);
649
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000650 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200651 * [9] check_exit_conditions_3 protected by lock->wait_lock.
652 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000653 * We must abort the chain walk if there is no lock owner even
654 * in the dead lock detection case, as we have nothing to
655 * follow here. This is the end of the chain we are walking.
656 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800657 if (!rt_mutex_owner(lock)) {
658 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200659 * If the requeue [7] above changed the top waiter,
660 * then we need to wake the new top waiter up to try
661 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800662 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000663 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800664 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100665 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200666 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800667 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700668
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200669 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700670 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700671 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100672 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700673
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200674 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700675 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000676 /*
677 * The waiter became the new top (highest priority)
678 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700679 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000680 * and adjust the priority of the owner.
681 */
682 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100683 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700684 __rt_mutex_adjust_prio(task);
685
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000686 } else if (prerequeue_top_waiter == waiter) {
687 /*
688 * The waiter was the top waiter on the lock, but is
689 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700690 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000691 * (highest priority) waiter and adjust the priority
692 * of the owner.
693 * The new top waiter is stored in @waiter so that
694 * @waiter == @top_waiter evaluates to true below and
695 * we continue to deboost the rest of the chain.
696 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100697 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700698 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100699 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700700 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000701 } else {
702 /*
703 * Nothing changed. No need to do any priority
704 * adjustment.
705 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700706 }
707
Thomas Gleixner82084982014-06-05 11:16:12 +0200708 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200709 * [12] check_exit_conditions_4() protected by task->pi_lock
710 * and lock->wait_lock. The actual decisions are made after we
711 * dropped the locks.
712 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200713 * Check whether the task which owns the current lock is pi
714 * blocked itself. If yes we store a pointer to the lock for
715 * the lock chain change detection above. After we dropped
716 * task->pi_lock next_lock cannot be dereferenced anymore.
717 */
718 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000719 /*
720 * Store the top waiter of @lock for the end of chain walk
721 * decision below.
722 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700723 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200724
725 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100726 raw_spin_unlock(&task->pi_lock);
727 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700728
Thomas Gleixner82084982014-06-05 11:16:12 +0200729 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200730 * Make the actual exit decisions [12], based on the stored
731 * values.
732 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200733 * We reached the end of the lock chain. Stop right here. No
734 * point to go back just to figure that out.
735 */
736 if (!next_lock)
737 goto out_put_task;
738
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000739 /*
740 * If the current waiter is not the top waiter on the lock,
741 * then we can stop the chain walk here if we are not in full
742 * deadlock detection mode.
743 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700744 if (!detect_deadlock && waiter != top_waiter)
745 goto out_put_task;
746
747 goto again;
748
749 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100750 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700751 out_put_task:
752 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700753
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700754 return ret;
755}
756
757/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700758 * Try to take an rt-mutex
759 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100760 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800761 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200762 * @lock: The lock to be acquired.
763 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700764 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200765 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700766 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800767static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200768 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700769{
770 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200771 * Before testing whether we can acquire @lock, we set the
772 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
773 * other tasks which try to modify @lock into the slow path
774 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700775 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200776 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
777 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700778 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200779 * - There is a lock owner. The caller must fixup the
780 * transient state if it does a trylock or leaves the lock
781 * function due to a signal or timeout.
782 *
783 * - @task acquires the lock and there are no other
784 * waiters. This is undone in rt_mutex_set_owner(@task) at
785 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700786 */
787 mark_rt_mutex_waiters(lock);
788
Thomas Gleixner358c3312014-06-11 01:01:13 +0200789 /*
790 * If @lock has an owner, give up.
791 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800792 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700793 return 0;
794
Lai Jiangshan81612392011-01-14 17:09:41 +0800795 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200796 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700797 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200798 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800799 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200800 if (waiter) {
801 /*
802 * If waiter is not the highest priority waiter of
803 * @lock, give up.
804 */
805 if (waiter != rt_mutex_top_waiter(lock))
806 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800807
808 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200809 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700810 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200811 */
812 rt_mutex_dequeue(lock, waiter);
813
814 } else {
815 /*
816 * If the lock has waiters already we check whether @task is
817 * eligible to take over the lock.
818 *
819 * If there are no other waiters, @task can acquire
820 * the lock. @task->pi_blocked_on is NULL, so it does
821 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800822 */
823 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200824 /*
825 * If @task->prio is greater than or equal to
826 * the top waiter priority (kernel view),
827 * @task lost.
828 */
829 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
830 return 0;
831
832 /*
833 * The current top waiter stays enqueued. We
834 * don't have to change anything in the lock
835 * waiters order.
836 */
837 } else {
838 /*
839 * No waiters. Take the lock without the
840 * pi_lock dance.@task->pi_blocked_on is NULL
841 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700842 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200843 */
844 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800845 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800846 }
847
Thomas Gleixner358c3312014-06-11 01:01:13 +0200848 /*
849 * Clear @task->pi_blocked_on. Requires protection by
850 * @task->pi_lock. Redundant operation for the @waiter == NULL
851 * case, but conditionals are more expensive than a redundant
852 * store.
853 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100854 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200855 task->pi_blocked_on = NULL;
856 /*
857 * Finish the lock acquisition. @task is the new owner. If
858 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700859 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200860 */
861 if (rt_mutex_has_waiters(lock))
862 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100863 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200864
865takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700866 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700867 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700868
Thomas Gleixner358c3312014-06-11 01:01:13 +0200869 /*
870 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
871 * are still waiters or clears it.
872 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800873 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700874
Lai Jiangshan81612392011-01-14 17:09:41 +0800875 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700876
877 return 1;
878}
879
880/*
881 * Task blocks on lock.
882 *
883 * Prepare waiter and propagate pi chain
884 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100885 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700886 */
887static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
888 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700889 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000890 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700891{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700892 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700893 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200894 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700895 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700896
Thomas Gleixner397335f2014-05-22 03:25:39 +0000897 /*
898 * Early deadlock detection. We really don't want the task to
899 * enqueue on itself just to untangle the mess later. It's not
900 * only an optimization. We drop the locks, so another waiter
901 * can come in before the chain walk detects the deadlock. So
902 * the other will detect the deadlock and return -EDEADLOCK,
903 * which is wrong, as the other waiter is not in a deadlock
904 * situation.
905 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200906 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000907 return -EDEADLK;
908
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100909 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700910 __rt_mutex_adjust_prio(task);
911 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700912 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100913 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700914
915 /* Get the top priority waiter on the lock */
916 if (rt_mutex_has_waiters(lock))
917 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100918 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700919
Darren Hart8dac4562009-04-03 13:40:12 -0700920 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700921
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100922 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700923
Lai Jiangshan81612392011-01-14 17:09:41 +0800924 if (!owner)
925 return 0;
926
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100927 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700928 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100929 rt_mutex_dequeue_pi(owner, top_waiter);
930 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700931
932 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700933 if (owner->pi_blocked_on)
934 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000935 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700936 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200937 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700938
Thomas Gleixner82084982014-06-05 11:16:12 +0200939 /* Store the lock on which owner is blocked or NULL */
940 next_lock = task_blocked_on_lock(owner);
941
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100942 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200943 /*
944 * Even if full deadlock detection is on, if the owner is not
945 * blocked itself, we can avoid finding this out in the chain
946 * walk.
947 */
948 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700949 return 0;
950
Steven Rostedtdb630632006-09-29 01:59:44 -0700951 /*
952 * The owner can't disappear while holding a lock,
953 * so the owner struct is protected by wait_lock.
954 * Gets dropped in rt_mutex_adjust_prio_chain()!
955 */
956 get_task_struct(owner);
957
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100958 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700959
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000960 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200961 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700962
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100963 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700964
965 return res;
966}
967
968/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700969 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700970 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700971 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100972 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700973 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700974static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
975 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700976{
977 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700978
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100979 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700980
981 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700982
983 /*
984 * Remove it from current->pi_waiters. We do not adjust a
985 * possible priority boost right now. We execute wakeup in the
986 * boosted mode and go back to normal after releasing
987 * lock->wait_lock.
988 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100989 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700990
Thomas Gleixner27e35712014-06-11 18:44:04 +0000991 /*
992 * As we are waking up the top waiter, and the waiter stays
993 * queued on the lock until it gets the lock, this lock
994 * obviously has waiters. Just set the bit here and this has
995 * the added benefit of forcing all new tasks into the
996 * slow path making sure no task of lower priority than
997 * the top waiter can steal this lock.
998 */
999 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001000
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001001 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001002
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001003 wake_q_add(wake_q, waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001004}
1005
1006/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001007 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001008 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001009 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001010 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001011 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001012static void remove_waiter(struct rt_mutex *lock,
1013 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001014{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001015 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001016 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001017 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001018
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001019 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001020 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001021 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001022 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001023
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001024 /*
1025 * Only update priority if the waiter was the highest priority
1026 * waiter of the lock and there is an owner to update.
1027 */
1028 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001029 return;
1030
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001031 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001032
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001033 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001034
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001035 if (rt_mutex_has_waiters(lock))
1036 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001037
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001038 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001039
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001040 /* Store the lock on which owner is blocked or NULL */
1041 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001042
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001043 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001044
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001045 /*
1046 * Don't walk the chain, if the owner task is not blocked
1047 * itself.
1048 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001049 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001050 return;
1051
Steven Rostedtdb630632006-09-29 01:59:44 -07001052 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1053 get_task_struct(owner);
1054
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001055 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001056
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001057 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1058 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001059
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001060 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001061}
1062
1063/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001064 * Recheck the pi chain, in case we got a priority setting
1065 *
1066 * Called from sched_setscheduler
1067 */
1068void rt_mutex_adjust_pi(struct task_struct *task)
1069{
1070 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001071 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001072 unsigned long flags;
1073
Thomas Gleixner1d615482009-11-17 14:54:03 +01001074 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001075
1076 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001077 if (!waiter || (waiter->prio == task->prio &&
1078 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001079 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001080 return;
1081 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001082 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001083 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001084
Steven Rostedtdb630632006-09-29 01:59:44 -07001085 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1086 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001087
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001088 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1089 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001090}
1091
Darren Hart8dac4562009-04-03 13:40:12 -07001092/**
1093 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1094 * @lock: the rt_mutex to take
1095 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001096 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001097 * @timeout: the pre-initialized and started timer, or NULL for none
1098 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001099 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001100 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001101 */
1102static int __sched
1103__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1104 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001105 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001106{
1107 int ret = 0;
1108
1109 for (;;) {
1110 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001111 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001112 break;
1113
1114 /*
1115 * TASK_INTERRUPTIBLE checks for signals and
1116 * timeout. Ignored otherwise.
1117 */
1118 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1119 /* Signal pending? */
1120 if (signal_pending(current))
1121 ret = -EINTR;
1122 if (timeout && !timeout->task)
1123 ret = -ETIMEDOUT;
1124 if (ret)
1125 break;
1126 }
1127
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001128 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001129
1130 debug_rt_mutex_print_deadlock(waiter);
1131
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001132 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001133
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001134 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001135 set_current_state(state);
1136 }
1137
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001138 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001139 return ret;
1140}
1141
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001142static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1143 struct rt_mutex_waiter *w)
1144{
1145 /*
1146 * If the result is not -EDEADLOCK or the caller requested
1147 * deadlock detection, nothing to do here.
1148 */
1149 if (res != -EDEADLOCK || detect_deadlock)
1150 return;
1151
1152 /*
1153 * Yell lowdly and stop the task right here.
1154 */
1155 rt_mutex_print_deadlock(w);
1156 while (1) {
1157 set_current_state(TASK_INTERRUPTIBLE);
1158 schedule();
1159 }
1160}
1161
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001162/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001163 * Slow path lock function:
1164 */
1165static int __sched
1166rt_mutex_slowlock(struct rt_mutex *lock, int state,
1167 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001168 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001169{
1170 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001171 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001172 int ret = 0;
1173
1174 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001175 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1176 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001177
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001178 /*
1179 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1180 * be called in early boot if the cmpxchg() fast path is disabled
1181 * (debug, no architecture support). In this case we will acquire the
1182 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1183 * enable interrupts in that early boot case. So we need to use the
1184 * irqsave/restore variants.
1185 */
1186 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001187
1188 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001189 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001190 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001191 return 0;
1192 }
1193
1194 set_current_state(state);
1195
1196 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001197 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001198 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001199
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001200 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001201
1202 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001203 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001204 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001205
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001206 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001207 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001208 if (rt_mutex_has_waiters(lock))
1209 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001210 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001211 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001212
1213 /*
1214 * try_to_take_rt_mutex() sets the waiter bit
1215 * unconditionally. We might have to fix that up.
1216 */
1217 fixup_rt_mutex_waiters(lock);
1218
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001219 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001220
1221 /* Remove pending timer: */
1222 if (unlikely(timeout))
1223 hrtimer_cancel(&timeout->timer);
1224
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001225 debug_rt_mutex_free_waiter(&waiter);
1226
1227 return ret;
1228}
1229
1230/*
1231 * Slow path try-lock function:
1232 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001233static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001234{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001235 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001236 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001237
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001238 /*
1239 * If the lock already has an owner we fail to get the lock.
1240 * This can be done without taking the @lock->wait_lock as
1241 * it is only being read, and this is a trylock anyway.
1242 */
1243 if (rt_mutex_owner(lock))
1244 return 0;
1245
1246 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001247 * The mutex has currently no owner. Lock the wait lock and try to
1248 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001249 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001250 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001251
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001252 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001253
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001254 /*
1255 * try_to_take_rt_mutex() sets the lock waiters bit
1256 * unconditionally. Clean this up.
1257 */
1258 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001259
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001260 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001261
1262 return ret;
1263}
1264
1265/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001266 * Slow path to release a rt-mutex.
1267 * Return whether the current task needs to undo a potential priority boosting.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001268 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001269static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1270 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001271{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001272 unsigned long flags;
1273
1274 /* irqsave required to support early boot calls */
1275 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001276
1277 debug_rt_mutex_unlock(lock);
1278
1279 rt_mutex_deadlock_account_unlock(current);
1280
Thomas Gleixner27e35712014-06-11 18:44:04 +00001281 /*
1282 * We must be careful here if the fast path is enabled. If we
1283 * have no waiters queued we cannot set owner to NULL here
1284 * because of:
1285 *
1286 * foo->lock->owner = NULL;
1287 * rtmutex_lock(foo->lock); <- fast path
1288 * free = atomic_dec_and_test(foo->refcnt);
1289 * rtmutex_unlock(foo->lock); <- fast path
1290 * if (free)
1291 * kfree(foo);
1292 * raw_spin_unlock(foo->lock->wait_lock);
1293 *
1294 * So for the fastpath enabled kernel:
1295 *
1296 * Nothing can set the waiters bit as long as we hold
1297 * lock->wait_lock. So we do the following sequence:
1298 *
1299 * owner = rt_mutex_owner(lock);
1300 * clear_rt_mutex_waiters(lock);
1301 * raw_spin_unlock(&lock->wait_lock);
1302 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1303 * return;
1304 * goto retry;
1305 *
1306 * The fastpath disabled variant is simple as all access to
1307 * lock->owner is serialized by lock->wait_lock:
1308 *
1309 * lock->owner = NULL;
1310 * raw_spin_unlock(&lock->wait_lock);
1311 */
1312 while (!rt_mutex_has_waiters(lock)) {
1313 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001314 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001315 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001316 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001317 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001318 }
1319
Thomas Gleixner27e35712014-06-11 18:44:04 +00001320 /*
1321 * The wakeup next waiter path does not suffer from the above
1322 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001323 *
1324 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001325 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001326 mark_wakeup_next_waiter(wake_q, lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001327
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001328 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001329
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001330 /* check PI boosting */
1331 return true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001332}
1333
1334/*
1335 * debug aware fast / slowpath lock,trylock,unlock
1336 *
1337 * The atomic acquire/release ops are compiled away, when either the
1338 * architecture does not support cmpxchg or when debugging is enabled.
1339 */
1340static inline int
1341rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001342 int (*slowfn)(struct rt_mutex *lock, int state,
1343 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001344 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001345{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001346 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001347 rt_mutex_deadlock_account_lock(lock, current);
1348 return 0;
1349 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001350 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001351}
1352
1353static inline int
1354rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001355 struct hrtimer_sleeper *timeout,
1356 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001357 int (*slowfn)(struct rt_mutex *lock, int state,
1358 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001359 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001360{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001361 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001362 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001363 rt_mutex_deadlock_account_lock(lock, current);
1364 return 0;
1365 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001366 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001367}
1368
1369static inline int
1370rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001371 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001372{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001373 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001374 rt_mutex_deadlock_account_lock(lock, current);
1375 return 1;
1376 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001377 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001378}
1379
1380static inline void
1381rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001382 bool (*slowfn)(struct rt_mutex *lock,
1383 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001384{
Waiman Long194a6b52016-11-17 11:46:38 -05001385 DEFINE_WAKE_Q(wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001386
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001387 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001388 rt_mutex_deadlock_account_unlock(current);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001389
1390 } else {
1391 bool deboost = slowfn(lock, &wake_q);
1392
1393 wake_up_q(&wake_q);
1394
1395 /* Undo pi boosting if necessary: */
1396 if (deboost)
1397 rt_mutex_adjust_prio(current);
1398 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001399}
1400
1401/**
1402 * rt_mutex_lock - lock a rt_mutex
1403 *
1404 * @lock: the rt_mutex to be locked
1405 */
1406void __sched rt_mutex_lock(struct rt_mutex *lock)
1407{
1408 might_sleep();
1409
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001410 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001411}
1412EXPORT_SYMBOL_GPL(rt_mutex_lock);
1413
1414/**
1415 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1416 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001417 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001418 *
1419 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001420 * 0 on success
1421 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001422 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001423int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001424{
1425 might_sleep();
1426
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001427 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001428}
1429EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1430
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001431/*
1432 * Futex variant with full deadlock detection.
1433 */
1434int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1435 struct hrtimer_sleeper *timeout)
1436{
1437 might_sleep();
1438
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001439 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1440 RT_MUTEX_FULL_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001441 rt_mutex_slowlock);
1442}
1443
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001444/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001445 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1446 * the timeout structure is provided
1447 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001448 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001449 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001450 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001451 *
1452 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001453 * 0 on success
1454 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001455 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001456 */
1457int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001458rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001459{
1460 might_sleep();
1461
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001462 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1463 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001464 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001465}
1466EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1467
1468/**
1469 * rt_mutex_trylock - try to lock a rt_mutex
1470 *
1471 * @lock: the rt_mutex to be locked
1472 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001473 * This function can only be called in thread context. It's safe to
1474 * call it from atomic regions, but not from hard interrupt or soft
1475 * interrupt context.
1476 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001477 * Returns 1 on success and 0 on contention
1478 */
1479int __sched rt_mutex_trylock(struct rt_mutex *lock)
1480{
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001481 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001482 return 0;
1483
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001484 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1485}
1486EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1487
1488/**
1489 * rt_mutex_unlock - unlock a rt_mutex
1490 *
1491 * @lock: the rt_mutex to be unlocked
1492 */
1493void __sched rt_mutex_unlock(struct rt_mutex *lock)
1494{
1495 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1496}
1497EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1498
Luis Henriques23b94b92009-04-29 21:54:51 +01001499/**
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001500 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1501 * @lock: the rt_mutex to be unlocked
1502 *
1503 * Returns: true/false indicating whether priority adjustment is
1504 * required or not.
1505 */
1506bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1507 struct wake_q_head *wqh)
1508{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001509 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001510 rt_mutex_deadlock_account_unlock(current);
1511 return false;
1512 }
1513 return rt_mutex_slowunlock(lock, wqh);
1514}
1515
1516/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001517 * rt_mutex_destroy - mark a mutex unusable
1518 * @lock: the mutex to be destroyed
1519 *
1520 * This function marks the mutex uninitialized, and any subsequent
1521 * use of the mutex is forbidden. The mutex must not be locked when
1522 * this function is called.
1523 */
1524void rt_mutex_destroy(struct rt_mutex *lock)
1525{
1526 WARN_ON(rt_mutex_is_locked(lock));
1527#ifdef CONFIG_DEBUG_RT_MUTEXES
1528 lock->magic = NULL;
1529#endif
1530}
1531
1532EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1533
1534/**
1535 * __rt_mutex_init - initialize the rt lock
1536 *
1537 * @lock: the rt lock to be initialized
1538 *
1539 * Initialize the rt lock to unlocked state.
1540 *
1541 * Initializing of a locked rt lock is not allowed
1542 */
1543void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1544{
1545 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001546 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001547 lock->waiters = RB_ROOT;
1548 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001549
1550 debug_rt_mutex_init(lock, name);
1551}
1552EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001553
1554/**
1555 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1556 * proxy owner
1557 *
1558 * @lock: the rt_mutex to be locked
1559 * @proxy_owner:the task to set as owner
1560 *
1561 * No locking. Caller has to do serializing itself
1562 * Special API call for PI-futex support
1563 */
1564void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1565 struct task_struct *proxy_owner)
1566{
1567 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001568 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001569 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001570 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1571}
1572
1573/**
1574 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1575 *
1576 * @lock: the rt_mutex to be locked
1577 *
1578 * No locking. Caller has to do serializing itself
1579 * Special API call for PI-futex support
1580 */
1581void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1582 struct task_struct *proxy_owner)
1583{
1584 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001585 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001586 rt_mutex_deadlock_account_unlock(proxy_owner);
1587}
1588
1589/**
Darren Hart8dac4562009-04-03 13:40:12 -07001590 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1591 * @lock: the rt_mutex to take
1592 * @waiter: the pre-initialized rt_mutex_waiter
1593 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001594 *
1595 * Returns:
1596 * 0 - task blocked on lock
1597 * 1 - acquired the lock for task, caller should wake it up
1598 * <0 - error
1599 *
1600 * Special API call for FUTEX_REQUEUE_PI support.
1601 */
1602int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1603 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001604 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001605{
1606 int ret;
1607
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001608 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001609
Lai Jiangshan81612392011-01-14 17:09:41 +08001610 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001611 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001612 return 1;
1613 }
1614
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001615 /* We enforce deadlock detection for futexes */
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001616 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1617 RT_MUTEX_FULL_CHAINWALK);
Darren Hart8dac4562009-04-03 13:40:12 -07001618
Lai Jiangshan81612392011-01-14 17:09:41 +08001619 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001620 /*
1621 * Reset the return value. We might have
1622 * returned with -EDEADLK and the owner
1623 * released the lock while we were walking the
1624 * pi chain. Let the waiter sort it out.
1625 */
1626 ret = 0;
1627 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001628
1629 if (unlikely(ret))
1630 remove_waiter(lock, waiter);
1631
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001632 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001633
1634 debug_rt_mutex_print_deadlock(waiter);
1635
1636 return ret;
1637}
1638
1639/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001640 * rt_mutex_next_owner - return the next owner of the lock
1641 *
1642 * @lock: the rt lock query
1643 *
1644 * Returns the next owner of the lock or NULL
1645 *
1646 * Caller has to serialize against other accessors to the lock
1647 * itself.
1648 *
1649 * Special API call for PI-futex support
1650 */
1651struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1652{
1653 if (!rt_mutex_has_waiters(lock))
1654 return NULL;
1655
1656 return rt_mutex_top_waiter(lock)->task;
1657}
Darren Hart8dac4562009-04-03 13:40:12 -07001658
1659/**
1660 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1661 * @lock: the rt_mutex we were woken on
1662 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001663 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001664 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001665 *
1666 * Complete the lock acquisition started our behalf by another thread.
1667 *
1668 * Returns:
1669 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001670 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001671 *
1672 * Special API call for PI-futex requeue support
1673 */
1674int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1675 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001676 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001677{
1678 int ret;
1679
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001680 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001681
1682 set_current_state(TASK_INTERRUPTIBLE);
1683
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001684 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001685 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001686
Lai Jiangshan81612392011-01-14 17:09:41 +08001687 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001688 remove_waiter(lock, waiter);
1689
1690 /*
1691 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1692 * have to fix that up.
1693 */
1694 fixup_rt_mutex_waiters(lock);
1695
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001696 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001697
Darren Hart8dac4562009-04-03 13:40:12 -07001698 return ret;
1699}