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