kvm: nVMX: Fix kernel panics induced by illegal INVEPT/INVVPID types
[linux/fpc-iii.git] / kernel / locking / spinlock_debug.c
blob0374a596cffac8439d36875ba2bd248753caf886
1 /*
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
6 * DEBUG_SPINLOCK.
7 */
9 #include <linux/spinlock.h>
10 #include <linux/nmi.h>
11 #include <linux/interrupt.h>
12 #include <linux/debug_locks.h>
13 #include <linux/delay.h>
14 #include <linux/export.h>
16 void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
17 struct lock_class_key *key)
19 #ifdef CONFIG_DEBUG_LOCK_ALLOC
21 * Make sure we are not reinitializing a held lock:
23 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
24 lockdep_init_map(&lock->dep_map, name, key, 0);
25 #endif
26 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
27 lock->magic = SPINLOCK_MAGIC;
28 lock->owner = SPINLOCK_OWNER_INIT;
29 lock->owner_cpu = -1;
32 EXPORT_SYMBOL(__raw_spin_lock_init);
34 void __rwlock_init(rwlock_t *lock, const char *name,
35 struct lock_class_key *key)
37 #ifdef CONFIG_DEBUG_LOCK_ALLOC
39 * Make sure we are not reinitializing a held lock:
41 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
42 lockdep_init_map(&lock->dep_map, name, key, 0);
43 #endif
44 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
45 lock->magic = RWLOCK_MAGIC;
46 lock->owner = SPINLOCK_OWNER_INIT;
47 lock->owner_cpu = -1;
50 EXPORT_SYMBOL(__rwlock_init);
52 static void spin_dump(raw_spinlock_t *lock, const char *msg)
54 struct task_struct *owner = NULL;
56 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
57 owner = lock->owner;
58 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
59 msg, raw_smp_processor_id(),
60 current->comm, task_pid_nr(current));
61 printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, "
62 ".owner_cpu: %d\n",
63 lock, lock->magic,
64 owner ? owner->comm : "<none>",
65 owner ? task_pid_nr(owner) : -1,
66 lock->owner_cpu);
67 dump_stack();
70 static void spin_bug(raw_spinlock_t *lock, const char *msg)
72 if (!debug_locks_off())
73 return;
75 spin_dump(lock, msg);
78 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
80 static inline void
81 debug_spin_lock_before(raw_spinlock_t *lock)
83 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
84 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
85 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
86 lock, "cpu recursion");
89 static inline void debug_spin_lock_after(raw_spinlock_t *lock)
91 lock->owner_cpu = raw_smp_processor_id();
92 lock->owner = current;
95 static inline void debug_spin_unlock(raw_spinlock_t *lock)
97 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
98 SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
99 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
100 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
101 lock, "wrong CPU");
102 lock->owner = SPINLOCK_OWNER_INIT;
103 lock->owner_cpu = -1;
106 static void __spin_lock_debug(raw_spinlock_t *lock)
108 u64 i;
109 u64 loops = loops_per_jiffy * HZ;
111 for (i = 0; i < loops; i++) {
112 if (arch_spin_trylock(&lock->raw_lock))
113 return;
114 __delay(1);
116 /* lockup suspected: */
117 spin_dump(lock, "lockup suspected");
118 #ifdef CONFIG_SMP
119 trigger_all_cpu_backtrace();
120 #endif
123 * The trylock above was causing a livelock. Give the lower level arch
124 * specific lock code a chance to acquire the lock. We have already
125 * printed a warning/backtrace at this point. The non-debug arch
126 * specific code might actually succeed in acquiring the lock. If it is
127 * not successful, the end-result is the same - there is no forward
128 * progress.
130 arch_spin_lock(&lock->raw_lock);
133 void do_raw_spin_lock(raw_spinlock_t *lock)
135 debug_spin_lock_before(lock);
136 if (unlikely(!arch_spin_trylock(&lock->raw_lock)))
137 __spin_lock_debug(lock);
138 debug_spin_lock_after(lock);
141 int do_raw_spin_trylock(raw_spinlock_t *lock)
143 int ret = arch_spin_trylock(&lock->raw_lock);
145 if (ret)
146 debug_spin_lock_after(lock);
147 #ifndef CONFIG_SMP
149 * Must not happen on UP:
151 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
152 #endif
153 return ret;
156 void do_raw_spin_unlock(raw_spinlock_t *lock)
158 debug_spin_unlock(lock);
159 arch_spin_unlock(&lock->raw_lock);
162 static void rwlock_bug(rwlock_t *lock, const char *msg)
164 if (!debug_locks_off())
165 return;
167 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
168 msg, raw_smp_processor_id(), current->comm,
169 task_pid_nr(current), lock);
170 dump_stack();
173 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
175 #if 0 /* __write_lock_debug() can lock up - maybe this can too? */
176 static void __read_lock_debug(rwlock_t *lock)
178 u64 i;
179 u64 loops = loops_per_jiffy * HZ;
180 int print_once = 1;
182 for (;;) {
183 for (i = 0; i < loops; i++) {
184 if (arch_read_trylock(&lock->raw_lock))
185 return;
186 __delay(1);
188 /* lockup suspected: */
189 if (print_once) {
190 print_once = 0;
191 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
192 "%s/%d, %p\n",
193 raw_smp_processor_id(), current->comm,
194 current->pid, lock);
195 dump_stack();
199 #endif
201 void do_raw_read_lock(rwlock_t *lock)
203 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
204 arch_read_lock(&lock->raw_lock);
207 int do_raw_read_trylock(rwlock_t *lock)
209 int ret = arch_read_trylock(&lock->raw_lock);
211 #ifndef CONFIG_SMP
213 * Must not happen on UP:
215 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
216 #endif
217 return ret;
220 void do_raw_read_unlock(rwlock_t *lock)
222 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
223 arch_read_unlock(&lock->raw_lock);
226 static inline void debug_write_lock_before(rwlock_t *lock)
228 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
229 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
230 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
231 lock, "cpu recursion");
234 static inline void debug_write_lock_after(rwlock_t *lock)
236 lock->owner_cpu = raw_smp_processor_id();
237 lock->owner = current;
240 static inline void debug_write_unlock(rwlock_t *lock)
242 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
243 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
244 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
245 lock, "wrong CPU");
246 lock->owner = SPINLOCK_OWNER_INIT;
247 lock->owner_cpu = -1;
250 #if 0 /* This can cause lockups */
251 static void __write_lock_debug(rwlock_t *lock)
253 u64 i;
254 u64 loops = loops_per_jiffy * HZ;
255 int print_once = 1;
257 for (;;) {
258 for (i = 0; i < loops; i++) {
259 if (arch_write_trylock(&lock->raw_lock))
260 return;
261 __delay(1);
263 /* lockup suspected: */
264 if (print_once) {
265 print_once = 0;
266 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
267 "%s/%d, %p\n",
268 raw_smp_processor_id(), current->comm,
269 current->pid, lock);
270 dump_stack();
274 #endif
276 void do_raw_write_lock(rwlock_t *lock)
278 debug_write_lock_before(lock);
279 arch_write_lock(&lock->raw_lock);
280 debug_write_lock_after(lock);
283 int do_raw_write_trylock(rwlock_t *lock)
285 int ret = arch_write_trylock(&lock->raw_lock);
287 if (ret)
288 debug_write_lock_after(lock);
289 #ifndef CONFIG_SMP
291 * Must not happen on UP:
293 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
294 #endif
295 return ret;
298 void do_raw_write_unlock(rwlock_t *lock)
300 debug_write_unlock(lock);
301 arch_write_unlock(&lock->raw_lock);