fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / lib / spinlock_debug.c
1 /*
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
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/module.h>
15
16 void __spin_lock_init(spinlock_t *lock, const char *name,
17                       struct lock_class_key *key)
18 {
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));
24         lockdep_init_map(&lock->dep_map, name, key, 0);
25 #endif
26         lock->raw_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
27         lock->magic = SPINLOCK_MAGIC;
28         lock->owner = SPINLOCK_OWNER_INIT;
29         lock->owner_cpu = -1;
30 }
31
32 EXPORT_SYMBOL(__spin_lock_init);
33
34 void __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));
42         lockdep_init_map(&lock->dep_map, name, key, 0);
43 #endif
44         lock->raw_lock = (raw_rwlock_t) __RAW_RW_LOCK_UNLOCKED;
45         lock->magic = RWLOCK_MAGIC;
46         lock->owner = SPINLOCK_OWNER_INIT;
47         lock->owner_cpu = -1;
48 }
49
50 EXPORT_SYMBOL(__rwlock_init);
51
52 static void spin_bug(spinlock_t *lock, const char *msg)
53 {
54         struct task_struct *owner = NULL;
55
56         if (!debug_locks_off())
57                 return;
58
59         if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
60                 owner = lock->owner;
61         printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d (%s)\n",
62                 msg, raw_smp_processor_id(),
63                 current->comm, current->pid, print_tainted());
64         printk(KERN_EMERG " lock: %p, .magic: %08x, .owner: %s/%d, "
65                         ".owner_cpu: %d\n",
66                 lock, lock->magic,
67                 owner ? owner->comm : "<none>",
68                 owner ? owner->pid : -1,
69                 lock->owner_cpu);
70         dump_stack();
71 }
72
73 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
74
75 static inline void
76 debug_spin_lock_before(spinlock_t *lock)
77 {
78         SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
79         SPIN_BUG_ON(lock->owner == current, lock, "recursion");
80         SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
81                                                         lock, "cpu recursion");
82 }
83
84 static inline void debug_spin_lock_after(spinlock_t *lock)
85 {
86         lock->owner_cpu = raw_smp_processor_id();
87         lock->owner = current;
88 }
89
90 static inline void debug_spin_unlock(spinlock_t *lock)
91 {
92         SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
93         SPIN_BUG_ON(!spin_is_locked(lock), lock, "already unlocked");
94         SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
95         SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
96                                                         lock, "wrong CPU");
97         lock->owner = SPINLOCK_OWNER_INIT;
98         lock->owner_cpu = -1;
99 }
100
101 static void __spin_lock_debug(spinlock_t *lock)
102 {
103         u64 i;
104         u64 loops = loops_per_jiffy * HZ;
105         int print_once = 1;
106
107         for (;;) {
108                 for (i = 0; i < loops; i++) {
109                         if (__raw_spin_trylock(&lock->raw_lock))
110                                 return;
111                 }
112                 /* lockup suspected: */
113                 if (print_once) {
114                         print_once = 0;
115                         printk(KERN_EMERG "BUG: spinlock lockup on CPU#%d, "
116                                         "%s/%d, %p (%s)\n",
117                                 raw_smp_processor_id(), current->comm,
118                                 current->pid, lock, print_tainted());
119                         dump_stack();
120 #ifdef CONFIG_SMP
121                         trigger_all_cpu_backtrace();
122 #endif
123                 }
124         }
125 }
126
127 void _raw_spin_lock(spinlock_t *lock)
128 {
129         debug_spin_lock_before(lock);
130         if (unlikely(!__raw_spin_trylock(&lock->raw_lock)))
131                 __spin_lock_debug(lock);
132         debug_spin_lock_after(lock);
133 }
134
135 int _raw_spin_trylock(spinlock_t *lock)
136 {
137         int ret = __raw_spin_trylock(&lock->raw_lock);
138
139         if (ret)
140                 debug_spin_lock_after(lock);
141 #ifndef CONFIG_SMP
142         /*
143          * Must not happen on UP:
144          */
145         SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
146 #endif
147         return ret;
148 }
149
150 void _raw_spin_unlock(spinlock_t *lock)
151 {
152         debug_spin_unlock(lock);
153         __raw_spin_unlock(&lock->raw_lock);
154 }
155
156 static void rwlock_bug(rwlock_t *lock, const char *msg)
157 {
158         if (!debug_locks_off())
159                 return;
160
161         printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p (%s)\n",
162                 msg, raw_smp_processor_id(), current->comm,
163                 current->pid, lock, print_tainted());
164         dump_stack();
165 }
166
167 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
168
169 #if 0           /* __write_lock_debug() can lock up - maybe this can too? */
170 static void __read_lock_debug(rwlock_t *lock)
171 {
172         u64 i;
173         u64 loops = loops_per_jiffy * HZ;
174         int print_once = 1;
175
176         for (;;) {
177                 for (i = 0; i < loops; i++) {
178                         if (__raw_read_trylock(&lock->raw_lock))
179                                 return;
180                 }
181                 /* lockup suspected: */
182                 if (print_once) {
183                         print_once = 0;
184                         printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
185                                         "%s/%d, %p (%s)\n",
186                                 raw_smp_processor_id(), current->comm,
187                                 current->pid, lock, print_tainted());
188                         dump_stack();
189                 }
190         }
191 }
192 #endif
193
194 void _raw_read_lock(rwlock_t *lock)
195 {
196         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
197         __raw_read_lock(&lock->raw_lock);
198 }
199
200 int _raw_read_trylock(rwlock_t *lock)
201 {
202         int ret = __raw_read_trylock(&lock->raw_lock);
203
204 #ifndef CONFIG_SMP
205         /*
206          * Must not happen on UP:
207          */
208         RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
209 #endif
210         return ret;
211 }
212
213 void _raw_read_unlock(rwlock_t *lock)
214 {
215         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
216         __raw_read_unlock(&lock->raw_lock);
217 }
218
219 static inline void debug_write_lock_before(rwlock_t *lock)
220 {
221         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
222         RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
223         RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
224                                                         lock, "cpu recursion");
225 }
226
227 static inline void debug_write_lock_after(rwlock_t *lock)
228 {
229         lock->owner_cpu = raw_smp_processor_id();
230         lock->owner = current;
231 }
232
233 static inline void debug_write_unlock(rwlock_t *lock)
234 {
235         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
236         RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
237         RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
238                                                         lock, "wrong CPU");
239         lock->owner = SPINLOCK_OWNER_INIT;
240         lock->owner_cpu = -1;
241 }
242
243 #if 0           /* This can cause lockups */
244 static void __write_lock_debug(rwlock_t *lock)
245 {
246         u64 i;
247         u64 loops = loops_per_jiffy * HZ;
248         int print_once = 1;
249
250         for (;;) {
251                 for (i = 0; i < loops; i++) {
252                         if (__raw_write_trylock(&lock->raw_lock))
253                                 return;
254                 }
255                 /* lockup suspected: */
256                 if (print_once) {
257                         print_once = 0;
258                         printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
259                                         "%s/%d, %p (%s)\n",
260                                 raw_smp_processor_id(), current->comm,
261                                 current->pid, lock, print_tainted());
262                         dump_stack();
263                 }
264         }
265 }
266 #endif
267
268 void _raw_write_lock(rwlock_t *lock)
269 {
270         debug_write_lock_before(lock);
271         __raw_write_lock(&lock->raw_lock);
272         debug_write_lock_after(lock);
273 }
274
275 int _raw_write_trylock(rwlock_t *lock)
276 {
277         int ret = __raw_write_trylock(&lock->raw_lock);
278
279         if (ret)
280                 debug_write_lock_after(lock);
281 #ifndef CONFIG_SMP
282         /*
283          * Must not happen on UP:
284          */
285         RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
286 #endif
287         return ret;
288 }
289
290 void _raw_write_unlock(rwlock_t *lock)
291 {
292         debug_write_unlock(lock);
293         __raw_write_unlock(&lock->raw_lock);
294 }