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>
14 static void spin_bug(spinlock_t *lock, const char *msg)
16 static long print_once = 1;
17 struct task_struct *owner = NULL;
19 if (xchg(&print_once, 0)) {
20 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
22 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
23 msg, raw_smp_processor_id(),
24 current->comm, current->pid);
25 printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
28 owner ? owner->comm : "<none>",
29 owner ? owner->pid : -1,
34 * We cannot continue on SMP:
36 // panic("bad locking");
41 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
43 static inline void debug_spin_lock_before(spinlock_t *lock)
45 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
46 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
47 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
48 lock, "cpu recursion");
51 static inline void debug_spin_lock_after(spinlock_t *lock)
53 lock->owner_cpu = raw_smp_processor_id();
54 lock->owner = current;
57 static inline void debug_spin_unlock(spinlock_t *lock)
59 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
60 SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked");
61 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
62 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
64 lock->owner = SPINLOCK_OWNER_INIT;
68 static void __spin_lock_debug(spinlock_t *lock)
74 for (i = 0; i < loops_per_jiffy * HZ; i++) {
75 if (__raw_spin_trylock(&lock->raw_lock))
79 /* lockup suspected: */
82 printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, "
84 raw_smp_processor_id(), current->comm,
91 void _raw_spin_lock(spinlock_t *lock)
93 debug_spin_lock_before(lock);
94 if (unlikely(!__raw_spin_trylock(&lock->raw_lock)))
95 __spin_lock_debug(lock);
96 debug_spin_lock_after(lock);
99 int _raw_spin_trylock(spinlock_t *lock)
101 int ret = __raw_spin_trylock(&lock->raw_lock);
104 debug_spin_lock_after(lock);
107 * Must not happen on UP:
109 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
114 void _raw_spin_unlock(spinlock_t *lock)
116 debug_spin_unlock(lock);
117 __raw_spin_unlock(&lock->raw_lock);
120 static void rwlock_bug(rwlock_t *lock, const char *msg)
122 static long print_once = 1;
124 if (xchg(&print_once, 0)) {
125 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
126 msg, raw_smp_processor_id(), current->comm,
131 * We cannot continue on SMP:
133 panic("bad locking");
138 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
140 static void __read_lock_debug(rwlock_t *lock)
146 for (i = 0; i < loops_per_jiffy * HZ; i++) {
147 if (__raw_read_trylock(&lock->raw_lock))
151 /* lockup suspected: */
154 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
156 raw_smp_processor_id(), current->comm,
163 void _raw_read_lock(rwlock_t *lock)
165 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
166 if (unlikely(!__raw_read_trylock(&lock->raw_lock)))
167 __read_lock_debug(lock);
170 int _raw_read_trylock(rwlock_t *lock)
172 int ret = __raw_read_trylock(&lock->raw_lock);
176 * Must not happen on UP:
178 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
183 void _raw_read_unlock(rwlock_t *lock)
185 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
186 __raw_read_unlock(&lock->raw_lock);
189 static inline void debug_write_lock_before(rwlock_t *lock)
191 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
192 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
193 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
194 lock, "cpu recursion");
197 static inline void debug_write_lock_after(rwlock_t *lock)
199 lock->owner_cpu = raw_smp_processor_id();
200 lock->owner = current;
203 static inline void debug_write_unlock(rwlock_t *lock)
205 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
206 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
207 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
209 lock->owner = SPINLOCK_OWNER_INIT;
210 lock->owner_cpu = -1;
213 static void __write_lock_debug(rwlock_t *lock)
219 for (i = 0; i < loops_per_jiffy * HZ; i++) {
220 if (__raw_write_trylock(&lock->raw_lock))
224 /* lockup suspected: */
227 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
229 raw_smp_processor_id(), current->comm,
236 void _raw_write_lock(rwlock_t *lock)
238 debug_write_lock_before(lock);
239 if (unlikely(!__raw_write_trylock(&lock->raw_lock)))
240 __write_lock_debug(lock);
241 debug_write_lock_after(lock);
244 int _raw_write_trylock(rwlock_t *lock)
246 int ret = __raw_write_trylock(&lock->raw_lock);
249 debug_write_lock_after(lock);
252 * Must not happen on UP:
254 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
259 void _raw_write_unlock(rwlock_t *lock)
261 debug_write_unlock(lock);
262 __raw_write_unlock(&lock->raw_lock);