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