drm/i915: We implement WaFbcAsynchFlipDisableFbcQueue on ilk and snb
[deliverable/linux.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;
f3cf139e 50static struct ww_mutex o, o2, o3;
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);
f3cf139e 950 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
cae2ed9a
IM
951 lockdep_reset();
952 I2(A); I2(B); I2(C); I2(D);
953 init_shared_classes();
1de99445 954
f3cf139e 955 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1de99445
ML
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 */
979#ifndef CONFIG_PROVE_LOCKING
166989e3 980 if (expected == FAILURE && debug_locks) {
1de99445
ML
981 expected_testcase_failures++;
982 printk("failed|");
166989e3
ML
983 }
984 else
985#endif
986 if (debug_locks != expected) {
1de99445
ML
987 unexpected_testcase_failures++;
988 printk("FAILED|");
989
990 dump_stack();
cae2ed9a
IM
991 } else {
992 testcase_successes++;
993 printk(" ok |");
994 }
995 testcase_total++;
996
997 if (debug_locks_verbose)
998 printk(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
999 lockclass_mask, debug_locks, expected);
1000 /*
1001 * Some tests (e.g. double-unlock) might corrupt the preemption
1002 * count, so restore it:
1003 */
1004 preempt_count() = saved_preempt_count;
1005#ifdef CONFIG_TRACE_IRQFLAGS
1006 if (softirq_count())
1007 current->softirqs_enabled = 0;
1008 else
1009 current->softirqs_enabled = 1;
1010#endif
1011
1012 reset_locks();
1013}
1014
1015static inline void print_testname(const char *testname)
1016{
1017 printk("%33s:", testname);
1018}
1019
1020#define DO_TESTCASE_1(desc, name, nr) \
1021 print_testname(desc"/"#nr); \
1022 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1023 printk("\n");
1024
1025#define DO_TESTCASE_1B(desc, name, nr) \
1026 print_testname(desc"/"#nr); \
1027 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1028 printk("\n");
1029
1030#define DO_TESTCASE_3(desc, name, nr) \
1031 print_testname(desc"/"#nr); \
1032 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1033 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1034 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1035 printk("\n");
1036
1037#define DO_TESTCASE_3RW(desc, name, nr) \
1038 print_testname(desc"/"#nr); \
1039 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1040 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1041 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1042 printk("\n");
1043
1044#define DO_TESTCASE_6(desc, name) \
1045 print_testname(desc); \
1046 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1047 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1048 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1049 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1050 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1051 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1052 printk("\n");
1053
1054#define DO_TESTCASE_6_SUCCESS(desc, name) \
1055 print_testname(desc); \
1056 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1057 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1058 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1059 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1060 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1061 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
1062 printk("\n");
1063
1064/*
1065 * 'read' variant: rlocks must not trigger.
1066 */
1067#define DO_TESTCASE_6R(desc, name) \
1068 print_testname(desc); \
1069 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1070 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1071 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1072 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1073 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1074 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
1075 printk("\n");
1076
1077#define DO_TESTCASE_2I(desc, name, nr) \
1078 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1079 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1080
1081#define DO_TESTCASE_2IB(desc, name, nr) \
1082 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1083 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1084
1085#define DO_TESTCASE_6I(desc, name, nr) \
1086 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1087 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1088
1089#define DO_TESTCASE_6IRW(desc, name, nr) \
1090 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1091 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1092
1093#define DO_TESTCASE_2x3(desc, name) \
1094 DO_TESTCASE_3(desc, name, 12); \
1095 DO_TESTCASE_3(desc, name, 21);
1096
1097#define DO_TESTCASE_2x6(desc, name) \
1098 DO_TESTCASE_6I(desc, name, 12); \
1099 DO_TESTCASE_6I(desc, name, 21);
1100
1101#define DO_TESTCASE_6x2(desc, name) \
1102 DO_TESTCASE_2I(desc, name, 123); \
1103 DO_TESTCASE_2I(desc, name, 132); \
1104 DO_TESTCASE_2I(desc, name, 213); \
1105 DO_TESTCASE_2I(desc, name, 231); \
1106 DO_TESTCASE_2I(desc, name, 312); \
1107 DO_TESTCASE_2I(desc, name, 321);
1108
1109#define DO_TESTCASE_6x2B(desc, name) \
1110 DO_TESTCASE_2IB(desc, name, 123); \
1111 DO_TESTCASE_2IB(desc, name, 132); \
1112 DO_TESTCASE_2IB(desc, name, 213); \
1113 DO_TESTCASE_2IB(desc, name, 231); \
1114 DO_TESTCASE_2IB(desc, name, 312); \
1115 DO_TESTCASE_2IB(desc, name, 321);
1116
1117#define DO_TESTCASE_6x6(desc, name) \
1118 DO_TESTCASE_6I(desc, name, 123); \
1119 DO_TESTCASE_6I(desc, name, 132); \
1120 DO_TESTCASE_6I(desc, name, 213); \
1121 DO_TESTCASE_6I(desc, name, 231); \
1122 DO_TESTCASE_6I(desc, name, 312); \
1123 DO_TESTCASE_6I(desc, name, 321);
1124
1125#define DO_TESTCASE_6x6RW(desc, name) \
1126 DO_TESTCASE_6IRW(desc, name, 123); \
1127 DO_TESTCASE_6IRW(desc, name, 132); \
1128 DO_TESTCASE_6IRW(desc, name, 213); \
1129 DO_TESTCASE_6IRW(desc, name, 231); \
1130 DO_TESTCASE_6IRW(desc, name, 312); \
1131 DO_TESTCASE_6IRW(desc, name, 321);
1132
1de99445
ML
1133static void ww_test_fail_acquire(void)
1134{
1135 int ret;
1136
1137 WWAI(&t);
1138 t.stamp++;
1139
1140 ret = WWL(&o, &t);
1141
1142 if (WARN_ON(!o.ctx) ||
1143 WARN_ON(ret))
1144 return;
1145
1146 /* No lockdep test, pure API */
1147 ret = WWL(&o, &t);
1148 WARN_ON(ret != -EALREADY);
1149
1150 ret = WWT(&o);
1151 WARN_ON(ret);
1152
1153 t2 = t;
1154 t2.stamp++;
1155 ret = WWL(&o, &t2);
1156 WARN_ON(ret != -EDEADLK);
1157 WWU(&o);
1158
1159 if (WWT(&o))
1160 WWU(&o);
1161#ifdef CONFIG_DEBUG_LOCK_ALLOC
1162 else
1163 DEBUG_LOCKS_WARN_ON(1);
1164#endif
1165}
1166
2fe3d4b1
ML
1167static void ww_test_normal(void)
1168{
1169 int ret;
1170
1171 WWAI(&t);
1172
1173 /*
1174 * None of the ww_mutex codepaths should be taken in the 'normal'
1175 * mutex calls. The easiest way to verify this is by using the
1176 * normal mutex calls, and making sure o.ctx is unmodified.
1177 */
1178
1179 /* mutex_lock (and indirectly, mutex_lock_nested) */
1180 o.ctx = (void *)~0UL;
1181 mutex_lock(&o.base);
1182 mutex_unlock(&o.base);
1183 WARN_ON(o.ctx != (void *)~0UL);
1184
1185 /* mutex_lock_interruptible (and *_nested) */
1186 o.ctx = (void *)~0UL;
1187 ret = mutex_lock_interruptible(&o.base);
1188 if (!ret)
1189 mutex_unlock(&o.base);
1190 else
1191 WARN_ON(1);
1192 WARN_ON(o.ctx != (void *)~0UL);
1193
1194 /* mutex_lock_killable (and *_nested) */
1195 o.ctx = (void *)~0UL;
1196 ret = mutex_lock_killable(&o.base);
1197 if (!ret)
1198 mutex_unlock(&o.base);
1199 else
1200 WARN_ON(1);
1201 WARN_ON(o.ctx != (void *)~0UL);
1202
1203 /* trylock, succeeding */
1204 o.ctx = (void *)~0UL;
1205 ret = mutex_trylock(&o.base);
1206 WARN_ON(!ret);
1207 if (ret)
1208 mutex_unlock(&o.base);
1209 else
1210 WARN_ON(1);
1211 WARN_ON(o.ctx != (void *)~0UL);
1212
1213 /* trylock, failing */
1214 o.ctx = (void *)~0UL;
1215 mutex_lock(&o.base);
1216 ret = mutex_trylock(&o.base);
1217 WARN_ON(ret);
1218 mutex_unlock(&o.base);
1219 WARN_ON(o.ctx != (void *)~0UL);
1220
1221 /* nest_lock */
1222 o.ctx = (void *)~0UL;
1223 mutex_lock_nest_lock(&o.base, &t);
1224 mutex_unlock(&o.base);
1225 WARN_ON(o.ctx != (void *)~0UL);
1226}
1227
1de99445
ML
1228static void ww_test_two_contexts(void)
1229{
1230 WWAI(&t);
1231 WWAI(&t2);
1232}
1233
1234static void ww_test_diff_class(void)
1235{
1236 WWAI(&t);
1237#ifdef CONFIG_DEBUG_MUTEXES
1238 t.ww_class = NULL;
1239#endif
1240 WWL(&o, &t);
1241}
1242
1243static void ww_test_context_done_twice(void)
1244{
1245 WWAI(&t);
1246 WWAD(&t);
1247 WWAD(&t);
1248 WWAF(&t);
1249}
1250
1251static void ww_test_context_unlock_twice(void)
1252{
1253 WWAI(&t);
1254 WWAD(&t);
1255 WWAF(&t);
1256 WWAF(&t);
1257}
1258
1259static void ww_test_context_fini_early(void)
1260{
1261 WWAI(&t);
1262 WWL(&o, &t);
1263 WWAD(&t);
1264 WWAF(&t);
1265}
1266
1267static void ww_test_context_lock_after_done(void)
1268{
1269 WWAI(&t);
1270 WWAD(&t);
1271 WWL(&o, &t);
1272}
1273
1274static void ww_test_object_unlock_twice(void)
1275{
1276 WWL1(&o);
1277 WWU(&o);
1278 WWU(&o);
1279}
1280
1281static void ww_test_object_lock_unbalanced(void)
1282{
1283 WWAI(&t);
1284 WWL(&o, &t);
1285 t.acquired = 0;
1286 WWU(&o);
1287 WWAF(&t);
1288}
1289
1290static void ww_test_object_lock_stale_context(void)
1291{
1292 WWAI(&t);
1293 o.ctx = &t2;
1294 WWL(&o, &t);
1295}
1296
f3cf139e
ML
1297static void ww_test_edeadlk_normal(void)
1298{
1299 int ret;
1300
1301 mutex_lock(&o2.base);
1302 o2.ctx = &t2;
1303 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1304
1305 WWAI(&t);
1306 t2 = t;
1307 t2.stamp--;
1308
1309 ret = WWL(&o, &t);
1310 WARN_ON(ret);
1311
1312 ret = WWL(&o2, &t);
1313 WARN_ON(ret != -EDEADLK);
1314
1315 o2.ctx = NULL;
1316 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1317 mutex_unlock(&o2.base);
1318 WWU(&o);
1319
1320 WWL(&o2, &t);
1321}
1322
1323static void ww_test_edeadlk_normal_slow(void)
1324{
1325 int ret;
1326
1327 mutex_lock(&o2.base);
1328 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1329 o2.ctx = &t2;
1330
1331 WWAI(&t);
1332 t2 = t;
1333 t2.stamp--;
1334
1335 ret = WWL(&o, &t);
1336 WARN_ON(ret);
1337
1338 ret = WWL(&o2, &t);
1339 WARN_ON(ret != -EDEADLK);
1340
1341 o2.ctx = NULL;
1342 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1343 mutex_unlock(&o2.base);
1344 WWU(&o);
1345
1346 ww_mutex_lock_slow(&o2, &t);
1347}
1348
1349static void ww_test_edeadlk_no_unlock(void)
1350{
1351 int ret;
1352
1353 mutex_lock(&o2.base);
1354 o2.ctx = &t2;
1355 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1356
1357 WWAI(&t);
1358 t2 = t;
1359 t2.stamp--;
1360
1361 ret = WWL(&o, &t);
1362 WARN_ON(ret);
1363
1364 ret = WWL(&o2, &t);
1365 WARN_ON(ret != -EDEADLK);
1366
1367 o2.ctx = NULL;
1368 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1369 mutex_unlock(&o2.base);
1370
1371 WWL(&o2, &t);
1372}
1373
1374static void ww_test_edeadlk_no_unlock_slow(void)
1375{
1376 int ret;
1377
1378 mutex_lock(&o2.base);
1379 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1380 o2.ctx = &t2;
1381
1382 WWAI(&t);
1383 t2 = t;
1384 t2.stamp--;
1385
1386 ret = WWL(&o, &t);
1387 WARN_ON(ret);
1388
1389 ret = WWL(&o2, &t);
1390 WARN_ON(ret != -EDEADLK);
1391
1392 o2.ctx = NULL;
1393 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1394 mutex_unlock(&o2.base);
1395
1396 ww_mutex_lock_slow(&o2, &t);
1397}
1398
1399static void ww_test_edeadlk_acquire_more(void)
1400{
1401 int ret;
1402
1403 mutex_lock(&o2.base);
1404 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1405 o2.ctx = &t2;
1406
1407 WWAI(&t);
1408 t2 = t;
1409 t2.stamp--;
1410
1411 ret = WWL(&o, &t);
1412 WARN_ON(ret);
1413
1414 ret = WWL(&o2, &t);
1415 WARN_ON(ret != -EDEADLK);
1416
1417 ret = WWL(&o3, &t);
1418}
1419
1420static void ww_test_edeadlk_acquire_more_slow(void)
1421{
1422 int ret;
1423
1424 mutex_lock(&o2.base);
1425 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1426 o2.ctx = &t2;
1427
1428 WWAI(&t);
1429 t2 = t;
1430 t2.stamp--;
1431
1432 ret = WWL(&o, &t);
1433 WARN_ON(ret);
1434
1435 ret = WWL(&o2, &t);
1436 WARN_ON(ret != -EDEADLK);
1437
1438 ww_mutex_lock_slow(&o3, &t);
1439}
1440
1441static void ww_test_edeadlk_acquire_more_edeadlk(void)
1442{
1443 int ret;
1444
1445 mutex_lock(&o2.base);
1446 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1447 o2.ctx = &t2;
1448
1449 mutex_lock(&o3.base);
1450 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1451 o3.ctx = &t2;
1452
1453 WWAI(&t);
1454 t2 = t;
1455 t2.stamp--;
1456
1457 ret = WWL(&o, &t);
1458 WARN_ON(ret);
1459
1460 ret = WWL(&o2, &t);
1461 WARN_ON(ret != -EDEADLK);
1462
1463 ret = WWL(&o3, &t);
1464 WARN_ON(ret != -EDEADLK);
1465}
1466
1467static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1468{
1469 int ret;
1470
1471 mutex_lock(&o2.base);
1472 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1473 o2.ctx = &t2;
1474
1475 mutex_lock(&o3.base);
1476 mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1477 o3.ctx = &t2;
1478
1479 WWAI(&t);
1480 t2 = t;
1481 t2.stamp--;
1482
1483 ret = WWL(&o, &t);
1484 WARN_ON(ret);
1485
1486 ret = WWL(&o2, &t);
1487 WARN_ON(ret != -EDEADLK);
1488
1489 ww_mutex_lock_slow(&o3, &t);
1490}
1491
1492static void ww_test_edeadlk_acquire_wrong(void)
1493{
1494 int ret;
1495
1496 mutex_lock(&o2.base);
1497 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1498 o2.ctx = &t2;
1499
1500 WWAI(&t);
1501 t2 = t;
1502 t2.stamp--;
1503
1504 ret = WWL(&o, &t);
1505 WARN_ON(ret);
1506
1507 ret = WWL(&o2, &t);
1508 WARN_ON(ret != -EDEADLK);
1509 if (!ret)
1510 WWU(&o2);
1511
1512 WWU(&o);
1513
1514 ret = WWL(&o3, &t);
1515}
1516
1517static void ww_test_edeadlk_acquire_wrong_slow(void)
1518{
1519 int ret;
1520
1521 mutex_lock(&o2.base);
1522 mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1523 o2.ctx = &t2;
1524
1525 WWAI(&t);
1526 t2 = t;
1527 t2.stamp--;
1528
1529 ret = WWL(&o, &t);
1530 WARN_ON(ret);
1531
1532 ret = WWL(&o2, &t);
1533 WARN_ON(ret != -EDEADLK);
1534 if (!ret)
1535 WWU(&o2);
1536
1537 WWU(&o);
1538
1539 ww_mutex_lock_slow(&o3, &t);
1540}
1541
1de99445
ML
1542static void ww_test_spin_nest_unlocked(void)
1543{
1544 raw_spin_lock_nest_lock(&lock_A, &o.base);
1545 U(A);
1546}
1547
1548static void ww_test_unneeded_slow(void)
1549{
1550 WWAI(&t);
1551
1552 ww_mutex_lock_slow(&o, &t);
1553}
1554
1555static void ww_test_context_block(void)
1556{
1557 int ret;
1558
1559 WWAI(&t);
1560
1561 ret = WWL(&o, &t);
1562 WARN_ON(ret);
1563 WWL1(&o2);
1564}
1565
1566static void ww_test_context_try(void)
1567{
1568 int ret;
1569
1570 WWAI(&t);
1571
1572 ret = WWL(&o, &t);
1573 WARN_ON(ret);
1574
1575 ret = WWT(&o2);
1576 WARN_ON(!ret);
1577 WWU(&o2);
1578 WWU(&o);
1579}
1580
1581static void ww_test_context_context(void)
1582{
1583 int ret;
1584
1585 WWAI(&t);
1586
1587 ret = WWL(&o, &t);
1588 WARN_ON(ret);
1589
1590 ret = WWL(&o2, &t);
1591 WARN_ON(ret);
1592
1593 WWU(&o2);
1594 WWU(&o);
1595}
1596
1597static void ww_test_try_block(void)
1598{
1599 bool ret;
1600
1601 ret = WWT(&o);
1602 WARN_ON(!ret);
1603
1604 WWL1(&o2);
1605 WWU(&o2);
1606 WWU(&o);
1607}
1608
1609static void ww_test_try_try(void)
1610{
1611 bool ret;
1612
1613 ret = WWT(&o);
1614 WARN_ON(!ret);
1615 ret = WWT(&o2);
1616 WARN_ON(!ret);
1617 WWU(&o2);
1618 WWU(&o);
1619}
1620
1621static void ww_test_try_context(void)
1622{
1623 int ret;
1624
1625 ret = WWT(&o);
1626 WARN_ON(!ret);
1627
1628 WWAI(&t);
1629
1630 ret = WWL(&o2, &t);
1631 WARN_ON(ret);
1632}
1633
1634static void ww_test_block_block(void)
1635{
1636 WWL1(&o);
1637 WWL1(&o2);
1638}
1639
1640static void ww_test_block_try(void)
1641{
1642 bool ret;
1643
1644 WWL1(&o);
1645 ret = WWT(&o2);
1646 WARN_ON(!ret);
1647}
1648
1649static void ww_test_block_context(void)
1650{
1651 int ret;
1652
1653 WWL1(&o);
1654 WWAI(&t);
1655
1656 ret = WWL(&o2, &t);
1657 WARN_ON(ret);
1658}
1659
1660static void ww_test_spin_block(void)
1661{
1662 L(A);
1663 U(A);
1664
1665 WWL1(&o);
1666 L(A);
1667 U(A);
1668 WWU(&o);
1669
1670 L(A);
1671 WWL1(&o);
1672 WWU(&o);
1673 U(A);
1674}
1675
1676static void ww_test_spin_try(void)
1677{
1678 bool ret;
1679
1680 L(A);
1681 U(A);
1682
1683 ret = WWT(&o);
1684 WARN_ON(!ret);
1685 L(A);
1686 U(A);
1687 WWU(&o);
1688
1689 L(A);
1690 ret = WWT(&o);
1691 WARN_ON(!ret);
1692 WWU(&o);
1693 U(A);
1694}
1695
1696static void ww_test_spin_context(void)
1697{
1698 int ret;
1699
1700 L(A);
1701 U(A);
1702
1703 WWAI(&t);
1704
1705 ret = WWL(&o, &t);
1706 WARN_ON(ret);
1707 L(A);
1708 U(A);
1709 WWU(&o);
1710
1711 L(A);
1712 ret = WWL(&o, &t);
1713 WARN_ON(ret);
1714 WWU(&o);
1715 U(A);
1716}
1717
1718static void ww_tests(void)
1719{
1720 printk(" --------------------------------------------------------------------------\n");
1721 printk(" | Wound/wait tests |\n");
1722 printk(" ---------------------\n");
1723
1724 print_testname("ww api failures");
1725 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
2fe3d4b1 1726 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1de99445
ML
1727 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1728 printk("\n");
1729
1730 print_testname("ww contexts mixing");
1731 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1732 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1733 printk("\n");
1734
1735 print_testname("finishing ww context");
1736 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1737 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1738 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1739 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1740 printk("\n");
1741
1742 print_testname("locking mismatches");
1743 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1744 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1745 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1746 printk("\n");
1747
f3cf139e
ML
1748 print_testname("EDEADLK handling");
1749 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1750 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1751 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1752 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1753 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1754 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1755 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1756 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1757 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1758 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1759 printk("\n");
1760
1de99445
ML
1761 print_testname("spinlock nest unlocked");
1762 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1763 printk("\n");
1764
1765 printk(" -----------------------------------------------------\n");
1766 printk(" |block | try |context|\n");
1767 printk(" -----------------------------------------------------\n");
1768
1769 print_testname("context");
1770 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1771 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1772 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1773 printk("\n");
1774
1775 print_testname("try");
1776 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1777 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1778 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1779 printk("\n");
1780
1781 print_testname("block");
1782 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1783 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1784 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1785 printk("\n");
1786
1787 print_testname("spinlock");
1788 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1789 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1790 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1791 printk("\n");
1792}
cae2ed9a
IM
1793
1794void locking_selftest(void)
1795{
1796 /*
1797 * Got a locking failure before the selftest ran?
1798 */
1799 if (!debug_locks) {
1800 printk("----------------------------------\n");
1801 printk("| Locking API testsuite disabled |\n");
1802 printk("----------------------------------\n");
1803 return;
1804 }
1805
1806 /*
1807 * Run the testsuite:
1808 */
1809 printk("------------------------\n");
1810 printk("| Locking API testsuite:\n");
1811 printk("----------------------------------------------------------------------------\n");
1812 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1813 printk(" --------------------------------------------------------------------------\n");
1814
1815 init_shared_classes();
1816 debug_locks_silent = !debug_locks_verbose;
1817
6c9076ec 1818 DO_TESTCASE_6R("A-A deadlock", AA);
cae2ed9a
IM
1819 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1820 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1821 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1822 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1823 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1824 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1825 DO_TESTCASE_6("double unlock", double_unlock);
1826 DO_TESTCASE_6("initialize held", init_held);
1827 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1828
1829 printk(" --------------------------------------------------------------------------\n");
1830 print_testname("recursive read-lock");
1831 printk(" |");
1832 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1833 printk(" |");
1834 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1835 printk("\n");
1836
1837 print_testname("recursive read-lock #2");
1838 printk(" |");
6c9076ec 1839 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
cae2ed9a
IM
1840 printk(" |");
1841 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1842 printk("\n");
1843
1844 print_testname("mixed read-write-lock");
1845 printk(" |");
1846 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1847 printk(" |");
1848 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1849 printk("\n");
1850
1851 print_testname("mixed write-read-lock");
1852 printk(" |");
1853 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1854 printk(" |");
1855 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1856 printk("\n");
1857
1858 printk(" --------------------------------------------------------------------------\n");
1859
1860 /*
1861 * irq-context testcases:
1862 */
1863 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1864 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1865 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1866 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1867 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1868 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1869
1870 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1871// DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1872
1de99445
ML
1873 ww_tests();
1874
cae2ed9a
IM
1875 if (unexpected_testcase_failures) {
1876 printk("-----------------------------------------------------------------\n");
1877 debug_locks = 0;
1878 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1879 unexpected_testcase_failures, testcase_total);
1880 printk("-----------------------------------------------------------------\n");
1881 } else if (expected_testcase_failures && testcase_successes) {
1882 printk("--------------------------------------------------------\n");
1883 printk("%3d out of %3d testcases failed, as expected. |\n",
1884 expected_testcase_failures, testcase_total);
1885 printk("----------------------------------------------------\n");
1886 debug_locks = 1;
1887 } else if (expected_testcase_failures && !testcase_successes) {
1888 printk("--------------------------------------------------------\n");
1889 printk("All %3d testcases failed, as expected. |\n",
1890 expected_testcase_failures);
1891 printk("----------------------------------------\n");
1892 debug_locks = 1;
1893 } else {
1894 printk("-------------------------------------------------------\n");
1895 printk("Good, all %3d testcases passed! |\n",
1896 testcase_successes);
1897 printk("---------------------------------\n");
1898 debug_locks = 1;
1899 }
1900 debug_locks_silent = 0;
1901}
This page took 0.612756 seconds and 5 git commands to generate.