blob: 7d63bc5dd9b2ee733b68e79c683c853a655d82fb [file] [log] [blame]
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001/*
2 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
3 *
4 * started by Ingo Molnar and Thomas Gleixner.
5 *
6 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
8 * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
9 * Copyright (C) 2006 Esben Nielsen
Steven Rostedtd07fe82c22006-07-30 03:04:03 -070010 *
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -070011 * See Documentation/locking/rt-mutex-design.txt for details.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070012 */
13#include <linux/spinlock.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060016#include <linux/sched/rt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010017#include <linux/sched/deadline.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010018#include <linux/sched/wake_q.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010019#include <linux/sched/debug.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070020#include <linux/timer.h>
21
22#include "rtmutex_common.h"
23
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070024/*
25 * lock->owner state tracking:
26 *
Lai Jiangshan81612392011-01-14 17:09:41 +080027 * lock->owner holds the task_struct pointer of the owner. Bit 0
28 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070029 *
Lai Jiangshan81612392011-01-14 17:09:41 +080030 * owner bit0
31 * NULL 0 lock is free (fast acquire possible)
32 * NULL 1 lock is free and has waiters and the top waiter
33 * is going to take the lock*
34 * taskpointer 0 lock is held (fast release possible)
35 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070036 *
37 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080038 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070039 *
Lai Jiangshan81612392011-01-14 17:09:41 +080040 * (*) It also can be a transitional state when grabbing the lock
41 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
42 * we need to set the bit0 before looking at the lock, and the owner may be
43 * NULL in this small time, hence this can be a transitional state.
44 *
45 * (**) There is a small time when bit 0 is set but there are no
46 * waiters. This can happen when grabbing the lock in the slow path.
47 * To prevent a cmpxchg of the owner releasing the lock, we need to
48 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070049 */
50
Thomas Gleixnerbd197232007-06-17 21:11:10 +020051static void
Lai Jiangshan81612392011-01-14 17:09:41 +080052rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070053{
Lai Jiangshan81612392011-01-14 17:09:41 +080054 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070055
56 if (rt_mutex_has_waiters(lock))
57 val |= RT_MUTEX_HAS_WAITERS;
58
59 lock->owner = (struct task_struct *)val;
60}
61
62static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
63{
64 lock->owner = (struct task_struct *)
65 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
66}
67
68static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
69{
Thomas Gleixnerdbb26052016-11-30 21:04:41 +000070 unsigned long owner, *p = (unsigned long *) &lock->owner;
71
72 if (rt_mutex_has_waiters(lock))
73 return;
74
75 /*
76 * The rbtree has no waiters enqueued, now make sure that the
77 * lock->owner still has the waiters bit set, otherwise the
78 * following can happen:
79 *
80 * CPU 0 CPU 1 CPU2
81 * l->owner=T1
82 * rt_mutex_lock(l)
83 * lock(l->lock)
84 * l->owner = T1 | HAS_WAITERS;
85 * enqueue(T2)
86 * boost()
87 * unlock(l->lock)
88 * block()
89 *
90 * rt_mutex_lock(l)
91 * lock(l->lock)
92 * l->owner = T1 | HAS_WAITERS;
93 * enqueue(T3)
94 * boost()
95 * unlock(l->lock)
96 * block()
97 * signal(->T2) signal(->T3)
98 * lock(l->lock)
99 * dequeue(T2)
100 * deboost()
101 * unlock(l->lock)
102 * lock(l->lock)
103 * dequeue(T3)
104 * ==> wait list is empty
105 * deboost()
106 * unlock(l->lock)
107 * lock(l->lock)
108 * fixup_rt_mutex_waiters()
109 * if (wait_list_empty(l) {
110 * l->owner = owner
111 * owner = l->owner & ~HAS_WAITERS;
112 * ==> l->owner = T1
113 * }
114 * lock(l->lock)
115 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
116 * if (wait_list_empty(l) {
117 * owner = l->owner & ~HAS_WAITERS;
118 * cmpxchg(l->owner, T1, NULL)
119 * ===> Success (l->owner = NULL)
120 *
121 * l->owner = owner
122 * ==> l->owner = T1
123 * }
124 *
125 * With the check for the waiter bit in place T3 on CPU2 will not
126 * overwrite. All tasks fiddling with the waiters bit are
127 * serialized by l->lock, so nothing else can modify the waiters
128 * bit. If the bit is set then nothing can change l->owner either
129 * so the simple RMW is safe. The cmpxchg() will simply fail if it
130 * happens in the middle of the RMW because the waiters bit is
131 * still set.
132 */
133 owner = READ_ONCE(*p);
134 if (owner & RT_MUTEX_HAS_WAITERS)
135 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700136}
137
138/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100139 * We can speed up the acquire/release, if there's no debugging state to be
140 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200141 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100142#ifndef CONFIG_DEBUG_RT_MUTEXES
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700143# define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
144# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
145# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
146
147/*
148 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
149 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
150 * relaxed semantics suffice.
151 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200152static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
153{
154 unsigned long owner, *p = (unsigned long *) &lock->owner;
155
156 do {
157 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700158 } while (cmpxchg_relaxed(p, owner,
159 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200160}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000161
162/*
163 * Safe fastpath aware unlock:
164 * 1) Clear the waiters bit
165 * 2) Drop lock->wait_lock
166 * 3) Try to unlock the lock with cmpxchg
167 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100168static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
169 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000170 __releases(lock->wait_lock)
171{
172 struct task_struct *owner = rt_mutex_owner(lock);
173
174 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100175 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000176 /*
177 * If a new waiter comes in between the unlock and the cmpxchg
178 * we have two situations:
179 *
180 * unlock(wait_lock);
181 * lock(wait_lock);
182 * cmpxchg(p, owner, 0) == owner
183 * mark_rt_mutex_waiters(lock);
184 * acquire(lock);
185 * or:
186 *
187 * unlock(wait_lock);
188 * lock(wait_lock);
189 * mark_rt_mutex_waiters(lock);
190 *
191 * cmpxchg(p, owner, 0) != owner
192 * enqueue_waiter();
193 * unlock(wait_lock);
194 * lock(wait_lock);
195 * wake waiter();
196 * unlock(wait_lock);
197 * lock(wait_lock);
198 * acquire(lock);
199 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700200 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000201}
202
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200203#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700204# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
205# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
206# define rt_mutex_cmpxchg_release(l,c,n) (0)
207
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200208static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
209{
210 lock->owner = (struct task_struct *)
211 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
212}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000213
214/*
215 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
216 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100217static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
218 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000219 __releases(lock->wait_lock)
220{
221 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100222 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000223 return true;
224}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200225#endif
226
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100227static inline int
228rt_mutex_waiter_less(struct rt_mutex_waiter *left,
229 struct rt_mutex_waiter *right)
230{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100231 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100232 return 1;
233
234 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100235 * If both waiters have dl_prio(), we check the deadlines of the
236 * associated tasks.
237 * If left waiter has a dl_prio(), and we didn't return 1 above,
238 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100239 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100240 if (dl_prio(left->prio))
Juri Lellif5240572015-09-02 11:01:35 +0100241 return dl_time_before(left->task->dl.deadline,
242 right->task->dl.deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100243
244 return 0;
245}
246
247static void
248rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
249{
250 struct rb_node **link = &lock->waiters.rb_node;
251 struct rb_node *parent = NULL;
252 struct rt_mutex_waiter *entry;
253 int leftmost = 1;
254
255 while (*link) {
256 parent = *link;
257 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
258 if (rt_mutex_waiter_less(waiter, entry)) {
259 link = &parent->rb_left;
260 } else {
261 link = &parent->rb_right;
262 leftmost = 0;
263 }
264 }
265
266 if (leftmost)
267 lock->waiters_leftmost = &waiter->tree_entry;
268
269 rb_link_node(&waiter->tree_entry, parent, link);
270 rb_insert_color(&waiter->tree_entry, &lock->waiters);
271}
272
273static void
274rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
275{
276 if (RB_EMPTY_NODE(&waiter->tree_entry))
277 return;
278
279 if (lock->waiters_leftmost == &waiter->tree_entry)
280 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
281
282 rb_erase(&waiter->tree_entry, &lock->waiters);
283 RB_CLEAR_NODE(&waiter->tree_entry);
284}
285
286static void
287rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
288{
289 struct rb_node **link = &task->pi_waiters.rb_node;
290 struct rb_node *parent = NULL;
291 struct rt_mutex_waiter *entry;
292 int leftmost = 1;
293
294 while (*link) {
295 parent = *link;
296 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
297 if (rt_mutex_waiter_less(waiter, entry)) {
298 link = &parent->rb_left;
299 } else {
300 link = &parent->rb_right;
301 leftmost = 0;
302 }
303 }
304
305 if (leftmost)
306 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
307
308 rb_link_node(&waiter->pi_tree_entry, parent, link);
309 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
310}
311
312static void
313rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
314{
315 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
316 return;
317
318 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
319 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
320
321 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
322 RB_CLEAR_NODE(&waiter->pi_tree_entry);
323}
324
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200325/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100326 * Calculate task priority from the waiter tree priority
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700327 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100328 * Return task->normal_prio when the waiter tree is empty or when
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700329 * the waiter is not allowed to do priority boosting
330 */
331int rt_mutex_getprio(struct task_struct *task)
332{
333 if (likely(!task_has_pi_waiters(task)))
334 return task->normal_prio;
335
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100336 return min(task_top_pi_waiter(task)->prio,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700337 task->normal_prio);
338}
339
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100340struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
341{
342 if (likely(!task_has_pi_waiters(task)))
343 return NULL;
344
345 return task_top_pi_waiter(task)->task;
346}
347
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700348/*
Thomas Gleixner0782e632015-05-05 19:49:49 +0200349 * Called by sched_setscheduler() to get the priority which will be
350 * effective after the change.
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100351 */
Thomas Gleixner0782e632015-05-05 19:49:49 +0200352int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100353{
354 if (!task_has_pi_waiters(task))
Thomas Gleixner0782e632015-05-05 19:49:49 +0200355 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100356
Thomas Gleixner0782e632015-05-05 19:49:49 +0200357 if (task_top_pi_waiter(task)->task->prio <= newprio)
358 return task_top_pi_waiter(task)->task->prio;
359 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100360}
361
362/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700363 * Adjust the priority of a task, after its pi_waiters got modified.
364 *
365 * This can be both boosting and unboosting. task->pi_lock must be held.
366 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200367static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700368{
369 int prio = rt_mutex_getprio(task);
370
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100371 if (task->prio != prio || dl_prio(prio))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700372 rt_mutex_setprio(task, prio);
373}
374
375/*
376 * Adjust task priority (undo boosting). Called from the exit path of
377 * rt_mutex_slowunlock() and rt_mutex_slowlock().
378 *
379 * (Note: We do this outside of the protection of lock->wait_lock to
380 * allow the lock to be taken while or before we readjust the priority
381 * of task. We do not use the spin_xx_mutex() variants here as we are
382 * outside of the debug path.)
383 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +0200384void rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700385{
386 unsigned long flags;
387
Thomas Gleixner1d615482009-11-17 14:54:03 +0100388 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700389 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100390 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700391}
392
393/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000394 * Deadlock detection is conditional:
395 *
396 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
397 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
398 *
399 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
400 * conducted independent of the detect argument.
401 *
402 * If the waiter argument is NULL this indicates the deboost path and
403 * deadlock detection is disabled independent of the detect argument
404 * and the config settings.
405 */
406static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
407 enum rtmutex_chainwalk chwalk)
408{
409 /*
410 * This is just a wrapper function for the following call,
411 * because debug_rt_mutex_detect_deadlock() smells like a magic
412 * debug feature and I wanted to keep the cond function in the
413 * main source file along with the comments instead of having
414 * two of the same in the headers.
415 */
416 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
417}
418
419/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700420 * Max number of times we'll walk the boosting chain:
421 */
422int max_lock_depth = 1024;
423
Thomas Gleixner82084982014-06-05 11:16:12 +0200424static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
425{
426 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
427}
428
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700429/*
430 * Adjust the priority chain. Also used for deadlock detection.
431 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200432 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200433 * @task: the task owning the mutex (owner) for which a chain walk is
434 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900435 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200436 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
437 * things for a task that has just got its priority adjusted, and
438 * is waiting on a mutex)
439 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
440 * we dropped its pi_lock. Is never dereferenced, only used for
441 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200442 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200443 * its priority to the mutex owner (can be NULL in the case
444 * depicted above or if the top waiter is gone away and we are
445 * actually deboosting the owner)
446 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200447 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700448 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200449 *
450 * Chain walk basics and protection scope
451 *
452 * [R] refcount on task
453 * [P] task->pi_lock held
454 * [L] rtmutex->wait_lock held
455 *
456 * Step Description Protected by
457 * function arguments:
458 * @task [R]
459 * @orig_lock if != NULL @top_task is blocked on it
460 * @next_lock Unprotected. Cannot be
461 * dereferenced. Only used for
462 * comparison.
463 * @orig_waiter if != NULL @top_task is blocked on it
464 * @top_task current, or in case of proxy
465 * locking protected by calling
466 * code
467 * again:
468 * loop_sanity_check();
469 * retry:
470 * [1] lock(task->pi_lock); [R] acquire [P]
471 * [2] waiter = task->pi_blocked_on; [P]
472 * [3] check_exit_conditions_1(); [P]
473 * [4] lock = waiter->lock; [P]
474 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
475 * unlock(task->pi_lock); release [P]
476 * goto retry;
477 * }
478 * [6] check_exit_conditions_2(); [P] + [L]
479 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
480 * [8] unlock(task->pi_lock); release [P]
481 * put_task_struct(task); release [R]
482 * [9] check_exit_conditions_3(); [L]
483 * [10] task = owner(lock); [L]
484 * get_task_struct(task); [L] acquire [R]
485 * lock(task->pi_lock); [L] acquire [P]
486 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
487 * [12] check_exit_conditions_4(); [P] + [L]
488 * [13] unlock(task->pi_lock); release [P]
489 * unlock(lock->wait_lock); release [L]
490 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700491 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200492static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000493 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200494 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200495 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200496 struct rt_mutex_waiter *orig_waiter,
497 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700498{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700499 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000500 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000501 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000502 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000503 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000504 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700505
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000506 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700507
508 /*
509 * The (de)boosting is a step by step approach with a lot of
510 * pitfalls. We want this to be preemptible and we want hold a
511 * maximum of two locks per step. So we have to check
512 * carefully whether things change under us.
513 */
514 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200515 /*
516 * We limit the lock chain length for each invocation.
517 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700518 if (++depth > max_lock_depth) {
519 static int prev_max;
520
521 /*
522 * Print this only once. If the admin changes the limit,
523 * print a new message when reaching the limit again.
524 */
525 if (prev_max != max_lock_depth) {
526 prev_max = max_lock_depth;
527 printk(KERN_WARNING "Maximum lock depth %d reached "
528 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700529 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700530 }
531 put_task_struct(task);
532
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200533 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700534 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200535
536 /*
537 * We are fully preemptible here and only hold the refcount on
538 * @task. So everything can have changed under us since the
539 * caller or our own code below (goto retry/again) dropped all
540 * locks.
541 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700542 retry:
543 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200544 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700545 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100546 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700547
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200548 /*
549 * [2] Get the waiter on which @task is blocked on.
550 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700551 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200552
553 /*
554 * [3] check_exit_conditions_1() protected by task->pi_lock.
555 */
556
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700557 /*
558 * Check whether the end of the boosting chain has been
559 * reached or the state of the chain has changed while we
560 * dropped the locks.
561 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800562 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700563 goto out_unlock_pi;
564
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700565 /*
566 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800567 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700568 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800569 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700570 goto out_unlock_pi;
571
572 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200573 * We dropped all locks after taking a refcount on @task, so
574 * the task might have moved on in the lock chain or even left
575 * the chain completely and blocks now on an unrelated lock or
576 * on @orig_lock.
577 *
578 * We stored the lock on which @task was blocked in @next_lock,
579 * so we can detect the chain change.
580 */
581 if (next_lock != waiter->lock)
582 goto out_unlock_pi;
583
584 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700585 * Drop out, when the task has no waiters. Note,
586 * top_waiter can be NULL, when we are in the deboosting
587 * mode!
588 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000589 if (top_waiter) {
590 if (!task_has_pi_waiters(task))
591 goto out_unlock_pi;
592 /*
593 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000594 * are not the top pi waiter of the task. If deadlock
595 * detection is enabled we continue, but stop the
596 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000597 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000598 if (top_waiter != task_top_pi_waiter(task)) {
599 if (!detect_deadlock)
600 goto out_unlock_pi;
601 else
602 requeue = false;
603 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000604 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700605
606 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000607 * If the waiter priority is the same as the task priority
608 * then there is no further priority adjustment necessary. If
609 * deadlock detection is off, we stop the chain walk. If its
610 * enabled we continue, but stop the requeueing in the chain
611 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700612 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000613 if (waiter->prio == task->prio) {
614 if (!detect_deadlock)
615 goto out_unlock_pi;
616 else
617 requeue = false;
618 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700619
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200620 /*
621 * [4] Get the next lock
622 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700623 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200624 /*
625 * [5] We need to trylock here as we are holding task->pi_lock,
626 * which is the reverse lock order versus the other rtmutex
627 * operations.
628 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100629 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100630 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700631 cpu_relax();
632 goto retry;
633 }
634
Thomas Gleixner397335f2014-05-22 03:25:39 +0000635 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200636 * [6] check_exit_conditions_2() protected by task->pi_lock and
637 * lock->wait_lock.
638 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000639 * Deadlock detection. If the lock is the same as the original
640 * lock which caused us to walk the lock chain or if the
641 * current lock is owned by the task which initiated the chain
642 * walk, we detected a deadlock.
643 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700644 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000645 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100646 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200647 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700648 goto out_unlock_pi;
649 }
650
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000651 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000652 * If we just follow the lock chain for deadlock detection, no
653 * need to do all the requeue operations. To avoid a truckload
654 * of conditionals around the various places below, just do the
655 * minimum chain walk checks.
656 */
657 if (!requeue) {
658 /*
659 * No requeue[7] here. Just release @task [8]
660 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100661 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000662 put_task_struct(task);
663
664 /*
665 * [9] check_exit_conditions_3 protected by lock->wait_lock.
666 * If there is no owner of the lock, end of chain.
667 */
668 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100669 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000670 return 0;
671 }
672
673 /* [10] Grab the next task, i.e. owner of @lock */
674 task = rt_mutex_owner(lock);
675 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100676 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000677
678 /*
679 * No requeue [11] here. We just do deadlock detection.
680 *
681 * [12] Store whether owner is blocked
682 * itself. Decision is made after dropping the locks
683 */
684 next_lock = task_blocked_on_lock(task);
685 /*
686 * Get the top waiter for the next iteration
687 */
688 top_waiter = rt_mutex_top_waiter(lock);
689
690 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100691 raw_spin_unlock(&task->pi_lock);
692 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000693
694 /* If owner is not blocked, end of chain. */
695 if (!next_lock)
696 goto out_put_task;
697 goto again;
698 }
699
700 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000701 * Store the current top waiter before doing the requeue
702 * operation on @lock. We need it for the boost/deboost
703 * decision below.
704 */
705 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700706
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700707 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100708 rt_mutex_dequeue(lock, waiter);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100709 waiter->prio = task->prio;
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100710 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700711
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200712 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100713 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200714 put_task_struct(task);
715
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000716 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200717 * [9] check_exit_conditions_3 protected by lock->wait_lock.
718 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000719 * We must abort the chain walk if there is no lock owner even
720 * in the dead lock detection case, as we have nothing to
721 * follow here. This is the end of the chain we are walking.
722 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800723 if (!rt_mutex_owner(lock)) {
724 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200725 * If the requeue [7] above changed the top waiter,
726 * then we need to wake the new top waiter up to try
727 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800728 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000729 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800730 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100731 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200732 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800733 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700734
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200735 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700736 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700737 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100738 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700739
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200740 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700741 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000742 /*
743 * The waiter became the new top (highest priority)
744 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700745 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000746 * and adjust the priority of the owner.
747 */
748 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100749 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700750 __rt_mutex_adjust_prio(task);
751
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000752 } else if (prerequeue_top_waiter == waiter) {
753 /*
754 * The waiter was the top waiter on the lock, but is
755 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700756 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000757 * (highest priority) waiter and adjust the priority
758 * of the owner.
759 * The new top waiter is stored in @waiter so that
760 * @waiter == @top_waiter evaluates to true below and
761 * we continue to deboost the rest of the chain.
762 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100763 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700764 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100765 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700766 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000767 } else {
768 /*
769 * Nothing changed. No need to do any priority
770 * adjustment.
771 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700772 }
773
Thomas Gleixner82084982014-06-05 11:16:12 +0200774 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200775 * [12] check_exit_conditions_4() protected by task->pi_lock
776 * and lock->wait_lock. The actual decisions are made after we
777 * dropped the locks.
778 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200779 * Check whether the task which owns the current lock is pi
780 * blocked itself. If yes we store a pointer to the lock for
781 * the lock chain change detection above. After we dropped
782 * task->pi_lock next_lock cannot be dereferenced anymore.
783 */
784 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000785 /*
786 * Store the top waiter of @lock for the end of chain walk
787 * decision below.
788 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700789 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200790
791 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100792 raw_spin_unlock(&task->pi_lock);
793 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700794
Thomas Gleixner82084982014-06-05 11:16:12 +0200795 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200796 * Make the actual exit decisions [12], based on the stored
797 * values.
798 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200799 * We reached the end of the lock chain. Stop right here. No
800 * point to go back just to figure that out.
801 */
802 if (!next_lock)
803 goto out_put_task;
804
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000805 /*
806 * If the current waiter is not the top waiter on the lock,
807 * then we can stop the chain walk here if we are not in full
808 * deadlock detection mode.
809 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700810 if (!detect_deadlock && waiter != top_waiter)
811 goto out_put_task;
812
813 goto again;
814
815 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100816 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700817 out_put_task:
818 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700819
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700820 return ret;
821}
822
823/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700824 * Try to take an rt-mutex
825 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100826 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800827 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200828 * @lock: The lock to be acquired.
829 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700830 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200831 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700832 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800833static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200834 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700835{
836 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200837 * Before testing whether we can acquire @lock, we set the
838 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
839 * other tasks which try to modify @lock into the slow path
840 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700841 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200842 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
843 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700844 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200845 * - There is a lock owner. The caller must fixup the
846 * transient state if it does a trylock or leaves the lock
847 * function due to a signal or timeout.
848 *
849 * - @task acquires the lock and there are no other
850 * waiters. This is undone in rt_mutex_set_owner(@task) at
851 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700852 */
853 mark_rt_mutex_waiters(lock);
854
Thomas Gleixner358c3312014-06-11 01:01:13 +0200855 /*
856 * If @lock has an owner, give up.
857 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800858 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700859 return 0;
860
Lai Jiangshan81612392011-01-14 17:09:41 +0800861 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200862 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700863 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200864 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800865 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200866 if (waiter) {
867 /*
868 * If waiter is not the highest priority waiter of
869 * @lock, give up.
870 */
871 if (waiter != rt_mutex_top_waiter(lock))
872 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800873
874 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200875 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700876 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200877 */
878 rt_mutex_dequeue(lock, waiter);
879
880 } else {
881 /*
882 * If the lock has waiters already we check whether @task is
883 * eligible to take over the lock.
884 *
885 * If there are no other waiters, @task can acquire
886 * the lock. @task->pi_blocked_on is NULL, so it does
887 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800888 */
889 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200890 /*
891 * If @task->prio is greater than or equal to
892 * the top waiter priority (kernel view),
893 * @task lost.
894 */
895 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
896 return 0;
897
898 /*
899 * The current top waiter stays enqueued. We
900 * don't have to change anything in the lock
901 * waiters order.
902 */
903 } else {
904 /*
905 * No waiters. Take the lock without the
906 * pi_lock dance.@task->pi_blocked_on is NULL
907 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700908 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200909 */
910 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800911 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800912 }
913
Thomas Gleixner358c3312014-06-11 01:01:13 +0200914 /*
915 * Clear @task->pi_blocked_on. Requires protection by
916 * @task->pi_lock. Redundant operation for the @waiter == NULL
917 * case, but conditionals are more expensive than a redundant
918 * store.
919 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100920 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200921 task->pi_blocked_on = NULL;
922 /*
923 * Finish the lock acquisition. @task is the new owner. If
924 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700925 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200926 */
927 if (rt_mutex_has_waiters(lock))
928 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100929 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200930
931takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700932 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700933 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700934
Thomas Gleixner358c3312014-06-11 01:01:13 +0200935 /*
936 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
937 * are still waiters or clears it.
938 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800939 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700940
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700941 return 1;
942}
943
944/*
945 * Task blocks on lock.
946 *
947 * Prepare waiter and propagate pi chain
948 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100949 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700950 */
951static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
952 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700953 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000954 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700955{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700956 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700957 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200958 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700959 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700960
Thomas Gleixner397335f2014-05-22 03:25:39 +0000961 /*
962 * Early deadlock detection. We really don't want the task to
963 * enqueue on itself just to untangle the mess later. It's not
964 * only an optimization. We drop the locks, so another waiter
965 * can come in before the chain walk detects the deadlock. So
966 * the other will detect the deadlock and return -EDEADLOCK,
967 * which is wrong, as the other waiter is not in a deadlock
968 * situation.
969 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200970 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000971 return -EDEADLK;
972
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100973 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700974 __rt_mutex_adjust_prio(task);
975 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700976 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100977 waiter->prio = task->prio;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700978
979 /* Get the top priority waiter on the lock */
980 if (rt_mutex_has_waiters(lock))
981 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100982 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700983
Darren Hart8dac4562009-04-03 13:40:12 -0700984 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700985
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100986 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700987
Lai Jiangshan81612392011-01-14 17:09:41 +0800988 if (!owner)
989 return 0;
990
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100991 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700992 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100993 rt_mutex_dequeue_pi(owner, top_waiter);
994 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700995
996 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700997 if (owner->pi_blocked_on)
998 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000999 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -07001000 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +02001001 }
Steven Rostedtdb630632006-09-29 01:59:44 -07001002
Thomas Gleixner82084982014-06-05 11:16:12 +02001003 /* Store the lock on which owner is blocked or NULL */
1004 next_lock = task_blocked_on_lock(owner);
1005
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001006 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +02001007 /*
1008 * Even if full deadlock detection is on, if the owner is not
1009 * blocked itself, we can avoid finding this out in the chain
1010 * walk.
1011 */
1012 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001013 return 0;
1014
Steven Rostedtdb630632006-09-29 01:59:44 -07001015 /*
1016 * The owner can't disappear while holding a lock,
1017 * so the owner struct is protected by wait_lock.
1018 * Gets dropped in rt_mutex_adjust_prio_chain()!
1019 */
1020 get_task_struct(owner);
1021
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001022 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001023
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001024 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +02001025 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001026
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001027 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001028
1029 return res;
1030}
1031
1032/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001033 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001034 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001035 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001036 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001037 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001038static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1039 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001040{
1041 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001042
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001043 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001044
1045 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001046
1047 /*
1048 * Remove it from current->pi_waiters. We do not adjust a
1049 * possible priority boost right now. We execute wakeup in the
1050 * boosted mode and go back to normal after releasing
1051 * lock->wait_lock.
1052 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001053 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001054
Thomas Gleixner27e35712014-06-11 18:44:04 +00001055 /*
1056 * As we are waking up the top waiter, and the waiter stays
1057 * queued on the lock until it gets the lock, this lock
1058 * obviously has waiters. Just set the bit here and this has
1059 * the added benefit of forcing all new tasks into the
1060 * slow path making sure no task of lower priority than
1061 * the top waiter can steal this lock.
1062 */
1063 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001064
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001065 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001066
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001067 wake_q_add(wake_q, waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001068}
1069
1070/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001071 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001072 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001073 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001074 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001075 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001076static void remove_waiter(struct rt_mutex *lock,
1077 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001078{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001079 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001080 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001081 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001082
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001083 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001084 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001085 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001086 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001087
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001088 /*
1089 * Only update priority if the waiter was the highest priority
1090 * waiter of the lock and there is an owner to update.
1091 */
1092 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001093 return;
1094
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001095 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001096
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001097 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001098
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001099 if (rt_mutex_has_waiters(lock))
1100 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001101
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001102 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001103
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001104 /* Store the lock on which owner is blocked or NULL */
1105 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001106
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001107 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001108
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001109 /*
1110 * Don't walk the chain, if the owner task is not blocked
1111 * itself.
1112 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001113 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001114 return;
1115
Steven Rostedtdb630632006-09-29 01:59:44 -07001116 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1117 get_task_struct(owner);
1118
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001119 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001120
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001121 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1122 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001123
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001124 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001125}
1126
1127/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001128 * Recheck the pi chain, in case we got a priority setting
1129 *
1130 * Called from sched_setscheduler
1131 */
1132void rt_mutex_adjust_pi(struct task_struct *task)
1133{
1134 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001135 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001136 unsigned long flags;
1137
Thomas Gleixner1d615482009-11-17 14:54:03 +01001138 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001139
1140 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001141 if (!waiter || (waiter->prio == task->prio &&
1142 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001143 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001144 return;
1145 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001146 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001147 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001148
Steven Rostedtdb630632006-09-29 01:59:44 -07001149 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1150 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001151
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001152 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1153 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001154}
1155
Darren Hart8dac4562009-04-03 13:40:12 -07001156/**
1157 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1158 * @lock: the rt_mutex to take
1159 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001160 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001161 * @timeout: the pre-initialized and started timer, or NULL for none
1162 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001163 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001164 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001165 */
1166static int __sched
1167__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1168 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001169 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001170{
1171 int ret = 0;
1172
1173 for (;;) {
1174 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001175 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001176 break;
1177
1178 /*
1179 * TASK_INTERRUPTIBLE checks for signals and
1180 * timeout. Ignored otherwise.
1181 */
Steven Rostedt (VMware)4009f4b2017-01-19 11:32:34 -05001182 if (likely(state == TASK_INTERRUPTIBLE)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001183 /* Signal pending? */
1184 if (signal_pending(current))
1185 ret = -EINTR;
1186 if (timeout && !timeout->task)
1187 ret = -ETIMEDOUT;
1188 if (ret)
1189 break;
1190 }
1191
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001192 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001193
1194 debug_rt_mutex_print_deadlock(waiter);
1195
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001196 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001197
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001198 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001199 set_current_state(state);
1200 }
1201
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001202 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001203 return ret;
1204}
1205
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001206static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1207 struct rt_mutex_waiter *w)
1208{
1209 /*
1210 * If the result is not -EDEADLOCK or the caller requested
1211 * deadlock detection, nothing to do here.
1212 */
1213 if (res != -EDEADLOCK || detect_deadlock)
1214 return;
1215
1216 /*
1217 * Yell lowdly and stop the task right here.
1218 */
1219 rt_mutex_print_deadlock(w);
1220 while (1) {
1221 set_current_state(TASK_INTERRUPTIBLE);
1222 schedule();
1223 }
1224}
1225
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001226/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001227 * Slow path lock function:
1228 */
1229static int __sched
1230rt_mutex_slowlock(struct rt_mutex *lock, int state,
1231 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001232 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001233{
1234 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001235 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001236 int ret = 0;
1237
1238 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001239 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1240 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001241
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001242 /*
1243 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1244 * be called in early boot if the cmpxchg() fast path is disabled
1245 * (debug, no architecture support). In this case we will acquire the
1246 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1247 * enable interrupts in that early boot case. So we need to use the
1248 * irqsave/restore variants.
1249 */
1250 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001251
1252 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001253 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001254 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001255 return 0;
1256 }
1257
1258 set_current_state(state);
1259
1260 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001261 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001262 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001263
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001264 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001265
1266 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001267 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001268 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001269
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001270 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001271 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001272 if (rt_mutex_has_waiters(lock))
1273 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001274 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001275 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001276
1277 /*
1278 * try_to_take_rt_mutex() sets the waiter bit
1279 * unconditionally. We might have to fix that up.
1280 */
1281 fixup_rt_mutex_waiters(lock);
1282
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001283 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001284
1285 /* Remove pending timer: */
1286 if (unlikely(timeout))
1287 hrtimer_cancel(&timeout->timer);
1288
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001289 debug_rt_mutex_free_waiter(&waiter);
1290
1291 return ret;
1292}
1293
1294/*
1295 * Slow path try-lock function:
1296 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001297static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001298{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001299 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001300 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001301
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001302 /*
1303 * If the lock already has an owner we fail to get the lock.
1304 * This can be done without taking the @lock->wait_lock as
1305 * it is only being read, and this is a trylock anyway.
1306 */
1307 if (rt_mutex_owner(lock))
1308 return 0;
1309
1310 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001311 * The mutex has currently no owner. Lock the wait lock and try to
1312 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001313 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001314 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001315
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001316 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001317
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001318 /*
1319 * try_to_take_rt_mutex() sets the lock waiters bit
1320 * unconditionally. Clean this up.
1321 */
1322 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001323
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001324 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001325
1326 return ret;
1327}
1328
1329/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001330 * Slow path to release a rt-mutex.
1331 * Return whether the current task needs to undo a potential priority boosting.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001332 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001333static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1334 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001335{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001336 unsigned long flags;
1337
1338 /* irqsave required to support early boot calls */
1339 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001340
1341 debug_rt_mutex_unlock(lock);
1342
Thomas Gleixner27e35712014-06-11 18:44:04 +00001343 /*
1344 * We must be careful here if the fast path is enabled. If we
1345 * have no waiters queued we cannot set owner to NULL here
1346 * because of:
1347 *
1348 * foo->lock->owner = NULL;
1349 * rtmutex_lock(foo->lock); <- fast path
1350 * free = atomic_dec_and_test(foo->refcnt);
1351 * rtmutex_unlock(foo->lock); <- fast path
1352 * if (free)
1353 * kfree(foo);
1354 * raw_spin_unlock(foo->lock->wait_lock);
1355 *
1356 * So for the fastpath enabled kernel:
1357 *
1358 * Nothing can set the waiters bit as long as we hold
1359 * lock->wait_lock. So we do the following sequence:
1360 *
1361 * owner = rt_mutex_owner(lock);
1362 * clear_rt_mutex_waiters(lock);
1363 * raw_spin_unlock(&lock->wait_lock);
1364 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1365 * return;
1366 * goto retry;
1367 *
1368 * The fastpath disabled variant is simple as all access to
1369 * lock->owner is serialized by lock->wait_lock:
1370 *
1371 * lock->owner = NULL;
1372 * raw_spin_unlock(&lock->wait_lock);
1373 */
1374 while (!rt_mutex_has_waiters(lock)) {
1375 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001376 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001377 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001378 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001379 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001380 }
1381
Thomas Gleixner27e35712014-06-11 18:44:04 +00001382 /*
1383 * The wakeup next waiter path does not suffer from the above
1384 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001385 *
1386 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001387 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001388 mark_wakeup_next_waiter(wake_q, lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001389
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001390 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001391
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001392 /* check PI boosting */
1393 return true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001394}
1395
1396/*
1397 * debug aware fast / slowpath lock,trylock,unlock
1398 *
1399 * The atomic acquire/release ops are compiled away, when either the
1400 * architecture does not support cmpxchg or when debugging is enabled.
1401 */
1402static inline int
1403rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001404 int (*slowfn)(struct rt_mutex *lock, int state,
1405 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001406 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001407{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001408 if (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, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001412}
1413
1414static inline int
1415rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001416 struct hrtimer_sleeper *timeout,
1417 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001418 int (*slowfn)(struct rt_mutex *lock, int state,
1419 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001420 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001421{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001422 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001423 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001424 return 0;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001425
1426 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001427}
1428
1429static inline int
1430rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001431 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001432{
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001433 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001434 return 1;
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001435
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001436 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001437}
1438
1439static inline void
1440rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001441 bool (*slowfn)(struct rt_mutex *lock,
1442 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001443{
Waiman Long194a6b52016-11-17 11:46:38 -05001444 DEFINE_WAKE_Q(wake_q);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001445 bool deboost;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001446
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001447 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1448 return;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001449
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001450 deboost = slowfn(lock, &wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001451
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001452 wake_up_q(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001453
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001454 /* Undo pi boosting if necessary: */
1455 if (deboost)
1456 rt_mutex_adjust_prio(current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001457}
1458
1459/**
1460 * rt_mutex_lock - lock a rt_mutex
1461 *
1462 * @lock: the rt_mutex to be locked
1463 */
1464void __sched rt_mutex_lock(struct rt_mutex *lock)
1465{
1466 might_sleep();
1467
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001468 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001469}
1470EXPORT_SYMBOL_GPL(rt_mutex_lock);
1471
1472/**
1473 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1474 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001475 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001476 *
1477 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001478 * 0 on success
1479 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001480 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001481int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001482{
1483 might_sleep();
1484
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001485 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001486}
1487EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1488
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001489/*
1490 * Futex variant with full deadlock detection.
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001491 * Futex variants must not use the fast-path, see __rt_mutex_futex_unlock().
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001492 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001493int __sched rt_mutex_timed_futex_lock(struct rt_mutex *lock,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001494 struct hrtimer_sleeper *timeout)
1495{
1496 might_sleep();
1497
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001498 return rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE,
1499 timeout, RT_MUTEX_FULL_CHAINWALK);
1500}
1501
1502/*
1503 * Futex variant, must not use fastpath.
1504 */
1505int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
1506{
1507 return rt_mutex_slowtrylock(lock);
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001508}
1509
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001510/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001511 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1512 * the timeout structure is provided
1513 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001514 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001515 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001516 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001517 *
1518 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001519 * 0 on success
1520 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001521 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001522 */
1523int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001524rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001525{
1526 might_sleep();
1527
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001528 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1529 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001530 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001531}
1532EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1533
1534/**
1535 * rt_mutex_trylock - try to lock a rt_mutex
1536 *
1537 * @lock: the rt_mutex to be locked
1538 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001539 * This function can only be called in thread context. It's safe to
1540 * call it from atomic regions, but not from hard interrupt or soft
1541 * interrupt context.
1542 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001543 * Returns 1 on success and 0 on contention
1544 */
1545int __sched rt_mutex_trylock(struct rt_mutex *lock)
1546{
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001547 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001548 return 0;
1549
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001550 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1551}
1552EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1553
1554/**
1555 * rt_mutex_unlock - unlock a rt_mutex
1556 *
1557 * @lock: the rt_mutex to be unlocked
1558 */
1559void __sched rt_mutex_unlock(struct rt_mutex *lock)
1560{
1561 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1562}
1563EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1564
Luis Henriques23b94b92009-04-29 21:54:51 +01001565/**
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001566 * Futex variant, that since futex variants do not use the fast-path, can be
1567 * simple and will not need to retry.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001568 */
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001569bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1570 struct wake_q_head *wake_q)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001571{
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001572 lockdep_assert_held(&lock->wait_lock);
Peter Zijlstrafffa9542017-03-22 11:35:50 +01001573
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001574 debug_rt_mutex_unlock(lock);
1575
1576 if (!rt_mutex_has_waiters(lock)) {
1577 lock->owner = NULL;
1578 return false; /* done */
1579 }
1580
1581 mark_wakeup_next_waiter(wake_q, lock);
1582 return true; /* deboost and wakeups */
1583}
1584
1585void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
1586{
1587 DEFINE_WAKE_Q(wake_q);
1588 bool deboost;
1589
1590 raw_spin_lock_irq(&lock->wait_lock);
1591 deboost = __rt_mutex_futex_unlock(lock, &wake_q);
1592 raw_spin_unlock_irq(&lock->wait_lock);
1593
1594 if (deboost) {
1595 wake_up_q(&wake_q);
1596 rt_mutex_adjust_prio(current);
1597 }
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001598}
1599
1600/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001601 * rt_mutex_destroy - mark a mutex unusable
1602 * @lock: the mutex to be destroyed
1603 *
1604 * This function marks the mutex uninitialized, and any subsequent
1605 * use of the mutex is forbidden. The mutex must not be locked when
1606 * this function is called.
1607 */
1608void rt_mutex_destroy(struct rt_mutex *lock)
1609{
1610 WARN_ON(rt_mutex_is_locked(lock));
1611#ifdef CONFIG_DEBUG_RT_MUTEXES
1612 lock->magic = NULL;
1613#endif
1614}
1615
1616EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1617
1618/**
1619 * __rt_mutex_init - initialize the rt lock
1620 *
1621 * @lock: the rt lock to be initialized
1622 *
1623 * Initialize the rt lock to unlocked state.
1624 *
1625 * Initializing of a locked rt lock is not allowed
1626 */
1627void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1628{
1629 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001630 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001631 lock->waiters = RB_ROOT;
1632 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001633
1634 debug_rt_mutex_init(lock, name);
1635}
1636EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001637
1638/**
1639 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1640 * proxy owner
1641 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001642 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001643 * @proxy_owner:the task to set as owner
1644 *
1645 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001646 *
1647 * Special API call for PI-futex support. This initializes the rtmutex and
1648 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
1649 * possible at this point because the pi_state which contains the rtmutex
1650 * is not yet visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001651 */
1652void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1653 struct task_struct *proxy_owner)
1654{
1655 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001656 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001657 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001658}
1659
1660/**
1661 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1662 *
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001663 * @lock: the rt_mutex to be locked
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001664 *
1665 * No locking. Caller has to do serializing itself
Thomas Gleixner84d82ec2016-11-30 21:04:45 +00001666 *
1667 * Special API call for PI-futex support. This merrily cleans up the rtmutex
1668 * (debugging) state. Concurrent operations on this rt_mutex are not
1669 * possible because it belongs to the pi_state which is about to be freed
1670 * and it is not longer visible to other tasks.
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001671 */
1672void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1673 struct task_struct *proxy_owner)
1674{
1675 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001676 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001677}
1678
1679/**
Darren Hart8dac4562009-04-03 13:40:12 -07001680 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1681 * @lock: the rt_mutex to take
1682 * @waiter: the pre-initialized rt_mutex_waiter
1683 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001684 *
1685 * Returns:
1686 * 0 - task blocked on lock
1687 * 1 - acquired the lock for task, caller should wake it up
1688 * <0 - error
1689 *
1690 * Special API call for FUTEX_REQUEUE_PI support.
1691 */
1692int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1693 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001694 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001695{
1696 int ret;
1697
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001698 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001699
Lai Jiangshan81612392011-01-14 17:09:41 +08001700 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001701 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001702 return 1;
1703 }
1704
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001705 /* We enforce deadlock detection for futexes */
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001706 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1707 RT_MUTEX_FULL_CHAINWALK);
Darren Hart8dac4562009-04-03 13:40:12 -07001708
Lai Jiangshan81612392011-01-14 17:09:41 +08001709 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001710 /*
1711 * Reset the return value. We might have
1712 * returned with -EDEADLK and the owner
1713 * released the lock while we were walking the
1714 * pi chain. Let the waiter sort it out.
1715 */
1716 ret = 0;
1717 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001718
1719 if (unlikely(ret))
1720 remove_waiter(lock, waiter);
1721
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001722 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001723
1724 debug_rt_mutex_print_deadlock(waiter);
1725
1726 return ret;
1727}
1728
1729/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001730 * rt_mutex_next_owner - return the next owner of the lock
1731 *
1732 * @lock: the rt lock query
1733 *
1734 * Returns the next owner of the lock or NULL
1735 *
1736 * Caller has to serialize against other accessors to the lock
1737 * itself.
1738 *
1739 * Special API call for PI-futex support
1740 */
1741struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1742{
1743 if (!rt_mutex_has_waiters(lock))
1744 return NULL;
1745
1746 return rt_mutex_top_waiter(lock)->task;
1747}
Darren Hart8dac4562009-04-03 13:40:12 -07001748
1749/**
1750 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1751 * @lock: the rt_mutex we were woken on
1752 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001753 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001754 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001755 *
1756 * Complete the lock acquisition started our behalf by another thread.
1757 *
1758 * Returns:
1759 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001760 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001761 *
1762 * Special API call for PI-futex requeue support
1763 */
1764int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1765 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001766 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001767{
1768 int ret;
1769
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001770 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001771
1772 set_current_state(TASK_INTERRUPTIBLE);
1773
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001774 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001775 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001776
Lai Jiangshan81612392011-01-14 17:09:41 +08001777 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001778 remove_waiter(lock, waiter);
1779
1780 /*
1781 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1782 * have to fix that up.
1783 */
1784 fixup_rt_mutex_waiters(lock);
1785
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001786 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001787
Darren Hart8dac4562009-04-03 13:40:12 -07001788 return ret;
1789}