blob: 7615e7722258cfd24c4173816f449a20917b6fb4 [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 Rostedtd07fe822006-07-30 03:04:03 -070010 *
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -070011 * See Documentation/locking/rt-mutex-design.txt for details.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070012 */
13#include <linux/spinlock.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070015#include <linux/sched.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060016#include <linux/sched/rt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010017#include <linux/sched/deadline.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070018#include <linux/timer.h>
19
20#include "rtmutex_common.h"
21
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070022/*
23 * lock->owner state tracking:
24 *
Lai Jiangshan81612392011-01-14 17:09:41 +080025 * lock->owner holds the task_struct pointer of the owner. Bit 0
26 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070027 *
Lai Jiangshan81612392011-01-14 17:09:41 +080028 * owner bit0
29 * NULL 0 lock is free (fast acquire possible)
30 * NULL 1 lock is free and has waiters and the top waiter
31 * is going to take the lock*
32 * taskpointer 0 lock is held (fast release possible)
33 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070034 *
35 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080036 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070037 *
Lai Jiangshan81612392011-01-14 17:09:41 +080038 * (*) It also can be a transitional state when grabbing the lock
39 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
40 * we need to set the bit0 before looking at the lock, and the owner may be
41 * NULL in this small time, hence this can be a transitional state.
42 *
43 * (**) There is a small time when bit 0 is set but there are no
44 * waiters. This can happen when grabbing the lock in the slow path.
45 * To prevent a cmpxchg of the owner releasing the lock, we need to
46 * set this bit before looking at the lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070047 */
48
Thomas Gleixnerbd197232007-06-17 21:11:10 +020049static void
Lai Jiangshan81612392011-01-14 17:09:41 +080050rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070051{
Lai Jiangshan81612392011-01-14 17:09:41 +080052 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070053
54 if (rt_mutex_has_waiters(lock))
55 val |= RT_MUTEX_HAS_WAITERS;
56
57 lock->owner = (struct task_struct *)val;
58}
59
60static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
61{
62 lock->owner = (struct task_struct *)
63 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
64}
65
66static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
67{
Thomas Gleixnerdbb26052016-11-30 21:04:41 +000068 unsigned long owner, *p = (unsigned long *) &lock->owner;
69
70 if (rt_mutex_has_waiters(lock))
71 return;
72
73 /*
74 * The rbtree has no waiters enqueued, now make sure that the
75 * lock->owner still has the waiters bit set, otherwise the
76 * following can happen:
77 *
78 * CPU 0 CPU 1 CPU2
79 * l->owner=T1
80 * rt_mutex_lock(l)
81 * lock(l->lock)
82 * l->owner = T1 | HAS_WAITERS;
83 * enqueue(T2)
84 * boost()
85 * unlock(l->lock)
86 * block()
87 *
88 * rt_mutex_lock(l)
89 * lock(l->lock)
90 * l->owner = T1 | HAS_WAITERS;
91 * enqueue(T3)
92 * boost()
93 * unlock(l->lock)
94 * block()
95 * signal(->T2) signal(->T3)
96 * lock(l->lock)
97 * dequeue(T2)
98 * deboost()
99 * unlock(l->lock)
100 * lock(l->lock)
101 * dequeue(T3)
102 * ==> wait list is empty
103 * deboost()
104 * unlock(l->lock)
105 * lock(l->lock)
106 * fixup_rt_mutex_waiters()
107 * if (wait_list_empty(l) {
108 * l->owner = owner
109 * owner = l->owner & ~HAS_WAITERS;
110 * ==> l->owner = T1
111 * }
112 * lock(l->lock)
113 * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
114 * if (wait_list_empty(l) {
115 * owner = l->owner & ~HAS_WAITERS;
116 * cmpxchg(l->owner, T1, NULL)
117 * ===> Success (l->owner = NULL)
118 *
119 * l->owner = owner
120 * ==> l->owner = T1
121 * }
122 *
123 * With the check for the waiter bit in place T3 on CPU2 will not
124 * overwrite. All tasks fiddling with the waiters bit are
125 * serialized by l->lock, so nothing else can modify the waiters
126 * bit. If the bit is set then nothing can change l->owner either
127 * so the simple RMW is safe. The cmpxchg() will simply fail if it
128 * happens in the middle of the RMW because the waiters bit is
129 * still set.
130 */
131 owner = READ_ONCE(*p);
132 if (owner & RT_MUTEX_HAS_WAITERS)
133 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700134}
135
136/*
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100137 * We can speed up the acquire/release, if there's no debugging state to be
138 * set up.
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200139 */
Sebastian Andrzej Siewiorcede8842015-02-25 18:56:13 +0100140#ifndef CONFIG_DEBUG_RT_MUTEXES
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700141# define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
142# define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
143# define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
144
145/*
146 * Callers must hold the ->wait_lock -- which is the whole purpose as we force
147 * all future threads that attempt to [Rmw] the lock to the slowpath. As such
148 * relaxed semantics suffice.
149 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200150static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
151{
152 unsigned long owner, *p = (unsigned long *) &lock->owner;
153
154 do {
155 owner = *p;
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700156 } while (cmpxchg_relaxed(p, owner,
157 owner | RT_MUTEX_HAS_WAITERS) != owner);
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200158}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000159
160/*
161 * Safe fastpath aware unlock:
162 * 1) Clear the waiters bit
163 * 2) Drop lock->wait_lock
164 * 3) Try to unlock the lock with cmpxchg
165 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100166static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
167 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000168 __releases(lock->wait_lock)
169{
170 struct task_struct *owner = rt_mutex_owner(lock);
171
172 clear_rt_mutex_waiters(lock);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100173 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000174 /*
175 * If a new waiter comes in between the unlock and the cmpxchg
176 * we have two situations:
177 *
178 * unlock(wait_lock);
179 * lock(wait_lock);
180 * cmpxchg(p, owner, 0) == owner
181 * mark_rt_mutex_waiters(lock);
182 * acquire(lock);
183 * or:
184 *
185 * unlock(wait_lock);
186 * lock(wait_lock);
187 * mark_rt_mutex_waiters(lock);
188 *
189 * cmpxchg(p, owner, 0) != owner
190 * enqueue_waiter();
191 * unlock(wait_lock);
192 * lock(wait_lock);
193 * wake waiter();
194 * unlock(wait_lock);
195 * lock(wait_lock);
196 * acquire(lock);
197 */
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700198 return rt_mutex_cmpxchg_release(lock, owner, NULL);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000199}
200
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200201#else
Davidlohr Bueso700318d2015-09-30 13:03:13 -0700202# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
203# define rt_mutex_cmpxchg_acquire(l,c,n) (0)
204# define rt_mutex_cmpxchg_release(l,c,n) (0)
205
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200206static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
207{
208 lock->owner = (struct task_struct *)
209 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
210}
Thomas Gleixner27e35712014-06-11 18:44:04 +0000211
212/*
213 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
214 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100215static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
216 unsigned long flags)
Thomas Gleixner27e35712014-06-11 18:44:04 +0000217 __releases(lock->wait_lock)
218{
219 lock->owner = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100220 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Thomas Gleixner27e35712014-06-11 18:44:04 +0000221 return true;
222}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200223#endif
224
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100225static inline int
226rt_mutex_waiter_less(struct rt_mutex_waiter *left,
227 struct rt_mutex_waiter *right)
228{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100229 if (left->prio < right->prio)
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100230 return 1;
231
232 /*
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100233 * If both waiters have dl_prio(), we check the deadlines of the
234 * associated tasks.
235 * If left waiter has a dl_prio(), and we didn't return 1 above,
236 * then right waiter has a dl_prio() too.
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100237 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100238 if (dl_prio(left->prio))
Peter Zijlstra0e6661b2017-03-23 15:56:13 +0100239 return dl_time_before(left->deadline, right->deadline);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100240
241 return 0;
242}
243
244static void
245rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
246{
247 struct rb_node **link = &lock->waiters.rb_node;
248 struct rb_node *parent = NULL;
249 struct rt_mutex_waiter *entry;
250 int leftmost = 1;
251
252 while (*link) {
253 parent = *link;
254 entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
255 if (rt_mutex_waiter_less(waiter, entry)) {
256 link = &parent->rb_left;
257 } else {
258 link = &parent->rb_right;
259 leftmost = 0;
260 }
261 }
262
263 if (leftmost)
264 lock->waiters_leftmost = &waiter->tree_entry;
265
266 rb_link_node(&waiter->tree_entry, parent, link);
267 rb_insert_color(&waiter->tree_entry, &lock->waiters);
268}
269
270static void
271rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
272{
273 if (RB_EMPTY_NODE(&waiter->tree_entry))
274 return;
275
276 if (lock->waiters_leftmost == &waiter->tree_entry)
277 lock->waiters_leftmost = rb_next(&waiter->tree_entry);
278
279 rb_erase(&waiter->tree_entry, &lock->waiters);
280 RB_CLEAR_NODE(&waiter->tree_entry);
281}
282
283static void
284rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
285{
286 struct rb_node **link = &task->pi_waiters.rb_node;
287 struct rb_node *parent = NULL;
288 struct rt_mutex_waiter *entry;
289 int leftmost = 1;
290
291 while (*link) {
292 parent = *link;
293 entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
294 if (rt_mutex_waiter_less(waiter, entry)) {
295 link = &parent->rb_left;
296 } else {
297 link = &parent->rb_right;
298 leftmost = 0;
299 }
300 }
301
302 if (leftmost)
303 task->pi_waiters_leftmost = &waiter->pi_tree_entry;
304
305 rb_link_node(&waiter->pi_tree_entry, parent, link);
306 rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
307}
308
309static void
310rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
311{
312 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
313 return;
314
315 if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
316 task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
317
318 rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
319 RB_CLEAR_NODE(&waiter->pi_tree_entry);
320}
321
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200322/*
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100323 * Calculate task priority from the waiter tree priority
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700324 *
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100325 * Return task->normal_prio when the waiter tree is empty or when
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700326 * the waiter is not allowed to do priority boosting
327 */
328int rt_mutex_getprio(struct task_struct *task)
329{
330 if (likely(!task_has_pi_waiters(task)))
331 return task->normal_prio;
332
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100333 return min(task_top_pi_waiter(task)->prio,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700334 task->normal_prio);
335}
336
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100337struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
338{
339 if (likely(!task_has_pi_waiters(task)))
340 return NULL;
341
342 return task_top_pi_waiter(task)->task;
343}
344
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700345/*
Thomas Gleixner0782e632015-05-05 19:49:49 +0200346 * Called by sched_setscheduler() to get the priority which will be
347 * effective after the change.
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100348 */
Thomas Gleixner0782e632015-05-05 19:49:49 +0200349int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100350{
351 if (!task_has_pi_waiters(task))
Thomas Gleixner0782e632015-05-05 19:49:49 +0200352 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100353
Thomas Gleixner0782e632015-05-05 19:49:49 +0200354 if (task_top_pi_waiter(task)->task->prio <= newprio)
355 return task_top_pi_waiter(task)->task->prio;
356 return newprio;
Thomas Gleixnerc365c292014-02-07 20:58:42 +0100357}
358
359/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700360 * Adjust the priority of a task, after its pi_waiters got modified.
361 *
362 * This can be both boosting and unboosting. task->pi_lock must be held.
363 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200364static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700365{
366 int prio = rt_mutex_getprio(task);
367
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100368 if (task->prio != prio || dl_prio(prio))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700369 rt_mutex_setprio(task, prio);
370}
371
372/*
373 * Adjust task priority (undo boosting). Called from the exit path of
374 * rt_mutex_slowunlock() and rt_mutex_slowlock().
375 *
376 * (Note: We do this outside of the protection of lock->wait_lock to
377 * allow the lock to be taken while or before we readjust the priority
378 * of task. We do not use the spin_xx_mutex() variants here as we are
379 * outside of the debug path.)
380 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +0200381void rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700382{
383 unsigned long flags;
384
Thomas Gleixner1d615482009-11-17 14:54:03 +0100385 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700386 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100387 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700388}
389
390/*
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000391 * Deadlock detection is conditional:
392 *
393 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
394 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
395 *
396 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
397 * conducted independent of the detect argument.
398 *
399 * If the waiter argument is NULL this indicates the deboost path and
400 * deadlock detection is disabled independent of the detect argument
401 * and the config settings.
402 */
403static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
404 enum rtmutex_chainwalk chwalk)
405{
406 /*
407 * This is just a wrapper function for the following call,
408 * because debug_rt_mutex_detect_deadlock() smells like a magic
409 * debug feature and I wanted to keep the cond function in the
410 * main source file along with the comments instead of having
411 * two of the same in the headers.
412 */
413 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
414}
415
416/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700417 * Max number of times we'll walk the boosting chain:
418 */
419int max_lock_depth = 1024;
420
Thomas Gleixner82084982014-06-05 11:16:12 +0200421static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
422{
423 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
424}
425
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700426/*
427 * Adjust the priority chain. Also used for deadlock detection.
428 * Decreases task's usage by one - may thus free the task.
Juri Lelli0c106172013-05-15 11:04:10 +0200429 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200430 * @task: the task owning the mutex (owner) for which a chain walk is
431 * probably needed
Tom(JeHyeon) Yeone6beaa362015-03-18 14:03:30 +0900432 * @chwalk: do we have to carry out deadlock detection?
Thomas Gleixner82084982014-06-05 11:16:12 +0200433 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
434 * things for a task that has just got its priority adjusted, and
435 * is waiting on a mutex)
436 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
437 * we dropped its pi_lock. Is never dereferenced, only used for
438 * comparison to detect lock chain changes.
Juri Lelli0c106172013-05-15 11:04:10 +0200439 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
Thomas Gleixner82084982014-06-05 11:16:12 +0200440 * its priority to the mutex owner (can be NULL in the case
441 * depicted above or if the top waiter is gone away and we are
442 * actually deboosting the owner)
443 * @top_task: the current top waiter
Juri Lelli0c106172013-05-15 11:04:10 +0200444 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700445 * Returns 0 or -EDEADLK.
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200446 *
447 * Chain walk basics and protection scope
448 *
449 * [R] refcount on task
450 * [P] task->pi_lock held
451 * [L] rtmutex->wait_lock held
452 *
453 * Step Description Protected by
454 * function arguments:
455 * @task [R]
456 * @orig_lock if != NULL @top_task is blocked on it
457 * @next_lock Unprotected. Cannot be
458 * dereferenced. Only used for
459 * comparison.
460 * @orig_waiter if != NULL @top_task is blocked on it
461 * @top_task current, or in case of proxy
462 * locking protected by calling
463 * code
464 * again:
465 * loop_sanity_check();
466 * retry:
467 * [1] lock(task->pi_lock); [R] acquire [P]
468 * [2] waiter = task->pi_blocked_on; [P]
469 * [3] check_exit_conditions_1(); [P]
470 * [4] lock = waiter->lock; [P]
471 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
472 * unlock(task->pi_lock); release [P]
473 * goto retry;
474 * }
475 * [6] check_exit_conditions_2(); [P] + [L]
476 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
477 * [8] unlock(task->pi_lock); release [P]
478 * put_task_struct(task); release [R]
479 * [9] check_exit_conditions_3(); [L]
480 * [10] task = owner(lock); [L]
481 * get_task_struct(task); [L] acquire [R]
482 * lock(task->pi_lock); [L] acquire [P]
483 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
484 * [12] check_exit_conditions_4(); [P] + [L]
485 * [13] unlock(task->pi_lock); release [P]
486 * unlock(lock->wait_lock); release [L]
487 * goto again;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700488 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200489static int rt_mutex_adjust_prio_chain(struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000490 enum rtmutex_chainwalk chwalk,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200491 struct rt_mutex *orig_lock,
Thomas Gleixner82084982014-06-05 11:16:12 +0200492 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200493 struct rt_mutex_waiter *orig_waiter,
494 struct task_struct *top_task)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700495{
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700496 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000497 struct rt_mutex_waiter *prerequeue_top_waiter;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000498 int ret = 0, depth = 0;
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000499 struct rt_mutex *lock;
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000500 bool detect_deadlock;
Thomas Gleixner67792e22014-05-22 03:25:57 +0000501 bool requeue = true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700502
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000503 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700504
505 /*
506 * The (de)boosting is a step by step approach with a lot of
507 * pitfalls. We want this to be preemptible and we want hold a
508 * maximum of two locks per step. So we have to check
509 * carefully whether things change under us.
510 */
511 again:
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200512 /*
513 * We limit the lock chain length for each invocation.
514 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700515 if (++depth > max_lock_depth) {
516 static int prev_max;
517
518 /*
519 * Print this only once. If the admin changes the limit,
520 * print a new message when reaching the limit again.
521 */
522 if (prev_max != max_lock_depth) {
523 prev_max = max_lock_depth;
524 printk(KERN_WARNING "Maximum lock depth %d reached "
525 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700526 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700527 }
528 put_task_struct(task);
529
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200530 return -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700531 }
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200532
533 /*
534 * We are fully preemptible here and only hold the refcount on
535 * @task. So everything can have changed under us since the
536 * caller or our own code below (goto retry/again) dropped all
537 * locks.
538 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700539 retry:
540 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200541 * [1] Task cannot go away as we did a get_task() before !
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700542 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100543 raw_spin_lock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700544
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200545 /*
546 * [2] Get the waiter on which @task is blocked on.
547 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700548 waiter = task->pi_blocked_on;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200549
550 /*
551 * [3] check_exit_conditions_1() protected by task->pi_lock.
552 */
553
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700554 /*
555 * Check whether the end of the boosting chain has been
556 * reached or the state of the chain has changed while we
557 * dropped the locks.
558 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800559 if (!waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700560 goto out_unlock_pi;
561
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700562 /*
563 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800564 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700565 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800566 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700567 goto out_unlock_pi;
568
569 /*
Thomas Gleixner82084982014-06-05 11:16:12 +0200570 * We dropped all locks after taking a refcount on @task, so
571 * the task might have moved on in the lock chain or even left
572 * the chain completely and blocks now on an unrelated lock or
573 * on @orig_lock.
574 *
575 * We stored the lock on which @task was blocked in @next_lock,
576 * so we can detect the chain change.
577 */
578 if (next_lock != waiter->lock)
579 goto out_unlock_pi;
580
581 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700582 * Drop out, when the task has no waiters. Note,
583 * top_waiter can be NULL, when we are in the deboosting
584 * mode!
585 */
Thomas Gleixner397335f2014-05-22 03:25:39 +0000586 if (top_waiter) {
587 if (!task_has_pi_waiters(task))
588 goto out_unlock_pi;
589 /*
590 * If deadlock detection is off, we stop here if we
Thomas Gleixner67792e22014-05-22 03:25:57 +0000591 * are not the top pi waiter of the task. If deadlock
592 * detection is enabled we continue, but stop the
593 * requeueing in the chain walk.
Thomas Gleixner397335f2014-05-22 03:25:39 +0000594 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000595 if (top_waiter != task_top_pi_waiter(task)) {
596 if (!detect_deadlock)
597 goto out_unlock_pi;
598 else
599 requeue = false;
600 }
Thomas Gleixner397335f2014-05-22 03:25:39 +0000601 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700602
603 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000604 * If the waiter priority is the same as the task priority
605 * then there is no further priority adjustment necessary. If
606 * deadlock detection is off, we stop the chain walk. If its
607 * enabled we continue, but stop the requeueing in the chain
608 * walk.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700609 */
Thomas Gleixner67792e22014-05-22 03:25:57 +0000610 if (waiter->prio == task->prio) {
611 if (!detect_deadlock)
612 goto out_unlock_pi;
613 else
614 requeue = false;
615 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700616
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200617 /*
618 * [4] Get the next lock
619 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700620 lock = waiter->lock;
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200621 /*
622 * [5] We need to trylock here as we are holding task->pi_lock,
623 * which is the reverse lock order versus the other rtmutex
624 * operations.
625 */
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100626 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100627 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700628 cpu_relax();
629 goto retry;
630 }
631
Thomas Gleixner397335f2014-05-22 03:25:39 +0000632 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200633 * [6] check_exit_conditions_2() protected by task->pi_lock and
634 * lock->wait_lock.
635 *
Thomas Gleixner397335f2014-05-22 03:25:39 +0000636 * Deadlock detection. If the lock is the same as the original
637 * lock which caused us to walk the lock chain or if the
638 * current lock is owned by the task which initiated the chain
639 * walk, we detected a deadlock.
640 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700641 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000642 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100643 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200644 ret = -EDEADLK;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700645 goto out_unlock_pi;
646 }
647
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000648 /*
Thomas Gleixner67792e22014-05-22 03:25:57 +0000649 * If we just follow the lock chain for deadlock detection, no
650 * need to do all the requeue operations. To avoid a truckload
651 * of conditionals around the various places below, just do the
652 * minimum chain walk checks.
653 */
654 if (!requeue) {
655 /*
656 * No requeue[7] here. Just release @task [8]
657 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100658 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000659 put_task_struct(task);
660
661 /*
662 * [9] check_exit_conditions_3 protected by lock->wait_lock.
663 * If there is no owner of the lock, end of chain.
664 */
665 if (!rt_mutex_owner(lock)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100666 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000667 return 0;
668 }
669
670 /* [10] Grab the next task, i.e. owner of @lock */
671 task = rt_mutex_owner(lock);
672 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100673 raw_spin_lock(&task->pi_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000674
675 /*
676 * No requeue [11] here. We just do deadlock detection.
677 *
678 * [12] Store whether owner is blocked
679 * itself. Decision is made after dropping the locks
680 */
681 next_lock = task_blocked_on_lock(task);
682 /*
683 * Get the top waiter for the next iteration
684 */
685 top_waiter = rt_mutex_top_waiter(lock);
686
687 /* [13] Drop locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100688 raw_spin_unlock(&task->pi_lock);
689 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner67792e22014-05-22 03:25:57 +0000690
691 /* If owner is not blocked, end of chain. */
692 if (!next_lock)
693 goto out_put_task;
694 goto again;
695 }
696
697 /*
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000698 * Store the current top waiter before doing the requeue
699 * operation on @lock. We need it for the boost/deboost
700 * decision below.
701 */
702 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700703
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700704 /* [7] Requeue the waiter in the lock waiter tree. */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100705 rt_mutex_dequeue(lock, waiter);
Peter Zijlstra0e6661b2017-03-23 15:56:13 +0100706
707 /*
708 * Update the waiter prio fields now that we're dequeued.
709 *
710 * These values can have changed through either:
711 *
712 * sys_sched_set_scheduler() / sys_sched_setattr()
713 *
714 * or
715 *
716 * DL CBS enforcement advancing the effective deadline.
717 *
718 * Even though pi_waiters also uses these fields, and that tree is only
719 * updated in [11], we can do this here, since we hold [L], which
720 * serializes all pi_waiters access and rb_erase() does not care about
721 * the values of the node being removed.
722 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100723 waiter->prio = task->prio;
Peter Zijlstra0e6661b2017-03-23 15:56:13 +0100724 waiter->deadline = task->dl.deadline;
725
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100726 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700727
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200728 /* [8] Release the task */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100729 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200730 put_task_struct(task);
731
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000732 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200733 * [9] check_exit_conditions_3 protected by lock->wait_lock.
734 *
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000735 * We must abort the chain walk if there is no lock owner even
736 * in the dead lock detection case, as we have nothing to
737 * follow here. This is the end of the chain we are walking.
738 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800739 if (!rt_mutex_owner(lock)) {
740 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200741 * If the requeue [7] above changed the top waiter,
742 * then we need to wake the new top waiter up to try
743 * to get the lock.
Lai Jiangshan81612392011-01-14 17:09:41 +0800744 */
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000745 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
Lai Jiangshan81612392011-01-14 17:09:41 +0800746 wake_up_process(rt_mutex_top_waiter(lock)->task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100747 raw_spin_unlock_irq(&lock->wait_lock);
Thomas Gleixner2ffa5a52014-06-07 12:10:36 +0200748 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800749 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700750
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200751 /* [10] Grab the next task, i.e. the owner of @lock */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700752 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700753 get_task_struct(task);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100754 raw_spin_lock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700755
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200756 /* [11] requeue the pi waiters if necessary */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700757 if (waiter == rt_mutex_top_waiter(lock)) {
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000758 /*
759 * The waiter became the new top (highest priority)
760 * waiter on the lock. Replace the previous top waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700761 * in the owner tasks pi waiters tree with this waiter
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000762 * and adjust the priority of the owner.
763 */
764 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
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);
767
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000768 } else if (prerequeue_top_waiter == waiter) {
769 /*
770 * The waiter was the top waiter on the lock, but is
771 * no longer the top prority waiter. Replace waiter in
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700772 * the owner tasks pi waiters tree with the new top
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000773 * (highest priority) waiter and adjust the priority
774 * of the owner.
775 * The new top waiter is stored in @waiter so that
776 * @waiter == @top_waiter evaluates to true below and
777 * we continue to deboost the rest of the chain.
778 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100779 rt_mutex_dequeue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700780 waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +0100781 rt_mutex_enqueue_pi(task, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700782 __rt_mutex_adjust_prio(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000783 } else {
784 /*
785 * Nothing changed. No need to do any priority
786 * adjustment.
787 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700788 }
789
Thomas Gleixner82084982014-06-05 11:16:12 +0200790 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200791 * [12] check_exit_conditions_4() protected by task->pi_lock
792 * and lock->wait_lock. The actual decisions are made after we
793 * dropped the locks.
794 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200795 * Check whether the task which owns the current lock is pi
796 * blocked itself. If yes we store a pointer to the lock for
797 * the lock chain change detection above. After we dropped
798 * task->pi_lock next_lock cannot be dereferenced anymore.
799 */
800 next_lock = task_blocked_on_lock(task);
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000801 /*
802 * Store the top waiter of @lock for the end of chain walk
803 * decision below.
804 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700805 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200806
807 /* [13] Drop the locks */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100808 raw_spin_unlock(&task->pi_lock);
809 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700810
Thomas Gleixner82084982014-06-05 11:16:12 +0200811 /*
Thomas Gleixner3eb65ae2014-06-09 19:40:34 +0200812 * Make the actual exit decisions [12], based on the stored
813 * values.
814 *
Thomas Gleixner82084982014-06-05 11:16:12 +0200815 * We reached the end of the lock chain. Stop right here. No
816 * point to go back just to figure that out.
817 */
818 if (!next_lock)
819 goto out_put_task;
820
Thomas Gleixnera57594a2014-05-22 03:25:54 +0000821 /*
822 * If the current waiter is not the top waiter on the lock,
823 * then we can stop the chain walk here if we are not in full
824 * deadlock detection mode.
825 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700826 if (!detect_deadlock && waiter != top_waiter)
827 goto out_put_task;
828
829 goto again;
830
831 out_unlock_pi:
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100832 raw_spin_unlock_irq(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700833 out_put_task:
834 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700835
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700836 return ret;
837}
838
839/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700840 * Try to take an rt-mutex
841 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100842 * Must be called with lock->wait_lock held and interrupts disabled
Lai Jiangshan81612392011-01-14 17:09:41 +0800843 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200844 * @lock: The lock to be acquired.
845 * @task: The task which wants to acquire the lock
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700846 * @waiter: The waiter that is queued to the lock's wait tree if the
Thomas Gleixner358c3312014-06-11 01:01:13 +0200847 * callsite called task_blocked_on_lock(), otherwise NULL
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700848 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800849static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
Thomas Gleixner358c3312014-06-11 01:01:13 +0200850 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700851{
Peter Zijlstra0e6661b2017-03-23 15:56:13 +0100852 lockdep_assert_held(&lock->wait_lock);
853
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700854 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200855 * Before testing whether we can acquire @lock, we set the
856 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
857 * other tasks which try to modify @lock into the slow path
858 * and they serialize on @lock->wait_lock.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700859 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200860 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
861 * as explained at the top of this file if and only if:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700862 *
Thomas Gleixner358c3312014-06-11 01:01:13 +0200863 * - There is a lock owner. The caller must fixup the
864 * transient state if it does a trylock or leaves the lock
865 * function due to a signal or timeout.
866 *
867 * - @task acquires the lock and there are no other
868 * waiters. This is undone in rt_mutex_set_owner(@task) at
869 * the end of this function.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700870 */
871 mark_rt_mutex_waiters(lock);
872
Thomas Gleixner358c3312014-06-11 01:01:13 +0200873 /*
874 * If @lock has an owner, give up.
875 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800876 if (rt_mutex_owner(lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700877 return 0;
878
Lai Jiangshan81612392011-01-14 17:09:41 +0800879 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200880 * If @waiter != NULL, @task has already enqueued the waiter
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700881 * into @lock waiter tree. If @waiter == NULL then this is a
Thomas Gleixner358c3312014-06-11 01:01:13 +0200882 * trylock attempt.
Lai Jiangshan81612392011-01-14 17:09:41 +0800883 */
Thomas Gleixner358c3312014-06-11 01:01:13 +0200884 if (waiter) {
885 /*
886 * If waiter is not the highest priority waiter of
887 * @lock, give up.
888 */
889 if (waiter != rt_mutex_top_waiter(lock))
890 return 0;
Lai Jiangshan81612392011-01-14 17:09:41 +0800891
892 /*
Thomas Gleixner358c3312014-06-11 01:01:13 +0200893 * We can acquire the lock. Remove the waiter from the
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700894 * lock waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200895 */
896 rt_mutex_dequeue(lock, waiter);
897
898 } else {
899 /*
900 * If the lock has waiters already we check whether @task is
901 * eligible to take over the lock.
902 *
903 * If there are no other waiters, @task can acquire
904 * the lock. @task->pi_blocked_on is NULL, so it does
905 * not need to be dequeued.
Lai Jiangshan81612392011-01-14 17:09:41 +0800906 */
907 if (rt_mutex_has_waiters(lock)) {
Thomas Gleixner358c3312014-06-11 01:01:13 +0200908 /*
909 * If @task->prio is greater than or equal to
910 * the top waiter priority (kernel view),
911 * @task lost.
912 */
913 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
914 return 0;
915
916 /*
917 * The current top waiter stays enqueued. We
918 * don't have to change anything in the lock
919 * waiters order.
920 */
921 } else {
922 /*
923 * No waiters. Take the lock without the
924 * pi_lock dance.@task->pi_blocked_on is NULL
925 * and we have no waiters to enqueue in @task
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700926 * pi waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200927 */
928 goto takeit;
Lai Jiangshan81612392011-01-14 17:09:41 +0800929 }
Lai Jiangshan81612392011-01-14 17:09:41 +0800930 }
931
Thomas Gleixner358c3312014-06-11 01:01:13 +0200932 /*
933 * Clear @task->pi_blocked_on. Requires protection by
934 * @task->pi_lock. Redundant operation for the @waiter == NULL
935 * case, but conditionals are more expensive than a redundant
936 * store.
937 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100938 raw_spin_lock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200939 task->pi_blocked_on = NULL;
940 /*
941 * Finish the lock acquisition. @task is the new owner. If
942 * other waiters exist we have to insert the highest priority
Davidlohr Bueso9f40a512015-05-19 10:24:57 -0700943 * waiter into @task->pi_waiters tree.
Thomas Gleixner358c3312014-06-11 01:01:13 +0200944 */
945 if (rt_mutex_has_waiters(lock))
946 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100947 raw_spin_unlock(&task->pi_lock);
Thomas Gleixner358c3312014-06-11 01:01:13 +0200948
949takeit:
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700950 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700951 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700952
Thomas Gleixner358c3312014-06-11 01:01:13 +0200953 /*
954 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
955 * are still waiters or clears it.
956 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800957 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700958
Lai Jiangshan81612392011-01-14 17:09:41 +0800959 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700960
961 return 1;
962}
963
964/*
965 * Task blocks on lock.
966 *
967 * Prepare waiter and propagate pi chain
968 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100969 * This must be called with lock->wait_lock held and interrupts disabled
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700970 */
971static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
972 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700973 struct task_struct *task,
Thomas Gleixner8930ed82014-05-22 03:25:47 +0000974 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700975{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700976 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700977 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +0200978 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700979 int chain_walk = 0, res;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700980
Peter Zijlstra0e6661b2017-03-23 15:56:13 +0100981 lockdep_assert_held(&lock->wait_lock);
982
Thomas Gleixner397335f2014-05-22 03:25:39 +0000983 /*
984 * Early deadlock detection. We really don't want the task to
985 * enqueue on itself just to untangle the mess later. It's not
986 * only an optimization. We drop the locks, so another waiter
987 * can come in before the chain walk detects the deadlock. So
988 * the other will detect the deadlock and return -EDEADLOCK,
989 * which is wrong, as the other waiter is not in a deadlock
990 * situation.
991 */
Thomas Gleixner3d5c9342014-06-05 12:34:23 +0200992 if (owner == task)
Thomas Gleixner397335f2014-05-22 03:25:39 +0000993 return -EDEADLK;
994
Thomas Gleixnerb4abf912016-01-13 11:25:38 +0100995 raw_spin_lock(&task->pi_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700996 __rt_mutex_adjust_prio(task);
997 waiter->task = task;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700998 waiter->lock = lock;
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100999 waiter->prio = task->prio;
Peter Zijlstra0e6661b2017-03-23 15:56:13 +01001000 waiter->deadline = task->dl.deadline;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001001
1002 /* Get the top priority waiter on the lock */
1003 if (rt_mutex_has_waiters(lock))
1004 top_waiter = rt_mutex_top_waiter(lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001005 rt_mutex_enqueue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001006
Darren Hart8dac4562009-04-03 13:40:12 -07001007 task->pi_blocked_on = waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001008
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001009 raw_spin_unlock(&task->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001010
Lai Jiangshan81612392011-01-14 17:09:41 +08001011 if (!owner)
1012 return 0;
1013
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001014 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001015 if (waiter == rt_mutex_top_waiter(lock)) {
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001016 rt_mutex_dequeue_pi(owner, top_waiter);
1017 rt_mutex_enqueue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001018
1019 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -07001020 if (owner->pi_blocked_on)
1021 chain_walk = 1;
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001022 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
Steven Rostedtdb630632006-09-29 01:59:44 -07001023 chain_walk = 1;
Thomas Gleixner82084982014-06-05 11:16:12 +02001024 }
Steven Rostedtdb630632006-09-29 01:59:44 -07001025
Thomas Gleixner82084982014-06-05 11:16:12 +02001026 /* Store the lock on which owner is blocked or NULL */
1027 next_lock = task_blocked_on_lock(owner);
1028
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001029 raw_spin_unlock(&owner->pi_lock);
Thomas Gleixner82084982014-06-05 11:16:12 +02001030 /*
1031 * Even if full deadlock detection is on, if the owner is not
1032 * blocked itself, we can avoid finding this out in the chain
1033 * walk.
1034 */
1035 if (!chain_walk || !next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001036 return 0;
1037
Steven Rostedtdb630632006-09-29 01:59:44 -07001038 /*
1039 * The owner can't disappear while holding a lock,
1040 * so the owner struct is protected by wait_lock.
1041 * Gets dropped in rt_mutex_adjust_prio_chain()!
1042 */
1043 get_task_struct(owner);
1044
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001045 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001046
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001047 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
Thomas Gleixner82084982014-06-05 11:16:12 +02001048 next_lock, waiter, task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001049
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001050 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001051
1052 return res;
1053}
1054
1055/*
Davidlohr Bueso9f40a512015-05-19 10:24:57 -07001056 * Remove the top waiter from the current tasks pi waiter tree and
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001057 * queue it up.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001058 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001059 * Called with lock->wait_lock held and interrupts disabled.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001060 */
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001061static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1062 struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001063{
1064 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001065
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001066 raw_spin_lock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001067
1068 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001069
1070 /*
1071 * Remove it from current->pi_waiters. We do not adjust a
1072 * possible priority boost right now. We execute wakeup in the
1073 * boosted mode and go back to normal after releasing
1074 * lock->wait_lock.
1075 */
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001076 rt_mutex_dequeue_pi(current, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001077
Thomas Gleixner27e35712014-06-11 18:44:04 +00001078 /*
1079 * As we are waking up the top waiter, and the waiter stays
1080 * queued on the lock until it gets the lock, this lock
1081 * obviously has waiters. Just set the bit here and this has
1082 * the added benefit of forcing all new tasks into the
1083 * slow path making sure no task of lower priority than
1084 * the top waiter can steal this lock.
1085 */
1086 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001087
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001088 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001089
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001090 wake_q_add(wake_q, waiter->task);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001091}
1092
1093/*
Lai Jiangshan81612392011-01-14 17:09:41 +08001094 * Remove a waiter from a lock and give up
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001095 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001096 * Must be called with lock->wait_lock held and interrupts disabled. I must
Lai Jiangshan81612392011-01-14 17:09:41 +08001097 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001098 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001099static void remove_waiter(struct rt_mutex *lock,
1100 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001101{
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001102 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -07001103 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001104 struct rt_mutex *next_lock;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001105
Peter Zijlstra0e6661b2017-03-23 15:56:13 +01001106 lockdep_assert_held(&lock->wait_lock);
1107
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001108 raw_spin_lock(&current->pi_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001109 rt_mutex_dequeue(lock, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001110 current->pi_blocked_on = NULL;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001111 raw_spin_unlock(&current->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001112
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001113 /*
1114 * Only update priority if the waiter was the highest priority
1115 * waiter of the lock and there is an owner to update.
1116 */
1117 if (!owner || !is_top_waiter)
Lai Jiangshan81612392011-01-14 17:09:41 +08001118 return;
1119
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001120 raw_spin_lock(&owner->pi_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001121
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001122 rt_mutex_dequeue_pi(owner, waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001123
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001124 if (rt_mutex_has_waiters(lock))
1125 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001126
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001127 __rt_mutex_adjust_prio(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001128
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001129 /* Store the lock on which owner is blocked or NULL */
1130 next_lock = task_blocked_on_lock(owner);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001131
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001132 raw_spin_unlock(&owner->pi_lock);
Steven Rostedtdb630632006-09-29 01:59:44 -07001133
Thomas Gleixner1ca7b862014-06-07 09:36:13 +02001134 /*
1135 * Don't walk the chain, if the owner task is not blocked
1136 * itself.
1137 */
Thomas Gleixner82084982014-06-05 11:16:12 +02001138 if (!next_lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001139 return;
1140
Steven Rostedtdb630632006-09-29 01:59:44 -07001141 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1142 get_task_struct(owner);
1143
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001144 raw_spin_unlock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001145
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001146 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1147 next_lock, NULL, current);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001148
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001149 raw_spin_lock_irq(&lock->wait_lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001150}
1151
1152/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001153 * Recheck the pi chain, in case we got a priority setting
1154 *
1155 * Called from sched_setscheduler
1156 */
1157void rt_mutex_adjust_pi(struct task_struct *task)
1158{
1159 struct rt_mutex_waiter *waiter;
Thomas Gleixner82084982014-06-05 11:16:12 +02001160 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001161 unsigned long flags;
1162
Thomas Gleixner1d615482009-11-17 14:54:03 +01001163 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001164
1165 waiter = task->pi_blocked_on;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001166 if (!waiter || (waiter->prio == task->prio &&
1167 !dl_prio(task->prio))) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001168 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001169 return;
1170 }
Thomas Gleixner82084982014-06-05 11:16:12 +02001171 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001172 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001173
Steven Rostedtdb630632006-09-29 01:59:44 -07001174 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1175 get_task_struct(task);
Thomas Gleixner82084982014-06-05 11:16:12 +02001176
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001177 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1178 next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001179}
1180
Darren Hart8dac4562009-04-03 13:40:12 -07001181/**
1182 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1183 * @lock: the rt_mutex to take
1184 * @state: the state the task should block in (TASK_INTERRUPTIBLE
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001185 * or TASK_UNINTERRUPTIBLE)
Darren Hart8dac4562009-04-03 13:40:12 -07001186 * @timeout: the pre-initialized and started timer, or NULL for none
1187 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001188 *
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001189 * Must be called with lock->wait_lock held and interrupts disabled
Darren Hart8dac4562009-04-03 13:40:12 -07001190 */
1191static int __sched
1192__rt_mutex_slowlock(struct rt_mutex *lock, int state,
1193 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +08001194 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001195{
1196 int ret = 0;
1197
1198 for (;;) {
1199 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001200 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -07001201 break;
1202
1203 /*
1204 * TASK_INTERRUPTIBLE checks for signals and
1205 * timeout. Ignored otherwise.
1206 */
1207 if (unlikely(state == TASK_INTERRUPTIBLE)) {
1208 /* Signal pending? */
1209 if (signal_pending(current))
1210 ret = -EINTR;
1211 if (timeout && !timeout->task)
1212 ret = -ETIMEDOUT;
1213 if (ret)
1214 break;
1215 }
1216
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001217 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001218
1219 debug_rt_mutex_print_deadlock(waiter);
1220
Davidlohr Bueso1b0b7c12015-07-01 13:29:48 -07001221 schedule();
Darren Hart8dac4562009-04-03 13:40:12 -07001222
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001223 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001224 set_current_state(state);
1225 }
1226
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001227 __set_current_state(TASK_RUNNING);
Darren Hart8dac4562009-04-03 13:40:12 -07001228 return ret;
1229}
1230
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001231static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1232 struct rt_mutex_waiter *w)
1233{
1234 /*
1235 * If the result is not -EDEADLOCK or the caller requested
1236 * deadlock detection, nothing to do here.
1237 */
1238 if (res != -EDEADLOCK || detect_deadlock)
1239 return;
1240
1241 /*
1242 * Yell lowdly and stop the task right here.
1243 */
1244 rt_mutex_print_deadlock(w);
1245 while (1) {
1246 set_current_state(TASK_INTERRUPTIBLE);
1247 schedule();
1248 }
1249}
1250
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07001251/*
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001252 * Slow path lock function:
1253 */
1254static int __sched
1255rt_mutex_slowlock(struct rt_mutex *lock, int state,
1256 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001257 enum rtmutex_chainwalk chwalk)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001258{
1259 struct rt_mutex_waiter waiter;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001260 unsigned long flags;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001261 int ret = 0;
1262
1263 debug_rt_mutex_init_waiter(&waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001264 RB_CLEAR_NODE(&waiter.pi_tree_entry);
1265 RB_CLEAR_NODE(&waiter.tree_entry);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001266
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001267 /*
1268 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1269 * be called in early boot if the cmpxchg() fast path is disabled
1270 * (debug, no architecture support). In this case we will acquire the
1271 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1272 * enable interrupts in that early boot case. So we need to use the
1273 * irqsave/restore variants.
1274 */
1275 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001276
1277 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +08001278 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001279 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001280 return 0;
1281 }
1282
1283 set_current_state(state);
1284
1285 /* Setup the timer, when timeout != NULL */
Thomas Gleixnerccdd92c2015-04-14 21:09:15 +00001286 if (unlikely(timeout))
Arjan van de Vencc584b22008-09-01 15:02:30 -07001287 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001288
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001289 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
Lai Jiangshan81612392011-01-14 17:09:41 +08001290
1291 if (likely(!ret))
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001292 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001293 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001294
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001295 if (unlikely(ret)) {
Sebastian Andrzej Siewior9d3e2d02015-02-27 17:57:09 +01001296 __set_current_state(TASK_RUNNING);
Sebastian Andrzej Siewior8d1e5a12015-02-17 16:43:43 +01001297 if (rt_mutex_has_waiters(lock))
1298 remove_waiter(lock, &waiter);
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001299 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001300 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001301
1302 /*
1303 * try_to_take_rt_mutex() sets the waiter bit
1304 * unconditionally. We might have to fix that up.
1305 */
1306 fixup_rt_mutex_waiters(lock);
1307
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001308 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001309
1310 /* Remove pending timer: */
1311 if (unlikely(timeout))
1312 hrtimer_cancel(&timeout->timer);
1313
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001314 debug_rt_mutex_free_waiter(&waiter);
1315
1316 return ret;
1317}
1318
1319/*
1320 * Slow path try-lock function:
1321 */
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001322static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001323{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001324 unsigned long flags;
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001325 int ret;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001326
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001327 /*
1328 * If the lock already has an owner we fail to get the lock.
1329 * This can be done without taking the @lock->wait_lock as
1330 * it is only being read, and this is a trylock anyway.
1331 */
1332 if (rt_mutex_owner(lock))
1333 return 0;
1334
1335 /*
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001336 * The mutex has currently no owner. Lock the wait lock and try to
1337 * acquire the lock. We use irqsave here to support early boot calls.
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001338 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001339 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001340
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001341 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001342
Thomas Gleixner88f2b4c2014-06-10 22:53:40 +02001343 /*
1344 * try_to_take_rt_mutex() sets the lock waiters bit
1345 * unconditionally. Clean this up.
1346 */
1347 fixup_rt_mutex_waiters(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001348
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001349 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001350
1351 return ret;
1352}
1353
1354/*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001355 * Slow path to release a rt-mutex.
1356 * Return whether the current task needs to undo a potential priority boosting.
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001357 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001358static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1359 struct wake_q_head *wake_q)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001360{
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001361 unsigned long flags;
1362
1363 /* irqsave required to support early boot calls */
1364 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001365
1366 debug_rt_mutex_unlock(lock);
1367
1368 rt_mutex_deadlock_account_unlock(current);
1369
Thomas Gleixner27e35712014-06-11 18:44:04 +00001370 /*
1371 * We must be careful here if the fast path is enabled. If we
1372 * have no waiters queued we cannot set owner to NULL here
1373 * because of:
1374 *
1375 * foo->lock->owner = NULL;
1376 * rtmutex_lock(foo->lock); <- fast path
1377 * free = atomic_dec_and_test(foo->refcnt);
1378 * rtmutex_unlock(foo->lock); <- fast path
1379 * if (free)
1380 * kfree(foo);
1381 * raw_spin_unlock(foo->lock->wait_lock);
1382 *
1383 * So for the fastpath enabled kernel:
1384 *
1385 * Nothing can set the waiters bit as long as we hold
1386 * lock->wait_lock. So we do the following sequence:
1387 *
1388 * owner = rt_mutex_owner(lock);
1389 * clear_rt_mutex_waiters(lock);
1390 * raw_spin_unlock(&lock->wait_lock);
1391 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1392 * return;
1393 * goto retry;
1394 *
1395 * The fastpath disabled variant is simple as all access to
1396 * lock->owner is serialized by lock->wait_lock:
1397 *
1398 * lock->owner = NULL;
1399 * raw_spin_unlock(&lock->wait_lock);
1400 */
1401 while (!rt_mutex_has_waiters(lock)) {
1402 /* Drops lock->wait_lock ! */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001403 if (unlock_rt_mutex_safe(lock, flags) == true)
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001404 return false;
Thomas Gleixner27e35712014-06-11 18:44:04 +00001405 /* Relock the rtmutex and try again */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001406 raw_spin_lock_irqsave(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001407 }
1408
Thomas Gleixner27e35712014-06-11 18:44:04 +00001409 /*
1410 * The wakeup next waiter path does not suffer from the above
1411 * race. See the comments there.
Davidlohr Bueso45ab4ef2015-05-19 10:24:55 -07001412 *
1413 * Queue the next waiter for wakeup once we release the wait_lock.
Thomas Gleixner27e35712014-06-11 18:44:04 +00001414 */
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001415 mark_wakeup_next_waiter(wake_q, lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001416
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001417 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001418
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001419 /* check PI boosting */
1420 return true;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001421}
1422
1423/*
1424 * debug aware fast / slowpath lock,trylock,unlock
1425 *
1426 * The atomic acquire/release ops are compiled away, when either the
1427 * architecture does not support cmpxchg or when debugging is enabled.
1428 */
1429static inline int
1430rt_mutex_fastlock(struct rt_mutex *lock, int state,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001431 int (*slowfn)(struct rt_mutex *lock, int state,
1432 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001433 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001434{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001435 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001436 rt_mutex_deadlock_account_lock(lock, current);
1437 return 0;
1438 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001439 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001440}
1441
1442static inline int
1443rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001444 struct hrtimer_sleeper *timeout,
1445 enum rtmutex_chainwalk chwalk,
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001446 int (*slowfn)(struct rt_mutex *lock, int state,
1447 struct hrtimer_sleeper *timeout,
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001448 enum rtmutex_chainwalk chwalk))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001449{
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001450 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001451 likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001452 rt_mutex_deadlock_account_lock(lock, current);
1453 return 0;
1454 } else
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001455 return slowfn(lock, state, timeout, chwalk);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001456}
1457
1458static inline int
1459rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001460 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001461{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001462 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001463 rt_mutex_deadlock_account_lock(lock, current);
1464 return 1;
1465 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001466 return slowfn(lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001467}
1468
1469static inline void
1470rt_mutex_fastunlock(struct rt_mutex *lock,
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001471 bool (*slowfn)(struct rt_mutex *lock,
1472 struct wake_q_head *wqh))
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001473{
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001474 WAKE_Q(wake_q);
1475
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001476 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001477 rt_mutex_deadlock_account_unlock(current);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001478
1479 } else {
1480 bool deboost = slowfn(lock, &wake_q);
1481
1482 wake_up_q(&wake_q);
1483
1484 /* Undo pi boosting if necessary: */
1485 if (deboost)
1486 rt_mutex_adjust_prio(current);
1487 }
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001488}
1489
1490/**
1491 * rt_mutex_lock - lock a rt_mutex
1492 *
1493 * @lock: the rt_mutex to be locked
1494 */
1495void __sched rt_mutex_lock(struct rt_mutex *lock)
1496{
1497 might_sleep();
1498
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001499 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001500}
1501EXPORT_SYMBOL_GPL(rt_mutex_lock);
1502
1503/**
1504 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1505 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001506 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001507 *
1508 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001509 * 0 on success
1510 * -EINTR when interrupted by a signal
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001511 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001512int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001513{
1514 might_sleep();
1515
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001516 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001517}
1518EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1519
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001520/*
1521 * Futex variant with full deadlock detection.
1522 */
1523int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1524 struct hrtimer_sleeper *timeout)
1525{
1526 might_sleep();
1527
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001528 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1529 RT_MUTEX_FULL_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001530 rt_mutex_slowlock);
1531}
1532
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001533/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001534 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1535 * the timeout structure is provided
1536 * by the caller
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001537 *
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001538 * @lock: the rt_mutex to be locked
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001539 * @timeout: timeout structure or NULL (no timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001540 *
1541 * Returns:
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001542 * 0 on success
1543 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001544 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001545 */
1546int
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001547rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001548{
1549 might_sleep();
1550
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001551 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1552 RT_MUTEX_MIN_CHAINWALK,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001553 rt_mutex_slowlock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001554}
1555EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1556
1557/**
1558 * rt_mutex_trylock - try to lock a rt_mutex
1559 *
1560 * @lock: the rt_mutex to be locked
1561 *
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001562 * This function can only be called in thread context. It's safe to
1563 * call it from atomic regions, but not from hard interrupt or soft
1564 * interrupt context.
1565 *
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001566 * Returns 1 on success and 0 on contention
1567 */
1568int __sched rt_mutex_trylock(struct rt_mutex *lock)
1569{
Sebastian Andrzej Siewiora461d5872016-05-27 15:47:18 +02001570 if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
Thomas Gleixner6ce47fd2015-05-13 22:49:12 +02001571 return 0;
1572
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001573 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1574}
1575EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1576
1577/**
1578 * rt_mutex_unlock - unlock a rt_mutex
1579 *
1580 * @lock: the rt_mutex to be unlocked
1581 */
1582void __sched rt_mutex_unlock(struct rt_mutex *lock)
1583{
1584 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1585}
1586EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1587
Luis Henriques23b94b92009-04-29 21:54:51 +01001588/**
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001589 * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1590 * @lock: the rt_mutex to be unlocked
1591 *
1592 * Returns: true/false indicating whether priority adjustment is
1593 * required or not.
1594 */
1595bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1596 struct wake_q_head *wqh)
1597{
Davidlohr Bueso700318d2015-09-30 13:03:13 -07001598 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001599 rt_mutex_deadlock_account_unlock(current);
1600 return false;
1601 }
1602 return rt_mutex_slowunlock(lock, wqh);
1603}
1604
1605/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001606 * rt_mutex_destroy - mark a mutex unusable
1607 * @lock: the mutex to be destroyed
1608 *
1609 * This function marks the mutex uninitialized, and any subsequent
1610 * use of the mutex is forbidden. The mutex must not be locked when
1611 * this function is called.
1612 */
1613void rt_mutex_destroy(struct rt_mutex *lock)
1614{
1615 WARN_ON(rt_mutex_is_locked(lock));
1616#ifdef CONFIG_DEBUG_RT_MUTEXES
1617 lock->magic = NULL;
1618#endif
1619}
1620
1621EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1622
1623/**
1624 * __rt_mutex_init - initialize the rt lock
1625 *
1626 * @lock: the rt lock to be initialized
1627 *
1628 * Initialize the rt lock to unlocked state.
1629 *
1630 * Initializing of a locked rt lock is not allowed
1631 */
1632void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1633{
1634 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001635 raw_spin_lock_init(&lock->wait_lock);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01001636 lock->waiters = RB_ROOT;
1637 lock->waiters_leftmost = NULL;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07001638
1639 debug_rt_mutex_init(lock, name);
1640}
1641EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001642
1643/**
1644 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1645 * proxy owner
1646 *
1647 * @lock: the rt_mutex to be locked
1648 * @proxy_owner:the task to set as owner
1649 *
1650 * No locking. Caller has to do serializing itself
1651 * Special API call for PI-futex support
1652 */
1653void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1654 struct task_struct *proxy_owner)
1655{
1656 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001657 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001658 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001659 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1660}
1661
1662/**
1663 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1664 *
1665 * @lock: the rt_mutex to be locked
1666 *
1667 * No locking. Caller has to do serializing itself
1668 * Special API call for PI-futex support
1669 */
1670void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1671 struct task_struct *proxy_owner)
1672{
1673 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001674 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001675 rt_mutex_deadlock_account_unlock(proxy_owner);
1676}
1677
1678/**
Darren Hart8dac4562009-04-03 13:40:12 -07001679 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1680 * @lock: the rt_mutex to take
1681 * @waiter: the pre-initialized rt_mutex_waiter
1682 * @task: the task to prepare
Darren Hart8dac4562009-04-03 13:40:12 -07001683 *
1684 * Returns:
1685 * 0 - task blocked on lock
1686 * 1 - acquired the lock for task, caller should wake it up
1687 * <0 - error
1688 *
1689 * Special API call for FUTEX_REQUEUE_PI support.
1690 */
1691int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1692 struct rt_mutex_waiter *waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001693 struct task_struct *task)
Darren Hart8dac4562009-04-03 13:40:12 -07001694{
1695 int ret;
1696
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001697 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001698
Lai Jiangshan81612392011-01-14 17:09:41 +08001699 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001700 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001701 return 1;
1702 }
1703
Thomas Gleixner3d5c9342014-06-05 12:34:23 +02001704 /* We enforce deadlock detection for futexes */
Thomas Gleixner8930ed82014-05-22 03:25:47 +00001705 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1706 RT_MUTEX_FULL_CHAINWALK);
Darren Hart8dac4562009-04-03 13:40:12 -07001707
Lai Jiangshan81612392011-01-14 17:09:41 +08001708 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001709 /*
1710 * Reset the return value. We might have
1711 * returned with -EDEADLK and the owner
1712 * released the lock while we were walking the
1713 * pi chain. Let the waiter sort it out.
1714 */
1715 ret = 0;
1716 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001717
1718 if (unlikely(ret))
1719 remove_waiter(lock, waiter);
1720
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001721 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001722
1723 debug_rt_mutex_print_deadlock(waiter);
1724
1725 return ret;
1726}
1727
1728/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001729 * rt_mutex_next_owner - return the next owner of the lock
1730 *
1731 * @lock: the rt lock query
1732 *
1733 * Returns the next owner of the lock or NULL
1734 *
1735 * Caller has to serialize against other accessors to the lock
1736 * itself.
1737 *
1738 * Special API call for PI-futex support
1739 */
1740struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1741{
1742 if (!rt_mutex_has_waiters(lock))
1743 return NULL;
1744
1745 return rt_mutex_top_waiter(lock)->task;
1746}
Darren Hart8dac4562009-04-03 13:40:12 -07001747
1748/**
Peter Zijlstrace813552017-03-22 11:35:57 +01001749 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
Darren Hart8dac4562009-04-03 13:40:12 -07001750 * @lock: the rt_mutex we were woken on
1751 * @to: the timeout, null if none. hrtimer should already have
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001752 * been started.
Darren Hart8dac4562009-04-03 13:40:12 -07001753 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -07001754 *
Peter Zijlstrace813552017-03-22 11:35:57 +01001755 * Wait for the the lock acquisition started on our behalf by
1756 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
1757 * rt_mutex_cleanup_proxy_lock().
Darren Hart8dac4562009-04-03 13:40:12 -07001758 *
1759 * Returns:
1760 * 0 - success
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001761 * <0 - error, one of -EINTR, -ETIMEDOUT
Darren Hart8dac4562009-04-03 13:40:12 -07001762 *
Peter Zijlstrace813552017-03-22 11:35:57 +01001763 * Special API call for PI-futex support
Darren Hart8dac4562009-04-03 13:40:12 -07001764 */
Peter Zijlstrace813552017-03-22 11:35:57 +01001765int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
Darren Hart8dac4562009-04-03 13:40:12 -07001766 struct hrtimer_sleeper *to,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001767 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -07001768{
1769 int ret;
1770
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001771 raw_spin_lock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001772
1773 set_current_state(TASK_INTERRUPTIBLE);
1774
Davidlohr Buesoafffc6c2015-02-01 22:16:24 -08001775 /* sleep on the mutex */
Lai Jiangshan81612392011-01-14 17:09:41 +08001776 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001777
Darren Hart8dac4562009-04-03 13:40:12 -07001778 /*
1779 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1780 * have to fix that up.
1781 */
1782 fixup_rt_mutex_waiters(lock);
1783
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001784 raw_spin_unlock_irq(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001785
Darren Hart8dac4562009-04-03 13:40:12 -07001786 return ret;
1787}
Peter Zijlstrace813552017-03-22 11:35:57 +01001788
1789/**
1790 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
1791 * @lock: the rt_mutex we were woken on
1792 * @waiter: the pre-initialized rt_mutex_waiter
1793 *
1794 * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
1795 *
1796 * Unless we acquired the lock; we're still enqueued on the wait-list and can
1797 * in fact still be granted ownership until we're removed. Therefore we can
1798 * find we are in fact the owner and must disregard the
1799 * rt_mutex_wait_proxy_lock() failure.
1800 *
1801 * Returns:
1802 * true - did the cleanup, we done.
1803 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
1804 * caller should disregards its return value.
1805 *
1806 * Special API call for PI-futex support
1807 */
1808bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
1809 struct rt_mutex_waiter *waiter)
1810{
1811 bool cleanup = false;
1812
1813 raw_spin_lock_irq(&lock->wait_lock);
1814 /*
1815 * Unless we're the owner; we're still enqueued on the wait_list.
1816 * So check if we became owner, if not, take us off the wait_list.
1817 */
1818 if (rt_mutex_owner(lock) != current) {
1819 remove_waiter(lock, waiter);
1820 fixup_rt_mutex_waiters(lock);
1821 cleanup = true;
1822 }
1823 raw_spin_unlock_irq(&lock->wait_lock);
1824
1825 return cleanup;
1826}