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/debug_locks.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
15 static void spin_bug(spinlock_t *lock, const char *msg)
17 struct task_struct *owner = NULL;
19 if (!debug_locks_off())
22 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
24 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
25 msg, raw_smp_processor_id(),
26 current->comm, current->pid);
27 printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
30 owner ? owner->comm : "<none>",
31 owner ? owner->pid : -1,
36 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
39 debug_spin_lock_before(spinlock_t *lock)
41 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
42 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
43 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
44 lock, "cpu recursion");
47 static inline void debug_spin_lock_after(spinlock_t *lock)
49 lock->owner_cpu = raw_smp_processor_id();
50 lock->owner = current;
53 static inline void debug_spin_unlock(spinlock_t *lock)
55 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
56 SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked");
57 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
58 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
60 lock->owner = SPINLOCK_OWNER_INIT;
64 static void __spin_lock_debug(spinlock_t *lock)
70 for (i = 0; i < loops_per_jiffy * HZ; i++) {
71 if (__raw_spin_trylock(&lock->raw_lock))
75 /* lockup suspected: */
78 printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, "
80 raw_smp_processor_id(), current->comm,
87 void _raw_spin_lock(spinlock_t *lock)
89 debug_spin_lock_before(lock);
90 if (unlikely(!__raw_spin_trylock(&lock->raw_lock)))
91 __spin_lock_debug(lock);
92 debug_spin_lock_after(lock);
95 int _raw_spin_trylock(spinlock_t *lock)
97 int ret = __raw_spin_trylock(&lock->raw_lock);
100 debug_spin_lock_after(lock);
103 * Must not happen on UP:
105 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
110 void _raw_spin_unlock(spinlock_t *lock)
112 debug_spin_unlock(lock);
113 __raw_spin_unlock(&lock->raw_lock);
116 static void rwlock_bug(rwlock_t *lock, const char *msg)
118 if (!debug_locks_off())
121 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
122 msg, raw_smp_processor_id(), current->comm,
127 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
129 static void __read_lock_debug(rwlock_t *lock)
135 for (i = 0; i < loops_per_jiffy * HZ; i++) {
136 if (__raw_read_trylock(&lock->raw_lock))
140 /* lockup suspected: */
143 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
145 raw_smp_processor_id(), current->comm,
152 void _raw_read_lock(rwlock_t *lock)
154 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
155 if (unlikely(!__raw_read_trylock(&lock->raw_lock)))
156 __read_lock_debug(lock);
159 int _raw_read_trylock(rwlock_t *lock)
161 int ret = __raw_read_trylock(&lock->raw_lock);
165 * Must not happen on UP:
167 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
172 void _raw_read_unlock(rwlock_t *lock)
174 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
175 __raw_read_unlock(&lock->raw_lock);
178 static inline void debug_write_lock_before(rwlock_t *lock)
180 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
181 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
182 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
183 lock, "cpu recursion");
186 static inline void debug_write_lock_after(rwlock_t *lock)
188 lock->owner_cpu = raw_smp_processor_id();
189 lock->owner = current;
192 static inline void debug_write_unlock(rwlock_t *lock)
194 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
195 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
196 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
198 lock->owner = SPINLOCK_OWNER_INIT;
199 lock->owner_cpu = -1;
202 static void __write_lock_debug(rwlock_t *lock)
208 for (i = 0; i < loops_per_jiffy * HZ; i++) {
209 if (__raw_write_trylock(&lock->raw_lock))
213 /* lockup suspected: */
216 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
218 raw_smp_processor_id(), current->comm,
225 void _raw_write_lock(rwlock_t *lock)
227 debug_write_lock_before(lock);
228 if (unlikely(!__raw_write_trylock(&lock->raw_lock)))
229 __write_lock_debug(lock);
230 debug_write_lock_after(lock);
233 int _raw_write_trylock(rwlock_t *lock)
235 int ret = __raw_write_trylock(&lock->raw_lock);
238 debug_write_lock_after(lock);
241 * Must not happen on UP:
243 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
248 void _raw_write_unlock(rwlock_t *lock)
250 debug_write_unlock(lock);
251 __raw_write_unlock(&lock->raw_lock);