blob: 1967297aee8b49e344c4eec21ffe22df5f9d7791 [file] [log] [blame]
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07001/*
2 * Copyright 2005, Red Hat, Inc., Ingo Molnar
3 * Released under the General Public License (GPL).
4 *
5 * This file contains the spinlock/rwlock implementations for
6 * DEBUG_SPINLOCK.
7 */
8
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07009#include <linux/spinlock.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010010#include <linux/nmi.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070011#include <linux/interrupt.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070012#include <linux/debug_locks.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070013#include <linux/delay.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050014#include <linux/export.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070015
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010016void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
17 struct lock_class_key *key)
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070018{
19#ifdef CONFIG_DEBUG_LOCK_ALLOC
20 /*
21 * Make sure we are not reinitializing a held lock:
22 */
23 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040024 lockdep_init_map(&lock->dep_map, name, key, 0);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070025#endif
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010026 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070027 lock->magic = SPINLOCK_MAGIC;
28 lock->owner = SPINLOCK_OWNER_INIT;
29 lock->owner_cpu = -1;
30}
31
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010032EXPORT_SYMBOL(__raw_spin_lock_init);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070033
34void __rwlock_init(rwlock_t *lock, const char *name,
35 struct lock_class_key *key)
36{
37#ifdef CONFIG_DEBUG_LOCK_ALLOC
38 /*
39 * Make sure we are not reinitializing a held lock:
40 */
41 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040042 lockdep_init_map(&lock->dep_map, name, key, 0);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070043#endif
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +010044 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070045 lock->magic = RWLOCK_MAGIC;
46 lock->owner = SPINLOCK_OWNER_INIT;
47 lock->owner_cpu = -1;
48}
49
50EXPORT_SYMBOL(__rwlock_init);
51
Akinobu Mita4e101b02011-10-31 17:12:29 -070052static void spin_dump(raw_spinlock_t *lock, const char *msg)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070053{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070054 struct task_struct *owner = NULL;
55
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070056 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(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070060 current->comm, task_pid_nr(current));
Stephen Boyd9e684932012-07-25 18:30:45 -070061 printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, "
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070062 ".owner_cpu: %d\n",
63 lock, lock->magic,
64 owner ? owner->comm : "<none>",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070065 owner ? task_pid_nr(owner) : -1,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070066 lock->owner_cpu);
67 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070068}
69
Akinobu Mita4e101b02011-10-31 17:12:29 -070070static void spin_bug(raw_spinlock_t *lock, const char *msg)
71{
72 if (!debug_locks_off())
73 return;
74
75 spin_dump(lock, msg);
76}
77
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070078#define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
79
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070080static inline void
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010081debug_spin_lock_before(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070082{
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");
87}
88
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010089static inline void debug_spin_lock_after(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070090{
91 lock->owner_cpu = raw_smp_processor_id();
92 lock->owner = current;
93}
94
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010095static inline void debug_spin_unlock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070096{
97 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010098 SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070099 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;
104}
105
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +0100106static void __spin_lock_debug(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700107{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700108 u64 i;
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700109 u64 loops = loops_per_jiffy * HZ;
110 int print_once = 1;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700111
112 for (;;) {
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700113 for (i = 0; i < loops; i++) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100114 if (arch_spin_trylock(&lock->raw_lock))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700115 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800116 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700117 }
118 /* lockup suspected: */
119 if (print_once) {
120 print_once = 0;
Akinobu Mita4e101b02011-10-31 17:12:29 -0700121 spin_dump(lock, "lockup");
Andrew Mortonbb81a092006-12-07 02:14:01 +0100122#ifdef CONFIG_SMP
123 trigger_all_cpu_backtrace();
124#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700125 }
126 }
127}
128
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100129void do_raw_spin_lock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700130{
131 debug_spin_lock_before(lock);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100132 if (unlikely(!arch_spin_trylock(&lock->raw_lock)))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700133 __spin_lock_debug(lock);
134 debug_spin_lock_after(lock);
135}
136
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100137int do_raw_spin_trylock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700138{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100139 int ret = arch_spin_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700140
141 if (ret)
142 debug_spin_lock_after(lock);
143#ifndef CONFIG_SMP
144 /*
145 * Must not happen on UP:
146 */
147 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
148#endif
149 return ret;
150}
151
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100152void do_raw_spin_unlock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700153{
154 debug_spin_unlock(lock);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100155 arch_spin_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700156}
157
158static void rwlock_bug(rwlock_t *lock, const char *msg)
159{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700160 if (!debug_locks_off())
161 return;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700162
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700163 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
164 msg, raw_smp_processor_id(), current->comm,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700165 task_pid_nr(current), lock);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700166 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700167}
168
169#define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
170
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700171#if 0 /* __write_lock_debug() can lock up - maybe this can too? */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700172static void __read_lock_debug(rwlock_t *lock)
173{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700174 u64 i;
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700175 u64 loops = loops_per_jiffy * HZ;
176 int print_once = 1;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700177
178 for (;;) {
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700179 for (i = 0; i < loops; i++) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100180 if (arch_read_trylock(&lock->raw_lock))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700181 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800182 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700183 }
184 /* lockup suspected: */
185 if (print_once) {
186 print_once = 0;
Dave Jones51989b92006-01-09 20:51:32 -0800187 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
188 "%s/%d, %p\n",
Ingo Molnarbb44f112005-12-20 11:54:17 +0100189 raw_smp_processor_id(), current->comm,
190 current->pid, lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700191 dump_stack();
192 }
193 }
194}
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700195#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700196
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100197void do_raw_read_lock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700198{
199 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnere5931942009-12-03 20:08:46 +0100200 arch_read_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700201}
202
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100203int do_raw_read_trylock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700204{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100205 int ret = arch_read_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700206
207#ifndef CONFIG_SMP
208 /*
209 * Must not happen on UP:
210 */
211 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
212#endif
213 return ret;
214}
215
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100216void do_raw_read_unlock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700217{
218 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnere5931942009-12-03 20:08:46 +0100219 arch_read_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700220}
221
222static inline void debug_write_lock_before(rwlock_t *lock)
223{
224 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
225 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
226 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
227 lock, "cpu recursion");
228}
229
230static inline void debug_write_lock_after(rwlock_t *lock)
231{
232 lock->owner_cpu = raw_smp_processor_id();
233 lock->owner = current;
234}
235
236static inline void debug_write_unlock(rwlock_t *lock)
237{
238 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
239 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
240 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
241 lock, "wrong CPU");
242 lock->owner = SPINLOCK_OWNER_INIT;
243 lock->owner_cpu = -1;
244}
245
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700246#if 0 /* This can cause lockups */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700247static void __write_lock_debug(rwlock_t *lock)
248{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700249 u64 i;
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700250 u64 loops = loops_per_jiffy * HZ;
251 int print_once = 1;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700252
253 for (;;) {
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700254 for (i = 0; i < loops; i++) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100255 if (arch_write_trylock(&lock->raw_lock))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700256 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800257 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700258 }
259 /* lockup suspected: */
260 if (print_once) {
261 print_once = 0;
Dave Jones51989b92006-01-09 20:51:32 -0800262 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
263 "%s/%d, %p\n",
Ingo Molnarbb44f112005-12-20 11:54:17 +0100264 raw_smp_processor_id(), current->comm,
265 current->pid, lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700266 dump_stack();
267 }
268 }
269}
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700270#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700271
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100272void do_raw_write_lock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700273{
274 debug_write_lock_before(lock);
Thomas Gleixnere5931942009-12-03 20:08:46 +0100275 arch_write_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700276 debug_write_lock_after(lock);
277}
278
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100279int do_raw_write_trylock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700280{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100281 int ret = arch_write_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700282
283 if (ret)
284 debug_write_lock_after(lock);
285#ifndef CONFIG_SMP
286 /*
287 * Must not happen on UP:
288 */
289 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
290#endif
291 return ret;
292}
293
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100294void do_raw_write_unlock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700295{
296 debug_write_unlock(lock);
Thomas Gleixnere5931942009-12-03 20:08:46 +0100297 arch_write_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700298}