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/sched.h>
16 #include <linux/delay.h>
17 #include <linux/module.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>
26 * Change this to 1 if you want to see the failure printouts:
28 static unsigned int debug_locks_verbose
;
30 static int __init
setup_debug_locks_verbose(char *str
)
32 get_option(&str
, &debug_locks_verbose
);
37 __setup("debug_locks_verbose=", setup_debug_locks_verbose
);
42 #define LOCKTYPE_SPIN 0x1
43 #define LOCKTYPE_RWLOCK 0x2
44 #define LOCKTYPE_MUTEX 0x4
45 #define LOCKTYPE_RWSEM 0x8
48 * Normal standalone locks, for the circular and irq-context
51 static DEFINE_SPINLOCK(lock_A
);
52 static DEFINE_SPINLOCK(lock_B
);
53 static DEFINE_SPINLOCK(lock_C
);
54 static DEFINE_SPINLOCK(lock_D
);
56 static DEFINE_RWLOCK(rwlock_A
);
57 static DEFINE_RWLOCK(rwlock_B
);
58 static DEFINE_RWLOCK(rwlock_C
);
59 static DEFINE_RWLOCK(rwlock_D
);
61 static DEFINE_MUTEX(mutex_A
);
62 static DEFINE_MUTEX(mutex_B
);
63 static DEFINE_MUTEX(mutex_C
);
64 static DEFINE_MUTEX(mutex_D
);
66 static DECLARE_RWSEM(rwsem_A
);
67 static DECLARE_RWSEM(rwsem_B
);
68 static DECLARE_RWSEM(rwsem_C
);
69 static DECLARE_RWSEM(rwsem_D
);
72 * Locks that we initialize dynamically as well so that
73 * e.g. X1 and X2 becomes two instances of the same class,
74 * but X* and Y* are different classes. We do this so that
75 * we do not trigger a real lockup:
77 static DEFINE_SPINLOCK(lock_X1
);
78 static DEFINE_SPINLOCK(lock_X2
);
79 static DEFINE_SPINLOCK(lock_Y1
);
80 static DEFINE_SPINLOCK(lock_Y2
);
81 static DEFINE_SPINLOCK(lock_Z1
);
82 static DEFINE_SPINLOCK(lock_Z2
);
84 static DEFINE_RWLOCK(rwlock_X1
);
85 static DEFINE_RWLOCK(rwlock_X2
);
86 static DEFINE_RWLOCK(rwlock_Y1
);
87 static DEFINE_RWLOCK(rwlock_Y2
);
88 static DEFINE_RWLOCK(rwlock_Z1
);
89 static DEFINE_RWLOCK(rwlock_Z2
);
91 static DEFINE_MUTEX(mutex_X1
);
92 static DEFINE_MUTEX(mutex_X2
);
93 static DEFINE_MUTEX(mutex_Y1
);
94 static DEFINE_MUTEX(mutex_Y2
);
95 static DEFINE_MUTEX(mutex_Z1
);
96 static DEFINE_MUTEX(mutex_Z2
);
98 static DECLARE_RWSEM(rwsem_X1
);
99 static DECLARE_RWSEM(rwsem_X2
);
100 static DECLARE_RWSEM(rwsem_Y1
);
101 static DECLARE_RWSEM(rwsem_Y2
);
102 static DECLARE_RWSEM(rwsem_Z1
);
103 static DECLARE_RWSEM(rwsem_Z2
);
106 * non-inlined runtime initializers, to let separate locks share
107 * the same lock-class:
109 #define INIT_CLASS_FUNC(class) \
110 static noinline void \
111 init_class_##class(spinlock_t *lock, rwlock_t *rwlock, struct mutex *mutex, \
112 struct rw_semaphore *rwsem) \
114 spin_lock_init(lock); \
115 rwlock_init(rwlock); \
124 static void init_shared_classes(void)
126 init_class_X(&lock_X1
, &rwlock_X1
, &mutex_X1
, &rwsem_X1
);
127 init_class_X(&lock_X2
, &rwlock_X2
, &mutex_X2
, &rwsem_X2
);
129 init_class_Y(&lock_Y1
, &rwlock_Y1
, &mutex_Y1
, &rwsem_Y1
);
130 init_class_Y(&lock_Y2
, &rwlock_Y2
, &mutex_Y2
, &rwsem_Y2
);
132 init_class_Z(&lock_Z1
, &rwlock_Z1
, &mutex_Z1
, &rwsem_Z1
);
133 init_class_Z(&lock_Z2
, &rwlock_Z2
, &mutex_Z2
, &rwsem_Z2
);
137 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
138 * The following functions use a lock from a simulated hardirq/softirq
139 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
142 #define HARDIRQ_DISABLE local_irq_disable
143 #define HARDIRQ_ENABLE local_irq_enable
145 #define HARDIRQ_ENTER() \
146 local_irq_disable(); \
150 #define HARDIRQ_EXIT() \
154 #define SOFTIRQ_DISABLE local_bh_disable
155 #define SOFTIRQ_ENABLE local_bh_enable
157 #define SOFTIRQ_ENTER() \
158 local_bh_disable(); \
159 local_irq_disable(); \
160 trace_softirq_enter(); \
161 WARN_ON(!in_softirq());
163 #define SOFTIRQ_EXIT() \
164 trace_softirq_exit(); \
165 local_irq_enable(); \
169 * Shortcuts for lock/unlock API variants, to keep
170 * the testcases compact:
172 #define L(x) spin_lock(&lock_##x)
173 #define U(x) spin_unlock(&lock_##x)
174 #define LU(x) L(x); U(x)
175 #define SI(x) spin_lock_init(&lock_##x)
177 #define WL(x) write_lock(&rwlock_##x)
178 #define WU(x) write_unlock(&rwlock_##x)
179 #define WLU(x) WL(x); WU(x)
181 #define RL(x) read_lock(&rwlock_##x)
182 #define RU(x) read_unlock(&rwlock_##x)
183 #define RLU(x) RL(x); RU(x)
184 #define RWI(x) rwlock_init(&rwlock_##x)
186 #define ML(x) mutex_lock(&mutex_##x)
187 #define MU(x) mutex_unlock(&mutex_##x)
188 #define MI(x) mutex_init(&mutex_##x)
190 #define WSL(x) down_write(&rwsem_##x)
191 #define WSU(x) up_write(&rwsem_##x)
193 #define RSL(x) down_read(&rwsem_##x)
194 #define RSU(x) up_read(&rwsem_##x)
195 #define RWSI(x) init_rwsem(&rwsem_##x)
197 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
200 * Generate different permutations of the same testcase, using
201 * the same basic lock-dependency/state events:
204 #define GENERATE_TESTCASE(name) \
206 static void name(void) { E(); }
208 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
210 static void name##_12(void) { E1(); E2(); } \
211 static void name##_21(void) { E2(); E1(); }
213 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
215 static void name##_123(void) { E1(); E2(); E3(); } \
216 static void name##_132(void) { E1(); E3(); E2(); } \
217 static void name##_213(void) { E2(); E1(); E3(); } \
218 static void name##_231(void) { E2(); E3(); E1(); } \
219 static void name##_312(void) { E3(); E1(); E2(); } \
220 static void name##_321(void) { E3(); E2(); E1(); }
229 LOCK(X2); /* this one should fail */
234 #include "locking-selftest-spin.h"
235 GENERATE_TESTCASE(AA_spin
)
236 #include "locking-selftest-wlock.h"
237 GENERATE_TESTCASE(AA_wlock
)
238 #include "locking-selftest-rlock.h"
239 GENERATE_TESTCASE(AA_rlock
)
240 #include "locking-selftest-mutex.h"
241 GENERATE_TESTCASE(AA_mutex
)
242 #include "locking-selftest-wsem.h"
243 GENERATE_TESTCASE(AA_wsem
)
244 #include "locking-selftest-rsem.h"
245 GENERATE_TESTCASE(AA_rsem
)
250 * Special-case for read-locking, they are
251 * allowed to recurse on the same lock class:
253 static void rlock_AA1(void)
256 RL(X1
); // this one should NOT fail
259 static void rlock_AA1B(void)
262 RL(X2
); // this one should NOT fail
265 static void rsem_AA1(void)
268 RSL(X1
); // this one should fail
271 static void rsem_AA1B(void)
274 RSL(X2
); // this one should fail
277 * The mixing of read and write locks is not allowed:
279 static void rlock_AA2(void)
282 WL(X2
); // this one should fail
285 static void rsem_AA2(void)
288 WSL(X2
); // this one should fail
291 static void rlock_AA3(void)
294 RL(X2
); // this one should fail
297 static void rsem_AA3(void)
300 RSL(X2
); // this one should fail
309 LOCK_UNLOCK_2(A, B); \
310 LOCK_UNLOCK_2(B, A); /* fail */
315 #include "locking-selftest-spin.h"
316 GENERATE_TESTCASE(ABBA_spin
)
317 #include "locking-selftest-wlock.h"
318 GENERATE_TESTCASE(ABBA_wlock
)
319 #include "locking-selftest-rlock.h"
320 GENERATE_TESTCASE(ABBA_rlock
)
321 #include "locking-selftest-mutex.h"
322 GENERATE_TESTCASE(ABBA_mutex
)
323 #include "locking-selftest-wsem.h"
324 GENERATE_TESTCASE(ABBA_wsem
)
325 #include "locking-selftest-rsem.h"
326 GENERATE_TESTCASE(ABBA_rsem
)
336 LOCK_UNLOCK_2(A, B); \
337 LOCK_UNLOCK_2(B, C); \
338 LOCK_UNLOCK_2(C, A); /* fail */
343 #include "locking-selftest-spin.h"
344 GENERATE_TESTCASE(ABBCCA_spin
)
345 #include "locking-selftest-wlock.h"
346 GENERATE_TESTCASE(ABBCCA_wlock
)
347 #include "locking-selftest-rlock.h"
348 GENERATE_TESTCASE(ABBCCA_rlock
)
349 #include "locking-selftest-mutex.h"
350 GENERATE_TESTCASE(ABBCCA_mutex
)
351 #include "locking-selftest-wsem.h"
352 GENERATE_TESTCASE(ABBCCA_wsem
)
353 #include "locking-selftest-rsem.h"
354 GENERATE_TESTCASE(ABBCCA_rsem
)
364 LOCK_UNLOCK_2(A, B); \
365 LOCK_UNLOCK_2(C, A); \
366 LOCK_UNLOCK_2(B, C); /* fail */
371 #include "locking-selftest-spin.h"
372 GENERATE_TESTCASE(ABCABC_spin
)
373 #include "locking-selftest-wlock.h"
374 GENERATE_TESTCASE(ABCABC_wlock
)
375 #include "locking-selftest-rlock.h"
376 GENERATE_TESTCASE(ABCABC_rlock
)
377 #include "locking-selftest-mutex.h"
378 GENERATE_TESTCASE(ABCABC_mutex
)
379 #include "locking-selftest-wsem.h"
380 GENERATE_TESTCASE(ABCABC_wsem
)
381 #include "locking-selftest-rsem.h"
382 GENERATE_TESTCASE(ABCABC_rsem
)
387 * AB BC CD DA deadlock:
392 LOCK_UNLOCK_2(A, B); \
393 LOCK_UNLOCK_2(B, C); \
394 LOCK_UNLOCK_2(C, D); \
395 LOCK_UNLOCK_2(D, A); /* fail */
400 #include "locking-selftest-spin.h"
401 GENERATE_TESTCASE(ABBCCDDA_spin
)
402 #include "locking-selftest-wlock.h"
403 GENERATE_TESTCASE(ABBCCDDA_wlock
)
404 #include "locking-selftest-rlock.h"
405 GENERATE_TESTCASE(ABBCCDDA_rlock
)
406 #include "locking-selftest-mutex.h"
407 GENERATE_TESTCASE(ABBCCDDA_mutex
)
408 #include "locking-selftest-wsem.h"
409 GENERATE_TESTCASE(ABBCCDDA_wsem
)
410 #include "locking-selftest-rsem.h"
411 GENERATE_TESTCASE(ABBCCDDA_rsem
)
416 * AB CD BD DA deadlock:
420 LOCK_UNLOCK_2(A, B); \
421 LOCK_UNLOCK_2(C, D); \
422 LOCK_UNLOCK_2(B, D); \
423 LOCK_UNLOCK_2(D, A); /* fail */
428 #include "locking-selftest-spin.h"
429 GENERATE_TESTCASE(ABCDBDDA_spin
)
430 #include "locking-selftest-wlock.h"
431 GENERATE_TESTCASE(ABCDBDDA_wlock
)
432 #include "locking-selftest-rlock.h"
433 GENERATE_TESTCASE(ABCDBDDA_rlock
)
434 #include "locking-selftest-mutex.h"
435 GENERATE_TESTCASE(ABCDBDDA_mutex
)
436 #include "locking-selftest-wsem.h"
437 GENERATE_TESTCASE(ABCDBDDA_wsem
)
438 #include "locking-selftest-rsem.h"
439 GENERATE_TESTCASE(ABCDBDDA_rsem
)
444 * AB CD BC DA deadlock:
448 LOCK_UNLOCK_2(A, B); \
449 LOCK_UNLOCK_2(C, D); \
450 LOCK_UNLOCK_2(B, C); \
451 LOCK_UNLOCK_2(D, A); /* fail */
456 #include "locking-selftest-spin.h"
457 GENERATE_TESTCASE(ABCDBCDA_spin
)
458 #include "locking-selftest-wlock.h"
459 GENERATE_TESTCASE(ABCDBCDA_wlock
)
460 #include "locking-selftest-rlock.h"
461 GENERATE_TESTCASE(ABCDBCDA_rlock
)
462 #include "locking-selftest-mutex.h"
463 GENERATE_TESTCASE(ABCDBCDA_mutex
)
464 #include "locking-selftest-wsem.h"
465 GENERATE_TESTCASE(ABCDBCDA_wsem
)
466 #include "locking-selftest-rsem.h"
467 GENERATE_TESTCASE(ABCDBCDA_rsem
)
478 UNLOCK(A); /* fail */
483 #include "locking-selftest-spin.h"
484 GENERATE_TESTCASE(double_unlock_spin
)
485 #include "locking-selftest-wlock.h"
486 GENERATE_TESTCASE(double_unlock_wlock
)
487 #include "locking-selftest-rlock.h"
488 GENERATE_TESTCASE(double_unlock_rlock
)
489 #include "locking-selftest-mutex.h"
490 GENERATE_TESTCASE(double_unlock_mutex
)
491 #include "locking-selftest-wsem.h"
492 GENERATE_TESTCASE(double_unlock_wsem
)
493 #include "locking-selftest-rsem.h"
494 GENERATE_TESTCASE(double_unlock_rsem
)
499 * Bad unlock ordering:
505 UNLOCK(A); /* fail */ \
511 #include "locking-selftest-spin.h"
512 GENERATE_TESTCASE(bad_unlock_order_spin
)
513 #include "locking-selftest-wlock.h"
514 GENERATE_TESTCASE(bad_unlock_order_wlock
)
515 #include "locking-selftest-rlock.h"
516 GENERATE_TESTCASE(bad_unlock_order_rlock
)
517 #include "locking-selftest-mutex.h"
518 GENERATE_TESTCASE(bad_unlock_order_mutex
)
519 #include "locking-selftest-wsem.h"
520 GENERATE_TESTCASE(bad_unlock_order_wsem
)
521 #include "locking-selftest-rsem.h"
522 GENERATE_TESTCASE(bad_unlock_order_rsem
)
527 * initializing a held lock:
537 #include "locking-selftest-spin.h"
538 GENERATE_TESTCASE(init_held_spin
)
539 #include "locking-selftest-wlock.h"
540 GENERATE_TESTCASE(init_held_wlock
)
541 #include "locking-selftest-rlock.h"
542 GENERATE_TESTCASE(init_held_rlock
)
543 #include "locking-selftest-mutex.h"
544 GENERATE_TESTCASE(init_held_mutex
)
545 #include "locking-selftest-wsem.h"
546 GENERATE_TESTCASE(init_held_wsem
)
547 #include "locking-selftest-rsem.h"
548 GENERATE_TESTCASE(init_held_rsem
)
553 * locking an irq-safe lock with irqs enabled:
568 * Generate 24 testcases:
570 #include "locking-selftest-spin-hardirq.h"
571 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin
)
573 #include "locking-selftest-rlock-hardirq.h"
574 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock
)
576 #include "locking-selftest-wlock-hardirq.h"
577 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock
)
579 #include "locking-selftest-spin-softirq.h"
580 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin
)
582 #include "locking-selftest-rlock-softirq.h"
583 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock
)
585 #include "locking-selftest-wlock-softirq.h"
586 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock
)
592 * Enabling hardirqs with a softirq-safe lock held:
609 * Generate 12 testcases:
611 #include "locking-selftest-spin.h"
612 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin
)
614 #include "locking-selftest-wlock.h"
615 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock
)
617 #include "locking-selftest-rlock.h"
618 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock
)
624 * Enabling irqs with an irq-safe lock held:
641 * Generate 24 testcases:
643 #include "locking-selftest-spin-hardirq.h"
644 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin
)
646 #include "locking-selftest-rlock-hardirq.h"
647 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock
)
649 #include "locking-selftest-wlock-hardirq.h"
650 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock
)
652 #include "locking-selftest-spin-softirq.h"
653 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin
)
655 #include "locking-selftest-rlock-softirq.h"
656 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock
)
658 #include "locking-selftest-wlock-softirq.h"
659 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock
)
665 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
687 * Generate 36 testcases:
689 #include "locking-selftest-spin-hardirq.h"
690 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin
)
692 #include "locking-selftest-rlock-hardirq.h"
693 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock
)
695 #include "locking-selftest-wlock-hardirq.h"
696 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock
)
698 #include "locking-selftest-spin-softirq.h"
699 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin
)
701 #include "locking-selftest-rlock-softirq.h"
702 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock
)
704 #include "locking-selftest-wlock-softirq.h"
705 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock
)
712 * If a lock turns into softirq-safe, but earlier it took
713 * a softirq-unsafe lock:
735 * Generate 36 testcases:
737 #include "locking-selftest-spin-hardirq.h"
738 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin
)
740 #include "locking-selftest-rlock-hardirq.h"
741 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock
)
743 #include "locking-selftest-wlock-hardirq.h"
744 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock
)
746 #include "locking-selftest-spin-softirq.h"
747 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin
)
749 #include "locking-selftest-rlock-softirq.h"
750 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock
)
752 #include "locking-selftest-wlock-softirq.h"
753 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock
)
760 * read-lock / write-lock irq inversion.
764 * CPU#1 is at #1, i.e. it has write-locked A, but has not
767 * CPU#2 is at #2, i.e. it has locked B.
769 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
771 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
797 * Generate 36 testcases:
799 #include "locking-selftest-spin-hardirq.h"
800 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin
)
802 #include "locking-selftest-rlock-hardirq.h"
803 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock
)
805 #include "locking-selftest-wlock-hardirq.h"
806 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock
)
808 #include "locking-selftest-spin-softirq.h"
809 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin
)
811 #include "locking-selftest-rlock-softirq.h"
812 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock
)
814 #include "locking-selftest-wlock-softirq.h"
815 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock
)
822 * read-lock / write-lock recursion that is actually safe.
847 * Generate 12 testcases:
849 #include "locking-selftest-hardirq.h"
850 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard
)
852 #include "locking-selftest-softirq.h"
853 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft
)
860 * read-lock / write-lock recursion that is unsafe.
885 * Generate 12 testcases:
887 #include "locking-selftest-hardirq.h"
888 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
890 #include "locking-selftest-softirq.h"
891 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
893 #ifdef CONFIG_DEBUG_LOCK_ALLOC
894 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
895 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
896 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
897 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
899 # define I_SPINLOCK(x)
915 spin_lock_init(&lock_##x); \
916 rwlock_init(&rwlock_##x); \
917 mutex_init(&mutex_##x); \
918 init_rwsem(&rwsem_##x); \
921 static void reset_locks(void)
924 I1(A
); I1(B
); I1(C
); I1(D
);
925 I1(X1
); I1(X2
); I1(Y1
); I1(Y2
); I1(Z1
); I1(Z2
);
927 I2(A
); I2(B
); I2(C
); I2(D
);
928 init_shared_classes();
934 static int testcase_total
;
935 static int testcase_successes
;
936 static int expected_testcase_failures
;
937 static int unexpected_testcase_failures
;
939 static void dotest(void (*testcase_fn
)(void), int expected
, int lockclass_mask
)
941 unsigned long saved_preempt_count
= preempt_count();
942 int expected_failure
= 0;
944 WARN_ON(irqs_disabled());
948 * Filter out expected failures:
950 #ifndef CONFIG_PROVE_LOCKING
951 if ((lockclass_mask
& LOCKTYPE_SPIN
) && debug_locks
!= expected
)
952 expected_failure
= 1;
953 if ((lockclass_mask
& LOCKTYPE_RWLOCK
) && debug_locks
!= expected
)
954 expected_failure
= 1;
955 if ((lockclass_mask
& LOCKTYPE_MUTEX
) && debug_locks
!= expected
)
956 expected_failure
= 1;
957 if ((lockclass_mask
& LOCKTYPE_RWSEM
) && debug_locks
!= expected
)
958 expected_failure
= 1;
960 if (debug_locks
!= expected
) {
961 if (expected_failure
) {
962 expected_testcase_failures
++;
965 unexpected_testcase_failures
++;
971 testcase_successes
++;
976 if (debug_locks_verbose
)
977 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
978 lockclass_mask
, debug_locks
, expected
);
980 * Some tests (e.g. double-unlock) might corrupt the preemption
981 * count, so restore it:
983 preempt_count() = saved_preempt_count
;
984 #ifdef CONFIG_TRACE_IRQFLAGS
986 current
->softirqs_enabled
= 0;
988 current
->softirqs_enabled
= 1;
994 static inline void print_testname(const char *testname
)
996 printk("%33s:", testname
);
999 #define DO_TESTCASE_1(desc, name, nr) \
1000 print_testname(desc"/"#nr); \
1001 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1004 #define DO_TESTCASE_1B(desc, name, nr) \
1005 print_testname(desc"/"#nr); \
1006 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1009 #define DO_TESTCASE_3(desc, name, nr) \
1010 print_testname(desc"/"#nr); \
1011 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1012 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1013 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1016 #define DO_TESTCASE_3RW(desc, name, nr) \
1017 print_testname(desc"/"#nr); \
1018 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1019 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1020 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1023 #define DO_TESTCASE_6(desc, name) \
1024 print_testname(desc); \
1025 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1026 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1027 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1028 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1029 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1030 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1033 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1034 print_testname(desc); \
1035 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1036 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1037 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1038 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1039 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1040 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1044 * 'read' variant: rlocks must not trigger.
1046 #define DO_TESTCASE_6R(desc, name) \
1047 print_testname(desc); \
1048 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1049 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1050 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1051 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1052 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1053 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1056 #define DO_TESTCASE_2I(desc, name, nr) \
1057 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1058 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1060 #define DO_TESTCASE_2IB(desc, name, nr) \
1061 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1062 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1064 #define DO_TESTCASE_6I(desc, name, nr) \
1065 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1066 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1068 #define DO_TESTCASE_6IRW(desc, name, nr) \
1069 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1070 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1072 #define DO_TESTCASE_2x3(desc, name) \
1073 DO_TESTCASE_3(desc, name, 12); \
1074 DO_TESTCASE_3(desc, name, 21);
1076 #define DO_TESTCASE_2x6(desc, name) \
1077 DO_TESTCASE_6I(desc, name, 12); \
1078 DO_TESTCASE_6I(desc, name, 21);
1080 #define DO_TESTCASE_6x2(desc, name) \
1081 DO_TESTCASE_2I(desc, name, 123); \
1082 DO_TESTCASE_2I(desc, name, 132); \
1083 DO_TESTCASE_2I(desc, name, 213); \
1084 DO_TESTCASE_2I(desc, name, 231); \
1085 DO_TESTCASE_2I(desc, name, 312); \
1086 DO_TESTCASE_2I(desc, name, 321);
1088 #define DO_TESTCASE_6x2B(desc, name) \
1089 DO_TESTCASE_2IB(desc, name, 123); \
1090 DO_TESTCASE_2IB(desc, name, 132); \
1091 DO_TESTCASE_2IB(desc, name, 213); \
1092 DO_TESTCASE_2IB(desc, name, 231); \
1093 DO_TESTCASE_2IB(desc, name, 312); \
1094 DO_TESTCASE_2IB(desc, name, 321);
1096 #define DO_TESTCASE_6x6(desc, name) \
1097 DO_TESTCASE_6I(desc, name, 123); \
1098 DO_TESTCASE_6I(desc, name, 132); \
1099 DO_TESTCASE_6I(desc, name, 213); \
1100 DO_TESTCASE_6I(desc, name, 231); \
1101 DO_TESTCASE_6I(desc, name, 312); \
1102 DO_TESTCASE_6I(desc, name, 321);
1104 #define DO_TESTCASE_6x6RW(desc, name) \
1105 DO_TESTCASE_6IRW(desc, name, 123); \
1106 DO_TESTCASE_6IRW(desc, name, 132); \
1107 DO_TESTCASE_6IRW(desc, name, 213); \
1108 DO_TESTCASE_6IRW(desc, name, 231); \
1109 DO_TESTCASE_6IRW(desc, name, 312); \
1110 DO_TESTCASE_6IRW(desc, name, 321);
1113 void locking_selftest(void)
1116 * Got a locking failure before the selftest ran?
1119 printk("----------------------------------\n");
1120 printk("| Locking API testsuite disabled |\n");
1121 printk("----------------------------------\n");
1126 * Run the testsuite:
1128 printk("------------------------\n");
1129 printk("| Locking API testsuite:\n");
1130 printk("----------------------------------------------------------------------------\n");
1131 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1132 printk(" --------------------------------------------------------------------------\n");
1134 init_shared_classes();
1135 debug_locks_silent
= !debug_locks_verbose
;
1137 DO_TESTCASE_6R("A-A deadlock", AA
);
1138 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA
);
1139 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA
);
1140 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC
);
1141 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA
);
1142 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA
);
1143 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA
);
1144 DO_TESTCASE_6("double unlock", double_unlock
);
1145 DO_TESTCASE_6("initialize held", init_held
);
1146 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order
);
1148 printk(" --------------------------------------------------------------------------\n");
1149 print_testname("recursive read-lock");
1151 dotest(rlock_AA1
, SUCCESS
, LOCKTYPE_RWLOCK
);
1153 dotest(rsem_AA1
, FAILURE
, LOCKTYPE_RWSEM
);
1156 print_testname("recursive read-lock #2");
1158 dotest(rlock_AA1B
, SUCCESS
, LOCKTYPE_RWLOCK
);
1160 dotest(rsem_AA1B
, FAILURE
, LOCKTYPE_RWSEM
);
1163 print_testname("mixed read-write-lock");
1165 dotest(rlock_AA2
, FAILURE
, LOCKTYPE_RWLOCK
);
1167 dotest(rsem_AA2
, FAILURE
, LOCKTYPE_RWSEM
);
1170 print_testname("mixed write-read-lock");
1172 dotest(rlock_AA3
, FAILURE
, LOCKTYPE_RWLOCK
);
1174 dotest(rsem_AA3
, FAILURE
, LOCKTYPE_RWSEM
);
1177 printk(" --------------------------------------------------------------------------\n");
1180 * irq-context testcases:
1182 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1
);
1183 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A
);
1184 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B
);
1185 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3
);
1186 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4
);
1187 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion
);
1189 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion
);
1190 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1192 if (unexpected_testcase_failures
) {
1193 printk("-----------------------------------------------------------------\n");
1195 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1196 unexpected_testcase_failures
, testcase_total
);
1197 printk("-----------------------------------------------------------------\n");
1198 } else if (expected_testcase_failures
&& testcase_successes
) {
1199 printk("--------------------------------------------------------\n");
1200 printk("%3d out of %3d testcases failed, as expected. |\n",
1201 expected_testcase_failures
, testcase_total
);
1202 printk("----------------------------------------------------\n");
1204 } else if (expected_testcase_failures
&& !testcase_successes
) {
1205 printk("--------------------------------------------------------\n");
1206 printk("All %3d testcases failed, as expected. |\n",
1207 expected_testcase_failures
);
1208 printk("----------------------------------------\n");
1211 printk("-------------------------------------------------------\n");
1212 printk("Good, all %3d testcases passed! |\n",
1213 testcase_successes
);
1214 printk("---------------------------------\n");
1217 debug_locks_silent
= 0;