writeback: rework wb_[dec|inc]_stat family of functions
[linux/fpc-iii.git] / lib / locking-selftest.c
blob6f2b135dc5e81996dc2582f56d59b5d1769f5377
1 /*
2 * lib/locking-selftest.c
4 * Testsuite for various locking APIs: spinlocks, rwlocks,
5 * mutexes and rw-semaphores.
7 * It is checking both false positives and false negatives.
9 * Started by Ingo Molnar:
11 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
13 #include <linux/rwsem.h>
14 #include <linux/mutex.h>
15 #include <linux/ww_mutex.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/lockdep.h>
19 #include <linux/spinlock.h>
20 #include <linux/kallsyms.h>
21 #include <linux/interrupt.h>
22 #include <linux/debug_locks.h>
23 #include <linux/irqflags.h>
24 #include <linux/rtmutex.h>
27 * Change this to 1 if you want to see the failure printouts:
29 static unsigned int debug_locks_verbose;
31 static DEFINE_WW_CLASS(ww_lockdep);
33 static int __init setup_debug_locks_verbose(char *str)
35 get_option(&str, &debug_locks_verbose);
37 return 1;
40 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
42 #define FAILURE 0
43 #define SUCCESS 1
45 #define LOCKTYPE_SPIN 0x1
46 #define LOCKTYPE_RWLOCK 0x2
47 #define LOCKTYPE_MUTEX 0x4
48 #define LOCKTYPE_RWSEM 0x8
49 #define LOCKTYPE_WW 0x10
50 #define LOCKTYPE_RTMUTEX 0x20
52 static struct ww_acquire_ctx t, t2;
53 static struct ww_mutex o, o2, o3;
56 * Normal standalone locks, for the circular and irq-context
57 * dependency tests:
59 static DEFINE_RAW_SPINLOCK(lock_A);
60 static DEFINE_RAW_SPINLOCK(lock_B);
61 static DEFINE_RAW_SPINLOCK(lock_C);
62 static DEFINE_RAW_SPINLOCK(lock_D);
64 static DEFINE_RWLOCK(rwlock_A);
65 static DEFINE_RWLOCK(rwlock_B);
66 static DEFINE_RWLOCK(rwlock_C);
67 static DEFINE_RWLOCK(rwlock_D);
69 static DEFINE_MUTEX(mutex_A);
70 static DEFINE_MUTEX(mutex_B);
71 static DEFINE_MUTEX(mutex_C);
72 static DEFINE_MUTEX(mutex_D);
74 static DECLARE_RWSEM(rwsem_A);
75 static DECLARE_RWSEM(rwsem_B);
76 static DECLARE_RWSEM(rwsem_C);
77 static DECLARE_RWSEM(rwsem_D);
79 #ifdef CONFIG_RT_MUTEXES
81 static DEFINE_RT_MUTEX(rtmutex_A);
82 static DEFINE_RT_MUTEX(rtmutex_B);
83 static DEFINE_RT_MUTEX(rtmutex_C);
84 static DEFINE_RT_MUTEX(rtmutex_D);
86 #endif
89 * Locks that we initialize dynamically as well so that
90 * e.g. X1 and X2 becomes two instances of the same class,
91 * but X* and Y* are different classes. We do this so that
92 * we do not trigger a real lockup:
94 static DEFINE_RAW_SPINLOCK(lock_X1);
95 static DEFINE_RAW_SPINLOCK(lock_X2);
96 static DEFINE_RAW_SPINLOCK(lock_Y1);
97 static DEFINE_RAW_SPINLOCK(lock_Y2);
98 static DEFINE_RAW_SPINLOCK(lock_Z1);
99 static DEFINE_RAW_SPINLOCK(lock_Z2);
101 static DEFINE_RWLOCK(rwlock_X1);
102 static DEFINE_RWLOCK(rwlock_X2);
103 static DEFINE_RWLOCK(rwlock_Y1);
104 static DEFINE_RWLOCK(rwlock_Y2);
105 static DEFINE_RWLOCK(rwlock_Z1);
106 static DEFINE_RWLOCK(rwlock_Z2);
108 static DEFINE_MUTEX(mutex_X1);
109 static DEFINE_MUTEX(mutex_X2);
110 static DEFINE_MUTEX(mutex_Y1);
111 static DEFINE_MUTEX(mutex_Y2);
112 static DEFINE_MUTEX(mutex_Z1);
113 static DEFINE_MUTEX(mutex_Z2);
115 static DECLARE_RWSEM(rwsem_X1);
116 static DECLARE_RWSEM(rwsem_X2);
117 static DECLARE_RWSEM(rwsem_Y1);
118 static DECLARE_RWSEM(rwsem_Y2);
119 static DECLARE_RWSEM(rwsem_Z1);
120 static DECLARE_RWSEM(rwsem_Z2);
122 #ifdef CONFIG_RT_MUTEXES
124 static DEFINE_RT_MUTEX(rtmutex_X1);
125 static DEFINE_RT_MUTEX(rtmutex_X2);
126 static DEFINE_RT_MUTEX(rtmutex_Y1);
127 static DEFINE_RT_MUTEX(rtmutex_Y2);
128 static DEFINE_RT_MUTEX(rtmutex_Z1);
129 static DEFINE_RT_MUTEX(rtmutex_Z2);
131 #endif
134 * non-inlined runtime initializers, to let separate locks share
135 * the same lock-class:
137 #define INIT_CLASS_FUNC(class) \
138 static noinline void \
139 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
140 struct mutex *mutex, struct rw_semaphore *rwsem)\
142 raw_spin_lock_init(lock); \
143 rwlock_init(rwlock); \
144 mutex_init(mutex); \
145 init_rwsem(rwsem); \
148 INIT_CLASS_FUNC(X)
149 INIT_CLASS_FUNC(Y)
150 INIT_CLASS_FUNC(Z)
152 static void init_shared_classes(void)
154 #ifdef CONFIG_RT_MUTEXES
155 static struct lock_class_key rt_X, rt_Y, rt_Z;
157 __rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
158 __rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
159 __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
160 __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
161 __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
162 __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
163 #endif
165 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
166 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
168 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
169 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
171 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
172 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
176 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
177 * The following functions use a lock from a simulated hardirq/softirq
178 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
181 #define HARDIRQ_DISABLE local_irq_disable
182 #define HARDIRQ_ENABLE local_irq_enable
184 #define HARDIRQ_ENTER() \
185 local_irq_disable(); \
186 __irq_enter(); \
187 WARN_ON(!in_irq());
189 #define HARDIRQ_EXIT() \
190 __irq_exit(); \
191 local_irq_enable();
193 #define SOFTIRQ_DISABLE local_bh_disable
194 #define SOFTIRQ_ENABLE local_bh_enable
196 #define SOFTIRQ_ENTER() \
197 local_bh_disable(); \
198 local_irq_disable(); \
199 lockdep_softirq_enter(); \
200 WARN_ON(!in_softirq());
202 #define SOFTIRQ_EXIT() \
203 lockdep_softirq_exit(); \
204 local_irq_enable(); \
205 local_bh_enable();
208 * Shortcuts for lock/unlock API variants, to keep
209 * the testcases compact:
211 #define L(x) raw_spin_lock(&lock_##x)
212 #define U(x) raw_spin_unlock(&lock_##x)
213 #define LU(x) L(x); U(x)
214 #define SI(x) raw_spin_lock_init(&lock_##x)
216 #define WL(x) write_lock(&rwlock_##x)
217 #define WU(x) write_unlock(&rwlock_##x)
218 #define WLU(x) WL(x); WU(x)
220 #define RL(x) read_lock(&rwlock_##x)
221 #define RU(x) read_unlock(&rwlock_##x)
222 #define RLU(x) RL(x); RU(x)
223 #define RWI(x) rwlock_init(&rwlock_##x)
225 #define ML(x) mutex_lock(&mutex_##x)
226 #define MU(x) mutex_unlock(&mutex_##x)
227 #define MI(x) mutex_init(&mutex_##x)
229 #define RTL(x) rt_mutex_lock(&rtmutex_##x)
230 #define RTU(x) rt_mutex_unlock(&rtmutex_##x)
231 #define RTI(x) rt_mutex_init(&rtmutex_##x)
233 #define WSL(x) down_write(&rwsem_##x)
234 #define WSU(x) up_write(&rwsem_##x)
236 #define RSL(x) down_read(&rwsem_##x)
237 #define RSU(x) up_read(&rwsem_##x)
238 #define RWSI(x) init_rwsem(&rwsem_##x)
240 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
241 #define WWAI(x) ww_acquire_init(x, &ww_lockdep)
242 #else
243 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
244 #endif
245 #define WWAD(x) ww_acquire_done(x)
246 #define WWAF(x) ww_acquire_fini(x)
248 #define WWL(x, c) ww_mutex_lock(x, c)
249 #define WWT(x) ww_mutex_trylock(x)
250 #define WWL1(x) ww_mutex_lock(x, NULL)
251 #define WWU(x) ww_mutex_unlock(x)
254 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
257 * Generate different permutations of the same testcase, using
258 * the same basic lock-dependency/state events:
261 #define GENERATE_TESTCASE(name) \
263 static void name(void) { E(); }
265 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \
267 static void name##_12(void) { E1(); E2(); } \
268 static void name##_21(void) { E2(); E1(); }
270 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \
272 static void name##_123(void) { E1(); E2(); E3(); } \
273 static void name##_132(void) { E1(); E3(); E2(); } \
274 static void name##_213(void) { E2(); E1(); E3(); } \
275 static void name##_231(void) { E2(); E3(); E1(); } \
276 static void name##_312(void) { E3(); E1(); E2(); } \
277 static void name##_321(void) { E3(); E2(); E1(); }
280 * AA deadlock:
283 #define E() \
285 LOCK(X1); \
286 LOCK(X2); /* this one should fail */
289 * 6 testcases:
291 #include "locking-selftest-spin.h"
292 GENERATE_TESTCASE(AA_spin)
293 #include "locking-selftest-wlock.h"
294 GENERATE_TESTCASE(AA_wlock)
295 #include "locking-selftest-rlock.h"
296 GENERATE_TESTCASE(AA_rlock)
297 #include "locking-selftest-mutex.h"
298 GENERATE_TESTCASE(AA_mutex)
299 #include "locking-selftest-wsem.h"
300 GENERATE_TESTCASE(AA_wsem)
301 #include "locking-selftest-rsem.h"
302 GENERATE_TESTCASE(AA_rsem)
304 #ifdef CONFIG_RT_MUTEXES
305 #include "locking-selftest-rtmutex.h"
306 GENERATE_TESTCASE(AA_rtmutex);
307 #endif
309 #undef E
312 * Special-case for read-locking, they are
313 * allowed to recurse on the same lock class:
315 static void rlock_AA1(void)
317 RL(X1);
318 RL(X1); // this one should NOT fail
321 static void rlock_AA1B(void)
323 RL(X1);
324 RL(X2); // this one should NOT fail
327 static void rsem_AA1(void)
329 RSL(X1);
330 RSL(X1); // this one should fail
333 static void rsem_AA1B(void)
335 RSL(X1);
336 RSL(X2); // this one should fail
339 * The mixing of read and write locks is not allowed:
341 static void rlock_AA2(void)
343 RL(X1);
344 WL(X2); // this one should fail
347 static void rsem_AA2(void)
349 RSL(X1);
350 WSL(X2); // this one should fail
353 static void rlock_AA3(void)
355 WL(X1);
356 RL(X2); // this one should fail
359 static void rsem_AA3(void)
361 WSL(X1);
362 RSL(X2); // this one should fail
366 * ABBA deadlock:
369 #define E() \
371 LOCK_UNLOCK_2(A, B); \
372 LOCK_UNLOCK_2(B, A); /* fail */
375 * 6 testcases:
377 #include "locking-selftest-spin.h"
378 GENERATE_TESTCASE(ABBA_spin)
379 #include "locking-selftest-wlock.h"
380 GENERATE_TESTCASE(ABBA_wlock)
381 #include "locking-selftest-rlock.h"
382 GENERATE_TESTCASE(ABBA_rlock)
383 #include "locking-selftest-mutex.h"
384 GENERATE_TESTCASE(ABBA_mutex)
385 #include "locking-selftest-wsem.h"
386 GENERATE_TESTCASE(ABBA_wsem)
387 #include "locking-selftest-rsem.h"
388 GENERATE_TESTCASE(ABBA_rsem)
390 #ifdef CONFIG_RT_MUTEXES
391 #include "locking-selftest-rtmutex.h"
392 GENERATE_TESTCASE(ABBA_rtmutex);
393 #endif
395 #undef E
398 * AB BC CA deadlock:
401 #define E() \
403 LOCK_UNLOCK_2(A, B); \
404 LOCK_UNLOCK_2(B, C); \
405 LOCK_UNLOCK_2(C, A); /* fail */
408 * 6 testcases:
410 #include "locking-selftest-spin.h"
411 GENERATE_TESTCASE(ABBCCA_spin)
412 #include "locking-selftest-wlock.h"
413 GENERATE_TESTCASE(ABBCCA_wlock)
414 #include "locking-selftest-rlock.h"
415 GENERATE_TESTCASE(ABBCCA_rlock)
416 #include "locking-selftest-mutex.h"
417 GENERATE_TESTCASE(ABBCCA_mutex)
418 #include "locking-selftest-wsem.h"
419 GENERATE_TESTCASE(ABBCCA_wsem)
420 #include "locking-selftest-rsem.h"
421 GENERATE_TESTCASE(ABBCCA_rsem)
423 #ifdef CONFIG_RT_MUTEXES
424 #include "locking-selftest-rtmutex.h"
425 GENERATE_TESTCASE(ABBCCA_rtmutex);
426 #endif
428 #undef E
431 * AB CA BC deadlock:
434 #define E() \
436 LOCK_UNLOCK_2(A, B); \
437 LOCK_UNLOCK_2(C, A); \
438 LOCK_UNLOCK_2(B, C); /* fail */
441 * 6 testcases:
443 #include "locking-selftest-spin.h"
444 GENERATE_TESTCASE(ABCABC_spin)
445 #include "locking-selftest-wlock.h"
446 GENERATE_TESTCASE(ABCABC_wlock)
447 #include "locking-selftest-rlock.h"
448 GENERATE_TESTCASE(ABCABC_rlock)
449 #include "locking-selftest-mutex.h"
450 GENERATE_TESTCASE(ABCABC_mutex)
451 #include "locking-selftest-wsem.h"
452 GENERATE_TESTCASE(ABCABC_wsem)
453 #include "locking-selftest-rsem.h"
454 GENERATE_TESTCASE(ABCABC_rsem)
456 #ifdef CONFIG_RT_MUTEXES
457 #include "locking-selftest-rtmutex.h"
458 GENERATE_TESTCASE(ABCABC_rtmutex);
459 #endif
461 #undef E
464 * AB BC CD DA deadlock:
467 #define E() \
469 LOCK_UNLOCK_2(A, B); \
470 LOCK_UNLOCK_2(B, C); \
471 LOCK_UNLOCK_2(C, D); \
472 LOCK_UNLOCK_2(D, A); /* fail */
475 * 6 testcases:
477 #include "locking-selftest-spin.h"
478 GENERATE_TESTCASE(ABBCCDDA_spin)
479 #include "locking-selftest-wlock.h"
480 GENERATE_TESTCASE(ABBCCDDA_wlock)
481 #include "locking-selftest-rlock.h"
482 GENERATE_TESTCASE(ABBCCDDA_rlock)
483 #include "locking-selftest-mutex.h"
484 GENERATE_TESTCASE(ABBCCDDA_mutex)
485 #include "locking-selftest-wsem.h"
486 GENERATE_TESTCASE(ABBCCDDA_wsem)
487 #include "locking-selftest-rsem.h"
488 GENERATE_TESTCASE(ABBCCDDA_rsem)
490 #ifdef CONFIG_RT_MUTEXES
491 #include "locking-selftest-rtmutex.h"
492 GENERATE_TESTCASE(ABBCCDDA_rtmutex);
493 #endif
495 #undef E
498 * AB CD BD DA deadlock:
500 #define E() \
502 LOCK_UNLOCK_2(A, B); \
503 LOCK_UNLOCK_2(C, D); \
504 LOCK_UNLOCK_2(B, D); \
505 LOCK_UNLOCK_2(D, A); /* fail */
508 * 6 testcases:
510 #include "locking-selftest-spin.h"
511 GENERATE_TESTCASE(ABCDBDDA_spin)
512 #include "locking-selftest-wlock.h"
513 GENERATE_TESTCASE(ABCDBDDA_wlock)
514 #include "locking-selftest-rlock.h"
515 GENERATE_TESTCASE(ABCDBDDA_rlock)
516 #include "locking-selftest-mutex.h"
517 GENERATE_TESTCASE(ABCDBDDA_mutex)
518 #include "locking-selftest-wsem.h"
519 GENERATE_TESTCASE(ABCDBDDA_wsem)
520 #include "locking-selftest-rsem.h"
521 GENERATE_TESTCASE(ABCDBDDA_rsem)
523 #ifdef CONFIG_RT_MUTEXES
524 #include "locking-selftest-rtmutex.h"
525 GENERATE_TESTCASE(ABCDBDDA_rtmutex);
526 #endif
528 #undef E
531 * AB CD BC DA deadlock:
533 #define E() \
535 LOCK_UNLOCK_2(A, B); \
536 LOCK_UNLOCK_2(C, D); \
537 LOCK_UNLOCK_2(B, C); \
538 LOCK_UNLOCK_2(D, A); /* fail */
541 * 6 testcases:
543 #include "locking-selftest-spin.h"
544 GENERATE_TESTCASE(ABCDBCDA_spin)
545 #include "locking-selftest-wlock.h"
546 GENERATE_TESTCASE(ABCDBCDA_wlock)
547 #include "locking-selftest-rlock.h"
548 GENERATE_TESTCASE(ABCDBCDA_rlock)
549 #include "locking-selftest-mutex.h"
550 GENERATE_TESTCASE(ABCDBCDA_mutex)
551 #include "locking-selftest-wsem.h"
552 GENERATE_TESTCASE(ABCDBCDA_wsem)
553 #include "locking-selftest-rsem.h"
554 GENERATE_TESTCASE(ABCDBCDA_rsem)
556 #ifdef CONFIG_RT_MUTEXES
557 #include "locking-selftest-rtmutex.h"
558 GENERATE_TESTCASE(ABCDBCDA_rtmutex);
559 #endif
561 #undef E
564 * Double unlock:
566 #define E() \
568 LOCK(A); \
569 UNLOCK(A); \
570 UNLOCK(A); /* fail */
573 * 6 testcases:
575 #include "locking-selftest-spin.h"
576 GENERATE_TESTCASE(double_unlock_spin)
577 #include "locking-selftest-wlock.h"
578 GENERATE_TESTCASE(double_unlock_wlock)
579 #include "locking-selftest-rlock.h"
580 GENERATE_TESTCASE(double_unlock_rlock)
581 #include "locking-selftest-mutex.h"
582 GENERATE_TESTCASE(double_unlock_mutex)
583 #include "locking-selftest-wsem.h"
584 GENERATE_TESTCASE(double_unlock_wsem)
585 #include "locking-selftest-rsem.h"
586 GENERATE_TESTCASE(double_unlock_rsem)
588 #ifdef CONFIG_RT_MUTEXES
589 #include "locking-selftest-rtmutex.h"
590 GENERATE_TESTCASE(double_unlock_rtmutex);
591 #endif
593 #undef E
596 * initializing a held lock:
598 #define E() \
600 LOCK(A); \
601 INIT(A); /* fail */
604 * 6 testcases:
606 #include "locking-selftest-spin.h"
607 GENERATE_TESTCASE(init_held_spin)
608 #include "locking-selftest-wlock.h"
609 GENERATE_TESTCASE(init_held_wlock)
610 #include "locking-selftest-rlock.h"
611 GENERATE_TESTCASE(init_held_rlock)
612 #include "locking-selftest-mutex.h"
613 GENERATE_TESTCASE(init_held_mutex)
614 #include "locking-selftest-wsem.h"
615 GENERATE_TESTCASE(init_held_wsem)
616 #include "locking-selftest-rsem.h"
617 GENERATE_TESTCASE(init_held_rsem)
619 #ifdef CONFIG_RT_MUTEXES
620 #include "locking-selftest-rtmutex.h"
621 GENERATE_TESTCASE(init_held_rtmutex);
622 #endif
624 #undef E
627 * locking an irq-safe lock with irqs enabled:
629 #define E1() \
631 IRQ_ENTER(); \
632 LOCK(A); \
633 UNLOCK(A); \
634 IRQ_EXIT();
636 #define E2() \
638 LOCK(A); \
639 UNLOCK(A);
642 * Generate 24 testcases:
644 #include "locking-selftest-spin-hardirq.h"
645 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
647 #include "locking-selftest-rlock-hardirq.h"
648 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
650 #include "locking-selftest-wlock-hardirq.h"
651 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
653 #include "locking-selftest-spin-softirq.h"
654 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
656 #include "locking-selftest-rlock-softirq.h"
657 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
659 #include "locking-selftest-wlock-softirq.h"
660 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
662 #undef E1
663 #undef E2
666 * Enabling hardirqs with a softirq-safe lock held:
668 #define E1() \
670 SOFTIRQ_ENTER(); \
671 LOCK(A); \
672 UNLOCK(A); \
673 SOFTIRQ_EXIT();
675 #define E2() \
677 HARDIRQ_DISABLE(); \
678 LOCK(A); \
679 HARDIRQ_ENABLE(); \
680 UNLOCK(A);
683 * Generate 12 testcases:
685 #include "locking-selftest-spin.h"
686 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
688 #include "locking-selftest-wlock.h"
689 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
691 #include "locking-selftest-rlock.h"
692 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
694 #undef E1
695 #undef E2
698 * Enabling irqs with an irq-safe lock held:
700 #define E1() \
702 IRQ_ENTER(); \
703 LOCK(A); \
704 UNLOCK(A); \
705 IRQ_EXIT();
707 #define E2() \
709 IRQ_DISABLE(); \
710 LOCK(A); \
711 IRQ_ENABLE(); \
712 UNLOCK(A);
715 * Generate 24 testcases:
717 #include "locking-selftest-spin-hardirq.h"
718 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
720 #include "locking-selftest-rlock-hardirq.h"
721 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
723 #include "locking-selftest-wlock-hardirq.h"
724 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
726 #include "locking-selftest-spin-softirq.h"
727 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
729 #include "locking-selftest-rlock-softirq.h"
730 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
732 #include "locking-selftest-wlock-softirq.h"
733 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
735 #undef E1
736 #undef E2
739 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
741 #define E1() \
743 LOCK(A); \
744 LOCK(B); \
745 UNLOCK(B); \
746 UNLOCK(A); \
748 #define E2() \
750 LOCK(B); \
751 UNLOCK(B);
753 #define E3() \
755 IRQ_ENTER(); \
756 LOCK(A); \
757 UNLOCK(A); \
758 IRQ_EXIT();
761 * Generate 36 testcases:
763 #include "locking-selftest-spin-hardirq.h"
764 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
766 #include "locking-selftest-rlock-hardirq.h"
767 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
769 #include "locking-selftest-wlock-hardirq.h"
770 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
772 #include "locking-selftest-spin-softirq.h"
773 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
775 #include "locking-selftest-rlock-softirq.h"
776 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
778 #include "locking-selftest-wlock-softirq.h"
779 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
781 #undef E1
782 #undef E2
783 #undef E3
786 * If a lock turns into softirq-safe, but earlier it took
787 * a softirq-unsafe lock:
790 #define E1() \
791 IRQ_DISABLE(); \
792 LOCK(A); \
793 LOCK(B); \
794 UNLOCK(B); \
795 UNLOCK(A); \
796 IRQ_ENABLE();
798 #define E2() \
799 LOCK(B); \
800 UNLOCK(B);
802 #define E3() \
803 IRQ_ENTER(); \
804 LOCK(A); \
805 UNLOCK(A); \
806 IRQ_EXIT();
809 * Generate 36 testcases:
811 #include "locking-selftest-spin-hardirq.h"
812 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
814 #include "locking-selftest-rlock-hardirq.h"
815 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
817 #include "locking-selftest-wlock-hardirq.h"
818 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
820 #include "locking-selftest-spin-softirq.h"
821 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
823 #include "locking-selftest-rlock-softirq.h"
824 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
826 #include "locking-selftest-wlock-softirq.h"
827 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
829 #undef E1
830 #undef E2
831 #undef E3
834 * read-lock / write-lock irq inversion.
836 * Deadlock scenario:
838 * CPU#1 is at #1, i.e. it has write-locked A, but has not
839 * taken B yet.
841 * CPU#2 is at #2, i.e. it has locked B.
843 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
845 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
846 * will spin on A.
849 #define E1() \
851 IRQ_DISABLE(); \
852 WL(A); \
853 LOCK(B); \
854 UNLOCK(B); \
855 WU(A); \
856 IRQ_ENABLE();
858 #define E2() \
860 LOCK(B); \
861 UNLOCK(B);
863 #define E3() \
865 IRQ_ENTER(); \
866 RL(A); \
867 RU(A); \
868 IRQ_EXIT();
871 * Generate 36 testcases:
873 #include "locking-selftest-spin-hardirq.h"
874 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
876 #include "locking-selftest-rlock-hardirq.h"
877 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
879 #include "locking-selftest-wlock-hardirq.h"
880 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
882 #include "locking-selftest-spin-softirq.h"
883 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
885 #include "locking-selftest-rlock-softirq.h"
886 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
888 #include "locking-selftest-wlock-softirq.h"
889 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
891 #undef E1
892 #undef E2
893 #undef E3
896 * read-lock / write-lock recursion that is actually safe.
899 #define E1() \
901 IRQ_DISABLE(); \
902 WL(A); \
903 WU(A); \
904 IRQ_ENABLE();
906 #define E2() \
908 RL(A); \
909 RU(A); \
911 #define E3() \
913 IRQ_ENTER(); \
914 RL(A); \
915 L(B); \
916 U(B); \
917 RU(A); \
918 IRQ_EXIT();
921 * Generate 12 testcases:
923 #include "locking-selftest-hardirq.h"
924 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
926 #include "locking-selftest-softirq.h"
927 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
929 #undef E1
930 #undef E2
931 #undef E3
934 * read-lock / write-lock recursion that is unsafe.
937 #define E1() \
939 IRQ_DISABLE(); \
940 L(B); \
941 WL(A); \
942 WU(A); \
943 U(B); \
944 IRQ_ENABLE();
946 #define E2() \
948 RL(A); \
949 RU(A); \
951 #define E3() \
953 IRQ_ENTER(); \
954 L(B); \
955 U(B); \
956 IRQ_EXIT();
959 * Generate 12 testcases:
961 #include "locking-selftest-hardirq.h"
962 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
964 #include "locking-selftest-softirq.h"
965 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
967 #ifdef CONFIG_DEBUG_LOCK_ALLOC
968 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
969 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
970 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
971 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
972 # define I_WW(x) lockdep_reset_lock(&x.dep_map)
973 #ifdef CONFIG_RT_MUTEXES
974 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
975 #endif
976 #else
977 # define I_SPINLOCK(x)
978 # define I_RWLOCK(x)
979 # define I_MUTEX(x)
980 # define I_RWSEM(x)
981 # define I_WW(x)
982 #endif
984 #ifndef I_RTMUTEX
985 # define I_RTMUTEX(x)
986 #endif
988 #ifdef CONFIG_RT_MUTEXES
989 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
990 #else
991 #define I2_RTMUTEX(x)
992 #endif
994 #define I1(x) \
995 do { \
996 I_SPINLOCK(x); \
997 I_RWLOCK(x); \
998 I_MUTEX(x); \
999 I_RWSEM(x); \
1000 I_RTMUTEX(x); \
1001 } while (0)
1003 #define I2(x) \
1004 do { \
1005 raw_spin_lock_init(&lock_##x); \
1006 rwlock_init(&rwlock_##x); \
1007 mutex_init(&mutex_##x); \
1008 init_rwsem(&rwsem_##x); \
1009 I2_RTMUTEX(x); \
1010 } while (0)
1012 static void reset_locks(void)
1014 local_irq_disable();
1015 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1016 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1018 I1(A); I1(B); I1(C); I1(D);
1019 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1020 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
1021 lockdep_reset();
1022 I2(A); I2(B); I2(C); I2(D);
1023 init_shared_classes();
1025 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1026 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1027 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1028 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
1029 local_irq_enable();
1032 #undef I
1034 static int testcase_total;
1035 static int testcase_successes;
1036 static int expected_testcase_failures;
1037 static int unexpected_testcase_failures;
1039 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1041 unsigned long saved_preempt_count = preempt_count();
1043 WARN_ON(irqs_disabled());
1045 testcase_fn();
1047 * Filter out expected failures:
1049 #ifndef CONFIG_PROVE_LOCKING
1050 if (expected == FAILURE && debug_locks) {
1051 expected_testcase_failures++;
1052 pr_cont("failed|");
1054 else
1055 #endif
1056 if (debug_locks != expected) {
1057 unexpected_testcase_failures++;
1058 pr_cont("FAILED|");
1060 dump_stack();
1061 } else {
1062 testcase_successes++;
1063 pr_cont(" ok |");
1065 testcase_total++;
1067 if (debug_locks_verbose)
1068 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1069 lockclass_mask, debug_locks, expected);
1071 * Some tests (e.g. double-unlock) might corrupt the preemption
1072 * count, so restore it:
1074 preempt_count_set(saved_preempt_count);
1075 #ifdef CONFIG_TRACE_IRQFLAGS
1076 if (softirq_count())
1077 current->softirqs_enabled = 0;
1078 else
1079 current->softirqs_enabled = 1;
1080 #endif
1082 reset_locks();
1085 #ifdef CONFIG_RT_MUTEXES
1086 #define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1087 #else
1088 #define dotest_rt(fn, e, m)
1089 #endif
1091 static inline void print_testname(const char *testname)
1093 printk("%33s:", testname);
1096 #define DO_TESTCASE_1(desc, name, nr) \
1097 print_testname(desc"/"#nr); \
1098 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1099 pr_cont("\n");
1101 #define DO_TESTCASE_1B(desc, name, nr) \
1102 print_testname(desc"/"#nr); \
1103 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1104 pr_cont("\n");
1106 #define DO_TESTCASE_3(desc, name, nr) \
1107 print_testname(desc"/"#nr); \
1108 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1109 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1110 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1111 pr_cont("\n");
1113 #define DO_TESTCASE_3RW(desc, name, nr) \
1114 print_testname(desc"/"#nr); \
1115 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1116 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1117 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1118 pr_cont("\n");
1120 #define DO_TESTCASE_6(desc, name) \
1121 print_testname(desc); \
1122 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1123 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1124 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1125 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1126 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1127 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1128 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1129 pr_cont("\n");
1131 #define DO_TESTCASE_6_SUCCESS(desc, name) \
1132 print_testname(desc); \
1133 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1134 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1135 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1136 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1137 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1138 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1139 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
1140 pr_cont("\n");
1143 * 'read' variant: rlocks must not trigger.
1145 #define DO_TESTCASE_6R(desc, name) \
1146 print_testname(desc); \
1147 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1148 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1149 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1150 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1151 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1152 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1153 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
1154 pr_cont("\n");
1156 #define DO_TESTCASE_2I(desc, name, nr) \
1157 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1158 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1160 #define DO_TESTCASE_2IB(desc, name, nr) \
1161 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1162 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1164 #define DO_TESTCASE_6I(desc, name, nr) \
1165 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1166 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1168 #define DO_TESTCASE_6IRW(desc, name, nr) \
1169 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1170 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1172 #define DO_TESTCASE_2x3(desc, name) \
1173 DO_TESTCASE_3(desc, name, 12); \
1174 DO_TESTCASE_3(desc, name, 21);
1176 #define DO_TESTCASE_2x6(desc, name) \
1177 DO_TESTCASE_6I(desc, name, 12); \
1178 DO_TESTCASE_6I(desc, name, 21);
1180 #define DO_TESTCASE_6x2(desc, name) \
1181 DO_TESTCASE_2I(desc, name, 123); \
1182 DO_TESTCASE_2I(desc, name, 132); \
1183 DO_TESTCASE_2I(desc, name, 213); \
1184 DO_TESTCASE_2I(desc, name, 231); \
1185 DO_TESTCASE_2I(desc, name, 312); \
1186 DO_TESTCASE_2I(desc, name, 321);
1188 #define DO_TESTCASE_6x2B(desc, name) \
1189 DO_TESTCASE_2IB(desc, name, 123); \
1190 DO_TESTCASE_2IB(desc, name, 132); \
1191 DO_TESTCASE_2IB(desc, name, 213); \
1192 DO_TESTCASE_2IB(desc, name, 231); \
1193 DO_TESTCASE_2IB(desc, name, 312); \
1194 DO_TESTCASE_2IB(desc, name, 321);
1196 #define DO_TESTCASE_6x6(desc, name) \
1197 DO_TESTCASE_6I(desc, name, 123); \
1198 DO_TESTCASE_6I(desc, name, 132); \
1199 DO_TESTCASE_6I(desc, name, 213); \
1200 DO_TESTCASE_6I(desc, name, 231); \
1201 DO_TESTCASE_6I(desc, name, 312); \
1202 DO_TESTCASE_6I(desc, name, 321);
1204 #define DO_TESTCASE_6x6RW(desc, name) \
1205 DO_TESTCASE_6IRW(desc, name, 123); \
1206 DO_TESTCASE_6IRW(desc, name, 132); \
1207 DO_TESTCASE_6IRW(desc, name, 213); \
1208 DO_TESTCASE_6IRW(desc, name, 231); \
1209 DO_TESTCASE_6IRW(desc, name, 312); \
1210 DO_TESTCASE_6IRW(desc, name, 321);
1212 static void ww_test_fail_acquire(void)
1214 int ret;
1216 WWAI(&t);
1217 t.stamp++;
1219 ret = WWL(&o, &t);
1221 if (WARN_ON(!o.ctx) ||
1222 WARN_ON(ret))
1223 return;
1225 /* No lockdep test, pure API */
1226 ret = WWL(&o, &t);
1227 WARN_ON(ret != -EALREADY);
1229 ret = WWT(&o);
1230 WARN_ON(ret);
1232 t2 = t;
1233 t2.stamp++;
1234 ret = WWL(&o, &t2);
1235 WARN_ON(ret != -EDEADLK);
1236 WWU(&o);
1238 if (WWT(&o))
1239 WWU(&o);
1240 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1241 else
1242 DEBUG_LOCKS_WARN_ON(1);
1243 #endif
1246 static void ww_test_normal(void)
1248 int ret;
1250 WWAI(&t);
1253 * None of the ww_mutex codepaths should be taken in the 'normal'
1254 * mutex calls. The easiest way to verify this is by using the
1255 * normal mutex calls, and making sure o.ctx is unmodified.
1258 /* mutex_lock (and indirectly, mutex_lock_nested) */
1259 o.ctx = (void *)~0UL;
1260 mutex_lock(&o.base);
1261 mutex_unlock(&o.base);
1262 WARN_ON(o.ctx != (void *)~0UL);
1264 /* mutex_lock_interruptible (and *_nested) */
1265 o.ctx = (void *)~0UL;
1266 ret = mutex_lock_interruptible(&o.base);
1267 if (!ret)
1268 mutex_unlock(&o.base);
1269 else
1270 WARN_ON(1);
1271 WARN_ON(o.ctx != (void *)~0UL);
1273 /* mutex_lock_killable (and *_nested) */
1274 o.ctx = (void *)~0UL;
1275 ret = mutex_lock_killable(&o.base);
1276 if (!ret)
1277 mutex_unlock(&o.base);
1278 else
1279 WARN_ON(1);
1280 WARN_ON(o.ctx != (void *)~0UL);
1282 /* trylock, succeeding */
1283 o.ctx = (void *)~0UL;
1284 ret = mutex_trylock(&o.base);
1285 WARN_ON(!ret);
1286 if (ret)
1287 mutex_unlock(&o.base);
1288 else
1289 WARN_ON(1);
1290 WARN_ON(o.ctx != (void *)~0UL);
1292 /* trylock, failing */
1293 o.ctx = (void *)~0UL;
1294 mutex_lock(&o.base);
1295 ret = mutex_trylock(&o.base);
1296 WARN_ON(ret);
1297 mutex_unlock(&o.base);
1298 WARN_ON(o.ctx != (void *)~0UL);
1300 /* nest_lock */
1301 o.ctx = (void *)~0UL;
1302 mutex_lock_nest_lock(&o.base, &t);
1303 mutex_unlock(&o.base);
1304 WARN_ON(o.ctx != (void *)~0UL);
1307 static void ww_test_two_contexts(void)
1309 WWAI(&t);
1310 WWAI(&t2);
1313 static void ww_test_diff_class(void)
1315 WWAI(&t);
1316 #ifdef CONFIG_DEBUG_MUTEXES
1317 t.ww_class = NULL;
1318 #endif
1319 WWL(&o, &t);
1322 static void ww_test_context_done_twice(void)
1324 WWAI(&t);
1325 WWAD(&t);
1326 WWAD(&t);
1327 WWAF(&t);
1330 static void ww_test_context_unlock_twice(void)
1332 WWAI(&t);
1333 WWAD(&t);
1334 WWAF(&t);
1335 WWAF(&t);
1338 static void ww_test_context_fini_early(void)
1340 WWAI(&t);
1341 WWL(&o, &t);
1342 WWAD(&t);
1343 WWAF(&t);
1346 static void ww_test_context_lock_after_done(void)
1348 WWAI(&t);
1349 WWAD(&t);
1350 WWL(&o, &t);
1353 static void ww_test_object_unlock_twice(void)
1355 WWL1(&o);
1356 WWU(&o);
1357 WWU(&o);
1360 static void ww_test_object_lock_unbalanced(void)
1362 WWAI(&t);
1363 WWL(&o, &t);
1364 t.acquired = 0;
1365 WWU(&o);
1366 WWAF(&t);
1369 static void ww_test_object_lock_stale_context(void)
1371 WWAI(&t);
1372 o.ctx = &t2;
1373 WWL(&o, &t);
1376 static void ww_test_edeadlk_normal(void)
1378 int ret;
1380 mutex_lock(&o2.base);
1381 o2.ctx = &t2;
1382 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1384 WWAI(&t);
1385 t2 = t;
1386 t2.stamp--;
1388 ret = WWL(&o, &t);
1389 WARN_ON(ret);
1391 ret = WWL(&o2, &t);
1392 WARN_ON(ret != -EDEADLK);
1394 o2.ctx = NULL;
1395 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1396 mutex_unlock(&o2.base);
1397 WWU(&o);
1399 WWL(&o2, &t);
1402 static void ww_test_edeadlk_normal_slow(void)
1404 int ret;
1406 mutex_lock(&o2.base);
1407 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1408 o2.ctx = &t2;
1410 WWAI(&t);
1411 t2 = t;
1412 t2.stamp--;
1414 ret = WWL(&o, &t);
1415 WARN_ON(ret);
1417 ret = WWL(&o2, &t);
1418 WARN_ON(ret != -EDEADLK);
1420 o2.ctx = NULL;
1421 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1422 mutex_unlock(&o2.base);
1423 WWU(&o);
1425 ww_mutex_lock_slow(&o2, &t);
1428 static void ww_test_edeadlk_no_unlock(void)
1430 int ret;
1432 mutex_lock(&o2.base);
1433 o2.ctx = &t2;
1434 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1436 WWAI(&t);
1437 t2 = t;
1438 t2.stamp--;
1440 ret = WWL(&o, &t);
1441 WARN_ON(ret);
1443 ret = WWL(&o2, &t);
1444 WARN_ON(ret != -EDEADLK);
1446 o2.ctx = NULL;
1447 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1448 mutex_unlock(&o2.base);
1450 WWL(&o2, &t);
1453 static void ww_test_edeadlk_no_unlock_slow(void)
1455 int ret;
1457 mutex_lock(&o2.base);
1458 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1459 o2.ctx = &t2;
1461 WWAI(&t);
1462 t2 = t;
1463 t2.stamp--;
1465 ret = WWL(&o, &t);
1466 WARN_ON(ret);
1468 ret = WWL(&o2, &t);
1469 WARN_ON(ret != -EDEADLK);
1471 o2.ctx = NULL;
1472 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1473 mutex_unlock(&o2.base);
1475 ww_mutex_lock_slow(&o2, &t);
1478 static void ww_test_edeadlk_acquire_more(void)
1480 int ret;
1482 mutex_lock(&o2.base);
1483 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1484 o2.ctx = &t2;
1486 WWAI(&t);
1487 t2 = t;
1488 t2.stamp--;
1490 ret = WWL(&o, &t);
1491 WARN_ON(ret);
1493 ret = WWL(&o2, &t);
1494 WARN_ON(ret != -EDEADLK);
1496 ret = WWL(&o3, &t);
1499 static void ww_test_edeadlk_acquire_more_slow(void)
1501 int ret;
1503 mutex_lock(&o2.base);
1504 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1505 o2.ctx = &t2;
1507 WWAI(&t);
1508 t2 = t;
1509 t2.stamp--;
1511 ret = WWL(&o, &t);
1512 WARN_ON(ret);
1514 ret = WWL(&o2, &t);
1515 WARN_ON(ret != -EDEADLK);
1517 ww_mutex_lock_slow(&o3, &t);
1520 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1522 int ret;
1524 mutex_lock(&o2.base);
1525 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1526 o2.ctx = &t2;
1528 mutex_lock(&o3.base);
1529 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1530 o3.ctx = &t2;
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 ret = WWL(&o3, &t);
1543 WARN_ON(ret != -EDEADLK);
1546 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1548 int ret;
1550 mutex_lock(&o2.base);
1551 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1552 o2.ctx = &t2;
1554 mutex_lock(&o3.base);
1555 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1556 o3.ctx = &t2;
1558 WWAI(&t);
1559 t2 = t;
1560 t2.stamp--;
1562 ret = WWL(&o, &t);
1563 WARN_ON(ret);
1565 ret = WWL(&o2, &t);
1566 WARN_ON(ret != -EDEADLK);
1568 ww_mutex_lock_slow(&o3, &t);
1571 static void ww_test_edeadlk_acquire_wrong(void)
1573 int ret;
1575 mutex_lock(&o2.base);
1576 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1577 o2.ctx = &t2;
1579 WWAI(&t);
1580 t2 = t;
1581 t2.stamp--;
1583 ret = WWL(&o, &t);
1584 WARN_ON(ret);
1586 ret = WWL(&o2, &t);
1587 WARN_ON(ret != -EDEADLK);
1588 if (!ret)
1589 WWU(&o2);
1591 WWU(&o);
1593 ret = WWL(&o3, &t);
1596 static void ww_test_edeadlk_acquire_wrong_slow(void)
1598 int ret;
1600 mutex_lock(&o2.base);
1601 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1602 o2.ctx = &t2;
1604 WWAI(&t);
1605 t2 = t;
1606 t2.stamp--;
1608 ret = WWL(&o, &t);
1609 WARN_ON(ret);
1611 ret = WWL(&o2, &t);
1612 WARN_ON(ret != -EDEADLK);
1613 if (!ret)
1614 WWU(&o2);
1616 WWU(&o);
1618 ww_mutex_lock_slow(&o3, &t);
1621 static void ww_test_spin_nest_unlocked(void)
1623 raw_spin_lock_nest_lock(&lock_A, &o.base);
1624 U(A);
1627 static void ww_test_unneeded_slow(void)
1629 WWAI(&t);
1631 ww_mutex_lock_slow(&o, &t);
1634 static void ww_test_context_block(void)
1636 int ret;
1638 WWAI(&t);
1640 ret = WWL(&o, &t);
1641 WARN_ON(ret);
1642 WWL1(&o2);
1645 static void ww_test_context_try(void)
1647 int ret;
1649 WWAI(&t);
1651 ret = WWL(&o, &t);
1652 WARN_ON(ret);
1654 ret = WWT(&o2);
1655 WARN_ON(!ret);
1656 WWU(&o2);
1657 WWU(&o);
1660 static void ww_test_context_context(void)
1662 int ret;
1664 WWAI(&t);
1666 ret = WWL(&o, &t);
1667 WARN_ON(ret);
1669 ret = WWL(&o2, &t);
1670 WARN_ON(ret);
1672 WWU(&o2);
1673 WWU(&o);
1676 static void ww_test_try_block(void)
1678 bool ret;
1680 ret = WWT(&o);
1681 WARN_ON(!ret);
1683 WWL1(&o2);
1684 WWU(&o2);
1685 WWU(&o);
1688 static void ww_test_try_try(void)
1690 bool ret;
1692 ret = WWT(&o);
1693 WARN_ON(!ret);
1694 ret = WWT(&o2);
1695 WARN_ON(!ret);
1696 WWU(&o2);
1697 WWU(&o);
1700 static void ww_test_try_context(void)
1702 int ret;
1704 ret = WWT(&o);
1705 WARN_ON(!ret);
1707 WWAI(&t);
1709 ret = WWL(&o2, &t);
1710 WARN_ON(ret);
1713 static void ww_test_block_block(void)
1715 WWL1(&o);
1716 WWL1(&o2);
1719 static void ww_test_block_try(void)
1721 bool ret;
1723 WWL1(&o);
1724 ret = WWT(&o2);
1725 WARN_ON(!ret);
1728 static void ww_test_block_context(void)
1730 int ret;
1732 WWL1(&o);
1733 WWAI(&t);
1735 ret = WWL(&o2, &t);
1736 WARN_ON(ret);
1739 static void ww_test_spin_block(void)
1741 L(A);
1742 U(A);
1744 WWL1(&o);
1745 L(A);
1746 U(A);
1747 WWU(&o);
1749 L(A);
1750 WWL1(&o);
1751 WWU(&o);
1752 U(A);
1755 static void ww_test_spin_try(void)
1757 bool ret;
1759 L(A);
1760 U(A);
1762 ret = WWT(&o);
1763 WARN_ON(!ret);
1764 L(A);
1765 U(A);
1766 WWU(&o);
1768 L(A);
1769 ret = WWT(&o);
1770 WARN_ON(!ret);
1771 WWU(&o);
1772 U(A);
1775 static void ww_test_spin_context(void)
1777 int ret;
1779 L(A);
1780 U(A);
1782 WWAI(&t);
1784 ret = WWL(&o, &t);
1785 WARN_ON(ret);
1786 L(A);
1787 U(A);
1788 WWU(&o);
1790 L(A);
1791 ret = WWL(&o, &t);
1792 WARN_ON(ret);
1793 WWU(&o);
1794 U(A);
1797 static void ww_tests(void)
1799 printk(" --------------------------------------------------------------------------\n");
1800 printk(" | Wound/wait tests |\n");
1801 printk(" ---------------------\n");
1803 print_testname("ww api failures");
1804 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1805 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1806 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1807 pr_cont("\n");
1809 print_testname("ww contexts mixing");
1810 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1811 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1812 pr_cont("\n");
1814 print_testname("finishing ww context");
1815 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1816 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1817 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1818 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1819 pr_cont("\n");
1821 print_testname("locking mismatches");
1822 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1823 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1824 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1825 pr_cont("\n");
1827 print_testname("EDEADLK handling");
1828 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1829 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1830 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1831 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1832 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1833 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1834 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1835 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1836 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1837 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1838 pr_cont("\n");
1840 print_testname("spinlock nest unlocked");
1841 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1842 pr_cont("\n");
1844 printk(" -----------------------------------------------------\n");
1845 printk(" |block | try |context|\n");
1846 printk(" -----------------------------------------------------\n");
1848 print_testname("context");
1849 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1850 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1851 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1852 pr_cont("\n");
1854 print_testname("try");
1855 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1856 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1857 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1858 pr_cont("\n");
1860 print_testname("block");
1861 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1862 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1863 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1864 pr_cont("\n");
1866 print_testname("spinlock");
1867 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1868 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1869 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1870 pr_cont("\n");
1873 void locking_selftest(void)
1876 * Got a locking failure before the selftest ran?
1878 if (!debug_locks) {
1879 printk("----------------------------------\n");
1880 printk("| Locking API testsuite disabled |\n");
1881 printk("----------------------------------\n");
1882 return;
1886 * Run the testsuite:
1888 printk("------------------------\n");
1889 printk("| Locking API testsuite:\n");
1890 printk("----------------------------------------------------------------------------\n");
1891 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1892 printk(" --------------------------------------------------------------------------\n");
1894 init_shared_classes();
1895 debug_locks_silent = !debug_locks_verbose;
1897 DO_TESTCASE_6R("A-A deadlock", AA);
1898 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1899 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1900 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1901 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1902 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1903 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1904 DO_TESTCASE_6("double unlock", double_unlock);
1905 DO_TESTCASE_6("initialize held", init_held);
1907 printk(" --------------------------------------------------------------------------\n");
1908 print_testname("recursive read-lock");
1909 pr_cont(" |");
1910 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1911 pr_cont(" |");
1912 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1913 pr_cont("\n");
1915 print_testname("recursive read-lock #2");
1916 pr_cont(" |");
1917 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
1918 pr_cont(" |");
1919 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1920 pr_cont("\n");
1922 print_testname("mixed read-write-lock");
1923 pr_cont(" |");
1924 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1925 pr_cont(" |");
1926 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1927 pr_cont("\n");
1929 print_testname("mixed write-read-lock");
1930 pr_cont(" |");
1931 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1932 pr_cont(" |");
1933 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1934 pr_cont("\n");
1936 printk(" --------------------------------------------------------------------------\n");
1939 * irq-context testcases:
1941 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1942 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1943 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1944 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1945 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1946 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1948 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1949 // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1951 ww_tests();
1953 if (unexpected_testcase_failures) {
1954 printk("-----------------------------------------------------------------\n");
1955 debug_locks = 0;
1956 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1957 unexpected_testcase_failures, testcase_total);
1958 printk("-----------------------------------------------------------------\n");
1959 } else if (expected_testcase_failures && testcase_successes) {
1960 printk("--------------------------------------------------------\n");
1961 printk("%3d out of %3d testcases failed, as expected. |\n",
1962 expected_testcase_failures, testcase_total);
1963 printk("----------------------------------------------------\n");
1964 debug_locks = 1;
1965 } else if (expected_testcase_failures && !testcase_successes) {
1966 printk("--------------------------------------------------------\n");
1967 printk("All %3d testcases failed, as expected. |\n",
1968 expected_testcase_failures);
1969 printk("----------------------------------------\n");
1970 debug_locks = 1;
1971 } else {
1972 printk("-------------------------------------------------------\n");
1973 printk("Good, all %3d testcases passed! |\n",
1974 testcase_successes);
1975 printk("---------------------------------\n");
1976 debug_locks = 1;
1978 debug_locks_silent = 0;