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/delay.h>
19 #include <linux/lockdep.h>
20 #include <linux/spinlock.h>
21 #include <linux/kallsyms.h>
22 #include <linux/interrupt.h>
23 #include <linux/debug_locks.h>
24 #include <linux/irqflags.h>
25 #include <linux/rtmutex.h>
28 * Change this to 1 if you want to see the failure printouts:
30 static unsigned int debug_locks_verbose;
31 unsigned int force_read_lock_recursive;
33 static DEFINE_WD_CLASS(ww_lockdep);
35 static int __init setup_debug_locks_verbose(char *str)
37 get_option(&str, &debug_locks_verbose);
42 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
47 #define LOCKTYPE_SPIN 0x1
48 #define LOCKTYPE_RWLOCK 0x2
49 #define LOCKTYPE_MUTEX 0x4
50 #define LOCKTYPE_RWSEM 0x8
51 #define LOCKTYPE_WW 0x10
52 #define LOCKTYPE_RTMUTEX 0x20
54 static struct ww_acquire_ctx t, t2;
55 static struct ww_mutex o, o2, o3;
58 * Normal standalone locks, for the circular and irq-context
61 static DEFINE_RAW_SPINLOCK(lock_A);
62 static DEFINE_RAW_SPINLOCK(lock_B);
63 static DEFINE_RAW_SPINLOCK(lock_C);
64 static DEFINE_RAW_SPINLOCK(lock_D);
66 static DEFINE_RWLOCK(rwlock_A);
67 static DEFINE_RWLOCK(rwlock_B);
68 static DEFINE_RWLOCK(rwlock_C);
69 static DEFINE_RWLOCK(rwlock_D);
71 static DEFINE_MUTEX(mutex_A);
72 static DEFINE_MUTEX(mutex_B);
73 static DEFINE_MUTEX(mutex_C);
74 static DEFINE_MUTEX(mutex_D);
76 static DECLARE_RWSEM(rwsem_A);
77 static DECLARE_RWSEM(rwsem_B);
78 static DECLARE_RWSEM(rwsem_C);
79 static DECLARE_RWSEM(rwsem_D);
81 #ifdef CONFIG_RT_MUTEXES
83 static DEFINE_RT_MUTEX(rtmutex_A);
84 static DEFINE_RT_MUTEX(rtmutex_B);
85 static DEFINE_RT_MUTEX(rtmutex_C);
86 static DEFINE_RT_MUTEX(rtmutex_D);
91 * Locks that we initialize dynamically as well so that
92 * e.g. X1 and X2 becomes two instances of the same class,
93 * but X* and Y* are different classes. We do this so that
94 * we do not trigger a real lockup:
96 static DEFINE_RAW_SPINLOCK(lock_X1);
97 static DEFINE_RAW_SPINLOCK(lock_X2);
98 static DEFINE_RAW_SPINLOCK(lock_Y1);
99 static DEFINE_RAW_SPINLOCK(lock_Y2);
100 static DEFINE_RAW_SPINLOCK(lock_Z1);
101 static DEFINE_RAW_SPINLOCK(lock_Z2);
103 static DEFINE_RWLOCK(rwlock_X1);
104 static DEFINE_RWLOCK(rwlock_X2);
105 static DEFINE_RWLOCK(rwlock_Y1);
106 static DEFINE_RWLOCK(rwlock_Y2);
107 static DEFINE_RWLOCK(rwlock_Z1);
108 static DEFINE_RWLOCK(rwlock_Z2);
110 static DEFINE_MUTEX(mutex_X1);
111 static DEFINE_MUTEX(mutex_X2);
112 static DEFINE_MUTEX(mutex_Y1);
113 static DEFINE_MUTEX(mutex_Y2);
114 static DEFINE_MUTEX(mutex_Z1);
115 static DEFINE_MUTEX(mutex_Z2);
117 static DECLARE_RWSEM(rwsem_X1);
118 static DECLARE_RWSEM(rwsem_X2);
119 static DECLARE_RWSEM(rwsem_Y1);
120 static DECLARE_RWSEM(rwsem_Y2);
121 static DECLARE_RWSEM(rwsem_Z1);
122 static DECLARE_RWSEM(rwsem_Z2);
124 #ifdef CONFIG_RT_MUTEXES
126 static DEFINE_RT_MUTEX(rtmutex_X1);
127 static DEFINE_RT_MUTEX(rtmutex_X2);
128 static DEFINE_RT_MUTEX(rtmutex_Y1);
129 static DEFINE_RT_MUTEX(rtmutex_Y2);
130 static DEFINE_RT_MUTEX(rtmutex_Z1);
131 static DEFINE_RT_MUTEX(rtmutex_Z2);
136 * non-inlined runtime initializers, to let separate locks share
137 * the same lock-class:
139 #define INIT_CLASS_FUNC(class) \
140 static noinline void \
141 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
142 struct mutex *mutex, struct rw_semaphore *rwsem)\
144 raw_spin_lock_init(lock); \
145 rwlock_init(rwlock); \
154 static void init_shared_classes(void)
156 #ifdef CONFIG_RT_MUTEXES
157 static struct lock_class_key rt_X, rt_Y, rt_Z;
159 __rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
160 __rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
161 __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
162 __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
163 __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
164 __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
167 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
168 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
170 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
171 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
173 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
174 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
178 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
179 * The following functions use a lock from a simulated hardirq/softirq
180 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
183 #define HARDIRQ_DISABLE local_irq_disable
184 #define HARDIRQ_ENABLE local_irq_enable
186 #define HARDIRQ_ENTER() \
187 local_irq_disable(); \
191 #define HARDIRQ_EXIT() \
195 #define SOFTIRQ_DISABLE local_bh_disable
196 #define SOFTIRQ_ENABLE local_bh_enable
198 #define SOFTIRQ_ENTER() \
199 local_bh_disable(); \
200 local_irq_disable(); \
201 lockdep_softirq_enter(); \
202 WARN_ON(!in_softirq());
204 #define SOFTIRQ_EXIT() \
205 lockdep_softirq_exit(); \
206 local_irq_enable(); \
210 * Shortcuts for lock/unlock API variants, to keep
211 * the testcases compact:
213 #define L(x) raw_spin_lock(&lock_##x)
214 #define U(x) raw_spin_unlock(&lock_##x)
215 #define LU(x) L(x); U(x)
216 #define SI(x) raw_spin_lock_init(&lock_##x)
218 #define WL(x) write_lock(&rwlock_##x)
219 #define WU(x) write_unlock(&rwlock_##x)
220 #define WLU(x) WL(x); WU(x)
222 #define RL(x) read_lock(&rwlock_##x)
223 #define RU(x) read_unlock(&rwlock_##x)
224 #define RLU(x) RL(x); RU(x)
225 #define RWI(x) rwlock_init(&rwlock_##x)
227 #define ML(x) mutex_lock(&mutex_##x)
228 #define MU(x) mutex_unlock(&mutex_##x)
229 #define MI(x) mutex_init(&mutex_##x)
231 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
232 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
233 #define RTI(x) rt_mutex_init(&rtmutex_##x)
235 #define WSL(x) down_write(&rwsem_##x)
236 #define WSU(x) up_write(&rwsem_##x)
238 #define RSL(x) down_read(&rwsem_##x)
239 #define RSU(x) up_read(&rwsem_##x)
240 #define RWSI(x) init_rwsem(&rwsem_##x)
242 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
243 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
245 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
247 #define WWAD(x) ww_acquire_done(x)
248 #define WWAF(x) ww_acquire_fini(x)
250 #define WWL(x, c) ww_mutex_lock(x, c)
251 #define WWT(x) ww_mutex_trylock(x)
252 #define WWL1(x) ww_mutex_lock(x, NULL)
253 #define WWU(x) ww_mutex_unlock(x)
256 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
259 * Generate different permutations of the same testcase, using
260 * the same basic lock-dependency/state events:
263 #define GENERATE_TESTCASE(name) \
265 static void name(void) { E(); }
267 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
269 static void name##_12(void) { E1(); E2(); } \
270 static void name##_21(void) { E2(); E1(); }
272 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
274 static void name##_123(void) { E1(); E2(); E3(); } \
275 static void name##_132(void) { E1(); E3(); E2(); } \
276 static void name##_213(void) { E2(); E1(); E3(); } \
277 static void name##_231(void) { E2(); E3(); E1(); } \
278 static void name##_312(void) { E3(); E1(); E2(); } \
279 static void name##_321(void) { E3(); E2(); E1(); }
288 LOCK(X2); /* this one should fail */
293 #include "locking-selftest-spin.h"
294 GENERATE_TESTCASE(AA_spin)
295 #include "locking-selftest-wlock.h"
296 GENERATE_TESTCASE(AA_wlock)
297 #include "locking-selftest-rlock.h"
298 GENERATE_TESTCASE(AA_rlock)
299 #include "locking-selftest-mutex.h"
300 GENERATE_TESTCASE(AA_mutex)
301 #include "locking-selftest-wsem.h"
302 GENERATE_TESTCASE(AA_wsem)
303 #include "locking-selftest-rsem.h"
304 GENERATE_TESTCASE(AA_rsem)
306 #ifdef CONFIG_RT_MUTEXES
307 #include "locking-selftest-rtmutex.h"
308 GENERATE_TESTCASE(AA_rtmutex);
314 * Special-case for read-locking, they are
315 * allowed to recurse on the same lock class:
317 static void rlock_AA1(void)
320 RL(X1); // this one should NOT fail
323 static void rlock_AA1B(void)
326 RL(X2); // this one should NOT fail
329 static void rsem_AA1(void)
332 RSL(X1); // this one should fail
335 static void rsem_AA1B(void)
338 RSL(X2); // this one should fail
341 * The mixing of read and write locks is not allowed:
343 static void rlock_AA2(void)
346 WL(X2); // this one should fail
349 static void rsem_AA2(void)
352 WSL(X2); // this one should fail
355 static void rlock_AA3(void)
358 RL(X2); // this one should fail
361 static void rsem_AA3(void)
364 RSL(X2); // this one should fail
373 static void rlock_ABBA1(void)
383 U(Y1); // should fail
386 static void rwsem_ABBA1(void)
396 MU(Y1); // should fail
405 * This test case is aimed at poking whether the chain cache prevents us from
406 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
407 * read/write locks, the following case may happen
409 * { read_lock(A)->lock(B) dependency exists }
415 * { Not a deadlock, B -> A is added in the chain cache }
421 * { B->A found in chain cache, not reported as a deadlock }
424 static void rlock_chaincache_ABBA1(void)
439 U(Y1); // should fail
448 static void rlock_ABBA2(void)
458 U(Y1); // should NOT fail
461 static void rwsem_ABBA2(void)
471 MU(Y1); // should fail
481 static void rlock_ABBA3(void)
491 U(Y1); // should fail
494 static void rwsem_ABBA3(void)
504 MU(Y1); // should fail
513 LOCK_UNLOCK_2(A, B); \
514 LOCK_UNLOCK_2(B, A); /* fail */
519 #include "locking-selftest-spin.h"
520 GENERATE_TESTCASE(ABBA_spin)
521 #include "locking-selftest-wlock.h"
522 GENERATE_TESTCASE(ABBA_wlock)
523 #include "locking-selftest-rlock.h"
524 GENERATE_TESTCASE(ABBA_rlock)
525 #include "locking-selftest-mutex.h"
526 GENERATE_TESTCASE(ABBA_mutex)
527 #include "locking-selftest-wsem.h"
528 GENERATE_TESTCASE(ABBA_wsem)
529 #include "locking-selftest-rsem.h"
530 GENERATE_TESTCASE(ABBA_rsem)
532 #ifdef CONFIG_RT_MUTEXES
533 #include "locking-selftest-rtmutex.h"
534 GENERATE_TESTCASE(ABBA_rtmutex);
545 LOCK_UNLOCK_2(A, B); \
546 LOCK_UNLOCK_2(B, C); \
547 LOCK_UNLOCK_2(C, A); /* fail */
552 #include "locking-selftest-spin.h"
553 GENERATE_TESTCASE(ABBCCA_spin)
554 #include "locking-selftest-wlock.h"
555 GENERATE_TESTCASE(ABBCCA_wlock)
556 #include "locking-selftest-rlock.h"
557 GENERATE_TESTCASE(ABBCCA_rlock)
558 #include "locking-selftest-mutex.h"
559 GENERATE_TESTCASE(ABBCCA_mutex)
560 #include "locking-selftest-wsem.h"
561 GENERATE_TESTCASE(ABBCCA_wsem)
562 #include "locking-selftest-rsem.h"
563 GENERATE_TESTCASE(ABBCCA_rsem)
565 #ifdef CONFIG_RT_MUTEXES
566 #include "locking-selftest-rtmutex.h"
567 GENERATE_TESTCASE(ABBCCA_rtmutex);
578 LOCK_UNLOCK_2(A, B); \
579 LOCK_UNLOCK_2(C, A); \
580 LOCK_UNLOCK_2(B, C); /* fail */
585 #include "locking-selftest-spin.h"
586 GENERATE_TESTCASE(ABCABC_spin)
587 #include "locking-selftest-wlock.h"
588 GENERATE_TESTCASE(ABCABC_wlock)
589 #include "locking-selftest-rlock.h"
590 GENERATE_TESTCASE(ABCABC_rlock)
591 #include "locking-selftest-mutex.h"
592 GENERATE_TESTCASE(ABCABC_mutex)
593 #include "locking-selftest-wsem.h"
594 GENERATE_TESTCASE(ABCABC_wsem)
595 #include "locking-selftest-rsem.h"
596 GENERATE_TESTCASE(ABCABC_rsem)
598 #ifdef CONFIG_RT_MUTEXES
599 #include "locking-selftest-rtmutex.h"
600 GENERATE_TESTCASE(ABCABC_rtmutex);
606 * AB BC CD DA deadlock:
611 LOCK_UNLOCK_2(A, B); \
612 LOCK_UNLOCK_2(B, C); \
613 LOCK_UNLOCK_2(C, D); \
614 LOCK_UNLOCK_2(D, A); /* fail */
619 #include "locking-selftest-spin.h"
620 GENERATE_TESTCASE(ABBCCDDA_spin)
621 #include "locking-selftest-wlock.h"
622 GENERATE_TESTCASE(ABBCCDDA_wlock)
623 #include "locking-selftest-rlock.h"
624 GENERATE_TESTCASE(ABBCCDDA_rlock)
625 #include "locking-selftest-mutex.h"
626 GENERATE_TESTCASE(ABBCCDDA_mutex)
627 #include "locking-selftest-wsem.h"
628 GENERATE_TESTCASE(ABBCCDDA_wsem)
629 #include "locking-selftest-rsem.h"
630 GENERATE_TESTCASE(ABBCCDDA_rsem)
632 #ifdef CONFIG_RT_MUTEXES
633 #include "locking-selftest-rtmutex.h"
634 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
640 * AB CD BD DA deadlock:
644 LOCK_UNLOCK_2(A, B); \
645 LOCK_UNLOCK_2(C, D); \
646 LOCK_UNLOCK_2(B, D); \
647 LOCK_UNLOCK_2(D, A); /* fail */
652 #include "locking-selftest-spin.h"
653 GENERATE_TESTCASE(ABCDBDDA_spin)
654 #include "locking-selftest-wlock.h"
655 GENERATE_TESTCASE(ABCDBDDA_wlock)
656 #include "locking-selftest-rlock.h"
657 GENERATE_TESTCASE(ABCDBDDA_rlock)
658 #include "locking-selftest-mutex.h"
659 GENERATE_TESTCASE(ABCDBDDA_mutex)
660 #include "locking-selftest-wsem.h"
661 GENERATE_TESTCASE(ABCDBDDA_wsem)
662 #include "locking-selftest-rsem.h"
663 GENERATE_TESTCASE(ABCDBDDA_rsem)
665 #ifdef CONFIG_RT_MUTEXES
666 #include "locking-selftest-rtmutex.h"
667 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
673 * AB CD BC DA deadlock:
677 LOCK_UNLOCK_2(A, B); \
678 LOCK_UNLOCK_2(C, D); \
679 LOCK_UNLOCK_2(B, C); \
680 LOCK_UNLOCK_2(D, A); /* fail */
685 #include "locking-selftest-spin.h"
686 GENERATE_TESTCASE(ABCDBCDA_spin)
687 #include "locking-selftest-wlock.h"
688 GENERATE_TESTCASE(ABCDBCDA_wlock)
689 #include "locking-selftest-rlock.h"
690 GENERATE_TESTCASE(ABCDBCDA_rlock)
691 #include "locking-selftest-mutex.h"
692 GENERATE_TESTCASE(ABCDBCDA_mutex)
693 #include "locking-selftest-wsem.h"
694 GENERATE_TESTCASE(ABCDBCDA_wsem)
695 #include "locking-selftest-rsem.h"
696 GENERATE_TESTCASE(ABCDBCDA_rsem)
698 #ifdef CONFIG_RT_MUTEXES
699 #include "locking-selftest-rtmutex.h"
700 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
712 UNLOCK(A); /* fail */
717 #include "locking-selftest-spin.h"
718 GENERATE_TESTCASE(double_unlock_spin)
719 #include "locking-selftest-wlock.h"
720 GENERATE_TESTCASE(double_unlock_wlock)
721 #include "locking-selftest-rlock.h"
722 GENERATE_TESTCASE(double_unlock_rlock)
723 #include "locking-selftest-mutex.h"
724 GENERATE_TESTCASE(double_unlock_mutex)
725 #include "locking-selftest-wsem.h"
726 GENERATE_TESTCASE(double_unlock_wsem)
727 #include "locking-selftest-rsem.h"
728 GENERATE_TESTCASE(double_unlock_rsem)
730 #ifdef CONFIG_RT_MUTEXES
731 #include "locking-selftest-rtmutex.h"
732 GENERATE_TESTCASE(double_unlock_rtmutex);
738 * initializing a held lock:
748 #include "locking-selftest-spin.h"
749 GENERATE_TESTCASE(init_held_spin)
750 #include "locking-selftest-wlock.h"
751 GENERATE_TESTCASE(init_held_wlock)
752 #include "locking-selftest-rlock.h"
753 GENERATE_TESTCASE(init_held_rlock)
754 #include "locking-selftest-mutex.h"
755 GENERATE_TESTCASE(init_held_mutex)
756 #include "locking-selftest-wsem.h"
757 GENERATE_TESTCASE(init_held_wsem)
758 #include "locking-selftest-rsem.h"
759 GENERATE_TESTCASE(init_held_rsem)
761 #ifdef CONFIG_RT_MUTEXES
762 #include "locking-selftest-rtmutex.h"
763 GENERATE_TESTCASE(init_held_rtmutex);
769 * locking an irq-safe lock with irqs enabled:
784 * Generate 24 testcases:
786 #include "locking-selftest-spin-hardirq.h"
787 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
789 #include "locking-selftest-rlock-hardirq.h"
790 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
792 #include "locking-selftest-wlock-hardirq.h"
793 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
795 #include "locking-selftest-spin-softirq.h"
796 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
798 #include "locking-selftest-rlock-softirq.h"
799 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
801 #include "locking-selftest-wlock-softirq.h"
802 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
808 * Enabling hardirqs with a softirq-safe lock held:
825 * Generate 12 testcases:
827 #include "locking-selftest-spin.h"
828 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
830 #include "locking-selftest-wlock.h"
831 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
833 #include "locking-selftest-rlock.h"
834 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
840 * Enabling irqs with an irq-safe lock held:
857 * Generate 24 testcases:
859 #include "locking-selftest-spin-hardirq.h"
860 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
862 #include "locking-selftest-rlock-hardirq.h"
863 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
865 #include "locking-selftest-wlock-hardirq.h"
866 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
868 #include "locking-selftest-spin-softirq.h"
869 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
871 #include "locking-selftest-rlock-softirq.h"
872 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
874 #include "locking-selftest-wlock-softirq.h"
875 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
881 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
903 * Generate 36 testcases:
905 #include "locking-selftest-spin-hardirq.h"
906 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
908 #include "locking-selftest-rlock-hardirq.h"
909 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
911 #include "locking-selftest-wlock-hardirq.h"
912 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
914 #include "locking-selftest-spin-softirq.h"
915 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
917 #include "locking-selftest-rlock-softirq.h"
918 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
920 #include "locking-selftest-wlock-softirq.h"
921 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
928 * If a lock turns into softirq-safe, but earlier it took
929 * a softirq-unsafe lock:
951 * Generate 36 testcases:
953 #include "locking-selftest-spin-hardirq.h"
954 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
956 #include "locking-selftest-rlock-hardirq.h"
957 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
959 #include "locking-selftest-wlock-hardirq.h"
960 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
962 #include "locking-selftest-spin-softirq.h"
963 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
965 #include "locking-selftest-rlock-softirq.h"
966 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
968 #include "locking-selftest-wlock-softirq.h"
969 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
976 * read-lock / write-lock irq inversion.
980 * CPU#1 is at #1, i.e. it has write-locked A, but has not
983 * CPU#2 is at #2, i.e. it has locked B.
985 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
987 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
1013 * Generate 36 testcases:
1015 #include "locking-selftest-spin-hardirq.h"
1016 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
1018 #include "locking-selftest-rlock-hardirq.h"
1019 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
1021 #include "locking-selftest-wlock-hardirq.h"
1022 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
1024 #include "locking-selftest-spin-softirq.h"
1025 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
1027 #include "locking-selftest-rlock-softirq.h"
1028 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
1030 #include "locking-selftest-wlock-softirq.h"
1031 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
1038 * write-read / write-read / write-read deadlock even if read is recursive
1062 #include "locking-selftest-rlock.h"
1063 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
1070 * write-write / read-read / write-read deadlock even if read is recursive
1094 #include "locking-selftest-rlock.h"
1095 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
1102 * write-write / read-read / read-write is not deadlock when read is recursive
1126 #include "locking-selftest-rlock.h"
1127 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
1134 * write-read / read-read / write-write is not deadlock when read is recursive
1158 #include "locking-selftest-rlock.h"
1159 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
1165 * read-lock / write-lock recursion that is actually safe.
1190 * Generate 24 testcases:
1192 #include "locking-selftest-hardirq.h"
1193 #include "locking-selftest-rlock.h"
1194 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
1196 #include "locking-selftest-wlock.h"
1197 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
1199 #include "locking-selftest-softirq.h"
1200 #include "locking-selftest-rlock.h"
1201 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
1203 #include "locking-selftest-wlock.h"
1204 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
1211 * read-lock / write-lock recursion that is unsafe.
1236 * Generate 24 testcases:
1238 #include "locking-selftest-hardirq.h"
1239 #include "locking-selftest-rlock.h"
1240 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
1242 #include "locking-selftest-wlock.h"
1243 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
1245 #include "locking-selftest-softirq.h"
1246 #include "locking-selftest-rlock.h"
1247 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
1249 #include "locking-selftest-wlock.h"
1250 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
1256 * read-lock / write-lock recursion that is unsafe.
1258 * A is a ENABLED_*_READ lock
1259 * B is a USED_IN_*_READ lock
1265 * write_lock(A); // if this one is read_lock(), no deadlock
1290 * Generate 24 testcases:
1292 #include "locking-selftest-hardirq.h"
1293 #include "locking-selftest-rlock.h"
1294 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
1296 #include "locking-selftest-wlock.h"
1297 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
1299 #include "locking-selftest-softirq.h"
1300 #include "locking-selftest-rlock.h"
1301 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
1303 #include "locking-selftest-wlock.h"
1304 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
1306 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1307 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1308 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1309 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1310 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1311 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1312 #ifdef CONFIG_RT_MUTEXES
1313 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1316 # define I_SPINLOCK(x)
1317 # define I_RWLOCK(x)
1324 # define I_RTMUTEX(x)
1327 #ifdef CONFIG_RT_MUTEXES
1328 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1330 #define I2_RTMUTEX(x)
1344 raw_spin_lock_init(&lock_##x); \
1345 rwlock_init(&rwlock_##x); \
1346 mutex_init(&mutex_##x); \
1347 init_rwsem(&rwsem_##x); \
1351 static void reset_locks(void)
1353 local_irq_disable();
1354 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1355 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1357 I1(A); I1(B); I1(C); I1(D);
1358 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1359 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
1361 I2(A); I2(B); I2(C); I2(D);
1362 init_shared_classes();
1364 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1365 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1366 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1367 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
1373 static int testcase_total;
1374 static int testcase_successes;
1375 static int expected_testcase_failures;
1376 static int unexpected_testcase_failures;
1378 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1380 unsigned long saved_preempt_count = preempt_count();
1382 WARN_ON(irqs_disabled());
1386 * Filter out expected failures:
1388 #ifndef CONFIG_PROVE_LOCKING
1389 if (expected == FAILURE && debug_locks) {
1390 expected_testcase_failures++;
1395 if (debug_locks != expected) {
1396 unexpected_testcase_failures++;
1399 testcase_successes++;
1404 if (debug_locks_verbose)
1405 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1406 lockclass_mask, debug_locks, expected);
1408 * Some tests (e.g. double-unlock) might corrupt the preemption
1409 * count, so restore it:
1411 preempt_count_set(saved_preempt_count);
1412 #ifdef CONFIG_TRACE_IRQFLAGS
1413 if (softirq_count())
1414 current->softirqs_enabled = 0;
1416 current->softirqs_enabled = 1;
1422 #ifdef CONFIG_RT_MUTEXES
1423 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1425 #define dotest_rt(fn, e, m)
1428 static inline void print_testname(const char *testname)
1430 printk("%33s:", testname);
1433 #define DO_TESTCASE_1(desc, name, nr) \
1434 print_testname(desc"/"#nr); \
1435 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1438 #define DO_TESTCASE_1B(desc, name, nr) \
1439 print_testname(desc"/"#nr); \
1440 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1443 #define DO_TESTCASE_1RR(desc, name, nr) \
1444 print_testname(desc"/"#nr); \
1446 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1449 #define DO_TESTCASE_1RRB(desc, name, nr) \
1450 print_testname(desc"/"#nr); \
1452 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1456 #define DO_TESTCASE_3(desc, name, nr) \
1457 print_testname(desc"/"#nr); \
1458 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1459 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1460 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1463 #define DO_TESTCASE_3RW(desc, name, nr) \
1464 print_testname(desc"/"#nr); \
1465 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1466 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1467 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1470 #define DO_TESTCASE_2RW(desc, name, nr) \
1471 print_testname(desc"/"#nr); \
1473 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1474 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1477 #define DO_TESTCASE_2x2RW(desc, name, nr) \
1478 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
1479 DO_TESTCASE_2RW("soft-"desc, name##_soft, nr) \
1481 #define DO_TESTCASE_6x2x2RW(desc, name) \
1482 DO_TESTCASE_2x2RW(desc, name, 123); \
1483 DO_TESTCASE_2x2RW(desc, name, 132); \
1484 DO_TESTCASE_2x2RW(desc, name, 213); \
1485 DO_TESTCASE_2x2RW(desc, name, 231); \
1486 DO_TESTCASE_2x2RW(desc, name, 312); \
1487 DO_TESTCASE_2x2RW(desc, name, 321);
1489 #define DO_TESTCASE_6(desc, name) \
1490 print_testname(desc); \
1491 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1492 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1493 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1494 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1495 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1496 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1497 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1500 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1501 print_testname(desc); \
1502 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1503 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1504 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1505 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1506 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1507 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1508 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1512 * 'read' variant: rlocks must not trigger.
1514 #define DO_TESTCASE_6R(desc, name) \
1515 print_testname(desc); \
1516 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1517 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1518 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1519 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1520 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1521 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1522 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1525 #define DO_TESTCASE_2I(desc, name, nr) \
1526 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1527 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1529 #define DO_TESTCASE_2IB(desc, name, nr) \
1530 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1531 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1533 #define DO_TESTCASE_6I(desc, name, nr) \
1534 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1535 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1537 #define DO_TESTCASE_6IRW(desc, name, nr) \
1538 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1539 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1541 #define DO_TESTCASE_2x3(desc, name) \
1542 DO_TESTCASE_3(desc, name, 12); \
1543 DO_TESTCASE_3(desc, name, 21);
1545 #define DO_TESTCASE_2x6(desc, name) \
1546 DO_TESTCASE_6I(desc, name, 12); \
1547 DO_TESTCASE_6I(desc, name, 21);
1549 #define DO_TESTCASE_6x2(desc, name) \
1550 DO_TESTCASE_2I(desc, name, 123); \
1551 DO_TESTCASE_2I(desc, name, 132); \
1552 DO_TESTCASE_2I(desc, name, 213); \
1553 DO_TESTCASE_2I(desc, name, 231); \
1554 DO_TESTCASE_2I(desc, name, 312); \
1555 DO_TESTCASE_2I(desc, name, 321);
1557 #define DO_TESTCASE_6x2B(desc, name) \
1558 DO_TESTCASE_2IB(desc, name, 123); \
1559 DO_TESTCASE_2IB(desc, name, 132); \
1560 DO_TESTCASE_2IB(desc, name, 213); \
1561 DO_TESTCASE_2IB(desc, name, 231); \
1562 DO_TESTCASE_2IB(desc, name, 312); \
1563 DO_TESTCASE_2IB(desc, name, 321);
1565 #define DO_TESTCASE_6x1RR(desc, name) \
1566 DO_TESTCASE_1RR(desc, name, 123); \
1567 DO_TESTCASE_1RR(desc, name, 132); \
1568 DO_TESTCASE_1RR(desc, name, 213); \
1569 DO_TESTCASE_1RR(desc, name, 231); \
1570 DO_TESTCASE_1RR(desc, name, 312); \
1571 DO_TESTCASE_1RR(desc, name, 321);
1573 #define DO_TESTCASE_6x1RRB(desc, name) \
1574 DO_TESTCASE_1RRB(desc, name, 123); \
1575 DO_TESTCASE_1RRB(desc, name, 132); \
1576 DO_TESTCASE_1RRB(desc, name, 213); \
1577 DO_TESTCASE_1RRB(desc, name, 231); \
1578 DO_TESTCASE_1RRB(desc, name, 312); \
1579 DO_TESTCASE_1RRB(desc, name, 321);
1581 #define DO_TESTCASE_6x6(desc, name) \
1582 DO_TESTCASE_6I(desc, name, 123); \
1583 DO_TESTCASE_6I(desc, name, 132); \
1584 DO_TESTCASE_6I(desc, name, 213); \
1585 DO_TESTCASE_6I(desc, name, 231); \
1586 DO_TESTCASE_6I(desc, name, 312); \
1587 DO_TESTCASE_6I(desc, name, 321);
1589 #define DO_TESTCASE_6x6RW(desc, name) \
1590 DO_TESTCASE_6IRW(desc, name, 123); \
1591 DO_TESTCASE_6IRW(desc, name, 132); \
1592 DO_TESTCASE_6IRW(desc, name, 213); \
1593 DO_TESTCASE_6IRW(desc, name, 231); \
1594 DO_TESTCASE_6IRW(desc, name, 312); \
1595 DO_TESTCASE_6IRW(desc, name, 321);
1597 static void ww_test_fail_acquire(void)
1606 if (WARN_ON(!o.ctx) ||
1610 /* No lockdep test, pure API */
1612 WARN_ON(ret != -EALREADY);
1620 WARN_ON(ret != -EDEADLK);
1625 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1627 DEBUG_LOCKS_WARN_ON(1);
1631 static void ww_test_normal(void)
1638 * None of the ww_mutex codepaths should be taken in the 'normal'
1639 * mutex calls. The easiest way to verify this is by using the
1640 * normal mutex calls, and making sure o.ctx is unmodified.
1643 /* mutex_lock (and indirectly, mutex_lock_nested) */
1644 o.ctx = (void *)~0UL;
1645 mutex_lock(&o.base);
1646 mutex_unlock(&o.base);
1647 WARN_ON(o.ctx != (void *)~0UL);
1649 /* mutex_lock_interruptible (and *_nested) */
1650 o.ctx = (void *)~0UL;
1651 ret = mutex_lock_interruptible(&o.base);
1653 mutex_unlock(&o.base);
1656 WARN_ON(o.ctx != (void *)~0UL);
1658 /* mutex_lock_killable (and *_nested) */
1659 o.ctx = (void *)~0UL;
1660 ret = mutex_lock_killable(&o.base);
1662 mutex_unlock(&o.base);
1665 WARN_ON(o.ctx != (void *)~0UL);
1667 /* trylock, succeeding */
1668 o.ctx = (void *)~0UL;
1669 ret = mutex_trylock(&o.base);
1672 mutex_unlock(&o.base);
1675 WARN_ON(o.ctx != (void *)~0UL);
1677 /* trylock, failing */
1678 o.ctx = (void *)~0UL;
1679 mutex_lock(&o.base);
1680 ret = mutex_trylock(&o.base);
1682 mutex_unlock(&o.base);
1683 WARN_ON(o.ctx != (void *)~0UL);
1686 o.ctx = (void *)~0UL;
1687 mutex_lock_nest_lock(&o.base, &t);
1688 mutex_unlock(&o.base);
1689 WARN_ON(o.ctx != (void *)~0UL);
1692 static void ww_test_two_contexts(void)
1698 static void ww_test_diff_class(void)
1701 #ifdef CONFIG_DEBUG_MUTEXES
1707 static void ww_test_context_done_twice(void)
1715 static void ww_test_context_unlock_twice(void)
1723 static void ww_test_context_fini_early(void)
1731 static void ww_test_context_lock_after_done(void)
1738 static void ww_test_object_unlock_twice(void)
1745 static void ww_test_object_lock_unbalanced(void)
1754 static void ww_test_object_lock_stale_context(void)
1761 static void ww_test_edeadlk_normal(void)
1765 mutex_lock(&o2.base);
1767 mutex_release(&o2.base.dep_map, _THIS_IP_);
1777 WARN_ON(ret != -EDEADLK);
1780 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1781 mutex_unlock(&o2.base);
1787 static void ww_test_edeadlk_normal_slow(void)
1791 mutex_lock(&o2.base);
1792 mutex_release(&o2.base.dep_map, _THIS_IP_);
1803 WARN_ON(ret != -EDEADLK);
1806 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1807 mutex_unlock(&o2.base);
1810 ww_mutex_lock_slow(&o2, &t);
1813 static void ww_test_edeadlk_no_unlock(void)
1817 mutex_lock(&o2.base);
1819 mutex_release(&o2.base.dep_map, _THIS_IP_);
1829 WARN_ON(ret != -EDEADLK);
1832 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1833 mutex_unlock(&o2.base);
1838 static void ww_test_edeadlk_no_unlock_slow(void)
1842 mutex_lock(&o2.base);
1843 mutex_release(&o2.base.dep_map, _THIS_IP_);
1854 WARN_ON(ret != -EDEADLK);
1857 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1858 mutex_unlock(&o2.base);
1860 ww_mutex_lock_slow(&o2, &t);
1863 static void ww_test_edeadlk_acquire_more(void)
1867 mutex_lock(&o2.base);
1868 mutex_release(&o2.base.dep_map, _THIS_IP_);
1879 WARN_ON(ret != -EDEADLK);
1884 static void ww_test_edeadlk_acquire_more_slow(void)
1888 mutex_lock(&o2.base);
1889 mutex_release(&o2.base.dep_map, _THIS_IP_);
1900 WARN_ON(ret != -EDEADLK);
1902 ww_mutex_lock_slow(&o3, &t);
1905 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1909 mutex_lock(&o2.base);
1910 mutex_release(&o2.base.dep_map, _THIS_IP_);
1913 mutex_lock(&o3.base);
1914 mutex_release(&o3.base.dep_map, _THIS_IP_);
1925 WARN_ON(ret != -EDEADLK);
1928 WARN_ON(ret != -EDEADLK);
1931 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1935 mutex_lock(&o2.base);
1936 mutex_release(&o2.base.dep_map, _THIS_IP_);
1939 mutex_lock(&o3.base);
1940 mutex_release(&o3.base.dep_map, _THIS_IP_);
1951 WARN_ON(ret != -EDEADLK);
1953 ww_mutex_lock_slow(&o3, &t);
1956 static void ww_test_edeadlk_acquire_wrong(void)
1960 mutex_lock(&o2.base);
1961 mutex_release(&o2.base.dep_map, _THIS_IP_);
1972 WARN_ON(ret != -EDEADLK);
1981 static void ww_test_edeadlk_acquire_wrong_slow(void)
1985 mutex_lock(&o2.base);
1986 mutex_release(&o2.base.dep_map, _THIS_IP_);
1997 WARN_ON(ret != -EDEADLK);
2003 ww_mutex_lock_slow(&o3, &t);
2006 static void ww_test_spin_nest_unlocked(void)
2008 raw_spin_lock_nest_lock(&lock_A, &o.base);
2012 static void ww_test_unneeded_slow(void)
2016 ww_mutex_lock_slow(&o, &t);
2019 static void ww_test_context_block(void)
2030 static void ww_test_context_try(void)
2045 static void ww_test_context_context(void)
2061 static void ww_test_try_block(void)
2073 static void ww_test_try_try(void)
2085 static void ww_test_try_context(void)
2098 static void ww_test_block_block(void)
2104 static void ww_test_block_try(void)
2113 static void ww_test_block_context(void)
2124 static void ww_test_spin_block(void)
2140 static void ww_test_spin_try(void)
2160 static void ww_test_spin_context(void)
2182 static void ww_tests(void)
2184 printk(" --------------------------------------------------------------------------\n");
2185 printk(" | Wound/wait tests |\n");
2186 printk(" ---------------------\n");
2188 print_testname("ww api failures");
2189 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
2190 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
2191 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
2194 print_testname("ww contexts mixing");
2195 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
2196 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
2199 print_testname("finishing ww context");
2200 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
2201 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
2202 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
2203 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
2206 print_testname("locking mismatches");
2207 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
2208 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
2209 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
2212 print_testname("EDEADLK handling");
2213 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
2214 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
2215 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
2216 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
2217 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
2218 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
2219 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
2220 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
2221 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
2222 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
2225 print_testname("spinlock nest unlocked");
2226 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
2229 printk(" -----------------------------------------------------\n");
2230 printk(" |block | try |context|\n");
2231 printk(" -----------------------------------------------------\n");
2233 print_testname("context");
2234 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
2235 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
2236 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
2239 print_testname("try");
2240 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
2241 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
2242 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
2245 print_testname("block");
2246 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
2247 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
2248 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
2251 print_testname("spinlock");
2252 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
2253 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
2254 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
2260 * <in hardirq handler>
2269 static void queued_read_lock_hardirq_RE_Er(void)
2272 read_lock(&rwlock_A);
2275 read_unlock(&rwlock_A);
2280 read_lock(&rwlock_A);
2281 read_unlock(&rwlock_A);
2287 * <in hardirq handler>
2294 * is not a deadlock.
2296 static void queued_read_lock_hardirq_ER_rE(void)
2300 read_lock(&rwlock_A);
2301 read_unlock(&rwlock_A);
2306 read_lock(&rwlock_A);
2309 read_unlock(&rwlock_A);
2317 * <in hardirq handler>
2321 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2323 static void queued_read_lock_hardirq_inversion(void)
2333 read_lock(&rwlock_A);
2334 read_unlock(&rwlock_A);
2338 read_lock(&rwlock_A);
2339 read_unlock(&rwlock_A);
2342 static void queued_read_lock_tests(void)
2344 printk(" --------------------------------------------------------------------------\n");
2345 printk(" | queued read lock tests |\n");
2346 printk(" ---------------------------\n");
2347 print_testname("hardirq read-lock/lock-read");
2348 dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
2351 print_testname("hardirq lock-read/read-lock");
2352 dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
2355 print_testname("hardirq inversion");
2356 dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
2360 void locking_selftest(void)
2363 * Got a locking failure before the selftest ran?
2366 printk("----------------------------------\n");
2367 printk("| Locking API testsuite disabled |\n");
2368 printk("----------------------------------\n");
2373 * treats read_lock() as recursive read locks for testing purpose
2375 force_read_lock_recursive = 1;
2378 * Run the testsuite:
2380 printk("------------------------\n");
2381 printk("| Locking API testsuite:\n");
2382 printk("----------------------------------------------------------------------------\n");
2383 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
2384 printk(" --------------------------------------------------------------------------\n");
2386 init_shared_classes();
2387 debug_locks_silent = !debug_locks_verbose;
2388 lockdep_set_selftest_task(current);
2390 DO_TESTCASE_6R("A-A deadlock", AA);
2391 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
2392 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
2393 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
2394 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
2395 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
2396 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
2397 DO_TESTCASE_6("double unlock", double_unlock);
2398 DO_TESTCASE_6("initialize held", init_held);
2400 printk(" --------------------------------------------------------------------------\n");
2401 print_testname("recursive read-lock");
2403 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
2405 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
2408 print_testname("recursive read-lock #2");
2410 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
2412 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
2415 print_testname("mixed read-write-lock");
2417 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
2419 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
2422 print_testname("mixed write-read-lock");
2424 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
2426 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
2429 print_testname("mixed read-lock/lock-write ABBA");
2431 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2433 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2435 print_testname("mixed read-lock/lock-read ABBA");
2437 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2439 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2441 print_testname("mixed write-lock/lock-write ABBA");
2443 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2445 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2447 print_testname("chain cached mixed R-L/L-W ABBA");
2449 dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2451 DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
2452 DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
2453 DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
2454 DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
2456 printk(" --------------------------------------------------------------------------\n");
2459 * irq-context testcases:
2461 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
2462 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
2463 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
2464 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
2465 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
2466 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
2468 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
2469 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
2470 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
2474 force_read_lock_recursive = 0;
2476 * queued_read_lock() specific test cases can be put here
2478 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
2479 queued_read_lock_tests();
2481 if (unexpected_testcase_failures) {
2482 printk("-----------------------------------------------------------------\n");
2484 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2485 unexpected_testcase_failures, testcase_total);
2486 printk("-----------------------------------------------------------------\n");
2487 } else if (expected_testcase_failures && testcase_successes) {
2488 printk("--------------------------------------------------------\n");
2489 printk("%3d out of %3d testcases failed, as expected. |\n",
2490 expected_testcase_failures, testcase_total);
2491 printk("----------------------------------------------------\n");
2493 } else if (expected_testcase_failures && !testcase_successes) {
2494 printk("--------------------------------------------------------\n");
2495 printk("All %3d testcases failed, as expected. |\n",
2496 expected_testcase_failures);
2497 printk("----------------------------------------\n");
2500 printk("-------------------------------------------------------\n");
2501 printk("Good, all %3d testcases passed! |\n",
2502 testcase_successes);
2503 printk("---------------------------------\n");
2506 lockdep_set_selftest_task(NULL);
2507 debug_locks_silent = 0;