blob: 955a47b6aa9315c23068d16ffe1bfec71ebffd7f [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>
Syed Rameez Mustafa1c014f32013-07-15 11:52:09 -070015#include <linux/bug.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070016
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010017void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
18 struct lock_class_key *key)
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070019{
20#ifdef CONFIG_DEBUG_LOCK_ALLOC
21 /*
22 * Make sure we are not reinitializing a held lock:
23 */
24 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040025 lockdep_init_map(&lock->dep_map, name, key, 0);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070026#endif
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010027 lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070028 lock->magic = SPINLOCK_MAGIC;
29 lock->owner = SPINLOCK_OWNER_INIT;
30 lock->owner_cpu = -1;
31}
32
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010033EXPORT_SYMBOL(__raw_spin_lock_init);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070034
35void __rwlock_init(rwlock_t *lock, const char *name,
36 struct lock_class_key *key)
37{
38#ifdef CONFIG_DEBUG_LOCK_ALLOC
39 /*
40 * Make sure we are not reinitializing a held lock:
41 */
42 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040043 lockdep_init_map(&lock->dep_map, name, key, 0);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070044#endif
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +010045 lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070046 lock->magic = RWLOCK_MAGIC;
47 lock->owner = SPINLOCK_OWNER_INIT;
48 lock->owner_cpu = -1;
49}
50
51EXPORT_SYMBOL(__rwlock_init);
52
Akinobu Mita4e101b02011-10-31 17:12:29 -070053static void spin_dump(raw_spinlock_t *lock, const char *msg)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070054{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070055 struct task_struct *owner = NULL;
56
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070057 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
58 owner = lock->owner;
59 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
60 msg, raw_smp_processor_id(),
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070061 current->comm, task_pid_nr(current));
Stephen Boyd9e684932012-07-25 18:30:45 -070062 printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, "
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070063 ".owner_cpu: %d\n",
64 lock, lock->magic,
65 owner ? owner->comm : "<none>",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070066 owner ? task_pid_nr(owner) : -1,
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070067 lock->owner_cpu);
Syed Rameez Mustafa1c014f32013-07-15 11:52:09 -070068 BUG_ON(PANIC_CORRUPTION);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070069 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070070}
71
Akinobu Mita4e101b02011-10-31 17:12:29 -070072static void spin_bug(raw_spinlock_t *lock, const char *msg)
73{
74 if (!debug_locks_off())
75 return;
76
77 spin_dump(lock, msg);
78}
79
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070080#define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
81
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070082static inline void
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010083debug_spin_lock_before(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070084{
85 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
86 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
87 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
88 lock, "cpu recursion");
89}
90
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010091static inline void debug_spin_lock_after(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070092{
93 lock->owner_cpu = raw_smp_processor_id();
94 lock->owner = current;
95}
96
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +010097static inline void debug_spin_unlock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070098{
99 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +0100100 SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700101 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
102 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
103 lock, "wrong CPU");
104 lock->owner = SPINLOCK_OWNER_INIT;
105 lock->owner_cpu = -1;
106}
107
Thomas Gleixnerc2f21ce2009-12-02 20:02:59 +0100108static void __spin_lock_debug(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700109{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700110 u64 i;
Syed Rameez Mustafa376a7502013-12-10 12:49:06 -0800111 u64 loops = (loops_per_jiffy * HZ);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700112
Vikram Mulukutla03747622012-08-02 13:18:15 -0700113 for (i = 0; i < loops; i++) {
114 if (arch_spin_trylock(&lock->raw_lock))
115 return;
116 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700117 }
Vikram Mulukutla03747622012-08-02 13:18:15 -0700118 /* lockup suspected: */
119 spin_dump(lock, "lockup");
120#ifdef CONFIG_SMP
121 trigger_all_cpu_backtrace();
122#endif
123
124 /*
125 * The trylock above was causing a livelock. Give the lower level arch
126 * specific lock code a chance to acquire the lock. We have already
127 * printed a warning/backtrace at this point. The non-debug arch
128 * specific code might actually succeed in acquiring the lock. If it is
129 * not successful, the end-result is the same - there is no forward
130 * progress.
131 */
132 arch_spin_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700133}
134
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100135void do_raw_spin_lock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700136{
137 debug_spin_lock_before(lock);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100138 if (unlikely(!arch_spin_trylock(&lock->raw_lock)))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700139 __spin_lock_debug(lock);
140 debug_spin_lock_after(lock);
141}
142
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100143int do_raw_spin_trylock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700144{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100145 int ret = arch_spin_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700146
147 if (ret)
148 debug_spin_lock_after(lock);
149#ifndef CONFIG_SMP
150 /*
151 * Must not happen on UP:
152 */
153 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
154#endif
155 return ret;
156}
157
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100158void do_raw_spin_unlock(raw_spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700159{
160 debug_spin_unlock(lock);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100161 arch_spin_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700162}
163
164static void rwlock_bug(rwlock_t *lock, const char *msg)
165{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700166 if (!debug_locks_off())
167 return;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700168
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700169 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
170 msg, raw_smp_processor_id(), current->comm,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700171 task_pid_nr(current), lock);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700172 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700173}
174
175#define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
176
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700177#if 0 /* __write_lock_debug() can lock up - maybe this can too? */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700178static void __read_lock_debug(rwlock_t *lock)
179{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700180 u64 i;
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700181 u64 loops = loops_per_jiffy * HZ;
182 int print_once = 1;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700183
184 for (;;) {
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700185 for (i = 0; i < loops; i++) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100186 if (arch_read_trylock(&lock->raw_lock))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700187 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800188 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700189 }
190 /* lockup suspected: */
191 if (print_once) {
192 print_once = 0;
Dave Jones51989b92006-01-09 20:51:32 -0800193 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
194 "%s/%d, %p\n",
Ingo Molnarbb44f112005-12-20 11:54:17 +0100195 raw_smp_processor_id(), current->comm,
196 current->pid, lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700197 dump_stack();
198 }
199 }
200}
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700201#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700202
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100203void do_raw_read_lock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700204{
205 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnere5931942009-12-03 20:08:46 +0100206 arch_read_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700207}
208
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100209int do_raw_read_trylock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700210{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100211 int ret = arch_read_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700212
213#ifndef CONFIG_SMP
214 /*
215 * Must not happen on UP:
216 */
217 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
218#endif
219 return ret;
220}
221
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100222void do_raw_read_unlock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700223{
224 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
Thomas Gleixnere5931942009-12-03 20:08:46 +0100225 arch_read_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700226}
227
228static inline void debug_write_lock_before(rwlock_t *lock)
229{
230 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
231 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
232 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
233 lock, "cpu recursion");
234}
235
236static inline void debug_write_lock_after(rwlock_t *lock)
237{
238 lock->owner_cpu = raw_smp_processor_id();
239 lock->owner = current;
240}
241
242static inline void debug_write_unlock(rwlock_t *lock)
243{
244 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
245 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
246 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
247 lock, "wrong CPU");
248 lock->owner = SPINLOCK_OWNER_INIT;
249 lock->owner_cpu = -1;
250}
251
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700252#if 0 /* This can cause lockups */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700253static void __write_lock_debug(rwlock_t *lock)
254{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700255 u64 i;
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700256 u64 loops = loops_per_jiffy * HZ;
257 int print_once = 1;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700258
259 for (;;) {
Chuck Ebbertc22f0082006-09-29 01:59:14 -0700260 for (i = 0; i < loops; i++) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100261 if (arch_write_trylock(&lock->raw_lock))
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700262 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800263 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700264 }
265 /* lockup suspected: */
266 if (print_once) {
267 print_once = 0;
Dave Jones51989b92006-01-09 20:51:32 -0800268 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
269 "%s/%d, %p\n",
Ingo Molnarbb44f112005-12-20 11:54:17 +0100270 raw_smp_processor_id(), current->comm,
271 current->pid, lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700272 dump_stack();
273 }
274 }
275}
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700276#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700277
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100278void do_raw_write_lock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700279{
280 debug_write_lock_before(lock);
Thomas Gleixnere5931942009-12-03 20:08:46 +0100281 arch_write_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700282 debug_write_lock_after(lock);
283}
284
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100285int do_raw_write_trylock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700286{
Thomas Gleixnere5931942009-12-03 20:08:46 +0100287 int ret = arch_write_trylock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700288
289 if (ret)
290 debug_write_lock_after(lock);
291#ifndef CONFIG_SMP
292 /*
293 * Must not happen on UP:
294 */
295 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
296#endif
297 return ret;
298}
299
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100300void do_raw_write_unlock(rwlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700301{
302 debug_write_unlock(lock);
Thomas Gleixnere5931942009-12-03 20:08:46 +0100303 arch_write_unlock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700304}