blob: b9550941690915bc23323cd772e060cda7d3a704 [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 Zijlstra19830e52017-03-23 15:56:14 +0100227/*
228 * Only use with rt_mutex_waiter_{less,equal}()
229 */
230#define task_to_waiter(p) \
231 &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
232
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100233static inline int
234rt_mutex_waiter_less(struct rt_mutex_waiter *left,
235 struct rt_mutex_waiter *right)
236{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100237 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100238 return 1;
239
240 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100241 * If both waiters have dl_prio(), we check the deadlines of the
242 * associated tasks.
243 * If left waiter has a dl_prio(), and we didn't return 1 above,
244 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100245 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100246 if (dl_prio(left->prio))
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100247 return dl_time_before(left->deadline, right->deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100248
249 return 0;
250}
251
Peter Zijlstra19830e52017-03-23 15:56:14 +0100252static inline int
253rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
254 struct rt_mutex_waiter *right)
255{
256 if (left->prio != right->prio)
257 return 0;
258
259 /*
260 * If both waiters have dl_prio(), we check the deadlines of the
261 * associated tasks.
262 * If left waiter has a dl_prio(), and we didn't return 0 above,
263 * then right waiter has a dl_prio() too.
264 */
265 if (dl_prio(left->prio))
266 return left->deadline == right->deadline;
267
268 return 1;
269}
270
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100271static void
272rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
273{
274 struct rb_node **link = &lock->waiters.rb_node;
275 struct rb_node *parent = NULL;
276 struct rt_mutex_waiter *entry;
277 int leftmost = 1;
278
279 while (*link) {
280 parent = *link;
281 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
282 if (rt_mutex_waiter_less(waiter, entry)) {
283 link = &parent->rb_left;
284 } else {
285 link = &parent->rb_right;
286 leftmost = 0;
287 }
288 }
289
290 if (leftmost)
291 lock->waiters_leftmost = &waiter->tree_entry;
292
293 rb_link_node(&waiter->tree_entry, parent, link);
294 rb_insert_color(&waiter->tree_entry, &lock->waiters);
295}
296
297static void
298rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
299{
300 if (RB_EMPTY_NODE(&waiter->tree_entry))
301 return;
302
303 if (lock->waiters_leftmost == &waiter->tree_entry)
304 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
305
306 rb_erase(&waiter->tree_entry, &lock->waiters);
307 RB_CLEAR_NODE(&waiter->tree_entry);
308}
309
310static void
311rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
312{
313 struct rb_node **link = &task->pi_waiters.rb_node;
314 struct rb_node *parent = NULL;
315 struct rt_mutex_waiter *entry;
316 int leftmost = 1;
317
318 while (*link) {
319 parent = *link;
320 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
321 if (rt_mutex_waiter_less(waiter, entry)) {
322 link = &parent->rb_left;
323 } else {
324 link = &parent->rb_right;
325 leftmost = 0;
326 }
327 }
328
329 if (leftmost)
330 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
331
332 rb_link_node(&waiter->pi_tree_entry, parent, link);
333 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
334}
335
336static void
337rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
338{
339 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
340 return;
341
342 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
343 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
344
345 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
346 RB_CLEAR_NODE(&waiter->pi_tree_entry);
347}
348
Peter Zijlstraacd58622017-03-23 15:56:11 +0100349static void rt_mutex_adjust_prio(struct task_struct *p)
Xunlei Pange96a77052017-03-23 15:56:08 +0100350{
Peter Zijlstraacd58622017-03-23 15:56:11 +0100351 struct task_struct *pi_task = NULL;
Xunlei Pange96a77052017-03-23 15:56:08 +0100352
Peter Zijlstraacd58622017-03-23 15:56:11 +0100353 lockdep_assert_held(&p->pi_lock);
Xunlei Pange96a77052017-03-23 15:56:08 +0100354
Peter Zijlstraacd58622017-03-23 15:56:11 +0100355 if (task_has_pi_waiters(p))
356 pi_task = task_top_pi_waiter(p)->task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700357
Peter Zijlstraacd58622017-03-23 15:56:11 +0100358 rt_mutex_setprio(p, pi_task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700359}
360
361/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000362 * Deadlock detection is conditional:
363 *
364 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
365 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
366 *
367 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
368 * conducted independent of the detect argument.
369 *
370 * If the waiter argument is NULL this indicates the deboost path and
371 * deadlock detection is disabled independent of the detect argument
372 * and the config settings.
373 */
374static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
375 enum rtmutex_chainwalk chwalk)
376{
377 /*
378 * This is just a wrapper function for the following call,
379 * because debug_rt_mutex_detect_deadlock() smells like a magic
380 * debug feature and I wanted to keep the cond function in the
381 * main source file along with the comments instead of having
382 * two of the same in the headers.
383 */
384 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
385}
386
387/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700388 * Max number of times we'll walk the boosting chain:
389 */
390int max_lock_depth = 1024;
391
Thomas Gleixner82084982014-06-05 11:16:12 +0200392static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
393{
394 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
395}
396
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700397/*
398 * Adjust the priority chain. Also used for deadlock detection.
399 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200400 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200401 * @task: the task owning the mutex (owner) for which a chain walk is
402 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900403 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200404 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
405 * things for a task that has just got its priority adjusted, and
406 * is waiting on a mutex)
407 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
408 * we dropped its pi_lock. Is never dereferenced, only used for
409 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200410 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200411 * its priority to the mutex owner (can be NULL in the case
412 * depicted above or if the top waiter is gone away and we are
413 * actually deboosting the owner)
414 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200415 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700416 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200417 *
418 * Chain walk basics and protection scope
419 *
420 * [R] refcount on task
421 * [P] task->pi_lock held
422 * [L] rtmutex->wait_lock held
423 *
424 * Step Description Protected by
425 * function arguments:
426 * @task [R]
427 * @orig_lock if != NULL @top_task is blocked on it
428 * @next_lock Unprotected. Cannot be
429 * dereferenced. Only used for
430 * comparison.
431 * @orig_waiter if != NULL @top_task is blocked on it
432 * @top_task current, or in case of proxy
433 * locking protected by calling
434 * code
435 * again:
436 * loop_sanity_check();
437 * retry:
438 * [1] lock(task->pi_lock); [R] acquire [P]
439 * [2] waiter = task->pi_blocked_on; [P]
440 * [3] check_exit_conditions_1(); [P]
441 * [4] lock = waiter->lock; [P]
442 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
443 * unlock(task->pi_lock); release [P]
444 * goto retry;
445 * }
446 * [6] check_exit_conditions_2(); [P] + [L]
447 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
448 * [8] unlock(task->pi_lock); release [P]
449 * put_task_struct(task); release [R]
450 * [9] check_exit_conditions_3(); [L]
451 * [10] task = owner(lock); [L]
452 * get_task_struct(task); [L] acquire [R]
453 * lock(task->pi_lock); [L] acquire [P]
454 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
455 * [12] check_exit_conditions_4(); [P] + [L]
456 * [13] unlock(task->pi_lock); release [P]
457 * unlock(lock->wait_lock); release [L]
458 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700459 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200460static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000461 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200462 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200463 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200464 struct rt_mutex_waiter *orig_waiter,
465 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700466{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700467 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000468 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000469 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000470 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000471 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000472 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700473
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000474 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700475
476 /*
477 * The (de)boosting is a step by step approach with a lot of
478 * pitfalls. We want this to be preemptible and we want hold a
479 * maximum of two locks per step. So we have to check
480 * carefully whether things change under us.
481 */
482 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200483 /*
484 * We limit the lock chain length for each invocation.
485 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700486 if (++depth > max_lock_depth) {
487 static int prev_max;
488
489 /*
490 * Print this only once. If the admin changes the limit,
491 * print a new message when reaching the limit again.
492 */
493 if (prev_max != max_lock_depth) {
494 prev_max = max_lock_depth;
495 printk(KERN_WARNING "Maximum lock depth %d reached "
496 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700497 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700498 }
499 put_task_struct(task);
500
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200501 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700502 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200503
504 /*
505 * We are fully preemptible here and only hold the refcount on
506 * @task. So everything can have changed under us since the
507 * caller or our own code below (goto retry/again) dropped all
508 * locks.
509 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700510 retry:
511 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200512 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700513 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100514 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700515
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200516 /*
517 * [2] Get the waiter on which @task is blocked on.
518 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700519 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200520
521 /*
522 * [3] check_exit_conditions_1() protected by task->pi_lock.
523 */
524
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700525 /*
526 * Check whether the end of the boosting chain has been
527 * reached or the state of the chain has changed while we
528 * dropped the locks.
529 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800530 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700531 goto out_unlock_pi;
532
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700533 /*
534 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800535 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700536 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800537 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700538 goto out_unlock_pi;
539
540 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200541 * We dropped all locks after taking a refcount on @task, so
542 * the task might have moved on in the lock chain or even left
543 * the chain completely and blocks now on an unrelated lock or
544 * on @orig_lock.
545 *
546 * We stored the lock on which @task was blocked in @next_lock,
547 * so we can detect the chain change.
548 */
549 if (next_lock != waiter->lock)
550 goto out_unlock_pi;
551
552 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700553 * Drop out, when the task has no waiters. Note,
554 * top_waiter can be NULL, when we are in the deboosting
555 * mode!
556 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000557 if (top_waiter) {
558 if (!task_has_pi_waiters(task))
559 goto out_unlock_pi;
560 /*
561 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000562 * are not the top pi waiter of the task. If deadlock
563 * detection is enabled we continue, but stop the
564 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000565 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000566 if (top_waiter != task_top_pi_waiter(task)) {
567 if (!detect_deadlock)
568 goto out_unlock_pi;
569 else
570 requeue = false;
571 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000572 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700573
574 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000575 * If the waiter priority is the same as the task priority
576 * then there is no further priority adjustment necessary. If
577 * deadlock detection is off, we stop the chain walk. If its
578 * enabled we continue, but stop the requeueing in the chain
579 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700580 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100581 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner67792e22014-05-22 03:25:57 +0000582 if (!detect_deadlock)
583 goto out_unlock_pi;
584 else
585 requeue = false;
586 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700587
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200588 /*
589 * [4] Get the next lock
590 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700591 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200592 /*
593 * [5] We need to trylock here as we are holding task->pi_lock,
594 * which is the reverse lock order versus the other rtmutex
595 * operations.
596 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100597 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100598 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700599 cpu_relax();
600 goto retry;
601 }
602
Thomas Gleixner397335f2014-05-22 03:25:39 +0000603 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200604 * [6] check_exit_conditions_2() protected by task->pi_lock and
605 * lock->wait_lock.
606 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000607 * Deadlock detection. If the lock is the same as the original
608 * lock which caused us to walk the lock chain or if the
609 * current lock is owned by the task which initiated the chain
610 * walk, we detected a deadlock.
611 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700612 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000613 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100614 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200615 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700616 goto out_unlock_pi;
617 }
618
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000619 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000620 * If we just follow the lock chain for deadlock detection, no
621 * need to do all the requeue operations. To avoid a truckload
622 * of conditionals around the various places below, just do the
623 * minimum chain walk checks.
624 */
625 if (!requeue) {
626 /*
627 * No requeue[7] here. Just release @task [8]
628 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100629 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000630 put_task_struct(task);
631
632 /*
633 * [9] check_exit_conditions_3 protected by lock->wait_lock.
634 * If there is no owner of the lock, end of chain.
635 */
636 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100637 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000638 return 0;
639 }
640
641 /* [10] Grab the next task, i.e. owner of @lock */
642 task = rt_mutex_owner(lock);
643 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100644 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000645
646 /*
647 * No requeue [11] here. We just do deadlock detection.
648 *
649 * [12] Store whether owner is blocked
650 * itself. Decision is made after dropping the locks
651 */
652 next_lock = task_blocked_on_lock(task);
653 /*
654 * Get the top waiter for the next iteration
655 */
656 top_waiter = rt_mutex_top_waiter(lock);
657
658 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100659 raw_spin_unlock(&task->pi_lock);
660 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000661
662 /* If owner is not blocked, end of chain. */
663 if (!next_lock)
664 goto out_put_task;
665 goto again;
666 }
667
668 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000669 * Store the current top waiter before doing the requeue
670 * operation on @lock. We need it for the boost/deboost
671 * decision below.
672 */
673 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700674
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700675 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100676 rt_mutex_dequeue(lock, waiter);
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100677
678 /*
679 * Update the waiter prio fields now that we're dequeued.
680 *
681 * These values can have changed through either:
682 *
683 * sys_sched_set_scheduler() / sys_sched_setattr()
684 *
685 * or
686 *
687 * DL CBS enforcement advancing the effective deadline.
688 *
689 * Even though pi_waiters also uses these fields, and that tree is only
690 * updated in [11], we can do this here, since we hold [L], which
691 * serializes all pi_waiters access and rb_erase() does not care about
692 * the values of the node being removed.
693 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100694 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100695 waiter->deadline = task->dl.deadline;
696
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100697 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700698
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200699 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100700 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200701 put_task_struct(task);
702
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000703 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200704 * [9] check_exit_conditions_3 protected by lock->wait_lock.
705 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000706 * We must abort the chain walk if there is no lock owner even
707 * in the dead lock detection case, as we have nothing to
708 * follow here. This is the end of the chain we are walking.
709 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800710 if (!rt_mutex_owner(lock)) {
711 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200712 * If the requeue [7] above changed the top waiter,
713 * then we need to wake the new top waiter up to try
714 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800715 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000716 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800717 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100718 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200719 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800720 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700721
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200722 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700723 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700724 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100725 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700726
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200727 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700728 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000729 /*
730 * The waiter became the new top (highest priority)
731 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700732 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000733 * and adjust the priority of the owner.
734 */
735 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100736 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100737 rt_mutex_adjust_prio(task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700738
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000739 } else if (prerequeue_top_waiter == waiter) {
740 /*
741 * The waiter was the top waiter on the lock, but is
742 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700743 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000744 * (highest priority) waiter and adjust the priority
745 * of the owner.
746 * The new top waiter is stored in @waiter so that
747 * @waiter == @top_waiter evaluates to true below and
748 * we continue to deboost the rest of the chain.
749 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100750 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700751 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100752 rt_mutex_enqueue_pi(task, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100753 rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000754 } else {
755 /*
756 * Nothing changed. No need to do any priority
757 * adjustment.
758 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700759 }
760
Thomas Gleixner82084982014-06-05 11:16:12 +0200761 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200762 * [12] check_exit_conditions_4() protected by task->pi_lock
763 * and lock->wait_lock. The actual decisions are made after we
764 * dropped the locks.
765 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200766 * Check whether the task which owns the current lock is pi
767 * blocked itself. If yes we store a pointer to the lock for
768 * the lock chain change detection above. After we dropped
769 * task->pi_lock next_lock cannot be dereferenced anymore.
770 */
771 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000772 /*
773 * Store the top waiter of @lock for the end of chain walk
774 * decision below.
775 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700776 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200777
778 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100779 raw_spin_unlock(&task->pi_lock);
780 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700781
Thomas Gleixner82084982014-06-05 11:16:12 +0200782 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200783 * Make the actual exit decisions [12], based on the stored
784 * values.
785 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200786 * We reached the end of the lock chain. Stop right here. No
787 * point to go back just to figure that out.
788 */
789 if (!next_lock)
790 goto out_put_task;
791
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000792 /*
793 * If the current waiter is not the top waiter on the lock,
794 * then we can stop the chain walk here if we are not in full
795 * deadlock detection mode.
796 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700797 if (!detect_deadlock && waiter != top_waiter)
798 goto out_put_task;
799
800 goto again;
801
802 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100803 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700804 out_put_task:
805 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700806
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700807 return ret;
808}
809
810/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700811 * Try to take an rt-mutex
812 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100813 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800814 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200815 * @lock: The lock to be acquired.
816 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700817 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200818 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700819 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800820static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200821 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700822{
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100823 lockdep_assert_held(&lock->wait_lock);
824
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700825 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200826 * Before testing whether we can acquire @lock, we set the
827 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
828 * other tasks which try to modify @lock into the slow path
829 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700830 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200831 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
832 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700833 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200834 * - There is a lock owner. The caller must fixup the
835 * transient state if it does a trylock or leaves the lock
836 * function due to a signal or timeout.
837 *
838 * - @task acquires the lock and there are no other
839 * waiters. This is undone in rt_mutex_set_owner(@task) at
840 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700841 */
842 mark_rt_mutex_waiters(lock);
843
Thomas Gleixner358c3312014-06-11 01:01:13 +0200844 /*
845 * If @lock has an owner, give up.
846 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800847 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700848 return 0;
849
Lai Jiangshan81612392011-01-14 17:09:41 +0800850 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200851 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700852 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200853 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800854 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200855 if (waiter) {
856 /*
857 * If waiter is not the highest priority waiter of
858 * @lock, give up.
859 */
860 if (waiter != rt_mutex_top_waiter(lock))
861 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800862
863 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200864 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700865 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200866 */
867 rt_mutex_dequeue(lock, waiter);
868
869 } else {
870 /*
871 * If the lock has waiters already we check whether @task is
872 * eligible to take over the lock.
873 *
874 * If there are no other waiters, @task can acquire
875 * the lock. @task->pi_blocked_on is NULL, so it does
876 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800877 */
878 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200879 /*
880 * If @task->prio is greater than or equal to
881 * the top waiter priority (kernel view),
882 * @task lost.
883 */
Peter Zijlstra19830e52017-03-23 15:56:14 +0100884 if (!rt_mutex_waiter_less(task_to_waiter(task),
885 rt_mutex_top_waiter(lock)))
Thomas Gleixner358c3312014-06-11 01:01:13 +0200886 return 0;
887
888 /*
889 * The current top waiter stays enqueued. We
890 * don't have to change anything in the lock
891 * waiters order.
892 */
893 } else {
894 /*
895 * No waiters. Take the lock without the
896 * pi_lock dance.@task->pi_blocked_on is NULL
897 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700898 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200899 */
900 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800901 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800902 }
903
Thomas Gleixner358c3312014-06-11 01:01:13 +0200904 /*
905 * Clear @task->pi_blocked_on. Requires protection by
906 * @task->pi_lock. Redundant operation for the @waiter == NULL
907 * case, but conditionals are more expensive than a redundant
908 * store.
909 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100910 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200911 task->pi_blocked_on = NULL;
912 /*
913 * Finish the lock acquisition. @task is the new owner. If
914 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700915 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200916 */
917 if (rt_mutex_has_waiters(lock))
918 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100919 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200920
921takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700922 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700923 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700924
Thomas Gleixner358c3312014-06-11 01:01:13 +0200925 /*
926 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
927 * are still waiters or clears it.
928 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800929 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700930
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700931 return 1;
932}
933
934/*
935 * Task blocks on lock.
936 *
937 * Prepare waiter and propagate pi chain
938 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100939 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700940 */
941static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
942 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700943 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000944 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700945{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700946 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700947 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200948 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700949 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700950
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100951 lockdep_assert_held(&lock->wait_lock);
952
Thomas Gleixner397335f2014-05-22 03:25:39 +0000953 /*
954 * Early deadlock detection. We really don't want the task to
955 * enqueue on itself just to untangle the mess later. It's not
956 * only an optimization. We drop the locks, so another waiter
957 * can come in before the chain walk detects the deadlock. So
958 * the other will detect the deadlock and return -EDEADLOCK,
959 * which is wrong, as the other waiter is not in a deadlock
960 * situation.
961 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200962 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000963 return -EDEADLK;
964
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100965 raw_spin_lock(&task->pi_lock);
Peter Zijlstraacd58622017-03-23 15:56:11 +0100966 rt_mutex_adjust_prio(task);
Darren Hart8dac4562009-04-03 13:40:12 -0700967 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700968 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100969 waiter->prio = task->prio;
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +0100970 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700971
972 /* Get the top priority waiter on the lock */
973 if (rt_mutex_has_waiters(lock))
974 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100975 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700976
Darren Hart8dac4562009-04-03 13:40:12 -0700977 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700978
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100979 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700980
Lai Jiangshan81612392011-01-14 17:09:41 +0800981 if (!owner)
982 return 0;
983
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100984 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700985 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100986 rt_mutex_dequeue_pi(owner, top_waiter);
987 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700988
Peter Zijlstraacd58622017-03-23 15:56:11 +0100989 rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700990 if (owner->pi_blocked_on)
991 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000992 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700993 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +0200994 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700995
Thomas Gleixner82084982014-06-05 11:16:12 +0200996 /* Store the lock on which owner is blocked or NULL */
997 next_lock = task_blocked_on_lock(owner);
998
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100999 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +02001000 /*
1001 * Even if full deadlock detection is on, if the owner is not
1002 * blocked itself, we can avoid finding this out in the chain
1003 * walk.
1004 */
1005 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001006 return 0;
1007
Steven Rostedtdb630632006-09-29 01:59:44 -07001008 /*
1009 * The owner can't disappear while holding a lock,
1010 * so the owner struct is protected by wait_lock.
1011 * Gets dropped in rt_mutex_adjust_prio_chain()!
1012 */
1013 get_task_struct(owner);
1014
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001015 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001016
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001017 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +02001018 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001019
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001020 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001021
1022 return res;
1023}
1024
1025/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001026 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001027 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001028 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001029 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001030 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001031static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1032 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001033{
1034 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001035
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001036 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001037
1038 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001039
1040 /*
Peter Zijlstraacd58622017-03-23 15:56:11 +01001041 * Remove it from current->pi_waiters and deboost.
1042 *
1043 * We must in fact deboost here in order to ensure we call
1044 * rt_mutex_setprio() to update p->pi_top_task before the
1045 * task unblocks.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001046 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001047 rt_mutex_dequeue_pi(current, waiter);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001048 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001049
Thomas Gleixner27e35712014-06-11 18:44:04 +00001050 /*
1051 * As we are waking up the top waiter, and the waiter stays
1052 * queued on the lock until it gets the lock, this lock
1053 * obviously has waiters. Just set the bit here and this has
1054 * the added benefit of forcing all new tasks into the
1055 * slow path making sure no task of lower priority than
1056 * the top waiter can steal this lock.
1057 */
1058 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001059
Peter Zijlstraacd58622017-03-23 15:56:11 +01001060 /*
1061 * We deboosted before waking the top waiter task such that we don't
1062 * run two tasks with the 'same' priority (and ensure the
1063 * p->pi_top_task pointer points to a blocked task). This however can
1064 * lead to priority inversion if we would get preempted after the
1065 * deboost but before waking our donor task, hence the preempt_disable()
1066 * before unlock.
1067 *
1068 * Pairs with preempt_enable() in rt_mutex_postunlock();
1069 */
1070 preempt_disable();
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001071 wake_q_add(wake_q, waiter->task);
Peter Zijlstraacd58622017-03-23 15:56:11 +01001072 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001073}
1074
1075/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001076 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001077 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001078 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001079 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001080 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001081static void remove_waiter(struct rt_mutex *lock,
1082 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001083{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001084 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001085 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001086 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001087
Peter Zijlstrae0aad5b2017-03-23 15:56:13 +01001088 lockdep_assert_held(&lock->wait_lock);
1089
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001090 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001091 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001092 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001093 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001094
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001095 /*
1096 * Only update priority if the waiter was the highest priority
1097 * waiter of the lock and there is an owner to update.
1098 */
1099 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001100 return;
1101
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001102 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001103
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001104 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001105
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001106 if (rt_mutex_has_waiters(lock))
1107 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001108
Peter Zijlstraacd58622017-03-23 15:56:11 +01001109 rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001110
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001111 /* Store the lock on which owner is blocked or NULL */
1112 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001113
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001114 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001115
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001116 /*
1117 * Don't walk the chain, if the owner task is not blocked
1118 * itself.
1119 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001120 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001121 return;
1122
Steven Rostedtdb630632006-09-29 01:59:44 -07001123 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1124 get_task_struct(owner);
1125
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001126 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001127
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001128 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1129 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001130
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001131 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001132}
1133
1134/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001135 * Recheck the pi chain, in case we got a priority setting
1136 *
1137 * Called from sched_setscheduler
1138 */
1139void rt_mutex_adjust_pi(struct task_struct *task)
1140{
1141 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001142 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001143 unsigned long flags;
1144
Thomas Gleixner1d615482009-11-17 14:54:03 +01001145 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001146
1147 waiter = task->pi_blocked_on;
Peter Zijlstra19830e52017-03-23 15:56:14 +01001148 if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001149 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001150 return;
1151 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001152 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001153 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001154
Steven Rostedtdb630632006-09-29 01:59:44 -07001155 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1156 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001157
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001158 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1159 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001160}
1161
Peter Zijlstra50809352017-03-22 11:35:56 +01001162void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1163{
1164 debug_rt_mutex_init_waiter(waiter);
1165 RB_CLEAR_NODE(&waiter->pi_tree_entry);
1166 RB_CLEAR_NODE(&waiter->tree_entry);
1167 waiter->task = NULL;
1168}
1169
Darren Hart8dac4562009-04-03 13:40:12 -07001170/**
1171 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1172 * @lock: the rt_mutex to take
1173 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001174 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001175 * @timeout: the pre-initialized and started timer, or NULL for none
1176 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001177 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001178 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001179 */
1180static int __sched
1181__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1182 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001183 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001184{
1185 int ret = 0;
1186
1187 for (;;) {
1188 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001189 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001190 break;
1191
1192 /*
1193 * TASK_INTERRUPTIBLE checks for signals and
1194 * timeout. Ignored otherwise.
1195 */
Steven Rostedt (VMware)4009f4b2017-01-19 11:32:34 -05001196 if (likely(state == TASK_INTERRUPTIBLE)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001197 /* Signal pending? */
1198 if (signal_pending(current))
1199 ret = -EINTR;
1200 if (timeout && !timeout->task)
1201 ret = -ETIMEDOUT;
1202 if (ret)
1203 break;
1204 }
1205
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001206 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001207
1208 debug_rt_mutex_print_deadlock(waiter);
1209
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001210 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001211
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001212 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001213 set_current_state(state);
1214 }
1215
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001216 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001217 return ret;
1218}
1219
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001220static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1221 struct rt_mutex_waiter *w)
1222{
1223 /*
1224 * If the result is not -EDEADLOCK or the caller requested
1225 * deadlock detection, nothing to do here.
1226 */
1227 if (res != -EDEADLOCK || detect_deadlock)
1228 return;
1229
1230 /*
1231 * Yell lowdly and stop the task right here.
1232 */
1233 rt_mutex_print_deadlock(w);
1234 while (1) {
1235 set_current_state(TASK_INTERRUPTIBLE);
1236 schedule();
1237 }
1238}
1239
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001240/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001241 * Slow path lock function:
1242 */
1243static int __sched
1244rt_mutex_slowlock(struct rt_mutex *lock, int state,
1245 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001246 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001247{
1248 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001249 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001250 int ret = 0;
1251
Peter Zijlstra50809352017-03-22 11:35:56 +01001252 rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001253
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001254 /*
1255 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1256 * be called in early boot if the cmpxchg() fast path is disabled
1257 * (debug, no architecture support). In this case we will acquire the
1258 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1259 * enable interrupts in that early boot case. So we need to use the
1260 * irqsave/restore variants.
1261 */
1262 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001263
1264 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001265 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001266 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001267 return 0;
1268 }
1269
1270 set_current_state(state);
1271
1272 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001273 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001274 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001275
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001276 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001277
1278 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001279 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001280 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001281
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001282 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001283 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001284 if (rt_mutex_has_waiters(lock))
1285 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001286 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001287 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001288
1289 /*
1290 * try_to_take_rt_mutex() sets the waiter bit
1291 * unconditionally. We might have to fix that up.
1292 */
1293 fixup_rt_mutex_waiters(lock);
1294
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001295 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001296
1297 /* Remove pending timer: */
1298 if (unlikely(timeout))
1299 hrtimer_cancel(&timeout->timer);
1300
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001301 debug_rt_mutex_free_waiter(&waiter);
1302
1303 return ret;
1304}
1305
1306/*
1307 * Slow path try-lock function:
1308 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001309static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001310{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001311 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001312 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001313
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001314 /*
1315 * If the lock already has an owner we fail to get the lock.
1316 * This can be done without taking the @lock->wait_lock as
1317 * it is only being read, and this is a trylock anyway.
1318 */
1319 if (rt_mutex_owner(lock))
1320 return 0;
1321
1322 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001323 * The mutex has currently no owner. Lock the wait lock and try to
1324 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001325 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001326 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001327
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001328 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001329
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001330 /*
1331 * try_to_take_rt_mutex() sets the lock waiters bit
1332 * unconditionally. Clean this up.
1333 */
1334 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001335
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001336 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001337
1338 return ret;
1339}
1340
1341/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001342 * Slow path to release a rt-mutex.
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001343 *
1344 * Return whether the current task needs to call rt_mutex_postunlock().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001345 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001346static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1347 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001348{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001349 unsigned long flags;
1350
1351 /* irqsave required to support early boot calls */
1352 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001353
1354 debug_rt_mutex_unlock(lock);
1355
Thomas Gleixner27e35712014-06-11 18:44:04 +00001356 /*
1357 * We must be careful here if the fast path is enabled. If we
1358 * have no waiters queued we cannot set owner to NULL here
1359 * because of:
1360 *
1361 * foo->lock->owner = NULL;
1362 * rtmutex_lock(foo->lock); <- fast path
1363 * free = atomic_dec_and_test(foo->refcnt);
1364 * rtmutex_unlock(foo->lock); <- fast path
1365 * if (free)
1366 * kfree(foo);
1367 * raw_spin_unlock(foo->lock->wait_lock);
1368 *
1369 * So for the fastpath enabled kernel:
1370 *
1371 * Nothing can set the waiters bit as long as we hold
1372 * lock->wait_lock. So we do the following sequence:
1373 *
1374 * owner = rt_mutex_owner(lock);
1375 * clear_rt_mutex_waiters(lock);
1376 * raw_spin_unlock(&lock->wait_lock);
1377 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1378 * return;
1379 * goto retry;
1380 *
1381 * The fastpath disabled variant is simple as all access to
1382 * lock->owner is serialized by lock->wait_lock:
1383 *
1384 * lock->owner = NULL;
1385 * raw_spin_unlock(&lock->wait_lock);
1386 */
1387 while (!rt_mutex_has_waiters(lock)) {
1388 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001389 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001390 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001391 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001392 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001393 }
1394
Thomas Gleixner27e35712014-06-11 18:44:04 +00001395 /*
1396 * The wakeup next waiter path does not suffer from the above
1397 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001398 *
1399 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001400 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001401 mark_wakeup_next_waiter(wake_q, lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001402 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001403
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001404 return true; /* call rt_mutex_postunlock() */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001405}
1406
1407/*
1408 * debug aware fast / slowpath lock,trylock,unlock
1409 *
1410 * The atomic acquire/release ops are compiled away, when either the
1411 * architecture does not support cmpxchg or when debugging is enabled.
1412 */
1413static inline int
1414rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001415 int (*slowfn)(struct rt_mutex *lock, int state,
1416 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001417 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001418{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001419 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001420 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001421
1422 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001423}
1424
1425static inline int
1426rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001427 struct hrtimer_sleeper *timeout,
1428 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001429 int (*slowfn)(struct rt_mutex *lock, int state,
1430 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001431 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001432{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001433 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001434 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001435 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001436
1437 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001438}
1439
1440static inline int
1441rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001442 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001443{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001444 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001445 return 1;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001446
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001447 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001448}
1449
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001450/*
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001451 * Performs the wakeup of the the top-waiter and re-enables preemption.
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001452 */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001453void rt_mutex_postunlock(struct wake_q_head *wake_q)
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001454{
1455 wake_up_q(wake_q);
1456
1457 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001458 preempt_enable();
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001459}
1460
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001461static inline void
1462rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001463 bool (*slowfn)(struct rt_mutex *lock,
1464 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001465{
Waiman Long194a6b52016-11-17 11:46:38 -05001466 DEFINE_WAKE_Q(wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001467
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001468 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1469 return;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001470
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001471 if (slowfn(lock, &wake_q))
1472 rt_mutex_postunlock(&wake_q);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001473}
1474
1475/**
1476 * rt_mutex_lock - lock a rt_mutex
1477 *
1478 * @lock: the rt_mutex to be locked
1479 */
1480void __sched rt_mutex_lock(struct rt_mutex *lock)
1481{
1482 might_sleep();
1483
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001484 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001485}
1486EXPORT_SYMBOL_GPL(rt_mutex_lock);
1487
1488/**
1489 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1490 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001491 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001492 *
1493 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001494 * 0 on success
1495 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001496 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001497int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001498{
1499 might_sleep();
1500
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001501 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001502}
1503EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1504
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001505/*
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001506 * Futex variant, must not use fastpath.
1507 */
1508int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1509{
1510 return rt_mutex_slowtrylock(lock);
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001511}
1512
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001513/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001514 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1515 * the timeout structure is provided
1516 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001517 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001518 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001519 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001520 *
1521 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001522 * 0 on success
1523 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001524 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001525 */
1526int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001527rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001528{
1529 might_sleep();
1530
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001531 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1532 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001533 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001534}
1535EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1536
1537/**
1538 * rt_mutex_trylock - try to lock a rt_mutex
1539 *
1540 * @lock: the rt_mutex to be locked
1541 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001542 * This function can only be called in thread context. It's safe to
1543 * call it from atomic regions, but not from hard interrupt or soft
1544 * interrupt context.
1545 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001546 * Returns 1 on success and 0 on contention
1547 */
1548int __sched rt_mutex_trylock(struct rt_mutex *lock)
1549{
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001550 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001551 return 0;
1552
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001553 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1554}
1555EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1556
1557/**
1558 * rt_mutex_unlock - unlock a rt_mutex
1559 *
1560 * @lock: the rt_mutex to be unlocked
1561 */
1562void __sched rt_mutex_unlock(struct rt_mutex *lock)
1563{
1564 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1565}
1566EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1567
Luis Henriques23b94b92009-04-29 21:54:51 +01001568/**
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001569 * Futex variant, that since futex variants do not use the fast-path, can be
1570 * simple and will not need to retry.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001571 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001572bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1573 struct wake_q_head *wake_q)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001574{
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001575 lockdep_assert_held(&lock->wait_lock);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001576
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001577 debug_rt_mutex_unlock(lock);
1578
1579 if (!rt_mutex_has_waiters(lock)) {
1580 lock->owner = NULL;
1581 return false; /* done */
1582 }
1583
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001584 /*
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001585 * We've already deboosted, mark_wakeup_next_waiter() will
1586 * retain preempt_disabled when we drop the wait_lock, to
1587 * avoid inversion prior to the wakeup. preempt_disable()
1588 * therein pairs with rt_mutex_postunlock().
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001589 */
Mike Galbraithdef34ea2017-04-05 10:08:27 +02001590 mark_wakeup_next_waiter(wake_q, lock);
Xunlei Pang2a1c6022017-03-23 15:56:07 +01001591
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001592 return true; /* call postunlock() */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001593}
1594
1595void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1596{
1597 DEFINE_WAKE_Q(wake_q);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001598 bool postunlock;
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001599
1600 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001601 postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001602 raw_spin_unlock_irq(&lock->wait_lock);
1603
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001604 if (postunlock)
1605 rt_mutex_postunlock(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001606}
1607
1608/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001609 * rt_mutex_destroy - mark a mutex unusable
1610 * @lock: the mutex to be destroyed
1611 *
1612 * This function marks the mutex uninitialized, and any subsequent
1613 * use of the mutex is forbidden. The mutex must not be locked when
1614 * this function is called.
1615 */
1616void rt_mutex_destroy(struct rt_mutex *lock)
1617{
1618 WARN_ON(rt_mutex_is_locked(lock));
1619#ifdef CONFIG_DEBUG_RT_MUTEXES
1620 lock->magic = NULL;
1621#endif
1622}
1623
1624EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1625
1626/**
1627 * __rt_mutex_init - initialize the rt lock
1628 *
1629 * @lock: the rt lock to be initialized
1630 *
1631 * Initialize the rt lock to unlocked state.
1632 *
1633 * Initializing of a locked rt lock is not allowed
1634 */
1635void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1636{
1637 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001638 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001639 lock->waiters = RB_ROOT;
1640 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001641
1642 debug_rt_mutex_init(lock, name);
1643}
1644EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001645
1646/**
1647 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1648 * proxy owner
1649 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001650 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001651 * @proxy_owner:the task to set as owner
1652 *
1653 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001654 *
1655 * Special API call for PI-futex support. This initializes the rtmutex and
1656 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1657 * possible at this point because the pi_state which contains the rtmutex
1658 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001659 */
1660void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1661 struct task_struct *proxy_owner)
1662{
1663 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001664 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001665 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001666}
1667
1668/**
1669 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1670 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001671 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001672 *
1673 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001674 *
1675 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1676 * (debugging) state. Concurrent operations on this rt_mutex are not
1677 * possible because it belongs to the pi_state which is about to be freed
1678 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001679 */
1680void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1681 struct task_struct *proxy_owner)
1682{
1683 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001684 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001685}
1686
Peter Zijlstra56222b22017-03-22 11:36:00 +01001687int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1688 struct rt_mutex_waiter *waiter,
1689 struct task_struct *task)
1690{
1691 int ret;
1692
1693 if (try_to_take_rt_mutex(lock, task, NULL))
1694 return 1;
1695
1696 /* We enforce deadlock detection for futexes */
1697 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1698 RT_MUTEX_FULL_CHAINWALK);
1699
1700 if (ret && !rt_mutex_owner(lock)) {
1701 /*
1702 * Reset the return value. We might have
1703 * returned with -EDEADLK and the owner
1704 * released the lock while we were walking the
1705 * pi chain. Let the waiter sort it out.
1706 */
1707 ret = 0;
1708 }
1709
1710 if (unlikely(ret))
1711 remove_waiter(lock, waiter);
1712
1713 debug_rt_mutex_print_deadlock(waiter);
1714
1715 return ret;
1716}
1717
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001718/**
Darren Hart8dac4562009-04-03 13:40:12 -07001719 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1720 * @lock: the rt_mutex to take
1721 * @waiter: the pre-initialized rt_mutex_waiter
1722 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001723 *
1724 * Returns:
1725 * 0 - task blocked on lock
1726 * 1 - acquired the lock for task, caller should wake it up
1727 * <0 - error
1728 *
1729 * Special API call for FUTEX_REQUEUE_PI support.
1730 */
1731int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1732 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001733 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001734{
1735 int ret;
1736
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001737 raw_spin_lock_irq(&lock->wait_lock);
Peter Zijlstra56222b22017-03-22 11:36:00 +01001738 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001739 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001740
Darren Hart8dac4562009-04-03 13:40:12 -07001741 return ret;
1742}
1743
1744/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001745 * rt_mutex_next_owner - return the next owner of the lock
1746 *
1747 * @lock: the rt lock query
1748 *
1749 * Returns the next owner of the lock or NULL
1750 *
1751 * Caller has to serialize against other accessors to the lock
1752 * itself.
1753 *
1754 * Special API call for PI-futex support
1755 */
1756struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1757{
1758 if (!rt_mutex_has_waiters(lock))
1759 return NULL;
1760
1761 return rt_mutex_top_waiter(lock)->task;
1762}
Darren Hart8dac4562009-04-03 13:40:12 -07001763
1764/**
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001765 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
Darren Hart8dac4562009-04-03 13:40:12 -07001766 * @lock: the rt_mutex we were woken on
1767 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001768 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001769 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001770 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001771 * Wait for the the lock acquisition started on our behalf by
1772 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1773 * rt_mutex_cleanup_proxy_lock().
Darren Hart8dac4562009-04-03 13:40:12 -07001774 *
1775 * Returns:
1776 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001777 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001778 *
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001779 * Special API call for PI-futex support
Darren Hart8dac4562009-04-03 13:40:12 -07001780 */
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001781int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
Darren Hart8dac4562009-04-03 13:40:12 -07001782 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001783 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001784{
1785 int ret;
1786
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001787 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001788
1789 set_current_state(TASK_INTERRUPTIBLE);
1790
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001791 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001792 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001793
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001794 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001795
Darren Hart8dac4562009-04-03 13:40:12 -07001796 return ret;
1797}
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001798
1799/**
1800 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1801 * @lock: the rt_mutex we were woken on
1802 * @waiter: the pre-initialized rt_mutex_waiter
1803 *
1804 * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
1805 *
1806 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1807 * in fact still be granted ownership until we're removed. Therefore we can
1808 * find we are in fact the owner and must disregard the
1809 * rt_mutex_wait_proxy_lock() failure.
1810 *
1811 * Returns:
1812 * true - did the cleanup, we done.
1813 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1814 * caller should disregards its return value.
1815 *
1816 * Special API call for PI-futex support
1817 */
1818bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1819 struct rt_mutex_waiter *waiter)
1820{
1821 bool cleanup = false;
1822
1823 raw_spin_lock_irq(&lock->wait_lock);
1824 /*
1825 * Unless we're the owner; we're still enqueued on the wait_list.
1826 * So check if we became owner, if not, take us off the wait_list.
1827 */
1828 if (rt_mutex_owner(lock) != current) {
1829 remove_waiter(lock, waiter);
1830 fixup_rt_mutex_waiters(lock);
1831 cleanup = true;
1832 }
Peter Zijlstracfafcd12017-03-22 11:35:58 +01001833
1834 /*
1835 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1836 * have to fix that up.
1837 */
1838 fixup_rt_mutex_waiters(lock);
1839
Peter Zijlstra38d589f2017-03-22 11:35:57 +01001840 raw_spin_unlock_irq(&lock->wait_lock);
1841
1842 return cleanup;
1843}