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/lockdep.h>
18 #include <linux/spinlock.h>
19 #include <linux/kallsyms.h>
20 #include <linux/interrupt.h>
21 #include <linux/debug_locks.h>
22 #include <linux/irqflags.h>
25 * Change this to 1 if you want to see the failure printouts:
27 static unsigned int debug_locks_verbose
;
29 static int __init
setup_debug_locks_verbose(char *str
)
31 get_option(&str
, &debug_locks_verbose
);
36 __setup("debug_locks_verbose=", setup_debug_locks_verbose
);
41 #define LOCKTYPE_SPIN 0x1
42 #define LOCKTYPE_RWLOCK 0x2
43 #define LOCKTYPE_MUTEX 0x4
44 #define LOCKTYPE_RWSEM 0x8
47 * Normal standalone locks, for the circular and irq-context
50 static DEFINE_RAW_SPINLOCK(lock_A
);
51 static DEFINE_RAW_SPINLOCK(lock_B
);
52 static DEFINE_RAW_SPINLOCK(lock_C
);
53 static DEFINE_RAW_SPINLOCK(lock_D
);
55 static DEFINE_RWLOCK(rwlock_A
);
56 static DEFINE_RWLOCK(rwlock_B
);
57 static DEFINE_RWLOCK(rwlock_C
);
58 static DEFINE_RWLOCK(rwlock_D
);
60 static DEFINE_MUTEX(mutex_A
);
61 static DEFINE_MUTEX(mutex_B
);
62 static DEFINE_MUTEX(mutex_C
);
63 static DEFINE_MUTEX(mutex_D
);
65 static DECLARE_RWSEM(rwsem_A
);
66 static DECLARE_RWSEM(rwsem_B
);
67 static DECLARE_RWSEM(rwsem_C
);
68 static DECLARE_RWSEM(rwsem_D
);
71 * Locks that we initialize dynamically as well so that
72 * e.g. X1 and X2 becomes two instances of the same class,
73 * but X* and Y* are different classes. We do this so that
74 * we do not trigger a real lockup:
76 static DEFINE_RAW_SPINLOCK(lock_X1
);
77 static DEFINE_RAW_SPINLOCK(lock_X2
);
78 static DEFINE_RAW_SPINLOCK(lock_Y1
);
79 static DEFINE_RAW_SPINLOCK(lock_Y2
);
80 static DEFINE_RAW_SPINLOCK(lock_Z1
);
81 static DEFINE_RAW_SPINLOCK(lock_Z2
);
83 static DEFINE_RWLOCK(rwlock_X1
);
84 static DEFINE_RWLOCK(rwlock_X2
);
85 static DEFINE_RWLOCK(rwlock_Y1
);
86 static DEFINE_RWLOCK(rwlock_Y2
);
87 static DEFINE_RWLOCK(rwlock_Z1
);
88 static DEFINE_RWLOCK(rwlock_Z2
);
90 static DEFINE_MUTEX(mutex_X1
);
91 static DEFINE_MUTEX(mutex_X2
);
92 static DEFINE_MUTEX(mutex_Y1
);
93 static DEFINE_MUTEX(mutex_Y2
);
94 static DEFINE_MUTEX(mutex_Z1
);
95 static DEFINE_MUTEX(mutex_Z2
);
97 static DECLARE_RWSEM(rwsem_X1
);
98 static DECLARE_RWSEM(rwsem_X2
);
99 static DECLARE_RWSEM(rwsem_Y1
);
100 static DECLARE_RWSEM(rwsem_Y2
);
101 static DECLARE_RWSEM(rwsem_Z1
);
102 static DECLARE_RWSEM(rwsem_Z2
);
105 * non-inlined runtime initializers, to let separate locks share
106 * the same lock-class:
108 #define INIT_CLASS_FUNC(class) \
109 static noinline void \
110 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
111 struct mutex *mutex, struct rw_semaphore *rwsem)\
113 raw_spin_lock_init(lock); \
114 rwlock_init(rwlock); \
123 static void init_shared_classes(void)
125 init_class_X(&lock_X1
, &rwlock_X1
, &mutex_X1
, &rwsem_X1
);
126 init_class_X(&lock_X2
, &rwlock_X2
, &mutex_X2
, &rwsem_X2
);
128 init_class_Y(&lock_Y1
, &rwlock_Y1
, &mutex_Y1
, &rwsem_Y1
);
129 init_class_Y(&lock_Y2
, &rwlock_Y2
, &mutex_Y2
, &rwsem_Y2
);
131 init_class_Z(&lock_Z1
, &rwlock_Z1
, &mutex_Z1
, &rwsem_Z1
);
132 init_class_Z(&lock_Z2
, &rwlock_Z2
, &mutex_Z2
, &rwsem_Z2
);
136 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
137 * The following functions use a lock from a simulated hardirq/softirq
138 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
141 #define HARDIRQ_DISABLE local_irq_disable
142 #define HARDIRQ_ENABLE local_irq_enable
144 #define HARDIRQ_ENTER() \
145 local_irq_disable(); \
149 #define HARDIRQ_EXIT() \
153 #define SOFTIRQ_DISABLE local_bh_disable
154 #define SOFTIRQ_ENABLE local_bh_enable
156 #define SOFTIRQ_ENTER() \
157 local_bh_disable(); \
158 local_irq_disable(); \
159 lockdep_softirq_enter(); \
160 WARN_ON(!in_softirq());
162 #define SOFTIRQ_EXIT() \
163 lockdep_softirq_exit(); \
164 local_irq_enable(); \
168 * Shortcuts for lock/unlock API variants, to keep
169 * the testcases compact:
171 #define L(x) raw_spin_lock(&lock_##x)
172 #define U(x) raw_spin_unlock(&lock_##x)
173 #define LU(x) L(x); U(x)
174 #define SI(x) raw_spin_lock_init(&lock_##x)
176 #define WL(x) write_lock(&rwlock_##x)
177 #define WU(x) write_unlock(&rwlock_##x)
178 #define WLU(x) WL(x); WU(x)
180 #define RL(x) read_lock(&rwlock_##x)
181 #define RU(x) read_unlock(&rwlock_##x)
182 #define RLU(x) RL(x); RU(x)
183 #define RWI(x) rwlock_init(&rwlock_##x)
185 #define ML(x) mutex_lock(&mutex_##x)
186 #define MU(x) mutex_unlock(&mutex_##x)
187 #define MI(x) mutex_init(&mutex_##x)
189 #define WSL(x) down_write(&rwsem_##x)
190 #define WSU(x) up_write(&rwsem_##x)
192 #define RSL(x) down_read(&rwsem_##x)
193 #define RSU(x) up_read(&rwsem_##x)
194 #define RWSI(x) init_rwsem(&rwsem_##x)
196 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
199 * Generate different permutations of the same testcase, using
200 * the same basic lock-dependency/state events:
203 #define GENERATE_TESTCASE(name) \
205 static void name(void) { E(); }
207 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
209 static void name##_12(void) { E1(); E2(); } \
210 static void name##_21(void) { E2(); E1(); }
212 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
214 static void name##_123(void) { E1(); E2(); E3(); } \
215 static void name##_132(void) { E1(); E3(); E2(); } \
216 static void name##_213(void) { E2(); E1(); E3(); } \
217 static void name##_231(void) { E2(); E3(); E1(); } \
218 static void name##_312(void) { E3(); E1(); E2(); } \
219 static void name##_321(void) { E3(); E2(); E1(); }
228 LOCK(X2); /* this one should fail */
233 #include "locking-selftest-spin.h"
234 GENERATE_TESTCASE(AA_spin
)
235 #include "locking-selftest-wlock.h"
236 GENERATE_TESTCASE(AA_wlock
)
237 #include "locking-selftest-rlock.h"
238 GENERATE_TESTCASE(AA_rlock
)
239 #include "locking-selftest-mutex.h"
240 GENERATE_TESTCASE(AA_mutex
)
241 #include "locking-selftest-wsem.h"
242 GENERATE_TESTCASE(AA_wsem
)
243 #include "locking-selftest-rsem.h"
244 GENERATE_TESTCASE(AA_rsem
)
249 * Special-case for read-locking, they are
250 * allowed to recurse on the same lock class:
252 static void rlock_AA1(void)
255 RL(X1
); // this one should NOT fail
258 static void rlock_AA1B(void)
261 RL(X2
); // this one should NOT fail
264 static void rsem_AA1(void)
267 RSL(X1
); // this one should fail
270 static void rsem_AA1B(void)
273 RSL(X2
); // this one should fail
276 * The mixing of read and write locks is not allowed:
278 static void rlock_AA2(void)
281 WL(X2
); // this one should fail
284 static void rsem_AA2(void)
287 WSL(X2
); // this one should fail
290 static void rlock_AA3(void)
293 RL(X2
); // this one should fail
296 static void rsem_AA3(void)
299 RSL(X2
); // this one should fail
308 LOCK_UNLOCK_2(A, B); \
309 LOCK_UNLOCK_2(B, A); /* fail */
314 #include "locking-selftest-spin.h"
315 GENERATE_TESTCASE(ABBA_spin
)
316 #include "locking-selftest-wlock.h"
317 GENERATE_TESTCASE(ABBA_wlock
)
318 #include "locking-selftest-rlock.h"
319 GENERATE_TESTCASE(ABBA_rlock
)
320 #include "locking-selftest-mutex.h"
321 GENERATE_TESTCASE(ABBA_mutex
)
322 #include "locking-selftest-wsem.h"
323 GENERATE_TESTCASE(ABBA_wsem
)
324 #include "locking-selftest-rsem.h"
325 GENERATE_TESTCASE(ABBA_rsem
)
335 LOCK_UNLOCK_2(A, B); \
336 LOCK_UNLOCK_2(B, C); \
337 LOCK_UNLOCK_2(C, A); /* fail */
342 #include "locking-selftest-spin.h"
343 GENERATE_TESTCASE(ABBCCA_spin
)
344 #include "locking-selftest-wlock.h"
345 GENERATE_TESTCASE(ABBCCA_wlock
)
346 #include "locking-selftest-rlock.h"
347 GENERATE_TESTCASE(ABBCCA_rlock
)
348 #include "locking-selftest-mutex.h"
349 GENERATE_TESTCASE(ABBCCA_mutex
)
350 #include "locking-selftest-wsem.h"
351 GENERATE_TESTCASE(ABBCCA_wsem
)
352 #include "locking-selftest-rsem.h"
353 GENERATE_TESTCASE(ABBCCA_rsem
)
363 LOCK_UNLOCK_2(A, B); \
364 LOCK_UNLOCK_2(C, A); \
365 LOCK_UNLOCK_2(B, C); /* fail */
370 #include "locking-selftest-spin.h"
371 GENERATE_TESTCASE(ABCABC_spin
)
372 #include "locking-selftest-wlock.h"
373 GENERATE_TESTCASE(ABCABC_wlock
)
374 #include "locking-selftest-rlock.h"
375 GENERATE_TESTCASE(ABCABC_rlock
)
376 #include "locking-selftest-mutex.h"
377 GENERATE_TESTCASE(ABCABC_mutex
)
378 #include "locking-selftest-wsem.h"
379 GENERATE_TESTCASE(ABCABC_wsem
)
380 #include "locking-selftest-rsem.h"
381 GENERATE_TESTCASE(ABCABC_rsem
)
386 * AB BC CD DA deadlock:
391 LOCK_UNLOCK_2(A, B); \
392 LOCK_UNLOCK_2(B, C); \
393 LOCK_UNLOCK_2(C, D); \
394 LOCK_UNLOCK_2(D, A); /* fail */
399 #include "locking-selftest-spin.h"
400 GENERATE_TESTCASE(ABBCCDDA_spin
)
401 #include "locking-selftest-wlock.h"
402 GENERATE_TESTCASE(ABBCCDDA_wlock
)
403 #include "locking-selftest-rlock.h"
404 GENERATE_TESTCASE(ABBCCDDA_rlock
)
405 #include "locking-selftest-mutex.h"
406 GENERATE_TESTCASE(ABBCCDDA_mutex
)
407 #include "locking-selftest-wsem.h"
408 GENERATE_TESTCASE(ABBCCDDA_wsem
)
409 #include "locking-selftest-rsem.h"
410 GENERATE_TESTCASE(ABBCCDDA_rsem
)
415 * AB CD BD DA deadlock:
419 LOCK_UNLOCK_2(A, B); \
420 LOCK_UNLOCK_2(C, D); \
421 LOCK_UNLOCK_2(B, D); \
422 LOCK_UNLOCK_2(D, A); /* fail */
427 #include "locking-selftest-spin.h"
428 GENERATE_TESTCASE(ABCDBDDA_spin
)
429 #include "locking-selftest-wlock.h"
430 GENERATE_TESTCASE(ABCDBDDA_wlock
)
431 #include "locking-selftest-rlock.h"
432 GENERATE_TESTCASE(ABCDBDDA_rlock
)
433 #include "locking-selftest-mutex.h"
434 GENERATE_TESTCASE(ABCDBDDA_mutex
)
435 #include "locking-selftest-wsem.h"
436 GENERATE_TESTCASE(ABCDBDDA_wsem
)
437 #include "locking-selftest-rsem.h"
438 GENERATE_TESTCASE(ABCDBDDA_rsem
)
443 * AB CD BC DA deadlock:
447 LOCK_UNLOCK_2(A, B); \
448 LOCK_UNLOCK_2(C, D); \
449 LOCK_UNLOCK_2(B, C); \
450 LOCK_UNLOCK_2(D, A); /* fail */
455 #include "locking-selftest-spin.h"
456 GENERATE_TESTCASE(ABCDBCDA_spin
)
457 #include "locking-selftest-wlock.h"
458 GENERATE_TESTCASE(ABCDBCDA_wlock
)
459 #include "locking-selftest-rlock.h"
460 GENERATE_TESTCASE(ABCDBCDA_rlock
)
461 #include "locking-selftest-mutex.h"
462 GENERATE_TESTCASE(ABCDBCDA_mutex
)
463 #include "locking-selftest-wsem.h"
464 GENERATE_TESTCASE(ABCDBCDA_wsem
)
465 #include "locking-selftest-rsem.h"
466 GENERATE_TESTCASE(ABCDBCDA_rsem
)
477 UNLOCK(A); /* fail */
482 #include "locking-selftest-spin.h"
483 GENERATE_TESTCASE(double_unlock_spin
)
484 #include "locking-selftest-wlock.h"
485 GENERATE_TESTCASE(double_unlock_wlock
)
486 #include "locking-selftest-rlock.h"
487 GENERATE_TESTCASE(double_unlock_rlock
)
488 #include "locking-selftest-mutex.h"
489 GENERATE_TESTCASE(double_unlock_mutex
)
490 #include "locking-selftest-wsem.h"
491 GENERATE_TESTCASE(double_unlock_wsem
)
492 #include "locking-selftest-rsem.h"
493 GENERATE_TESTCASE(double_unlock_rsem
)
498 * Bad unlock ordering:
504 UNLOCK(A); /* fail */ \
510 #include "locking-selftest-spin.h"
511 GENERATE_TESTCASE(bad_unlock_order_spin
)
512 #include "locking-selftest-wlock.h"
513 GENERATE_TESTCASE(bad_unlock_order_wlock
)
514 #include "locking-selftest-rlock.h"
515 GENERATE_TESTCASE(bad_unlock_order_rlock
)
516 #include "locking-selftest-mutex.h"
517 GENERATE_TESTCASE(bad_unlock_order_mutex
)
518 #include "locking-selftest-wsem.h"
519 GENERATE_TESTCASE(bad_unlock_order_wsem
)
520 #include "locking-selftest-rsem.h"
521 GENERATE_TESTCASE(bad_unlock_order_rsem
)
526 * initializing a held lock:
536 #include "locking-selftest-spin.h"
537 GENERATE_TESTCASE(init_held_spin
)
538 #include "locking-selftest-wlock.h"
539 GENERATE_TESTCASE(init_held_wlock
)
540 #include "locking-selftest-rlock.h"
541 GENERATE_TESTCASE(init_held_rlock
)
542 #include "locking-selftest-mutex.h"
543 GENERATE_TESTCASE(init_held_mutex
)
544 #include "locking-selftest-wsem.h"
545 GENERATE_TESTCASE(init_held_wsem
)
546 #include "locking-selftest-rsem.h"
547 GENERATE_TESTCASE(init_held_rsem
)
552 * locking an irq-safe lock with irqs enabled:
567 * Generate 24 testcases:
569 #include "locking-selftest-spin-hardirq.h"
570 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin
)
572 #include "locking-selftest-rlock-hardirq.h"
573 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock
)
575 #include "locking-selftest-wlock-hardirq.h"
576 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock
)
578 #include "locking-selftest-spin-softirq.h"
579 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin
)
581 #include "locking-selftest-rlock-softirq.h"
582 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock
)
584 #include "locking-selftest-wlock-softirq.h"
585 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock
)
591 * Enabling hardirqs with a softirq-safe lock held:
608 * Generate 12 testcases:
610 #include "locking-selftest-spin.h"
611 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin
)
613 #include "locking-selftest-wlock.h"
614 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock
)
616 #include "locking-selftest-rlock.h"
617 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock
)
623 * Enabling irqs with an irq-safe lock held:
640 * Generate 24 testcases:
642 #include "locking-selftest-spin-hardirq.h"
643 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin
)
645 #include "locking-selftest-rlock-hardirq.h"
646 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock
)
648 #include "locking-selftest-wlock-hardirq.h"
649 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock
)
651 #include "locking-selftest-spin-softirq.h"
652 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin
)
654 #include "locking-selftest-rlock-softirq.h"
655 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock
)
657 #include "locking-selftest-wlock-softirq.h"
658 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock
)
664 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
686 * Generate 36 testcases:
688 #include "locking-selftest-spin-hardirq.h"
689 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin
)
691 #include "locking-selftest-rlock-hardirq.h"
692 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock
)
694 #include "locking-selftest-wlock-hardirq.h"
695 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock
)
697 #include "locking-selftest-spin-softirq.h"
698 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin
)
700 #include "locking-selftest-rlock-softirq.h"
701 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock
)
703 #include "locking-selftest-wlock-softirq.h"
704 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock
)
711 * If a lock turns into softirq-safe, but earlier it took
712 * a softirq-unsafe lock:
734 * Generate 36 testcases:
736 #include "locking-selftest-spin-hardirq.h"
737 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin
)
739 #include "locking-selftest-rlock-hardirq.h"
740 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock
)
742 #include "locking-selftest-wlock-hardirq.h"
743 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock
)
745 #include "locking-selftest-spin-softirq.h"
746 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin
)
748 #include "locking-selftest-rlock-softirq.h"
749 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock
)
751 #include "locking-selftest-wlock-softirq.h"
752 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock
)
759 * read-lock / write-lock irq inversion.
763 * CPU#1 is at #1, i.e. it has write-locked A, but has not
766 * CPU#2 is at #2, i.e. it has locked B.
768 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
770 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
796 * Generate 36 testcases:
798 #include "locking-selftest-spin-hardirq.h"
799 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin
)
801 #include "locking-selftest-rlock-hardirq.h"
802 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock
)
804 #include "locking-selftest-wlock-hardirq.h"
805 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock
)
807 #include "locking-selftest-spin-softirq.h"
808 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin
)
810 #include "locking-selftest-rlock-softirq.h"
811 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock
)
813 #include "locking-selftest-wlock-softirq.h"
814 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock
)
821 * read-lock / write-lock recursion that is actually safe.
846 * Generate 12 testcases:
848 #include "locking-selftest-hardirq.h"
849 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard
)
851 #include "locking-selftest-softirq.h"
852 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft
)
859 * read-lock / write-lock recursion that is unsafe.
884 * Generate 12 testcases:
886 #include "locking-selftest-hardirq.h"
887 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
889 #include "locking-selftest-softirq.h"
890 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
892 #ifdef CONFIG_DEBUG_LOCK_ALLOC
893 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
894 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
895 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
896 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
898 # define I_SPINLOCK(x)
914 raw_spin_lock_init(&lock_##x); \
915 rwlock_init(&rwlock_##x); \
916 mutex_init(&mutex_##x); \
917 init_rwsem(&rwsem_##x); \
920 static void reset_locks(void)
923 I1(A
); I1(B
); I1(C
); I1(D
);
924 I1(X1
); I1(X2
); I1(Y1
); I1(Y2
); I1(Z1
); I1(Z2
);
926 I2(A
); I2(B
); I2(C
); I2(D
);
927 init_shared_classes();
933 static int testcase_total
;
934 static int testcase_successes
;
935 static int expected_testcase_failures
;
936 static int unexpected_testcase_failures
;
938 static void dotest(void (*testcase_fn
)(void), int expected
, int lockclass_mask
)
940 unsigned long saved_preempt_count
= preempt_count();
941 int expected_failure
= 0;
943 WARN_ON(irqs_disabled());
947 * Filter out expected failures:
949 #ifndef CONFIG_PROVE_LOCKING
950 if ((lockclass_mask
& LOCKTYPE_SPIN
) && debug_locks
!= expected
)
951 expected_failure
= 1;
952 if ((lockclass_mask
& LOCKTYPE_RWLOCK
) && debug_locks
!= expected
)
953 expected_failure
= 1;
954 if ((lockclass_mask
& LOCKTYPE_MUTEX
) && debug_locks
!= expected
)
955 expected_failure
= 1;
956 if ((lockclass_mask
& LOCKTYPE_RWSEM
) && debug_locks
!= expected
)
957 expected_failure
= 1;
959 if (debug_locks
!= expected
) {
960 if (expected_failure
) {
961 expected_testcase_failures
++;
964 unexpected_testcase_failures
++;
970 testcase_successes
++;
975 if (debug_locks_verbose
)
976 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
977 lockclass_mask
, debug_locks
, expected
);
979 * Some tests (e.g. double-unlock) might corrupt the preemption
980 * count, so restore it:
982 preempt_count() = saved_preempt_count
;
983 #ifdef CONFIG_TRACE_IRQFLAGS
985 current
->softirqs_enabled
= 0;
987 current
->softirqs_enabled
= 1;
993 static inline void print_testname(const char *testname
)
995 printk("%33s:", testname
);
998 #define DO_TESTCASE_1(desc, name, nr) \
999 print_testname(desc"/"#nr); \
1000 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1003 #define DO_TESTCASE_1B(desc, name, nr) \
1004 print_testname(desc"/"#nr); \
1005 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1008 #define DO_TESTCASE_3(desc, name, nr) \
1009 print_testname(desc"/"#nr); \
1010 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1011 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1012 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1015 #define DO_TESTCASE_3RW(desc, name, nr) \
1016 print_testname(desc"/"#nr); \
1017 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1018 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1019 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1022 #define DO_TESTCASE_6(desc, name) \
1023 print_testname(desc); \
1024 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1025 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1026 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1027 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1028 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1029 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1032 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1033 print_testname(desc); \
1034 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1035 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1036 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1037 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1038 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1039 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1043 * 'read' variant: rlocks must not trigger.
1045 #define DO_TESTCASE_6R(desc, name) \
1046 print_testname(desc); \
1047 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1048 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1049 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1050 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1051 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1052 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1055 #define DO_TESTCASE_2I(desc, name, nr) \
1056 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1057 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1059 #define DO_TESTCASE_2IB(desc, name, nr) \
1060 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1061 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1063 #define DO_TESTCASE_6I(desc, name, nr) \
1064 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1065 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1067 #define DO_TESTCASE_6IRW(desc, name, nr) \
1068 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1069 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1071 #define DO_TESTCASE_2x3(desc, name) \
1072 DO_TESTCASE_3(desc, name, 12); \
1073 DO_TESTCASE_3(desc, name, 21);
1075 #define DO_TESTCASE_2x6(desc, name) \
1076 DO_TESTCASE_6I(desc, name, 12); \
1077 DO_TESTCASE_6I(desc, name, 21);
1079 #define DO_TESTCASE_6x2(desc, name) \
1080 DO_TESTCASE_2I(desc, name, 123); \
1081 DO_TESTCASE_2I(desc, name, 132); \
1082 DO_TESTCASE_2I(desc, name, 213); \
1083 DO_TESTCASE_2I(desc, name, 231); \
1084 DO_TESTCASE_2I(desc, name, 312); \
1085 DO_TESTCASE_2I(desc, name, 321);
1087 #define DO_TESTCASE_6x2B(desc, name) \
1088 DO_TESTCASE_2IB(desc, name, 123); \
1089 DO_TESTCASE_2IB(desc, name, 132); \
1090 DO_TESTCASE_2IB(desc, name, 213); \
1091 DO_TESTCASE_2IB(desc, name, 231); \
1092 DO_TESTCASE_2IB(desc, name, 312); \
1093 DO_TESTCASE_2IB(desc, name, 321);
1095 #define DO_TESTCASE_6x6(desc, name) \
1096 DO_TESTCASE_6I(desc, name, 123); \
1097 DO_TESTCASE_6I(desc, name, 132); \
1098 DO_TESTCASE_6I(desc, name, 213); \
1099 DO_TESTCASE_6I(desc, name, 231); \
1100 DO_TESTCASE_6I(desc, name, 312); \
1101 DO_TESTCASE_6I(desc, name, 321);
1103 #define DO_TESTCASE_6x6RW(desc, name) \
1104 DO_TESTCASE_6IRW(desc, name, 123); \
1105 DO_TESTCASE_6IRW(desc, name, 132); \
1106 DO_TESTCASE_6IRW(desc, name, 213); \
1107 DO_TESTCASE_6IRW(desc, name, 231); \
1108 DO_TESTCASE_6IRW(desc, name, 312); \
1109 DO_TESTCASE_6IRW(desc, name, 321);
1112 void locking_selftest(void)
1115 * Got a locking failure before the selftest ran?
1118 printk("----------------------------------\n");
1119 printk("| Locking API testsuite disabled |\n");
1120 printk("----------------------------------\n");
1125 * Run the testsuite:
1127 printk("------------------------\n");
1128 printk("| Locking API testsuite:\n");
1129 printk("----------------------------------------------------------------------------\n");
1130 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1131 printk(" --------------------------------------------------------------------------\n");
1133 init_shared_classes();
1134 debug_locks_silent
= !debug_locks_verbose
;
1136 DO_TESTCASE_6R("A-A deadlock", AA
);
1137 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA
);
1138 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA
);
1139 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC
);
1140 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA
);
1141 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA
);
1142 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA
);
1143 DO_TESTCASE_6("double unlock", double_unlock
);
1144 DO_TESTCASE_6("initialize held", init_held
);
1145 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order
);
1147 printk(" --------------------------------------------------------------------------\n");
1148 print_testname("recursive read-lock");
1150 dotest(rlock_AA1
, SUCCESS
, LOCKTYPE_RWLOCK
);
1152 dotest(rsem_AA1
, FAILURE
, LOCKTYPE_RWSEM
);
1155 print_testname("recursive read-lock #2");
1157 dotest(rlock_AA1B
, SUCCESS
, LOCKTYPE_RWLOCK
);
1159 dotest(rsem_AA1B
, FAILURE
, LOCKTYPE_RWSEM
);
1162 print_testname("mixed read-write-lock");
1164 dotest(rlock_AA2
, FAILURE
, LOCKTYPE_RWLOCK
);
1166 dotest(rsem_AA2
, FAILURE
, LOCKTYPE_RWSEM
);
1169 print_testname("mixed write-read-lock");
1171 dotest(rlock_AA3
, FAILURE
, LOCKTYPE_RWLOCK
);
1173 dotest(rsem_AA3
, FAILURE
, LOCKTYPE_RWSEM
);
1176 printk(" --------------------------------------------------------------------------\n");
1179 * irq-context testcases:
1181 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1
);
1182 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A
);
1183 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B
);
1184 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3
);
1185 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4
);
1186 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion
);
1188 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion
);
1189 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1191 if (unexpected_testcase_failures
) {
1192 printk("-----------------------------------------------------------------\n");
1194 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1195 unexpected_testcase_failures
, testcase_total
);
1196 printk("-----------------------------------------------------------------\n");
1197 } else if (expected_testcase_failures
&& testcase_successes
) {
1198 printk("--------------------------------------------------------\n");
1199 printk("%3d out of %3d testcases failed, as expected. |\n",
1200 expected_testcase_failures
, testcase_total
);
1201 printk("----------------------------------------------------\n");
1203 } else if (expected_testcase_failures
&& !testcase_successes
) {
1204 printk("--------------------------------------------------------\n");
1205 printk("All %3d testcases failed, as expected. |\n",
1206 expected_testcase_failures
);
1207 printk("----------------------------------------\n");
1210 printk("-------------------------------------------------------\n");
1211 printk("Good, all %3d testcases passed! |\n",
1212 testcase_successes
);
1213 printk("---------------------------------\n");
1216 debug_locks_silent
= 0;