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/config.h>
10 #include <linux/spinlock.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
16 static int __init nopanic_setup(char *str)
21 EXPORT_SYMBOL_GPL(nopanic);
22 __setup("dontpanic", nopanic_setup);
25 static void spin_bug(spinlock_t *lock, const char *msg)
27 static long print_once = 1;
28 struct task_struct *owner = NULL;
30 if (xchg(&print_once, 0)) {
31 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
33 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d (%s)\n",
34 msg, raw_smp_processor_id(),
35 current->comm, current->pid, print_tainted());
36 printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
39 owner ? owner->comm : "<none>",
40 owner ? owner->pid : -1,
45 * We cannot continue on SMP:
48 printk("bad locking\n");
55 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
57 static inline void debug_spin_lock_before(spinlock_t *lock)
59 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
60 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
61 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
62 lock, "cpu recursion");
65 static inline void debug_spin_lock_after(spinlock_t *lock)
67 lock->owner_cpu = raw_smp_processor_id();
68 lock->owner = current;
71 static inline void debug_spin_unlock(spinlock_t *lock)
73 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
74 SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked");
75 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
76 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
78 lock->owner = SPINLOCK_OWNER_INIT;
82 static void __spin_lock_debug(spinlock_t *lock)
88 for (i = 0; i < loops_per_jiffy * HZ; i++) {
89 if (__raw_spin_trylock(&lock->raw_lock))
92 /* lockup suspected: */
95 printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, "
97 raw_smp_processor_id(), current->comm,
98 current->pid, lock, print_tainted());
104 void _raw_spin_lock(spinlock_t *lock)
106 debug_spin_lock_before(lock);
107 if (unlikely(!__raw_spin_trylock(&lock->raw_lock)))
108 __spin_lock_debug(lock);
109 debug_spin_lock_after(lock);
112 int _raw_spin_trylock(spinlock_t *lock)
114 int ret = __raw_spin_trylock(&lock->raw_lock);
117 debug_spin_lock_after(lock);
120 * Must not happen on UP:
122 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
127 void _raw_spin_unlock(spinlock_t *lock)
129 debug_spin_unlock(lock);
130 __raw_spin_unlock(&lock->raw_lock);
133 static void rwlock_bug(rwlock_t *lock, const char *msg)
135 static long print_once = 1;
137 if (xchg(&print_once, 0)) {
138 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p (%s)\n",
139 msg, raw_smp_processor_id(), current->comm,
140 current->pid, lock, print_tainted());
144 * We cannot continue on SMP:
146 panic("bad locking");
151 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
153 static void __read_lock_debug(rwlock_t *lock)
159 for (i = 0; i < loops_per_jiffy * HZ; i++) {
160 if (__raw_read_trylock(&lock->raw_lock))
163 /* lockup suspected: */
166 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
168 raw_smp_processor_id(), current->comm,
169 current->pid, lock, print_tainted());
175 void _raw_read_lock(rwlock_t *lock)
177 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
178 if (unlikely(!__raw_read_trylock(&lock->raw_lock)))
179 __read_lock_debug(lock);
182 int _raw_read_trylock(rwlock_t *lock)
184 int ret = __raw_read_trylock(&lock->raw_lock);
188 * Must not happen on UP:
190 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
195 void _raw_read_unlock(rwlock_t *lock)
197 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
198 __raw_read_unlock(&lock->raw_lock);
201 static inline void debug_write_lock_before(rwlock_t *lock)
203 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
204 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
205 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
206 lock, "cpu recursion");
209 static inline void debug_write_lock_after(rwlock_t *lock)
211 lock->owner_cpu = raw_smp_processor_id();
212 lock->owner = current;
215 static inline void debug_write_unlock(rwlock_t *lock)
217 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
218 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
219 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
221 lock->owner = SPINLOCK_OWNER_INIT;
222 lock->owner_cpu = -1;
225 static void __write_lock_debug(rwlock_t *lock)
231 for (i = 0; i < loops_per_jiffy * HZ; i++) {
232 if (__raw_write_trylock(&lock->raw_lock))
235 /* lockup suspected: */
238 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
240 raw_smp_processor_id(), current->comm,
241 current->pid, lock, print_tainted());
247 void _raw_write_lock(rwlock_t *lock)
249 debug_write_lock_before(lock);
250 if (unlikely(!__raw_write_trylock(&lock->raw_lock)))
251 __write_lock_debug(lock);
252 debug_write_lock_after(lock);
255 int _raw_write_trylock(rwlock_t *lock)
257 int ret = __raw_write_trylock(&lock->raw_lock);
260 debug_write_lock_after(lock);
263 * Must not happen on UP:
265 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
270 void _raw_write_unlock(rwlock_t *lock)
272 debug_write_unlock(lock);
273 __raw_write_unlock(&lock->raw_lock);