1 // SPDX-License-Identifier: GPL-2.0
3 * lib/locking-selftest.c
5 * Testsuite for various locking APIs: spinlocks, rwlocks,
6 * mutexes and rw-semaphores.
8 * It is checking both false positives and false negatives.
10 * Started by Ingo Molnar:
12 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
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>
30 * Change this to 1 if you want to see the failure printouts:
32 static unsigned int debug_locks_verbose;
33 unsigned int force_read_lock_recursive;
35 static DEFINE_WD_CLASS(ww_lockdep);
37 static int __init setup_debug_locks_verbose(char *str)
39 get_option(&str, &debug_locks_verbose);
44 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
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
57 static struct ww_acquire_ctx t, t2;
58 static struct ww_mutex o, o2, o3;
61 * Normal standalone locks, for the circular and irq-context
64 static DEFINE_SPINLOCK(lock_A);
65 static DEFINE_SPINLOCK(lock_B);
66 static DEFINE_SPINLOCK(lock_C);
67 static DEFINE_SPINLOCK(lock_D);
69 static DEFINE_RAW_SPINLOCK(raw_lock_A);
70 static DEFINE_RAW_SPINLOCK(raw_lock_B);
72 static DEFINE_RWLOCK(rwlock_A);
73 static DEFINE_RWLOCK(rwlock_B);
74 static DEFINE_RWLOCK(rwlock_C);
75 static DEFINE_RWLOCK(rwlock_D);
77 static DEFINE_MUTEX(mutex_A);
78 static DEFINE_MUTEX(mutex_B);
79 static DEFINE_MUTEX(mutex_C);
80 static DEFINE_MUTEX(mutex_D);
82 static DECLARE_RWSEM(rwsem_A);
83 static DECLARE_RWSEM(rwsem_B);
84 static DECLARE_RWSEM(rwsem_C);
85 static DECLARE_RWSEM(rwsem_D);
87 #ifdef CONFIG_RT_MUTEXES
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);
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:
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);
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);
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);
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);
130 #ifdef CONFIG_RT_MUTEXES
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);
141 static local_lock_t local_A = INIT_LOCAL_LOCK(local_A);
144 * non-inlined runtime initializers, to let separate locks share
145 * the same lock-class:
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)\
152 spin_lock_init(lock); \
153 rwlock_init(rwlock); \
162 static void init_shared_classes(void)
164 #ifdef CONFIG_RT_MUTEXES
165 static struct lock_class_key rt_X, rt_Y, rt_Z;
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);
175 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
176 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
178 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
179 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
181 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
182 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
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:
191 #define HARDIRQ_DISABLE local_irq_disable
192 #define HARDIRQ_ENABLE local_irq_enable
194 #define HARDIRQ_ENTER() \
195 local_irq_disable(); \
199 #define HARDIRQ_EXIT() \
203 #define SOFTIRQ_DISABLE local_bh_disable
204 #define SOFTIRQ_ENABLE local_bh_enable
206 #define SOFTIRQ_ENTER() \
207 local_bh_disable(); \
208 local_irq_disable(); \
209 lockdep_softirq_enter(); \
210 WARN_ON(!in_softirq());
212 #define SOFTIRQ_EXIT() \
213 lockdep_softirq_exit(); \
214 local_irq_enable(); \
218 * Shortcuts for lock/unlock API variants, to keep
219 * the testcases compact:
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)
226 #define WL(x) write_lock(&rwlock_##x)
227 #define WU(x) write_unlock(&rwlock_##x)
228 #define WLU(x) WL(x); WU(x)
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)
235 #define ML(x) mutex_lock(&mutex_##x)
236 #define MU(x) mutex_unlock(&mutex_##x)
237 #define MI(x) mutex_init(&mutex_##x)
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)
243 #define WSL(x) down_write(&rwsem_##x)
244 #define WSU(x) up_write(&rwsem_##x)
246 #define RSL(x) down_read(&rwsem_##x)
247 #define RSU(x) up_read(&rwsem_##x)
248 #define RWSI(x) init_rwsem(&rwsem_##x)
250 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
251 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
253 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
255 #define WWAD(x) ww_acquire_done(x)
256 #define WWAF(x) ww_acquire_fini(x)
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)
264 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
267 * Generate different permutations of the same testcase, using
268 * the same basic lock-dependency/state events:
271 #define GENERATE_TESTCASE(name) \
273 static void name(void) { E(); }
275 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
277 static void name##_12(void) { E1(); E2(); } \
278 static void name##_21(void) { E2(); E1(); }
280 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
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(); }
296 LOCK(X2); /* this one should fail */
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)
314 #ifdef CONFIG_RT_MUTEXES
315 #include "locking-selftest-rtmutex.h"
316 GENERATE_TESTCASE(AA_rtmutex);
322 * Special-case for read-locking, they are
323 * allowed to recurse on the same lock class:
325 static void rlock_AA1(void)
328 RL(X1); // this one should NOT fail
331 static void rlock_AA1B(void)
334 RL(X2); // this one should NOT fail
337 static void rsem_AA1(void)
340 RSL(X1); // this one should fail
343 static void rsem_AA1B(void)
346 RSL(X2); // this one should fail
349 * The mixing of read and write locks is not allowed:
351 static void rlock_AA2(void)
354 WL(X2); // this one should fail
357 static void rsem_AA2(void)
360 WSL(X2); // this one should fail
363 static void rlock_AA3(void)
366 RL(X2); // this one should fail
369 static void rsem_AA3(void)
372 RSL(X2); // this one should fail
381 static void rlock_ABBA1(void)
391 U(Y1); // should fail
394 static void rwsem_ABBA1(void)
404 MU(Y1); // should fail
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
417 * { read_lock(A)->lock(B) dependency exists }
423 * { Not a deadlock, B -> A is added in the chain cache }
429 * { B->A found in chain cache, not reported as a deadlock }
432 static void rlock_chaincache_ABBA1(void)
447 U(Y1); // should fail
456 static void rlock_ABBA2(void)
466 U(Y1); // should NOT fail
469 static void rwsem_ABBA2(void)
479 MU(Y1); // should fail
489 static void rlock_ABBA3(void)
499 U(Y1); // should fail
502 static void rwsem_ABBA3(void)
512 MU(Y1); // should fail
521 LOCK_UNLOCK_2(A, B); \
522 LOCK_UNLOCK_2(B, A); /* fail */
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)
540 #ifdef CONFIG_RT_MUTEXES
541 #include "locking-selftest-rtmutex.h"
542 GENERATE_TESTCASE(ABBA_rtmutex);
553 LOCK_UNLOCK_2(A, B); \
554 LOCK_UNLOCK_2(B, C); \
555 LOCK_UNLOCK_2(C, A); /* fail */
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)
573 #ifdef CONFIG_RT_MUTEXES
574 #include "locking-selftest-rtmutex.h"
575 GENERATE_TESTCASE(ABBCCA_rtmutex);
586 LOCK_UNLOCK_2(A, B); \
587 LOCK_UNLOCK_2(C, A); \
588 LOCK_UNLOCK_2(B, C); /* fail */
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)
606 #ifdef CONFIG_RT_MUTEXES
607 #include "locking-selftest-rtmutex.h"
608 GENERATE_TESTCASE(ABCABC_rtmutex);
614 * AB BC CD DA deadlock:
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 */
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)
640 #ifdef CONFIG_RT_MUTEXES
641 #include "locking-selftest-rtmutex.h"
642 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
648 * AB CD BD DA deadlock:
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 */
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)
673 #ifdef CONFIG_RT_MUTEXES
674 #include "locking-selftest-rtmutex.h"
675 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
681 * AB CD BC DA deadlock:
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 */
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)
706 #ifdef CONFIG_RT_MUTEXES
707 #include "locking-selftest-rtmutex.h"
708 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
720 UNLOCK(A); /* fail */
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)
738 #ifdef CONFIG_RT_MUTEXES
739 #include "locking-selftest-rtmutex.h"
740 GENERATE_TESTCASE(double_unlock_rtmutex);
746 * initializing a held lock:
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)
769 #ifdef CONFIG_RT_MUTEXES
770 #include "locking-selftest-rtmutex.h"
771 GENERATE_TESTCASE(init_held_rtmutex);
777 * locking an irq-safe lock with irqs enabled:
792 * Generate 24 testcases:
794 #include "locking-selftest-spin-hardirq.h"
795 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
797 #include "locking-selftest-rlock-hardirq.h"
798 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
800 #include "locking-selftest-wlock-hardirq.h"
801 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
803 #include "locking-selftest-spin-softirq.h"
804 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
806 #include "locking-selftest-rlock-softirq.h"
807 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
809 #include "locking-selftest-wlock-softirq.h"
810 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
816 * Enabling hardirqs with a softirq-safe lock held:
833 * Generate 12 testcases:
835 #include "locking-selftest-spin.h"
836 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
838 #include "locking-selftest-wlock.h"
839 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
841 #include "locking-selftest-rlock.h"
842 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
848 * Enabling irqs with an irq-safe lock held:
865 * Generate 24 testcases:
867 #include "locking-selftest-spin-hardirq.h"
868 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
870 #include "locking-selftest-rlock-hardirq.h"
871 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
873 #include "locking-selftest-wlock-hardirq.h"
874 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
876 #include "locking-selftest-spin-softirq.h"
877 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
879 #include "locking-selftest-rlock-softirq.h"
880 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
882 #include "locking-selftest-wlock-softirq.h"
883 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
889 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
911 * Generate 36 testcases:
913 #include "locking-selftest-spin-hardirq.h"
914 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
916 #include "locking-selftest-rlock-hardirq.h"
917 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
919 #include "locking-selftest-wlock-hardirq.h"
920 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
922 #include "locking-selftest-spin-softirq.h"
923 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
925 #include "locking-selftest-rlock-softirq.h"
926 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
928 #include "locking-selftest-wlock-softirq.h"
929 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
936 * If a lock turns into softirq-safe, but earlier it took
937 * a softirq-unsafe lock:
959 * Generate 36 testcases:
961 #include "locking-selftest-spin-hardirq.h"
962 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
964 #include "locking-selftest-rlock-hardirq.h"
965 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
967 #include "locking-selftest-wlock-hardirq.h"
968 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
970 #include "locking-selftest-spin-softirq.h"
971 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
973 #include "locking-selftest-rlock-softirq.h"
974 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
976 #include "locking-selftest-wlock-softirq.h"
977 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
984 * read-lock / write-lock irq inversion.
988 * CPU#1 is at #1, i.e. it has write-locked A, but has not
991 * CPU#2 is at #2, i.e. it has locked B.
993 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
995 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
1021 * Generate 36 testcases:
1023 #include "locking-selftest-spin-hardirq.h"
1024 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
1026 #include "locking-selftest-rlock-hardirq.h"
1027 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
1029 #include "locking-selftest-wlock-hardirq.h"
1030 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
1032 #include "locking-selftest-spin-softirq.h"
1033 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
1035 #include "locking-selftest-rlock-softirq.h"
1036 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
1038 #include "locking-selftest-wlock-softirq.h"
1039 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
1046 * write-read / write-read / write-read deadlock even if read is recursive
1070 #include "locking-selftest-rlock.h"
1071 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
1078 * write-write / read-read / write-read deadlock even if read is recursive
1102 #include "locking-selftest-rlock.h"
1103 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
1110 * write-write / read-read / read-write is not deadlock when read is recursive
1134 #include "locking-selftest-rlock.h"
1135 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
1142 * write-read / read-read / write-write is not deadlock when read is recursive
1166 #include "locking-selftest-rlock.h"
1167 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
1173 * read-lock / write-lock recursion that is actually safe.
1198 * Generate 24 testcases:
1200 #include "locking-selftest-hardirq.h"
1201 #include "locking-selftest-rlock.h"
1202 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
1204 #include "locking-selftest-wlock.h"
1205 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
1207 #include "locking-selftest-softirq.h"
1208 #include "locking-selftest-rlock.h"
1209 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
1211 #include "locking-selftest-wlock.h"
1212 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
1219 * read-lock / write-lock recursion that is unsafe.
1244 * Generate 24 testcases:
1246 #include "locking-selftest-hardirq.h"
1247 #include "locking-selftest-rlock.h"
1248 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
1250 #include "locking-selftest-wlock.h"
1251 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
1253 #include "locking-selftest-softirq.h"
1254 #include "locking-selftest-rlock.h"
1255 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
1257 #include "locking-selftest-wlock.h"
1258 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
1264 * read-lock / write-lock recursion that is unsafe.
1266 * A is a ENABLED_*_READ lock
1267 * B is a USED_IN_*_READ lock
1273 * write_lock(A); // if this one is read_lock(), no deadlock
1298 * Generate 24 testcases:
1300 #include "locking-selftest-hardirq.h"
1301 #include "locking-selftest-rlock.h"
1302 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
1304 #include "locking-selftest-wlock.h"
1305 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
1307 #include "locking-selftest-softirq.h"
1308 #include "locking-selftest-rlock.h"
1309 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
1311 #include "locking-selftest-wlock.h"
1312 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
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)
1326 # define I_SPINLOCK(x)
1327 # define I_RAW_SPINLOCK(x)
1328 # define I_RWLOCK(x)
1332 # define I_LOCAL_LOCK(x)
1336 # define I_RTMUTEX(x)
1339 #ifdef CONFIG_RT_MUTEXES
1340 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1342 #define I2_RTMUTEX(x)
1356 spin_lock_init(&lock_##x); \
1357 rwlock_init(&rwlock_##x); \
1358 mutex_init(&mutex_##x); \
1359 init_rwsem(&rwsem_##x); \
1363 static void reset_locks(void)
1365 local_irq_disable();
1366 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1367 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
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);
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);
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));
1392 static int testcase_total;
1393 static int testcase_successes;
1394 static int expected_testcase_failures;
1395 static int unexpected_testcase_failures;
1397 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1399 unsigned long saved_preempt_count = preempt_count();
1401 WARN_ON(irqs_disabled());
1403 debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
1407 * Filter out expected failures:
1409 #ifndef CONFIG_PROVE_LOCKING
1410 if (expected == FAILURE && debug_locks) {
1411 expected_testcase_failures++;
1416 if (debug_locks != expected) {
1417 unexpected_testcase_failures++;
1420 testcase_successes++;
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);
1429 * Some tests (e.g. double-unlock) might corrupt the preemption
1430 * count, so restore it:
1432 preempt_count_set(saved_preempt_count);
1433 #ifdef CONFIG_TRACE_IRQFLAGS
1434 if (softirq_count())
1435 current->softirqs_enabled = 0;
1437 current->softirqs_enabled = 1;
1443 #ifdef CONFIG_RT_MUTEXES
1444 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1446 #define dotest_rt(fn, e, m)
1449 static inline void print_testname(const char *testname)
1451 printk("%33s:", testname);
1454 #define DO_TESTCASE_1(desc, name, nr) \
1455 print_testname(desc"/"#nr); \
1456 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1459 #define DO_TESTCASE_1B(desc, name, nr) \
1460 print_testname(desc"/"#nr); \
1461 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1464 #define DO_TESTCASE_1RR(desc, name, nr) \
1465 print_testname(desc"/"#nr); \
1467 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1470 #define DO_TESTCASE_1RRB(desc, name, nr) \
1471 print_testname(desc"/"#nr); \
1473 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
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); \
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); \
1491 #define DO_TESTCASE_2RW(desc, name, nr) \
1492 print_testname(desc"/"#nr); \
1494 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1495 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
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) \
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);
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); \
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); \
1533 * 'read' variant: rlocks must not trigger.
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); \
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);
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);
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);
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);
1562 #define DO_TESTCASE_2x3(desc, name) \
1563 DO_TESTCASE_3(desc, name, 12); \
1564 DO_TESTCASE_3(desc, name, 21);
1566 #define DO_TESTCASE_2x6(desc, name) \
1567 DO_TESTCASE_6I(desc, name, 12); \
1568 DO_TESTCASE_6I(desc, name, 21);
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);
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);
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);
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);
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);
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);
1618 static void ww_test_fail_acquire(void)
1627 if (WARN_ON(!o.ctx) ||
1631 /* No lockdep test, pure API */
1633 WARN_ON(ret != -EALREADY);
1641 WARN_ON(ret != -EDEADLK);
1646 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1648 DEBUG_LOCKS_WARN_ON(1);
1652 static void ww_test_normal(void)
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.
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);
1670 /* mutex_lock_interruptible (and *_nested) */
1671 o.ctx = (void *)~0UL;
1672 ret = mutex_lock_interruptible(&o.base);
1674 mutex_unlock(&o.base);
1677 WARN_ON(o.ctx != (void *)~0UL);
1679 /* mutex_lock_killable (and *_nested) */
1680 o.ctx = (void *)~0UL;
1681 ret = mutex_lock_killable(&o.base);
1683 mutex_unlock(&o.base);
1686 WARN_ON(o.ctx != (void *)~0UL);
1688 /* trylock, succeeding */
1689 o.ctx = (void *)~0UL;
1690 ret = mutex_trylock(&o.base);
1693 mutex_unlock(&o.base);
1696 WARN_ON(o.ctx != (void *)~0UL);
1698 /* trylock, failing */
1699 o.ctx = (void *)~0UL;
1700 mutex_lock(&o.base);
1701 ret = mutex_trylock(&o.base);
1703 mutex_unlock(&o.base);
1704 WARN_ON(o.ctx != (void *)~0UL);
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);
1713 static void ww_test_two_contexts(void)
1719 static void ww_test_diff_class(void)
1722 #ifdef CONFIG_DEBUG_MUTEXES
1728 static void ww_test_context_done_twice(void)
1736 static void ww_test_context_unlock_twice(void)
1744 static void ww_test_context_fini_early(void)
1752 static void ww_test_context_lock_after_done(void)
1759 static void ww_test_object_unlock_twice(void)
1766 static void ww_test_object_lock_unbalanced(void)
1775 static void ww_test_object_lock_stale_context(void)
1782 static void ww_test_edeadlk_normal(void)
1786 mutex_lock(&o2.base);
1788 mutex_release(&o2.base.dep_map, _THIS_IP_);
1798 WARN_ON(ret != -EDEADLK);
1801 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1802 mutex_unlock(&o2.base);
1808 static void ww_test_edeadlk_normal_slow(void)
1812 mutex_lock(&o2.base);
1813 mutex_release(&o2.base.dep_map, _THIS_IP_);
1824 WARN_ON(ret != -EDEADLK);
1827 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1828 mutex_unlock(&o2.base);
1831 ww_mutex_lock_slow(&o2, &t);
1834 static void ww_test_edeadlk_no_unlock(void)
1838 mutex_lock(&o2.base);
1840 mutex_release(&o2.base.dep_map, _THIS_IP_);
1850 WARN_ON(ret != -EDEADLK);
1853 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1854 mutex_unlock(&o2.base);
1859 static void ww_test_edeadlk_no_unlock_slow(void)
1863 mutex_lock(&o2.base);
1864 mutex_release(&o2.base.dep_map, _THIS_IP_);
1875 WARN_ON(ret != -EDEADLK);
1878 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1879 mutex_unlock(&o2.base);
1881 ww_mutex_lock_slow(&o2, &t);
1884 static void ww_test_edeadlk_acquire_more(void)
1888 mutex_lock(&o2.base);
1889 mutex_release(&o2.base.dep_map, _THIS_IP_);
1900 WARN_ON(ret != -EDEADLK);
1905 static void ww_test_edeadlk_acquire_more_slow(void)
1909 mutex_lock(&o2.base);
1910 mutex_release(&o2.base.dep_map, _THIS_IP_);
1921 WARN_ON(ret != -EDEADLK);
1923 ww_mutex_lock_slow(&o3, &t);
1926 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1930 mutex_lock(&o2.base);
1931 mutex_release(&o2.base.dep_map, _THIS_IP_);
1934 mutex_lock(&o3.base);
1935 mutex_release(&o3.base.dep_map, _THIS_IP_);
1946 WARN_ON(ret != -EDEADLK);
1949 WARN_ON(ret != -EDEADLK);
1952 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1956 mutex_lock(&o2.base);
1957 mutex_release(&o2.base.dep_map, _THIS_IP_);
1960 mutex_lock(&o3.base);
1961 mutex_release(&o3.base.dep_map, _THIS_IP_);
1972 WARN_ON(ret != -EDEADLK);
1974 ww_mutex_lock_slow(&o3, &t);
1977 static void ww_test_edeadlk_acquire_wrong(void)
1981 mutex_lock(&o2.base);
1982 mutex_release(&o2.base.dep_map, _THIS_IP_);
1993 WARN_ON(ret != -EDEADLK);
2002 static void ww_test_edeadlk_acquire_wrong_slow(void)
2006 mutex_lock(&o2.base);
2007 mutex_release(&o2.base.dep_map, _THIS_IP_);
2018 WARN_ON(ret != -EDEADLK);
2024 ww_mutex_lock_slow(&o3, &t);
2027 static void ww_test_spin_nest_unlocked(void)
2029 spin_lock_nest_lock(&lock_A, &o.base);
2033 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2034 static void ww_test_spin_nest_lock(void)
2036 spin_lock(&lock_X1);
2037 spin_lock_nest_lock(&lock_Y1, &lock_X1);
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);
2046 static void ww_test_unneeded_slow(void)
2050 ww_mutex_lock_slow(&o, &t);
2053 static void ww_test_context_block(void)
2064 static void ww_test_context_try(void)
2079 static void ww_test_context_context(void)
2095 static void ww_test_try_block(void)
2107 static void ww_test_try_try(void)
2119 static void ww_test_try_context(void)
2132 static void ww_test_block_block(void)
2138 static void ww_test_block_try(void)
2147 static void ww_test_block_context(void)
2158 static void ww_test_spin_block(void)
2174 static void ww_test_spin_try(void)
2194 static void ww_test_spin_context(void)
2216 static void ww_tests(void)
2218 printk(" --------------------------------------------------------------------------\n");
2219 printk(" | Wound/wait tests |\n");
2220 printk(" ---------------------\n");
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);
2228 print_testname("ww contexts mixing");
2229 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
2230 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
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);
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);
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);
2259 print_testname("spinlock nest unlocked");
2260 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
2263 print_testname("spinlock nest test");
2264 dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW);
2267 printk(" -----------------------------------------------------\n");
2268 printk(" |block | try |context|\n");
2269 printk(" -----------------------------------------------------\n");
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);
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);
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);
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);
2298 * <in hardirq handler>
2307 static void queued_read_lock_hardirq_RE_Er(void)
2310 read_lock(&rwlock_A);
2313 read_unlock(&rwlock_A);
2318 read_lock(&rwlock_A);
2319 read_unlock(&rwlock_A);
2325 * <in hardirq handler>
2332 * is not a deadlock.
2334 static void queued_read_lock_hardirq_ER_rE(void)
2338 read_lock(&rwlock_A);
2339 read_unlock(&rwlock_A);
2344 read_lock(&rwlock_A);
2347 read_unlock(&rwlock_A);
2355 * <in hardirq handler>
2359 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2361 static void queued_read_lock_hardirq_inversion(void)
2371 read_lock(&rwlock_A);
2372 read_unlock(&rwlock_A);
2376 read_lock(&rwlock_A);
2377 read_unlock(&rwlock_A);
2380 static void queued_read_lock_tests(void)
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);
2389 print_testname("hardirq lock-read/read-lock");
2390 dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
2393 print_testname("hardirq inversion");
2394 dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
2398 static void fs_reclaim_correct_nesting(void)
2400 fs_reclaim_acquire(GFP_KERNEL);
2401 might_alloc(GFP_NOFS);
2402 fs_reclaim_release(GFP_KERNEL);
2405 static void fs_reclaim_wrong_nesting(void)
2407 fs_reclaim_acquire(GFP_KERNEL);
2408 might_alloc(GFP_KERNEL);
2409 fs_reclaim_release(GFP_KERNEL);
2412 static void fs_reclaim_protected_nesting(void)
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);
2423 static void fs_reclaim_tests(void)
2425 printk(" --------------------\n");
2426 printk(" | fs_reclaim tests |\n");
2427 printk(" --------------------\n");
2429 print_testname("correct nesting");
2430 dotest(fs_reclaim_correct_nesting, SUCCESS, 0);
2433 print_testname("wrong nesting");
2434 dotest(fs_reclaim_wrong_nesting, FAILURE, 0);
2437 print_testname("protected nesting");
2438 dotest(fs_reclaim_protected_nesting, SUCCESS, 0);
2442 #define __guard(cleanup) __maybe_unused __attribute__((__cleanup__(cleanup)))
2444 static void hardirq_exit(int *_)
2449 #define HARDIRQ_CONTEXT(name, ...) \
2450 int hardirq_guard_##name __guard(hardirq_exit); \
2453 #define NOTTHREADED_HARDIRQ_CONTEXT(name, ...) \
2454 int notthreaded_hardirq_guard_##name __guard(hardirq_exit); \
2455 local_irq_disable(); \
2459 static void softirq_exit(int *_)
2464 #define SOFTIRQ_CONTEXT(name, ...) \
2465 int softirq_guard_##name __guard(softirq_exit); \
2468 static void rcu_exit(int *_)
2473 #define RCU_CONTEXT(name, ...) \
2474 int rcu_guard_##name __guard(rcu_exit); \
2477 static void rcu_bh_exit(int *_)
2479 rcu_read_unlock_bh();
2482 #define RCU_BH_CONTEXT(name, ...) \
2483 int rcu_bh_guard_##name __guard(rcu_bh_exit); \
2486 static void rcu_sched_exit(int *_)
2488 rcu_read_unlock_sched();
2491 #define RCU_SCHED_CONTEXT(name, ...) \
2492 int rcu_sched_guard_##name __guard(rcu_sched_exit); \
2493 rcu_read_lock_sched();
2495 static void rcu_callback_exit(int *_)
2497 rcu_lock_release(&rcu_callback_map);
2500 #define RCU_CALLBACK_CONTEXT(name, ...) \
2501 int rcu_callback_guard_##name __guard(rcu_callback_exit); \
2502 rcu_lock_acquire(&rcu_callback_map);
2505 static void raw_spinlock_exit(raw_spinlock_t **lock)
2507 raw_spin_unlock(*lock);
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));
2514 static void spinlock_exit(spinlock_t **lock)
2519 #define SPINLOCK_CONTEXT(name, lock) \
2520 spinlock_t *spinlock_guard_##name __guard(spinlock_exit) = &(lock); \
2523 static void mutex_exit(struct mutex **lock)
2525 mutex_unlock(*lock);
2528 #define MUTEX_CONTEXT(name, lock) \
2529 struct mutex *mutex_guard_##name __guard(mutex_exit) = &(lock); \
2530 mutex_lock(&(lock));
2532 #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock) \
2534 static void __maybe_unused inner##_in_##outer(void) \
2536 outer##_CONTEXT(_, outer_lock); \
2538 inner##_CONTEXT(_, inner_lock); \
2543 * wait contexts (considering PREEMPT_RT)
2545 * o: inner is allowed in outer
2546 * x: inner is disallowed in outer
2548 * \ inner | RCU | RAW_SPIN | SPIN | MUTEX
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 * ---------------+-------+----------+------+-------
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)
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)
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); \
2598 * the outer context only allows the preemption introduced by spinlock_t (which
2599 * is a sleepable lock for PREEMPT_RT)
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); \
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); \
2614 static void wait_context_tests(void)
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);
2625 print_testname("in hardirq context (not threaded)");
2626 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ);
2629 print_testname("in softirq context");
2630 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ);
2633 print_testname("in RCU context");
2634 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU);
2637 print_testname("in RCU-bh context");
2638 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH);
2641 print_testname("in RCU callback context");
2642 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_CALLBACK);
2645 print_testname("in RCU-sched context");
2646 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED);
2649 print_testname("in RAW_SPINLOCK context");
2650 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RAW_SPINLOCK);
2653 print_testname("in SPINLOCK context");
2654 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SPINLOCK);
2657 print_testname("in MUTEX context");
2658 DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(MUTEX);
2662 static void local_lock_2(void)
2664 local_lock_acquire(&local_A); /* IRQ-ON */
2665 local_lock_release(&local_A);
2668 spin_lock(&lock_A); /* IN-IRQ */
2669 spin_unlock(&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);
2680 static void local_lock_3A(void)
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);
2688 spin_lock(&lock_A); /* IN-IRQ */
2689 spin_unlock(&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);
2700 static void local_lock_3B(void)
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);
2708 spin_lock(&lock_A); /* IN-IRQ */
2709 spin_unlock(&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);
2721 spin_lock(&lock_B); /* IN-IRQ <-> IRQ-ON cycle, true */
2722 spin_unlock(&lock_B);
2723 spin_unlock(&lock_A);
2728 static void local_lock_tests(void)
2730 printk(" --------------------------------------------------------------------------\n");
2731 printk(" | local_lock tests |\n");
2732 printk(" ---------------------\n");
2734 print_testname("local_lock inversion 2");
2735 dotest(local_lock_2, SUCCESS, LOCKTYPE_LL);
2738 print_testname("local_lock inversion 3A");
2739 dotest(local_lock_3A, SUCCESS, LOCKTYPE_LL);
2742 print_testname("local_lock inversion 3B");
2743 dotest(local_lock_3B, FAILURE, LOCKTYPE_LL);
2747 void locking_selftest(void)
2750 * Got a locking failure before the selftest ran?
2753 printk("----------------------------------\n");
2754 printk("| Locking API testsuite disabled |\n");
2755 printk("----------------------------------\n");
2760 * treats read_lock() as recursive read locks for testing purpose
2762 force_read_lock_recursive = 1;
2765 * Run the testsuite:
2767 printk("------------------------\n");
2768 printk("| Locking API testsuite:\n");
2769 printk("----------------------------------------------------------------------------\n");
2770 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
2771 printk(" --------------------------------------------------------------------------\n");
2773 init_shared_classes();
2774 lockdep_set_selftest_task(current);
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);
2786 printk(" --------------------------------------------------------------------------\n");
2787 print_testname("recursive read-lock");
2789 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
2791 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
2794 print_testname("recursive read-lock #2");
2796 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
2798 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
2801 print_testname("mixed read-write-lock");
2803 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
2805 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
2808 print_testname("mixed write-read-lock");
2810 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
2812 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
2815 print_testname("mixed read-lock/lock-write ABBA");
2817 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2819 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2821 print_testname("mixed read-lock/lock-read ABBA");
2823 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2825 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2827 print_testname("mixed write-lock/lock-write ABBA");
2829 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2831 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2833 print_testname("chain cached mixed R-L/L-W ABBA");
2835 dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
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);
2842 printk(" --------------------------------------------------------------------------\n");
2845 * irq-context testcases:
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);
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);
2860 force_read_lock_recursive = 0;
2862 * queued_read_lock() specific test cases can be put here
2864 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
2865 queued_read_lock_tests();
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();
2875 if (unexpected_testcase_failures) {
2876 printk("-----------------------------------------------------------------\n");
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");
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");
2894 printk("-------------------------------------------------------\n");
2895 printk("Good, all %3d testcases passed! |\n",
2896 testcase_successes);
2897 printk("---------------------------------\n");
2900 lockdep_set_selftest_task(NULL);
2901 debug_locks_silent = 0;