2 * lib/locking-selftest.c
4 * Testsuite for various locking APIs: spinlocks, rwlocks,
5 * mutexes and rw-semaphores.
7 * It is checking both false positives and false negatives.
9 * Started by Ingo Molnar:
11 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
13 #include <linux/rwsem.h>
14 #include <linux/mutex.h>
15 #include <linux/ww_mutex.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/lockdep.h>
19 #include <linux/spinlock.h>
20 #include <linux/kallsyms.h>
21 #include <linux/interrupt.h>
22 #include <linux/debug_locks.h>
23 #include <linux/irqflags.h>
24 #include <linux/rtmutex.h>
27 * Change this to 1 if you want to see the failure printouts:
29 static unsigned int debug_locks_verbose
;
31 static DEFINE_WW_CLASS(ww_lockdep
);
33 static int __init
setup_debug_locks_verbose(char *str
)
35 get_option(&str
, &debug_locks_verbose
);
40 __setup("debug_locks_verbose=", setup_debug_locks_verbose
);
45 #define LOCKTYPE_SPIN 0x1
46 #define LOCKTYPE_RWLOCK 0x2
47 #define LOCKTYPE_MUTEX 0x4
48 #define LOCKTYPE_RWSEM 0x8
49 #define LOCKTYPE_WW 0x10
50 #define LOCKTYPE_RTMUTEX 0x20
52 static struct ww_acquire_ctx t
, t2
;
53 static struct ww_mutex o
, o2
, o3
;
56 * Normal standalone locks, for the circular and irq-context
59 static DEFINE_RAW_SPINLOCK(lock_A
);
60 static DEFINE_RAW_SPINLOCK(lock_B
);
61 static DEFINE_RAW_SPINLOCK(lock_C
);
62 static DEFINE_RAW_SPINLOCK(lock_D
);
64 static DEFINE_RWLOCK(rwlock_A
);
65 static DEFINE_RWLOCK(rwlock_B
);
66 static DEFINE_RWLOCK(rwlock_C
);
67 static DEFINE_RWLOCK(rwlock_D
);
69 static DEFINE_MUTEX(mutex_A
);
70 static DEFINE_MUTEX(mutex_B
);
71 static DEFINE_MUTEX(mutex_C
);
72 static DEFINE_MUTEX(mutex_D
);
74 static DECLARE_RWSEM(rwsem_A
);
75 static DECLARE_RWSEM(rwsem_B
);
76 static DECLARE_RWSEM(rwsem_C
);
77 static DECLARE_RWSEM(rwsem_D
);
79 #ifdef CONFIG_RT_MUTEXES
81 static DEFINE_RT_MUTEX(rtmutex_A
);
82 static DEFINE_RT_MUTEX(rtmutex_B
);
83 static DEFINE_RT_MUTEX(rtmutex_C
);
84 static DEFINE_RT_MUTEX(rtmutex_D
);
89 * Locks that we initialize dynamically as well so that
90 * e.g. X1 and X2 becomes two instances of the same class,
91 * but X* and Y* are different classes. We do this so that
92 * we do not trigger a real lockup:
94 static DEFINE_RAW_SPINLOCK(lock_X1
);
95 static DEFINE_RAW_SPINLOCK(lock_X2
);
96 static DEFINE_RAW_SPINLOCK(lock_Y1
);
97 static DEFINE_RAW_SPINLOCK(lock_Y2
);
98 static DEFINE_RAW_SPINLOCK(lock_Z1
);
99 static DEFINE_RAW_SPINLOCK(lock_Z2
);
101 static DEFINE_RWLOCK(rwlock_X1
);
102 static DEFINE_RWLOCK(rwlock_X2
);
103 static DEFINE_RWLOCK(rwlock_Y1
);
104 static DEFINE_RWLOCK(rwlock_Y2
);
105 static DEFINE_RWLOCK(rwlock_Z1
);
106 static DEFINE_RWLOCK(rwlock_Z2
);
108 static DEFINE_MUTEX(mutex_X1
);
109 static DEFINE_MUTEX(mutex_X2
);
110 static DEFINE_MUTEX(mutex_Y1
);
111 static DEFINE_MUTEX(mutex_Y2
);
112 static DEFINE_MUTEX(mutex_Z1
);
113 static DEFINE_MUTEX(mutex_Z2
);
115 static DECLARE_RWSEM(rwsem_X1
);
116 static DECLARE_RWSEM(rwsem_X2
);
117 static DECLARE_RWSEM(rwsem_Y1
);
118 static DECLARE_RWSEM(rwsem_Y2
);
119 static DECLARE_RWSEM(rwsem_Z1
);
120 static DECLARE_RWSEM(rwsem_Z2
);
122 #ifdef CONFIG_RT_MUTEXES
124 static DEFINE_RT_MUTEX(rtmutex_X1
);
125 static DEFINE_RT_MUTEX(rtmutex_X2
);
126 static DEFINE_RT_MUTEX(rtmutex_Y1
);
127 static DEFINE_RT_MUTEX(rtmutex_Y2
);
128 static DEFINE_RT_MUTEX(rtmutex_Z1
);
129 static DEFINE_RT_MUTEX(rtmutex_Z2
);
134 * non-inlined runtime initializers, to let separate locks share
135 * the same lock-class:
137 #define INIT_CLASS_FUNC(class) \
138 static noinline void \
139 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
140 struct mutex *mutex, struct rw_semaphore *rwsem)\
142 raw_spin_lock_init(lock); \
143 rwlock_init(rwlock); \
152 static void init_shared_classes(void)
154 #ifdef CONFIG_RT_MUTEXES
155 static struct lock_class_key rt_X
, rt_Y
, rt_Z
;
157 __rt_mutex_init(&rtmutex_X1
, __func__
, &rt_X
);
158 __rt_mutex_init(&rtmutex_X2
, __func__
, &rt_X
);
159 __rt_mutex_init(&rtmutex_Y1
, __func__
, &rt_Y
);
160 __rt_mutex_init(&rtmutex_Y2
, __func__
, &rt_Y
);
161 __rt_mutex_init(&rtmutex_Z1
, __func__
, &rt_Z
);
162 __rt_mutex_init(&rtmutex_Z2
, __func__
, &rt_Z
);
165 init_class_X(&lock_X1
, &rwlock_X1
, &mutex_X1
, &rwsem_X1
);
166 init_class_X(&lock_X2
, &rwlock_X2
, &mutex_X2
, &rwsem_X2
);
168 init_class_Y(&lock_Y1
, &rwlock_Y1
, &mutex_Y1
, &rwsem_Y1
);
169 init_class_Y(&lock_Y2
, &rwlock_Y2
, &mutex_Y2
, &rwsem_Y2
);
171 init_class_Z(&lock_Z1
, &rwlock_Z1
, &mutex_Z1
, &rwsem_Z1
);
172 init_class_Z(&lock_Z2
, &rwlock_Z2
, &mutex_Z2
, &rwsem_Z2
);
176 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
177 * The following functions use a lock from a simulated hardirq/softirq
178 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
181 #define HARDIRQ_DISABLE local_irq_disable
182 #define HARDIRQ_ENABLE local_irq_enable
184 #define HARDIRQ_ENTER() \
185 local_irq_disable(); \
189 #define HARDIRQ_EXIT() \
193 #define SOFTIRQ_DISABLE local_bh_disable
194 #define SOFTIRQ_ENABLE local_bh_enable
196 #define SOFTIRQ_ENTER() \
197 local_bh_disable(); \
198 local_irq_disable(); \
199 lockdep_softirq_enter(); \
200 WARN_ON(!in_softirq());
202 #define SOFTIRQ_EXIT() \
203 lockdep_softirq_exit(); \
204 local_irq_enable(); \
208 * Shortcuts for lock/unlock API variants, to keep
209 * the testcases compact:
211 #define L(x) raw_spin_lock(&lock_##x)
212 #define U(x) raw_spin_unlock(&lock_##x)
213 #define LU(x) L(x); U(x)
214 #define SI(x) raw_spin_lock_init(&lock_##x)
216 #define WL(x) write_lock(&rwlock_##x)
217 #define WU(x) write_unlock(&rwlock_##x)
218 #define WLU(x) WL(x); WU(x)
220 #define RL(x) read_lock(&rwlock_##x)
221 #define RU(x) read_unlock(&rwlock_##x)
222 #define RLU(x) RL(x); RU(x)
223 #define RWI(x) rwlock_init(&rwlock_##x)
225 #define ML(x) mutex_lock(&mutex_##x)
226 #define MU(x) mutex_unlock(&mutex_##x)
227 #define MI(x) mutex_init(&mutex_##x)
229 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
230 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
231 #define RTI(x) rt_mutex_init(&rtmutex_##x)
233 #define WSL(x) down_write(&rwsem_##x)
234 #define WSU(x) up_write(&rwsem_##x)
236 #define RSL(x) down_read(&rwsem_##x)
237 #define RSU(x) up_read(&rwsem_##x)
238 #define RWSI(x) init_rwsem(&rwsem_##x)
240 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
241 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
243 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
245 #define WWAD(x) ww_acquire_done(x)
246 #define WWAF(x) ww_acquire_fini(x)
248 #define WWL(x, c) ww_mutex_lock(x, c)
249 #define WWT(x) ww_mutex_trylock(x)
250 #define WWL1(x) ww_mutex_lock(x, NULL)
251 #define WWU(x) ww_mutex_unlock(x)
254 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
257 * Generate different permutations of the same testcase, using
258 * the same basic lock-dependency/state events:
261 #define GENERATE_TESTCASE(name) \
263 static void name(void) { E(); }
265 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
267 static void name##_12(void) { E1(); E2(); } \
268 static void name##_21(void) { E2(); E1(); }
270 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
272 static void name##_123(void) { E1(); E2(); E3(); } \
273 static void name##_132(void) { E1(); E3(); E2(); } \
274 static void name##_213(void) { E2(); E1(); E3(); } \
275 static void name##_231(void) { E2(); E3(); E1(); } \
276 static void name##_312(void) { E3(); E1(); E2(); } \
277 static void name##_321(void) { E3(); E2(); E1(); }
286 LOCK(X2); /* this one should fail */
291 #include "locking-selftest-spin.h"
292 GENERATE_TESTCASE(AA_spin
)
293 #include "locking-selftest-wlock.h"
294 GENERATE_TESTCASE(AA_wlock
)
295 #include "locking-selftest-rlock.h"
296 GENERATE_TESTCASE(AA_rlock
)
297 #include "locking-selftest-mutex.h"
298 GENERATE_TESTCASE(AA_mutex
)
299 #include "locking-selftest-wsem.h"
300 GENERATE_TESTCASE(AA_wsem
)
301 #include "locking-selftest-rsem.h"
302 GENERATE_TESTCASE(AA_rsem
)
304 #ifdef CONFIG_RT_MUTEXES
305 #include "locking-selftest-rtmutex.h"
306 GENERATE_TESTCASE(AA_rtmutex
);
312 * Special-case for read-locking, they are
313 * allowed to recurse on the same lock class:
315 static void rlock_AA1(void)
318 RL(X1
); // this one should NOT fail
321 static void rlock_AA1B(void)
324 RL(X2
); // this one should NOT fail
327 static void rsem_AA1(void)
330 RSL(X1
); // this one should fail
333 static void rsem_AA1B(void)
336 RSL(X2
); // this one should fail
339 * The mixing of read and write locks is not allowed:
341 static void rlock_AA2(void)
344 WL(X2
); // this one should fail
347 static void rsem_AA2(void)
350 WSL(X2
); // this one should fail
353 static void rlock_AA3(void)
356 RL(X2
); // this one should fail
359 static void rsem_AA3(void)
362 RSL(X2
); // this one should fail
371 LOCK_UNLOCK_2(A, B); \
372 LOCK_UNLOCK_2(B, A); /* fail */
377 #include "locking-selftest-spin.h"
378 GENERATE_TESTCASE(ABBA_spin
)
379 #include "locking-selftest-wlock.h"
380 GENERATE_TESTCASE(ABBA_wlock
)
381 #include "locking-selftest-rlock.h"
382 GENERATE_TESTCASE(ABBA_rlock
)
383 #include "locking-selftest-mutex.h"
384 GENERATE_TESTCASE(ABBA_mutex
)
385 #include "locking-selftest-wsem.h"
386 GENERATE_TESTCASE(ABBA_wsem
)
387 #include "locking-selftest-rsem.h"
388 GENERATE_TESTCASE(ABBA_rsem
)
390 #ifdef CONFIG_RT_MUTEXES
391 #include "locking-selftest-rtmutex.h"
392 GENERATE_TESTCASE(ABBA_rtmutex
);
403 LOCK_UNLOCK_2(A, B); \
404 LOCK_UNLOCK_2(B, C); \
405 LOCK_UNLOCK_2(C, A); /* fail */
410 #include "locking-selftest-spin.h"
411 GENERATE_TESTCASE(ABBCCA_spin
)
412 #include "locking-selftest-wlock.h"
413 GENERATE_TESTCASE(ABBCCA_wlock
)
414 #include "locking-selftest-rlock.h"
415 GENERATE_TESTCASE(ABBCCA_rlock
)
416 #include "locking-selftest-mutex.h"
417 GENERATE_TESTCASE(ABBCCA_mutex
)
418 #include "locking-selftest-wsem.h"
419 GENERATE_TESTCASE(ABBCCA_wsem
)
420 #include "locking-selftest-rsem.h"
421 GENERATE_TESTCASE(ABBCCA_rsem
)
423 #ifdef CONFIG_RT_MUTEXES
424 #include "locking-selftest-rtmutex.h"
425 GENERATE_TESTCASE(ABBCCA_rtmutex
);
436 LOCK_UNLOCK_2(A, B); \
437 LOCK_UNLOCK_2(C, A); \
438 LOCK_UNLOCK_2(B, C); /* fail */
443 #include "locking-selftest-spin.h"
444 GENERATE_TESTCASE(ABCABC_spin
)
445 #include "locking-selftest-wlock.h"
446 GENERATE_TESTCASE(ABCABC_wlock
)
447 #include "locking-selftest-rlock.h"
448 GENERATE_TESTCASE(ABCABC_rlock
)
449 #include "locking-selftest-mutex.h"
450 GENERATE_TESTCASE(ABCABC_mutex
)
451 #include "locking-selftest-wsem.h"
452 GENERATE_TESTCASE(ABCABC_wsem
)
453 #include "locking-selftest-rsem.h"
454 GENERATE_TESTCASE(ABCABC_rsem
)
456 #ifdef CONFIG_RT_MUTEXES
457 #include "locking-selftest-rtmutex.h"
458 GENERATE_TESTCASE(ABCABC_rtmutex
);
464 * AB BC CD DA deadlock:
469 LOCK_UNLOCK_2(A, B); \
470 LOCK_UNLOCK_2(B, C); \
471 LOCK_UNLOCK_2(C, D); \
472 LOCK_UNLOCK_2(D, A); /* fail */
477 #include "locking-selftest-spin.h"
478 GENERATE_TESTCASE(ABBCCDDA_spin
)
479 #include "locking-selftest-wlock.h"
480 GENERATE_TESTCASE(ABBCCDDA_wlock
)
481 #include "locking-selftest-rlock.h"
482 GENERATE_TESTCASE(ABBCCDDA_rlock
)
483 #include "locking-selftest-mutex.h"
484 GENERATE_TESTCASE(ABBCCDDA_mutex
)
485 #include "locking-selftest-wsem.h"
486 GENERATE_TESTCASE(ABBCCDDA_wsem
)
487 #include "locking-selftest-rsem.h"
488 GENERATE_TESTCASE(ABBCCDDA_rsem
)
490 #ifdef CONFIG_RT_MUTEXES
491 #include "locking-selftest-rtmutex.h"
492 GENERATE_TESTCASE(ABBCCDDA_rtmutex
);
498 * AB CD BD DA deadlock:
502 LOCK_UNLOCK_2(A, B); \
503 LOCK_UNLOCK_2(C, D); \
504 LOCK_UNLOCK_2(B, D); \
505 LOCK_UNLOCK_2(D, A); /* fail */
510 #include "locking-selftest-spin.h"
511 GENERATE_TESTCASE(ABCDBDDA_spin
)
512 #include "locking-selftest-wlock.h"
513 GENERATE_TESTCASE(ABCDBDDA_wlock
)
514 #include "locking-selftest-rlock.h"
515 GENERATE_TESTCASE(ABCDBDDA_rlock
)
516 #include "locking-selftest-mutex.h"
517 GENERATE_TESTCASE(ABCDBDDA_mutex
)
518 #include "locking-selftest-wsem.h"
519 GENERATE_TESTCASE(ABCDBDDA_wsem
)
520 #include "locking-selftest-rsem.h"
521 GENERATE_TESTCASE(ABCDBDDA_rsem
)
523 #ifdef CONFIG_RT_MUTEXES
524 #include "locking-selftest-rtmutex.h"
525 GENERATE_TESTCASE(ABCDBDDA_rtmutex
);
531 * AB CD BC DA deadlock:
535 LOCK_UNLOCK_2(A, B); \
536 LOCK_UNLOCK_2(C, D); \
537 LOCK_UNLOCK_2(B, C); \
538 LOCK_UNLOCK_2(D, A); /* fail */
543 #include "locking-selftest-spin.h"
544 GENERATE_TESTCASE(ABCDBCDA_spin
)
545 #include "locking-selftest-wlock.h"
546 GENERATE_TESTCASE(ABCDBCDA_wlock
)
547 #include "locking-selftest-rlock.h"
548 GENERATE_TESTCASE(ABCDBCDA_rlock
)
549 #include "locking-selftest-mutex.h"
550 GENERATE_TESTCASE(ABCDBCDA_mutex
)
551 #include "locking-selftest-wsem.h"
552 GENERATE_TESTCASE(ABCDBCDA_wsem
)
553 #include "locking-selftest-rsem.h"
554 GENERATE_TESTCASE(ABCDBCDA_rsem
)
556 #ifdef CONFIG_RT_MUTEXES
557 #include "locking-selftest-rtmutex.h"
558 GENERATE_TESTCASE(ABCDBCDA_rtmutex
);
570 UNLOCK(A); /* fail */
575 #include "locking-selftest-spin.h"
576 GENERATE_TESTCASE(double_unlock_spin
)
577 #include "locking-selftest-wlock.h"
578 GENERATE_TESTCASE(double_unlock_wlock
)
579 #include "locking-selftest-rlock.h"
580 GENERATE_TESTCASE(double_unlock_rlock
)
581 #include "locking-selftest-mutex.h"
582 GENERATE_TESTCASE(double_unlock_mutex
)
583 #include "locking-selftest-wsem.h"
584 GENERATE_TESTCASE(double_unlock_wsem
)
585 #include "locking-selftest-rsem.h"
586 GENERATE_TESTCASE(double_unlock_rsem
)
588 #ifdef CONFIG_RT_MUTEXES
589 #include "locking-selftest-rtmutex.h"
590 GENERATE_TESTCASE(double_unlock_rtmutex
);
596 * initializing a held lock:
606 #include "locking-selftest-spin.h"
607 GENERATE_TESTCASE(init_held_spin
)
608 #include "locking-selftest-wlock.h"
609 GENERATE_TESTCASE(init_held_wlock
)
610 #include "locking-selftest-rlock.h"
611 GENERATE_TESTCASE(init_held_rlock
)
612 #include "locking-selftest-mutex.h"
613 GENERATE_TESTCASE(init_held_mutex
)
614 #include "locking-selftest-wsem.h"
615 GENERATE_TESTCASE(init_held_wsem
)
616 #include "locking-selftest-rsem.h"
617 GENERATE_TESTCASE(init_held_rsem
)
619 #ifdef CONFIG_RT_MUTEXES
620 #include "locking-selftest-rtmutex.h"
621 GENERATE_TESTCASE(init_held_rtmutex
);
627 * locking an irq-safe lock with irqs enabled:
642 * Generate 24 testcases:
644 #include "locking-selftest-spin-hardirq.h"
645 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin
)
647 #include "locking-selftest-rlock-hardirq.h"
648 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock
)
650 #include "locking-selftest-wlock-hardirq.h"
651 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock
)
653 #include "locking-selftest-spin-softirq.h"
654 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin
)
656 #include "locking-selftest-rlock-softirq.h"
657 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock
)
659 #include "locking-selftest-wlock-softirq.h"
660 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock
)
666 * Enabling hardirqs with a softirq-safe lock held:
683 * Generate 12 testcases:
685 #include "locking-selftest-spin.h"
686 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin
)
688 #include "locking-selftest-wlock.h"
689 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock
)
691 #include "locking-selftest-rlock.h"
692 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock
)
698 * Enabling irqs with an irq-safe lock held:
715 * Generate 24 testcases:
717 #include "locking-selftest-spin-hardirq.h"
718 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin
)
720 #include "locking-selftest-rlock-hardirq.h"
721 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock
)
723 #include "locking-selftest-wlock-hardirq.h"
724 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock
)
726 #include "locking-selftest-spin-softirq.h"
727 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin
)
729 #include "locking-selftest-rlock-softirq.h"
730 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock
)
732 #include "locking-selftest-wlock-softirq.h"
733 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock
)
739 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
761 * Generate 36 testcases:
763 #include "locking-selftest-spin-hardirq.h"
764 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin
)
766 #include "locking-selftest-rlock-hardirq.h"
767 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock
)
769 #include "locking-selftest-wlock-hardirq.h"
770 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock
)
772 #include "locking-selftest-spin-softirq.h"
773 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin
)
775 #include "locking-selftest-rlock-softirq.h"
776 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock
)
778 #include "locking-selftest-wlock-softirq.h"
779 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock
)
786 * If a lock turns into softirq-safe, but earlier it took
787 * a softirq-unsafe lock:
809 * Generate 36 testcases:
811 #include "locking-selftest-spin-hardirq.h"
812 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin
)
814 #include "locking-selftest-rlock-hardirq.h"
815 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock
)
817 #include "locking-selftest-wlock-hardirq.h"
818 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock
)
820 #include "locking-selftest-spin-softirq.h"
821 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin
)
823 #include "locking-selftest-rlock-softirq.h"
824 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock
)
826 #include "locking-selftest-wlock-softirq.h"
827 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock
)
834 * read-lock / write-lock irq inversion.
838 * CPU#1 is at #1, i.e. it has write-locked A, but has not
841 * CPU#2 is at #2, i.e. it has locked B.
843 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
845 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
871 * Generate 36 testcases:
873 #include "locking-selftest-spin-hardirq.h"
874 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin
)
876 #include "locking-selftest-rlock-hardirq.h"
877 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock
)
879 #include "locking-selftest-wlock-hardirq.h"
880 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock
)
882 #include "locking-selftest-spin-softirq.h"
883 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin
)
885 #include "locking-selftest-rlock-softirq.h"
886 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock
)
888 #include "locking-selftest-wlock-softirq.h"
889 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock
)
896 * read-lock / write-lock recursion that is actually safe.
921 * Generate 12 testcases:
923 #include "locking-selftest-hardirq.h"
924 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard
)
926 #include "locking-selftest-softirq.h"
927 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft
)
934 * read-lock / write-lock recursion that is unsafe.
959 * Generate 12 testcases:
961 #include "locking-selftest-hardirq.h"
962 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
964 #include "locking-selftest-softirq.h"
965 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
967 #ifdef CONFIG_DEBUG_LOCK_ALLOC
968 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
969 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
970 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
971 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
972 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
973 #ifdef CONFIG_RT_MUTEXES
974 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
977 # define I_SPINLOCK(x)
985 # define I_RTMUTEX(x)
988 #ifdef CONFIG_RT_MUTEXES
989 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
991 #define I2_RTMUTEX(x)
1005 raw_spin_lock_init(&lock_##x); \
1006 rwlock_init(&rwlock_##x); \
1007 mutex_init(&mutex_##x); \
1008 init_rwsem(&rwsem_##x); \
1012 static void reset_locks(void)
1014 local_irq_disable();
1015 lockdep_free_key_range(&ww_lockdep
.acquire_key
, 1);
1016 lockdep_free_key_range(&ww_lockdep
.mutex_key
, 1);
1018 I1(A
); I1(B
); I1(C
); I1(D
);
1019 I1(X1
); I1(X2
); I1(Y1
); I1(Y2
); I1(Z1
); I1(Z2
);
1020 I_WW(t
); I_WW(t2
); I_WW(o
.base
); I_WW(o2
.base
); I_WW(o3
.base
);
1022 I2(A
); I2(B
); I2(C
); I2(D
);
1023 init_shared_classes();
1025 ww_mutex_init(&o
, &ww_lockdep
); ww_mutex_init(&o2
, &ww_lockdep
); ww_mutex_init(&o3
, &ww_lockdep
);
1026 memset(&t
, 0, sizeof(t
)); memset(&t2
, 0, sizeof(t2
));
1027 memset(&ww_lockdep
.acquire_key
, 0, sizeof(ww_lockdep
.acquire_key
));
1028 memset(&ww_lockdep
.mutex_key
, 0, sizeof(ww_lockdep
.mutex_key
));
1034 static int testcase_total
;
1035 static int testcase_successes
;
1036 static int expected_testcase_failures
;
1037 static int unexpected_testcase_failures
;
1039 static void dotest(void (*testcase_fn
)(void), int expected
, int lockclass_mask
)
1041 unsigned long saved_preempt_count
= preempt_count();
1043 WARN_ON(irqs_disabled());
1047 * Filter out expected failures:
1049 #ifndef CONFIG_PROVE_LOCKING
1050 if (expected
== FAILURE
&& debug_locks
) {
1051 expected_testcase_failures
++;
1056 if (debug_locks
!= expected
) {
1057 unexpected_testcase_failures
++;
1062 testcase_successes
++;
1067 if (debug_locks_verbose
)
1068 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1069 lockclass_mask
, debug_locks
, expected
);
1071 * Some tests (e.g. double-unlock) might corrupt the preemption
1072 * count, so restore it:
1074 preempt_count_set(saved_preempt_count
);
1075 #ifdef CONFIG_TRACE_IRQFLAGS
1076 if (softirq_count())
1077 current
->softirqs_enabled
= 0;
1079 current
->softirqs_enabled
= 1;
1085 #ifdef CONFIG_RT_MUTEXES
1086 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1088 #define dotest_rt(fn, e, m)
1091 static inline void print_testname(const char *testname
)
1093 printk("%33s:", testname
);
1096 #define DO_TESTCASE_1(desc, name, nr) \
1097 print_testname(desc"/"#nr); \
1098 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1101 #define DO_TESTCASE_1B(desc, name, nr) \
1102 print_testname(desc"/"#nr); \
1103 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1106 #define DO_TESTCASE_3(desc, name, nr) \
1107 print_testname(desc"/"#nr); \
1108 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1109 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1110 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1113 #define DO_TESTCASE_3RW(desc, name, nr) \
1114 print_testname(desc"/"#nr); \
1115 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1116 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1117 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1120 #define DO_TESTCASE_6(desc, name) \
1121 print_testname(desc); \
1122 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1123 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1124 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1125 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1126 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1127 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1128 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1131 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1132 print_testname(desc); \
1133 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1134 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1135 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1136 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1137 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1138 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1139 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1143 * 'read' variant: rlocks must not trigger.
1145 #define DO_TESTCASE_6R(desc, name) \
1146 print_testname(desc); \
1147 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1148 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1149 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1150 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1151 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1152 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1153 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1156 #define DO_TESTCASE_2I(desc, name, nr) \
1157 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1158 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1160 #define DO_TESTCASE_2IB(desc, name, nr) \
1161 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1162 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1164 #define DO_TESTCASE_6I(desc, name, nr) \
1165 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1166 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1168 #define DO_TESTCASE_6IRW(desc, name, nr) \
1169 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1170 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1172 #define DO_TESTCASE_2x3(desc, name) \
1173 DO_TESTCASE_3(desc, name, 12); \
1174 DO_TESTCASE_3(desc, name, 21);
1176 #define DO_TESTCASE_2x6(desc, name) \
1177 DO_TESTCASE_6I(desc, name, 12); \
1178 DO_TESTCASE_6I(desc, name, 21);
1180 #define DO_TESTCASE_6x2(desc, name) \
1181 DO_TESTCASE_2I(desc, name, 123); \
1182 DO_TESTCASE_2I(desc, name, 132); \
1183 DO_TESTCASE_2I(desc, name, 213); \
1184 DO_TESTCASE_2I(desc, name, 231); \
1185 DO_TESTCASE_2I(desc, name, 312); \
1186 DO_TESTCASE_2I(desc, name, 321);
1188 #define DO_TESTCASE_6x2B(desc, name) \
1189 DO_TESTCASE_2IB(desc, name, 123); \
1190 DO_TESTCASE_2IB(desc, name, 132); \
1191 DO_TESTCASE_2IB(desc, name, 213); \
1192 DO_TESTCASE_2IB(desc, name, 231); \
1193 DO_TESTCASE_2IB(desc, name, 312); \
1194 DO_TESTCASE_2IB(desc, name, 321);
1196 #define DO_TESTCASE_6x6(desc, name) \
1197 DO_TESTCASE_6I(desc, name, 123); \
1198 DO_TESTCASE_6I(desc, name, 132); \
1199 DO_TESTCASE_6I(desc, name, 213); \
1200 DO_TESTCASE_6I(desc, name, 231); \
1201 DO_TESTCASE_6I(desc, name, 312); \
1202 DO_TESTCASE_6I(desc, name, 321);
1204 #define DO_TESTCASE_6x6RW(desc, name) \
1205 DO_TESTCASE_6IRW(desc, name, 123); \
1206 DO_TESTCASE_6IRW(desc, name, 132); \
1207 DO_TESTCASE_6IRW(desc, name, 213); \
1208 DO_TESTCASE_6IRW(desc, name, 231); \
1209 DO_TESTCASE_6IRW(desc, name, 312); \
1210 DO_TESTCASE_6IRW(desc, name, 321);
1212 static void ww_test_fail_acquire(void)
1221 if (WARN_ON(!o
.ctx
) ||
1225 /* No lockdep test, pure API */
1227 WARN_ON(ret
!= -EALREADY
);
1235 WARN_ON(ret
!= -EDEADLK
);
1240 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1242 DEBUG_LOCKS_WARN_ON(1);
1246 static void ww_test_normal(void)
1253 * None of the ww_mutex codepaths should be taken in the 'normal'
1254 * mutex calls. The easiest way to verify this is by using the
1255 * normal mutex calls, and making sure o.ctx is unmodified.
1258 /* mutex_lock (and indirectly, mutex_lock_nested) */
1259 o
.ctx
= (void *)~0UL;
1260 mutex_lock(&o
.base
);
1261 mutex_unlock(&o
.base
);
1262 WARN_ON(o
.ctx
!= (void *)~0UL);
1264 /* mutex_lock_interruptible (and *_nested) */
1265 o
.ctx
= (void *)~0UL;
1266 ret
= mutex_lock_interruptible(&o
.base
);
1268 mutex_unlock(&o
.base
);
1271 WARN_ON(o
.ctx
!= (void *)~0UL);
1273 /* mutex_lock_killable (and *_nested) */
1274 o
.ctx
= (void *)~0UL;
1275 ret
= mutex_lock_killable(&o
.base
);
1277 mutex_unlock(&o
.base
);
1280 WARN_ON(o
.ctx
!= (void *)~0UL);
1282 /* trylock, succeeding */
1283 o
.ctx
= (void *)~0UL;
1284 ret
= mutex_trylock(&o
.base
);
1287 mutex_unlock(&o
.base
);
1290 WARN_ON(o
.ctx
!= (void *)~0UL);
1292 /* trylock, failing */
1293 o
.ctx
= (void *)~0UL;
1294 mutex_lock(&o
.base
);
1295 ret
= mutex_trylock(&o
.base
);
1297 mutex_unlock(&o
.base
);
1298 WARN_ON(o
.ctx
!= (void *)~0UL);
1301 o
.ctx
= (void *)~0UL;
1302 mutex_lock_nest_lock(&o
.base
, &t
);
1303 mutex_unlock(&o
.base
);
1304 WARN_ON(o
.ctx
!= (void *)~0UL);
1307 static void ww_test_two_contexts(void)
1313 static void ww_test_diff_class(void)
1316 #ifdef CONFIG_DEBUG_MUTEXES
1322 static void ww_test_context_done_twice(void)
1330 static void ww_test_context_unlock_twice(void)
1338 static void ww_test_context_fini_early(void)
1346 static void ww_test_context_lock_after_done(void)
1353 static void ww_test_object_unlock_twice(void)
1360 static void ww_test_object_lock_unbalanced(void)
1369 static void ww_test_object_lock_stale_context(void)
1376 static void ww_test_edeadlk_normal(void)
1380 mutex_lock(&o2
.base
);
1382 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1392 WARN_ON(ret
!= -EDEADLK
);
1395 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1396 mutex_unlock(&o2
.base
);
1402 static void ww_test_edeadlk_normal_slow(void)
1406 mutex_lock(&o2
.base
);
1407 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1418 WARN_ON(ret
!= -EDEADLK
);
1421 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1422 mutex_unlock(&o2
.base
);
1425 ww_mutex_lock_slow(&o2
, &t
);
1428 static void ww_test_edeadlk_no_unlock(void)
1432 mutex_lock(&o2
.base
);
1434 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1444 WARN_ON(ret
!= -EDEADLK
);
1447 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1448 mutex_unlock(&o2
.base
);
1453 static void ww_test_edeadlk_no_unlock_slow(void)
1457 mutex_lock(&o2
.base
);
1458 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1469 WARN_ON(ret
!= -EDEADLK
);
1472 mutex_acquire(&o2
.base
.dep_map
, 0, 1, _THIS_IP_
);
1473 mutex_unlock(&o2
.base
);
1475 ww_mutex_lock_slow(&o2
, &t
);
1478 static void ww_test_edeadlk_acquire_more(void)
1482 mutex_lock(&o2
.base
);
1483 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1494 WARN_ON(ret
!= -EDEADLK
);
1499 static void ww_test_edeadlk_acquire_more_slow(void)
1503 mutex_lock(&o2
.base
);
1504 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1515 WARN_ON(ret
!= -EDEADLK
);
1517 ww_mutex_lock_slow(&o3
, &t
);
1520 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1524 mutex_lock(&o2
.base
);
1525 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1528 mutex_lock(&o3
.base
);
1529 mutex_release(&o3
.base
.dep_map
, 1, _THIS_IP_
);
1540 WARN_ON(ret
!= -EDEADLK
);
1543 WARN_ON(ret
!= -EDEADLK
);
1546 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1550 mutex_lock(&o2
.base
);
1551 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1554 mutex_lock(&o3
.base
);
1555 mutex_release(&o3
.base
.dep_map
, 1, _THIS_IP_
);
1566 WARN_ON(ret
!= -EDEADLK
);
1568 ww_mutex_lock_slow(&o3
, &t
);
1571 static void ww_test_edeadlk_acquire_wrong(void)
1575 mutex_lock(&o2
.base
);
1576 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1587 WARN_ON(ret
!= -EDEADLK
);
1596 static void ww_test_edeadlk_acquire_wrong_slow(void)
1600 mutex_lock(&o2
.base
);
1601 mutex_release(&o2
.base
.dep_map
, 1, _THIS_IP_
);
1612 WARN_ON(ret
!= -EDEADLK
);
1618 ww_mutex_lock_slow(&o3
, &t
);
1621 static void ww_test_spin_nest_unlocked(void)
1623 raw_spin_lock_nest_lock(&lock_A
, &o
.base
);
1627 static void ww_test_unneeded_slow(void)
1631 ww_mutex_lock_slow(&o
, &t
);
1634 static void ww_test_context_block(void)
1645 static void ww_test_context_try(void)
1660 static void ww_test_context_context(void)
1676 static void ww_test_try_block(void)
1688 static void ww_test_try_try(void)
1700 static void ww_test_try_context(void)
1713 static void ww_test_block_block(void)
1719 static void ww_test_block_try(void)
1728 static void ww_test_block_context(void)
1739 static void ww_test_spin_block(void)
1755 static void ww_test_spin_try(void)
1775 static void ww_test_spin_context(void)
1797 static void ww_tests(void)
1799 printk(" --------------------------------------------------------------------------\n");
1800 printk(" | Wound/wait tests |\n");
1801 printk(" ---------------------\n");
1803 print_testname("ww api failures");
1804 dotest(ww_test_fail_acquire
, SUCCESS
, LOCKTYPE_WW
);
1805 dotest(ww_test_normal
, SUCCESS
, LOCKTYPE_WW
);
1806 dotest(ww_test_unneeded_slow
, FAILURE
, LOCKTYPE_WW
);
1809 print_testname("ww contexts mixing");
1810 dotest(ww_test_two_contexts
, FAILURE
, LOCKTYPE_WW
);
1811 dotest(ww_test_diff_class
, FAILURE
, LOCKTYPE_WW
);
1814 print_testname("finishing ww context");
1815 dotest(ww_test_context_done_twice
, FAILURE
, LOCKTYPE_WW
);
1816 dotest(ww_test_context_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
1817 dotest(ww_test_context_fini_early
, FAILURE
, LOCKTYPE_WW
);
1818 dotest(ww_test_context_lock_after_done
, FAILURE
, LOCKTYPE_WW
);
1821 print_testname("locking mismatches");
1822 dotest(ww_test_object_unlock_twice
, FAILURE
, LOCKTYPE_WW
);
1823 dotest(ww_test_object_lock_unbalanced
, FAILURE
, LOCKTYPE_WW
);
1824 dotest(ww_test_object_lock_stale_context
, FAILURE
, LOCKTYPE_WW
);
1827 print_testname("EDEADLK handling");
1828 dotest(ww_test_edeadlk_normal
, SUCCESS
, LOCKTYPE_WW
);
1829 dotest(ww_test_edeadlk_normal_slow
, SUCCESS
, LOCKTYPE_WW
);
1830 dotest(ww_test_edeadlk_no_unlock
, FAILURE
, LOCKTYPE_WW
);
1831 dotest(ww_test_edeadlk_no_unlock_slow
, FAILURE
, LOCKTYPE_WW
);
1832 dotest(ww_test_edeadlk_acquire_more
, FAILURE
, LOCKTYPE_WW
);
1833 dotest(ww_test_edeadlk_acquire_more_slow
, FAILURE
, LOCKTYPE_WW
);
1834 dotest(ww_test_edeadlk_acquire_more_edeadlk
, FAILURE
, LOCKTYPE_WW
);
1835 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow
, FAILURE
, LOCKTYPE_WW
);
1836 dotest(ww_test_edeadlk_acquire_wrong
, FAILURE
, LOCKTYPE_WW
);
1837 dotest(ww_test_edeadlk_acquire_wrong_slow
, FAILURE
, LOCKTYPE_WW
);
1840 print_testname("spinlock nest unlocked");
1841 dotest(ww_test_spin_nest_unlocked
, FAILURE
, LOCKTYPE_WW
);
1844 printk(" -----------------------------------------------------\n");
1845 printk(" |block | try |context|\n");
1846 printk(" -----------------------------------------------------\n");
1848 print_testname("context");
1849 dotest(ww_test_context_block
, FAILURE
, LOCKTYPE_WW
);
1850 dotest(ww_test_context_try
, SUCCESS
, LOCKTYPE_WW
);
1851 dotest(ww_test_context_context
, SUCCESS
, LOCKTYPE_WW
);
1854 print_testname("try");
1855 dotest(ww_test_try_block
, FAILURE
, LOCKTYPE_WW
);
1856 dotest(ww_test_try_try
, SUCCESS
, LOCKTYPE_WW
);
1857 dotest(ww_test_try_context
, FAILURE
, LOCKTYPE_WW
);
1860 print_testname("block");
1861 dotest(ww_test_block_block
, FAILURE
, LOCKTYPE_WW
);
1862 dotest(ww_test_block_try
, SUCCESS
, LOCKTYPE_WW
);
1863 dotest(ww_test_block_context
, FAILURE
, LOCKTYPE_WW
);
1866 print_testname("spinlock");
1867 dotest(ww_test_spin_block
, FAILURE
, LOCKTYPE_WW
);
1868 dotest(ww_test_spin_try
, SUCCESS
, LOCKTYPE_WW
);
1869 dotest(ww_test_spin_context
, FAILURE
, LOCKTYPE_WW
);
1873 void locking_selftest(void)
1876 * Got a locking failure before the selftest ran?
1879 printk("----------------------------------\n");
1880 printk("| Locking API testsuite disabled |\n");
1881 printk("----------------------------------\n");
1886 * Run the testsuite:
1888 printk("------------------------\n");
1889 printk("| Locking API testsuite:\n");
1890 printk("----------------------------------------------------------------------------\n");
1891 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1892 printk(" --------------------------------------------------------------------------\n");
1894 init_shared_classes();
1895 debug_locks_silent
= !debug_locks_verbose
;
1897 DO_TESTCASE_6R("A-A deadlock", AA
);
1898 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA
);
1899 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA
);
1900 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC
);
1901 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA
);
1902 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA
);
1903 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA
);
1904 DO_TESTCASE_6("double unlock", double_unlock
);
1905 DO_TESTCASE_6("initialize held", init_held
);
1907 printk(" --------------------------------------------------------------------------\n");
1908 print_testname("recursive read-lock");
1910 dotest(rlock_AA1
, SUCCESS
, LOCKTYPE_RWLOCK
);
1912 dotest(rsem_AA1
, FAILURE
, LOCKTYPE_RWSEM
);
1915 print_testname("recursive read-lock #2");
1917 dotest(rlock_AA1B
, SUCCESS
, LOCKTYPE_RWLOCK
);
1919 dotest(rsem_AA1B
, FAILURE
, LOCKTYPE_RWSEM
);
1922 print_testname("mixed read-write-lock");
1924 dotest(rlock_AA2
, FAILURE
, LOCKTYPE_RWLOCK
);
1926 dotest(rsem_AA2
, FAILURE
, LOCKTYPE_RWSEM
);
1929 print_testname("mixed write-read-lock");
1931 dotest(rlock_AA3
, FAILURE
, LOCKTYPE_RWLOCK
);
1933 dotest(rsem_AA3
, FAILURE
, LOCKTYPE_RWSEM
);
1936 printk(" --------------------------------------------------------------------------\n");
1939 * irq-context testcases:
1941 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1
);
1942 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A
);
1943 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B
);
1944 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3
);
1945 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4
);
1946 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion
);
1948 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion
);
1949 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1953 if (unexpected_testcase_failures
) {
1954 printk("-----------------------------------------------------------------\n");
1956 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1957 unexpected_testcase_failures
, testcase_total
);
1958 printk("-----------------------------------------------------------------\n");
1959 } else if (expected_testcase_failures
&& testcase_successes
) {
1960 printk("--------------------------------------------------------\n");
1961 printk("%3d out of %3d testcases failed, as expected. |\n",
1962 expected_testcase_failures
, testcase_total
);
1963 printk("----------------------------------------------------\n");
1965 } else if (expected_testcase_failures
&& !testcase_successes
) {
1966 printk("--------------------------------------------------------\n");
1967 printk("All %3d testcases failed, as expected. |\n",
1968 expected_testcase_failures
);
1969 printk("----------------------------------------\n");
1972 printk("-------------------------------------------------------\n");
1973 printk("Good, all %3d testcases passed! |\n",
1974 testcase_successes
);
1975 printk("---------------------------------\n");
1978 debug_locks_silent
= 0;