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