blob: 37faefdbb6783f7744bf9381f1f49f16b83a5a69 [file] [log] [blame]
Ingo Molnarcae2ed92006-07-03 00:24:48 -07001/*
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>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070017#include <linux/lockdep.h>
Ingo Molnarcae2ed92006-07-03 00:24:48 -070018#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
Maarten Lankhorst1de99442013-06-20 13:31:24 +020029static DEFINE_WW_CLASS(ww_lockdep);
30
Ingo Molnarcae2ed92006-07-03 00:24:48 -070031static 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
Maarten Lankhorst1de99442013-06-20 13:31:24 +020047#define LOCKTYPE_WW 0x10
48
49static struct ww_acquire_ctx t, t2;
50static struct ww_mutex o, o2;
Ingo Molnarcae2ed92006-07-03 00:24:48 -070051
52/*
53 * Normal standalone locks, for the circular and irq-context
54 * dependency tests:
55 */
Yong Zhang9fb1b902012-04-16 15:01:55 +080056static DEFINE_RAW_SPINLOCK(lock_A);
57static DEFINE_RAW_SPINLOCK(lock_B);
58static DEFINE_RAW_SPINLOCK(lock_C);
59static DEFINE_RAW_SPINLOCK(lock_D);
Ingo Molnarcae2ed92006-07-03 00:24:48 -070060
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 */
Yong Zhang9fb1b902012-04-16 15:01:55 +080082static 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);
Ingo Molnarcae2ed92006-07-03 00:24:48 -070088
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 \
Yong Zhang9fb1b902012-04-16 15:01:55 +0800116init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
117 struct mutex *mutex, struct rw_semaphore *rwsem)\
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700118{ \
Yong Zhang9fb1b902012-04-16 15:01:55 +0800119 raw_spin_lock_init(lock); \
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700120 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(); \
Frederic Weisbeckerba9f2072011-05-20 02:09:54 +0200152 __irq_enter(); \
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700153 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(); \
Ingo Molnard820ac42009-03-13 01:30:40 +0100165 lockdep_softirq_enter(); \
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700166 WARN_ON(!in_softirq());
167
168#define SOFTIRQ_EXIT() \
Ingo Molnard820ac42009-03-13 01:30:40 +0100169 lockdep_softirq_exit(); \
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700170 local_irq_enable(); \
171 local_bh_enable();
172
173/*
174 * Shortcuts for lock/unlock API variants, to keep
175 * the testcases compact:
176 */
Yong Zhang9fb1b902012-04-16 15:01:55 +0800177#define L(x) raw_spin_lock(&lock_##x)
178#define U(x) raw_spin_unlock(&lock_##x)
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700179#define LU(x) L(x); U(x)
Yong Zhang9fb1b902012-04-16 15:01:55 +0800180#define SI(x) raw_spin_lock_init(&lock_##x)
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700181
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
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200202#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
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700216#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
Ingo Molnar6c9076e2006-07-03 00:24:51 -0700270 * allowed to recurse on the same lock class:
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700271 */
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);
Ingo Molnar6c9076e2006-07-03 00:24:51 -0700281 RL(X2); // this one should NOT fail
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700282}
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
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700912#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)
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200917# define I_WW(x) lockdep_reset_lock(&x.dep_map)
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700918#else
919# define I_SPINLOCK(x)
920# define I_RWLOCK(x)
921# define I_MUTEX(x)
922# define I_RWSEM(x)
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200923# define I_WW(x)
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700924#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 { \
Yong Zhang9fb1b902012-04-16 15:01:55 +0800936 raw_spin_lock_init(&lock_##x); \
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700937 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();
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200945 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
946 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
947
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700948 I1(A); I1(B); I1(C); I1(D);
949 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200950 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base);
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700951 lockdep_reset();
952 I2(A); I2(B); I2(C); I2(D);
953 init_shared_classes();
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200954
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));
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700959 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();
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700972
973 WARN_ON(irqs_disabled());
974
975 testcase_fn();
976 /*
977 * Filter out expected failures:
978 */
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700979 if (debug_locks != expected) {
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200980#ifndef CONFIG_PROVE_LOCKING
981 expected_testcase_failures++;
982 printk("failed|");
983#else
984 unexpected_testcase_failures++;
985 printk("FAILED|");
Ingo Molnar2ee91f12006-12-06 20:39:32 -0800986
Maarten Lankhorst1de99442013-06-20 13:31:24 +0200987 dump_stack();
988#endif
Ingo Molnarcae2ed92006-07-03 00:24:48 -0700989 } 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
Maarten Lankhorst1de99442013-06-20 13:31:24 +02001131static 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
Maarten Lankhorst2fe3d4b2013-06-20 13:31:30 +02001165static void ww_test_normal(void)
1166{
1167 int ret;
1168
1169 WWAI(&t);
1170
1171 /*
1172 * None of the ww_mutex codepaths should be taken in the 'normal'
1173 * mutex calls. The easiest way to verify this is by using the
1174 * normal mutex calls, and making sure o.ctx is unmodified.
1175 */
1176
1177 /* mutex_lock (and indirectly, mutex_lock_nested) */
1178 o.ctx = (void *)~0UL;
1179 mutex_lock(&o.base);
1180 mutex_unlock(&o.base);
1181 WARN_ON(o.ctx != (void *)~0UL);
1182
1183 /* mutex_lock_interruptible (and *_nested) */
1184 o.ctx = (void *)~0UL;
1185 ret = mutex_lock_interruptible(&o.base);
1186 if (!ret)
1187 mutex_unlock(&o.base);
1188 else
1189 WARN_ON(1);
1190 WARN_ON(o.ctx != (void *)~0UL);
1191
1192 /* mutex_lock_killable (and *_nested) */
1193 o.ctx = (void *)~0UL;
1194 ret = mutex_lock_killable(&o.base);
1195 if (!ret)
1196 mutex_unlock(&o.base);
1197 else
1198 WARN_ON(1);
1199 WARN_ON(o.ctx != (void *)~0UL);
1200
1201 /* trylock, succeeding */
1202 o.ctx = (void *)~0UL;
1203 ret = mutex_trylock(&o.base);
1204 WARN_ON(!ret);
1205 if (ret)
1206 mutex_unlock(&o.base);
1207 else
1208 WARN_ON(1);
1209 WARN_ON(o.ctx != (void *)~0UL);
1210
1211 /* trylock, failing */
1212 o.ctx = (void *)~0UL;
1213 mutex_lock(&o.base);
1214 ret = mutex_trylock(&o.base);
1215 WARN_ON(ret);
1216 mutex_unlock(&o.base);
1217 WARN_ON(o.ctx != (void *)~0UL);
1218
1219 /* nest_lock */
1220 o.ctx = (void *)~0UL;
1221 mutex_lock_nest_lock(&o.base, &t);
1222 mutex_unlock(&o.base);
1223 WARN_ON(o.ctx != (void *)~0UL);
1224}
1225
Maarten Lankhorst1de99442013-06-20 13:31:24 +02001226static void ww_test_two_contexts(void)
1227{
1228 WWAI(&t);
1229 WWAI(&t2);
1230}
1231
1232static void ww_test_diff_class(void)
1233{
1234 WWAI(&t);
1235#ifdef CONFIG_DEBUG_MUTEXES
1236 t.ww_class = NULL;
1237#endif
1238 WWL(&o, &t);
1239}
1240
1241static void ww_test_context_done_twice(void)
1242{
1243 WWAI(&t);
1244 WWAD(&t);
1245 WWAD(&t);
1246 WWAF(&t);
1247}
1248
1249static void ww_test_context_unlock_twice(void)
1250{
1251 WWAI(&t);
1252 WWAD(&t);
1253 WWAF(&t);
1254 WWAF(&t);
1255}
1256
1257static void ww_test_context_fini_early(void)
1258{
1259 WWAI(&t);
1260 WWL(&o, &t);
1261 WWAD(&t);
1262 WWAF(&t);
1263}
1264
1265static void ww_test_context_lock_after_done(void)
1266{
1267 WWAI(&t);
1268 WWAD(&t);
1269 WWL(&o, &t);
1270}
1271
1272static void ww_test_object_unlock_twice(void)
1273{
1274 WWL1(&o);
1275 WWU(&o);
1276 WWU(&o);
1277}
1278
1279static void ww_test_object_lock_unbalanced(void)
1280{
1281 WWAI(&t);
1282 WWL(&o, &t);
1283 t.acquired = 0;
1284 WWU(&o);
1285 WWAF(&t);
1286}
1287
1288static void ww_test_object_lock_stale_context(void)
1289{
1290 WWAI(&t);
1291 o.ctx = &t2;
1292 WWL(&o, &t);
1293}
1294
1295static void ww_test_spin_nest_unlocked(void)
1296{
1297 raw_spin_lock_nest_lock(&lock_A, &o.base);
1298 U(A);
1299}
1300
1301static void ww_test_unneeded_slow(void)
1302{
1303 WWAI(&t);
1304
1305 ww_mutex_lock_slow(&o, &t);
1306}
1307
1308static void ww_test_context_block(void)
1309{
1310 int ret;
1311
1312 WWAI(&t);
1313
1314 ret = WWL(&o, &t);
1315 WARN_ON(ret);
1316 WWL1(&o2);
1317}
1318
1319static void ww_test_context_try(void)
1320{
1321 int ret;
1322
1323 WWAI(&t);
1324
1325 ret = WWL(&o, &t);
1326 WARN_ON(ret);
1327
1328 ret = WWT(&o2);
1329 WARN_ON(!ret);
1330 WWU(&o2);
1331 WWU(&o);
1332}
1333
1334static void ww_test_context_context(void)
1335{
1336 int ret;
1337
1338 WWAI(&t);
1339
1340 ret = WWL(&o, &t);
1341 WARN_ON(ret);
1342
1343 ret = WWL(&o2, &t);
1344 WARN_ON(ret);
1345
1346 WWU(&o2);
1347 WWU(&o);
1348}
1349
1350static void ww_test_try_block(void)
1351{
1352 bool ret;
1353
1354 ret = WWT(&o);
1355 WARN_ON(!ret);
1356
1357 WWL1(&o2);
1358 WWU(&o2);
1359 WWU(&o);
1360}
1361
1362static void ww_test_try_try(void)
1363{
1364 bool ret;
1365
1366 ret = WWT(&o);
1367 WARN_ON(!ret);
1368 ret = WWT(&o2);
1369 WARN_ON(!ret);
1370 WWU(&o2);
1371 WWU(&o);
1372}
1373
1374static void ww_test_try_context(void)
1375{
1376 int ret;
1377
1378 ret = WWT(&o);
1379 WARN_ON(!ret);
1380
1381 WWAI(&t);
1382
1383 ret = WWL(&o2, &t);
1384 WARN_ON(ret);
1385}
1386
1387static void ww_test_block_block(void)
1388{
1389 WWL1(&o);
1390 WWL1(&o2);
1391}
1392
1393static void ww_test_block_try(void)
1394{
1395 bool ret;
1396
1397 WWL1(&o);
1398 ret = WWT(&o2);
1399 WARN_ON(!ret);
1400}
1401
1402static void ww_test_block_context(void)
1403{
1404 int ret;
1405
1406 WWL1(&o);
1407 WWAI(&t);
1408
1409 ret = WWL(&o2, &t);
1410 WARN_ON(ret);
1411}
1412
1413static void ww_test_spin_block(void)
1414{
1415 L(A);
1416 U(A);
1417
1418 WWL1(&o);
1419 L(A);
1420 U(A);
1421 WWU(&o);
1422
1423 L(A);
1424 WWL1(&o);
1425 WWU(&o);
1426 U(A);
1427}
1428
1429static void ww_test_spin_try(void)
1430{
1431 bool ret;
1432
1433 L(A);
1434 U(A);
1435
1436 ret = WWT(&o);
1437 WARN_ON(!ret);
1438 L(A);
1439 U(A);
1440 WWU(&o);
1441
1442 L(A);
1443 ret = WWT(&o);
1444 WARN_ON(!ret);
1445 WWU(&o);
1446 U(A);
1447}
1448
1449static void ww_test_spin_context(void)
1450{
1451 int ret;
1452
1453 L(A);
1454 U(A);
1455
1456 WWAI(&t);
1457
1458 ret = WWL(&o, &t);
1459 WARN_ON(ret);
1460 L(A);
1461 U(A);
1462 WWU(&o);
1463
1464 L(A);
1465 ret = WWL(&o, &t);
1466 WARN_ON(ret);
1467 WWU(&o);
1468 U(A);
1469}
1470
1471static void ww_tests(void)
1472{
1473 printk(" --------------------------------------------------------------------------\n");
1474 printk(" | Wound/wait tests |\n");
1475 printk(" ---------------------\n");
1476
1477 print_testname("ww api failures");
1478 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
Maarten Lankhorst2fe3d4b2013-06-20 13:31:30 +02001479 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
Maarten Lankhorst1de99442013-06-20 13:31:24 +02001480 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1481 printk("\n");
1482
1483 print_testname("ww contexts mixing");
1484 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1485 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1486 printk("\n");
1487
1488 print_testname("finishing ww context");
1489 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1490 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1491 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1492 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1493 printk("\n");
1494
1495 print_testname("locking mismatches");
1496 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1497 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1498 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1499 printk("\n");
1500
1501 print_testname("spinlock nest unlocked");
1502 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1503 printk("\n");
1504
1505 printk(" -----------------------------------------------------\n");
1506 printk(" |block | try |context|\n");
1507 printk(" -----------------------------------------------------\n");
1508
1509 print_testname("context");
1510 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1511 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1512 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1513 printk("\n");
1514
1515 print_testname("try");
1516 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1517 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1518 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1519 printk("\n");
1520
1521 print_testname("block");
1522 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1523 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1524 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1525 printk("\n");
1526
1527 print_testname("spinlock");
1528 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1529 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1530 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1531 printk("\n");
1532}
Ingo Molnarcae2ed92006-07-03 00:24:48 -07001533
1534void locking_selftest(void)
1535{
1536 /*
1537 * Got a locking failure before the selftest ran?
1538 */
1539 if (!debug_locks) {
1540 printk("----------------------------------\n");
1541 printk("| Locking API testsuite disabled |\n");
1542 printk("----------------------------------\n");
1543 return;
1544 }
1545
1546 /*
1547 * Run the testsuite:
1548 */
1549 printk("------------------------\n");
1550 printk("| Locking API testsuite:\n");
1551 printk("----------------------------------------------------------------------------\n");
1552 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
1553 printk(" --------------------------------------------------------------------------\n");
1554
1555 init_shared_classes();
1556 debug_locks_silent = !debug_locks_verbose;
1557
Ingo Molnar6c9076e2006-07-03 00:24:51 -07001558 DO_TESTCASE_6R("A-A deadlock", AA);
Ingo Molnarcae2ed92006-07-03 00:24:48 -07001559 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1560 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1561 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1562 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1563 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1564 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1565 DO_TESTCASE_6("double unlock", double_unlock);
1566 DO_TESTCASE_6("initialize held", init_held);
1567 DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1568
1569 printk(" --------------------------------------------------------------------------\n");
1570 print_testname("recursive read-lock");
1571 printk(" |");
1572 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1573 printk(" |");
1574 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1575 printk("\n");
1576
1577 print_testname("recursive read-lock #2");
1578 printk(" |");
Ingo Molnar6c9076e2006-07-03 00:24:51 -07001579 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
Ingo Molnarcae2ed92006-07-03 00:24:48 -07001580 printk(" |");
1581 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1582 printk("\n");
1583
1584 print_testname("mixed read-write-lock");
1585 printk(" |");
1586 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1587 printk(" |");
1588 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1589 printk("\n");
1590
1591 print_testname("mixed write-read-lock");
1592 printk(" |");
1593 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1594 printk(" |");
1595 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1596 printk("\n");
1597
1598 printk(" --------------------------------------------------------------------------\n");
1599
1600 /*
1601 * irq-context testcases:
1602 */
1603 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1604 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1605 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1606 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1607 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1608 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1609
1610 DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1611// DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1612
Maarten Lankhorst1de99442013-06-20 13:31:24 +02001613 ww_tests();
1614
Ingo Molnarcae2ed92006-07-03 00:24:48 -07001615 if (unexpected_testcase_failures) {
1616 printk("-----------------------------------------------------------------\n");
1617 debug_locks = 0;
1618 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1619 unexpected_testcase_failures, testcase_total);
1620 printk("-----------------------------------------------------------------\n");
1621 } else if (expected_testcase_failures && testcase_successes) {
1622 printk("--------------------------------------------------------\n");
1623 printk("%3d out of %3d testcases failed, as expected. |\n",
1624 expected_testcase_failures, testcase_total);
1625 printk("----------------------------------------------------\n");
1626 debug_locks = 1;
1627 } else if (expected_testcase_failures && !testcase_successes) {
1628 printk("--------------------------------------------------------\n");
1629 printk("All %3d testcases failed, as expected. |\n",
1630 expected_testcase_failures);
1631 printk("----------------------------------------\n");
1632 debug_locks = 1;
1633 } else {
1634 printk("-------------------------------------------------------\n");
1635 printk("Good, all %3d testcases passed! |\n",
1636 testcase_successes);
1637 printk("---------------------------------\n");
1638 debug_locks = 1;
1639 }
1640 debug_locks_silent = 0;
1641}