blob: c6eda049ef9f8dca30ba288e67cf184b3eba187e [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))
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100241 return dl_time_before(left->deadline, right->deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100242
243 return 0;
244}
245
246static void
247rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
248{
249 struct rb_node **link = &lock->waiters.rb_node;
250 struct rb_node *parent = NULL;
251 struct rt_mutex_waiter *entry;
252 int leftmost = 1;
253
254 while (*link) {
255 parent = *link;
256 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
257 if (rt_mutex_waiter_less(waiter, entry)) {
258 link = &parent->rb_left;
259 } else {
260 link = &parent->rb_right;
261 leftmost = 0;
262 }
263 }
264
265 if (leftmost)
266 lock->waiters_leftmost = &waiter->tree_entry;
267
268 rb_link_node(&waiter->tree_entry, parent, link);
269 rb_insert_color(&waiter->tree_entry, &lock->waiters);
270}
271
272static void
273rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
274{
275 if (RB_EMPTY_NODE(&waiter->tree_entry))
276 return;
277
278 if (lock->waiters_leftmost == &waiter->tree_entry)
279 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
280
281 rb_erase(&waiter->tree_entry, &lock->waiters);
282 RB_CLEAR_NODE(&waiter->tree_entry);
283}
284
285static void
286rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
287{
288 struct rb_node **link = &task->pi_waiters.rb_node;
289 struct rb_node *parent = NULL;
290 struct rt_mutex_waiter *entry;
291 int leftmost = 1;
292
293 while (*link) {
294 parent = *link;
295 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
296 if (rt_mutex_waiter_less(waiter, entry)) {
297 link = &parent->rb_left;
298 } else {
299 link = &parent->rb_right;
300 leftmost = 0;
301 }
302 }
303
304 if (leftmost)
305 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
306
307 rb_link_node(&waiter->pi_tree_entry, parent, link);
308 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
309}
310
311static void
312rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
313{
314 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
315 return;
316
317 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
318 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
319
320 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
321 RB_CLEAR_NODE(&waiter->pi_tree_entry);
322}
323
Peter Zijlstraacd58622017-03-23 15:56:11 +0100324static void rt_mutex_adjust_prio(struct task_struct *p)
Xunlei Pange96a77052017-03-23 15:56:08 +0100325{
Peter Zijlstraacd58622017-03-23 15:56:11 +0100326 struct task_struct *pi_task = NULL;
Xunlei Pange96a77052017-03-23 15:56:08 +0100327
Peter Zijlstraacd58622017-03-23 15:56:11 +0100328 lockdep_assert_held(&p->pi_lock);
Xunlei Pange96a77052017-03-23 15:56:08 +0100329
Peter Zijlstraacd58622017-03-23 15:56:11 +0100330 if (task_has_pi_waiters(p))
331 pi_task = task_top_pi_waiter(p)->task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700332
Peter Zijlstraacd58622017-03-23 15:56:11 +0100333 rt_mutex_setprio(p, pi_task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700334}
335
336/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000337 * Deadlock detection is conditional:
338 *
339 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
340 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
341 *
342 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
343 * conducted independent of the detect argument.
344 *
345 * If the waiter argument is NULL this indicates the deboost path and
346 * deadlock detection is disabled independent of the detect argument
347 * and the config settings.
348 */
349static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
350 enum rtmutex_chainwalk chwalk)
351{
352 /*
353 * This is just a wrapper function for the following call,
354 * because debug_rt_mutex_detect_deadlock() smells like a magic
355 * debug feature and I wanted to keep the cond function in the
356 * main source file along with the comments instead of having
357 * two of the same in the headers.
358 */
359 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
360}
361
362/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700363 * Max number of times we'll walk the boosting chain:
364 */
365int max_lock_depth = 1024;
366
Thomas Gleixner82084982014-06-05 11:16:12 +0200367static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
368{
369 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
370}
371
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700372/*
373 * Adjust the priority chain. Also used for deadlock detection.
374 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200375 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200376 * @task: the task owning the mutex (owner) for which a chain walk is
377 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900378 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200379 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
380 * things for a task that has just got its priority adjusted, and
381 * is waiting on a mutex)
382 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
383 * we dropped its pi_lock. Is never dereferenced, only used for
384 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200385 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200386 * its priority to the mutex owner (can be NULL in the case
387 * depicted above or if the top waiter is gone away and we are
388 * actually deboosting the owner)
389 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200390 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700391 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200392 *
393 * Chain walk basics and protection scope
394 *
395 * [R] refcount on task
396 * [P] task->pi_lock held
397 * [L] rtmutex->wait_lock held
398 *
399 * Step Description Protected by
400 * function arguments:
401 * @task [R]
402 * @orig_lock if != NULL @top_task is blocked on it
403 * @next_lock Unprotected. Cannot be
404 * dereferenced. Only used for
405 * comparison.
406 * @orig_waiter if != NULL @top_task is blocked on it
407 * @top_task current, or in case of proxy
408 * locking protected by calling
409 * code
410 * again:
411 * loop_sanity_check();
412 * retry:
413 * [1] lock(task->pi_lock); [R] acquire [P]
414 * [2] waiter = task->pi_blocked_on; [P]
415 * [3] check_exit_conditions_1(); [P]
416 * [4] lock = waiter->lock; [P]
417 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
418 * unlock(task->pi_lock); release [P]
419 * goto retry;
420 * }
421 * [6] check_exit_conditions_2(); [P] + [L]
422 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
423 * [8] unlock(task->pi_lock); release [P]
424 * put_task_struct(task); release [R]
425 * [9] check_exit_conditions_3(); [L]
426 * [10] task = owner(lock); [L]
427 * get_task_struct(task); [L] acquire [R]
428 * lock(task->pi_lock); [L] acquire [P]
429 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
430 * [12] check_exit_conditions_4(); [P] + [L]
431 * [13] unlock(task->pi_lock); release [P]
432 * unlock(lock->wait_lock); release [L]
433 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700434 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200435static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000436 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200437 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200438 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200439 struct rt_mutex_waiter *orig_waiter,
440 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700441{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700442 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000443 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000444 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000445 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000446 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000447 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700448
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000449 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700450
451 /*
452 * The (de)boosting is a step by step approach with a lot of
453 * pitfalls. We want this to be preemptible and we want hold a
454 * maximum of two locks per step. So we have to check
455 * carefully whether things change under us.
456 */
457 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200458 /*
459 * We limit the lock chain length for each invocation.
460 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700461 if (++depth > max_lock_depth) {
462 static int prev_max;
463
464 /*
465 * Print this only once. If the admin changes the limit,
466 * print a new message when reaching the limit again.
467 */
468 if (prev_max != max_lock_depth) {
469 prev_max = max_lock_depth;
470 printk(KERN_WARNING "Maximum lock depth %d reached "
471 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700472 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700473 }
474 put_task_struct(task);
475
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200476 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700477 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200478
479 /*
480 * We are fully preemptible here and only hold the refcount on
481 * @task. So everything can have changed under us since the
482 * caller or our own code below (goto retry/again) dropped all
483 * locks.
484 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700485 retry:
486 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200487 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700488 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100489 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700490
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200491 /*
492 * [2] Get the waiter on which @task is blocked on.
493 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700494 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200495
496 /*
497 * [3] check_exit_conditions_1() protected by task->pi_lock.
498 */
499
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700500 /*
501 * Check whether the end of the boosting chain has been
502 * reached or the state of the chain has changed while we
503 * dropped the locks.
504 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800505 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700506 goto out_unlock_pi;
507
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700508 /*
509 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800510 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700511 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800512 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700513 goto out_unlock_pi;
514
515 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200516 * We dropped all locks after taking a refcount on @task, so
517 * the task might have moved on in the lock chain or even left
518 * the chain completely and blocks now on an unrelated lock or
519 * on @orig_lock.
520 *
521 * We stored the lock on which @task was blocked in @next_lock,
522 * so we can detect the chain change.
523 */
524 if (next_lock != waiter->lock)
525 goto out_unlock_pi;
526
527 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700528 * Drop out, when the task has no waiters. Note,
529 * top_waiter can be NULL, when we are in the deboosting
530 * mode!
531 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000532 if (top_waiter) {
533 if (!task_has_pi_waiters(task))
534 goto out_unlock_pi;
535 /*
536 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000537 * are not the top pi waiter of the task. If deadlock
538 * detection is enabled we continue, but stop the
539 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000540 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000541 if (top_waiter != task_top_pi_waiter(task)) {
542 if (!detect_deadlock)
543 goto out_unlock_pi;
544 else
545 requeue = false;
546 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000547 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700548
549 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000550 * If the waiter priority is the same as the task priority
551 * then there is no further priority adjustment necessary. If
552 * deadlock detection is off, we stop the chain walk. If its
553 * enabled we continue, but stop the requeueing in the chain
554 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700555 */
Xunlei Pang85e2d4f2017-03-23 15:56:09 +0100556 if (waiter->prio == task->prio && !dl_task(task)) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000557 if (!detect_deadlock)
558 goto out_unlock_pi;
559 else
560 requeue = false;
561 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700562
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200563 /*
564 * [4] Get the next lock
565 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700566 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200567 /*
568 * [5] We need to trylock here as we are holding task->pi_lock,
569 * which is the reverse lock order versus the other rtmutex
570 * operations.
571 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100572 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100573 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700574 cpu_relax();
575 goto retry;
576 }
577
Thomas Gleixner397335f2014-05-22 03:25:39 +0000578 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200579 * [6] check_exit_conditions_2() protected by task->pi_lock and
580 * lock->wait_lock.
581 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000582 * Deadlock detection. If the lock is the same as the original
583 * lock which caused us to walk the lock chain or if the
584 * current lock is owned by the task which initiated the chain
585 * walk, we detected a deadlock.
586 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700587 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000588 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100589 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200590 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700591 goto out_unlock_pi;
592 }
593
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000594 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000595 * If we just follow the lock chain for deadlock detection, no
596 * need to do all the requeue operations. To avoid a truckload
597 * of conditionals around the various places below, just do the
598 * minimum chain walk checks.
599 */
600 if (!requeue) {
601 /*
602 * No requeue[7] here. Just release @task [8]
603 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100604 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000605 put_task_struct(task);
606
607 /*
608 * [9] check_exit_conditions_3 protected by lock->wait_lock.
609 * If there is no owner of the lock, end of chain.
610 */
611 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100612 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000613 return 0;
614 }
615
616 /* [10] Grab the next task, i.e. owner of @lock */
617 task = rt_mutex_owner(lock);
618 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100619 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000620
621 /*
622 * No requeue [11] here. We just do deadlock detection.
623 *
624 * [12] Store whether owner is blocked
625 * itself. Decision is made after dropping the locks
626 */
627 next_lock = task_blocked_on_lock(task);
628 /*
629 * Get the top waiter for the next iteration
630 */
631 top_waiter = rt_mutex_top_waiter(lock);
632
633 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100634 raw_spin_unlock(&task->pi_lock);
635 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000636
637 /* If owner is not blocked, end of chain. */
638 if (!next_lock)
639 goto out_put_task;
640 goto again;
641 }
642
643 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000644 * Store the current top waiter before doing the requeue
645 * operation on @lock. We need it for the boost/deboost
646 * decision below.
647 */
648 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700649
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700650 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100651 rt_mutex_dequeue(lock, waiter);
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100652
653 /*
654 * Update the waiter prio fields now that we're dequeued.
655 *
656 * These values can have changed through either:
657 *
658 * sys_sched_set_scheduler() / sys_sched_setattr()
659 *
660 * or
661 *
662 * DL CBS enforcement advancing the effective deadline.
663 *
664 * Even though pi_waiters also uses these fields, and that tree is only
665 * updated in [11], we can do this here, since we hold [L], which
666 * serializes all pi_waiters access and rb_erase() does not care about
667 * the values of the node being removed.
668 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100669 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100670 waiter->deadline = task->dl.deadline;
671
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100672 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700673
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200674 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100675 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200676 put_task_struct(task);
677
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000678 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200679 * [9] check_exit_conditions_3 protected by lock->wait_lock.
680 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000681 * We must abort the chain walk if there is no lock owner even
682 * in the dead lock detection case, as we have nothing to
683 * follow here. This is the end of the chain we are walking.
684 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800685 if (!rt_mutex_owner(lock)) {
686 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200687 * If the requeue [7] above changed the top waiter,
688 * then we need to wake the new top waiter up to try
689 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800690 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000691 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800692 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100693 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200694 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800695 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700696
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200697 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700698 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700699 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100700 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700701
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200702 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700703 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000704 /*
705 * The waiter became the new top (highest priority)
706 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700707 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000708 * and adjust the priority of the owner.
709 */
710 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100711 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100712 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700713
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000714 } else if (prerequeue_top_waiter == waiter) {
715 /*
716 * The waiter was the top waiter on the lock, but is
717 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700718 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000719 * (highest priority) waiter and adjust the priority
720 * of the owner.
721 * The new top waiter is stored in @waiter so that
722 * @waiter == @top_waiter evaluates to true below and
723 * we continue to deboost the rest of the chain.
724 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100725 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700726 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100727 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100728 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000729 } else {
730 /*
731 * Nothing changed. No need to do any priority
732 * adjustment.
733 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700734 }
735
Thomas Gleixner82084982014-06-05 11:16:12 +0200736 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200737 * [12] check_exit_conditions_4() protected by task->pi_lock
738 * and lock->wait_lock. The actual decisions are made after we
739 * dropped the locks.
740 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200741 * Check whether the task which owns the current lock is pi
742 * blocked itself. If yes we store a pointer to the lock for
743 * the lock chain change detection above. After we dropped
744 * task->pi_lock next_lock cannot be dereferenced anymore.
745 */
746 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000747 /*
748 * Store the top waiter of @lock for the end of chain walk
749 * decision below.
750 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700751 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200752
753 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100754 raw_spin_unlock(&task->pi_lock);
755 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700756
Thomas Gleixner82084982014-06-05 11:16:12 +0200757 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200758 * Make the actual exit decisions [12], based on the stored
759 * values.
760 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200761 * We reached the end of the lock chain. Stop right here. No
762 * point to go back just to figure that out.
763 */
764 if (!next_lock)
765 goto out_put_task;
766
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000767 /*
768 * If the current waiter is not the top waiter on the lock,
769 * then we can stop the chain walk here if we are not in full
770 * deadlock detection mode.
771 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700772 if (!detect_deadlock && waiter != top_waiter)
773 goto out_put_task;
774
775 goto again;
776
777 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100778 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700779 out_put_task:
780 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700781
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700782 return ret;
783}
784
785/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700786 * Try to take an rt-mutex
787 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100788 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800789 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200790 * @lock: The lock to be acquired.
791 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700792 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200793 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700794 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800795static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200796 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700797{
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100798 lockdep_assert_held(&lock->wait_lock);
799
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700800 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200801 * Before testing whether we can acquire @lock, we set the
802 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
803 * other tasks which try to modify @lock into the slow path
804 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700805 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200806 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
807 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700808 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200809 * - There is a lock owner. The caller must fixup the
810 * transient state if it does a trylock or leaves the lock
811 * function due to a signal or timeout.
812 *
813 * - @task acquires the lock and there are no other
814 * waiters. This is undone in rt_mutex_set_owner(@task) at
815 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700816 */
817 mark_rt_mutex_waiters(lock);
818
Thomas Gleixner358c3312014-06-11 01:01:13 +0200819 /*
820 * If @lock has an owner, give up.
821 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800822 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700823 return 0;
824
Lai Jiangshan81612392011-01-14 17:09:41 +0800825 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200826 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700827 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200828 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800829 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200830 if (waiter) {
831 /*
832 * If waiter is not the highest priority waiter of
833 * @lock, give up.
834 */
835 if (waiter != rt_mutex_top_waiter(lock))
836 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800837
838 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200839 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700840 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200841 */
842 rt_mutex_dequeue(lock, waiter);
843
844 } else {
845 /*
846 * If the lock has waiters already we check whether @task is
847 * eligible to take over the lock.
848 *
849 * If there are no other waiters, @task can acquire
850 * the lock. @task->pi_blocked_on is NULL, so it does
851 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800852 */
853 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200854 /*
855 * If @task->prio is greater than or equal to
856 * the top waiter priority (kernel view),
857 * @task lost.
858 */
859 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
860 return 0;
861
862 /*
863 * The current top waiter stays enqueued. We
864 * don't have to change anything in the lock
865 * waiters order.
866 */
867 } else {
868 /*
869 * No waiters. Take the lock without the
870 * pi_lock dance.@task->pi_blocked_on is NULL
871 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700872 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200873 */
874 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800875 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800876 }
877
Thomas Gleixner358c3312014-06-11 01:01:13 +0200878 /*
879 * Clear @task->pi_blocked_on. Requires protection by
880 * @task->pi_lock. Redundant operation for the @waiter == NULL
881 * case, but conditionals are more expensive than a redundant
882 * store.
883 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100884 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200885 task->pi_blocked_on = NULL;
886 /*
887 * Finish the lock acquisition. @task is the new owner. If
888 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700889 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200890 */
891 if (rt_mutex_has_waiters(lock))
892 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100893 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200894
895takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700896 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700897 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700898
Thomas Gleixner358c3312014-06-11 01:01:13 +0200899 /*
900 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
901 * are still waiters or clears it.
902 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800903 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700904
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700905 return 1;
906}
907
908/*
909 * Task blocks on lock.
910 *
911 * Prepare waiter and propagate pi chain
912 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100913 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700914 */
915static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
916 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700917 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000918 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700919{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700920 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700921 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200922 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700923 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700924
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100925 lockdep_assert_held(&lock->wait_lock);
926
Thomas Gleixner397335f2014-05-22 03:25:39 +0000927 /*
928 * Early deadlock detection. We really don't want the task to
929 * enqueue on itself just to untangle the mess later. It's not
930 * only an optimization. We drop the locks, so another waiter
931 * can come in before the chain walk detects the deadlock. So
932 * the other will detect the deadlock and return -EDEADLOCK,
933 * which is wrong, as the other waiter is not in a deadlock
934 * situation.
935 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200936 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000937 return -EDEADLK;
938
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100939 raw_spin_lock(&task->pi_lock);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100940 rt_mutex_adjust_prio(task);
Darren Hart8dac4562009-04-03 13:40:12 -0700941 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700942 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100943 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100944 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700945
946 /* Get the top priority waiter on the lock */
947 if (rt_mutex_has_waiters(lock))
948 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100949 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700950
Darren Hart8dac4562009-04-03 13:40:12 -0700951 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700952
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100953 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700954
Lai Jiangshan81612392011-01-14 17:09:41 +0800955 if (!owner)
956 return 0;
957
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100958 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700959 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100960 rt_mutex_dequeue_pi(owner, top_waiter);
961 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700962
Peter Zijlstraacd58622017-03-23 15:56:11 +0100963 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700964 if (owner->pi_blocked_on)
965 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000966 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700967 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200968 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700969
Thomas Gleixner82084982014-06-05 11:16:12 +0200970 /* Store the lock on which owner is blocked or NULL */
971 next_lock = task_blocked_on_lock(owner);
972
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100973 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +0200974 /*
975 * Even if full deadlock detection is on, if the owner is not
976 * blocked itself, we can avoid finding this out in the chain
977 * walk.
978 */
979 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700980 return 0;
981
Steven Rostedtdb630632006-09-29 01:59:44 -0700982 /*
983 * The owner can't disappear while holding a lock,
984 * so the owner struct is protected by wait_lock.
985 * Gets dropped in rt_mutex_adjust_prio_chain()!
986 */
987 get_task_struct(owner);
988
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100989 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700990
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000991 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200992 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700993
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100994 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700995
996 return res;
997}
998
999/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001000 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001001 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001002 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001003 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001004 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001005static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1006 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001007{
1008 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001009
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001010 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001011
1012 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001013
1014 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +01001015 * Remove it from current->pi_waiters and deboost.
1016 *
1017 * We must in fact deboost here in order to ensure we call
1018 * rt_mutex_setprio() to update p->pi_top_task before the
1019 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001020 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001021 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001022 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001023
Thomas Gleixner27e35712014-06-11 18:44:04 +00001024 /*
1025 * As we are waking up the top waiter, and the waiter stays
1026 * queued on the lock until it gets the lock, this lock
1027 * obviously has waiters. Just set the bit here and this has
1028 * the added benefit of forcing all new tasks into the
1029 * slow path making sure no task of lower priority than
1030 * the top waiter can steal this lock.
1031 */
1032 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001033
Peter Zijlstraacd58622017-03-23 15:56:11 +01001034 /*
1035 * We deboosted before waking the top waiter task such that we don't
1036 * run two tasks with the 'same' priority (and ensure the
1037 * p->pi_top_task pointer points to a blocked task). This however can
1038 * lead to priority inversion if we would get preempted after the
1039 * deboost but before waking our donor task, hence the preempt_disable()
1040 * before unlock.
1041 *
1042 * Pairs with preempt_enable() in rt_mutex_postunlock();
1043 */
1044 preempt_disable();
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001045 wake_q_add(wake_q, waiter->task);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001046 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001047}
1048
1049/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001050 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001051 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001052 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001053 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001054 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001055static void remove_waiter(struct rt_mutex *lock,
1056 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001057{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001058 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001059 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001060 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001061
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +01001062 lockdep_assert_held(&lock->wait_lock);
1063
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001064 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001065 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001066 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001067 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001068
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001069 /*
1070 * Only update priority if the waiter was the highest priority
1071 * waiter of the lock and there is an owner to update.
1072 */
1073 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001074 return;
1075
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001076 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001077
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001078 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001079
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001080 if (rt_mutex_has_waiters(lock))
1081 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001082
Peter Zijlstraacd58622017-03-23 15:56:11 +01001083 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001084
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001085 /* Store the lock on which owner is blocked or NULL */
1086 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001087
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001088 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001089
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001090 /*
1091 * Don't walk the chain, if the owner task is not blocked
1092 * itself.
1093 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001094 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001095 return;
1096
Steven Rostedtdb630632006-09-29 01:59:44 -07001097 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1098 get_task_struct(owner);
1099
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001100 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001101
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001102 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1103 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001104
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001105 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001106}
1107
1108/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001109 * Recheck the pi chain, in case we got a priority setting
1110 *
1111 * Called from sched_setscheduler
1112 */
1113void rt_mutex_adjust_pi(struct task_struct *task)
1114{
1115 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001116 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001117 unsigned long flags;
1118
Thomas Gleixner1d615482009-11-17 14:54:03 +01001119 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001120
1121 waiter = task->pi_blocked_on;
Peter Zijlstraacd58622017-03-23 15:56:11 +01001122 if (!waiter || (waiter->prio == task->prio && !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001123 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001124 return;
1125 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001126 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001127 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001128
Steven Rostedtdb630632006-09-29 01:59:44 -07001129 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1130 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001131
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001132 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1133 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001134}
1135
Peter Zijlstra50809352017-03-22 11:35:56 +01001136void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1137{
1138 debug_rt_mutex_init_waiter(waiter);
1139 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1140 RB_CLEAR_NODE(&waiter->tree_entry);
1141 waiter->task = NULL;
1142}
1143
Darren Hart8dac4562009-04-03 13:40:12 -07001144/**
1145 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1146 * @lock: the rt_mutex to take
1147 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001148 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001149 * @timeout: the pre-initialized and started timer, or NULL for none
1150 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001151 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001152 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001153 */
1154static int __sched
1155__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1156 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001157 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001158{
1159 int ret = 0;
1160
1161 for (;;) {
1162 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001163 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001164 break;
1165
1166 /*
1167 * TASK_INTERRUPTIBLE checks for signals and
1168 * timeout. Ignored otherwise.
1169 */
Steven Rostedt (VMware)4009f4b2017-01-19 11:32:34 -05001170 if (likely(state == TASK_INTERRUPTIBLE)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001171 /* Signal pending? */
1172 if (signal_pending(current))
1173 ret = -EINTR;
1174 if (timeout && !timeout->task)
1175 ret = -ETIMEDOUT;
1176 if (ret)
1177 break;
1178 }
1179
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001180 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001181
1182 debug_rt_mutex_print_deadlock(waiter);
1183
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001184 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001185
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001186 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001187 set_current_state(state);
1188 }
1189
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001190 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001191 return ret;
1192}
1193
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001194static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1195 struct rt_mutex_waiter *w)
1196{
1197 /*
1198 * If the result is not -EDEADLOCK or the caller requested
1199 * deadlock detection, nothing to do here.
1200 */
1201 if (res != -EDEADLOCK || detect_deadlock)
1202 return;
1203
1204 /*
1205 * Yell lowdly and stop the task right here.
1206 */
1207 rt_mutex_print_deadlock(w);
1208 while (1) {
1209 set_current_state(TASK_INTERRUPTIBLE);
1210 schedule();
1211 }
1212}
1213
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001214/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001215 * Slow path lock function:
1216 */
1217static int __sched
1218rt_mutex_slowlock(struct rt_mutex *lock, int state,
1219 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001220 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001221{
1222 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001223 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001224 int ret = 0;
1225
Peter Zijlstra50809352017-03-22 11:35:56 +01001226 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001227
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001228 /*
1229 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1230 * be called in early boot if the cmpxchg() fast path is disabled
1231 * (debug, no architecture support). In this case we will acquire the
1232 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1233 * enable interrupts in that early boot case. So we need to use the
1234 * irqsave/restore variants.
1235 */
1236 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001237
1238 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001239 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001240 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001241 return 0;
1242 }
1243
1244 set_current_state(state);
1245
1246 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001247 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001248 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001249
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001250 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001251
1252 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001253 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001254 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001255
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001256 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001257 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001258 if (rt_mutex_has_waiters(lock))
1259 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001260 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001261 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001262
1263 /*
1264 * try_to_take_rt_mutex() sets the waiter bit
1265 * unconditionally. We might have to fix that up.
1266 */
1267 fixup_rt_mutex_waiters(lock);
1268
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001269 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001270
1271 /* Remove pending timer: */
1272 if (unlikely(timeout))
1273 hrtimer_cancel(&timeout->timer);
1274
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001275 debug_rt_mutex_free_waiter(&waiter);
1276
1277 return ret;
1278}
1279
1280/*
1281 * Slow path try-lock function:
1282 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001283static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001284{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001285 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001286 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001287
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001288 /*
1289 * If the lock already has an owner we fail to get the lock.
1290 * This can be done without taking the @lock->wait_lock as
1291 * it is only being read, and this is a trylock anyway.
1292 */
1293 if (rt_mutex_owner(lock))
1294 return 0;
1295
1296 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001297 * The mutex has currently no owner. Lock the wait lock and try to
1298 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001299 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001300 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001301
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001302 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001303
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001304 /*
1305 * try_to_take_rt_mutex() sets the lock waiters bit
1306 * unconditionally. Clean this up.
1307 */
1308 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001309
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001310 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001311
1312 return ret;
1313}
1314
1315/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001316 * Slow path to release a rt-mutex.
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001317 *
1318 * Return whether the current task needs to call rt_mutex_postunlock().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001319 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001320static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1321 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001322{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001323 unsigned long flags;
1324
1325 /* irqsave required to support early boot calls */
1326 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001327
1328 debug_rt_mutex_unlock(lock);
1329
Thomas Gleixner27e35712014-06-11 18:44:04 +00001330 /*
1331 * We must be careful here if the fast path is enabled. If we
1332 * have no waiters queued we cannot set owner to NULL here
1333 * because of:
1334 *
1335 * foo->lock->owner = NULL;
1336 * rtmutex_lock(foo->lock); <- fast path
1337 * free = atomic_dec_and_test(foo->refcnt);
1338 * rtmutex_unlock(foo->lock); <- fast path
1339 * if (free)
1340 * kfree(foo);
1341 * raw_spin_unlock(foo->lock->wait_lock);
1342 *
1343 * So for the fastpath enabled kernel:
1344 *
1345 * Nothing can set the waiters bit as long as we hold
1346 * lock->wait_lock. So we do the following sequence:
1347 *
1348 * owner = rt_mutex_owner(lock);
1349 * clear_rt_mutex_waiters(lock);
1350 * raw_spin_unlock(&lock->wait_lock);
1351 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1352 * return;
1353 * goto retry;
1354 *
1355 * The fastpath disabled variant is simple as all access to
1356 * lock->owner is serialized by lock->wait_lock:
1357 *
1358 * lock->owner = NULL;
1359 * raw_spin_unlock(&lock->wait_lock);
1360 */
1361 while (!rt_mutex_has_waiters(lock)) {
1362 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001363 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001364 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001365 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001366 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001367 }
1368
Thomas Gleixner27e35712014-06-11 18:44:04 +00001369 /*
1370 * The wakeup next waiter path does not suffer from the above
1371 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001372 *
1373 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001374 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001375 mark_wakeup_next_waiter(wake_q, lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001376 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001377
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001378 return true; /* call rt_mutex_postunlock() */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001379}
1380
1381/*
1382 * debug aware fast / slowpath lock,trylock,unlock
1383 *
1384 * The atomic acquire/release ops are compiled away, when either the
1385 * architecture does not support cmpxchg or when debugging is enabled.
1386 */
1387static inline int
1388rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001389 int (*slowfn)(struct rt_mutex *lock, int state,
1390 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001391 enum rtmutex_chainwalk chwalk))
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 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001395
1396 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001397}
1398
1399static inline int
1400rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001401 struct hrtimer_sleeper *timeout,
1402 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001403 int (*slowfn)(struct rt_mutex *lock, int state,
1404 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001405 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001406{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001407 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001408 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001409 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001410
1411 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001412}
1413
1414static inline int
1415rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001416 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001417{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001418 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001419 return 1;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001420
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001421 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001422}
1423
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001424/*
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001425 * Performs the wakeup of the the top-waiter and re-enables preemption.
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001426 */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001427void rt_mutex_postunlock(struct wake_q_head *wake_q)
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001428{
1429 wake_up_q(wake_q);
1430
1431 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001432 preempt_enable();
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001433}
1434
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001435static inline void
1436rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001437 bool (*slowfn)(struct rt_mutex *lock,
1438 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001439{
Waiman Long194a6b52016-11-17 11:46:38 -05001440 DEFINE_WAKE_Q(wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001441
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001442 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1443 return;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001444
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001445 if (slowfn(lock, &wake_q))
1446 rt_mutex_postunlock(&wake_q);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001447}
1448
1449/**
1450 * rt_mutex_lock - lock a rt_mutex
1451 *
1452 * @lock: the rt_mutex to be locked
1453 */
1454void __sched rt_mutex_lock(struct rt_mutex *lock)
1455{
1456 might_sleep();
1457
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001458 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001459}
1460EXPORT_SYMBOL_GPL(rt_mutex_lock);
1461
1462/**
1463 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1464 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001465 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001466 *
1467 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001468 * 0 on success
1469 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001470 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001471int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001472{
1473 might_sleep();
1474
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001475 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001476}
1477EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1478
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001479/*
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001480 * Futex variant, must not use fastpath.
1481 */
1482int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1483{
1484 return rt_mutex_slowtrylock(lock);
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001485}
1486
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001487/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001488 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1489 * the timeout structure is provided
1490 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001491 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001492 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001493 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001494 *
1495 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001496 * 0 on success
1497 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001498 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001499 */
1500int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001501rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001502{
1503 might_sleep();
1504
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001505 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1506 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001507 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001508}
1509EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1510
1511/**
1512 * rt_mutex_trylock - try to lock a rt_mutex
1513 *
1514 * @lock: the rt_mutex to be locked
1515 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001516 * This function can only be called in thread context. It's safe to
1517 * call it from atomic regions, but not from hard interrupt or soft
1518 * interrupt context.
1519 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001520 * Returns 1 on success and 0 on contention
1521 */
1522int __sched rt_mutex_trylock(struct rt_mutex *lock)
1523{
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001524 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001525 return 0;
1526
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001527 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1528}
1529EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1530
1531/**
1532 * rt_mutex_unlock - unlock a rt_mutex
1533 *
1534 * @lock: the rt_mutex to be unlocked
1535 */
1536void __sched rt_mutex_unlock(struct rt_mutex *lock)
1537{
1538 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1539}
1540EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1541
Luis Henriques23b94b92009-04-29 21:54:51 +01001542/**
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001543 * Futex variant, that since futex variants do not use the fast-path, can be
1544 * simple and will not need to retry.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001545 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001546bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1547 struct wake_q_head *wake_q)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001548{
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001549 lockdep_assert_held(&lock->wait_lock);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001550
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001551 debug_rt_mutex_unlock(lock);
1552
1553 if (!rt_mutex_has_waiters(lock)) {
1554 lock->owner = NULL;
1555 return false; /* done */
1556 }
1557
1558 mark_wakeup_next_waiter(wake_q, lock);
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001559 /*
1560 * We've already deboosted, retain preempt_disabled when dropping
1561 * the wait_lock to avoid inversion until the wakeup. Matched
1562 * by rt_mutex_postunlock();
1563 */
1564 preempt_disable();
1565
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001566 return true; /* call postunlock() */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001567}
1568
1569void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1570{
1571 DEFINE_WAKE_Q(wake_q);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001572 bool postunlock;
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001573
1574 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001575 postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001576 raw_spin_unlock_irq(&lock->wait_lock);
1577
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001578 if (postunlock)
1579 rt_mutex_postunlock(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001580}
1581
1582/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001583 * rt_mutex_destroy - mark a mutex unusable
1584 * @lock: the mutex to be destroyed
1585 *
1586 * This function marks the mutex uninitialized, and any subsequent
1587 * use of the mutex is forbidden. The mutex must not be locked when
1588 * this function is called.
1589 */
1590void rt_mutex_destroy(struct rt_mutex *lock)
1591{
1592 WARN_ON(rt_mutex_is_locked(lock));
1593#ifdef CONFIG_DEBUG_RT_MUTEXES
1594 lock->magic = NULL;
1595#endif
1596}
1597
1598EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1599
1600/**
1601 * __rt_mutex_init - initialize the rt lock
1602 *
1603 * @lock: the rt lock to be initialized
1604 *
1605 * Initialize the rt lock to unlocked state.
1606 *
1607 * Initializing of a locked rt lock is not allowed
1608 */
1609void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1610{
1611 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001612 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001613 lock->waiters = RB_ROOT;
1614 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001615
1616 debug_rt_mutex_init(lock, name);
1617}
1618EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001619
1620/**
1621 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1622 * proxy owner
1623 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001624 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001625 * @proxy_owner:the task to set as owner
1626 *
1627 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001628 *
1629 * Special API call for PI-futex support. This initializes the rtmutex and
1630 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1631 * possible at this point because the pi_state which contains the rtmutex
1632 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001633 */
1634void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1635 struct task_struct *proxy_owner)
1636{
1637 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001638 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001639 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001640}
1641
1642/**
1643 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1644 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001645 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001646 *
1647 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001648 *
1649 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1650 * (debugging) state. Concurrent operations on this rt_mutex are not
1651 * possible because it belongs to the pi_state which is about to be freed
1652 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001653 */
1654void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1655 struct task_struct *proxy_owner)
1656{
1657 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001658 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001659}
1660
Peter Zijlstra56222b22017-03-22 11:36:00 +01001661int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1662 struct rt_mutex_waiter *waiter,
1663 struct task_struct *task)
1664{
1665 int ret;
1666
1667 if (try_to_take_rt_mutex(lock, task, NULL))
1668 return 1;
1669
1670 /* We enforce deadlock detection for futexes */
1671 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1672 RT_MUTEX_FULL_CHAINWALK);
1673
1674 if (ret && !rt_mutex_owner(lock)) {
1675 /*
1676 * Reset the return value. We might have
1677 * returned with -EDEADLK and the owner
1678 * released the lock while we were walking the
1679 * pi chain. Let the waiter sort it out.
1680 */
1681 ret = 0;
1682 }
1683
1684 if (unlikely(ret))
1685 remove_waiter(lock, waiter);
1686
1687 debug_rt_mutex_print_deadlock(waiter);
1688
1689 return ret;
1690}
1691
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001692/**
Darren Hart8dac4562009-04-03 13:40:12 -07001693 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1694 * @lock: the rt_mutex to take
1695 * @waiter: the pre-initialized rt_mutex_waiter
1696 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001697 *
1698 * Returns:
1699 * 0 - task blocked on lock
1700 * 1 - acquired the lock for task, caller should wake it up
1701 * <0 - error
1702 *
1703 * Special API call for FUTEX_REQUEUE_PI support.
1704 */
1705int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1706 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001707 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001708{
1709 int ret;
1710
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001711 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstra56222b22017-03-22 11:36:00 +01001712 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001713 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001714
Darren Hart8dac4562009-04-03 13:40:12 -07001715 return ret;
1716}
1717
1718/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001719 * rt_mutex_next_owner - return the next owner of the lock
1720 *
1721 * @lock: the rt lock query
1722 *
1723 * Returns the next owner of the lock or NULL
1724 *
1725 * Caller has to serialize against other accessors to the lock
1726 * itself.
1727 *
1728 * Special API call for PI-futex support
1729 */
1730struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1731{
1732 if (!rt_mutex_has_waiters(lock))
1733 return NULL;
1734
1735 return rt_mutex_top_waiter(lock)->task;
1736}
Darren Hart8dac4562009-04-03 13:40:12 -07001737
1738/**
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001739 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
Darren Hart8dac4562009-04-03 13:40:12 -07001740 * @lock: the rt_mutex we were woken on
1741 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001742 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001743 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001744 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001745 * Wait for the the lock acquisition started on our behalf by
1746 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1747 * rt_mutex_cleanup_proxy_lock().
Darren Hart8dac4562009-04-03 13:40:12 -07001748 *
1749 * Returns:
1750 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001751 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001752 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001753 * Special API call for PI-futex support
Darren Hart8dac4562009-04-03 13:40:12 -07001754 */
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001755int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
Darren Hart8dac4562009-04-03 13:40:12 -07001756 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001757 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001758{
1759 int ret;
1760
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001761 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001762
1763 set_current_state(TASK_INTERRUPTIBLE);
1764
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001765 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001766 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001767
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001768 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001769
Darren Hart8dac4562009-04-03 13:40:12 -07001770 return ret;
1771}
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001772
1773/**
1774 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1775 * @lock: the rt_mutex we were woken on
1776 * @waiter: the pre-initialized rt_mutex_waiter
1777 *
1778 * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
1779 *
1780 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1781 * in fact still be granted ownership until we're removed. Therefore we can
1782 * find we are in fact the owner and must disregard the
1783 * rt_mutex_wait_proxy_lock() failure.
1784 *
1785 * Returns:
1786 * true - did the cleanup, we done.
1787 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1788 * caller should disregards its return value.
1789 *
1790 * Special API call for PI-futex support
1791 */
1792bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1793 struct rt_mutex_waiter *waiter)
1794{
1795 bool cleanup = false;
1796
1797 raw_spin_lock_irq(&lock->wait_lock);
1798 /*
1799 * Unless we're the owner; we're still enqueued on the wait_list.
1800 * So check if we became owner, if not, take us off the wait_list.
1801 */
1802 if (rt_mutex_owner(lock) != current) {
1803 remove_waiter(lock, waiter);
1804 fixup_rt_mutex_waiters(lock);
1805 cleanup = true;
1806 }
Peter Zijlstracfafcd12017-03-22 11:35:58 +01001807
1808 /*
1809 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1810 * have to fix that up.
1811 */
1812 fixup_rt_mutex_waiters(lock);
1813
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001814 raw_spin_unlock_irq(&lock->wait_lock);
1815
1816 return cleanup;
1817}