Adding packet forward test cases involving multiple VMs
[kvmfornfv.git] / kernel / lib / locking-selftest.c
1 /*
2  * lib/locking-selftest.c
3  *
4  * Testsuite for various locking APIs: spinlocks, rwlocks,
5  * mutexes and rw-semaphores.
6  *
7  * It is checking both false positives and false negatives.
8  *
9  * Started by Ingo Molnar:
10  *
11  *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
12  */
13 #include <linux/rwsem.h>
14 #include <linux/mutex.h>
15 #include <linux/ww_mutex.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/lockdep.h>
19 #include <linux/spinlock.h>
20 #include <linux/kallsyms.h>
21 #include <linux/interrupt.h>
22 #include <linux/debug_locks.h>
23 #include <linux/irqflags.h>
24
25 /*
26  * Change this to 1 if you want to see the failure printouts:
27  */
28 static unsigned int debug_locks_verbose;
29
30 static DEFINE_WW_CLASS(ww_lockdep);
31
32 static int __init setup_debug_locks_verbose(char *str)
33 {
34         get_option(&str, &debug_locks_verbose);
35
36         return 1;
37 }
38
39 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
40
41 #define FAILURE         0
42 #define SUCCESS         1
43
44 #define LOCKTYPE_SPIN   0x1
45 #define LOCKTYPE_RWLOCK 0x2
46 #define LOCKTYPE_MUTEX  0x4
47 #define LOCKTYPE_RWSEM  0x8
48 #define LOCKTYPE_WW     0x10
49
50 static struct ww_acquire_ctx t, t2;
51 static struct ww_mutex o, o2, o3;
52
53 /*
54  * Normal standalone locks, for the circular and irq-context
55  * dependency tests:
56  */
57 static DEFINE_RAW_SPINLOCK(lock_A);
58 static DEFINE_RAW_SPINLOCK(lock_B);
59 static DEFINE_RAW_SPINLOCK(lock_C);
60 static DEFINE_RAW_SPINLOCK(lock_D);
61
62 static DEFINE_RWLOCK(rwlock_A);
63 static DEFINE_RWLOCK(rwlock_B);
64 static DEFINE_RWLOCK(rwlock_C);
65 static DEFINE_RWLOCK(rwlock_D);
66
67 static DEFINE_MUTEX(mutex_A);
68 static DEFINE_MUTEX(mutex_B);
69 static DEFINE_MUTEX(mutex_C);
70 static DEFINE_MUTEX(mutex_D);
71
72 static DECLARE_RWSEM(rwsem_A);
73 static DECLARE_RWSEM(rwsem_B);
74 static DECLARE_RWSEM(rwsem_C);
75 static DECLARE_RWSEM(rwsem_D);
76
77 /*
78  * Locks that we initialize dynamically as well so that
79  * e.g. X1 and X2 becomes two instances of the same class,
80  * but X* and Y* are different classes. We do this so that
81  * we do not trigger a real lockup:
82  */
83 static DEFINE_RAW_SPINLOCK(lock_X1);
84 static DEFINE_RAW_SPINLOCK(lock_X2);
85 static DEFINE_RAW_SPINLOCK(lock_Y1);
86 static DEFINE_RAW_SPINLOCK(lock_Y2);
87 static DEFINE_RAW_SPINLOCK(lock_Z1);
88 static DEFINE_RAW_SPINLOCK(lock_Z2);
89
90 static DEFINE_RWLOCK(rwlock_X1);
91 static DEFINE_RWLOCK(rwlock_X2);
92 static DEFINE_RWLOCK(rwlock_Y1);
93 static DEFINE_RWLOCK(rwlock_Y2);
94 static DEFINE_RWLOCK(rwlock_Z1);
95 static DEFINE_RWLOCK(rwlock_Z2);
96
97 static DEFINE_MUTEX(mutex_X1);
98 static DEFINE_MUTEX(mutex_X2);
99 static DEFINE_MUTEX(mutex_Y1);
100 static DEFINE_MUTEX(mutex_Y2);
101 static DEFINE_MUTEX(mutex_Z1);
102 static DEFINE_MUTEX(mutex_Z2);
103
104 static DECLARE_RWSEM(rwsem_X1);
105 static DECLARE_RWSEM(rwsem_X2);
106 static DECLARE_RWSEM(rwsem_Y1);
107 static DECLARE_RWSEM(rwsem_Y2);
108 static DECLARE_RWSEM(rwsem_Z1);
109 static DECLARE_RWSEM(rwsem_Z2);
110
111 /*
112  * non-inlined runtime initializers, to let separate locks share
113  * the same lock-class:
114  */
115 #define INIT_CLASS_FUNC(class)                          \
116 static noinline void                                    \
117 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
118         struct mutex *mutex, struct rw_semaphore *rwsem)\
119 {                                                       \
120         raw_spin_lock_init(lock);                       \
121         rwlock_init(rwlock);                            \
122         mutex_init(mutex);                              \
123         init_rwsem(rwsem);                              \
124 }
125
126 INIT_CLASS_FUNC(X)
127 INIT_CLASS_FUNC(Y)
128 INIT_CLASS_FUNC(Z)
129
130 static void init_shared_classes(void)
131 {
132         init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
133         init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
134
135         init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
136         init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
137
138         init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
139         init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
140 }
141
142 /*
143  * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
144  * The following functions use a lock from a simulated hardirq/softirq
145  * context, causing the locks to be marked as hardirq-safe/softirq-safe:
146  */
147
148 #define HARDIRQ_DISABLE         local_irq_disable
149 #define HARDIRQ_ENABLE          local_irq_enable
150
151 #define HARDIRQ_ENTER()                         \
152         local_irq_disable();                    \
153         __irq_enter();                          \
154         WARN_ON(!in_irq());
155
156 #define HARDIRQ_EXIT()                          \
157         __irq_exit();                           \
158         local_irq_enable();
159
160 #define SOFTIRQ_DISABLE         local_bh_disable
161 #define SOFTIRQ_ENABLE          local_bh_enable
162
163 #define SOFTIRQ_ENTER()                         \
164                 local_bh_disable();             \
165                 local_irq_disable();            \
166                 lockdep_softirq_enter();        \
167                 WARN_ON(!in_softirq());
168
169 #define SOFTIRQ_EXIT()                          \
170                 lockdep_softirq_exit();         \
171                 local_irq_enable();             \
172                 local_bh_enable();
173
174 /*
175  * Shortcuts for lock/unlock API variants, to keep
176  * the testcases compact:
177  */
178 #define L(x)                    raw_spin_lock(&lock_##x)
179 #define U(x)                    raw_spin_unlock(&lock_##x)
180 #define LU(x)                   L(x); U(x)
181 #define SI(x)                   raw_spin_lock_init(&lock_##x)
182
183 #define WL(x)                   write_lock(&rwlock_##x)
184 #define WU(x)                   write_unlock(&rwlock_##x)
185 #define WLU(x)                  WL(x); WU(x)
186
187 #define RL(x)                   read_lock(&rwlock_##x)
188 #define RU(x)                   read_unlock(&rwlock_##x)
189 #define RLU(x)                  RL(x); RU(x)
190 #define RWI(x)                  rwlock_init(&rwlock_##x)
191
192 #define ML(x)                   mutex_lock(&mutex_##x)
193 #define MU(x)                   mutex_unlock(&mutex_##x)
194 #define MI(x)                   mutex_init(&mutex_##x)
195
196 #define WSL(x)                  down_write(&rwsem_##x)
197 #define WSU(x)                  up_write(&rwsem_##x)
198
199 #define RSL(x)                  down_read(&rwsem_##x)
200 #define RSU(x)                  up_read(&rwsem_##x)
201 #define RWSI(x)                 init_rwsem(&rwsem_##x)
202
203 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
204 #define WWAI(x)                 ww_acquire_init(x, &ww_lockdep)
205 #else
206 #define WWAI(x)                 do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
207 #endif
208 #define WWAD(x)                 ww_acquire_done(x)
209 #define WWAF(x)                 ww_acquire_fini(x)
210
211 #define WWL(x, c)               ww_mutex_lock(x, c)
212 #define WWT(x)                  ww_mutex_trylock(x)
213 #define WWL1(x)                 ww_mutex_lock(x, NULL)
214 #define WWU(x)                  ww_mutex_unlock(x)
215
216
217 #define LOCK_UNLOCK_2(x,y)      LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
218
219 /*
220  * Generate different permutations of the same testcase, using
221  * the same basic lock-dependency/state events:
222  */
223
224 #define GENERATE_TESTCASE(name)                 \
225                                                 \
226 static void name(void) { E(); }
227
228 #define GENERATE_PERMUTATIONS_2_EVENTS(name)    \
229                                                 \
230 static void name##_12(void) { E1(); E2(); }     \
231 static void name##_21(void) { E2(); E1(); }
232
233 #define GENERATE_PERMUTATIONS_3_EVENTS(name)            \
234                                                         \
235 static void name##_123(void) { E1(); E2(); E3(); }      \
236 static void name##_132(void) { E1(); E3(); E2(); }      \
237 static void name##_213(void) { E2(); E1(); E3(); }      \
238 static void name##_231(void) { E2(); E3(); E1(); }      \
239 static void name##_312(void) { E3(); E1(); E2(); }      \
240 static void name##_321(void) { E3(); E2(); E1(); }
241
242 /*
243  * AA deadlock:
244  */
245
246 #define E()                                     \
247                                                 \
248         LOCK(X1);                               \
249         LOCK(X2); /* this one should fail */
250
251 /*
252  * 6 testcases:
253  */
254 #include "locking-selftest-spin.h"
255 GENERATE_TESTCASE(AA_spin)
256 #include "locking-selftest-wlock.h"
257 GENERATE_TESTCASE(AA_wlock)
258 #include "locking-selftest-rlock.h"
259 GENERATE_TESTCASE(AA_rlock)
260 #include "locking-selftest-mutex.h"
261 GENERATE_TESTCASE(AA_mutex)
262 #include "locking-selftest-wsem.h"
263 GENERATE_TESTCASE(AA_wsem)
264 #include "locking-selftest-rsem.h"
265 GENERATE_TESTCASE(AA_rsem)
266
267 #undef E
268
269 /*
270  * Special-case for read-locking, they are
271  * allowed to recurse on the same lock class:
272  */
273 static void rlock_AA1(void)
274 {
275         RL(X1);
276         RL(X1); // this one should NOT fail
277 }
278
279 static void rlock_AA1B(void)
280 {
281         RL(X1);
282         RL(X2); // this one should NOT fail
283 }
284
285 static void rsem_AA1(void)
286 {
287         RSL(X1);
288         RSL(X1); // this one should fail
289 }
290
291 static void rsem_AA1B(void)
292 {
293         RSL(X1);
294         RSL(X2); // this one should fail
295 }
296 /*
297  * The mixing of read and write locks is not allowed:
298  */
299 static void rlock_AA2(void)
300 {
301         RL(X1);
302         WL(X2); // this one should fail
303 }
304
305 static void rsem_AA2(void)
306 {
307         RSL(X1);
308         WSL(X2); // this one should fail
309 }
310
311 static void rlock_AA3(void)
312 {
313         WL(X1);
314         RL(X2); // this one should fail
315 }
316
317 static void rsem_AA3(void)
318 {
319         WSL(X1);
320         RSL(X2); // this one should fail
321 }
322
323 /*
324  * ABBA deadlock:
325  */
326
327 #define E()                                     \
328                                                 \
329         LOCK_UNLOCK_2(A, B);                    \
330         LOCK_UNLOCK_2(B, A); /* fail */
331
332 /*
333  * 6 testcases:
334  */
335 #include "locking-selftest-spin.h"
336 GENERATE_TESTCASE(ABBA_spin)
337 #include "locking-selftest-wlock.h"
338 GENERATE_TESTCASE(ABBA_wlock)
339 #include "locking-selftest-rlock.h"
340 GENERATE_TESTCASE(ABBA_rlock)
341 #include "locking-selftest-mutex.h"
342 GENERATE_TESTCASE(ABBA_mutex)
343 #include "locking-selftest-wsem.h"
344 GENERATE_TESTCASE(ABBA_wsem)
345 #include "locking-selftest-rsem.h"
346 GENERATE_TESTCASE(ABBA_rsem)
347
348 #undef E
349
350 /*
351  * AB BC CA deadlock:
352  */
353
354 #define E()                                     \
355                                                 \
356         LOCK_UNLOCK_2(A, B);                    \
357         LOCK_UNLOCK_2(B, C);                    \
358         LOCK_UNLOCK_2(C, A); /* fail */
359
360 /*
361  * 6 testcases:
362  */
363 #include "locking-selftest-spin.h"
364 GENERATE_TESTCASE(ABBCCA_spin)
365 #include "locking-selftest-wlock.h"
366 GENERATE_TESTCASE(ABBCCA_wlock)
367 #include "locking-selftest-rlock.h"
368 GENERATE_TESTCASE(ABBCCA_rlock)
369 #include "locking-selftest-mutex.h"
370 GENERATE_TESTCASE(ABBCCA_mutex)
371 #include "locking-selftest-wsem.h"
372 GENERATE_TESTCASE(ABBCCA_wsem)
373 #include "locking-selftest-rsem.h"
374 GENERATE_TESTCASE(ABBCCA_rsem)
375
376 #undef E
377
378 /*
379  * AB CA BC deadlock:
380  */
381
382 #define E()                                     \
383                                                 \
384         LOCK_UNLOCK_2(A, B);                    \
385         LOCK_UNLOCK_2(C, A);                    \
386         LOCK_UNLOCK_2(B, C); /* fail */
387
388 /*
389  * 6 testcases:
390  */
391 #include "locking-selftest-spin.h"
392 GENERATE_TESTCASE(ABCABC_spin)
393 #include "locking-selftest-wlock.h"
394 GENERATE_TESTCASE(ABCABC_wlock)
395 #include "locking-selftest-rlock.h"
396 GENERATE_TESTCASE(ABCABC_rlock)
397 #include "locking-selftest-mutex.h"
398 GENERATE_TESTCASE(ABCABC_mutex)
399 #include "locking-selftest-wsem.h"
400 GENERATE_TESTCASE(ABCABC_wsem)
401 #include "locking-selftest-rsem.h"
402 GENERATE_TESTCASE(ABCABC_rsem)
403
404 #undef E
405
406 /*
407  * AB BC CD DA deadlock:
408  */
409
410 #define E()                                     \
411                                                 \
412         LOCK_UNLOCK_2(A, B);                    \
413         LOCK_UNLOCK_2(B, C);                    \
414         LOCK_UNLOCK_2(C, D);                    \
415         LOCK_UNLOCK_2(D, A); /* fail */
416
417 /*
418  * 6 testcases:
419  */
420 #include "locking-selftest-spin.h"
421 GENERATE_TESTCASE(ABBCCDDA_spin)
422 #include "locking-selftest-wlock.h"
423 GENERATE_TESTCASE(ABBCCDDA_wlock)
424 #include "locking-selftest-rlock.h"
425 GENERATE_TESTCASE(ABBCCDDA_rlock)
426 #include "locking-selftest-mutex.h"
427 GENERATE_TESTCASE(ABBCCDDA_mutex)
428 #include "locking-selftest-wsem.h"
429 GENERATE_TESTCASE(ABBCCDDA_wsem)
430 #include "locking-selftest-rsem.h"
431 GENERATE_TESTCASE(ABBCCDDA_rsem)
432
433 #undef E
434
435 /*
436  * AB CD BD DA deadlock:
437  */
438 #define E()                                     \
439                                                 \
440         LOCK_UNLOCK_2(A, B);                    \
441         LOCK_UNLOCK_2(C, D);                    \
442         LOCK_UNLOCK_2(B, D);                    \
443         LOCK_UNLOCK_2(D, A); /* fail */
444
445 /*
446  * 6 testcases:
447  */
448 #include "locking-selftest-spin.h"
449 GENERATE_TESTCASE(ABCDBDDA_spin)
450 #include "locking-selftest-wlock.h"
451 GENERATE_TESTCASE(ABCDBDDA_wlock)
452 #include "locking-selftest-rlock.h"
453 GENERATE_TESTCASE(ABCDBDDA_rlock)
454 #include "locking-selftest-mutex.h"
455 GENERATE_TESTCASE(ABCDBDDA_mutex)
456 #include "locking-selftest-wsem.h"
457 GENERATE_TESTCASE(ABCDBDDA_wsem)
458 #include "locking-selftest-rsem.h"
459 GENERATE_TESTCASE(ABCDBDDA_rsem)
460
461 #undef E
462
463 /*
464  * AB CD BC DA deadlock:
465  */
466 #define E()                                     \
467                                                 \
468         LOCK_UNLOCK_2(A, B);                    \
469         LOCK_UNLOCK_2(C, D);                    \
470         LOCK_UNLOCK_2(B, C);                    \
471         LOCK_UNLOCK_2(D, A); /* fail */
472
473 /*
474  * 6 testcases:
475  */
476 #include "locking-selftest-spin.h"
477 GENERATE_TESTCASE(ABCDBCDA_spin)
478 #include "locking-selftest-wlock.h"
479 GENERATE_TESTCASE(ABCDBCDA_wlock)
480 #include "locking-selftest-rlock.h"
481 GENERATE_TESTCASE(ABCDBCDA_rlock)
482 #include "locking-selftest-mutex.h"
483 GENERATE_TESTCASE(ABCDBCDA_mutex)
484 #include "locking-selftest-wsem.h"
485 GENERATE_TESTCASE(ABCDBCDA_wsem)
486 #include "locking-selftest-rsem.h"
487 GENERATE_TESTCASE(ABCDBCDA_rsem)
488
489 #undef E
490
491 /*
492  * Double unlock:
493  */
494 #define E()                                     \
495                                                 \
496         LOCK(A);                                \
497         UNLOCK(A);                              \
498         UNLOCK(A); /* fail */
499
500 /*
501  * 6 testcases:
502  */
503 #include "locking-selftest-spin.h"
504 GENERATE_TESTCASE(double_unlock_spin)
505 #include "locking-selftest-wlock.h"
506 GENERATE_TESTCASE(double_unlock_wlock)
507 #include "locking-selftest-rlock.h"
508 GENERATE_TESTCASE(double_unlock_rlock)
509 #include "locking-selftest-mutex.h"
510 GENERATE_TESTCASE(double_unlock_mutex)
511 #include "locking-selftest-wsem.h"
512 GENERATE_TESTCASE(double_unlock_wsem)
513 #include "locking-selftest-rsem.h"
514 GENERATE_TESTCASE(double_unlock_rsem)
515
516 #undef E
517
518 /*
519  * Bad unlock ordering:
520  */
521 #define E()                                     \
522                                                 \
523         LOCK(A);                                \
524         LOCK(B);                                \
525         UNLOCK(A); /* fail */                   \
526         UNLOCK(B);
527
528 /*
529  * 6 testcases:
530  */
531 #include "locking-selftest-spin.h"
532 GENERATE_TESTCASE(bad_unlock_order_spin)
533 #include "locking-selftest-wlock.h"
534 GENERATE_TESTCASE(bad_unlock_order_wlock)
535 #include "locking-selftest-rlock.h"
536 GENERATE_TESTCASE(bad_unlock_order_rlock)
537 #include "locking-selftest-mutex.h"
538 GENERATE_TESTCASE(bad_unlock_order_mutex)
539 #include "locking-selftest-wsem.h"
540 GENERATE_TESTCASE(bad_unlock_order_wsem)
541 #include "locking-selftest-rsem.h"
542 GENERATE_TESTCASE(bad_unlock_order_rsem)
543
544 #undef E
545
546 /*
547  * initializing a held lock:
548  */
549 #define E()                                     \
550                                                 \
551         LOCK(A);                                \
552         INIT(A); /* fail */
553
554 /*
555  * 6 testcases:
556  */
557 #include "locking-selftest-spin.h"
558 GENERATE_TESTCASE(init_held_spin)
559 #include "locking-selftest-wlock.h"
560 GENERATE_TESTCASE(init_held_wlock)
561 #include "locking-selftest-rlock.h"
562 GENERATE_TESTCASE(init_held_rlock)
563 #include "locking-selftest-mutex.h"
564 GENERATE_TESTCASE(init_held_mutex)
565 #include "locking-selftest-wsem.h"
566 GENERATE_TESTCASE(init_held_wsem)
567 #include "locking-selftest-rsem.h"
568 GENERATE_TESTCASE(init_held_rsem)
569
570 #undef E
571
572 /*
573  * locking an irq-safe lock with irqs enabled:
574  */
575 #define E1()                            \
576                                         \
577         IRQ_ENTER();                    \
578         LOCK(A);                        \
579         UNLOCK(A);                      \
580         IRQ_EXIT();
581
582 #define E2()                            \
583                                         \
584         LOCK(A);                        \
585         UNLOCK(A);
586
587 /*
588  * Generate 24 testcases:
589  */
590 #include "locking-selftest-spin-hardirq.h"
591 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
592
593 #ifndef CONFIG_PREEMPT_RT_FULL
594
595 #include "locking-selftest-rlock-hardirq.h"
596 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
597
598 #include "locking-selftest-wlock-hardirq.h"
599 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
600
601 #include "locking-selftest-spin-softirq.h"
602 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
603
604 #include "locking-selftest-rlock-softirq.h"
605 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
606
607 #include "locking-selftest-wlock-softirq.h"
608 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
609
610 #endif
611
612 #undef E1
613 #undef E2
614
615 #ifndef CONFIG_PREEMPT_RT_FULL
616 /*
617  * Enabling hardirqs with a softirq-safe lock held:
618  */
619 #define E1()                            \
620                                         \
621         SOFTIRQ_ENTER();                \
622         LOCK(A);                        \
623         UNLOCK(A);                      \
624         SOFTIRQ_EXIT();
625
626 #define E2()                            \
627                                         \
628         HARDIRQ_DISABLE();              \
629         LOCK(A);                        \
630         HARDIRQ_ENABLE();               \
631         UNLOCK(A);
632
633 /*
634  * Generate 12 testcases:
635  */
636 #include "locking-selftest-spin.h"
637 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
638
639 #include "locking-selftest-wlock.h"
640 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
641
642 #include "locking-selftest-rlock.h"
643 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
644
645 #undef E1
646 #undef E2
647
648 #endif
649
650 /*
651  * Enabling irqs with an irq-safe lock held:
652  */
653 #define E1()                            \
654                                         \
655         IRQ_ENTER();                    \
656         LOCK(A);                        \
657         UNLOCK(A);                      \
658         IRQ_EXIT();
659
660 #define E2()                            \
661                                         \
662         IRQ_DISABLE();                  \
663         LOCK(A);                        \
664         IRQ_ENABLE();                   \
665         UNLOCK(A);
666
667 /*
668  * Generate 24 testcases:
669  */
670 #include "locking-selftest-spin-hardirq.h"
671 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
672
673 #ifndef CONFIG_PREEMPT_RT_FULL
674
675 #include "locking-selftest-rlock-hardirq.h"
676 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
677
678 #include "locking-selftest-wlock-hardirq.h"
679 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
680
681 #include "locking-selftest-spin-softirq.h"
682 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
683
684 #include "locking-selftest-rlock-softirq.h"
685 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
686
687 #include "locking-selftest-wlock-softirq.h"
688 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
689
690 #endif
691
692 #undef E1
693 #undef E2
694
695 /*
696  * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
697  */
698 #define E1()                            \
699                                         \
700         LOCK(A);                        \
701         LOCK(B);                        \
702         UNLOCK(B);                      \
703         UNLOCK(A);                      \
704
705 #define E2()                            \
706                                         \
707         LOCK(B);                        \
708         UNLOCK(B);
709
710 #define E3()                            \
711                                         \
712         IRQ_ENTER();                    \
713         LOCK(A);                        \
714         UNLOCK(A);                      \
715         IRQ_EXIT();
716
717 /*
718  * Generate 36 testcases:
719  */
720 #include "locking-selftest-spin-hardirq.h"
721 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
722
723 #ifndef CONFIG_PREEMPT_RT_FULL
724
725 #include "locking-selftest-rlock-hardirq.h"
726 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
727
728 #include "locking-selftest-wlock-hardirq.h"
729 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
730
731 #include "locking-selftest-spin-softirq.h"
732 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
733
734 #include "locking-selftest-rlock-softirq.h"
735 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
736
737 #include "locking-selftest-wlock-softirq.h"
738 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
739
740 #endif
741
742 #undef E1
743 #undef E2
744 #undef E3
745
746 /*
747  * If a lock turns into softirq-safe, but earlier it took
748  * a softirq-unsafe lock:
749  */
750
751 #define E1()                            \
752         IRQ_DISABLE();                  \
753         LOCK(A);                        \
754         LOCK(B);                        \
755         UNLOCK(B);                      \
756         UNLOCK(A);                      \
757         IRQ_ENABLE();
758
759 #define E2()                            \
760         LOCK(B);                        \
761         UNLOCK(B);
762
763 #define E3()                            \
764         IRQ_ENTER();                    \
765         LOCK(A);                        \
766         UNLOCK(A);                      \
767         IRQ_EXIT();
768
769 /*
770  * Generate 36 testcases:
771  */
772 #include "locking-selftest-spin-hardirq.h"
773 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
774
775 #ifndef CONFIG_PREEMPT_RT_FULL
776
777 #include "locking-selftest-rlock-hardirq.h"
778 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
779
780 #include "locking-selftest-wlock-hardirq.h"
781 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
782
783 #include "locking-selftest-spin-softirq.h"
784 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
785
786 #include "locking-selftest-rlock-softirq.h"
787 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
788
789 #include "locking-selftest-wlock-softirq.h"
790 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
791
792 #endif
793
794 #undef E1
795 #undef E2
796 #undef E3
797
798 #ifndef CONFIG_PREEMPT_RT_FULL
799
800 /*
801  * read-lock / write-lock irq inversion.
802  *
803  * Deadlock scenario:
804  *
805  * CPU#1 is at #1, i.e. it has write-locked A, but has not
806  * taken B yet.
807  *
808  * CPU#2 is at #2, i.e. it has locked B.
809  *
810  * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
811  *
812  * The deadlock occurs because CPU#1 will spin on B, and CPU#2
813  * will spin on A.
814  */
815
816 #define E1()                            \
817                                         \
818         IRQ_DISABLE();                  \
819         WL(A);                          \
820         LOCK(B);                        \
821         UNLOCK(B);                      \
822         WU(A);                          \
823         IRQ_ENABLE();
824
825 #define E2()                            \
826                                         \
827         LOCK(B);                        \
828         UNLOCK(B);
829
830 #define E3()                            \
831                                         \
832         IRQ_ENTER();                    \
833         RL(A);                          \
834         RU(A);                          \
835         IRQ_EXIT();
836
837 /*
838  * Generate 36 testcases:
839  */
840 #include "locking-selftest-spin-hardirq.h"
841 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
842
843 #include "locking-selftest-rlock-hardirq.h"
844 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
845
846 #include "locking-selftest-wlock-hardirq.h"
847 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
848
849 #include "locking-selftest-spin-softirq.h"
850 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
851
852 #include "locking-selftest-rlock-softirq.h"
853 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
854
855 #include "locking-selftest-wlock-softirq.h"
856 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
857
858 #undef E1
859 #undef E2
860 #undef E3
861
862 #endif
863
864 #ifndef CONFIG_PREEMPT_RT_FULL
865
866 /*
867  * read-lock / write-lock recursion that is actually safe.
868  */
869
870 #define E1()                            \
871                                         \
872         IRQ_DISABLE();                  \
873         WL(A);                          \
874         WU(A);                          \
875         IRQ_ENABLE();
876
877 #define E2()                            \
878                                         \
879         RL(A);                          \
880         RU(A);                          \
881
882 #define E3()                            \
883                                         \
884         IRQ_ENTER();                    \
885         RL(A);                          \
886         L(B);                           \
887         U(B);                           \
888         RU(A);                          \
889         IRQ_EXIT();
890
891 /*
892  * Generate 12 testcases:
893  */
894 #include "locking-selftest-hardirq.h"
895 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
896
897 #include "locking-selftest-softirq.h"
898 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
899
900 #undef E1
901 #undef E2
902 #undef E3
903
904 #endif
905
906 /*
907  * read-lock / write-lock recursion that is unsafe.
908  */
909
910 #define E1()                            \
911                                         \
912         IRQ_DISABLE();                  \
913         L(B);                           \
914         WL(A);                          \
915         WU(A);                          \
916         U(B);                           \
917         IRQ_ENABLE();
918
919 #define E2()                            \
920                                         \
921         RL(A);                          \
922         RU(A);                          \
923
924 #define E3()                            \
925                                         \
926         IRQ_ENTER();                    \
927         L(B);                           \
928         U(B);                           \
929         IRQ_EXIT();
930
931 /*
932  * Generate 12 testcases:
933  */
934 #include "locking-selftest-hardirq.h"
935 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
936
937 #include "locking-selftest-softirq.h"
938 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
939
940 #ifdef CONFIG_DEBUG_LOCK_ALLOC
941 # define I_SPINLOCK(x)  lockdep_reset_lock(&lock_##x.dep_map)
942 # define I_RWLOCK(x)    lockdep_reset_lock(&rwlock_##x.dep_map)
943 # define I_MUTEX(x)     lockdep_reset_lock(&mutex_##x.dep_map)
944 # define I_RWSEM(x)     lockdep_reset_lock(&rwsem_##x.dep_map)
945 # define I_WW(x)        lockdep_reset_lock(&x.dep_map)
946 #else
947 # define I_SPINLOCK(x)
948 # define I_RWLOCK(x)
949 # define I_MUTEX(x)
950 # define I_RWSEM(x)
951 # define I_WW(x)
952 #endif
953
954 #define I1(x)                                   \
955         do {                                    \
956                 I_SPINLOCK(x);                  \
957                 I_RWLOCK(x);                    \
958                 I_MUTEX(x);                     \
959                 I_RWSEM(x);                     \
960         } while (0)
961
962 #define I2(x)                                   \
963         do {                                    \
964                 raw_spin_lock_init(&lock_##x);  \
965                 rwlock_init(&rwlock_##x);       \
966                 mutex_init(&mutex_##x);         \
967                 init_rwsem(&rwsem_##x);         \
968         } while (0)
969
970 static void reset_locks(void)
971 {
972         local_irq_disable();
973         lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
974         lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
975
976         I1(A); I1(B); I1(C); I1(D);
977         I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
978         I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
979         lockdep_reset();
980         I2(A); I2(B); I2(C); I2(D);
981         init_shared_classes();
982
983         ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
984         memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
985         memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
986         memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
987         local_irq_enable();
988 }
989
990 #undef I
991
992 static int testcase_total;
993 static int testcase_successes;
994 static int expected_testcase_failures;
995 static int unexpected_testcase_failures;
996
997 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
998 {
999         unsigned long saved_preempt_count = preempt_count();
1000
1001         WARN_ON(irqs_disabled());
1002
1003         testcase_fn();
1004         /*
1005          * Filter out expected failures:
1006          */
1007 #ifndef CONFIG_PROVE_LOCKING
1008         if (expected == FAILURE && debug_locks) {
1009                 expected_testcase_failures++;
1010                 printk("failed|");
1011         }
1012         else
1013 #endif
1014         if (debug_locks != expected) {
1015                 unexpected_testcase_failures++;
1016                 printk("FAILED|");
1017
1018                 dump_stack();
1019         } else {
1020                 testcase_successes++;
1021                 printk("  ok  |");
1022         }
1023         testcase_total++;
1024
1025         if (debug_locks_verbose)
1026                 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1027                         lockclass_mask, debug_locks, expected);
1028         /*
1029          * Some tests (e.g. double-unlock) might corrupt the preemption
1030          * count, so restore it:
1031          */
1032         preempt_count_set(saved_preempt_count);
1033 #ifdef CONFIG_TRACE_IRQFLAGS
1034         if (softirq_count())
1035                 current->softirqs_enabled = 0;
1036         else
1037                 current->softirqs_enabled = 1;
1038 #endif
1039
1040         reset_locks();
1041 }
1042
1043 static inline void print_testname(const char *testname)
1044 {
1045         printk("%33s:", testname);
1046 }
1047
1048 #define DO_TESTCASE_1(desc, name, nr)                           \
1049         print_testname(desc"/"#nr);                             \
1050         dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);          \
1051         printk("\n");
1052
1053 #define DO_TESTCASE_1B(desc, name, nr)                          \
1054         print_testname(desc"/"#nr);                             \
1055         dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);          \
1056         printk("\n");
1057
1058 #define DO_TESTCASE_3(desc, name, nr)                           \
1059         print_testname(desc"/"#nr);                             \
1060         dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN);       \
1061         dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);    \
1062         dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
1063         printk("\n");
1064
1065 #define DO_TESTCASE_3RW(desc, name, nr)                         \
1066         print_testname(desc"/"#nr);                             \
1067         dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1068         dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);    \
1069         dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
1070         printk("\n");
1071
1072 #define DO_TESTCASE_6(desc, name)                               \
1073         print_testname(desc);                                   \
1074         dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);            \
1075         dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);         \
1076         dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK);         \
1077         dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);          \
1078         dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);           \
1079         dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);           \
1080         printk("\n");
1081
1082 #define DO_TESTCASE_6_SUCCESS(desc, name)                       \
1083         print_testname(desc);                                   \
1084         dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN);            \
1085         dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK);         \
1086         dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);         \
1087         dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX);          \
1088         dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM);           \
1089         dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM);           \
1090         printk("\n");
1091
1092 /*
1093  * 'read' variant: rlocks must not trigger.
1094  */
1095 #define DO_TESTCASE_6R(desc, name)                              \
1096         print_testname(desc);                                   \
1097         dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);            \
1098         dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);         \
1099         dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);         \
1100         dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);          \
1101         dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);           \
1102         dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);           \
1103         printk("\n");
1104
1105 #define DO_TESTCASE_2I(desc, name, nr)                          \
1106         DO_TESTCASE_1("hard-"desc, name##_hard, nr);            \
1107         DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1108
1109 #define DO_TESTCASE_2IB(desc, name, nr)                         \
1110         DO_TESTCASE_1B("hard-"desc, name##_hard, nr);           \
1111         DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1112
1113 #define DO_TESTCASE_6I(desc, name, nr)                          \
1114         DO_TESTCASE_3("hard-"desc, name##_hard, nr);            \
1115         DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1116
1117 #define DO_TESTCASE_6IRW(desc, name, nr)                        \
1118         DO_TESTCASE_3RW("hard-"desc, name##_hard, nr);          \
1119         DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1120
1121 #define DO_TESTCASE_2x3(desc, name)                             \
1122         DO_TESTCASE_3(desc, name, 12);                          \
1123         DO_TESTCASE_3(desc, name, 21);
1124
1125 #define DO_TESTCASE_2x6(desc, name)                             \
1126         DO_TESTCASE_6I(desc, name, 12);                         \
1127         DO_TESTCASE_6I(desc, name, 21);
1128
1129 #define DO_TESTCASE_6x2(desc, name)                             \
1130         DO_TESTCASE_2I(desc, name, 123);                        \
1131         DO_TESTCASE_2I(desc, name, 132);                        \
1132         DO_TESTCASE_2I(desc, name, 213);                        \
1133         DO_TESTCASE_2I(desc, name, 231);                        \
1134         DO_TESTCASE_2I(desc, name, 312);                        \
1135         DO_TESTCASE_2I(desc, name, 321);
1136
1137 #define DO_TESTCASE_6x2B(desc, name)                            \
1138         DO_TESTCASE_2IB(desc, name, 123);                       \
1139         DO_TESTCASE_2IB(desc, name, 132);                       \
1140         DO_TESTCASE_2IB(desc, name, 213);                       \
1141         DO_TESTCASE_2IB(desc, name, 231);                       \
1142         DO_TESTCASE_2IB(desc, name, 312);                       \
1143         DO_TESTCASE_2IB(desc, name, 321);
1144
1145 #define DO_TESTCASE_6x6(desc, name)                             \
1146         DO_TESTCASE_6I(desc, name, 123);                        \
1147         DO_TESTCASE_6I(desc, name, 132);                        \
1148         DO_TESTCASE_6I(desc, name, 213);                        \
1149         DO_TESTCASE_6I(desc, name, 231);                        \
1150         DO_TESTCASE_6I(desc, name, 312);                        \
1151         DO_TESTCASE_6I(desc, name, 321);
1152
1153 #define DO_TESTCASE_6x6RW(desc, name)                           \
1154         DO_TESTCASE_6IRW(desc, name, 123);                      \
1155         DO_TESTCASE_6IRW(desc, name, 132);                      \
1156         DO_TESTCASE_6IRW(desc, name, 213);                      \
1157         DO_TESTCASE_6IRW(desc, name, 231);                      \
1158         DO_TESTCASE_6IRW(desc, name, 312);                      \
1159         DO_TESTCASE_6IRW(desc, name, 321);
1160
1161 static void ww_test_fail_acquire(void)
1162 {
1163         int ret;
1164
1165         WWAI(&t);
1166         t.stamp++;
1167
1168         ret = WWL(&o, &t);
1169
1170         if (WARN_ON(!o.ctx) ||
1171             WARN_ON(ret))
1172                 return;
1173
1174         /* No lockdep test, pure API */
1175         ret = WWL(&o, &t);
1176         WARN_ON(ret != -EALREADY);
1177
1178         ret = WWT(&o);
1179         WARN_ON(ret);
1180
1181         t2 = t;
1182         t2.stamp++;
1183         ret = WWL(&o, &t2);
1184         WARN_ON(ret != -EDEADLK);
1185         WWU(&o);
1186
1187         if (WWT(&o))
1188                 WWU(&o);
1189 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1190         else
1191                 DEBUG_LOCKS_WARN_ON(1);
1192 #endif
1193 }
1194
1195 static void ww_test_normal(void)
1196 {
1197         int ret;
1198
1199         WWAI(&t);
1200
1201         /*
1202          * None of the ww_mutex codepaths should be taken in the 'normal'
1203          * mutex calls. The easiest way to verify this is by using the
1204          * normal mutex calls, and making sure o.ctx is unmodified.
1205          */
1206
1207         /* mutex_lock (and indirectly, mutex_lock_nested) */
1208         o.ctx = (void *)~0UL;
1209         mutex_lock(&o.base);
1210         mutex_unlock(&o.base);
1211         WARN_ON(o.ctx != (void *)~0UL);
1212
1213         /* mutex_lock_interruptible (and *_nested) */
1214         o.ctx = (void *)~0UL;
1215         ret = mutex_lock_interruptible(&o.base);
1216         if (!ret)
1217                 mutex_unlock(&o.base);
1218         else
1219                 WARN_ON(1);
1220         WARN_ON(o.ctx != (void *)~0UL);
1221
1222         /* mutex_lock_killable (and *_nested) */
1223         o.ctx = (void *)~0UL;
1224         ret = mutex_lock_killable(&o.base);
1225         if (!ret)
1226                 mutex_unlock(&o.base);
1227         else
1228                 WARN_ON(1);
1229         WARN_ON(o.ctx != (void *)~0UL);
1230
1231         /* trylock, succeeding */
1232         o.ctx = (void *)~0UL;
1233         ret = mutex_trylock(&o.base);
1234         WARN_ON(!ret);
1235         if (ret)
1236                 mutex_unlock(&o.base);
1237         else
1238                 WARN_ON(1);
1239         WARN_ON(o.ctx != (void *)~0UL);
1240
1241         /* trylock, failing */
1242         o.ctx = (void *)~0UL;
1243         mutex_lock(&o.base);
1244         ret = mutex_trylock(&o.base);
1245         WARN_ON(ret);
1246         mutex_unlock(&o.base);
1247         WARN_ON(o.ctx != (void *)~0UL);
1248
1249         /* nest_lock */
1250         o.ctx = (void *)~0UL;
1251         mutex_lock_nest_lock(&o.base, &t);
1252         mutex_unlock(&o.base);
1253         WARN_ON(o.ctx != (void *)~0UL);
1254 }
1255
1256 static void ww_test_two_contexts(void)
1257 {
1258         WWAI(&t);
1259         WWAI(&t2);
1260 }
1261
1262 static void ww_test_diff_class(void)
1263 {
1264         WWAI(&t);
1265 #ifdef CONFIG_DEBUG_MUTEXES
1266         t.ww_class = NULL;
1267 #endif
1268         WWL(&o, &t);
1269 }
1270
1271 static void ww_test_context_done_twice(void)
1272 {
1273         WWAI(&t);
1274         WWAD(&t);
1275         WWAD(&t);
1276         WWAF(&t);
1277 }
1278
1279 static void ww_test_context_unlock_twice(void)
1280 {
1281         WWAI(&t);
1282         WWAD(&t);
1283         WWAF(&t);
1284         WWAF(&t);
1285 }
1286
1287 static void ww_test_context_fini_early(void)
1288 {
1289         WWAI(&t);
1290         WWL(&o, &t);
1291         WWAD(&t);
1292         WWAF(&t);
1293 }
1294
1295 static void ww_test_context_lock_after_done(void)
1296 {
1297         WWAI(&t);
1298         WWAD(&t);
1299         WWL(&o, &t);
1300 }
1301
1302 static void ww_test_object_unlock_twice(void)
1303 {
1304         WWL1(&o);
1305         WWU(&o);
1306         WWU(&o);
1307 }
1308
1309 static void ww_test_object_lock_unbalanced(void)
1310 {
1311         WWAI(&t);
1312         WWL(&o, &t);
1313         t.acquired = 0;
1314         WWU(&o);
1315         WWAF(&t);
1316 }
1317
1318 static void ww_test_object_lock_stale_context(void)
1319 {
1320         WWAI(&t);
1321         o.ctx = &t2;
1322         WWL(&o, &t);
1323 }
1324
1325 static void ww_test_edeadlk_normal(void)
1326 {
1327         int ret;
1328
1329         mutex_lock(&o2.base);
1330         o2.ctx = &t2;
1331         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1332
1333         WWAI(&t);
1334         t2 = t;
1335         t2.stamp--;
1336
1337         ret = WWL(&o, &t);
1338         WARN_ON(ret);
1339
1340         ret = WWL(&o2, &t);
1341         WARN_ON(ret != -EDEADLK);
1342
1343         o2.ctx = NULL;
1344         mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1345         mutex_unlock(&o2.base);
1346         WWU(&o);
1347
1348         WWL(&o2, &t);
1349 }
1350
1351 static void ww_test_edeadlk_normal_slow(void)
1352 {
1353         int ret;
1354
1355         mutex_lock(&o2.base);
1356         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1357         o2.ctx = &t2;
1358
1359         WWAI(&t);
1360         t2 = t;
1361         t2.stamp--;
1362
1363         ret = WWL(&o, &t);
1364         WARN_ON(ret);
1365
1366         ret = WWL(&o2, &t);
1367         WARN_ON(ret != -EDEADLK);
1368
1369         o2.ctx = NULL;
1370         mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1371         mutex_unlock(&o2.base);
1372         WWU(&o);
1373
1374         ww_mutex_lock_slow(&o2, &t);
1375 }
1376
1377 static void ww_test_edeadlk_no_unlock(void)
1378 {
1379         int ret;
1380
1381         mutex_lock(&o2.base);
1382         o2.ctx = &t2;
1383         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1384
1385         WWAI(&t);
1386         t2 = t;
1387         t2.stamp--;
1388
1389         ret = WWL(&o, &t);
1390         WARN_ON(ret);
1391
1392         ret = WWL(&o2, &t);
1393         WARN_ON(ret != -EDEADLK);
1394
1395         o2.ctx = NULL;
1396         mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1397         mutex_unlock(&o2.base);
1398
1399         WWL(&o2, &t);
1400 }
1401
1402 static void ww_test_edeadlk_no_unlock_slow(void)
1403 {
1404         int ret;
1405
1406         mutex_lock(&o2.base);
1407         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1408         o2.ctx = &t2;
1409
1410         WWAI(&t);
1411         t2 = t;
1412         t2.stamp--;
1413
1414         ret = WWL(&o, &t);
1415         WARN_ON(ret);
1416
1417         ret = WWL(&o2, &t);
1418         WARN_ON(ret != -EDEADLK);
1419
1420         o2.ctx = NULL;
1421         mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1422         mutex_unlock(&o2.base);
1423
1424         ww_mutex_lock_slow(&o2, &t);
1425 }
1426
1427 static void ww_test_edeadlk_acquire_more(void)
1428 {
1429         int ret;
1430
1431         mutex_lock(&o2.base);
1432         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1433         o2.ctx = &t2;
1434
1435         WWAI(&t);
1436         t2 = t;
1437         t2.stamp--;
1438
1439         ret = WWL(&o, &t);
1440         WARN_ON(ret);
1441
1442         ret = WWL(&o2, &t);
1443         WARN_ON(ret != -EDEADLK);
1444
1445         ret = WWL(&o3, &t);
1446 }
1447
1448 static void ww_test_edeadlk_acquire_more_slow(void)
1449 {
1450         int ret;
1451
1452         mutex_lock(&o2.base);
1453         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1454         o2.ctx = &t2;
1455
1456         WWAI(&t);
1457         t2 = t;
1458         t2.stamp--;
1459
1460         ret = WWL(&o, &t);
1461         WARN_ON(ret);
1462
1463         ret = WWL(&o2, &t);
1464         WARN_ON(ret != -EDEADLK);
1465
1466         ww_mutex_lock_slow(&o3, &t);
1467 }
1468
1469 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1470 {
1471         int ret;
1472
1473         mutex_lock(&o2.base);
1474         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1475         o2.ctx = &t2;
1476
1477         mutex_lock(&o3.base);
1478         mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1479         o3.ctx = &t2;
1480
1481         WWAI(&t);
1482         t2 = t;
1483         t2.stamp--;
1484
1485         ret = WWL(&o, &t);
1486         WARN_ON(ret);
1487
1488         ret = WWL(&o2, &t);
1489         WARN_ON(ret != -EDEADLK);
1490
1491         ret = WWL(&o3, &t);
1492         WARN_ON(ret != -EDEADLK);
1493 }
1494
1495 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1496 {
1497         int ret;
1498
1499         mutex_lock(&o2.base);
1500         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1501         o2.ctx = &t2;
1502
1503         mutex_lock(&o3.base);
1504         mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1505         o3.ctx = &t2;
1506
1507         WWAI(&t);
1508         t2 = t;
1509         t2.stamp--;
1510
1511         ret = WWL(&o, &t);
1512         WARN_ON(ret);
1513
1514         ret = WWL(&o2, &t);
1515         WARN_ON(ret != -EDEADLK);
1516
1517         ww_mutex_lock_slow(&o3, &t);
1518 }
1519
1520 static void ww_test_edeadlk_acquire_wrong(void)
1521 {
1522         int ret;
1523
1524         mutex_lock(&o2.base);
1525         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1526         o2.ctx = &t2;
1527
1528         WWAI(&t);
1529         t2 = t;
1530         t2.stamp--;
1531
1532         ret = WWL(&o, &t);
1533         WARN_ON(ret);
1534
1535         ret = WWL(&o2, &t);
1536         WARN_ON(ret != -EDEADLK);
1537         if (!ret)
1538                 WWU(&o2);
1539
1540         WWU(&o);
1541
1542         ret = WWL(&o3, &t);
1543 }
1544
1545 static void ww_test_edeadlk_acquire_wrong_slow(void)
1546 {
1547         int ret;
1548
1549         mutex_lock(&o2.base);
1550         mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1551         o2.ctx = &t2;
1552
1553         WWAI(&t);
1554         t2 = t;
1555         t2.stamp--;
1556
1557         ret = WWL(&o, &t);
1558         WARN_ON(ret);
1559
1560         ret = WWL(&o2, &t);
1561         WARN_ON(ret != -EDEADLK);
1562         if (!ret)
1563                 WWU(&o2);
1564
1565         WWU(&o);
1566
1567         ww_mutex_lock_slow(&o3, &t);
1568 }
1569
1570 static void ww_test_spin_nest_unlocked(void)
1571 {
1572         raw_spin_lock_nest_lock(&lock_A, &o.base);
1573         U(A);
1574 }
1575
1576 static void ww_test_unneeded_slow(void)
1577 {
1578         WWAI(&t);
1579
1580         ww_mutex_lock_slow(&o, &t);
1581 }
1582
1583 static void ww_test_context_block(void)
1584 {
1585         int ret;
1586
1587         WWAI(&t);
1588
1589         ret = WWL(&o, &t);
1590         WARN_ON(ret);
1591         WWL1(&o2);
1592 }
1593
1594 static void ww_test_context_try(void)
1595 {
1596         int ret;
1597
1598         WWAI(&t);
1599
1600         ret = WWL(&o, &t);
1601         WARN_ON(ret);
1602
1603         ret = WWT(&o2);
1604         WARN_ON(!ret);
1605         WWU(&o2);
1606         WWU(&o);
1607 }
1608
1609 static void ww_test_context_context(void)
1610 {
1611         int ret;
1612
1613         WWAI(&t);
1614
1615         ret = WWL(&o, &t);
1616         WARN_ON(ret);
1617
1618         ret = WWL(&o2, &t);
1619         WARN_ON(ret);
1620
1621         WWU(&o2);
1622         WWU(&o);
1623 }
1624
1625 static void ww_test_try_block(void)
1626 {
1627         bool ret;
1628
1629         ret = WWT(&o);
1630         WARN_ON(!ret);
1631
1632         WWL1(&o2);
1633         WWU(&o2);
1634         WWU(&o);
1635 }
1636
1637 static void ww_test_try_try(void)
1638 {
1639         bool ret;
1640
1641         ret = WWT(&o);
1642         WARN_ON(!ret);
1643         ret = WWT(&o2);
1644         WARN_ON(!ret);
1645         WWU(&o2);
1646         WWU(&o);
1647 }
1648
1649 static void ww_test_try_context(void)
1650 {
1651         int ret;
1652
1653         ret = WWT(&o);
1654         WARN_ON(!ret);
1655
1656         WWAI(&t);
1657
1658         ret = WWL(&o2, &t);
1659         WARN_ON(ret);
1660 }
1661
1662 static void ww_test_block_block(void)
1663 {
1664         WWL1(&o);
1665         WWL1(&o2);
1666 }
1667
1668 static void ww_test_block_try(void)
1669 {
1670         bool ret;
1671
1672         WWL1(&o);
1673         ret = WWT(&o2);
1674         WARN_ON(!ret);
1675 }
1676
1677 static void ww_test_block_context(void)
1678 {
1679         int ret;
1680
1681         WWL1(&o);
1682         WWAI(&t);
1683
1684         ret = WWL(&o2, &t);
1685         WARN_ON(ret);
1686 }
1687
1688 static void ww_test_spin_block(void)
1689 {
1690         L(A);
1691         U(A);
1692
1693         WWL1(&o);
1694         L(A);
1695         U(A);
1696         WWU(&o);
1697
1698         L(A);
1699         WWL1(&o);
1700         WWU(&o);
1701         U(A);
1702 }
1703
1704 static void ww_test_spin_try(void)
1705 {
1706         bool ret;
1707
1708         L(A);
1709         U(A);
1710
1711         ret = WWT(&o);
1712         WARN_ON(!ret);
1713         L(A);
1714         U(A);
1715         WWU(&o);
1716
1717         L(A);
1718         ret = WWT(&o);
1719         WARN_ON(!ret);
1720         WWU(&o);
1721         U(A);
1722 }
1723
1724 static void ww_test_spin_context(void)
1725 {
1726         int ret;
1727
1728         L(A);
1729         U(A);
1730
1731         WWAI(&t);
1732
1733         ret = WWL(&o, &t);
1734         WARN_ON(ret);
1735         L(A);
1736         U(A);
1737         WWU(&o);
1738
1739         L(A);
1740         ret = WWL(&o, &t);
1741         WARN_ON(ret);
1742         WWU(&o);
1743         U(A);
1744 }
1745
1746 static void ww_tests(void)
1747 {
1748         printk("  --------------------------------------------------------------------------\n");
1749         printk("  | Wound/wait tests |\n");
1750         printk("  ---------------------\n");
1751
1752         print_testname("ww api failures");
1753         dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1754         dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1755         dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1756         printk("\n");
1757
1758         print_testname("ww contexts mixing");
1759         dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1760         dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1761         printk("\n");
1762
1763         print_testname("finishing ww context");
1764         dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1765         dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1766         dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1767         dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1768         printk("\n");
1769
1770         print_testname("locking mismatches");
1771         dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1772         dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1773         dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1774         printk("\n");
1775
1776         print_testname("EDEADLK handling");
1777         dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1778         dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1779         dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1780         dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1781         dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1782         dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1783         dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1784         dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1785         dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1786         dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1787         printk("\n");
1788
1789         print_testname("spinlock nest unlocked");
1790         dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1791         printk("\n");
1792
1793         printk("  -----------------------------------------------------\n");
1794         printk("                                 |block | try  |context|\n");
1795         printk("  -----------------------------------------------------\n");
1796
1797         print_testname("context");
1798         dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1799         dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1800         dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1801         printk("\n");
1802
1803         print_testname("try");
1804         dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1805         dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1806         dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1807         printk("\n");
1808
1809         print_testname("block");
1810         dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1811         dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1812         dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1813         printk("\n");
1814
1815         print_testname("spinlock");
1816         dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1817         dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1818         dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1819         printk("\n");
1820 }
1821
1822 void locking_selftest(void)
1823 {
1824         /*
1825          * Got a locking failure before the selftest ran?
1826          */
1827         if (!debug_locks) {
1828                 printk("----------------------------------\n");
1829                 printk("| Locking API testsuite disabled |\n");
1830                 printk("----------------------------------\n");
1831                 return;
1832         }
1833
1834         /*
1835          * Run the testsuite:
1836          */
1837         printk("------------------------\n");
1838         printk("| Locking API testsuite:\n");
1839         printk("----------------------------------------------------------------------------\n");
1840         printk("                                 | spin |wlock |rlock |mutex | wsem | rsem |\n");
1841         printk("  --------------------------------------------------------------------------\n");
1842
1843         init_shared_classes();
1844         debug_locks_silent = !debug_locks_verbose;
1845
1846         DO_TESTCASE_6R("A-A deadlock", AA);
1847         DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1848         DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1849         DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1850         DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1851         DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1852         DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1853         DO_TESTCASE_6("double unlock", double_unlock);
1854         DO_TESTCASE_6("initialize held", init_held);
1855         DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1856
1857         printk("  --------------------------------------------------------------------------\n");
1858         print_testname("recursive read-lock");
1859         printk("             |");
1860         dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1861         printk("             |");
1862         dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1863         printk("\n");
1864
1865         print_testname("recursive read-lock #2");
1866         printk("             |");
1867         dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
1868         printk("             |");
1869         dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1870         printk("\n");
1871
1872         print_testname("mixed read-write-lock");
1873         printk("             |");
1874         dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1875         printk("             |");
1876         dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1877         printk("\n");
1878
1879         print_testname("mixed write-read-lock");
1880         printk("             |");
1881         dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1882         printk("             |");
1883         dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1884         printk("\n");
1885
1886         printk("  --------------------------------------------------------------------------\n");
1887
1888 #ifndef CONFIG_PREEMPT_RT_FULL
1889         /*
1890          * irq-context testcases:
1891          */
1892         DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1893         DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1894         DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1895         DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1896         DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1897         DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1898
1899         DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1900 //      DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1901 #else
1902         /* On -rt, we only do hardirq context test for raw spinlock */
1903         DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
1904         DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
1905
1906         DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
1907         DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
1908
1909         DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
1910         DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
1911         DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
1912         DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
1913         DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
1914         DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
1915
1916         DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
1917         DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
1918         DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
1919         DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
1920         DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
1921         DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
1922 #endif
1923
1924         ww_tests();
1925
1926         if (unexpected_testcase_failures) {
1927                 printk("-----------------------------------------------------------------\n");
1928                 debug_locks = 0;
1929                 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1930                         unexpected_testcase_failures, testcase_total);
1931                 printk("-----------------------------------------------------------------\n");
1932         } else if (expected_testcase_failures && testcase_successes) {
1933                 printk("--------------------------------------------------------\n");
1934                 printk("%3d out of %3d testcases failed, as expected. |\n",
1935                         expected_testcase_failures, testcase_total);
1936                 printk("----------------------------------------------------\n");
1937                 debug_locks = 1;
1938         } else if (expected_testcase_failures && !testcase_successes) {
1939                 printk("--------------------------------------------------------\n");
1940                 printk("All %3d testcases failed, as expected. |\n",
1941                         expected_testcase_failures);
1942                 printk("----------------------------------------\n");
1943                 debug_locks = 1;
1944         } else {
1945                 printk("-------------------------------------------------------\n");
1946                 printk("Good, all %3d testcases passed! |\n",
1947                         testcase_successes);
1948                 printk("---------------------------------\n");
1949                 debug_locks = 1;
1950         }
1951         debug_locks_silent = 0;
1952 }