2 * lib/locking-selftest.c
4 * Testsuite for various locking APIs: spinlocks, rwlocks,
5 * mutexes and rw-semaphores.
7 * It is checking both false positives and false negatives.
9 * Started by Ingo Molnar:
11 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
13 #include <linux/rwsem.h>
14 #include <linux/mutex.h>
15 #include <linux/ww_mutex.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/lockdep.h>
19 #include <linux/spinlock.h>
20 #include <linux/kallsyms.h>
21 #include <linux/interrupt.h>
22 #include <linux/debug_locks.h>
23 #include <linux/irqflags.h>
24 #include <linux/rtmutex.h>
27 * Change this to 1 if you want to see the failure printouts:
29 static unsigned int debug_locks_verbose
;
31 static DEFINE_WW_CLASS(ww_lockdep
);
33 static int __init
setup_debug_locks_verbose(char *str
)
35 get_option(&str
, &debug_locks_verbose
);
40 __setup("debug_locks_verbose=", setup_debug_locks_verbose
);
45 #define LOCKTYPE_SPIN 0x1
46 #define LOCKTYPE_RWLOCK 0x2
47 #define LOCKTYPE_MUTEX 0x4
48 #define LOCKTYPE_RWSEM 0x8
49 #define LOCKTYPE_WW 0x10
50 #define LOCKTYPE_RTMUTEX 0x20
52 static struct ww_acquire_ctx t
, t2
;
53 static struct ww_mutex o
, o2
, o3
;
56 * Normal standalone locks, for the circular and irq-context
59 static DEFINE_RAW_SPINLOCK(lock_A
);
60 static DEFINE_RAW_SPINLOCK(lock_B
);
61 static DEFINE_RAW_SPINLOCK(lock_C
);
62 static DEFINE_RAW_SPINLOCK(lock_D
);
64 static DEFINE_RWLOCK(rwlock_A
);
65 static DEFINE_RWLOCK(rwlock_B
);
66 static DEFINE_RWLOCK(rwlock_C
);
67 static DEFINE_RWLOCK(rwlock_D
);
69 static DEFINE_MUTEX(mutex_A
);
70 static DEFINE_MUTEX(mutex_B
);
71 static DEFINE_MUTEX(mutex_C
);
72 static DEFINE_MUTEX(mutex_D
);
74 static DECLARE_RWSEM(rwsem_A
);
75 static DECLARE_RWSEM(rwsem_B
);
76 static DECLARE_RWSEM(rwsem_C
);
77 static DECLARE_RWSEM(rwsem_D
);
79 #ifdef CONFIG_RT_MUTEXES
81 static DEFINE_RT_MUTEX(rtmutex_A
);
82 static DEFINE_RT_MUTEX(rtmutex_B
);
83 static DEFINE_RT_MUTEX(rtmutex_C
);
84 static DEFINE_RT_MUTEX(rtmutex_D
);
89 * Locks that we initialize dynamically as well so that
90 * e.g. X1 and X2 becomes two instances of the same class,
91 * but X* and Y* are different classes. We do this so that
92 * we do not trigger a real lockup:
94 static DEFINE_RAW_SPINLOCK(lock_X1
);
95 static DEFINE_RAW_SPINLOCK(lock_X2
);
96 static DEFINE_RAW_SPINLOCK(lock_Y1
);
97 static DEFINE_RAW_SPINLOCK(lock_Y2
);
98 static DEFINE_RAW_SPINLOCK(lock_Z1
);
99 static DEFINE_RAW_SPINLOCK(lock_Z2
);
101 static DEFINE_RWLOCK(rwlock_X1
);
102 static DEFINE_RWLOCK(rwlock_X2
);
103 static DEFINE_RWLOCK(rwlock_Y1
);
104 static DEFINE_RWLOCK(rwlock_Y2
);
105 static DEFINE_RWLOCK(rwlock_Z1
);
106 static DEFINE_RWLOCK(rwlock_Z2
);
108 static DEFINE_MUTEX(mutex_X1
);
109 static DEFINE_MUTEX(mutex_X2
);
110 static DEFINE_MUTEX(mutex_Y1
);
111 static DEFINE_MUTEX(mutex_Y2
);
112 static DEFINE_MUTEX(mutex_Z1
);
113 static DEFINE_MUTEX(mutex_Z2
);
115 static DECLARE_RWSEM(rwsem_X1
);
116 static DECLARE_RWSEM(rwsem_X2
);
117 static DECLARE_RWSEM(rwsem_Y1
);
118 static DECLARE_RWSEM(rwsem_Y2
);
119 static DECLARE_RWSEM(rwsem_Z1
);
120 static DECLARE_RWSEM(rwsem_Z2
);
122 #ifdef CONFIG_RT_MUTEXES
124 static DEFINE_RT_MUTEX(rtmutex_X1
);
125 static DEFINE_RT_MUTEX(rtmutex_X2
);
126 static DEFINE_RT_MUTEX(rtmutex_Y1
);
127 static DEFINE_RT_MUTEX(rtmutex_Y2
);
128 static DEFINE_RT_MUTEX(rtmutex_Z1
);
129 static DEFINE_RT_MUTEX(rtmutex_Z2
);
134 * non-inlined runtime initializers, to let separate locks share
135 * the same lock-class:
137 #define INIT_CLASS_FUNC(class) \
138 static noinline void \
139 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
140 struct mutex *mutex, struct rw_semaphore *rwsem)\
142 raw_spin_lock_init(lock); \
143 rwlock_init(rwlock); \
152 static void init_shared_classes(void)
154 #ifdef CONFIG_RT_MUTEXES
155 static struct lock_class_key rt_X
, rt_Y
, rt_Z
;
157 __rt_mutex_init(&rtmutex_X1
, __func__
, &rt_X
);
158 __rt_mutex_init(&rtmutex_X2
, __func__
, &rt_X
);
159 __rt_mutex_init(&rtmutex_Y1
, __func__
, &rt_Y
);
160 __rt_mutex_init(&rtmutex_Y2
, __func__
, &rt_Y
);
161 __rt_mutex_init(&rtmutex_Z1
, __func__
, &rt_Z
);
162 __rt_mutex_init(&rtmutex_Z2
, __func__
, &rt_Z
);
165 init_class_X(&lock_X1
, &rwlock_X1
, &mutex_X1
, &rwsem_X1
);
166 init_class_X(&lock_X2
, &rwlock_X2
, &mutex_X2
, &rwsem_X2
);
168 init_class_Y(&lock_Y1
, &rwlock_Y1
, &mutex_Y1
, &rwsem_Y1
);
169 init_class_Y(&lock_Y2
, &rwlock_Y2
, &mutex_Y2
, &rwsem_Y2
);
171 init_class_Z(&lock_Z1
, &rwlock_Z1
, &mutex_Z1
, &rwsem_Z1
);
172 init_class_Z(&lock_Z2
, &rwlock_Z2
, &mutex_Z2
, &rwsem_Z2
);
176 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
177 * The following functions use a lock from a simulated hardirq/softirq
178 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
181 #define HARDIRQ_DISABLE local_irq_disable
182 #define HARDIRQ_ENABLE local_irq_enable
184 #define HARDIRQ_ENTER() \
185 local_irq_disable(); \
189 #define HARDIRQ_EXIT() \
193 #define SOFTIRQ_DISABLE local_bh_disable
194 #define SOFTIRQ_ENABLE local_bh_enable
196 #define SOFTIRQ_ENTER() \
197 local_bh_disable(); \
198 local_irq_disable(); \
199 lockdep_softirq_enter(); \
200 WARN_ON(!in_softirq());
202 #define SOFTIRQ_EXIT() \
203 lockdep_softirq_exit(); \
204 local_irq_enable(); \
208 * Shortcuts for lock/unlock API variants, to keep
209 * the testcases compact:
211 #define L(x) raw_spin_lock(&lock_##x)
212 #define U(x) raw_spin_unlock(&lock_##x)
213 #define LU(x) L(x); U(x)
214 #define SI(x) raw_spin_lock_init(&lock_##x)
216 #define WL(x) write_lock(&rwlock_##x)
217 #define WU(x) write_unlock(&rwlock_##x)
218 #define WLU(x) WL(x); WU(x)
220 #define RL(x) read_lock(&rwlock_##x)
221 #define RU(x) read_unlock(&rwlock_##x)
222 #define RLU(x) RL(x); RU(x)
223 #define RWI(x) rwlock_init(&rwlock_##x)
225 #define ML(x) mutex_lock(&mutex_##x)
226 #define MU(x) mutex_unlock(&mutex_##x)
227 #define MI(x) mutex_init(&mutex_##x)
229 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
230 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
231 #define RTI(x) rt_mutex_init(&rtmutex_##x)
233 #define WSL(x) down_write(&rwsem_##x)
234 #define WSU(x) up_write(&rwsem_##x)
236 #define RSL(x) down_read(&rwsem_##x)
237 #define RSU(x) up_read(&rwsem_##x)
238 #define RWSI(x) init_rwsem(&rwsem_##x)
240 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
241 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
243 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
245 #define WWAD(x) ww_acquire_done(x)
246 #define WWAF(x) ww_acquire_fini(x)
248 #define WWL(x, c) ww_mutex_lock(x, c)
249 #define WWT(x) ww_mutex_trylock(x)
250 #define WWL1(x) ww_mutex_lock(x, NULL)
251 #define WWU(x) ww_mutex_unlock(x)
254 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
257 * Generate different permutations of the same testcase, using
258 * the same basic lock-dependency/state events:
261 #define GENERATE_TESTCASE(name) \
263 static void name(void) { E(); }
265 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
267 static void name##_12(void) { E1(); E2(); } \
268 static void name##_21(void) { E2(); E1(); }
270 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
272 static void name##_123(void) { E1(); E2(); E3(); } \
273 static void name##_132(void) { E1(); E3(); E2(); } \
274 static void name##_213(void) { E2(); E1(); E3(); } \
275 static void name##_231(void) { E2(); E3(); E1(); } \
276 static void name##_312(void) { E3(); E1(); E2(); } \
277 static void name##_321(void) { E3(); E2(); E1(); }
286 LOCK(X2); /* this one should fail */
291 #include "locking-selftest-spin.h"
292 GENERATE_TESTCASE(AA_spin
)
293 #include "locking-selftest-wlock.h"
294 GENERATE_TESTCASE(AA_wlock
)
295 #include "locking-selftest-rlock.h"
296 GENERATE_TESTCASE(AA_rlock
)
297 #include "locking-selftest-mutex.h"
298 GENERATE_TESTCASE(AA_mutex
)
299 #include "locking-selftest-wsem.h"
300 GENERATE_TESTCASE(AA_wsem
)
301 #include "locking-selftest-rsem.h"
302 GENERATE_TESTCASE(AA_rsem
)
304 #ifdef CONFIG_RT_MUTEXES
305 #include "locking-selftest-rtmutex.h"
306 GENERATE_TESTCASE(AA_rtmutex
);
312 * Special-case for read-locking, they are
313 * allowed to recurse on the same lock class:
315 static void rlock_AA1(void)
318 RL(X1
); // this one should NOT fail
321 static void rlock_AA1B(void)
324 RL(X2
); // this one should NOT fail
327 static void rsem_AA1(void)
330 RSL(X1
); // this one should fail
333 static void rsem_AA1B(void)
336 RSL(X2
); // this one should fail
339 * The mixing of read and write locks is not allowed:
341 static void rlock_AA2(void)
344 WL(X2
); // this one should fail
347 static void rsem_AA2(void)
350 WSL(X2
); // this one should fail
353 static void rlock_AA3(void)
356 RL(X2
); // this one should fail
359 static void rsem_AA3(void)
362 RSL(X2
); // this one should fail
371 static void rlock_ABBA1(void)
381 U(Y1
); // should fail
384 static void rwsem_ABBA1(void)
394 MU(Y1
); // should fail
403 static void rlock_ABBA2(void)
413 U(Y1
); // should NOT fail
416 static void rwsem_ABBA2(void)
426 MU(Y1
); // should fail
436 static void rlock_ABBA3(void)
446 U(Y1
); // should fail
449 static void rwsem_ABBA3(void)
459 MU(Y1
); // should fail
468 LOCK_UNLOCK_2(A, B); \
469 LOCK_UNLOCK_2(B, A); /* fail */
474 #include "locking-selftest-spin.h"
475 GENERATE_TESTCASE(ABBA_spin
)
476 #include "locking-selftest-wlock.h"
477 GENERATE_TESTCASE(ABBA_wlock
)
478 #include "locking-selftest-rlock.h"
479 GENERATE_TESTCASE(ABBA_rlock
)
480 #include "locking-selftest-mutex.h"
481 GENERATE_TESTCASE(ABBA_mutex
)
482 #include "locking-selftest-wsem.h"
483 GENERATE_TESTCASE(ABBA_wsem
)
484 #include "locking-selftest-rsem.h"
485 GENERATE_TESTCASE(ABBA_rsem
)
487 #ifdef CONFIG_RT_MUTEXES
488 #include "locking-selftest-rtmutex.h"
489 GENERATE_TESTCASE(ABBA_rtmutex
);
500 LOCK_UNLOCK_2(A, B); \
501 LOCK_UNLOCK_2(B, C); \
502 LOCK_UNLOCK_2(C, A); /* fail */
507 #include "locking-selftest-spin.h"
508 GENERATE_TESTCASE(ABBCCA_spin
)
509 #include "locking-selftest-wlock.h"
510 GENERATE_TESTCASE(ABBCCA_wlock
)
511 #include "locking-selftest-rlock.h"
512 GENERATE_TESTCASE(ABBCCA_rlock
)
513 #include "locking-selftest-mutex.h"
514 GENERATE_TESTCASE(ABBCCA_mutex
)
515 #include "locking-selftest-wsem.h"
516 GENERATE_TESTCASE(ABBCCA_wsem
)
517 #include "locking-selftest-rsem.h"
518 GENERATE_TESTCASE(ABBCCA_rsem
)
520 #ifdef CONFIG_RT_MUTEXES
521 #include "locking-selftest-rtmutex.h"
522 GENERATE_TESTCASE(ABBCCA_rtmutex
);
533 LOCK_UNLOCK_2(A, B); \
534 LOCK_UNLOCK_2(C, A); \
535 LOCK_UNLOCK_2(B, C); /* fail */
540 #include "locking-selftest-spin.h"
541 GENERATE_TESTCASE(ABCABC_spin
)
542 #include "locking-selftest-wlock.h"
543 GENERATE_TESTCASE(ABCABC_wlock
)
544 #include "locking-selftest-rlock.h"
545 GENERATE_TESTCASE(ABCABC_rlock
)
546 #include "locking-selftest-mutex.h"
547 GENERATE_TESTCASE(ABCABC_mutex
)
548 #include "locking-selftest-wsem.h"
549 GENERATE_TESTCASE(ABCABC_wsem
)
550 #include "locking-selftest-rsem.h"
551 GENERATE_TESTCASE(ABCABC_rsem
)
553 #ifdef CONFIG_RT_MUTEXES
554 #include "locking-selftest-rtmutex.h"
555 GENERATE_TESTCASE(ABCABC_rtmutex
);
561 * AB BC CD DA deadlock:
566 LOCK_UNLOCK_2(A, B); \
567 LOCK_UNLOCK_2(B, C); \
568 LOCK_UNLOCK_2(C, D); \
569 LOCK_UNLOCK_2(D, A); /* fail */
574 #include "locking-selftest-spin.h"
575 GENERATE_TESTCASE(ABBCCDDA_spin
)
576 #include "locking-selftest-wlock.h"
577 GENERATE_TESTCASE(ABBCCDDA_wlock
)
578 #include "locking-selftest-rlock.h"
579 GENERATE_TESTCASE(ABBCCDDA_rlock
)
580 #include "locking-selftest-mutex.h"
581 GENERATE_TESTCASE(ABBCCDDA_mutex
)
582 #include "locking-selftest-wsem.h"
583 GENERATE_TESTCASE(ABBCCDDA_wsem
)
584 #include "locking-selftest-rsem.h"
585 GENERATE_TESTCASE(ABBCCDDA_rsem
)
587 #ifdef CONFIG_RT_MUTEXES
588 #include "locking-selftest-rtmutex.h"
589 GENERATE_TESTCASE(ABBCCDDA_rtmutex
);
595 * AB CD BD DA deadlock:
599 LOCK_UNLOCK_2(A, B); \
600 LOCK_UNLOCK_2(C, D); \
601 LOCK_UNLOCK_2(B, D); \
602 LOCK_UNLOCK_2(D, A); /* fail */
607 #include "locking-selftest-spin.h"
608 GENERATE_TESTCASE(ABCDBDDA_spin
)
609 #include "locking-selftest-wlock.h"
610 GENERATE_TESTCASE(ABCDBDDA_wlock
)
611 #include "locking-selftest-rlock.h"
612 GENERATE_TESTCASE(ABCDBDDA_rlock
)
613 #include "locking-selftest-mutex.h"
614 GENERATE_TESTCASE(ABCDBDDA_mutex
)
615 #include "locking-selftest-wsem.h"
616 GENERATE_TESTCASE(ABCDBDDA_wsem
)
617 #include "locking-selftest-rsem.h"
618 GENERATE_TESTCASE(ABCDBDDA_rsem
)
620 #ifdef CONFIG_RT_MUTEXES
621 #include "locking-selftest-rtmutex.h"
622 GENERATE_TESTCASE(ABCDBDDA_rtmutex
);
628 * AB CD BC DA deadlock:
632 LOCK_UNLOCK_2(A, B); \
633 LOCK_UNLOCK_2(C, D); \
634 LOCK_UNLOCK_2(B, C); \
635 LOCK_UNLOCK_2(D, A); /* fail */
640 #include "locking-selftest-spin.h"
641 GENERATE_TESTCASE(ABCDBCDA_spin
)
642 #include "locking-selftest-wlock.h"
643 GENERATE_TESTCASE(ABCDBCDA_wlock
)
644 #include "locking-selftest-rlock.h"
645 GENERATE_TESTCASE(ABCDBCDA_rlock
)
646 #include "locking-selftest-mutex.h"
647 GENERATE_TESTCASE(ABCDBCDA_mutex
)
648 #include "locking-selftest-wsem.h"
649 GENERATE_TESTCASE(ABCDBCDA_wsem
)
650 #include "locking-selftest-rsem.h"
651 GENERATE_TESTCASE(ABCDBCDA_rsem
)
653 #ifdef CONFIG_RT_MUTEXES
654 #include "locking-selftest-rtmutex.h"
655 GENERATE_TESTCASE(ABCDBCDA_rtmutex
);
667 UNLOCK(A); /* fail */
672 #include "locking-selftest-spin.h"
673 GENERATE_TESTCASE(double_unlock_spin
)
674 #include "locking-selftest-wlock.h"
675 GENERATE_TESTCASE(double_unlock_wlock
)
676 #include "locking-selftest-rlock.h"
677 GENERATE_TESTCASE(double_unlock_rlock
)
678 #include "locking-selftest-mutex.h"
679 GENERATE_TESTCASE(double_unlock_mutex
)
680 #include "locking-selftest-wsem.h"
681 GENERATE_TESTCASE(double_unlock_wsem
)
682 #include "locking-selftest-rsem.h"
683 GENERATE_TESTCASE(double_unlock_rsem
)
685 #ifdef CONFIG_RT_MUTEXES
686 #include "locking-selftest-rtmutex.h"
687 GENERATE_TESTCASE(double_unlock_rtmutex
);
693 * initializing a held lock:
703 #include "locking-selftest-spin.h"
704 GENERATE_TESTCASE(init_held_spin
)
705 #include "locking-selftest-wlock.h"
706 GENERATE_TESTCASE(init_held_wlock
)
707 #include "locking-selftest-rlock.h"
708 GENERATE_TESTCASE(init_held_rlock
)
709 #include "locking-selftest-mutex.h"
710 GENERATE_TESTCASE(init_held_mutex
)
711 #include "locking-selftest-wsem.h"
712 GENERATE_TESTCASE(init_held_wsem
)
713 #include "locking-selftest-rsem.h"
714 GENERATE_TESTCASE(init_held_rsem
)
716 #ifdef CONFIG_RT_MUTEXES
717 #include "locking-selftest-rtmutex.h"
718 GENERATE_TESTCASE(init_held_rtmutex
);
724 * locking an irq-safe lock with irqs enabled:
739 * Generate 24 testcases:
741 #include "locking-selftest-spin-hardirq.h"
742 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin
)
744 #include "locking-selftest-rlock-hardirq.h"
745 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock
)
747 #include "locking-selftest-wlock-hardirq.h"
748 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock
)
750 #include "locking-selftest-spin-softirq.h"
751 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin
)
753 #include "locking-selftest-rlock-softirq.h"
754 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock
)
756 #include "locking-selftest-wlock-softirq.h"
757 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock
)
763 * Enabling hardirqs with a softirq-safe lock held:
780 * Generate 12 testcases:
782 #include "locking-selftest-spin.h"
783 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin
)
785 #include "locking-selftest-wlock.h"
786 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock
)
788 #include "locking-selftest-rlock.h"
789 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock
)
795 * Enabling irqs with an irq-safe lock held:
812 * Generate 24 testcases:
814 #include "locking-selftest-spin-hardirq.h"
815 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin
)
817 #include "locking-selftest-rlock-hardirq.h"
818 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock
)
820 #include "locking-selftest-wlock-hardirq.h"
821 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock
)
823 #include "locking-selftest-spin-softirq.h"
824 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin
)
826 #include "locking-selftest-rlock-softirq.h"
827 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock
)
829 #include "locking-selftest-wlock-softirq.h"
830 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock
)
836 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
858 * Generate 36 testcases:
860 #include "locking-selftest-spin-hardirq.h"
861 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin
)
863 #include "locking-selftest-rlock-hardirq.h"
864 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock
)
866 #include "locking-selftest-wlock-hardirq.h"
867 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock
)
869 #include "locking-selftest-spin-softirq.h"
870 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin
)
872 #include "locking-selftest-rlock-softirq.h"
873 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock
)
875 #include "locking-selftest-wlock-softirq.h"
876 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock
)
883 * If a lock turns into softirq-safe, but earlier it took
884 * a softirq-unsafe lock:
906 * Generate 36 testcases:
908 #include "locking-selftest-spin-hardirq.h"
909 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin
)
911 #include "locking-selftest-rlock-hardirq.h"
912 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock
)
914 #include "locking-selftest-wlock-hardirq.h"
915 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock
)
917 #include "locking-selftest-spin-softirq.h"
918 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin
)
920 #include "locking-selftest-rlock-softirq.h"
921 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock
)
923 #include "locking-selftest-wlock-softirq.h"
924 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock
)
931 * read-lock / write-lock irq inversion.
935 * CPU#1 is at #1, i.e. it has write-locked A, but has not
938 * CPU#2 is at #2, i.e. it has locked B.
940 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
942 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
968 * Generate 36 testcases:
970 #include "locking-selftest-spin-hardirq.h"
971 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin
)
973 #include "locking-selftest-rlock-hardirq.h"
974 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock
)
976 #include "locking-selftest-wlock-hardirq.h"
977 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock
)
979 #include "locking-selftest-spin-softirq.h"
980 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin
)
982 #include "locking-selftest-rlock-softirq.h"
983 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock
)
985 #include "locking-selftest-wlock-softirq.h"
986 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock
)
993 * read-lock / write-lock recursion that is actually safe.
1018 * Generate 12 testcases:
1020 #include "locking-selftest-hardirq.h"
1021 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard
)
1023 #include "locking-selftest-softirq.h"
1024 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft
)
1031 * read-lock / write-lock recursion that is unsafe.
1056 * Generate 12 testcases:
1058 #include "locking-selftest-hardirq.h"
1059 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
1061 #include "locking-selftest-softirq.h"
1062 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
1064 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1065 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1066 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1067 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1068 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1069 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1070 #ifdef CONFIG_RT_MUTEXES
1071 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1074 # define I_SPINLOCK(x)
1075 # define I_RWLOCK(x)
1082 # define I_RTMUTEX(x)
1085 #ifdef CONFIG_RT_MUTEXES
1086 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1088 #define I2_RTMUTEX(x)
1102 raw_spin_lock_init(&lock_##x); \
1103 rwlock_init(&rwlock_##x); \
1104 mutex_init(&mutex_##x); \
1105 init_rwsem(&rwsem_##x); \
1109 static void reset_locks(void)
1111 local_irq_disable();
1112 lockdep_free_key_range(&ww_lockdep
.acquire_key
, 1);
1113 lockdep_free_key_range(&ww_lockdep
.mutex_key
, 1);
1115 I1(A
); I1(B
); I1(C
); I1(D
);
1116 I1(X1
); I1(X2
); I1(Y1
); I1(Y2
); I1(Z1
); I1(Z2
);
1117 I_WW(t
); I_WW(t2
); I_WW(o
.base
); I_WW(o2
.base
); I_WW(o3
.base
);
1119 I2(A
); I2(B
); I2(C
); I2(D
);
1120 init_shared_classes();
1122 ww_mutex_init(&o
, &ww_lockdep
); ww_mutex_init(&o2
, &ww_lockdep
); ww_mutex_init(&o3
, &ww_lockdep
);
1123 memset(&t
, 0, sizeof(t
)); memset(&t2
, 0, sizeof(t2
));
1124 memset(&ww_lockdep
.acquire_key
, 0, sizeof(ww_lockdep
.acquire_key
));
1125 memset(&ww_lockdep
.mutex_key
, 0, sizeof(ww_lockdep
.mutex_key
));
1131 static int testcase_total
;
1132 static int testcase_successes
;
1133 static int expected_testcase_failures
;
1134 static int unexpected_testcase_failures
;
1136 static void dotest(void (*testcase_fn
)(void), int expected
, int lockclass_mask
)
1138 unsigned long saved_preempt_count
= preempt_count();
1140 WARN_ON(irqs_disabled());
1144 * Filter out expected failures:
1146 #ifndef CONFIG_PROVE_LOCKING
1147 if (expected
== FAILURE
&& debug_locks
) {
1148 expected_testcase_failures
++;
1153 if (debug_locks
!= expected
) {
1154 unexpected_testcase_failures
++;
1157 testcase_successes
++;
1162 if (debug_locks_verbose
)
1163 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1164 lockclass_mask
, debug_locks
, expected
);
1166 * Some tests (e.g. double-unlock) might corrupt the preemption
1167 * count, so restore it:
1169 preempt_count_set(saved_preempt_count
);
1170 #ifdef CONFIG_TRACE_IRQFLAGS
1171 if (softirq_count())
1172 current
->softirqs_enabled
= 0;
1174 current
->softirqs_enabled
= 1;
1180 #ifdef CONFIG_RT_MUTEXES
1181 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1183 #define dotest_rt(fn, e, m)
1186 static inline void print_testname(const char *testname
)
1188 printk("%33s:", testname
);
1191 #define DO_TESTCASE_1(desc, name, nr) \
1192 print_testname(desc"/"#nr); \
1193 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1196 #define DO_TESTCASE_1B(desc, name, nr) \
1197 print_testname(desc"/"#nr); \
1198 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1201 #define DO_TESTCASE_3(desc, name, nr) \
1202 print_testname(desc"/"#nr); \
1203 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1204 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1205 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1208 #define DO_TESTCASE_3RW(desc, name, nr) \
1209 print_testname(desc"/"#nr); \
1210 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1211 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1212 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1215 #define DO_TESTCASE_6(desc, name) \
1216 print_testname(desc); \
1217 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1218 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1219 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1220 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1221 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1222 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1223 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1226 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1227 print_testname(desc); \
1228 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1229 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1230 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1231 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1232 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1233 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1234 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1238 * 'read' variant: rlocks must not trigger.
1240 #define DO_TESTCASE_6R(desc, name) \
1241 print_testname(desc); \
1242 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1243 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1244 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1245 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1246 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1247 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1248 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1251 #define DO_TESTCASE_2I(desc, name, nr) \
1252 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1253 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1255 #define DO_TESTCASE_2IB(desc, name, nr) \
1256 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1257 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1259 #define DO_TESTCASE_6I(desc, name, nr) \
1260 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1261 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1263 #define DO_TESTCASE_6IRW(desc, name, nr) \
1264 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1265 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1267 #define DO_TESTCASE_2x3(desc, name) \
1268 DO_TESTCASE_3(desc, name, 12); \
1269 DO_TESTCASE_3(desc, name, 21);
1271 #define DO_TESTCASE_2x6(desc, name) \
1272 DO_TESTCASE_6I(desc, name, 12); \
1273 DO_TESTCASE_6I(desc, name, 21);
1275 #define DO_TESTCASE_6x2(desc, name) \
1276 DO_TESTCASE_2I(desc, name, 123); \
1277 DO_TESTCASE_2I(desc, name, 132); \
1278 DO_TESTCASE_2I(desc, name, 213); \
1279 DO_TESTCASE_2I(desc, name, 231); \
1280 DO_TESTCASE_2I(desc, name, 312); \
1281 DO_TESTCASE_2I(desc, name, 321);
1283 #define DO_TESTCASE_6x2B(desc, name) \
1284 DO_TESTCASE_2IB(desc, name, 123); \
1285 DO_TESTCASE_2IB(desc, name, 132); \
1286 DO_TESTCASE_2IB(desc, name, 213); \
1287 DO_TESTCASE_2IB(desc, name, 231); \
1288 DO_TESTCASE_2IB(desc, name, 312); \
1289 DO_TESTCASE_2IB(desc, name, 321);
1291 #define DO_TESTCASE_6x6(desc, name) \
1292 DO_TESTCASE_6I(desc, name, 123); \
1293 DO_TESTCASE_6I(desc, name, 132); \
1294 DO_TESTCASE_6I(desc, name, 213); \
1295 DO_TESTCASE_6I(desc, name, 231); \
1296 DO_TESTCASE_6I(desc, name, 312); \
1297 DO_TESTCASE_6I(desc, name, 321);
1299 #define DO_TESTCASE_6x6RW(desc, name) \
1300 DO_TESTCASE_6IRW(desc, name, 123); \
1301 DO_TESTCASE_6IRW(desc, name, 132); \
1302 DO_TESTCASE_6IRW(desc, name, 213); \
1303 DO_TESTCASE_6IRW(desc, name, 231); \
1304 DO_TESTCASE_6IRW(desc, name, 312); \
1305 DO_TESTCASE_6IRW(desc, name, 321);
1307 static void ww_test_fail_acquire(void)
1316 if (WARN_ON(!o
.ctx
) ||
1320 /* No lockdep test, pure API */
1322 WARN_ON(ret
!= -EALREADY
);
1330 WARN_ON(ret
!= -EDEADLK
);
1335 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1337 DEBUG_LOCKS_WARN_ON(1);
1341 static void ww_test_normal(void)
1348 * None of the ww_mutex codepaths should be taken in the 'normal'
1349 * mutex calls. The easiest way to verify this is by using the
1350 * normal mutex calls, and making sure o.ctx is unmodified.
1353 /* mutex_lock (and indirectly, mutex_lock_nested) */
1354 o
.ctx
= (void *)~0UL;
1355 mutex_lock(&o
.base
);
1356 mutex_unlock(&o
.base
);
1357 WARN_ON(o
.ctx
!= (void *)~0UL);
1359 /* mutex_lock_interruptible (and *_nested) */
1360 o
.ctx
= (void *)~0UL;
1361 ret
= mutex_lock_interruptible(&o
.base
);
1363 mutex_unlock(&o
.base
);
1366 WARN_ON(o
.ctx
!= (void *)~0UL);
1368 /* mutex_lock_killable (and *_nested) */
1369 o
.ctx
= (void *)~0UL;
1370 ret
= mutex_lock_killable(&o
.base
);
1372 mutex_unlock(&o
.base
);
1375 WARN_ON(o
.ctx
!= (void *)~0UL);
1377 /* trylock, succeeding */
1378 o
.ctx
= (void *)~0UL;
1379 ret
= mutex_trylock(&o
.base
);
1382 mutex_unlock(&o
.base
);
1385 WARN_ON(o
.ctx
!= (void *)~0UL);
1387 /* trylock, failing */
1388 o
.ctx
= (void *)~0UL;
1389 mutex_lock(&o
.base
);
1390 ret
= mutex_trylock(&o
.base
);
1392 mutex_unlock(&o
.base
);
1393 WARN_ON(o
.ctx
!= (void *)~0UL);
1396 o
.ctx
= (void *)~0UL;
1397 mutex_lock_nest_lock(&o
.base
, &t
);
1398 mutex_unlock(&o
.base
);
1399 WARN_ON(o
.ctx
!= (void *)~0UL);
1402 static void ww_test_two_contexts(void)
1408 static void ww_test_diff_class(void)
1411 #ifdef CONFIG_DEBUG_MUTEXES
1417 static void ww_test_context_done_twice(void)
1425 static void ww_test_context_unlock_twice(void)
1433 static void ww_test_context_fini_early(void)
1441 static void ww_test_context_lock_after_done(void)
1448 static void ww_test_object_unlock_twice(void)
1455 static void ww_test_object_lock_unbalanced(void)
1464 static void ww_test_object_lock_stale_context(void)
1471 static void ww_test_edeadlk_normal(void)
1475 mutex_lock(&o2
.base
);
1477 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1487 WARN_ON(ret
!= -EDEADLK
);
1490 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1491 mutex_unlock(&o2
.base
);
1497 static void ww_test_edeadlk_normal_slow(void)
1501 mutex_lock(&o2
.base
);
1502 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1513 WARN_ON(ret
!= -EDEADLK
);
1516 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1517 mutex_unlock(&o2
.base
);
1520 ww_mutex_lock_slow(&o2
, &t
);
1523 static void ww_test_edeadlk_no_unlock(void)
1527 mutex_lock(&o2
.base
);
1529 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1539 WARN_ON(ret
!= -EDEADLK
);
1542 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1543 mutex_unlock(&o2
.base
);
1548 static void ww_test_edeadlk_no_unlock_slow(void)
1552 mutex_lock(&o2
.base
);
1553 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1564 WARN_ON(ret
!= -EDEADLK
);
1567 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1568 mutex_unlock(&o2
.base
);
1570 ww_mutex_lock_slow(&o2
, &t
);
1573 static void ww_test_edeadlk_acquire_more(void)
1577 mutex_lock(&o2
.base
);
1578 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1589 WARN_ON(ret
!= -EDEADLK
);
1594 static void ww_test_edeadlk_acquire_more_slow(void)
1598 mutex_lock(&o2
.base
);
1599 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1610 WARN_ON(ret
!= -EDEADLK
);
1612 ww_mutex_lock_slow(&o3
, &t
);
1615 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1619 mutex_lock(&o2
.base
);
1620 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1623 mutex_lock(&o3
.base
);
1624 mutex_release(&o3
.base
.dep_map
, 1, _THIS_IP_
);
1635 WARN_ON(ret
!= -EDEADLK
);
1638 WARN_ON(ret
!= -EDEADLK
);
1641 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1645 mutex_lock(&o2
.base
);
1646 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1649 mutex_lock(&o3
.base
);
1650 mutex_release(&o3
.base
.dep_map
, 1, _THIS_IP_
);
1661 WARN_ON(ret
!= -EDEADLK
);
1663 ww_mutex_lock_slow(&o3
, &t
);
1666 static void ww_test_edeadlk_acquire_wrong(void)
1670 mutex_lock(&o2
.base
);
1671 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1682 WARN_ON(ret
!= -EDEADLK
);
1691 static void ww_test_edeadlk_acquire_wrong_slow(void)
1695 mutex_lock(&o2
.base
);
1696 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1707 WARN_ON(ret
!= -EDEADLK
);
1713 ww_mutex_lock_slow(&o3
, &t
);
1716 static void ww_test_spin_nest_unlocked(void)
1718 raw_spin_lock_nest_lock(&lock_A
, &o
.base
);
1722 static void ww_test_unneeded_slow(void)
1726 ww_mutex_lock_slow(&o
, &t
);
1729 static void ww_test_context_block(void)
1740 static void ww_test_context_try(void)
1755 static void ww_test_context_context(void)
1771 static void ww_test_try_block(void)
1783 static void ww_test_try_try(void)
1795 static void ww_test_try_context(void)
1808 static void ww_test_block_block(void)
1814 static void ww_test_block_try(void)
1823 static void ww_test_block_context(void)
1834 static void ww_test_spin_block(void)
1850 static void ww_test_spin_try(void)
1870 static void ww_test_spin_context(void)
1892 static void ww_tests(void)
1894 printk(" --------------------------------------------------------------------------\n");
1895 printk(" | Wound/wait tests |\n");
1896 printk(" ---------------------\n");
1898 print_testname("ww api failures");
1899 dotest(ww_test_fail_acquire
, SUCCESS
, LOCKTYPE_WW
);
1900 dotest(ww_test_normal
, SUCCESS
, LOCKTYPE_WW
);
1901 dotest(ww_test_unneeded_slow
, FAILURE
, LOCKTYPE_WW
);
1904 print_testname("ww contexts mixing");
1905 dotest(ww_test_two_contexts
, FAILURE
, LOCKTYPE_WW
);
1906 dotest(ww_test_diff_class
, FAILURE
, LOCKTYPE_WW
);
1909 print_testname("finishing ww context");
1910 dotest(ww_test_context_done_twice
, FAILURE
, LOCKTYPE_WW
);
1911 dotest(ww_test_context_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
1912 dotest(ww_test_context_fini_early
, FAILURE
, LOCKTYPE_WW
);
1913 dotest(ww_test_context_lock_after_done
, FAILURE
, LOCKTYPE_WW
);
1916 print_testname("locking mismatches");
1917 dotest(ww_test_object_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
1918 dotest(ww_test_object_lock_unbalanced
, FAILURE
, LOCKTYPE_WW
);
1919 dotest(ww_test_object_lock_stale_context
, FAILURE
, LOCKTYPE_WW
);
1922 print_testname("EDEADLK handling");
1923 dotest(ww_test_edeadlk_normal
, SUCCESS
, LOCKTYPE_WW
);
1924 dotest(ww_test_edeadlk_normal_slow
, SUCCESS
, LOCKTYPE_WW
);
1925 dotest(ww_test_edeadlk_no_unlock
, FAILURE
, LOCKTYPE_WW
);
1926 dotest(ww_test_edeadlk_no_unlock_slow
, FAILURE
, LOCKTYPE_WW
);
1927 dotest(ww_test_edeadlk_acquire_more
, FAILURE
, LOCKTYPE_WW
);
1928 dotest(ww_test_edeadlk_acquire_more_slow
, FAILURE
, LOCKTYPE_WW
);
1929 dotest(ww_test_edeadlk_acquire_more_edeadlk
, FAILURE
, LOCKTYPE_WW
);
1930 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow
, FAILURE
, LOCKTYPE_WW
);
1931 dotest(ww_test_edeadlk_acquire_wrong
, FAILURE
, LOCKTYPE_WW
);
1932 dotest(ww_test_edeadlk_acquire_wrong_slow
, FAILURE
, LOCKTYPE_WW
);
1935 print_testname("spinlock nest unlocked");
1936 dotest(ww_test_spin_nest_unlocked
, FAILURE
, LOCKTYPE_WW
);
1939 printk(" -----------------------------------------------------\n");
1940 printk(" |block | try |context|\n");
1941 printk(" -----------------------------------------------------\n");
1943 print_testname("context");
1944 dotest(ww_test_context_block
, FAILURE
, LOCKTYPE_WW
);
1945 dotest(ww_test_context_try
, SUCCESS
, LOCKTYPE_WW
);
1946 dotest(ww_test_context_context
, SUCCESS
, LOCKTYPE_WW
);
1949 print_testname("try");
1950 dotest(ww_test_try_block
, FAILURE
, LOCKTYPE_WW
);
1951 dotest(ww_test_try_try
, SUCCESS
, LOCKTYPE_WW
);
1952 dotest(ww_test_try_context
, FAILURE
, LOCKTYPE_WW
);
1955 print_testname("block");
1956 dotest(ww_test_block_block
, FAILURE
, LOCKTYPE_WW
);
1957 dotest(ww_test_block_try
, SUCCESS
, LOCKTYPE_WW
);
1958 dotest(ww_test_block_context
, FAILURE
, LOCKTYPE_WW
);
1961 print_testname("spinlock");
1962 dotest(ww_test_spin_block
, FAILURE
, LOCKTYPE_WW
);
1963 dotest(ww_test_spin_try
, SUCCESS
, LOCKTYPE_WW
);
1964 dotest(ww_test_spin_context
, FAILURE
, LOCKTYPE_WW
);
1968 void locking_selftest(void)
1971 * Got a locking failure before the selftest ran?
1974 printk("----------------------------------\n");
1975 printk("| Locking API testsuite disabled |\n");
1976 printk("----------------------------------\n");
1981 * Run the testsuite:
1983 printk("------------------------\n");
1984 printk("| Locking API testsuite:\n");
1985 printk("----------------------------------------------------------------------------\n");
1986 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1987 printk(" --------------------------------------------------------------------------\n");
1989 init_shared_classes();
1990 debug_locks_silent
= !debug_locks_verbose
;
1992 DO_TESTCASE_6R("A-A deadlock", AA
);
1993 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA
);
1994 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA
);
1995 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC
);
1996 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA
);
1997 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA
);
1998 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA
);
1999 DO_TESTCASE_6("double unlock", double_unlock
);
2000 DO_TESTCASE_6("initialize held", init_held
);
2002 printk(" --------------------------------------------------------------------------\n");
2003 print_testname("recursive read-lock");
2005 dotest(rlock_AA1
, SUCCESS
, LOCKTYPE_RWLOCK
);
2007 dotest(rsem_AA1
, FAILURE
, LOCKTYPE_RWSEM
);
2010 print_testname("recursive read-lock #2");
2012 dotest(rlock_AA1B
, SUCCESS
, LOCKTYPE_RWLOCK
);
2014 dotest(rsem_AA1B
, FAILURE
, LOCKTYPE_RWSEM
);
2017 print_testname("mixed read-write-lock");
2019 dotest(rlock_AA2
, FAILURE
, LOCKTYPE_RWLOCK
);
2021 dotest(rsem_AA2
, FAILURE
, LOCKTYPE_RWSEM
);
2024 print_testname("mixed write-read-lock");
2026 dotest(rlock_AA3
, FAILURE
, LOCKTYPE_RWLOCK
);
2028 dotest(rsem_AA3
, FAILURE
, LOCKTYPE_RWSEM
);
2031 print_testname("mixed read-lock/lock-write ABBA");
2033 dotest(rlock_ABBA1
, FAILURE
, LOCKTYPE_RWLOCK
);
2034 #ifdef CONFIG_PROVE_LOCKING
2036 * Lockdep does indeed fail here, but there's nothing we can do about
2037 * that now. Don't kill lockdep for it.
2039 unexpected_testcase_failures
--;
2043 dotest(rwsem_ABBA1
, FAILURE
, LOCKTYPE_RWSEM
);
2045 print_testname("mixed read-lock/lock-read ABBA");
2047 dotest(rlock_ABBA2
, SUCCESS
, LOCKTYPE_RWLOCK
);
2049 dotest(rwsem_ABBA2
, FAILURE
, LOCKTYPE_RWSEM
);
2051 print_testname("mixed write-lock/lock-write ABBA");
2053 dotest(rlock_ABBA3
, FAILURE
, LOCKTYPE_RWLOCK
);
2055 dotest(rwsem_ABBA3
, FAILURE
, LOCKTYPE_RWSEM
);
2057 printk(" --------------------------------------------------------------------------\n");
2060 * irq-context testcases:
2062 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1
);
2063 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A
);
2064 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B
);
2065 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3
);
2066 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4
);
2067 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion
);
2069 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion
);
2070 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
2074 if (unexpected_testcase_failures
) {
2075 printk("-----------------------------------------------------------------\n");
2077 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2078 unexpected_testcase_failures
, testcase_total
);
2079 printk("-----------------------------------------------------------------\n");
2080 } else if (expected_testcase_failures
&& testcase_successes
) {
2081 printk("--------------------------------------------------------\n");
2082 printk("%3d out of %3d testcases failed, as expected. |\n",
2083 expected_testcase_failures
, testcase_total
);
2084 printk("----------------------------------------------------\n");
2086 } else if (expected_testcase_failures
&& !testcase_successes
) {
2087 printk("--------------------------------------------------------\n");
2088 printk("All %3d testcases failed, as expected. |\n",
2089 expected_testcase_failures
);
2090 printk("----------------------------------------\n");
2093 printk("-------------------------------------------------------\n");
2094 printk("Good, all %3d testcases passed! |\n",
2095 testcase_successes
);
2096 printk("---------------------------------\n");
2099 debug_locks_silent
= 0;