blob: 00b49cdbb4e0a3fe172afbff67543a1777251c67 [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 Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.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 Molnarb17b0152017-02-08 18:51:35 +010019#include <linux/sched/debug.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070020#include <linux/timer.h>
21
22#include "rtmutex_common.h"
23
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070024/*
25 * lock->owner state tracking:
26 *
Lai Jiangshan81612392011-01-14 17:09:41 +080027 * lock->owner holds the task_struct pointer of the owner. Bit 0
28 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070029 *
Lai Jiangshan81612392011-01-14 17:09:41 +080030 * owner bit0
31 * NULL 0 lock is free (fast acquire possible)
32 * NULL 1 lock is free and has waiters and the top waiter
33 * is going to take the lock*
34 * taskpointer 0 lock is held (fast release possible)
35 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070036 *
37 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080038 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070039 *
Lai Jiangshan81612392011-01-14 17:09:41 +080040 * (*) It also can be a transitional state when grabbing the lock
41 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
42 * we need to set the bit0 before looking at the lock, and the owner may be
43 * NULL in this small time, hence this can be a transitional state.
44 *
45 * (**) There is a small time when bit 0 is set but there are no
46 * waiters. This can happen when grabbing the lock in the slow path.
47 * To prevent a cmpxchg of the owner releasing the lock, we need to
48 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070049 */
50
Thomas Gleixnerbd197232007-06-17 21:11:10 +020051static void
Lai Jiangshan81612392011-01-14 17:09:41 +080052rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070053{
Lai Jiangshan81612392011-01-14 17:09:41 +080054 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070055
56 if (rt_mutex_has_waiters(lock))
57 val |= RT_MUTEX_HAS_WAITERS;
58
59 lock->owner = (struct task_struct *)val;
60}
61
62static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
63{
64 lock->owner = (struct task_struct *)
65 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
66}
67
68static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
69{
Thomas Gleixnerdbb26052016-11-30 21:04:41 +000070 unsigned long owner, *p = (unsigned long *) &lock->owner;
71
72 if (rt_mutex_has_waiters(lock))
73 return;
74
75 /*
76 * The rbtree has no waiters enqueued, now make sure that the
77 * lock->owner still has the waiters bit set, otherwise the
78 * following can happen:
79 *
80 * CPU 0 CPU 1 CPU2
81 * l->owner=T1
82 * rt_mutex_lock(l)
83 * lock(l->lock)
84 * l->owner = T1 | HAS_WAITERS;
85 * enqueue(T2)
86 * boost()
87 * unlock(l->lock)
88 * block()
89 *
90 * rt_mutex_lock(l)
91 * lock(l->lock)
92 * l->owner = T1 | HAS_WAITERS;
93 * enqueue(T3)
94 * boost()
95 * unlock(l->lock)
96 * block()
97 * signal(->T2) signal(->T3)
98 * lock(l->lock)
99 * dequeue(T2)
100 * deboost()
101 * unlock(l->lock)
102 * lock(l->lock)
103 * dequeue(T3)
104 * ==> wait list is empty
105 * deboost()
106 * unlock(l->lock)
107 * lock(l->lock)
108 * fixup_rt_mutex_waiters()
109 * if (wait_list_empty(l) {
110 * l->owner = owner
111 * owner = l->owner & ~HAS_WAITERS;
112 * ==> l->owner = T1
113 * }
114 * lock(l->lock)
115 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
116 * if (wait_list_empty(l) {
117 * owner = l->owner & ~HAS_WAITERS;
118 * cmpxchg(l->owner, T1, NULL)
119 * ===> Success (l->owner = NULL)
120 *
121 * l->owner = owner
122 * ==> l->owner = T1
123 * }
124 *
125 * With the check for the waiter bit in place T3 on CPU2 will not
126 * overwrite. All tasks fiddling with the waiters bit are
127 * serialized by l->lock, so nothing else can modify the waiters
128 * bit. If the bit is set then nothing can change l->owner either
129 * so the simple RMW is safe. The cmpxchg() will simply fail if it
130 * happens in the middle of the RMW because the waiters bit is
131 * still set.
132 */
133 owner = READ_ONCE(*p);
134 if (owner & RT_MUTEX_HAS_WAITERS)
135 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700136}
137
138/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100139 * We can speed up the acquire/release, if there's no debugging state to be
140 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200141 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100142#ifndef CONFIG_DEBUG_RT_MUTEXES
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700143# define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
144# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
145# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
146
147/*
148 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
149 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
150 * relaxed semantics suffice.
151 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200152static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
153{
154 unsigned long owner, *p = (unsigned long *) &lock->owner;
155
156 do {
157 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700158 } while (cmpxchg_relaxed(p, owner,
159 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200160}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000161
162/*
163 * Safe fastpath aware unlock:
164 * 1) Clear the waiters bit
165 * 2) Drop lock->wait_lock
166 * 3) Try to unlock the lock with cmpxchg
167 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100168static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
169 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000170 __releases(lock->wait_lock)
171{
172 struct task_struct *owner = rt_mutex_owner(lock);
173
174 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100175 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000176 /*
177 * If a new waiter comes in between the unlock and the cmpxchg
178 * we have two situations:
179 *
180 * unlock(wait_lock);
181 * lock(wait_lock);
182 * cmpxchg(p, owner, 0) == owner
183 * mark_rt_mutex_waiters(lock);
184 * acquire(lock);
185 * or:
186 *
187 * unlock(wait_lock);
188 * lock(wait_lock);
189 * mark_rt_mutex_waiters(lock);
190 *
191 * cmpxchg(p, owner, 0) != owner
192 * enqueue_waiter();
193 * unlock(wait_lock);
194 * lock(wait_lock);
195 * wake waiter();
196 * unlock(wait_lock);
197 * lock(wait_lock);
198 * acquire(lock);
199 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700200 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000201}
202
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200203#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700204# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
205# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
206# define rt_mutex_cmpxchg_release(l,c,n) (0)
207
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200208static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
209{
210 lock->owner = (struct task_struct *)
211 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
212}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000213
214/*
215 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
216 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100217static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
218 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000219 __releases(lock->wait_lock)
220{
221 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100222 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000223 return true;
224}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200225#endif
226
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100227static inline int
228rt_mutex_waiter_less(struct rt_mutex_waiter *left,
229 struct rt_mutex_waiter *right)
230{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100231 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100232 return 1;
233
234 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100235 * If both waiters have dl_prio(), we check the deadlines of the
236 * associated tasks.
237 * If left waiter has a dl_prio(), and we didn't return 1 above,
238 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100239 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100240 if (dl_prio(left->prio))
Juri Lellif5240572015-09-02 11:01:35 +0100241 return dl_time_before(left->task->dl.deadline,
242 right->task->dl.deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100243
244 return 0;
245}
246
247static void
248rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
249{
250 struct rb_node **link = &lock->waiters.rb_node;
251 struct rb_node *parent = NULL;
252 struct rt_mutex_waiter *entry;
253 int leftmost = 1;
254
255 while (*link) {
256 parent = *link;
257 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
258 if (rt_mutex_waiter_less(waiter, entry)) {
259 link = &parent->rb_left;
260 } else {
261 link = &parent->rb_right;
262 leftmost = 0;
263 }
264 }
265
266 if (leftmost)
267 lock->waiters_leftmost = &waiter->tree_entry;
268
269 rb_link_node(&waiter->tree_entry, parent, link);
270 rb_insert_color(&waiter->tree_entry, &lock->waiters);
271}
272
273static void
274rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
275{
276 if (RB_EMPTY_NODE(&waiter->tree_entry))
277 return;
278
279 if (lock->waiters_leftmost == &waiter->tree_entry)
280 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
281
282 rb_erase(&waiter->tree_entry, &lock->waiters);
283 RB_CLEAR_NODE(&waiter->tree_entry);
284}
285
286static void
287rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
288{
289 struct rb_node **link = &task->pi_waiters.rb_node;
290 struct rb_node *parent = NULL;
291 struct rt_mutex_waiter *entry;
292 int leftmost = 1;
293
294 while (*link) {
295 parent = *link;
296 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
297 if (rt_mutex_waiter_less(waiter, entry)) {
298 link = &parent->rb_left;
299 } else {
300 link = &parent->rb_right;
301 leftmost = 0;
302 }
303 }
304
305 if (leftmost)
306 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
307
308 rb_link_node(&waiter->pi_tree_entry, parent, link);
309 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
310}
311
312static void
313rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
314{
315 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
316 return;
317
318 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
319 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
320
321 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
322 RB_CLEAR_NODE(&waiter->pi_tree_entry);
323}
324
Peter Zijlstraacd58622017-03-23 15:56:11 +0100325static void rt_mutex_adjust_prio(struct task_struct *p)
Xunlei Pange96a77052017-03-23 15:56:08 +0100326{
Peter Zijlstraacd58622017-03-23 15:56:11 +0100327 struct task_struct *pi_task = NULL;
Xunlei Pange96a77052017-03-23 15:56:08 +0100328
Peter Zijlstraacd58622017-03-23 15:56:11 +0100329 lockdep_assert_held(&p->pi_lock);
Xunlei Pange96a77052017-03-23 15:56:08 +0100330
Peter Zijlstraacd58622017-03-23 15:56:11 +0100331 if (task_has_pi_waiters(p))
332 pi_task = task_top_pi_waiter(p)->task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700333
Peter Zijlstraacd58622017-03-23 15:56:11 +0100334 rt_mutex_setprio(p, pi_task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700335}
336
337/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000338 * Deadlock detection is conditional:
339 *
340 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
341 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
342 *
343 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
344 * conducted independent of the detect argument.
345 *
346 * If the waiter argument is NULL this indicates the deboost path and
347 * deadlock detection is disabled independent of the detect argument
348 * and the config settings.
349 */
350static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
351 enum rtmutex_chainwalk chwalk)
352{
353 /*
354 * This is just a wrapper function for the following call,
355 * because debug_rt_mutex_detect_deadlock() smells like a magic
356 * debug feature and I wanted to keep the cond function in the
357 * main source file along with the comments instead of having
358 * two of the same in the headers.
359 */
360 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
361}
362
363/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700364 * Max number of times we'll walk the boosting chain:
365 */
366int max_lock_depth = 1024;
367
Thomas Gleixner82084982014-06-05 11:16:12 +0200368static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
369{
370 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
371}
372
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700373/*
374 * Adjust the priority chain. Also used for deadlock detection.
375 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200376 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200377 * @task: the task owning the mutex (owner) for which a chain walk is
378 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900379 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200380 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
381 * things for a task that has just got its priority adjusted, and
382 * is waiting on a mutex)
383 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
384 * we dropped its pi_lock. Is never dereferenced, only used for
385 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200386 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200387 * its priority to the mutex owner (can be NULL in the case
388 * depicted above or if the top waiter is gone away and we are
389 * actually deboosting the owner)
390 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200391 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700392 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200393 *
394 * Chain walk basics and protection scope
395 *
396 * [R] refcount on task
397 * [P] task->pi_lock held
398 * [L] rtmutex->wait_lock held
399 *
400 * Step Description Protected by
401 * function arguments:
402 * @task [R]
403 * @orig_lock if != NULL @top_task is blocked on it
404 * @next_lock Unprotected. Cannot be
405 * dereferenced. Only used for
406 * comparison.
407 * @orig_waiter if != NULL @top_task is blocked on it
408 * @top_task current, or in case of proxy
409 * locking protected by calling
410 * code
411 * again:
412 * loop_sanity_check();
413 * retry:
414 * [1] lock(task->pi_lock); [R] acquire [P]
415 * [2] waiter = task->pi_blocked_on; [P]
416 * [3] check_exit_conditions_1(); [P]
417 * [4] lock = waiter->lock; [P]
418 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
419 * unlock(task->pi_lock); release [P]
420 * goto retry;
421 * }
422 * [6] check_exit_conditions_2(); [P] + [L]
423 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
424 * [8] unlock(task->pi_lock); release [P]
425 * put_task_struct(task); release [R]
426 * [9] check_exit_conditions_3(); [L]
427 * [10] task = owner(lock); [L]
428 * get_task_struct(task); [L] acquire [R]
429 * lock(task->pi_lock); [L] acquire [P]
430 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
431 * [12] check_exit_conditions_4(); [P] + [L]
432 * [13] unlock(task->pi_lock); release [P]
433 * unlock(lock->wait_lock); release [L]
434 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700435 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200436static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000437 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200438 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200439 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200440 struct rt_mutex_waiter *orig_waiter,
441 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700442{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700443 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000444 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000445 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000446 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000447 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000448 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700449
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000450 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700451
452 /*
453 * The (de)boosting is a step by step approach with a lot of
454 * pitfalls. We want this to be preemptible and we want hold a
455 * maximum of two locks per step. So we have to check
456 * carefully whether things change under us.
457 */
458 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200459 /*
460 * We limit the lock chain length for each invocation.
461 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700462 if (++depth > max_lock_depth) {
463 static int prev_max;
464
465 /*
466 * Print this only once. If the admin changes the limit,
467 * print a new message when reaching the limit again.
468 */
469 if (prev_max != max_lock_depth) {
470 prev_max = max_lock_depth;
471 printk(KERN_WARNING "Maximum lock depth %d reached "
472 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700473 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700474 }
475 put_task_struct(task);
476
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200477 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700478 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200479
480 /*
481 * We are fully preemptible here and only hold the refcount on
482 * @task. So everything can have changed under us since the
483 * caller or our own code below (goto retry/again) dropped all
484 * locks.
485 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700486 retry:
487 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200488 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700489 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100490 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700491
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200492 /*
493 * [2] Get the waiter on which @task is blocked on.
494 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700495 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200496
497 /*
498 * [3] check_exit_conditions_1() protected by task->pi_lock.
499 */
500
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700501 /*
502 * Check whether the end of the boosting chain has been
503 * reached or the state of the chain has changed while we
504 * dropped the locks.
505 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800506 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700507 goto out_unlock_pi;
508
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700509 /*
510 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800511 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700512 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800513 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700514 goto out_unlock_pi;
515
516 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200517 * We dropped all locks after taking a refcount on @task, so
518 * the task might have moved on in the lock chain or even left
519 * the chain completely and blocks now on an unrelated lock or
520 * on @orig_lock.
521 *
522 * We stored the lock on which @task was blocked in @next_lock,
523 * so we can detect the chain change.
524 */
525 if (next_lock != waiter->lock)
526 goto out_unlock_pi;
527
528 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700529 * Drop out, when the task has no waiters. Note,
530 * top_waiter can be NULL, when we are in the deboosting
531 * mode!
532 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000533 if (top_waiter) {
534 if (!task_has_pi_waiters(task))
535 goto out_unlock_pi;
536 /*
537 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000538 * are not the top pi waiter of the task. If deadlock
539 * detection is enabled we continue, but stop the
540 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000541 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000542 if (top_waiter != task_top_pi_waiter(task)) {
543 if (!detect_deadlock)
544 goto out_unlock_pi;
545 else
546 requeue = false;
547 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000548 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700549
550 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000551 * If the waiter priority is the same as the task priority
552 * then there is no further priority adjustment necessary. If
553 * deadlock detection is off, we stop the chain walk. If its
554 * enabled we continue, but stop the requeueing in the chain
555 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700556 */
Xunlei Pang85e2d4f2017-03-23 15:56:09 +0100557 if (waiter->prio == task->prio && !dl_task(task)) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000558 if (!detect_deadlock)
559 goto out_unlock_pi;
560 else
561 requeue = false;
562 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700563
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200564 /*
565 * [4] Get the next lock
566 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700567 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200568 /*
569 * [5] We need to trylock here as we are holding task->pi_lock,
570 * which is the reverse lock order versus the other rtmutex
571 * operations.
572 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100573 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100574 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700575 cpu_relax();
576 goto retry;
577 }
578
Thomas Gleixner397335f2014-05-22 03:25:39 +0000579 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200580 * [6] check_exit_conditions_2() protected by task->pi_lock and
581 * lock->wait_lock.
582 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000583 * Deadlock detection. If the lock is the same as the original
584 * lock which caused us to walk the lock chain or if the
585 * current lock is owned by the task which initiated the chain
586 * walk, we detected a deadlock.
587 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700588 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000589 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100590 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200591 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700592 goto out_unlock_pi;
593 }
594
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000595 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000596 * If we just follow the lock chain for deadlock detection, no
597 * need to do all the requeue operations. To avoid a truckload
598 * of conditionals around the various places below, just do the
599 * minimum chain walk checks.
600 */
601 if (!requeue) {
602 /*
603 * No requeue[7] here. Just release @task [8]
604 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100605 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000606 put_task_struct(task);
607
608 /*
609 * [9] check_exit_conditions_3 protected by lock->wait_lock.
610 * If there is no owner of the lock, end of chain.
611 */
612 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100613 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000614 return 0;
615 }
616
617 /* [10] Grab the next task, i.e. owner of @lock */
618 task = rt_mutex_owner(lock);
619 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100620 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000621
622 /*
623 * No requeue [11] here. We just do deadlock detection.
624 *
625 * [12] Store whether owner is blocked
626 * itself. Decision is made after dropping the locks
627 */
628 next_lock = task_blocked_on_lock(task);
629 /*
630 * Get the top waiter for the next iteration
631 */
632 top_waiter = rt_mutex_top_waiter(lock);
633
634 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100635 raw_spin_unlock(&task->pi_lock);
636 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000637
638 /* If owner is not blocked, end of chain. */
639 if (!next_lock)
640 goto out_put_task;
641 goto again;
642 }
643
644 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000645 * Store the current top waiter before doing the requeue
646 * operation on @lock. We need it for the boost/deboost
647 * decision below.
648 */
649 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700650
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700651 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100652 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100653 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100654 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700655
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200656 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100657 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200658 put_task_struct(task);
659
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000660 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200661 * [9] check_exit_conditions_3 protected by lock->wait_lock.
662 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000663 * We must abort the chain walk if there is no lock owner even
664 * in the dead lock detection case, as we have nothing to
665 * follow here. This is the end of the chain we are walking.
666 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800667 if (!rt_mutex_owner(lock)) {
668 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200669 * If the requeue [7] above changed the top waiter,
670 * then we need to wake the new top waiter up to try
671 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800672 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000673 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800674 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100675 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200676 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800677 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700678
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200679 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700680 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700681 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100682 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700683
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200684 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700685 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000686 /*
687 * The waiter became the new top (highest priority)
688 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700689 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000690 * and adjust the priority of the owner.
691 */
692 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100693 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100694 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700695
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000696 } else if (prerequeue_top_waiter == waiter) {
697 /*
698 * The waiter was the top waiter on the lock, but is
699 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700700 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000701 * (highest priority) waiter and adjust the priority
702 * of the owner.
703 * The new top waiter is stored in @waiter so that
704 * @waiter == @top_waiter evaluates to true below and
705 * we continue to deboost the rest of the chain.
706 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100707 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700708 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100709 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100710 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000711 } else {
712 /*
713 * Nothing changed. No need to do any priority
714 * adjustment.
715 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700716 }
717
Thomas Gleixner82084982014-06-05 11:16:12 +0200718 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200719 * [12] check_exit_conditions_4() protected by task->pi_lock
720 * and lock->wait_lock. The actual decisions are made after we
721 * dropped the locks.
722 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200723 * Check whether the task which owns the current lock is pi
724 * blocked itself. If yes we store a pointer to the lock for
725 * the lock chain change detection above. After we dropped
726 * task->pi_lock next_lock cannot be dereferenced anymore.
727 */
728 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000729 /*
730 * Store the top waiter of @lock for the end of chain walk
731 * decision below.
732 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700733 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200734
735 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100736 raw_spin_unlock(&task->pi_lock);
737 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700738
Thomas Gleixner82084982014-06-05 11:16:12 +0200739 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200740 * Make the actual exit decisions [12], based on the stored
741 * values.
742 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200743 * We reached the end of the lock chain. Stop right here. No
744 * point to go back just to figure that out.
745 */
746 if (!next_lock)
747 goto out_put_task;
748
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000749 /*
750 * If the current waiter is not the top waiter on the lock,
751 * then we can stop the chain walk here if we are not in full
752 * deadlock detection mode.
753 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700754 if (!detect_deadlock && waiter != top_waiter)
755 goto out_put_task;
756
757 goto again;
758
759 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100760 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700761 out_put_task:
762 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700763
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700764 return ret;
765}
766
767/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700768 * Try to take an rt-mutex
769 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100770 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800771 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200772 * @lock: The lock to be acquired.
773 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700774 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200775 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700776 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800777static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200778 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700779{
780 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200781 * Before testing whether we can acquire @lock, we set the
782 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
783 * other tasks which try to modify @lock into the slow path
784 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700785 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200786 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
787 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700788 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200789 * - There is a lock owner. The caller must fixup the
790 * transient state if it does a trylock or leaves the lock
791 * function due to a signal or timeout.
792 *
793 * - @task acquires the lock and there are no other
794 * waiters. This is undone in rt_mutex_set_owner(@task) at
795 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700796 */
797 mark_rt_mutex_waiters(lock);
798
Thomas Gleixner358c3312014-06-11 01:01:13 +0200799 /*
800 * If @lock has an owner, give up.
801 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800802 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700803 return 0;
804
Lai Jiangshan81612392011-01-14 17:09:41 +0800805 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200806 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700807 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200808 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800809 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200810 if (waiter) {
811 /*
812 * If waiter is not the highest priority waiter of
813 * @lock, give up.
814 */
815 if (waiter != rt_mutex_top_waiter(lock))
816 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800817
818 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200819 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700820 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200821 */
822 rt_mutex_dequeue(lock, waiter);
823
824 } else {
825 /*
826 * If the lock has waiters already we check whether @task is
827 * eligible to take over the lock.
828 *
829 * If there are no other waiters, @task can acquire
830 * the lock. @task->pi_blocked_on is NULL, so it does
831 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800832 */
833 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200834 /*
835 * If @task->prio is greater than or equal to
836 * the top waiter priority (kernel view),
837 * @task lost.
838 */
839 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
840 return 0;
841
842 /*
843 * The current top waiter stays enqueued. We
844 * don't have to change anything in the lock
845 * waiters order.
846 */
847 } else {
848 /*
849 * No waiters. Take the lock without the
850 * pi_lock dance.@task->pi_blocked_on is NULL
851 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700852 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200853 */
854 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800855 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800856 }
857
Thomas Gleixner358c3312014-06-11 01:01:13 +0200858 /*
859 * Clear @task->pi_blocked_on. Requires protection by
860 * @task->pi_lock. Redundant operation for the @waiter == NULL
861 * case, but conditionals are more expensive than a redundant
862 * store.
863 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100864 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200865 task->pi_blocked_on = NULL;
866 /*
867 * Finish the lock acquisition. @task is the new owner. If
868 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700869 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200870 */
871 if (rt_mutex_has_waiters(lock))
872 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100873 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200874
875takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700876 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700877 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700878
Thomas Gleixner358c3312014-06-11 01:01:13 +0200879 /*
880 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
881 * are still waiters or clears it.
882 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800883 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700884
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700885 return 1;
886}
887
888/*
889 * Task blocks on lock.
890 *
891 * Prepare waiter and propagate pi chain
892 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100893 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700894 */
895static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
896 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700897 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000898 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700899{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700900 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700901 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200902 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700903 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700904
Thomas Gleixner397335f2014-05-22 03:25:39 +0000905 /*
906 * Early deadlock detection. We really don't want the task to
907 * enqueue on itself just to untangle the mess later. It's not
908 * only an optimization. We drop the locks, so another waiter
909 * can come in before the chain walk detects the deadlock. So
910 * the other will detect the deadlock and return -EDEADLOCK,
911 * which is wrong, as the other waiter is not in a deadlock
912 * situation.
913 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200914 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000915 return -EDEADLK;
916
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100917 raw_spin_lock(&task->pi_lock);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100918 rt_mutex_adjust_prio(task);
Darren Hart8dac4562009-04-03 13:40:12 -0700919 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700920 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100921 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700922
923 /* Get the top priority waiter on the lock */
924 if (rt_mutex_has_waiters(lock))
925 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100926 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700927
Darren Hart8dac4562009-04-03 13:40:12 -0700928 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700929
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100930 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700931
Lai Jiangshan81612392011-01-14 17:09:41 +0800932 if (!owner)
933 return 0;
934
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100935 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700936 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100937 rt_mutex_dequeue_pi(owner, top_waiter);
938 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700939
Peter Zijlstraacd58622017-03-23 15:56:11 +0100940 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700941 if (owner->pi_blocked_on)
942 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000943 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700944 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200945 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700946
Thomas Gleixner82084982014-06-05 11:16:12 +0200947 /* Store the lock on which owner is blocked or NULL */
948 next_lock = task_blocked_on_lock(owner);
949
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100950 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200951 /*
952 * Even if full deadlock detection is on, if the owner is not
953 * blocked itself, we can avoid finding this out in the chain
954 * walk.
955 */
956 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700957 return 0;
958
Steven Rostedtdb630632006-09-29 01:59:44 -0700959 /*
960 * The owner can't disappear while holding a lock,
961 * so the owner struct is protected by wait_lock.
962 * Gets dropped in rt_mutex_adjust_prio_chain()!
963 */
964 get_task_struct(owner);
965
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100966 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700967
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000968 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200969 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700970
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100971 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700972
973 return res;
974}
975
976/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700977 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700978 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700979 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100980 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700981 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -0700982static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
983 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700984{
985 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700986
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100987 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700988
989 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700990
991 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +0100992 * Remove it from current->pi_waiters and deboost.
993 *
994 * We must in fact deboost here in order to ensure we call
995 * rt_mutex_setprio() to update p->pi_top_task before the
996 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700997 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100998 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100999 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001000
Thomas Gleixner27e35712014-06-11 18:44:04 +00001001 /*
1002 * As we are waking up the top waiter, and the waiter stays
1003 * queued on the lock until it gets the lock, this lock
1004 * obviously has waiters. Just set the bit here and this has
1005 * the added benefit of forcing all new tasks into the
1006 * slow path making sure no task of lower priority than
1007 * the top waiter can steal this lock.
1008 */
1009 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001010
Peter Zijlstraacd58622017-03-23 15:56:11 +01001011 /*
1012 * We deboosted before waking the top waiter task such that we don't
1013 * run two tasks with the 'same' priority (and ensure the
1014 * p->pi_top_task pointer points to a blocked task). This however can
1015 * lead to priority inversion if we would get preempted after the
1016 * deboost but before waking our donor task, hence the preempt_disable()
1017 * before unlock.
1018 *
1019 * Pairs with preempt_enable() in rt_mutex_postunlock();
1020 */
1021 preempt_disable();
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001022 wake_q_add(wake_q, waiter->task);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001023 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001024}
1025
1026/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001027 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001028 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001029 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001030 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001031 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001032static void remove_waiter(struct rt_mutex *lock,
1033 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001034{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001035 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001036 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001037 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001038
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001039 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001040 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001041 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001042 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001043
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001044 /*
1045 * Only update priority if the waiter was the highest priority
1046 * waiter of the lock and there is an owner to update.
1047 */
1048 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001049 return;
1050
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001051 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001052
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001053 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001054
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001055 if (rt_mutex_has_waiters(lock))
1056 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001057
Peter Zijlstraacd58622017-03-23 15:56:11 +01001058 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001059
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001060 /* Store the lock on which owner is blocked or NULL */
1061 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001062
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001063 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001064
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001065 /*
1066 * Don't walk the chain, if the owner task is not blocked
1067 * itself.
1068 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001069 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001070 return;
1071
Steven Rostedtdb630632006-09-29 01:59:44 -07001072 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1073 get_task_struct(owner);
1074
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001075 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001076
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001077 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1078 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001079
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001080 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001081}
1082
1083/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001084 * Recheck the pi chain, in case we got a priority setting
1085 *
1086 * Called from sched_setscheduler
1087 */
1088void rt_mutex_adjust_pi(struct task_struct *task)
1089{
1090 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001091 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001092 unsigned long flags;
1093
Thomas Gleixner1d615482009-11-17 14:54:03 +01001094 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001095
1096 waiter = task->pi_blocked_on;
Peter Zijlstraacd58622017-03-23 15:56:11 +01001097 if (!waiter || (waiter->prio == task->prio && !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001098 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001099 return;
1100 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001101 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001102 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001103
Steven Rostedtdb630632006-09-29 01:59:44 -07001104 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1105 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001106
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001107 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1108 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001109}
1110
Peter Zijlstra50809352017-03-22 11:35:56 +01001111void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1112{
1113 debug_rt_mutex_init_waiter(waiter);
1114 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1115 RB_CLEAR_NODE(&waiter->tree_entry);
1116 waiter->task = NULL;
1117}
1118
Darren Hart8dac4562009-04-03 13:40:12 -07001119/**
1120 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1121 * @lock: the rt_mutex to take
1122 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001123 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001124 * @timeout: the pre-initialized and started timer, or NULL for none
1125 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001126 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001127 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001128 */
1129static int __sched
1130__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1131 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001132 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001133{
1134 int ret = 0;
1135
1136 for (;;) {
1137 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001138 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001139 break;
1140
1141 /*
1142 * TASK_INTERRUPTIBLE checks for signals and
1143 * timeout. Ignored otherwise.
1144 */
Steven Rostedt (VMware)4009f4b2017-01-19 11:32:34 -05001145 if (likely(state == TASK_INTERRUPTIBLE)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001146 /* Signal pending? */
1147 if (signal_pending(current))
1148 ret = -EINTR;
1149 if (timeout && !timeout->task)
1150 ret = -ETIMEDOUT;
1151 if (ret)
1152 break;
1153 }
1154
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001155 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001156
1157 debug_rt_mutex_print_deadlock(waiter);
1158
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001159 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001160
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001161 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001162 set_current_state(state);
1163 }
1164
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001165 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001166 return ret;
1167}
1168
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001169static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1170 struct rt_mutex_waiter *w)
1171{
1172 /*
1173 * If the result is not -EDEADLOCK or the caller requested
1174 * deadlock detection, nothing to do here.
1175 */
1176 if (res != -EDEADLOCK || detect_deadlock)
1177 return;
1178
1179 /*
1180 * Yell lowdly and stop the task right here.
1181 */
1182 rt_mutex_print_deadlock(w);
1183 while (1) {
1184 set_current_state(TASK_INTERRUPTIBLE);
1185 schedule();
1186 }
1187}
1188
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001189/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001190 * Slow path lock function:
1191 */
1192static int __sched
1193rt_mutex_slowlock(struct rt_mutex *lock, int state,
1194 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001195 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001196{
1197 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001198 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001199 int ret = 0;
1200
Peter Zijlstra50809352017-03-22 11:35:56 +01001201 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001202
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001203 /*
1204 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1205 * be called in early boot if the cmpxchg() fast path is disabled
1206 * (debug, no architecture support). In this case we will acquire the
1207 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1208 * enable interrupts in that early boot case. So we need to use the
1209 * irqsave/restore variants.
1210 */
1211 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001212
1213 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001214 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001215 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001216 return 0;
1217 }
1218
1219 set_current_state(state);
1220
1221 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001222 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001223 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001224
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001225 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001226
1227 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001228 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001229 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001230
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001231 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001232 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001233 if (rt_mutex_has_waiters(lock))
1234 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001235 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001236 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001237
1238 /*
1239 * try_to_take_rt_mutex() sets the waiter bit
1240 * unconditionally. We might have to fix that up.
1241 */
1242 fixup_rt_mutex_waiters(lock);
1243
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001244 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001245
1246 /* Remove pending timer: */
1247 if (unlikely(timeout))
1248 hrtimer_cancel(&timeout->timer);
1249
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001250 debug_rt_mutex_free_waiter(&waiter);
1251
1252 return ret;
1253}
1254
1255/*
1256 * Slow path try-lock function:
1257 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001258static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001259{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001260 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001261 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001262
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001263 /*
1264 * If the lock already has an owner we fail to get the lock.
1265 * This can be done without taking the @lock->wait_lock as
1266 * it is only being read, and this is a trylock anyway.
1267 */
1268 if (rt_mutex_owner(lock))
1269 return 0;
1270
1271 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001272 * The mutex has currently no owner. Lock the wait lock and try to
1273 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001274 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001275 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001276
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001277 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001278
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001279 /*
1280 * try_to_take_rt_mutex() sets the lock waiters bit
1281 * unconditionally. Clean this up.
1282 */
1283 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001284
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001285 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001286
1287 return ret;
1288}
1289
1290/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001291 * Slow path to release a rt-mutex.
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001292 *
1293 * Return whether the current task needs to call rt_mutex_postunlock().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001294 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001295static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1296 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001297{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001298 unsigned long flags;
1299
1300 /* irqsave required to support early boot calls */
1301 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001302
1303 debug_rt_mutex_unlock(lock);
1304
Thomas Gleixner27e35712014-06-11 18:44:04 +00001305 /*
1306 * We must be careful here if the fast path is enabled. If we
1307 * have no waiters queued we cannot set owner to NULL here
1308 * because of:
1309 *
1310 * foo->lock->owner = NULL;
1311 * rtmutex_lock(foo->lock); <- fast path
1312 * free = atomic_dec_and_test(foo->refcnt);
1313 * rtmutex_unlock(foo->lock); <- fast path
1314 * if (free)
1315 * kfree(foo);
1316 * raw_spin_unlock(foo->lock->wait_lock);
1317 *
1318 * So for the fastpath enabled kernel:
1319 *
1320 * Nothing can set the waiters bit as long as we hold
1321 * lock->wait_lock. So we do the following sequence:
1322 *
1323 * owner = rt_mutex_owner(lock);
1324 * clear_rt_mutex_waiters(lock);
1325 * raw_spin_unlock(&lock->wait_lock);
1326 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1327 * return;
1328 * goto retry;
1329 *
1330 * The fastpath disabled variant is simple as all access to
1331 * lock->owner is serialized by lock->wait_lock:
1332 *
1333 * lock->owner = NULL;
1334 * raw_spin_unlock(&lock->wait_lock);
1335 */
1336 while (!rt_mutex_has_waiters(lock)) {
1337 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001338 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001339 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001340 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001341 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001342 }
1343
Thomas Gleixner27e35712014-06-11 18:44:04 +00001344 /*
1345 * The wakeup next waiter path does not suffer from the above
1346 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001347 *
1348 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001349 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001350 mark_wakeup_next_waiter(wake_q, lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001351 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001352
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001353 return true; /* call rt_mutex_postunlock() */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001354}
1355
1356/*
1357 * debug aware fast / slowpath lock,trylock,unlock
1358 *
1359 * The atomic acquire/release ops are compiled away, when either the
1360 * architecture does not support cmpxchg or when debugging is enabled.
1361 */
1362static inline int
1363rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001364 int (*slowfn)(struct rt_mutex *lock, int state,
1365 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001366 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001367{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001368 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001369 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001370
1371 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001372}
1373
1374static inline int
1375rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001376 struct hrtimer_sleeper *timeout,
1377 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001378 int (*slowfn)(struct rt_mutex *lock, int state,
1379 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001380 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001381{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001382 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001383 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001384 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001385
1386 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001387}
1388
1389static inline int
1390rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001391 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001392{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001393 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001394 return 1;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001395
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001396 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001397}
1398
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001399/*
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001400 * Performs the wakeup of the the top-waiter and re-enables preemption.
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001401 */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001402void rt_mutex_postunlock(struct wake_q_head *wake_q)
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001403{
1404 wake_up_q(wake_q);
1405
1406 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001407 preempt_enable();
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001408}
1409
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001410static inline void
1411rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001412 bool (*slowfn)(struct rt_mutex *lock,
1413 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001414{
Waiman Long194a6b52016-11-17 11:46:38 -05001415 DEFINE_WAKE_Q(wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001416
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001417 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1418 return;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001419
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001420 if (slowfn(lock, &wake_q))
1421 rt_mutex_postunlock(&wake_q);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001422}
1423
1424/**
1425 * rt_mutex_lock - lock a rt_mutex
1426 *
1427 * @lock: the rt_mutex to be locked
1428 */
1429void __sched rt_mutex_lock(struct rt_mutex *lock)
1430{
1431 might_sleep();
1432
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001433 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001434}
1435EXPORT_SYMBOL_GPL(rt_mutex_lock);
1436
1437/**
1438 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1439 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001440 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001441 *
1442 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001443 * 0 on success
1444 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001445 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001446int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001447{
1448 might_sleep();
1449
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001450 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001451}
1452EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1453
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001454/*
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001455 * Futex variant, must not use fastpath.
1456 */
1457int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1458{
1459 return rt_mutex_slowtrylock(lock);
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001460}
1461
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001462/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001463 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1464 * the timeout structure is provided
1465 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001466 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001467 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001468 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001469 *
1470 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001471 * 0 on success
1472 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001473 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001474 */
1475int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001476rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001477{
1478 might_sleep();
1479
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001480 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1481 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001482 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001483}
1484EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1485
1486/**
1487 * rt_mutex_trylock - try to lock a rt_mutex
1488 *
1489 * @lock: the rt_mutex to be locked
1490 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001491 * This function can only be called in thread context. It's safe to
1492 * call it from atomic regions, but not from hard interrupt or soft
1493 * interrupt context.
1494 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001495 * Returns 1 on success and 0 on contention
1496 */
1497int __sched rt_mutex_trylock(struct rt_mutex *lock)
1498{
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001499 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001500 return 0;
1501
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001502 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1503}
1504EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1505
1506/**
1507 * rt_mutex_unlock - unlock a rt_mutex
1508 *
1509 * @lock: the rt_mutex to be unlocked
1510 */
1511void __sched rt_mutex_unlock(struct rt_mutex *lock)
1512{
1513 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1514}
1515EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1516
Luis Henriques23b94b92009-04-29 21:54:51 +01001517/**
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001518 * Futex variant, that since futex variants do not use the fast-path, can be
1519 * simple and will not need to retry.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001520 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001521bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1522 struct wake_q_head *wake_q)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001523{
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001524 lockdep_assert_held(&lock->wait_lock);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001525
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001526 debug_rt_mutex_unlock(lock);
1527
1528 if (!rt_mutex_has_waiters(lock)) {
1529 lock->owner = NULL;
1530 return false; /* done */
1531 }
1532
1533 mark_wakeup_next_waiter(wake_q, lock);
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001534 /*
1535 * We've already deboosted, retain preempt_disabled when dropping
1536 * the wait_lock to avoid inversion until the wakeup. Matched
1537 * by rt_mutex_postunlock();
1538 */
1539 preempt_disable();
1540
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001541 return true; /* call postunlock() */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001542}
1543
1544void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1545{
1546 DEFINE_WAKE_Q(wake_q);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001547 bool postunlock;
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001548
1549 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001550 postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001551 raw_spin_unlock_irq(&lock->wait_lock);
1552
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001553 if (postunlock)
1554 rt_mutex_postunlock(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001555}
1556
1557/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001558 * rt_mutex_destroy - mark a mutex unusable
1559 * @lock: the mutex to be destroyed
1560 *
1561 * This function marks the mutex uninitialized, and any subsequent
1562 * use of the mutex is forbidden. The mutex must not be locked when
1563 * this function is called.
1564 */
1565void rt_mutex_destroy(struct rt_mutex *lock)
1566{
1567 WARN_ON(rt_mutex_is_locked(lock));
1568#ifdef CONFIG_DEBUG_RT_MUTEXES
1569 lock->magic = NULL;
1570#endif
1571}
1572
1573EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1574
1575/**
1576 * __rt_mutex_init - initialize the rt lock
1577 *
1578 * @lock: the rt lock to be initialized
1579 *
1580 * Initialize the rt lock to unlocked state.
1581 *
1582 * Initializing of a locked rt lock is not allowed
1583 */
1584void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1585{
1586 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001587 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001588 lock->waiters = RB_ROOT;
1589 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001590
1591 debug_rt_mutex_init(lock, name);
1592}
1593EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001594
1595/**
1596 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1597 * proxy owner
1598 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001599 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001600 * @proxy_owner:the task to set as owner
1601 *
1602 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001603 *
1604 * Special API call for PI-futex support. This initializes the rtmutex and
1605 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1606 * possible at this point because the pi_state which contains the rtmutex
1607 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001608 */
1609void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1610 struct task_struct *proxy_owner)
1611{
1612 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001613 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001614 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001615}
1616
1617/**
1618 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1619 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001620 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001621 *
1622 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001623 *
1624 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1625 * (debugging) state. Concurrent operations on this rt_mutex are not
1626 * possible because it belongs to the pi_state which is about to be freed
1627 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001628 */
1629void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1630 struct task_struct *proxy_owner)
1631{
1632 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001633 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001634}
1635
Peter Zijlstra56222b22017-03-22 11:36:00 +01001636int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1637 struct rt_mutex_waiter *waiter,
1638 struct task_struct *task)
1639{
1640 int ret;
1641
1642 if (try_to_take_rt_mutex(lock, task, NULL))
1643 return 1;
1644
1645 /* We enforce deadlock detection for futexes */
1646 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1647 RT_MUTEX_FULL_CHAINWALK);
1648
1649 if (ret && !rt_mutex_owner(lock)) {
1650 /*
1651 * Reset the return value. We might have
1652 * returned with -EDEADLK and the owner
1653 * released the lock while we were walking the
1654 * pi chain. Let the waiter sort it out.
1655 */
1656 ret = 0;
1657 }
1658
1659 if (unlikely(ret))
1660 remove_waiter(lock, waiter);
1661
1662 debug_rt_mutex_print_deadlock(waiter);
1663
1664 return ret;
1665}
1666
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001667/**
Darren Hart8dac4562009-04-03 13:40:12 -07001668 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1669 * @lock: the rt_mutex to take
1670 * @waiter: the pre-initialized rt_mutex_waiter
1671 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001672 *
1673 * Returns:
1674 * 0 - task blocked on lock
1675 * 1 - acquired the lock for task, caller should wake it up
1676 * <0 - error
1677 *
1678 * Special API call for FUTEX_REQUEUE_PI support.
1679 */
1680int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1681 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001682 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001683{
1684 int ret;
1685
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001686 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstra56222b22017-03-22 11:36:00 +01001687 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001688 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001689
Darren Hart8dac4562009-04-03 13:40:12 -07001690 return ret;
1691}
1692
1693/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001694 * rt_mutex_next_owner - return the next owner of the lock
1695 *
1696 * @lock: the rt lock query
1697 *
1698 * Returns the next owner of the lock or NULL
1699 *
1700 * Caller has to serialize against other accessors to the lock
1701 * itself.
1702 *
1703 * Special API call for PI-futex support
1704 */
1705struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1706{
1707 if (!rt_mutex_has_waiters(lock))
1708 return NULL;
1709
1710 return rt_mutex_top_waiter(lock)->task;
1711}
Darren Hart8dac4562009-04-03 13:40:12 -07001712
1713/**
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001714 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
Darren Hart8dac4562009-04-03 13:40:12 -07001715 * @lock: the rt_mutex we were woken on
1716 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001717 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001718 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001719 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001720 * Wait for the the lock acquisition started on our behalf by
1721 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1722 * rt_mutex_cleanup_proxy_lock().
Darren Hart8dac4562009-04-03 13:40:12 -07001723 *
1724 * Returns:
1725 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001726 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001727 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001728 * Special API call for PI-futex support
Darren Hart8dac4562009-04-03 13:40:12 -07001729 */
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001730int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
Darren Hart8dac4562009-04-03 13:40:12 -07001731 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001732 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001733{
1734 int ret;
1735
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001736 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001737
1738 set_current_state(TASK_INTERRUPTIBLE);
1739
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001740 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001741 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001742
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001743 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001744
Darren Hart8dac4562009-04-03 13:40:12 -07001745 return ret;
1746}
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001747
1748/**
1749 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1750 * @lock: the rt_mutex we were woken on
1751 * @waiter: the pre-initialized rt_mutex_waiter
1752 *
1753 * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
1754 *
1755 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1756 * in fact still be granted ownership until we're removed. Therefore we can
1757 * find we are in fact the owner and must disregard the
1758 * rt_mutex_wait_proxy_lock() failure.
1759 *
1760 * Returns:
1761 * true - did the cleanup, we done.
1762 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1763 * caller should disregards its return value.
1764 *
1765 * Special API call for PI-futex support
1766 */
1767bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1768 struct rt_mutex_waiter *waiter)
1769{
1770 bool cleanup = false;
1771
1772 raw_spin_lock_irq(&lock->wait_lock);
1773 /*
1774 * Unless we're the owner; we're still enqueued on the wait_list.
1775 * So check if we became owner, if not, take us off the wait_list.
1776 */
1777 if (rt_mutex_owner(lock) != current) {
1778 remove_waiter(lock, waiter);
1779 fixup_rt_mutex_waiters(lock);
1780 cleanup = true;
1781 }
Peter Zijlstracfafcd12017-03-22 11:35:58 +01001782
1783 /*
1784 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1785 * have to fix that up.
1786 */
1787 fixup_rt_mutex_waiters(lock);
1788
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001789 raw_spin_unlock_irq(&lock->wait_lock);
1790
1791 return cleanup;
1792}