locking/lockdep: Exclude local_lock_t from IRQ inversions
[linux-block.git] / lib / locking-selftest.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
cae2ed9a
IM
2/*
3 * lib/locking-selftest.c
4 *
5 * Testsuite for various locking APIs: spinlocks, rwlocks,
6 * mutexes and rw-semaphores.
7 *
8 * It is checking both false positives and false negatives.
9 *
10 * Started by Ingo Molnar:
11 *
12 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
13 */
14#include <linux/rwsem.h>
15#include <linux/mutex.h>
1b375dc3 16#include <linux/ww_mutex.h>
cae2ed9a 17#include <linux/sched.h>
d5037d1d 18#include <linux/sched/mm.h>
cae2ed9a 19#include <linux/delay.h>
fbb9ce95 20#include <linux/lockdep.h>
cae2ed9a
IM
21#include <linux/spinlock.h>
22#include <linux/kallsyms.h>
23#include <linux/interrupt.h>
24#include <linux/debug_locks.h>
25#include <linux/irqflags.h>
018956d6 26#include <linux/rtmutex.h>
cae2ed9a
IM
27
28/*
29 * Change this to 1 if you want to see the failure printouts:
30 */
31static unsigned int debug_locks_verbose;
e9181886 32unsigned int force_read_lock_recursive;
cae2ed9a 33
08295b3b 34static DEFINE_WD_CLASS(ww_lockdep);
1de99445 35
cae2ed9a
IM
36static int __init setup_debug_locks_verbose(char *str)
37{
38 get_option(&str, &debug_locks_verbose);
39
40 return 1;
41}
42
43__setup("debug_locks_verbose=", setup_debug_locks_verbose);
44
45#define FAILURE 0
46#define SUCCESS 1
47
48#define LOCKTYPE_SPIN 0x1
49#define LOCKTYPE_RWLOCK 0x2
50#define LOCKTYPE_MUTEX 0x4
51#define LOCKTYPE_RWSEM 0x8
1de99445 52#define LOCKTYPE_WW 0x10
018956d6 53#define LOCKTYPE_RTMUTEX 0x20
1de99445
ML
54
55static struct ww_acquire_ctx t, t2;
f3cf139e 56static struct ww_mutex o, o2, o3;
cae2ed9a
IM
57
58/*
59 * Normal standalone locks, for the circular and irq-context
60 * dependency tests:
61 */
a2e9ae58
PZ
62static DEFINE_SPINLOCK(lock_A);
63static DEFINE_SPINLOCK(lock_B);
64static DEFINE_SPINLOCK(lock_C);
65static DEFINE_SPINLOCK(lock_D);
cae2ed9a 66
9271a40d
BF
67static DEFINE_RAW_SPINLOCK(raw_lock_A);
68static DEFINE_RAW_SPINLOCK(raw_lock_B);
69
cae2ed9a
IM
70static DEFINE_RWLOCK(rwlock_A);
71static DEFINE_RWLOCK(rwlock_B);
72static DEFINE_RWLOCK(rwlock_C);
73static DEFINE_RWLOCK(rwlock_D);
74
75static DEFINE_MUTEX(mutex_A);
76static DEFINE_MUTEX(mutex_B);
77static DEFINE_MUTEX(mutex_C);
78static DEFINE_MUTEX(mutex_D);
79
80static DECLARE_RWSEM(rwsem_A);
81static DECLARE_RWSEM(rwsem_B);
82static DECLARE_RWSEM(rwsem_C);
83static DECLARE_RWSEM(rwsem_D);
84
018956d6
PZ
85#ifdef CONFIG_RT_MUTEXES
86
87static DEFINE_RT_MUTEX(rtmutex_A);
88static DEFINE_RT_MUTEX(rtmutex_B);
89static DEFINE_RT_MUTEX(rtmutex_C);
90static DEFINE_RT_MUTEX(rtmutex_D);
91
92#endif
93
cae2ed9a
IM
94/*
95 * Locks that we initialize dynamically as well so that
96 * e.g. X1 and X2 becomes two instances of the same class,
97 * but X* and Y* are different classes. We do this so that
98 * we do not trigger a real lockup:
99 */
a2e9ae58
PZ
100static DEFINE_SPINLOCK(lock_X1);
101static DEFINE_SPINLOCK(lock_X2);
102static DEFINE_SPINLOCK(lock_Y1);
103static DEFINE_SPINLOCK(lock_Y2);
104static DEFINE_SPINLOCK(lock_Z1);
105static DEFINE_SPINLOCK(lock_Z2);
cae2ed9a
IM
106
107static DEFINE_RWLOCK(rwlock_X1);
108static DEFINE_RWLOCK(rwlock_X2);
109static DEFINE_RWLOCK(rwlock_Y1);
110static DEFINE_RWLOCK(rwlock_Y2);
111static DEFINE_RWLOCK(rwlock_Z1);
112static DEFINE_RWLOCK(rwlock_Z2);
113
114static DEFINE_MUTEX(mutex_X1);
115static DEFINE_MUTEX(mutex_X2);
116static DEFINE_MUTEX(mutex_Y1);
117static DEFINE_MUTEX(mutex_Y2);
118static DEFINE_MUTEX(mutex_Z1);
119static DEFINE_MUTEX(mutex_Z2);
120
121static DECLARE_RWSEM(rwsem_X1);
122static DECLARE_RWSEM(rwsem_X2);
123static DECLARE_RWSEM(rwsem_Y1);
124static DECLARE_RWSEM(rwsem_Y2);
125static DECLARE_RWSEM(rwsem_Z1);
126static DECLARE_RWSEM(rwsem_Z2);
127
018956d6
PZ
128#ifdef CONFIG_RT_MUTEXES
129
130static DEFINE_RT_MUTEX(rtmutex_X1);
131static DEFINE_RT_MUTEX(rtmutex_X2);
132static DEFINE_RT_MUTEX(rtmutex_Y1);
133static DEFINE_RT_MUTEX(rtmutex_Y2);
134static DEFINE_RT_MUTEX(rtmutex_Z1);
135static DEFINE_RT_MUTEX(rtmutex_Z2);
136
137#endif
138
cae2ed9a
IM
139/*
140 * non-inlined runtime initializers, to let separate locks share
141 * the same lock-class:
142 */
143#define INIT_CLASS_FUNC(class) \
144static noinline void \
a2e9ae58 145init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \
9fb1b90c 146 struct mutex *mutex, struct rw_semaphore *rwsem)\
cae2ed9a 147{ \
a2e9ae58 148 spin_lock_init(lock); \
cae2ed9a
IM
149 rwlock_init(rwlock); \
150 mutex_init(mutex); \
151 init_rwsem(rwsem); \
152}
153
154INIT_CLASS_FUNC(X)
155INIT_CLASS_FUNC(Y)
156INIT_CLASS_FUNC(Z)
157
158static void init_shared_classes(void)
159{
018956d6
PZ
160#ifdef CONFIG_RT_MUTEXES
161 static struct lock_class_key rt_X, rt_Y, rt_Z;
162
163 __rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
164 __rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
165 __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
166 __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
167 __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
168 __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
169#endif
170
cae2ed9a
IM
171 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
172 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
173
174 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
175 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
176
177 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
178 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
179}
180
181/*
182 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
183 * The following functions use a lock from a simulated hardirq/softirq
184 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
185 */
186
187#define HARDIRQ_DISABLE local_irq_disable
188#define HARDIRQ_ENABLE local_irq_enable
189
190#define HARDIRQ_ENTER() \
191 local_irq_disable(); \
ba9f207c 192 __irq_enter(); \
cae2ed9a
IM
193 WARN_ON(!in_irq());
194
195#define HARDIRQ_EXIT() \
196 __irq_exit(); \
197 local_irq_enable();
198
199#define SOFTIRQ_DISABLE local_bh_disable
200#define SOFTIRQ_ENABLE local_bh_enable
201
202#define SOFTIRQ_ENTER() \
203 local_bh_disable(); \
204 local_irq_disable(); \
d820ac4c 205 lockdep_softirq_enter(); \
cae2ed9a
IM
206 WARN_ON(!in_softirq());
207
208#define SOFTIRQ_EXIT() \
d820ac4c 209 lockdep_softirq_exit(); \
cae2ed9a
IM
210 local_irq_enable(); \
211 local_bh_enable();
212
213/*
214 * Shortcuts for lock/unlock API variants, to keep
215 * the testcases compact:
216 */
a2e9ae58
PZ
217#define L(x) spin_lock(&lock_##x)
218#define U(x) spin_unlock(&lock_##x)
cae2ed9a 219#define LU(x) L(x); U(x)
a2e9ae58 220#define SI(x) spin_lock_init(&lock_##x)
cae2ed9a
IM
221
222#define WL(x) write_lock(&rwlock_##x)
223#define WU(x) write_unlock(&rwlock_##x)
224#define WLU(x) WL(x); WU(x)
225
226#define RL(x) read_lock(&rwlock_##x)
227#define RU(x) read_unlock(&rwlock_##x)
228#define RLU(x) RL(x); RU(x)
229#define RWI(x) rwlock_init(&rwlock_##x)
230
231#define ML(x) mutex_lock(&mutex_##x)
232#define MU(x) mutex_unlock(&mutex_##x)
233#define MI(x) mutex_init(&mutex_##x)
234
018956d6
PZ
235#define RTL(x) rt_mutex_lock(&rtmutex_##x)
236#define RTU(x) rt_mutex_unlock(&rtmutex_##x)
237#define RTI(x) rt_mutex_init(&rtmutex_##x)
238
cae2ed9a
IM
239#define WSL(x) down_write(&rwsem_##x)
240#define WSU(x) up_write(&rwsem_##x)
241
242#define RSL(x) down_read(&rwsem_##x)
243#define RSU(x) up_read(&rwsem_##x)
244#define RWSI(x) init_rwsem(&rwsem_##x)
245
1de99445
ML
246#ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
247#define WWAI(x) ww_acquire_init(x, &ww_lockdep)
248#else
249#define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
250#endif
251#define WWAD(x) ww_acquire_done(x)
252#define WWAF(x) ww_acquire_fini(x)
253
254#define WWL(x, c) ww_mutex_lock(x, c)
255#define WWT(x) ww_mutex_trylock(x)
256#define WWL1(x) ww_mutex_lock(x, NULL)
257#define WWU(x) ww_mutex_unlock(x)
258
259
cae2ed9a
IM
260#define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
261
262/*
263 * Generate different permutations of the same testcase, using
264 * the same basic lock-dependency/state events:
265 */
266
267#define GENERATE_TESTCASE(name) \
268 \
269static void name(void) { E(); }
270
271#define GENERATE_PERMUTATIONS_2_EVENTS(name) \
272 \
273static void name##_12(void) { E1(); E2(); } \
274static void name##_21(void) { E2(); E1(); }
275
276#define GENERATE_PERMUTATIONS_3_EVENTS(name) \
277 \
278static void name##_123(void) { E1(); E2(); E3(); } \
279static void name##_132(void) { E1(); E3(); E2(); } \
280static void name##_213(void) { E2(); E1(); E3(); } \
281static void name##_231(void) { E2(); E3(); E1(); } \
282static void name##_312(void) { E3(); E1(); E2(); } \
283static void name##_321(void) { E3(); E2(); E1(); }
284
285/*
286 * AA deadlock:
287 */
288
289#define E() \
290 \
291 LOCK(X1); \
292 LOCK(X2); /* this one should fail */
293
294/*
295 * 6 testcases:
296 */
297#include "locking-selftest-spin.h"
298GENERATE_TESTCASE(AA_spin)
299#include "locking-selftest-wlock.h"
300GENERATE_TESTCASE(AA_wlock)
301#include "locking-selftest-rlock.h"
302GENERATE_TESTCASE(AA_rlock)
303#include "locking-selftest-mutex.h"
304GENERATE_TESTCASE(AA_mutex)
305#include "locking-selftest-wsem.h"
306GENERATE_TESTCASE(AA_wsem)
307#include "locking-selftest-rsem.h"
308GENERATE_TESTCASE(AA_rsem)
309
018956d6
PZ
310#ifdef CONFIG_RT_MUTEXES
311#include "locking-selftest-rtmutex.h"
312GENERATE_TESTCASE(AA_rtmutex);
313#endif
314
cae2ed9a
IM
315#undef E
316
317/*
318 * Special-case for read-locking, they are
6c9076ec 319 * allowed to recurse on the same lock class:
cae2ed9a
IM
320 */
321static void rlock_AA1(void)
322{
323 RL(X1);
324 RL(X1); // this one should NOT fail
325}
326
327static void rlock_AA1B(void)
328{
329 RL(X1);
6c9076ec 330 RL(X2); // this one should NOT fail
cae2ed9a
IM
331}
332
333static void rsem_AA1(void)
334{
335 RSL(X1);
336 RSL(X1); // this one should fail
337}
338
339static void rsem_AA1B(void)
340{
341 RSL(X1);
342 RSL(X2); // this one should fail
343}
344/*
345 * The mixing of read and write locks is not allowed:
346 */
347static void rlock_AA2(void)
348{
349 RL(X1);
350 WL(X2); // this one should fail
351}
352
353static void rsem_AA2(void)
354{
355 RSL(X1);
356 WSL(X2); // this one should fail
357}
358
359static void rlock_AA3(void)
360{
361 WL(X1);
362 RL(X2); // this one should fail
363}
364
365static void rsem_AA3(void)
366{
367 WSL(X1);
368 RSL(X2); // this one should fail
369}
370
e9149858
PZ
371/*
372 * read_lock(A)
373 * spin_lock(B)
374 * spin_lock(B)
375 * write_lock(A)
376 */
377static void rlock_ABBA1(void)
378{
379 RL(X1);
380 L(Y1);
381 U(Y1);
382 RU(X1);
383
384 L(Y1);
385 WL(X1);
386 WU(X1);
387 U(Y1); // should fail
388}
389
390static void rwsem_ABBA1(void)
391{
392 RSL(X1);
393 ML(Y1);
394 MU(Y1);
395 RSU(X1);
396
397 ML(Y1);
398 WSL(X1);
399 WSU(X1);
400 MU(Y1); // should fail
401}
402
d4f200e5
BF
403/*
404 * read_lock(A)
405 * spin_lock(B)
406 * spin_lock(B)
407 * write_lock(A)
408 *
409 * This test case is aimed at poking whether the chain cache prevents us from
410 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
411 * read/write locks, the following case may happen
412 *
413 * { read_lock(A)->lock(B) dependency exists }
414 *
415 * P0:
416 * lock(B);
417 * read_lock(A);
418 *
419 * { Not a deadlock, B -> A is added in the chain cache }
420 *
421 * P1:
422 * lock(B);
423 * write_lock(A);
424 *
425 * { B->A found in chain cache, not reported as a deadlock }
426 *
427 */
428static void rlock_chaincache_ABBA1(void)
429{
430 RL(X1);
431 L(Y1);
432 U(Y1);
433 RU(X1);
434
435 L(Y1);
436 RL(X1);
437 RU(X1);
438 U(Y1);
439
440 L(Y1);
441 WL(X1);
442 WU(X1);
443 U(Y1); // should fail
444}
445
e9149858
PZ
446/*
447 * read_lock(A)
448 * spin_lock(B)
449 * spin_lock(B)
450 * read_lock(A)
451 */
452static void rlock_ABBA2(void)
453{
454 RL(X1);
455 L(Y1);
456 U(Y1);
457 RU(X1);
458
459 L(Y1);
460 RL(X1);
461 RU(X1);
462 U(Y1); // should NOT fail
463}
464
465static void rwsem_ABBA2(void)
466{
467 RSL(X1);
468 ML(Y1);
469 MU(Y1);
470 RSU(X1);
471
472 ML(Y1);
473 RSL(X1);
474 RSU(X1);
475 MU(Y1); // should fail
476}
477
478
479/*
480 * write_lock(A)
481 * spin_lock(B)
482 * spin_lock(B)
483 * write_lock(A)
484 */
485static void rlock_ABBA3(void)
486{
487 WL(X1);
488 L(Y1);
489 U(Y1);
490 WU(X1);
491
492 L(Y1);
493 WL(X1);
494 WU(X1);
495 U(Y1); // should fail
496}
497
498static void rwsem_ABBA3(void)
499{
500 WSL(X1);
501 ML(Y1);
502 MU(Y1);
503 WSU(X1);
504
505 ML(Y1);
506 WSL(X1);
507 WSU(X1);
508 MU(Y1); // should fail
509}
510
cae2ed9a
IM
511/*
512 * ABBA deadlock:
513 */
514
515#define E() \
516 \
517 LOCK_UNLOCK_2(A, B); \
518 LOCK_UNLOCK_2(B, A); /* fail */
519
520/*
521 * 6 testcases:
522 */
523#include "locking-selftest-spin.h"
524GENERATE_TESTCASE(ABBA_spin)
525#include "locking-selftest-wlock.h"
526GENERATE_TESTCASE(ABBA_wlock)
527#include "locking-selftest-rlock.h"
528GENERATE_TESTCASE(ABBA_rlock)
529#include "locking-selftest-mutex.h"
530GENERATE_TESTCASE(ABBA_mutex)
531#include "locking-selftest-wsem.h"
532GENERATE_TESTCASE(ABBA_wsem)
533#include "locking-selftest-rsem.h"
534GENERATE_TESTCASE(ABBA_rsem)
535
018956d6
PZ
536#ifdef CONFIG_RT_MUTEXES
537#include "locking-selftest-rtmutex.h"
538GENERATE_TESTCASE(ABBA_rtmutex);
539#endif
540
cae2ed9a
IM
541#undef E
542
543/*
544 * AB BC CA deadlock:
545 */
546
547#define E() \
548 \
549 LOCK_UNLOCK_2(A, B); \
550 LOCK_UNLOCK_2(B, C); \
551 LOCK_UNLOCK_2(C, A); /* fail */
552
553/*
554 * 6 testcases:
555 */
556#include "locking-selftest-spin.h"
557GENERATE_TESTCASE(ABBCCA_spin)
558#include "locking-selftest-wlock.h"
559GENERATE_TESTCASE(ABBCCA_wlock)
560#include "locking-selftest-rlock.h"
561GENERATE_TESTCASE(ABBCCA_rlock)
562#include "locking-selftest-mutex.h"
563GENERATE_TESTCASE(ABBCCA_mutex)
564#include "locking-selftest-wsem.h"
565GENERATE_TESTCASE(ABBCCA_wsem)
566#include "locking-selftest-rsem.h"
567GENERATE_TESTCASE(ABBCCA_rsem)
568
018956d6
PZ
569#ifdef CONFIG_RT_MUTEXES
570#include "locking-selftest-rtmutex.h"
571GENERATE_TESTCASE(ABBCCA_rtmutex);
572#endif
573
cae2ed9a
IM
574#undef E
575
576/*
577 * AB CA BC deadlock:
578 */
579
580#define E() \
581 \
582 LOCK_UNLOCK_2(A, B); \
583 LOCK_UNLOCK_2(C, A); \
584 LOCK_UNLOCK_2(B, C); /* fail */
585
586/*
587 * 6 testcases:
588 */
589#include "locking-selftest-spin.h"
590GENERATE_TESTCASE(ABCABC_spin)
591#include "locking-selftest-wlock.h"
592GENERATE_TESTCASE(ABCABC_wlock)
593#include "locking-selftest-rlock.h"
594GENERATE_TESTCASE(ABCABC_rlock)
595#include "locking-selftest-mutex.h"
596GENERATE_TESTCASE(ABCABC_mutex)
597#include "locking-selftest-wsem.h"
598GENERATE_TESTCASE(ABCABC_wsem)
599#include "locking-selftest-rsem.h"
600GENERATE_TESTCASE(ABCABC_rsem)
601
018956d6
PZ
602#ifdef CONFIG_RT_MUTEXES
603#include "locking-selftest-rtmutex.h"
604GENERATE_TESTCASE(ABCABC_rtmutex);
605#endif
606
cae2ed9a
IM
607#undef E
608
609/*
610 * AB BC CD DA deadlock:
611 */
612
613#define E() \
614 \
615 LOCK_UNLOCK_2(A, B); \
616 LOCK_UNLOCK_2(B, C); \
617 LOCK_UNLOCK_2(C, D); \
618 LOCK_UNLOCK_2(D, A); /* fail */
619
620/*
621 * 6 testcases:
622 */
623#include "locking-selftest-spin.h"
624GENERATE_TESTCASE(ABBCCDDA_spin)
625#include "locking-selftest-wlock.h"
626GENERATE_TESTCASE(ABBCCDDA_wlock)
627#include "locking-selftest-rlock.h"
628GENERATE_TESTCASE(ABBCCDDA_rlock)
629#include "locking-selftest-mutex.h"
630GENERATE_TESTCASE(ABBCCDDA_mutex)
631#include "locking-selftest-wsem.h"
632GENERATE_TESTCASE(ABBCCDDA_wsem)
633#include "locking-selftest-rsem.h"
634GENERATE_TESTCASE(ABBCCDDA_rsem)
635
018956d6
PZ
636#ifdef CONFIG_RT_MUTEXES
637#include "locking-selftest-rtmutex.h"
638GENERATE_TESTCASE(ABBCCDDA_rtmutex);
639#endif
640
cae2ed9a
IM
641#undef E
642
643/*
644 * AB CD BD DA deadlock:
645 */
646#define E() \
647 \
648 LOCK_UNLOCK_2(A, B); \
649 LOCK_UNLOCK_2(C, D); \
650 LOCK_UNLOCK_2(B, D); \
651 LOCK_UNLOCK_2(D, A); /* fail */
652
653/*
654 * 6 testcases:
655 */
656#include "locking-selftest-spin.h"
657GENERATE_TESTCASE(ABCDBDDA_spin)
658#include "locking-selftest-wlock.h"
659GENERATE_TESTCASE(ABCDBDDA_wlock)
660#include "locking-selftest-rlock.h"
661GENERATE_TESTCASE(ABCDBDDA_rlock)
662#include "locking-selftest-mutex.h"
663GENERATE_TESTCASE(ABCDBDDA_mutex)
664#include "locking-selftest-wsem.h"
665GENERATE_TESTCASE(ABCDBDDA_wsem)
666#include "locking-selftest-rsem.h"
667GENERATE_TESTCASE(ABCDBDDA_rsem)
668
018956d6
PZ
669#ifdef CONFIG_RT_MUTEXES
670#include "locking-selftest-rtmutex.h"
671GENERATE_TESTCASE(ABCDBDDA_rtmutex);
672#endif
673
cae2ed9a
IM
674#undef E
675
676/*
677 * AB CD BC DA deadlock:
678 */
679#define E() \
680 \
681 LOCK_UNLOCK_2(A, B); \
682 LOCK_UNLOCK_2(C, D); \
683 LOCK_UNLOCK_2(B, C); \
684 LOCK_UNLOCK_2(D, A); /* fail */
685
686/*
687 * 6 testcases:
688 */
689#include "locking-selftest-spin.h"
690GENERATE_TESTCASE(ABCDBCDA_spin)
691#include "locking-selftest-wlock.h"
692GENERATE_TESTCASE(ABCDBCDA_wlock)
693#include "locking-selftest-rlock.h"
694GENERATE_TESTCASE(ABCDBCDA_rlock)
695#include "locking-selftest-mutex.h"
696GENERATE_TESTCASE(ABCDBCDA_mutex)
697#include "locking-selftest-wsem.h"
698GENERATE_TESTCASE(ABCDBCDA_wsem)
699#include "locking-selftest-rsem.h"
700GENERATE_TESTCASE(ABCDBCDA_rsem)
701
018956d6
PZ
702#ifdef CONFIG_RT_MUTEXES
703#include "locking-selftest-rtmutex.h"
704GENERATE_TESTCASE(ABCDBCDA_rtmutex);
705#endif
706
cae2ed9a
IM
707#undef E
708
709/*
710 * Double unlock:
711 */
712#define E() \
713 \
714 LOCK(A); \
715 UNLOCK(A); \
716 UNLOCK(A); /* fail */
717
718/*
719 * 6 testcases:
720 */
721#include "locking-selftest-spin.h"
722GENERATE_TESTCASE(double_unlock_spin)
723#include "locking-selftest-wlock.h"
724GENERATE_TESTCASE(double_unlock_wlock)
725#include "locking-selftest-rlock.h"
726GENERATE_TESTCASE(double_unlock_rlock)
727#include "locking-selftest-mutex.h"
728GENERATE_TESTCASE(double_unlock_mutex)
729#include "locking-selftest-wsem.h"
730GENERATE_TESTCASE(double_unlock_wsem)
731#include "locking-selftest-rsem.h"
732GENERATE_TESTCASE(double_unlock_rsem)
733
018956d6
PZ
734#ifdef CONFIG_RT_MUTEXES
735#include "locking-selftest-rtmutex.h"
736GENERATE_TESTCASE(double_unlock_rtmutex);
737#endif
738
cae2ed9a
IM
739#undef E
740
cae2ed9a
IM
741/*
742 * initializing a held lock:
743 */
744#define E() \
745 \
746 LOCK(A); \
747 INIT(A); /* fail */
748
749/*
750 * 6 testcases:
751 */
752#include "locking-selftest-spin.h"
753GENERATE_TESTCASE(init_held_spin)
754#include "locking-selftest-wlock.h"
755GENERATE_TESTCASE(init_held_wlock)
756#include "locking-selftest-rlock.h"
757GENERATE_TESTCASE(init_held_rlock)
758#include "locking-selftest-mutex.h"
759GENERATE_TESTCASE(init_held_mutex)
760#include "locking-selftest-wsem.h"
761GENERATE_TESTCASE(init_held_wsem)
762#include "locking-selftest-rsem.h"
763GENERATE_TESTCASE(init_held_rsem)
764
018956d6
PZ
765#ifdef CONFIG_RT_MUTEXES
766#include "locking-selftest-rtmutex.h"
767GENERATE_TESTCASE(init_held_rtmutex);
768#endif
769
cae2ed9a
IM
770#undef E
771
772/*
773 * locking an irq-safe lock with irqs enabled:
774 */
775#define E1() \
776 \
777 IRQ_ENTER(); \
778 LOCK(A); \
779 UNLOCK(A); \
780 IRQ_EXIT();
781
782#define E2() \
783 \
784 LOCK(A); \
785 UNLOCK(A);
786
787/*
788 * Generate 24 testcases:
789 */
790#include "locking-selftest-spin-hardirq.h"
791GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
792
793#include "locking-selftest-rlock-hardirq.h"
794GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
795
796#include "locking-selftest-wlock-hardirq.h"
797GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
798
799#include "locking-selftest-spin-softirq.h"
800GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
801
802#include "locking-selftest-rlock-softirq.h"
803GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
804
805#include "locking-selftest-wlock-softirq.h"
806GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
807
808#undef E1
809#undef E2
810
811/*
812 * Enabling hardirqs with a softirq-safe lock held:
813 */
814#define E1() \
815 \
816 SOFTIRQ_ENTER(); \
817 LOCK(A); \
818 UNLOCK(A); \
819 SOFTIRQ_EXIT();
820
821#define E2() \
822 \
823 HARDIRQ_DISABLE(); \
824 LOCK(A); \
825 HARDIRQ_ENABLE(); \
826 UNLOCK(A);
827
828/*
829 * Generate 12 testcases:
830 */
831#include "locking-selftest-spin.h"
832GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
833
834#include "locking-selftest-wlock.h"
835GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
836
837#include "locking-selftest-rlock.h"
838GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
839
840#undef E1
841#undef E2
842
843/*
844 * Enabling irqs with an irq-safe lock held:
845 */
846#define E1() \
847 \
848 IRQ_ENTER(); \
849 LOCK(A); \
850 UNLOCK(A); \
851 IRQ_EXIT();
852
853#define E2() \
854 \
855 IRQ_DISABLE(); \
856 LOCK(A); \
857 IRQ_ENABLE(); \
858 UNLOCK(A);
859
860/*
861 * Generate 24 testcases:
862 */
863#include "locking-selftest-spin-hardirq.h"
864GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
865
866#include "locking-selftest-rlock-hardirq.h"
867GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
868
869#include "locking-selftest-wlock-hardirq.h"
870GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
871
872#include "locking-selftest-spin-softirq.h"
873GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
874
875#include "locking-selftest-rlock-softirq.h"
876GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
877
878#include "locking-selftest-wlock-softirq.h"
879GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
880
881#undef E1
882#undef E2
883
884/*
885 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
886 */
887#define E1() \
888 \
889 LOCK(A); \
890 LOCK(B); \
891 UNLOCK(B); \
892 UNLOCK(A); \
893
894#define E2() \
895 \
896 LOCK(B); \
897 UNLOCK(B);
898
899#define E3() \
900 \
901 IRQ_ENTER(); \
902 LOCK(A); \
903 UNLOCK(A); \
904 IRQ_EXIT();
905
906/*
907 * Generate 36 testcases:
908 */
909#include "locking-selftest-spin-hardirq.h"
910GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
911
912#include "locking-selftest-rlock-hardirq.h"
913GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
914
915#include "locking-selftest-wlock-hardirq.h"
916GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
917
918#include "locking-selftest-spin-softirq.h"
919GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
920
921#include "locking-selftest-rlock-softirq.h"
922GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
923
924#include "locking-selftest-wlock-softirq.h"
925GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
926
927#undef E1
928#undef E2
929#undef E3
930
931/*
932 * If a lock turns into softirq-safe, but earlier it took
933 * a softirq-unsafe lock:
934 */
935
936#define E1() \
937 IRQ_DISABLE(); \
938 LOCK(A); \
939 LOCK(B); \
940 UNLOCK(B); \
941 UNLOCK(A); \
942 IRQ_ENABLE();
943
944#define E2() \
945 LOCK(B); \
946 UNLOCK(B);
947
948#define E3() \
949 IRQ_ENTER(); \
950 LOCK(A); \
951 UNLOCK(A); \
952 IRQ_EXIT();
953
954/*
955 * Generate 36 testcases:
956 */
957#include "locking-selftest-spin-hardirq.h"
958GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
959
960#include "locking-selftest-rlock-hardirq.h"
961GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
962
963#include "locking-selftest-wlock-hardirq.h"
964GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
965
966#include "locking-selftest-spin-softirq.h"
967GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
968
969#include "locking-selftest-rlock-softirq.h"
970GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
971
972#include "locking-selftest-wlock-softirq.h"
973GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
974
975#undef E1
976#undef E2
977#undef E3
978
979/*
980 * read-lock / write-lock irq inversion.
981 *
982 * Deadlock scenario:
983 *
984 * CPU#1 is at #1, i.e. it has write-locked A, but has not
985 * taken B yet.
986 *
987 * CPU#2 is at #2, i.e. it has locked B.
988 *
989 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
990 *
991 * The deadlock occurs because CPU#1 will spin on B, and CPU#2
992 * will spin on A.
993 */
994
995#define E1() \
996 \
997 IRQ_DISABLE(); \
998 WL(A); \
999 LOCK(B); \
1000 UNLOCK(B); \
1001 WU(A); \
1002 IRQ_ENABLE();
1003
1004#define E2() \
1005 \
1006 LOCK(B); \
1007 UNLOCK(B);
1008
1009#define E3() \
1010 \
1011 IRQ_ENTER(); \
1012 RL(A); \
1013 RU(A); \
1014 IRQ_EXIT();
1015
1016/*
1017 * Generate 36 testcases:
1018 */
1019#include "locking-selftest-spin-hardirq.h"
1020GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
1021
1022#include "locking-selftest-rlock-hardirq.h"
1023GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
1024
1025#include "locking-selftest-wlock-hardirq.h"
1026GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
1027
1028#include "locking-selftest-spin-softirq.h"
1029GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
1030
1031#include "locking-selftest-rlock-softirq.h"
1032GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
1033
1034#include "locking-selftest-wlock-softirq.h"
1035GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
1036
1037#undef E1
1038#undef E2
1039#undef E3
1040
8ef7ca75
BF
1041/*
1042 * write-read / write-read / write-read deadlock even if read is recursive
1043 */
1044
1045#define E1() \
1046 \
1047 WL(X1); \
1048 RL(Y1); \
1049 RU(Y1); \
1050 WU(X1);
1051
1052#define E2() \
1053 \
1054 WL(Y1); \
1055 RL(Z1); \
1056 RU(Z1); \
1057 WU(Y1);
1058
1059#define E3() \
1060 \
1061 WL(Z1); \
1062 RL(X1); \
1063 RU(X1); \
1064 WU(Z1);
1065
1066#include "locking-selftest-rlock.h"
1067GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
1068
1069#undef E1
1070#undef E2
1071#undef E3
1072
1073/*
1074 * write-write / read-read / write-read deadlock even if read is recursive
1075 */
1076
1077#define E1() \
1078 \
1079 WL(X1); \
1080 WL(Y1); \
1081 WU(Y1); \
1082 WU(X1);
1083
1084#define E2() \
1085 \
1086 RL(Y1); \
1087 RL(Z1); \
1088 RU(Z1); \
1089 RU(Y1);
1090
1091#define E3() \
1092 \
1093 WL(Z1); \
1094 RL(X1); \
1095 RU(X1); \
1096 WU(Z1);
1097
1098#include "locking-selftest-rlock.h"
1099GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
1100
1101#undef E1
1102#undef E2
1103#undef E3
1104
1105/*
1106 * write-write / read-read / read-write is not deadlock when read is recursive
1107 */
1108
1109#define E1() \
1110 \
1111 WL(X1); \
1112 WL(Y1); \
1113 WU(Y1); \
1114 WU(X1);
1115
1116#define E2() \
1117 \
1118 RL(Y1); \
1119 RL(Z1); \
1120 RU(Z1); \
1121 RU(Y1);
1122
1123#define E3() \
1124 \
1125 RL(Z1); \
1126 WL(X1); \
1127 WU(X1); \
1128 RU(Z1);
1129
1130#include "locking-selftest-rlock.h"
1131GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
1132
1133#undef E1
1134#undef E2
1135#undef E3
1136
1137/*
1138 * write-read / read-read / write-write is not deadlock when read is recursive
1139 */
1140
1141#define E1() \
1142 \
1143 WL(X1); \
1144 RL(Y1); \
1145 RU(Y1); \
1146 WU(X1);
1147
1148#define E2() \
1149 \
1150 RL(Y1); \
1151 RL(Z1); \
1152 RU(Z1); \
1153 RU(Y1);
1154
1155#define E3() \
1156 \
1157 WL(Z1); \
1158 WL(X1); \
1159 WU(X1); \
1160 WU(Z1);
1161
1162#include "locking-selftest-rlock.h"
1163GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
1164
1165#undef E1
1166#undef E2
1167#undef E3
cae2ed9a
IM
1168/*
1169 * read-lock / write-lock recursion that is actually safe.
1170 */
1171
1172#define E1() \
1173 \
1174 IRQ_DISABLE(); \
1175 WL(A); \
1176 WU(A); \
1177 IRQ_ENABLE();
1178
1179#define E2() \
1180 \
1181 RL(A); \
1182 RU(A); \
1183
1184#define E3() \
1185 \
1186 IRQ_ENTER(); \
31e0d747 1187 LOCK(A); \
cae2ed9a
IM
1188 L(B); \
1189 U(B); \
31e0d747 1190 UNLOCK(A); \
cae2ed9a
IM
1191 IRQ_EXIT();
1192
1193/*
31e0d747 1194 * Generate 24 testcases:
cae2ed9a
IM
1195 */
1196#include "locking-selftest-hardirq.h"
31e0d747
BF
1197#include "locking-selftest-rlock.h"
1198GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
1199
1200#include "locking-selftest-wlock.h"
1201GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
cae2ed9a
IM
1202
1203#include "locking-selftest-softirq.h"
31e0d747
BF
1204#include "locking-selftest-rlock.h"
1205GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
1206
1207#include "locking-selftest-wlock.h"
1208GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
cae2ed9a
IM
1209
1210#undef E1
1211#undef E2
1212#undef E3
1213
1214/*
1215 * read-lock / write-lock recursion that is unsafe.
1216 */
1217
1218#define E1() \
1219 \
1220 IRQ_DISABLE(); \
1221 L(B); \
31e0d747
BF
1222 LOCK(A); \
1223 UNLOCK(A); \
cae2ed9a
IM
1224 U(B); \
1225 IRQ_ENABLE();
1226
1227#define E2() \
1228 \
1229 RL(A); \
1230 RU(A); \
1231
1232#define E3() \
1233 \
1234 IRQ_ENTER(); \
1235 L(B); \
1236 U(B); \
1237 IRQ_EXIT();
1238
1239/*
31e0d747 1240 * Generate 24 testcases:
cae2ed9a
IM
1241 */
1242#include "locking-selftest-hardirq.h"
31e0d747
BF
1243#include "locking-selftest-rlock.h"
1244GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
1245
1246#include "locking-selftest-wlock.h"
1247GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
cae2ed9a
IM
1248
1249#include "locking-selftest-softirq.h"
31e0d747
BF
1250#include "locking-selftest-rlock.h"
1251GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
1252
1253#include "locking-selftest-wlock.h"
1254GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
cae2ed9a 1255
96a16f45
BF
1256#undef E1
1257#undef E2
1258#undef E3
1259/*
1260 * read-lock / write-lock recursion that is unsafe.
1261 *
1262 * A is a ENABLED_*_READ lock
1263 * B is a USED_IN_*_READ lock
1264 *
1265 * read_lock(A);
1266 * write_lock(B);
1267 * <interrupt>
1268 * read_lock(B);
1269 * write_lock(A); // if this one is read_lock(), no deadlock
1270 */
1271
1272#define E1() \
1273 \
1274 IRQ_DISABLE(); \
1275 WL(B); \
1276 LOCK(A); \
1277 UNLOCK(A); \
1278 WU(B); \
1279 IRQ_ENABLE();
1280
1281#define E2() \
1282 \
1283 RL(A); \
1284 RU(A); \
1285
1286#define E3() \
1287 \
1288 IRQ_ENTER(); \
1289 RL(B); \
1290 RU(B); \
1291 IRQ_EXIT();
1292
1293/*
1294 * Generate 24 testcases:
1295 */
1296#include "locking-selftest-hardirq.h"
1297#include "locking-selftest-rlock.h"
1298GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
1299
1300#include "locking-selftest-wlock.h"
1301GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
1302
1303#include "locking-selftest-softirq.h"
1304#include "locking-selftest-rlock.h"
1305GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
1306
1307#include "locking-selftest-wlock.h"
1308GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
1309
cae2ed9a
IM
1310#ifdef CONFIG_DEBUG_LOCK_ALLOC
1311# define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
9271a40d 1312# define I_RAW_SPINLOCK(x) lockdep_reset_lock(&raw_lock_##x.dep_map)
cae2ed9a
IM
1313# define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map)
1314# define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
1315# define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
1de99445 1316# define I_WW(x) lockdep_reset_lock(&x.dep_map)
018956d6
PZ
1317#ifdef CONFIG_RT_MUTEXES
1318# define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map)
1319#endif
cae2ed9a
IM
1320#else
1321# define I_SPINLOCK(x)
9271a40d 1322# define I_RAW_SPINLOCK(x)
cae2ed9a
IM
1323# define I_RWLOCK(x)
1324# define I_MUTEX(x)
1325# define I_RWSEM(x)
1de99445 1326# define I_WW(x)
cae2ed9a
IM
1327#endif
1328
018956d6
PZ
1329#ifndef I_RTMUTEX
1330# define I_RTMUTEX(x)
1331#endif
1332
1333#ifdef CONFIG_RT_MUTEXES
1334#define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x)
1335#else
1336#define I2_RTMUTEX(x)
1337#endif
1338
cae2ed9a
IM
1339#define I1(x) \
1340 do { \
1341 I_SPINLOCK(x); \
1342 I_RWLOCK(x); \
1343 I_MUTEX(x); \
1344 I_RWSEM(x); \
018956d6 1345 I_RTMUTEX(x); \
cae2ed9a
IM
1346 } while (0)
1347
1348#define I2(x) \
1349 do { \
a2e9ae58 1350 spin_lock_init(&lock_##x); \
cae2ed9a
IM
1351 rwlock_init(&rwlock_##x); \
1352 mutex_init(&mutex_##x); \
1353 init_rwsem(&rwsem_##x); \
018956d6 1354 I2_RTMUTEX(x); \
cae2ed9a
IM
1355 } while (0)
1356
1357static void reset_locks(void)
1358{
1359 local_irq_disable();
1de99445
ML
1360 lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
1361 lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
1362
cae2ed9a
IM
1363 I1(A); I1(B); I1(C); I1(D);
1364 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
f3cf139e 1365 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
9271a40d 1366 I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B);
cae2ed9a
IM
1367 lockdep_reset();
1368 I2(A); I2(B); I2(C); I2(D);
1369 init_shared_classes();
9271a40d
BF
1370 raw_spin_lock_init(&raw_lock_A);
1371 raw_spin_lock_init(&raw_lock_B);
1de99445 1372
f3cf139e 1373 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1de99445
ML
1374 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
1375 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
1376 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
cae2ed9a
IM
1377 local_irq_enable();
1378}
1379
1380#undef I
1381
1382static int testcase_total;
1383static int testcase_successes;
1384static int expected_testcase_failures;
1385static int unexpected_testcase_failures;
1386
1387static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
1388{
1389 unsigned long saved_preempt_count = preempt_count();
cae2ed9a
IM
1390
1391 WARN_ON(irqs_disabled());
1392
5831c0f7
PZ
1393 debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
1394
cae2ed9a
IM
1395 testcase_fn();
1396 /*
1397 * Filter out expected failures:
1398 */
1399#ifndef CONFIG_PROVE_LOCKING
166989e3 1400 if (expected == FAILURE && debug_locks) {
1de99445 1401 expected_testcase_failures++;
25139409 1402 pr_cont("failed|");
166989e3
ML
1403 }
1404 else
1405#endif
1406 if (debug_locks != expected) {
1de99445 1407 unexpected_testcase_failures++;
25139409 1408 pr_cont("FAILED|");
cae2ed9a
IM
1409 } else {
1410 testcase_successes++;
25139409 1411 pr_cont(" ok |");
cae2ed9a
IM
1412 }
1413 testcase_total++;
1414
5831c0f7 1415 if (debug_locks_verbose & lockclass_mask)
25139409 1416 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
cae2ed9a
IM
1417 lockclass_mask, debug_locks, expected);
1418 /*
1419 * Some tests (e.g. double-unlock) might corrupt the preemption
1420 * count, so restore it:
1421 */
4a2b4b22 1422 preempt_count_set(saved_preempt_count);
cae2ed9a
IM
1423#ifdef CONFIG_TRACE_IRQFLAGS
1424 if (softirq_count())
1425 current->softirqs_enabled = 0;
1426 else
1427 current->softirqs_enabled = 1;
1428#endif
1429
1430 reset_locks();
1431}
1432
018956d6
PZ
1433#ifdef CONFIG_RT_MUTEXES
1434#define dotest_rt(fn, e, m) dotest((fn), (e), (m))
1435#else
1436#define dotest_rt(fn, e, m)
1437#endif
1438
cae2ed9a
IM
1439static inline void print_testname(const char *testname)
1440{
1441 printk("%33s:", testname);
1442}
1443
1444#define DO_TESTCASE_1(desc, name, nr) \
1445 print_testname(desc"/"#nr); \
1446 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
25139409 1447 pr_cont("\n");
cae2ed9a
IM
1448
1449#define DO_TESTCASE_1B(desc, name, nr) \
1450 print_testname(desc"/"#nr); \
1451 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
25139409 1452 pr_cont("\n");
cae2ed9a 1453
8ef7ca75
BF
1454#define DO_TESTCASE_1RR(desc, name, nr) \
1455 print_testname(desc"/"#nr); \
1456 pr_cont(" |"); \
1457 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1458 pr_cont("\n");
1459
1460#define DO_TESTCASE_1RRB(desc, name, nr) \
1461 print_testname(desc"/"#nr); \
1462 pr_cont(" |"); \
1463 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1464 pr_cont("\n");
1465
1466
cae2ed9a
IM
1467#define DO_TESTCASE_3(desc, name, nr) \
1468 print_testname(desc"/"#nr); \
1469 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \
1470 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1471 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
25139409 1472 pr_cont("\n");
cae2ed9a
IM
1473
1474#define DO_TESTCASE_3RW(desc, name, nr) \
1475 print_testname(desc"/"#nr); \
1476 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1477 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1478 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
25139409 1479 pr_cont("\n");
cae2ed9a 1480
31e0d747
BF
1481#define DO_TESTCASE_2RW(desc, name, nr) \
1482 print_testname(desc"/"#nr); \
1483 pr_cont(" |"); \
1484 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \
1485 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \
1486 pr_cont("\n");
1487
1488#define DO_TESTCASE_2x2RW(desc, name, nr) \
1489 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
1490 DO_TESTCASE_2RW("soft-"desc, name##_soft, nr) \
1491
1492#define DO_TESTCASE_6x2x2RW(desc, name) \
1493 DO_TESTCASE_2x2RW(desc, name, 123); \
1494 DO_TESTCASE_2x2RW(desc, name, 132); \
1495 DO_TESTCASE_2x2RW(desc, name, 213); \
1496 DO_TESTCASE_2x2RW(desc, name, 231); \
1497 DO_TESTCASE_2x2RW(desc, name, 312); \
1498 DO_TESTCASE_2x2RW(desc, name, 321);
1499
cae2ed9a
IM
1500#define DO_TESTCASE_6(desc, name) \
1501 print_testname(desc); \
1502 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1503 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1504 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \
1505 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1506 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1507 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
018956d6 1508 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
25139409 1509 pr_cont("\n");
cae2ed9a
IM
1510
1511#define DO_TESTCASE_6_SUCCESS(desc, name) \
1512 print_testname(desc); \
1513 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \
1514 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \
1515 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1516 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \
1517 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \
1518 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \
018956d6 1519 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \
25139409 1520 pr_cont("\n");
cae2ed9a
IM
1521
1522/*
1523 * 'read' variant: rlocks must not trigger.
1524 */
1525#define DO_TESTCASE_6R(desc, name) \
1526 print_testname(desc); \
1527 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \
1528 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \
1529 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \
1530 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \
1531 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \
1532 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \
018956d6 1533 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \
25139409 1534 pr_cont("\n");
cae2ed9a
IM
1535
1536#define DO_TESTCASE_2I(desc, name, nr) \
1537 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1538 DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1539
1540#define DO_TESTCASE_2IB(desc, name, nr) \
1541 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1542 DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1543
1544#define DO_TESTCASE_6I(desc, name, nr) \
1545 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1546 DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1547
1548#define DO_TESTCASE_6IRW(desc, name, nr) \
1549 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1550 DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1551
1552#define DO_TESTCASE_2x3(desc, name) \
1553 DO_TESTCASE_3(desc, name, 12); \
1554 DO_TESTCASE_3(desc, name, 21);
1555
1556#define DO_TESTCASE_2x6(desc, name) \
1557 DO_TESTCASE_6I(desc, name, 12); \
1558 DO_TESTCASE_6I(desc, name, 21);
1559
1560#define DO_TESTCASE_6x2(desc, name) \
1561 DO_TESTCASE_2I(desc, name, 123); \
1562 DO_TESTCASE_2I(desc, name, 132); \
1563 DO_TESTCASE_2I(desc, name, 213); \
1564 DO_TESTCASE_2I(desc, name, 231); \
1565 DO_TESTCASE_2I(desc, name, 312); \
1566 DO_TESTCASE_2I(desc, name, 321);
1567
1568#define DO_TESTCASE_6x2B(desc, name) \
1569 DO_TESTCASE_2IB(desc, name, 123); \
1570 DO_TESTCASE_2IB(desc, name, 132); \
1571 DO_TESTCASE_2IB(desc, name, 213); \
1572 DO_TESTCASE_2IB(desc, name, 231); \
1573 DO_TESTCASE_2IB(desc, name, 312); \
1574 DO_TESTCASE_2IB(desc, name, 321);
1575
8ef7ca75
BF
1576#define DO_TESTCASE_6x1RR(desc, name) \
1577 DO_TESTCASE_1RR(desc, name, 123); \
1578 DO_TESTCASE_1RR(desc, name, 132); \
1579 DO_TESTCASE_1RR(desc, name, 213); \
1580 DO_TESTCASE_1RR(desc, name, 231); \
1581 DO_TESTCASE_1RR(desc, name, 312); \
1582 DO_TESTCASE_1RR(desc, name, 321);
1583
1584#define DO_TESTCASE_6x1RRB(desc, name) \
1585 DO_TESTCASE_1RRB(desc, name, 123); \
1586 DO_TESTCASE_1RRB(desc, name, 132); \
1587 DO_TESTCASE_1RRB(desc, name, 213); \
1588 DO_TESTCASE_1RRB(desc, name, 231); \
1589 DO_TESTCASE_1RRB(desc, name, 312); \
1590 DO_TESTCASE_1RRB(desc, name, 321);
1591
cae2ed9a
IM
1592#define DO_TESTCASE_6x6(desc, name) \
1593 DO_TESTCASE_6I(desc, name, 123); \
1594 DO_TESTCASE_6I(desc, name, 132); \
1595 DO_TESTCASE_6I(desc, name, 213); \
1596 DO_TESTCASE_6I(desc, name, 231); \
1597 DO_TESTCASE_6I(desc, name, 312); \
1598 DO_TESTCASE_6I(desc, name, 321);
1599
1600#define DO_TESTCASE_6x6RW(desc, name) \
1601 DO_TESTCASE_6IRW(desc, name, 123); \
1602 DO_TESTCASE_6IRW(desc, name, 132); \
1603 DO_TESTCASE_6IRW(desc, name, 213); \
1604 DO_TESTCASE_6IRW(desc, name, 231); \
1605 DO_TESTCASE_6IRW(desc, name, 312); \
1606 DO_TESTCASE_6IRW(desc, name, 321);
1607
1de99445
ML
1608static void ww_test_fail_acquire(void)
1609{
1610 int ret;
1611
1612 WWAI(&t);
1613 t.stamp++;
1614
1615 ret = WWL(&o, &t);
1616
1617 if (WARN_ON(!o.ctx) ||
1618 WARN_ON(ret))
1619 return;
1620
1621 /* No lockdep test, pure API */
1622 ret = WWL(&o, &t);
1623 WARN_ON(ret != -EALREADY);
1624
1625 ret = WWT(&o);
1626 WARN_ON(ret);
1627
1628 t2 = t;
1629 t2.stamp++;
1630 ret = WWL(&o, &t2);
1631 WARN_ON(ret != -EDEADLK);
1632 WWU(&o);
1633
1634 if (WWT(&o))
1635 WWU(&o);
1636#ifdef CONFIG_DEBUG_LOCK_ALLOC
1637 else
1638 DEBUG_LOCKS_WARN_ON(1);
1639#endif
1640}
1641
2fe3d4b1
ML
1642static void ww_test_normal(void)
1643{
1644 int ret;
1645
1646 WWAI(&t);
1647
1648 /*
1649 * None of the ww_mutex codepaths should be taken in the 'normal'
1650 * mutex calls. The easiest way to verify this is by using the
1651 * normal mutex calls, and making sure o.ctx is unmodified.
1652 */
1653
1654 /* mutex_lock (and indirectly, mutex_lock_nested) */
1655 o.ctx = (void *)~0UL;
1656 mutex_lock(&o.base);
1657 mutex_unlock(&o.base);
1658 WARN_ON(o.ctx != (void *)~0UL);
1659
1660 /* mutex_lock_interruptible (and *_nested) */
1661 o.ctx = (void *)~0UL;
1662 ret = mutex_lock_interruptible(&o.base);
1663 if (!ret)
1664 mutex_unlock(&o.base);
1665 else
1666 WARN_ON(1);
1667 WARN_ON(o.ctx != (void *)~0UL);
1668
1669 /* mutex_lock_killable (and *_nested) */
1670 o.ctx = (void *)~0UL;
1671 ret = mutex_lock_killable(&o.base);
1672 if (!ret)
1673 mutex_unlock(&o.base);
1674 else
1675 WARN_ON(1);
1676 WARN_ON(o.ctx != (void *)~0UL);
1677
1678 /* trylock, succeeding */
1679 o.ctx = (void *)~0UL;
1680 ret = mutex_trylock(&o.base);
1681 WARN_ON(!ret);
1682 if (ret)
1683 mutex_unlock(&o.base);
1684 else
1685 WARN_ON(1);
1686 WARN_ON(o.ctx != (void *)~0UL);
1687
1688 /* trylock, failing */
1689 o.ctx = (void *)~0UL;
1690 mutex_lock(&o.base);
1691 ret = mutex_trylock(&o.base);
1692 WARN_ON(ret);
1693 mutex_unlock(&o.base);
1694 WARN_ON(o.ctx != (void *)~0UL);
1695
1696 /* nest_lock */
1697 o.ctx = (void *)~0UL;
1698 mutex_lock_nest_lock(&o.base, &t);
1699 mutex_unlock(&o.base);
1700 WARN_ON(o.ctx != (void *)~0UL);
1701}
1702
1de99445
ML
1703static void ww_test_two_contexts(void)
1704{
1705 WWAI(&t);
1706 WWAI(&t2);
1707}
1708
1709static void ww_test_diff_class(void)
1710{
1711 WWAI(&t);
1712#ifdef CONFIG_DEBUG_MUTEXES
1713 t.ww_class = NULL;
1714#endif
1715 WWL(&o, &t);
1716}
1717
1718static void ww_test_context_done_twice(void)
1719{
1720 WWAI(&t);
1721 WWAD(&t);
1722 WWAD(&t);
1723 WWAF(&t);
1724}
1725
1726static void ww_test_context_unlock_twice(void)
1727{
1728 WWAI(&t);
1729 WWAD(&t);
1730 WWAF(&t);
1731 WWAF(&t);
1732}
1733
1734static void ww_test_context_fini_early(void)
1735{
1736 WWAI(&t);
1737 WWL(&o, &t);
1738 WWAD(&t);
1739 WWAF(&t);
1740}
1741
1742static void ww_test_context_lock_after_done(void)
1743{
1744 WWAI(&t);
1745 WWAD(&t);
1746 WWL(&o, &t);
1747}
1748
1749static void ww_test_object_unlock_twice(void)
1750{
1751 WWL1(&o);
1752 WWU(&o);
1753 WWU(&o);
1754}
1755
1756static void ww_test_object_lock_unbalanced(void)
1757{
1758 WWAI(&t);
1759 WWL(&o, &t);
1760 t.acquired = 0;
1761 WWU(&o);
1762 WWAF(&t);
1763}
1764
1765static void ww_test_object_lock_stale_context(void)
1766{
1767 WWAI(&t);
1768 o.ctx = &t2;
1769 WWL(&o, &t);
1770}
1771
f3cf139e
ML
1772static void ww_test_edeadlk_normal(void)
1773{
1774 int ret;
1775
1776 mutex_lock(&o2.base);
1777 o2.ctx = &t2;
5facae4f 1778 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1779
1780 WWAI(&t);
1781 t2 = t;
1782 t2.stamp--;
1783
1784 ret = WWL(&o, &t);
1785 WARN_ON(ret);
1786
1787 ret = WWL(&o2, &t);
1788 WARN_ON(ret != -EDEADLK);
1789
1790 o2.ctx = NULL;
1791 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1792 mutex_unlock(&o2.base);
1793 WWU(&o);
1794
1795 WWL(&o2, &t);
1796}
1797
1798static void ww_test_edeadlk_normal_slow(void)
1799{
1800 int ret;
1801
1802 mutex_lock(&o2.base);
5facae4f 1803 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1804 o2.ctx = &t2;
1805
1806 WWAI(&t);
1807 t2 = t;
1808 t2.stamp--;
1809
1810 ret = WWL(&o, &t);
1811 WARN_ON(ret);
1812
1813 ret = WWL(&o2, &t);
1814 WARN_ON(ret != -EDEADLK);
1815
1816 o2.ctx = NULL;
1817 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1818 mutex_unlock(&o2.base);
1819 WWU(&o);
1820
1821 ww_mutex_lock_slow(&o2, &t);
1822}
1823
1824static void ww_test_edeadlk_no_unlock(void)
1825{
1826 int ret;
1827
1828 mutex_lock(&o2.base);
1829 o2.ctx = &t2;
5facae4f 1830 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1831
1832 WWAI(&t);
1833 t2 = t;
1834 t2.stamp--;
1835
1836 ret = WWL(&o, &t);
1837 WARN_ON(ret);
1838
1839 ret = WWL(&o2, &t);
1840 WARN_ON(ret != -EDEADLK);
1841
1842 o2.ctx = NULL;
1843 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1844 mutex_unlock(&o2.base);
1845
1846 WWL(&o2, &t);
1847}
1848
1849static void ww_test_edeadlk_no_unlock_slow(void)
1850{
1851 int ret;
1852
1853 mutex_lock(&o2.base);
5facae4f 1854 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1855 o2.ctx = &t2;
1856
1857 WWAI(&t);
1858 t2 = t;
1859 t2.stamp--;
1860
1861 ret = WWL(&o, &t);
1862 WARN_ON(ret);
1863
1864 ret = WWL(&o2, &t);
1865 WARN_ON(ret != -EDEADLK);
1866
1867 o2.ctx = NULL;
1868 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1869 mutex_unlock(&o2.base);
1870
1871 ww_mutex_lock_slow(&o2, &t);
1872}
1873
1874static void ww_test_edeadlk_acquire_more(void)
1875{
1876 int ret;
1877
1878 mutex_lock(&o2.base);
5facae4f 1879 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1880 o2.ctx = &t2;
1881
1882 WWAI(&t);
1883 t2 = t;
1884 t2.stamp--;
1885
1886 ret = WWL(&o, &t);
1887 WARN_ON(ret);
1888
1889 ret = WWL(&o2, &t);
1890 WARN_ON(ret != -EDEADLK);
1891
1892 ret = WWL(&o3, &t);
1893}
1894
1895static void ww_test_edeadlk_acquire_more_slow(void)
1896{
1897 int ret;
1898
1899 mutex_lock(&o2.base);
5facae4f 1900 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1901 o2.ctx = &t2;
1902
1903 WWAI(&t);
1904 t2 = t;
1905 t2.stamp--;
1906
1907 ret = WWL(&o, &t);
1908 WARN_ON(ret);
1909
1910 ret = WWL(&o2, &t);
1911 WARN_ON(ret != -EDEADLK);
1912
1913 ww_mutex_lock_slow(&o3, &t);
1914}
1915
1916static void ww_test_edeadlk_acquire_more_edeadlk(void)
1917{
1918 int ret;
1919
1920 mutex_lock(&o2.base);
5facae4f 1921 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1922 o2.ctx = &t2;
1923
1924 mutex_lock(&o3.base);
5facae4f 1925 mutex_release(&o3.base.dep_map, _THIS_IP_);
f3cf139e
ML
1926 o3.ctx = &t2;
1927
1928 WWAI(&t);
1929 t2 = t;
1930 t2.stamp--;
1931
1932 ret = WWL(&o, &t);
1933 WARN_ON(ret);
1934
1935 ret = WWL(&o2, &t);
1936 WARN_ON(ret != -EDEADLK);
1937
1938 ret = WWL(&o3, &t);
1939 WARN_ON(ret != -EDEADLK);
1940}
1941
1942static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1943{
1944 int ret;
1945
1946 mutex_lock(&o2.base);
5facae4f 1947 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1948 o2.ctx = &t2;
1949
1950 mutex_lock(&o3.base);
5facae4f 1951 mutex_release(&o3.base.dep_map, _THIS_IP_);
f3cf139e
ML
1952 o3.ctx = &t2;
1953
1954 WWAI(&t);
1955 t2 = t;
1956 t2.stamp--;
1957
1958 ret = WWL(&o, &t);
1959 WARN_ON(ret);
1960
1961 ret = WWL(&o2, &t);
1962 WARN_ON(ret != -EDEADLK);
1963
1964 ww_mutex_lock_slow(&o3, &t);
1965}
1966
1967static void ww_test_edeadlk_acquire_wrong(void)
1968{
1969 int ret;
1970
1971 mutex_lock(&o2.base);
5facae4f 1972 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1973 o2.ctx = &t2;
1974
1975 WWAI(&t);
1976 t2 = t;
1977 t2.stamp--;
1978
1979 ret = WWL(&o, &t);
1980 WARN_ON(ret);
1981
1982 ret = WWL(&o2, &t);
1983 WARN_ON(ret != -EDEADLK);
1984 if (!ret)
1985 WWU(&o2);
1986
1987 WWU(&o);
1988
1989 ret = WWL(&o3, &t);
1990}
1991
1992static void ww_test_edeadlk_acquire_wrong_slow(void)
1993{
1994 int ret;
1995
1996 mutex_lock(&o2.base);
5facae4f 1997 mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139e
ML
1998 o2.ctx = &t2;
1999
2000 WWAI(&t);
2001 t2 = t;
2002 t2.stamp--;
2003
2004 ret = WWL(&o, &t);
2005 WARN_ON(ret);
2006
2007 ret = WWL(&o2, &t);
2008 WARN_ON(ret != -EDEADLK);
2009 if (!ret)
2010 WWU(&o2);
2011
2012 WWU(&o);
2013
2014 ww_mutex_lock_slow(&o3, &t);
2015}
2016
1de99445
ML
2017static void ww_test_spin_nest_unlocked(void)
2018{
a2e9ae58 2019 spin_lock_nest_lock(&lock_A, &o.base);
1de99445
ML
2020 U(A);
2021}
2022
e04ce676
BF
2023/* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2024static void ww_test_spin_nest_lock(void)
2025{
2026 spin_lock(&lock_X1);
2027 spin_lock_nest_lock(&lock_Y1, &lock_X1);
2028 spin_lock(&lock_A);
2029 spin_lock_nest_lock(&lock_Y2, &lock_X1);
2030 spin_unlock(&lock_A);
2031 spin_unlock(&lock_Y2);
2032 spin_unlock(&lock_Y1);
2033 spin_unlock(&lock_X1);
2034}
2035
1de99445
ML
2036static void ww_test_unneeded_slow(void)
2037{
2038 WWAI(&t);
2039
2040 ww_mutex_lock_slow(&o, &t);
2041}
2042
2043static void ww_test_context_block(void)
2044{
2045 int ret;
2046
2047 WWAI(&t);
2048
2049 ret = WWL(&o, &t);
2050 WARN_ON(ret);
2051 WWL1(&o2);
2052}
2053
2054static void ww_test_context_try(void)
2055{
2056 int ret;
2057
2058 WWAI(&t);
2059
2060 ret = WWL(&o, &t);
2061 WARN_ON(ret);
2062
2063 ret = WWT(&o2);
2064 WARN_ON(!ret);
2065 WWU(&o2);
2066 WWU(&o);
2067}
2068
2069static void ww_test_context_context(void)
2070{
2071 int ret;
2072
2073 WWAI(&t);
2074
2075 ret = WWL(&o, &t);
2076 WARN_ON(ret);
2077
2078 ret = WWL(&o2, &t);
2079 WARN_ON(ret);
2080
2081 WWU(&o2);
2082 WWU(&o);
2083}
2084
2085static void ww_test_try_block(void)
2086{
2087 bool ret;
2088
2089 ret = WWT(&o);
2090 WARN_ON(!ret);
2091
2092 WWL1(&o2);
2093 WWU(&o2);
2094 WWU(&o);
2095}
2096
2097static void ww_test_try_try(void)
2098{
2099 bool ret;
2100
2101 ret = WWT(&o);
2102 WARN_ON(!ret);
2103 ret = WWT(&o2);
2104 WARN_ON(!ret);
2105 WWU(&o2);
2106 WWU(&o);
2107}
2108
2109static void ww_test_try_context(void)
2110{
2111 int ret;
2112
2113 ret = WWT(&o);
2114 WARN_ON(!ret);
2115
2116 WWAI(&t);
2117
2118 ret = WWL(&o2, &t);
2119 WARN_ON(ret);
2120}
2121
2122static void ww_test_block_block(void)
2123{
2124 WWL1(&o);
2125 WWL1(&o2);
2126}
2127
2128static void ww_test_block_try(void)
2129{
2130 bool ret;
2131
2132 WWL1(&o);
2133 ret = WWT(&o2);
2134 WARN_ON(!ret);
2135}
2136
2137static void ww_test_block_context(void)
2138{
2139 int ret;
2140
2141 WWL1(&o);
2142 WWAI(&t);
2143
2144 ret = WWL(&o2, &t);
2145 WARN_ON(ret);
2146}
2147
2148static void ww_test_spin_block(void)
2149{
2150 L(A);
2151 U(A);
2152
2153 WWL1(&o);
2154 L(A);
2155 U(A);
2156 WWU(&o);
2157
2158 L(A);
2159 WWL1(&o);
2160 WWU(&o);
2161 U(A);
2162}
2163
2164static void ww_test_spin_try(void)
2165{
2166 bool ret;
2167
2168 L(A);
2169 U(A);
2170
2171 ret = WWT(&o);
2172 WARN_ON(!ret);
2173 L(A);
2174 U(A);
2175 WWU(&o);
2176
2177 L(A);
2178 ret = WWT(&o);
2179 WARN_ON(!ret);
2180 WWU(&o);
2181 U(A);
2182}
2183
2184static void ww_test_spin_context(void)
2185{
2186 int ret;
2187
2188 L(A);
2189 U(A);
2190
2191 WWAI(&t);
2192
2193 ret = WWL(&o, &t);
2194 WARN_ON(ret);
2195 L(A);
2196 U(A);
2197 WWU(&o);
2198
2199 L(A);
2200 ret = WWL(&o, &t);
2201 WARN_ON(ret);
2202 WWU(&o);
2203 U(A);
2204}
2205
2206static void ww_tests(void)
2207{
2208 printk(" --------------------------------------------------------------------------\n");
2209 printk(" | Wound/wait tests |\n");
2210 printk(" ---------------------\n");
2211
2212 print_testname("ww api failures");
2213 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
2fe3d4b1 2214 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1de99445 2215 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
25139409 2216 pr_cont("\n");
1de99445
ML
2217
2218 print_testname("ww contexts mixing");
2219 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
2220 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
25139409 2221 pr_cont("\n");
1de99445
ML
2222
2223 print_testname("finishing ww context");
2224 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
2225 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
2226 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
2227 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
25139409 2228 pr_cont("\n");
1de99445
ML
2229
2230 print_testname("locking mismatches");
2231 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
2232 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
2233 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
25139409 2234 pr_cont("\n");
1de99445 2235
f3cf139e
ML
2236 print_testname("EDEADLK handling");
2237 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
2238 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
2239 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
2240 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
2241 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
2242 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
2243 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
2244 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
2245 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
2246 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
25139409 2247 pr_cont("\n");
f3cf139e 2248
1de99445
ML
2249 print_testname("spinlock nest unlocked");
2250 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
25139409 2251 pr_cont("\n");
1de99445 2252
e04ce676
BF
2253 print_testname("spinlock nest test");
2254 dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW);
2255 pr_cont("\n");
2256
1de99445
ML
2257 printk(" -----------------------------------------------------\n");
2258 printk(" |block | try |context|\n");
2259 printk(" -----------------------------------------------------\n");
2260
2261 print_testname("context");
2262 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
2263 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
2264 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
25139409 2265 pr_cont("\n");
1de99445
ML
2266
2267 print_testname("try");
2268 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
2269 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
2270 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
25139409 2271 pr_cont("\n");
1de99445
ML
2272
2273 print_testname("block");
2274 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
2275 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
2276 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
25139409 2277 pr_cont("\n");
1de99445
ML
2278
2279 print_testname("spinlock");
2280 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
2281 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
2282 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
25139409 2283 pr_cont("\n");
1de99445 2284}
cae2ed9a 2285
ad56450d
BF
2286
2287/*
2288 * <in hardirq handler>
2289 * read_lock(&A);
2290 * <hardirq disable>
2291 * spin_lock(&B);
2292 * spin_lock(&B);
2293 * read_lock(&A);
2294 *
2295 * is a deadlock.
2296 */
2297static void queued_read_lock_hardirq_RE_Er(void)
2298{
2299 HARDIRQ_ENTER();
2300 read_lock(&rwlock_A);
2301 LOCK(B);
2302 UNLOCK(B);
2303 read_unlock(&rwlock_A);
2304 HARDIRQ_EXIT();
2305
2306 HARDIRQ_DISABLE();
2307 LOCK(B);
2308 read_lock(&rwlock_A);
2309 read_unlock(&rwlock_A);
2310 UNLOCK(B);
2311 HARDIRQ_ENABLE();
2312}
2313
2314/*
2315 * <in hardirq handler>
2316 * spin_lock(&B);
2317 * <hardirq disable>
2318 * read_lock(&A);
2319 * read_lock(&A);
2320 * spin_lock(&B);
2321 *
2322 * is not a deadlock.
2323 */
2324static void queued_read_lock_hardirq_ER_rE(void)
2325{
2326 HARDIRQ_ENTER();
2327 LOCK(B);
2328 read_lock(&rwlock_A);
2329 read_unlock(&rwlock_A);
2330 UNLOCK(B);
2331 HARDIRQ_EXIT();
2332
2333 HARDIRQ_DISABLE();
2334 read_lock(&rwlock_A);
2335 LOCK(B);
2336 UNLOCK(B);
2337 read_unlock(&rwlock_A);
2338 HARDIRQ_ENABLE();
2339}
2340
2341/*
2342 * <hardirq disable>
2343 * spin_lock(&B);
2344 * read_lock(&A);
2345 * <in hardirq handler>
2346 * spin_lock(&B);
2347 * read_lock(&A);
2348 *
2349 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2350 */
2351static void queued_read_lock_hardirq_inversion(void)
2352{
2353
2354 HARDIRQ_ENTER();
2355 LOCK(B);
2356 UNLOCK(B);
2357 HARDIRQ_EXIT();
2358
2359 HARDIRQ_DISABLE();
2360 LOCK(B);
2361 read_lock(&rwlock_A);
2362 read_unlock(&rwlock_A);
2363 UNLOCK(B);
2364 HARDIRQ_ENABLE();
2365
2366 read_lock(&rwlock_A);
2367 read_unlock(&rwlock_A);
2368}
2369
2370static void queued_read_lock_tests(void)
2371{
2372 printk(" --------------------------------------------------------------------------\n");
2373 printk(" | queued read lock tests |\n");
2374 printk(" ---------------------------\n");
2375 print_testname("hardirq read-lock/lock-read");
2376 dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
2377 pr_cont("\n");
2378
2379 print_testname("hardirq lock-read/read-lock");
2380 dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
2381 pr_cont("\n");
2382
2383 print_testname("hardirq inversion");
2384 dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
2385 pr_cont("\n");
2386}
2387
d5037d1d
DV
2388static void fs_reclaim_correct_nesting(void)
2389{
2390 fs_reclaim_acquire(GFP_KERNEL);
2391 might_alloc(GFP_NOFS);
2392 fs_reclaim_release(GFP_KERNEL);
2393}
2394
2395static void fs_reclaim_wrong_nesting(void)
2396{
2397 fs_reclaim_acquire(GFP_KERNEL);
2398 might_alloc(GFP_KERNEL);
2399 fs_reclaim_release(GFP_KERNEL);
2400}
2401
2402static void fs_reclaim_protected_nesting(void)
2403{
2404 unsigned int flags;
2405
2406 fs_reclaim_acquire(GFP_KERNEL);
2407 flags = memalloc_nofs_save();
2408 might_alloc(GFP_KERNEL);
2409 memalloc_nofs_restore(flags);
2410 fs_reclaim_release(GFP_KERNEL);
2411}
2412
2413static void fs_reclaim_tests(void)
2414{
2415 printk(" --------------------\n");
2416 printk(" | fs_reclaim tests |\n");
2417 printk(" --------------------\n");
2418
2419 print_testname("correct nesting");
2420 dotest(fs_reclaim_correct_nesting, SUCCESS, 0);
2421 pr_cont("\n");
2422
2423 print_testname("wrong nesting");
2424 dotest(fs_reclaim_wrong_nesting, FAILURE, 0);
2425 pr_cont("\n");
2426
2427 print_testname("protected nesting");
2428 dotest(fs_reclaim_protected_nesting, SUCCESS, 0);
2429 pr_cont("\n");
2430}
2431
9271a40d
BF
2432#define __guard(cleanup) __maybe_unused __attribute__((__cleanup__(cleanup)))
2433
2434static void hardirq_exit(int *_)
2435{
2436 HARDIRQ_EXIT();
2437}
2438
2439#define HARDIRQ_CONTEXT(name, ...) \
2440 int hardirq_guard_##name __guard(hardirq_exit); \
2441 HARDIRQ_ENTER();
2442
2443#define NOTTHREADED_HARDIRQ_CONTEXT(name, ...) \
2444 int notthreaded_hardirq_guard_##name __guard(hardirq_exit); \
2445 local_irq_disable(); \
2446 __irq_enter(); \
2447 WARN_ON(!in_irq());
2448
2449static void softirq_exit(int *_)
2450{
2451 SOFTIRQ_EXIT();
2452}
2453
2454#define SOFTIRQ_CONTEXT(name, ...) \
2455 int softirq_guard_##name __guard(softirq_exit); \
2456 SOFTIRQ_ENTER();
2457
2458static void rcu_exit(int *_)
2459{
2460 rcu_read_unlock();
2461}
2462
2463#define RCU_CONTEXT(name, ...) \
2464 int rcu_guard_##name __guard(rcu_exit); \
2465 rcu_read_lock();
2466
2467static void rcu_bh_exit(int *_)
2468{
2469 rcu_read_unlock_bh();
2470}
2471
2472#define RCU_BH_CONTEXT(name, ...) \
2473 int rcu_bh_guard_##name __guard(rcu_bh_exit); \
2474 rcu_read_lock_bh();
2475
2476static void rcu_sched_exit(int *_)
2477{
2478 rcu_read_unlock_sched();
2479}
2480
2481#define RCU_SCHED_CONTEXT(name, ...) \
2482 int rcu_sched_guard_##name __guard(rcu_sched_exit); \
2483 rcu_read_lock_sched();
2484
2485static void rcu_callback_exit(int *_)
2486{
2487 rcu_lock_release(&rcu_callback_map);
2488}
2489
2490#define RCU_CALLBACK_CONTEXT(name, ...) \
2491 int rcu_callback_guard_##name __guard(rcu_callback_exit); \
2492 rcu_lock_acquire(&rcu_callback_map);
2493
2494
2495static void raw_spinlock_exit(raw_spinlock_t **lock)
2496{
2497 raw_spin_unlock(*lock);
2498}
2499
2500#define RAW_SPINLOCK_CONTEXT(name, lock) \
2501 raw_spinlock_t *raw_spinlock_guard_##name __guard(raw_spinlock_exit) = &(lock); \
2502 raw_spin_lock(&(lock));
2503
2504static void spinlock_exit(spinlock_t **lock)
2505{
2506 spin_unlock(*lock);
2507}
2508
2509#define SPINLOCK_CONTEXT(name, lock) \
2510 spinlock_t *spinlock_guard_##name __guard(spinlock_exit) = &(lock); \
2511 spin_lock(&(lock));
2512
2513static void mutex_exit(struct mutex **lock)
2514{
2515 mutex_unlock(*lock);
2516}
2517
2518#define MUTEX_CONTEXT(name, lock) \
2519 struct mutex *mutex_guard_##name __guard(mutex_exit) = &(lock); \
2520 mutex_lock(&(lock));
2521
2522#define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock) \
2523 \
2524static void __maybe_unused inner##_in_##outer(void) \
2525{ \
2526 outer##_CONTEXT(_, outer_lock); \
2527 { \
2528 inner##_CONTEXT(_, inner_lock); \
2529 } \
2530}
2531
2532/*
2533 * wait contexts (considering PREEMPT_RT)
2534 *
2535 * o: inner is allowed in outer
2536 * x: inner is disallowed in outer
2537 *
2538 * \ inner | RCU | RAW_SPIN | SPIN | MUTEX
2539 * outer \ | | | |
2540 * ---------------+-------+----------+------+-------
2541 * HARDIRQ | o | o | o | x
2542 * ---------------+-------+----------+------+-------
2543 * NOTTHREADED_IRQ| o | o | x | x
2544 * ---------------+-------+----------+------+-------
2545 * SOFTIRQ | o | o | o | x
2546 * ---------------+-------+----------+------+-------
2547 * RCU | o | o | o | x
2548 * ---------------+-------+----------+------+-------
2549 * RCU_BH | o | o | o | x
2550 * ---------------+-------+----------+------+-------
2551 * RCU_CALLBACK | o | o | o | x
2552 * ---------------+-------+----------+------+-------
2553 * RCU_SCHED | o | o | x | x
2554 * ---------------+-------+----------+------+-------
2555 * RAW_SPIN | o | o | x | x
2556 * ---------------+-------+----------+------+-------
2557 * SPIN | o | o | o | x
2558 * ---------------+-------+----------+------+-------
2559 * MUTEX | o | o | o | o
2560 * ---------------+-------+----------+------+-------
2561 */
2562
2563#define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock) \
2564GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock) \
2565GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock) \
2566GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock) \
2567GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock) \
2568GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock) \
2569GENERATE_2_CONTEXT_TESTCASE(RCU_CALLBACK, , inner, inner_lock) \
2570GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock) \
2571GENERATE_2_CONTEXT_TESTCASE(RAW_SPINLOCK, raw_lock_A, inner, inner_lock) \
2572GENERATE_2_CONTEXT_TESTCASE(SPINLOCK, lock_A, inner, inner_lock) \
2573GENERATE_2_CONTEXT_TESTCASE(MUTEX, mutex_A, inner, inner_lock)
2574
2575GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU, )
2576GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RAW_SPINLOCK, raw_lock_B)
2577GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(SPINLOCK, lock_B)
2578GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(MUTEX, mutex_B)
2579
2580/* the outer context allows all kinds of preemption */
2581#define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer) \
2582 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2583 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2584 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2585 dotest(MUTEX_in_##outer, SUCCESS, LOCKTYPE_MUTEX); \
2586
2587/*
2588 * the outer context only allows the preemption introduced by spinlock_t (which
2589 * is a sleepable lock for PREEMPT_RT)
2590 */
2591#define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer) \
2592 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2593 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2594 dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2595 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2596
2597/* the outer doesn't allows any kind of preemption */
2598#define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer) \
2599 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \
2600 dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN); \
2601 dotest(SPINLOCK_in_##outer, FAILURE, LOCKTYPE_SPIN); \
2602 dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX); \
2603
2604static void wait_context_tests(void)
2605{
2606 printk(" --------------------------------------------------------------------------\n");
2607 printk(" | wait context tests |\n");
2608 printk(" --------------------------------------------------------------------------\n");
2609 printk(" | rcu | raw | spin |mutex |\n");
2610 printk(" --------------------------------------------------------------------------\n");
2611 print_testname("in hardirq context");
2612 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ);
2613 pr_cont("\n");
2614
2615 print_testname("in hardirq context (not threaded)");
2616 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ);
2617 pr_cont("\n");
2618
2619 print_testname("in softirq context");
2620 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ);
2621 pr_cont("\n");
2622
2623 print_testname("in RCU context");
2624 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU);
2625 pr_cont("\n");
2626
2627 print_testname("in RCU-bh context");
2628 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH);
2629 pr_cont("\n");
2630
2631 print_testname("in RCU callback context");
2632 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_CALLBACK);
2633 pr_cont("\n");
2634
2635 print_testname("in RCU-sched context");
2636 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED);
2637 pr_cont("\n");
2638
2639 print_testname("in RAW_SPINLOCK context");
2640 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RAW_SPINLOCK);
2641 pr_cont("\n");
2642
2643 print_testname("in SPINLOCK context");
2644 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SPINLOCK);
2645 pr_cont("\n");
2646
2647 print_testname("in MUTEX context");
2648 DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(MUTEX);
2649 pr_cont("\n");
2650}
2651
cae2ed9a
IM
2652void locking_selftest(void)
2653{
2654 /*
2655 * Got a locking failure before the selftest ran?
2656 */
2657 if (!debug_locks) {
2658 printk("----------------------------------\n");
2659 printk("| Locking API testsuite disabled |\n");
2660 printk("----------------------------------\n");
2661 return;
2662 }
2663
e9181886
BF
2664 /*
2665 * treats read_lock() as recursive read locks for testing purpose
2666 */
2667 force_read_lock_recursive = 1;
2668
cae2ed9a
IM
2669 /*
2670 * Run the testsuite:
2671 */
2672 printk("------------------------\n");
2673 printk("| Locking API testsuite:\n");
2674 printk("----------------------------------------------------------------------------\n");
2675 printk(" | spin |wlock |rlock |mutex | wsem | rsem |\n");
2676 printk(" --------------------------------------------------------------------------\n");
2677
2678 init_shared_classes();
cdc84d79 2679 lockdep_set_selftest_task(current);
cae2ed9a 2680
6c9076ec 2681 DO_TESTCASE_6R("A-A deadlock", AA);
cae2ed9a
IM
2682 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
2683 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
2684 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
2685 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
2686 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
2687 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
2688 DO_TESTCASE_6("double unlock", double_unlock);
2689 DO_TESTCASE_6("initialize held", init_held);
cae2ed9a
IM
2690
2691 printk(" --------------------------------------------------------------------------\n");
2692 print_testname("recursive read-lock");
25139409 2693 pr_cont(" |");
cae2ed9a 2694 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
25139409 2695 pr_cont(" |");
cae2ed9a 2696 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
25139409 2697 pr_cont("\n");
cae2ed9a
IM
2698
2699 print_testname("recursive read-lock #2");
25139409 2700 pr_cont(" |");
6c9076ec 2701 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
25139409 2702 pr_cont(" |");
cae2ed9a 2703 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
25139409 2704 pr_cont("\n");
cae2ed9a
IM
2705
2706 print_testname("mixed read-write-lock");
25139409 2707 pr_cont(" |");
cae2ed9a 2708 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
25139409 2709 pr_cont(" |");
cae2ed9a 2710 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
25139409 2711 pr_cont("\n");
cae2ed9a
IM
2712
2713 print_testname("mixed write-read-lock");
25139409 2714 pr_cont(" |");
cae2ed9a 2715 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
25139409 2716 pr_cont(" |");
cae2ed9a 2717 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
25139409 2718 pr_cont("\n");
cae2ed9a 2719
e9149858
PZ
2720 print_testname("mixed read-lock/lock-write ABBA");
2721 pr_cont(" |");
2722 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2723 pr_cont(" |");
2724 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
2725
2726 print_testname("mixed read-lock/lock-read ABBA");
2727 pr_cont(" |");
2728 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
2729 pr_cont(" |");
2730 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
2731
2732 print_testname("mixed write-lock/lock-write ABBA");
2733 pr_cont(" |");
2734 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
2735 pr_cont(" |");
2736 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
2737
d4f200e5
BF
2738 print_testname("chain cached mixed R-L/L-W ABBA");
2739 pr_cont(" |");
2740 dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
2741
8ef7ca75
BF
2742 DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
2743 DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
2744 DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
2745 DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
2746
cae2ed9a
IM
2747 printk(" --------------------------------------------------------------------------\n");
2748
2749 /*
2750 * irq-context testcases:
2751 */
2752 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
2753 DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
2754 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
2755 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
2756 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
2757 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
2758
31e0d747
BF
2759 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
2760 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
96a16f45 2761 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
cae2ed9a 2762
1de99445
ML
2763 ww_tests();
2764
e9181886
BF
2765 force_read_lock_recursive = 0;
2766 /*
2767 * queued_read_lock() specific test cases can be put here
2768 */
ad56450d
BF
2769 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
2770 queued_read_lock_tests();
e9181886 2771
d5037d1d
DV
2772 fs_reclaim_tests();
2773
9271a40d
BF
2774 /* Wait context test cases that are specific for RAW_LOCK_NESTING */
2775 if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING))
2776 wait_context_tests();
2777
cae2ed9a
IM
2778 if (unexpected_testcase_failures) {
2779 printk("-----------------------------------------------------------------\n");
2780 debug_locks = 0;
2781 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
2782 unexpected_testcase_failures, testcase_total);
2783 printk("-----------------------------------------------------------------\n");
2784 } else if (expected_testcase_failures && testcase_successes) {
2785 printk("--------------------------------------------------------\n");
2786 printk("%3d out of %3d testcases failed, as expected. |\n",
2787 expected_testcase_failures, testcase_total);
2788 printk("----------------------------------------------------\n");
2789 debug_locks = 1;
2790 } else if (expected_testcase_failures && !testcase_successes) {
2791 printk("--------------------------------------------------------\n");
2792 printk("All %3d testcases failed, as expected. |\n",
2793 expected_testcase_failures);
2794 printk("----------------------------------------\n");
2795 debug_locks = 1;
2796 } else {
2797 printk("-------------------------------------------------------\n");
2798 printk("Good, all %3d testcases passed! |\n",
2799 testcase_successes);
2800 printk("---------------------------------\n");
2801 debug_locks = 1;
2802 }
cdc84d79 2803 lockdep_set_selftest_task(NULL);
cae2ed9a
IM
2804 debug_locks_silent = 0;
2805}