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>
29 #ifdef CONFIG_PREEMPT_RT
32 # define NON_RT(...) __VA_ARGS__
36 * Change this to 1 if you want to see the failure printouts:
38 static unsigned int debug_locks_verbose;
39 unsigned int force_read_lock_recursive;
41 static DEFINE_WD_CLASS(ww_lockdep);
43 static int __init setup_debug_locks_verbose(char *str)
45 get_option(&str, &debug_locks_verbose);
50 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
55 #define LOCKTYPE_SPIN 0x1
56 #define LOCKTYPE_RWLOCK 0x2
57 #define LOCKTYPE_MUTEX 0x4
58 #define LOCKTYPE_RWSEM 0x8
59 #define LOCKTYPE_WW 0x10
60 #define LOCKTYPE_RTMUTEX 0x20
61 #define LOCKTYPE_LL 0x40
62 #define LOCKTYPE_SPECIAL 0x80
64 static struct ww_acquire_ctx t, t2;
65 static struct ww_mutex o, o2, o3;
68 * Normal standalone locks, for the circular and irq-context
71 static DEFINE_SPINLOCK(lock_A);
72 static DEFINE_SPINLOCK(lock_B);
73 static DEFINE_SPINLOCK(lock_C);
74 static DEFINE_SPINLOCK(lock_D);
76 static DEFINE_RAW_SPINLOCK(raw_lock_A);
77 static DEFINE_RAW_SPINLOCK(raw_lock_B);
79 static DEFINE_RWLOCK(rwlock_A);
80 static DEFINE_RWLOCK(rwlock_B);
81 static DEFINE_RWLOCK(rwlock_C);
82 static DEFINE_RWLOCK(rwlock_D);
84 static DEFINE_MUTEX(mutex_A);
85 static DEFINE_MUTEX(mutex_B);
86 static DEFINE_MUTEX(mutex_C);
87 static DEFINE_MUTEX(mutex_D);
89 static DECLARE_RWSEM(rwsem_A);
90 static DECLARE_RWSEM(rwsem_B);
91 static DECLARE_RWSEM(rwsem_C);
92 static DECLARE_RWSEM(rwsem_D);
94 #ifdef CONFIG_RT_MUTEXES
96 static DEFINE_RT_MUTEX(rtmutex_A);
97 static DEFINE_RT_MUTEX(rtmutex_B);
98 static DEFINE_RT_MUTEX(rtmutex_C);
99 static DEFINE_RT_MUTEX(rtmutex_D);
104 * Locks that we initialize dynamically as well so that
105 * e.g. X1 and X2 becomes two instances of the same class,
106 * but X* and Y* are different classes. We do this so that
107 * we do not trigger a real lockup:
109 static DEFINE_SPINLOCK(lock_X1);
110 static DEFINE_SPINLOCK(lock_X2);
111 static DEFINE_SPINLOCK(lock_Y1);
112 static DEFINE_SPINLOCK(lock_Y2);
113 static DEFINE_SPINLOCK(lock_Z1);
114 static DEFINE_SPINLOCK(lock_Z2);
116 static DEFINE_RWLOCK(rwlock_X1);
117 static DEFINE_RWLOCK(rwlock_X2);
118 static DEFINE_RWLOCK(rwlock_Y1);
119 static DEFINE_RWLOCK(rwlock_Y2);
120 static DEFINE_RWLOCK(rwlock_Z1);
121 static DEFINE_RWLOCK(rwlock_Z2);
123 static DEFINE_MUTEX(mutex_X1);
124 static DEFINE_MUTEX(mutex_X2);
125 static DEFINE_MUTEX(mutex_Y1);
126 static DEFINE_MUTEX(mutex_Y2);
127 static DEFINE_MUTEX(mutex_Z1);
128 static DEFINE_MUTEX(mutex_Z2);
130 static DECLARE_RWSEM(rwsem_X1);
131 static DECLARE_RWSEM(rwsem_X2);
132 static DECLARE_RWSEM(rwsem_Y1);
133 static DECLARE_RWSEM(rwsem_Y2);
134 static DECLARE_RWSEM(rwsem_Z1);
135 static DECLARE_RWSEM(rwsem_Z2);
137 #ifdef CONFIG_RT_MUTEXES
139 static DEFINE_RT_MUTEX(rtmutex_X1);
140 static DEFINE_RT_MUTEX(rtmutex_X2);
141 static DEFINE_RT_MUTEX(rtmutex_Y1);
142 static DEFINE_RT_MUTEX(rtmutex_Y2);
143 static DEFINE_RT_MUTEX(rtmutex_Z1);
144 static DEFINE_RT_MUTEX(rtmutex_Z2);
148 static DEFINE_PER_CPU(local_lock_t, local_A);
151 * non-inlined runtime initializers, to let separate locks share
152 * the same lock-class:
154 #define INIT_CLASS_FUNC(class) \
155 static noinline void \
156 init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \
157 struct mutex *mutex, struct rw_semaphore *rwsem)\
159 spin_lock_init(lock); \
160 rwlock_init(rwlock); \
169 static void init_shared_classes(void)
171 #ifdef CONFIG_RT_MUTEXES
172 static struct lock_class_key rt_X, rt_Y, rt_Z;
174 __rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
175 __rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
176 __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
177 __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
178 __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
179 __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
182 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
183 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
185 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
186 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
188 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
189 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
193 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
194 * The following functions use a lock from a simulated hardirq/softirq
195 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
198 #define HARDIRQ_DISABLE local_irq_disable
199 #define HARDIRQ_ENABLE local_irq_enable
201 #define HARDIRQ_ENTER() \
202 local_irq_disable(); \
204 lockdep_hardirq_threaded(); \
207 #define HARDIRQ_EXIT() \
211 #define SOFTIRQ_DISABLE local_bh_disable
212 #define SOFTIRQ_ENABLE local_bh_enable
214 #define SOFTIRQ_ENTER() \
215 local_bh_disable(); \
216 local_irq_disable(); \
217 lockdep_softirq_enter(); \
218 WARN_ON(!in_softirq());
220 #define SOFTIRQ_EXIT() \
221 lockdep_softirq_exit(); \
222 local_irq_enable(); \
226 * Shortcuts for lock/unlock API variants, to keep
227 * the testcases compact:
229 #define L(x) spin_lock(&lock_##x)
230 #define U(x) spin_unlock(&lock_##x)
231 #define LU(x) L(x); U(x)
232 #define SI(x) spin_lock_init(&lock_##x)
234 #define WL(x) write_lock(&rwlock_##x)
235 #define WU(x) write_unlock(&rwlock_##x)
236 #define WLU(x) WL(x); WU(x)
238 #define RL(x) read_lock(&rwlock_##x)
239 #define RU(x) read_unlock(&rwlock_##x)
240 #define RLU(x) RL(x); RU(x)
241 #define RWI(x) rwlock_init(&rwlock_##x)
243 #define ML(x) mutex_lock(&mutex_##x)
244 #define MU(x) mutex_unlock(&mutex_##x)
245 #define MI(x) mutex_init(&mutex_##x)
247 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
248 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
249 #define RTI(x) rt_mutex_init(&rtmutex_##x)
251 #define WSL(x) down_write(&rwsem_##x)
252 #define WSU(x) up_write(&rwsem_##x)
254 #define RSL(x) down_read(&rwsem_##x)
255 #define RSU(x) up_read(&rwsem_##x)
256 #define RWSI(x) init_rwsem(&rwsem_##x)
258 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
259 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
261 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
263 #define WWAD(x) ww_acquire_done(x)
264 #define WWAF(x) ww_acquire_fini(x)
266 #define WWL(x, c) ww_mutex_lock(x, c)
267 #define WWT(x) ww_mutex_trylock(x)
268 #define WWL1(x) ww_mutex_lock(x, NULL)
269 #define WWU(x) ww_mutex_unlock(x)
272 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
275 * Generate different permutations of the same testcase, using
276 * the same basic lock-dependency/state events:
279 #define GENERATE_TESTCASE(name) \
281 static void name(void) { E(); }
283 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
285 static void name##_12(void) { E1(); E2(); } \
286 static void name##_21(void) { E2(); E1(); }
288 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
290 static void name##_123(void) { E1(); E2(); E3(); } \
291 static void name##_132(void) { E1(); E3(); E2(); } \
292 static void name##_213(void) { E2(); E1(); E3(); } \
293 static void name##_231(void) { E2(); E3(); E1(); } \
294 static void name##_312(void) { E3(); E1(); E2(); } \
295 static void name##_321(void) { E3(); E2(); E1(); }
304 LOCK(X2); /* this one should fail */
309 #include "locking-selftest-spin.h"
310 GENERATE_TESTCASE(AA_spin)
311 #include "locking-selftest-wlock.h"
312 GENERATE_TESTCASE(AA_wlock)
313 #include "locking-selftest-rlock.h"
314 GENERATE_TESTCASE(AA_rlock)
315 #include "locking-selftest-mutex.h"
316 GENERATE_TESTCASE(AA_mutex)
317 #include "locking-selftest-wsem.h"
318 GENERATE_TESTCASE(AA_wsem)
319 #include "locking-selftest-rsem.h"
320 GENERATE_TESTCASE(AA_rsem)
322 #ifdef CONFIG_RT_MUTEXES
323 #include "locking-selftest-rtmutex.h"
324 GENERATE_TESTCASE(AA_rtmutex);
330 * Special-case for read-locking, they are
331 * allowed to recurse on the same lock class:
333 static void rlock_AA1(void)
336 RL(X1); // this one should NOT fail
339 static void rlock_AA1B(void)
342 RL(X2); // this one should NOT fail
345 static void rsem_AA1(void)
348 RSL(X1); // this one should fail
351 static void rsem_AA1B(void)
354 RSL(X2); // this one should fail
357 * The mixing of read and write locks is not allowed:
359 static void rlock_AA2(void)
362 WL(X2); // this one should fail
365 static void rsem_AA2(void)
368 WSL(X2); // this one should fail
371 static void rlock_AA3(void)
374 RL(X2); // this one should fail
377 static void rsem_AA3(void)
380 RSL(X2); // this one should fail
389 static void rlock_ABBA1(void)
399 U(Y1); // should fail
402 static void rwsem_ABBA1(void)
412 MU(Y1); // should fail
421 * This test case is aimed at poking whether the chain cache prevents us from
422 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
423 * read/write locks, the following case may happen
425 * { read_lock(A)->lock(B) dependency exists }
431 * { Not a deadlock, B -> A is added in the chain cache }
437 * { B->A found in chain cache, not reported as a deadlock }
440 static void rlock_chaincache_ABBA1(void)
455 U(Y1); // should fail
464 static void rlock_ABBA2(void)
474 U(Y1); // should NOT fail
477 static void rwsem_ABBA2(void)
487 MU(Y1); // should fail
497 static void rlock_ABBA3(void)
507 U(Y1); // should fail
510 static void rwsem_ABBA3(void)
520 MU(Y1); // should fail
529 LOCK_UNLOCK_2(A, B); \
530 LOCK_UNLOCK_2(B, A); /* fail */
535 #include "locking-selftest-spin.h"
536 GENERATE_TESTCASE(ABBA_spin)
537 #include "locking-selftest-wlock.h"
538 GENERATE_TESTCASE(ABBA_wlock)
539 #include "locking-selftest-rlock.h"
540 GENERATE_TESTCASE(ABBA_rlock)
541 #include "locking-selftest-mutex.h"
542 GENERATE_TESTCASE(ABBA_mutex)
543 #include "locking-selftest-wsem.h"
544 GENERATE_TESTCASE(ABBA_wsem)
545 #include "locking-selftest-rsem.h"
546 GENERATE_TESTCASE(ABBA_rsem)
548 #ifdef CONFIG_RT_MUTEXES
549 #include "locking-selftest-rtmutex.h"
550 GENERATE_TESTCASE(ABBA_rtmutex);
561 LOCK_UNLOCK_2(A, B); \
562 LOCK_UNLOCK_2(B, C); \
563 LOCK_UNLOCK_2(C, A); /* fail */
568 #include "locking-selftest-spin.h"
569 GENERATE_TESTCASE(ABBCCA_spin)
570 #include "locking-selftest-wlock.h"
571 GENERATE_TESTCASE(ABBCCA_wlock)
572 #include "locking-selftest-rlock.h"
573 GENERATE_TESTCASE(ABBCCA_rlock)
574 #include "locking-selftest-mutex.h"
575 GENERATE_TESTCASE(ABBCCA_mutex)
576 #include "locking-selftest-wsem.h"
577 GENERATE_TESTCASE(ABBCCA_wsem)
578 #include "locking-selftest-rsem.h"
579 GENERATE_TESTCASE(ABBCCA_rsem)
581 #ifdef CONFIG_RT_MUTEXES
582 #include "locking-selftest-rtmutex.h"
583 GENERATE_TESTCASE(ABBCCA_rtmutex);
594 LOCK_UNLOCK_2(A, B); \
595 LOCK_UNLOCK_2(C, A); \
596 LOCK_UNLOCK_2(B, C); /* fail */
601 #include "locking-selftest-spin.h"
602 GENERATE_TESTCASE(ABCABC_spin)
603 #include "locking-selftest-wlock.h"
604 GENERATE_TESTCASE(ABCABC_wlock)
605 #include "locking-selftest-rlock.h"
606 GENERATE_TESTCASE(ABCABC_rlock)
607 #include "locking-selftest-mutex.h"
608 GENERATE_TESTCASE(ABCABC_mutex)
609 #include "locking-selftest-wsem.h"
610 GENERATE_TESTCASE(ABCABC_wsem)
611 #include "locking-selftest-rsem.h"
612 GENERATE_TESTCASE(ABCABC_rsem)
614 #ifdef CONFIG_RT_MUTEXES
615 #include "locking-selftest-rtmutex.h"
616 GENERATE_TESTCASE(ABCABC_rtmutex);
622 * AB BC CD DA deadlock:
627 LOCK_UNLOCK_2(A, B); \
628 LOCK_UNLOCK_2(B, C); \
629 LOCK_UNLOCK_2(C, D); \
630 LOCK_UNLOCK_2(D, A); /* fail */
635 #include "locking-selftest-spin.h"
636 GENERATE_TESTCASE(ABBCCDDA_spin)
637 #include "locking-selftest-wlock.h"
638 GENERATE_TESTCASE(ABBCCDDA_wlock)
639 #include "locking-selftest-rlock.h"
640 GENERATE_TESTCASE(ABBCCDDA_rlock)
641 #include "locking-selftest-mutex.h"
642 GENERATE_TESTCASE(ABBCCDDA_mutex)
643 #include "locking-selftest-wsem.h"
644 GENERATE_TESTCASE(ABBCCDDA_wsem)
645 #include "locking-selftest-rsem.h"
646 GENERATE_TESTCASE(ABBCCDDA_rsem)
648 #ifdef CONFIG_RT_MUTEXES
649 #include "locking-selftest-rtmutex.h"
650 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
656 * AB CD BD DA deadlock:
660 LOCK_UNLOCK_2(A, B); \
661 LOCK_UNLOCK_2(C, D); \
662 LOCK_UNLOCK_2(B, D); \
663 LOCK_UNLOCK_2(D, A); /* fail */
668 #include "locking-selftest-spin.h"
669 GENERATE_TESTCASE(ABCDBDDA_spin)
670 #include "locking-selftest-wlock.h"
671 GENERATE_TESTCASE(ABCDBDDA_wlock)
672 #include "locking-selftest-rlock.h"
673 GENERATE_TESTCASE(ABCDBDDA_rlock)
674 #include "locking-selftest-mutex.h"
675 GENERATE_TESTCASE(ABCDBDDA_mutex)
676 #include "locking-selftest-wsem.h"
677 GENERATE_TESTCASE(ABCDBDDA_wsem)
678 #include "locking-selftest-rsem.h"
679 GENERATE_TESTCASE(ABCDBDDA_rsem)
681 #ifdef CONFIG_RT_MUTEXES
682 #include "locking-selftest-rtmutex.h"
683 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
689 * AB CD BC DA deadlock:
693 LOCK_UNLOCK_2(A, B); \
694 LOCK_UNLOCK_2(C, D); \
695 LOCK_UNLOCK_2(B, C); \
696 LOCK_UNLOCK_2(D, A); /* fail */
701 #include "locking-selftest-spin.h"
702 GENERATE_TESTCASE(ABCDBCDA_spin)
703 #include "locking-selftest-wlock.h"
704 GENERATE_TESTCASE(ABCDBCDA_wlock)
705 #include "locking-selftest-rlock.h"
706 GENERATE_TESTCASE(ABCDBCDA_rlock)
707 #include "locking-selftest-mutex.h"
708 GENERATE_TESTCASE(ABCDBCDA_mutex)
709 #include "locking-selftest-wsem.h"
710 GENERATE_TESTCASE(ABCDBCDA_wsem)
711 #include "locking-selftest-rsem.h"
712 GENERATE_TESTCASE(ABCDBCDA_rsem)
714 #ifdef CONFIG_RT_MUTEXES
715 #include "locking-selftest-rtmutex.h"
716 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
721 #ifdef CONFIG_PREEMPT_RT
722 # define RT_PREPARE_DBL_UNLOCK() { migrate_disable(); rcu_read_lock(); }
724 # define RT_PREPARE_DBL_UNLOCK()
732 RT_PREPARE_DBL_UNLOCK(); \
734 UNLOCK(A); /* fail */
739 #include "locking-selftest-spin.h"
740 GENERATE_TESTCASE(double_unlock_spin)
741 #include "locking-selftest-wlock.h"
742 GENERATE_TESTCASE(double_unlock_wlock)
743 #include "locking-selftest-rlock.h"
744 GENERATE_TESTCASE(double_unlock_rlock)
745 #include "locking-selftest-mutex.h"
746 GENERATE_TESTCASE(double_unlock_mutex)
747 #include "locking-selftest-wsem.h"
748 GENERATE_TESTCASE(double_unlock_wsem)
749 #include "locking-selftest-rsem.h"
750 GENERATE_TESTCASE(double_unlock_rsem)
752 #ifdef CONFIG_RT_MUTEXES
753 #include "locking-selftest-rtmutex.h"
754 GENERATE_TESTCASE(double_unlock_rtmutex);
760 * initializing a held lock:
770 #include "locking-selftest-spin.h"
771 GENERATE_TESTCASE(init_held_spin)
772 #include "locking-selftest-wlock.h"
773 GENERATE_TESTCASE(init_held_wlock)
774 #include "locking-selftest-rlock.h"
775 GENERATE_TESTCASE(init_held_rlock)
776 #include "locking-selftest-mutex.h"
777 GENERATE_TESTCASE(init_held_mutex)
778 #include "locking-selftest-wsem.h"
779 GENERATE_TESTCASE(init_held_wsem)
780 #include "locking-selftest-rsem.h"
781 GENERATE_TESTCASE(init_held_rsem)
783 #ifdef CONFIG_RT_MUTEXES
784 #include "locking-selftest-rtmutex.h"
785 GENERATE_TESTCASE(init_held_rtmutex);
791 * locking an irq-safe lock with irqs enabled:
806 * Generate 24 testcases:
808 #include "locking-selftest-spin-hardirq.h"
809 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
811 #include "locking-selftest-rlock-hardirq.h"
812 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
814 #include "locking-selftest-wlock-hardirq.h"
815 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
817 #ifndef CONFIG_PREEMPT_RT
818 #include "locking-selftest-spin-softirq.h"
819 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
821 #include "locking-selftest-rlock-softirq.h"
822 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
824 #include "locking-selftest-wlock-softirq.h"
825 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
831 #ifndef CONFIG_PREEMPT_RT
833 * Enabling hardirqs with a softirq-safe lock held:
850 * Generate 12 testcases:
852 #include "locking-selftest-spin.h"
853 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
855 #include "locking-selftest-wlock.h"
856 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
858 #include "locking-selftest-rlock.h"
859 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
867 * Enabling irqs with an irq-safe lock held:
884 * Generate 24 testcases:
886 #include "locking-selftest-spin-hardirq.h"
887 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
889 #include "locking-selftest-rlock-hardirq.h"
890 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
892 #include "locking-selftest-wlock-hardirq.h"
893 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
895 #ifndef CONFIG_PREEMPT_RT
896 #include "locking-selftest-spin-softirq.h"
897 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
899 #include "locking-selftest-rlock-softirq.h"
900 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
902 #include "locking-selftest-wlock-softirq.h"
903 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
910 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
932 * Generate 36 testcases:
934 #include "locking-selftest-spin-hardirq.h"
935 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
937 #include "locking-selftest-rlock-hardirq.h"
938 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
940 #include "locking-selftest-wlock-hardirq.h"
941 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
943 #ifndef CONFIG_PREEMPT_RT
944 #include "locking-selftest-spin-softirq.h"
945 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
947 #include "locking-selftest-rlock-softirq.h"
948 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
950 #include "locking-selftest-wlock-softirq.h"
951 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
959 * If a lock turns into softirq-safe, but earlier it took
960 * a softirq-unsafe lock:
982 * Generate 36 testcases:
984 #include "locking-selftest-spin-hardirq.h"
985 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
987 #include "locking-selftest-rlock-hardirq.h"
988 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
990 #include "locking-selftest-wlock-hardirq.h"
991 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
993 #ifndef CONFIG_PREEMPT_RT
994 #include "locking-selftest-spin-softirq.h"
995 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
997 #include "locking-selftest-rlock-softirq.h"
998 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
1000 #include "locking-selftest-wlock-softirq.h"
1001 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
1009 * read-lock / write-lock irq inversion.
1011 * Deadlock scenario:
1013 * CPU#1 is at #1, i.e. it has write-locked A, but has not
1016 * CPU#2 is at #2, i.e. it has locked B.
1018 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
1020 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
1046 * Generate 36 testcases:
1048 #include "locking-selftest-spin-hardirq.h"
1049 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
1051 #include "locking-selftest-rlock-hardirq.h"
1052 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
1054 #include "locking-selftest-wlock-hardirq.h"
1055 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
1057 #ifndef CONFIG_PREEMPT_RT
1058 #include "locking-selftest-spin-softirq.h"
1059 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
1061 #include "locking-selftest-rlock-softirq.h"
1062 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
1064 #include "locking-selftest-wlock-softirq.h"
1065 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
1073 * write-read / write-read / write-read deadlock even if read is recursive
1097 #include "locking-selftest-rlock.h"
1098 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
1105 * write-write / read-read / write-read deadlock even if read is recursive
1129 #include "locking-selftest-rlock.h"
1130 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
1137 * write-write / read-read / read-write is not deadlock when read is recursive
1161 #include "locking-selftest-rlock.h"
1162 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
1169 * write-read / read-read / write-write is not deadlock when read is recursive
1193 #include "locking-selftest-rlock.h"
1194 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
1200 * read-lock / write-lock recursion that is actually safe.
1225 * Generate 24 testcases:
1227 #include "locking-selftest-hardirq.h"
1228 #include "locking-selftest-rlock.h"
1229 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
1231 #include "locking-selftest-wlock.h"
1232 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
1234 #ifndef CONFIG_PREEMPT_RT
1235 #include "locking-selftest-softirq.h"
1236 #include "locking-selftest-rlock.h"
1237 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
1239 #include "locking-selftest-wlock.h"
1240 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
1248 * read-lock / write-lock recursion that is unsafe.
1273 * Generate 24 testcases:
1275 #include "locking-selftest-hardirq.h"
1276 #include "locking-selftest-rlock.h"
1277 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
1279 #include "locking-selftest-wlock.h"
1280 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
1282 #ifndef CONFIG_PREEMPT_RT
1283 #include "locking-selftest-softirq.h"
1284 #include "locking-selftest-rlock.h"
1285 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
1287 #include "locking-selftest-wlock.h"
1288 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
1295 * read-lock / write-lock recursion that is unsafe.
1297 * A is a ENABLED_*_READ lock
1298 * B is a USED_IN_*_READ lock
1304 * write_lock(A); // if this one is read_lock(), no deadlock
1329 * Generate 24 testcases:
1331 #include "locking-selftest-hardirq.h"
1332 #include "locking-selftest-rlock.h"
1333 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
1335 #include "locking-selftest-wlock.h"
1336 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
1338 #ifndef CONFIG_PREEMPT_RT
1339 #include "locking-selftest-softirq.h"
1340 #include "locking-selftest-rlock.h"
1341 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
1343 #include "locking-selftest-wlock.h"
1344 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
1347 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1348 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1349 # define I_RAW_SPINLOCK(x) lockdep_reset_lock(&raw_lock_##x.dep_map)
1350 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1351 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1352 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1353 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1354 # define I_LOCAL_LOCK(x) lockdep_reset_lock(this_cpu_ptr(&local_##x.dep_map))
1355 #ifdef CONFIG_RT_MUTEXES
1356 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1359 # define I_SPINLOCK(x)
1360 # define I_RAW_SPINLOCK(x)
1361 # define I_RWLOCK(x)
1365 # define I_LOCAL_LOCK(x)
1369 # define I_RTMUTEX(x)
1372 #ifdef CONFIG_RT_MUTEXES
1373 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1375 #define I2_RTMUTEX(x)
1389 spin_lock_init(&lock_##x); \
1390 rwlock_init(&rwlock_##x); \
1391 mutex_init(&mutex_##x); \
1392 init_rwsem(&rwsem_##x); \
1396 static void reset_locks(void)
1398 local_irq_disable();
1399 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1400 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1402 I1(A); I1(B); I1(C); I1(D);
1403 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1404 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
1405 I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B);
1410 I2(A); I2(B); I2(C); I2(D);
1411 init_shared_classes();
1412 raw_spin_lock_init(&raw_lock_A);
1413 raw_spin_lock_init(&raw_lock_B);
1414 local_lock_init(this_cpu_ptr(&local_A));
1416 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1417 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1418 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1419 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
1425 static int testcase_total;
1426 static int testcase_successes;
1427 static int expected_testcase_failures;
1428 static int unexpected_testcase_failures;
1430 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1432 int saved_preempt_count = preempt_count();
1433 #ifdef CONFIG_PREEMPT_RT
1435 int saved_mgd_count = current->migration_disabled;
1437 int saved_rcu_count = current->rcu_read_lock_nesting;
1440 WARN_ON(irqs_disabled());
1442 debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
1446 * Filter out expected failures:
1448 #ifndef CONFIG_PROVE_LOCKING
1449 if (expected == FAILURE && debug_locks) {
1450 expected_testcase_failures++;
1455 if (debug_locks != expected) {
1456 unexpected_testcase_failures++;
1459 testcase_successes++;
1464 if (debug_locks_verbose & lockclass_mask)
1465 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1466 lockclass_mask, debug_locks, expected);
1468 * Some tests (e.g. double-unlock) might corrupt the preemption
1469 * count, so restore it:
1471 preempt_count_set(saved_preempt_count);
1473 #ifdef CONFIG_PREEMPT_RT
1475 while (current->migration_disabled > saved_mgd_count)
1479 while (current->rcu_read_lock_nesting > saved_rcu_count)
1481 WARN_ON_ONCE(current->rcu_read_lock_nesting < saved_rcu_count);
1484 #ifdef CONFIG_TRACE_IRQFLAGS
1485 if (softirq_count())
1486 current->softirqs_enabled = 0;
1488 current->softirqs_enabled = 1;
1494 #ifdef CONFIG_RT_MUTEXES
1495 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1497 #define dotest_rt(fn, e, m)
1500 static inline void print_testname(const char *testname)
1502 printk("%33s:", testname);
1505 #define DO_TESTCASE_1(desc, name, nr) \
1506 print_testname(desc"/"#nr); \
1507 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1510 #define DO_TESTCASE_1B(desc, name, nr) \
1511 print_testname(desc"/"#nr); \
1512 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1515 #define DO_TESTCASE_1RR(desc, name, nr) \
1516 print_testname(desc"/"#nr); \
1518 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1521 #define DO_TESTCASE_1RRB(desc, name, nr) \
1522 print_testname(desc"/"#nr); \
1524 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1528 #define DO_TESTCASE_3(desc, name, nr) \
1529 print_testname(desc"/"#nr); \
1530 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1531 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1532 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1535 #define DO_TESTCASE_3RW(desc, name, nr) \
1536 print_testname(desc"/"#nr); \
1537 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1538 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1539 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1542 #define DO_TESTCASE_2RW(desc, name, nr) \
1543 print_testname(desc"/"#nr); \
1545 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1546 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1549 #define DO_TESTCASE_2x2RW(desc, name, nr) \
1550 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
1551 NON_RT(DO_TESTCASE_2RW("soft-"desc, name##_soft, nr)) \
1553 #define DO_TESTCASE_6x2x2RW(desc, name) \
1554 DO_TESTCASE_2x2RW(desc, name, 123); \
1555 DO_TESTCASE_2x2RW(desc, name, 132); \
1556 DO_TESTCASE_2x2RW(desc, name, 213); \
1557 DO_TESTCASE_2x2RW(desc, name, 231); \
1558 DO_TESTCASE_2x2RW(desc, name, 312); \
1559 DO_TESTCASE_2x2RW(desc, name, 321);
1561 #define DO_TESTCASE_6(desc, name) \
1562 print_testname(desc); \
1563 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1564 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1565 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1566 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1567 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1568 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1569 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1572 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1573 print_testname(desc); \
1574 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1575 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1576 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1577 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1578 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1579 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1580 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1584 * 'read' variant: rlocks must not trigger.
1586 #define DO_TESTCASE_6R(desc, name) \
1587 print_testname(desc); \
1588 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1589 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1590 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1591 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1592 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1593 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1594 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1597 #define DO_TESTCASE_2I(desc, name, nr) \
1598 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1599 NON_RT(DO_TESTCASE_1("soft-"desc, name##_soft, nr));
1601 #define DO_TESTCASE_2IB(desc, name, nr) \
1602 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1603 NON_RT(DO_TESTCASE_1B("soft-"desc, name##_soft, nr));
1605 #define DO_TESTCASE_6I(desc, name, nr) \
1606 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1607 NON_RT(DO_TESTCASE_3("soft-"desc, name##_soft, nr));
1609 #define DO_TESTCASE_6IRW(desc, name, nr) \
1610 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1611 NON_RT(DO_TESTCASE_3RW("soft-"desc, name##_soft, nr));
1613 #define DO_TESTCASE_2x3(desc, name) \
1614 DO_TESTCASE_3(desc, name, 12); \
1615 DO_TESTCASE_3(desc, name, 21);
1617 #define DO_TESTCASE_2x6(desc, name) \
1618 DO_TESTCASE_6I(desc, name, 12); \
1619 DO_TESTCASE_6I(desc, name, 21);
1621 #define DO_TESTCASE_6x2(desc, name) \
1622 DO_TESTCASE_2I(desc, name, 123); \
1623 DO_TESTCASE_2I(desc, name, 132); \
1624 DO_TESTCASE_2I(desc, name, 213); \
1625 DO_TESTCASE_2I(desc, name, 231); \
1626 DO_TESTCASE_2I(desc, name, 312); \
1627 DO_TESTCASE_2I(desc, name, 321);
1629 #define DO_TESTCASE_6x2B(desc, name) \
1630 DO_TESTCASE_2IB(desc, name, 123); \
1631 DO_TESTCASE_2IB(desc, name, 132); \
1632 DO_TESTCASE_2IB(desc, name, 213); \
1633 DO_TESTCASE_2IB(desc, name, 231); \
1634 DO_TESTCASE_2IB(desc, name, 312); \
1635 DO_TESTCASE_2IB(desc, name, 321);
1637 #define DO_TESTCASE_6x1RR(desc, name) \
1638 DO_TESTCASE_1RR(desc, name, 123); \
1639 DO_TESTCASE_1RR(desc, name, 132); \
1640 DO_TESTCASE_1RR(desc, name, 213); \
1641 DO_TESTCASE_1RR(desc, name, 231); \
1642 DO_TESTCASE_1RR(desc, name, 312); \
1643 DO_TESTCASE_1RR(desc, name, 321);
1645 #define DO_TESTCASE_6x1RRB(desc, name) \
1646 DO_TESTCASE_1RRB(desc, name, 123); \
1647 DO_TESTCASE_1RRB(desc, name, 132); \
1648 DO_TESTCASE_1RRB(desc, name, 213); \
1649 DO_TESTCASE_1RRB(desc, name, 231); \
1650 DO_TESTCASE_1RRB(desc, name, 312); \
1651 DO_TESTCASE_1RRB(desc, name, 321);
1653 #define DO_TESTCASE_6x6(desc, name) \
1654 DO_TESTCASE_6I(desc, name, 123); \
1655 DO_TESTCASE_6I(desc, name, 132); \
1656 DO_TESTCASE_6I(desc, name, 213); \
1657 DO_TESTCASE_6I(desc, name, 231); \
1658 DO_TESTCASE_6I(desc, name, 312); \
1659 DO_TESTCASE_6I(desc, name, 321);
1661 #define DO_TESTCASE_6x6RW(desc, name) \
1662 DO_TESTCASE_6IRW(desc, name, 123); \
1663 DO_TESTCASE_6IRW(desc, name, 132); \
1664 DO_TESTCASE_6IRW(desc, name, 213); \
1665 DO_TESTCASE_6IRW(desc, name, 231); \
1666 DO_TESTCASE_6IRW(desc, name, 312); \
1667 DO_TESTCASE_6IRW(desc, name, 321);
1669 static void ww_test_fail_acquire(void)
1678 if (WARN_ON(!o.ctx) ||
1682 /* No lockdep test, pure API */
1684 WARN_ON(ret != -EALREADY);
1692 WARN_ON(ret != -EDEADLK);
1697 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1699 DEBUG_LOCKS_WARN_ON(1);
1703 #ifdef CONFIG_PREEMPT_RT
1704 #define ww_mutex_base_lock(b) rt_mutex_lock(b)
1705 #define ww_mutex_base_lock_nest_lock(b, b2) rt_mutex_lock_nest_lock(b, b2)
1706 #define ww_mutex_base_lock_interruptible(b) rt_mutex_lock_interruptible(b)
1707 #define ww_mutex_base_lock_killable(b) rt_mutex_lock_killable(b)
1708 #define ww_mutex_base_unlock(b) rt_mutex_unlock(b)
1710 #define ww_mutex_base_lock(b) mutex_lock(b)
1711 #define ww_mutex_base_lock_nest_lock(b, b2) mutex_lock_nest_lock(b, b2)
1712 #define ww_mutex_base_lock_interruptible(b) mutex_lock_interruptible(b)
1713 #define ww_mutex_base_lock_killable(b) mutex_lock_killable(b)
1714 #define ww_mutex_base_unlock(b) mutex_unlock(b)
1717 static void ww_test_normal(void)
1724 * None of the ww_mutex codepaths should be taken in the 'normal'
1725 * mutex calls. The easiest way to verify this is by using the
1726 * normal mutex calls, and making sure o.ctx is unmodified.
1729 /* mutex_lock (and indirectly, mutex_lock_nested) */
1730 o.ctx = (void *)~0UL;
1731 ww_mutex_base_lock(&o.base);
1732 ww_mutex_base_unlock(&o.base);
1733 WARN_ON(o.ctx != (void *)~0UL);
1735 /* mutex_lock_interruptible (and *_nested) */
1736 o.ctx = (void *)~0UL;
1737 ret = ww_mutex_base_lock_interruptible(&o.base);
1739 ww_mutex_base_unlock(&o.base);
1742 WARN_ON(o.ctx != (void *)~0UL);
1744 /* mutex_lock_killable (and *_nested) */
1745 o.ctx = (void *)~0UL;
1746 ret = ww_mutex_base_lock_killable(&o.base);
1748 ww_mutex_base_unlock(&o.base);
1751 WARN_ON(o.ctx != (void *)~0UL);
1753 /* trylock, succeeding */
1754 o.ctx = (void *)~0UL;
1755 ret = ww_mutex_base_trylock(&o.base);
1758 ww_mutex_base_unlock(&o.base);
1761 WARN_ON(o.ctx != (void *)~0UL);
1763 /* trylock, failing */
1764 o.ctx = (void *)~0UL;
1765 ww_mutex_base_lock(&o.base);
1766 ret = ww_mutex_base_trylock(&o.base);
1768 ww_mutex_base_unlock(&o.base);
1769 WARN_ON(o.ctx != (void *)~0UL);
1772 o.ctx = (void *)~0UL;
1773 ww_mutex_base_lock_nest_lock(&o.base, &t);
1774 ww_mutex_base_unlock(&o.base);
1775 WARN_ON(o.ctx != (void *)~0UL);
1778 static void ww_test_two_contexts(void)
1784 static void ww_test_diff_class(void)
1787 #ifdef DEBUG_WW_MUTEXES
1793 static void ww_test_context_done_twice(void)
1801 static void ww_test_context_unlock_twice(void)
1809 static void ww_test_context_fini_early(void)
1817 static void ww_test_context_lock_after_done(void)
1824 static void ww_test_object_unlock_twice(void)
1831 static void ww_test_object_lock_unbalanced(void)
1840 static void ww_test_object_lock_stale_context(void)
1847 static void ww_test_edeadlk_normal(void)
1851 ww_mutex_base_lock(&o2.base);
1853 mutex_release(&o2.base.dep_map, _THIS_IP_);
1863 WARN_ON(ret != -EDEADLK);
1866 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1867 ww_mutex_base_unlock(&o2.base);
1873 static void ww_test_edeadlk_normal_slow(void)
1877 ww_mutex_base_lock(&o2.base);
1878 mutex_release(&o2.base.dep_map, _THIS_IP_);
1889 WARN_ON(ret != -EDEADLK);
1892 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1893 ww_mutex_base_unlock(&o2.base);
1896 ww_mutex_lock_slow(&o2, &t);
1899 static void ww_test_edeadlk_no_unlock(void)
1903 ww_mutex_base_lock(&o2.base);
1905 mutex_release(&o2.base.dep_map, _THIS_IP_);
1915 WARN_ON(ret != -EDEADLK);
1918 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1919 ww_mutex_base_unlock(&o2.base);
1924 static void ww_test_edeadlk_no_unlock_slow(void)
1928 ww_mutex_base_lock(&o2.base);
1929 mutex_release(&o2.base.dep_map, _THIS_IP_);
1940 WARN_ON(ret != -EDEADLK);
1943 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1944 ww_mutex_base_unlock(&o2.base);
1946 ww_mutex_lock_slow(&o2, &t);
1949 static void ww_test_edeadlk_acquire_more(void)
1953 ww_mutex_base_lock(&o2.base);
1954 mutex_release(&o2.base.dep_map, _THIS_IP_);
1965 WARN_ON(ret != -EDEADLK);
1970 static void ww_test_edeadlk_acquire_more_slow(void)
1974 ww_mutex_base_lock(&o2.base);
1975 mutex_release(&o2.base.dep_map, _THIS_IP_);
1986 WARN_ON(ret != -EDEADLK);
1988 ww_mutex_lock_slow(&o3, &t);
1991 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1995 ww_mutex_base_lock(&o2.base);
1996 mutex_release(&o2.base.dep_map, _THIS_IP_);
1999 ww_mutex_base_lock(&o3.base);
2000 mutex_release(&o3.base.dep_map, _THIS_IP_);
2011 WARN_ON(ret != -EDEADLK);
2014 WARN_ON(ret != -EDEADLK);
2017 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
2021 ww_mutex_base_lock(&o2.base);
2022 mutex_release(&o2.base.dep_map, _THIS_IP_);
2025 ww_mutex_base_lock(&o3.base);
2026 mutex_release(&o3.base.dep_map, _THIS_IP_);
2037 WARN_ON(ret != -EDEADLK);
2039 ww_mutex_lock_slow(&o3, &t);
2042 static void ww_test_edeadlk_acquire_wrong(void)
2046 ww_mutex_base_lock(&o2.base);
2047 mutex_release(&o2.base.dep_map, _THIS_IP_);
2058 WARN_ON(ret != -EDEADLK);
2067 static void ww_test_edeadlk_acquire_wrong_slow(void)
2071 ww_mutex_base_lock(&o2.base);
2072 mutex_release(&o2.base.dep_map, _THIS_IP_);
2083 WARN_ON(ret != -EDEADLK);
2089 ww_mutex_lock_slow(&o3, &t);
2092 static void ww_test_spin_nest_unlocked(void)
2094 spin_lock_nest_lock(&lock_A, &o.base);
2098 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2099 static void ww_test_spin_nest_lock(void)
2101 spin_lock(&lock_X1);
2102 spin_lock_nest_lock(&lock_Y1, &lock_X1);
2104 spin_lock_nest_lock(&lock_Y2, &lock_X1);
2105 spin_unlock(&lock_A);
2106 spin_unlock(&lock_Y2);
2107 spin_unlock(&lock_Y1);
2108 spin_unlock(&lock_X1);
2111 static void ww_test_unneeded_slow(void)
2115 ww_mutex_lock_slow(&o, &t);
2118 static void ww_test_context_block(void)
2129 static void ww_test_context_try(void)
2144 static void ww_test_context_context(void)
2160 static void ww_test_try_block(void)
2172 static void ww_test_try_try(void)
2184 static void ww_test_try_context(void)
2197 static void ww_test_block_block(void)
2203 static void ww_test_block_try(void)
2212 static void ww_test_block_context(void)
2223 static void ww_test_spin_block(void)
2239 static void ww_test_spin_try(void)
2259 static void ww_test_spin_context(void)
2281 static void ww_tests(void)
2283 printk(" --------------------------------------------------------------------------\n");
2284 printk(" | Wound/wait tests |\n");
2285 printk(" ---------------------\n");
2287 print_testname("ww api failures");
2288 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
2289 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
2290 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
2293 print_testname("ww contexts mixing");
2294 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
2295 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
2298 print_testname("finishing ww context");
2299 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
2300 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
2301 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
2302 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
2305 print_testname("locking mismatches");
2306 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
2307 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
2308 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
2311 print_testname("EDEADLK handling");
2312 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
2313 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
2314 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
2315 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
2316 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
2317 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
2318 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
2319 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
2320 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
2321 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
2324 print_testname("spinlock nest unlocked");
2325 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
2328 print_testname("spinlock nest test");
2329 dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW);
2332 printk(" -----------------------------------------------------\n");
2333 printk(" |block | try |context|\n");
2334 printk(" -----------------------------------------------------\n");
2336 print_testname("context");
2337 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
2338 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
2339 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
2342 print_testname("try");
2343 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
2344 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
2345 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
2348 print_testname("block");
2349 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
2350 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
2351 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
2354 print_testname("spinlock");
2355 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
2356 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
2357 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
2363 * <in hardirq handler>
2372 static void queued_read_lock_hardirq_RE_Er(void)
2375 read_lock(&rwlock_A);
2378 read_unlock(&rwlock_A);
2383 read_lock(&rwlock_A);
2384 read_unlock(&rwlock_A);
2390 * <in hardirq handler>
2397 * is not a deadlock.
2399 static void queued_read_lock_hardirq_ER_rE(void)
2403 read_lock(&rwlock_A);
2404 read_unlock(&rwlock_A);
2409 read_lock(&rwlock_A);
2412 read_unlock(&rwlock_A);
2420 * <in hardirq handler>
2424 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2426 static void queued_read_lock_hardirq_inversion(void)
2436 read_lock(&rwlock_A);
2437 read_unlock(&rwlock_A);
2441 read_lock(&rwlock_A);
2442 read_unlock(&rwlock_A);
2445 static void queued_read_lock_tests(void)
2447 printk(" --------------------------------------------------------------------------\n");
2448 printk(" | queued read lock tests |\n");
2449 printk(" ---------------------------\n");
2450 print_testname("hardirq read-lock/lock-read");
2451 dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
2454 print_testname("hardirq lock-read/read-lock");
2455 dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
2458 print_testname("hardirq inversion");
2459 dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
2463 static void fs_reclaim_correct_nesting(void)
2465 fs_reclaim_acquire(GFP_KERNEL);
2466 might_alloc(GFP_NOFS);
2467 fs_reclaim_release(GFP_KERNEL);
2470 static void fs_reclaim_wrong_nesting(void)
2472 fs_reclaim_acquire(GFP_KERNEL);
2473 might_alloc(GFP_KERNEL);
2474 fs_reclaim_release(GFP_KERNEL);
2477 static void fs_reclaim_protected_nesting(void)
2481 fs_reclaim_acquire(GFP_KERNEL);
2482 flags = memalloc_nofs_save();
2483 might_alloc(GFP_KERNEL);
2484 memalloc_nofs_restore(flags);
2485 fs_reclaim_release(GFP_KERNEL);
2488 static void fs_reclaim_tests(void)
2490 printk(" --------------------\n");
2491 printk(" | fs_reclaim tests |\n");
2492 printk(" --------------------\n");
2494 print_testname("correct nesting");
2495 dotest(fs_reclaim_correct_nesting, SUCCESS, 0);
2498 print_testname("wrong nesting");
2499 dotest(fs_reclaim_wrong_nesting, FAILURE, 0);
2502 print_testname("protected nesting");
2503 dotest(fs_reclaim_protected_nesting, SUCCESS, 0);
2507 #define __guard(cleanup) __maybe_unused __attribute__((__cleanup__(cleanup)))
2509 static void hardirq_exit(int *_)
2514 #define HARDIRQ_CONTEXT(name, ...) \
2515 int hardirq_guard_##name __guard(hardirq_exit); \
2518 #define NOTTHREADED_HARDIRQ_CONTEXT(name, ...) \
2519 int notthreaded_hardirq_guard_##name __guard(hardirq_exit); \
2520 local_irq_disable(); \
2524 static void softirq_exit(int *_)
2529 #define SOFTIRQ_CONTEXT(name, ...) \
2530 int softirq_guard_##name __guard(softirq_exit); \
2533 static void rcu_exit(int *_)
2538 #define RCU_CONTEXT(name, ...) \
2539 int rcu_guard_##name __guard(rcu_exit); \
2542 static void rcu_bh_exit(int *_)
2544 rcu_read_unlock_bh();
2547 #define RCU_BH_CONTEXT(name, ...) \
2548 int rcu_bh_guard_##name __guard(rcu_bh_exit); \
2551 static void rcu_sched_exit(int *_)
2553 rcu_read_unlock_sched();
2556 #define RCU_SCHED_CONTEXT(name, ...) \
2557 int rcu_sched_guard_##name __guard(rcu_sched_exit); \
2558 rcu_read_lock_sched();
2560 static void raw_spinlock_exit(raw_spinlock_t **lock)
2562 raw_spin_unlock(*lock);
2565 #define RAW_SPINLOCK_CONTEXT(name, lock) \
2566 raw_spinlock_t *raw_spinlock_guard_##name __guard(raw_spinlock_exit) = &(lock); \
2567 raw_spin_lock(&(lock));
2569 static void spinlock_exit(spinlock_t **lock)
2574 #define SPINLOCK_CONTEXT(name, lock) \
2575 spinlock_t *spinlock_guard_##name __guard(spinlock_exit) = &(lock); \
2578 static void mutex_exit(struct mutex **lock)
2580 mutex_unlock(*lock);
2583 #define MUTEX_CONTEXT(name, lock) \
2584 struct mutex *mutex_guard_##name __guard(mutex_exit) = &(lock); \
2585 mutex_lock(&(lock));
2587 #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock) \
2589 static void __maybe_unused inner##_in_##outer(void) \
2591 outer##_CONTEXT(_, outer_lock); \
2593 inner##_CONTEXT(_, inner_lock); \
2598 * wait contexts (considering PREEMPT_RT)
2600 * o: inner is allowed in outer
2601 * x: inner is disallowed in outer
2603 * \ inner | RCU | RAW_SPIN | SPIN | MUTEX
2605 * ---------------+-------+----------+------+-------
2606 * HARDIRQ | o | o | o | x
2607 * ---------------+-------+----------+------+-------
2608 * NOTTHREADED_IRQ| o | o | x | x
2609 * ---------------+-------+----------+------+-------
2610 * SOFTIRQ | o | o | o | x
2611 * ---------------+-------+----------+------+-------
2612 * RCU | o | o | o | x
2613 * ---------------+-------+----------+------+-------
2614 * RCU_BH | o | o | o | x
2615 * ---------------+-------+----------+------+-------
2616 * RCU_SCHED | o | o | x | x
2617 * ---------------+-------+----------+------+-------
2618 * RAW_SPIN | o | o | x | x
2619 * ---------------+-------+----------+------+-------
2620 * SPIN | o | o | o | x
2621 * ---------------+-------+----------+------+-------
2622 * MUTEX | o | o | o | o
2623 * ---------------+-------+----------+------+-------
2626 #define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock) \
2627 GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock) \
2628 GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock) \
2629 GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock) \
2630 GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock) \
2631 GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock) \
2632 GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock) \
2633 GENERATE_2_CONTEXT_TESTCASE(RAW_SPINLOCK, raw_lock_A, inner, inner_lock) \
2634 GENERATE_2_CONTEXT_TESTCASE(SPINLOCK, lock_A, inner, inner_lock) \
2635 GENERATE_2_CONTEXT_TESTCASE(MUTEX, mutex_A, inner, inner_lock)
2637 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU, )
2638 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RAW_SPINLOCK, raw_lock_B)
2639 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(SPINLOCK, lock_B)
2640 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(MUTEX, mutex_B)
2642 /* the outer context allows all kinds of preemption */
2643 #define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer) \
2644 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2645 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2646 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2647 dotest(MUTEX_in_##outer, SUCCESS, LOCKTYPE_MUTEX); \
2650 * the outer context only allows the preemption introduced by spinlock_t (which
2651 * is a sleepable lock for PREEMPT_RT)
2653 #define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer) \
2654 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2655 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2656 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2657 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2659 /* the outer doesn't allows any kind of preemption */
2660 #define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer) \
2661 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2662 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2663 dotest(SPINLOCK_in_##outer, FAILURE, LOCKTYPE_SPIN); \
2664 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2666 static void wait_context_tests(void)
2668 printk(" --------------------------------------------------------------------------\n");
2669 printk(" | wait context tests |\n");
2670 printk(" --------------------------------------------------------------------------\n");
2671 printk(" | rcu | raw | spin |mutex |\n");
2672 printk(" --------------------------------------------------------------------------\n");
2673 print_testname("in hardirq context");
2674 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ);
2677 print_testname("in hardirq context (not threaded)");
2678 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ);
2681 print_testname("in softirq context");
2682 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ);
2685 print_testname("in RCU context");
2686 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU);
2689 print_testname("in RCU-bh context");
2690 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH);
2693 print_testname("in RCU-sched context");
2694 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED);
2697 print_testname("in RAW_SPINLOCK context");
2698 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RAW_SPINLOCK);
2701 print_testname("in SPINLOCK context");
2702 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SPINLOCK);
2705 print_testname("in MUTEX context");
2706 DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(MUTEX);
2710 static void local_lock_2(void)
2712 local_lock(&local_A); /* IRQ-ON */
2713 local_unlock(&local_A);
2716 spin_lock(&lock_A); /* IN-IRQ */
2717 spin_unlock(&lock_A);
2722 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle, false */
2723 local_unlock(&local_A);
2724 spin_unlock(&lock_A);
2728 static void local_lock_3A(void)
2730 local_lock(&local_A); /* IRQ-ON */
2731 spin_lock(&lock_B); /* IRQ-ON */
2732 spin_unlock(&lock_B);
2733 local_unlock(&local_A);
2736 spin_lock(&lock_A); /* IN-IRQ */
2737 spin_unlock(&lock_A);
2742 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2743 local_unlock(&local_A);
2744 spin_unlock(&lock_A);
2748 static void local_lock_3B(void)
2750 local_lock(&local_A); /* IRQ-ON */
2751 spin_lock(&lock_B); /* IRQ-ON */
2752 spin_unlock(&lock_B);
2753 local_unlock(&local_A);
2756 spin_lock(&lock_A); /* IN-IRQ */
2757 spin_unlock(&lock_A);
2762 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
2763 local_unlock(&local_A);
2764 spin_unlock(&lock_A);
2769 spin_lock(&lock_B); /* IN-IRQ <-> IRQ-ON cycle, true */
2770 spin_unlock(&lock_B);
2771 spin_unlock(&lock_A);
2776 static void local_lock_tests(void)
2778 printk(" --------------------------------------------------------------------------\n");
2779 printk(" | local_lock tests |\n");
2780 printk(" ---------------------\n");
2782 print_testname("local_lock inversion 2");
2783 dotest(local_lock_2, SUCCESS, LOCKTYPE_LL);
2786 print_testname("local_lock inversion 3A");
2787 dotest(local_lock_3A, SUCCESS, LOCKTYPE_LL);
2790 print_testname("local_lock inversion 3B");
2791 dotest(local_lock_3B, FAILURE, LOCKTYPE_LL);
2795 static void hardirq_deadlock_softirq_not_deadlock(void)
2797 /* mutex_A is hardirq-unsafe and softirq-unsafe */
2798 /* mutex_A -> lock_C */
2799 mutex_lock(&mutex_A);
2802 spin_unlock(&lock_C);
2804 mutex_unlock(&mutex_A);
2806 /* lock_A is hardirq-safe */
2809 spin_unlock(&lock_A);
2812 /* lock_A -> lock_B */
2816 spin_unlock(&lock_B);
2817 spin_unlock(&lock_A);
2820 /* lock_B -> lock_C */
2824 spin_unlock(&lock_C);
2825 spin_unlock(&lock_B);
2828 /* lock_D is softirq-safe */
2831 spin_unlock(&lock_D);
2834 /* And lock_D is hardirq-unsafe */
2837 spin_unlock(&lock_D);
2841 * mutex_A -> lock_C -> lock_D is softirq-unsafe -> softirq-safe, not
2844 * lock_A -> lock_B -> lock_C -> lock_D is hardirq-safe ->
2845 * hardirq-unsafe, deadlock.
2850 spin_unlock(&lock_D);
2851 spin_unlock(&lock_C);
2855 void locking_selftest(void)
2858 * Got a locking failure before the selftest ran?
2861 printk("----------------------------------\n");
2862 printk("| Locking API testsuite disabled |\n");
2863 printk("----------------------------------\n");
2868 * treats read_lock() as recursive read locks for testing purpose
2870 force_read_lock_recursive = 1;
2873 * Run the testsuite:
2875 printk("------------------------\n");
2876 printk("| Locking API testsuite:\n");
2877 printk("----------------------------------------------------------------------------\n");
2878 printk(" | spin |wlock |rlock |mutex | wsem | rsem |rtmutex\n");
2879 printk(" --------------------------------------------------------------------------\n");
2881 init_shared_classes();
2882 lockdep_set_selftest_task(current);
2884 DO_TESTCASE_6R("A-A deadlock", AA);
2885 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
2886 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
2887 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
2888 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
2889 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
2890 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
2891 DO_TESTCASE_6("double unlock", double_unlock);
2892 DO_TESTCASE_6("initialize held", init_held);
2894 printk(" --------------------------------------------------------------------------\n");
2895 print_testname("recursive read-lock");
2897 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
2899 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
2902 print_testname("recursive read-lock #2");
2904 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
2906 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
2909 print_testname("mixed read-write-lock");
2911 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
2913 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
2916 print_testname("mixed write-read-lock");
2918 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
2920 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
2923 print_testname("mixed read-lock/lock-write ABBA");
2925 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2927 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2929 print_testname("mixed read-lock/lock-read ABBA");
2931 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2933 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2935 print_testname("mixed write-lock/lock-write ABBA");
2937 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2939 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2941 print_testname("chain cached mixed R-L/L-W ABBA");
2943 dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2945 DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
2946 DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
2947 DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
2948 DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
2950 printk(" --------------------------------------------------------------------------\n");
2952 * irq-context testcases:
2954 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
2955 NON_RT(DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A));
2956 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
2957 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
2958 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
2959 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
2961 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
2962 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
2963 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
2967 force_read_lock_recursive = 0;
2969 * queued_read_lock() specific test cases can be put here
2971 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
2972 queued_read_lock_tests();
2976 /* Wait context test cases that are specific for RAW_LOCK_NESTING */
2977 if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING))
2978 wait_context_tests();
2982 print_testname("hardirq_unsafe_softirq_safe");
2983 dotest(hardirq_deadlock_softirq_not_deadlock, FAILURE, LOCKTYPE_SPECIAL);
2986 if (unexpected_testcase_failures) {
2987 printk("-----------------------------------------------------------------\n");
2989 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2990 unexpected_testcase_failures, testcase_total);
2991 printk("-----------------------------------------------------------------\n");
2992 } else if (expected_testcase_failures && testcase_successes) {
2993 printk("--------------------------------------------------------\n");
2994 printk("%3d out of %3d testcases failed, as expected. |\n",
2995 expected_testcase_failures, testcase_total);
2996 printk("----------------------------------------------------\n");
2998 } else if (expected_testcase_failures && !testcase_successes) {
2999 printk("--------------------------------------------------------\n");
3000 printk("All %3d testcases failed, as expected. |\n",
3001 expected_testcase_failures);
3002 printk("----------------------------------------\n");
3005 printk("-------------------------------------------------------\n");
3006 printk("Good, all %3d testcases passed! |\n",
3007 testcase_successes);
3008 printk("---------------------------------\n");
3011 lockdep_set_selftest_task(NULL);
3012 debug_locks_silent = 0;