blob: 58c577dd82e5240e551d151b73ea31c55de8c309 [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>
10#include <linux/interrupt.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070011#include <linux/debug_locks.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070012#include <linux/delay.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070013#include <linux/module.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070014
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070015void __spin_lock_init(spinlock_t *lock, const char *name,
16 struct lock_class_key *key)
17{
18#ifdef CONFIG_DEBUG_LOCK_ALLOC
19 /*
20 * Make sure we are not reinitializing a held lock:
21 */
22 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
23 lockdep_init_map(&lock->dep_map, name, key);
24#endif
25 lock->raw_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
26 lock->magic = SPINLOCK_MAGIC;
27 lock->owner = SPINLOCK_OWNER_INIT;
28 lock->owner_cpu = -1;
29}
30
31EXPORT_SYMBOL(__spin_lock_init);
32
33void __rwlock_init(rwlock_t *lock, const char *name,
34 struct lock_class_key *key)
35{
36#ifdef CONFIG_DEBUG_LOCK_ALLOC
37 /*
38 * Make sure we are not reinitializing a held lock:
39 */
40 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
41 lockdep_init_map(&lock->dep_map, name, key);
42#endif
43 lock->raw_lock = (raw_rwlock_t) __RAW_RW_LOCK_UNLOCKED;
44 lock->magic = RWLOCK_MAGIC;
45 lock->owner = SPINLOCK_OWNER_INIT;
46 lock->owner_cpu = -1;
47}
48
49EXPORT_SYMBOL(__rwlock_init);
50
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070051static void spin_bug(spinlock_t *lock, const char *msg)
52{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070053 struct task_struct *owner = NULL;
54
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070055 if (!debug_locks_off())
56 return;
57
58 if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
59 owner = lock->owner;
60 printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
61 msg, raw_smp_processor_id(),
62 current->comm, current->pid);
63 printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
64 ".owner_cpu: %d\n",
65 lock, lock->magic,
66 owner ? owner->comm : "<none>",
67 owner ? owner->pid : -1,
68 lock->owner_cpu);
69 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070070}
71
72#define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
73
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070074static inline void
75debug_spin_lock_before(spinlock_t *lock)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070076{
77 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
78 SPIN_BUG_ON(lock->owner == current, lock, "recursion");
79 SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
80 lock, "cpu recursion");
81}
82
83static inline void debug_spin_lock_after(spinlock_t *lock)
84{
85 lock->owner_cpu = raw_smp_processor_id();
86 lock->owner = current;
87}
88
89static inline void debug_spin_unlock(spinlock_t *lock)
90{
91 SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
92 SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked");
93 SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
94 SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
95 lock, "wrong CPU");
96 lock->owner = SPINLOCK_OWNER_INIT;
97 lock->owner_cpu = -1;
98}
99
100static void __spin_lock_debug(spinlock_t *lock)
101{
102 int print_once = 1;
103 u64 i;
104
105 for (;;) {
106 for (i = 0; i < loops_per_jiffy * HZ; i++) {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700107 if (__raw_spin_trylock(&lock->raw_lock))
108 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800109 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700110 }
111 /* lockup suspected: */
112 if (print_once) {
113 print_once = 0;
Dave Jones51989b92006-01-09 20:51:32 -0800114 printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, "
115 "%s/%d, %p\n",
Ingo Molnarbb44f112005-12-20 11:54:17 +0100116 raw_smp_processor_id(), current->comm,
117 current->pid, lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700118 dump_stack();
119 }
120 }
121}
122
123void _raw_spin_lock(spinlock_t *lock)
124{
125 debug_spin_lock_before(lock);
126 if (unlikely(!__raw_spin_trylock(&lock->raw_lock)))
127 __spin_lock_debug(lock);
128 debug_spin_lock_after(lock);
129}
130
131int _raw_spin_trylock(spinlock_t *lock)
132{
133 int ret = __raw_spin_trylock(&lock->raw_lock);
134
135 if (ret)
136 debug_spin_lock_after(lock);
137#ifndef CONFIG_SMP
138 /*
139 * Must not happen on UP:
140 */
141 SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
142#endif
143 return ret;
144}
145
146void _raw_spin_unlock(spinlock_t *lock)
147{
148 debug_spin_unlock(lock);
149 __raw_spin_unlock(&lock->raw_lock);
150}
151
152static void rwlock_bug(rwlock_t *lock, const char *msg)
153{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700154 if (!debug_locks_off())
155 return;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700156
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700157 printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
158 msg, raw_smp_processor_id(), current->comm,
159 current->pid, lock);
160 dump_stack();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700161}
162
163#define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
164
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700165#if 0 /* __write_lock_debug() can lock up - maybe this can too? */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700166static void __read_lock_debug(rwlock_t *lock)
167{
168 int print_once = 1;
169 u64 i;
170
171 for (;;) {
172 for (i = 0; i < loops_per_jiffy * HZ; i++) {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700173 if (__raw_read_trylock(&lock->raw_lock))
174 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800175 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700176 }
177 /* lockup suspected: */
178 if (print_once) {
179 print_once = 0;
Dave Jones51989b92006-01-09 20:51:32 -0800180 printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
181 "%s/%d, %p\n",
Ingo Molnarbb44f112005-12-20 11:54:17 +0100182 raw_smp_processor_id(), current->comm,
183 current->pid, lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700184 dump_stack();
185 }
186 }
187}
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700188#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700189
190void _raw_read_lock(rwlock_t *lock)
191{
192 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700193 __raw_read_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700194}
195
196int _raw_read_trylock(rwlock_t *lock)
197{
198 int ret = __raw_read_trylock(&lock->raw_lock);
199
200#ifndef CONFIG_SMP
201 /*
202 * Must not happen on UP:
203 */
204 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
205#endif
206 return ret;
207}
208
209void _raw_read_unlock(rwlock_t *lock)
210{
211 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
212 __raw_read_unlock(&lock->raw_lock);
213}
214
215static inline void debug_write_lock_before(rwlock_t *lock)
216{
217 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
218 RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
219 RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
220 lock, "cpu recursion");
221}
222
223static inline void debug_write_lock_after(rwlock_t *lock)
224{
225 lock->owner_cpu = raw_smp_processor_id();
226 lock->owner = current;
227}
228
229static inline void debug_write_unlock(rwlock_t *lock)
230{
231 RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
232 RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
233 RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
234 lock, "wrong CPU");
235 lock->owner = SPINLOCK_OWNER_INIT;
236 lock->owner_cpu = -1;
237}
238
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700239#if 0 /* This can cause lockups */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700240static void __write_lock_debug(rwlock_t *lock)
241{
242 int print_once = 1;
243 u64 i;
244
245 for (;;) {
246 for (i = 0; i < loops_per_jiffy * HZ; i++) {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700247 if (__raw_write_trylock(&lock->raw_lock))
248 return;
Ingo Molnare0a60292006-02-07 12:58:54 -0800249 __delay(1);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700250 }
251 /* lockup suspected: */
252 if (print_once) {
253 print_once = 0;
Dave Jones51989b92006-01-09 20:51:32 -0800254 printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
255 "%s/%d, %p\n",
Ingo Molnarbb44f112005-12-20 11:54:17 +0100256 raw_smp_processor_id(), current->comm,
257 current->pid, lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700258 dump_stack();
259 }
260 }
261}
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700262#endif
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700263
264void _raw_write_lock(rwlock_t *lock)
265{
266 debug_write_lock_before(lock);
Andrew Morton72f0b4e2006-08-05 12:13:47 -0700267 __raw_write_lock(&lock->raw_lock);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700268 debug_write_lock_after(lock);
269}
270
271int _raw_write_trylock(rwlock_t *lock)
272{
273 int ret = __raw_write_trylock(&lock->raw_lock);
274
275 if (ret)
276 debug_write_lock_after(lock);
277#ifndef CONFIG_SMP
278 /*
279 * Must not happen on UP:
280 */
281 RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
282#endif
283 return ret;
284}
285
286void _raw_write_unlock(rwlock_t *lock)
287{
288 debug_write_unlock(lock);
289 __raw_write_unlock(&lock->raw_lock);
290}