xref: /linux/lib/locking-selftest.c (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * lib/locking-selftest.c
4  *
5  * Testsuite for various locking APIs: spinlocks, rwlocks,
6  * mutexes and rw-semaphores.
7  *
8  * It is checking both false positives and false negatives.
9  *
10  * Started by Ingo Molnar:
11  *
12  *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
13  */
14 #include <linux/rwsem.h>
15 #include <linux/mutex.h>
16 #include <linux/ww_mutex.h>
17 #include <linux/sched.h>
18 #include <linux/sched/mm.h>
19 #include <linux/delay.h>
20 #include <linux/lockdep.h>
21 #include <linux/spinlock.h>
22 #include <linux/kallsyms.h>
23 #include <linux/interrupt.h>
24 #include <linux/debug_locks.h>
25 #include <linux/irqflags.h>
26 #include <linux/rtmutex.h>
27 #include <linux/local_lock.h>
28 
29 /*
30  * Change this to 1 if you want to see the failure printouts:
31  */
32 static unsigned int debug_locks_verbose;
33 unsigned int force_read_lock_recursive;
34 
35 static DEFINE_WD_CLASS(ww_lockdep);
36 
37 static int __init setup_debug_locks_verbose(char *str)
38 {
39 	get_option(&str, &debug_locks_verbose);
40 
41 	return 1;
42 }
43 
44 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
45 
46 #define FAILURE		0
47 #define SUCCESS		1
48 
49 #define LOCKTYPE_SPIN	0x1
50 #define LOCKTYPE_RWLOCK	0x2
51 #define LOCKTYPE_MUTEX	0x4
52 #define LOCKTYPE_RWSEM	0x8
53 #define LOCKTYPE_WW	0x10
54 #define LOCKTYPE_RTMUTEX 0x20
55 #define LOCKTYPE_LL	0x40
56 
57 static struct ww_acquire_ctx t, t2;
58 static struct ww_mutex o, o2, o3;
59 
60 /*
61  * Normal standalone locks, for the circular and irq-context
62  * dependency tests:
63  */
64 static DEFINE_SPINLOCK(lock_A);
65 static DEFINE_SPINLOCK(lock_B);
66 static DEFINE_SPINLOCK(lock_C);
67 static DEFINE_SPINLOCK(lock_D);
68 
69 static DEFINE_RAW_SPINLOCK(raw_lock_A);
70 static DEFINE_RAW_SPINLOCK(raw_lock_B);
71 
72 static DEFINE_RWLOCK(rwlock_A);
73 static DEFINE_RWLOCK(rwlock_B);
74 static DEFINE_RWLOCK(rwlock_C);
75 static DEFINE_RWLOCK(rwlock_D);
76 
77 static DEFINE_MUTEX(mutex_A);
78 static DEFINE_MUTEX(mutex_B);
79 static DEFINE_MUTEX(mutex_C);
80 static DEFINE_MUTEX(mutex_D);
81 
82 static DECLARE_RWSEM(rwsem_A);
83 static DECLARE_RWSEM(rwsem_B);
84 static DECLARE_RWSEM(rwsem_C);
85 static DECLARE_RWSEM(rwsem_D);
86 
87 #ifdef CONFIG_RT_MUTEXES
88 
89 static DEFINE_RT_MUTEX(rtmutex_A);
90 static DEFINE_RT_MUTEX(rtmutex_B);
91 static DEFINE_RT_MUTEX(rtmutex_C);
92 static DEFINE_RT_MUTEX(rtmutex_D);
93 
94 #endif
95 
96 /*
97  * Locks that we initialize dynamically as well so that
98  * e.g. X1 and X2 becomes two instances of the same class,
99  * but X* and Y* are different classes. We do this so that
100  * we do not trigger a real lockup:
101  */
102 static DEFINE_SPINLOCK(lock_X1);
103 static DEFINE_SPINLOCK(lock_X2);
104 static DEFINE_SPINLOCK(lock_Y1);
105 static DEFINE_SPINLOCK(lock_Y2);
106 static DEFINE_SPINLOCK(lock_Z1);
107 static DEFINE_SPINLOCK(lock_Z2);
108 
109 static DEFINE_RWLOCK(rwlock_X1);
110 static DEFINE_RWLOCK(rwlock_X2);
111 static DEFINE_RWLOCK(rwlock_Y1);
112 static DEFINE_RWLOCK(rwlock_Y2);
113 static DEFINE_RWLOCK(rwlock_Z1);
114 static DEFINE_RWLOCK(rwlock_Z2);
115 
116 static DEFINE_MUTEX(mutex_X1);
117 static DEFINE_MUTEX(mutex_X2);
118 static DEFINE_MUTEX(mutex_Y1);
119 static DEFINE_MUTEX(mutex_Y2);
120 static DEFINE_MUTEX(mutex_Z1);
121 static DEFINE_MUTEX(mutex_Z2);
122 
123 static DECLARE_RWSEM(rwsem_X1);
124 static DECLARE_RWSEM(rwsem_X2);
125 static DECLARE_RWSEM(rwsem_Y1);
126 static DECLARE_RWSEM(rwsem_Y2);
127 static DECLARE_RWSEM(rwsem_Z1);
128 static DECLARE_RWSEM(rwsem_Z2);
129 
130 #ifdef CONFIG_RT_MUTEXES
131 
132 static DEFINE_RT_MUTEX(rtmutex_X1);
133 static DEFINE_RT_MUTEX(rtmutex_X2);
134 static DEFINE_RT_MUTEX(rtmutex_Y1);
135 static DEFINE_RT_MUTEX(rtmutex_Y2);
136 static DEFINE_RT_MUTEX(rtmutex_Z1);
137 static DEFINE_RT_MUTEX(rtmutex_Z2);
138 
139 #endif
140 
141 static local_lock_t local_A = INIT_LOCAL_LOCK(local_A);
142 
143 /*
144  * non-inlined runtime initializers, to let separate locks share
145  * the same lock-class:
146  */
147 #define INIT_CLASS_FUNC(class) 				\
148 static noinline void					\
149 init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \
150 	struct mutex *mutex, struct rw_semaphore *rwsem)\
151 {							\
152 	spin_lock_init(lock);			\
153 	rwlock_init(rwlock);				\
154 	mutex_init(mutex);				\
155 	init_rwsem(rwsem);				\
156 }
157 
158 INIT_CLASS_FUNC(X)
159 INIT_CLASS_FUNC(Y)
160 INIT_CLASS_FUNC(Z)
161 
162 static void init_shared_classes(void)
163 {
164 #ifdef CONFIG_RT_MUTEXES
165 	static struct lock_class_key rt_X, rt_Y, rt_Z;
166 
167 	__rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
168 	__rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
169 	__rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
170 	__rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
171 	__rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
172 	__rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
173 #endif
174 
175 	init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
176 	init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
177 
178 	init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
179 	init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
180 
181 	init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
182 	init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
183 }
184 
185 /*
186  * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
187  * The following functions use a lock from a simulated hardirq/softirq
188  * context, causing the locks to be marked as hardirq-safe/softirq-safe:
189  */
190 
191 #define HARDIRQ_DISABLE		local_irq_disable
192 #define HARDIRQ_ENABLE		local_irq_enable
193 
194 #define HARDIRQ_ENTER()				\
195 	local_irq_disable();			\
196 	__irq_enter();				\
197 	WARN_ON(!in_irq());
198 
199 #define HARDIRQ_EXIT()				\
200 	__irq_exit();				\
201 	local_irq_enable();
202 
203 #define SOFTIRQ_DISABLE		local_bh_disable
204 #define SOFTIRQ_ENABLE		local_bh_enable
205 
206 #define SOFTIRQ_ENTER()				\
207 		local_bh_disable();		\
208 		local_irq_disable();		\
209 		lockdep_softirq_enter();	\
210 		WARN_ON(!in_softirq());
211 
212 #define SOFTIRQ_EXIT()				\
213 		lockdep_softirq_exit();		\
214 		local_irq_enable();		\
215 		local_bh_enable();
216 
217 /*
218  * Shortcuts for lock/unlock API variants, to keep
219  * the testcases compact:
220  */
221 #define L(x)			spin_lock(&lock_##x)
222 #define U(x)			spin_unlock(&lock_##x)
223 #define LU(x)			L(x); U(x)
224 #define SI(x)			spin_lock_init(&lock_##x)
225 
226 #define WL(x)			write_lock(&rwlock_##x)
227 #define WU(x)			write_unlock(&rwlock_##x)
228 #define WLU(x)			WL(x); WU(x)
229 
230 #define RL(x)			read_lock(&rwlock_##x)
231 #define RU(x)			read_unlock(&rwlock_##x)
232 #define RLU(x)			RL(x); RU(x)
233 #define RWI(x)			rwlock_init(&rwlock_##x)
234 
235 #define ML(x)			mutex_lock(&mutex_##x)
236 #define MU(x)			mutex_unlock(&mutex_##x)
237 #define MI(x)			mutex_init(&mutex_##x)
238 
239 #define RTL(x)			rt_mutex_lock(&rtmutex_##x)
240 #define RTU(x)			rt_mutex_unlock(&rtmutex_##x)
241 #define RTI(x)			rt_mutex_init(&rtmutex_##x)
242 
243 #define WSL(x)			down_write(&rwsem_##x)
244 #define WSU(x)			up_write(&rwsem_##x)
245 
246 #define RSL(x)			down_read(&rwsem_##x)
247 #define RSU(x)			up_read(&rwsem_##x)
248 #define RWSI(x)			init_rwsem(&rwsem_##x)
249 
250 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
251 #define WWAI(x)			ww_acquire_init(x, &ww_lockdep)
252 #else
253 #define WWAI(x)			do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
254 #endif
255 #define WWAD(x)			ww_acquire_done(x)
256 #define WWAF(x)			ww_acquire_fini(x)
257 
258 #define WWL(x, c)		ww_mutex_lock(x, c)
259 #define WWT(x)			ww_mutex_trylock(x)
260 #define WWL1(x)			ww_mutex_lock(x, NULL)
261 #define WWU(x)			ww_mutex_unlock(x)
262 
263 
264 #define LOCK_UNLOCK_2(x,y)	LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
265 
266 /*
267  * Generate different permutations of the same testcase, using
268  * the same basic lock-dependency/state events:
269  */
270 
271 #define GENERATE_TESTCASE(name)			\
272 						\
273 static void name(void) { E(); }
274 
275 #define GENERATE_PERMUTATIONS_2_EVENTS(name)	\
276 						\
277 static void name##_12(void) { E1(); E2(); }	\
278 static void name##_21(void) { E2(); E1(); }
279 
280 #define GENERATE_PERMUTATIONS_3_EVENTS(name)		\
281 							\
282 static void name##_123(void) { E1(); E2(); E3(); }	\
283 static void name##_132(void) { E1(); E3(); E2(); }	\
284 static void name##_213(void) { E2(); E1(); E3(); }	\
285 static void name##_231(void) { E2(); E3(); E1(); }	\
286 static void name##_312(void) { E3(); E1(); E2(); }	\
287 static void name##_321(void) { E3(); E2(); E1(); }
288 
289 /*
290  * AA deadlock:
291  */
292 
293 #define E()					\
294 						\
295 	LOCK(X1);				\
296 	LOCK(X2); /* this one should fail */
297 
298 /*
299  * 6 testcases:
300  */
301 #include "locking-selftest-spin.h"
302 GENERATE_TESTCASE(AA_spin)
303 #include "locking-selftest-wlock.h"
304 GENERATE_TESTCASE(AA_wlock)
305 #include "locking-selftest-rlock.h"
306 GENERATE_TESTCASE(AA_rlock)
307 #include "locking-selftest-mutex.h"
308 GENERATE_TESTCASE(AA_mutex)
309 #include "locking-selftest-wsem.h"
310 GENERATE_TESTCASE(AA_wsem)
311 #include "locking-selftest-rsem.h"
312 GENERATE_TESTCASE(AA_rsem)
313 
314 #ifdef CONFIG_RT_MUTEXES
315 #include "locking-selftest-rtmutex.h"
316 GENERATE_TESTCASE(AA_rtmutex);
317 #endif
318 
319 #undef E
320 
321 /*
322  * Special-case for read-locking, they are
323  * allowed to recurse on the same lock class:
324  */
325 static void rlock_AA1(void)
326 {
327 	RL(X1);
328 	RL(X1); // this one should NOT fail
329 }
330 
331 static void rlock_AA1B(void)
332 {
333 	RL(X1);
334 	RL(X2); // this one should NOT fail
335 }
336 
337 static void rsem_AA1(void)
338 {
339 	RSL(X1);
340 	RSL(X1); // this one should fail
341 }
342 
343 static void rsem_AA1B(void)
344 {
345 	RSL(X1);
346 	RSL(X2); // this one should fail
347 }
348 /*
349  * The mixing of read and write locks is not allowed:
350  */
351 static void rlock_AA2(void)
352 {
353 	RL(X1);
354 	WL(X2); // this one should fail
355 }
356 
357 static void rsem_AA2(void)
358 {
359 	RSL(X1);
360 	WSL(X2); // this one should fail
361 }
362 
363 static void rlock_AA3(void)
364 {
365 	WL(X1);
366 	RL(X2); // this one should fail
367 }
368 
369 static void rsem_AA3(void)
370 {
371 	WSL(X1);
372 	RSL(X2); // this one should fail
373 }
374 
375 /*
376  * read_lock(A)
377  * spin_lock(B)
378  *		spin_lock(B)
379  *		write_lock(A)
380  */
381 static void rlock_ABBA1(void)
382 {
383 	RL(X1);
384 	L(Y1);
385 	U(Y1);
386 	RU(X1);
387 
388 	L(Y1);
389 	WL(X1);
390 	WU(X1);
391 	U(Y1); // should fail
392 }
393 
394 static void rwsem_ABBA1(void)
395 {
396 	RSL(X1);
397 	ML(Y1);
398 	MU(Y1);
399 	RSU(X1);
400 
401 	ML(Y1);
402 	WSL(X1);
403 	WSU(X1);
404 	MU(Y1); // should fail
405 }
406 
407 /*
408  * read_lock(A)
409  * spin_lock(B)
410  *		spin_lock(B)
411  *		write_lock(A)
412  *
413  * This test case is aimed at poking whether the chain cache prevents us from
414  * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
415  * read/write locks, the following case may happen
416  *
417  * 	{ read_lock(A)->lock(B) dependency exists }
418  *
419  * 	P0:
420  * 	lock(B);
421  * 	read_lock(A);
422  *
423  *	{ Not a deadlock, B -> A is added in the chain cache }
424  *
425  *	P1:
426  *	lock(B);
427  *	write_lock(A);
428  *
429  *	{ B->A found in chain cache, not reported as a deadlock }
430  *
431  */
432 static void rlock_chaincache_ABBA1(void)
433 {
434 	RL(X1);
435 	L(Y1);
436 	U(Y1);
437 	RU(X1);
438 
439 	L(Y1);
440 	RL(X1);
441 	RU(X1);
442 	U(Y1);
443 
444 	L(Y1);
445 	WL(X1);
446 	WU(X1);
447 	U(Y1); // should fail
448 }
449 
450 /*
451  * read_lock(A)
452  * spin_lock(B)
453  *		spin_lock(B)
454  *		read_lock(A)
455  */
456 static void rlock_ABBA2(void)
457 {
458 	RL(X1);
459 	L(Y1);
460 	U(Y1);
461 	RU(X1);
462 
463 	L(Y1);
464 	RL(X1);
465 	RU(X1);
466 	U(Y1); // should NOT fail
467 }
468 
469 static void rwsem_ABBA2(void)
470 {
471 	RSL(X1);
472 	ML(Y1);
473 	MU(Y1);
474 	RSU(X1);
475 
476 	ML(Y1);
477 	RSL(X1);
478 	RSU(X1);
479 	MU(Y1); // should fail
480 }
481 
482 
483 /*
484  * write_lock(A)
485  * spin_lock(B)
486  *		spin_lock(B)
487  *		write_lock(A)
488  */
489 static void rlock_ABBA3(void)
490 {
491 	WL(X1);
492 	L(Y1);
493 	U(Y1);
494 	WU(X1);
495 
496 	L(Y1);
497 	WL(X1);
498 	WU(X1);
499 	U(Y1); // should fail
500 }
501 
502 static void rwsem_ABBA3(void)
503 {
504 	WSL(X1);
505 	ML(Y1);
506 	MU(Y1);
507 	WSU(X1);
508 
509 	ML(Y1);
510 	WSL(X1);
511 	WSU(X1);
512 	MU(Y1); // should fail
513 }
514 
515 /*
516  * ABBA deadlock:
517  */
518 
519 #define E()					\
520 						\
521 	LOCK_UNLOCK_2(A, B);			\
522 	LOCK_UNLOCK_2(B, A); /* fail */
523 
524 /*
525  * 6 testcases:
526  */
527 #include "locking-selftest-spin.h"
528 GENERATE_TESTCASE(ABBA_spin)
529 #include "locking-selftest-wlock.h"
530 GENERATE_TESTCASE(ABBA_wlock)
531 #include "locking-selftest-rlock.h"
532 GENERATE_TESTCASE(ABBA_rlock)
533 #include "locking-selftest-mutex.h"
534 GENERATE_TESTCASE(ABBA_mutex)
535 #include "locking-selftest-wsem.h"
536 GENERATE_TESTCASE(ABBA_wsem)
537 #include "locking-selftest-rsem.h"
538 GENERATE_TESTCASE(ABBA_rsem)
539 
540 #ifdef CONFIG_RT_MUTEXES
541 #include "locking-selftest-rtmutex.h"
542 GENERATE_TESTCASE(ABBA_rtmutex);
543 #endif
544 
545 #undef E
546 
547 /*
548  * AB BC CA deadlock:
549  */
550 
551 #define E()					\
552 						\
553 	LOCK_UNLOCK_2(A, B);			\
554 	LOCK_UNLOCK_2(B, C);			\
555 	LOCK_UNLOCK_2(C, A); /* fail */
556 
557 /*
558  * 6 testcases:
559  */
560 #include "locking-selftest-spin.h"
561 GENERATE_TESTCASE(ABBCCA_spin)
562 #include "locking-selftest-wlock.h"
563 GENERATE_TESTCASE(ABBCCA_wlock)
564 #include "locking-selftest-rlock.h"
565 GENERATE_TESTCASE(ABBCCA_rlock)
566 #include "locking-selftest-mutex.h"
567 GENERATE_TESTCASE(ABBCCA_mutex)
568 #include "locking-selftest-wsem.h"
569 GENERATE_TESTCASE(ABBCCA_wsem)
570 #include "locking-selftest-rsem.h"
571 GENERATE_TESTCASE(ABBCCA_rsem)
572 
573 #ifdef CONFIG_RT_MUTEXES
574 #include "locking-selftest-rtmutex.h"
575 GENERATE_TESTCASE(ABBCCA_rtmutex);
576 #endif
577 
578 #undef E
579 
580 /*
581  * AB CA BC deadlock:
582  */
583 
584 #define E()					\
585 						\
586 	LOCK_UNLOCK_2(A, B);			\
587 	LOCK_UNLOCK_2(C, A);			\
588 	LOCK_UNLOCK_2(B, C); /* fail */
589 
590 /*
591  * 6 testcases:
592  */
593 #include "locking-selftest-spin.h"
594 GENERATE_TESTCASE(ABCABC_spin)
595 #include "locking-selftest-wlock.h"
596 GENERATE_TESTCASE(ABCABC_wlock)
597 #include "locking-selftest-rlock.h"
598 GENERATE_TESTCASE(ABCABC_rlock)
599 #include "locking-selftest-mutex.h"
600 GENERATE_TESTCASE(ABCABC_mutex)
601 #include "locking-selftest-wsem.h"
602 GENERATE_TESTCASE(ABCABC_wsem)
603 #include "locking-selftest-rsem.h"
604 GENERATE_TESTCASE(ABCABC_rsem)
605 
606 #ifdef CONFIG_RT_MUTEXES
607 #include "locking-selftest-rtmutex.h"
608 GENERATE_TESTCASE(ABCABC_rtmutex);
609 #endif
610 
611 #undef E
612 
613 /*
614  * AB BC CD DA deadlock:
615  */
616 
617 #define E()					\
618 						\
619 	LOCK_UNLOCK_2(A, B);			\
620 	LOCK_UNLOCK_2(B, C);			\
621 	LOCK_UNLOCK_2(C, D);			\
622 	LOCK_UNLOCK_2(D, A); /* fail */
623 
624 /*
625  * 6 testcases:
626  */
627 #include "locking-selftest-spin.h"
628 GENERATE_TESTCASE(ABBCCDDA_spin)
629 #include "locking-selftest-wlock.h"
630 GENERATE_TESTCASE(ABBCCDDA_wlock)
631 #include "locking-selftest-rlock.h"
632 GENERATE_TESTCASE(ABBCCDDA_rlock)
633 #include "locking-selftest-mutex.h"
634 GENERATE_TESTCASE(ABBCCDDA_mutex)
635 #include "locking-selftest-wsem.h"
636 GENERATE_TESTCASE(ABBCCDDA_wsem)
637 #include "locking-selftest-rsem.h"
638 GENERATE_TESTCASE(ABBCCDDA_rsem)
639 
640 #ifdef CONFIG_RT_MUTEXES
641 #include "locking-selftest-rtmutex.h"
642 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
643 #endif
644 
645 #undef E
646 
647 /*
648  * AB CD BD DA deadlock:
649  */
650 #define E()					\
651 						\
652 	LOCK_UNLOCK_2(A, B);			\
653 	LOCK_UNLOCK_2(C, D);			\
654 	LOCK_UNLOCK_2(B, D);			\
655 	LOCK_UNLOCK_2(D, A); /* fail */
656 
657 /*
658  * 6 testcases:
659  */
660 #include "locking-selftest-spin.h"
661 GENERATE_TESTCASE(ABCDBDDA_spin)
662 #include "locking-selftest-wlock.h"
663 GENERATE_TESTCASE(ABCDBDDA_wlock)
664 #include "locking-selftest-rlock.h"
665 GENERATE_TESTCASE(ABCDBDDA_rlock)
666 #include "locking-selftest-mutex.h"
667 GENERATE_TESTCASE(ABCDBDDA_mutex)
668 #include "locking-selftest-wsem.h"
669 GENERATE_TESTCASE(ABCDBDDA_wsem)
670 #include "locking-selftest-rsem.h"
671 GENERATE_TESTCASE(ABCDBDDA_rsem)
672 
673 #ifdef CONFIG_RT_MUTEXES
674 #include "locking-selftest-rtmutex.h"
675 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
676 #endif
677 
678 #undef E
679 
680 /*
681  * AB CD BC DA deadlock:
682  */
683 #define E()					\
684 						\
685 	LOCK_UNLOCK_2(A, B);			\
686 	LOCK_UNLOCK_2(C, D);			\
687 	LOCK_UNLOCK_2(B, C);			\
688 	LOCK_UNLOCK_2(D, A); /* fail */
689 
690 /*
691  * 6 testcases:
692  */
693 #include "locking-selftest-spin.h"
694 GENERATE_TESTCASE(ABCDBCDA_spin)
695 #include "locking-selftest-wlock.h"
696 GENERATE_TESTCASE(ABCDBCDA_wlock)
697 #include "locking-selftest-rlock.h"
698 GENERATE_TESTCASE(ABCDBCDA_rlock)
699 #include "locking-selftest-mutex.h"
700 GENERATE_TESTCASE(ABCDBCDA_mutex)
701 #include "locking-selftest-wsem.h"
702 GENERATE_TESTCASE(ABCDBCDA_wsem)
703 #include "locking-selftest-rsem.h"
704 GENERATE_TESTCASE(ABCDBCDA_rsem)
705 
706 #ifdef CONFIG_RT_MUTEXES
707 #include "locking-selftest-rtmutex.h"
708 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
709 #endif
710 
711 #undef E
712 
713 /*
714  * Double unlock:
715  */
716 #define E()					\
717 						\
718 	LOCK(A);				\
719 	UNLOCK(A);				\
720 	UNLOCK(A); /* fail */
721 
722 /*
723  * 6 testcases:
724  */
725 #include "locking-selftest-spin.h"
726 GENERATE_TESTCASE(double_unlock_spin)
727 #include "locking-selftest-wlock.h"
728 GENERATE_TESTCASE(double_unlock_wlock)
729 #include "locking-selftest-rlock.h"
730 GENERATE_TESTCASE(double_unlock_rlock)
731 #include "locking-selftest-mutex.h"
732 GENERATE_TESTCASE(double_unlock_mutex)
733 #include "locking-selftest-wsem.h"
734 GENERATE_TESTCASE(double_unlock_wsem)
735 #include "locking-selftest-rsem.h"
736 GENERATE_TESTCASE(double_unlock_rsem)
737 
738 #ifdef CONFIG_RT_MUTEXES
739 #include "locking-selftest-rtmutex.h"
740 GENERATE_TESTCASE(double_unlock_rtmutex);
741 #endif
742 
743 #undef E
744 
745 /*
746  * initializing a held lock:
747  */
748 #define E()					\
749 						\
750 	LOCK(A);				\
751 	INIT(A); /* fail */
752 
753 /*
754  * 6 testcases:
755  */
756 #include "locking-selftest-spin.h"
757 GENERATE_TESTCASE(init_held_spin)
758 #include "locking-selftest-wlock.h"
759 GENERATE_TESTCASE(init_held_wlock)
760 #include "locking-selftest-rlock.h"
761 GENERATE_TESTCASE(init_held_rlock)
762 #include "locking-selftest-mutex.h"
763 GENERATE_TESTCASE(init_held_mutex)
764 #include "locking-selftest-wsem.h"
765 GENERATE_TESTCASE(init_held_wsem)
766 #include "locking-selftest-rsem.h"
767 GENERATE_TESTCASE(init_held_rsem)
768 
769 #ifdef CONFIG_RT_MUTEXES
770 #include "locking-selftest-rtmutex.h"
771 GENERATE_TESTCASE(init_held_rtmutex);
772 #endif
773 
774 #undef E
775 
776 /*
777  * locking an irq-safe lock with irqs enabled:
778  */
779 #define E1()				\
780 					\
781 	IRQ_ENTER();			\
782 	LOCK(A);			\
783 	UNLOCK(A);			\
784 	IRQ_EXIT();
785 
786 #define E2()				\
787 					\
788 	LOCK(A);			\
789 	UNLOCK(A);
790 
791 /*
792  * Generate 24 testcases:
793  */
794 #include "locking-selftest-spin-hardirq.h"
795 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
796 
797 #include "locking-selftest-rlock-hardirq.h"
798 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
799 
800 #include "locking-selftest-wlock-hardirq.h"
801 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
802 
803 #include "locking-selftest-spin-softirq.h"
804 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
805 
806 #include "locking-selftest-rlock-softirq.h"
807 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
808 
809 #include "locking-selftest-wlock-softirq.h"
810 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
811 
812 #undef E1
813 #undef E2
814 
815 /*
816  * Enabling hardirqs with a softirq-safe lock held:
817  */
818 #define E1()				\
819 					\
820 	SOFTIRQ_ENTER();		\
821 	LOCK(A);			\
822 	UNLOCK(A);			\
823 	SOFTIRQ_EXIT();
824 
825 #define E2()				\
826 					\
827 	HARDIRQ_DISABLE();		\
828 	LOCK(A);			\
829 	HARDIRQ_ENABLE();		\
830 	UNLOCK(A);
831 
832 /*
833  * Generate 12 testcases:
834  */
835 #include "locking-selftest-spin.h"
836 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
837 
838 #include "locking-selftest-wlock.h"
839 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
840 
841 #include "locking-selftest-rlock.h"
842 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
843 
844 #undef E1
845 #undef E2
846 
847 /*
848  * Enabling irqs with an irq-safe lock held:
849  */
850 #define E1()				\
851 					\
852 	IRQ_ENTER();			\
853 	LOCK(A);			\
854 	UNLOCK(A);			\
855 	IRQ_EXIT();
856 
857 #define E2()				\
858 					\
859 	IRQ_DISABLE();			\
860 	LOCK(A);			\
861 	IRQ_ENABLE();			\
862 	UNLOCK(A);
863 
864 /*
865  * Generate 24 testcases:
866  */
867 #include "locking-selftest-spin-hardirq.h"
868 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
869 
870 #include "locking-selftest-rlock-hardirq.h"
871 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
872 
873 #include "locking-selftest-wlock-hardirq.h"
874 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
875 
876 #include "locking-selftest-spin-softirq.h"
877 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
878 
879 #include "locking-selftest-rlock-softirq.h"
880 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
881 
882 #include "locking-selftest-wlock-softirq.h"
883 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
884 
885 #undef E1
886 #undef E2
887 
888 /*
889  * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
890  */
891 #define E1()				\
892 					\
893 	LOCK(A);			\
894 	LOCK(B);			\
895 	UNLOCK(B);			\
896 	UNLOCK(A);			\
897 
898 #define E2()				\
899 					\
900 	LOCK(B);			\
901 	UNLOCK(B);
902 
903 #define E3()				\
904 					\
905 	IRQ_ENTER();			\
906 	LOCK(A);			\
907 	UNLOCK(A);			\
908 	IRQ_EXIT();
909 
910 /*
911  * Generate 36 testcases:
912  */
913 #include "locking-selftest-spin-hardirq.h"
914 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
915 
916 #include "locking-selftest-rlock-hardirq.h"
917 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
918 
919 #include "locking-selftest-wlock-hardirq.h"
920 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
921 
922 #include "locking-selftest-spin-softirq.h"
923 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
924 
925 #include "locking-selftest-rlock-softirq.h"
926 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
927 
928 #include "locking-selftest-wlock-softirq.h"
929 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
930 
931 #undef E1
932 #undef E2
933 #undef E3
934 
935 /*
936  * If a lock turns into softirq-safe, but earlier it took
937  * a softirq-unsafe lock:
938  */
939 
940 #define E1()				\
941 	IRQ_DISABLE();			\
942 	LOCK(A);			\
943 	LOCK(B);			\
944 	UNLOCK(B);			\
945 	UNLOCK(A);			\
946 	IRQ_ENABLE();
947 
948 #define E2()				\
949 	LOCK(B);			\
950 	UNLOCK(B);
951 
952 #define E3()				\
953 	IRQ_ENTER();			\
954 	LOCK(A);			\
955 	UNLOCK(A);			\
956 	IRQ_EXIT();
957 
958 /*
959  * Generate 36 testcases:
960  */
961 #include "locking-selftest-spin-hardirq.h"
962 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
963 
964 #include "locking-selftest-rlock-hardirq.h"
965 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
966 
967 #include "locking-selftest-wlock-hardirq.h"
968 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
969 
970 #include "locking-selftest-spin-softirq.h"
971 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
972 
973 #include "locking-selftest-rlock-softirq.h"
974 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
975 
976 #include "locking-selftest-wlock-softirq.h"
977 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
978 
979 #undef E1
980 #undef E2
981 #undef E3
982 
983 /*
984  * read-lock / write-lock irq inversion.
985  *
986  * Deadlock scenario:
987  *
988  * CPU#1 is at #1, i.e. it has write-locked A, but has not
989  * taken B yet.
990  *
991  * CPU#2 is at #2, i.e. it has locked B.
992  *
993  * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
994  *
995  * The deadlock occurs because CPU#1 will spin on B, and CPU#2
996  * will spin on A.
997  */
998 
999 #define E1()				\
1000 					\
1001 	IRQ_DISABLE();			\
1002 	WL(A);				\
1003 	LOCK(B);			\
1004 	UNLOCK(B);			\
1005 	WU(A);				\
1006 	IRQ_ENABLE();
1007 
1008 #define E2()				\
1009 					\
1010 	LOCK(B);			\
1011 	UNLOCK(B);
1012 
1013 #define E3()				\
1014 					\
1015 	IRQ_ENTER();			\
1016 	RL(A);				\
1017 	RU(A);				\
1018 	IRQ_EXIT();
1019 
1020 /*
1021  * Generate 36 testcases:
1022  */
1023 #include "locking-selftest-spin-hardirq.h"
1024 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
1025 
1026 #include "locking-selftest-rlock-hardirq.h"
1027 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
1028 
1029 #include "locking-selftest-wlock-hardirq.h"
1030 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
1031 
1032 #include "locking-selftest-spin-softirq.h"
1033 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
1034 
1035 #include "locking-selftest-rlock-softirq.h"
1036 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
1037 
1038 #include "locking-selftest-wlock-softirq.h"
1039 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
1040 
1041 #undef E1
1042 #undef E2
1043 #undef E3
1044 
1045 /*
1046  * write-read / write-read / write-read deadlock even if read is recursive
1047  */
1048 
1049 #define E1()				\
1050 					\
1051 	WL(X1);				\
1052 	RL(Y1);				\
1053 	RU(Y1);				\
1054 	WU(X1);
1055 
1056 #define E2()				\
1057 					\
1058 	WL(Y1);				\
1059 	RL(Z1);				\
1060 	RU(Z1);				\
1061 	WU(Y1);
1062 
1063 #define E3()				\
1064 					\
1065 	WL(Z1);				\
1066 	RL(X1);				\
1067 	RU(X1);				\
1068 	WU(Z1);
1069 
1070 #include "locking-selftest-rlock.h"
1071 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
1072 
1073 #undef E1
1074 #undef E2
1075 #undef E3
1076 
1077 /*
1078  * write-write / read-read / write-read deadlock even if read is recursive
1079  */
1080 
1081 #define E1()				\
1082 					\
1083 	WL(X1);				\
1084 	WL(Y1);				\
1085 	WU(Y1);				\
1086 	WU(X1);
1087 
1088 #define E2()				\
1089 					\
1090 	RL(Y1);				\
1091 	RL(Z1);				\
1092 	RU(Z1);				\
1093 	RU(Y1);
1094 
1095 #define E3()				\
1096 					\
1097 	WL(Z1);				\
1098 	RL(X1);				\
1099 	RU(X1);				\
1100 	WU(Z1);
1101 
1102 #include "locking-selftest-rlock.h"
1103 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
1104 
1105 #undef E1
1106 #undef E2
1107 #undef E3
1108 
1109 /*
1110  * write-write / read-read / read-write is not deadlock when read is recursive
1111  */
1112 
1113 #define E1()				\
1114 					\
1115 	WL(X1);				\
1116 	WL(Y1);				\
1117 	WU(Y1);				\
1118 	WU(X1);
1119 
1120 #define E2()				\
1121 					\
1122 	RL(Y1);				\
1123 	RL(Z1);				\
1124 	RU(Z1);				\
1125 	RU(Y1);
1126 
1127 #define E3()				\
1128 					\
1129 	RL(Z1);				\
1130 	WL(X1);				\
1131 	WU(X1);				\
1132 	RU(Z1);
1133 
1134 #include "locking-selftest-rlock.h"
1135 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
1136 
1137 #undef E1
1138 #undef E2
1139 #undef E3
1140 
1141 /*
1142  * write-read / read-read / write-write is not deadlock when read is recursive
1143  */
1144 
1145 #define E1()				\
1146 					\
1147 	WL(X1);				\
1148 	RL(Y1);				\
1149 	RU(Y1);				\
1150 	WU(X1);
1151 
1152 #define E2()				\
1153 					\
1154 	RL(Y1);				\
1155 	RL(Z1);				\
1156 	RU(Z1);				\
1157 	RU(Y1);
1158 
1159 #define E3()				\
1160 					\
1161 	WL(Z1);				\
1162 	WL(X1);				\
1163 	WU(X1);				\
1164 	WU(Z1);
1165 
1166 #include "locking-selftest-rlock.h"
1167 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
1168 
1169 #undef E1
1170 #undef E2
1171 #undef E3
1172 /*
1173  * read-lock / write-lock recursion that is actually safe.
1174  */
1175 
1176 #define E1()				\
1177 					\
1178 	IRQ_DISABLE();			\
1179 	WL(A);				\
1180 	WU(A);				\
1181 	IRQ_ENABLE();
1182 
1183 #define E2()				\
1184 					\
1185 	RL(A);				\
1186 	RU(A);				\
1187 
1188 #define E3()				\
1189 					\
1190 	IRQ_ENTER();			\
1191 	LOCK(A);			\
1192 	L(B);				\
1193 	U(B);				\
1194 	UNLOCK(A);			\
1195 	IRQ_EXIT();
1196 
1197 /*
1198  * Generate 24 testcases:
1199  */
1200 #include "locking-selftest-hardirq.h"
1201 #include "locking-selftest-rlock.h"
1202 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
1203 
1204 #include "locking-selftest-wlock.h"
1205 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
1206 
1207 #include "locking-selftest-softirq.h"
1208 #include "locking-selftest-rlock.h"
1209 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
1210 
1211 #include "locking-selftest-wlock.h"
1212 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
1213 
1214 #undef E1
1215 #undef E2
1216 #undef E3
1217 
1218 /*
1219  * read-lock / write-lock recursion that is unsafe.
1220  */
1221 
1222 #define E1()				\
1223 					\
1224 	IRQ_DISABLE();			\
1225 	L(B);				\
1226 	LOCK(A);			\
1227 	UNLOCK(A);			\
1228 	U(B);				\
1229 	IRQ_ENABLE();
1230 
1231 #define E2()				\
1232 					\
1233 	RL(A);				\
1234 	RU(A);				\
1235 
1236 #define E3()				\
1237 					\
1238 	IRQ_ENTER();			\
1239 	L(B);				\
1240 	U(B);				\
1241 	IRQ_EXIT();
1242 
1243 /*
1244  * Generate 24 testcases:
1245  */
1246 #include "locking-selftest-hardirq.h"
1247 #include "locking-selftest-rlock.h"
1248 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
1249 
1250 #include "locking-selftest-wlock.h"
1251 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
1252 
1253 #include "locking-selftest-softirq.h"
1254 #include "locking-selftest-rlock.h"
1255 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
1256 
1257 #include "locking-selftest-wlock.h"
1258 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
1259 
1260 #undef E1
1261 #undef E2
1262 #undef E3
1263 /*
1264  * read-lock / write-lock recursion that is unsafe.
1265  *
1266  * A is a ENABLED_*_READ lock
1267  * B is a USED_IN_*_READ lock
1268  *
1269  * read_lock(A);
1270  *			write_lock(B);
1271  * <interrupt>
1272  * read_lock(B);
1273  * 			write_lock(A); // if this one is read_lock(), no deadlock
1274  */
1275 
1276 #define E1()				\
1277 					\
1278 	IRQ_DISABLE();			\
1279 	WL(B);				\
1280 	LOCK(A);			\
1281 	UNLOCK(A);			\
1282 	WU(B);				\
1283 	IRQ_ENABLE();
1284 
1285 #define E2()				\
1286 					\
1287 	RL(A);				\
1288 	RU(A);				\
1289 
1290 #define E3()				\
1291 					\
1292 	IRQ_ENTER();			\
1293 	RL(B);				\
1294 	RU(B);				\
1295 	IRQ_EXIT();
1296 
1297 /*
1298  * Generate 24 testcases:
1299  */
1300 #include "locking-selftest-hardirq.h"
1301 #include "locking-selftest-rlock.h"
1302 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
1303 
1304 #include "locking-selftest-wlock.h"
1305 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
1306 
1307 #include "locking-selftest-softirq.h"
1308 #include "locking-selftest-rlock.h"
1309 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
1310 
1311 #include "locking-selftest-wlock.h"
1312 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
1313 
1314 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1315 # define I_SPINLOCK(x)	lockdep_reset_lock(&lock_##x.dep_map)
1316 # define I_RAW_SPINLOCK(x)	lockdep_reset_lock(&raw_lock_##x.dep_map)
1317 # define I_RWLOCK(x)	lockdep_reset_lock(&rwlock_##x.dep_map)
1318 # define I_MUTEX(x)	lockdep_reset_lock(&mutex_##x.dep_map)
1319 # define I_RWSEM(x)	lockdep_reset_lock(&rwsem_##x.dep_map)
1320 # define I_WW(x)	lockdep_reset_lock(&x.dep_map)
1321 # define I_LOCAL_LOCK(x) lockdep_reset_lock(&local_##x.dep_map)
1322 #ifdef CONFIG_RT_MUTEXES
1323 # define I_RTMUTEX(x)	lockdep_reset_lock(&rtmutex_##x.dep_map)
1324 #endif
1325 #else
1326 # define I_SPINLOCK(x)
1327 # define I_RAW_SPINLOCK(x)
1328 # define I_RWLOCK(x)
1329 # define I_MUTEX(x)
1330 # define I_RWSEM(x)
1331 # define I_WW(x)
1332 # define I_LOCAL_LOCK(x)
1333 #endif
1334 
1335 #ifndef I_RTMUTEX
1336 # define I_RTMUTEX(x)
1337 #endif
1338 
1339 #ifdef CONFIG_RT_MUTEXES
1340 #define I2_RTMUTEX(x)	rt_mutex_init(&rtmutex_##x)
1341 #else
1342 #define I2_RTMUTEX(x)
1343 #endif
1344 
1345 #define I1(x)					\
1346 	do {					\
1347 		I_SPINLOCK(x);			\
1348 		I_RWLOCK(x);			\
1349 		I_MUTEX(x);			\
1350 		I_RWSEM(x);			\
1351 		I_RTMUTEX(x);			\
1352 	} while (0)
1353 
1354 #define I2(x)					\
1355 	do {					\
1356 		spin_lock_init(&lock_##x);	\
1357 		rwlock_init(&rwlock_##x);	\
1358 		mutex_init(&mutex_##x);		\
1359 		init_rwsem(&rwsem_##x);		\
1360 		I2_RTMUTEX(x);			\
1361 	} while (0)
1362 
1363 static void reset_locks(void)
1364 {
1365 	local_irq_disable();
1366 	lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1367 	lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1368 
1369 	I1(A); I1(B); I1(C); I1(D);
1370 	I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1371 	I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
1372 	I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B);
1373 	I_LOCAL_LOCK(A);
1374 
1375 	lockdep_reset();
1376 
1377 	I2(A); I2(B); I2(C); I2(D);
1378 	init_shared_classes();
1379 	raw_spin_lock_init(&raw_lock_A);
1380 	raw_spin_lock_init(&raw_lock_B);
1381 	local_lock_init(&local_A);
1382 
1383 	ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1384 	memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1385 	memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1386 	memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
1387 	local_irq_enable();
1388 }
1389 
1390 #undef I
1391 
1392 static int testcase_total;
1393 static int testcase_successes;
1394 static int expected_testcase_failures;
1395 static int unexpected_testcase_failures;
1396 
1397 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1398 {
1399 	unsigned long saved_preempt_count = preempt_count();
1400 
1401 	WARN_ON(irqs_disabled());
1402 
1403 	debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
1404 
1405 	testcase_fn();
1406 	/*
1407 	 * Filter out expected failures:
1408 	 */
1409 #ifndef CONFIG_PROVE_LOCKING
1410 	if (expected == FAILURE && debug_locks) {
1411 		expected_testcase_failures++;
1412 		pr_cont("failed|");
1413 	}
1414 	else
1415 #endif
1416 	if (debug_locks != expected) {
1417 		unexpected_testcase_failures++;
1418 		pr_cont("FAILED|");
1419 	} else {
1420 		testcase_successes++;
1421 		pr_cont("  ok  |");
1422 	}
1423 	testcase_total++;
1424 
1425 	if (debug_locks_verbose & lockclass_mask)
1426 		pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1427 			lockclass_mask, debug_locks, expected);
1428 	/*
1429 	 * Some tests (e.g. double-unlock) might corrupt the preemption
1430 	 * count, so restore it:
1431 	 */
1432 	preempt_count_set(saved_preempt_count);
1433 #ifdef CONFIG_TRACE_IRQFLAGS
1434 	if (softirq_count())
1435 		current->softirqs_enabled = 0;
1436 	else
1437 		current->softirqs_enabled = 1;
1438 #endif
1439 
1440 	reset_locks();
1441 }
1442 
1443 #ifdef CONFIG_RT_MUTEXES
1444 #define dotest_rt(fn, e, m)	dotest((fn), (e), (m))
1445 #else
1446 #define dotest_rt(fn, e, m)
1447 #endif
1448 
1449 static inline void print_testname(const char *testname)
1450 {
1451 	printk("%33s:", testname);
1452 }
1453 
1454 #define DO_TESTCASE_1(desc, name, nr)				\
1455 	print_testname(desc"/"#nr);				\
1456 	dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);		\
1457 	pr_cont("\n");
1458 
1459 #define DO_TESTCASE_1B(desc, name, nr)				\
1460 	print_testname(desc"/"#nr);				\
1461 	dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);		\
1462 	pr_cont("\n");
1463 
1464 #define DO_TESTCASE_1RR(desc, name, nr)				\
1465 	print_testname(desc"/"#nr);				\
1466 	pr_cont("             |");				\
1467 	dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);		\
1468 	pr_cont("\n");
1469 
1470 #define DO_TESTCASE_1RRB(desc, name, nr)			\
1471 	print_testname(desc"/"#nr);				\
1472 	pr_cont("             |");				\
1473 	dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);		\
1474 	pr_cont("\n");
1475 
1476 
1477 #define DO_TESTCASE_3(desc, name, nr)				\
1478 	print_testname(desc"/"#nr);				\
1479 	dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN);	\
1480 	dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);	\
1481 	dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);	\
1482 	pr_cont("\n");
1483 
1484 #define DO_TESTCASE_3RW(desc, name, nr)				\
1485 	print_testname(desc"/"#nr);				\
1486 	dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1487 	dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);	\
1488 	dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);	\
1489 	pr_cont("\n");
1490 
1491 #define DO_TESTCASE_2RW(desc, name, nr)				\
1492 	print_testname(desc"/"#nr);				\
1493 	pr_cont("      |");					\
1494 	dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);	\
1495 	dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);	\
1496 	pr_cont("\n");
1497 
1498 #define DO_TESTCASE_2x2RW(desc, name, nr)			\
1499 	DO_TESTCASE_2RW("hard-"desc, name##_hard, nr)		\
1500 	DO_TESTCASE_2RW("soft-"desc, name##_soft, nr)		\
1501 
1502 #define DO_TESTCASE_6x2x2RW(desc, name)				\
1503 	DO_TESTCASE_2x2RW(desc, name, 123);			\
1504 	DO_TESTCASE_2x2RW(desc, name, 132);			\
1505 	DO_TESTCASE_2x2RW(desc, name, 213);			\
1506 	DO_TESTCASE_2x2RW(desc, name, 231);			\
1507 	DO_TESTCASE_2x2RW(desc, name, 312);			\
1508 	DO_TESTCASE_2x2RW(desc, name, 321);
1509 
1510 #define DO_TESTCASE_6(desc, name)				\
1511 	print_testname(desc);					\
1512 	dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);		\
1513 	dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);		\
1514 	dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK);		\
1515 	dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);		\
1516 	dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);		\
1517 	dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);		\
1518 	dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX);	\
1519 	pr_cont("\n");
1520 
1521 #define DO_TESTCASE_6_SUCCESS(desc, name)			\
1522 	print_testname(desc);					\
1523 	dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN);		\
1524 	dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK);		\
1525 	dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);		\
1526 	dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX);		\
1527 	dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM);		\
1528 	dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM);		\
1529 	dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX);	\
1530 	pr_cont("\n");
1531 
1532 /*
1533  * 'read' variant: rlocks must not trigger.
1534  */
1535 #define DO_TESTCASE_6R(desc, name)				\
1536 	print_testname(desc);					\
1537 	dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);		\
1538 	dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);		\
1539 	dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);		\
1540 	dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);		\
1541 	dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);		\
1542 	dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);		\
1543 	dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX);	\
1544 	pr_cont("\n");
1545 
1546 #define DO_TESTCASE_2I(desc, name, nr)				\
1547 	DO_TESTCASE_1("hard-"desc, name##_hard, nr);		\
1548 	DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1549 
1550 #define DO_TESTCASE_2IB(desc, name, nr)				\
1551 	DO_TESTCASE_1B("hard-"desc, name##_hard, nr);		\
1552 	DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1553 
1554 #define DO_TESTCASE_6I(desc, name, nr)				\
1555 	DO_TESTCASE_3("hard-"desc, name##_hard, nr);		\
1556 	DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1557 
1558 #define DO_TESTCASE_6IRW(desc, name, nr)			\
1559 	DO_TESTCASE_3RW("hard-"desc, name##_hard, nr);		\
1560 	DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1561 
1562 #define DO_TESTCASE_2x3(desc, name)				\
1563 	DO_TESTCASE_3(desc, name, 12);				\
1564 	DO_TESTCASE_3(desc, name, 21);
1565 
1566 #define DO_TESTCASE_2x6(desc, name)				\
1567 	DO_TESTCASE_6I(desc, name, 12);				\
1568 	DO_TESTCASE_6I(desc, name, 21);
1569 
1570 #define DO_TESTCASE_6x2(desc, name)				\
1571 	DO_TESTCASE_2I(desc, name, 123);			\
1572 	DO_TESTCASE_2I(desc, name, 132);			\
1573 	DO_TESTCASE_2I(desc, name, 213);			\
1574 	DO_TESTCASE_2I(desc, name, 231);			\
1575 	DO_TESTCASE_2I(desc, name, 312);			\
1576 	DO_TESTCASE_2I(desc, name, 321);
1577 
1578 #define DO_TESTCASE_6x2B(desc, name)				\
1579 	DO_TESTCASE_2IB(desc, name, 123);			\
1580 	DO_TESTCASE_2IB(desc, name, 132);			\
1581 	DO_TESTCASE_2IB(desc, name, 213);			\
1582 	DO_TESTCASE_2IB(desc, name, 231);			\
1583 	DO_TESTCASE_2IB(desc, name, 312);			\
1584 	DO_TESTCASE_2IB(desc, name, 321);
1585 
1586 #define DO_TESTCASE_6x1RR(desc, name)				\
1587 	DO_TESTCASE_1RR(desc, name, 123);			\
1588 	DO_TESTCASE_1RR(desc, name, 132);			\
1589 	DO_TESTCASE_1RR(desc, name, 213);			\
1590 	DO_TESTCASE_1RR(desc, name, 231);			\
1591 	DO_TESTCASE_1RR(desc, name, 312);			\
1592 	DO_TESTCASE_1RR(desc, name, 321);
1593 
1594 #define DO_TESTCASE_6x1RRB(desc, name)				\
1595 	DO_TESTCASE_1RRB(desc, name, 123);			\
1596 	DO_TESTCASE_1RRB(desc, name, 132);			\
1597 	DO_TESTCASE_1RRB(desc, name, 213);			\
1598 	DO_TESTCASE_1RRB(desc, name, 231);			\
1599 	DO_TESTCASE_1RRB(desc, name, 312);			\
1600 	DO_TESTCASE_1RRB(desc, name, 321);
1601 
1602 #define DO_TESTCASE_6x6(desc, name)				\
1603 	DO_TESTCASE_6I(desc, name, 123);			\
1604 	DO_TESTCASE_6I(desc, name, 132);			\
1605 	DO_TESTCASE_6I(desc, name, 213);			\
1606 	DO_TESTCASE_6I(desc, name, 231);			\
1607 	DO_TESTCASE_6I(desc, name, 312);			\
1608 	DO_TESTCASE_6I(desc, name, 321);
1609 
1610 #define DO_TESTCASE_6x6RW(desc, name)				\
1611 	DO_TESTCASE_6IRW(desc, name, 123);			\
1612 	DO_TESTCASE_6IRW(desc, name, 132);			\
1613 	DO_TESTCASE_6IRW(desc, name, 213);			\
1614 	DO_TESTCASE_6IRW(desc, name, 231);			\
1615 	DO_TESTCASE_6IRW(desc, name, 312);			\
1616 	DO_TESTCASE_6IRW(desc, name, 321);
1617 
1618 static void ww_test_fail_acquire(void)
1619 {
1620 	int ret;
1621 
1622 	WWAI(&t);
1623 	t.stamp++;
1624 
1625 	ret = WWL(&o, &t);
1626 
1627 	if (WARN_ON(!o.ctx) ||
1628 	    WARN_ON(ret))
1629 		return;
1630 
1631 	/* No lockdep test, pure API */
1632 	ret = WWL(&o, &t);
1633 	WARN_ON(ret != -EALREADY);
1634 
1635 	ret = WWT(&o);
1636 	WARN_ON(ret);
1637 
1638 	t2 = t;
1639 	t2.stamp++;
1640 	ret = WWL(&o, &t2);
1641 	WARN_ON(ret != -EDEADLK);
1642 	WWU(&o);
1643 
1644 	if (WWT(&o))
1645 		WWU(&o);
1646 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1647 	else
1648 		DEBUG_LOCKS_WARN_ON(1);
1649 #endif
1650 }
1651 
1652 static void ww_test_normal(void)
1653 {
1654 	int ret;
1655 
1656 	WWAI(&t);
1657 
1658 	/*
1659 	 * None of the ww_mutex codepaths should be taken in the 'normal'
1660 	 * mutex calls. The easiest way to verify this is by using the
1661 	 * normal mutex calls, and making sure o.ctx is unmodified.
1662 	 */
1663 
1664 	/* mutex_lock (and indirectly, mutex_lock_nested) */
1665 	o.ctx = (void *)~0UL;
1666 	mutex_lock(&o.base);
1667 	mutex_unlock(&o.base);
1668 	WARN_ON(o.ctx != (void *)~0UL);
1669 
1670 	/* mutex_lock_interruptible (and *_nested) */
1671 	o.ctx = (void *)~0UL;
1672 	ret = mutex_lock_interruptible(&o.base);
1673 	if (!ret)
1674 		mutex_unlock(&o.base);
1675 	else
1676 		WARN_ON(1);
1677 	WARN_ON(o.ctx != (void *)~0UL);
1678 
1679 	/* mutex_lock_killable (and *_nested) */
1680 	o.ctx = (void *)~0UL;
1681 	ret = mutex_lock_killable(&o.base);
1682 	if (!ret)
1683 		mutex_unlock(&o.base);
1684 	else
1685 		WARN_ON(1);
1686 	WARN_ON(o.ctx != (void *)~0UL);
1687 
1688 	/* trylock, succeeding */
1689 	o.ctx = (void *)~0UL;
1690 	ret = mutex_trylock(&o.base);
1691 	WARN_ON(!ret);
1692 	if (ret)
1693 		mutex_unlock(&o.base);
1694 	else
1695 		WARN_ON(1);
1696 	WARN_ON(o.ctx != (void *)~0UL);
1697 
1698 	/* trylock, failing */
1699 	o.ctx = (void *)~0UL;
1700 	mutex_lock(&o.base);
1701 	ret = mutex_trylock(&o.base);
1702 	WARN_ON(ret);
1703 	mutex_unlock(&o.base);
1704 	WARN_ON(o.ctx != (void *)~0UL);
1705 
1706 	/* nest_lock */
1707 	o.ctx = (void *)~0UL;
1708 	mutex_lock_nest_lock(&o.base, &t);
1709 	mutex_unlock(&o.base);
1710 	WARN_ON(o.ctx != (void *)~0UL);
1711 }
1712 
1713 static void ww_test_two_contexts(void)
1714 {
1715 	WWAI(&t);
1716 	WWAI(&t2);
1717 }
1718 
1719 static void ww_test_diff_class(void)
1720 {
1721 	WWAI(&t);
1722 #ifdef CONFIG_DEBUG_MUTEXES
1723 	t.ww_class = NULL;
1724 #endif
1725 	WWL(&o, &t);
1726 }
1727 
1728 static void ww_test_context_done_twice(void)
1729 {
1730 	WWAI(&t);
1731 	WWAD(&t);
1732 	WWAD(&t);
1733 	WWAF(&t);
1734 }
1735 
1736 static void ww_test_context_unlock_twice(void)
1737 {
1738 	WWAI(&t);
1739 	WWAD(&t);
1740 	WWAF(&t);
1741 	WWAF(&t);
1742 }
1743 
1744 static void ww_test_context_fini_early(void)
1745 {
1746 	WWAI(&t);
1747 	WWL(&o, &t);
1748 	WWAD(&t);
1749 	WWAF(&t);
1750 }
1751 
1752 static void ww_test_context_lock_after_done(void)
1753 {
1754 	WWAI(&t);
1755 	WWAD(&t);
1756 	WWL(&o, &t);
1757 }
1758 
1759 static void ww_test_object_unlock_twice(void)
1760 {
1761 	WWL1(&o);
1762 	WWU(&o);
1763 	WWU(&o);
1764 }
1765 
1766 static void ww_test_object_lock_unbalanced(void)
1767 {
1768 	WWAI(&t);
1769 	WWL(&o, &t);
1770 	t.acquired = 0;
1771 	WWU(&o);
1772 	WWAF(&t);
1773 }
1774 
1775 static void ww_test_object_lock_stale_context(void)
1776 {
1777 	WWAI(&t);
1778 	o.ctx = &t2;
1779 	WWL(&o, &t);
1780 }
1781 
1782 static void ww_test_edeadlk_normal(void)
1783 {
1784 	int ret;
1785 
1786 	mutex_lock(&o2.base);
1787 	o2.ctx = &t2;
1788 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1789 
1790 	WWAI(&t);
1791 	t2 = t;
1792 	t2.stamp--;
1793 
1794 	ret = WWL(&o, &t);
1795 	WARN_ON(ret);
1796 
1797 	ret = WWL(&o2, &t);
1798 	WARN_ON(ret != -EDEADLK);
1799 
1800 	o2.ctx = NULL;
1801 	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1802 	mutex_unlock(&o2.base);
1803 	WWU(&o);
1804 
1805 	WWL(&o2, &t);
1806 }
1807 
1808 static void ww_test_edeadlk_normal_slow(void)
1809 {
1810 	int ret;
1811 
1812 	mutex_lock(&o2.base);
1813 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1814 	o2.ctx = &t2;
1815 
1816 	WWAI(&t);
1817 	t2 = t;
1818 	t2.stamp--;
1819 
1820 	ret = WWL(&o, &t);
1821 	WARN_ON(ret);
1822 
1823 	ret = WWL(&o2, &t);
1824 	WARN_ON(ret != -EDEADLK);
1825 
1826 	o2.ctx = NULL;
1827 	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1828 	mutex_unlock(&o2.base);
1829 	WWU(&o);
1830 
1831 	ww_mutex_lock_slow(&o2, &t);
1832 }
1833 
1834 static void ww_test_edeadlk_no_unlock(void)
1835 {
1836 	int ret;
1837 
1838 	mutex_lock(&o2.base);
1839 	o2.ctx = &t2;
1840 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1841 
1842 	WWAI(&t);
1843 	t2 = t;
1844 	t2.stamp--;
1845 
1846 	ret = WWL(&o, &t);
1847 	WARN_ON(ret);
1848 
1849 	ret = WWL(&o2, &t);
1850 	WARN_ON(ret != -EDEADLK);
1851 
1852 	o2.ctx = NULL;
1853 	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1854 	mutex_unlock(&o2.base);
1855 
1856 	WWL(&o2, &t);
1857 }
1858 
1859 static void ww_test_edeadlk_no_unlock_slow(void)
1860 {
1861 	int ret;
1862 
1863 	mutex_lock(&o2.base);
1864 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1865 	o2.ctx = &t2;
1866 
1867 	WWAI(&t);
1868 	t2 = t;
1869 	t2.stamp--;
1870 
1871 	ret = WWL(&o, &t);
1872 	WARN_ON(ret);
1873 
1874 	ret = WWL(&o2, &t);
1875 	WARN_ON(ret != -EDEADLK);
1876 
1877 	o2.ctx = NULL;
1878 	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1879 	mutex_unlock(&o2.base);
1880 
1881 	ww_mutex_lock_slow(&o2, &t);
1882 }
1883 
1884 static void ww_test_edeadlk_acquire_more(void)
1885 {
1886 	int ret;
1887 
1888 	mutex_lock(&o2.base);
1889 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1890 	o2.ctx = &t2;
1891 
1892 	WWAI(&t);
1893 	t2 = t;
1894 	t2.stamp--;
1895 
1896 	ret = WWL(&o, &t);
1897 	WARN_ON(ret);
1898 
1899 	ret = WWL(&o2, &t);
1900 	WARN_ON(ret != -EDEADLK);
1901 
1902 	ret = WWL(&o3, &t);
1903 }
1904 
1905 static void ww_test_edeadlk_acquire_more_slow(void)
1906 {
1907 	int ret;
1908 
1909 	mutex_lock(&o2.base);
1910 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1911 	o2.ctx = &t2;
1912 
1913 	WWAI(&t);
1914 	t2 = t;
1915 	t2.stamp--;
1916 
1917 	ret = WWL(&o, &t);
1918 	WARN_ON(ret);
1919 
1920 	ret = WWL(&o2, &t);
1921 	WARN_ON(ret != -EDEADLK);
1922 
1923 	ww_mutex_lock_slow(&o3, &t);
1924 }
1925 
1926 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1927 {
1928 	int ret;
1929 
1930 	mutex_lock(&o2.base);
1931 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1932 	o2.ctx = &t2;
1933 
1934 	mutex_lock(&o3.base);
1935 	mutex_release(&o3.base.dep_map, _THIS_IP_);
1936 	o3.ctx = &t2;
1937 
1938 	WWAI(&t);
1939 	t2 = t;
1940 	t2.stamp--;
1941 
1942 	ret = WWL(&o, &t);
1943 	WARN_ON(ret);
1944 
1945 	ret = WWL(&o2, &t);
1946 	WARN_ON(ret != -EDEADLK);
1947 
1948 	ret = WWL(&o3, &t);
1949 	WARN_ON(ret != -EDEADLK);
1950 }
1951 
1952 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1953 {
1954 	int ret;
1955 
1956 	mutex_lock(&o2.base);
1957 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1958 	o2.ctx = &t2;
1959 
1960 	mutex_lock(&o3.base);
1961 	mutex_release(&o3.base.dep_map, _THIS_IP_);
1962 	o3.ctx = &t2;
1963 
1964 	WWAI(&t);
1965 	t2 = t;
1966 	t2.stamp--;
1967 
1968 	ret = WWL(&o, &t);
1969 	WARN_ON(ret);
1970 
1971 	ret = WWL(&o2, &t);
1972 	WARN_ON(ret != -EDEADLK);
1973 
1974 	ww_mutex_lock_slow(&o3, &t);
1975 }
1976 
1977 static void ww_test_edeadlk_acquire_wrong(void)
1978 {
1979 	int ret;
1980 
1981 	mutex_lock(&o2.base);
1982 	mutex_release(&o2.base.dep_map, _THIS_IP_);
1983 	o2.ctx = &t2;
1984 
1985 	WWAI(&t);
1986 	t2 = t;
1987 	t2.stamp--;
1988 
1989 	ret = WWL(&o, &t);
1990 	WARN_ON(ret);
1991 
1992 	ret = WWL(&o2, &t);
1993 	WARN_ON(ret != -EDEADLK);
1994 	if (!ret)
1995 		WWU(&o2);
1996 
1997 	WWU(&o);
1998 
1999 	ret = WWL(&o3, &t);
2000 }
2001 
2002 static void ww_test_edeadlk_acquire_wrong_slow(void)
2003 {
2004 	int ret;
2005 
2006 	mutex_lock(&o2.base);
2007 	mutex_release(&o2.base.dep_map, _THIS_IP_);
2008 	o2.ctx = &t2;
2009 
2010 	WWAI(&t);
2011 	t2 = t;
2012 	t2.stamp--;
2013 
2014 	ret = WWL(&o, &t);
2015 	WARN_ON(ret);
2016 
2017 	ret = WWL(&o2, &t);
2018 	WARN_ON(ret != -EDEADLK);
2019 	if (!ret)
2020 		WWU(&o2);
2021 
2022 	WWU(&o);
2023 
2024 	ww_mutex_lock_slow(&o3, &t);
2025 }
2026 
2027 static void ww_test_spin_nest_unlocked(void)
2028 {
2029 	spin_lock_nest_lock(&lock_A, &o.base);
2030 	U(A);
2031 }
2032 
2033 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2034 static void ww_test_spin_nest_lock(void)
2035 {
2036 	spin_lock(&lock_X1);
2037 	spin_lock_nest_lock(&lock_Y1, &lock_X1);
2038 	spin_lock(&lock_A);
2039 	spin_lock_nest_lock(&lock_Y2, &lock_X1);
2040 	spin_unlock(&lock_A);
2041 	spin_unlock(&lock_Y2);
2042 	spin_unlock(&lock_Y1);
2043 	spin_unlock(&lock_X1);
2044 }
2045 
2046 static void ww_test_unneeded_slow(void)
2047 {
2048 	WWAI(&t);
2049 
2050 	ww_mutex_lock_slow(&o, &t);
2051 }
2052 
2053 static void ww_test_context_block(void)
2054 {
2055 	int ret;
2056 
2057 	WWAI(&t);
2058 
2059 	ret = WWL(&o, &t);
2060 	WARN_ON(ret);
2061 	WWL1(&o2);
2062 }
2063 
2064 static void ww_test_context_try(void)
2065 {
2066 	int ret;
2067 
2068 	WWAI(&t);
2069 
2070 	ret = WWL(&o, &t);
2071 	WARN_ON(ret);
2072 
2073 	ret = WWT(&o2);
2074 	WARN_ON(!ret);
2075 	WWU(&o2);
2076 	WWU(&o);
2077 }
2078 
2079 static void ww_test_context_context(void)
2080 {
2081 	int ret;
2082 
2083 	WWAI(&t);
2084 
2085 	ret = WWL(&o, &t);
2086 	WARN_ON(ret);
2087 
2088 	ret = WWL(&o2, &t);
2089 	WARN_ON(ret);
2090 
2091 	WWU(&o2);
2092 	WWU(&o);
2093 }
2094 
2095 static void ww_test_try_block(void)
2096 {
2097 	bool ret;
2098 
2099 	ret = WWT(&o);
2100 	WARN_ON(!ret);
2101 
2102 	WWL1(&o2);
2103 	WWU(&o2);
2104 	WWU(&o);
2105 }
2106 
2107 static void ww_test_try_try(void)
2108 {
2109 	bool ret;
2110 
2111 	ret = WWT(&o);
2112 	WARN_ON(!ret);
2113 	ret = WWT(&o2);
2114 	WARN_ON(!ret);
2115 	WWU(&o2);
2116 	WWU(&o);
2117 }
2118 
2119 static void ww_test_try_context(void)
2120 {
2121 	int ret;
2122 
2123 	ret = WWT(&o);
2124 	WARN_ON(!ret);
2125 
2126 	WWAI(&t);
2127 
2128 	ret = WWL(&o2, &t);
2129 	WARN_ON(ret);
2130 }
2131 
2132 static void ww_test_block_block(void)
2133 {
2134 	WWL1(&o);
2135 	WWL1(&o2);
2136 }
2137 
2138 static void ww_test_block_try(void)
2139 {
2140 	bool ret;
2141 
2142 	WWL1(&o);
2143 	ret = WWT(&o2);
2144 	WARN_ON(!ret);
2145 }
2146 
2147 static void ww_test_block_context(void)
2148 {
2149 	int ret;
2150 
2151 	WWL1(&o);
2152 	WWAI(&t);
2153 
2154 	ret = WWL(&o2, &t);
2155 	WARN_ON(ret);
2156 }
2157 
2158 static void ww_test_spin_block(void)
2159 {
2160 	L(A);
2161 	U(A);
2162 
2163 	WWL1(&o);
2164 	L(A);
2165 	U(A);
2166 	WWU(&o);
2167 
2168 	L(A);
2169 	WWL1(&o);
2170 	WWU(&o);
2171 	U(A);
2172 }
2173 
2174 static void ww_test_spin_try(void)
2175 {
2176 	bool ret;
2177 
2178 	L(A);
2179 	U(A);
2180 
2181 	ret = WWT(&o);
2182 	WARN_ON(!ret);
2183 	L(A);
2184 	U(A);
2185 	WWU(&o);
2186 
2187 	L(A);
2188 	ret = WWT(&o);
2189 	WARN_ON(!ret);
2190 	WWU(&o);
2191 	U(A);
2192 }
2193 
2194 static void ww_test_spin_context(void)
2195 {
2196 	int ret;
2197 
2198 	L(A);
2199 	U(A);
2200 
2201 	WWAI(&t);
2202 
2203 	ret = WWL(&o, &t);
2204 	WARN_ON(ret);
2205 	L(A);
2206 	U(A);
2207 	WWU(&o);
2208 
2209 	L(A);
2210 	ret = WWL(&o, &t);
2211 	WARN_ON(ret);
2212 	WWU(&o);
2213 	U(A);
2214 }
2215 
2216 static void ww_tests(void)
2217 {
2218 	printk("  --------------------------------------------------------------------------\n");
2219 	printk("  | Wound/wait tests |\n");
2220 	printk("  ---------------------\n");
2221 
2222 	print_testname("ww api failures");
2223 	dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
2224 	dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
2225 	dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
2226 	pr_cont("\n");
2227 
2228 	print_testname("ww contexts mixing");
2229 	dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
2230 	dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
2231 	pr_cont("\n");
2232 
2233 	print_testname("finishing ww context");
2234 	dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
2235 	dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
2236 	dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
2237 	dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
2238 	pr_cont("\n");
2239 
2240 	print_testname("locking mismatches");
2241 	dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
2242 	dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
2243 	dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
2244 	pr_cont("\n");
2245 
2246 	print_testname("EDEADLK handling");
2247 	dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
2248 	dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
2249 	dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
2250 	dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
2251 	dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
2252 	dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
2253 	dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
2254 	dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
2255 	dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
2256 	dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
2257 	pr_cont("\n");
2258 
2259 	print_testname("spinlock nest unlocked");
2260 	dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
2261 	pr_cont("\n");
2262 
2263 	print_testname("spinlock nest test");
2264 	dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW);
2265 	pr_cont("\n");
2266 
2267 	printk("  -----------------------------------------------------\n");
2268 	printk("                                 |block | try  |context|\n");
2269 	printk("  -----------------------------------------------------\n");
2270 
2271 	print_testname("context");
2272 	dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
2273 	dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
2274 	dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
2275 	pr_cont("\n");
2276 
2277 	print_testname("try");
2278 	dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
2279 	dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
2280 	dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
2281 	pr_cont("\n");
2282 
2283 	print_testname("block");
2284 	dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
2285 	dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
2286 	dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
2287 	pr_cont("\n");
2288 
2289 	print_testname("spinlock");
2290 	dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
2291 	dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
2292 	dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
2293 	pr_cont("\n");
2294 }
2295 
2296 
2297 /*
2298  * <in hardirq handler>
2299  * read_lock(&A);
2300  *			<hardirq disable>
2301  *			spin_lock(&B);
2302  * spin_lock(&B);
2303  *			read_lock(&A);
2304  *
2305  * is a deadlock.
2306  */
2307 static void queued_read_lock_hardirq_RE_Er(void)
2308 {
2309 	HARDIRQ_ENTER();
2310 	read_lock(&rwlock_A);
2311 	LOCK(B);
2312 	UNLOCK(B);
2313 	read_unlock(&rwlock_A);
2314 	HARDIRQ_EXIT();
2315 
2316 	HARDIRQ_DISABLE();
2317 	LOCK(B);
2318 	read_lock(&rwlock_A);
2319 	read_unlock(&rwlock_A);
2320 	UNLOCK(B);
2321 	HARDIRQ_ENABLE();
2322 }
2323 
2324 /*
2325  * <in hardirq handler>
2326  * spin_lock(&B);
2327  *			<hardirq disable>
2328  *			read_lock(&A);
2329  * read_lock(&A);
2330  *			spin_lock(&B);
2331  *
2332  * is not a deadlock.
2333  */
2334 static void queued_read_lock_hardirq_ER_rE(void)
2335 {
2336 	HARDIRQ_ENTER();
2337 	LOCK(B);
2338 	read_lock(&rwlock_A);
2339 	read_unlock(&rwlock_A);
2340 	UNLOCK(B);
2341 	HARDIRQ_EXIT();
2342 
2343 	HARDIRQ_DISABLE();
2344 	read_lock(&rwlock_A);
2345 	LOCK(B);
2346 	UNLOCK(B);
2347 	read_unlock(&rwlock_A);
2348 	HARDIRQ_ENABLE();
2349 }
2350 
2351 /*
2352  * <hardirq disable>
2353  * spin_lock(&B);
2354  *			read_lock(&A);
2355  *			<in hardirq handler>
2356  *			spin_lock(&B);
2357  * read_lock(&A);
2358  *
2359  * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2360  */
2361 static void queued_read_lock_hardirq_inversion(void)
2362 {
2363 
2364 	HARDIRQ_ENTER();
2365 	LOCK(B);
2366 	UNLOCK(B);
2367 	HARDIRQ_EXIT();
2368 
2369 	HARDIRQ_DISABLE();
2370 	LOCK(B);
2371 	read_lock(&rwlock_A);
2372 	read_unlock(&rwlock_A);
2373 	UNLOCK(B);
2374 	HARDIRQ_ENABLE();
2375 
2376 	read_lock(&rwlock_A);
2377 	read_unlock(&rwlock_A);
2378 }
2379 
2380 static void queued_read_lock_tests(void)
2381 {
2382 	printk("  --------------------------------------------------------------------------\n");
2383 	printk("  | queued read lock tests |\n");
2384 	printk("  ---------------------------\n");
2385 	print_testname("hardirq read-lock/lock-read");
2386 	dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
2387 	pr_cont("\n");
2388 
2389 	print_testname("hardirq lock-read/read-lock");
2390 	dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
2391 	pr_cont("\n");
2392 
2393 	print_testname("hardirq inversion");
2394 	dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
2395 	pr_cont("\n");
2396 }
2397 
2398 static void fs_reclaim_correct_nesting(void)
2399 {
2400 	fs_reclaim_acquire(GFP_KERNEL);
2401 	might_alloc(GFP_NOFS);
2402 	fs_reclaim_release(GFP_KERNEL);
2403 }
2404 
2405 static void fs_reclaim_wrong_nesting(void)
2406 {
2407 	fs_reclaim_acquire(GFP_KERNEL);
2408 	might_alloc(GFP_KERNEL);
2409 	fs_reclaim_release(GFP_KERNEL);
2410 }
2411 
2412 static void fs_reclaim_protected_nesting(void)
2413 {
2414 	unsigned int flags;
2415 
2416 	fs_reclaim_acquire(GFP_KERNEL);
2417 	flags = memalloc_nofs_save();
2418 	might_alloc(GFP_KERNEL);
2419 	memalloc_nofs_restore(flags);
2420 	fs_reclaim_release(GFP_KERNEL);
2421 }
2422 
2423 static void fs_reclaim_tests(void)
2424 {
2425 	printk("  --------------------\n");
2426 	printk("  | fs_reclaim tests |\n");
2427 	printk("  --------------------\n");
2428 
2429 	print_testname("correct nesting");
2430 	dotest(fs_reclaim_correct_nesting, SUCCESS, 0);
2431 	pr_cont("\n");
2432 
2433 	print_testname("wrong nesting");
2434 	dotest(fs_reclaim_wrong_nesting, FAILURE, 0);
2435 	pr_cont("\n");
2436 
2437 	print_testname("protected nesting");
2438 	dotest(fs_reclaim_protected_nesting, SUCCESS, 0);
2439 	pr_cont("\n");
2440 }
2441 
2442 #define __guard(cleanup) __maybe_unused __attribute__((__cleanup__(cleanup)))
2443 
2444 static void hardirq_exit(int *_)
2445 {
2446 	HARDIRQ_EXIT();
2447 }
2448 
2449 #define HARDIRQ_CONTEXT(name, ...)					\
2450 	int hardirq_guard_##name __guard(hardirq_exit);			\
2451 	HARDIRQ_ENTER();
2452 
2453 #define NOTTHREADED_HARDIRQ_CONTEXT(name, ...)				\
2454 	int notthreaded_hardirq_guard_##name __guard(hardirq_exit);	\
2455 	local_irq_disable();						\
2456 	__irq_enter();							\
2457 	WARN_ON(!in_irq());
2458 
2459 static void softirq_exit(int *_)
2460 {
2461 	SOFTIRQ_EXIT();
2462 }
2463 
2464 #define SOFTIRQ_CONTEXT(name, ...)				\
2465 	int softirq_guard_##name __guard(softirq_exit);		\
2466 	SOFTIRQ_ENTER();
2467 
2468 static void rcu_exit(int *_)
2469 {
2470 	rcu_read_unlock();
2471 }
2472 
2473 #define RCU_CONTEXT(name, ...)					\
2474 	int rcu_guard_##name __guard(rcu_exit);			\
2475 	rcu_read_lock();
2476 
2477 static void rcu_bh_exit(int *_)
2478 {
2479 	rcu_read_unlock_bh();
2480 }
2481 
2482 #define RCU_BH_CONTEXT(name, ...)				\
2483 	int rcu_bh_guard_##name __guard(rcu_bh_exit);		\
2484 	rcu_read_lock_bh();
2485 
2486 static void rcu_sched_exit(int *_)
2487 {
2488 	rcu_read_unlock_sched();
2489 }
2490 
2491 #define RCU_SCHED_CONTEXT(name, ...)				\
2492 	int rcu_sched_guard_##name __guard(rcu_sched_exit);	\
2493 	rcu_read_lock_sched();
2494 
2495 static void rcu_callback_exit(int *_)
2496 {
2497 	rcu_lock_release(&rcu_callback_map);
2498 }
2499 
2500 #define RCU_CALLBACK_CONTEXT(name, ...)					\
2501 	int rcu_callback_guard_##name __guard(rcu_callback_exit);	\
2502 	rcu_lock_acquire(&rcu_callback_map);
2503 
2504 
2505 static void raw_spinlock_exit(raw_spinlock_t **lock)
2506 {
2507 	raw_spin_unlock(*lock);
2508 }
2509 
2510 #define RAW_SPINLOCK_CONTEXT(name, lock)						\
2511 	raw_spinlock_t *raw_spinlock_guard_##name __guard(raw_spinlock_exit) = &(lock);	\
2512 	raw_spin_lock(&(lock));
2513 
2514 static void spinlock_exit(spinlock_t **lock)
2515 {
2516 	spin_unlock(*lock);
2517 }
2518 
2519 #define SPINLOCK_CONTEXT(name, lock)						\
2520 	spinlock_t *spinlock_guard_##name __guard(spinlock_exit) = &(lock);	\
2521 	spin_lock(&(lock));
2522 
2523 static void mutex_exit(struct mutex **lock)
2524 {
2525 	mutex_unlock(*lock);
2526 }
2527 
2528 #define MUTEX_CONTEXT(name, lock)					\
2529 	struct mutex *mutex_guard_##name __guard(mutex_exit) = &(lock);	\
2530 	mutex_lock(&(lock));
2531 
2532 #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock)	\
2533 										\
2534 static void __maybe_unused inner##_in_##outer(void)				\
2535 {										\
2536 	outer##_CONTEXT(_, outer_lock);						\
2537 	{									\
2538 		inner##_CONTEXT(_, inner_lock);					\
2539 	}									\
2540 }
2541 
2542 /*
2543  * wait contexts (considering PREEMPT_RT)
2544  *
2545  * o: inner is allowed in outer
2546  * x: inner is disallowed in outer
2547  *
2548  *       \  inner |  RCU  | RAW_SPIN | SPIN | MUTEX
2549  * outer  \       |       |          |      |
2550  * ---------------+-------+----------+------+-------
2551  * HARDIRQ        |   o   |    o     |  o   |  x
2552  * ---------------+-------+----------+------+-------
2553  * NOTTHREADED_IRQ|   o   |    o     |  x   |  x
2554  * ---------------+-------+----------+------+-------
2555  * SOFTIRQ        |   o   |    o     |  o   |  x
2556  * ---------------+-------+----------+------+-------
2557  * RCU            |   o   |    o     |  o   |  x
2558  * ---------------+-------+----------+------+-------
2559  * RCU_BH         |   o   |    o     |  o   |  x
2560  * ---------------+-------+----------+------+-------
2561  * RCU_CALLBACK   |   o   |    o     |  o   |  x
2562  * ---------------+-------+----------+------+-------
2563  * RCU_SCHED      |   o   |    o     |  x   |  x
2564  * ---------------+-------+----------+------+-------
2565  * RAW_SPIN       |   o   |    o     |  x   |  x
2566  * ---------------+-------+----------+------+-------
2567  * SPIN           |   o   |    o     |  o   |  x
2568  * ---------------+-------+----------+------+-------
2569  * MUTEX          |   o   |    o     |  o   |  o
2570  * ---------------+-------+----------+------+-------
2571  */
2572 
2573 #define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock)		\
2574 GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock)			\
2575 GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock)		\
2576 GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock)			\
2577 GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock)				\
2578 GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock)			\
2579 GENERATE_2_CONTEXT_TESTCASE(RCU_CALLBACK, , inner, inner_lock)			\
2580 GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock)			\
2581 GENERATE_2_CONTEXT_TESTCASE(RAW_SPINLOCK, raw_lock_A, inner, inner_lock)	\
2582 GENERATE_2_CONTEXT_TESTCASE(SPINLOCK, lock_A, inner, inner_lock)		\
2583 GENERATE_2_CONTEXT_TESTCASE(MUTEX, mutex_A, inner, inner_lock)
2584 
2585 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU, )
2586 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RAW_SPINLOCK, raw_lock_B)
2587 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(SPINLOCK, lock_B)
2588 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(MUTEX, mutex_B)
2589 
2590 /* the outer context allows all kinds of preemption */
2591 #define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer)			\
2592 	dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK);		\
2593 	dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);	\
2594 	dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);		\
2595 	dotest(MUTEX_in_##outer, SUCCESS, LOCKTYPE_MUTEX);		\
2596 
2597 /*
2598  * the outer context only allows the preemption introduced by spinlock_t (which
2599  * is a sleepable lock for PREEMPT_RT)
2600  */
2601 #define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer)		\
2602 	dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK);		\
2603 	dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);	\
2604 	dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);		\
2605 	dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX);		\
2606 
2607 /* the outer doesn't allows any kind of preemption */
2608 #define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer)			\
2609 	dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK);		\
2610 	dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);	\
2611 	dotest(SPINLOCK_in_##outer, FAILURE, LOCKTYPE_SPIN);		\
2612 	dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX);		\
2613 
2614 static void wait_context_tests(void)
2615 {
2616 	printk("  --------------------------------------------------------------------------\n");
2617 	printk("  | wait context tests |\n");
2618 	printk("  --------------------------------------------------------------------------\n");
2619 	printk("                                 | rcu  | raw  | spin |mutex |\n");
2620 	printk("  --------------------------------------------------------------------------\n");
2621 	print_testname("in hardirq context");
2622 	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ);
2623 	pr_cont("\n");
2624 
2625 	print_testname("in hardirq context (not threaded)");
2626 	DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ);
2627 	pr_cont("\n");
2628 
2629 	print_testname("in softirq context");
2630 	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ);
2631 	pr_cont("\n");
2632 
2633 	print_testname("in RCU context");
2634 	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU);
2635 	pr_cont("\n");
2636 
2637 	print_testname("in RCU-bh context");
2638 	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH);
2639 	pr_cont("\n");
2640 
2641 	print_testname("in RCU callback context");
2642 	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_CALLBACK);
2643 	pr_cont("\n");
2644 
2645 	print_testname("in RCU-sched context");
2646 	DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED);
2647 	pr_cont("\n");
2648 
2649 	print_testname("in RAW_SPINLOCK context");
2650 	DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RAW_SPINLOCK);
2651 	pr_cont("\n");
2652 
2653 	print_testname("in SPINLOCK context");
2654 	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SPINLOCK);
2655 	pr_cont("\n");
2656 
2657 	print_testname("in MUTEX context");
2658 	DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(MUTEX);
2659 	pr_cont("\n");
2660 }
2661 
2662 static void local_lock_2(void)
2663 {
2664 	local_lock_acquire(&local_A);	/* IRQ-ON */
2665 	local_lock_release(&local_A);
2666 
2667 	HARDIRQ_ENTER();
2668 	spin_lock(&lock_A);		/* IN-IRQ */
2669 	spin_unlock(&lock_A);
2670 	HARDIRQ_EXIT()
2671 
2672 	HARDIRQ_DISABLE();
2673 	spin_lock(&lock_A);
2674 	local_lock_acquire(&local_A);	/* IN-IRQ <-> IRQ-ON cycle, false */
2675 	local_lock_release(&local_A);
2676 	spin_unlock(&lock_A);
2677 	HARDIRQ_ENABLE();
2678 }
2679 
2680 static void local_lock_3A(void)
2681 {
2682 	local_lock_acquire(&local_A);	/* IRQ-ON */
2683 	spin_lock(&lock_B);		/* IRQ-ON */
2684 	spin_unlock(&lock_B);
2685 	local_lock_release(&local_A);
2686 
2687 	HARDIRQ_ENTER();
2688 	spin_lock(&lock_A);		/* IN-IRQ */
2689 	spin_unlock(&lock_A);
2690 	HARDIRQ_EXIT()
2691 
2692 	HARDIRQ_DISABLE();
2693 	spin_lock(&lock_A);
2694 	local_lock_acquire(&local_A);	/* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2695 	local_lock_release(&local_A);
2696 	spin_unlock(&lock_A);
2697 	HARDIRQ_ENABLE();
2698 }
2699 
2700 static void local_lock_3B(void)
2701 {
2702 	local_lock_acquire(&local_A);	/* IRQ-ON */
2703 	spin_lock(&lock_B);		/* IRQ-ON */
2704 	spin_unlock(&lock_B);
2705 	local_lock_release(&local_A);
2706 
2707 	HARDIRQ_ENTER();
2708 	spin_lock(&lock_A);		/* IN-IRQ */
2709 	spin_unlock(&lock_A);
2710 	HARDIRQ_EXIT()
2711 
2712 	HARDIRQ_DISABLE();
2713 	spin_lock(&lock_A);
2714 	local_lock_acquire(&local_A);	/* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2715 	local_lock_release(&local_A);
2716 	spin_unlock(&lock_A);
2717 	HARDIRQ_ENABLE();
2718 
2719 	HARDIRQ_DISABLE();
2720 	spin_lock(&lock_A);
2721 	spin_lock(&lock_B);		/* IN-IRQ <-> IRQ-ON cycle, true */
2722 	spin_unlock(&lock_B);
2723 	spin_unlock(&lock_A);
2724 	HARDIRQ_DISABLE();
2725 
2726 }
2727 
2728 static void local_lock_tests(void)
2729 {
2730 	printk("  --------------------------------------------------------------------------\n");
2731 	printk("  | local_lock tests |\n");
2732 	printk("  ---------------------\n");
2733 
2734 	print_testname("local_lock inversion  2");
2735 	dotest(local_lock_2, SUCCESS, LOCKTYPE_LL);
2736 	pr_cont("\n");
2737 
2738 	print_testname("local_lock inversion 3A");
2739 	dotest(local_lock_3A, SUCCESS, LOCKTYPE_LL);
2740 	pr_cont("\n");
2741 
2742 	print_testname("local_lock inversion 3B");
2743 	dotest(local_lock_3B, FAILURE, LOCKTYPE_LL);
2744 	pr_cont("\n");
2745 }
2746 
2747 void locking_selftest(void)
2748 {
2749 	/*
2750 	 * Got a locking failure before the selftest ran?
2751 	 */
2752 	if (!debug_locks) {
2753 		printk("----------------------------------\n");
2754 		printk("| Locking API testsuite disabled |\n");
2755 		printk("----------------------------------\n");
2756 		return;
2757 	}
2758 
2759 	/*
2760 	 * treats read_lock() as recursive read locks for testing purpose
2761 	 */
2762 	force_read_lock_recursive = 1;
2763 
2764 	/*
2765 	 * Run the testsuite:
2766 	 */
2767 	printk("------------------------\n");
2768 	printk("| Locking API testsuite:\n");
2769 	printk("----------------------------------------------------------------------------\n");
2770 	printk("                                 | spin |wlock |rlock |mutex | wsem | rsem |\n");
2771 	printk("  --------------------------------------------------------------------------\n");
2772 
2773 	init_shared_classes();
2774 	lockdep_set_selftest_task(current);
2775 
2776 	DO_TESTCASE_6R("A-A deadlock", AA);
2777 	DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
2778 	DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
2779 	DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
2780 	DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
2781 	DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
2782 	DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
2783 	DO_TESTCASE_6("double unlock", double_unlock);
2784 	DO_TESTCASE_6("initialize held", init_held);
2785 
2786 	printk("  --------------------------------------------------------------------------\n");
2787 	print_testname("recursive read-lock");
2788 	pr_cont("             |");
2789 	dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
2790 	pr_cont("             |");
2791 	dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
2792 	pr_cont("\n");
2793 
2794 	print_testname("recursive read-lock #2");
2795 	pr_cont("             |");
2796 	dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
2797 	pr_cont("             |");
2798 	dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
2799 	pr_cont("\n");
2800 
2801 	print_testname("mixed read-write-lock");
2802 	pr_cont("             |");
2803 	dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
2804 	pr_cont("             |");
2805 	dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
2806 	pr_cont("\n");
2807 
2808 	print_testname("mixed write-read-lock");
2809 	pr_cont("             |");
2810 	dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
2811 	pr_cont("             |");
2812 	dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
2813 	pr_cont("\n");
2814 
2815 	print_testname("mixed read-lock/lock-write ABBA");
2816 	pr_cont("             |");
2817 	dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2818 	pr_cont("             |");
2819 	dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2820 
2821 	print_testname("mixed read-lock/lock-read ABBA");
2822 	pr_cont("             |");
2823 	dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2824 	pr_cont("             |");
2825 	dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2826 
2827 	print_testname("mixed write-lock/lock-write ABBA");
2828 	pr_cont("             |");
2829 	dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2830 	pr_cont("             |");
2831 	dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2832 
2833 	print_testname("chain cached mixed R-L/L-W ABBA");
2834 	pr_cont("             |");
2835 	dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2836 
2837 	DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
2838 	DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
2839 	DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
2840 	DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
2841 
2842 	printk("  --------------------------------------------------------------------------\n");
2843 
2844 	/*
2845 	 * irq-context testcases:
2846 	 */
2847 	DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
2848 	DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
2849 	DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
2850 	DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
2851 	DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
2852 	DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
2853 
2854 	DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
2855 	DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
2856 	DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
2857 
2858 	ww_tests();
2859 
2860 	force_read_lock_recursive = 0;
2861 	/*
2862 	 * queued_read_lock() specific test cases can be put here
2863 	 */
2864 	if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
2865 		queued_read_lock_tests();
2866 
2867 	fs_reclaim_tests();
2868 
2869 	/* Wait context test cases that are specific for RAW_LOCK_NESTING */
2870 	if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING))
2871 		wait_context_tests();
2872 
2873 	local_lock_tests();
2874 
2875 	if (unexpected_testcase_failures) {
2876 		printk("-----------------------------------------------------------------\n");
2877 		debug_locks = 0;
2878 		printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2879 			unexpected_testcase_failures, testcase_total);
2880 		printk("-----------------------------------------------------------------\n");
2881 	} else if (expected_testcase_failures && testcase_successes) {
2882 		printk("--------------------------------------------------------\n");
2883 		printk("%3d out of %3d testcases failed, as expected. |\n",
2884 			expected_testcase_failures, testcase_total);
2885 		printk("----------------------------------------------------\n");
2886 		debug_locks = 1;
2887 	} else if (expected_testcase_failures && !testcase_successes) {
2888 		printk("--------------------------------------------------------\n");
2889 		printk("All %3d testcases failed, as expected. |\n",
2890 			expected_testcase_failures);
2891 		printk("----------------------------------------\n");
2892 		debug_locks = 1;
2893 	} else {
2894 		printk("-------------------------------------------------------\n");
2895 		printk("Good, all %3d testcases passed! |\n",
2896 			testcase_successes);
2897 		printk("---------------------------------\n");
2898 		debug_locks = 1;
2899 	}
2900 	lockdep_set_selftest_task(NULL);
2901 	debug_locks_silent = 0;
2902 }
2903