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(); \
197 lockdep_hardirq_threaded(); \
200 #define HARDIRQ_EXIT() \
204 #define SOFTIRQ_DISABLE local_bh_disable
205 #define SOFTIRQ_ENABLE local_bh_enable
207 #define SOFTIRQ_ENTER() \
208 local_bh_disable(); \
209 local_irq_disable(); \
210 lockdep_softirq_enter(); \
211 WARN_ON(!in_softirq());
213 #define SOFTIRQ_EXIT() \
214 lockdep_softirq_exit(); \
215 local_irq_enable(); \
219 * Shortcuts for lock/unlock API variants, to keep
220 * the testcases compact:
222 #define L(x) spin_lock(&lock_##x)
223 #define U(x) spin_unlock(&lock_##x)
224 #define LU(x) L(x); U(x)
225 #define SI(x) spin_lock_init(&lock_##x)
227 #define WL(x) write_lock(&rwlock_##x)
228 #define WU(x) write_unlock(&rwlock_##x)
229 #define WLU(x) WL(x); WU(x)
231 #define RL(x) read_lock(&rwlock_##x)
232 #define RU(x) read_unlock(&rwlock_##x)
233 #define RLU(x) RL(x); RU(x)
234 #define RWI(x) rwlock_init(&rwlock_##x)
236 #define ML(x) mutex_lock(&mutex_##x)
237 #define MU(x) mutex_unlock(&mutex_##x)
238 #define MI(x) mutex_init(&mutex_##x)
240 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
241 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
242 #define RTI(x) rt_mutex_init(&rtmutex_##x)
244 #define WSL(x) down_write(&rwsem_##x)
245 #define WSU(x) up_write(&rwsem_##x)
247 #define RSL(x) down_read(&rwsem_##x)
248 #define RSU(x) up_read(&rwsem_##x)
249 #define RWSI(x) init_rwsem(&rwsem_##x)
251 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
252 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
254 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
256 #define WWAD(x) ww_acquire_done(x)
257 #define WWAF(x) ww_acquire_fini(x)
259 #define WWL(x, c) ww_mutex_lock(x, c)
260 #define WWT(x) ww_mutex_trylock(x)
261 #define WWL1(x) ww_mutex_lock(x, NULL)
262 #define WWU(x) ww_mutex_unlock(x)
265 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
268 * Generate different permutations of the same testcase, using
269 * the same basic lock-dependency/state events:
272 #define GENERATE_TESTCASE(name) \
274 static void name(void) { E(); }
276 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
278 static void name##_12(void) { E1(); E2(); } \
279 static void name##_21(void) { E2(); E1(); }
281 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
283 static void name##_123(void) { E1(); E2(); E3(); } \
284 static void name##_132(void) { E1(); E3(); E2(); } \
285 static void name##_213(void) { E2(); E1(); E3(); } \
286 static void name##_231(void) { E2(); E3(); E1(); } \
287 static void name##_312(void) { E3(); E1(); E2(); } \
288 static void name##_321(void) { E3(); E2(); E1(); }
297 LOCK(X2); /* this one should fail */
302 #include "locking-selftest-spin.h"
303 GENERATE_TESTCASE(AA_spin)
304 #include "locking-selftest-wlock.h"
305 GENERATE_TESTCASE(AA_wlock)
306 #include "locking-selftest-rlock.h"
307 GENERATE_TESTCASE(AA_rlock)
308 #include "locking-selftest-mutex.h"
309 GENERATE_TESTCASE(AA_mutex)
310 #include "locking-selftest-wsem.h"
311 GENERATE_TESTCASE(AA_wsem)
312 #include "locking-selftest-rsem.h"
313 GENERATE_TESTCASE(AA_rsem)
315 #ifdef CONFIG_RT_MUTEXES
316 #include "locking-selftest-rtmutex.h"
317 GENERATE_TESTCASE(AA_rtmutex);
323 * Special-case for read-locking, they are
324 * allowed to recurse on the same lock class:
326 static void rlock_AA1(void)
329 RL(X1); // this one should NOT fail
332 static void rlock_AA1B(void)
335 RL(X2); // this one should NOT fail
338 static void rsem_AA1(void)
341 RSL(X1); // this one should fail
344 static void rsem_AA1B(void)
347 RSL(X2); // this one should fail
350 * The mixing of read and write locks is not allowed:
352 static void rlock_AA2(void)
355 WL(X2); // this one should fail
358 static void rsem_AA2(void)
361 WSL(X2); // this one should fail
364 static void rlock_AA3(void)
367 RL(X2); // this one should fail
370 static void rsem_AA3(void)
373 RSL(X2); // this one should fail
382 static void rlock_ABBA1(void)
392 U(Y1); // should fail
395 static void rwsem_ABBA1(void)
405 MU(Y1); // should fail
414 * This test case is aimed at poking whether the chain cache prevents us from
415 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
416 * read/write locks, the following case may happen
418 * { read_lock(A)->lock(B) dependency exists }
424 * { Not a deadlock, B -> A is added in the chain cache }
430 * { B->A found in chain cache, not reported as a deadlock }
433 static void rlock_chaincache_ABBA1(void)
448 U(Y1); // should fail
457 static void rlock_ABBA2(void)
467 U(Y1); // should NOT fail
470 static void rwsem_ABBA2(void)
480 MU(Y1); // should fail
490 static void rlock_ABBA3(void)
500 U(Y1); // should fail
503 static void rwsem_ABBA3(void)
513 MU(Y1); // should fail
522 LOCK_UNLOCK_2(A, B); \
523 LOCK_UNLOCK_2(B, A); /* fail */
528 #include "locking-selftest-spin.h"
529 GENERATE_TESTCASE(ABBA_spin)
530 #include "locking-selftest-wlock.h"
531 GENERATE_TESTCASE(ABBA_wlock)
532 #include "locking-selftest-rlock.h"
533 GENERATE_TESTCASE(ABBA_rlock)
534 #include "locking-selftest-mutex.h"
535 GENERATE_TESTCASE(ABBA_mutex)
536 #include "locking-selftest-wsem.h"
537 GENERATE_TESTCASE(ABBA_wsem)
538 #include "locking-selftest-rsem.h"
539 GENERATE_TESTCASE(ABBA_rsem)
541 #ifdef CONFIG_RT_MUTEXES
542 #include "locking-selftest-rtmutex.h"
543 GENERATE_TESTCASE(ABBA_rtmutex);
554 LOCK_UNLOCK_2(A, B); \
555 LOCK_UNLOCK_2(B, C); \
556 LOCK_UNLOCK_2(C, A); /* fail */
561 #include "locking-selftest-spin.h"
562 GENERATE_TESTCASE(ABBCCA_spin)
563 #include "locking-selftest-wlock.h"
564 GENERATE_TESTCASE(ABBCCA_wlock)
565 #include "locking-selftest-rlock.h"
566 GENERATE_TESTCASE(ABBCCA_rlock)
567 #include "locking-selftest-mutex.h"
568 GENERATE_TESTCASE(ABBCCA_mutex)
569 #include "locking-selftest-wsem.h"
570 GENERATE_TESTCASE(ABBCCA_wsem)
571 #include "locking-selftest-rsem.h"
572 GENERATE_TESTCASE(ABBCCA_rsem)
574 #ifdef CONFIG_RT_MUTEXES
575 #include "locking-selftest-rtmutex.h"
576 GENERATE_TESTCASE(ABBCCA_rtmutex);
587 LOCK_UNLOCK_2(A, B); \
588 LOCK_UNLOCK_2(C, A); \
589 LOCK_UNLOCK_2(B, C); /* fail */
594 #include "locking-selftest-spin.h"
595 GENERATE_TESTCASE(ABCABC_spin)
596 #include "locking-selftest-wlock.h"
597 GENERATE_TESTCASE(ABCABC_wlock)
598 #include "locking-selftest-rlock.h"
599 GENERATE_TESTCASE(ABCABC_rlock)
600 #include "locking-selftest-mutex.h"
601 GENERATE_TESTCASE(ABCABC_mutex)
602 #include "locking-selftest-wsem.h"
603 GENERATE_TESTCASE(ABCABC_wsem)
604 #include "locking-selftest-rsem.h"
605 GENERATE_TESTCASE(ABCABC_rsem)
607 #ifdef CONFIG_RT_MUTEXES
608 #include "locking-selftest-rtmutex.h"
609 GENERATE_TESTCASE(ABCABC_rtmutex);
615 * AB BC CD DA deadlock:
620 LOCK_UNLOCK_2(A, B); \
621 LOCK_UNLOCK_2(B, C); \
622 LOCK_UNLOCK_2(C, D); \
623 LOCK_UNLOCK_2(D, A); /* fail */
628 #include "locking-selftest-spin.h"
629 GENERATE_TESTCASE(ABBCCDDA_spin)
630 #include "locking-selftest-wlock.h"
631 GENERATE_TESTCASE(ABBCCDDA_wlock)
632 #include "locking-selftest-rlock.h"
633 GENERATE_TESTCASE(ABBCCDDA_rlock)
634 #include "locking-selftest-mutex.h"
635 GENERATE_TESTCASE(ABBCCDDA_mutex)
636 #include "locking-selftest-wsem.h"
637 GENERATE_TESTCASE(ABBCCDDA_wsem)
638 #include "locking-selftest-rsem.h"
639 GENERATE_TESTCASE(ABBCCDDA_rsem)
641 #ifdef CONFIG_RT_MUTEXES
642 #include "locking-selftest-rtmutex.h"
643 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
649 * AB CD BD DA deadlock:
653 LOCK_UNLOCK_2(A, B); \
654 LOCK_UNLOCK_2(C, D); \
655 LOCK_UNLOCK_2(B, D); \
656 LOCK_UNLOCK_2(D, A); /* fail */
661 #include "locking-selftest-spin.h"
662 GENERATE_TESTCASE(ABCDBDDA_spin)
663 #include "locking-selftest-wlock.h"
664 GENERATE_TESTCASE(ABCDBDDA_wlock)
665 #include "locking-selftest-rlock.h"
666 GENERATE_TESTCASE(ABCDBDDA_rlock)
667 #include "locking-selftest-mutex.h"
668 GENERATE_TESTCASE(ABCDBDDA_mutex)
669 #include "locking-selftest-wsem.h"
670 GENERATE_TESTCASE(ABCDBDDA_wsem)
671 #include "locking-selftest-rsem.h"
672 GENERATE_TESTCASE(ABCDBDDA_rsem)
674 #ifdef CONFIG_RT_MUTEXES
675 #include "locking-selftest-rtmutex.h"
676 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
682 * AB CD BC DA deadlock:
686 LOCK_UNLOCK_2(A, B); \
687 LOCK_UNLOCK_2(C, D); \
688 LOCK_UNLOCK_2(B, C); \
689 LOCK_UNLOCK_2(D, A); /* fail */
694 #include "locking-selftest-spin.h"
695 GENERATE_TESTCASE(ABCDBCDA_spin)
696 #include "locking-selftest-wlock.h"
697 GENERATE_TESTCASE(ABCDBCDA_wlock)
698 #include "locking-selftest-rlock.h"
699 GENERATE_TESTCASE(ABCDBCDA_rlock)
700 #include "locking-selftest-mutex.h"
701 GENERATE_TESTCASE(ABCDBCDA_mutex)
702 #include "locking-selftest-wsem.h"
703 GENERATE_TESTCASE(ABCDBCDA_wsem)
704 #include "locking-selftest-rsem.h"
705 GENERATE_TESTCASE(ABCDBCDA_rsem)
707 #ifdef CONFIG_RT_MUTEXES
708 #include "locking-selftest-rtmutex.h"
709 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
721 UNLOCK(A); /* fail */
726 #include "locking-selftest-spin.h"
727 GENERATE_TESTCASE(double_unlock_spin)
728 #include "locking-selftest-wlock.h"
729 GENERATE_TESTCASE(double_unlock_wlock)
730 #include "locking-selftest-rlock.h"
731 GENERATE_TESTCASE(double_unlock_rlock)
732 #include "locking-selftest-mutex.h"
733 GENERATE_TESTCASE(double_unlock_mutex)
734 #include "locking-selftest-wsem.h"
735 GENERATE_TESTCASE(double_unlock_wsem)
736 #include "locking-selftest-rsem.h"
737 GENERATE_TESTCASE(double_unlock_rsem)
739 #ifdef CONFIG_RT_MUTEXES
740 #include "locking-selftest-rtmutex.h"
741 GENERATE_TESTCASE(double_unlock_rtmutex);
747 * initializing a held lock:
757 #include "locking-selftest-spin.h"
758 GENERATE_TESTCASE(init_held_spin)
759 #include "locking-selftest-wlock.h"
760 GENERATE_TESTCASE(init_held_wlock)
761 #include "locking-selftest-rlock.h"
762 GENERATE_TESTCASE(init_held_rlock)
763 #include "locking-selftest-mutex.h"
764 GENERATE_TESTCASE(init_held_mutex)
765 #include "locking-selftest-wsem.h"
766 GENERATE_TESTCASE(init_held_wsem)
767 #include "locking-selftest-rsem.h"
768 GENERATE_TESTCASE(init_held_rsem)
770 #ifdef CONFIG_RT_MUTEXES
771 #include "locking-selftest-rtmutex.h"
772 GENERATE_TESTCASE(init_held_rtmutex);
778 * locking an irq-safe lock with irqs enabled:
793 * Generate 24 testcases:
795 #include "locking-selftest-spin-hardirq.h"
796 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
798 #include "locking-selftest-rlock-hardirq.h"
799 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
801 #include "locking-selftest-wlock-hardirq.h"
802 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
804 #include "locking-selftest-spin-softirq.h"
805 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
807 #include "locking-selftest-rlock-softirq.h"
808 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
810 #include "locking-selftest-wlock-softirq.h"
811 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
817 * Enabling hardirqs with a softirq-safe lock held:
834 * Generate 12 testcases:
836 #include "locking-selftest-spin.h"
837 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
839 #include "locking-selftest-wlock.h"
840 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
842 #include "locking-selftest-rlock.h"
843 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
849 * Enabling irqs with an irq-safe lock held:
866 * Generate 24 testcases:
868 #include "locking-selftest-spin-hardirq.h"
869 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
871 #include "locking-selftest-rlock-hardirq.h"
872 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
874 #include "locking-selftest-wlock-hardirq.h"
875 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
877 #include "locking-selftest-spin-softirq.h"
878 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
880 #include "locking-selftest-rlock-softirq.h"
881 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
883 #include "locking-selftest-wlock-softirq.h"
884 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
890 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
912 * Generate 36 testcases:
914 #include "locking-selftest-spin-hardirq.h"
915 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
917 #include "locking-selftest-rlock-hardirq.h"
918 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
920 #include "locking-selftest-wlock-hardirq.h"
921 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
923 #include "locking-selftest-spin-softirq.h"
924 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
926 #include "locking-selftest-rlock-softirq.h"
927 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
929 #include "locking-selftest-wlock-softirq.h"
930 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
937 * If a lock turns into softirq-safe, but earlier it took
938 * a softirq-unsafe lock:
960 * Generate 36 testcases:
962 #include "locking-selftest-spin-hardirq.h"
963 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
965 #include "locking-selftest-rlock-hardirq.h"
966 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
968 #include "locking-selftest-wlock-hardirq.h"
969 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
971 #include "locking-selftest-spin-softirq.h"
972 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
974 #include "locking-selftest-rlock-softirq.h"
975 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
977 #include "locking-selftest-wlock-softirq.h"
978 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
985 * read-lock / write-lock irq inversion.
989 * CPU#1 is at #1, i.e. it has write-locked A, but has not
992 * CPU#2 is at #2, i.e. it has locked B.
994 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
996 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
1022 * Generate 36 testcases:
1024 #include "locking-selftest-spin-hardirq.h"
1025 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
1027 #include "locking-selftest-rlock-hardirq.h"
1028 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
1030 #include "locking-selftest-wlock-hardirq.h"
1031 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
1033 #include "locking-selftest-spin-softirq.h"
1034 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
1036 #include "locking-selftest-rlock-softirq.h"
1037 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
1039 #include "locking-selftest-wlock-softirq.h"
1040 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
1047 * write-read / write-read / write-read deadlock even if read is recursive
1071 #include "locking-selftest-rlock.h"
1072 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
1079 * write-write / read-read / write-read deadlock even if read is recursive
1103 #include "locking-selftest-rlock.h"
1104 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
1111 * write-write / read-read / read-write is not deadlock when read is recursive
1135 #include "locking-selftest-rlock.h"
1136 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
1143 * write-read / read-read / write-write is not deadlock when read is recursive
1167 #include "locking-selftest-rlock.h"
1168 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
1174 * read-lock / write-lock recursion that is actually safe.
1199 * Generate 24 testcases:
1201 #include "locking-selftest-hardirq.h"
1202 #include "locking-selftest-rlock.h"
1203 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
1205 #include "locking-selftest-wlock.h"
1206 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
1208 #include "locking-selftest-softirq.h"
1209 #include "locking-selftest-rlock.h"
1210 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
1212 #include "locking-selftest-wlock.h"
1213 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
1220 * read-lock / write-lock recursion that is unsafe.
1245 * Generate 24 testcases:
1247 #include "locking-selftest-hardirq.h"
1248 #include "locking-selftest-rlock.h"
1249 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
1251 #include "locking-selftest-wlock.h"
1252 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
1254 #include "locking-selftest-softirq.h"
1255 #include "locking-selftest-rlock.h"
1256 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
1258 #include "locking-selftest-wlock.h"
1259 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
1265 * read-lock / write-lock recursion that is unsafe.
1267 * A is a ENABLED_*_READ lock
1268 * B is a USED_IN_*_READ lock
1274 * write_lock(A); // if this one is read_lock(), no deadlock
1299 * Generate 24 testcases:
1301 #include "locking-selftest-hardirq.h"
1302 #include "locking-selftest-rlock.h"
1303 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
1305 #include "locking-selftest-wlock.h"
1306 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
1308 #include "locking-selftest-softirq.h"
1309 #include "locking-selftest-rlock.h"
1310 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
1312 #include "locking-selftest-wlock.h"
1313 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
1315 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1316 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1317 # define I_RAW_SPINLOCK(x) lockdep_reset_lock(&raw_lock_##x.dep_map)
1318 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1319 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1320 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1321 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1322 # define I_LOCAL_LOCK(x) lockdep_reset_lock(&local_##x.dep_map)
1323 #ifdef CONFIG_RT_MUTEXES
1324 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1327 # define I_SPINLOCK(x)
1328 # define I_RAW_SPINLOCK(x)
1329 # define I_RWLOCK(x)
1333 # define I_LOCAL_LOCK(x)
1337 # define I_RTMUTEX(x)
1340 #ifdef CONFIG_RT_MUTEXES
1341 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1343 #define I2_RTMUTEX(x)
1357 spin_lock_init(&lock_##x); \
1358 rwlock_init(&rwlock_##x); \
1359 mutex_init(&mutex_##x); \
1360 init_rwsem(&rwsem_##x); \
1364 static void reset_locks(void)
1366 local_irq_disable();
1367 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1368 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1370 I1(A); I1(B); I1(C); I1(D);
1371 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1372 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
1373 I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B);
1378 I2(A); I2(B); I2(C); I2(D);
1379 init_shared_classes();
1380 raw_spin_lock_init(&raw_lock_A);
1381 raw_spin_lock_init(&raw_lock_B);
1382 local_lock_init(&local_A);
1384 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1385 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1386 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1387 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
1393 static int testcase_total;
1394 static int testcase_successes;
1395 static int expected_testcase_failures;
1396 static int unexpected_testcase_failures;
1398 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1400 unsigned long saved_preempt_count = preempt_count();
1402 WARN_ON(irqs_disabled());
1404 debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
1408 * Filter out expected failures:
1410 #ifndef CONFIG_PROVE_LOCKING
1411 if (expected == FAILURE && debug_locks) {
1412 expected_testcase_failures++;
1417 if (debug_locks != expected) {
1418 unexpected_testcase_failures++;
1421 testcase_successes++;
1426 if (debug_locks_verbose & lockclass_mask)
1427 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1428 lockclass_mask, debug_locks, expected);
1430 * Some tests (e.g. double-unlock) might corrupt the preemption
1431 * count, so restore it:
1433 preempt_count_set(saved_preempt_count);
1434 #ifdef CONFIG_TRACE_IRQFLAGS
1435 if (softirq_count())
1436 current->softirqs_enabled = 0;
1438 current->softirqs_enabled = 1;
1444 #ifdef CONFIG_RT_MUTEXES
1445 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1447 #define dotest_rt(fn, e, m)
1450 static inline void print_testname(const char *testname)
1452 printk("%33s:", testname);
1455 #define DO_TESTCASE_1(desc, name, nr) \
1456 print_testname(desc"/"#nr); \
1457 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1460 #define DO_TESTCASE_1B(desc, name, nr) \
1461 print_testname(desc"/"#nr); \
1462 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1465 #define DO_TESTCASE_1RR(desc, name, nr) \
1466 print_testname(desc"/"#nr); \
1468 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1471 #define DO_TESTCASE_1RRB(desc, name, nr) \
1472 print_testname(desc"/"#nr); \
1474 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1478 #define DO_TESTCASE_3(desc, name, nr) \
1479 print_testname(desc"/"#nr); \
1480 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1481 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1482 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1485 #define DO_TESTCASE_3RW(desc, name, nr) \
1486 print_testname(desc"/"#nr); \
1487 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1488 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1489 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1492 #define DO_TESTCASE_2RW(desc, name, nr) \
1493 print_testname(desc"/"#nr); \
1495 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1496 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1499 #define DO_TESTCASE_2x2RW(desc, name, nr) \
1500 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
1501 DO_TESTCASE_2RW("soft-"desc, name##_soft, nr) \
1503 #define DO_TESTCASE_6x2x2RW(desc, name) \
1504 DO_TESTCASE_2x2RW(desc, name, 123); \
1505 DO_TESTCASE_2x2RW(desc, name, 132); \
1506 DO_TESTCASE_2x2RW(desc, name, 213); \
1507 DO_TESTCASE_2x2RW(desc, name, 231); \
1508 DO_TESTCASE_2x2RW(desc, name, 312); \
1509 DO_TESTCASE_2x2RW(desc, name, 321);
1511 #define DO_TESTCASE_6(desc, name) \
1512 print_testname(desc); \
1513 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1514 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1515 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1516 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1517 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1518 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1519 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1522 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1523 print_testname(desc); \
1524 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1525 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1526 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1527 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1528 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1529 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1530 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1534 * 'read' variant: rlocks must not trigger.
1536 #define DO_TESTCASE_6R(desc, name) \
1537 print_testname(desc); \
1538 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1539 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1540 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1541 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1542 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1543 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1544 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1547 #define DO_TESTCASE_2I(desc, name, nr) \
1548 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1549 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1551 #define DO_TESTCASE_2IB(desc, name, nr) \
1552 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1553 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1555 #define DO_TESTCASE_6I(desc, name, nr) \
1556 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1557 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1559 #define DO_TESTCASE_6IRW(desc, name, nr) \
1560 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1561 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1563 #define DO_TESTCASE_2x3(desc, name) \
1564 DO_TESTCASE_3(desc, name, 12); \
1565 DO_TESTCASE_3(desc, name, 21);
1567 #define DO_TESTCASE_2x6(desc, name) \
1568 DO_TESTCASE_6I(desc, name, 12); \
1569 DO_TESTCASE_6I(desc, name, 21);
1571 #define DO_TESTCASE_6x2(desc, name) \
1572 DO_TESTCASE_2I(desc, name, 123); \
1573 DO_TESTCASE_2I(desc, name, 132); \
1574 DO_TESTCASE_2I(desc, name, 213); \
1575 DO_TESTCASE_2I(desc, name, 231); \
1576 DO_TESTCASE_2I(desc, name, 312); \
1577 DO_TESTCASE_2I(desc, name, 321);
1579 #define DO_TESTCASE_6x2B(desc, name) \
1580 DO_TESTCASE_2IB(desc, name, 123); \
1581 DO_TESTCASE_2IB(desc, name, 132); \
1582 DO_TESTCASE_2IB(desc, name, 213); \
1583 DO_TESTCASE_2IB(desc, name, 231); \
1584 DO_TESTCASE_2IB(desc, name, 312); \
1585 DO_TESTCASE_2IB(desc, name, 321);
1587 #define DO_TESTCASE_6x1RR(desc, name) \
1588 DO_TESTCASE_1RR(desc, name, 123); \
1589 DO_TESTCASE_1RR(desc, name, 132); \
1590 DO_TESTCASE_1RR(desc, name, 213); \
1591 DO_TESTCASE_1RR(desc, name, 231); \
1592 DO_TESTCASE_1RR(desc, name, 312); \
1593 DO_TESTCASE_1RR(desc, name, 321);
1595 #define DO_TESTCASE_6x1RRB(desc, name) \
1596 DO_TESTCASE_1RRB(desc, name, 123); \
1597 DO_TESTCASE_1RRB(desc, name, 132); \
1598 DO_TESTCASE_1RRB(desc, name, 213); \
1599 DO_TESTCASE_1RRB(desc, name, 231); \
1600 DO_TESTCASE_1RRB(desc, name, 312); \
1601 DO_TESTCASE_1RRB(desc, name, 321);
1603 #define DO_TESTCASE_6x6(desc, name) \
1604 DO_TESTCASE_6I(desc, name, 123); \
1605 DO_TESTCASE_6I(desc, name, 132); \
1606 DO_TESTCASE_6I(desc, name, 213); \
1607 DO_TESTCASE_6I(desc, name, 231); \
1608 DO_TESTCASE_6I(desc, name, 312); \
1609 DO_TESTCASE_6I(desc, name, 321);
1611 #define DO_TESTCASE_6x6RW(desc, name) \
1612 DO_TESTCASE_6IRW(desc, name, 123); \
1613 DO_TESTCASE_6IRW(desc, name, 132); \
1614 DO_TESTCASE_6IRW(desc, name, 213); \
1615 DO_TESTCASE_6IRW(desc, name, 231); \
1616 DO_TESTCASE_6IRW(desc, name, 312); \
1617 DO_TESTCASE_6IRW(desc, name, 321);
1619 static void ww_test_fail_acquire(void)
1628 if (WARN_ON(!o.ctx) ||
1632 /* No lockdep test, pure API */
1634 WARN_ON(ret != -EALREADY);
1642 WARN_ON(ret != -EDEADLK);
1647 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1649 DEBUG_LOCKS_WARN_ON(1);
1653 static void ww_test_normal(void)
1660 * None of the ww_mutex codepaths should be taken in the 'normal'
1661 * mutex calls. The easiest way to verify this is by using the
1662 * normal mutex calls, and making sure o.ctx is unmodified.
1665 /* mutex_lock (and indirectly, mutex_lock_nested) */
1666 o.ctx = (void *)~0UL;
1667 mutex_lock(&o.base);
1668 mutex_unlock(&o.base);
1669 WARN_ON(o.ctx != (void *)~0UL);
1671 /* mutex_lock_interruptible (and *_nested) */
1672 o.ctx = (void *)~0UL;
1673 ret = mutex_lock_interruptible(&o.base);
1675 mutex_unlock(&o.base);
1678 WARN_ON(o.ctx != (void *)~0UL);
1680 /* mutex_lock_killable (and *_nested) */
1681 o.ctx = (void *)~0UL;
1682 ret = mutex_lock_killable(&o.base);
1684 mutex_unlock(&o.base);
1687 WARN_ON(o.ctx != (void *)~0UL);
1689 /* trylock, succeeding */
1690 o.ctx = (void *)~0UL;
1691 ret = mutex_trylock(&o.base);
1694 mutex_unlock(&o.base);
1697 WARN_ON(o.ctx != (void *)~0UL);
1699 /* trylock, failing */
1700 o.ctx = (void *)~0UL;
1701 mutex_lock(&o.base);
1702 ret = mutex_trylock(&o.base);
1704 mutex_unlock(&o.base);
1705 WARN_ON(o.ctx != (void *)~0UL);
1708 o.ctx = (void *)~0UL;
1709 mutex_lock_nest_lock(&o.base, &t);
1710 mutex_unlock(&o.base);
1711 WARN_ON(o.ctx != (void *)~0UL);
1714 static void ww_test_two_contexts(void)
1720 static void ww_test_diff_class(void)
1723 #ifdef CONFIG_DEBUG_MUTEXES
1729 static void ww_test_context_done_twice(void)
1737 static void ww_test_context_unlock_twice(void)
1745 static void ww_test_context_fini_early(void)
1753 static void ww_test_context_lock_after_done(void)
1760 static void ww_test_object_unlock_twice(void)
1767 static void ww_test_object_lock_unbalanced(void)
1776 static void ww_test_object_lock_stale_context(void)
1783 static void ww_test_edeadlk_normal(void)
1787 mutex_lock(&o2.base);
1789 mutex_release(&o2.base.dep_map, _THIS_IP_);
1799 WARN_ON(ret != -EDEADLK);
1802 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1803 mutex_unlock(&o2.base);
1809 static void ww_test_edeadlk_normal_slow(void)
1813 mutex_lock(&o2.base);
1814 mutex_release(&o2.base.dep_map, _THIS_IP_);
1825 WARN_ON(ret != -EDEADLK);
1828 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1829 mutex_unlock(&o2.base);
1832 ww_mutex_lock_slow(&o2, &t);
1835 static void ww_test_edeadlk_no_unlock(void)
1839 mutex_lock(&o2.base);
1841 mutex_release(&o2.base.dep_map, _THIS_IP_);
1851 WARN_ON(ret != -EDEADLK);
1854 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1855 mutex_unlock(&o2.base);
1860 static void ww_test_edeadlk_no_unlock_slow(void)
1864 mutex_lock(&o2.base);
1865 mutex_release(&o2.base.dep_map, _THIS_IP_);
1876 WARN_ON(ret != -EDEADLK);
1879 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1880 mutex_unlock(&o2.base);
1882 ww_mutex_lock_slow(&o2, &t);
1885 static void ww_test_edeadlk_acquire_more(void)
1889 mutex_lock(&o2.base);
1890 mutex_release(&o2.base.dep_map, _THIS_IP_);
1901 WARN_ON(ret != -EDEADLK);
1906 static void ww_test_edeadlk_acquire_more_slow(void)
1910 mutex_lock(&o2.base);
1911 mutex_release(&o2.base.dep_map, _THIS_IP_);
1922 WARN_ON(ret != -EDEADLK);
1924 ww_mutex_lock_slow(&o3, &t);
1927 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1931 mutex_lock(&o2.base);
1932 mutex_release(&o2.base.dep_map, _THIS_IP_);
1935 mutex_lock(&o3.base);
1936 mutex_release(&o3.base.dep_map, _THIS_IP_);
1947 WARN_ON(ret != -EDEADLK);
1950 WARN_ON(ret != -EDEADLK);
1953 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1957 mutex_lock(&o2.base);
1958 mutex_release(&o2.base.dep_map, _THIS_IP_);
1961 mutex_lock(&o3.base);
1962 mutex_release(&o3.base.dep_map, _THIS_IP_);
1973 WARN_ON(ret != -EDEADLK);
1975 ww_mutex_lock_slow(&o3, &t);
1978 static void ww_test_edeadlk_acquire_wrong(void)
1982 mutex_lock(&o2.base);
1983 mutex_release(&o2.base.dep_map, _THIS_IP_);
1994 WARN_ON(ret != -EDEADLK);
2003 static void ww_test_edeadlk_acquire_wrong_slow(void)
2007 mutex_lock(&o2.base);
2008 mutex_release(&o2.base.dep_map, _THIS_IP_);
2019 WARN_ON(ret != -EDEADLK);
2025 ww_mutex_lock_slow(&o3, &t);
2028 static void ww_test_spin_nest_unlocked(void)
2030 spin_lock_nest_lock(&lock_A, &o.base);
2034 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2035 static void ww_test_spin_nest_lock(void)
2037 spin_lock(&lock_X1);
2038 spin_lock_nest_lock(&lock_Y1, &lock_X1);
2040 spin_lock_nest_lock(&lock_Y2, &lock_X1);
2041 spin_unlock(&lock_A);
2042 spin_unlock(&lock_Y2);
2043 spin_unlock(&lock_Y1);
2044 spin_unlock(&lock_X1);
2047 static void ww_test_unneeded_slow(void)
2051 ww_mutex_lock_slow(&o, &t);
2054 static void ww_test_context_block(void)
2065 static void ww_test_context_try(void)
2080 static void ww_test_context_context(void)
2096 static void ww_test_try_block(void)
2108 static void ww_test_try_try(void)
2120 static void ww_test_try_context(void)
2133 static void ww_test_block_block(void)
2139 static void ww_test_block_try(void)
2148 static void ww_test_block_context(void)
2159 static void ww_test_spin_block(void)
2175 static void ww_test_spin_try(void)
2195 static void ww_test_spin_context(void)
2217 static void ww_tests(void)
2219 printk(" --------------------------------------------------------------------------\n");
2220 printk(" | Wound/wait tests |\n");
2221 printk(" ---------------------\n");
2223 print_testname("ww api failures");
2224 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
2225 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
2226 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
2229 print_testname("ww contexts mixing");
2230 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
2231 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
2234 print_testname("finishing ww context");
2235 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
2236 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
2237 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
2238 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
2241 print_testname("locking mismatches");
2242 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
2243 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
2244 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
2247 print_testname("EDEADLK handling");
2248 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
2249 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
2250 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
2251 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
2252 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
2253 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
2254 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
2255 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
2256 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
2257 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
2260 print_testname("spinlock nest unlocked");
2261 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
2264 print_testname("spinlock nest test");
2265 dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW);
2268 printk(" -----------------------------------------------------\n");
2269 printk(" |block | try |context|\n");
2270 printk(" -----------------------------------------------------\n");
2272 print_testname("context");
2273 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
2274 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
2275 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
2278 print_testname("try");
2279 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
2280 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
2281 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
2284 print_testname("block");
2285 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
2286 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
2287 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
2290 print_testname("spinlock");
2291 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
2292 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
2293 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
2299 * <in hardirq handler>
2308 static void queued_read_lock_hardirq_RE_Er(void)
2311 read_lock(&rwlock_A);
2314 read_unlock(&rwlock_A);
2319 read_lock(&rwlock_A);
2320 read_unlock(&rwlock_A);
2326 * <in hardirq handler>
2333 * is not a deadlock.
2335 static void queued_read_lock_hardirq_ER_rE(void)
2339 read_lock(&rwlock_A);
2340 read_unlock(&rwlock_A);
2345 read_lock(&rwlock_A);
2348 read_unlock(&rwlock_A);
2356 * <in hardirq handler>
2360 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2362 static void queued_read_lock_hardirq_inversion(void)
2372 read_lock(&rwlock_A);
2373 read_unlock(&rwlock_A);
2377 read_lock(&rwlock_A);
2378 read_unlock(&rwlock_A);
2381 static void queued_read_lock_tests(void)
2383 printk(" --------------------------------------------------------------------------\n");
2384 printk(" | queued read lock tests |\n");
2385 printk(" ---------------------------\n");
2386 print_testname("hardirq read-lock/lock-read");
2387 dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
2390 print_testname("hardirq lock-read/read-lock");
2391 dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
2394 print_testname("hardirq inversion");
2395 dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
2399 static void fs_reclaim_correct_nesting(void)
2401 fs_reclaim_acquire(GFP_KERNEL);
2402 might_alloc(GFP_NOFS);
2403 fs_reclaim_release(GFP_KERNEL);
2406 static void fs_reclaim_wrong_nesting(void)
2408 fs_reclaim_acquire(GFP_KERNEL);
2409 might_alloc(GFP_KERNEL);
2410 fs_reclaim_release(GFP_KERNEL);
2413 static void fs_reclaim_protected_nesting(void)
2417 fs_reclaim_acquire(GFP_KERNEL);
2418 flags = memalloc_nofs_save();
2419 might_alloc(GFP_KERNEL);
2420 memalloc_nofs_restore(flags);
2421 fs_reclaim_release(GFP_KERNEL);
2424 static void fs_reclaim_tests(void)
2426 printk(" --------------------\n");
2427 printk(" | fs_reclaim tests |\n");
2428 printk(" --------------------\n");
2430 print_testname("correct nesting");
2431 dotest(fs_reclaim_correct_nesting, SUCCESS, 0);
2434 print_testname("wrong nesting");
2435 dotest(fs_reclaim_wrong_nesting, FAILURE, 0);
2438 print_testname("protected nesting");
2439 dotest(fs_reclaim_protected_nesting, SUCCESS, 0);
2443 #define __guard(cleanup) __maybe_unused __attribute__((__cleanup__(cleanup)))
2445 static void hardirq_exit(int *_)
2450 #define HARDIRQ_CONTEXT(name, ...) \
2451 int hardirq_guard_##name __guard(hardirq_exit); \
2454 #define NOTTHREADED_HARDIRQ_CONTEXT(name, ...) \
2455 int notthreaded_hardirq_guard_##name __guard(hardirq_exit); \
2456 local_irq_disable(); \
2460 static void softirq_exit(int *_)
2465 #define SOFTIRQ_CONTEXT(name, ...) \
2466 int softirq_guard_##name __guard(softirq_exit); \
2469 static void rcu_exit(int *_)
2474 #define RCU_CONTEXT(name, ...) \
2475 int rcu_guard_##name __guard(rcu_exit); \
2478 static void rcu_bh_exit(int *_)
2480 rcu_read_unlock_bh();
2483 #define RCU_BH_CONTEXT(name, ...) \
2484 int rcu_bh_guard_##name __guard(rcu_bh_exit); \
2487 static void rcu_sched_exit(int *_)
2489 rcu_read_unlock_sched();
2492 #define RCU_SCHED_CONTEXT(name, ...) \
2493 int rcu_sched_guard_##name __guard(rcu_sched_exit); \
2494 rcu_read_lock_sched();
2496 static void rcu_callback_exit(int *_)
2498 rcu_lock_release(&rcu_callback_map);
2501 #define RCU_CALLBACK_CONTEXT(name, ...) \
2502 int rcu_callback_guard_##name __guard(rcu_callback_exit); \
2503 rcu_lock_acquire(&rcu_callback_map);
2506 static void raw_spinlock_exit(raw_spinlock_t **lock)
2508 raw_spin_unlock(*lock);
2511 #define RAW_SPINLOCK_CONTEXT(name, lock) \
2512 raw_spinlock_t *raw_spinlock_guard_##name __guard(raw_spinlock_exit) = &(lock); \
2513 raw_spin_lock(&(lock));
2515 static void spinlock_exit(spinlock_t **lock)
2520 #define SPINLOCK_CONTEXT(name, lock) \
2521 spinlock_t *spinlock_guard_##name __guard(spinlock_exit) = &(lock); \
2524 static void mutex_exit(struct mutex **lock)
2526 mutex_unlock(*lock);
2529 #define MUTEX_CONTEXT(name, lock) \
2530 struct mutex *mutex_guard_##name __guard(mutex_exit) = &(lock); \
2531 mutex_lock(&(lock));
2533 #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock) \
2535 static void __maybe_unused inner##_in_##outer(void) \
2537 outer##_CONTEXT(_, outer_lock); \
2539 inner##_CONTEXT(_, inner_lock); \
2544 * wait contexts (considering PREEMPT_RT)
2546 * o: inner is allowed in outer
2547 * x: inner is disallowed in outer
2549 * \ inner | RCU | RAW_SPIN | SPIN | MUTEX
2551 * ---------------+-------+----------+------+-------
2552 * HARDIRQ | o | o | o | x
2553 * ---------------+-------+----------+------+-------
2554 * NOTTHREADED_IRQ| o | o | x | x
2555 * ---------------+-------+----------+------+-------
2556 * SOFTIRQ | o | o | o | x
2557 * ---------------+-------+----------+------+-------
2558 * RCU | o | o | o | x
2559 * ---------------+-------+----------+------+-------
2560 * RCU_BH | o | o | o | x
2561 * ---------------+-------+----------+------+-------
2562 * RCU_CALLBACK | o | o | o | x
2563 * ---------------+-------+----------+------+-------
2564 * RCU_SCHED | o | o | x | x
2565 * ---------------+-------+----------+------+-------
2566 * RAW_SPIN | o | o | x | x
2567 * ---------------+-------+----------+------+-------
2568 * SPIN | o | o | o | x
2569 * ---------------+-------+----------+------+-------
2570 * MUTEX | o | o | o | o
2571 * ---------------+-------+----------+------+-------
2574 #define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock) \
2575 GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock) \
2576 GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock) \
2577 GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock) \
2578 GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock) \
2579 GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock) \
2580 GENERATE_2_CONTEXT_TESTCASE(RCU_CALLBACK, , inner, inner_lock) \
2581 GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock) \
2582 GENERATE_2_CONTEXT_TESTCASE(RAW_SPINLOCK, raw_lock_A, inner, inner_lock) \
2583 GENERATE_2_CONTEXT_TESTCASE(SPINLOCK, lock_A, inner, inner_lock) \
2584 GENERATE_2_CONTEXT_TESTCASE(MUTEX, mutex_A, inner, inner_lock)
2586 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU, )
2587 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RAW_SPINLOCK, raw_lock_B)
2588 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(SPINLOCK, lock_B)
2589 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(MUTEX, mutex_B)
2591 /* the outer context allows all kinds of preemption */
2592 #define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer) \
2593 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2594 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2595 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2596 dotest(MUTEX_in_##outer, SUCCESS, LOCKTYPE_MUTEX); \
2599 * the outer context only allows the preemption introduced by spinlock_t (which
2600 * is a sleepable lock for PREEMPT_RT)
2602 #define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer) \
2603 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2604 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2605 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2606 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2608 /* the outer doesn't allows any kind of preemption */
2609 #define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer) \
2610 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2611 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2612 dotest(SPINLOCK_in_##outer, FAILURE, LOCKTYPE_SPIN); \
2613 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2615 static void wait_context_tests(void)
2617 printk(" --------------------------------------------------------------------------\n");
2618 printk(" | wait context tests |\n");
2619 printk(" --------------------------------------------------------------------------\n");
2620 printk(" | rcu | raw | spin |mutex |\n");
2621 printk(" --------------------------------------------------------------------------\n");
2622 print_testname("in hardirq context");
2623 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ);
2626 print_testname("in hardirq context (not threaded)");
2627 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ);
2630 print_testname("in softirq context");
2631 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ);
2634 print_testname("in RCU context");
2635 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU);
2638 print_testname("in RCU-bh context");
2639 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH);
2642 print_testname("in RCU callback context");
2643 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_CALLBACK);
2646 print_testname("in RCU-sched context");
2647 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED);
2650 print_testname("in RAW_SPINLOCK context");
2651 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RAW_SPINLOCK);
2654 print_testname("in SPINLOCK context");
2655 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SPINLOCK);
2658 print_testname("in MUTEX context");
2659 DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(MUTEX);
2663 static void local_lock_2(void)
2665 local_lock_acquire(&local_A); /* IRQ-ON */
2666 local_lock_release(&local_A);
2669 spin_lock(&lock_A); /* IN-IRQ */
2670 spin_unlock(&lock_A);
2675 local_lock_acquire(&local_A); /* IN-IRQ <-> IRQ-ON cycle, false */
2676 local_lock_release(&local_A);
2677 spin_unlock(&lock_A);
2681 static void local_lock_3A(void)
2683 local_lock_acquire(&local_A); /* IRQ-ON */
2684 spin_lock(&lock_B); /* IRQ-ON */
2685 spin_unlock(&lock_B);
2686 local_lock_release(&local_A);
2689 spin_lock(&lock_A); /* IN-IRQ */
2690 spin_unlock(&lock_A);
2695 local_lock_acquire(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2696 local_lock_release(&local_A);
2697 spin_unlock(&lock_A);
2701 static void local_lock_3B(void)
2703 local_lock_acquire(&local_A); /* IRQ-ON */
2704 spin_lock(&lock_B); /* IRQ-ON */
2705 spin_unlock(&lock_B);
2706 local_lock_release(&local_A);
2709 spin_lock(&lock_A); /* IN-IRQ */
2710 spin_unlock(&lock_A);
2715 local_lock_acquire(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2716 local_lock_release(&local_A);
2717 spin_unlock(&lock_A);
2722 spin_lock(&lock_B); /* IN-IRQ <-> IRQ-ON cycle, true */
2723 spin_unlock(&lock_B);
2724 spin_unlock(&lock_A);
2729 static void local_lock_tests(void)
2731 printk(" --------------------------------------------------------------------------\n");
2732 printk(" | local_lock tests |\n");
2733 printk(" ---------------------\n");
2735 print_testname("local_lock inversion 2");
2736 dotest(local_lock_2, SUCCESS, LOCKTYPE_LL);
2739 print_testname("local_lock inversion 3A");
2740 dotest(local_lock_3A, SUCCESS, LOCKTYPE_LL);
2743 print_testname("local_lock inversion 3B");
2744 dotest(local_lock_3B, FAILURE, LOCKTYPE_LL);
2748 void locking_selftest(void)
2751 * Got a locking failure before the selftest ran?
2754 printk("----------------------------------\n");
2755 printk("| Locking API testsuite disabled |\n");
2756 printk("----------------------------------\n");
2761 * treats read_lock() as recursive read locks for testing purpose
2763 force_read_lock_recursive = 1;
2766 * Run the testsuite:
2768 printk("------------------------\n");
2769 printk("| Locking API testsuite:\n");
2770 printk("----------------------------------------------------------------------------\n");
2771 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
2772 printk(" --------------------------------------------------------------------------\n");
2774 init_shared_classes();
2775 lockdep_set_selftest_task(current);
2777 DO_TESTCASE_6R("A-A deadlock", AA);
2778 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
2779 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
2780 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
2781 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
2782 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
2783 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
2784 DO_TESTCASE_6("double unlock", double_unlock);
2785 DO_TESTCASE_6("initialize held", init_held);
2787 printk(" --------------------------------------------------------------------------\n");
2788 print_testname("recursive read-lock");
2790 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
2792 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
2795 print_testname("recursive read-lock #2");
2797 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
2799 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
2802 print_testname("mixed read-write-lock");
2804 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
2806 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
2809 print_testname("mixed write-read-lock");
2811 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
2813 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
2816 print_testname("mixed read-lock/lock-write ABBA");
2818 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2820 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2822 print_testname("mixed read-lock/lock-read ABBA");
2824 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2826 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2828 print_testname("mixed write-lock/lock-write ABBA");
2830 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2832 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2834 print_testname("chain cached mixed R-L/L-W ABBA");
2836 dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2838 DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
2839 DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
2840 DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
2841 DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
2843 printk(" --------------------------------------------------------------------------\n");
2846 * irq-context testcases:
2848 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
2849 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
2850 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
2851 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
2852 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
2853 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
2855 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
2856 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
2857 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
2861 force_read_lock_recursive = 0;
2863 * queued_read_lock() specific test cases can be put here
2865 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
2866 queued_read_lock_tests();
2870 /* Wait context test cases that are specific for RAW_LOCK_NESTING */
2871 if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING))
2872 wait_context_tests();
2876 if (unexpected_testcase_failures) {
2877 printk("-----------------------------------------------------------------\n");
2879 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2880 unexpected_testcase_failures, testcase_total);
2881 printk("-----------------------------------------------------------------\n");
2882 } else if (expected_testcase_failures && testcase_successes) {
2883 printk("--------------------------------------------------------\n");
2884 printk("%3d out of %3d testcases failed, as expected. |\n",
2885 expected_testcase_failures, testcase_total);
2886 printk("----------------------------------------------------\n");
2888 } else if (expected_testcase_failures && !testcase_successes) {
2889 printk("--------------------------------------------------------\n");
2890 printk("All %3d testcases failed, as expected. |\n",
2891 expected_testcase_failures);
2892 printk("----------------------------------------\n");
2895 printk("-------------------------------------------------------\n");
2896 printk("Good, all %3d testcases passed! |\n",
2897 testcase_successes);
2898 printk("---------------------------------\n");
2901 lockdep_set_selftest_task(NULL);
2902 debug_locks_silent = 0;