s390/bpf: Fix skb_copy_bits() parameter passing
[linux/fpc-iii.git] / lib / locking-selftest.c
blob872a15a2a6374017af2bc0f8a446f86736f9b45e
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>
26 * Change this to 1 if you want to see the failure printouts:
28 static unsigned int debug_locks_verbose;
30 static DEFINE_WW_CLASS(ww_lockdep);
32 static int __init setup_debug_locks_verbose(char *str)
34 get_option(&str, &debug_locks_verbose);
36 return 1;
39 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
41 #define FAILURE 0
42 #define SUCCESS 1
44 #define LOCKTYPE_SPIN 0x1
45 #define LOCKTYPE_RWLOCK 0x2
46 #define LOCKTYPE_MUTEX 0x4
47 #define LOCKTYPE_RWSEM 0x8
48 #define LOCKTYPE_WW 0x10
50 static struct ww_acquire_ctx t, t2;
51 static struct ww_mutex o, o2, o3;
54 * Normal standalone locks, for the circular and irq-context
55 * dependency tests:
57 static DEFINE_RAW_SPINLOCK(lock_A);
58 static DEFINE_RAW_SPINLOCK(lock_B);
59 static DEFINE_RAW_SPINLOCK(lock_C);
60 static DEFINE_RAW_SPINLOCK(lock_D);
62 static DEFINE_RWLOCK(rwlock_A);
63 static DEFINE_RWLOCK(rwlock_B);
64 static DEFINE_RWLOCK(rwlock_C);
65 static DEFINE_RWLOCK(rwlock_D);
67 static DEFINE_MUTEX(mutex_A);
68 static DEFINE_MUTEX(mutex_B);
69 static DEFINE_MUTEX(mutex_C);
70 static DEFINE_MUTEX(mutex_D);
72 static DECLARE_RWSEM(rwsem_A);
73 static DECLARE_RWSEM(rwsem_B);
74 static DECLARE_RWSEM(rwsem_C);
75 static DECLARE_RWSEM(rwsem_D);
78 * Locks that we initialize dynamically as well so that
79 * e.g. X1 and X2 becomes two instances of the same class,
80 * but X* and Y* are different classes. We do this so that
81 * we do not trigger a real lockup:
83 static DEFINE_RAW_SPINLOCK(lock_X1);
84 static DEFINE_RAW_SPINLOCK(lock_X2);
85 static DEFINE_RAW_SPINLOCK(lock_Y1);
86 static DEFINE_RAW_SPINLOCK(lock_Y2);
87 static DEFINE_RAW_SPINLOCK(lock_Z1);
88 static DEFINE_RAW_SPINLOCK(lock_Z2);
90 static DEFINE_RWLOCK(rwlock_X1);
91 static DEFINE_RWLOCK(rwlock_X2);
92 static DEFINE_RWLOCK(rwlock_Y1);
93 static DEFINE_RWLOCK(rwlock_Y2);
94 static DEFINE_RWLOCK(rwlock_Z1);
95 static DEFINE_RWLOCK(rwlock_Z2);
97 static DEFINE_MUTEX(mutex_X1);
98 static DEFINE_MUTEX(mutex_X2);
99 static DEFINE_MUTEX(mutex_Y1);
100 static DEFINE_MUTEX(mutex_Y2);
101 static DEFINE_MUTEX(mutex_Z1);
102 static DEFINE_MUTEX(mutex_Z2);
104 static DECLARE_RWSEM(rwsem_X1);
105 static DECLARE_RWSEM(rwsem_X2);
106 static DECLARE_RWSEM(rwsem_Y1);
107 static DECLARE_RWSEM(rwsem_Y2);
108 static DECLARE_RWSEM(rwsem_Z1);
109 static DECLARE_RWSEM(rwsem_Z2);
112 * non-inlined runtime initializers, to let separate locks share
113 * the same lock-class:
115 #define INIT_CLASS_FUNC(class) \
116 static noinline void \
117 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
118 struct mutex *mutex, struct rw_semaphore *rwsem)\
120 raw_spin_lock_init(lock); \
121 rwlock_init(rwlock); \
122 mutex_init(mutex); \
123 init_rwsem(rwsem); \
126 INIT_CLASS_FUNC(X)
127 INIT_CLASS_FUNC(Y)
128 INIT_CLASS_FUNC(Z)
130 static void init_shared_classes(void)
132 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
133 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
135 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
136 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
138 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
139 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
143 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
144 * The following functions use a lock from a simulated hardirq/softirq
145 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
148 #define HARDIRQ_DISABLE local_irq_disable
149 #define HARDIRQ_ENABLE local_irq_enable
151 #define HARDIRQ_ENTER() \
152 local_irq_disable(); \
153 __irq_enter(); \
154 WARN_ON(!in_irq());
156 #define HARDIRQ_EXIT() \
157 __irq_exit(); \
158 local_irq_enable();
160 #define SOFTIRQ_DISABLE local_bh_disable
161 #define SOFTIRQ_ENABLE local_bh_enable
163 #define SOFTIRQ_ENTER() \
164 local_bh_disable(); \
165 local_irq_disable(); \
166 lockdep_softirq_enter(); \
167 WARN_ON(!in_softirq());
169 #define SOFTIRQ_EXIT() \
170 lockdep_softirq_exit(); \
171 local_irq_enable(); \
172 local_bh_enable();
175 * Shortcuts for lock/unlock API variants, to keep
176 * the testcases compact:
178 #define L(x) raw_spin_lock(&lock_##x)
179 #define U(x) raw_spin_unlock(&lock_##x)
180 #define LU(x) L(x); U(x)
181 #define SI(x) raw_spin_lock_init(&lock_##x)
183 #define WL(x) write_lock(&rwlock_##x)
184 #define WU(x) write_unlock(&rwlock_##x)
185 #define WLU(x) WL(x); WU(x)
187 #define RL(x) read_lock(&rwlock_##x)
188 #define RU(x) read_unlock(&rwlock_##x)
189 #define RLU(x) RL(x); RU(x)
190 #define RWI(x) rwlock_init(&rwlock_##x)
192 #define ML(x) mutex_lock(&mutex_##x)
193 #define MU(x) mutex_unlock(&mutex_##x)
194 #define MI(x) mutex_init(&mutex_##x)
196 #define WSL(x) down_write(&rwsem_##x)
197 #define WSU(x) up_write(&rwsem_##x)
199 #define RSL(x) down_read(&rwsem_##x)
200 #define RSU(x) up_read(&rwsem_##x)
201 #define RWSI(x) init_rwsem(&rwsem_##x)
203 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
204 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
205 #else
206 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
207 #endif
208 #define WWAD(x) ww_acquire_done(x)
209 #define WWAF(x) ww_acquire_fini(x)
211 #define WWL(x, c) ww_mutex_lock(x, c)
212 #define WWT(x) ww_mutex_trylock(x)
213 #define WWL1(x) ww_mutex_lock(x, NULL)
214 #define WWU(x) ww_mutex_unlock(x)
217 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
220 * Generate different permutations of the same testcase, using
221 * the same basic lock-dependency/state events:
224 #define GENERATE_TESTCASE(name) \
226 static void name(void) { E(); }
228 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
230 static void name##_12(void) { E1(); E2(); } \
231 static void name##_21(void) { E2(); E1(); }
233 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
235 static void name##_123(void) { E1(); E2(); E3(); } \
236 static void name##_132(void) { E1(); E3(); E2(); } \
237 static void name##_213(void) { E2(); E1(); E3(); } \
238 static void name##_231(void) { E2(); E3(); E1(); } \
239 static void name##_312(void) { E3(); E1(); E2(); } \
240 static void name##_321(void) { E3(); E2(); E1(); }
243 * AA deadlock:
246 #define E() \
248 LOCK(X1); \
249 LOCK(X2); /* this one should fail */
252 * 6 testcases:
254 #include "locking-selftest-spin.h"
255 GENERATE_TESTCASE(AA_spin)
256 #include "locking-selftest-wlock.h"
257 GENERATE_TESTCASE(AA_wlock)
258 #include "locking-selftest-rlock.h"
259 GENERATE_TESTCASE(AA_rlock)
260 #include "locking-selftest-mutex.h"
261 GENERATE_TESTCASE(AA_mutex)
262 #include "locking-selftest-wsem.h"
263 GENERATE_TESTCASE(AA_wsem)
264 #include "locking-selftest-rsem.h"
265 GENERATE_TESTCASE(AA_rsem)
267 #undef E
270 * Special-case for read-locking, they are
271 * allowed to recurse on the same lock class:
273 static void rlock_AA1(void)
275 RL(X1);
276 RL(X1); // this one should NOT fail
279 static void rlock_AA1B(void)
281 RL(X1);
282 RL(X2); // this one should NOT fail
285 static void rsem_AA1(void)
287 RSL(X1);
288 RSL(X1); // this one should fail
291 static void rsem_AA1B(void)
293 RSL(X1);
294 RSL(X2); // this one should fail
297 * The mixing of read and write locks is not allowed:
299 static void rlock_AA2(void)
301 RL(X1);
302 WL(X2); // this one should fail
305 static void rsem_AA2(void)
307 RSL(X1);
308 WSL(X2); // this one should fail
311 static void rlock_AA3(void)
313 WL(X1);
314 RL(X2); // this one should fail
317 static void rsem_AA3(void)
319 WSL(X1);
320 RSL(X2); // this one should fail
324 * ABBA deadlock:
327 #define E() \
329 LOCK_UNLOCK_2(A, B); \
330 LOCK_UNLOCK_2(B, A); /* fail */
333 * 6 testcases:
335 #include "locking-selftest-spin.h"
336 GENERATE_TESTCASE(ABBA_spin)
337 #include "locking-selftest-wlock.h"
338 GENERATE_TESTCASE(ABBA_wlock)
339 #include "locking-selftest-rlock.h"
340 GENERATE_TESTCASE(ABBA_rlock)
341 #include "locking-selftest-mutex.h"
342 GENERATE_TESTCASE(ABBA_mutex)
343 #include "locking-selftest-wsem.h"
344 GENERATE_TESTCASE(ABBA_wsem)
345 #include "locking-selftest-rsem.h"
346 GENERATE_TESTCASE(ABBA_rsem)
348 #undef E
351 * AB BC CA deadlock:
354 #define E() \
356 LOCK_UNLOCK_2(A, B); \
357 LOCK_UNLOCK_2(B, C); \
358 LOCK_UNLOCK_2(C, A); /* fail */
361 * 6 testcases:
363 #include "locking-selftest-spin.h"
364 GENERATE_TESTCASE(ABBCCA_spin)
365 #include "locking-selftest-wlock.h"
366 GENERATE_TESTCASE(ABBCCA_wlock)
367 #include "locking-selftest-rlock.h"
368 GENERATE_TESTCASE(ABBCCA_rlock)
369 #include "locking-selftest-mutex.h"
370 GENERATE_TESTCASE(ABBCCA_mutex)
371 #include "locking-selftest-wsem.h"
372 GENERATE_TESTCASE(ABBCCA_wsem)
373 #include "locking-selftest-rsem.h"
374 GENERATE_TESTCASE(ABBCCA_rsem)
376 #undef E
379 * AB CA BC deadlock:
382 #define E() \
384 LOCK_UNLOCK_2(A, B); \
385 LOCK_UNLOCK_2(C, A); \
386 LOCK_UNLOCK_2(B, C); /* fail */
389 * 6 testcases:
391 #include "locking-selftest-spin.h"
392 GENERATE_TESTCASE(ABCABC_spin)
393 #include "locking-selftest-wlock.h"
394 GENERATE_TESTCASE(ABCABC_wlock)
395 #include "locking-selftest-rlock.h"
396 GENERATE_TESTCASE(ABCABC_rlock)
397 #include "locking-selftest-mutex.h"
398 GENERATE_TESTCASE(ABCABC_mutex)
399 #include "locking-selftest-wsem.h"
400 GENERATE_TESTCASE(ABCABC_wsem)
401 #include "locking-selftest-rsem.h"
402 GENERATE_TESTCASE(ABCABC_rsem)
404 #undef E
407 * AB BC CD DA deadlock:
410 #define E() \
412 LOCK_UNLOCK_2(A, B); \
413 LOCK_UNLOCK_2(B, C); \
414 LOCK_UNLOCK_2(C, D); \
415 LOCK_UNLOCK_2(D, A); /* fail */
418 * 6 testcases:
420 #include "locking-selftest-spin.h"
421 GENERATE_TESTCASE(ABBCCDDA_spin)
422 #include "locking-selftest-wlock.h"
423 GENERATE_TESTCASE(ABBCCDDA_wlock)
424 #include "locking-selftest-rlock.h"
425 GENERATE_TESTCASE(ABBCCDDA_rlock)
426 #include "locking-selftest-mutex.h"
427 GENERATE_TESTCASE(ABBCCDDA_mutex)
428 #include "locking-selftest-wsem.h"
429 GENERATE_TESTCASE(ABBCCDDA_wsem)
430 #include "locking-selftest-rsem.h"
431 GENERATE_TESTCASE(ABBCCDDA_rsem)
433 #undef E
436 * AB CD BD DA deadlock:
438 #define E() \
440 LOCK_UNLOCK_2(A, B); \
441 LOCK_UNLOCK_2(C, D); \
442 LOCK_UNLOCK_2(B, D); \
443 LOCK_UNLOCK_2(D, A); /* fail */
446 * 6 testcases:
448 #include "locking-selftest-spin.h"
449 GENERATE_TESTCASE(ABCDBDDA_spin)
450 #include "locking-selftest-wlock.h"
451 GENERATE_TESTCASE(ABCDBDDA_wlock)
452 #include "locking-selftest-rlock.h"
453 GENERATE_TESTCASE(ABCDBDDA_rlock)
454 #include "locking-selftest-mutex.h"
455 GENERATE_TESTCASE(ABCDBDDA_mutex)
456 #include "locking-selftest-wsem.h"
457 GENERATE_TESTCASE(ABCDBDDA_wsem)
458 #include "locking-selftest-rsem.h"
459 GENERATE_TESTCASE(ABCDBDDA_rsem)
461 #undef E
464 * AB CD BC DA deadlock:
466 #define E() \
468 LOCK_UNLOCK_2(A, B); \
469 LOCK_UNLOCK_2(C, D); \
470 LOCK_UNLOCK_2(B, C); \
471 LOCK_UNLOCK_2(D, A); /* fail */
474 * 6 testcases:
476 #include "locking-selftest-spin.h"
477 GENERATE_TESTCASE(ABCDBCDA_spin)
478 #include "locking-selftest-wlock.h"
479 GENERATE_TESTCASE(ABCDBCDA_wlock)
480 #include "locking-selftest-rlock.h"
481 GENERATE_TESTCASE(ABCDBCDA_rlock)
482 #include "locking-selftest-mutex.h"
483 GENERATE_TESTCASE(ABCDBCDA_mutex)
484 #include "locking-selftest-wsem.h"
485 GENERATE_TESTCASE(ABCDBCDA_wsem)
486 #include "locking-selftest-rsem.h"
487 GENERATE_TESTCASE(ABCDBCDA_rsem)
489 #undef E
492 * Double unlock:
494 #define E() \
496 LOCK(A); \
497 UNLOCK(A); \
498 UNLOCK(A); /* fail */
501 * 6 testcases:
503 #include "locking-selftest-spin.h"
504 GENERATE_TESTCASE(double_unlock_spin)
505 #include "locking-selftest-wlock.h"
506 GENERATE_TESTCASE(double_unlock_wlock)
507 #include "locking-selftest-rlock.h"
508 GENERATE_TESTCASE(double_unlock_rlock)
509 #include "locking-selftest-mutex.h"
510 GENERATE_TESTCASE(double_unlock_mutex)
511 #include "locking-selftest-wsem.h"
512 GENERATE_TESTCASE(double_unlock_wsem)
513 #include "locking-selftest-rsem.h"
514 GENERATE_TESTCASE(double_unlock_rsem)
516 #undef E
519 * Bad unlock ordering:
521 #define E() \
523 LOCK(A); \
524 LOCK(B); \
525 UNLOCK(A); /* fail */ \
526 UNLOCK(B);
529 * 6 testcases:
531 #include "locking-selftest-spin.h"
532 GENERATE_TESTCASE(bad_unlock_order_spin)
533 #include "locking-selftest-wlock.h"
534 GENERATE_TESTCASE(bad_unlock_order_wlock)
535 #include "locking-selftest-rlock.h"
536 GENERATE_TESTCASE(bad_unlock_order_rlock)
537 #include "locking-selftest-mutex.h"
538 GENERATE_TESTCASE(bad_unlock_order_mutex)
539 #include "locking-selftest-wsem.h"
540 GENERATE_TESTCASE(bad_unlock_order_wsem)
541 #include "locking-selftest-rsem.h"
542 GENERATE_TESTCASE(bad_unlock_order_rsem)
544 #undef E
547 * initializing a held lock:
549 #define E() \
551 LOCK(A); \
552 INIT(A); /* fail */
555 * 6 testcases:
557 #include "locking-selftest-spin.h"
558 GENERATE_TESTCASE(init_held_spin)
559 #include "locking-selftest-wlock.h"
560 GENERATE_TESTCASE(init_held_wlock)
561 #include "locking-selftest-rlock.h"
562 GENERATE_TESTCASE(init_held_rlock)
563 #include "locking-selftest-mutex.h"
564 GENERATE_TESTCASE(init_held_mutex)
565 #include "locking-selftest-wsem.h"
566 GENERATE_TESTCASE(init_held_wsem)
567 #include "locking-selftest-rsem.h"
568 GENERATE_TESTCASE(init_held_rsem)
570 #undef E
573 * locking an irq-safe lock with irqs enabled:
575 #define E1() \
577 IRQ_ENTER(); \
578 LOCK(A); \
579 UNLOCK(A); \
580 IRQ_EXIT();
582 #define E2() \
584 LOCK(A); \
585 UNLOCK(A);
588 * Generate 24 testcases:
590 #include "locking-selftest-spin-hardirq.h"
591 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
593 #include "locking-selftest-rlock-hardirq.h"
594 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
596 #include "locking-selftest-wlock-hardirq.h"
597 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
599 #include "locking-selftest-spin-softirq.h"
600 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
602 #include "locking-selftest-rlock-softirq.h"
603 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
605 #include "locking-selftest-wlock-softirq.h"
606 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
608 #undef E1
609 #undef E2
612 * Enabling hardirqs with a softirq-safe lock held:
614 #define E1() \
616 SOFTIRQ_ENTER(); \
617 LOCK(A); \
618 UNLOCK(A); \
619 SOFTIRQ_EXIT();
621 #define E2() \
623 HARDIRQ_DISABLE(); \
624 LOCK(A); \
625 HARDIRQ_ENABLE(); \
626 UNLOCK(A);
629 * Generate 12 testcases:
631 #include "locking-selftest-spin.h"
632 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
634 #include "locking-selftest-wlock.h"
635 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
637 #include "locking-selftest-rlock.h"
638 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
640 #undef E1
641 #undef E2
644 * Enabling irqs with an irq-safe lock held:
646 #define E1() \
648 IRQ_ENTER(); \
649 LOCK(A); \
650 UNLOCK(A); \
651 IRQ_EXIT();
653 #define E2() \
655 IRQ_DISABLE(); \
656 LOCK(A); \
657 IRQ_ENABLE(); \
658 UNLOCK(A);
661 * Generate 24 testcases:
663 #include "locking-selftest-spin-hardirq.h"
664 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
666 #include "locking-selftest-rlock-hardirq.h"
667 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
669 #include "locking-selftest-wlock-hardirq.h"
670 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
672 #include "locking-selftest-spin-softirq.h"
673 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
675 #include "locking-selftest-rlock-softirq.h"
676 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
678 #include "locking-selftest-wlock-softirq.h"
679 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
681 #undef E1
682 #undef E2
685 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
687 #define E1() \
689 LOCK(A); \
690 LOCK(B); \
691 UNLOCK(B); \
692 UNLOCK(A); \
694 #define E2() \
696 LOCK(B); \
697 UNLOCK(B);
699 #define E3() \
701 IRQ_ENTER(); \
702 LOCK(A); \
703 UNLOCK(A); \
704 IRQ_EXIT();
707 * Generate 36 testcases:
709 #include "locking-selftest-spin-hardirq.h"
710 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
712 #include "locking-selftest-rlock-hardirq.h"
713 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
715 #include "locking-selftest-wlock-hardirq.h"
716 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
718 #include "locking-selftest-spin-softirq.h"
719 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
721 #include "locking-selftest-rlock-softirq.h"
722 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
724 #include "locking-selftest-wlock-softirq.h"
725 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
727 #undef E1
728 #undef E2
729 #undef E3
732 * If a lock turns into softirq-safe, but earlier it took
733 * a softirq-unsafe lock:
736 #define E1() \
737 IRQ_DISABLE(); \
738 LOCK(A); \
739 LOCK(B); \
740 UNLOCK(B); \
741 UNLOCK(A); \
742 IRQ_ENABLE();
744 #define E2() \
745 LOCK(B); \
746 UNLOCK(B);
748 #define E3() \
749 IRQ_ENTER(); \
750 LOCK(A); \
751 UNLOCK(A); \
752 IRQ_EXIT();
755 * Generate 36 testcases:
757 #include "locking-selftest-spin-hardirq.h"
758 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
760 #include "locking-selftest-rlock-hardirq.h"
761 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
763 #include "locking-selftest-wlock-hardirq.h"
764 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
766 #include "locking-selftest-spin-softirq.h"
767 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
769 #include "locking-selftest-rlock-softirq.h"
770 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
772 #include "locking-selftest-wlock-softirq.h"
773 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
775 #undef E1
776 #undef E2
777 #undef E3
780 * read-lock / write-lock irq inversion.
782 * Deadlock scenario:
784 * CPU#1 is at #1, i.e. it has write-locked A, but has not
785 * taken B yet.
787 * CPU#2 is at #2, i.e. it has locked B.
789 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
791 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
792 * will spin on A.
795 #define E1() \
797 IRQ_DISABLE(); \
798 WL(A); \
799 LOCK(B); \
800 UNLOCK(B); \
801 WU(A); \
802 IRQ_ENABLE();
804 #define E2() \
806 LOCK(B); \
807 UNLOCK(B);
809 #define E3() \
811 IRQ_ENTER(); \
812 RL(A); \
813 RU(A); \
814 IRQ_EXIT();
817 * Generate 36 testcases:
819 #include "locking-selftest-spin-hardirq.h"
820 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
822 #include "locking-selftest-rlock-hardirq.h"
823 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
825 #include "locking-selftest-wlock-hardirq.h"
826 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
828 #include "locking-selftest-spin-softirq.h"
829 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
831 #include "locking-selftest-rlock-softirq.h"
832 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
834 #include "locking-selftest-wlock-softirq.h"
835 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
837 #undef E1
838 #undef E2
839 #undef E3
842 * read-lock / write-lock recursion that is actually safe.
845 #define E1() \
847 IRQ_DISABLE(); \
848 WL(A); \
849 WU(A); \
850 IRQ_ENABLE();
852 #define E2() \
854 RL(A); \
855 RU(A); \
857 #define E3() \
859 IRQ_ENTER(); \
860 RL(A); \
861 L(B); \
862 U(B); \
863 RU(A); \
864 IRQ_EXIT();
867 * Generate 12 testcases:
869 #include "locking-selftest-hardirq.h"
870 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
872 #include "locking-selftest-softirq.h"
873 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
875 #undef E1
876 #undef E2
877 #undef E3
880 * read-lock / write-lock recursion that is unsafe.
883 #define E1() \
885 IRQ_DISABLE(); \
886 L(B); \
887 WL(A); \
888 WU(A); \
889 U(B); \
890 IRQ_ENABLE();
892 #define E2() \
894 RL(A); \
895 RU(A); \
897 #define E3() \
899 IRQ_ENTER(); \
900 L(B); \
901 U(B); \
902 IRQ_EXIT();
905 * Generate 12 testcases:
907 #include "locking-selftest-hardirq.h"
908 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
910 #include "locking-selftest-softirq.h"
911 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
913 #ifdef CONFIG_DEBUG_LOCK_ALLOC
914 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
915 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
916 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
917 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
918 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
919 #else
920 # define I_SPINLOCK(x)
921 # define I_RWLOCK(x)
922 # define I_MUTEX(x)
923 # define I_RWSEM(x)
924 # define I_WW(x)
925 #endif
927 #define I1(x) \
928 do { \
929 I_SPINLOCK(x); \
930 I_RWLOCK(x); \
931 I_MUTEX(x); \
932 I_RWSEM(x); \
933 } while (0)
935 #define I2(x) \
936 do { \
937 raw_spin_lock_init(&lock_##x); \
938 rwlock_init(&rwlock_##x); \
939 mutex_init(&mutex_##x); \
940 init_rwsem(&rwsem_##x); \
941 } while (0)
943 static void reset_locks(void)
945 local_irq_disable();
946 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
947 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
949 I1(A); I1(B); I1(C); I1(D);
950 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
951 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
952 lockdep_reset();
953 I2(A); I2(B); I2(C); I2(D);
954 init_shared_classes();
956 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
957 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
958 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
959 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
960 local_irq_enable();
963 #undef I
965 static int testcase_total;
966 static int testcase_successes;
967 static int expected_testcase_failures;
968 static int unexpected_testcase_failures;
970 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
972 unsigned long saved_preempt_count = preempt_count();
974 WARN_ON(irqs_disabled());
976 testcase_fn();
978 * Filter out expected failures:
980 #ifndef CONFIG_PROVE_LOCKING
981 if (expected == FAILURE && debug_locks) {
982 expected_testcase_failures++;
983 printk("failed|");
985 else
986 #endif
987 if (debug_locks != expected) {
988 unexpected_testcase_failures++;
989 printk("FAILED|");
991 dump_stack();
992 } else {
993 testcase_successes++;
994 printk(" ok |");
996 testcase_total++;
998 if (debug_locks_verbose)
999 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1000 lockclass_mask, debug_locks, expected);
1002 * Some tests (e.g. double-unlock) might corrupt the preemption
1003 * count, so restore it:
1005 preempt_count_set(saved_preempt_count);
1006 #ifdef CONFIG_TRACE_IRQFLAGS
1007 if (softirq_count())
1008 current->softirqs_enabled = 0;
1009 else
1010 current->softirqs_enabled = 1;
1011 #endif
1013 reset_locks();
1016 static inline void print_testname(const char *testname)
1018 printk("%33s:", testname);
1021 #define DO_TESTCASE_1(desc, name, nr) \
1022 print_testname(desc"/"#nr); \
1023 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1024 printk("\n");
1026 #define DO_TESTCASE_1B(desc, name, nr) \
1027 print_testname(desc"/"#nr); \
1028 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1029 printk("\n");
1031 #define DO_TESTCASE_3(desc, name, nr) \
1032 print_testname(desc"/"#nr); \
1033 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1034 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1035 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1036 printk("\n");
1038 #define DO_TESTCASE_3RW(desc, name, nr) \
1039 print_testname(desc"/"#nr); \
1040 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1041 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1042 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1043 printk("\n");
1045 #define DO_TESTCASE_6(desc, name) \
1046 print_testname(desc); \
1047 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1048 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1049 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1050 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1051 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1052 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1053 printk("\n");
1055 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1056 print_testname(desc); \
1057 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1058 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1059 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1060 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1061 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1062 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1063 printk("\n");
1066 * 'read' variant: rlocks must not trigger.
1068 #define DO_TESTCASE_6R(desc, name) \
1069 print_testname(desc); \
1070 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1071 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1072 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1073 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1074 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1075 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1076 printk("\n");
1078 #define DO_TESTCASE_2I(desc, name, nr) \
1079 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1080 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1082 #define DO_TESTCASE_2IB(desc, name, nr) \
1083 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1084 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1086 #define DO_TESTCASE_6I(desc, name, nr) \
1087 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1088 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1090 #define DO_TESTCASE_6IRW(desc, name, nr) \
1091 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1092 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1094 #define DO_TESTCASE_2x3(desc, name) \
1095 DO_TESTCASE_3(desc, name, 12); \
1096 DO_TESTCASE_3(desc, name, 21);
1098 #define DO_TESTCASE_2x6(desc, name) \
1099 DO_TESTCASE_6I(desc, name, 12); \
1100 DO_TESTCASE_6I(desc, name, 21);
1102 #define DO_TESTCASE_6x2(desc, name) \
1103 DO_TESTCASE_2I(desc, name, 123); \
1104 DO_TESTCASE_2I(desc, name, 132); \
1105 DO_TESTCASE_2I(desc, name, 213); \
1106 DO_TESTCASE_2I(desc, name, 231); \
1107 DO_TESTCASE_2I(desc, name, 312); \
1108 DO_TESTCASE_2I(desc, name, 321);
1110 #define DO_TESTCASE_6x2B(desc, name) \
1111 DO_TESTCASE_2IB(desc, name, 123); \
1112 DO_TESTCASE_2IB(desc, name, 132); \
1113 DO_TESTCASE_2IB(desc, name, 213); \
1114 DO_TESTCASE_2IB(desc, name, 231); \
1115 DO_TESTCASE_2IB(desc, name, 312); \
1116 DO_TESTCASE_2IB(desc, name, 321);
1118 #define DO_TESTCASE_6x6(desc, name) \
1119 DO_TESTCASE_6I(desc, name, 123); \
1120 DO_TESTCASE_6I(desc, name, 132); \
1121 DO_TESTCASE_6I(desc, name, 213); \
1122 DO_TESTCASE_6I(desc, name, 231); \
1123 DO_TESTCASE_6I(desc, name, 312); \
1124 DO_TESTCASE_6I(desc, name, 321);
1126 #define DO_TESTCASE_6x6RW(desc, name) \
1127 DO_TESTCASE_6IRW(desc, name, 123); \
1128 DO_TESTCASE_6IRW(desc, name, 132); \
1129 DO_TESTCASE_6IRW(desc, name, 213); \
1130 DO_TESTCASE_6IRW(desc, name, 231); \
1131 DO_TESTCASE_6IRW(desc, name, 312); \
1132 DO_TESTCASE_6IRW(desc, name, 321);
1134 static void ww_test_fail_acquire(void)
1136 int ret;
1138 WWAI(&t);
1139 t.stamp++;
1141 ret = WWL(&o, &t);
1143 if (WARN_ON(!o.ctx) ||
1144 WARN_ON(ret))
1145 return;
1147 /* No lockdep test, pure API */
1148 ret = WWL(&o, &t);
1149 WARN_ON(ret != -EALREADY);
1151 ret = WWT(&o);
1152 WARN_ON(ret);
1154 t2 = t;
1155 t2.stamp++;
1156 ret = WWL(&o, &t2);
1157 WARN_ON(ret != -EDEADLK);
1158 WWU(&o);
1160 if (WWT(&o))
1161 WWU(&o);
1162 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1163 else
1164 DEBUG_LOCKS_WARN_ON(1);
1165 #endif
1168 static void ww_test_normal(void)
1170 int ret;
1172 WWAI(&t);
1175 * None of the ww_mutex codepaths should be taken in the 'normal'
1176 * mutex calls. The easiest way to verify this is by using the
1177 * normal mutex calls, and making sure o.ctx is unmodified.
1180 /* mutex_lock (and indirectly, mutex_lock_nested) */
1181 o.ctx = (void *)~0UL;
1182 mutex_lock(&o.base);
1183 mutex_unlock(&o.base);
1184 WARN_ON(o.ctx != (void *)~0UL);
1186 /* mutex_lock_interruptible (and *_nested) */
1187 o.ctx = (void *)~0UL;
1188 ret = mutex_lock_interruptible(&o.base);
1189 if (!ret)
1190 mutex_unlock(&o.base);
1191 else
1192 WARN_ON(1);
1193 WARN_ON(o.ctx != (void *)~0UL);
1195 /* mutex_lock_killable (and *_nested) */
1196 o.ctx = (void *)~0UL;
1197 ret = mutex_lock_killable(&o.base);
1198 if (!ret)
1199 mutex_unlock(&o.base);
1200 else
1201 WARN_ON(1);
1202 WARN_ON(o.ctx != (void *)~0UL);
1204 /* trylock, succeeding */
1205 o.ctx = (void *)~0UL;
1206 ret = mutex_trylock(&o.base);
1207 WARN_ON(!ret);
1208 if (ret)
1209 mutex_unlock(&o.base);
1210 else
1211 WARN_ON(1);
1212 WARN_ON(o.ctx != (void *)~0UL);
1214 /* trylock, failing */
1215 o.ctx = (void *)~0UL;
1216 mutex_lock(&o.base);
1217 ret = mutex_trylock(&o.base);
1218 WARN_ON(ret);
1219 mutex_unlock(&o.base);
1220 WARN_ON(o.ctx != (void *)~0UL);
1222 /* nest_lock */
1223 o.ctx = (void *)~0UL;
1224 mutex_lock_nest_lock(&o.base, &t);
1225 mutex_unlock(&o.base);
1226 WARN_ON(o.ctx != (void *)~0UL);
1229 static void ww_test_two_contexts(void)
1231 WWAI(&t);
1232 WWAI(&t2);
1235 static void ww_test_diff_class(void)
1237 WWAI(&t);
1238 #ifdef CONFIG_DEBUG_MUTEXES
1239 t.ww_class = NULL;
1240 #endif
1241 WWL(&o, &t);
1244 static void ww_test_context_done_twice(void)
1246 WWAI(&t);
1247 WWAD(&t);
1248 WWAD(&t);
1249 WWAF(&t);
1252 static void ww_test_context_unlock_twice(void)
1254 WWAI(&t);
1255 WWAD(&t);
1256 WWAF(&t);
1257 WWAF(&t);
1260 static void ww_test_context_fini_early(void)
1262 WWAI(&t);
1263 WWL(&o, &t);
1264 WWAD(&t);
1265 WWAF(&t);
1268 static void ww_test_context_lock_after_done(void)
1270 WWAI(&t);
1271 WWAD(&t);
1272 WWL(&o, &t);
1275 static void ww_test_object_unlock_twice(void)
1277 WWL1(&o);
1278 WWU(&o);
1279 WWU(&o);
1282 static void ww_test_object_lock_unbalanced(void)
1284 WWAI(&t);
1285 WWL(&o, &t);
1286 t.acquired = 0;
1287 WWU(&o);
1288 WWAF(&t);
1291 static void ww_test_object_lock_stale_context(void)
1293 WWAI(&t);
1294 o.ctx = &t2;
1295 WWL(&o, &t);
1298 static void ww_test_edeadlk_normal(void)
1300 int ret;
1302 mutex_lock(&o2.base);
1303 o2.ctx = &t2;
1304 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1306 WWAI(&t);
1307 t2 = t;
1308 t2.stamp--;
1310 ret = WWL(&o, &t);
1311 WARN_ON(ret);
1313 ret = WWL(&o2, &t);
1314 WARN_ON(ret != -EDEADLK);
1316 o2.ctx = NULL;
1317 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1318 mutex_unlock(&o2.base);
1319 WWU(&o);
1321 WWL(&o2, &t);
1324 static void ww_test_edeadlk_normal_slow(void)
1326 int ret;
1328 mutex_lock(&o2.base);
1329 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1330 o2.ctx = &t2;
1332 WWAI(&t);
1333 t2 = t;
1334 t2.stamp--;
1336 ret = WWL(&o, &t);
1337 WARN_ON(ret);
1339 ret = WWL(&o2, &t);
1340 WARN_ON(ret != -EDEADLK);
1342 o2.ctx = NULL;
1343 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1344 mutex_unlock(&o2.base);
1345 WWU(&o);
1347 ww_mutex_lock_slow(&o2, &t);
1350 static void ww_test_edeadlk_no_unlock(void)
1352 int ret;
1354 mutex_lock(&o2.base);
1355 o2.ctx = &t2;
1356 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1358 WWAI(&t);
1359 t2 = t;
1360 t2.stamp--;
1362 ret = WWL(&o, &t);
1363 WARN_ON(ret);
1365 ret = WWL(&o2, &t);
1366 WARN_ON(ret != -EDEADLK);
1368 o2.ctx = NULL;
1369 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1370 mutex_unlock(&o2.base);
1372 WWL(&o2, &t);
1375 static void ww_test_edeadlk_no_unlock_slow(void)
1377 int ret;
1379 mutex_lock(&o2.base);
1380 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1381 o2.ctx = &t2;
1383 WWAI(&t);
1384 t2 = t;
1385 t2.stamp--;
1387 ret = WWL(&o, &t);
1388 WARN_ON(ret);
1390 ret = WWL(&o2, &t);
1391 WARN_ON(ret != -EDEADLK);
1393 o2.ctx = NULL;
1394 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1395 mutex_unlock(&o2.base);
1397 ww_mutex_lock_slow(&o2, &t);
1400 static void ww_test_edeadlk_acquire_more(void)
1402 int ret;
1404 mutex_lock(&o2.base);
1405 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1406 o2.ctx = &t2;
1408 WWAI(&t);
1409 t2 = t;
1410 t2.stamp--;
1412 ret = WWL(&o, &t);
1413 WARN_ON(ret);
1415 ret = WWL(&o2, &t);
1416 WARN_ON(ret != -EDEADLK);
1418 ret = WWL(&o3, &t);
1421 static void ww_test_edeadlk_acquire_more_slow(void)
1423 int ret;
1425 mutex_lock(&o2.base);
1426 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1427 o2.ctx = &t2;
1429 WWAI(&t);
1430 t2 = t;
1431 t2.stamp--;
1433 ret = WWL(&o, &t);
1434 WARN_ON(ret);
1436 ret = WWL(&o2, &t);
1437 WARN_ON(ret != -EDEADLK);
1439 ww_mutex_lock_slow(&o3, &t);
1442 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1444 int ret;
1446 mutex_lock(&o2.base);
1447 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1448 o2.ctx = &t2;
1450 mutex_lock(&o3.base);
1451 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1452 o3.ctx = &t2;
1454 WWAI(&t);
1455 t2 = t;
1456 t2.stamp--;
1458 ret = WWL(&o, &t);
1459 WARN_ON(ret);
1461 ret = WWL(&o2, &t);
1462 WARN_ON(ret != -EDEADLK);
1464 ret = WWL(&o3, &t);
1465 WARN_ON(ret != -EDEADLK);
1468 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1470 int ret;
1472 mutex_lock(&o2.base);
1473 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1474 o2.ctx = &t2;
1476 mutex_lock(&o3.base);
1477 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1478 o3.ctx = &t2;
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 ww_mutex_lock_slow(&o3, &t);
1493 static void ww_test_edeadlk_acquire_wrong(void)
1495 int ret;
1497 mutex_lock(&o2.base);
1498 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1499 o2.ctx = &t2;
1501 WWAI(&t);
1502 t2 = t;
1503 t2.stamp--;
1505 ret = WWL(&o, &t);
1506 WARN_ON(ret);
1508 ret = WWL(&o2, &t);
1509 WARN_ON(ret != -EDEADLK);
1510 if (!ret)
1511 WWU(&o2);
1513 WWU(&o);
1515 ret = WWL(&o3, &t);
1518 static void ww_test_edeadlk_acquire_wrong_slow(void)
1520 int ret;
1522 mutex_lock(&o2.base);
1523 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1524 o2.ctx = &t2;
1526 WWAI(&t);
1527 t2 = t;
1528 t2.stamp--;
1530 ret = WWL(&o, &t);
1531 WARN_ON(ret);
1533 ret = WWL(&o2, &t);
1534 WARN_ON(ret != -EDEADLK);
1535 if (!ret)
1536 WWU(&o2);
1538 WWU(&o);
1540 ww_mutex_lock_slow(&o3, &t);
1543 static void ww_test_spin_nest_unlocked(void)
1545 raw_spin_lock_nest_lock(&lock_A, &o.base);
1546 U(A);
1549 static void ww_test_unneeded_slow(void)
1551 WWAI(&t);
1553 ww_mutex_lock_slow(&o, &t);
1556 static void ww_test_context_block(void)
1558 int ret;
1560 WWAI(&t);
1562 ret = WWL(&o, &t);
1563 WARN_ON(ret);
1564 WWL1(&o2);
1567 static void ww_test_context_try(void)
1569 int ret;
1571 WWAI(&t);
1573 ret = WWL(&o, &t);
1574 WARN_ON(ret);
1576 ret = WWT(&o2);
1577 WARN_ON(!ret);
1578 WWU(&o2);
1579 WWU(&o);
1582 static void ww_test_context_context(void)
1584 int ret;
1586 WWAI(&t);
1588 ret = WWL(&o, &t);
1589 WARN_ON(ret);
1591 ret = WWL(&o2, &t);
1592 WARN_ON(ret);
1594 WWU(&o2);
1595 WWU(&o);
1598 static void ww_test_try_block(void)
1600 bool ret;
1602 ret = WWT(&o);
1603 WARN_ON(!ret);
1605 WWL1(&o2);
1606 WWU(&o2);
1607 WWU(&o);
1610 static void ww_test_try_try(void)
1612 bool ret;
1614 ret = WWT(&o);
1615 WARN_ON(!ret);
1616 ret = WWT(&o2);
1617 WARN_ON(!ret);
1618 WWU(&o2);
1619 WWU(&o);
1622 static void ww_test_try_context(void)
1624 int ret;
1626 ret = WWT(&o);
1627 WARN_ON(!ret);
1629 WWAI(&t);
1631 ret = WWL(&o2, &t);
1632 WARN_ON(ret);
1635 static void ww_test_block_block(void)
1637 WWL1(&o);
1638 WWL1(&o2);
1641 static void ww_test_block_try(void)
1643 bool ret;
1645 WWL1(&o);
1646 ret = WWT(&o2);
1647 WARN_ON(!ret);
1650 static void ww_test_block_context(void)
1652 int ret;
1654 WWL1(&o);
1655 WWAI(&t);
1657 ret = WWL(&o2, &t);
1658 WARN_ON(ret);
1661 static void ww_test_spin_block(void)
1663 L(A);
1664 U(A);
1666 WWL1(&o);
1667 L(A);
1668 U(A);
1669 WWU(&o);
1671 L(A);
1672 WWL1(&o);
1673 WWU(&o);
1674 U(A);
1677 static void ww_test_spin_try(void)
1679 bool ret;
1681 L(A);
1682 U(A);
1684 ret = WWT(&o);
1685 WARN_ON(!ret);
1686 L(A);
1687 U(A);
1688 WWU(&o);
1690 L(A);
1691 ret = WWT(&o);
1692 WARN_ON(!ret);
1693 WWU(&o);
1694 U(A);
1697 static void ww_test_spin_context(void)
1699 int ret;
1701 L(A);
1702 U(A);
1704 WWAI(&t);
1706 ret = WWL(&o, &t);
1707 WARN_ON(ret);
1708 L(A);
1709 U(A);
1710 WWU(&o);
1712 L(A);
1713 ret = WWL(&o, &t);
1714 WARN_ON(ret);
1715 WWU(&o);
1716 U(A);
1719 static void ww_tests(void)
1721 printk(" --------------------------------------------------------------------------\n");
1722 printk(" | Wound/wait tests |\n");
1723 printk(" ---------------------\n");
1725 print_testname("ww api failures");
1726 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1727 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1728 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1729 printk("\n");
1731 print_testname("ww contexts mixing");
1732 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1733 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1734 printk("\n");
1736 print_testname("finishing ww context");
1737 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1738 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1739 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1740 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1741 printk("\n");
1743 print_testname("locking mismatches");
1744 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1745 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1746 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1747 printk("\n");
1749 print_testname("EDEADLK handling");
1750 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1751 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1752 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1753 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1754 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1755 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1756 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1757 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1758 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1759 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1760 printk("\n");
1762 print_testname("spinlock nest unlocked");
1763 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1764 printk("\n");
1766 printk(" -----------------------------------------------------\n");
1767 printk(" |block | try |context|\n");
1768 printk(" -----------------------------------------------------\n");
1770 print_testname("context");
1771 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1772 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1773 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1774 printk("\n");
1776 print_testname("try");
1777 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1778 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1779 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1780 printk("\n");
1782 print_testname("block");
1783 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1784 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1785 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1786 printk("\n");
1788 print_testname("spinlock");
1789 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1790 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1791 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1792 printk("\n");
1795 void locking_selftest(void)
1798 * Got a locking failure before the selftest ran?
1800 if (!debug_locks) {
1801 printk("----------------------------------\n");
1802 printk("| Locking API testsuite disabled |\n");
1803 printk("----------------------------------\n");
1804 return;
1808 * Run the testsuite:
1810 printk("------------------------\n");
1811 printk("| Locking API testsuite:\n");
1812 printk("----------------------------------------------------------------------------\n");
1813 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1814 printk(" --------------------------------------------------------------------------\n");
1816 init_shared_classes();
1817 debug_locks_silent = !debug_locks_verbose;
1819 DO_TESTCASE_6R("A-A deadlock", AA);
1820 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1821 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1822 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1823 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1824 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1825 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1826 DO_TESTCASE_6("double unlock", double_unlock);
1827 DO_TESTCASE_6("initialize held", init_held);
1828 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1830 printk(" --------------------------------------------------------------------------\n");
1831 print_testname("recursive read-lock");
1832 printk(" |");
1833 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1834 printk(" |");
1835 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1836 printk("\n");
1838 print_testname("recursive read-lock #2");
1839 printk(" |");
1840 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
1841 printk(" |");
1842 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1843 printk("\n");
1845 print_testname("mixed read-write-lock");
1846 printk(" |");
1847 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1848 printk(" |");
1849 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1850 printk("\n");
1852 print_testname("mixed write-read-lock");
1853 printk(" |");
1854 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1855 printk(" |");
1856 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1857 printk("\n");
1859 printk(" --------------------------------------------------------------------------\n");
1862 * irq-context testcases:
1864 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1865 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1866 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1867 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1868 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1869 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1871 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1872 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1874 ww_tests();
1876 if (unexpected_testcase_failures) {
1877 printk("-----------------------------------------------------------------\n");
1878 debug_locks = 0;
1879 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1880 unexpected_testcase_failures, testcase_total);
1881 printk("-----------------------------------------------------------------\n");
1882 } else if (expected_testcase_failures && testcase_successes) {
1883 printk("--------------------------------------------------------\n");
1884 printk("%3d out of %3d testcases failed, as expected. |\n",
1885 expected_testcase_failures, testcase_total);
1886 printk("----------------------------------------------------\n");
1887 debug_locks = 1;
1888 } else if (expected_testcase_failures && !testcase_successes) {
1889 printk("--------------------------------------------------------\n");
1890 printk("All %3d testcases failed, as expected. |\n",
1891 expected_testcase_failures);
1892 printk("----------------------------------------\n");
1893 debug_locks = 1;
1894 } else {
1895 printk("-------------------------------------------------------\n");
1896 printk("Good, all %3d testcases passed! |\n",
1897 testcase_successes);
1898 printk("---------------------------------\n");
1899 debug_locks = 1;
1901 debug_locks_silent = 0;