Merge tag 'usb-serial-4.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / lib / locking-selftest.c
blob2b827b8a1d8c9945dfa04dc94bfda82216e045f8
1 /*
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);
37 return 1;
40 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
42 #define FAILURE 0
43 #define SUCCESS 1
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
57 * dependency tests:
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);
86 #endif
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);
131 #endif
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); \
144 mutex_init(mutex); \
145 init_rwsem(rwsem); \
148 INIT_CLASS_FUNC(X)
149 INIT_CLASS_FUNC(Y)
150 INIT_CLASS_FUNC(Z)
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);
163 #endif
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(); \
186 __irq_enter(); \
187 WARN_ON(!in_irq());
189 #define HARDIRQ_EXIT() \
190 __irq_exit(); \
191 local_irq_enable();
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(); \
205 local_bh_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)
242 #else
243 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
244 #endif
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(); }
280 * AA deadlock:
283 #define E() \
285 LOCK(X1); \
286 LOCK(X2); /* this one should fail */
289 * 6 testcases:
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);
307 #endif
309 #undef E
312 * Special-case for read-locking, they are
313 * allowed to recurse on the same lock class:
315 static void rlock_AA1(void)
317 RL(X1);
318 RL(X1); // this one should NOT fail
321 static void rlock_AA1B(void)
323 RL(X1);
324 RL(X2); // this one should NOT fail
327 static void rsem_AA1(void)
329 RSL(X1);
330 RSL(X1); // this one should fail
333 static void rsem_AA1B(void)
335 RSL(X1);
336 RSL(X2); // this one should fail
339 * The mixing of read and write locks is not allowed:
341 static void rlock_AA2(void)
343 RL(X1);
344 WL(X2); // this one should fail
347 static void rsem_AA2(void)
349 RSL(X1);
350 WSL(X2); // this one should fail
353 static void rlock_AA3(void)
355 WL(X1);
356 RL(X2); // this one should fail
359 static void rsem_AA3(void)
361 WSL(X1);
362 RSL(X2); // this one should fail
366 * read_lock(A)
367 * spin_lock(B)
368 * spin_lock(B)
369 * write_lock(A)
371 static void rlock_ABBA1(void)
373 RL(X1);
374 L(Y1);
375 U(Y1);
376 RU(X1);
378 L(Y1);
379 WL(X1);
380 WU(X1);
381 U(Y1); // should fail
384 static void rwsem_ABBA1(void)
386 RSL(X1);
387 ML(Y1);
388 MU(Y1);
389 RSU(X1);
391 ML(Y1);
392 WSL(X1);
393 WSU(X1);
394 MU(Y1); // should fail
398 * read_lock(A)
399 * spin_lock(B)
400 * spin_lock(B)
401 * read_lock(A)
403 static void rlock_ABBA2(void)
405 RL(X1);
406 L(Y1);
407 U(Y1);
408 RU(X1);
410 L(Y1);
411 RL(X1);
412 RU(X1);
413 U(Y1); // should NOT fail
416 static void rwsem_ABBA2(void)
418 RSL(X1);
419 ML(Y1);
420 MU(Y1);
421 RSU(X1);
423 ML(Y1);
424 RSL(X1);
425 RSU(X1);
426 MU(Y1); // should fail
431 * write_lock(A)
432 * spin_lock(B)
433 * spin_lock(B)
434 * write_lock(A)
436 static void rlock_ABBA3(void)
438 WL(X1);
439 L(Y1);
440 U(Y1);
441 WU(X1);
443 L(Y1);
444 WL(X1);
445 WU(X1);
446 U(Y1); // should fail
449 static void rwsem_ABBA3(void)
451 WSL(X1);
452 ML(Y1);
453 MU(Y1);
454 WSU(X1);
456 ML(Y1);
457 WSL(X1);
458 WSU(X1);
459 MU(Y1); // should fail
463 * ABBA deadlock:
466 #define E() \
468 LOCK_UNLOCK_2(A, B); \
469 LOCK_UNLOCK_2(B, A); /* fail */
472 * 6 testcases:
474 #include "locking-selftest-spin.h"
475 GENERATE_TESTCASE(ABBA_spin)
476 #include "locking-selftest-wlock.h"
477 GENERATE_TESTCASE(ABBA_wlock)
478 #include "locking-selftest-rlock.h"
479 GENERATE_TESTCASE(ABBA_rlock)
480 #include "locking-selftest-mutex.h"
481 GENERATE_TESTCASE(ABBA_mutex)
482 #include "locking-selftest-wsem.h"
483 GENERATE_TESTCASE(ABBA_wsem)
484 #include "locking-selftest-rsem.h"
485 GENERATE_TESTCASE(ABBA_rsem)
487 #ifdef CONFIG_RT_MUTEXES
488 #include "locking-selftest-rtmutex.h"
489 GENERATE_TESTCASE(ABBA_rtmutex);
490 #endif
492 #undef E
495 * AB BC CA deadlock:
498 #define E() \
500 LOCK_UNLOCK_2(A, B); \
501 LOCK_UNLOCK_2(B, C); \
502 LOCK_UNLOCK_2(C, A); /* fail */
505 * 6 testcases:
507 #include "locking-selftest-spin.h"
508 GENERATE_TESTCASE(ABBCCA_spin)
509 #include "locking-selftest-wlock.h"
510 GENERATE_TESTCASE(ABBCCA_wlock)
511 #include "locking-selftest-rlock.h"
512 GENERATE_TESTCASE(ABBCCA_rlock)
513 #include "locking-selftest-mutex.h"
514 GENERATE_TESTCASE(ABBCCA_mutex)
515 #include "locking-selftest-wsem.h"
516 GENERATE_TESTCASE(ABBCCA_wsem)
517 #include "locking-selftest-rsem.h"
518 GENERATE_TESTCASE(ABBCCA_rsem)
520 #ifdef CONFIG_RT_MUTEXES
521 #include "locking-selftest-rtmutex.h"
522 GENERATE_TESTCASE(ABBCCA_rtmutex);
523 #endif
525 #undef E
528 * AB CA BC deadlock:
531 #define E() \
533 LOCK_UNLOCK_2(A, B); \
534 LOCK_UNLOCK_2(C, A); \
535 LOCK_UNLOCK_2(B, C); /* fail */
538 * 6 testcases:
540 #include "locking-selftest-spin.h"
541 GENERATE_TESTCASE(ABCABC_spin)
542 #include "locking-selftest-wlock.h"
543 GENERATE_TESTCASE(ABCABC_wlock)
544 #include "locking-selftest-rlock.h"
545 GENERATE_TESTCASE(ABCABC_rlock)
546 #include "locking-selftest-mutex.h"
547 GENERATE_TESTCASE(ABCABC_mutex)
548 #include "locking-selftest-wsem.h"
549 GENERATE_TESTCASE(ABCABC_wsem)
550 #include "locking-selftest-rsem.h"
551 GENERATE_TESTCASE(ABCABC_rsem)
553 #ifdef CONFIG_RT_MUTEXES
554 #include "locking-selftest-rtmutex.h"
555 GENERATE_TESTCASE(ABCABC_rtmutex);
556 #endif
558 #undef E
561 * AB BC CD DA deadlock:
564 #define E() \
566 LOCK_UNLOCK_2(A, B); \
567 LOCK_UNLOCK_2(B, C); \
568 LOCK_UNLOCK_2(C, D); \
569 LOCK_UNLOCK_2(D, A); /* fail */
572 * 6 testcases:
574 #include "locking-selftest-spin.h"
575 GENERATE_TESTCASE(ABBCCDDA_spin)
576 #include "locking-selftest-wlock.h"
577 GENERATE_TESTCASE(ABBCCDDA_wlock)
578 #include "locking-selftest-rlock.h"
579 GENERATE_TESTCASE(ABBCCDDA_rlock)
580 #include "locking-selftest-mutex.h"
581 GENERATE_TESTCASE(ABBCCDDA_mutex)
582 #include "locking-selftest-wsem.h"
583 GENERATE_TESTCASE(ABBCCDDA_wsem)
584 #include "locking-selftest-rsem.h"
585 GENERATE_TESTCASE(ABBCCDDA_rsem)
587 #ifdef CONFIG_RT_MUTEXES
588 #include "locking-selftest-rtmutex.h"
589 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
590 #endif
592 #undef E
595 * AB CD BD DA deadlock:
597 #define E() \
599 LOCK_UNLOCK_2(A, B); \
600 LOCK_UNLOCK_2(C, D); \
601 LOCK_UNLOCK_2(B, D); \
602 LOCK_UNLOCK_2(D, A); /* fail */
605 * 6 testcases:
607 #include "locking-selftest-spin.h"
608 GENERATE_TESTCASE(ABCDBDDA_spin)
609 #include "locking-selftest-wlock.h"
610 GENERATE_TESTCASE(ABCDBDDA_wlock)
611 #include "locking-selftest-rlock.h"
612 GENERATE_TESTCASE(ABCDBDDA_rlock)
613 #include "locking-selftest-mutex.h"
614 GENERATE_TESTCASE(ABCDBDDA_mutex)
615 #include "locking-selftest-wsem.h"
616 GENERATE_TESTCASE(ABCDBDDA_wsem)
617 #include "locking-selftest-rsem.h"
618 GENERATE_TESTCASE(ABCDBDDA_rsem)
620 #ifdef CONFIG_RT_MUTEXES
621 #include "locking-selftest-rtmutex.h"
622 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
623 #endif
625 #undef E
628 * AB CD BC DA deadlock:
630 #define E() \
632 LOCK_UNLOCK_2(A, B); \
633 LOCK_UNLOCK_2(C, D); \
634 LOCK_UNLOCK_2(B, C); \
635 LOCK_UNLOCK_2(D, A); /* fail */
638 * 6 testcases:
640 #include "locking-selftest-spin.h"
641 GENERATE_TESTCASE(ABCDBCDA_spin)
642 #include "locking-selftest-wlock.h"
643 GENERATE_TESTCASE(ABCDBCDA_wlock)
644 #include "locking-selftest-rlock.h"
645 GENERATE_TESTCASE(ABCDBCDA_rlock)
646 #include "locking-selftest-mutex.h"
647 GENERATE_TESTCASE(ABCDBCDA_mutex)
648 #include "locking-selftest-wsem.h"
649 GENERATE_TESTCASE(ABCDBCDA_wsem)
650 #include "locking-selftest-rsem.h"
651 GENERATE_TESTCASE(ABCDBCDA_rsem)
653 #ifdef CONFIG_RT_MUTEXES
654 #include "locking-selftest-rtmutex.h"
655 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
656 #endif
658 #undef E
661 * Double unlock:
663 #define E() \
665 LOCK(A); \
666 UNLOCK(A); \
667 UNLOCK(A); /* fail */
670 * 6 testcases:
672 #include "locking-selftest-spin.h"
673 GENERATE_TESTCASE(double_unlock_spin)
674 #include "locking-selftest-wlock.h"
675 GENERATE_TESTCASE(double_unlock_wlock)
676 #include "locking-selftest-rlock.h"
677 GENERATE_TESTCASE(double_unlock_rlock)
678 #include "locking-selftest-mutex.h"
679 GENERATE_TESTCASE(double_unlock_mutex)
680 #include "locking-selftest-wsem.h"
681 GENERATE_TESTCASE(double_unlock_wsem)
682 #include "locking-selftest-rsem.h"
683 GENERATE_TESTCASE(double_unlock_rsem)
685 #ifdef CONFIG_RT_MUTEXES
686 #include "locking-selftest-rtmutex.h"
687 GENERATE_TESTCASE(double_unlock_rtmutex);
688 #endif
690 #undef E
693 * initializing a held lock:
695 #define E() \
697 LOCK(A); \
698 INIT(A); /* fail */
701 * 6 testcases:
703 #include "locking-selftest-spin.h"
704 GENERATE_TESTCASE(init_held_spin)
705 #include "locking-selftest-wlock.h"
706 GENERATE_TESTCASE(init_held_wlock)
707 #include "locking-selftest-rlock.h"
708 GENERATE_TESTCASE(init_held_rlock)
709 #include "locking-selftest-mutex.h"
710 GENERATE_TESTCASE(init_held_mutex)
711 #include "locking-selftest-wsem.h"
712 GENERATE_TESTCASE(init_held_wsem)
713 #include "locking-selftest-rsem.h"
714 GENERATE_TESTCASE(init_held_rsem)
716 #ifdef CONFIG_RT_MUTEXES
717 #include "locking-selftest-rtmutex.h"
718 GENERATE_TESTCASE(init_held_rtmutex);
719 #endif
721 #undef E
724 * locking an irq-safe lock with irqs enabled:
726 #define E1() \
728 IRQ_ENTER(); \
729 LOCK(A); \
730 UNLOCK(A); \
731 IRQ_EXIT();
733 #define E2() \
735 LOCK(A); \
736 UNLOCK(A);
739 * Generate 24 testcases:
741 #include "locking-selftest-spin-hardirq.h"
742 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
744 #include "locking-selftest-rlock-hardirq.h"
745 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
747 #include "locking-selftest-wlock-hardirq.h"
748 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
750 #include "locking-selftest-spin-softirq.h"
751 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
753 #include "locking-selftest-rlock-softirq.h"
754 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
756 #include "locking-selftest-wlock-softirq.h"
757 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
759 #undef E1
760 #undef E2
763 * Enabling hardirqs with a softirq-safe lock held:
765 #define E1() \
767 SOFTIRQ_ENTER(); \
768 LOCK(A); \
769 UNLOCK(A); \
770 SOFTIRQ_EXIT();
772 #define E2() \
774 HARDIRQ_DISABLE(); \
775 LOCK(A); \
776 HARDIRQ_ENABLE(); \
777 UNLOCK(A);
780 * Generate 12 testcases:
782 #include "locking-selftest-spin.h"
783 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
785 #include "locking-selftest-wlock.h"
786 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
788 #include "locking-selftest-rlock.h"
789 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
791 #undef E1
792 #undef E2
795 * Enabling irqs with an irq-safe lock held:
797 #define E1() \
799 IRQ_ENTER(); \
800 LOCK(A); \
801 UNLOCK(A); \
802 IRQ_EXIT();
804 #define E2() \
806 IRQ_DISABLE(); \
807 LOCK(A); \
808 IRQ_ENABLE(); \
809 UNLOCK(A);
812 * Generate 24 testcases:
814 #include "locking-selftest-spin-hardirq.h"
815 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
817 #include "locking-selftest-rlock-hardirq.h"
818 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
820 #include "locking-selftest-wlock-hardirq.h"
821 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
823 #include "locking-selftest-spin-softirq.h"
824 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
826 #include "locking-selftest-rlock-softirq.h"
827 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
829 #include "locking-selftest-wlock-softirq.h"
830 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
832 #undef E1
833 #undef E2
836 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
838 #define E1() \
840 LOCK(A); \
841 LOCK(B); \
842 UNLOCK(B); \
843 UNLOCK(A); \
845 #define E2() \
847 LOCK(B); \
848 UNLOCK(B);
850 #define E3() \
852 IRQ_ENTER(); \
853 LOCK(A); \
854 UNLOCK(A); \
855 IRQ_EXIT();
858 * Generate 36 testcases:
860 #include "locking-selftest-spin-hardirq.h"
861 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
863 #include "locking-selftest-rlock-hardirq.h"
864 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
866 #include "locking-selftest-wlock-hardirq.h"
867 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
869 #include "locking-selftest-spin-softirq.h"
870 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
872 #include "locking-selftest-rlock-softirq.h"
873 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
875 #include "locking-selftest-wlock-softirq.h"
876 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
878 #undef E1
879 #undef E2
880 #undef E3
883 * If a lock turns into softirq-safe, but earlier it took
884 * a softirq-unsafe lock:
887 #define E1() \
888 IRQ_DISABLE(); \
889 LOCK(A); \
890 LOCK(B); \
891 UNLOCK(B); \
892 UNLOCK(A); \
893 IRQ_ENABLE();
895 #define E2() \
896 LOCK(B); \
897 UNLOCK(B);
899 #define E3() \
900 IRQ_ENTER(); \
901 LOCK(A); \
902 UNLOCK(A); \
903 IRQ_EXIT();
906 * Generate 36 testcases:
908 #include "locking-selftest-spin-hardirq.h"
909 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
911 #include "locking-selftest-rlock-hardirq.h"
912 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
914 #include "locking-selftest-wlock-hardirq.h"
915 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
917 #include "locking-selftest-spin-softirq.h"
918 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
920 #include "locking-selftest-rlock-softirq.h"
921 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
923 #include "locking-selftest-wlock-softirq.h"
924 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
926 #undef E1
927 #undef E2
928 #undef E3
931 * read-lock / write-lock irq inversion.
933 * Deadlock scenario:
935 * CPU#1 is at #1, i.e. it has write-locked A, but has not
936 * taken B yet.
938 * CPU#2 is at #2, i.e. it has locked B.
940 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
942 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
943 * will spin on A.
946 #define E1() \
948 IRQ_DISABLE(); \
949 WL(A); \
950 LOCK(B); \
951 UNLOCK(B); \
952 WU(A); \
953 IRQ_ENABLE();
955 #define E2() \
957 LOCK(B); \
958 UNLOCK(B);
960 #define E3() \
962 IRQ_ENTER(); \
963 RL(A); \
964 RU(A); \
965 IRQ_EXIT();
968 * Generate 36 testcases:
970 #include "locking-selftest-spin-hardirq.h"
971 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
973 #include "locking-selftest-rlock-hardirq.h"
974 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
976 #include "locking-selftest-wlock-hardirq.h"
977 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
979 #include "locking-selftest-spin-softirq.h"
980 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
982 #include "locking-selftest-rlock-softirq.h"
983 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
985 #include "locking-selftest-wlock-softirq.h"
986 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
988 #undef E1
989 #undef E2
990 #undef E3
993 * read-lock / write-lock recursion that is actually safe.
996 #define E1() \
998 IRQ_DISABLE(); \
999 WL(A); \
1000 WU(A); \
1001 IRQ_ENABLE();
1003 #define E2() \
1005 RL(A); \
1006 RU(A); \
1008 #define E3() \
1010 IRQ_ENTER(); \
1011 RL(A); \
1012 L(B); \
1013 U(B); \
1014 RU(A); \
1015 IRQ_EXIT();
1018 * Generate 12 testcases:
1020 #include "locking-selftest-hardirq.h"
1021 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
1023 #include "locking-selftest-softirq.h"
1024 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
1026 #undef E1
1027 #undef E2
1028 #undef E3
1031 * read-lock / write-lock recursion that is unsafe.
1034 #define E1() \
1036 IRQ_DISABLE(); \
1037 L(B); \
1038 WL(A); \
1039 WU(A); \
1040 U(B); \
1041 IRQ_ENABLE();
1043 #define E2() \
1045 RL(A); \
1046 RU(A); \
1048 #define E3() \
1050 IRQ_ENTER(); \
1051 L(B); \
1052 U(B); \
1053 IRQ_EXIT();
1056 * Generate 12 testcases:
1058 #include "locking-selftest-hardirq.h"
1059 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
1061 #include "locking-selftest-softirq.h"
1062 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
1064 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1065 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1066 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1067 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1068 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1069 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1070 #ifdef CONFIG_RT_MUTEXES
1071 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1072 #endif
1073 #else
1074 # define I_SPINLOCK(x)
1075 # define I_RWLOCK(x)
1076 # define I_MUTEX(x)
1077 # define I_RWSEM(x)
1078 # define I_WW(x)
1079 #endif
1081 #ifndef I_RTMUTEX
1082 # define I_RTMUTEX(x)
1083 #endif
1085 #ifdef CONFIG_RT_MUTEXES
1086 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1087 #else
1088 #define I2_RTMUTEX(x)
1089 #endif
1091 #define I1(x) \
1092 do { \
1093 I_SPINLOCK(x); \
1094 I_RWLOCK(x); \
1095 I_MUTEX(x); \
1096 I_RWSEM(x); \
1097 I_RTMUTEX(x); \
1098 } while (0)
1100 #define I2(x) \
1101 do { \
1102 raw_spin_lock_init(&lock_##x); \
1103 rwlock_init(&rwlock_##x); \
1104 mutex_init(&mutex_##x); \
1105 init_rwsem(&rwsem_##x); \
1106 I2_RTMUTEX(x); \
1107 } while (0)
1109 static void reset_locks(void)
1111 local_irq_disable();
1112 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1113 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1115 I1(A); I1(B); I1(C); I1(D);
1116 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1117 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
1118 lockdep_reset();
1119 I2(A); I2(B); I2(C); I2(D);
1120 init_shared_classes();
1122 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1123 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1124 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1125 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
1126 local_irq_enable();
1129 #undef I
1131 static int testcase_total;
1132 static int testcase_successes;
1133 static int expected_testcase_failures;
1134 static int unexpected_testcase_failures;
1136 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1138 unsigned long saved_preempt_count = preempt_count();
1140 WARN_ON(irqs_disabled());
1142 testcase_fn();
1144 * Filter out expected failures:
1146 #ifndef CONFIG_PROVE_LOCKING
1147 if (expected == FAILURE && debug_locks) {
1148 expected_testcase_failures++;
1149 pr_cont("failed|");
1151 else
1152 #endif
1153 if (debug_locks != expected) {
1154 unexpected_testcase_failures++;
1155 pr_cont("FAILED|");
1156 } else {
1157 testcase_successes++;
1158 pr_cont(" ok |");
1160 testcase_total++;
1162 if (debug_locks_verbose)
1163 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1164 lockclass_mask, debug_locks, expected);
1166 * Some tests (e.g. double-unlock) might corrupt the preemption
1167 * count, so restore it:
1169 preempt_count_set(saved_preempt_count);
1170 #ifdef CONFIG_TRACE_IRQFLAGS
1171 if (softirq_count())
1172 current->softirqs_enabled = 0;
1173 else
1174 current->softirqs_enabled = 1;
1175 #endif
1177 reset_locks();
1180 #ifdef CONFIG_RT_MUTEXES
1181 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1182 #else
1183 #define dotest_rt(fn, e, m)
1184 #endif
1186 static inline void print_testname(const char *testname)
1188 printk("%33s:", testname);
1191 #define DO_TESTCASE_1(desc, name, nr) \
1192 print_testname(desc"/"#nr); \
1193 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1194 pr_cont("\n");
1196 #define DO_TESTCASE_1B(desc, name, nr) \
1197 print_testname(desc"/"#nr); \
1198 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1199 pr_cont("\n");
1201 #define DO_TESTCASE_3(desc, name, nr) \
1202 print_testname(desc"/"#nr); \
1203 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1204 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1205 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1206 pr_cont("\n");
1208 #define DO_TESTCASE_3RW(desc, name, nr) \
1209 print_testname(desc"/"#nr); \
1210 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1211 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1212 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1213 pr_cont("\n");
1215 #define DO_TESTCASE_6(desc, name) \
1216 print_testname(desc); \
1217 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1218 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1219 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1220 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1221 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1222 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1223 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1224 pr_cont("\n");
1226 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1227 print_testname(desc); \
1228 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1229 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1230 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1231 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1232 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1233 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1234 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1235 pr_cont("\n");
1238 * 'read' variant: rlocks must not trigger.
1240 #define DO_TESTCASE_6R(desc, name) \
1241 print_testname(desc); \
1242 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1243 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1244 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1245 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1246 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1247 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1248 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1249 pr_cont("\n");
1251 #define DO_TESTCASE_2I(desc, name, nr) \
1252 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1253 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1255 #define DO_TESTCASE_2IB(desc, name, nr) \
1256 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1257 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1259 #define DO_TESTCASE_6I(desc, name, nr) \
1260 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1261 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1263 #define DO_TESTCASE_6IRW(desc, name, nr) \
1264 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1265 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1267 #define DO_TESTCASE_2x3(desc, name) \
1268 DO_TESTCASE_3(desc, name, 12); \
1269 DO_TESTCASE_3(desc, name, 21);
1271 #define DO_TESTCASE_2x6(desc, name) \
1272 DO_TESTCASE_6I(desc, name, 12); \
1273 DO_TESTCASE_6I(desc, name, 21);
1275 #define DO_TESTCASE_6x2(desc, name) \
1276 DO_TESTCASE_2I(desc, name, 123); \
1277 DO_TESTCASE_2I(desc, name, 132); \
1278 DO_TESTCASE_2I(desc, name, 213); \
1279 DO_TESTCASE_2I(desc, name, 231); \
1280 DO_TESTCASE_2I(desc, name, 312); \
1281 DO_TESTCASE_2I(desc, name, 321);
1283 #define DO_TESTCASE_6x2B(desc, name) \
1284 DO_TESTCASE_2IB(desc, name, 123); \
1285 DO_TESTCASE_2IB(desc, name, 132); \
1286 DO_TESTCASE_2IB(desc, name, 213); \
1287 DO_TESTCASE_2IB(desc, name, 231); \
1288 DO_TESTCASE_2IB(desc, name, 312); \
1289 DO_TESTCASE_2IB(desc, name, 321);
1291 #define DO_TESTCASE_6x6(desc, name) \
1292 DO_TESTCASE_6I(desc, name, 123); \
1293 DO_TESTCASE_6I(desc, name, 132); \
1294 DO_TESTCASE_6I(desc, name, 213); \
1295 DO_TESTCASE_6I(desc, name, 231); \
1296 DO_TESTCASE_6I(desc, name, 312); \
1297 DO_TESTCASE_6I(desc, name, 321);
1299 #define DO_TESTCASE_6x6RW(desc, name) \
1300 DO_TESTCASE_6IRW(desc, name, 123); \
1301 DO_TESTCASE_6IRW(desc, name, 132); \
1302 DO_TESTCASE_6IRW(desc, name, 213); \
1303 DO_TESTCASE_6IRW(desc, name, 231); \
1304 DO_TESTCASE_6IRW(desc, name, 312); \
1305 DO_TESTCASE_6IRW(desc, name, 321);
1307 static void ww_test_fail_acquire(void)
1309 int ret;
1311 WWAI(&t);
1312 t.stamp++;
1314 ret = WWL(&o, &t);
1316 if (WARN_ON(!o.ctx) ||
1317 WARN_ON(ret))
1318 return;
1320 /* No lockdep test, pure API */
1321 ret = WWL(&o, &t);
1322 WARN_ON(ret != -EALREADY);
1324 ret = WWT(&o);
1325 WARN_ON(ret);
1327 t2 = t;
1328 t2.stamp++;
1329 ret = WWL(&o, &t2);
1330 WARN_ON(ret != -EDEADLK);
1331 WWU(&o);
1333 if (WWT(&o))
1334 WWU(&o);
1335 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1336 else
1337 DEBUG_LOCKS_WARN_ON(1);
1338 #endif
1341 static void ww_test_normal(void)
1343 int ret;
1345 WWAI(&t);
1348 * None of the ww_mutex codepaths should be taken in the 'normal'
1349 * mutex calls. The easiest way to verify this is by using the
1350 * normal mutex calls, and making sure o.ctx is unmodified.
1353 /* mutex_lock (and indirectly, mutex_lock_nested) */
1354 o.ctx = (void *)~0UL;
1355 mutex_lock(&o.base);
1356 mutex_unlock(&o.base);
1357 WARN_ON(o.ctx != (void *)~0UL);
1359 /* mutex_lock_interruptible (and *_nested) */
1360 o.ctx = (void *)~0UL;
1361 ret = mutex_lock_interruptible(&o.base);
1362 if (!ret)
1363 mutex_unlock(&o.base);
1364 else
1365 WARN_ON(1);
1366 WARN_ON(o.ctx != (void *)~0UL);
1368 /* mutex_lock_killable (and *_nested) */
1369 o.ctx = (void *)~0UL;
1370 ret = mutex_lock_killable(&o.base);
1371 if (!ret)
1372 mutex_unlock(&o.base);
1373 else
1374 WARN_ON(1);
1375 WARN_ON(o.ctx != (void *)~0UL);
1377 /* trylock, succeeding */
1378 o.ctx = (void *)~0UL;
1379 ret = mutex_trylock(&o.base);
1380 WARN_ON(!ret);
1381 if (ret)
1382 mutex_unlock(&o.base);
1383 else
1384 WARN_ON(1);
1385 WARN_ON(o.ctx != (void *)~0UL);
1387 /* trylock, failing */
1388 o.ctx = (void *)~0UL;
1389 mutex_lock(&o.base);
1390 ret = mutex_trylock(&o.base);
1391 WARN_ON(ret);
1392 mutex_unlock(&o.base);
1393 WARN_ON(o.ctx != (void *)~0UL);
1395 /* nest_lock */
1396 o.ctx = (void *)~0UL;
1397 mutex_lock_nest_lock(&o.base, &t);
1398 mutex_unlock(&o.base);
1399 WARN_ON(o.ctx != (void *)~0UL);
1402 static void ww_test_two_contexts(void)
1404 WWAI(&t);
1405 WWAI(&t2);
1408 static void ww_test_diff_class(void)
1410 WWAI(&t);
1411 #ifdef CONFIG_DEBUG_MUTEXES
1412 t.ww_class = NULL;
1413 #endif
1414 WWL(&o, &t);
1417 static void ww_test_context_done_twice(void)
1419 WWAI(&t);
1420 WWAD(&t);
1421 WWAD(&t);
1422 WWAF(&t);
1425 static void ww_test_context_unlock_twice(void)
1427 WWAI(&t);
1428 WWAD(&t);
1429 WWAF(&t);
1430 WWAF(&t);
1433 static void ww_test_context_fini_early(void)
1435 WWAI(&t);
1436 WWL(&o, &t);
1437 WWAD(&t);
1438 WWAF(&t);
1441 static void ww_test_context_lock_after_done(void)
1443 WWAI(&t);
1444 WWAD(&t);
1445 WWL(&o, &t);
1448 static void ww_test_object_unlock_twice(void)
1450 WWL1(&o);
1451 WWU(&o);
1452 WWU(&o);
1455 static void ww_test_object_lock_unbalanced(void)
1457 WWAI(&t);
1458 WWL(&o, &t);
1459 t.acquired = 0;
1460 WWU(&o);
1461 WWAF(&t);
1464 static void ww_test_object_lock_stale_context(void)
1466 WWAI(&t);
1467 o.ctx = &t2;
1468 WWL(&o, &t);
1471 static void ww_test_edeadlk_normal(void)
1473 int ret;
1475 mutex_lock(&o2.base);
1476 o2.ctx = &t2;
1477 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1479 WWAI(&t);
1480 t2 = t;
1481 t2.stamp--;
1483 ret = WWL(&o, &t);
1484 WARN_ON(ret);
1486 ret = WWL(&o2, &t);
1487 WARN_ON(ret != -EDEADLK);
1489 o2.ctx = NULL;
1490 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1491 mutex_unlock(&o2.base);
1492 WWU(&o);
1494 WWL(&o2, &t);
1497 static void ww_test_edeadlk_normal_slow(void)
1499 int ret;
1501 mutex_lock(&o2.base);
1502 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1503 o2.ctx = &t2;
1505 WWAI(&t);
1506 t2 = t;
1507 t2.stamp--;
1509 ret = WWL(&o, &t);
1510 WARN_ON(ret);
1512 ret = WWL(&o2, &t);
1513 WARN_ON(ret != -EDEADLK);
1515 o2.ctx = NULL;
1516 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1517 mutex_unlock(&o2.base);
1518 WWU(&o);
1520 ww_mutex_lock_slow(&o2, &t);
1523 static void ww_test_edeadlk_no_unlock(void)
1525 int ret;
1527 mutex_lock(&o2.base);
1528 o2.ctx = &t2;
1529 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1531 WWAI(&t);
1532 t2 = t;
1533 t2.stamp--;
1535 ret = WWL(&o, &t);
1536 WARN_ON(ret);
1538 ret = WWL(&o2, &t);
1539 WARN_ON(ret != -EDEADLK);
1541 o2.ctx = NULL;
1542 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1543 mutex_unlock(&o2.base);
1545 WWL(&o2, &t);
1548 static void ww_test_edeadlk_no_unlock_slow(void)
1550 int ret;
1552 mutex_lock(&o2.base);
1553 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1554 o2.ctx = &t2;
1556 WWAI(&t);
1557 t2 = t;
1558 t2.stamp--;
1560 ret = WWL(&o, &t);
1561 WARN_ON(ret);
1563 ret = WWL(&o2, &t);
1564 WARN_ON(ret != -EDEADLK);
1566 o2.ctx = NULL;
1567 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1568 mutex_unlock(&o2.base);
1570 ww_mutex_lock_slow(&o2, &t);
1573 static void ww_test_edeadlk_acquire_more(void)
1575 int ret;
1577 mutex_lock(&o2.base);
1578 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1579 o2.ctx = &t2;
1581 WWAI(&t);
1582 t2 = t;
1583 t2.stamp--;
1585 ret = WWL(&o, &t);
1586 WARN_ON(ret);
1588 ret = WWL(&o2, &t);
1589 WARN_ON(ret != -EDEADLK);
1591 ret = WWL(&o3, &t);
1594 static void ww_test_edeadlk_acquire_more_slow(void)
1596 int ret;
1598 mutex_lock(&o2.base);
1599 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1600 o2.ctx = &t2;
1602 WWAI(&t);
1603 t2 = t;
1604 t2.stamp--;
1606 ret = WWL(&o, &t);
1607 WARN_ON(ret);
1609 ret = WWL(&o2, &t);
1610 WARN_ON(ret != -EDEADLK);
1612 ww_mutex_lock_slow(&o3, &t);
1615 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1617 int ret;
1619 mutex_lock(&o2.base);
1620 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1621 o2.ctx = &t2;
1623 mutex_lock(&o3.base);
1624 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1625 o3.ctx = &t2;
1627 WWAI(&t);
1628 t2 = t;
1629 t2.stamp--;
1631 ret = WWL(&o, &t);
1632 WARN_ON(ret);
1634 ret = WWL(&o2, &t);
1635 WARN_ON(ret != -EDEADLK);
1637 ret = WWL(&o3, &t);
1638 WARN_ON(ret != -EDEADLK);
1641 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1643 int ret;
1645 mutex_lock(&o2.base);
1646 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1647 o2.ctx = &t2;
1649 mutex_lock(&o3.base);
1650 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1651 o3.ctx = &t2;
1653 WWAI(&t);
1654 t2 = t;
1655 t2.stamp--;
1657 ret = WWL(&o, &t);
1658 WARN_ON(ret);
1660 ret = WWL(&o2, &t);
1661 WARN_ON(ret != -EDEADLK);
1663 ww_mutex_lock_slow(&o3, &t);
1666 static void ww_test_edeadlk_acquire_wrong(void)
1668 int ret;
1670 mutex_lock(&o2.base);
1671 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1672 o2.ctx = &t2;
1674 WWAI(&t);
1675 t2 = t;
1676 t2.stamp--;
1678 ret = WWL(&o, &t);
1679 WARN_ON(ret);
1681 ret = WWL(&o2, &t);
1682 WARN_ON(ret != -EDEADLK);
1683 if (!ret)
1684 WWU(&o2);
1686 WWU(&o);
1688 ret = WWL(&o3, &t);
1691 static void ww_test_edeadlk_acquire_wrong_slow(void)
1693 int ret;
1695 mutex_lock(&o2.base);
1696 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1697 o2.ctx = &t2;
1699 WWAI(&t);
1700 t2 = t;
1701 t2.stamp--;
1703 ret = WWL(&o, &t);
1704 WARN_ON(ret);
1706 ret = WWL(&o2, &t);
1707 WARN_ON(ret != -EDEADLK);
1708 if (!ret)
1709 WWU(&o2);
1711 WWU(&o);
1713 ww_mutex_lock_slow(&o3, &t);
1716 static void ww_test_spin_nest_unlocked(void)
1718 raw_spin_lock_nest_lock(&lock_A, &o.base);
1719 U(A);
1722 static void ww_test_unneeded_slow(void)
1724 WWAI(&t);
1726 ww_mutex_lock_slow(&o, &t);
1729 static void ww_test_context_block(void)
1731 int ret;
1733 WWAI(&t);
1735 ret = WWL(&o, &t);
1736 WARN_ON(ret);
1737 WWL1(&o2);
1740 static void ww_test_context_try(void)
1742 int ret;
1744 WWAI(&t);
1746 ret = WWL(&o, &t);
1747 WARN_ON(ret);
1749 ret = WWT(&o2);
1750 WARN_ON(!ret);
1751 WWU(&o2);
1752 WWU(&o);
1755 static void ww_test_context_context(void)
1757 int ret;
1759 WWAI(&t);
1761 ret = WWL(&o, &t);
1762 WARN_ON(ret);
1764 ret = WWL(&o2, &t);
1765 WARN_ON(ret);
1767 WWU(&o2);
1768 WWU(&o);
1771 static void ww_test_try_block(void)
1773 bool ret;
1775 ret = WWT(&o);
1776 WARN_ON(!ret);
1778 WWL1(&o2);
1779 WWU(&o2);
1780 WWU(&o);
1783 static void ww_test_try_try(void)
1785 bool ret;
1787 ret = WWT(&o);
1788 WARN_ON(!ret);
1789 ret = WWT(&o2);
1790 WARN_ON(!ret);
1791 WWU(&o2);
1792 WWU(&o);
1795 static void ww_test_try_context(void)
1797 int ret;
1799 ret = WWT(&o);
1800 WARN_ON(!ret);
1802 WWAI(&t);
1804 ret = WWL(&o2, &t);
1805 WARN_ON(ret);
1808 static void ww_test_block_block(void)
1810 WWL1(&o);
1811 WWL1(&o2);
1814 static void ww_test_block_try(void)
1816 bool ret;
1818 WWL1(&o);
1819 ret = WWT(&o2);
1820 WARN_ON(!ret);
1823 static void ww_test_block_context(void)
1825 int ret;
1827 WWL1(&o);
1828 WWAI(&t);
1830 ret = WWL(&o2, &t);
1831 WARN_ON(ret);
1834 static void ww_test_spin_block(void)
1836 L(A);
1837 U(A);
1839 WWL1(&o);
1840 L(A);
1841 U(A);
1842 WWU(&o);
1844 L(A);
1845 WWL1(&o);
1846 WWU(&o);
1847 U(A);
1850 static void ww_test_spin_try(void)
1852 bool ret;
1854 L(A);
1855 U(A);
1857 ret = WWT(&o);
1858 WARN_ON(!ret);
1859 L(A);
1860 U(A);
1861 WWU(&o);
1863 L(A);
1864 ret = WWT(&o);
1865 WARN_ON(!ret);
1866 WWU(&o);
1867 U(A);
1870 static void ww_test_spin_context(void)
1872 int ret;
1874 L(A);
1875 U(A);
1877 WWAI(&t);
1879 ret = WWL(&o, &t);
1880 WARN_ON(ret);
1881 L(A);
1882 U(A);
1883 WWU(&o);
1885 L(A);
1886 ret = WWL(&o, &t);
1887 WARN_ON(ret);
1888 WWU(&o);
1889 U(A);
1892 static void ww_tests(void)
1894 printk(" --------------------------------------------------------------------------\n");
1895 printk(" | Wound/wait tests |\n");
1896 printk(" ---------------------\n");
1898 print_testname("ww api failures");
1899 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1900 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1901 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1902 pr_cont("\n");
1904 print_testname("ww contexts mixing");
1905 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1906 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1907 pr_cont("\n");
1909 print_testname("finishing ww context");
1910 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1911 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1912 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1913 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1914 pr_cont("\n");
1916 print_testname("locking mismatches");
1917 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1918 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1919 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1920 pr_cont("\n");
1922 print_testname("EDEADLK handling");
1923 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1924 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1925 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1926 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1927 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1928 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1929 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1930 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1931 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1932 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1933 pr_cont("\n");
1935 print_testname("spinlock nest unlocked");
1936 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1937 pr_cont("\n");
1939 printk(" -----------------------------------------------------\n");
1940 printk(" |block | try |context|\n");
1941 printk(" -----------------------------------------------------\n");
1943 print_testname("context");
1944 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1945 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1946 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1947 pr_cont("\n");
1949 print_testname("try");
1950 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1951 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1952 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1953 pr_cont("\n");
1955 print_testname("block");
1956 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1957 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1958 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1959 pr_cont("\n");
1961 print_testname("spinlock");
1962 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1963 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1964 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1965 pr_cont("\n");
1968 void locking_selftest(void)
1971 * Got a locking failure before the selftest ran?
1973 if (!debug_locks) {
1974 printk("----------------------------------\n");
1975 printk("| Locking API testsuite disabled |\n");
1976 printk("----------------------------------\n");
1977 return;
1981 * Run the testsuite:
1983 printk("------------------------\n");
1984 printk("| Locking API testsuite:\n");
1985 printk("----------------------------------------------------------------------------\n");
1986 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1987 printk(" --------------------------------------------------------------------------\n");
1989 init_shared_classes();
1990 debug_locks_silent = !debug_locks_verbose;
1992 DO_TESTCASE_6R("A-A deadlock", AA);
1993 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1994 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1995 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1996 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1997 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1998 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1999 DO_TESTCASE_6("double unlock", double_unlock);
2000 DO_TESTCASE_6("initialize held", init_held);
2002 printk(" --------------------------------------------------------------------------\n");
2003 print_testname("recursive read-lock");
2004 pr_cont(" |");
2005 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
2006 pr_cont(" |");
2007 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
2008 pr_cont("\n");
2010 print_testname("recursive read-lock #2");
2011 pr_cont(" |");
2012 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
2013 pr_cont(" |");
2014 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
2015 pr_cont("\n");
2017 print_testname("mixed read-write-lock");
2018 pr_cont(" |");
2019 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
2020 pr_cont(" |");
2021 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
2022 pr_cont("\n");
2024 print_testname("mixed write-read-lock");
2025 pr_cont(" |");
2026 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
2027 pr_cont(" |");
2028 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
2029 pr_cont("\n");
2031 print_testname("mixed read-lock/lock-write ABBA");
2032 pr_cont(" |");
2033 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2034 #ifdef CONFIG_PROVE_LOCKING
2036 * Lockdep does indeed fail here, but there's nothing we can do about
2037 * that now. Don't kill lockdep for it.
2039 unexpected_testcase_failures--;
2040 #endif
2042 pr_cont(" |");
2043 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2045 print_testname("mixed read-lock/lock-read ABBA");
2046 pr_cont(" |");
2047 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2048 pr_cont(" |");
2049 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2051 print_testname("mixed write-lock/lock-write ABBA");
2052 pr_cont(" |");
2053 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2054 pr_cont(" |");
2055 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2057 printk(" --------------------------------------------------------------------------\n");
2060 * irq-context testcases:
2062 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
2063 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
2064 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
2065 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
2066 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
2067 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
2069 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
2070 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
2072 ww_tests();
2074 if (unexpected_testcase_failures) {
2075 printk("-----------------------------------------------------------------\n");
2076 debug_locks = 0;
2077 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2078 unexpected_testcase_failures, testcase_total);
2079 printk("-----------------------------------------------------------------\n");
2080 } else if (expected_testcase_failures && testcase_successes) {
2081 printk("--------------------------------------------------------\n");
2082 printk("%3d out of %3d testcases failed, as expected. |\n",
2083 expected_testcase_failures, testcase_total);
2084 printk("----------------------------------------------------\n");
2085 debug_locks = 1;
2086 } else if (expected_testcase_failures && !testcase_successes) {
2087 printk("--------------------------------------------------------\n");
2088 printk("All %3d testcases failed, as expected. |\n",
2089 expected_testcase_failures);
2090 printk("----------------------------------------\n");
2091 debug_locks = 1;
2092 } else {
2093 printk("-------------------------------------------------------\n");
2094 printk("Good, all %3d testcases passed! |\n",
2095 testcase_successes);
2096 printk("---------------------------------\n");
2097 debug_locks = 1;
2099 debug_locks_silent = 0;