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 DEFINE_WW_CLASS(ww_lockdep);
31 static int __init setup_debug_locks_verbose(char *str)
33 get_option(&str, &debug_locks_verbose);
38 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
43 #define LOCKTYPE_SPIN 0x1
44 #define LOCKTYPE_RWLOCK 0x2
45 #define LOCKTYPE_MUTEX 0x4
46 #define LOCKTYPE_RWSEM 0x8
47 #define LOCKTYPE_WW 0x10
49 static struct ww_acquire_ctx t, t2;
50 static struct ww_mutex o, o2, o3;
53 * Normal standalone locks, for the circular and irq-context
56 static DEFINE_RAW_SPINLOCK(lock_A);
57 static DEFINE_RAW_SPINLOCK(lock_B);
58 static DEFINE_RAW_SPINLOCK(lock_C);
59 static DEFINE_RAW_SPINLOCK(lock_D);
61 static DEFINE_RWLOCK(rwlock_A);
62 static DEFINE_RWLOCK(rwlock_B);
63 static DEFINE_RWLOCK(rwlock_C);
64 static DEFINE_RWLOCK(rwlock_D);
66 static DEFINE_MUTEX(mutex_A);
67 static DEFINE_MUTEX(mutex_B);
68 static DEFINE_MUTEX(mutex_C);
69 static DEFINE_MUTEX(mutex_D);
71 static DECLARE_RWSEM(rwsem_A);
72 static DECLARE_RWSEM(rwsem_B);
73 static DECLARE_RWSEM(rwsem_C);
74 static DECLARE_RWSEM(rwsem_D);
77 * Locks that we initialize dynamically as well so that
78 * e.g. X1 and X2 becomes two instances of the same class,
79 * but X* and Y* are different classes. We do this so that
80 * we do not trigger a real lockup:
82 static DEFINE_RAW_SPINLOCK(lock_X1);
83 static DEFINE_RAW_SPINLOCK(lock_X2);
84 static DEFINE_RAW_SPINLOCK(lock_Y1);
85 static DEFINE_RAW_SPINLOCK(lock_Y2);
86 static DEFINE_RAW_SPINLOCK(lock_Z1);
87 static DEFINE_RAW_SPINLOCK(lock_Z2);
89 static DEFINE_RWLOCK(rwlock_X1);
90 static DEFINE_RWLOCK(rwlock_X2);
91 static DEFINE_RWLOCK(rwlock_Y1);
92 static DEFINE_RWLOCK(rwlock_Y2);
93 static DEFINE_RWLOCK(rwlock_Z1);
94 static DEFINE_RWLOCK(rwlock_Z2);
96 static DEFINE_MUTEX(mutex_X1);
97 static DEFINE_MUTEX(mutex_X2);
98 static DEFINE_MUTEX(mutex_Y1);
99 static DEFINE_MUTEX(mutex_Y2);
100 static DEFINE_MUTEX(mutex_Z1);
101 static DEFINE_MUTEX(mutex_Z2);
103 static DECLARE_RWSEM(rwsem_X1);
104 static DECLARE_RWSEM(rwsem_X2);
105 static DECLARE_RWSEM(rwsem_Y1);
106 static DECLARE_RWSEM(rwsem_Y2);
107 static DECLARE_RWSEM(rwsem_Z1);
108 static DECLARE_RWSEM(rwsem_Z2);
111 * non-inlined runtime initializers, to let separate locks share
112 * the same lock-class:
114 #define INIT_CLASS_FUNC(class) \
115 static noinline void \
116 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
117 struct mutex *mutex, struct rw_semaphore *rwsem)\
119 raw_spin_lock_init(lock); \
120 rwlock_init(rwlock); \
129 static void init_shared_classes(void)
131 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
132 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
134 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
135 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
137 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
138 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
142 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
143 * The following functions use a lock from a simulated hardirq/softirq
144 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
147 #define HARDIRQ_DISABLE local_irq_disable
148 #define HARDIRQ_ENABLE local_irq_enable
150 #define HARDIRQ_ENTER() \
151 local_irq_disable(); \
155 #define HARDIRQ_EXIT() \
159 #define SOFTIRQ_DISABLE local_bh_disable
160 #define SOFTIRQ_ENABLE local_bh_enable
162 #define SOFTIRQ_ENTER() \
163 local_bh_disable(); \
164 local_irq_disable(); \
165 lockdep_softirq_enter(); \
166 WARN_ON(!in_softirq());
168 #define SOFTIRQ_EXIT() \
169 lockdep_softirq_exit(); \
170 local_irq_enable(); \
174 * Shortcuts for lock/unlock API variants, to keep
175 * the testcases compact:
177 #define L(x) raw_spin_lock(&lock_##x)
178 #define U(x) raw_spin_unlock(&lock_##x)
179 #define LU(x) L(x); U(x)
180 #define SI(x) raw_spin_lock_init(&lock_##x)
182 #define WL(x) write_lock(&rwlock_##x)
183 #define WU(x) write_unlock(&rwlock_##x)
184 #define WLU(x) WL(x); WU(x)
186 #define RL(x) read_lock(&rwlock_##x)
187 #define RU(x) read_unlock(&rwlock_##x)
188 #define RLU(x) RL(x); RU(x)
189 #define RWI(x) rwlock_init(&rwlock_##x)
191 #define ML(x) mutex_lock(&mutex_##x)
192 #define MU(x) mutex_unlock(&mutex_##x)
193 #define MI(x) mutex_init(&mutex_##x)
195 #define WSL(x) down_write(&rwsem_##x)
196 #define WSU(x) up_write(&rwsem_##x)
198 #define RSL(x) down_read(&rwsem_##x)
199 #define RSU(x) up_read(&rwsem_##x)
200 #define RWSI(x) init_rwsem(&rwsem_##x)
202 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
203 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
205 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
207 #define WWAD(x) ww_acquire_done(x)
208 #define WWAF(x) ww_acquire_fini(x)
210 #define WWL(x, c) ww_mutex_lock(x, c)
211 #define WWT(x) ww_mutex_trylock(x)
212 #define WWL1(x) ww_mutex_lock(x, NULL)
213 #define WWU(x) ww_mutex_unlock(x)
216 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
219 * Generate different permutations of the same testcase, using
220 * the same basic lock-dependency/state events:
223 #define GENERATE_TESTCASE(name) \
225 static void name(void) { E(); }
227 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
229 static void name##_12(void) { E1(); E2(); } \
230 static void name##_21(void) { E2(); E1(); }
232 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
234 static void name##_123(void) { E1(); E2(); E3(); } \
235 static void name##_132(void) { E1(); E3(); E2(); } \
236 static void name##_213(void) { E2(); E1(); E3(); } \
237 static void name##_231(void) { E2(); E3(); E1(); } \
238 static void name##_312(void) { E3(); E1(); E2(); } \
239 static void name##_321(void) { E3(); E2(); E1(); }
248 LOCK(X2); /* this one should fail */
253 #include "locking-selftest-spin.h"
254 GENERATE_TESTCASE(AA_spin)
255 #include "locking-selftest-wlock.h"
256 GENERATE_TESTCASE(AA_wlock)
257 #include "locking-selftest-rlock.h"
258 GENERATE_TESTCASE(AA_rlock)
259 #include "locking-selftest-mutex.h"
260 GENERATE_TESTCASE(AA_mutex)
261 #include "locking-selftest-wsem.h"
262 GENERATE_TESTCASE(AA_wsem)
263 #include "locking-selftest-rsem.h"
264 GENERATE_TESTCASE(AA_rsem)
269 * Special-case for read-locking, they are
270 * allowed to recurse on the same lock class:
272 static void rlock_AA1(void)
275 RL(X1); // this one should NOT fail
278 static void rlock_AA1B(void)
281 RL(X2); // this one should NOT fail
284 static void rsem_AA1(void)
287 RSL(X1); // this one should fail
290 static void rsem_AA1B(void)
293 RSL(X2); // this one should fail
296 * The mixing of read and write locks is not allowed:
298 static void rlock_AA2(void)
301 WL(X2); // this one should fail
304 static void rsem_AA2(void)
307 WSL(X2); // this one should fail
310 static void rlock_AA3(void)
313 RL(X2); // this one should fail
316 static void rsem_AA3(void)
319 RSL(X2); // this one should fail
328 LOCK_UNLOCK_2(A, B); \
329 LOCK_UNLOCK_2(B, A); /* fail */
334 #include "locking-selftest-spin.h"
335 GENERATE_TESTCASE(ABBA_spin)
336 #include "locking-selftest-wlock.h"
337 GENERATE_TESTCASE(ABBA_wlock)
338 #include "locking-selftest-rlock.h"
339 GENERATE_TESTCASE(ABBA_rlock)
340 #include "locking-selftest-mutex.h"
341 GENERATE_TESTCASE(ABBA_mutex)
342 #include "locking-selftest-wsem.h"
343 GENERATE_TESTCASE(ABBA_wsem)
344 #include "locking-selftest-rsem.h"
345 GENERATE_TESTCASE(ABBA_rsem)
355 LOCK_UNLOCK_2(A, B); \
356 LOCK_UNLOCK_2(B, C); \
357 LOCK_UNLOCK_2(C, A); /* fail */
362 #include "locking-selftest-spin.h"
363 GENERATE_TESTCASE(ABBCCA_spin)
364 #include "locking-selftest-wlock.h"
365 GENERATE_TESTCASE(ABBCCA_wlock)
366 #include "locking-selftest-rlock.h"
367 GENERATE_TESTCASE(ABBCCA_rlock)
368 #include "locking-selftest-mutex.h"
369 GENERATE_TESTCASE(ABBCCA_mutex)
370 #include "locking-selftest-wsem.h"
371 GENERATE_TESTCASE(ABBCCA_wsem)
372 #include "locking-selftest-rsem.h"
373 GENERATE_TESTCASE(ABBCCA_rsem)
383 LOCK_UNLOCK_2(A, B); \
384 LOCK_UNLOCK_2(C, A); \
385 LOCK_UNLOCK_2(B, C); /* fail */
390 #include "locking-selftest-spin.h"
391 GENERATE_TESTCASE(ABCABC_spin)
392 #include "locking-selftest-wlock.h"
393 GENERATE_TESTCASE(ABCABC_wlock)
394 #include "locking-selftest-rlock.h"
395 GENERATE_TESTCASE(ABCABC_rlock)
396 #include "locking-selftest-mutex.h"
397 GENERATE_TESTCASE(ABCABC_mutex)
398 #include "locking-selftest-wsem.h"
399 GENERATE_TESTCASE(ABCABC_wsem)
400 #include "locking-selftest-rsem.h"
401 GENERATE_TESTCASE(ABCABC_rsem)
406 * AB BC CD DA deadlock:
411 LOCK_UNLOCK_2(A, B); \
412 LOCK_UNLOCK_2(B, C); \
413 LOCK_UNLOCK_2(C, D); \
414 LOCK_UNLOCK_2(D, A); /* fail */
419 #include "locking-selftest-spin.h"
420 GENERATE_TESTCASE(ABBCCDDA_spin)
421 #include "locking-selftest-wlock.h"
422 GENERATE_TESTCASE(ABBCCDDA_wlock)
423 #include "locking-selftest-rlock.h"
424 GENERATE_TESTCASE(ABBCCDDA_rlock)
425 #include "locking-selftest-mutex.h"
426 GENERATE_TESTCASE(ABBCCDDA_mutex)
427 #include "locking-selftest-wsem.h"
428 GENERATE_TESTCASE(ABBCCDDA_wsem)
429 #include "locking-selftest-rsem.h"
430 GENERATE_TESTCASE(ABBCCDDA_rsem)
435 * AB CD BD DA deadlock:
439 LOCK_UNLOCK_2(A, B); \
440 LOCK_UNLOCK_2(C, D); \
441 LOCK_UNLOCK_2(B, D); \
442 LOCK_UNLOCK_2(D, A); /* fail */
447 #include "locking-selftest-spin.h"
448 GENERATE_TESTCASE(ABCDBDDA_spin)
449 #include "locking-selftest-wlock.h"
450 GENERATE_TESTCASE(ABCDBDDA_wlock)
451 #include "locking-selftest-rlock.h"
452 GENERATE_TESTCASE(ABCDBDDA_rlock)
453 #include "locking-selftest-mutex.h"
454 GENERATE_TESTCASE(ABCDBDDA_mutex)
455 #include "locking-selftest-wsem.h"
456 GENERATE_TESTCASE(ABCDBDDA_wsem)
457 #include "locking-selftest-rsem.h"
458 GENERATE_TESTCASE(ABCDBDDA_rsem)
463 * AB CD BC DA deadlock:
467 LOCK_UNLOCK_2(A, B); \
468 LOCK_UNLOCK_2(C, D); \
469 LOCK_UNLOCK_2(B, C); \
470 LOCK_UNLOCK_2(D, A); /* fail */
475 #include "locking-selftest-spin.h"
476 GENERATE_TESTCASE(ABCDBCDA_spin)
477 #include "locking-selftest-wlock.h"
478 GENERATE_TESTCASE(ABCDBCDA_wlock)
479 #include "locking-selftest-rlock.h"
480 GENERATE_TESTCASE(ABCDBCDA_rlock)
481 #include "locking-selftest-mutex.h"
482 GENERATE_TESTCASE(ABCDBCDA_mutex)
483 #include "locking-selftest-wsem.h"
484 GENERATE_TESTCASE(ABCDBCDA_wsem)
485 #include "locking-selftest-rsem.h"
486 GENERATE_TESTCASE(ABCDBCDA_rsem)
497 UNLOCK(A); /* fail */
502 #include "locking-selftest-spin.h"
503 GENERATE_TESTCASE(double_unlock_spin)
504 #include "locking-selftest-wlock.h"
505 GENERATE_TESTCASE(double_unlock_wlock)
506 #include "locking-selftest-rlock.h"
507 GENERATE_TESTCASE(double_unlock_rlock)
508 #include "locking-selftest-mutex.h"
509 GENERATE_TESTCASE(double_unlock_mutex)
510 #include "locking-selftest-wsem.h"
511 GENERATE_TESTCASE(double_unlock_wsem)
512 #include "locking-selftest-rsem.h"
513 GENERATE_TESTCASE(double_unlock_rsem)
518 * Bad unlock ordering:
524 UNLOCK(A); /* fail */ \
530 #include "locking-selftest-spin.h"
531 GENERATE_TESTCASE(bad_unlock_order_spin)
532 #include "locking-selftest-wlock.h"
533 GENERATE_TESTCASE(bad_unlock_order_wlock)
534 #include "locking-selftest-rlock.h"
535 GENERATE_TESTCASE(bad_unlock_order_rlock)
536 #include "locking-selftest-mutex.h"
537 GENERATE_TESTCASE(bad_unlock_order_mutex)
538 #include "locking-selftest-wsem.h"
539 GENERATE_TESTCASE(bad_unlock_order_wsem)
540 #include "locking-selftest-rsem.h"
541 GENERATE_TESTCASE(bad_unlock_order_rsem)
546 * initializing a held lock:
556 #include "locking-selftest-spin.h"
557 GENERATE_TESTCASE(init_held_spin)
558 #include "locking-selftest-wlock.h"
559 GENERATE_TESTCASE(init_held_wlock)
560 #include "locking-selftest-rlock.h"
561 GENERATE_TESTCASE(init_held_rlock)
562 #include "locking-selftest-mutex.h"
563 GENERATE_TESTCASE(init_held_mutex)
564 #include "locking-selftest-wsem.h"
565 GENERATE_TESTCASE(init_held_wsem)
566 #include "locking-selftest-rsem.h"
567 GENERATE_TESTCASE(init_held_rsem)
572 * locking an irq-safe lock with irqs enabled:
587 * Generate 24 testcases:
589 #include "locking-selftest-spin-hardirq.h"
590 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
592 #include "locking-selftest-rlock-hardirq.h"
593 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
595 #include "locking-selftest-wlock-hardirq.h"
596 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
598 #include "locking-selftest-spin-softirq.h"
599 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
601 #include "locking-selftest-rlock-softirq.h"
602 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
604 #include "locking-selftest-wlock-softirq.h"
605 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
611 * Enabling hardirqs with a softirq-safe lock held:
628 * Generate 12 testcases:
630 #include "locking-selftest-spin.h"
631 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
633 #include "locking-selftest-wlock.h"
634 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
636 #include "locking-selftest-rlock.h"
637 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
643 * Enabling irqs with an irq-safe lock held:
660 * Generate 24 testcases:
662 #include "locking-selftest-spin-hardirq.h"
663 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
665 #include "locking-selftest-rlock-hardirq.h"
666 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
668 #include "locking-selftest-wlock-hardirq.h"
669 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
671 #include "locking-selftest-spin-softirq.h"
672 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
674 #include "locking-selftest-rlock-softirq.h"
675 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
677 #include "locking-selftest-wlock-softirq.h"
678 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
684 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
706 * Generate 36 testcases:
708 #include "locking-selftest-spin-hardirq.h"
709 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
711 #include "locking-selftest-rlock-hardirq.h"
712 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
714 #include "locking-selftest-wlock-hardirq.h"
715 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
717 #include "locking-selftest-spin-softirq.h"
718 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
720 #include "locking-selftest-rlock-softirq.h"
721 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
723 #include "locking-selftest-wlock-softirq.h"
724 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
731 * If a lock turns into softirq-safe, but earlier it took
732 * a softirq-unsafe lock:
754 * Generate 36 testcases:
756 #include "locking-selftest-spin-hardirq.h"
757 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
759 #include "locking-selftest-rlock-hardirq.h"
760 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
762 #include "locking-selftest-wlock-hardirq.h"
763 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
765 #include "locking-selftest-spin-softirq.h"
766 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
768 #include "locking-selftest-rlock-softirq.h"
769 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
771 #include "locking-selftest-wlock-softirq.h"
772 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
779 * read-lock / write-lock irq inversion.
783 * CPU#1 is at #1, i.e. it has write-locked A, but has not
786 * CPU#2 is at #2, i.e. it has locked B.
788 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
790 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
816 * Generate 36 testcases:
818 #include "locking-selftest-spin-hardirq.h"
819 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
821 #include "locking-selftest-rlock-hardirq.h"
822 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
824 #include "locking-selftest-wlock-hardirq.h"
825 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
827 #include "locking-selftest-spin-softirq.h"
828 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
830 #include "locking-selftest-rlock-softirq.h"
831 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
833 #include "locking-selftest-wlock-softirq.h"
834 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
841 * read-lock / write-lock recursion that is actually safe.
866 * Generate 12 testcases:
868 #include "locking-selftest-hardirq.h"
869 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
871 #include "locking-selftest-softirq.h"
872 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
879 * read-lock / write-lock recursion that is unsafe.
904 * Generate 12 testcases:
906 #include "locking-selftest-hardirq.h"
907 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
909 #include "locking-selftest-softirq.h"
910 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
912 #ifdef CONFIG_DEBUG_LOCK_ALLOC
913 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
914 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
915 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
916 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
917 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
919 # define I_SPINLOCK(x)
936 raw_spin_lock_init(&lock_##x); \
937 rwlock_init(&rwlock_##x); \
938 mutex_init(&mutex_##x); \
939 init_rwsem(&rwsem_##x); \
942 static void reset_locks(void)
945 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
946 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
948 I1(A); I1(B); I1(C); I1(D);
949 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
950 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
952 I2(A); I2(B); I2(C); I2(D);
953 init_shared_classes();
955 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
956 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
957 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
958 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
964 static int testcase_total;
965 static int testcase_successes;
966 static int expected_testcase_failures;
967 static int unexpected_testcase_failures;
969 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
971 unsigned long saved_preempt_count = preempt_count();
973 WARN_ON(irqs_disabled());
977 * Filter out expected failures:
979 #ifndef CONFIG_PROVE_LOCKING
980 if (expected == FAILURE && debug_locks) {
981 expected_testcase_failures++;
986 if (debug_locks != expected) {
987 unexpected_testcase_failures++;
992 testcase_successes++;
997 if (debug_locks_verbose)
998 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
999 lockclass_mask, debug_locks, expected);
1001 * Some tests (e.g. double-unlock) might corrupt the preemption
1002 * count, so restore it:
1004 preempt_count() = saved_preempt_count;
1005 #ifdef CONFIG_TRACE_IRQFLAGS
1006 if (softirq_count())
1007 current->softirqs_enabled = 0;
1009 current->softirqs_enabled = 1;
1015 static inline void print_testname(const char *testname)
1017 printk("%33s:", testname);
1020 #define DO_TESTCASE_1(desc, name, nr) \
1021 print_testname(desc"/"#nr); \
1022 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1025 #define DO_TESTCASE_1B(desc, name, nr) \
1026 print_testname(desc"/"#nr); \
1027 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1030 #define DO_TESTCASE_3(desc, name, nr) \
1031 print_testname(desc"/"#nr); \
1032 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1033 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1034 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1037 #define DO_TESTCASE_3RW(desc, name, nr) \
1038 print_testname(desc"/"#nr); \
1039 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1040 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1041 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1044 #define DO_TESTCASE_6(desc, name) \
1045 print_testname(desc); \
1046 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1047 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1048 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1049 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1050 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1051 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1054 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1055 print_testname(desc); \
1056 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1057 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1058 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1059 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1060 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1061 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1065 * 'read' variant: rlocks must not trigger.
1067 #define DO_TESTCASE_6R(desc, name) \
1068 print_testname(desc); \
1069 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1070 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1071 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1072 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1073 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1074 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1077 #define DO_TESTCASE_2I(desc, name, nr) \
1078 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1079 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1081 #define DO_TESTCASE_2IB(desc, name, nr) \
1082 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1083 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1085 #define DO_TESTCASE_6I(desc, name, nr) \
1086 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1087 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1089 #define DO_TESTCASE_6IRW(desc, name, nr) \
1090 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1091 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1093 #define DO_TESTCASE_2x3(desc, name) \
1094 DO_TESTCASE_3(desc, name, 12); \
1095 DO_TESTCASE_3(desc, name, 21);
1097 #define DO_TESTCASE_2x6(desc, name) \
1098 DO_TESTCASE_6I(desc, name, 12); \
1099 DO_TESTCASE_6I(desc, name, 21);
1101 #define DO_TESTCASE_6x2(desc, name) \
1102 DO_TESTCASE_2I(desc, name, 123); \
1103 DO_TESTCASE_2I(desc, name, 132); \
1104 DO_TESTCASE_2I(desc, name, 213); \
1105 DO_TESTCASE_2I(desc, name, 231); \
1106 DO_TESTCASE_2I(desc, name, 312); \
1107 DO_TESTCASE_2I(desc, name, 321);
1109 #define DO_TESTCASE_6x2B(desc, name) \
1110 DO_TESTCASE_2IB(desc, name, 123); \
1111 DO_TESTCASE_2IB(desc, name, 132); \
1112 DO_TESTCASE_2IB(desc, name, 213); \
1113 DO_TESTCASE_2IB(desc, name, 231); \
1114 DO_TESTCASE_2IB(desc, name, 312); \
1115 DO_TESTCASE_2IB(desc, name, 321);
1117 #define DO_TESTCASE_6x6(desc, name) \
1118 DO_TESTCASE_6I(desc, name, 123); \
1119 DO_TESTCASE_6I(desc, name, 132); \
1120 DO_TESTCASE_6I(desc, name, 213); \
1121 DO_TESTCASE_6I(desc, name, 231); \
1122 DO_TESTCASE_6I(desc, name, 312); \
1123 DO_TESTCASE_6I(desc, name, 321);
1125 #define DO_TESTCASE_6x6RW(desc, name) \
1126 DO_TESTCASE_6IRW(desc, name, 123); \
1127 DO_TESTCASE_6IRW(desc, name, 132); \
1128 DO_TESTCASE_6IRW(desc, name, 213); \
1129 DO_TESTCASE_6IRW(desc, name, 231); \
1130 DO_TESTCASE_6IRW(desc, name, 312); \
1131 DO_TESTCASE_6IRW(desc, name, 321);
1133 static void ww_test_fail_acquire(void)
1142 if (WARN_ON(!o.ctx) ||
1146 /* No lockdep test, pure API */
1148 WARN_ON(ret != -EALREADY);
1156 WARN_ON(ret != -EDEADLK);
1161 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1163 DEBUG_LOCKS_WARN_ON(1);
1167 static void ww_test_normal(void)
1174 * None of the ww_mutex codepaths should be taken in the 'normal'
1175 * mutex calls. The easiest way to verify this is by using the
1176 * normal mutex calls, and making sure o.ctx is unmodified.
1179 /* mutex_lock (and indirectly, mutex_lock_nested) */
1180 o.ctx = (void *)~0UL;
1181 mutex_lock(&o.base);
1182 mutex_unlock(&o.base);
1183 WARN_ON(o.ctx != (void *)~0UL);
1185 /* mutex_lock_interruptible (and *_nested) */
1186 o.ctx = (void *)~0UL;
1187 ret = mutex_lock_interruptible(&o.base);
1189 mutex_unlock(&o.base);
1192 WARN_ON(o.ctx != (void *)~0UL);
1194 /* mutex_lock_killable (and *_nested) */
1195 o.ctx = (void *)~0UL;
1196 ret = mutex_lock_killable(&o.base);
1198 mutex_unlock(&o.base);
1201 WARN_ON(o.ctx != (void *)~0UL);
1203 /* trylock, succeeding */
1204 o.ctx = (void *)~0UL;
1205 ret = mutex_trylock(&o.base);
1208 mutex_unlock(&o.base);
1211 WARN_ON(o.ctx != (void *)~0UL);
1213 /* trylock, failing */
1214 o.ctx = (void *)~0UL;
1215 mutex_lock(&o.base);
1216 ret = mutex_trylock(&o.base);
1218 mutex_unlock(&o.base);
1219 WARN_ON(o.ctx != (void *)~0UL);
1222 o.ctx = (void *)~0UL;
1223 mutex_lock_nest_lock(&o.base, &t);
1224 mutex_unlock(&o.base);
1225 WARN_ON(o.ctx != (void *)~0UL);
1228 static void ww_test_two_contexts(void)
1234 static void ww_test_diff_class(void)
1237 #ifdef CONFIG_DEBUG_MUTEXES
1243 static void ww_test_context_done_twice(void)
1251 static void ww_test_context_unlock_twice(void)
1259 static void ww_test_context_fini_early(void)
1267 static void ww_test_context_lock_after_done(void)
1274 static void ww_test_object_unlock_twice(void)
1281 static void ww_test_object_lock_unbalanced(void)
1290 static void ww_test_object_lock_stale_context(void)
1297 static void ww_test_edeadlk_normal(void)
1301 mutex_lock(&o2.base);
1303 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1313 WARN_ON(ret != -EDEADLK);
1316 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1317 mutex_unlock(&o2.base);
1323 static void ww_test_edeadlk_normal_slow(void)
1327 mutex_lock(&o2.base);
1328 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1339 WARN_ON(ret != -EDEADLK);
1342 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1343 mutex_unlock(&o2.base);
1346 ww_mutex_lock_slow(&o2, &t);
1349 static void ww_test_edeadlk_no_unlock(void)
1353 mutex_lock(&o2.base);
1355 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1365 WARN_ON(ret != -EDEADLK);
1368 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1369 mutex_unlock(&o2.base);
1374 static void ww_test_edeadlk_no_unlock_slow(void)
1378 mutex_lock(&o2.base);
1379 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1390 WARN_ON(ret != -EDEADLK);
1393 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1394 mutex_unlock(&o2.base);
1396 ww_mutex_lock_slow(&o2, &t);
1399 static void ww_test_edeadlk_acquire_more(void)
1403 mutex_lock(&o2.base);
1404 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1415 WARN_ON(ret != -EDEADLK);
1420 static void ww_test_edeadlk_acquire_more_slow(void)
1424 mutex_lock(&o2.base);
1425 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1436 WARN_ON(ret != -EDEADLK);
1438 ww_mutex_lock_slow(&o3, &t);
1441 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1445 mutex_lock(&o2.base);
1446 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1449 mutex_lock(&o3.base);
1450 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1461 WARN_ON(ret != -EDEADLK);
1464 WARN_ON(ret != -EDEADLK);
1467 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1471 mutex_lock(&o2.base);
1472 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1475 mutex_lock(&o3.base);
1476 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1487 WARN_ON(ret != -EDEADLK);
1489 ww_mutex_lock_slow(&o3, &t);
1492 static void ww_test_edeadlk_acquire_wrong(void)
1496 mutex_lock(&o2.base);
1497 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1508 WARN_ON(ret != -EDEADLK);
1517 static void ww_test_edeadlk_acquire_wrong_slow(void)
1521 mutex_lock(&o2.base);
1522 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1533 WARN_ON(ret != -EDEADLK);
1539 ww_mutex_lock_slow(&o3, &t);
1542 static void ww_test_spin_nest_unlocked(void)
1544 raw_spin_lock_nest_lock(&lock_A, &o.base);
1548 static void ww_test_unneeded_slow(void)
1552 ww_mutex_lock_slow(&o, &t);
1555 static void ww_test_context_block(void)
1566 static void ww_test_context_try(void)
1581 static void ww_test_context_context(void)
1597 static void ww_test_try_block(void)
1609 static void ww_test_try_try(void)
1621 static void ww_test_try_context(void)
1634 static void ww_test_block_block(void)
1640 static void ww_test_block_try(void)
1649 static void ww_test_block_context(void)
1660 static void ww_test_spin_block(void)
1676 static void ww_test_spin_try(void)
1696 static void ww_test_spin_context(void)
1718 static void ww_tests(void)
1720 printk(" --------------------------------------------------------------------------\n");
1721 printk(" | Wound/wait tests |\n");
1722 printk(" ---------------------\n");
1724 print_testname("ww api failures");
1725 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1726 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1727 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1730 print_testname("ww contexts mixing");
1731 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1732 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1735 print_testname("finishing ww context");
1736 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1737 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1738 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1739 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1742 print_testname("locking mismatches");
1743 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1744 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1745 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1748 print_testname("EDEADLK handling");
1749 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1750 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1751 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1752 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1753 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1754 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1755 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1756 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1757 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1758 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1761 print_testname("spinlock nest unlocked");
1762 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1765 printk(" -----------------------------------------------------\n");
1766 printk(" |block | try |context|\n");
1767 printk(" -----------------------------------------------------\n");
1769 print_testname("context");
1770 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1771 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1772 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1775 print_testname("try");
1776 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1777 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1778 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1781 print_testname("block");
1782 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1783 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1784 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1787 print_testname("spinlock");
1788 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1789 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1790 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1794 void locking_selftest(void)
1797 * Got a locking failure before the selftest ran?
1800 printk("----------------------------------\n");
1801 printk("| Locking API testsuite disabled |\n");
1802 printk("----------------------------------\n");
1807 * Run the testsuite:
1809 printk("------------------------\n");
1810 printk("| Locking API testsuite:\n");
1811 printk("----------------------------------------------------------------------------\n");
1812 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1813 printk(" --------------------------------------------------------------------------\n");
1815 init_shared_classes();
1816 debug_locks_silent = !debug_locks_verbose;
1818 DO_TESTCASE_6R("A-A deadlock", AA);
1819 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1820 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1821 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1822 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1823 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1824 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1825 DO_TESTCASE_6("double unlock", double_unlock);
1826 DO_TESTCASE_6("initialize held", init_held);
1827 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1829 printk(" --------------------------------------------------------------------------\n");
1830 print_testname("recursive read-lock");
1832 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1834 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1837 print_testname("recursive read-lock #2");
1839 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
1841 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1844 print_testname("mixed read-write-lock");
1846 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1848 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1851 print_testname("mixed write-read-lock");
1853 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1855 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1858 printk(" --------------------------------------------------------------------------\n");
1861 * irq-context testcases:
1863 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1864 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1865 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1866 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1867 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1868 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1870 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1871 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1875 if (unexpected_testcase_failures) {
1876 printk("-----------------------------------------------------------------\n");
1878 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1879 unexpected_testcase_failures, testcase_total);
1880 printk("-----------------------------------------------------------------\n");
1881 } else if (expected_testcase_failures && testcase_successes) {
1882 printk("--------------------------------------------------------\n");
1883 printk("%3d out of %3d testcases failed, as expected. |\n",
1884 expected_testcase_failures, testcase_total);
1885 printk("----------------------------------------------------\n");
1887 } else if (expected_testcase_failures && !testcase_successes) {
1888 printk("--------------------------------------------------------\n");
1889 printk("All %3d testcases failed, as expected. |\n",
1890 expected_testcase_failures);
1891 printk("----------------------------------------\n");
1894 printk("-------------------------------------------------------\n");
1895 printk("Good, all %3d testcases passed! |\n",
1896 testcase_successes);
1897 printk("---------------------------------\n");
1900 debug_locks_silent = 0;