4 * Real-Time Preemption Support
6 * started by Ingo Molnar:
8 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
11 * historic credit for proving that Linux spinlocks can be implemented via
12 * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
13 * and others) who prototyped it on 2.4 and did lots of comparative
14 * research and analysis; TimeSys, for proving that you can implement a
15 * fully preemptible kernel via the use of IRQ threading and mutexes;
16 * Bill Huey for persuasively arguing on lkml that the mutex model is the
17 * right one; and to MontaVista, who ported pmutexes to 2.6.
19 * This code is a from-scratch implementation and is not based on pmutexes,
20 * but the idea of converting spinlocks to mutexes is used here too.
22 * lock debugging, locking tree, deadlock detection:
24 * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
25 * Released under the General Public License (GPL).
27 * Includes portions of the generic R/W semaphore implementation from:
29 * Copyright (c) 2001 David Howells (dhowells@redhat.com).
30 * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
31 * - Derived also from comments by Linus
33 * Pending ownership of locks and ownership stealing:
35 * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
37 * (also by Steven Rostedt)
38 * - Converted single pi_lock to individual task locks.
41 * Doing priority inheritance with help of the scheduler.
43 * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
44 * - major rework based on Esben Nielsens initial patch
45 * - replaced thread_info references by task_struct refs
46 * - removed task->pending_owner dependency
47 * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
48 * in the scheduler return path as discussed with Steven Rostedt
50 * Copyright (C) 2006, Kihon Technologies Inc.
51 * Steven Rostedt <rostedt@goodmis.org>
52 * - debugged and patched Thomas Gleixner's rework.
53 * - added back the cmpxchg to the rework.
54 * - turned atomic require back on for SMP.
57 #include <linux/spinlock.h>
58 #include <linux/rt_lock.h>
59 #include <linux/sched.h>
60 #include <linux/delay.h>
61 #include <linux/module.h>
62 #include <linux/spinlock.h>
63 #include <linux/kallsyms.h>
64 #include <linux/syscalls.h>
65 #include <linux/interrupt.h>
66 #include <linux/plist.h>
68 #include <linux/futex.h>
69 #include <linux/hrtimer.h>
71 #include "rtmutex_common.h"
73 #ifdef CONFIG_PREEMPT_RT
75 * Unlock these on crash:
77 void zap_rt_locks(void)
84 * struct mutex functions
86 void __mutex_init(struct mutex
*lock
, char *name
, struct lock_class_key
*key
)
88 #ifdef CONFIG_DEBUG_LOCK_ALLOC
90 * Make sure we are not reinitializing a held lock:
92 debug_check_no_locks_freed((void *)lock
, sizeof(*lock
));
93 lockdep_init_map(&lock
->dep_map
, name
, key
, 0);
95 __rt_mutex_init(&lock
->lock
, name
);
97 EXPORT_SYMBOL(__mutex_init
);
99 void __lockfunc
_mutex_lock(struct mutex
*lock
)
101 mutex_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
102 rt_mutex_lock(&lock
->lock
);
104 EXPORT_SYMBOL(_mutex_lock
);
106 int __lockfunc
_mutex_lock_interruptible(struct mutex
*lock
)
110 mutex_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
111 ret
= rt_mutex_lock_interruptible(&lock
->lock
, 0);
113 mutex_release(&lock
->dep_map
, 1, _RET_IP_
);
116 EXPORT_SYMBOL(_mutex_lock_interruptible
);
118 int __lockfunc
_mutex_lock_killable(struct mutex
*lock
)
122 mutex_acquire(&lock
->dep_map
, 0, 0, _RET_IP_
);
123 ret
= rt_mutex_lock_killable(&lock
->lock
, 0);
125 mutex_release(&lock
->dep_map
, 1, _RET_IP_
);
128 EXPORT_SYMBOL(_mutex_lock_killable
);
130 #ifdef CONFIG_DEBUG_LOCK_ALLOC
131 void __lockfunc
_mutex_lock_nested(struct mutex
*lock
, int subclass
)
133 mutex_acquire(&lock
->dep_map
, subclass
, 0, _RET_IP_
);
134 rt_mutex_lock(&lock
->lock
);
136 EXPORT_SYMBOL(_mutex_lock_nested
);
138 int __lockfunc
_mutex_lock_interruptible_nested(struct mutex
*lock
, int subclass
)
142 mutex_acquire(&lock
->dep_map
, subclass
, 0, _RET_IP_
);
143 ret
= rt_mutex_lock_interruptible(&lock
->lock
, 0);
145 mutex_release(&lock
->dep_map
, 1, _RET_IP_
);
148 EXPORT_SYMBOL(_mutex_lock_interruptible_nested
);
150 int __lockfunc
_mutex_lock_killable_nested(struct mutex
*lock
, int subclass
)
154 mutex_acquire(&lock
->dep_map
, subclass
, 0, _RET_IP_
);
155 ret
= rt_mutex_lock_killable(&lock
->lock
, 0);
157 mutex_release(&lock
->dep_map
, 1, _RET_IP_
);
160 EXPORT_SYMBOL(_mutex_lock_killable_nested
);
163 int __lockfunc
_mutex_trylock(struct mutex
*lock
)
165 int ret
= rt_mutex_trylock(&lock
->lock
);
168 mutex_acquire(&lock
->dep_map
, 0, 1, _RET_IP_
);
172 EXPORT_SYMBOL(_mutex_trylock
);
174 void __lockfunc
_mutex_unlock(struct mutex
*lock
)
176 mutex_release(&lock
->dep_map
, 1, _RET_IP_
);
177 rt_mutex_unlock(&lock
->lock
);
179 EXPORT_SYMBOL(_mutex_unlock
);
184 int __lockfunc
rt_write_trylock(rwlock_t
*rwlock
)
186 int ret
= rt_mutex_trylock(&rwlock
->lock
);
189 rwlock_acquire(&rwlock
->dep_map
, 0, 1, _RET_IP_
);
193 EXPORT_SYMBOL(rt_write_trylock
);
195 int __lockfunc
rt_write_trylock_irqsave(rwlock_t
*rwlock
, unsigned long *flags
)
198 return rt_write_trylock(rwlock
);
200 EXPORT_SYMBOL(rt_write_trylock_irqsave
);
202 int __lockfunc
rt_read_trylock(rwlock_t
*rwlock
)
204 struct rt_mutex
*lock
= &rwlock
->lock
;
208 * recursive read locks succeed when current owns the lock
210 if (rt_mutex_real_owner(lock
) != current
|| !rwlock
->read_depth
)
211 ret
= rt_mutex_trylock(lock
);
214 rwlock
->read_depth
++;
215 rwlock_acquire_read(&rwlock
->dep_map
, 0, 1, _RET_IP_
);
220 EXPORT_SYMBOL(rt_read_trylock
);
222 void __lockfunc
rt_write_lock(rwlock_t
*rwlock
)
224 rwlock_acquire(&rwlock
->dep_map
, 0, 0, _RET_IP_
);
225 __rt_spin_lock(&rwlock
->lock
);
227 EXPORT_SYMBOL(rt_write_lock
);
229 void __lockfunc
rt_read_lock(rwlock_t
*rwlock
)
231 struct rt_mutex
*lock
= &rwlock
->lock
;
233 rwlock_acquire_read(&rwlock
->dep_map
, 0, 0, _RET_IP_
);
236 * recursive read locks succeed when current owns the lock
238 if (rt_mutex_real_owner(lock
) != current
|| !rwlock
->read_depth
)
239 __rt_spin_lock(lock
);
240 rwlock
->read_depth
++;
243 EXPORT_SYMBOL(rt_read_lock
);
245 void __lockfunc
rt_write_unlock(rwlock_t
*rwlock
)
247 /* NOTE: we always pass in '1' for nested, for simplicity */
248 rwlock_release(&rwlock
->dep_map
, 1, _RET_IP_
);
249 __rt_spin_unlock(&rwlock
->lock
);
251 EXPORT_SYMBOL(rt_write_unlock
);
253 void __lockfunc
rt_read_unlock(rwlock_t
*rwlock
)
255 rwlock_release(&rwlock
->dep_map
, 1, _RET_IP_
);
257 BUG_ON(rwlock
->read_depth
<= 0);
259 /* Release the lock only when read_depth is down to 0 */
260 if (--rwlock
->read_depth
== 0)
261 __rt_spin_unlock(&rwlock
->lock
);
263 EXPORT_SYMBOL(rt_read_unlock
);
265 unsigned long __lockfunc
rt_write_lock_irqsave(rwlock_t
*rwlock
)
267 rt_write_lock(rwlock
);
271 EXPORT_SYMBOL(rt_write_lock_irqsave
);
273 unsigned long __lockfunc
rt_read_lock_irqsave(rwlock_t
*rwlock
)
275 rt_read_lock(rwlock
);
279 EXPORT_SYMBOL(rt_read_lock_irqsave
);
281 void __rt_rwlock_init(rwlock_t
*rwlock
, char *name
, struct lock_class_key
*key
)
283 #ifdef CONFIG_DEBUG_LOCK_ALLOC
285 * Make sure we are not reinitializing a held lock:
287 debug_check_no_locks_freed((void *)rwlock
, sizeof(*rwlock
));
288 lockdep_init_map(&rwlock
->dep_map
, name
, key
, 0);
290 __rt_mutex_init(&rwlock
->lock
, name
);
291 rwlock
->read_depth
= 0;
293 EXPORT_SYMBOL(__rt_rwlock_init
);
299 void rt_up_write(struct rw_semaphore
*rwsem
)
301 rwsem_release(&rwsem
->dep_map
, 1, _RET_IP_
);
302 rt_mutex_unlock(&rwsem
->lock
);
304 EXPORT_SYMBOL(rt_up_write
);
306 void rt_up_read(struct rw_semaphore
*rwsem
)
308 rwsem_release(&rwsem
->dep_map
, 1, _RET_IP_
);
309 rt_mutex_unlock(&rwsem
->lock
);
311 EXPORT_SYMBOL(rt_up_read
);
314 * downgrade a write lock into a read lock
315 * - just wake up any readers at the front of the queue
317 void rt_downgrade_write(struct rw_semaphore
*rwsem
)
319 BUG_ON(rt_mutex_real_owner(&rwsem
->lock
) != current
);
321 EXPORT_SYMBOL(rt_downgrade_write
);
323 int rt_down_write_trylock(struct rw_semaphore
*rwsem
)
325 int ret
= rt_mutex_trylock(&rwsem
->lock
);
328 rwsem_acquire(&rwsem
->dep_map
, 0, 1, _RET_IP_
);
331 EXPORT_SYMBOL(rt_down_write_trylock
);
333 void rt_down_write(struct rw_semaphore
*rwsem
)
335 rwsem_acquire(&rwsem
->dep_map
, 0, 0, _RET_IP_
);
336 rt_mutex_lock(&rwsem
->lock
);
338 EXPORT_SYMBOL(rt_down_write
);
340 void rt_down_write_nested(struct rw_semaphore
*rwsem
, int subclass
)
342 rwsem_acquire(&rwsem
->dep_map
, subclass
, 0, _RET_IP_
);
343 rt_mutex_lock(&rwsem
->lock
);
345 EXPORT_SYMBOL(rt_down_write_nested
);
347 int rt_down_read_trylock(struct rw_semaphore
*rwsem
)
349 int ret
= rt_mutex_trylock(&rwsem
->lock
);
352 rwsem_acquire(&rwsem
->dep_map
, 0, 1, _RET_IP_
);
355 EXPORT_SYMBOL(rt_down_read_trylock
);
357 static void __rt_down_read(struct rw_semaphore
*rwsem
, int subclass
)
359 rwsem_acquire_read(&rwsem
->dep_map
, subclass
, 0, _RET_IP_
);
360 rt_mutex_lock(&rwsem
->lock
);
363 void rt_down_read(struct rw_semaphore
*rwsem
)
365 __rt_down_read(rwsem
, 0);
367 EXPORT_SYMBOL(rt_down_read
);
369 void rt_down_read_nested(struct rw_semaphore
*rwsem
, int subclass
)
371 __rt_down_read(rwsem
, subclass
);
373 EXPORT_SYMBOL(rt_down_read_nested
);
375 void __rt_rwsem_init(struct rw_semaphore
*rwsem
, char *name
,
376 struct lock_class_key
*key
)
378 #ifdef CONFIG_DEBUG_LOCK_ALLOC
380 * Make sure we are not reinitializing a held lock:
382 debug_check_no_locks_freed((void *)rwsem
, sizeof(*rwsem
));
383 lockdep_init_map(&rwsem
->dep_map
, name
, key
, 0);
385 __rt_mutex_init(&rwsem
->lock
, name
);
387 EXPORT_SYMBOL(__rt_rwsem_init
);
393 * Linux Semaphores implemented via RT-mutexes.
395 * In the down() variants we use the mutex as the semaphore blocking
396 * object: we always acquire it, decrease the counter and keep the lock
397 * locked if we did the 1->0 transition. The next down() will then block.
399 * In the up() path we atomically increase the counter and do the
400 * unlock if we were the one doing the 0->1 transition.
403 static inline void __down_complete(struct semaphore
*sem
)
405 int count
= atomic_dec_return(&sem
->count
);
407 if (unlikely(count
> 0))
408 rt_mutex_unlock(&sem
->lock
);
411 void rt_down(struct semaphore
*sem
)
413 rt_mutex_lock(&sem
->lock
);
414 __down_complete(sem
);
416 EXPORT_SYMBOL(rt_down
);
418 int rt_down_interruptible(struct semaphore
*sem
)
422 ret
= rt_mutex_lock_interruptible(&sem
->lock
, 0);
425 __down_complete(sem
);
428 EXPORT_SYMBOL(rt_down_interruptible
);
430 int rt_down_timeout(struct semaphore
*sem
, long jiff
)
432 struct hrtimer_sleeper t
;
434 unsigned long expires
= jiffies
+ jiff
+ 1;
438 * rt_mutex_slowlock can use an interruptible, but this needs to
439 * be TASK_INTERRUPTIBLE. The down_timeout uses TASK_UNINTERRUPTIBLE.
440 * To handle this we loop if a signal caused the timeout and the
441 * we recalculate the new timeout.
442 * Yes Thomas, this is a hack! But we can fix it right later.
445 jiffies_to_timespec(jiff
, &ts
);
446 hrtimer_init_on_stack(&t
.timer
, HRTIMER_MODE_REL
, CLOCK_MONOTONIC
);
447 t
.timer
._expires
= timespec_to_ktime(ts
);
449 ret
= rt_mutex_timed_lock(&sem
->lock
, &t
, 0);
453 /* signal occured, but the down_timeout doesn't handle them */
454 jiff
= expires
- jiffies
;
459 __down_complete(sem
);
465 EXPORT_SYMBOL(rt_down_timeout
);
468 * try to down the semaphore, 0 on success and 1 on failure. (inverted)
470 int rt_down_trylock(struct semaphore
*sem
)
473 * Here we are a tiny bit different from ordinary Linux semaphores,
474 * because we can get 'transient' locking-failures when say a
475 * process decreases the count from 9 to 8 and locks/releases the
476 * embedded mutex internally. It would be quite complex to remove
477 * these transient failures so lets try it the simple way first:
479 if (rt_mutex_trylock(&sem
->lock
)) {
480 __down_complete(sem
);
485 EXPORT_SYMBOL(rt_down_trylock
);
487 void rt_up(struct semaphore
*sem
)
492 * Disable preemption to make sure a highprio trylock-er cannot
493 * preempt us here and get into an infinite loop:
496 count
= atomic_inc_return(&sem
->count
);
498 * If we did the 0 -> 1 transition then we are the ones to unlock it:
500 if (likely(count
== 1))
501 rt_mutex_unlock(&sem
->lock
);
504 EXPORT_SYMBOL(rt_up
);
506 void __sema_init(struct semaphore
*sem
, int val
,
507 char *name
, char *file
, int line
)
509 atomic_set(&sem
->count
, val
);
512 __rt_mutex_init(&sem
->lock
, name
);
513 rt_mutex_lock(&sem
->lock
);
516 __rt_mutex_init(&sem
->lock
, name
);
520 EXPORT_SYMBOL(__sema_init
);
522 void __init_MUTEX(struct semaphore
*sem
, char *name
, char *file
,
525 __sema_init(sem
, 1, name
, file
, line
);
527 EXPORT_SYMBOL(__init_MUTEX
);