mutex: Add w/w tests to lib/locking-selftest.c
[linux-2.6-block.git] / lib / locking-selftest.c
CommitLineData
cae2ed9a
IM
1/*
2 * lib/locking-selftest.c
3 *
4 * Testsuite for various locking APIs: spinlocks, rwlocks,
5 * mutexes and rw-semaphores.
6 *
7 * It is checking both false positives and false negatives.
8 *
9 * Started by Ingo Molnar:
10 *
11 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
12 */
13#include <linux/rwsem.h>
14#include <linux/mutex.h>
15#include <linux/sched.h>
16#include <linux/delay.h>
fbb9ce95 17#include <linux/lockdep.h>
cae2ed9a
IM
18#include <linux/spinlock.h>
19#include <linux/kallsyms.h>
20#include <linux/interrupt.h>
21#include <linux/debug_locks.h>
22#include <linux/irqflags.h>
23
24/*
25 * Change this to 1 if you want to see the failure printouts:
26 */
27static unsigned int debug_locks_verbose;
28
1de99445
ML
29static DEFINE_WW_CLASS(ww_lockdep);
30
cae2ed9a
IM
31static int __init setup_debug_locks_verbose(char *str)
32{
33 get_option(&str, &debug_locks_verbose);
34
35 return 1;
36}
37
38__setup("debug_locks_verbose=", setup_debug_locks_verbose);
39
40#define FAILURE 0
41#define SUCCESS 1
42
43#define LOCKTYPE_SPIN 0x1
44#define LOCKTYPE_RWLOCK 0x2
45#define LOCKTYPE_MUTEX 0x4
46#define LOCKTYPE_RWSEM 0x8
1de99445
ML
47#define LOCKTYPE_WW 0x10
48
49static struct ww_acquire_ctx t, t2;
50static struct ww_mutex o, o2;
cae2ed9a
IM
51
52/*
53 * Normal standalone locks, for the circular and irq-context
54 * dependency tests:
55 */
9fb1b90c
YZ
56static DEFINE_RAW_SPINLOCK(lock_A);
57static DEFINE_RAW_SPINLOCK(lock_B);
58static DEFINE_RAW_SPINLOCK(lock_C);
59static DEFINE_RAW_SPINLOCK(lock_D);
cae2ed9a
IM
60
61static DEFINE_RWLOCK(rwlock_A);
62static DEFINE_RWLOCK(rwlock_B);
63static DEFINE_RWLOCK(rwlock_C);
64static DEFINE_RWLOCK(rwlock_D);
65
66static DEFINE_MUTEX(mutex_A);
67static DEFINE_MUTEX(mutex_B);
68static DEFINE_MUTEX(mutex_C);
69static DEFINE_MUTEX(mutex_D);
70
71static DECLARE_RWSEM(rwsem_A);
72static DECLARE_RWSEM(rwsem_B);
73static DECLARE_RWSEM(rwsem_C);
74static DECLARE_RWSEM(rwsem_D);
75
76/*
77 * Locks that we initialize dynamically as well so that
78 * e.g. X1 and X2 becomes two instances of the same class,
79 * but X* and Y* are different classes. We do this so that
80 * we do not trigger a real lockup:
81 */
9fb1b90c
YZ
82static DEFINE_RAW_SPINLOCK(lock_X1);
83static DEFINE_RAW_SPINLOCK(lock_X2);
84static DEFINE_RAW_SPINLOCK(lock_Y1);
85static DEFINE_RAW_SPINLOCK(lock_Y2);
86static DEFINE_RAW_SPINLOCK(lock_Z1);
87static DEFINE_RAW_SPINLOCK(lock_Z2);
cae2ed9a
IM
88
89static DEFINE_RWLOCK(rwlock_X1);
90static DEFINE_RWLOCK(rwlock_X2);
91static DEFINE_RWLOCK(rwlock_Y1);
92static DEFINE_RWLOCK(rwlock_Y2);
93static DEFINE_RWLOCK(rwlock_Z1);
94static DEFINE_RWLOCK(rwlock_Z2);
95
96static DEFINE_MUTEX(mutex_X1);
97static DEFINE_MUTEX(mutex_X2);
98static DEFINE_MUTEX(mutex_Y1);
99static DEFINE_MUTEX(mutex_Y2);
100static DEFINE_MUTEX(mutex_Z1);
101static DEFINE_MUTEX(mutex_Z2);
102
103static DECLARE_RWSEM(rwsem_X1);
104static DECLARE_RWSEM(rwsem_X2);
105static DECLARE_RWSEM(rwsem_Y1);
106static DECLARE_RWSEM(rwsem_Y2);
107static DECLARE_RWSEM(rwsem_Z1);
108static DECLARE_RWSEM(rwsem_Z2);
109
110/*
111 * non-inlined runtime initializers, to let separate locks share
112 * the same lock-class:
113 */
114#define INIT_CLASS_FUNC(class) \
115static noinline void \
9fb1b90c
YZ
116init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
117 struct mutex *mutex, struct rw_semaphore *rwsem)\
cae2ed9a 118{ \
9fb1b90c 119 raw_spin_lock_init(lock); \
cae2ed9a
IM
120 rwlock_init(rwlock); \
121 mutex_init(mutex); \
122 init_rwsem(rwsem); \
123}
124
125INIT_CLASS_FUNC(X)
126INIT_CLASS_FUNC(Y)
127INIT_CLASS_FUNC(Z)
128
129static void init_shared_classes(void)
130{
131 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
132 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
133
134 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
135 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
136
137 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
138 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
139}
140
141/*
142 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
143 * The following functions use a lock from a simulated hardirq/softirq
144 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
145 */
146
147#define HARDIRQ_DISABLE local_irq_disable
148#define HARDIRQ_ENABLE local_irq_enable
149
150#define HARDIRQ_ENTER() \
151 local_irq_disable(); \
ba9f207c 152 __irq_enter(); \
cae2ed9a
IM
153 WARN_ON(!in_irq());
154
155#define HARDIRQ_EXIT() \
156 __irq_exit(); \
157 local_irq_enable();
158
159#define SOFTIRQ_DISABLE local_bh_disable
160#define SOFTIRQ_ENABLE local_bh_enable
161
162#define SOFTIRQ_ENTER() \
163 local_bh_disable(); \
164 local_irq_disable(); \
d820ac4c 165 lockdep_softirq_enter(); \
cae2ed9a
IM
166 WARN_ON(!in_softirq());
167
168#define SOFTIRQ_EXIT() \
d820ac4c 169 lockdep_softirq_exit(); \
cae2ed9a
IM
170 local_irq_enable(); \
171 local_bh_enable();
172
173/*
174 * Shortcuts for lock/unlock API variants, to keep
175 * the testcases compact:
176 */
9fb1b90c
YZ
177#define L(x) raw_spin_lock(&lock_##x)
178#define U(x) raw_spin_unlock(&lock_##x)
cae2ed9a 179#define LU(x) L(x); U(x)
9fb1b90c 180#define SI(x) raw_spin_lock_init(&lock_##x)
cae2ed9a
IM
181
182#define WL(x) write_lock(&rwlock_##x)
183#define WU(x) write_unlock(&rwlock_##x)
184#define WLU(x) WL(x); WU(x)
185
186#define RL(x) read_lock(&rwlock_##x)
187#define RU(x) read_unlock(&rwlock_##x)
188#define RLU(x) RL(x); RU(x)
189#define RWI(x) rwlock_init(&rwlock_##x)
190
191#define ML(x) mutex_lock(&mutex_##x)
192#define MU(x) mutex_unlock(&mutex_##x)
193#define MI(x) mutex_init(&mutex_##x)
194
195#define WSL(x) down_write(&rwsem_##x)
196#define WSU(x) up_write(&rwsem_##x)
197
198#define RSL(x) down_read(&rwsem_##x)
199#define RSU(x) up_read(&rwsem_##x)
200#define RWSI(x) init_rwsem(&rwsem_##x)
201
1de99445
ML
202#ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
203#define WWAI(x) ww_acquire_init(x, &ww_lockdep)
204#else
205#define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
206#endif
207#define WWAD(x) ww_acquire_done(x)
208#define WWAF(x) ww_acquire_fini(x)
209
210#define WWL(x, c) ww_mutex_lock(x, c)
211#define WWT(x) ww_mutex_trylock(x)
212#define WWL1(x) ww_mutex_lock(x, NULL)
213#define WWU(x) ww_mutex_unlock(x)
214
215
cae2ed9a
IM
216#define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
217
218/*
219 * Generate different permutations of the same testcase, using
220 * the same basic lock-dependency/state events:
221 */
222
223#define GENERATE_TESTCASE(name) \
224 \
225static void name(void) { E(); }
226
227#define GENERATE_PERMUTATIONS_2_EVENTS(name) \
228 \
229static void name##_12(void) { E1(); E2(); } \
230static void name##_21(void) { E2(); E1(); }
231
232#define GENERATE_PERMUTATIONS_3_EVENTS(name) \
233 \
234static void name##_123(void) { E1(); E2(); E3(); } \
235static void name##_132(void) { E1(); E3(); E2(); } \
236static void name##_213(void) { E2(); E1(); E3(); } \
237static void name##_231(void) { E2(); E3(); E1(); } \
238static void name##_312(void) { E3(); E1(); E2(); } \
239static void name##_321(void) { E3(); E2(); E1(); }
240
241/*
242 * AA deadlock:
243 */
244
245#define E() \
246 \
247 LOCK(X1); \
248 LOCK(X2); /* this one should fail */
249
250/*
251 * 6 testcases:
252 */
253#include "locking-selftest-spin.h"
254GENERATE_TESTCASE(AA_spin)
255#include "locking-selftest-wlock.h"
256GENERATE_TESTCASE(AA_wlock)
257#include "locking-selftest-rlock.h"
258GENERATE_TESTCASE(AA_rlock)
259#include "locking-selftest-mutex.h"
260GENERATE_TESTCASE(AA_mutex)
261#include "locking-selftest-wsem.h"
262GENERATE_TESTCASE(AA_wsem)
263#include "locking-selftest-rsem.h"
264GENERATE_TESTCASE(AA_rsem)
265
266#undef E
267
268/*
269 * Special-case for read-locking, they are
6c9076ec 270 * allowed to recurse on the same lock class:
cae2ed9a
IM
271 */
272static void rlock_AA1(void)
273{
274 RL(X1);
275 RL(X1); // this one should NOT fail
276}
277
278static void rlock_AA1B(void)
279{
280 RL(X1);
6c9076ec 281 RL(X2); // this one should NOT fail
cae2ed9a
IM
282}
283
284static void rsem_AA1(void)
285{
286 RSL(X1);
287 RSL(X1); // this one should fail
288}
289
290static void rsem_AA1B(void)
291{
292 RSL(X1);
293 RSL(X2); // this one should fail
294}
295/*
296 * The mixing of read and write locks is not allowed:
297 */
298static void rlock_AA2(void)
299{
300 RL(X1);
301 WL(X2); // this one should fail
302}
303
304static void rsem_AA2(void)
305{
306 RSL(X1);
307 WSL(X2); // this one should fail
308}
309
310static void rlock_AA3(void)
311{
312 WL(X1);
313 RL(X2); // this one should fail
314}
315
316static void rsem_AA3(void)
317{
318 WSL(X1);
319 RSL(X2); // this one should fail
320}
321
322/*
323 * ABBA deadlock:
324 */
325
326#define E() \
327 \
328 LOCK_UNLOCK_2(A, B); \
329 LOCK_UNLOCK_2(B, A); /* fail */
330
331/*
332 * 6 testcases:
333 */
334#include "locking-selftest-spin.h"
335GENERATE_TESTCASE(ABBA_spin)
336#include "locking-selftest-wlock.h"
337GENERATE_TESTCASE(ABBA_wlock)
338#include "locking-selftest-rlock.h"
339GENERATE_TESTCASE(ABBA_rlock)
340#include "locking-selftest-mutex.h"
341GENERATE_TESTCASE(ABBA_mutex)
342#include "locking-selftest-wsem.h"
343GENERATE_TESTCASE(ABBA_wsem)
344#include "locking-selftest-rsem.h"
345GENERATE_TESTCASE(ABBA_rsem)
346
347#undef E
348
349/*
350 * AB BC CA deadlock:
351 */
352
353#define E() \
354 \
355 LOCK_UNLOCK_2(A, B); \
356 LOCK_UNLOCK_2(B, C); \
357 LOCK_UNLOCK_2(C, A); /* fail */
358
359/*
360 * 6 testcases:
361 */
362#include "locking-selftest-spin.h"
363GENERATE_TESTCASE(ABBCCA_spin)
364#include "locking-selftest-wlock.h"
365GENERATE_TESTCASE(ABBCCA_wlock)
366#include "locking-selftest-rlock.h"
367GENERATE_TESTCASE(ABBCCA_rlock)
368#include "locking-selftest-mutex.h"
369GENERATE_TESTCASE(ABBCCA_mutex)
370#include "locking-selftest-wsem.h"
371GENERATE_TESTCASE(ABBCCA_wsem)
372#include "locking-selftest-rsem.h"
373GENERATE_TESTCASE(ABBCCA_rsem)
374
375#undef E
376
377/*
378 * AB CA BC deadlock:
379 */
380
381#define E() \
382 \
383 LOCK_UNLOCK_2(A, B); \
384 LOCK_UNLOCK_2(C, A); \
385 LOCK_UNLOCK_2(B, C); /* fail */
386
387/*
388 * 6 testcases:
389 */
390#include "locking-selftest-spin.h"
391GENERATE_TESTCASE(ABCABC_spin)
392#include "locking-selftest-wlock.h"
393GENERATE_TESTCASE(ABCABC_wlock)
394#include "locking-selftest-rlock.h"
395GENERATE_TESTCASE(ABCABC_rlock)
396#include "locking-selftest-mutex.h"
397GENERATE_TESTCASE(ABCABC_mutex)
398#include "locking-selftest-wsem.h"
399GENERATE_TESTCASE(ABCABC_wsem)
400#include "locking-selftest-rsem.h"
401GENERATE_TESTCASE(ABCABC_rsem)
402
403#undef E
404
405/*
406 * AB BC CD DA deadlock:
407 */
408
409#define E() \
410 \
411 LOCK_UNLOCK_2(A, B); \
412 LOCK_UNLOCK_2(B, C); \
413 LOCK_UNLOCK_2(C, D); \
414 LOCK_UNLOCK_2(D, A); /* fail */
415
416/*
417 * 6 testcases:
418 */
419#include "locking-selftest-spin.h"
420GENERATE_TESTCASE(ABBCCDDA_spin)
421#include "locking-selftest-wlock.h"
422GENERATE_TESTCASE(ABBCCDDA_wlock)
423#include "locking-selftest-rlock.h"
424GENERATE_TESTCASE(ABBCCDDA_rlock)
425#include "locking-selftest-mutex.h"
426GENERATE_TESTCASE(ABBCCDDA_mutex)
427#include "locking-selftest-wsem.h"
428GENERATE_TESTCASE(ABBCCDDA_wsem)
429#include "locking-selftest-rsem.h"
430GENERATE_TESTCASE(ABBCCDDA_rsem)
431
432#undef E
433
434/*
435 * AB CD BD DA deadlock:
436 */
437#define E() \
438 \
439 LOCK_UNLOCK_2(A, B); \
440 LOCK_UNLOCK_2(C, D); \
441 LOCK_UNLOCK_2(B, D); \
442 LOCK_UNLOCK_2(D, A); /* fail */
443
444/*
445 * 6 testcases:
446 */
447#include "locking-selftest-spin.h"
448GENERATE_TESTCASE(ABCDBDDA_spin)
449#include "locking-selftest-wlock.h"
450GENERATE_TESTCASE(ABCDBDDA_wlock)
451#include "locking-selftest-rlock.h"
452GENERATE_TESTCASE(ABCDBDDA_rlock)
453#include "locking-selftest-mutex.h"
454GENERATE_TESTCASE(ABCDBDDA_mutex)
455#include "locking-selftest-wsem.h"
456GENERATE_TESTCASE(ABCDBDDA_wsem)
457#include "locking-selftest-rsem.h"
458GENERATE_TESTCASE(ABCDBDDA_rsem)
459
460#undef E
461
462/*
463 * AB CD BC DA deadlock:
464 */
465#define E() \
466 \
467 LOCK_UNLOCK_2(A, B); \
468 LOCK_UNLOCK_2(C, D); \
469 LOCK_UNLOCK_2(B, C); \
470 LOCK_UNLOCK_2(D, A); /* fail */
471
472/*
473 * 6 testcases:
474 */
475#include "locking-selftest-spin.h"
476GENERATE_TESTCASE(ABCDBCDA_spin)
477#include "locking-selftest-wlock.h"
478GENERATE_TESTCASE(ABCDBCDA_wlock)
479#include "locking-selftest-rlock.h"
480GENERATE_TESTCASE(ABCDBCDA_rlock)
481#include "locking-selftest-mutex.h"
482GENERATE_TESTCASE(ABCDBCDA_mutex)
483#include "locking-selftest-wsem.h"
484GENERATE_TESTCASE(ABCDBCDA_wsem)
485#include "locking-selftest-rsem.h"
486GENERATE_TESTCASE(ABCDBCDA_rsem)
487
488#undef E
489
490/*
491 * Double unlock:
492 */
493#define E() \
494 \
495 LOCK(A); \
496 UNLOCK(A); \
497 UNLOCK(A); /* fail */
498
499/*
500 * 6 testcases:
501 */
502#include "locking-selftest-spin.h"
503GENERATE_TESTCASE(double_unlock_spin)
504#include "locking-selftest-wlock.h"
505GENERATE_TESTCASE(double_unlock_wlock)
506#include "locking-selftest-rlock.h"
507GENERATE_TESTCASE(double_unlock_rlock)
508#include "locking-selftest-mutex.h"
509GENERATE_TESTCASE(double_unlock_mutex)
510#include "locking-selftest-wsem.h"
511GENERATE_TESTCASE(double_unlock_wsem)
512#include "locking-selftest-rsem.h"
513GENERATE_TESTCASE(double_unlock_rsem)
514
515#undef E
516
517/*
518 * Bad unlock ordering:
519 */
520#define E() \
521 \
522 LOCK(A); \
523 LOCK(B); \
524 UNLOCK(A); /* fail */ \
525 UNLOCK(B);
526
527/*
528 * 6 testcases:
529 */
530#include "locking-selftest-spin.h"
531GENERATE_TESTCASE(bad_unlock_order_spin)
532#include "locking-selftest-wlock.h"
533GENERATE_TESTCASE(bad_unlock_order_wlock)
534#include "locking-selftest-rlock.h"
535GENERATE_TESTCASE(bad_unlock_order_rlock)
536#include "locking-selftest-mutex.h"
537GENERATE_TESTCASE(bad_unlock_order_mutex)
538#include "locking-selftest-wsem.h"
539GENERATE_TESTCASE(bad_unlock_order_wsem)
540#include "locking-selftest-rsem.h"
541GENERATE_TESTCASE(bad_unlock_order_rsem)
542
543#undef E
544
545/*
546 * initializing a held lock:
547 */
548#define E() \
549 \
550 LOCK(A); \
551 INIT(A); /* fail */
552
553/*
554 * 6 testcases:
555 */
556#include "locking-selftest-spin.h"
557GENERATE_TESTCASE(init_held_spin)
558#include "locking-selftest-wlock.h"
559GENERATE_TESTCASE(init_held_wlock)
560#include "locking-selftest-rlock.h"
561GENERATE_TESTCASE(init_held_rlock)
562#include "locking-selftest-mutex.h"
563GENERATE_TESTCASE(init_held_mutex)
564#include "locking-selftest-wsem.h"
565GENERATE_TESTCASE(init_held_wsem)
566#include "locking-selftest-rsem.h"
567GENERATE_TESTCASE(init_held_rsem)
568
569#undef E
570
571/*
572 * locking an irq-safe lock with irqs enabled:
573 */
574#define E1() \
575 \
576 IRQ_ENTER(); \
577 LOCK(A); \
578 UNLOCK(A); \
579 IRQ_EXIT();
580
581#define E2() \
582 \
583 LOCK(A); \
584 UNLOCK(A);
585
586/*
587 * Generate 24 testcases:
588 */
589#include "locking-selftest-spin-hardirq.h"
590GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
591
592#include "locking-selftest-rlock-hardirq.h"
593GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
594
595#include "locking-selftest-wlock-hardirq.h"
596GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
597
598#include "locking-selftest-spin-softirq.h"
599GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
600
601#include "locking-selftest-rlock-softirq.h"
602GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
603
604#include "locking-selftest-wlock-softirq.h"
605GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
606
607#undef E1
608#undef E2
609
610/*
611 * Enabling hardirqs with a softirq-safe lock held:
612 */
613#define E1() \
614 \
615 SOFTIRQ_ENTER(); \
616 LOCK(A); \
617 UNLOCK(A); \
618 SOFTIRQ_EXIT();
619
620#define E2() \
621 \
622 HARDIRQ_DISABLE(); \
623 LOCK(A); \
624 HARDIRQ_ENABLE(); \
625 UNLOCK(A);
626
627/*
628 * Generate 12 testcases:
629 */
630#include "locking-selftest-spin.h"
631GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
632
633#include "locking-selftest-wlock.h"
634GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
635
636#include "locking-selftest-rlock.h"
637GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
638
639#undef E1
640#undef E2
641
642/*
643 * Enabling irqs with an irq-safe lock held:
644 */
645#define E1() \
646 \
647 IRQ_ENTER(); \
648 LOCK(A); \
649 UNLOCK(A); \
650 IRQ_EXIT();
651
652#define E2() \
653 \
654 IRQ_DISABLE(); \
655 LOCK(A); \
656 IRQ_ENABLE(); \
657 UNLOCK(A);
658
659/*
660 * Generate 24 testcases:
661 */
662#include "locking-selftest-spin-hardirq.h"
663GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
664
665#include "locking-selftest-rlock-hardirq.h"
666GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
667
668#include "locking-selftest-wlock-hardirq.h"
669GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
670
671#include "locking-selftest-spin-softirq.h"
672GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
673
674#include "locking-selftest-rlock-softirq.h"
675GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
676
677#include "locking-selftest-wlock-softirq.h"
678GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
679
680#undef E1
681#undef E2
682
683/*
684 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
685 */
686#define E1() \
687 \
688 LOCK(A); \
689 LOCK(B); \
690 UNLOCK(B); \
691 UNLOCK(A); \
692
693#define E2() \
694 \
695 LOCK(B); \
696 UNLOCK(B);
697
698#define E3() \
699 \
700 IRQ_ENTER(); \
701 LOCK(A); \
702 UNLOCK(A); \
703 IRQ_EXIT();
704
705/*
706 * Generate 36 testcases:
707 */
708#include "locking-selftest-spin-hardirq.h"
709GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
710
711#include "locking-selftest-rlock-hardirq.h"
712GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
713
714#include "locking-selftest-wlock-hardirq.h"
715GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
716
717#include "locking-selftest-spin-softirq.h"
718GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
719
720#include "locking-selftest-rlock-softirq.h"
721GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
722
723#include "locking-selftest-wlock-softirq.h"
724GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
725
726#undef E1
727#undef E2
728#undef E3
729
730/*
731 * If a lock turns into softirq-safe, but earlier it took
732 * a softirq-unsafe lock:
733 */
734
735#define E1() \
736 IRQ_DISABLE(); \
737 LOCK(A); \
738 LOCK(B); \
739 UNLOCK(B); \
740 UNLOCK(A); \
741 IRQ_ENABLE();
742
743#define E2() \
744 LOCK(B); \
745 UNLOCK(B);
746
747#define E3() \
748 IRQ_ENTER(); \
749 LOCK(A); \
750 UNLOCK(A); \
751 IRQ_EXIT();
752
753/*
754 * Generate 36 testcases:
755 */
756#include "locking-selftest-spin-hardirq.h"
757GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
758
759#include "locking-selftest-rlock-hardirq.h"
760GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
761
762#include "locking-selftest-wlock-hardirq.h"
763GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
764
765#include "locking-selftest-spin-softirq.h"
766GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
767
768#include "locking-selftest-rlock-softirq.h"
769GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
770
771#include "locking-selftest-wlock-softirq.h"
772GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
773
774#undef E1
775#undef E2
776#undef E3
777
778/*
779 * read-lock / write-lock irq inversion.
780 *
781 * Deadlock scenario:
782 *
783 * CPU#1 is at #1, i.e. it has write-locked A, but has not
784 * taken B yet.
785 *
786 * CPU#2 is at #2, i.e. it has locked B.
787 *
788 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
789 *
790 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
791 * will spin on A.
792 */
793
794#define E1() \
795 \
796 IRQ_DISABLE(); \
797 WL(A); \
798 LOCK(B); \
799 UNLOCK(B); \
800 WU(A); \
801 IRQ_ENABLE();
802
803#define E2() \
804 \
805 LOCK(B); \
806 UNLOCK(B);
807
808#define E3() \
809 \
810 IRQ_ENTER(); \
811 RL(A); \
812 RU(A); \
813 IRQ_EXIT();
814
815/*
816 * Generate 36 testcases:
817 */
818#include "locking-selftest-spin-hardirq.h"
819GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
820
821#include "locking-selftest-rlock-hardirq.h"
822GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
823
824#include "locking-selftest-wlock-hardirq.h"
825GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
826
827#include "locking-selftest-spin-softirq.h"
828GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
829
830#include "locking-selftest-rlock-softirq.h"
831GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
832
833#include "locking-selftest-wlock-softirq.h"
834GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
835
836#undef E1
837#undef E2
838#undef E3
839
840/*
841 * read-lock / write-lock recursion that is actually safe.
842 */
843
844#define E1() \
845 \
846 IRQ_DISABLE(); \
847 WL(A); \
848 WU(A); \
849 IRQ_ENABLE();
850
851#define E2() \
852 \
853 RL(A); \
854 RU(A); \
855
856#define E3() \
857 \
858 IRQ_ENTER(); \
859 RL(A); \
860 L(B); \
861 U(B); \
862 RU(A); \
863 IRQ_EXIT();
864
865/*
866 * Generate 12 testcases:
867 */
868#include "locking-selftest-hardirq.h"
869GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
870
871#include "locking-selftest-softirq.h"
872GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
873
874#undef E1
875#undef E2
876#undef E3
877
878/*
879 * read-lock / write-lock recursion that is unsafe.
880 */
881
882#define E1() \
883 \
884 IRQ_DISABLE(); \
885 L(B); \
886 WL(A); \
887 WU(A); \
888 U(B); \
889 IRQ_ENABLE();
890
891#define E2() \
892 \
893 RL(A); \
894 RU(A); \
895
896#define E3() \
897 \
898 IRQ_ENTER(); \
899 L(B); \
900 U(B); \
901 IRQ_EXIT();
902
903/*
904 * Generate 12 testcases:
905 */
906#include "locking-selftest-hardirq.h"
907// GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
908
909#include "locking-selftest-softirq.h"
910// GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
911
cae2ed9a
IM
912#ifdef CONFIG_DEBUG_LOCK_ALLOC
913# define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
914# define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
915# define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
916# define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1de99445 917# define I_WW(x) lockdep_reset_lock(&x.dep_map)
cae2ed9a
IM
918#else
919# define I_SPINLOCK(x)
920# define I_RWLOCK(x)
921# define I_MUTEX(x)
922# define I_RWSEM(x)
1de99445 923# define I_WW(x)
cae2ed9a
IM
924#endif
925
926#define I1(x) \
927 do { \
928 I_SPINLOCK(x); \
929 I_RWLOCK(x); \
930 I_MUTEX(x); \
931 I_RWSEM(x); \
932 } while (0)
933
934#define I2(x) \
935 do { \
9fb1b90c 936 raw_spin_lock_init(&lock_##x); \
cae2ed9a
IM
937 rwlock_init(&rwlock_##x); \
938 mutex_init(&mutex_##x); \
939 init_rwsem(&rwsem_##x); \
940 } while (0)
941
942static void reset_locks(void)
943{
944 local_irq_disable();
1de99445
ML
945 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
946 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
947
cae2ed9a
IM
948 I1(A); I1(B); I1(C); I1(D);
949 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
1de99445 950 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base);
cae2ed9a
IM
951 lockdep_reset();
952 I2(A); I2(B); I2(C); I2(D);
953 init_shared_classes();
1de99445
ML
954
955 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep);
956 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
957 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
958 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
cae2ed9a
IM
959 local_irq_enable();
960}
961
962#undef I
963
964static int testcase_total;
965static int testcase_successes;
966static int expected_testcase_failures;
967static int unexpected_testcase_failures;
968
969static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
970{
971 unsigned long saved_preempt_count = preempt_count();
cae2ed9a
IM
972
973 WARN_ON(irqs_disabled());
974
975 testcase_fn();
976 /*
977 * Filter out expected failures:
978 */
1de99445 979 if (debug_locks != expected) {
cae2ed9a 980#ifndef CONFIG_PROVE_LOCKING
1de99445
ML
981 expected_testcase_failures++;
982 printk("failed|");
983#else
984 unexpected_testcase_failures++;
985 printk("FAILED|");
986
987 dump_stack();
cae2ed9a 988#endif
cae2ed9a
IM
989 } else {
990 testcase_successes++;
991 printk(" ok |");
992 }
993 testcase_total++;
994
995 if (debug_locks_verbose)
996 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
997 lockclass_mask, debug_locks, expected);
998 /*
999 * Some tests (e.g. double-unlock) might corrupt the preemption
1000 * count, so restore it:
1001 */
1002 preempt_count() = saved_preempt_count;
1003#ifdef CONFIG_TRACE_IRQFLAGS
1004 if (softirq_count())
1005 current->softirqs_enabled = 0;
1006 else
1007 current->softirqs_enabled = 1;
1008#endif
1009
1010 reset_locks();
1011}
1012
1013static inline void print_testname(const char *testname)
1014{
1015 printk("%33s:", testname);
1016}
1017
1018#define DO_TESTCASE_1(desc, name, nr) \
1019 print_testname(desc"/"#nr); \
1020 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1021 printk("\n");
1022
1023#define DO_TESTCASE_1B(desc, name, nr) \
1024 print_testname(desc"/"#nr); \
1025 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1026 printk("\n");
1027
1028#define DO_TESTCASE_3(desc, name, nr) \
1029 print_testname(desc"/"#nr); \
1030 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1031 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1032 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1033 printk("\n");
1034
1035#define DO_TESTCASE_3RW(desc, name, nr) \
1036 print_testname(desc"/"#nr); \
1037 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1038 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1039 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1040 printk("\n");
1041
1042#define DO_TESTCASE_6(desc, name) \
1043 print_testname(desc); \
1044 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1045 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1046 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1047 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1048 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1049 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1050 printk("\n");
1051
1052#define DO_TESTCASE_6_SUCCESS(desc, name) \
1053 print_testname(desc); \
1054 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1055 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1056 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1057 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1058 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1059 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1060 printk("\n");
1061
1062/*
1063 * 'read' variant: rlocks must not trigger.
1064 */
1065#define DO_TESTCASE_6R(desc, name) \
1066 print_testname(desc); \
1067 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1068 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1069 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1070 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1071 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1072 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1073 printk("\n");
1074
1075#define DO_TESTCASE_2I(desc, name, nr) \
1076 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1077 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1078
1079#define DO_TESTCASE_2IB(desc, name, nr) \
1080 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1081 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1082
1083#define DO_TESTCASE_6I(desc, name, nr) \
1084 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1085 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1086
1087#define DO_TESTCASE_6IRW(desc, name, nr) \
1088 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1089 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1090
1091#define DO_TESTCASE_2x3(desc, name) \
1092 DO_TESTCASE_3(desc, name, 12); \
1093 DO_TESTCASE_3(desc, name, 21);
1094
1095#define DO_TESTCASE_2x6(desc, name) \
1096 DO_TESTCASE_6I(desc, name, 12); \
1097 DO_TESTCASE_6I(desc, name, 21);
1098
1099#define DO_TESTCASE_6x2(desc, name) \
1100 DO_TESTCASE_2I(desc, name, 123); \
1101 DO_TESTCASE_2I(desc, name, 132); \
1102 DO_TESTCASE_2I(desc, name, 213); \
1103 DO_TESTCASE_2I(desc, name, 231); \
1104 DO_TESTCASE_2I(desc, name, 312); \
1105 DO_TESTCASE_2I(desc, name, 321);
1106
1107#define DO_TESTCASE_6x2B(desc, name) \
1108 DO_TESTCASE_2IB(desc, name, 123); \
1109 DO_TESTCASE_2IB(desc, name, 132); \
1110 DO_TESTCASE_2IB(desc, name, 213); \
1111 DO_TESTCASE_2IB(desc, name, 231); \
1112 DO_TESTCASE_2IB(desc, name, 312); \
1113 DO_TESTCASE_2IB(desc, name, 321);
1114
1115#define DO_TESTCASE_6x6(desc, name) \
1116 DO_TESTCASE_6I(desc, name, 123); \
1117 DO_TESTCASE_6I(desc, name, 132); \
1118 DO_TESTCASE_6I(desc, name, 213); \
1119 DO_TESTCASE_6I(desc, name, 231); \
1120 DO_TESTCASE_6I(desc, name, 312); \
1121 DO_TESTCASE_6I(desc, name, 321);
1122
1123#define DO_TESTCASE_6x6RW(desc, name) \
1124 DO_TESTCASE_6IRW(desc, name, 123); \
1125 DO_TESTCASE_6IRW(desc, name, 132); \
1126 DO_TESTCASE_6IRW(desc, name, 213); \
1127 DO_TESTCASE_6IRW(desc, name, 231); \
1128 DO_TESTCASE_6IRW(desc, name, 312); \
1129 DO_TESTCASE_6IRW(desc, name, 321);
1130
1de99445
ML
1131static void ww_test_fail_acquire(void)
1132{
1133 int ret;
1134
1135 WWAI(&t);
1136 t.stamp++;
1137
1138 ret = WWL(&o, &t);
1139
1140 if (WARN_ON(!o.ctx) ||
1141 WARN_ON(ret))
1142 return;
1143
1144 /* No lockdep test, pure API */
1145 ret = WWL(&o, &t);
1146 WARN_ON(ret != -EALREADY);
1147
1148 ret = WWT(&o);
1149 WARN_ON(ret);
1150
1151 t2 = t;
1152 t2.stamp++;
1153 ret = WWL(&o, &t2);
1154 WARN_ON(ret != -EDEADLK);
1155 WWU(&o);
1156
1157 if (WWT(&o))
1158 WWU(&o);
1159#ifdef CONFIG_DEBUG_LOCK_ALLOC
1160 else
1161 DEBUG_LOCKS_WARN_ON(1);
1162#endif
1163}
1164
1165static void ww_test_two_contexts(void)
1166{
1167 WWAI(&t);
1168 WWAI(&t2);
1169}
1170
1171static void ww_test_diff_class(void)
1172{
1173 WWAI(&t);
1174#ifdef CONFIG_DEBUG_MUTEXES
1175 t.ww_class = NULL;
1176#endif
1177 WWL(&o, &t);
1178}
1179
1180static void ww_test_context_done_twice(void)
1181{
1182 WWAI(&t);
1183 WWAD(&t);
1184 WWAD(&t);
1185 WWAF(&t);
1186}
1187
1188static void ww_test_context_unlock_twice(void)
1189{
1190 WWAI(&t);
1191 WWAD(&t);
1192 WWAF(&t);
1193 WWAF(&t);
1194}
1195
1196static void ww_test_context_fini_early(void)
1197{
1198 WWAI(&t);
1199 WWL(&o, &t);
1200 WWAD(&t);
1201 WWAF(&t);
1202}
1203
1204static void ww_test_context_lock_after_done(void)
1205{
1206 WWAI(&t);
1207 WWAD(&t);
1208 WWL(&o, &t);
1209}
1210
1211static void ww_test_object_unlock_twice(void)
1212{
1213 WWL1(&o);
1214 WWU(&o);
1215 WWU(&o);
1216}
1217
1218static void ww_test_object_lock_unbalanced(void)
1219{
1220 WWAI(&t);
1221 WWL(&o, &t);
1222 t.acquired = 0;
1223 WWU(&o);
1224 WWAF(&t);
1225}
1226
1227static void ww_test_object_lock_stale_context(void)
1228{
1229 WWAI(&t);
1230 o.ctx = &t2;
1231 WWL(&o, &t);
1232}
1233
1234static void ww_test_spin_nest_unlocked(void)
1235{
1236 raw_spin_lock_nest_lock(&lock_A, &o.base);
1237 U(A);
1238}
1239
1240static void ww_test_unneeded_slow(void)
1241{
1242 WWAI(&t);
1243
1244 ww_mutex_lock_slow(&o, &t);
1245}
1246
1247static void ww_test_context_block(void)
1248{
1249 int ret;
1250
1251 WWAI(&t);
1252
1253 ret = WWL(&o, &t);
1254 WARN_ON(ret);
1255 WWL1(&o2);
1256}
1257
1258static void ww_test_context_try(void)
1259{
1260 int ret;
1261
1262 WWAI(&t);
1263
1264 ret = WWL(&o, &t);
1265 WARN_ON(ret);
1266
1267 ret = WWT(&o2);
1268 WARN_ON(!ret);
1269 WWU(&o2);
1270 WWU(&o);
1271}
1272
1273static void ww_test_context_context(void)
1274{
1275 int ret;
1276
1277 WWAI(&t);
1278
1279 ret = WWL(&o, &t);
1280 WARN_ON(ret);
1281
1282 ret = WWL(&o2, &t);
1283 WARN_ON(ret);
1284
1285 WWU(&o2);
1286 WWU(&o);
1287}
1288
1289static void ww_test_try_block(void)
1290{
1291 bool ret;
1292
1293 ret = WWT(&o);
1294 WARN_ON(!ret);
1295
1296 WWL1(&o2);
1297 WWU(&o2);
1298 WWU(&o);
1299}
1300
1301static void ww_test_try_try(void)
1302{
1303 bool ret;
1304
1305 ret = WWT(&o);
1306 WARN_ON(!ret);
1307 ret = WWT(&o2);
1308 WARN_ON(!ret);
1309 WWU(&o2);
1310 WWU(&o);
1311}
1312
1313static void ww_test_try_context(void)
1314{
1315 int ret;
1316
1317 ret = WWT(&o);
1318 WARN_ON(!ret);
1319
1320 WWAI(&t);
1321
1322 ret = WWL(&o2, &t);
1323 WARN_ON(ret);
1324}
1325
1326static void ww_test_block_block(void)
1327{
1328 WWL1(&o);
1329 WWL1(&o2);
1330}
1331
1332static void ww_test_block_try(void)
1333{
1334 bool ret;
1335
1336 WWL1(&o);
1337 ret = WWT(&o2);
1338 WARN_ON(!ret);
1339}
1340
1341static void ww_test_block_context(void)
1342{
1343 int ret;
1344
1345 WWL1(&o);
1346 WWAI(&t);
1347
1348 ret = WWL(&o2, &t);
1349 WARN_ON(ret);
1350}
1351
1352static void ww_test_spin_block(void)
1353{
1354 L(A);
1355 U(A);
1356
1357 WWL1(&o);
1358 L(A);
1359 U(A);
1360 WWU(&o);
1361
1362 L(A);
1363 WWL1(&o);
1364 WWU(&o);
1365 U(A);
1366}
1367
1368static void ww_test_spin_try(void)
1369{
1370 bool ret;
1371
1372 L(A);
1373 U(A);
1374
1375 ret = WWT(&o);
1376 WARN_ON(!ret);
1377 L(A);
1378 U(A);
1379 WWU(&o);
1380
1381 L(A);
1382 ret = WWT(&o);
1383 WARN_ON(!ret);
1384 WWU(&o);
1385 U(A);
1386}
1387
1388static void ww_test_spin_context(void)
1389{
1390 int ret;
1391
1392 L(A);
1393 U(A);
1394
1395 WWAI(&t);
1396
1397 ret = WWL(&o, &t);
1398 WARN_ON(ret);
1399 L(A);
1400 U(A);
1401 WWU(&o);
1402
1403 L(A);
1404 ret = WWL(&o, &t);
1405 WARN_ON(ret);
1406 WWU(&o);
1407 U(A);
1408}
1409
1410static void ww_tests(void)
1411{
1412 printk(" --------------------------------------------------------------------------\n");
1413 printk(" | Wound/wait tests |\n");
1414 printk(" ---------------------\n");
1415
1416 print_testname("ww api failures");
1417 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1418 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1419 printk("\n");
1420
1421 print_testname("ww contexts mixing");
1422 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1423 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1424 printk("\n");
1425
1426 print_testname("finishing ww context");
1427 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1428 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1429 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1430 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1431 printk("\n");
1432
1433 print_testname("locking mismatches");
1434 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1435 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1436 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1437 printk("\n");
1438
1439 print_testname("spinlock nest unlocked");
1440 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1441 printk("\n");
1442
1443 printk(" -----------------------------------------------------\n");
1444 printk(" |block | try |context|\n");
1445 printk(" -----------------------------------------------------\n");
1446
1447 print_testname("context");
1448 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1449 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1450 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1451 printk("\n");
1452
1453 print_testname("try");
1454 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1455 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1456 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1457 printk("\n");
1458
1459 print_testname("block");
1460 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1461 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1462 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1463 printk("\n");
1464
1465 print_testname("spinlock");
1466 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1467 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1468 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1469 printk("\n");
1470}
cae2ed9a
IM
1471
1472void locking_selftest(void)
1473{
1474 /*
1475 * Got a locking failure before the selftest ran?
1476 */
1477 if (!debug_locks) {
1478 printk("----------------------------------\n");
1479 printk("| Locking API testsuite disabled |\n");
1480 printk("----------------------------------\n");
1481 return;
1482 }
1483
1484 /*
1485 * Run the testsuite:
1486 */
1487 printk("------------------------\n");
1488 printk("| Locking API testsuite:\n");
1489 printk("----------------------------------------------------------------------------\n");
1490 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1491 printk(" --------------------------------------------------------------------------\n");
1492
1493 init_shared_classes();
1494 debug_locks_silent = !debug_locks_verbose;
1495
6c9076ec 1496 DO_TESTCASE_6R("A-A deadlock", AA);
cae2ed9a
IM
1497 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1498 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1499 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1500 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1501 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1502 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1503 DO_TESTCASE_6("double unlock", double_unlock);
1504 DO_TESTCASE_6("initialize held", init_held);
1505 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1506
1507 printk(" --------------------------------------------------------------------------\n");
1508 print_testname("recursive read-lock");
1509 printk(" |");
1510 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1511 printk(" |");
1512 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1513 printk("\n");
1514
1515 print_testname("recursive read-lock #2");
1516 printk(" |");
6c9076ec 1517 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
cae2ed9a
IM
1518 printk(" |");
1519 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1520 printk("\n");
1521
1522 print_testname("mixed read-write-lock");
1523 printk(" |");
1524 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1525 printk(" |");
1526 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1527 printk("\n");
1528
1529 print_testname("mixed write-read-lock");
1530 printk(" |");
1531 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1532 printk(" |");
1533 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1534 printk("\n");
1535
1536 printk(" --------------------------------------------------------------------------\n");
1537
1538 /*
1539 * irq-context testcases:
1540 */
1541 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1542 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1543 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1544 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1545 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1546 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1547
1548 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1549// DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1550
1de99445
ML
1551 ww_tests();
1552
cae2ed9a
IM
1553 if (unexpected_testcase_failures) {
1554 printk("-----------------------------------------------------------------\n");
1555 debug_locks = 0;
1556 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1557 unexpected_testcase_failures, testcase_total);
1558 printk("-----------------------------------------------------------------\n");
1559 } else if (expected_testcase_failures && testcase_successes) {
1560 printk("--------------------------------------------------------\n");
1561 printk("%3d out of %3d testcases failed, as expected. |\n",
1562 expected_testcase_failures, testcase_total);
1563 printk("----------------------------------------------------\n");
1564 debug_locks = 1;
1565 } else if (expected_testcase_failures && !testcase_successes) {
1566 printk("--------------------------------------------------------\n");
1567 printk("All %3d testcases failed, as expected. |\n",
1568 expected_testcase_failures);
1569 printk("----------------------------------------\n");
1570 debug_locks = 1;
1571 } else {
1572 printk("-------------------------------------------------------\n");
1573 printk("Good, all %3d testcases passed! |\n",
1574 testcase_successes);
1575 printk("---------------------------------\n");
1576 debug_locks = 1;
1577 }
1578 debug_locks_silent = 0;
1579}