Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / kernel / locking / 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/export.h>
15
16 void __raw_spin_lock_init(raw_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 = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
27         lock->magic = SPINLOCK_MAGIC;
28         lock->owner = SPINLOCK_OWNER_INIT;
29         lock->owner_cpu = -1;
30 }
31
32 EXPORT_SYMBOL(__raw_spin_lock_init);
33
34 #ifndef CONFIG_PREEMPT_RT_FULL
35 void __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));
43         lockdep_init_map(&lock->dep_map, name, key, 0);
44 #endif
45         lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
46         lock->magic = RWLOCK_MAGIC;
47         lock->owner = SPINLOCK_OWNER_INIT;
48         lock->owner_cpu = -1;
49 }
50
51 EXPORT_SYMBOL(__rwlock_init);
52 #endif
53
54 static void spin_dump(raw_spinlock_t *lock, const char *msg)
55 {
56         struct task_struct *owner = NULL;
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, task_pid_nr(current));
63         printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, "
64                         ".owner_cpu: %d\n",
65                 lock, lock->magic,
66                 owner ? owner->comm : "<none>",
67                 owner ? task_pid_nr(owner) : -1,
68                 lock->owner_cpu);
69         dump_stack();
70 }
71
72 static 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
80 #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
81
82 static inline void
83 debug_spin_lock_before(raw_spinlock_t *lock)
84 {
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
91 static inline void debug_spin_lock_after(raw_spinlock_t *lock)
92 {
93         lock->owner_cpu = raw_smp_processor_id();
94         lock->owner = current;
95 }
96
97 static inline void debug_spin_unlock(raw_spinlock_t *lock)
98 {
99         SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
100         SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
101         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
108 static void __spin_lock_debug(raw_spinlock_t *lock)
109 {
110         u64 i;
111         u64 loops = loops_per_jiffy * HZ;
112
113         for (i = 0; i < loops; i++) {
114                 if (arch_spin_trylock(&lock->raw_lock))
115                         return;
116                 __delay(1);
117         }
118         /* lockup suspected: */
119         spin_dump(lock, "lockup suspected");
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);
133 }
134
135 void do_raw_spin_lock(raw_spinlock_t *lock)
136 {
137         debug_spin_lock_before(lock);
138         if (unlikely(!arch_spin_trylock(&lock->raw_lock)))
139                 __spin_lock_debug(lock);
140         debug_spin_lock_after(lock);
141 }
142
143 int do_raw_spin_trylock(raw_spinlock_t *lock)
144 {
145         int ret = arch_spin_trylock(&lock->raw_lock);
146
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
158 void do_raw_spin_unlock(raw_spinlock_t *lock)
159 {
160         debug_spin_unlock(lock);
161         arch_spin_unlock(&lock->raw_lock);
162 }
163
164 #ifndef CONFIG_PREEMPT_RT_FULL
165 static void rwlock_bug(rwlock_t *lock, const char *msg)
166 {
167         if (!debug_locks_off())
168                 return;
169
170         printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
171                 msg, raw_smp_processor_id(), current->comm,
172                 task_pid_nr(current), lock);
173         dump_stack();
174 }
175
176 #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
177
178 #if 0           /* __write_lock_debug() can lock up - maybe this can too? */
179 static void __read_lock_debug(rwlock_t *lock)
180 {
181         u64 i;
182         u64 loops = loops_per_jiffy * HZ;
183         int print_once = 1;
184
185         for (;;) {
186                 for (i = 0; i < loops; i++) {
187                         if (arch_read_trylock(&lock->raw_lock))
188                                 return;
189                         __delay(1);
190                 }
191                 /* lockup suspected: */
192                 if (print_once) {
193                         print_once = 0;
194                         printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
195                                         "%s/%d, %p\n",
196                                 raw_smp_processor_id(), current->comm,
197                                 current->pid, lock);
198                         dump_stack();
199                 }
200         }
201 }
202 #endif
203
204 void do_raw_read_lock(rwlock_t *lock)
205 {
206         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
207         arch_read_lock(&lock->raw_lock);
208 }
209
210 int do_raw_read_trylock(rwlock_t *lock)
211 {
212         int ret = arch_read_trylock(&lock->raw_lock);
213
214 #ifndef CONFIG_SMP
215         /*
216          * Must not happen on UP:
217          */
218         RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
219 #endif
220         return ret;
221 }
222
223 void do_raw_read_unlock(rwlock_t *lock)
224 {
225         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
226         arch_read_unlock(&lock->raw_lock);
227 }
228
229 static inline void debug_write_lock_before(rwlock_t *lock)
230 {
231         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
232         RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
233         RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
234                                                         lock, "cpu recursion");
235 }
236
237 static inline void debug_write_lock_after(rwlock_t *lock)
238 {
239         lock->owner_cpu = raw_smp_processor_id();
240         lock->owner = current;
241 }
242
243 static inline void debug_write_unlock(rwlock_t *lock)
244 {
245         RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
246         RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
247         RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
248                                                         lock, "wrong CPU");
249         lock->owner = SPINLOCK_OWNER_INIT;
250         lock->owner_cpu = -1;
251 }
252
253 #if 0           /* This can cause lockups */
254 static void __write_lock_debug(rwlock_t *lock)
255 {
256         u64 i;
257         u64 loops = loops_per_jiffy * HZ;
258         int print_once = 1;
259
260         for (;;) {
261                 for (i = 0; i < loops; i++) {
262                         if (arch_write_trylock(&lock->raw_lock))
263                                 return;
264                         __delay(1);
265                 }
266                 /* lockup suspected: */
267                 if (print_once) {
268                         print_once = 0;
269                         printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
270                                         "%s/%d, %p\n",
271                                 raw_smp_processor_id(), current->comm,
272                                 current->pid, lock);
273                         dump_stack();
274                 }
275         }
276 }
277 #endif
278
279 void do_raw_write_lock(rwlock_t *lock)
280 {
281         debug_write_lock_before(lock);
282         arch_write_lock(&lock->raw_lock);
283         debug_write_lock_after(lock);
284 }
285
286 int do_raw_write_trylock(rwlock_t *lock)
287 {
288         int ret = arch_write_trylock(&lock->raw_lock);
289
290         if (ret)
291                 debug_write_lock_after(lock);
292 #ifndef CONFIG_SMP
293         /*
294          * Must not happen on UP:
295          */
296         RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
297 #endif
298         return ret;
299 }
300
301 void do_raw_write_unlock(rwlock_t *lock)
302 {
303         debug_write_unlock(lock);
304         arch_write_unlock(&lock->raw_lock);
305 }
306
307 #endif