2 * Copyright 2005, Red Hat, Inc., Ingo Molnar
3 * Released under the General Public License (GPL).
5 * This file contains the spinlock/rwlock implementations for
9 #include <linux/spinlock.h>
10 #include <linux/nmi.h>
11 #include <linux/interrupt.h>
12 #include <linux/debug_locks.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
16 void __spin_lock_init(spinlock_t *lock, const char *name,
17 struct lock_class_key *key)
19 #ifdef CONFIG_DEBUG_LOCK_ALLOC
21 * Make sure we are not reinitializing a held lock:
23 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
24 lockdep_init_map(&lock->dep_map, name, key, 0);
26 lock->raw_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
27 lock->magic = SPINLOCK_MAGIC;
28 lock->owner = SPINLOCK_OWNER_INIT;
32 EXPORT_SYMBOL(__spin_lock_init);
34 void __rwlock_init(rwlock_t *lock, const char *name,
35 struct lock_class_key *key)
37 #ifdef CONFIG_DEBUG_LOCK_ALLOC
39 * Make sure we are not reinitializing a held lock:
41 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
42 lockdep_init_map(&lock->dep_map, name, key, 0);
44 lock->raw_lock = (raw_rwlock_t) __RAW_RW_LOCK_UNLOCKED;
45 lock->magic = RWLOCK_MAGIC;
46 lock->owner = SPINLOCK_OWNER_INIT;
50 EXPORT_SYMBOL(__rwlock_init);
52 static void spin_bug(spinlock_t *lock, const char *msg)
54 struct task_struct *owner = NULL;
56 if (!debug_locks_off())
59 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
61 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
62 msg, raw_smp_processor_id(),
63 current->comm, current->pid);
64 printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
67 owner ? owner->comm : "<none>",
68 owner ? owner->pid : -1,
73 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
76 debug_spin_lock_before(spinlock_t *lock)
78 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
79 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
80 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
81 lock, "cpu recursion");
84 static inline void debug_spin_lock_after(spinlock_t *lock)
86 lock->owner_cpu = raw_smp_processor_id();
87 lock->owner = current;
90 static inline void debug_spin_unlock(spinlock_t *lock)
92 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
93 SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked");
94 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
95 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
97 lock->owner = SPINLOCK_OWNER_INIT;
101 static void __spin_lock_debug(spinlock_t *lock)
104 u64 loops = loops_per_jiffy * HZ;
108 for (i = 0; i < loops; i++) {
109 if (__raw_spin_trylock(&lock->raw_lock))
113 /* lockup suspected: */
116 printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, "
118 raw_smp_processor_id(), current->comm,
122 trigger_all_cpu_backtrace();
128 void _raw_spin_lock(spinlock_t *lock)
130 debug_spin_lock_before(lock);
131 if (unlikely(!__raw_spin_trylock(&lock->raw_lock)))
132 __spin_lock_debug(lock);
133 debug_spin_lock_after(lock);
136 int _raw_spin_trylock(spinlock_t *lock)
138 int ret = __raw_spin_trylock(&lock->raw_lock);
141 debug_spin_lock_after(lock);
144 * Must not happen on UP:
146 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
151 void _raw_spin_unlock(spinlock_t *lock)
153 debug_spin_unlock(lock);
154 __raw_spin_unlock(&lock->raw_lock);
157 static void rwlock_bug(rwlock_t *lock, const char *msg)
159 if (!debug_locks_off())
162 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
163 msg, raw_smp_processor_id(), current->comm,
168 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
170 #if 0 /* __write_lock_debug() can lock up - maybe this can too? */
171 static void __read_lock_debug(rwlock_t *lock)
174 u64 loops = loops_per_jiffy * HZ;
178 for (i = 0; i < loops; i++) {
179 if (__raw_read_trylock(&lock->raw_lock))
183 /* lockup suspected: */
186 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
188 raw_smp_processor_id(), current->comm,
196 void _raw_read_lock(rwlock_t *lock)
198 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
199 __raw_read_lock(&lock->raw_lock);
202 int _raw_read_trylock(rwlock_t *lock)
204 int ret = __raw_read_trylock(&lock->raw_lock);
208 * Must not happen on UP:
210 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
215 void _raw_read_unlock(rwlock_t *lock)
217 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
218 __raw_read_unlock(&lock->raw_lock);
221 static inline void debug_write_lock_before(rwlock_t *lock)
223 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
224 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
225 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
226 lock, "cpu recursion");
229 static inline void debug_write_lock_after(rwlock_t *lock)
231 lock->owner_cpu = raw_smp_processor_id();
232 lock->owner = current;
235 static inline void debug_write_unlock(rwlock_t *lock)
237 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
238 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
239 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
241 lock->owner = SPINLOCK_OWNER_INIT;
242 lock->owner_cpu = -1;
245 #if 0 /* This can cause lockups */
246 static void __write_lock_debug(rwlock_t *lock)
249 u64 loops = loops_per_jiffy * HZ;
253 for (i = 0; i < loops; i++) {
254 if (__raw_write_trylock(&lock->raw_lock))
258 /* lockup suspected: */
261 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
263 raw_smp_processor_id(), current->comm,
271 void _raw_write_lock(rwlock_t *lock)
273 debug_write_lock_before(lock);
274 __raw_write_lock(&lock->raw_lock);
275 debug_write_lock_after(lock);
278 int _raw_write_trylock(rwlock_t *lock)
280 int ret = __raw_write_trylock(&lock->raw_lock);
283 debug_write_lock_after(lock);
286 * Must not happen on UP:
288 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
293 void _raw_write_unlock(rwlock_t *lock)
295 debug_write_unlock(lock);
296 __raw_write_unlock(&lock->raw_lock);