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
56 #define LOCKTYPE_SPECIAL 0x80
58 static struct ww_acquire_ctx t
, t2
;
59 static struct ww_mutex o
, o2
, o3
;
62 * Normal standalone locks, for the circular and irq-context
65 static DEFINE_SPINLOCK(lock_A
);
66 static DEFINE_SPINLOCK(lock_B
);
67 static DEFINE_SPINLOCK(lock_C
);
68 static DEFINE_SPINLOCK(lock_D
);
70 static DEFINE_RAW_SPINLOCK(raw_lock_A
);
71 static DEFINE_RAW_SPINLOCK(raw_lock_B
);
73 static DEFINE_RWLOCK(rwlock_A
);
74 static DEFINE_RWLOCK(rwlock_B
);
75 static DEFINE_RWLOCK(rwlock_C
);
76 static DEFINE_RWLOCK(rwlock_D
);
78 static DEFINE_MUTEX(mutex_A
);
79 static DEFINE_MUTEX(mutex_B
);
80 static DEFINE_MUTEX(mutex_C
);
81 static DEFINE_MUTEX(mutex_D
);
83 static DECLARE_RWSEM(rwsem_A
);
84 static DECLARE_RWSEM(rwsem_B
);
85 static DECLARE_RWSEM(rwsem_C
);
86 static DECLARE_RWSEM(rwsem_D
);
88 #ifdef CONFIG_RT_MUTEXES
90 static DEFINE_RT_MUTEX(rtmutex_A
);
91 static DEFINE_RT_MUTEX(rtmutex_B
);
92 static DEFINE_RT_MUTEX(rtmutex_C
);
93 static DEFINE_RT_MUTEX(rtmutex_D
);
98 * Locks that we initialize dynamically as well so that
99 * e.g. X1 and X2 becomes two instances of the same class,
100 * but X* and Y* are different classes. We do this so that
101 * we do not trigger a real lockup:
103 static DEFINE_SPINLOCK(lock_X1
);
104 static DEFINE_SPINLOCK(lock_X2
);
105 static DEFINE_SPINLOCK(lock_Y1
);
106 static DEFINE_SPINLOCK(lock_Y2
);
107 static DEFINE_SPINLOCK(lock_Z1
);
108 static DEFINE_SPINLOCK(lock_Z2
);
110 static DEFINE_RWLOCK(rwlock_X1
);
111 static DEFINE_RWLOCK(rwlock_X2
);
112 static DEFINE_RWLOCK(rwlock_Y1
);
113 static DEFINE_RWLOCK(rwlock_Y2
);
114 static DEFINE_RWLOCK(rwlock_Z1
);
115 static DEFINE_RWLOCK(rwlock_Z2
);
117 static DEFINE_MUTEX(mutex_X1
);
118 static DEFINE_MUTEX(mutex_X2
);
119 static DEFINE_MUTEX(mutex_Y1
);
120 static DEFINE_MUTEX(mutex_Y2
);
121 static DEFINE_MUTEX(mutex_Z1
);
122 static DEFINE_MUTEX(mutex_Z2
);
124 static DECLARE_RWSEM(rwsem_X1
);
125 static DECLARE_RWSEM(rwsem_X2
);
126 static DECLARE_RWSEM(rwsem_Y1
);
127 static DECLARE_RWSEM(rwsem_Y2
);
128 static DECLARE_RWSEM(rwsem_Z1
);
129 static DECLARE_RWSEM(rwsem_Z2
);
131 #ifdef CONFIG_RT_MUTEXES
133 static DEFINE_RT_MUTEX(rtmutex_X1
);
134 static DEFINE_RT_MUTEX(rtmutex_X2
);
135 static DEFINE_RT_MUTEX(rtmutex_Y1
);
136 static DEFINE_RT_MUTEX(rtmutex_Y2
);
137 static DEFINE_RT_MUTEX(rtmutex_Z1
);
138 static DEFINE_RT_MUTEX(rtmutex_Z2
);
142 static local_lock_t local_A
= INIT_LOCAL_LOCK(local_A
);
145 * non-inlined runtime initializers, to let separate locks share
146 * the same lock-class:
148 #define INIT_CLASS_FUNC(class) \
149 static noinline void \
150 init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \
151 struct mutex *mutex, struct rw_semaphore *rwsem)\
153 spin_lock_init(lock); \
154 rwlock_init(rwlock); \
163 static void init_shared_classes(void)
165 #ifdef CONFIG_RT_MUTEXES
166 static struct lock_class_key rt_X
, rt_Y
, rt_Z
;
168 __rt_mutex_init(&rtmutex_X1
, __func__
, &rt_X
);
169 __rt_mutex_init(&rtmutex_X2
, __func__
, &rt_X
);
170 __rt_mutex_init(&rtmutex_Y1
, __func__
, &rt_Y
);
171 __rt_mutex_init(&rtmutex_Y2
, __func__
, &rt_Y
);
172 __rt_mutex_init(&rtmutex_Z1
, __func__
, &rt_Z
);
173 __rt_mutex_init(&rtmutex_Z2
, __func__
, &rt_Z
);
176 init_class_X(&lock_X1
, &rwlock_X1
, &mutex_X1
, &rwsem_X1
);
177 init_class_X(&lock_X2
, &rwlock_X2
, &mutex_X2
, &rwsem_X2
);
179 init_class_Y(&lock_Y1
, &rwlock_Y1
, &mutex_Y1
, &rwsem_Y1
);
180 init_class_Y(&lock_Y2
, &rwlock_Y2
, &mutex_Y2
, &rwsem_Y2
);
182 init_class_Z(&lock_Z1
, &rwlock_Z1
, &mutex_Z1
, &rwsem_Z1
);
183 init_class_Z(&lock_Z2
, &rwlock_Z2
, &mutex_Z2
, &rwsem_Z2
);
187 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
188 * The following functions use a lock from a simulated hardirq/softirq
189 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
192 #define HARDIRQ_DISABLE local_irq_disable
193 #define HARDIRQ_ENABLE local_irq_enable
195 #define HARDIRQ_ENTER() \
196 local_irq_disable(); \
198 lockdep_hardirq_threaded(); \
201 #define HARDIRQ_EXIT() \
205 #define SOFTIRQ_DISABLE local_bh_disable
206 #define SOFTIRQ_ENABLE local_bh_enable
208 #define SOFTIRQ_ENTER() \
209 local_bh_disable(); \
210 local_irq_disable(); \
211 lockdep_softirq_enter(); \
212 WARN_ON(!in_softirq());
214 #define SOFTIRQ_EXIT() \
215 lockdep_softirq_exit(); \
216 local_irq_enable(); \
220 * Shortcuts for lock/unlock API variants, to keep
221 * the testcases compact:
223 #define L(x) spin_lock(&lock_##x)
224 #define U(x) spin_unlock(&lock_##x)
225 #define LU(x) L(x); U(x)
226 #define SI(x) spin_lock_init(&lock_##x)
228 #define WL(x) write_lock(&rwlock_##x)
229 #define WU(x) write_unlock(&rwlock_##x)
230 #define WLU(x) WL(x); WU(x)
232 #define RL(x) read_lock(&rwlock_##x)
233 #define RU(x) read_unlock(&rwlock_##x)
234 #define RLU(x) RL(x); RU(x)
235 #define RWI(x) rwlock_init(&rwlock_##x)
237 #define ML(x) mutex_lock(&mutex_##x)
238 #define MU(x) mutex_unlock(&mutex_##x)
239 #define MI(x) mutex_init(&mutex_##x)
241 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
242 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
243 #define RTI(x) rt_mutex_init(&rtmutex_##x)
245 #define WSL(x) down_write(&rwsem_##x)
246 #define WSU(x) up_write(&rwsem_##x)
248 #define RSL(x) down_read(&rwsem_##x)
249 #define RSU(x) up_read(&rwsem_##x)
250 #define RWSI(x) init_rwsem(&rwsem_##x)
252 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
253 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
255 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
257 #define WWAD(x) ww_acquire_done(x)
258 #define WWAF(x) ww_acquire_fini(x)
260 #define WWL(x, c) ww_mutex_lock(x, c)
261 #define WWT(x) ww_mutex_trylock(x)
262 #define WWL1(x) ww_mutex_lock(x, NULL)
263 #define WWU(x) ww_mutex_unlock(x)
266 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
269 * Generate different permutations of the same testcase, using
270 * the same basic lock-dependency/state events:
273 #define GENERATE_TESTCASE(name) \
275 static void name(void) { E(); }
277 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
279 static void name##_12(void) { E1(); E2(); } \
280 static void name##_21(void) { E2(); E1(); }
282 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
284 static void name##_123(void) { E1(); E2(); E3(); } \
285 static void name##_132(void) { E1(); E3(); E2(); } \
286 static void name##_213(void) { E2(); E1(); E3(); } \
287 static void name##_231(void) { E2(); E3(); E1(); } \
288 static void name##_312(void) { E3(); E1(); E2(); } \
289 static void name##_321(void) { E3(); E2(); E1(); }
298 LOCK(X2); /* this one should fail */
303 #include "locking-selftest-spin.h"
304 GENERATE_TESTCASE(AA_spin
)
305 #include "locking-selftest-wlock.h"
306 GENERATE_TESTCASE(AA_wlock
)
307 #include "locking-selftest-rlock.h"
308 GENERATE_TESTCASE(AA_rlock
)
309 #include "locking-selftest-mutex.h"
310 GENERATE_TESTCASE(AA_mutex
)
311 #include "locking-selftest-wsem.h"
312 GENERATE_TESTCASE(AA_wsem
)
313 #include "locking-selftest-rsem.h"
314 GENERATE_TESTCASE(AA_rsem
)
316 #ifdef CONFIG_RT_MUTEXES
317 #include "locking-selftest-rtmutex.h"
318 GENERATE_TESTCASE(AA_rtmutex
);
324 * Special-case for read-locking, they are
325 * allowed to recurse on the same lock class:
327 static void rlock_AA1(void)
330 RL(X1
); // this one should NOT fail
333 static void rlock_AA1B(void)
336 RL(X2
); // this one should NOT fail
339 static void rsem_AA1(void)
342 RSL(X1
); // this one should fail
345 static void rsem_AA1B(void)
348 RSL(X2
); // this one should fail
351 * The mixing of read and write locks is not allowed:
353 static void rlock_AA2(void)
356 WL(X2
); // this one should fail
359 static void rsem_AA2(void)
362 WSL(X2
); // this one should fail
365 static void rlock_AA3(void)
368 RL(X2
); // this one should fail
371 static void rsem_AA3(void)
374 RSL(X2
); // this one should fail
383 static void rlock_ABBA1(void)
393 U(Y1
); // should fail
396 static void rwsem_ABBA1(void)
406 MU(Y1
); // should fail
415 * This test case is aimed at poking whether the chain cache prevents us from
416 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
417 * read/write locks, the following case may happen
419 * { read_lock(A)->lock(B) dependency exists }
425 * { Not a deadlock, B -> A is added in the chain cache }
431 * { B->A found in chain cache, not reported as a deadlock }
434 static void rlock_chaincache_ABBA1(void)
449 U(Y1
); // should fail
458 static void rlock_ABBA2(void)
468 U(Y1
); // should NOT fail
471 static void rwsem_ABBA2(void)
481 MU(Y1
); // should fail
491 static void rlock_ABBA3(void)
501 U(Y1
); // should fail
504 static void rwsem_ABBA3(void)
514 MU(Y1
); // should fail
523 LOCK_UNLOCK_2(A, B); \
524 LOCK_UNLOCK_2(B, A); /* fail */
529 #include "locking-selftest-spin.h"
530 GENERATE_TESTCASE(ABBA_spin
)
531 #include "locking-selftest-wlock.h"
532 GENERATE_TESTCASE(ABBA_wlock
)
533 #include "locking-selftest-rlock.h"
534 GENERATE_TESTCASE(ABBA_rlock
)
535 #include "locking-selftest-mutex.h"
536 GENERATE_TESTCASE(ABBA_mutex
)
537 #include "locking-selftest-wsem.h"
538 GENERATE_TESTCASE(ABBA_wsem
)
539 #include "locking-selftest-rsem.h"
540 GENERATE_TESTCASE(ABBA_rsem
)
542 #ifdef CONFIG_RT_MUTEXES
543 #include "locking-selftest-rtmutex.h"
544 GENERATE_TESTCASE(ABBA_rtmutex
);
555 LOCK_UNLOCK_2(A, B); \
556 LOCK_UNLOCK_2(B, C); \
557 LOCK_UNLOCK_2(C, A); /* fail */
562 #include "locking-selftest-spin.h"
563 GENERATE_TESTCASE(ABBCCA_spin
)
564 #include "locking-selftest-wlock.h"
565 GENERATE_TESTCASE(ABBCCA_wlock
)
566 #include "locking-selftest-rlock.h"
567 GENERATE_TESTCASE(ABBCCA_rlock
)
568 #include "locking-selftest-mutex.h"
569 GENERATE_TESTCASE(ABBCCA_mutex
)
570 #include "locking-selftest-wsem.h"
571 GENERATE_TESTCASE(ABBCCA_wsem
)
572 #include "locking-selftest-rsem.h"
573 GENERATE_TESTCASE(ABBCCA_rsem
)
575 #ifdef CONFIG_RT_MUTEXES
576 #include "locking-selftest-rtmutex.h"
577 GENERATE_TESTCASE(ABBCCA_rtmutex
);
588 LOCK_UNLOCK_2(A, B); \
589 LOCK_UNLOCK_2(C, A); \
590 LOCK_UNLOCK_2(B, C); /* fail */
595 #include "locking-selftest-spin.h"
596 GENERATE_TESTCASE(ABCABC_spin
)
597 #include "locking-selftest-wlock.h"
598 GENERATE_TESTCASE(ABCABC_wlock
)
599 #include "locking-selftest-rlock.h"
600 GENERATE_TESTCASE(ABCABC_rlock
)
601 #include "locking-selftest-mutex.h"
602 GENERATE_TESTCASE(ABCABC_mutex
)
603 #include "locking-selftest-wsem.h"
604 GENERATE_TESTCASE(ABCABC_wsem
)
605 #include "locking-selftest-rsem.h"
606 GENERATE_TESTCASE(ABCABC_rsem
)
608 #ifdef CONFIG_RT_MUTEXES
609 #include "locking-selftest-rtmutex.h"
610 GENERATE_TESTCASE(ABCABC_rtmutex
);
616 * AB BC CD DA deadlock:
621 LOCK_UNLOCK_2(A, B); \
622 LOCK_UNLOCK_2(B, C); \
623 LOCK_UNLOCK_2(C, D); \
624 LOCK_UNLOCK_2(D, A); /* fail */
629 #include "locking-selftest-spin.h"
630 GENERATE_TESTCASE(ABBCCDDA_spin
)
631 #include "locking-selftest-wlock.h"
632 GENERATE_TESTCASE(ABBCCDDA_wlock
)
633 #include "locking-selftest-rlock.h"
634 GENERATE_TESTCASE(ABBCCDDA_rlock
)
635 #include "locking-selftest-mutex.h"
636 GENERATE_TESTCASE(ABBCCDDA_mutex
)
637 #include "locking-selftest-wsem.h"
638 GENERATE_TESTCASE(ABBCCDDA_wsem
)
639 #include "locking-selftest-rsem.h"
640 GENERATE_TESTCASE(ABBCCDDA_rsem
)
642 #ifdef CONFIG_RT_MUTEXES
643 #include "locking-selftest-rtmutex.h"
644 GENERATE_TESTCASE(ABBCCDDA_rtmutex
);
650 * AB CD BD DA deadlock:
654 LOCK_UNLOCK_2(A, B); \
655 LOCK_UNLOCK_2(C, D); \
656 LOCK_UNLOCK_2(B, D); \
657 LOCK_UNLOCK_2(D, A); /* fail */
662 #include "locking-selftest-spin.h"
663 GENERATE_TESTCASE(ABCDBDDA_spin
)
664 #include "locking-selftest-wlock.h"
665 GENERATE_TESTCASE(ABCDBDDA_wlock
)
666 #include "locking-selftest-rlock.h"
667 GENERATE_TESTCASE(ABCDBDDA_rlock
)
668 #include "locking-selftest-mutex.h"
669 GENERATE_TESTCASE(ABCDBDDA_mutex
)
670 #include "locking-selftest-wsem.h"
671 GENERATE_TESTCASE(ABCDBDDA_wsem
)
672 #include "locking-selftest-rsem.h"
673 GENERATE_TESTCASE(ABCDBDDA_rsem
)
675 #ifdef CONFIG_RT_MUTEXES
676 #include "locking-selftest-rtmutex.h"
677 GENERATE_TESTCASE(ABCDBDDA_rtmutex
);
683 * AB CD BC DA deadlock:
687 LOCK_UNLOCK_2(A, B); \
688 LOCK_UNLOCK_2(C, D); \
689 LOCK_UNLOCK_2(B, C); \
690 LOCK_UNLOCK_2(D, A); /* fail */
695 #include "locking-selftest-spin.h"
696 GENERATE_TESTCASE(ABCDBCDA_spin
)
697 #include "locking-selftest-wlock.h"
698 GENERATE_TESTCASE(ABCDBCDA_wlock
)
699 #include "locking-selftest-rlock.h"
700 GENERATE_TESTCASE(ABCDBCDA_rlock
)
701 #include "locking-selftest-mutex.h"
702 GENERATE_TESTCASE(ABCDBCDA_mutex
)
703 #include "locking-selftest-wsem.h"
704 GENERATE_TESTCASE(ABCDBCDA_wsem
)
705 #include "locking-selftest-rsem.h"
706 GENERATE_TESTCASE(ABCDBCDA_rsem
)
708 #ifdef CONFIG_RT_MUTEXES
709 #include "locking-selftest-rtmutex.h"
710 GENERATE_TESTCASE(ABCDBCDA_rtmutex
);
722 UNLOCK(A); /* fail */
727 #include "locking-selftest-spin.h"
728 GENERATE_TESTCASE(double_unlock_spin
)
729 #include "locking-selftest-wlock.h"
730 GENERATE_TESTCASE(double_unlock_wlock
)
731 #include "locking-selftest-rlock.h"
732 GENERATE_TESTCASE(double_unlock_rlock
)
733 #include "locking-selftest-mutex.h"
734 GENERATE_TESTCASE(double_unlock_mutex
)
735 #include "locking-selftest-wsem.h"
736 GENERATE_TESTCASE(double_unlock_wsem
)
737 #include "locking-selftest-rsem.h"
738 GENERATE_TESTCASE(double_unlock_rsem
)
740 #ifdef CONFIG_RT_MUTEXES
741 #include "locking-selftest-rtmutex.h"
742 GENERATE_TESTCASE(double_unlock_rtmutex
);
748 * initializing a held lock:
758 #include "locking-selftest-spin.h"
759 GENERATE_TESTCASE(init_held_spin
)
760 #include "locking-selftest-wlock.h"
761 GENERATE_TESTCASE(init_held_wlock
)
762 #include "locking-selftest-rlock.h"
763 GENERATE_TESTCASE(init_held_rlock
)
764 #include "locking-selftest-mutex.h"
765 GENERATE_TESTCASE(init_held_mutex
)
766 #include "locking-selftest-wsem.h"
767 GENERATE_TESTCASE(init_held_wsem
)
768 #include "locking-selftest-rsem.h"
769 GENERATE_TESTCASE(init_held_rsem
)
771 #ifdef CONFIG_RT_MUTEXES
772 #include "locking-selftest-rtmutex.h"
773 GENERATE_TESTCASE(init_held_rtmutex
);
779 * locking an irq-safe lock with irqs enabled:
794 * Generate 24 testcases:
796 #include "locking-selftest-spin-hardirq.h"
797 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin
)
799 #include "locking-selftest-rlock-hardirq.h"
800 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock
)
802 #include "locking-selftest-wlock-hardirq.h"
803 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock
)
805 #include "locking-selftest-spin-softirq.h"
806 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin
)
808 #include "locking-selftest-rlock-softirq.h"
809 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock
)
811 #include "locking-selftest-wlock-softirq.h"
812 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock
)
818 * Enabling hardirqs with a softirq-safe lock held:
835 * Generate 12 testcases:
837 #include "locking-selftest-spin.h"
838 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin
)
840 #include "locking-selftest-wlock.h"
841 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock
)
843 #include "locking-selftest-rlock.h"
844 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock
)
850 * Enabling irqs with an irq-safe lock held:
867 * Generate 24 testcases:
869 #include "locking-selftest-spin-hardirq.h"
870 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin
)
872 #include "locking-selftest-rlock-hardirq.h"
873 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock
)
875 #include "locking-selftest-wlock-hardirq.h"
876 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock
)
878 #include "locking-selftest-spin-softirq.h"
879 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin
)
881 #include "locking-selftest-rlock-softirq.h"
882 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock
)
884 #include "locking-selftest-wlock-softirq.h"
885 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock
)
891 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
913 * Generate 36 testcases:
915 #include "locking-selftest-spin-hardirq.h"
916 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin
)
918 #include "locking-selftest-rlock-hardirq.h"
919 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock
)
921 #include "locking-selftest-wlock-hardirq.h"
922 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock
)
924 #include "locking-selftest-spin-softirq.h"
925 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin
)
927 #include "locking-selftest-rlock-softirq.h"
928 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock
)
930 #include "locking-selftest-wlock-softirq.h"
931 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock
)
938 * If a lock turns into softirq-safe, but earlier it took
939 * a softirq-unsafe lock:
961 * Generate 36 testcases:
963 #include "locking-selftest-spin-hardirq.h"
964 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin
)
966 #include "locking-selftest-rlock-hardirq.h"
967 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock
)
969 #include "locking-selftest-wlock-hardirq.h"
970 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock
)
972 #include "locking-selftest-spin-softirq.h"
973 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin
)
975 #include "locking-selftest-rlock-softirq.h"
976 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock
)
978 #include "locking-selftest-wlock-softirq.h"
979 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock
)
986 * read-lock / write-lock irq inversion.
990 * CPU#1 is at #1, i.e. it has write-locked A, but has not
993 * CPU#2 is at #2, i.e. it has locked B.
995 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
997 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
1023 * Generate 36 testcases:
1025 #include "locking-selftest-spin-hardirq.h"
1026 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin
)
1028 #include "locking-selftest-rlock-hardirq.h"
1029 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock
)
1031 #include "locking-selftest-wlock-hardirq.h"
1032 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock
)
1034 #include "locking-selftest-spin-softirq.h"
1035 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin
)
1037 #include "locking-selftest-rlock-softirq.h"
1038 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock
)
1040 #include "locking-selftest-wlock-softirq.h"
1041 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock
)
1048 * write-read / write-read / write-read deadlock even if read is recursive
1072 #include "locking-selftest-rlock.h"
1073 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1
)
1080 * write-write / read-read / write-read deadlock even if read is recursive
1104 #include "locking-selftest-rlock.h"
1105 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1
)
1112 * write-write / read-read / read-write is not deadlock when read is recursive
1136 #include "locking-selftest-rlock.h"
1137 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1
)
1144 * write-read / read-read / write-write is not deadlock when read is recursive
1168 #include "locking-selftest-rlock.h"
1169 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1
)
1175 * read-lock / write-lock recursion that is actually safe.
1200 * Generate 24 testcases:
1202 #include "locking-selftest-hardirq.h"
1203 #include "locking-selftest-rlock.h"
1204 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock
)
1206 #include "locking-selftest-wlock.h"
1207 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock
)
1209 #include "locking-selftest-softirq.h"
1210 #include "locking-selftest-rlock.h"
1211 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock
)
1213 #include "locking-selftest-wlock.h"
1214 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock
)
1221 * read-lock / write-lock recursion that is unsafe.
1246 * Generate 24 testcases:
1248 #include "locking-selftest-hardirq.h"
1249 #include "locking-selftest-rlock.h"
1250 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock
)
1252 #include "locking-selftest-wlock.h"
1253 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock
)
1255 #include "locking-selftest-softirq.h"
1256 #include "locking-selftest-rlock.h"
1257 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock
)
1259 #include "locking-selftest-wlock.h"
1260 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock
)
1266 * read-lock / write-lock recursion that is unsafe.
1268 * A is a ENABLED_*_READ lock
1269 * B is a USED_IN_*_READ lock
1275 * write_lock(A); // if this one is read_lock(), no deadlock
1300 * Generate 24 testcases:
1302 #include "locking-selftest-hardirq.h"
1303 #include "locking-selftest-rlock.h"
1304 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock
)
1306 #include "locking-selftest-wlock.h"
1307 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock
)
1309 #include "locking-selftest-softirq.h"
1310 #include "locking-selftest-rlock.h"
1311 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock
)
1313 #include "locking-selftest-wlock.h"
1314 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock
)
1316 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1317 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1318 # define I_RAW_SPINLOCK(x) lockdep_reset_lock(&raw_lock_##x.dep_map)
1319 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1320 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1321 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1322 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1323 # define I_LOCAL_LOCK(x) lockdep_reset_lock(&local_##x.dep_map)
1324 #ifdef CONFIG_RT_MUTEXES
1325 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1328 # define I_SPINLOCK(x)
1329 # define I_RAW_SPINLOCK(x)
1330 # define I_RWLOCK(x)
1334 # define I_LOCAL_LOCK(x)
1338 # define I_RTMUTEX(x)
1341 #ifdef CONFIG_RT_MUTEXES
1342 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1344 #define I2_RTMUTEX(x)
1358 spin_lock_init(&lock_##x); \
1359 rwlock_init(&rwlock_##x); \
1360 mutex_init(&mutex_##x); \
1361 init_rwsem(&rwsem_##x); \
1365 static void reset_locks(void)
1367 local_irq_disable();
1368 lockdep_free_key_range(&ww_lockdep
.acquire_key
, 1);
1369 lockdep_free_key_range(&ww_lockdep
.mutex_key
, 1);
1371 I1(A
); I1(B
); I1(C
); I1(D
);
1372 I1(X1
); I1(X2
); I1(Y1
); I1(Y2
); I1(Z1
); I1(Z2
);
1373 I_WW(t
); I_WW(t2
); I_WW(o
.base
); I_WW(o2
.base
); I_WW(o3
.base
);
1374 I_RAW_SPINLOCK(A
); I_RAW_SPINLOCK(B
);
1379 I2(A
); I2(B
); I2(C
); I2(D
);
1380 init_shared_classes();
1381 raw_spin_lock_init(&raw_lock_A
);
1382 raw_spin_lock_init(&raw_lock_B
);
1383 local_lock_init(&local_A
);
1385 ww_mutex_init(&o
, &ww_lockdep
); ww_mutex_init(&o2
, &ww_lockdep
); ww_mutex_init(&o3
, &ww_lockdep
);
1386 memset(&t
, 0, sizeof(t
)); memset(&t2
, 0, sizeof(t2
));
1387 memset(&ww_lockdep
.acquire_key
, 0, sizeof(ww_lockdep
.acquire_key
));
1388 memset(&ww_lockdep
.mutex_key
, 0, sizeof(ww_lockdep
.mutex_key
));
1394 static int testcase_total
;
1395 static int testcase_successes
;
1396 static int expected_testcase_failures
;
1397 static int unexpected_testcase_failures
;
1399 static void dotest(void (*testcase_fn
)(void), int expected
, int lockclass_mask
)
1401 unsigned long saved_preempt_count
= preempt_count();
1403 WARN_ON(irqs_disabled());
1405 debug_locks_silent
= !(debug_locks_verbose
& lockclass_mask
);
1409 * Filter out expected failures:
1411 #ifndef CONFIG_PROVE_LOCKING
1412 if (expected
== FAILURE
&& debug_locks
) {
1413 expected_testcase_failures
++;
1418 if (debug_locks
!= expected
) {
1419 unexpected_testcase_failures
++;
1422 testcase_successes
++;
1427 if (debug_locks_verbose
& lockclass_mask
)
1428 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1429 lockclass_mask
, debug_locks
, expected
);
1431 * Some tests (e.g. double-unlock) might corrupt the preemption
1432 * count, so restore it:
1434 preempt_count_set(saved_preempt_count
);
1435 #ifdef CONFIG_TRACE_IRQFLAGS
1436 if (softirq_count())
1437 current
->softirqs_enabled
= 0;
1439 current
->softirqs_enabled
= 1;
1445 #ifdef CONFIG_RT_MUTEXES
1446 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1448 #define dotest_rt(fn, e, m)
1451 static inline void print_testname(const char *testname
)
1453 printk("%33s:", testname
);
1456 #define DO_TESTCASE_1(desc, name, nr) \
1457 print_testname(desc"/"#nr); \
1458 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1461 #define DO_TESTCASE_1B(desc, name, nr) \
1462 print_testname(desc"/"#nr); \
1463 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1466 #define DO_TESTCASE_1RR(desc, name, nr) \
1467 print_testname(desc"/"#nr); \
1469 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1472 #define DO_TESTCASE_1RRB(desc, name, nr) \
1473 print_testname(desc"/"#nr); \
1475 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1479 #define DO_TESTCASE_3(desc, name, nr) \
1480 print_testname(desc"/"#nr); \
1481 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1482 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1483 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1486 #define DO_TESTCASE_3RW(desc, name, nr) \
1487 print_testname(desc"/"#nr); \
1488 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1489 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1490 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1493 #define DO_TESTCASE_2RW(desc, name, nr) \
1494 print_testname(desc"/"#nr); \
1496 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1497 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1500 #define DO_TESTCASE_2x2RW(desc, name, nr) \
1501 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
1502 DO_TESTCASE_2RW("soft-"desc, name##_soft, nr) \
1504 #define DO_TESTCASE_6x2x2RW(desc, name) \
1505 DO_TESTCASE_2x2RW(desc, name, 123); \
1506 DO_TESTCASE_2x2RW(desc, name, 132); \
1507 DO_TESTCASE_2x2RW(desc, name, 213); \
1508 DO_TESTCASE_2x2RW(desc, name, 231); \
1509 DO_TESTCASE_2x2RW(desc, name, 312); \
1510 DO_TESTCASE_2x2RW(desc, name, 321);
1512 #define DO_TESTCASE_6(desc, name) \
1513 print_testname(desc); \
1514 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1515 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1516 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1517 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1518 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1519 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1520 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1523 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1524 print_testname(desc); \
1525 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1526 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1527 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1528 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1529 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1530 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1531 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1535 * 'read' variant: rlocks must not trigger.
1537 #define DO_TESTCASE_6R(desc, name) \
1538 print_testname(desc); \
1539 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1540 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1541 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1542 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1543 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1544 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1545 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1548 #define DO_TESTCASE_2I(desc, name, nr) \
1549 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1550 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1552 #define DO_TESTCASE_2IB(desc, name, nr) \
1553 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1554 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1556 #define DO_TESTCASE_6I(desc, name, nr) \
1557 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1558 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1560 #define DO_TESTCASE_6IRW(desc, name, nr) \
1561 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1562 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1564 #define DO_TESTCASE_2x3(desc, name) \
1565 DO_TESTCASE_3(desc, name, 12); \
1566 DO_TESTCASE_3(desc, name, 21);
1568 #define DO_TESTCASE_2x6(desc, name) \
1569 DO_TESTCASE_6I(desc, name, 12); \
1570 DO_TESTCASE_6I(desc, name, 21);
1572 #define DO_TESTCASE_6x2(desc, name) \
1573 DO_TESTCASE_2I(desc, name, 123); \
1574 DO_TESTCASE_2I(desc, name, 132); \
1575 DO_TESTCASE_2I(desc, name, 213); \
1576 DO_TESTCASE_2I(desc, name, 231); \
1577 DO_TESTCASE_2I(desc, name, 312); \
1578 DO_TESTCASE_2I(desc, name, 321);
1580 #define DO_TESTCASE_6x2B(desc, name) \
1581 DO_TESTCASE_2IB(desc, name, 123); \
1582 DO_TESTCASE_2IB(desc, name, 132); \
1583 DO_TESTCASE_2IB(desc, name, 213); \
1584 DO_TESTCASE_2IB(desc, name, 231); \
1585 DO_TESTCASE_2IB(desc, name, 312); \
1586 DO_TESTCASE_2IB(desc, name, 321);
1588 #define DO_TESTCASE_6x1RR(desc, name) \
1589 DO_TESTCASE_1RR(desc, name, 123); \
1590 DO_TESTCASE_1RR(desc, name, 132); \
1591 DO_TESTCASE_1RR(desc, name, 213); \
1592 DO_TESTCASE_1RR(desc, name, 231); \
1593 DO_TESTCASE_1RR(desc, name, 312); \
1594 DO_TESTCASE_1RR(desc, name, 321);
1596 #define DO_TESTCASE_6x1RRB(desc, name) \
1597 DO_TESTCASE_1RRB(desc, name, 123); \
1598 DO_TESTCASE_1RRB(desc, name, 132); \
1599 DO_TESTCASE_1RRB(desc, name, 213); \
1600 DO_TESTCASE_1RRB(desc, name, 231); \
1601 DO_TESTCASE_1RRB(desc, name, 312); \
1602 DO_TESTCASE_1RRB(desc, name, 321);
1604 #define DO_TESTCASE_6x6(desc, name) \
1605 DO_TESTCASE_6I(desc, name, 123); \
1606 DO_TESTCASE_6I(desc, name, 132); \
1607 DO_TESTCASE_6I(desc, name, 213); \
1608 DO_TESTCASE_6I(desc, name, 231); \
1609 DO_TESTCASE_6I(desc, name, 312); \
1610 DO_TESTCASE_6I(desc, name, 321);
1612 #define DO_TESTCASE_6x6RW(desc, name) \
1613 DO_TESTCASE_6IRW(desc, name, 123); \
1614 DO_TESTCASE_6IRW(desc, name, 132); \
1615 DO_TESTCASE_6IRW(desc, name, 213); \
1616 DO_TESTCASE_6IRW(desc, name, 231); \
1617 DO_TESTCASE_6IRW(desc, name, 312); \
1618 DO_TESTCASE_6IRW(desc, name, 321);
1620 static void ww_test_fail_acquire(void)
1629 if (WARN_ON(!o
.ctx
) ||
1633 /* No lockdep test, pure API */
1635 WARN_ON(ret
!= -EALREADY
);
1643 WARN_ON(ret
!= -EDEADLK
);
1648 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1650 DEBUG_LOCKS_WARN_ON(1);
1654 static void ww_test_normal(void)
1661 * None of the ww_mutex codepaths should be taken in the 'normal'
1662 * mutex calls. The easiest way to verify this is by using the
1663 * normal mutex calls, and making sure o.ctx is unmodified.
1666 /* mutex_lock (and indirectly, mutex_lock_nested) */
1667 o
.ctx
= (void *)~0UL;
1668 mutex_lock(&o
.base
);
1669 mutex_unlock(&o
.base
);
1670 WARN_ON(o
.ctx
!= (void *)~0UL);
1672 /* mutex_lock_interruptible (and *_nested) */
1673 o
.ctx
= (void *)~0UL;
1674 ret
= mutex_lock_interruptible(&o
.base
);
1676 mutex_unlock(&o
.base
);
1679 WARN_ON(o
.ctx
!= (void *)~0UL);
1681 /* mutex_lock_killable (and *_nested) */
1682 o
.ctx
= (void *)~0UL;
1683 ret
= mutex_lock_killable(&o
.base
);
1685 mutex_unlock(&o
.base
);
1688 WARN_ON(o
.ctx
!= (void *)~0UL);
1690 /* trylock, succeeding */
1691 o
.ctx
= (void *)~0UL;
1692 ret
= mutex_trylock(&o
.base
);
1695 mutex_unlock(&o
.base
);
1698 WARN_ON(o
.ctx
!= (void *)~0UL);
1700 /* trylock, failing */
1701 o
.ctx
= (void *)~0UL;
1702 mutex_lock(&o
.base
);
1703 ret
= mutex_trylock(&o
.base
);
1705 mutex_unlock(&o
.base
);
1706 WARN_ON(o
.ctx
!= (void *)~0UL);
1709 o
.ctx
= (void *)~0UL;
1710 mutex_lock_nest_lock(&o
.base
, &t
);
1711 mutex_unlock(&o
.base
);
1712 WARN_ON(o
.ctx
!= (void *)~0UL);
1715 static void ww_test_two_contexts(void)
1721 static void ww_test_diff_class(void)
1724 #ifdef CONFIG_DEBUG_MUTEXES
1730 static void ww_test_context_done_twice(void)
1738 static void ww_test_context_unlock_twice(void)
1746 static void ww_test_context_fini_early(void)
1754 static void ww_test_context_lock_after_done(void)
1761 static void ww_test_object_unlock_twice(void)
1768 static void ww_test_object_lock_unbalanced(void)
1777 static void ww_test_object_lock_stale_context(void)
1784 static void ww_test_edeadlk_normal(void)
1788 mutex_lock(&o2
.base
);
1790 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1800 WARN_ON(ret
!= -EDEADLK
);
1803 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1804 mutex_unlock(&o2
.base
);
1810 static void ww_test_edeadlk_normal_slow(void)
1814 mutex_lock(&o2
.base
);
1815 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1826 WARN_ON(ret
!= -EDEADLK
);
1829 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1830 mutex_unlock(&o2
.base
);
1833 ww_mutex_lock_slow(&o2
, &t
);
1836 static void ww_test_edeadlk_no_unlock(void)
1840 mutex_lock(&o2
.base
);
1842 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1852 WARN_ON(ret
!= -EDEADLK
);
1855 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1856 mutex_unlock(&o2
.base
);
1861 static void ww_test_edeadlk_no_unlock_slow(void)
1865 mutex_lock(&o2
.base
);
1866 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1877 WARN_ON(ret
!= -EDEADLK
);
1880 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1881 mutex_unlock(&o2
.base
);
1883 ww_mutex_lock_slow(&o2
, &t
);
1886 static void ww_test_edeadlk_acquire_more(void)
1890 mutex_lock(&o2
.base
);
1891 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1902 WARN_ON(ret
!= -EDEADLK
);
1907 static void ww_test_edeadlk_acquire_more_slow(void)
1911 mutex_lock(&o2
.base
);
1912 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1923 WARN_ON(ret
!= -EDEADLK
);
1925 ww_mutex_lock_slow(&o3
, &t
);
1928 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1932 mutex_lock(&o2
.base
);
1933 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1936 mutex_lock(&o3
.base
);
1937 mutex_release(&o3
.base
.dep_map
, _THIS_IP_
);
1948 WARN_ON(ret
!= -EDEADLK
);
1951 WARN_ON(ret
!= -EDEADLK
);
1954 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1958 mutex_lock(&o2
.base
);
1959 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1962 mutex_lock(&o3
.base
);
1963 mutex_release(&o3
.base
.dep_map
, _THIS_IP_
);
1974 WARN_ON(ret
!= -EDEADLK
);
1976 ww_mutex_lock_slow(&o3
, &t
);
1979 static void ww_test_edeadlk_acquire_wrong(void)
1983 mutex_lock(&o2
.base
);
1984 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1995 WARN_ON(ret
!= -EDEADLK
);
2004 static void ww_test_edeadlk_acquire_wrong_slow(void)
2008 mutex_lock(&o2
.base
);
2009 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
2020 WARN_ON(ret
!= -EDEADLK
);
2026 ww_mutex_lock_slow(&o3
, &t
);
2029 static void ww_test_spin_nest_unlocked(void)
2031 spin_lock_nest_lock(&lock_A
, &o
.base
);
2035 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2036 static void ww_test_spin_nest_lock(void)
2038 spin_lock(&lock_X1
);
2039 spin_lock_nest_lock(&lock_Y1
, &lock_X1
);
2041 spin_lock_nest_lock(&lock_Y2
, &lock_X1
);
2042 spin_unlock(&lock_A
);
2043 spin_unlock(&lock_Y2
);
2044 spin_unlock(&lock_Y1
);
2045 spin_unlock(&lock_X1
);
2048 static void ww_test_unneeded_slow(void)
2052 ww_mutex_lock_slow(&o
, &t
);
2055 static void ww_test_context_block(void)
2066 static void ww_test_context_try(void)
2081 static void ww_test_context_context(void)
2097 static void ww_test_try_block(void)
2109 static void ww_test_try_try(void)
2121 static void ww_test_try_context(void)
2134 static void ww_test_block_block(void)
2140 static void ww_test_block_try(void)
2149 static void ww_test_block_context(void)
2160 static void ww_test_spin_block(void)
2176 static void ww_test_spin_try(void)
2196 static void ww_test_spin_context(void)
2218 static void ww_tests(void)
2220 printk(" --------------------------------------------------------------------------\n");
2221 printk(" | Wound/wait tests |\n");
2222 printk(" ---------------------\n");
2224 print_testname("ww api failures");
2225 dotest(ww_test_fail_acquire
, SUCCESS
, LOCKTYPE_WW
);
2226 dotest(ww_test_normal
, SUCCESS
, LOCKTYPE_WW
);
2227 dotest(ww_test_unneeded_slow
, FAILURE
, LOCKTYPE_WW
);
2230 print_testname("ww contexts mixing");
2231 dotest(ww_test_two_contexts
, FAILURE
, LOCKTYPE_WW
);
2232 dotest(ww_test_diff_class
, FAILURE
, LOCKTYPE_WW
);
2235 print_testname("finishing ww context");
2236 dotest(ww_test_context_done_twice
, FAILURE
, LOCKTYPE_WW
);
2237 dotest(ww_test_context_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
2238 dotest(ww_test_context_fini_early
, FAILURE
, LOCKTYPE_WW
);
2239 dotest(ww_test_context_lock_after_done
, FAILURE
, LOCKTYPE_WW
);
2242 print_testname("locking mismatches");
2243 dotest(ww_test_object_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
2244 dotest(ww_test_object_lock_unbalanced
, FAILURE
, LOCKTYPE_WW
);
2245 dotest(ww_test_object_lock_stale_context
, FAILURE
, LOCKTYPE_WW
);
2248 print_testname("EDEADLK handling");
2249 dotest(ww_test_edeadlk_normal
, SUCCESS
, LOCKTYPE_WW
);
2250 dotest(ww_test_edeadlk_normal_slow
, SUCCESS
, LOCKTYPE_WW
);
2251 dotest(ww_test_edeadlk_no_unlock
, FAILURE
, LOCKTYPE_WW
);
2252 dotest(ww_test_edeadlk_no_unlock_slow
, FAILURE
, LOCKTYPE_WW
);
2253 dotest(ww_test_edeadlk_acquire_more
, FAILURE
, LOCKTYPE_WW
);
2254 dotest(ww_test_edeadlk_acquire_more_slow
, FAILURE
, LOCKTYPE_WW
);
2255 dotest(ww_test_edeadlk_acquire_more_edeadlk
, FAILURE
, LOCKTYPE_WW
);
2256 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow
, FAILURE
, LOCKTYPE_WW
);
2257 dotest(ww_test_edeadlk_acquire_wrong
, FAILURE
, LOCKTYPE_WW
);
2258 dotest(ww_test_edeadlk_acquire_wrong_slow
, FAILURE
, LOCKTYPE_WW
);
2261 print_testname("spinlock nest unlocked");
2262 dotest(ww_test_spin_nest_unlocked
, FAILURE
, LOCKTYPE_WW
);
2265 print_testname("spinlock nest test");
2266 dotest(ww_test_spin_nest_lock
, SUCCESS
, LOCKTYPE_WW
);
2269 printk(" -----------------------------------------------------\n");
2270 printk(" |block | try |context|\n");
2271 printk(" -----------------------------------------------------\n");
2273 print_testname("context");
2274 dotest(ww_test_context_block
, FAILURE
, LOCKTYPE_WW
);
2275 dotest(ww_test_context_try
, SUCCESS
, LOCKTYPE_WW
);
2276 dotest(ww_test_context_context
, SUCCESS
, LOCKTYPE_WW
);
2279 print_testname("try");
2280 dotest(ww_test_try_block
, FAILURE
, LOCKTYPE_WW
);
2281 dotest(ww_test_try_try
, SUCCESS
, LOCKTYPE_WW
);
2282 dotest(ww_test_try_context
, FAILURE
, LOCKTYPE_WW
);
2285 print_testname("block");
2286 dotest(ww_test_block_block
, FAILURE
, LOCKTYPE_WW
);
2287 dotest(ww_test_block_try
, SUCCESS
, LOCKTYPE_WW
);
2288 dotest(ww_test_block_context
, FAILURE
, LOCKTYPE_WW
);
2291 print_testname("spinlock");
2292 dotest(ww_test_spin_block
, FAILURE
, LOCKTYPE_WW
);
2293 dotest(ww_test_spin_try
, SUCCESS
, LOCKTYPE_WW
);
2294 dotest(ww_test_spin_context
, FAILURE
, LOCKTYPE_WW
);
2300 * <in hardirq handler>
2309 static void queued_read_lock_hardirq_RE_Er(void)
2312 read_lock(&rwlock_A
);
2315 read_unlock(&rwlock_A
);
2320 read_lock(&rwlock_A
);
2321 read_unlock(&rwlock_A
);
2327 * <in hardirq handler>
2334 * is not a deadlock.
2336 static void queued_read_lock_hardirq_ER_rE(void)
2340 read_lock(&rwlock_A
);
2341 read_unlock(&rwlock_A
);
2346 read_lock(&rwlock_A
);
2349 read_unlock(&rwlock_A
);
2357 * <in hardirq handler>
2361 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2363 static void queued_read_lock_hardirq_inversion(void)
2373 read_lock(&rwlock_A
);
2374 read_unlock(&rwlock_A
);
2378 read_lock(&rwlock_A
);
2379 read_unlock(&rwlock_A
);
2382 static void queued_read_lock_tests(void)
2384 printk(" --------------------------------------------------------------------------\n");
2385 printk(" | queued read lock tests |\n");
2386 printk(" ---------------------------\n");
2387 print_testname("hardirq read-lock/lock-read");
2388 dotest(queued_read_lock_hardirq_RE_Er
, FAILURE
, LOCKTYPE_RWLOCK
);
2391 print_testname("hardirq lock-read/read-lock");
2392 dotest(queued_read_lock_hardirq_ER_rE
, SUCCESS
, LOCKTYPE_RWLOCK
);
2395 print_testname("hardirq inversion");
2396 dotest(queued_read_lock_hardirq_inversion
, FAILURE
, LOCKTYPE_RWLOCK
);
2400 static void fs_reclaim_correct_nesting(void)
2402 fs_reclaim_acquire(GFP_KERNEL
);
2403 might_alloc(GFP_NOFS
);
2404 fs_reclaim_release(GFP_KERNEL
);
2407 static void fs_reclaim_wrong_nesting(void)
2409 fs_reclaim_acquire(GFP_KERNEL
);
2410 might_alloc(GFP_KERNEL
);
2411 fs_reclaim_release(GFP_KERNEL
);
2414 static void fs_reclaim_protected_nesting(void)
2418 fs_reclaim_acquire(GFP_KERNEL
);
2419 flags
= memalloc_nofs_save();
2420 might_alloc(GFP_KERNEL
);
2421 memalloc_nofs_restore(flags
);
2422 fs_reclaim_release(GFP_KERNEL
);
2425 static void fs_reclaim_tests(void)
2427 printk(" --------------------\n");
2428 printk(" | fs_reclaim tests |\n");
2429 printk(" --------------------\n");
2431 print_testname("correct nesting");
2432 dotest(fs_reclaim_correct_nesting
, SUCCESS
, 0);
2435 print_testname("wrong nesting");
2436 dotest(fs_reclaim_wrong_nesting
, FAILURE
, 0);
2439 print_testname("protected nesting");
2440 dotest(fs_reclaim_protected_nesting
, SUCCESS
, 0);
2444 #define __guard(cleanup) __maybe_unused __attribute__((__cleanup__(cleanup)))
2446 static void hardirq_exit(int *_
)
2451 #define HARDIRQ_CONTEXT(name, ...) \
2452 int hardirq_guard_##name __guard(hardirq_exit); \
2455 #define NOTTHREADED_HARDIRQ_CONTEXT(name, ...) \
2456 int notthreaded_hardirq_guard_##name __guard(hardirq_exit); \
2457 local_irq_disable(); \
2461 static void softirq_exit(int *_
)
2466 #define SOFTIRQ_CONTEXT(name, ...) \
2467 int softirq_guard_##name __guard(softirq_exit); \
2470 static void rcu_exit(int *_
)
2475 #define RCU_CONTEXT(name, ...) \
2476 int rcu_guard_##name __guard(rcu_exit); \
2479 static void rcu_bh_exit(int *_
)
2481 rcu_read_unlock_bh();
2484 #define RCU_BH_CONTEXT(name, ...) \
2485 int rcu_bh_guard_##name __guard(rcu_bh_exit); \
2488 static void rcu_sched_exit(int *_
)
2490 rcu_read_unlock_sched();
2493 #define RCU_SCHED_CONTEXT(name, ...) \
2494 int rcu_sched_guard_##name __guard(rcu_sched_exit); \
2495 rcu_read_lock_sched();
2497 static void raw_spinlock_exit(raw_spinlock_t
**lock
)
2499 raw_spin_unlock(*lock
);
2502 #define RAW_SPINLOCK_CONTEXT(name, lock) \
2503 raw_spinlock_t *raw_spinlock_guard_##name __guard(raw_spinlock_exit) = &(lock); \
2504 raw_spin_lock(&(lock));
2506 static void spinlock_exit(spinlock_t
**lock
)
2511 #define SPINLOCK_CONTEXT(name, lock) \
2512 spinlock_t *spinlock_guard_##name __guard(spinlock_exit) = &(lock); \
2515 static void mutex_exit(struct mutex
**lock
)
2517 mutex_unlock(*lock
);
2520 #define MUTEX_CONTEXT(name, lock) \
2521 struct mutex *mutex_guard_##name __guard(mutex_exit) = &(lock); \
2522 mutex_lock(&(lock));
2524 #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock) \
2526 static void __maybe_unused inner##_in_##outer(void) \
2528 outer##_CONTEXT(_, outer_lock); \
2530 inner##_CONTEXT(_, inner_lock); \
2535 * wait contexts (considering PREEMPT_RT)
2537 * o: inner is allowed in outer
2538 * x: inner is disallowed in outer
2540 * \ inner | RCU | RAW_SPIN | SPIN | MUTEX
2542 * ---------------+-------+----------+------+-------
2543 * HARDIRQ | o | o | o | x
2544 * ---------------+-------+----------+------+-------
2545 * NOTTHREADED_IRQ| o | o | x | x
2546 * ---------------+-------+----------+------+-------
2547 * SOFTIRQ | o | o | o | x
2548 * ---------------+-------+----------+------+-------
2549 * RCU | o | o | o | x
2550 * ---------------+-------+----------+------+-------
2551 * RCU_BH | o | o | o | x
2552 * ---------------+-------+----------+------+-------
2553 * RCU_SCHED | o | o | x | x
2554 * ---------------+-------+----------+------+-------
2555 * RAW_SPIN | o | o | x | x
2556 * ---------------+-------+----------+------+-------
2557 * SPIN | o | o | o | x
2558 * ---------------+-------+----------+------+-------
2559 * MUTEX | o | o | o | o
2560 * ---------------+-------+----------+------+-------
2563 #define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock) \
2564 GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock) \
2565 GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock) \
2566 GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock) \
2567 GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock) \
2568 GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock) \
2569 GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock) \
2570 GENERATE_2_CONTEXT_TESTCASE(RAW_SPINLOCK, raw_lock_A, inner, inner_lock) \
2571 GENERATE_2_CONTEXT_TESTCASE(SPINLOCK, lock_A, inner, inner_lock) \
2572 GENERATE_2_CONTEXT_TESTCASE(MUTEX, mutex_A, inner, inner_lock)
2574 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU
, )
2575 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RAW_SPINLOCK
, raw_lock_B
)
2576 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(SPINLOCK
, lock_B
)
2577 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(MUTEX
, mutex_B
)
2579 /* the outer context allows all kinds of preemption */
2580 #define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer) \
2581 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2582 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2583 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2584 dotest(MUTEX_in_##outer, SUCCESS, LOCKTYPE_MUTEX); \
2587 * the outer context only allows the preemption introduced by spinlock_t (which
2588 * is a sleepable lock for PREEMPT_RT)
2590 #define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer) \
2591 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2592 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2593 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2594 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2596 /* the outer doesn't allows any kind of preemption */
2597 #define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer) \
2598 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2599 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2600 dotest(SPINLOCK_in_##outer, FAILURE, LOCKTYPE_SPIN); \
2601 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2603 static void wait_context_tests(void)
2605 printk(" --------------------------------------------------------------------------\n");
2606 printk(" | wait context tests |\n");
2607 printk(" --------------------------------------------------------------------------\n");
2608 printk(" | rcu | raw | spin |mutex |\n");
2609 printk(" --------------------------------------------------------------------------\n");
2610 print_testname("in hardirq context");
2611 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ
);
2614 print_testname("in hardirq context (not threaded)");
2615 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ
);
2618 print_testname("in softirq context");
2619 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ
);
2622 print_testname("in RCU context");
2623 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU
);
2626 print_testname("in RCU-bh context");
2627 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH
);
2630 print_testname("in RCU-sched context");
2631 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED
);
2634 print_testname("in RAW_SPINLOCK context");
2635 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RAW_SPINLOCK
);
2638 print_testname("in SPINLOCK context");
2639 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SPINLOCK
);
2642 print_testname("in MUTEX context");
2643 DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(MUTEX
);
2647 static void local_lock_2(void)
2649 local_lock_acquire(&local_A
); /* IRQ-ON */
2650 local_lock_release(&local_A
);
2653 spin_lock(&lock_A
); /* IN-IRQ */
2654 spin_unlock(&lock_A
);
2659 local_lock_acquire(&local_A
); /* IN-IRQ <-> IRQ-ON cycle, false */
2660 local_lock_release(&local_A
);
2661 spin_unlock(&lock_A
);
2665 static void local_lock_3A(void)
2667 local_lock_acquire(&local_A
); /* IRQ-ON */
2668 spin_lock(&lock_B
); /* IRQ-ON */
2669 spin_unlock(&lock_B
);
2670 local_lock_release(&local_A
);
2673 spin_lock(&lock_A
); /* IN-IRQ */
2674 spin_unlock(&lock_A
);
2679 local_lock_acquire(&local_A
); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2680 local_lock_release(&local_A
);
2681 spin_unlock(&lock_A
);
2685 static void local_lock_3B(void)
2687 local_lock_acquire(&local_A
); /* IRQ-ON */
2688 spin_lock(&lock_B
); /* IRQ-ON */
2689 spin_unlock(&lock_B
);
2690 local_lock_release(&local_A
);
2693 spin_lock(&lock_A
); /* IN-IRQ */
2694 spin_unlock(&lock_A
);
2699 local_lock_acquire(&local_A
); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2700 local_lock_release(&local_A
);
2701 spin_unlock(&lock_A
);
2706 spin_lock(&lock_B
); /* IN-IRQ <-> IRQ-ON cycle, true */
2707 spin_unlock(&lock_B
);
2708 spin_unlock(&lock_A
);
2713 static void local_lock_tests(void)
2715 printk(" --------------------------------------------------------------------------\n");
2716 printk(" | local_lock tests |\n");
2717 printk(" ---------------------\n");
2719 print_testname("local_lock inversion 2");
2720 dotest(local_lock_2
, SUCCESS
, LOCKTYPE_LL
);
2723 print_testname("local_lock inversion 3A");
2724 dotest(local_lock_3A
, SUCCESS
, LOCKTYPE_LL
);
2727 print_testname("local_lock inversion 3B");
2728 dotest(local_lock_3B
, FAILURE
, LOCKTYPE_LL
);
2732 static void hardirq_deadlock_softirq_not_deadlock(void)
2734 /* mutex_A is hardirq-unsafe and softirq-unsafe */
2735 /* mutex_A -> lock_C */
2736 mutex_lock(&mutex_A
);
2739 spin_unlock(&lock_C
);
2741 mutex_unlock(&mutex_A
);
2743 /* lock_A is hardirq-safe */
2746 spin_unlock(&lock_A
);
2749 /* lock_A -> lock_B */
2753 spin_unlock(&lock_B
);
2754 spin_unlock(&lock_A
);
2757 /* lock_B -> lock_C */
2761 spin_unlock(&lock_C
);
2762 spin_unlock(&lock_B
);
2765 /* lock_D is softirq-safe */
2768 spin_unlock(&lock_D
);
2771 /* And lock_D is hardirq-unsafe */
2774 spin_unlock(&lock_D
);
2778 * mutex_A -> lock_C -> lock_D is softirq-unsafe -> softirq-safe, not
2781 * lock_A -> lock_B -> lock_C -> lock_D is hardirq-safe ->
2782 * hardirq-unsafe, deadlock.
2787 spin_unlock(&lock_D
);
2788 spin_unlock(&lock_C
);
2792 void locking_selftest(void)
2795 * Got a locking failure before the selftest ran?
2798 printk("----------------------------------\n");
2799 printk("| Locking API testsuite disabled |\n");
2800 printk("----------------------------------\n");
2805 * treats read_lock() as recursive read locks for testing purpose
2807 force_read_lock_recursive
= 1;
2810 * Run the testsuite:
2812 printk("------------------------\n");
2813 printk("| Locking API testsuite:\n");
2814 printk("----------------------------------------------------------------------------\n");
2815 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
2816 printk(" --------------------------------------------------------------------------\n");
2818 init_shared_classes();
2819 lockdep_set_selftest_task(current
);
2821 DO_TESTCASE_6R("A-A deadlock", AA
);
2822 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA
);
2823 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA
);
2824 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC
);
2825 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA
);
2826 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA
);
2827 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA
);
2828 DO_TESTCASE_6("double unlock", double_unlock
);
2829 DO_TESTCASE_6("initialize held", init_held
);
2831 printk(" --------------------------------------------------------------------------\n");
2832 print_testname("recursive read-lock");
2834 dotest(rlock_AA1
, SUCCESS
, LOCKTYPE_RWLOCK
);
2836 dotest(rsem_AA1
, FAILURE
, LOCKTYPE_RWSEM
);
2839 print_testname("recursive read-lock #2");
2841 dotest(rlock_AA1B
, SUCCESS
, LOCKTYPE_RWLOCK
);
2843 dotest(rsem_AA1B
, FAILURE
, LOCKTYPE_RWSEM
);
2846 print_testname("mixed read-write-lock");
2848 dotest(rlock_AA2
, FAILURE
, LOCKTYPE_RWLOCK
);
2850 dotest(rsem_AA2
, FAILURE
, LOCKTYPE_RWSEM
);
2853 print_testname("mixed write-read-lock");
2855 dotest(rlock_AA3
, FAILURE
, LOCKTYPE_RWLOCK
);
2857 dotest(rsem_AA3
, FAILURE
, LOCKTYPE_RWSEM
);
2860 print_testname("mixed read-lock/lock-write ABBA");
2862 dotest(rlock_ABBA1
, FAILURE
, LOCKTYPE_RWLOCK
);
2864 dotest(rwsem_ABBA1
, FAILURE
, LOCKTYPE_RWSEM
);
2866 print_testname("mixed read-lock/lock-read ABBA");
2868 dotest(rlock_ABBA2
, SUCCESS
, LOCKTYPE_RWLOCK
);
2870 dotest(rwsem_ABBA2
, FAILURE
, LOCKTYPE_RWSEM
);
2872 print_testname("mixed write-lock/lock-write ABBA");
2874 dotest(rlock_ABBA3
, FAILURE
, LOCKTYPE_RWLOCK
);
2876 dotest(rwsem_ABBA3
, FAILURE
, LOCKTYPE_RWSEM
);
2878 print_testname("chain cached mixed R-L/L-W ABBA");
2880 dotest(rlock_chaincache_ABBA1
, FAILURE
, LOCKTYPE_RWLOCK
);
2882 DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1
);
2883 DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1
);
2884 DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1
);
2885 DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1
);
2887 printk(" --------------------------------------------------------------------------\n");
2890 * irq-context testcases:
2892 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1
);
2893 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A
);
2894 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B
);
2895 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3
);
2896 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4
);
2897 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion
);
2899 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion
);
2900 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2
);
2901 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3
);
2905 force_read_lock_recursive
= 0;
2907 * queued_read_lock() specific test cases can be put here
2909 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS
))
2910 queued_read_lock_tests();
2914 /* Wait context test cases that are specific for RAW_LOCK_NESTING */
2915 if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING
))
2916 wait_context_tests();
2920 print_testname("hardirq_unsafe_softirq_safe");
2921 dotest(hardirq_deadlock_softirq_not_deadlock
, FAILURE
, LOCKTYPE_SPECIAL
);
2924 if (unexpected_testcase_failures
) {
2925 printk("-----------------------------------------------------------------\n");
2927 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2928 unexpected_testcase_failures
, testcase_total
);
2929 printk("-----------------------------------------------------------------\n");
2930 } else if (expected_testcase_failures
&& testcase_successes
) {
2931 printk("--------------------------------------------------------\n");
2932 printk("%3d out of %3d testcases failed, as expected. |\n",
2933 expected_testcase_failures
, testcase_total
);
2934 printk("----------------------------------------------------\n");
2936 } else if (expected_testcase_failures
&& !testcase_successes
) {
2937 printk("--------------------------------------------------------\n");
2938 printk("All %3d testcases failed, as expected. |\n",
2939 expected_testcase_failures
);
2940 printk("----------------------------------------\n");
2943 printk("-------------------------------------------------------\n");
2944 printk("Good, all %3d testcases passed! |\n",
2945 testcase_successes
);
2946 printk("---------------------------------\n");
2949 lockdep_set_selftest_task(NULL
);
2950 debug_locks_silent
= 0;