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 #if 0 /* __write_lock_debug() can lock up - maybe this can too? */
154 static void __read_lock_debug(rwlock_t *lock)
160 for (i = 0; i < loops_per_jiffy * HZ; i++) {
161 if (__raw_read_trylock(&lock->raw_lock))
164 /* lockup suspected: */
167 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
169 raw_smp_processor_id(), current->comm,
170 current->pid, lock, print_tainted());
177 void _raw_read_lock(rwlock_t *lock)
179 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
180 __raw_read_lock(&lock->raw_lock);
183 int _raw_read_trylock(rwlock_t *lock)
185 int ret = __raw_read_trylock(&lock->raw_lock);
189 * Must not happen on UP:
191 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
196 void _raw_read_unlock(rwlock_t *lock)
198 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
199 __raw_read_unlock(&lock->raw_lock);
202 static inline void debug_write_lock_before(rwlock_t *lock)
204 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
205 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
206 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
207 lock, "cpu recursion");
210 static inline void debug_write_lock_after(rwlock_t *lock)
212 lock->owner_cpu = raw_smp_processor_id();
213 lock->owner = current;
216 static inline void debug_write_unlock(rwlock_t *lock)
218 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
219 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
220 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
222 lock->owner = SPINLOCK_OWNER_INIT;
223 lock->owner_cpu = -1;
226 #if 0 /* This can cause lockups */
227 static void __write_lock_debug(rwlock_t *lock)
233 for (i = 0; i < loops_per_jiffy * HZ; i++) {
234 if (__raw_write_trylock(&lock->raw_lock))
237 /* lockup suspected: */
240 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
242 raw_smp_processor_id(), current->comm,
243 current->pid, lock, print_tainted());
250 void _raw_write_lock(rwlock_t *lock)
252 debug_write_lock_before(lock);
253 __raw_write_lock(&lock->raw_lock);
254 debug_write_lock_after(lock);
257 int _raw_write_trylock(rwlock_t *lock)
259 int ret = __raw_write_trylock(&lock->raw_lock);
262 debug_write_lock_after(lock);
265 * Must not happen on UP:
267 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
272 void _raw_write_unlock(rwlock_t *lock)
274 debug_write_unlock(lock);
275 __raw_write_unlock(&lock->raw_lock);