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