blob: 375ae48479fabd33cb9aa87877c174247fbb3b1e [file] [log] [blame]
Ingo Molnar23f78d42006-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 *
11 * See Documentation/rt-mutex-design.txt for details.
Ingo Molnar23f78d42006-06-27 02:54:53 -070012 */
13#include <linux/spinlock.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Ingo Molnar23f78d42006-06-27 02:54:53 -070015#include <linux/sched.h>
16#include <linux/timer.h>
17
18#include "rtmutex_common.h"
19
Ingo Molnar23f78d42006-06-27 02:54:53 -070020/*
21 * lock->owner state tracking:
22 *
Lai Jiangshan81612392011-01-14 17:09:41 +080023 * lock->owner holds the task_struct pointer of the owner. Bit 0
24 * is used to keep track of the "lock has waiters" state.
Ingo Molnar23f78d42006-06-27 02:54:53 -070025 *
Lai Jiangshan81612392011-01-14 17:09:41 +080026 * owner bit0
27 * NULL 0 lock is free (fast acquire possible)
28 * NULL 1 lock is free and has waiters and the top waiter
29 * is going to take the lock*
30 * taskpointer 0 lock is held (fast release possible)
31 * taskpointer 1 lock is held and has waiters**
Ingo Molnar23f78d42006-06-27 02:54:53 -070032 *
33 * The fast atomic compare exchange based acquire and release is only
Lai Jiangshan81612392011-01-14 17:09:41 +080034 * possible when bit 0 of lock->owner is 0.
Ingo Molnar23f78d42006-06-27 02:54:53 -070035 *
Lai Jiangshan81612392011-01-14 17:09:41 +080036 * (*) It also can be a transitional state when grabbing the lock
37 * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
38 * we need to set the bit0 before looking at the lock, and the owner may be
39 * NULL in this small time, hence this can be a transitional state.
40 *
41 * (**) There is a small time when bit 0 is set but there are no
42 * waiters. This can happen when grabbing the lock in the slow path.
43 * To prevent a cmpxchg of the owner releasing the lock, we need to
44 * set this bit before looking at the lock.
Ingo Molnar23f78d42006-06-27 02:54:53 -070045 */
46
Thomas Gleixnerbd197232007-06-17 21:11:10 +020047static void
Lai Jiangshan81612392011-01-14 17:09:41 +080048rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
Ingo Molnar23f78d42006-06-27 02:54:53 -070049{
Lai Jiangshan81612392011-01-14 17:09:41 +080050 unsigned long val = (unsigned long)owner;
Ingo Molnar23f78d42006-06-27 02:54:53 -070051
52 if (rt_mutex_has_waiters(lock))
53 val |= RT_MUTEX_HAS_WAITERS;
54
55 lock->owner = (struct task_struct *)val;
56}
57
58static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
59{
60 lock->owner = (struct task_struct *)
61 ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
62}
63
64static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
65{
66 if (!rt_mutex_has_waiters(lock))
67 clear_rt_mutex_waiters(lock);
68}
69
70/*
Nick Reuter75ec7712017-06-29 20:22:10 -050071 * We can speed up the acquire/release, if the architecture
72 * supports cmpxchg and if there's no debugging state to be set up
Thomas Gleixnerbd197232007-06-17 21:11:10 +020073 */
Nick Reuter75ec7712017-06-29 20:22:10 -050074#if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
Thomas Gleixnerbd197232007-06-17 21:11:10 +020075# define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
76static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
77{
78 unsigned long owner, *p = (unsigned long *) &lock->owner;
79
80 do {
81 owner = *p;
82 } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
83}
Thomas Gleixner2a777942014-06-11 18:44:04 +000084
85/*
86 * Safe fastpath aware unlock:
87 * 1) Clear the waiters bit
88 * 2) Drop lock->wait_lock
89 * 3) Try to unlock the lock with cmpxchg
90 */
91static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
92 __releases(lock->wait_lock)
93{
94 struct task_struct *owner = rt_mutex_owner(lock);
95
96 clear_rt_mutex_waiters(lock);
97 raw_spin_unlock(&lock->wait_lock);
98 /*
99 * If a new waiter comes in between the unlock and the cmpxchg
100 * we have two situations:
101 *
102 * unlock(wait_lock);
103 * lock(wait_lock);
104 * cmpxchg(p, owner, 0) == owner
105 * mark_rt_mutex_waiters(lock);
106 * acquire(lock);
107 * or:
108 *
109 * unlock(wait_lock);
110 * lock(wait_lock);
111 * mark_rt_mutex_waiters(lock);
112 *
113 * cmpxchg(p, owner, 0) != owner
114 * enqueue_waiter();
115 * unlock(wait_lock);
116 * lock(wait_lock);
117 * wake waiter();
118 * unlock(wait_lock);
119 * lock(wait_lock);
120 * acquire(lock);
121 */
122 return rt_mutex_cmpxchg(lock, owner, NULL);
123}
124
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200125#else
126# define rt_mutex_cmpxchg(l,c,n) (0)
127static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
128{
129 lock->owner = (struct task_struct *)
130 ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
131}
Thomas Gleixner2a777942014-06-11 18:44:04 +0000132
133/*
134 * Simple slow path only version: lock->owner is protected by lock->wait_lock.
135 */
136static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
137 __releases(lock->wait_lock)
138{
139 lock->owner = NULL;
140 raw_spin_unlock(&lock->wait_lock);
141 return true;
142}
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200143#endif
144
145/*
Ingo Molnar23f78d42006-06-27 02:54:53 -0700146 * Calculate task priority from the waiter list priority
147 *
148 * Return task->normal_prio when the waiter list is empty or when
149 * the waiter is not allowed to do priority boosting
150 */
151int rt_mutex_getprio(struct task_struct *task)
152{
153 if (likely(!task_has_pi_waiters(task)))
154 return task->normal_prio;
155
156 return min(task_top_pi_waiter(task)->pi_list_entry.prio,
157 task->normal_prio);
158}
159
160/*
161 * Adjust the priority of a task, after its pi_waiters got modified.
162 *
163 * This can be both boosting and unboosting. task->pi_lock must be held.
164 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200165static void __rt_mutex_adjust_prio(struct task_struct *task)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700166{
167 int prio = rt_mutex_getprio(task);
168
169 if (task->prio != prio)
170 rt_mutex_setprio(task, prio);
171}
172
173/*
174 * Adjust task priority (undo boosting). Called from the exit path of
175 * rt_mutex_slowunlock() and rt_mutex_slowlock().
176 *
177 * (Note: We do this outside of the protection of lock->wait_lock to
178 * allow the lock to be taken while or before we readjust the priority
179 * of task. We do not use the spin_xx_mutex() variants here as we are
180 * outside of the debug path.)
181 */
182static void rt_mutex_adjust_prio(struct task_struct *task)
183{
184 unsigned long flags;
185
Thomas Gleixner1d615482009-11-17 14:54:03 +0100186 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700187 __rt_mutex_adjust_prio(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100188 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700189}
190
191/*
192 * Max number of times we'll walk the boosting chain:
193 */
194int max_lock_depth = 1024;
195
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200196static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
197{
198 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
199}
200
Ingo Molnar23f78d42006-06-27 02:54:53 -0700201/*
202 * Adjust the priority chain. Also used for deadlock detection.
203 * Decreases task's usage by one - may thus free the task.
204 * Returns 0 or -EDEADLK.
205 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200206static int rt_mutex_adjust_prio_chain(struct task_struct *task,
207 int deadlock_detect,
208 struct rt_mutex *orig_lock,
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200209 struct rt_mutex *next_lock,
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200210 struct rt_mutex_waiter *orig_waiter,
211 struct task_struct *top_task)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700212{
213 struct rt_mutex *lock;
214 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
215 int detect_deadlock, ret = 0, depth = 0;
216 unsigned long flags;
217
218 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
219 deadlock_detect);
220
221 /*
222 * The (de)boosting is a step by step approach with a lot of
223 * pitfalls. We want this to be preemptible and we want hold a
224 * maximum of two locks per step. So we have to check
225 * carefully whether things change under us.
226 */
227 again:
228 if (++depth > max_lock_depth) {
229 static int prev_max;
230
231 /*
232 * Print this only once. If the admin changes the limit,
233 * print a new message when reaching the limit again.
234 */
235 if (prev_max != max_lock_depth) {
236 prev_max = max_lock_depth;
237 printk(KERN_WARNING "Maximum lock depth %d reached "
238 "task: %s (%d)\n", max_lock_depth,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700239 top_task->comm, task_pid_nr(top_task));
Ingo Molnar23f78d42006-06-27 02:54:53 -0700240 }
241 put_task_struct(task);
242
Thomas Gleixnerea018da2014-06-05 12:34:23 +0200243 return -EDEADLK;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700244 }
245 retry:
246 /*
247 * Task can not go away as we did a get_task() before !
248 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100249 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700250
251 waiter = task->pi_blocked_on;
252 /*
253 * Check whether the end of the boosting chain has been
254 * reached or the state of the chain has changed while we
255 * dropped the locks.
256 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800257 if (!waiter)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700258 goto out_unlock_pi;
259
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700260 /*
261 * Check the orig_waiter state. After we dropped the locks,
Lai Jiangshan81612392011-01-14 17:09:41 +0800262 * the previous owner of the lock might have released the lock.
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700263 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800264 if (orig_waiter && !rt_mutex_owner(orig_lock))
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700265 goto out_unlock_pi;
266
267 /*
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200268 * We dropped all locks after taking a refcount on @task, so
269 * the task might have moved on in the lock chain or even left
270 * the chain completely and blocks now on an unrelated lock or
271 * on @orig_lock.
272 *
273 * We stored the lock on which @task was blocked in @next_lock,
274 * so we can detect the chain change.
275 */
276 if (next_lock != waiter->lock)
277 goto out_unlock_pi;
278
279 /*
Thomas Gleixner1a539a82007-06-08 13:46:58 -0700280 * Drop out, when the task has no waiters. Note,
281 * top_waiter can be NULL, when we are in the deboosting
282 * mode!
283 */
Thomas Gleixner90b421b2014-05-22 03:25:39 +0000284 if (top_waiter) {
285 if (!task_has_pi_waiters(task))
286 goto out_unlock_pi;
287 /*
288 * If deadlock detection is off, we stop here if we
289 * are not the top pi waiter of the task.
290 */
291 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
292 goto out_unlock_pi;
293 }
Ingo Molnar23f78d42006-06-27 02:54:53 -0700294
295 /*
296 * When deadlock detection is off then we check, if further
297 * priority adjustment is necessary.
298 */
299 if (!detect_deadlock && waiter->list_entry.prio == task->prio)
300 goto out_unlock_pi;
301
302 lock = waiter->lock;
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100303 if (!raw_spin_trylock(&lock->wait_lock)) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100304 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700305 cpu_relax();
306 goto retry;
307 }
308
Thomas Gleixner90b421b2014-05-22 03:25:39 +0000309 /*
310 * Deadlock detection. If the lock is the same as the original
311 * lock which caused us to walk the lock chain or if the
312 * current lock is owned by the task which initiated the chain
313 * walk, we detected a deadlock.
314 */
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700315 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
Ingo Molnar23f78d42006-06-27 02:54:53 -0700316 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100317 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixnerea018da2014-06-05 12:34:23 +0200318 ret = -EDEADLK;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700319 goto out_unlock_pi;
320 }
321
322 top_waiter = rt_mutex_top_waiter(lock);
323
324 /* Requeue the waiter */
325 plist_del(&waiter->list_entry, &lock->wait_list);
326 waiter->list_entry.prio = task->prio;
327 plist_add(&waiter->list_entry, &lock->wait_list);
328
329 /* Release the task */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100330 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Lai Jiangshan81612392011-01-14 17:09:41 +0800331 if (!rt_mutex_owner(lock)) {
332 /*
333 * If the requeue above changed the top waiter, then we need
334 * to wake the new top waiter up to try to get the lock.
335 */
336
337 if (top_waiter != rt_mutex_top_waiter(lock))
338 wake_up_process(rt_mutex_top_waiter(lock)->task);
339 raw_spin_unlock(&lock->wait_lock);
340 goto out_put_task;
341 }
Ingo Molnar23f78d42006-06-27 02:54:53 -0700342 put_task_struct(task);
343
344 /* Grab the next task */
345 task = rt_mutex_owner(lock);
Steven Rostedtdb630632006-09-29 01:59:44 -0700346 get_task_struct(task);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100347 raw_spin_lock_irqsave(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700348
349 if (waiter == rt_mutex_top_waiter(lock)) {
350 /* Boost the owner */
351 plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
352 waiter->pi_list_entry.prio = waiter->list_entry.prio;
353 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
354 __rt_mutex_adjust_prio(task);
355
356 } else if (top_waiter == waiter) {
357 /* Deboost the owner */
358 plist_del(&waiter->pi_list_entry, &task->pi_waiters);
359 waiter = rt_mutex_top_waiter(lock);
360 waiter->pi_list_entry.prio = waiter->list_entry.prio;
361 plist_add(&waiter->pi_list_entry, &task->pi_waiters);
362 __rt_mutex_adjust_prio(task);
363 }
364
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200365 /*
366 * Check whether the task which owns the current lock is pi
367 * blocked itself. If yes we store a pointer to the lock for
368 * the lock chain change detection above. After we dropped
369 * task->pi_lock next_lock cannot be dereferenced anymore.
370 */
371 next_lock = task_blocked_on_lock(task);
372
Thomas Gleixner1d615482009-11-17 14:54:03 +0100373 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700374
375 top_waiter = rt_mutex_top_waiter(lock);
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100376 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700377
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200378 /*
379 * We reached the end of the lock chain. Stop right here. No
380 * point to go back just to figure that out.
381 */
382 if (!next_lock)
383 goto out_put_task;
384
Ingo Molnar23f78d42006-06-27 02:54:53 -0700385 if (!detect_deadlock && waiter != top_waiter)
386 goto out_put_task;
387
388 goto again;
389
390 out_unlock_pi:
Thomas Gleixner1d615482009-11-17 14:54:03 +0100391 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700392 out_put_task:
393 put_task_struct(task);
Ingo Molnar36c8b582006-07-03 00:25:41 -0700394
Ingo Molnar23f78d42006-06-27 02:54:53 -0700395 return ret;
396}
397
398/*
Ingo Molnar23f78d42006-06-27 02:54:53 -0700399 * Try to take an rt-mutex
400 *
Ingo Molnar23f78d42006-06-27 02:54:53 -0700401 * Must be called with lock->wait_lock held.
Lai Jiangshan81612392011-01-14 17:09:41 +0800402 *
403 * @lock: the lock to be acquired.
404 * @task: the task which wants to acquire the lock
405 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700406 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800407static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
408 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700409{
410 /*
411 * We have to be careful here if the atomic speedups are
412 * enabled, such that, when
413 * - no other waiter is on the lock
414 * - the lock has been released since we did the cmpxchg
415 * the lock can be released or taken while we are doing the
416 * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
417 *
418 * The atomic acquire/release aware variant of
419 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
420 * the WAITERS bit, the atomic release / acquire can not
421 * happen anymore and lock->wait_lock protects us from the
422 * non-atomic case.
423 *
424 * Note, that this might set lock->owner =
425 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
426 * any more. This is fixed up when we take the ownership.
427 * This is the transitional state explained at the top of this file.
428 */
429 mark_rt_mutex_waiters(lock);
430
Lai Jiangshan81612392011-01-14 17:09:41 +0800431 if (rt_mutex_owner(lock))
Ingo Molnar23f78d42006-06-27 02:54:53 -0700432 return 0;
433
Lai Jiangshan81612392011-01-14 17:09:41 +0800434 /*
435 * It will get the lock because of one of these conditions:
436 * 1) there is no waiter
437 * 2) higher priority than waiters
438 * 3) it is top waiter
439 */
440 if (rt_mutex_has_waiters(lock)) {
441 if (task->prio >= rt_mutex_top_waiter(lock)->list_entry.prio) {
442 if (!waiter || waiter != rt_mutex_top_waiter(lock))
443 return 0;
444 }
445 }
446
447 if (waiter || rt_mutex_has_waiters(lock)) {
448 unsigned long flags;
449 struct rt_mutex_waiter *top;
450
451 raw_spin_lock_irqsave(&task->pi_lock, flags);
452
453 /* remove the queued waiter. */
454 if (waiter) {
455 plist_del(&waiter->list_entry, &lock->wait_list);
456 task->pi_blocked_on = NULL;
457 }
458
459 /*
460 * We have to enqueue the top waiter(if it exists) into
461 * task->pi_waiters list.
462 */
463 if (rt_mutex_has_waiters(lock)) {
464 top = rt_mutex_top_waiter(lock);
465 top->pi_list_entry.prio = top->list_entry.prio;
466 plist_add(&top->pi_list_entry, &task->pi_waiters);
467 }
468 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
469 }
470
Ingo Molnar23f78d42006-06-27 02:54:53 -0700471 /* We got the lock. */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700472 debug_rt_mutex_lock(lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700473
Lai Jiangshan81612392011-01-14 17:09:41 +0800474 rt_mutex_set_owner(lock, task);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700475
Lai Jiangshan81612392011-01-14 17:09:41 +0800476 rt_mutex_deadlock_account_lock(lock, task);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700477
478 return 1;
479}
480
481/*
482 * Task blocks on lock.
483 *
484 * Prepare waiter and propagate pi chain
485 *
486 * This must be called with lock->wait_lock held.
487 */
488static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
489 struct rt_mutex_waiter *waiter,
Darren Hart8dac4562009-04-03 13:40:12 -0700490 struct task_struct *task,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700491 int detect_deadlock)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700492{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700493 struct task_struct *owner = rt_mutex_owner(lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700494 struct rt_mutex_waiter *top_waiter = waiter;
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200495 struct rt_mutex *next_lock;
Steven Rostedtdb630632006-09-29 01:59:44 -0700496 int chain_walk = 0, res;
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200497 unsigned long flags;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700498
Thomas Gleixner90b421b2014-05-22 03:25:39 +0000499 /*
500 * Early deadlock detection. We really don't want the task to
501 * enqueue on itself just to untangle the mess later. It's not
502 * only an optimization. We drop the locks, so another waiter
503 * can come in before the chain walk detects the deadlock. So
504 * the other will detect the deadlock and return -EDEADLOCK,
505 * which is wrong, as the other waiter is not in a deadlock
506 * situation.
507 */
Thomas Gleixnerea018da2014-06-05 12:34:23 +0200508 if (owner == task)
Thomas Gleixner90b421b2014-05-22 03:25:39 +0000509 return -EDEADLK;
510
Thomas Gleixner1d615482009-11-17 14:54:03 +0100511 raw_spin_lock_irqsave(&task->pi_lock, flags);
Darren Hart8dac4562009-04-03 13:40:12 -0700512 __rt_mutex_adjust_prio(task);
513 waiter->task = task;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700514 waiter->lock = lock;
Darren Hart8dac4562009-04-03 13:40:12 -0700515 plist_node_init(&waiter->list_entry, task->prio);
516 plist_node_init(&waiter->pi_list_entry, task->prio);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700517
518 /* Get the top priority waiter on the lock */
519 if (rt_mutex_has_waiters(lock))
520 top_waiter = rt_mutex_top_waiter(lock);
521 plist_add(&waiter->list_entry, &lock->wait_list);
522
Darren Hart8dac4562009-04-03 13:40:12 -0700523 task->pi_blocked_on = waiter;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700524
Thomas Gleixner1d615482009-11-17 14:54:03 +0100525 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700526
Lai Jiangshan81612392011-01-14 17:09:41 +0800527 if (!owner)
528 return 0;
529
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200530 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700531 if (waiter == rt_mutex_top_waiter(lock)) {
Ingo Molnar23f78d42006-06-27 02:54:53 -0700532 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
533 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
534
535 __rt_mutex_adjust_prio(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700536 if (owner->pi_blocked_on)
537 chain_walk = 1;
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200538 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
Steven Rostedtdb630632006-09-29 01:59:44 -0700539 chain_walk = 1;
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200540 }
Steven Rostedtdb630632006-09-29 01:59:44 -0700541
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200542 /* Store the lock on which owner is blocked or NULL */
543 next_lock = task_blocked_on_lock(owner);
544
545 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
546 /*
547 * Even if full deadlock detection is on, if the owner is not
548 * blocked itself, we can avoid finding this out in the chain
549 * walk.
550 */
551 if (!chain_walk || !next_lock)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700552 return 0;
553
Steven Rostedtdb630632006-09-29 01:59:44 -0700554 /*
555 * The owner can't disappear while holding a lock,
556 * so the owner struct is protected by wait_lock.
557 * Gets dropped in rt_mutex_adjust_prio_chain()!
558 */
559 get_task_struct(owner);
560
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100561 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700562
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200563 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
564 next_lock, waiter, task);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700565
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100566 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700567
568 return res;
569}
570
571/*
572 * Wake up the next waiter on the lock.
573 *
Thomas Gleixner2a777942014-06-11 18:44:04 +0000574 * Remove the top waiter from the current tasks pi waiter list and
575 * wake it up.
Ingo Molnar23f78d42006-06-27 02:54:53 -0700576 *
577 * Called with lock->wait_lock held.
578 */
579static void wakeup_next_waiter(struct rt_mutex *lock)
580{
581 struct rt_mutex_waiter *waiter;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700582 unsigned long flags;
583
Thomas Gleixner1d615482009-11-17 14:54:03 +0100584 raw_spin_lock_irqsave(&current->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700585
586 waiter = rt_mutex_top_waiter(lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700587
588 /*
589 * Remove it from current->pi_waiters. We do not adjust a
590 * possible priority boost right now. We execute wakeup in the
591 * boosted mode and go back to normal after releasing
592 * lock->wait_lock.
593 */
594 plist_del(&waiter->pi_list_entry, &current->pi_waiters);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700595
Thomas Gleixner2a777942014-06-11 18:44:04 +0000596 /*
597 * As we are waking up the top waiter, and the waiter stays
598 * queued on the lock until it gets the lock, this lock
599 * obviously has waiters. Just set the bit here and this has
600 * the added benefit of forcing all new tasks into the
601 * slow path making sure no task of lower priority than
602 * the top waiter can steal this lock.
603 */
604 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700605
Thomas Gleixner1d615482009-11-17 14:54:03 +0100606 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700607
Thomas Gleixner2a777942014-06-11 18:44:04 +0000608 /*
609 * It's safe to dereference waiter as it cannot go away as
610 * long as we hold lock->wait_lock. The waiter task needs to
611 * acquire it in order to dequeue the waiter.
612 */
Lai Jiangshan81612392011-01-14 17:09:41 +0800613 wake_up_process(waiter->task);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700614}
615
616/*
Lai Jiangshan81612392011-01-14 17:09:41 +0800617 * Remove a waiter from a lock and give up
Ingo Molnar23f78d42006-06-27 02:54:53 -0700618 *
Lai Jiangshan81612392011-01-14 17:09:41 +0800619 * Must be called with lock->wait_lock held and
620 * have just failed to try_to_take_rt_mutex().
Ingo Molnar23f78d42006-06-27 02:54:53 -0700621 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200622static void remove_waiter(struct rt_mutex *lock,
623 struct rt_mutex_waiter *waiter)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700624{
625 int first = (waiter == rt_mutex_top_waiter(lock));
Ingo Molnar36c8b582006-07-03 00:25:41 -0700626 struct task_struct *owner = rt_mutex_owner(lock);
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200627 struct rt_mutex *next_lock = NULL;
Ingo Molnar23f78d42006-06-27 02:54:53 -0700628 unsigned long flags;
629
Thomas Gleixner1d615482009-11-17 14:54:03 +0100630 raw_spin_lock_irqsave(&current->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700631 plist_del(&waiter->list_entry, &lock->wait_list);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700632 current->pi_blocked_on = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100633 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700634
Lai Jiangshan81612392011-01-14 17:09:41 +0800635 if (!owner)
636 return;
637
638 if (first) {
Ingo Molnar23f78d42006-06-27 02:54:53 -0700639
Thomas Gleixner1d615482009-11-17 14:54:03 +0100640 raw_spin_lock_irqsave(&owner->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700641
642 plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
643
644 if (rt_mutex_has_waiters(lock)) {
645 struct rt_mutex_waiter *next;
646
647 next = rt_mutex_top_waiter(lock);
648 plist_add(&next->pi_list_entry, &owner->pi_waiters);
649 }
650 __rt_mutex_adjust_prio(owner);
651
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200652 /* Store the lock on which owner is blocked or NULL */
653 next_lock = task_blocked_on_lock(owner);
Steven Rostedtdb630632006-09-29 01:59:44 -0700654
Thomas Gleixner1d615482009-11-17 14:54:03 +0100655 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700656 }
657
658 WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
659
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200660 if (!next_lock)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700661 return;
662
Steven Rostedtdb630632006-09-29 01:59:44 -0700663 /* gets dropped in rt_mutex_adjust_prio_chain()! */
664 get_task_struct(owner);
665
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100666 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700667
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200668 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700669
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100670 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700671}
672
673/*
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700674 * Recheck the pi chain, in case we got a priority setting
675 *
676 * Called from sched_setscheduler
677 */
678void rt_mutex_adjust_pi(struct task_struct *task)
679{
680 struct rt_mutex_waiter *waiter;
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200681 struct rt_mutex *next_lock;
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700682 unsigned long flags;
683
Thomas Gleixner1d615482009-11-17 14:54:03 +0100684 raw_spin_lock_irqsave(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700685
686 waiter = task->pi_blocked_on;
687 if (!waiter || waiter->list_entry.prio == task->prio) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100688 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700689 return;
690 }
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200691 next_lock = waiter->lock;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100692 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700693
Steven Rostedtdb630632006-09-29 01:59:44 -0700694 /* gets dropped in rt_mutex_adjust_prio_chain()! */
695 get_task_struct(task);
Thomas Gleixner307e2e02014-06-05 11:16:12 +0200696
697 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700698}
699
Darren Hart8dac4562009-04-03 13:40:12 -0700700/**
701 * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
702 * @lock: the rt_mutex to take
703 * @state: the state the task should block in (TASK_INTERRUPTIBLE
704 * or TASK_UNINTERRUPTIBLE)
705 * @timeout: the pre-initialized and started timer, or NULL for none
706 * @waiter: the pre-initialized rt_mutex_waiter
Darren Hart8dac4562009-04-03 13:40:12 -0700707 *
708 * lock->wait_lock must be held by the caller.
709 */
710static int __sched
711__rt_mutex_slowlock(struct rt_mutex *lock, int state,
712 struct hrtimer_sleeper *timeout,
Lai Jiangshan81612392011-01-14 17:09:41 +0800713 struct rt_mutex_waiter *waiter)
Darren Hart8dac4562009-04-03 13:40:12 -0700714{
715 int ret = 0;
716
717 for (;;) {
718 /* Try to acquire the lock: */
Lai Jiangshan81612392011-01-14 17:09:41 +0800719 if (try_to_take_rt_mutex(lock, current, waiter))
Darren Hart8dac4562009-04-03 13:40:12 -0700720 break;
721
722 /*
723 * TASK_INTERRUPTIBLE checks for signals and
724 * timeout. Ignored otherwise.
725 */
726 if (unlikely(state == TASK_INTERRUPTIBLE)) {
727 /* Signal pending? */
728 if (signal_pending(current))
729 ret = -EINTR;
730 if (timeout && !timeout->task)
731 ret = -ETIMEDOUT;
732 if (ret)
733 break;
734 }
735
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100736 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700737
738 debug_rt_mutex_print_deadlock(waiter);
739
Lai Jiangshan81612392011-01-14 17:09:41 +0800740 schedule_rt_mutex(lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700741
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100742 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -0700743 set_current_state(state);
744 }
745
746 return ret;
747}
748
Thomas Gleixnerea018da2014-06-05 12:34:23 +0200749static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
750 struct rt_mutex_waiter *w)
751{
752 /*
753 * If the result is not -EDEADLOCK or the caller requested
754 * deadlock detection, nothing to do here.
755 */
756 if (res != -EDEADLOCK || detect_deadlock)
757 return;
758
759 /*
760 * Yell lowdly and stop the task right here.
761 */
762 rt_mutex_print_deadlock(w);
763 while (1) {
764 set_current_state(TASK_INTERRUPTIBLE);
765 schedule();
766 }
767}
768
Thomas Gleixner95e02ca2006-06-27 02:55:02 -0700769/*
Ingo Molnar23f78d42006-06-27 02:54:53 -0700770 * Slow path lock function:
771 */
772static int __sched
773rt_mutex_slowlock(struct rt_mutex *lock, int state,
774 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700775 int detect_deadlock)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700776{
777 struct rt_mutex_waiter waiter;
778 int ret = 0;
779
780 debug_rt_mutex_init_waiter(&waiter);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700781
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100782 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700783
784 /* Try to acquire the lock again: */
Lai Jiangshan81612392011-01-14 17:09:41 +0800785 if (try_to_take_rt_mutex(lock, current, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100786 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700787 return 0;
788 }
789
790 set_current_state(state);
791
792 /* Setup the timer, when timeout != NULL */
Peter Zijlstra720a2592008-02-13 15:45:36 +0100793 if (unlikely(timeout)) {
Arjan van de Vencc584b22008-09-01 15:02:30 -0700794 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Peter Zijlstra720a2592008-02-13 15:45:36 +0100795 if (!hrtimer_active(&timeout->timer))
796 timeout->task = NULL;
797 }
Ingo Molnar23f78d42006-06-27 02:54:53 -0700798
Lai Jiangshan81612392011-01-14 17:09:41 +0800799 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
800
801 if (likely(!ret))
802 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700803
804 set_current_state(TASK_RUNNING);
805
Thomas Gleixnerea018da2014-06-05 12:34:23 +0200806 if (unlikely(ret)) {
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700807 remove_waiter(lock, &waiter);
Thomas Gleixnerea018da2014-06-05 12:34:23 +0200808 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
809 }
Ingo Molnar23f78d42006-06-27 02:54:53 -0700810
811 /*
812 * try_to_take_rt_mutex() sets the waiter bit
813 * unconditionally. We might have to fix that up.
814 */
815 fixup_rt_mutex_waiters(lock);
816
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100817 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700818
819 /* Remove pending timer: */
820 if (unlikely(timeout))
821 hrtimer_cancel(&timeout->timer);
822
Ingo Molnar23f78d42006-06-27 02:54:53 -0700823 debug_rt_mutex_free_waiter(&waiter);
824
825 return ret;
826}
827
828/*
829 * Slow path try-lock function:
830 */
831static inline int
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700832rt_mutex_slowtrylock(struct rt_mutex *lock)
Ingo Molnar23f78d42006-06-27 02:54:53 -0700833{
834 int ret = 0;
835
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100836 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700837
838 if (likely(rt_mutex_owner(lock) != current)) {
839
Lai Jiangshan81612392011-01-14 17:09:41 +0800840 ret = try_to_take_rt_mutex(lock, current, NULL);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700841 /*
842 * try_to_take_rt_mutex() sets the lock waiters
843 * bit unconditionally. Clean this up.
844 */
845 fixup_rt_mutex_waiters(lock);
846 }
847
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100848 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700849
850 return ret;
851}
852
853/*
854 * Slow path to release a rt-mutex:
855 */
856static void __sched
857rt_mutex_slowunlock(struct rt_mutex *lock)
858{
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100859 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700860
861 debug_rt_mutex_unlock(lock);
862
863 rt_mutex_deadlock_account_unlock(current);
864
Thomas Gleixner2a777942014-06-11 18:44:04 +0000865 /*
866 * We must be careful here if the fast path is enabled. If we
867 * have no waiters queued we cannot set owner to NULL here
868 * because of:
869 *
870 * foo->lock->owner = NULL;
871 * rtmutex_lock(foo->lock); <- fast path
872 * free = atomic_dec_and_test(foo->refcnt);
873 * rtmutex_unlock(foo->lock); <- fast path
874 * if (free)
875 * kfree(foo);
876 * raw_spin_unlock(foo->lock->wait_lock);
877 *
878 * So for the fastpath enabled kernel:
879 *
880 * Nothing can set the waiters bit as long as we hold
881 * lock->wait_lock. So we do the following sequence:
882 *
883 * owner = rt_mutex_owner(lock);
884 * clear_rt_mutex_waiters(lock);
885 * raw_spin_unlock(&lock->wait_lock);
886 * if (cmpxchg(&lock->owner, owner, 0) == owner)
887 * return;
888 * goto retry;
889 *
890 * The fastpath disabled variant is simple as all access to
891 * lock->owner is serialized by lock->wait_lock:
892 *
893 * lock->owner = NULL;
894 * raw_spin_unlock(&lock->wait_lock);
895 */
896 while (!rt_mutex_has_waiters(lock)) {
897 /* Drops lock->wait_lock ! */
898 if (unlock_rt_mutex_safe(lock) == true)
899 return;
900 /* Relock the rtmutex and try again */
901 raw_spin_lock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700902 }
903
Thomas Gleixner2a777942014-06-11 18:44:04 +0000904 /*
905 * The wakeup next waiter path does not suffer from the above
906 * race. See the comments there.
907 */
Ingo Molnar23f78d42006-06-27 02:54:53 -0700908 wakeup_next_waiter(lock);
909
Thomas Gleixnerd209d742009-11-17 18:22:11 +0100910 raw_spin_unlock(&lock->wait_lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700911
912 /* Undo pi boosting if necessary: */
913 rt_mutex_adjust_prio(current);
914}
915
916/*
917 * debug aware fast / slowpath lock,trylock,unlock
918 *
919 * The atomic acquire/release ops are compiled away, when either the
920 * architecture does not support cmpxchg or when debugging is enabled.
921 */
922static inline int
923rt_mutex_fastlock(struct rt_mutex *lock, int state,
924 int detect_deadlock,
925 int (*slowfn)(struct rt_mutex *lock, int state,
926 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700927 int detect_deadlock))
Ingo Molnar23f78d42006-06-27 02:54:53 -0700928{
929 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
930 rt_mutex_deadlock_account_lock(lock, current);
931 return 0;
932 } else
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700933 return slowfn(lock, state, NULL, detect_deadlock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700934}
935
936static inline int
937rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
938 struct hrtimer_sleeper *timeout, int detect_deadlock,
939 int (*slowfn)(struct rt_mutex *lock, int state,
940 struct hrtimer_sleeper *timeout,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700941 int detect_deadlock))
Ingo Molnar23f78d42006-06-27 02:54:53 -0700942{
943 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
944 rt_mutex_deadlock_account_lock(lock, current);
945 return 0;
946 } else
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700947 return slowfn(lock, state, timeout, detect_deadlock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700948}
949
950static inline int
951rt_mutex_fasttrylock(struct rt_mutex *lock,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700952 int (*slowfn)(struct rt_mutex *lock))
Ingo Molnar23f78d42006-06-27 02:54:53 -0700953{
954 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
955 rt_mutex_deadlock_account_lock(lock, current);
956 return 1;
957 }
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700958 return slowfn(lock);
Ingo Molnar23f78d42006-06-27 02:54:53 -0700959}
960
961static inline void
962rt_mutex_fastunlock(struct rt_mutex *lock,
963 void (*slowfn)(struct rt_mutex *lock))
964{
965 if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
966 rt_mutex_deadlock_account_unlock(current);
967 else
968 slowfn(lock);
969}
970
971/**
972 * rt_mutex_lock - lock a rt_mutex
973 *
974 * @lock: the rt_mutex to be locked
975 */
976void __sched rt_mutex_lock(struct rt_mutex *lock)
977{
978 might_sleep();
979
980 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
981}
982EXPORT_SYMBOL_GPL(rt_mutex_lock);
983
984/**
985 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
986 *
987 * @lock: the rt_mutex to be locked
988 * @detect_deadlock: deadlock detection on/off
989 *
990 * Returns:
991 * 0 on success
992 * -EINTR when interrupted by a signal
993 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
994 */
995int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
996 int detect_deadlock)
997{
998 might_sleep();
999
1000 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
1001 detect_deadlock, rt_mutex_slowlock);
1002}
1003EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1004
1005/**
Luis Henriques23b94b92009-04-29 21:54:51 +01001006 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1007 * the timeout structure is provided
1008 * by the caller
Ingo Molnar23f78d42006-06-27 02:54:53 -07001009 *
1010 * @lock: the rt_mutex to be locked
1011 * @timeout: timeout structure or NULL (no timeout)
1012 * @detect_deadlock: deadlock detection on/off
1013 *
1014 * Returns:
1015 * 0 on success
1016 * -EINTR when interrupted by a signal
Jean Delvare3ac49a12009-06-04 16:20:28 +02001017 * -ETIMEDOUT when the timeout expired
Ingo Molnar23f78d42006-06-27 02:54:53 -07001018 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1019 */
1020int
1021rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
1022 int detect_deadlock)
1023{
1024 might_sleep();
1025
1026 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1027 detect_deadlock, rt_mutex_slowlock);
1028}
1029EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1030
1031/**
1032 * rt_mutex_trylock - try to lock a rt_mutex
1033 *
1034 * @lock: the rt_mutex to be locked
1035 *
1036 * Returns 1 on success and 0 on contention
1037 */
1038int __sched rt_mutex_trylock(struct rt_mutex *lock)
1039{
1040 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1041}
1042EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1043
1044/**
1045 * rt_mutex_unlock - unlock a rt_mutex
1046 *
1047 * @lock: the rt_mutex to be unlocked
1048 */
1049void __sched rt_mutex_unlock(struct rt_mutex *lock)
1050{
1051 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1052}
1053EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1054
Luis Henriques23b94b92009-04-29 21:54:51 +01001055/**
Ingo Molnar23f78d42006-06-27 02:54:53 -07001056 * rt_mutex_destroy - mark a mutex unusable
1057 * @lock: the mutex to be destroyed
1058 *
1059 * This function marks the mutex uninitialized, and any subsequent
1060 * use of the mutex is forbidden. The mutex must not be locked when
1061 * this function is called.
1062 */
1063void rt_mutex_destroy(struct rt_mutex *lock)
1064{
1065 WARN_ON(rt_mutex_is_locked(lock));
1066#ifdef CONFIG_DEBUG_RT_MUTEXES
1067 lock->magic = NULL;
1068#endif
1069}
1070
1071EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1072
1073/**
1074 * __rt_mutex_init - initialize the rt lock
1075 *
1076 * @lock: the rt lock to be initialized
1077 *
1078 * Initialize the rt lock to unlocked state.
1079 *
1080 * Initializing of a locked rt lock is not allowed
1081 */
1082void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1083{
1084 lock->owner = NULL;
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001085 raw_spin_lock_init(&lock->wait_lock);
Dima Zavin732375c2011-07-07 17:27:59 -07001086 plist_head_init(&lock->wait_list);
Ingo Molnar23f78d42006-06-27 02:54:53 -07001087
1088 debug_rt_mutex_init(lock, name);
1089}
1090EXPORT_SYMBOL_GPL(__rt_mutex_init);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001091
1092/**
1093 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1094 * proxy owner
1095 *
1096 * @lock: the rt_mutex to be locked
1097 * @proxy_owner:the task to set as owner
1098 *
1099 * No locking. Caller has to do serializing itself
1100 * Special API call for PI-futex support
1101 */
1102void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1103 struct task_struct *proxy_owner)
1104{
1105 __rt_mutex_init(lock, NULL);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001106 debug_rt_mutex_proxy_lock(lock, proxy_owner);
Lai Jiangshan81612392011-01-14 17:09:41 +08001107 rt_mutex_set_owner(lock, proxy_owner);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001108 rt_mutex_deadlock_account_lock(lock, proxy_owner);
1109}
1110
1111/**
1112 * rt_mutex_proxy_unlock - release a lock on behalf of owner
1113 *
1114 * @lock: the rt_mutex to be locked
1115 *
1116 * No locking. Caller has to do serializing itself
1117 * Special API call for PI-futex support
1118 */
1119void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1120 struct task_struct *proxy_owner)
1121{
1122 debug_rt_mutex_proxy_unlock(lock);
Lai Jiangshan81612392011-01-14 17:09:41 +08001123 rt_mutex_set_owner(lock, NULL);
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001124 rt_mutex_deadlock_account_unlock(proxy_owner);
1125}
1126
1127/**
Darren Hart8dac4562009-04-03 13:40:12 -07001128 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1129 * @lock: the rt_mutex to take
1130 * @waiter: the pre-initialized rt_mutex_waiter
1131 * @task: the task to prepare
1132 * @detect_deadlock: perform deadlock detection (1) or not (0)
1133 *
1134 * Returns:
1135 * 0 - task blocked on lock
1136 * 1 - acquired the lock for task, caller should wake it up
1137 * <0 - error
1138 *
1139 * Special API call for FUTEX_REQUEUE_PI support.
1140 */
1141int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1142 struct rt_mutex_waiter *waiter,
1143 struct task_struct *task, int detect_deadlock)
1144{
1145 int ret;
1146
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001147 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001148
Lai Jiangshan81612392011-01-14 17:09:41 +08001149 if (try_to_take_rt_mutex(lock, task, NULL)) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001150 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001151 return 1;
1152 }
1153
Thomas Gleixnerea018da2014-06-05 12:34:23 +02001154 /* We enforce deadlock detection for futexes */
1155 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
Darren Hart8dac4562009-04-03 13:40:12 -07001156
Lai Jiangshan81612392011-01-14 17:09:41 +08001157 if (ret && !rt_mutex_owner(lock)) {
Darren Hart8dac4562009-04-03 13:40:12 -07001158 /*
1159 * Reset the return value. We might have
1160 * returned with -EDEADLK and the owner
1161 * released the lock while we were walking the
1162 * pi chain. Let the waiter sort it out.
1163 */
1164 ret = 0;
1165 }
Lai Jiangshan81612392011-01-14 17:09:41 +08001166
1167 if (unlikely(ret))
1168 remove_waiter(lock, waiter);
1169
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001170 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001171
1172 debug_rt_mutex_print_deadlock(waiter);
1173
1174 return ret;
1175}
1176
1177/**
Ingo Molnar0cdbee92006-06-27 02:54:57 -07001178 * rt_mutex_next_owner - return the next owner of the lock
1179 *
1180 * @lock: the rt lock query
1181 *
1182 * Returns the next owner of the lock or NULL
1183 *
1184 * Caller has to serialize against other accessors to the lock
1185 * itself.
1186 *
1187 * Special API call for PI-futex support
1188 */
1189struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1190{
1191 if (!rt_mutex_has_waiters(lock))
1192 return NULL;
1193
1194 return rt_mutex_top_waiter(lock)->task;
1195}
Darren Hart8dac4562009-04-03 13:40:12 -07001196
1197/**
1198 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1199 * @lock: the rt_mutex we were woken on
1200 * @to: the timeout, null if none. hrtimer should already have
1201 * been started.
1202 * @waiter: the pre-initialized rt_mutex_waiter
1203 * @detect_deadlock: perform deadlock detection (1) or not (0)
1204 *
1205 * Complete the lock acquisition started our behalf by another thread.
1206 *
1207 * Returns:
1208 * 0 - success
1209 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
1210 *
1211 * Special API call for PI-futex requeue support
1212 */
1213int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1214 struct hrtimer_sleeper *to,
1215 struct rt_mutex_waiter *waiter,
1216 int detect_deadlock)
1217{
1218 int ret;
1219
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001220 raw_spin_lock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001221
1222 set_current_state(TASK_INTERRUPTIBLE);
1223
Lai Jiangshan81612392011-01-14 17:09:41 +08001224 ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
Darren Hart8dac4562009-04-03 13:40:12 -07001225
1226 set_current_state(TASK_RUNNING);
1227
Lai Jiangshan81612392011-01-14 17:09:41 +08001228 if (unlikely(ret))
Darren Hart8dac4562009-04-03 13:40:12 -07001229 remove_waiter(lock, waiter);
1230
1231 /*
1232 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1233 * have to fix that up.
1234 */
1235 fixup_rt_mutex_waiters(lock);
1236
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001237 raw_spin_unlock(&lock->wait_lock);
Darren Hart8dac4562009-04-03 13:40:12 -07001238
Darren Hart8dac4562009-04-03 13:40:12 -07001239 return ret;
1240}