Linux 4.19.133
[linux/fpc-iii.git] / lib / locking-selftest.c
blob1e1bbf171eca4076a39383d51b75224dbb4099d0
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * lib/locking-selftest.c
5 * Testsuite for various locking APIs: spinlocks, rwlocks,
6 * mutexes and rw-semaphores.
8 * It is checking both false positives and false negatives.
10 * Started by Ingo Molnar:
12 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
14 #include <linux/rwsem.h>
15 #include <linux/mutex.h>
16 #include <linux/ww_mutex.h>
17 #include <linux/sched.h>
18 #include <linux/delay.h>
19 #include <linux/lockdep.h>
20 #include <linux/spinlock.h>
21 #include <linux/kallsyms.h>
22 #include <linux/interrupt.h>
23 #include <linux/debug_locks.h>
24 #include <linux/irqflags.h>
25 #include <linux/rtmutex.h>
28 * Change this to 1 if you want to see the failure printouts:
30 static unsigned int debug_locks_verbose;
32 static DEFINE_WD_CLASS(ww_lockdep);
34 static int __init setup_debug_locks_verbose(char *str)
36 get_option(&str, &debug_locks_verbose);
38 return 1;
41 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
43 #define FAILURE 0
44 #define SUCCESS 1
46 #define LOCKTYPE_SPIN 0x1
47 #define LOCKTYPE_RWLOCK 0x2
48 #define LOCKTYPE_MUTEX 0x4
49 #define LOCKTYPE_RWSEM 0x8
50 #define LOCKTYPE_WW 0x10
51 #define LOCKTYPE_RTMUTEX 0x20
53 static struct ww_acquire_ctx t, t2;
54 static struct ww_mutex o, o2, o3;
57 * Normal standalone locks, for the circular and irq-context
58 * dependency tests:
60 static DEFINE_RAW_SPINLOCK(lock_A);
61 static DEFINE_RAW_SPINLOCK(lock_B);
62 static DEFINE_RAW_SPINLOCK(lock_C);
63 static DEFINE_RAW_SPINLOCK(lock_D);
65 static DEFINE_RWLOCK(rwlock_A);
66 static DEFINE_RWLOCK(rwlock_B);
67 static DEFINE_RWLOCK(rwlock_C);
68 static DEFINE_RWLOCK(rwlock_D);
70 static DEFINE_MUTEX(mutex_A);
71 static DEFINE_MUTEX(mutex_B);
72 static DEFINE_MUTEX(mutex_C);
73 static DEFINE_MUTEX(mutex_D);
75 static DECLARE_RWSEM(rwsem_A);
76 static DECLARE_RWSEM(rwsem_B);
77 static DECLARE_RWSEM(rwsem_C);
78 static DECLARE_RWSEM(rwsem_D);
80 #ifdef CONFIG_RT_MUTEXES
82 static DEFINE_RT_MUTEX(rtmutex_A);
83 static DEFINE_RT_MUTEX(rtmutex_B);
84 static DEFINE_RT_MUTEX(rtmutex_C);
85 static DEFINE_RT_MUTEX(rtmutex_D);
87 #endif
90 * Locks that we initialize dynamically as well so that
91 * e.g. X1 and X2 becomes two instances of the same class,
92 * but X* and Y* are different classes. We do this so that
93 * we do not trigger a real lockup:
95 static DEFINE_RAW_SPINLOCK(lock_X1);
96 static DEFINE_RAW_SPINLOCK(lock_X2);
97 static DEFINE_RAW_SPINLOCK(lock_Y1);
98 static DEFINE_RAW_SPINLOCK(lock_Y2);
99 static DEFINE_RAW_SPINLOCK(lock_Z1);
100 static DEFINE_RAW_SPINLOCK(lock_Z2);
102 static DEFINE_RWLOCK(rwlock_X1);
103 static DEFINE_RWLOCK(rwlock_X2);
104 static DEFINE_RWLOCK(rwlock_Y1);
105 static DEFINE_RWLOCK(rwlock_Y2);
106 static DEFINE_RWLOCK(rwlock_Z1);
107 static DEFINE_RWLOCK(rwlock_Z2);
109 static DEFINE_MUTEX(mutex_X1);
110 static DEFINE_MUTEX(mutex_X2);
111 static DEFINE_MUTEX(mutex_Y1);
112 static DEFINE_MUTEX(mutex_Y2);
113 static DEFINE_MUTEX(mutex_Z1);
114 static DEFINE_MUTEX(mutex_Z2);
116 static DECLARE_RWSEM(rwsem_X1);
117 static DECLARE_RWSEM(rwsem_X2);
118 static DECLARE_RWSEM(rwsem_Y1);
119 static DECLARE_RWSEM(rwsem_Y2);
120 static DECLARE_RWSEM(rwsem_Z1);
121 static DECLARE_RWSEM(rwsem_Z2);
123 #ifdef CONFIG_RT_MUTEXES
125 static DEFINE_RT_MUTEX(rtmutex_X1);
126 static DEFINE_RT_MUTEX(rtmutex_X2);
127 static DEFINE_RT_MUTEX(rtmutex_Y1);
128 static DEFINE_RT_MUTEX(rtmutex_Y2);
129 static DEFINE_RT_MUTEX(rtmutex_Z1);
130 static DEFINE_RT_MUTEX(rtmutex_Z2);
132 #endif
135 * non-inlined runtime initializers, to let separate locks share
136 * the same lock-class:
138 #define INIT_CLASS_FUNC(class) \
139 static noinline void \
140 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
141 struct mutex *mutex, struct rw_semaphore *rwsem)\
143 raw_spin_lock_init(lock); \
144 rwlock_init(rwlock); \
145 mutex_init(mutex); \
146 init_rwsem(rwsem); \
149 INIT_CLASS_FUNC(X)
150 INIT_CLASS_FUNC(Y)
151 INIT_CLASS_FUNC(Z)
153 static void init_shared_classes(void)
155 #ifdef CONFIG_RT_MUTEXES
156 static struct lock_class_key rt_X, rt_Y, rt_Z;
158 __rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
159 __rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
160 __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
161 __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
162 __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
163 __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
164 #endif
166 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
167 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
169 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
170 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
172 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
173 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
177 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
178 * The following functions use a lock from a simulated hardirq/softirq
179 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
182 #define HARDIRQ_DISABLE local_irq_disable
183 #define HARDIRQ_ENABLE local_irq_enable
185 #define HARDIRQ_ENTER() \
186 local_irq_disable(); \
187 __irq_enter(); \
188 WARN_ON(!in_irq());
190 #define HARDIRQ_EXIT() \
191 __irq_exit(); \
192 local_irq_enable();
194 #define SOFTIRQ_DISABLE local_bh_disable
195 #define SOFTIRQ_ENABLE local_bh_enable
197 #define SOFTIRQ_ENTER() \
198 local_bh_disable(); \
199 local_irq_disable(); \
200 lockdep_softirq_enter(); \
201 WARN_ON(!in_softirq());
203 #define SOFTIRQ_EXIT() \
204 lockdep_softirq_exit(); \
205 local_irq_enable(); \
206 local_bh_enable();
209 * Shortcuts for lock/unlock API variants, to keep
210 * the testcases compact:
212 #define L(x) raw_spin_lock(&lock_##x)
213 #define U(x) raw_spin_unlock(&lock_##x)
214 #define LU(x) L(x); U(x)
215 #define SI(x) raw_spin_lock_init(&lock_##x)
217 #define WL(x) write_lock(&rwlock_##x)
218 #define WU(x) write_unlock(&rwlock_##x)
219 #define WLU(x) WL(x); WU(x)
221 #define RL(x) read_lock(&rwlock_##x)
222 #define RU(x) read_unlock(&rwlock_##x)
223 #define RLU(x) RL(x); RU(x)
224 #define RWI(x) rwlock_init(&rwlock_##x)
226 #define ML(x) mutex_lock(&mutex_##x)
227 #define MU(x) mutex_unlock(&mutex_##x)
228 #define MI(x) mutex_init(&mutex_##x)
230 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
231 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
232 #define RTI(x) rt_mutex_init(&rtmutex_##x)
234 #define WSL(x) down_write(&rwsem_##x)
235 #define WSU(x) up_write(&rwsem_##x)
237 #define RSL(x) down_read(&rwsem_##x)
238 #define RSU(x) up_read(&rwsem_##x)
239 #define RWSI(x) init_rwsem(&rwsem_##x)
241 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
242 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
243 #else
244 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
245 #endif
246 #define WWAD(x) ww_acquire_done(x)
247 #define WWAF(x) ww_acquire_fini(x)
249 #define WWL(x, c) ww_mutex_lock(x, c)
250 #define WWT(x) ww_mutex_trylock(x)
251 #define WWL1(x) ww_mutex_lock(x, NULL)
252 #define WWU(x) ww_mutex_unlock(x)
255 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
258 * Generate different permutations of the same testcase, using
259 * the same basic lock-dependency/state events:
262 #define GENERATE_TESTCASE(name) \
264 static void name(void) { E(); }
266 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
268 static void name##_12(void) { E1(); E2(); } \
269 static void name##_21(void) { E2(); E1(); }
271 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
273 static void name##_123(void) { E1(); E2(); E3(); } \
274 static void name##_132(void) { E1(); E3(); E2(); } \
275 static void name##_213(void) { E2(); E1(); E3(); } \
276 static void name##_231(void) { E2(); E3(); E1(); } \
277 static void name##_312(void) { E3(); E1(); E2(); } \
278 static void name##_321(void) { E3(); E2(); E1(); }
281 * AA deadlock:
284 #define E() \
286 LOCK(X1); \
287 LOCK(X2); /* this one should fail */
290 * 6 testcases:
292 #include "locking-selftest-spin.h"
293 GENERATE_TESTCASE(AA_spin)
294 #include "locking-selftest-wlock.h"
295 GENERATE_TESTCASE(AA_wlock)
296 #include "locking-selftest-rlock.h"
297 GENERATE_TESTCASE(AA_rlock)
298 #include "locking-selftest-mutex.h"
299 GENERATE_TESTCASE(AA_mutex)
300 #include "locking-selftest-wsem.h"
301 GENERATE_TESTCASE(AA_wsem)
302 #include "locking-selftest-rsem.h"
303 GENERATE_TESTCASE(AA_rsem)
305 #ifdef CONFIG_RT_MUTEXES
306 #include "locking-selftest-rtmutex.h"
307 GENERATE_TESTCASE(AA_rtmutex);
308 #endif
310 #undef E
313 * Special-case for read-locking, they are
314 * allowed to recurse on the same lock class:
316 static void rlock_AA1(void)
318 RL(X1);
319 RL(X1); // this one should NOT fail
322 static void rlock_AA1B(void)
324 RL(X1);
325 RL(X2); // this one should NOT fail
328 static void rsem_AA1(void)
330 RSL(X1);
331 RSL(X1); // this one should fail
334 static void rsem_AA1B(void)
336 RSL(X1);
337 RSL(X2); // this one should fail
340 * The mixing of read and write locks is not allowed:
342 static void rlock_AA2(void)
344 RL(X1);
345 WL(X2); // this one should fail
348 static void rsem_AA2(void)
350 RSL(X1);
351 WSL(X2); // this one should fail
354 static void rlock_AA3(void)
356 WL(X1);
357 RL(X2); // this one should fail
360 static void rsem_AA3(void)
362 WSL(X1);
363 RSL(X2); // this one should fail
367 * read_lock(A)
368 * spin_lock(B)
369 * spin_lock(B)
370 * write_lock(A)
372 static void rlock_ABBA1(void)
374 RL(X1);
375 L(Y1);
376 U(Y1);
377 RU(X1);
379 L(Y1);
380 WL(X1);
381 WU(X1);
382 U(Y1); // should fail
385 static void rwsem_ABBA1(void)
387 RSL(X1);
388 ML(Y1);
389 MU(Y1);
390 RSU(X1);
392 ML(Y1);
393 WSL(X1);
394 WSU(X1);
395 MU(Y1); // should fail
399 * read_lock(A)
400 * spin_lock(B)
401 * spin_lock(B)
402 * read_lock(A)
404 static void rlock_ABBA2(void)
406 RL(X1);
407 L(Y1);
408 U(Y1);
409 RU(X1);
411 L(Y1);
412 RL(X1);
413 RU(X1);
414 U(Y1); // should NOT fail
417 static void rwsem_ABBA2(void)
419 RSL(X1);
420 ML(Y1);
421 MU(Y1);
422 RSU(X1);
424 ML(Y1);
425 RSL(X1);
426 RSU(X1);
427 MU(Y1); // should fail
432 * write_lock(A)
433 * spin_lock(B)
434 * spin_lock(B)
435 * write_lock(A)
437 static void rlock_ABBA3(void)
439 WL(X1);
440 L(Y1);
441 U(Y1);
442 WU(X1);
444 L(Y1);
445 WL(X1);
446 WU(X1);
447 U(Y1); // should fail
450 static void rwsem_ABBA3(void)
452 WSL(X1);
453 ML(Y1);
454 MU(Y1);
455 WSU(X1);
457 ML(Y1);
458 WSL(X1);
459 WSU(X1);
460 MU(Y1); // should fail
464 * ABBA deadlock:
467 #define E() \
469 LOCK_UNLOCK_2(A, B); \
470 LOCK_UNLOCK_2(B, A); /* fail */
473 * 6 testcases:
475 #include "locking-selftest-spin.h"
476 GENERATE_TESTCASE(ABBA_spin)
477 #include "locking-selftest-wlock.h"
478 GENERATE_TESTCASE(ABBA_wlock)
479 #include "locking-selftest-rlock.h"
480 GENERATE_TESTCASE(ABBA_rlock)
481 #include "locking-selftest-mutex.h"
482 GENERATE_TESTCASE(ABBA_mutex)
483 #include "locking-selftest-wsem.h"
484 GENERATE_TESTCASE(ABBA_wsem)
485 #include "locking-selftest-rsem.h"
486 GENERATE_TESTCASE(ABBA_rsem)
488 #ifdef CONFIG_RT_MUTEXES
489 #include "locking-selftest-rtmutex.h"
490 GENERATE_TESTCASE(ABBA_rtmutex);
491 #endif
493 #undef E
496 * AB BC CA deadlock:
499 #define E() \
501 LOCK_UNLOCK_2(A, B); \
502 LOCK_UNLOCK_2(B, C); \
503 LOCK_UNLOCK_2(C, A); /* fail */
506 * 6 testcases:
508 #include "locking-selftest-spin.h"
509 GENERATE_TESTCASE(ABBCCA_spin)
510 #include "locking-selftest-wlock.h"
511 GENERATE_TESTCASE(ABBCCA_wlock)
512 #include "locking-selftest-rlock.h"
513 GENERATE_TESTCASE(ABBCCA_rlock)
514 #include "locking-selftest-mutex.h"
515 GENERATE_TESTCASE(ABBCCA_mutex)
516 #include "locking-selftest-wsem.h"
517 GENERATE_TESTCASE(ABBCCA_wsem)
518 #include "locking-selftest-rsem.h"
519 GENERATE_TESTCASE(ABBCCA_rsem)
521 #ifdef CONFIG_RT_MUTEXES
522 #include "locking-selftest-rtmutex.h"
523 GENERATE_TESTCASE(ABBCCA_rtmutex);
524 #endif
526 #undef E
529 * AB CA BC deadlock:
532 #define E() \
534 LOCK_UNLOCK_2(A, B); \
535 LOCK_UNLOCK_2(C, A); \
536 LOCK_UNLOCK_2(B, C); /* fail */
539 * 6 testcases:
541 #include "locking-selftest-spin.h"
542 GENERATE_TESTCASE(ABCABC_spin)
543 #include "locking-selftest-wlock.h"
544 GENERATE_TESTCASE(ABCABC_wlock)
545 #include "locking-selftest-rlock.h"
546 GENERATE_TESTCASE(ABCABC_rlock)
547 #include "locking-selftest-mutex.h"
548 GENERATE_TESTCASE(ABCABC_mutex)
549 #include "locking-selftest-wsem.h"
550 GENERATE_TESTCASE(ABCABC_wsem)
551 #include "locking-selftest-rsem.h"
552 GENERATE_TESTCASE(ABCABC_rsem)
554 #ifdef CONFIG_RT_MUTEXES
555 #include "locking-selftest-rtmutex.h"
556 GENERATE_TESTCASE(ABCABC_rtmutex);
557 #endif
559 #undef E
562 * AB BC CD DA deadlock:
565 #define E() \
567 LOCK_UNLOCK_2(A, B); \
568 LOCK_UNLOCK_2(B, C); \
569 LOCK_UNLOCK_2(C, D); \
570 LOCK_UNLOCK_2(D, A); /* fail */
573 * 6 testcases:
575 #include "locking-selftest-spin.h"
576 GENERATE_TESTCASE(ABBCCDDA_spin)
577 #include "locking-selftest-wlock.h"
578 GENERATE_TESTCASE(ABBCCDDA_wlock)
579 #include "locking-selftest-rlock.h"
580 GENERATE_TESTCASE(ABBCCDDA_rlock)
581 #include "locking-selftest-mutex.h"
582 GENERATE_TESTCASE(ABBCCDDA_mutex)
583 #include "locking-selftest-wsem.h"
584 GENERATE_TESTCASE(ABBCCDDA_wsem)
585 #include "locking-selftest-rsem.h"
586 GENERATE_TESTCASE(ABBCCDDA_rsem)
588 #ifdef CONFIG_RT_MUTEXES
589 #include "locking-selftest-rtmutex.h"
590 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
591 #endif
593 #undef E
596 * AB CD BD DA deadlock:
598 #define E() \
600 LOCK_UNLOCK_2(A, B); \
601 LOCK_UNLOCK_2(C, D); \
602 LOCK_UNLOCK_2(B, D); \
603 LOCK_UNLOCK_2(D, A); /* fail */
606 * 6 testcases:
608 #include "locking-selftest-spin.h"
609 GENERATE_TESTCASE(ABCDBDDA_spin)
610 #include "locking-selftest-wlock.h"
611 GENERATE_TESTCASE(ABCDBDDA_wlock)
612 #include "locking-selftest-rlock.h"
613 GENERATE_TESTCASE(ABCDBDDA_rlock)
614 #include "locking-selftest-mutex.h"
615 GENERATE_TESTCASE(ABCDBDDA_mutex)
616 #include "locking-selftest-wsem.h"
617 GENERATE_TESTCASE(ABCDBDDA_wsem)
618 #include "locking-selftest-rsem.h"
619 GENERATE_TESTCASE(ABCDBDDA_rsem)
621 #ifdef CONFIG_RT_MUTEXES
622 #include "locking-selftest-rtmutex.h"
623 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
624 #endif
626 #undef E
629 * AB CD BC DA deadlock:
631 #define E() \
633 LOCK_UNLOCK_2(A, B); \
634 LOCK_UNLOCK_2(C, D); \
635 LOCK_UNLOCK_2(B, C); \
636 LOCK_UNLOCK_2(D, A); /* fail */
639 * 6 testcases:
641 #include "locking-selftest-spin.h"
642 GENERATE_TESTCASE(ABCDBCDA_spin)
643 #include "locking-selftest-wlock.h"
644 GENERATE_TESTCASE(ABCDBCDA_wlock)
645 #include "locking-selftest-rlock.h"
646 GENERATE_TESTCASE(ABCDBCDA_rlock)
647 #include "locking-selftest-mutex.h"
648 GENERATE_TESTCASE(ABCDBCDA_mutex)
649 #include "locking-selftest-wsem.h"
650 GENERATE_TESTCASE(ABCDBCDA_wsem)
651 #include "locking-selftest-rsem.h"
652 GENERATE_TESTCASE(ABCDBCDA_rsem)
654 #ifdef CONFIG_RT_MUTEXES
655 #include "locking-selftest-rtmutex.h"
656 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
657 #endif
659 #undef E
662 * Double unlock:
664 #define E() \
666 LOCK(A); \
667 UNLOCK(A); \
668 UNLOCK(A); /* fail */
671 * 6 testcases:
673 #include "locking-selftest-spin.h"
674 GENERATE_TESTCASE(double_unlock_spin)
675 #include "locking-selftest-wlock.h"
676 GENERATE_TESTCASE(double_unlock_wlock)
677 #include "locking-selftest-rlock.h"
678 GENERATE_TESTCASE(double_unlock_rlock)
679 #include "locking-selftest-mutex.h"
680 GENERATE_TESTCASE(double_unlock_mutex)
681 #include "locking-selftest-wsem.h"
682 GENERATE_TESTCASE(double_unlock_wsem)
683 #include "locking-selftest-rsem.h"
684 GENERATE_TESTCASE(double_unlock_rsem)
686 #ifdef CONFIG_RT_MUTEXES
687 #include "locking-selftest-rtmutex.h"
688 GENERATE_TESTCASE(double_unlock_rtmutex);
689 #endif
691 #undef E
694 * initializing a held lock:
696 #define E() \
698 LOCK(A); \
699 INIT(A); /* fail */
702 * 6 testcases:
704 #include "locking-selftest-spin.h"
705 GENERATE_TESTCASE(init_held_spin)
706 #include "locking-selftest-wlock.h"
707 GENERATE_TESTCASE(init_held_wlock)
708 #include "locking-selftest-rlock.h"
709 GENERATE_TESTCASE(init_held_rlock)
710 #include "locking-selftest-mutex.h"
711 GENERATE_TESTCASE(init_held_mutex)
712 #include "locking-selftest-wsem.h"
713 GENERATE_TESTCASE(init_held_wsem)
714 #include "locking-selftest-rsem.h"
715 GENERATE_TESTCASE(init_held_rsem)
717 #ifdef CONFIG_RT_MUTEXES
718 #include "locking-selftest-rtmutex.h"
719 GENERATE_TESTCASE(init_held_rtmutex);
720 #endif
722 #undef E
725 * locking an irq-safe lock with irqs enabled:
727 #define E1() \
729 IRQ_ENTER(); \
730 LOCK(A); \
731 UNLOCK(A); \
732 IRQ_EXIT();
734 #define E2() \
736 LOCK(A); \
737 UNLOCK(A);
740 * Generate 24 testcases:
742 #include "locking-selftest-spin-hardirq.h"
743 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
745 #include "locking-selftest-rlock-hardirq.h"
746 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
748 #include "locking-selftest-wlock-hardirq.h"
749 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
751 #include "locking-selftest-spin-softirq.h"
752 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
754 #include "locking-selftest-rlock-softirq.h"
755 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
757 #include "locking-selftest-wlock-softirq.h"
758 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
760 #undef E1
761 #undef E2
764 * Enabling hardirqs with a softirq-safe lock held:
766 #define E1() \
768 SOFTIRQ_ENTER(); \
769 LOCK(A); \
770 UNLOCK(A); \
771 SOFTIRQ_EXIT();
773 #define E2() \
775 HARDIRQ_DISABLE(); \
776 LOCK(A); \
777 HARDIRQ_ENABLE(); \
778 UNLOCK(A);
781 * Generate 12 testcases:
783 #include "locking-selftest-spin.h"
784 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
786 #include "locking-selftest-wlock.h"
787 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
789 #include "locking-selftest-rlock.h"
790 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
792 #undef E1
793 #undef E2
796 * Enabling irqs with an irq-safe lock held:
798 #define E1() \
800 IRQ_ENTER(); \
801 LOCK(A); \
802 UNLOCK(A); \
803 IRQ_EXIT();
805 #define E2() \
807 IRQ_DISABLE(); \
808 LOCK(A); \
809 IRQ_ENABLE(); \
810 UNLOCK(A);
813 * Generate 24 testcases:
815 #include "locking-selftest-spin-hardirq.h"
816 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
818 #include "locking-selftest-rlock-hardirq.h"
819 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
821 #include "locking-selftest-wlock-hardirq.h"
822 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
824 #include "locking-selftest-spin-softirq.h"
825 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
827 #include "locking-selftest-rlock-softirq.h"
828 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
830 #include "locking-selftest-wlock-softirq.h"
831 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
833 #undef E1
834 #undef E2
837 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
839 #define E1() \
841 LOCK(A); \
842 LOCK(B); \
843 UNLOCK(B); \
844 UNLOCK(A); \
846 #define E2() \
848 LOCK(B); \
849 UNLOCK(B);
851 #define E3() \
853 IRQ_ENTER(); \
854 LOCK(A); \
855 UNLOCK(A); \
856 IRQ_EXIT();
859 * Generate 36 testcases:
861 #include "locking-selftest-spin-hardirq.h"
862 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
864 #include "locking-selftest-rlock-hardirq.h"
865 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
867 #include "locking-selftest-wlock-hardirq.h"
868 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
870 #include "locking-selftest-spin-softirq.h"
871 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
873 #include "locking-selftest-rlock-softirq.h"
874 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
876 #include "locking-selftest-wlock-softirq.h"
877 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
879 #undef E1
880 #undef E2
881 #undef E3
884 * If a lock turns into softirq-safe, but earlier it took
885 * a softirq-unsafe lock:
888 #define E1() \
889 IRQ_DISABLE(); \
890 LOCK(A); \
891 LOCK(B); \
892 UNLOCK(B); \
893 UNLOCK(A); \
894 IRQ_ENABLE();
896 #define E2() \
897 LOCK(B); \
898 UNLOCK(B);
900 #define E3() \
901 IRQ_ENTER(); \
902 LOCK(A); \
903 UNLOCK(A); \
904 IRQ_EXIT();
907 * Generate 36 testcases:
909 #include "locking-selftest-spin-hardirq.h"
910 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
912 #include "locking-selftest-rlock-hardirq.h"
913 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
915 #include "locking-selftest-wlock-hardirq.h"
916 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
918 #include "locking-selftest-spin-softirq.h"
919 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
921 #include "locking-selftest-rlock-softirq.h"
922 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
924 #include "locking-selftest-wlock-softirq.h"
925 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
927 #undef E1
928 #undef E2
929 #undef E3
932 * read-lock / write-lock irq inversion.
934 * Deadlock scenario:
936 * CPU#1 is at #1, i.e. it has write-locked A, but has not
937 * taken B yet.
939 * CPU#2 is at #2, i.e. it has locked B.
941 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
943 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
944 * will spin on A.
947 #define E1() \
949 IRQ_DISABLE(); \
950 WL(A); \
951 LOCK(B); \
952 UNLOCK(B); \
953 WU(A); \
954 IRQ_ENABLE();
956 #define E2() \
958 LOCK(B); \
959 UNLOCK(B);
961 #define E3() \
963 IRQ_ENTER(); \
964 RL(A); \
965 RU(A); \
966 IRQ_EXIT();
969 * Generate 36 testcases:
971 #include "locking-selftest-spin-hardirq.h"
972 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
974 #include "locking-selftest-rlock-hardirq.h"
975 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
977 #include "locking-selftest-wlock-hardirq.h"
978 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
980 #include "locking-selftest-spin-softirq.h"
981 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
983 #include "locking-selftest-rlock-softirq.h"
984 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
986 #include "locking-selftest-wlock-softirq.h"
987 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
989 #undef E1
990 #undef E2
991 #undef E3
994 * read-lock / write-lock recursion that is actually safe.
997 #define E1() \
999 IRQ_DISABLE(); \
1000 WL(A); \
1001 WU(A); \
1002 IRQ_ENABLE();
1004 #define E2() \
1006 RL(A); \
1007 RU(A); \
1009 #define E3() \
1011 IRQ_ENTER(); \
1012 RL(A); \
1013 L(B); \
1014 U(B); \
1015 RU(A); \
1016 IRQ_EXIT();
1019 * Generate 12 testcases:
1021 #include "locking-selftest-hardirq.h"
1022 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
1024 #include "locking-selftest-softirq.h"
1025 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
1027 #undef E1
1028 #undef E2
1029 #undef E3
1032 * read-lock / write-lock recursion that is unsafe.
1035 #define E1() \
1037 IRQ_DISABLE(); \
1038 L(B); \
1039 WL(A); \
1040 WU(A); \
1041 U(B); \
1042 IRQ_ENABLE();
1044 #define E2() \
1046 RL(A); \
1047 RU(A); \
1049 #define E3() \
1051 IRQ_ENTER(); \
1052 L(B); \
1053 U(B); \
1054 IRQ_EXIT();
1057 * Generate 12 testcases:
1059 #include "locking-selftest-hardirq.h"
1060 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
1062 #include "locking-selftest-softirq.h"
1063 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
1065 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1066 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
1067 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1068 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1069 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1070 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
1071 #ifdef CONFIG_RT_MUTEXES
1072 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1073 #endif
1074 #else
1075 # define I_SPINLOCK(x)
1076 # define I_RWLOCK(x)
1077 # define I_MUTEX(x)
1078 # define I_RWSEM(x)
1079 # define I_WW(x)
1080 #endif
1082 #ifndef I_RTMUTEX
1083 # define I_RTMUTEX(x)
1084 #endif
1086 #ifdef CONFIG_RT_MUTEXES
1087 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1088 #else
1089 #define I2_RTMUTEX(x)
1090 #endif
1092 #define I1(x) \
1093 do { \
1094 I_SPINLOCK(x); \
1095 I_RWLOCK(x); \
1096 I_MUTEX(x); \
1097 I_RWSEM(x); \
1098 I_RTMUTEX(x); \
1099 } while (0)
1101 #define I2(x) \
1102 do { \
1103 raw_spin_lock_init(&lock_##x); \
1104 rwlock_init(&rwlock_##x); \
1105 mutex_init(&mutex_##x); \
1106 init_rwsem(&rwsem_##x); \
1107 I2_RTMUTEX(x); \
1108 } while (0)
1110 static void reset_locks(void)
1112 local_irq_disable();
1113 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1114 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1116 I1(A); I1(B); I1(C); I1(D);
1117 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1118 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
1119 lockdep_reset();
1120 I2(A); I2(B); I2(C); I2(D);
1121 init_shared_classes();
1123 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1124 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1125 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1126 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
1127 local_irq_enable();
1130 #undef I
1132 static int testcase_total;
1133 static int testcase_successes;
1134 static int expected_testcase_failures;
1135 static int unexpected_testcase_failures;
1137 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1139 unsigned long saved_preempt_count = preempt_count();
1141 WARN_ON(irqs_disabled());
1143 testcase_fn();
1145 * Filter out expected failures:
1147 #ifndef CONFIG_PROVE_LOCKING
1148 if (expected == FAILURE && debug_locks) {
1149 expected_testcase_failures++;
1150 pr_cont("failed|");
1152 else
1153 #endif
1154 if (debug_locks != expected) {
1155 unexpected_testcase_failures++;
1156 pr_cont("FAILED|");
1157 } else {
1158 testcase_successes++;
1159 pr_cont(" ok |");
1161 testcase_total++;
1163 if (debug_locks_verbose)
1164 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1165 lockclass_mask, debug_locks, expected);
1167 * Some tests (e.g. double-unlock) might corrupt the preemption
1168 * count, so restore it:
1170 preempt_count_set(saved_preempt_count);
1171 #ifdef CONFIG_TRACE_IRQFLAGS
1172 if (softirq_count())
1173 current->softirqs_enabled = 0;
1174 else
1175 current->softirqs_enabled = 1;
1176 #endif
1178 reset_locks();
1181 #ifdef CONFIG_RT_MUTEXES
1182 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1183 #else
1184 #define dotest_rt(fn, e, m)
1185 #endif
1187 static inline void print_testname(const char *testname)
1189 printk("%33s:", testname);
1192 #define DO_TESTCASE_1(desc, name, nr) \
1193 print_testname(desc"/"#nr); \
1194 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1195 pr_cont("\n");
1197 #define DO_TESTCASE_1B(desc, name, nr) \
1198 print_testname(desc"/"#nr); \
1199 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1200 pr_cont("\n");
1202 #define DO_TESTCASE_3(desc, name, nr) \
1203 print_testname(desc"/"#nr); \
1204 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1205 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1206 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1207 pr_cont("\n");
1209 #define DO_TESTCASE_3RW(desc, name, nr) \
1210 print_testname(desc"/"#nr); \
1211 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1212 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1213 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1214 pr_cont("\n");
1216 #define DO_TESTCASE_6(desc, name) \
1217 print_testname(desc); \
1218 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1219 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1220 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1221 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1222 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1223 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1224 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1225 pr_cont("\n");
1227 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1228 print_testname(desc); \
1229 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1230 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1231 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1232 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1233 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1234 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1235 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1236 pr_cont("\n");
1239 * 'read' variant: rlocks must not trigger.
1241 #define DO_TESTCASE_6R(desc, name) \
1242 print_testname(desc); \
1243 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1244 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1245 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1246 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1247 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1248 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1249 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1250 pr_cont("\n");
1252 #define DO_TESTCASE_2I(desc, name, nr) \
1253 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1254 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1256 #define DO_TESTCASE_2IB(desc, name, nr) \
1257 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1258 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1260 #define DO_TESTCASE_6I(desc, name, nr) \
1261 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1262 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1264 #define DO_TESTCASE_6IRW(desc, name, nr) \
1265 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1266 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1268 #define DO_TESTCASE_2x3(desc, name) \
1269 DO_TESTCASE_3(desc, name, 12); \
1270 DO_TESTCASE_3(desc, name, 21);
1272 #define DO_TESTCASE_2x6(desc, name) \
1273 DO_TESTCASE_6I(desc, name, 12); \
1274 DO_TESTCASE_6I(desc, name, 21);
1276 #define DO_TESTCASE_6x2(desc, name) \
1277 DO_TESTCASE_2I(desc, name, 123); \
1278 DO_TESTCASE_2I(desc, name, 132); \
1279 DO_TESTCASE_2I(desc, name, 213); \
1280 DO_TESTCASE_2I(desc, name, 231); \
1281 DO_TESTCASE_2I(desc, name, 312); \
1282 DO_TESTCASE_2I(desc, name, 321);
1284 #define DO_TESTCASE_6x2B(desc, name) \
1285 DO_TESTCASE_2IB(desc, name, 123); \
1286 DO_TESTCASE_2IB(desc, name, 132); \
1287 DO_TESTCASE_2IB(desc, name, 213); \
1288 DO_TESTCASE_2IB(desc, name, 231); \
1289 DO_TESTCASE_2IB(desc, name, 312); \
1290 DO_TESTCASE_2IB(desc, name, 321);
1292 #define DO_TESTCASE_6x6(desc, name) \
1293 DO_TESTCASE_6I(desc, name, 123); \
1294 DO_TESTCASE_6I(desc, name, 132); \
1295 DO_TESTCASE_6I(desc, name, 213); \
1296 DO_TESTCASE_6I(desc, name, 231); \
1297 DO_TESTCASE_6I(desc, name, 312); \
1298 DO_TESTCASE_6I(desc, name, 321);
1300 #define DO_TESTCASE_6x6RW(desc, name) \
1301 DO_TESTCASE_6IRW(desc, name, 123); \
1302 DO_TESTCASE_6IRW(desc, name, 132); \
1303 DO_TESTCASE_6IRW(desc, name, 213); \
1304 DO_TESTCASE_6IRW(desc, name, 231); \
1305 DO_TESTCASE_6IRW(desc, name, 312); \
1306 DO_TESTCASE_6IRW(desc, name, 321);
1308 static void ww_test_fail_acquire(void)
1310 int ret;
1312 WWAI(&t);
1313 t.stamp++;
1315 ret = WWL(&o, &t);
1317 if (WARN_ON(!o.ctx) ||
1318 WARN_ON(ret))
1319 return;
1321 /* No lockdep test, pure API */
1322 ret = WWL(&o, &t);
1323 WARN_ON(ret != -EALREADY);
1325 ret = WWT(&o);
1326 WARN_ON(ret);
1328 t2 = t;
1329 t2.stamp++;
1330 ret = WWL(&o, &t2);
1331 WARN_ON(ret != -EDEADLK);
1332 WWU(&o);
1334 if (WWT(&o))
1335 WWU(&o);
1336 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1337 else
1338 DEBUG_LOCKS_WARN_ON(1);
1339 #endif
1342 static void ww_test_normal(void)
1344 int ret;
1346 WWAI(&t);
1349 * None of the ww_mutex codepaths should be taken in the 'normal'
1350 * mutex calls. The easiest way to verify this is by using the
1351 * normal mutex calls, and making sure o.ctx is unmodified.
1354 /* mutex_lock (and indirectly, mutex_lock_nested) */
1355 o.ctx = (void *)~0UL;
1356 mutex_lock(&o.base);
1357 mutex_unlock(&o.base);
1358 WARN_ON(o.ctx != (void *)~0UL);
1360 /* mutex_lock_interruptible (and *_nested) */
1361 o.ctx = (void *)~0UL;
1362 ret = mutex_lock_interruptible(&o.base);
1363 if (!ret)
1364 mutex_unlock(&o.base);
1365 else
1366 WARN_ON(1);
1367 WARN_ON(o.ctx != (void *)~0UL);
1369 /* mutex_lock_killable (and *_nested) */
1370 o.ctx = (void *)~0UL;
1371 ret = mutex_lock_killable(&o.base);
1372 if (!ret)
1373 mutex_unlock(&o.base);
1374 else
1375 WARN_ON(1);
1376 WARN_ON(o.ctx != (void *)~0UL);
1378 /* trylock, succeeding */
1379 o.ctx = (void *)~0UL;
1380 ret = mutex_trylock(&o.base);
1381 WARN_ON(!ret);
1382 if (ret)
1383 mutex_unlock(&o.base);
1384 else
1385 WARN_ON(1);
1386 WARN_ON(o.ctx != (void *)~0UL);
1388 /* trylock, failing */
1389 o.ctx = (void *)~0UL;
1390 mutex_lock(&o.base);
1391 ret = mutex_trylock(&o.base);
1392 WARN_ON(ret);
1393 mutex_unlock(&o.base);
1394 WARN_ON(o.ctx != (void *)~0UL);
1396 /* nest_lock */
1397 o.ctx = (void *)~0UL;
1398 mutex_lock_nest_lock(&o.base, &t);
1399 mutex_unlock(&o.base);
1400 WARN_ON(o.ctx != (void *)~0UL);
1403 static void ww_test_two_contexts(void)
1405 WWAI(&t);
1406 WWAI(&t2);
1409 static void ww_test_diff_class(void)
1411 WWAI(&t);
1412 #ifdef CONFIG_DEBUG_MUTEXES
1413 t.ww_class = NULL;
1414 #endif
1415 WWL(&o, &t);
1418 static void ww_test_context_done_twice(void)
1420 WWAI(&t);
1421 WWAD(&t);
1422 WWAD(&t);
1423 WWAF(&t);
1426 static void ww_test_context_unlock_twice(void)
1428 WWAI(&t);
1429 WWAD(&t);
1430 WWAF(&t);
1431 WWAF(&t);
1434 static void ww_test_context_fini_early(void)
1436 WWAI(&t);
1437 WWL(&o, &t);
1438 WWAD(&t);
1439 WWAF(&t);
1442 static void ww_test_context_lock_after_done(void)
1444 WWAI(&t);
1445 WWAD(&t);
1446 WWL(&o, &t);
1449 static void ww_test_object_unlock_twice(void)
1451 WWL1(&o);
1452 WWU(&o);
1453 WWU(&o);
1456 static void ww_test_object_lock_unbalanced(void)
1458 WWAI(&t);
1459 WWL(&o, &t);
1460 t.acquired = 0;
1461 WWU(&o);
1462 WWAF(&t);
1465 static void ww_test_object_lock_stale_context(void)
1467 WWAI(&t);
1468 o.ctx = &t2;
1469 WWL(&o, &t);
1472 static void ww_test_edeadlk_normal(void)
1474 int ret;
1476 mutex_lock(&o2.base);
1477 o2.ctx = &t2;
1478 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1480 WWAI(&t);
1481 t2 = t;
1482 t2.stamp--;
1484 ret = WWL(&o, &t);
1485 WARN_ON(ret);
1487 ret = WWL(&o2, &t);
1488 WARN_ON(ret != -EDEADLK);
1490 o2.ctx = NULL;
1491 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1492 mutex_unlock(&o2.base);
1493 WWU(&o);
1495 WWL(&o2, &t);
1498 static void ww_test_edeadlk_normal_slow(void)
1500 int ret;
1502 mutex_lock(&o2.base);
1503 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1504 o2.ctx = &t2;
1506 WWAI(&t);
1507 t2 = t;
1508 t2.stamp--;
1510 ret = WWL(&o, &t);
1511 WARN_ON(ret);
1513 ret = WWL(&o2, &t);
1514 WARN_ON(ret != -EDEADLK);
1516 o2.ctx = NULL;
1517 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1518 mutex_unlock(&o2.base);
1519 WWU(&o);
1521 ww_mutex_lock_slow(&o2, &t);
1524 static void ww_test_edeadlk_no_unlock(void)
1526 int ret;
1528 mutex_lock(&o2.base);
1529 o2.ctx = &t2;
1530 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1532 WWAI(&t);
1533 t2 = t;
1534 t2.stamp--;
1536 ret = WWL(&o, &t);
1537 WARN_ON(ret);
1539 ret = WWL(&o2, &t);
1540 WARN_ON(ret != -EDEADLK);
1542 o2.ctx = NULL;
1543 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1544 mutex_unlock(&o2.base);
1546 WWL(&o2, &t);
1549 static void ww_test_edeadlk_no_unlock_slow(void)
1551 int ret;
1553 mutex_lock(&o2.base);
1554 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1555 o2.ctx = &t2;
1557 WWAI(&t);
1558 t2 = t;
1559 t2.stamp--;
1561 ret = WWL(&o, &t);
1562 WARN_ON(ret);
1564 ret = WWL(&o2, &t);
1565 WARN_ON(ret != -EDEADLK);
1567 o2.ctx = NULL;
1568 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1569 mutex_unlock(&o2.base);
1571 ww_mutex_lock_slow(&o2, &t);
1574 static void ww_test_edeadlk_acquire_more(void)
1576 int ret;
1578 mutex_lock(&o2.base);
1579 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1580 o2.ctx = &t2;
1582 WWAI(&t);
1583 t2 = t;
1584 t2.stamp--;
1586 ret = WWL(&o, &t);
1587 WARN_ON(ret);
1589 ret = WWL(&o2, &t);
1590 WARN_ON(ret != -EDEADLK);
1592 ret = WWL(&o3, &t);
1595 static void ww_test_edeadlk_acquire_more_slow(void)
1597 int ret;
1599 mutex_lock(&o2.base);
1600 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1601 o2.ctx = &t2;
1603 WWAI(&t);
1604 t2 = t;
1605 t2.stamp--;
1607 ret = WWL(&o, &t);
1608 WARN_ON(ret);
1610 ret = WWL(&o2, &t);
1611 WARN_ON(ret != -EDEADLK);
1613 ww_mutex_lock_slow(&o3, &t);
1616 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1618 int ret;
1620 mutex_lock(&o2.base);
1621 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1622 o2.ctx = &t2;
1624 mutex_lock(&o3.base);
1625 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1626 o3.ctx = &t2;
1628 WWAI(&t);
1629 t2 = t;
1630 t2.stamp--;
1632 ret = WWL(&o, &t);
1633 WARN_ON(ret);
1635 ret = WWL(&o2, &t);
1636 WARN_ON(ret != -EDEADLK);
1638 ret = WWL(&o3, &t);
1639 WARN_ON(ret != -EDEADLK);
1642 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1644 int ret;
1646 mutex_lock(&o2.base);
1647 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1648 o2.ctx = &t2;
1650 mutex_lock(&o3.base);
1651 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1652 o3.ctx = &t2;
1654 WWAI(&t);
1655 t2 = t;
1656 t2.stamp--;
1658 ret = WWL(&o, &t);
1659 WARN_ON(ret);
1661 ret = WWL(&o2, &t);
1662 WARN_ON(ret != -EDEADLK);
1664 ww_mutex_lock_slow(&o3, &t);
1667 static void ww_test_edeadlk_acquire_wrong(void)
1669 int ret;
1671 mutex_lock(&o2.base);
1672 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1673 o2.ctx = &t2;
1675 WWAI(&t);
1676 t2 = t;
1677 t2.stamp--;
1679 ret = WWL(&o, &t);
1680 WARN_ON(ret);
1682 ret = WWL(&o2, &t);
1683 WARN_ON(ret != -EDEADLK);
1684 if (!ret)
1685 WWU(&o2);
1687 WWU(&o);
1689 ret = WWL(&o3, &t);
1692 static void ww_test_edeadlk_acquire_wrong_slow(void)
1694 int ret;
1696 mutex_lock(&o2.base);
1697 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1698 o2.ctx = &t2;
1700 WWAI(&t);
1701 t2 = t;
1702 t2.stamp--;
1704 ret = WWL(&o, &t);
1705 WARN_ON(ret);
1707 ret = WWL(&o2, &t);
1708 WARN_ON(ret != -EDEADLK);
1709 if (!ret)
1710 WWU(&o2);
1712 WWU(&o);
1714 ww_mutex_lock_slow(&o3, &t);
1717 static void ww_test_spin_nest_unlocked(void)
1719 raw_spin_lock_nest_lock(&lock_A, &o.base);
1720 U(A);
1723 static void ww_test_unneeded_slow(void)
1725 WWAI(&t);
1727 ww_mutex_lock_slow(&o, &t);
1730 static void ww_test_context_block(void)
1732 int ret;
1734 WWAI(&t);
1736 ret = WWL(&o, &t);
1737 WARN_ON(ret);
1738 WWL1(&o2);
1741 static void ww_test_context_try(void)
1743 int ret;
1745 WWAI(&t);
1747 ret = WWL(&o, &t);
1748 WARN_ON(ret);
1750 ret = WWT(&o2);
1751 WARN_ON(!ret);
1752 WWU(&o2);
1753 WWU(&o);
1756 static void ww_test_context_context(void)
1758 int ret;
1760 WWAI(&t);
1762 ret = WWL(&o, &t);
1763 WARN_ON(ret);
1765 ret = WWL(&o2, &t);
1766 WARN_ON(ret);
1768 WWU(&o2);
1769 WWU(&o);
1772 static void ww_test_try_block(void)
1774 bool ret;
1776 ret = WWT(&o);
1777 WARN_ON(!ret);
1779 WWL1(&o2);
1780 WWU(&o2);
1781 WWU(&o);
1784 static void ww_test_try_try(void)
1786 bool ret;
1788 ret = WWT(&o);
1789 WARN_ON(!ret);
1790 ret = WWT(&o2);
1791 WARN_ON(!ret);
1792 WWU(&o2);
1793 WWU(&o);
1796 static void ww_test_try_context(void)
1798 int ret;
1800 ret = WWT(&o);
1801 WARN_ON(!ret);
1803 WWAI(&t);
1805 ret = WWL(&o2, &t);
1806 WARN_ON(ret);
1809 static void ww_test_block_block(void)
1811 WWL1(&o);
1812 WWL1(&o2);
1815 static void ww_test_block_try(void)
1817 bool ret;
1819 WWL1(&o);
1820 ret = WWT(&o2);
1821 WARN_ON(!ret);
1824 static void ww_test_block_context(void)
1826 int ret;
1828 WWL1(&o);
1829 WWAI(&t);
1831 ret = WWL(&o2, &t);
1832 WARN_ON(ret);
1835 static void ww_test_spin_block(void)
1837 L(A);
1838 U(A);
1840 WWL1(&o);
1841 L(A);
1842 U(A);
1843 WWU(&o);
1845 L(A);
1846 WWL1(&o);
1847 WWU(&o);
1848 U(A);
1851 static void ww_test_spin_try(void)
1853 bool ret;
1855 L(A);
1856 U(A);
1858 ret = WWT(&o);
1859 WARN_ON(!ret);
1860 L(A);
1861 U(A);
1862 WWU(&o);
1864 L(A);
1865 ret = WWT(&o);
1866 WARN_ON(!ret);
1867 WWU(&o);
1868 U(A);
1871 static void ww_test_spin_context(void)
1873 int ret;
1875 L(A);
1876 U(A);
1878 WWAI(&t);
1880 ret = WWL(&o, &t);
1881 WARN_ON(ret);
1882 L(A);
1883 U(A);
1884 WWU(&o);
1886 L(A);
1887 ret = WWL(&o, &t);
1888 WARN_ON(ret);
1889 WWU(&o);
1890 U(A);
1893 static void ww_tests(void)
1895 printk(" --------------------------------------------------------------------------\n");
1896 printk(" | Wound/wait tests |\n");
1897 printk(" ---------------------\n");
1899 print_testname("ww api failures");
1900 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1901 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1902 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1903 pr_cont("\n");
1905 print_testname("ww contexts mixing");
1906 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1907 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1908 pr_cont("\n");
1910 print_testname("finishing ww context");
1911 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1912 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1913 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1914 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1915 pr_cont("\n");
1917 print_testname("locking mismatches");
1918 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1919 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1920 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1921 pr_cont("\n");
1923 print_testname("EDEADLK handling");
1924 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1925 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1926 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1927 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1928 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1929 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1930 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1931 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1932 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1933 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1934 pr_cont("\n");
1936 print_testname("spinlock nest unlocked");
1937 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1938 pr_cont("\n");
1940 printk(" -----------------------------------------------------\n");
1941 printk(" |block | try |context|\n");
1942 printk(" -----------------------------------------------------\n");
1944 print_testname("context");
1945 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1946 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1947 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1948 pr_cont("\n");
1950 print_testname("try");
1951 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1952 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1953 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1954 pr_cont("\n");
1956 print_testname("block");
1957 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1958 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1959 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1960 pr_cont("\n");
1962 print_testname("spinlock");
1963 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1964 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1965 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1966 pr_cont("\n");
1969 void locking_selftest(void)
1972 * Got a locking failure before the selftest ran?
1974 if (!debug_locks) {
1975 printk("----------------------------------\n");
1976 printk("| Locking API testsuite disabled |\n");
1977 printk("----------------------------------\n");
1978 return;
1982 * Run the testsuite:
1984 printk("------------------------\n");
1985 printk("| Locking API testsuite:\n");
1986 printk("----------------------------------------------------------------------------\n");
1987 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1988 printk(" --------------------------------------------------------------------------\n");
1990 init_shared_classes();
1991 debug_locks_silent = !debug_locks_verbose;
1993 DO_TESTCASE_6R("A-A deadlock", AA);
1994 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1995 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1996 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1997 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1998 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1999 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
2000 DO_TESTCASE_6("double unlock", double_unlock);
2001 DO_TESTCASE_6("initialize held", init_held);
2003 printk(" --------------------------------------------------------------------------\n");
2004 print_testname("recursive read-lock");
2005 pr_cont(" |");
2006 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
2007 pr_cont(" |");
2008 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
2009 pr_cont("\n");
2011 print_testname("recursive read-lock #2");
2012 pr_cont(" |");
2013 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
2014 pr_cont(" |");
2015 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
2016 pr_cont("\n");
2018 print_testname("mixed read-write-lock");
2019 pr_cont(" |");
2020 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
2021 pr_cont(" |");
2022 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
2023 pr_cont("\n");
2025 print_testname("mixed write-read-lock");
2026 pr_cont(" |");
2027 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
2028 pr_cont(" |");
2029 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
2030 pr_cont("\n");
2032 print_testname("mixed read-lock/lock-write ABBA");
2033 pr_cont(" |");
2034 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2035 #ifdef CONFIG_PROVE_LOCKING
2037 * Lockdep does indeed fail here, but there's nothing we can do about
2038 * that now. Don't kill lockdep for it.
2040 unexpected_testcase_failures--;
2041 #endif
2043 pr_cont(" |");
2044 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2046 print_testname("mixed read-lock/lock-read ABBA");
2047 pr_cont(" |");
2048 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2049 pr_cont(" |");
2050 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2052 print_testname("mixed write-lock/lock-write ABBA");
2053 pr_cont(" |");
2054 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2055 pr_cont(" |");
2056 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2058 printk(" --------------------------------------------------------------------------\n");
2061 * irq-context testcases:
2063 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
2064 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
2065 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
2066 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
2067 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
2068 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
2070 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
2071 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
2073 ww_tests();
2075 if (unexpected_testcase_failures) {
2076 printk("-----------------------------------------------------------------\n");
2077 debug_locks = 0;
2078 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2079 unexpected_testcase_failures, testcase_total);
2080 printk("-----------------------------------------------------------------\n");
2081 } else if (expected_testcase_failures && testcase_successes) {
2082 printk("--------------------------------------------------------\n");
2083 printk("%3d out of %3d testcases failed, as expected. |\n",
2084 expected_testcase_failures, testcase_total);
2085 printk("----------------------------------------------------\n");
2086 debug_locks = 1;
2087 } else if (expected_testcase_failures && !testcase_successes) {
2088 printk("--------------------------------------------------------\n");
2089 printk("All %3d testcases failed, as expected. |\n",
2090 expected_testcase_failures);
2091 printk("----------------------------------------\n");
2092 debug_locks = 1;
2093 } else {
2094 printk("-------------------------------------------------------\n");
2095 printk("Good, all %3d testcases passed! |\n",
2096 testcase_successes);
2097 printk("---------------------------------\n");
2098 debug_locks = 1;
2100 debug_locks_silent = 0;