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/interrupt.h>
11 #include <linux/delay.h>
13 static void spin_bug(spinlock_t *lock, const char *msg)
15 static long print_once = 1;
16 struct task_struct *owner = NULL;
18 if (xchg(&print_once, 0)) {
19 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
21 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
22 msg, raw_smp_processor_id(),
23 current->comm, current->pid);
24 printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
27 owner ? owner->comm : "<none>",
28 owner ? owner->pid : -1,
33 * We cannot continue on SMP:
35 // panic("bad locking");
40 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
42 static inline void debug_spin_lock_before(spinlock_t *lock)
44 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
45 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
46 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
47 lock, "cpu recursion");
50 static inline void debug_spin_lock_after(spinlock_t *lock)
52 lock->owner_cpu = raw_smp_processor_id();
53 lock->owner = current;
56 static inline void debug_spin_unlock(spinlock_t *lock)
58 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
59 SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked");
60 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
61 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
63 lock->owner = SPINLOCK_OWNER_INIT;
67 static void __spin_lock_debug(spinlock_t *lock)
73 for (i = 0; i < loops_per_jiffy * HZ; i++) {
74 if (__raw_spin_trylock(&lock->raw_lock))
78 /* lockup suspected: */
81 printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, "
83 raw_smp_processor_id(), current->comm,
90 void _raw_spin_lock(spinlock_t *lock)
92 debug_spin_lock_before(lock);
93 if (unlikely(!__raw_spin_trylock(&lock->raw_lock)))
94 __spin_lock_debug(lock);
95 debug_spin_lock_after(lock);
98 int _raw_spin_trylock(spinlock_t *lock)
100 int ret = __raw_spin_trylock(&lock->raw_lock);
103 debug_spin_lock_after(lock);
106 * Must not happen on UP:
108 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
113 void _raw_spin_unlock(spinlock_t *lock)
115 debug_spin_unlock(lock);
116 __raw_spin_unlock(&lock->raw_lock);
119 static void rwlock_bug(rwlock_t *lock, const char *msg)
121 static long print_once = 1;
123 if (xchg(&print_once, 0)) {
124 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
125 msg, raw_smp_processor_id(), current->comm,
130 * We cannot continue on SMP:
132 panic("bad locking");
137 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
139 static void __read_lock_debug(rwlock_t *lock)
145 for (i = 0; i < loops_per_jiffy * HZ; i++) {
146 if (__raw_read_trylock(&lock->raw_lock))
150 /* lockup suspected: */
153 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
155 raw_smp_processor_id(), current->comm,
162 void _raw_read_lock(rwlock_t *lock)
164 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
165 if (unlikely(!__raw_read_trylock(&lock->raw_lock)))
166 __read_lock_debug(lock);
169 int _raw_read_trylock(rwlock_t *lock)
171 int ret = __raw_read_trylock(&lock->raw_lock);
175 * Must not happen on UP:
177 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
182 void _raw_read_unlock(rwlock_t *lock)
184 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
185 __raw_read_unlock(&lock->raw_lock);
188 static inline void debug_write_lock_before(rwlock_t *lock)
190 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
191 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
192 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
193 lock, "cpu recursion");
196 static inline void debug_write_lock_after(rwlock_t *lock)
198 lock->owner_cpu = raw_smp_processor_id();
199 lock->owner = current;
202 static inline void debug_write_unlock(rwlock_t *lock)
204 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
205 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
206 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
208 lock->owner = SPINLOCK_OWNER_INIT;
209 lock->owner_cpu = -1;
212 static void __write_lock_debug(rwlock_t *lock)
218 for (i = 0; i < loops_per_jiffy * HZ; i++) {
219 if (__raw_write_trylock(&lock->raw_lock))
223 /* lockup suspected: */
226 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
228 raw_smp_processor_id(), current->comm,
235 void _raw_write_lock(rwlock_t *lock)
237 debug_write_lock_before(lock);
238 if (unlikely(!__raw_write_trylock(&lock->raw_lock)))
239 __write_lock_debug(lock);
240 debug_write_lock_after(lock);
243 int _raw_write_trylock(rwlock_t *lock)
245 int ret = __raw_write_trylock(&lock->raw_lock);
248 debug_write_lock_after(lock);
251 * Must not happen on UP:
253 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
258 void _raw_write_unlock(rwlock_t *lock)
260 debug_write_unlock(lock);
261 __raw_write_unlock(&lock->raw_lock);