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;
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);
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);
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 if (debug_locks != expected) {
980 #ifndef CONFIG_PROVE_LOCKING
981 expected_testcase_failures++;
984 unexpected_testcase_failures++;
990 testcase_successes++;
995 if (debug_locks_verbose)
996 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
997 lockclass_mask, debug_locks, expected);
999 * Some tests (e.g. double-unlock) might corrupt the preemption
1000 * count, so restore it:
1002 preempt_count() = saved_preempt_count;
1003 #ifdef CONFIG_TRACE_IRQFLAGS
1004 if (softirq_count())
1005 current->softirqs_enabled = 0;
1007 current->softirqs_enabled = 1;
1013 static inline void print_testname(const char *testname)
1015 printk("%33s:", testname);
1018 #define DO_TESTCASE_1(desc, name, nr) \
1019 print_testname(desc"/"#nr); \
1020 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1023 #define DO_TESTCASE_1B(desc, name, nr) \
1024 print_testname(desc"/"#nr); \
1025 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1028 #define DO_TESTCASE_3(desc, name, nr) \
1029 print_testname(desc"/"#nr); \
1030 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1031 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1032 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1035 #define DO_TESTCASE_3RW(desc, name, nr) \
1036 print_testname(desc"/"#nr); \
1037 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1038 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1039 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1042 #define DO_TESTCASE_6(desc, name) \
1043 print_testname(desc); \
1044 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1045 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1046 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1047 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1048 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1049 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1052 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1053 print_testname(desc); \
1054 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1055 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1056 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1057 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1058 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1059 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1063 * 'read' variant: rlocks must not trigger.
1065 #define DO_TESTCASE_6R(desc, name) \
1066 print_testname(desc); \
1067 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1068 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1069 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1070 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1071 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1072 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1075 #define DO_TESTCASE_2I(desc, name, nr) \
1076 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1077 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1079 #define DO_TESTCASE_2IB(desc, name, nr) \
1080 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1081 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1083 #define DO_TESTCASE_6I(desc, name, nr) \
1084 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1085 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1087 #define DO_TESTCASE_6IRW(desc, name, nr) \
1088 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1089 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1091 #define DO_TESTCASE_2x3(desc, name) \
1092 DO_TESTCASE_3(desc, name, 12); \
1093 DO_TESTCASE_3(desc, name, 21);
1095 #define DO_TESTCASE_2x6(desc, name) \
1096 DO_TESTCASE_6I(desc, name, 12); \
1097 DO_TESTCASE_6I(desc, name, 21);
1099 #define DO_TESTCASE_6x2(desc, name) \
1100 DO_TESTCASE_2I(desc, name, 123); \
1101 DO_TESTCASE_2I(desc, name, 132); \
1102 DO_TESTCASE_2I(desc, name, 213); \
1103 DO_TESTCASE_2I(desc, name, 231); \
1104 DO_TESTCASE_2I(desc, name, 312); \
1105 DO_TESTCASE_2I(desc, name, 321);
1107 #define DO_TESTCASE_6x2B(desc, name) \
1108 DO_TESTCASE_2IB(desc, name, 123); \
1109 DO_TESTCASE_2IB(desc, name, 132); \
1110 DO_TESTCASE_2IB(desc, name, 213); \
1111 DO_TESTCASE_2IB(desc, name, 231); \
1112 DO_TESTCASE_2IB(desc, name, 312); \
1113 DO_TESTCASE_2IB(desc, name, 321);
1115 #define DO_TESTCASE_6x6(desc, name) \
1116 DO_TESTCASE_6I(desc, name, 123); \
1117 DO_TESTCASE_6I(desc, name, 132); \
1118 DO_TESTCASE_6I(desc, name, 213); \
1119 DO_TESTCASE_6I(desc, name, 231); \
1120 DO_TESTCASE_6I(desc, name, 312); \
1121 DO_TESTCASE_6I(desc, name, 321);
1123 #define DO_TESTCASE_6x6RW(desc, name) \
1124 DO_TESTCASE_6IRW(desc, name, 123); \
1125 DO_TESTCASE_6IRW(desc, name, 132); \
1126 DO_TESTCASE_6IRW(desc, name, 213); \
1127 DO_TESTCASE_6IRW(desc, name, 231); \
1128 DO_TESTCASE_6IRW(desc, name, 312); \
1129 DO_TESTCASE_6IRW(desc, name, 321);
1131 static void ww_test_fail_acquire(void)
1140 if (WARN_ON(!o.ctx) ||
1144 /* No lockdep test, pure API */
1146 WARN_ON(ret != -EALREADY);
1154 WARN_ON(ret != -EDEADLK);
1159 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1161 DEBUG_LOCKS_WARN_ON(1);
1165 static void ww_test_normal(void)
1172 * None of the ww_mutex codepaths should be taken in the 'normal'
1173 * mutex calls. The easiest way to verify this is by using the
1174 * normal mutex calls, and making sure o.ctx is unmodified.
1177 /* mutex_lock (and indirectly, mutex_lock_nested) */
1178 o.ctx = (void *)~0UL;
1179 mutex_lock(&o.base);
1180 mutex_unlock(&o.base);
1181 WARN_ON(o.ctx != (void *)~0UL);
1183 /* mutex_lock_interruptible (and *_nested) */
1184 o.ctx = (void *)~0UL;
1185 ret = mutex_lock_interruptible(&o.base);
1187 mutex_unlock(&o.base);
1190 WARN_ON(o.ctx != (void *)~0UL);
1192 /* mutex_lock_killable (and *_nested) */
1193 o.ctx = (void *)~0UL;
1194 ret = mutex_lock_killable(&o.base);
1196 mutex_unlock(&o.base);
1199 WARN_ON(o.ctx != (void *)~0UL);
1201 /* trylock, succeeding */
1202 o.ctx = (void *)~0UL;
1203 ret = mutex_trylock(&o.base);
1206 mutex_unlock(&o.base);
1209 WARN_ON(o.ctx != (void *)~0UL);
1211 /* trylock, failing */
1212 o.ctx = (void *)~0UL;
1213 mutex_lock(&o.base);
1214 ret = mutex_trylock(&o.base);
1216 mutex_unlock(&o.base);
1217 WARN_ON(o.ctx != (void *)~0UL);
1220 o.ctx = (void *)~0UL;
1221 mutex_lock_nest_lock(&o.base, &t);
1222 mutex_unlock(&o.base);
1223 WARN_ON(o.ctx != (void *)~0UL);
1226 static void ww_test_two_contexts(void)
1232 static void ww_test_diff_class(void)
1235 #ifdef CONFIG_DEBUG_MUTEXES
1241 static void ww_test_context_done_twice(void)
1249 static void ww_test_context_unlock_twice(void)
1257 static void ww_test_context_fini_early(void)
1265 static void ww_test_context_lock_after_done(void)
1272 static void ww_test_object_unlock_twice(void)
1279 static void ww_test_object_lock_unbalanced(void)
1288 static void ww_test_object_lock_stale_context(void)
1295 static void ww_test_spin_nest_unlocked(void)
1297 raw_spin_lock_nest_lock(&lock_A, &o.base);
1301 static void ww_test_unneeded_slow(void)
1305 ww_mutex_lock_slow(&o, &t);
1308 static void ww_test_context_block(void)
1319 static void ww_test_context_try(void)
1334 static void ww_test_context_context(void)
1350 static void ww_test_try_block(void)
1362 static void ww_test_try_try(void)
1374 static void ww_test_try_context(void)
1387 static void ww_test_block_block(void)
1393 static void ww_test_block_try(void)
1402 static void ww_test_block_context(void)
1413 static void ww_test_spin_block(void)
1429 static void ww_test_spin_try(void)
1449 static void ww_test_spin_context(void)
1471 static void ww_tests(void)
1473 printk(" --------------------------------------------------------------------------\n");
1474 printk(" | Wound/wait tests |\n");
1475 printk(" ---------------------\n");
1477 print_testname("ww api failures");
1478 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1479 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1480 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1483 print_testname("ww contexts mixing");
1484 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1485 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1488 print_testname("finishing ww context");
1489 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1490 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1491 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1492 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1495 print_testname("locking mismatches");
1496 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1497 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1498 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1501 print_testname("spinlock nest unlocked");
1502 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1505 printk(" -----------------------------------------------------\n");
1506 printk(" |block | try |context|\n");
1507 printk(" -----------------------------------------------------\n");
1509 print_testname("context");
1510 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1511 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1512 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1515 print_testname("try");
1516 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1517 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1518 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1521 print_testname("block");
1522 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1523 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1524 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1527 print_testname("spinlock");
1528 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1529 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1530 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1534 void locking_selftest(void)
1537 * Got a locking failure before the selftest ran?
1540 printk("----------------------------------\n");
1541 printk("| Locking API testsuite disabled |\n");
1542 printk("----------------------------------\n");
1547 * Run the testsuite:
1549 printk("------------------------\n");
1550 printk("| Locking API testsuite:\n");
1551 printk("----------------------------------------------------------------------------\n");
1552 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1553 printk(" --------------------------------------------------------------------------\n");
1555 init_shared_classes();
1556 debug_locks_silent = !debug_locks_verbose;
1558 DO_TESTCASE_6R("A-A deadlock", AA);
1559 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1560 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1561 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1562 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1563 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1564 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1565 DO_TESTCASE_6("double unlock", double_unlock);
1566 DO_TESTCASE_6("initialize held", init_held);
1567 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1569 printk(" --------------------------------------------------------------------------\n");
1570 print_testname("recursive read-lock");
1572 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1574 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1577 print_testname("recursive read-lock #2");
1579 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
1581 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1584 print_testname("mixed read-write-lock");
1586 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1588 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1591 print_testname("mixed write-read-lock");
1593 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1595 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1598 printk(" --------------------------------------------------------------------------\n");
1601 * irq-context testcases:
1603 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1604 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1605 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1606 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1607 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1608 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1610 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1611 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1615 if (unexpected_testcase_failures) {
1616 printk("-----------------------------------------------------------------\n");
1618 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1619 unexpected_testcase_failures, testcase_total);
1620 printk("-----------------------------------------------------------------\n");
1621 } else if (expected_testcase_failures && testcase_successes) {
1622 printk("--------------------------------------------------------\n");
1623 printk("%3d out of %3d testcases failed, as expected. |\n",
1624 expected_testcase_failures, testcase_total);
1625 printk("----------------------------------------------------\n");
1627 } else if (expected_testcase_failures && !testcase_successes) {
1628 printk("--------------------------------------------------------\n");
1629 printk("All %3d testcases failed, as expected. |\n",
1630 expected_testcase_failures);
1631 printk("----------------------------------------\n");
1634 printk("-------------------------------------------------------\n");
1635 printk("Good, all %3d testcases passed! |\n",
1636 testcase_successes);
1637 printk("---------------------------------\n");
1640 debug_locks_silent = 0;