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>
29 * Change this to 1 if you want to see the failure printouts:
31 static unsigned int debug_locks_verbose
;
32 unsigned int force_read_lock_recursive
;
34 static DEFINE_WD_CLASS(ww_lockdep
);
36 static int __init
setup_debug_locks_verbose(char *str
)
38 get_option(&str
, &debug_locks_verbose
);
43 __setup("debug_locks_verbose=", setup_debug_locks_verbose
);
48 #define LOCKTYPE_SPIN 0x1
49 #define LOCKTYPE_RWLOCK 0x2
50 #define LOCKTYPE_MUTEX 0x4
51 #define LOCKTYPE_RWSEM 0x8
52 #define LOCKTYPE_WW 0x10
53 #define LOCKTYPE_RTMUTEX 0x20
55 static struct ww_acquire_ctx t
, t2
;
56 static struct ww_mutex o
, o2
, o3
;
59 * Normal standalone locks, for the circular and irq-context
62 static DEFINE_SPINLOCK(lock_A
);
63 static DEFINE_SPINLOCK(lock_B
);
64 static DEFINE_SPINLOCK(lock_C
);
65 static DEFINE_SPINLOCK(lock_D
);
67 static DEFINE_RWLOCK(rwlock_A
);
68 static DEFINE_RWLOCK(rwlock_B
);
69 static DEFINE_RWLOCK(rwlock_C
);
70 static DEFINE_RWLOCK(rwlock_D
);
72 static DEFINE_MUTEX(mutex_A
);
73 static DEFINE_MUTEX(mutex_B
);
74 static DEFINE_MUTEX(mutex_C
);
75 static DEFINE_MUTEX(mutex_D
);
77 static DECLARE_RWSEM(rwsem_A
);
78 static DECLARE_RWSEM(rwsem_B
);
79 static DECLARE_RWSEM(rwsem_C
);
80 static DECLARE_RWSEM(rwsem_D
);
82 #ifdef CONFIG_RT_MUTEXES
84 static DEFINE_RT_MUTEX(rtmutex_A
);
85 static DEFINE_RT_MUTEX(rtmutex_B
);
86 static DEFINE_RT_MUTEX(rtmutex_C
);
87 static DEFINE_RT_MUTEX(rtmutex_D
);
92 * Locks that we initialize dynamically as well so that
93 * e.g. X1 and X2 becomes two instances of the same class,
94 * but X* and Y* are different classes. We do this so that
95 * we do not trigger a real lockup:
97 static DEFINE_SPINLOCK(lock_X1
);
98 static DEFINE_SPINLOCK(lock_X2
);
99 static DEFINE_SPINLOCK(lock_Y1
);
100 static DEFINE_SPINLOCK(lock_Y2
);
101 static DEFINE_SPINLOCK(lock_Z1
);
102 static DEFINE_SPINLOCK(lock_Z2
);
104 static DEFINE_RWLOCK(rwlock_X1
);
105 static DEFINE_RWLOCK(rwlock_X2
);
106 static DEFINE_RWLOCK(rwlock_Y1
);
107 static DEFINE_RWLOCK(rwlock_Y2
);
108 static DEFINE_RWLOCK(rwlock_Z1
);
109 static DEFINE_RWLOCK(rwlock_Z2
);
111 static DEFINE_MUTEX(mutex_X1
);
112 static DEFINE_MUTEX(mutex_X2
);
113 static DEFINE_MUTEX(mutex_Y1
);
114 static DEFINE_MUTEX(mutex_Y2
);
115 static DEFINE_MUTEX(mutex_Z1
);
116 static DEFINE_MUTEX(mutex_Z2
);
118 static DECLARE_RWSEM(rwsem_X1
);
119 static DECLARE_RWSEM(rwsem_X2
);
120 static DECLARE_RWSEM(rwsem_Y1
);
121 static DECLARE_RWSEM(rwsem_Y2
);
122 static DECLARE_RWSEM(rwsem_Z1
);
123 static DECLARE_RWSEM(rwsem_Z2
);
125 #ifdef CONFIG_RT_MUTEXES
127 static DEFINE_RT_MUTEX(rtmutex_X1
);
128 static DEFINE_RT_MUTEX(rtmutex_X2
);
129 static DEFINE_RT_MUTEX(rtmutex_Y1
);
130 static DEFINE_RT_MUTEX(rtmutex_Y2
);
131 static DEFINE_RT_MUTEX(rtmutex_Z1
);
132 static DEFINE_RT_MUTEX(rtmutex_Z2
);
137 * non-inlined runtime initializers, to let separate locks share
138 * the same lock-class:
140 #define INIT_CLASS_FUNC(class) \
141 static noinline void \
142 init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \
143 struct mutex *mutex, struct rw_semaphore *rwsem)\
145 spin_lock_init(lock); \
146 rwlock_init(rwlock); \
155 static void init_shared_classes(void)
157 #ifdef CONFIG_RT_MUTEXES
158 static struct lock_class_key rt_X
, rt_Y
, rt_Z
;
160 __rt_mutex_init(&rtmutex_X1
, __func__
, &rt_X
);
161 __rt_mutex_init(&rtmutex_X2
, __func__
, &rt_X
);
162 __rt_mutex_init(&rtmutex_Y1
, __func__
, &rt_Y
);
163 __rt_mutex_init(&rtmutex_Y2
, __func__
, &rt_Y
);
164 __rt_mutex_init(&rtmutex_Z1
, __func__
, &rt_Z
);
165 __rt_mutex_init(&rtmutex_Z2
, __func__
, &rt_Z
);
168 init_class_X(&lock_X1
, &rwlock_X1
, &mutex_X1
, &rwsem_X1
);
169 init_class_X(&lock_X2
, &rwlock_X2
, &mutex_X2
, &rwsem_X2
);
171 init_class_Y(&lock_Y1
, &rwlock_Y1
, &mutex_Y1
, &rwsem_Y1
);
172 init_class_Y(&lock_Y2
, &rwlock_Y2
, &mutex_Y2
, &rwsem_Y2
);
174 init_class_Z(&lock_Z1
, &rwlock_Z1
, &mutex_Z1
, &rwsem_Z1
);
175 init_class_Z(&lock_Z2
, &rwlock_Z2
, &mutex_Z2
, &rwsem_Z2
);
179 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
180 * The following functions use a lock from a simulated hardirq/softirq
181 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
184 #define HARDIRQ_DISABLE local_irq_disable
185 #define HARDIRQ_ENABLE local_irq_enable
187 #define HARDIRQ_ENTER() \
188 local_irq_disable(); \
192 #define HARDIRQ_EXIT() \
196 #define SOFTIRQ_DISABLE local_bh_disable
197 #define SOFTIRQ_ENABLE local_bh_enable
199 #define SOFTIRQ_ENTER() \
200 local_bh_disable(); \
201 local_irq_disable(); \
202 lockdep_softirq_enter(); \
203 WARN_ON(!in_softirq());
205 #define SOFTIRQ_EXIT() \
206 lockdep_softirq_exit(); \
207 local_irq_enable(); \
211 * Shortcuts for lock/unlock API variants, to keep
212 * the testcases compact:
214 #define L(x) spin_lock(&lock_##x)
215 #define U(x) spin_unlock(&lock_##x)
216 #define LU(x) L(x); U(x)
217 #define SI(x) spin_lock_init(&lock_##x)
219 #define WL(x) write_lock(&rwlock_##x)
220 #define WU(x) write_unlock(&rwlock_##x)
221 #define WLU(x) WL(x); WU(x)
223 #define RL(x) read_lock(&rwlock_##x)
224 #define RU(x) read_unlock(&rwlock_##x)
225 #define RLU(x) RL(x); RU(x)
226 #define RWI(x) rwlock_init(&rwlock_##x)
228 #define ML(x) mutex_lock(&mutex_##x)
229 #define MU(x) mutex_unlock(&mutex_##x)
230 #define MI(x) mutex_init(&mutex_##x)
232 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
233 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
234 #define RTI(x) rt_mutex_init(&rtmutex_##x)
236 #define WSL(x) down_write(&rwsem_##x)
237 #define WSU(x) up_write(&rwsem_##x)
239 #define RSL(x) down_read(&rwsem_##x)
240 #define RSU(x) up_read(&rwsem_##x)
241 #define RWSI(x) init_rwsem(&rwsem_##x)
243 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
244 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
246 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
248 #define WWAD(x) ww_acquire_done(x)
249 #define WWAF(x) ww_acquire_fini(x)
251 #define WWL(x, c) ww_mutex_lock(x, c)
252 #define WWT(x) ww_mutex_trylock(x)
253 #define WWL1(x) ww_mutex_lock(x, NULL)
254 #define WWU(x) ww_mutex_unlock(x)
257 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
260 * Generate different permutations of the same testcase, using
261 * the same basic lock-dependency/state events:
264 #define GENERATE_TESTCASE(name) \
266 static void name(void) { E(); }
268 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
270 static void name##_12(void) { E1(); E2(); } \
271 static void name##_21(void) { E2(); E1(); }
273 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
275 static void name##_123(void) { E1(); E2(); E3(); } \
276 static void name##_132(void) { E1(); E3(); E2(); } \
277 static void name##_213(void) { E2(); E1(); E3(); } \
278 static void name##_231(void) { E2(); E3(); E1(); } \
279 static void name##_312(void) { E3(); E1(); E2(); } \
280 static void name##_321(void) { E3(); E2(); E1(); }
289 LOCK(X2); /* this one should fail */
294 #include "locking-selftest-spin.h"
295 GENERATE_TESTCASE(AA_spin
)
296 #include "locking-selftest-wlock.h"
297 GENERATE_TESTCASE(AA_wlock
)
298 #include "locking-selftest-rlock.h"
299 GENERATE_TESTCASE(AA_rlock
)
300 #include "locking-selftest-mutex.h"
301 GENERATE_TESTCASE(AA_mutex
)
302 #include "locking-selftest-wsem.h"
303 GENERATE_TESTCASE(AA_wsem
)
304 #include "locking-selftest-rsem.h"
305 GENERATE_TESTCASE(AA_rsem
)
307 #ifdef CONFIG_RT_MUTEXES
308 #include "locking-selftest-rtmutex.h"
309 GENERATE_TESTCASE(AA_rtmutex
);
315 * Special-case for read-locking, they are
316 * allowed to recurse on the same lock class:
318 static void rlock_AA1(void)
321 RL(X1
); // this one should NOT fail
324 static void rlock_AA1B(void)
327 RL(X2
); // this one should NOT fail
330 static void rsem_AA1(void)
333 RSL(X1
); // this one should fail
336 static void rsem_AA1B(void)
339 RSL(X2
); // this one should fail
342 * The mixing of read and write locks is not allowed:
344 static void rlock_AA2(void)
347 WL(X2
); // this one should fail
350 static void rsem_AA2(void)
353 WSL(X2
); // this one should fail
356 static void rlock_AA3(void)
359 RL(X2
); // this one should fail
362 static void rsem_AA3(void)
365 RSL(X2
); // this one should fail
374 static void rlock_ABBA1(void)
384 U(Y1
); // should fail
387 static void rwsem_ABBA1(void)
397 MU(Y1
); // should fail
406 * This test case is aimed at poking whether the chain cache prevents us from
407 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
408 * read/write locks, the following case may happen
410 * { read_lock(A)->lock(B) dependency exists }
416 * { Not a deadlock, B -> A is added in the chain cache }
422 * { B->A found in chain cache, not reported as a deadlock }
425 static void rlock_chaincache_ABBA1(void)
440 U(Y1
); // should fail
449 static void rlock_ABBA2(void)
459 U(Y1
); // should NOT fail
462 static void rwsem_ABBA2(void)
472 MU(Y1
); // should fail
482 static void rlock_ABBA3(void)
492 U(Y1
); // should fail
495 static void rwsem_ABBA3(void)
505 MU(Y1
); // should fail
514 LOCK_UNLOCK_2(A, B); \
515 LOCK_UNLOCK_2(B, A); /* fail */
520 #include "locking-selftest-spin.h"
521 GENERATE_TESTCASE(ABBA_spin
)
522 #include "locking-selftest-wlock.h"
523 GENERATE_TESTCASE(ABBA_wlock
)
524 #include "locking-selftest-rlock.h"
525 GENERATE_TESTCASE(ABBA_rlock
)
526 #include "locking-selftest-mutex.h"
527 GENERATE_TESTCASE(ABBA_mutex
)
528 #include "locking-selftest-wsem.h"
529 GENERATE_TESTCASE(ABBA_wsem
)
530 #include "locking-selftest-rsem.h"
531 GENERATE_TESTCASE(ABBA_rsem
)
533 #ifdef CONFIG_RT_MUTEXES
534 #include "locking-selftest-rtmutex.h"
535 GENERATE_TESTCASE(ABBA_rtmutex
);
546 LOCK_UNLOCK_2(A, B); \
547 LOCK_UNLOCK_2(B, C); \
548 LOCK_UNLOCK_2(C, A); /* fail */
553 #include "locking-selftest-spin.h"
554 GENERATE_TESTCASE(ABBCCA_spin
)
555 #include "locking-selftest-wlock.h"
556 GENERATE_TESTCASE(ABBCCA_wlock
)
557 #include "locking-selftest-rlock.h"
558 GENERATE_TESTCASE(ABBCCA_rlock
)
559 #include "locking-selftest-mutex.h"
560 GENERATE_TESTCASE(ABBCCA_mutex
)
561 #include "locking-selftest-wsem.h"
562 GENERATE_TESTCASE(ABBCCA_wsem
)
563 #include "locking-selftest-rsem.h"
564 GENERATE_TESTCASE(ABBCCA_rsem
)
566 #ifdef CONFIG_RT_MUTEXES
567 #include "locking-selftest-rtmutex.h"
568 GENERATE_TESTCASE(ABBCCA_rtmutex
);
579 LOCK_UNLOCK_2(A, B); \
580 LOCK_UNLOCK_2(C, A); \
581 LOCK_UNLOCK_2(B, C); /* fail */
586 #include "locking-selftest-spin.h"
587 GENERATE_TESTCASE(ABCABC_spin
)
588 #include "locking-selftest-wlock.h"
589 GENERATE_TESTCASE(ABCABC_wlock
)
590 #include "locking-selftest-rlock.h"
591 GENERATE_TESTCASE(ABCABC_rlock
)
592 #include "locking-selftest-mutex.h"
593 GENERATE_TESTCASE(ABCABC_mutex
)
594 #include "locking-selftest-wsem.h"
595 GENERATE_TESTCASE(ABCABC_wsem
)
596 #include "locking-selftest-rsem.h"
597 GENERATE_TESTCASE(ABCABC_rsem
)
599 #ifdef CONFIG_RT_MUTEXES
600 #include "locking-selftest-rtmutex.h"
601 GENERATE_TESTCASE(ABCABC_rtmutex
);
607 * AB BC CD DA deadlock:
612 LOCK_UNLOCK_2(A, B); \
613 LOCK_UNLOCK_2(B, C); \
614 LOCK_UNLOCK_2(C, D); \
615 LOCK_UNLOCK_2(D, A); /* fail */
620 #include "locking-selftest-spin.h"
621 GENERATE_TESTCASE(ABBCCDDA_spin
)
622 #include "locking-selftest-wlock.h"
623 GENERATE_TESTCASE(ABBCCDDA_wlock
)
624 #include "locking-selftest-rlock.h"
625 GENERATE_TESTCASE(ABBCCDDA_rlock
)
626 #include "locking-selftest-mutex.h"
627 GENERATE_TESTCASE(ABBCCDDA_mutex
)
628 #include "locking-selftest-wsem.h"
629 GENERATE_TESTCASE(ABBCCDDA_wsem
)
630 #include "locking-selftest-rsem.h"
631 GENERATE_TESTCASE(ABBCCDDA_rsem
)
633 #ifdef CONFIG_RT_MUTEXES
634 #include "locking-selftest-rtmutex.h"
635 GENERATE_TESTCASE(ABBCCDDA_rtmutex
);
641 * AB CD BD DA deadlock:
645 LOCK_UNLOCK_2(A, B); \
646 LOCK_UNLOCK_2(C, D); \
647 LOCK_UNLOCK_2(B, D); \
648 LOCK_UNLOCK_2(D, A); /* fail */
653 #include "locking-selftest-spin.h"
654 GENERATE_TESTCASE(ABCDBDDA_spin
)
655 #include "locking-selftest-wlock.h"
656 GENERATE_TESTCASE(ABCDBDDA_wlock
)
657 #include "locking-selftest-rlock.h"
658 GENERATE_TESTCASE(ABCDBDDA_rlock
)
659 #include "locking-selftest-mutex.h"
660 GENERATE_TESTCASE(ABCDBDDA_mutex
)
661 #include "locking-selftest-wsem.h"
662 GENERATE_TESTCASE(ABCDBDDA_wsem
)
663 #include "locking-selftest-rsem.h"
664 GENERATE_TESTCASE(ABCDBDDA_rsem
)
666 #ifdef CONFIG_RT_MUTEXES
667 #include "locking-selftest-rtmutex.h"
668 GENERATE_TESTCASE(ABCDBDDA_rtmutex
);
674 * AB CD BC DA deadlock:
678 LOCK_UNLOCK_2(A, B); \
679 LOCK_UNLOCK_2(C, D); \
680 LOCK_UNLOCK_2(B, C); \
681 LOCK_UNLOCK_2(D, A); /* fail */
686 #include "locking-selftest-spin.h"
687 GENERATE_TESTCASE(ABCDBCDA_spin
)
688 #include "locking-selftest-wlock.h"
689 GENERATE_TESTCASE(ABCDBCDA_wlock
)
690 #include "locking-selftest-rlock.h"
691 GENERATE_TESTCASE(ABCDBCDA_rlock
)
692 #include "locking-selftest-mutex.h"
693 GENERATE_TESTCASE(ABCDBCDA_mutex
)
694 #include "locking-selftest-wsem.h"
695 GENERATE_TESTCASE(ABCDBCDA_wsem
)
696 #include "locking-selftest-rsem.h"
697 GENERATE_TESTCASE(ABCDBCDA_rsem
)
699 #ifdef CONFIG_RT_MUTEXES
700 #include "locking-selftest-rtmutex.h"
701 GENERATE_TESTCASE(ABCDBCDA_rtmutex
);
713 UNLOCK(A); /* fail */
718 #include "locking-selftest-spin.h"
719 GENERATE_TESTCASE(double_unlock_spin
)
720 #include "locking-selftest-wlock.h"
721 GENERATE_TESTCASE(double_unlock_wlock
)
722 #include "locking-selftest-rlock.h"
723 GENERATE_TESTCASE(double_unlock_rlock
)
724 #include "locking-selftest-mutex.h"
725 GENERATE_TESTCASE(double_unlock_mutex
)
726 #include "locking-selftest-wsem.h"
727 GENERATE_TESTCASE(double_unlock_wsem
)
728 #include "locking-selftest-rsem.h"
729 GENERATE_TESTCASE(double_unlock_rsem
)
731 #ifdef CONFIG_RT_MUTEXES
732 #include "locking-selftest-rtmutex.h"
733 GENERATE_TESTCASE(double_unlock_rtmutex
);
739 * initializing a held lock:
749 #include "locking-selftest-spin.h"
750 GENERATE_TESTCASE(init_held_spin
)
751 #include "locking-selftest-wlock.h"
752 GENERATE_TESTCASE(init_held_wlock
)
753 #include "locking-selftest-rlock.h"
754 GENERATE_TESTCASE(init_held_rlock
)
755 #include "locking-selftest-mutex.h"
756 GENERATE_TESTCASE(init_held_mutex
)
757 #include "locking-selftest-wsem.h"
758 GENERATE_TESTCASE(init_held_wsem
)
759 #include "locking-selftest-rsem.h"
760 GENERATE_TESTCASE(init_held_rsem
)
762 #ifdef CONFIG_RT_MUTEXES
763 #include "locking-selftest-rtmutex.h"
764 GENERATE_TESTCASE(init_held_rtmutex
);
770 * locking an irq-safe lock with irqs enabled:
785 * Generate 24 testcases:
787 #include "locking-selftest-spin-hardirq.h"
788 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin
)
790 #include "locking-selftest-rlock-hardirq.h"
791 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock
)
793 #include "locking-selftest-wlock-hardirq.h"
794 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock
)
796 #include "locking-selftest-spin-softirq.h"
797 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin
)
799 #include "locking-selftest-rlock-softirq.h"
800 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock
)
802 #include "locking-selftest-wlock-softirq.h"
803 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock
)
809 * Enabling hardirqs with a softirq-safe lock held:
826 * Generate 12 testcases:
828 #include "locking-selftest-spin.h"
829 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin
)
831 #include "locking-selftest-wlock.h"
832 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock
)
834 #include "locking-selftest-rlock.h"
835 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock
)
841 * Enabling irqs with an irq-safe lock held:
858 * Generate 24 testcases:
860 #include "locking-selftest-spin-hardirq.h"
861 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin
)
863 #include "locking-selftest-rlock-hardirq.h"
864 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock
)
866 #include "locking-selftest-wlock-hardirq.h"
867 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock
)
869 #include "locking-selftest-spin-softirq.h"
870 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin
)
872 #include "locking-selftest-rlock-softirq.h"
873 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock
)
875 #include "locking-selftest-wlock-softirq.h"
876 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock
)
882 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
904 * Generate 36 testcases:
906 #include "locking-selftest-spin-hardirq.h"
907 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin
)
909 #include "locking-selftest-rlock-hardirq.h"
910 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock
)
912 #include "locking-selftest-wlock-hardirq.h"
913 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock
)
915 #include "locking-selftest-spin-softirq.h"
916 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin
)
918 #include "locking-selftest-rlock-softirq.h"
919 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock
)
921 #include "locking-selftest-wlock-softirq.h"
922 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock
)
929 * If a lock turns into softirq-safe, but earlier it took
930 * a softirq-unsafe lock:
952 * Generate 36 testcases:
954 #include "locking-selftest-spin-hardirq.h"
955 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin
)
957 #include "locking-selftest-rlock-hardirq.h"
958 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock
)
960 #include "locking-selftest-wlock-hardirq.h"
961 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock
)
963 #include "locking-selftest-spin-softirq.h"
964 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin
)
966 #include "locking-selftest-rlock-softirq.h"
967 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock
)
969 #include "locking-selftest-wlock-softirq.h"
970 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock
)
977 * read-lock / write-lock irq inversion.
981 * CPU#1 is at #1, i.e. it has write-locked A, but has not
984 * CPU#2 is at #2, i.e. it has locked B.
986 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
988 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
1014 * Generate 36 testcases:
1016 #include "locking-selftest-spin-hardirq.h"
1017 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin
)
1019 #include "locking-selftest-rlock-hardirq.h"
1020 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock
)
1022 #include "locking-selftest-wlock-hardirq.h"
1023 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock
)
1025 #include "locking-selftest-spin-softirq.h"
1026 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin
)
1028 #include "locking-selftest-rlock-softirq.h"
1029 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock
)
1031 #include "locking-selftest-wlock-softirq.h"
1032 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock
)
1039 * write-read / write-read / write-read deadlock even if read is recursive
1063 #include "locking-selftest-rlock.h"
1064 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1
)
1071 * write-write / read-read / write-read deadlock even if read is recursive
1095 #include "locking-selftest-rlock.h"
1096 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1
)
1103 * write-write / read-read / read-write is not deadlock when read is recursive
1127 #include "locking-selftest-rlock.h"
1128 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1
)
1135 * write-read / read-read / write-write is not deadlock when read is recursive
1159 #include "locking-selftest-rlock.h"
1160 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1
)
1166 * read-lock / write-lock recursion that is actually safe.
1191 * Generate 24 testcases:
1193 #include "locking-selftest-hardirq.h"
1194 #include "locking-selftest-rlock.h"
1195 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock
)
1197 #include "locking-selftest-wlock.h"
1198 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock
)
1200 #include "locking-selftest-softirq.h"
1201 #include "locking-selftest-rlock.h"
1202 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock
)
1204 #include "locking-selftest-wlock.h"
1205 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock
)
1212 * read-lock / write-lock recursion that is unsafe.
1237 * Generate 24 testcases:
1239 #include "locking-selftest-hardirq.h"
1240 #include "locking-selftest-rlock.h"
1241 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock
)
1243 #include "locking-selftest-wlock.h"
1244 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock
)
1246 #include "locking-selftest-softirq.h"
1247 #include "locking-selftest-rlock.h"
1248 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock
)
1250 #include "locking-selftest-wlock.h"
1251 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock
)
1257 * read-lock / write-lock recursion that is unsafe.
1259 * A is a ENABLED_*_READ lock
1260 * B is a USED_IN_*_READ lock
1266 * write_lock(A); // if this one is read_lock(), no deadlock
1291 * Generate 24 testcases:
1293 #include "locking-selftest-hardirq.h"
1294 #include "locking-selftest-rlock.h"
1295 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock
)
1297 #include "locking-selftest-wlock.h"
1298 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock
)
1300 #include "locking-selftest-softirq.h"
1301 #include "locking-selftest-rlock.h"
1302 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock
)
1304 #include "locking-selftest-wlock.h"
1305 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock
)
1307 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1308 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1309 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1310 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1311 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1312 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1313 #ifdef CONFIG_RT_MUTEXES
1314 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1317 # define I_SPINLOCK(x)
1318 # define I_RWLOCK(x)
1325 # define I_RTMUTEX(x)
1328 #ifdef CONFIG_RT_MUTEXES
1329 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1331 #define I2_RTMUTEX(x)
1345 spin_lock_init(&lock_##x); \
1346 rwlock_init(&rwlock_##x); \
1347 mutex_init(&mutex_##x); \
1348 init_rwsem(&rwsem_##x); \
1352 static void reset_locks(void)
1354 local_irq_disable();
1355 lockdep_free_key_range(&ww_lockdep
.acquire_key
, 1);
1356 lockdep_free_key_range(&ww_lockdep
.mutex_key
, 1);
1358 I1(A
); I1(B
); I1(C
); I1(D
);
1359 I1(X1
); I1(X2
); I1(Y1
); I1(Y2
); I1(Z1
); I1(Z2
);
1360 I_WW(t
); I_WW(t2
); I_WW(o
.base
); I_WW(o2
.base
); I_WW(o3
.base
);
1362 I2(A
); I2(B
); I2(C
); I2(D
);
1363 init_shared_classes();
1365 ww_mutex_init(&o
, &ww_lockdep
); ww_mutex_init(&o2
, &ww_lockdep
); ww_mutex_init(&o3
, &ww_lockdep
);
1366 memset(&t
, 0, sizeof(t
)); memset(&t2
, 0, sizeof(t2
));
1367 memset(&ww_lockdep
.acquire_key
, 0, sizeof(ww_lockdep
.acquire_key
));
1368 memset(&ww_lockdep
.mutex_key
, 0, sizeof(ww_lockdep
.mutex_key
));
1374 static int testcase_total
;
1375 static int testcase_successes
;
1376 static int expected_testcase_failures
;
1377 static int unexpected_testcase_failures
;
1379 static void dotest(void (*testcase_fn
)(void), int expected
, int lockclass_mask
)
1381 unsigned long saved_preempt_count
= preempt_count();
1383 WARN_ON(irqs_disabled());
1387 * Filter out expected failures:
1389 #ifndef CONFIG_PROVE_LOCKING
1390 if (expected
== FAILURE
&& debug_locks
) {
1391 expected_testcase_failures
++;
1396 if (debug_locks
!= expected
) {
1397 unexpected_testcase_failures
++;
1400 testcase_successes
++;
1405 if (debug_locks_verbose
)
1406 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1407 lockclass_mask
, debug_locks
, expected
);
1409 * Some tests (e.g. double-unlock) might corrupt the preemption
1410 * count, so restore it:
1412 preempt_count_set(saved_preempt_count
);
1413 #ifdef CONFIG_TRACE_IRQFLAGS
1414 if (softirq_count())
1415 current
->softirqs_enabled
= 0;
1417 current
->softirqs_enabled
= 1;
1423 #ifdef CONFIG_RT_MUTEXES
1424 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1426 #define dotest_rt(fn, e, m)
1429 static inline void print_testname(const char *testname
)
1431 printk("%33s:", testname
);
1434 #define DO_TESTCASE_1(desc, name, nr) \
1435 print_testname(desc"/"#nr); \
1436 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1439 #define DO_TESTCASE_1B(desc, name, nr) \
1440 print_testname(desc"/"#nr); \
1441 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1444 #define DO_TESTCASE_1RR(desc, name, nr) \
1445 print_testname(desc"/"#nr); \
1447 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1450 #define DO_TESTCASE_1RRB(desc, name, nr) \
1451 print_testname(desc"/"#nr); \
1453 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1457 #define DO_TESTCASE_3(desc, name, nr) \
1458 print_testname(desc"/"#nr); \
1459 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1460 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1461 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1464 #define DO_TESTCASE_3RW(desc, name, nr) \
1465 print_testname(desc"/"#nr); \
1466 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1467 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1468 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1471 #define DO_TESTCASE_2RW(desc, name, nr) \
1472 print_testname(desc"/"#nr); \
1474 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1475 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1478 #define DO_TESTCASE_2x2RW(desc, name, nr) \
1479 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
1480 DO_TESTCASE_2RW("soft-"desc, name##_soft, nr) \
1482 #define DO_TESTCASE_6x2x2RW(desc, name) \
1483 DO_TESTCASE_2x2RW(desc, name, 123); \
1484 DO_TESTCASE_2x2RW(desc, name, 132); \
1485 DO_TESTCASE_2x2RW(desc, name, 213); \
1486 DO_TESTCASE_2x2RW(desc, name, 231); \
1487 DO_TESTCASE_2x2RW(desc, name, 312); \
1488 DO_TESTCASE_2x2RW(desc, name, 321);
1490 #define DO_TESTCASE_6(desc, name) \
1491 print_testname(desc); \
1492 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1493 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1494 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1495 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1496 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1497 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1498 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1501 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1502 print_testname(desc); \
1503 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1504 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1505 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1506 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1507 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1508 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1509 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1513 * 'read' variant: rlocks must not trigger.
1515 #define DO_TESTCASE_6R(desc, name) \
1516 print_testname(desc); \
1517 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1518 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1519 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1520 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1521 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1522 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1523 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1526 #define DO_TESTCASE_2I(desc, name, nr) \
1527 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1528 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1530 #define DO_TESTCASE_2IB(desc, name, nr) \
1531 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1532 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1534 #define DO_TESTCASE_6I(desc, name, nr) \
1535 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1536 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1538 #define DO_TESTCASE_6IRW(desc, name, nr) \
1539 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1540 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1542 #define DO_TESTCASE_2x3(desc, name) \
1543 DO_TESTCASE_3(desc, name, 12); \
1544 DO_TESTCASE_3(desc, name, 21);
1546 #define DO_TESTCASE_2x6(desc, name) \
1547 DO_TESTCASE_6I(desc, name, 12); \
1548 DO_TESTCASE_6I(desc, name, 21);
1550 #define DO_TESTCASE_6x2(desc, name) \
1551 DO_TESTCASE_2I(desc, name, 123); \
1552 DO_TESTCASE_2I(desc, name, 132); \
1553 DO_TESTCASE_2I(desc, name, 213); \
1554 DO_TESTCASE_2I(desc, name, 231); \
1555 DO_TESTCASE_2I(desc, name, 312); \
1556 DO_TESTCASE_2I(desc, name, 321);
1558 #define DO_TESTCASE_6x2B(desc, name) \
1559 DO_TESTCASE_2IB(desc, name, 123); \
1560 DO_TESTCASE_2IB(desc, name, 132); \
1561 DO_TESTCASE_2IB(desc, name, 213); \
1562 DO_TESTCASE_2IB(desc, name, 231); \
1563 DO_TESTCASE_2IB(desc, name, 312); \
1564 DO_TESTCASE_2IB(desc, name, 321);
1566 #define DO_TESTCASE_6x1RR(desc, name) \
1567 DO_TESTCASE_1RR(desc, name, 123); \
1568 DO_TESTCASE_1RR(desc, name, 132); \
1569 DO_TESTCASE_1RR(desc, name, 213); \
1570 DO_TESTCASE_1RR(desc, name, 231); \
1571 DO_TESTCASE_1RR(desc, name, 312); \
1572 DO_TESTCASE_1RR(desc, name, 321);
1574 #define DO_TESTCASE_6x1RRB(desc, name) \
1575 DO_TESTCASE_1RRB(desc, name, 123); \
1576 DO_TESTCASE_1RRB(desc, name, 132); \
1577 DO_TESTCASE_1RRB(desc, name, 213); \
1578 DO_TESTCASE_1RRB(desc, name, 231); \
1579 DO_TESTCASE_1RRB(desc, name, 312); \
1580 DO_TESTCASE_1RRB(desc, name, 321);
1582 #define DO_TESTCASE_6x6(desc, name) \
1583 DO_TESTCASE_6I(desc, name, 123); \
1584 DO_TESTCASE_6I(desc, name, 132); \
1585 DO_TESTCASE_6I(desc, name, 213); \
1586 DO_TESTCASE_6I(desc, name, 231); \
1587 DO_TESTCASE_6I(desc, name, 312); \
1588 DO_TESTCASE_6I(desc, name, 321);
1590 #define DO_TESTCASE_6x6RW(desc, name) \
1591 DO_TESTCASE_6IRW(desc, name, 123); \
1592 DO_TESTCASE_6IRW(desc, name, 132); \
1593 DO_TESTCASE_6IRW(desc, name, 213); \
1594 DO_TESTCASE_6IRW(desc, name, 231); \
1595 DO_TESTCASE_6IRW(desc, name, 312); \
1596 DO_TESTCASE_6IRW(desc, name, 321);
1598 static void ww_test_fail_acquire(void)
1607 if (WARN_ON(!o
.ctx
) ||
1611 /* No lockdep test, pure API */
1613 WARN_ON(ret
!= -EALREADY
);
1621 WARN_ON(ret
!= -EDEADLK
);
1626 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1628 DEBUG_LOCKS_WARN_ON(1);
1632 static void ww_test_normal(void)
1639 * None of the ww_mutex codepaths should be taken in the 'normal'
1640 * mutex calls. The easiest way to verify this is by using the
1641 * normal mutex calls, and making sure o.ctx is unmodified.
1644 /* mutex_lock (and indirectly, mutex_lock_nested) */
1645 o
.ctx
= (void *)~0UL;
1646 mutex_lock(&o
.base
);
1647 mutex_unlock(&o
.base
);
1648 WARN_ON(o
.ctx
!= (void *)~0UL);
1650 /* mutex_lock_interruptible (and *_nested) */
1651 o
.ctx
= (void *)~0UL;
1652 ret
= mutex_lock_interruptible(&o
.base
);
1654 mutex_unlock(&o
.base
);
1657 WARN_ON(o
.ctx
!= (void *)~0UL);
1659 /* mutex_lock_killable (and *_nested) */
1660 o
.ctx
= (void *)~0UL;
1661 ret
= mutex_lock_killable(&o
.base
);
1663 mutex_unlock(&o
.base
);
1666 WARN_ON(o
.ctx
!= (void *)~0UL);
1668 /* trylock, succeeding */
1669 o
.ctx
= (void *)~0UL;
1670 ret
= mutex_trylock(&o
.base
);
1673 mutex_unlock(&o
.base
);
1676 WARN_ON(o
.ctx
!= (void *)~0UL);
1678 /* trylock, failing */
1679 o
.ctx
= (void *)~0UL;
1680 mutex_lock(&o
.base
);
1681 ret
= mutex_trylock(&o
.base
);
1683 mutex_unlock(&o
.base
);
1684 WARN_ON(o
.ctx
!= (void *)~0UL);
1687 o
.ctx
= (void *)~0UL;
1688 mutex_lock_nest_lock(&o
.base
, &t
);
1689 mutex_unlock(&o
.base
);
1690 WARN_ON(o
.ctx
!= (void *)~0UL);
1693 static void ww_test_two_contexts(void)
1699 static void ww_test_diff_class(void)
1702 #ifdef CONFIG_DEBUG_MUTEXES
1708 static void ww_test_context_done_twice(void)
1716 static void ww_test_context_unlock_twice(void)
1724 static void ww_test_context_fini_early(void)
1732 static void ww_test_context_lock_after_done(void)
1739 static void ww_test_object_unlock_twice(void)
1746 static void ww_test_object_lock_unbalanced(void)
1755 static void ww_test_object_lock_stale_context(void)
1762 static void ww_test_edeadlk_normal(void)
1766 mutex_lock(&o2
.base
);
1768 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1778 WARN_ON(ret
!= -EDEADLK
);
1781 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1782 mutex_unlock(&o2
.base
);
1788 static void ww_test_edeadlk_normal_slow(void)
1792 mutex_lock(&o2
.base
);
1793 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1804 WARN_ON(ret
!= -EDEADLK
);
1807 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1808 mutex_unlock(&o2
.base
);
1811 ww_mutex_lock_slow(&o2
, &t
);
1814 static void ww_test_edeadlk_no_unlock(void)
1818 mutex_lock(&o2
.base
);
1820 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1830 WARN_ON(ret
!= -EDEADLK
);
1833 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1834 mutex_unlock(&o2
.base
);
1839 static void ww_test_edeadlk_no_unlock_slow(void)
1843 mutex_lock(&o2
.base
);
1844 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1855 WARN_ON(ret
!= -EDEADLK
);
1858 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1859 mutex_unlock(&o2
.base
);
1861 ww_mutex_lock_slow(&o2
, &t
);
1864 static void ww_test_edeadlk_acquire_more(void)
1868 mutex_lock(&o2
.base
);
1869 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1880 WARN_ON(ret
!= -EDEADLK
);
1885 static void ww_test_edeadlk_acquire_more_slow(void)
1889 mutex_lock(&o2
.base
);
1890 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1901 WARN_ON(ret
!= -EDEADLK
);
1903 ww_mutex_lock_slow(&o3
, &t
);
1906 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1910 mutex_lock(&o2
.base
);
1911 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1914 mutex_lock(&o3
.base
);
1915 mutex_release(&o3
.base
.dep_map
, _THIS_IP_
);
1926 WARN_ON(ret
!= -EDEADLK
);
1929 WARN_ON(ret
!= -EDEADLK
);
1932 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1936 mutex_lock(&o2
.base
);
1937 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1940 mutex_lock(&o3
.base
);
1941 mutex_release(&o3
.base
.dep_map
, _THIS_IP_
);
1952 WARN_ON(ret
!= -EDEADLK
);
1954 ww_mutex_lock_slow(&o3
, &t
);
1957 static void ww_test_edeadlk_acquire_wrong(void)
1961 mutex_lock(&o2
.base
);
1962 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1973 WARN_ON(ret
!= -EDEADLK
);
1982 static void ww_test_edeadlk_acquire_wrong_slow(void)
1986 mutex_lock(&o2
.base
);
1987 mutex_release(&o2
.base
.dep_map
, _THIS_IP_
);
1998 WARN_ON(ret
!= -EDEADLK
);
2004 ww_mutex_lock_slow(&o3
, &t
);
2007 static void ww_test_spin_nest_unlocked(void)
2009 spin_lock_nest_lock(&lock_A
, &o
.base
);
2013 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2014 static void ww_test_spin_nest_lock(void)
2016 spin_lock(&lock_X1
);
2017 spin_lock_nest_lock(&lock_Y1
, &lock_X1
);
2019 spin_lock_nest_lock(&lock_Y2
, &lock_X1
);
2020 spin_unlock(&lock_A
);
2021 spin_unlock(&lock_Y2
);
2022 spin_unlock(&lock_Y1
);
2023 spin_unlock(&lock_X1
);
2026 static void ww_test_unneeded_slow(void)
2030 ww_mutex_lock_slow(&o
, &t
);
2033 static void ww_test_context_block(void)
2044 static void ww_test_context_try(void)
2059 static void ww_test_context_context(void)
2075 static void ww_test_try_block(void)
2087 static void ww_test_try_try(void)
2099 static void ww_test_try_context(void)
2112 static void ww_test_block_block(void)
2118 static void ww_test_block_try(void)
2127 static void ww_test_block_context(void)
2138 static void ww_test_spin_block(void)
2154 static void ww_test_spin_try(void)
2174 static void ww_test_spin_context(void)
2196 static void ww_tests(void)
2198 printk(" --------------------------------------------------------------------------\n");
2199 printk(" | Wound/wait tests |\n");
2200 printk(" ---------------------\n");
2202 print_testname("ww api failures");
2203 dotest(ww_test_fail_acquire
, SUCCESS
, LOCKTYPE_WW
);
2204 dotest(ww_test_normal
, SUCCESS
, LOCKTYPE_WW
);
2205 dotest(ww_test_unneeded_slow
, FAILURE
, LOCKTYPE_WW
);
2208 print_testname("ww contexts mixing");
2209 dotest(ww_test_two_contexts
, FAILURE
, LOCKTYPE_WW
);
2210 dotest(ww_test_diff_class
, FAILURE
, LOCKTYPE_WW
);
2213 print_testname("finishing ww context");
2214 dotest(ww_test_context_done_twice
, FAILURE
, LOCKTYPE_WW
);
2215 dotest(ww_test_context_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
2216 dotest(ww_test_context_fini_early
, FAILURE
, LOCKTYPE_WW
);
2217 dotest(ww_test_context_lock_after_done
, FAILURE
, LOCKTYPE_WW
);
2220 print_testname("locking mismatches");
2221 dotest(ww_test_object_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
2222 dotest(ww_test_object_lock_unbalanced
, FAILURE
, LOCKTYPE_WW
);
2223 dotest(ww_test_object_lock_stale_context
, FAILURE
, LOCKTYPE_WW
);
2226 print_testname("EDEADLK handling");
2227 dotest(ww_test_edeadlk_normal
, SUCCESS
, LOCKTYPE_WW
);
2228 dotest(ww_test_edeadlk_normal_slow
, SUCCESS
, LOCKTYPE_WW
);
2229 dotest(ww_test_edeadlk_no_unlock
, FAILURE
, LOCKTYPE_WW
);
2230 dotest(ww_test_edeadlk_no_unlock_slow
, FAILURE
, LOCKTYPE_WW
);
2231 dotest(ww_test_edeadlk_acquire_more
, FAILURE
, LOCKTYPE_WW
);
2232 dotest(ww_test_edeadlk_acquire_more_slow
, FAILURE
, LOCKTYPE_WW
);
2233 dotest(ww_test_edeadlk_acquire_more_edeadlk
, FAILURE
, LOCKTYPE_WW
);
2234 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow
, FAILURE
, LOCKTYPE_WW
);
2235 dotest(ww_test_edeadlk_acquire_wrong
, FAILURE
, LOCKTYPE_WW
);
2236 dotest(ww_test_edeadlk_acquire_wrong_slow
, FAILURE
, LOCKTYPE_WW
);
2239 print_testname("spinlock nest unlocked");
2240 dotest(ww_test_spin_nest_unlocked
, FAILURE
, LOCKTYPE_WW
);
2243 print_testname("spinlock nest test");
2244 dotest(ww_test_spin_nest_lock
, SUCCESS
, LOCKTYPE_WW
);
2247 printk(" -----------------------------------------------------\n");
2248 printk(" |block | try |context|\n");
2249 printk(" -----------------------------------------------------\n");
2251 print_testname("context");
2252 dotest(ww_test_context_block
, FAILURE
, LOCKTYPE_WW
);
2253 dotest(ww_test_context_try
, SUCCESS
, LOCKTYPE_WW
);
2254 dotest(ww_test_context_context
, SUCCESS
, LOCKTYPE_WW
);
2257 print_testname("try");
2258 dotest(ww_test_try_block
, FAILURE
, LOCKTYPE_WW
);
2259 dotest(ww_test_try_try
, SUCCESS
, LOCKTYPE_WW
);
2260 dotest(ww_test_try_context
, FAILURE
, LOCKTYPE_WW
);
2263 print_testname("block");
2264 dotest(ww_test_block_block
, FAILURE
, LOCKTYPE_WW
);
2265 dotest(ww_test_block_try
, SUCCESS
, LOCKTYPE_WW
);
2266 dotest(ww_test_block_context
, FAILURE
, LOCKTYPE_WW
);
2269 print_testname("spinlock");
2270 dotest(ww_test_spin_block
, FAILURE
, LOCKTYPE_WW
);
2271 dotest(ww_test_spin_try
, SUCCESS
, LOCKTYPE_WW
);
2272 dotest(ww_test_spin_context
, FAILURE
, LOCKTYPE_WW
);
2278 * <in hardirq handler>
2287 static void queued_read_lock_hardirq_RE_Er(void)
2290 read_lock(&rwlock_A
);
2293 read_unlock(&rwlock_A
);
2298 read_lock(&rwlock_A
);
2299 read_unlock(&rwlock_A
);
2305 * <in hardirq handler>
2312 * is not a deadlock.
2314 static void queued_read_lock_hardirq_ER_rE(void)
2318 read_lock(&rwlock_A
);
2319 read_unlock(&rwlock_A
);
2324 read_lock(&rwlock_A
);
2327 read_unlock(&rwlock_A
);
2335 * <in hardirq handler>
2339 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2341 static void queued_read_lock_hardirq_inversion(void)
2351 read_lock(&rwlock_A
);
2352 read_unlock(&rwlock_A
);
2356 read_lock(&rwlock_A
);
2357 read_unlock(&rwlock_A
);
2360 static void queued_read_lock_tests(void)
2362 printk(" --------------------------------------------------------------------------\n");
2363 printk(" | queued read lock tests |\n");
2364 printk(" ---------------------------\n");
2365 print_testname("hardirq read-lock/lock-read");
2366 dotest(queued_read_lock_hardirq_RE_Er
, FAILURE
, LOCKTYPE_RWLOCK
);
2369 print_testname("hardirq lock-read/read-lock");
2370 dotest(queued_read_lock_hardirq_ER_rE
, SUCCESS
, LOCKTYPE_RWLOCK
);
2373 print_testname("hardirq inversion");
2374 dotest(queued_read_lock_hardirq_inversion
, FAILURE
, LOCKTYPE_RWLOCK
);
2378 static void fs_reclaim_correct_nesting(void)
2380 fs_reclaim_acquire(GFP_KERNEL
);
2381 might_alloc(GFP_NOFS
);
2382 fs_reclaim_release(GFP_KERNEL
);
2385 static void fs_reclaim_wrong_nesting(void)
2387 fs_reclaim_acquire(GFP_KERNEL
);
2388 might_alloc(GFP_KERNEL
);
2389 fs_reclaim_release(GFP_KERNEL
);
2392 static void fs_reclaim_protected_nesting(void)
2396 fs_reclaim_acquire(GFP_KERNEL
);
2397 flags
= memalloc_nofs_save();
2398 might_alloc(GFP_KERNEL
);
2399 memalloc_nofs_restore(flags
);
2400 fs_reclaim_release(GFP_KERNEL
);
2403 static void fs_reclaim_tests(void)
2405 printk(" --------------------\n");
2406 printk(" | fs_reclaim tests |\n");
2407 printk(" --------------------\n");
2409 print_testname("correct nesting");
2410 dotest(fs_reclaim_correct_nesting
, SUCCESS
, 0);
2413 print_testname("wrong nesting");
2414 dotest(fs_reclaim_wrong_nesting
, FAILURE
, 0);
2417 print_testname("protected nesting");
2418 dotest(fs_reclaim_protected_nesting
, SUCCESS
, 0);
2422 void locking_selftest(void)
2425 * Got a locking failure before the selftest ran?
2428 printk("----------------------------------\n");
2429 printk("| Locking API testsuite disabled |\n");
2430 printk("----------------------------------\n");
2435 * treats read_lock() as recursive read locks for testing purpose
2437 force_read_lock_recursive
= 1;
2440 * Run the testsuite:
2442 printk("------------------------\n");
2443 printk("| Locking API testsuite:\n");
2444 printk("----------------------------------------------------------------------------\n");
2445 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
2446 printk(" --------------------------------------------------------------------------\n");
2448 init_shared_classes();
2449 debug_locks_silent
= !debug_locks_verbose
;
2450 lockdep_set_selftest_task(current
);
2452 DO_TESTCASE_6R("A-A deadlock", AA
);
2453 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA
);
2454 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA
);
2455 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC
);
2456 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA
);
2457 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA
);
2458 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA
);
2459 DO_TESTCASE_6("double unlock", double_unlock
);
2460 DO_TESTCASE_6("initialize held", init_held
);
2462 printk(" --------------------------------------------------------------------------\n");
2463 print_testname("recursive read-lock");
2465 dotest(rlock_AA1
, SUCCESS
, LOCKTYPE_RWLOCK
);
2467 dotest(rsem_AA1
, FAILURE
, LOCKTYPE_RWSEM
);
2470 print_testname("recursive read-lock #2");
2472 dotest(rlock_AA1B
, SUCCESS
, LOCKTYPE_RWLOCK
);
2474 dotest(rsem_AA1B
, FAILURE
, LOCKTYPE_RWSEM
);
2477 print_testname("mixed read-write-lock");
2479 dotest(rlock_AA2
, FAILURE
, LOCKTYPE_RWLOCK
);
2481 dotest(rsem_AA2
, FAILURE
, LOCKTYPE_RWSEM
);
2484 print_testname("mixed write-read-lock");
2486 dotest(rlock_AA3
, FAILURE
, LOCKTYPE_RWLOCK
);
2488 dotest(rsem_AA3
, FAILURE
, LOCKTYPE_RWSEM
);
2491 print_testname("mixed read-lock/lock-write ABBA");
2493 dotest(rlock_ABBA1
, FAILURE
, LOCKTYPE_RWLOCK
);
2495 dotest(rwsem_ABBA1
, FAILURE
, LOCKTYPE_RWSEM
);
2497 print_testname("mixed read-lock/lock-read ABBA");
2499 dotest(rlock_ABBA2
, SUCCESS
, LOCKTYPE_RWLOCK
);
2501 dotest(rwsem_ABBA2
, FAILURE
, LOCKTYPE_RWSEM
);
2503 print_testname("mixed write-lock/lock-write ABBA");
2505 dotest(rlock_ABBA3
, FAILURE
, LOCKTYPE_RWLOCK
);
2507 dotest(rwsem_ABBA3
, FAILURE
, LOCKTYPE_RWSEM
);
2509 print_testname("chain cached mixed R-L/L-W ABBA");
2511 dotest(rlock_chaincache_ABBA1
, FAILURE
, LOCKTYPE_RWLOCK
);
2513 DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1
);
2514 DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1
);
2515 DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1
);
2516 DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1
);
2518 printk(" --------------------------------------------------------------------------\n");
2521 * irq-context testcases:
2523 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1
);
2524 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A
);
2525 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B
);
2526 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3
);
2527 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4
);
2528 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion
);
2530 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion
);
2531 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2
);
2532 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3
);
2536 force_read_lock_recursive
= 0;
2538 * queued_read_lock() specific test cases can be put here
2540 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS
))
2541 queued_read_lock_tests();
2545 if (unexpected_testcase_failures
) {
2546 printk("-----------------------------------------------------------------\n");
2548 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2549 unexpected_testcase_failures
, testcase_total
);
2550 printk("-----------------------------------------------------------------\n");
2551 } else if (expected_testcase_failures
&& testcase_successes
) {
2552 printk("--------------------------------------------------------\n");
2553 printk("%3d out of %3d testcases failed, as expected. |\n",
2554 expected_testcase_failures
, testcase_total
);
2555 printk("----------------------------------------------------\n");
2557 } else if (expected_testcase_failures
&& !testcase_successes
) {
2558 printk("--------------------------------------------------------\n");
2559 printk("All %3d testcases failed, as expected. |\n",
2560 expected_testcase_failures
);
2561 printk("----------------------------------------\n");
2564 printk("-------------------------------------------------------\n");
2565 printk("Good, all %3d testcases passed! |\n",
2566 testcase_successes
);
2567 printk("---------------------------------\n");
2570 lockdep_set_selftest_task(NULL
);
2571 debug_locks_silent
= 0;