Blame view

lib/locking-selftest.c 63.7 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * lib/locking-selftest.c
   *
   * Testsuite for various locking APIs: spinlocks, rwlocks,
   * mutexes and rw-semaphores.
   *
   * It is checking both false positives and false negatives.
   *
   * Started by Ingo Molnar:
   *
   *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
   */
  #include <linux/rwsem.h>
  #include <linux/mutex.h>
1b375dc30   Maarten Lankhorst   mutex: Move ww_mu...
16
  #include <linux/ww_mutex.h>
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
17
  #include <linux/sched.h>
d5037d1d8   Daniel Vetter   locking/selftests...
18
  #include <linux/sched/mm.h>
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
19
  #include <linux/delay.h>
fbb9ce953   Ingo Molnar   [PATCH] lockdep: ...
20
  #include <linux/lockdep.h>
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
21
22
23
24
25
  #include <linux/spinlock.h>
  #include <linux/kallsyms.h>
  #include <linux/interrupt.h>
  #include <linux/debug_locks.h>
  #include <linux/irqflags.h>
018956d64   Peter Zijlstra   locking/selftest:...
26
  #include <linux/rtmutex.h>
7e923e6a3   Peter Zijlstra   locking/selftests...
27
  #include <linux/local_lock.h>
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
28
29
30
31
32
  
  /*
   * Change this to 1 if you want to see the failure printouts:
   */
  static unsigned int debug_locks_verbose;
e91818861   Boqun Feng   locking: More acc...
33
  unsigned int force_read_lock_recursive;
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
34

08295b3b5   Thomas Hellstrom   locking: Implemen...
35
  static DEFINE_WD_CLASS(ww_lockdep);
1de994452   Maarten Lankhorst   mutex: Add w/w te...
36

cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  static int __init setup_debug_locks_verbose(char *str)
  {
  	get_option(&str, &debug_locks_verbose);
  
  	return 1;
  }
  
  __setup("debug_locks_verbose=", setup_debug_locks_verbose);
  
  #define FAILURE		0
  #define SUCCESS		1
  
  #define LOCKTYPE_SPIN	0x1
  #define LOCKTYPE_RWLOCK	0x2
  #define LOCKTYPE_MUTEX	0x4
  #define LOCKTYPE_RWSEM	0x8
1de994452   Maarten Lankhorst   mutex: Add w/w te...
53
  #define LOCKTYPE_WW	0x10
018956d64   Peter Zijlstra   locking/selftest:...
54
  #define LOCKTYPE_RTMUTEX 0x20
7e923e6a3   Peter Zijlstra   locking/selftests...
55
  #define LOCKTYPE_LL	0x40
8946ccc25   Boqun Feng   locking/selftests...
56
  #define LOCKTYPE_SPECIAL 0x80
1de994452   Maarten Lankhorst   mutex: Add w/w te...
57
58
  
  static struct ww_acquire_ctx t, t2;
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
59
  static struct ww_mutex o, o2, o3;
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
60
61
62
63
64
  
  /*
   * Normal standalone locks, for the circular and irq-context
   * dependency tests:
   */
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
65
66
67
68
  static DEFINE_SPINLOCK(lock_A);
  static DEFINE_SPINLOCK(lock_B);
  static DEFINE_SPINLOCK(lock_C);
  static DEFINE_SPINLOCK(lock_D);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
69

9271a40d2   Boqun Feng   lockdep/selftest:...
70
71
  static DEFINE_RAW_SPINLOCK(raw_lock_A);
  static DEFINE_RAW_SPINLOCK(raw_lock_B);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  static DEFINE_RWLOCK(rwlock_A);
  static DEFINE_RWLOCK(rwlock_B);
  static DEFINE_RWLOCK(rwlock_C);
  static DEFINE_RWLOCK(rwlock_D);
  
  static DEFINE_MUTEX(mutex_A);
  static DEFINE_MUTEX(mutex_B);
  static DEFINE_MUTEX(mutex_C);
  static DEFINE_MUTEX(mutex_D);
  
  static DECLARE_RWSEM(rwsem_A);
  static DECLARE_RWSEM(rwsem_B);
  static DECLARE_RWSEM(rwsem_C);
  static DECLARE_RWSEM(rwsem_D);
018956d64   Peter Zijlstra   locking/selftest:...
86
87
88
89
90
91
92
93
  #ifdef CONFIG_RT_MUTEXES
  
  static DEFINE_RT_MUTEX(rtmutex_A);
  static DEFINE_RT_MUTEX(rtmutex_B);
  static DEFINE_RT_MUTEX(rtmutex_C);
  static DEFINE_RT_MUTEX(rtmutex_D);
  
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
94
95
96
97
98
99
  /*
   * Locks that we initialize dynamically as well so that
   * e.g. X1 and X2 becomes two instances of the same class,
   * but X* and Y* are different classes. We do this so that
   * we do not trigger a real lockup:
   */
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
100
101
102
103
104
105
  static DEFINE_SPINLOCK(lock_X1);
  static DEFINE_SPINLOCK(lock_X2);
  static DEFINE_SPINLOCK(lock_Y1);
  static DEFINE_SPINLOCK(lock_Y2);
  static DEFINE_SPINLOCK(lock_Z1);
  static DEFINE_SPINLOCK(lock_Z2);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  
  static DEFINE_RWLOCK(rwlock_X1);
  static DEFINE_RWLOCK(rwlock_X2);
  static DEFINE_RWLOCK(rwlock_Y1);
  static DEFINE_RWLOCK(rwlock_Y2);
  static DEFINE_RWLOCK(rwlock_Z1);
  static DEFINE_RWLOCK(rwlock_Z2);
  
  static DEFINE_MUTEX(mutex_X1);
  static DEFINE_MUTEX(mutex_X2);
  static DEFINE_MUTEX(mutex_Y1);
  static DEFINE_MUTEX(mutex_Y2);
  static DEFINE_MUTEX(mutex_Z1);
  static DEFINE_MUTEX(mutex_Z2);
  
  static DECLARE_RWSEM(rwsem_X1);
  static DECLARE_RWSEM(rwsem_X2);
  static DECLARE_RWSEM(rwsem_Y1);
  static DECLARE_RWSEM(rwsem_Y2);
  static DECLARE_RWSEM(rwsem_Z1);
  static DECLARE_RWSEM(rwsem_Z2);
018956d64   Peter Zijlstra   locking/selftest:...
127
128
129
130
131
132
133
134
135
136
  #ifdef CONFIG_RT_MUTEXES
  
  static DEFINE_RT_MUTEX(rtmutex_X1);
  static DEFINE_RT_MUTEX(rtmutex_X2);
  static DEFINE_RT_MUTEX(rtmutex_Y1);
  static DEFINE_RT_MUTEX(rtmutex_Y2);
  static DEFINE_RT_MUTEX(rtmutex_Z1);
  static DEFINE_RT_MUTEX(rtmutex_Z2);
  
  #endif
7e923e6a3   Peter Zijlstra   locking/selftests...
137
  static local_lock_t local_A = INIT_LOCAL_LOCK(local_A);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
138
139
140
141
142
143
  /*
   * non-inlined runtime initializers, to let separate locks share
   * the same lock-class:
   */
  #define INIT_CLASS_FUNC(class) 				\
  static noinline void					\
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
144
  init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \
9fb1b90ce   Yong Zhang   lockdep: Selftest...
145
  	struct mutex *mutex, struct rw_semaphore *rwsem)\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
146
  {							\
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
147
  	spin_lock_init(lock);			\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
148
149
150
151
152
153
154
155
156
157
158
  	rwlock_init(rwlock);				\
  	mutex_init(mutex);				\
  	init_rwsem(rwsem);				\
  }
  
  INIT_CLASS_FUNC(X)
  INIT_CLASS_FUNC(Y)
  INIT_CLASS_FUNC(Z)
  
  static void init_shared_classes(void)
  {
018956d64   Peter Zijlstra   locking/selftest:...
159
160
161
162
163
164
165
166
167
168
  #ifdef CONFIG_RT_MUTEXES
  	static struct lock_class_key rt_X, rt_Y, rt_Z;
  
  	__rt_mutex_init(&rtmutex_X1, __func__, &rt_X);
  	__rt_mutex_init(&rtmutex_X2, __func__, &rt_X);
  	__rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);
  	__rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);
  	__rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);
  	__rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  	init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
  	init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
  
  	init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
  	init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
  
  	init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
  	init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
  }
  
  /*
   * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
   * The following functions use a lock from a simulated hardirq/softirq
   * context, causing the locks to be marked as hardirq-safe/softirq-safe:
   */
  
  #define HARDIRQ_DISABLE		local_irq_disable
  #define HARDIRQ_ENABLE		local_irq_enable
  
  #define HARDIRQ_ENTER()				\
  	local_irq_disable();			\
ba9f207c9   Frederic Weisbecker   rcu: Fix unpaired...
190
  	__irq_enter();				\
c0c2c0dad   Peter Zijlstra   lockdep/selftests...
191
  	lockdep_hardirq_threaded();		\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
192
193
194
195
196
197
198
199
200
201
202
203
  	WARN_ON(!in_irq());
  
  #define HARDIRQ_EXIT()				\
  	__irq_exit();				\
  	local_irq_enable();
  
  #define SOFTIRQ_DISABLE		local_bh_disable
  #define SOFTIRQ_ENABLE		local_bh_enable
  
  #define SOFTIRQ_ENTER()				\
  		local_bh_disable();		\
  		local_irq_disable();		\
d820ac4c2   Ingo Molnar   locking: rename t...
204
  		lockdep_softirq_enter();	\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
205
206
207
  		WARN_ON(!in_softirq());
  
  #define SOFTIRQ_EXIT()				\
d820ac4c2   Ingo Molnar   locking: rename t...
208
  		lockdep_softirq_exit();		\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
209
210
211
212
213
214
215
  		local_irq_enable();		\
  		local_bh_enable();
  
  /*
   * Shortcuts for lock/unlock API variants, to keep
   * the testcases compact:
   */
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
216
217
  #define L(x)			spin_lock(&lock_##x)
  #define U(x)			spin_unlock(&lock_##x)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
218
  #define LU(x)			L(x); U(x)
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
219
  #define SI(x)			spin_lock_init(&lock_##x)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
220
221
222
223
224
225
226
227
228
229
230
231
232
  
  #define WL(x)			write_lock(&rwlock_##x)
  #define WU(x)			write_unlock(&rwlock_##x)
  #define WLU(x)			WL(x); WU(x)
  
  #define RL(x)			read_lock(&rwlock_##x)
  #define RU(x)			read_unlock(&rwlock_##x)
  #define RLU(x)			RL(x); RU(x)
  #define RWI(x)			rwlock_init(&rwlock_##x)
  
  #define ML(x)			mutex_lock(&mutex_##x)
  #define MU(x)			mutex_unlock(&mutex_##x)
  #define MI(x)			mutex_init(&mutex_##x)
018956d64   Peter Zijlstra   locking/selftest:...
233
234
235
  #define RTL(x)			rt_mutex_lock(&rtmutex_##x)
  #define RTU(x)			rt_mutex_unlock(&rtmutex_##x)
  #define RTI(x)			rt_mutex_init(&rtmutex_##x)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
236
237
238
239
240
241
  #define WSL(x)			down_write(&rwsem_##x)
  #define WSU(x)			up_write(&rwsem_##x)
  
  #define RSL(x)			down_read(&rwsem_##x)
  #define RSU(x)			up_read(&rwsem_##x)
  #define RWSI(x)			init_rwsem(&rwsem_##x)
1de994452   Maarten Lankhorst   mutex: Add w/w te...
242
243
244
245
246
247
248
249
250
251
252
253
  #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  #define WWAI(x)			ww_acquire_init(x, &ww_lockdep)
  #else
  #define WWAI(x)			do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
  #endif
  #define WWAD(x)			ww_acquire_done(x)
  #define WWAF(x)			ww_acquire_fini(x)
  
  #define WWL(x, c)		ww_mutex_lock(x, c)
  #define WWT(x)			ww_mutex_trylock(x)
  #define WWL1(x)			ww_mutex_lock(x, NULL)
  #define WWU(x)			ww_mutex_unlock(x)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  #define LOCK_UNLOCK_2(x,y)	LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
  
  /*
   * Generate different permutations of the same testcase, using
   * the same basic lock-dependency/state events:
   */
  
  #define GENERATE_TESTCASE(name)			\
  						\
  static void name(void) { E(); }
  
  #define GENERATE_PERMUTATIONS_2_EVENTS(name)	\
  						\
  static void name##_12(void) { E1(); E2(); }	\
  static void name##_21(void) { E2(); E1(); }
  
  #define GENERATE_PERMUTATIONS_3_EVENTS(name)		\
  							\
  static void name##_123(void) { E1(); E2(); E3(); }	\
  static void name##_132(void) { E1(); E3(); E2(); }	\
  static void name##_213(void) { E2(); E1(); E3(); }	\
  static void name##_231(void) { E2(); E3(); E1(); }	\
  static void name##_312(void) { E3(); E1(); E2(); }	\
  static void name##_321(void) { E3(); E2(); E1(); }
  
  /*
   * AA deadlock:
   */
  
  #define E()					\
  						\
  	LOCK(X1);				\
  	LOCK(X2); /* this one should fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(AA_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(AA_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(AA_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(AA_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(AA_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(AA_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
303
304
305
306
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(AA_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
307
308
309
310
  #undef E
  
  /*
   * Special-case for read-locking, they are
6c9076ec9   Ingo Molnar   [PATCH] lockdep: ...
311
   * allowed to recurse on the same lock class:
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
312
313
314
315
316
317
318
319
320
321
   */
  static void rlock_AA1(void)
  {
  	RL(X1);
  	RL(X1); // this one should NOT fail
  }
  
  static void rlock_AA1B(void)
  {
  	RL(X1);
6c9076ec9   Ingo Molnar   [PATCH] lockdep: ...
322
  	RL(X2); // this one should NOT fail
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
  }
  
  static void rsem_AA1(void)
  {
  	RSL(X1);
  	RSL(X1); // this one should fail
  }
  
  static void rsem_AA1B(void)
  {
  	RSL(X1);
  	RSL(X2); // this one should fail
  }
  /*
   * The mixing of read and write locks is not allowed:
   */
  static void rlock_AA2(void)
  {
  	RL(X1);
  	WL(X2); // this one should fail
  }
  
  static void rsem_AA2(void)
  {
  	RSL(X1);
  	WSL(X2); // this one should fail
  }
  
  static void rlock_AA3(void)
  {
  	WL(X1);
  	RL(X2); // this one should fail
  }
  
  static void rsem_AA3(void)
  {
  	WSL(X1);
  	RSL(X2); // this one should fail
  }
  
  /*
e91498589   Peter Zijlstra   locking/lockdep/s...
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
   * read_lock(A)
   * spin_lock(B)
   *		spin_lock(B)
   *		write_lock(A)
   */
  static void rlock_ABBA1(void)
  {
  	RL(X1);
  	L(Y1);
  	U(Y1);
  	RU(X1);
  
  	L(Y1);
  	WL(X1);
  	WU(X1);
  	U(Y1); // should fail
  }
  
  static void rwsem_ABBA1(void)
  {
  	RSL(X1);
  	ML(Y1);
  	MU(Y1);
  	RSU(X1);
  
  	ML(Y1);
  	WSL(X1);
  	WSU(X1);
  	MU(Y1); // should fail
  }
  
  /*
   * read_lock(A)
   * spin_lock(B)
   *		spin_lock(B)
d4f200e57   Boqun Feng   lockdep/selftest:...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
   *		write_lock(A)
   *
   * This test case is aimed at poking whether the chain cache prevents us from
   * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
   * read/write locks, the following case may happen
   *
   * 	{ read_lock(A)->lock(B) dependency exists }
   *
   * 	P0:
   * 	lock(B);
   * 	read_lock(A);
   *
   *	{ Not a deadlock, B -> A is added in the chain cache }
   *
   *	P1:
   *	lock(B);
   *	write_lock(A);
   *
   *	{ B->A found in chain cache, not reported as a deadlock }
   *
   */
  static void rlock_chaincache_ABBA1(void)
  {
  	RL(X1);
  	L(Y1);
  	U(Y1);
  	RU(X1);
  
  	L(Y1);
  	RL(X1);
  	RU(X1);
  	U(Y1);
  
  	L(Y1);
  	WL(X1);
  	WU(X1);
  	U(Y1); // should fail
  }
  
  /*
   * read_lock(A)
   * spin_lock(B)
   *		spin_lock(B)
e91498589   Peter Zijlstra   locking/lockdep/s...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
   *		read_lock(A)
   */
  static void rlock_ABBA2(void)
  {
  	RL(X1);
  	L(Y1);
  	U(Y1);
  	RU(X1);
  
  	L(Y1);
  	RL(X1);
  	RU(X1);
  	U(Y1); // should NOT fail
  }
  
  static void rwsem_ABBA2(void)
  {
  	RSL(X1);
  	ML(Y1);
  	MU(Y1);
  	RSU(X1);
  
  	ML(Y1);
  	RSL(X1);
  	RSU(X1);
  	MU(Y1); // should fail
  }
  
  
  /*
   * write_lock(A)
   * spin_lock(B)
   *		spin_lock(B)
   *		write_lock(A)
   */
  static void rlock_ABBA3(void)
  {
  	WL(X1);
  	L(Y1);
  	U(Y1);
  	WU(X1);
  
  	L(Y1);
  	WL(X1);
  	WU(X1);
  	U(Y1); // should fail
  }
  
  static void rwsem_ABBA3(void)
  {
  	WSL(X1);
  	ML(Y1);
  	MU(Y1);
  	WSU(X1);
  
  	ML(Y1);
  	WSL(X1);
  	WSU(X1);
  	MU(Y1); // should fail
  }
  
  /*
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
   * ABBA deadlock:
   */
  
  #define E()					\
  						\
  	LOCK_UNLOCK_2(A, B);			\
  	LOCK_UNLOCK_2(B, A); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(ABBA_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(ABBA_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(ABBA_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(ABBA_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(ABBA_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(ABBA_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
527
528
529
530
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(ABBA_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  #undef E
  
  /*
   * AB BC CA deadlock:
   */
  
  #define E()					\
  						\
  	LOCK_UNLOCK_2(A, B);			\
  	LOCK_UNLOCK_2(B, C);			\
  	LOCK_UNLOCK_2(C, A); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(ABBCCA_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(ABBCCA_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(ABBCCA_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(ABBCCA_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(ABBCCA_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(ABBCCA_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
558
559
560
561
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(ABBCCA_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  #undef E
  
  /*
   * AB CA BC deadlock:
   */
  
  #define E()					\
  						\
  	LOCK_UNLOCK_2(A, B);			\
  	LOCK_UNLOCK_2(C, A);			\
  	LOCK_UNLOCK_2(B, C); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(ABCABC_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(ABCABC_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(ABCABC_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(ABCABC_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(ABCABC_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(ABCABC_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
589
590
591
592
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(ABCABC_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  #undef E
  
  /*
   * AB BC CD DA deadlock:
   */
  
  #define E()					\
  						\
  	LOCK_UNLOCK_2(A, B);			\
  	LOCK_UNLOCK_2(B, C);			\
  	LOCK_UNLOCK_2(C, D);			\
  	LOCK_UNLOCK_2(D, A); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(ABBCCDDA_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(ABBCCDDA_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(ABBCCDDA_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(ABBCCDDA_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(ABBCCDDA_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(ABBCCDDA_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
621
622
623
624
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(ABBCCDDA_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  #undef E
  
  /*
   * AB CD BD DA deadlock:
   */
  #define E()					\
  						\
  	LOCK_UNLOCK_2(A, B);			\
  	LOCK_UNLOCK_2(C, D);			\
  	LOCK_UNLOCK_2(B, D);			\
  	LOCK_UNLOCK_2(D, A); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(ABCDBDDA_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(ABCDBDDA_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(ABCDBDDA_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(ABCDBDDA_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(ABCDBDDA_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(ABCDBDDA_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
652
653
654
655
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(ABCDBDDA_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
  #undef E
  
  /*
   * AB CD BC DA deadlock:
   */
  #define E()					\
  						\
  	LOCK_UNLOCK_2(A, B);			\
  	LOCK_UNLOCK_2(C, D);			\
  	LOCK_UNLOCK_2(B, C);			\
  	LOCK_UNLOCK_2(D, A); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(ABCDBCDA_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(ABCDBCDA_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(ABCDBCDA_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(ABCDBCDA_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(ABCDBCDA_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(ABCDBCDA_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
683
684
685
686
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(ABCDBCDA_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
  #undef E
  
  /*
   * Double unlock:
   */
  #define E()					\
  						\
  	LOCK(A);				\
  	UNLOCK(A);				\
  	UNLOCK(A); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(double_unlock_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(double_unlock_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(double_unlock_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(double_unlock_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(double_unlock_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(double_unlock_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
713
714
715
716
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(double_unlock_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
717
718
719
  #undef E
  
  /*
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
   * initializing a held lock:
   */
  #define E()					\
  						\
  	LOCK(A);				\
  	INIT(A); /* fail */
  
  /*
   * 6 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_TESTCASE(init_held_spin)
  #include "locking-selftest-wlock.h"
  GENERATE_TESTCASE(init_held_wlock)
  #include "locking-selftest-rlock.h"
  GENERATE_TESTCASE(init_held_rlock)
  #include "locking-selftest-mutex.h"
  GENERATE_TESTCASE(init_held_mutex)
  #include "locking-selftest-wsem.h"
  GENERATE_TESTCASE(init_held_wsem)
  #include "locking-selftest-rsem.h"
  GENERATE_TESTCASE(init_held_rsem)
018956d64   Peter Zijlstra   locking/selftest:...
742
743
744
745
  #ifdef CONFIG_RT_MUTEXES
  #include "locking-selftest-rtmutex.h"
  GENERATE_TESTCASE(init_held_rtmutex);
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  #undef E
  
  /*
   * locking an irq-safe lock with irqs enabled:
   */
  #define E1()				\
  					\
  	IRQ_ENTER();			\
  	LOCK(A);			\
  	UNLOCK(A);			\
  	IRQ_EXIT();
  
  #define E2()				\
  					\
  	LOCK(A);			\
  	UNLOCK(A);
  
  /*
   * Generate 24 testcases:
   */
  #include "locking-selftest-spin-hardirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  
  #include "locking-selftest-rlock-hardirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  
  #include "locking-selftest-wlock-hardirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
  
  #include "locking-selftest-spin-softirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
  
  #include "locking-selftest-rlock-softirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
  
  #include "locking-selftest-wlock-softirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  
  #undef E1
  #undef E2
  
  /*
   * Enabling hardirqs with a softirq-safe lock held:
   */
  #define E1()				\
  					\
  	SOFTIRQ_ENTER();		\
  	LOCK(A);			\
  	UNLOCK(A);			\
  	SOFTIRQ_EXIT();
  
  #define E2()				\
  					\
  	HARDIRQ_DISABLE();		\
  	LOCK(A);			\
  	HARDIRQ_ENABLE();		\
  	UNLOCK(A);
  
  /*
   * Generate 12 testcases:
   */
  #include "locking-selftest-spin.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
  
  #include "locking-selftest-wlock.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
  
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  
  #undef E1
  #undef E2
  
  /*
   * Enabling irqs with an irq-safe lock held:
   */
  #define E1()				\
  					\
  	IRQ_ENTER();			\
  	LOCK(A);			\
  	UNLOCK(A);			\
  	IRQ_EXIT();
  
  #define E2()				\
  					\
  	IRQ_DISABLE();			\
  	LOCK(A);			\
  	IRQ_ENABLE();			\
  	UNLOCK(A);
  
  /*
   * Generate 24 testcases:
   */
  #include "locking-selftest-spin-hardirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  
  #include "locking-selftest-rlock-hardirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  
  #include "locking-selftest-wlock-hardirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
  
  #include "locking-selftest-spin-softirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
  
  #include "locking-selftest-rlock-softirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
  
  #include "locking-selftest-wlock-softirq.h"
  GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  
  #undef E1
  #undef E2
  
  /*
   * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
   */
  #define E1()				\
  					\
  	LOCK(A);			\
  	LOCK(B);			\
  	UNLOCK(B);			\
  	UNLOCK(A);			\
  
  #define E2()				\
  					\
  	LOCK(B);			\
  	UNLOCK(B);
  
  #define E3()				\
  					\
  	IRQ_ENTER();			\
  	LOCK(A);			\
  	UNLOCK(A);			\
  	IRQ_EXIT();
  
  /*
   * Generate 36 testcases:
   */
  #include "locking-selftest-spin-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  
  #include "locking-selftest-rlock-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  
  #include "locking-selftest-wlock-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
  
  #include "locking-selftest-spin-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
  
  #include "locking-selftest-rlock-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
  
  #include "locking-selftest-wlock-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  
  #undef E1
  #undef E2
  #undef E3
  
  /*
   * If a lock turns into softirq-safe, but earlier it took
   * a softirq-unsafe lock:
   */
  
  #define E1()				\
  	IRQ_DISABLE();			\
  	LOCK(A);			\
  	LOCK(B);			\
  	UNLOCK(B);			\
  	UNLOCK(A);			\
  	IRQ_ENABLE();
  
  #define E2()				\
  	LOCK(B);			\
  	UNLOCK(B);
  
  #define E3()				\
  	IRQ_ENTER();			\
  	LOCK(A);			\
  	UNLOCK(A);			\
  	IRQ_EXIT();
  
  /*
   * Generate 36 testcases:
   */
  #include "locking-selftest-spin-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  
  #include "locking-selftest-rlock-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  
  #include "locking-selftest-wlock-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
  
  #include "locking-selftest-spin-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
  
  #include "locking-selftest-rlock-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
  
  #include "locking-selftest-wlock-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  
  #undef E1
  #undef E2
  #undef E3
  
  /*
   * read-lock / write-lock irq inversion.
   *
   * Deadlock scenario:
   *
   * CPU#1 is at #1, i.e. it has write-locked A, but has not
   * taken B yet.
   *
   * CPU#2 is at #2, i.e. it has locked B.
   *
   * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
   *
   * The deadlock occurs because CPU#1 will spin on B, and CPU#2
   * will spin on A.
   */
  
  #define E1()				\
  					\
  	IRQ_DISABLE();			\
  	WL(A);				\
  	LOCK(B);			\
  	UNLOCK(B);			\
  	WU(A);				\
  	IRQ_ENABLE();
  
  #define E2()				\
  					\
  	LOCK(B);			\
  	UNLOCK(B);
  
  #define E3()				\
  					\
  	IRQ_ENTER();			\
  	RL(A);				\
  	RU(A);				\
  	IRQ_EXIT();
  
  /*
   * Generate 36 testcases:
   */
  #include "locking-selftest-spin-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
  
  #include "locking-selftest-rlock-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
  
  #include "locking-selftest-wlock-hardirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
  
  #include "locking-selftest-spin-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
  
  #include "locking-selftest-rlock-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
  
  #include "locking-selftest-wlock-softirq.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
  
  #undef E1
  #undef E2
  #undef E3
  
  /*
8ef7ca751   Boqun Feng   lockdep/selftest:...
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
   * write-read / write-read / write-read deadlock even if read is recursive
   */
  
  #define E1()				\
  					\
  	WL(X1);				\
  	RL(Y1);				\
  	RU(Y1);				\
  	WU(X1);
  
  #define E2()				\
  					\
  	WL(Y1);				\
  	RL(Z1);				\
  	RU(Z1);				\
  	WU(Y1);
  
  #define E3()				\
  					\
  	WL(Z1);				\
  	RL(X1);				\
  	RU(X1);				\
  	WU(Z1);
  
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
  
  #undef E1
  #undef E2
  #undef E3
  
  /*
   * write-write / read-read / write-read deadlock even if read is recursive
   */
  
  #define E1()				\
  					\
  	WL(X1);				\
  	WL(Y1);				\
  	WU(Y1);				\
  	WU(X1);
  
  #define E2()				\
  					\
  	RL(Y1);				\
  	RL(Z1);				\
  	RU(Z1);				\
  	RU(Y1);
  
  #define E3()				\
  					\
  	WL(Z1);				\
  	RL(X1);				\
  	RU(X1);				\
  	WU(Z1);
  
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
  
  #undef E1
  #undef E2
  #undef E3
  
  /*
   * write-write / read-read / read-write is not deadlock when read is recursive
   */
  
  #define E1()				\
  					\
  	WL(X1);				\
  	WL(Y1);				\
  	WU(Y1);				\
  	WU(X1);
  
  #define E2()				\
  					\
  	RL(Y1);				\
  	RL(Z1);				\
  	RU(Z1);				\
  	RU(Y1);
  
  #define E3()				\
  					\
  	RL(Z1);				\
  	WL(X1);				\
  	WU(X1);				\
  	RU(Z1);
  
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
  
  #undef E1
  #undef E2
  #undef E3
  
  /*
   * write-read / read-read / write-write is not deadlock when read is recursive
   */
  
  #define E1()				\
  					\
  	WL(X1);				\
  	RL(Y1);				\
  	RU(Y1);				\
  	WU(X1);
  
  #define E2()				\
  					\
  	RL(Y1);				\
  	RL(Z1);				\
  	RU(Z1);				\
  	RU(Y1);
  
  #define E3()				\
  					\
  	WL(Z1);				\
  	WL(X1);				\
  	WU(X1);				\
  	WU(Z1);
  
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
  
  #undef E1
  #undef E2
  #undef E3
  /*
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
   * read-lock / write-lock recursion that is actually safe.
   */
  
  #define E1()				\
  					\
  	IRQ_DISABLE();			\
  	WL(A);				\
  	WU(A);				\
  	IRQ_ENABLE();
  
  #define E2()				\
  					\
  	RL(A);				\
  	RU(A);				\
  
  #define E3()				\
  					\
  	IRQ_ENTER();			\
31e0d7477   Boqun Feng   lockdep/selftest:...
1163
  	LOCK(A);			\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1164
1165
  	L(B);				\
  	U(B);				\
31e0d7477   Boqun Feng   lockdep/selftest:...
1166
  	UNLOCK(A);			\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1167
1168
1169
  	IRQ_EXIT();
  
  /*
31e0d7477   Boqun Feng   lockdep/selftest:...
1170
   * Generate 24 testcases:
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1171
1172
   */
  #include "locking-selftest-hardirq.h"
31e0d7477   Boqun Feng   lockdep/selftest:...
1173
1174
1175
1176
1177
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
  
  #include "locking-selftest-wlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1178
1179
  
  #include "locking-selftest-softirq.h"
31e0d7477   Boqun Feng   lockdep/selftest:...
1180
1181
1182
1183
1184
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
  
  #include "locking-selftest-wlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
  
  #undef E1
  #undef E2
  #undef E3
  
  /*
   * read-lock / write-lock recursion that is unsafe.
   */
  
  #define E1()				\
  					\
  	IRQ_DISABLE();			\
  	L(B);				\
31e0d7477   Boqun Feng   lockdep/selftest:...
1198
1199
  	LOCK(A);			\
  	UNLOCK(A);			\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
  	U(B);				\
  	IRQ_ENABLE();
  
  #define E2()				\
  					\
  	RL(A);				\
  	RU(A);				\
  
  #define E3()				\
  					\
  	IRQ_ENTER();			\
  	L(B);				\
  	U(B);				\
  	IRQ_EXIT();
  
  /*
31e0d7477   Boqun Feng   lockdep/selftest:...
1216
   * Generate 24 testcases:
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1217
1218
   */
  #include "locking-selftest-hardirq.h"
31e0d7477   Boqun Feng   lockdep/selftest:...
1219
1220
1221
1222
1223
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
  
  #include "locking-selftest-wlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1224
1225
  
  #include "locking-selftest-softirq.h"
31e0d7477   Boqun Feng   lockdep/selftest:...
1226
1227
1228
1229
1230
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
  
  #include "locking-selftest-wlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1231

96a16f45a   Boqun Feng   lockdep/selftest:...
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
  #undef E1
  #undef E2
  #undef E3
  /*
   * read-lock / write-lock recursion that is unsafe.
   *
   * A is a ENABLED_*_READ lock
   * B is a USED_IN_*_READ lock
   *
   * read_lock(A);
   *			write_lock(B);
   * <interrupt>
   * read_lock(B);
   * 			write_lock(A); // if this one is read_lock(), no deadlock
   */
  
  #define E1()				\
  					\
  	IRQ_DISABLE();			\
  	WL(B);				\
  	LOCK(A);			\
  	UNLOCK(A);			\
  	WU(B);				\
  	IRQ_ENABLE();
  
  #define E2()				\
  					\
  	RL(A);				\
  	RU(A);				\
  
  #define E3()				\
  					\
  	IRQ_ENTER();			\
  	RL(B);				\
  	RU(B);				\
  	IRQ_EXIT();
  
  /*
   * Generate 24 testcases:
   */
  #include "locking-selftest-hardirq.h"
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
  
  #include "locking-selftest-wlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
  
  #include "locking-selftest-softirq.h"
  #include "locking-selftest-rlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
  
  #include "locking-selftest-wlock.h"
  GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1285
1286
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  # define I_SPINLOCK(x)	lockdep_reset_lock(&lock_##x.dep_map)
9271a40d2   Boqun Feng   lockdep/selftest:...
1287
  # define I_RAW_SPINLOCK(x)	lockdep_reset_lock(&raw_lock_##x.dep_map)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1288
1289
1290
  # define I_RWLOCK(x)	lockdep_reset_lock(&rwlock_##x.dep_map)
  # define I_MUTEX(x)	lockdep_reset_lock(&mutex_##x.dep_map)
  # define I_RWSEM(x)	lockdep_reset_lock(&rwsem_##x.dep_map)
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1291
  # define I_WW(x)	lockdep_reset_lock(&x.dep_map)
7e923e6a3   Peter Zijlstra   locking/selftests...
1292
  # define I_LOCAL_LOCK(x) lockdep_reset_lock(&local_##x.dep_map)
018956d64   Peter Zijlstra   locking/selftest:...
1293
1294
1295
  #ifdef CONFIG_RT_MUTEXES
  # define I_RTMUTEX(x)	lockdep_reset_lock(&rtmutex_##x.dep_map)
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1296
1297
  #else
  # define I_SPINLOCK(x)
9271a40d2   Boqun Feng   lockdep/selftest:...
1298
  # define I_RAW_SPINLOCK(x)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1299
1300
1301
  # define I_RWLOCK(x)
  # define I_MUTEX(x)
  # define I_RWSEM(x)
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1302
  # define I_WW(x)
7e923e6a3   Peter Zijlstra   locking/selftests...
1303
  # define I_LOCAL_LOCK(x)
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1304
  #endif
018956d64   Peter Zijlstra   locking/selftest:...
1305
1306
1307
1308
1309
1310
1311
1312
1313
  #ifndef I_RTMUTEX
  # define I_RTMUTEX(x)
  #endif
  
  #ifdef CONFIG_RT_MUTEXES
  #define I2_RTMUTEX(x)	rt_mutex_init(&rtmutex_##x)
  #else
  #define I2_RTMUTEX(x)
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1314
1315
1316
1317
1318
1319
  #define I1(x)					\
  	do {					\
  		I_SPINLOCK(x);			\
  		I_RWLOCK(x);			\
  		I_MUTEX(x);			\
  		I_RWSEM(x);			\
018956d64   Peter Zijlstra   locking/selftest:...
1320
  		I_RTMUTEX(x);			\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1321
1322
1323
1324
  	} while (0)
  
  #define I2(x)					\
  	do {					\
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
1325
  		spin_lock_init(&lock_##x);	\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1326
1327
1328
  		rwlock_init(&rwlock_##x);	\
  		mutex_init(&mutex_##x);		\
  		init_rwsem(&rwsem_##x);		\
018956d64   Peter Zijlstra   locking/selftest:...
1329
  		I2_RTMUTEX(x);			\
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1330
1331
1332
1333
1334
  	} while (0)
  
  static void reset_locks(void)
  {
  	local_irq_disable();
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1335
1336
  	lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
  	lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1337
1338
  	I1(A); I1(B); I1(C); I1(D);
  	I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1339
  	I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
9271a40d2   Boqun Feng   lockdep/selftest:...
1340
  	I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B);
7e923e6a3   Peter Zijlstra   locking/selftests...
1341
  	I_LOCAL_LOCK(A);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1342
  	lockdep_reset();
7e923e6a3   Peter Zijlstra   locking/selftests...
1343

cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1344
1345
  	I2(A); I2(B); I2(C); I2(D);
  	init_shared_classes();
9271a40d2   Boqun Feng   lockdep/selftest:...
1346
1347
  	raw_spin_lock_init(&raw_lock_A);
  	raw_spin_lock_init(&raw_lock_B);
7e923e6a3   Peter Zijlstra   locking/selftests...
1348
  	local_lock_init(&local_A);
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1349

f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1350
  	ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1351
1352
1353
  	memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
  	memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
  	memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
  	local_irq_enable();
  }
  
  #undef I
  
  static int testcase_total;
  static int testcase_successes;
  static int expected_testcase_failures;
  static int unexpected_testcase_failures;
  
  static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
  {
  	unsigned long saved_preempt_count = preempt_count();
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1367
1368
  
  	WARN_ON(irqs_disabled());
5831c0f71   Peter Zijlstra   locking/selftests...
1369
  	debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1370
1371
1372
1373
1374
  	testcase_fn();
  	/*
  	 * Filter out expected failures:
  	 */
  #ifndef CONFIG_PROVE_LOCKING
166989e36   Maarten Lankhorst   locking-selftests...
1375
  	if (expected == FAILURE && debug_locks) {
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1376
  		expected_testcase_failures++;
251394098   Michael Ellerman   locking/selftest:...
1377
  		pr_cont("failed|");
166989e36   Maarten Lankhorst   locking-selftests...
1378
1379
1380
1381
  	}
  	else
  #endif
  	if (debug_locks != expected) {
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1382
  		unexpected_testcase_failures++;
251394098   Michael Ellerman   locking/selftest:...
1383
  		pr_cont("FAILED|");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1384
1385
  	} else {
  		testcase_successes++;
251394098   Michael Ellerman   locking/selftest:...
1386
  		pr_cont("  ok  |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1387
1388
  	}
  	testcase_total++;
5831c0f71   Peter Zijlstra   locking/selftests...
1389
  	if (debug_locks_verbose & lockclass_mask)
251394098   Michael Ellerman   locking/selftest:...
1390
1391
  		pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d
  ",
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1392
1393
1394
1395
1396
  			lockclass_mask, debug_locks, expected);
  	/*
  	 * Some tests (e.g. double-unlock) might corrupt the preemption
  	 * count, so restore it:
  	 */
4a2b4b222   Peter Zijlstra   sched: Introduce ...
1397
  	preempt_count_set(saved_preempt_count);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1398
1399
1400
1401
1402
1403
1404
1405
1406
  #ifdef CONFIG_TRACE_IRQFLAGS
  	if (softirq_count())
  		current->softirqs_enabled = 0;
  	else
  		current->softirqs_enabled = 1;
  #endif
  
  	reset_locks();
  }
018956d64   Peter Zijlstra   locking/selftest:...
1407
1408
1409
1410
1411
  #ifdef CONFIG_RT_MUTEXES
  #define dotest_rt(fn, e, m)	dotest((fn), (e), (m))
  #else
  #define dotest_rt(fn, e, m)
  #endif
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1412
1413
1414
1415
1416
1417
1418
1419
  static inline void print_testname(const char *testname)
  {
  	printk("%33s:", testname);
  }
  
  #define DO_TESTCASE_1(desc, name, nr)				\
  	print_testname(desc"/"#nr);				\
  	dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);		\
251394098   Michael Ellerman   locking/selftest:...
1420
1421
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1422
1423
1424
1425
  
  #define DO_TESTCASE_1B(desc, name, nr)				\
  	print_testname(desc"/"#nr);				\
  	dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);		\
251394098   Michael Ellerman   locking/selftest:...
1426
1427
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1428

8ef7ca751   Boqun Feng   lockdep/selftest:...
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
  #define DO_TESTCASE_1RR(desc, name, nr)				\
  	print_testname(desc"/"#nr);				\
  	pr_cont("             |");				\
  	dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);		\
  	pr_cont("
  ");
  
  #define DO_TESTCASE_1RRB(desc, name, nr)			\
  	print_testname(desc"/"#nr);				\
  	pr_cont("             |");				\
  	dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);		\
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1442
1443
1444
1445
1446
  #define DO_TESTCASE_3(desc, name, nr)				\
  	print_testname(desc"/"#nr);				\
  	dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN);	\
  	dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);	\
  	dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);	\
251394098   Michael Ellerman   locking/selftest:...
1447
1448
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1449
1450
1451
1452
1453
1454
  
  #define DO_TESTCASE_3RW(desc, name, nr)				\
  	print_testname(desc"/"#nr);				\
  	dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
  	dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);	\
  	dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);	\
251394098   Michael Ellerman   locking/selftest:...
1455
1456
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1457

31e0d7477   Boqun Feng   lockdep/selftest:...
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
  #define DO_TESTCASE_2RW(desc, name, nr)				\
  	print_testname(desc"/"#nr);				\
  	pr_cont("      |");					\
  	dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);	\
  	dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);	\
  	pr_cont("
  ");
  
  #define DO_TESTCASE_2x2RW(desc, name, nr)			\
  	DO_TESTCASE_2RW("hard-"desc, name##_hard, nr)		\
  	DO_TESTCASE_2RW("soft-"desc, name##_soft, nr)		\
  
  #define DO_TESTCASE_6x2x2RW(desc, name)				\
  	DO_TESTCASE_2x2RW(desc, name, 123);			\
  	DO_TESTCASE_2x2RW(desc, name, 132);			\
  	DO_TESTCASE_2x2RW(desc, name, 213);			\
  	DO_TESTCASE_2x2RW(desc, name, 231);			\
  	DO_TESTCASE_2x2RW(desc, name, 312);			\
  	DO_TESTCASE_2x2RW(desc, name, 321);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1477
1478
1479
1480
1481
1482
1483
1484
  #define DO_TESTCASE_6(desc, name)				\
  	print_testname(desc);					\
  	dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);		\
  	dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);		\
  	dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK);		\
  	dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);		\
  	dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);		\
  	dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);		\
018956d64   Peter Zijlstra   locking/selftest:...
1485
  	dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX);	\
251394098   Michael Ellerman   locking/selftest:...
1486
1487
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1488
1489
1490
1491
1492
1493
1494
1495
1496
  
  #define DO_TESTCASE_6_SUCCESS(desc, name)			\
  	print_testname(desc);					\
  	dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN);		\
  	dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK);		\
  	dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);		\
  	dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX);		\
  	dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM);		\
  	dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM);		\
018956d64   Peter Zijlstra   locking/selftest:...
1497
  	dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX);	\
251394098   Michael Ellerman   locking/selftest:...
1498
1499
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
  
  /*
   * 'read' variant: rlocks must not trigger.
   */
  #define DO_TESTCASE_6R(desc, name)				\
  	print_testname(desc);					\
  	dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);		\
  	dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);		\
  	dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);		\
  	dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);		\
  	dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);		\
  	dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);		\
018956d64   Peter Zijlstra   locking/selftest:...
1512
  	dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX);	\
251394098   Michael Ellerman   locking/selftest:...
1513
1514
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
  
  #define DO_TESTCASE_2I(desc, name, nr)				\
  	DO_TESTCASE_1("hard-"desc, name##_hard, nr);		\
  	DO_TESTCASE_1("soft-"desc, name##_soft, nr);
  
  #define DO_TESTCASE_2IB(desc, name, nr)				\
  	DO_TESTCASE_1B("hard-"desc, name##_hard, nr);		\
  	DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
  
  #define DO_TESTCASE_6I(desc, name, nr)				\
  	DO_TESTCASE_3("hard-"desc, name##_hard, nr);		\
  	DO_TESTCASE_3("soft-"desc, name##_soft, nr);
  
  #define DO_TESTCASE_6IRW(desc, name, nr)			\
  	DO_TESTCASE_3RW("hard-"desc, name##_hard, nr);		\
  	DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
  
  #define DO_TESTCASE_2x3(desc, name)				\
  	DO_TESTCASE_3(desc, name, 12);				\
  	DO_TESTCASE_3(desc, name, 21);
  
  #define DO_TESTCASE_2x6(desc, name)				\
  	DO_TESTCASE_6I(desc, name, 12);				\
  	DO_TESTCASE_6I(desc, name, 21);
  
  #define DO_TESTCASE_6x2(desc, name)				\
  	DO_TESTCASE_2I(desc, name, 123);			\
  	DO_TESTCASE_2I(desc, name, 132);			\
  	DO_TESTCASE_2I(desc, name, 213);			\
  	DO_TESTCASE_2I(desc, name, 231);			\
  	DO_TESTCASE_2I(desc, name, 312);			\
  	DO_TESTCASE_2I(desc, name, 321);
  
  #define DO_TESTCASE_6x2B(desc, name)				\
  	DO_TESTCASE_2IB(desc, name, 123);			\
  	DO_TESTCASE_2IB(desc, name, 132);			\
  	DO_TESTCASE_2IB(desc, name, 213);			\
  	DO_TESTCASE_2IB(desc, name, 231);			\
  	DO_TESTCASE_2IB(desc, name, 312);			\
  	DO_TESTCASE_2IB(desc, name, 321);
8ef7ca751   Boqun Feng   lockdep/selftest:...
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
  #define DO_TESTCASE_6x1RR(desc, name)				\
  	DO_TESTCASE_1RR(desc, name, 123);			\
  	DO_TESTCASE_1RR(desc, name, 132);			\
  	DO_TESTCASE_1RR(desc, name, 213);			\
  	DO_TESTCASE_1RR(desc, name, 231);			\
  	DO_TESTCASE_1RR(desc, name, 312);			\
  	DO_TESTCASE_1RR(desc, name, 321);
  
  #define DO_TESTCASE_6x1RRB(desc, name)				\
  	DO_TESTCASE_1RRB(desc, name, 123);			\
  	DO_TESTCASE_1RRB(desc, name, 132);			\
  	DO_TESTCASE_1RRB(desc, name, 213);			\
  	DO_TESTCASE_1RRB(desc, name, 231);			\
  	DO_TESTCASE_1RRB(desc, name, 312);			\
  	DO_TESTCASE_1RRB(desc, name, 321);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
  #define DO_TESTCASE_6x6(desc, name)				\
  	DO_TESTCASE_6I(desc, name, 123);			\
  	DO_TESTCASE_6I(desc, name, 132);			\
  	DO_TESTCASE_6I(desc, name, 213);			\
  	DO_TESTCASE_6I(desc, name, 231);			\
  	DO_TESTCASE_6I(desc, name, 312);			\
  	DO_TESTCASE_6I(desc, name, 321);
  
  #define DO_TESTCASE_6x6RW(desc, name)				\
  	DO_TESTCASE_6IRW(desc, name, 123);			\
  	DO_TESTCASE_6IRW(desc, name, 132);			\
  	DO_TESTCASE_6IRW(desc, name, 213);			\
  	DO_TESTCASE_6IRW(desc, name, 231);			\
  	DO_TESTCASE_6IRW(desc, name, 312);			\
  	DO_TESTCASE_6IRW(desc, name, 321);
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
  static void ww_test_fail_acquire(void)
  {
  	int ret;
  
  	WWAI(&t);
  	t.stamp++;
  
  	ret = WWL(&o, &t);
  
  	if (WARN_ON(!o.ctx) ||
  	    WARN_ON(ret))
  		return;
  
  	/* No lockdep test, pure API */
  	ret = WWL(&o, &t);
  	WARN_ON(ret != -EALREADY);
  
  	ret = WWT(&o);
  	WARN_ON(ret);
  
  	t2 = t;
  	t2.stamp++;
  	ret = WWL(&o, &t2);
  	WARN_ON(ret != -EDEADLK);
  	WWU(&o);
  
  	if (WWT(&o))
  		WWU(&o);
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  	else
  		DEBUG_LOCKS_WARN_ON(1);
  #endif
  }
2fe3d4b14   Maarten Lankhorst   mutex: Add more t...
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
  static void ww_test_normal(void)
  {
  	int ret;
  
  	WWAI(&t);
  
  	/*
  	 * None of the ww_mutex codepaths should be taken in the 'normal'
  	 * mutex calls. The easiest way to verify this is by using the
  	 * normal mutex calls, and making sure o.ctx is unmodified.
  	 */
  
  	/* mutex_lock (and indirectly, mutex_lock_nested) */
  	o.ctx = (void *)~0UL;
  	mutex_lock(&o.base);
  	mutex_unlock(&o.base);
  	WARN_ON(o.ctx != (void *)~0UL);
  
  	/* mutex_lock_interruptible (and *_nested) */
  	o.ctx = (void *)~0UL;
  	ret = mutex_lock_interruptible(&o.base);
  	if (!ret)
  		mutex_unlock(&o.base);
  	else
  		WARN_ON(1);
  	WARN_ON(o.ctx != (void *)~0UL);
  
  	/* mutex_lock_killable (and *_nested) */
  	o.ctx = (void *)~0UL;
  	ret = mutex_lock_killable(&o.base);
  	if (!ret)
  		mutex_unlock(&o.base);
  	else
  		WARN_ON(1);
  	WARN_ON(o.ctx != (void *)~0UL);
  
  	/* trylock, succeeding */
  	o.ctx = (void *)~0UL;
  	ret = mutex_trylock(&o.base);
  	WARN_ON(!ret);
  	if (ret)
  		mutex_unlock(&o.base);
  	else
  		WARN_ON(1);
  	WARN_ON(o.ctx != (void *)~0UL);
  
  	/* trylock, failing */
  	o.ctx = (void *)~0UL;
  	mutex_lock(&o.base);
  	ret = mutex_trylock(&o.base);
  	WARN_ON(ret);
  	mutex_unlock(&o.base);
  	WARN_ON(o.ctx != (void *)~0UL);
  
  	/* nest_lock */
  	o.ctx = (void *)~0UL;
  	mutex_lock_nest_lock(&o.base, &t);
  	mutex_unlock(&o.base);
  	WARN_ON(o.ctx != (void *)~0UL);
  }
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
  static void ww_test_two_contexts(void)
  {
  	WWAI(&t);
  	WWAI(&t2);
  }
  
  static void ww_test_diff_class(void)
  {
  	WWAI(&t);
  #ifdef CONFIG_DEBUG_MUTEXES
  	t.ww_class = NULL;
  #endif
  	WWL(&o, &t);
  }
  
  static void ww_test_context_done_twice(void)
  {
  	WWAI(&t);
  	WWAD(&t);
  	WWAD(&t);
  	WWAF(&t);
  }
  
  static void ww_test_context_unlock_twice(void)
  {
  	WWAI(&t);
  	WWAD(&t);
  	WWAF(&t);
  	WWAF(&t);
  }
  
  static void ww_test_context_fini_early(void)
  {
  	WWAI(&t);
  	WWL(&o, &t);
  	WWAD(&t);
  	WWAF(&t);
  }
  
  static void ww_test_context_lock_after_done(void)
  {
  	WWAI(&t);
  	WWAD(&t);
  	WWL(&o, &t);
  }
  
  static void ww_test_object_unlock_twice(void)
  {
  	WWL1(&o);
  	WWU(&o);
  	WWU(&o);
  }
  
  static void ww_test_object_lock_unbalanced(void)
  {
  	WWAI(&t);
  	WWL(&o, &t);
  	t.acquired = 0;
  	WWU(&o);
  	WWAF(&t);
  }
  
  static void ww_test_object_lock_stale_context(void)
  {
  	WWAI(&t);
  	o.ctx = &t2;
  	WWL(&o, &t);
  }
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1746
1747
1748
1749
1750
1751
  static void ww_test_edeadlk_normal(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
  	o2.ctx = &t2;
5facae4f3   Qian Cai   locking/lockdep: ...
1752
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	o2.ctx = NULL;
  	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  	mutex_unlock(&o2.base);
  	WWU(&o);
  
  	WWL(&o2, &t);
  }
  
  static void ww_test_edeadlk_normal_slow(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1777
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
  	o2.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	o2.ctx = NULL;
  	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  	mutex_unlock(&o2.base);
  	WWU(&o);
  
  	ww_mutex_lock_slow(&o2, &t);
  }
  
  static void ww_test_edeadlk_no_unlock(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
  	o2.ctx = &t2;
5facae4f3   Qian Cai   locking/lockdep: ...
1804
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	o2.ctx = NULL;
  	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  	mutex_unlock(&o2.base);
  
  	WWL(&o2, &t);
  }
  
  static void ww_test_edeadlk_no_unlock_slow(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1828
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
  	o2.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	o2.ctx = NULL;
  	mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
  	mutex_unlock(&o2.base);
  
  	ww_mutex_lock_slow(&o2, &t);
  }
  
  static void ww_test_edeadlk_acquire_more(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1853
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
  	o2.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	ret = WWL(&o3, &t);
  }
  
  static void ww_test_edeadlk_acquire_more_slow(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1874
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
  	o2.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	ww_mutex_lock_slow(&o3, &t);
  }
  
  static void ww_test_edeadlk_acquire_more_edeadlk(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1895
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1896
1897
1898
  	o2.ctx = &t2;
  
  	mutex_lock(&o3.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1899
  	mutex_release(&o3.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
  	o3.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	ret = WWL(&o3, &t);
  	WARN_ON(ret != -EDEADLK);
  }
  
  static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1921
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1922
1923
1924
  	o2.ctx = &t2;
  
  	mutex_lock(&o3.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1925
  	mutex_release(&o3.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
  	o3.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  
  	ww_mutex_lock_slow(&o3, &t);
  }
  
  static void ww_test_edeadlk_acquire_wrong(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1946
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
  	o2.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  	if (!ret)
  		WWU(&o2);
  
  	WWU(&o);
  
  	ret = WWL(&o3, &t);
  }
  
  static void ww_test_edeadlk_acquire_wrong_slow(void)
  {
  	int ret;
  
  	mutex_lock(&o2.base);
5facae4f3   Qian Cai   locking/lockdep: ...
1971
  	mutex_release(&o2.base.dep_map, _THIS_IP_);
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
  	o2.ctx = &t2;
  
  	WWAI(&t);
  	t2 = t;
  	t2.stamp--;
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret != -EDEADLK);
  	if (!ret)
  		WWU(&o2);
  
  	WWU(&o);
  
  	ww_mutex_lock_slow(&o3, &t);
  }
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1990
1991
  static void ww_test_spin_nest_unlocked(void)
  {
a2e9ae58d   Peter Zijlstra   lockdep/selftests...
1992
  	spin_lock_nest_lock(&lock_A, &o.base);
1de994452   Maarten Lankhorst   mutex: Add w/w te...
1993
1994
  	U(A);
  }
e04ce676e   Boqun Feng   lockdep/selftest:...
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
  /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
  static void ww_test_spin_nest_lock(void)
  {
  	spin_lock(&lock_X1);
  	spin_lock_nest_lock(&lock_Y1, &lock_X1);
  	spin_lock(&lock_A);
  	spin_lock_nest_lock(&lock_Y2, &lock_X1);
  	spin_unlock(&lock_A);
  	spin_unlock(&lock_Y2);
  	spin_unlock(&lock_Y1);
  	spin_unlock(&lock_X1);
  }
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
  static void ww_test_unneeded_slow(void)
  {
  	WWAI(&t);
  
  	ww_mutex_lock_slow(&o, &t);
  }
  
  static void ww_test_context_block(void)
  {
  	int ret;
  
  	WWAI(&t);
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  	WWL1(&o2);
  }
  
  static void ww_test_context_try(void)
  {
  	int ret;
  
  	WWAI(&t);
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWT(&o2);
  	WARN_ON(!ret);
  	WWU(&o2);
  	WWU(&o);
  }
  
  static void ww_test_context_context(void)
  {
  	int ret;
  
  	WWAI(&t);
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret);
  
  	WWU(&o2);
  	WWU(&o);
  }
  
  static void ww_test_try_block(void)
  {
  	bool ret;
  
  	ret = WWT(&o);
  	WARN_ON(!ret);
  
  	WWL1(&o2);
  	WWU(&o2);
  	WWU(&o);
  }
  
  static void ww_test_try_try(void)
  {
  	bool ret;
  
  	ret = WWT(&o);
  	WARN_ON(!ret);
  	ret = WWT(&o2);
  	WARN_ON(!ret);
  	WWU(&o2);
  	WWU(&o);
  }
  
  static void ww_test_try_context(void)
  {
  	int ret;
  
  	ret = WWT(&o);
  	WARN_ON(!ret);
  
  	WWAI(&t);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret);
  }
  
  static void ww_test_block_block(void)
  {
  	WWL1(&o);
  	WWL1(&o2);
  }
  
  static void ww_test_block_try(void)
  {
  	bool ret;
  
  	WWL1(&o);
  	ret = WWT(&o2);
  	WARN_ON(!ret);
  }
  
  static void ww_test_block_context(void)
  {
  	int ret;
  
  	WWL1(&o);
  	WWAI(&t);
  
  	ret = WWL(&o2, &t);
  	WARN_ON(ret);
  }
  
  static void ww_test_spin_block(void)
  {
  	L(A);
  	U(A);
  
  	WWL1(&o);
  	L(A);
  	U(A);
  	WWU(&o);
  
  	L(A);
  	WWL1(&o);
  	WWU(&o);
  	U(A);
  }
  
  static void ww_test_spin_try(void)
  {
  	bool ret;
  
  	L(A);
  	U(A);
  
  	ret = WWT(&o);
  	WARN_ON(!ret);
  	L(A);
  	U(A);
  	WWU(&o);
  
  	L(A);
  	ret = WWT(&o);
  	WARN_ON(!ret);
  	WWU(&o);
  	U(A);
  }
  
  static void ww_test_spin_context(void)
  {
  	int ret;
  
  	L(A);
  	U(A);
  
  	WWAI(&t);
  
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  	L(A);
  	U(A);
  	WWU(&o);
  
  	L(A);
  	ret = WWL(&o, &t);
  	WARN_ON(ret);
  	WWU(&o);
  	U(A);
  }
  
  static void ww_tests(void)
  {
  	printk("  --------------------------------------------------------------------------
  ");
  	printk("  | Wound/wait tests |
  ");
  	printk("  ---------------------
  ");
  
  	print_testname("ww api failures");
  	dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
2fe3d4b14   Maarten Lankhorst   mutex: Add more t...
2188
  	dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2189
  	dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2190
2191
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2192
2193
2194
2195
  
  	print_testname("ww contexts mixing");
  	dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2196
2197
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2198
2199
2200
2201
2202
2203
  
  	print_testname("finishing ww context");
  	dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2204
2205
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2206
2207
2208
2209
2210
  
  	print_testname("locking mismatches");
  	dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2211
2212
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2213

f3cf139ef   Maarten Lankhorst   mutex: Add more w...
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
  	print_testname("EDEADLK handling");
  	dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2225
2226
  	pr_cont("
  ");
f3cf139ef   Maarten Lankhorst   mutex: Add more w...
2227

1de994452   Maarten Lankhorst   mutex: Add w/w te...
2228
2229
  	print_testname("spinlock nest unlocked");
  	dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2230
2231
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2232

e04ce676e   Boqun Feng   lockdep/selftest:...
2233
2234
2235
2236
  	print_testname("spinlock nest test");
  	dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW);
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
  	printk("  -----------------------------------------------------
  ");
  	printk("                                 |block | try  |context|
  ");
  	printk("  -----------------------------------------------------
  ");
  
  	print_testname("context");
  	dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
  	dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2248
2249
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2250
2251
2252
2253
2254
  
  	print_testname("try");
  	dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
  	dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2255
2256
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2257
2258
2259
2260
2261
  
  	print_testname("block");
  	dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
  	dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2262
2263
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2264
2265
2266
2267
2268
  
  	print_testname("spinlock");
  	dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
  	dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
  	dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
251394098   Michael Ellerman   locking/selftest:...
2269
2270
  	pr_cont("
  ");
1de994452   Maarten Lankhorst   mutex: Add w/w te...
2271
  }
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2272

ad56450db   Boqun Feng   locking/selftest:...
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
  
  /*
   * <in hardirq handler>
   * read_lock(&A);
   *			<hardirq disable>
   *			spin_lock(&B);
   * spin_lock(&B);
   *			read_lock(&A);
   *
   * is a deadlock.
   */
  static void queued_read_lock_hardirq_RE_Er(void)
  {
  	HARDIRQ_ENTER();
  	read_lock(&rwlock_A);
  	LOCK(B);
  	UNLOCK(B);
  	read_unlock(&rwlock_A);
  	HARDIRQ_EXIT();
  
  	HARDIRQ_DISABLE();
  	LOCK(B);
  	read_lock(&rwlock_A);
  	read_unlock(&rwlock_A);
  	UNLOCK(B);
  	HARDIRQ_ENABLE();
  }
  
  /*
   * <in hardirq handler>
   * spin_lock(&B);
   *			<hardirq disable>
   *			read_lock(&A);
   * read_lock(&A);
   *			spin_lock(&B);
   *
   * is not a deadlock.
   */
  static void queued_read_lock_hardirq_ER_rE(void)
  {
  	HARDIRQ_ENTER();
  	LOCK(B);
  	read_lock(&rwlock_A);
  	read_unlock(&rwlock_A);
  	UNLOCK(B);
  	HARDIRQ_EXIT();
  
  	HARDIRQ_DISABLE();
  	read_lock(&rwlock_A);
  	LOCK(B);
  	UNLOCK(B);
  	read_unlock(&rwlock_A);
  	HARDIRQ_ENABLE();
  }
  
  /*
   * <hardirq disable>
   * spin_lock(&B);
   *			read_lock(&A);
   *			<in hardirq handler>
   *			spin_lock(&B);
   * read_lock(&A);
   *
   * is a deadlock. Because the two read_lock()s are both non-recursive readers.
   */
  static void queued_read_lock_hardirq_inversion(void)
  {
  
  	HARDIRQ_ENTER();
  	LOCK(B);
  	UNLOCK(B);
  	HARDIRQ_EXIT();
  
  	HARDIRQ_DISABLE();
  	LOCK(B);
  	read_lock(&rwlock_A);
  	read_unlock(&rwlock_A);
  	UNLOCK(B);
  	HARDIRQ_ENABLE();
  
  	read_lock(&rwlock_A);
  	read_unlock(&rwlock_A);
  }
  
  static void queued_read_lock_tests(void)
  {
  	printk("  --------------------------------------------------------------------------
  ");
  	printk("  | queued read lock tests |
  ");
  	printk("  ---------------------------
  ");
  	print_testname("hardirq read-lock/lock-read");
  	dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
  	pr_cont("
  ");
  
  	print_testname("hardirq lock-read/read-lock");
  	dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
  	pr_cont("
  ");
  
  	print_testname("hardirq inversion");
  	dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
  	pr_cont("
  ");
  }
d5037d1d8   Daniel Vetter   locking/selftests...
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
  static void fs_reclaim_correct_nesting(void)
  {
  	fs_reclaim_acquire(GFP_KERNEL);
  	might_alloc(GFP_NOFS);
  	fs_reclaim_release(GFP_KERNEL);
  }
  
  static void fs_reclaim_wrong_nesting(void)
  {
  	fs_reclaim_acquire(GFP_KERNEL);
  	might_alloc(GFP_KERNEL);
  	fs_reclaim_release(GFP_KERNEL);
  }
  
  static void fs_reclaim_protected_nesting(void)
  {
  	unsigned int flags;
  
  	fs_reclaim_acquire(GFP_KERNEL);
  	flags = memalloc_nofs_save();
  	might_alloc(GFP_KERNEL);
  	memalloc_nofs_restore(flags);
  	fs_reclaim_release(GFP_KERNEL);
  }
  
  static void fs_reclaim_tests(void)
  {
  	printk("  --------------------
  ");
  	printk("  | fs_reclaim tests |
  ");
  	printk("  --------------------
  ");
  
  	print_testname("correct nesting");
  	dotest(fs_reclaim_correct_nesting, SUCCESS, 0);
  	pr_cont("
  ");
  
  	print_testname("wrong nesting");
  	dotest(fs_reclaim_wrong_nesting, FAILURE, 0);
  	pr_cont("
  ");
  
  	print_testname("protected nesting");
  	dotest(fs_reclaim_protected_nesting, SUCCESS, 0);
  	pr_cont("
  ");
  }
9271a40d2   Boqun Feng   lockdep/selftest:...
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
  #define __guard(cleanup) __maybe_unused __attribute__((__cleanup__(cleanup)))
  
  static void hardirq_exit(int *_)
  {
  	HARDIRQ_EXIT();
  }
  
  #define HARDIRQ_CONTEXT(name, ...)					\
  	int hardirq_guard_##name __guard(hardirq_exit);			\
  	HARDIRQ_ENTER();
  
  #define NOTTHREADED_HARDIRQ_CONTEXT(name, ...)				\
  	int notthreaded_hardirq_guard_##name __guard(hardirq_exit);	\
  	local_irq_disable();						\
  	__irq_enter();							\
  	WARN_ON(!in_irq());
  
  static void softirq_exit(int *_)
  {
  	SOFTIRQ_EXIT();
  }
  
  #define SOFTIRQ_CONTEXT(name, ...)				\
  	int softirq_guard_##name __guard(softirq_exit);		\
  	SOFTIRQ_ENTER();
  
  static void rcu_exit(int *_)
  {
  	rcu_read_unlock();
  }
  
  #define RCU_CONTEXT(name, ...)					\
  	int rcu_guard_##name __guard(rcu_exit);			\
  	rcu_read_lock();
  
  static void rcu_bh_exit(int *_)
  {
  	rcu_read_unlock_bh();
  }
  
  #define RCU_BH_CONTEXT(name, ...)				\
  	int rcu_bh_guard_##name __guard(rcu_bh_exit);		\
  	rcu_read_lock_bh();
  
  static void rcu_sched_exit(int *_)
  {
  	rcu_read_unlock_sched();
  }
  
  #define RCU_SCHED_CONTEXT(name, ...)				\
  	int rcu_sched_guard_##name __guard(rcu_sched_exit);	\
  	rcu_read_lock_sched();
9271a40d2   Boqun Feng   lockdep/selftest:...
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
  static void raw_spinlock_exit(raw_spinlock_t **lock)
  {
  	raw_spin_unlock(*lock);
  }
  
  #define RAW_SPINLOCK_CONTEXT(name, lock)						\
  	raw_spinlock_t *raw_spinlock_guard_##name __guard(raw_spinlock_exit) = &(lock);	\
  	raw_spin_lock(&(lock));
  
  static void spinlock_exit(spinlock_t **lock)
  {
  	spin_unlock(*lock);
  }
  
  #define SPINLOCK_CONTEXT(name, lock)						\
  	spinlock_t *spinlock_guard_##name __guard(spinlock_exit) = &(lock);	\
  	spin_lock(&(lock));
  
  static void mutex_exit(struct mutex **lock)
  {
  	mutex_unlock(*lock);
  }
  
  #define MUTEX_CONTEXT(name, lock)					\
  	struct mutex *mutex_guard_##name __guard(mutex_exit) = &(lock);	\
  	mutex_lock(&(lock));
  
  #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock)	\
  										\
  static void __maybe_unused inner##_in_##outer(void)				\
  {										\
  	outer##_CONTEXT(_, outer_lock);						\
  	{									\
  		inner##_CONTEXT(_, inner_lock);					\
  	}									\
  }
  
  /*
   * wait contexts (considering PREEMPT_RT)
   *
   * o: inner is allowed in outer
   * x: inner is disallowed in outer
   *
   *       \  inner |  RCU  | RAW_SPIN | SPIN | MUTEX
   * outer  \       |       |          |      |
   * ---------------+-------+----------+------+-------
   * HARDIRQ        |   o   |    o     |  o   |  x
   * ---------------+-------+----------+------+-------
   * NOTTHREADED_IRQ|   o   |    o     |  x   |  x
   * ---------------+-------+----------+------+-------
   * SOFTIRQ        |   o   |    o     |  o   |  x
   * ---------------+-------+----------+------+-------
   * RCU            |   o   |    o     |  o   |  x
   * ---------------+-------+----------+------+-------
   * RCU_BH         |   o   |    o     |  o   |  x
   * ---------------+-------+----------+------+-------
9271a40d2   Boqun Feng   lockdep/selftest:...
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
   * RCU_SCHED      |   o   |    o     |  x   |  x
   * ---------------+-------+----------+------+-------
   * RAW_SPIN       |   o   |    o     |  x   |  x
   * ---------------+-------+----------+------+-------
   * SPIN           |   o   |    o     |  o   |  x
   * ---------------+-------+----------+------+-------
   * MUTEX          |   o   |    o     |  o   |  o
   * ---------------+-------+----------+------+-------
   */
  
  #define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock)		\
  GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock)			\
  GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock)		\
  GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock)			\
  GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock)				\
  GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock)			\
9271a40d2   Boqun Feng   lockdep/selftest:...
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
  GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock)			\
  GENERATE_2_CONTEXT_TESTCASE(RAW_SPINLOCK, raw_lock_A, inner, inner_lock)	\
  GENERATE_2_CONTEXT_TESTCASE(SPINLOCK, lock_A, inner, inner_lock)		\
  GENERATE_2_CONTEXT_TESTCASE(MUTEX, mutex_A, inner, inner_lock)
  
  GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU, )
  GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RAW_SPINLOCK, raw_lock_B)
  GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(SPINLOCK, lock_B)
  GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(MUTEX, mutex_B)
  
  /* the outer context allows all kinds of preemption */
  #define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer)			\
  	dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK);		\
  	dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);	\
  	dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);		\
  	dotest(MUTEX_in_##outer, SUCCESS, LOCKTYPE_MUTEX);		\
  
  /*
   * the outer context only allows the preemption introduced by spinlock_t (which
   * is a sleepable lock for PREEMPT_RT)
   */
  #define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer)		\
  	dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK);		\
  	dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);	\
  	dotest(SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);		\
  	dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX);		\
  
  /* the outer doesn't allows any kind of preemption */
  #define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer)			\
  	dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK);		\
  	dotest(RAW_SPINLOCK_in_##outer, SUCCESS, LOCKTYPE_SPIN);	\
  	dotest(SPINLOCK_in_##outer, FAILURE, LOCKTYPE_SPIN);		\
  	dotest(MUTEX_in_##outer, FAILURE, LOCKTYPE_MUTEX);		\
  
  static void wait_context_tests(void)
  {
  	printk("  --------------------------------------------------------------------------
  ");
  	printk("  | wait context tests |
  ");
  	printk("  --------------------------------------------------------------------------
  ");
  	printk("                                 | rcu  | raw  | spin |mutex |
  ");
  	printk("  --------------------------------------------------------------------------
  ");
  	print_testname("in hardirq context");
  	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ);
  	pr_cont("
  ");
  
  	print_testname("in hardirq context (not threaded)");
  	DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ);
  	pr_cont("
  ");
  
  	print_testname("in softirq context");
  	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ);
  	pr_cont("
  ");
  
  	print_testname("in RCU context");
  	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU);
  	pr_cont("
  ");
  
  	print_testname("in RCU-bh context");
  	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH);
  	pr_cont("
  ");
9271a40d2   Boqun Feng   lockdep/selftest:...
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
  	print_testname("in RCU-sched context");
  	DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED);
  	pr_cont("
  ");
  
  	print_testname("in RAW_SPINLOCK context");
  	DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RAW_SPINLOCK);
  	pr_cont("
  ");
  
  	print_testname("in SPINLOCK context");
  	DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SPINLOCK);
  	pr_cont("
  ");
  
  	print_testname("in MUTEX context");
  	DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(MUTEX);
  	pr_cont("
  ");
  }
7e923e6a3   Peter Zijlstra   locking/selftests...
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
  static void local_lock_2(void)
  {
  	local_lock_acquire(&local_A);	/* IRQ-ON */
  	local_lock_release(&local_A);
  
  	HARDIRQ_ENTER();
  	spin_lock(&lock_A);		/* IN-IRQ */
  	spin_unlock(&lock_A);
  	HARDIRQ_EXIT()
  
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_A);
  	local_lock_acquire(&local_A);	/* IN-IRQ <-> IRQ-ON cycle, false */
  	local_lock_release(&local_A);
  	spin_unlock(&lock_A);
  	HARDIRQ_ENABLE();
  }
  
  static void local_lock_3A(void)
  {
  	local_lock_acquire(&local_A);	/* IRQ-ON */
  	spin_lock(&lock_B);		/* IRQ-ON */
  	spin_unlock(&lock_B);
  	local_lock_release(&local_A);
  
  	HARDIRQ_ENTER();
  	spin_lock(&lock_A);		/* IN-IRQ */
  	spin_unlock(&lock_A);
  	HARDIRQ_EXIT()
  
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_A);
  	local_lock_acquire(&local_A);	/* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
  	local_lock_release(&local_A);
  	spin_unlock(&lock_A);
  	HARDIRQ_ENABLE();
  }
  
  static void local_lock_3B(void)
  {
  	local_lock_acquire(&local_A);	/* IRQ-ON */
  	spin_lock(&lock_B);		/* IRQ-ON */
  	spin_unlock(&lock_B);
  	local_lock_release(&local_A);
  
  	HARDIRQ_ENTER();
  	spin_lock(&lock_A);		/* IN-IRQ */
  	spin_unlock(&lock_A);
  	HARDIRQ_EXIT()
  
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_A);
  	local_lock_acquire(&local_A);	/* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */
  	local_lock_release(&local_A);
  	spin_unlock(&lock_A);
  	HARDIRQ_ENABLE();
  
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_A);
  	spin_lock(&lock_B);		/* IN-IRQ <-> IRQ-ON cycle, true */
  	spin_unlock(&lock_B);
  	spin_unlock(&lock_A);
  	HARDIRQ_DISABLE();
  
  }
  
  static void local_lock_tests(void)
  {
  	printk("  --------------------------------------------------------------------------
  ");
  	printk("  | local_lock tests |
  ");
  	printk("  ---------------------
  ");
  
  	print_testname("local_lock inversion  2");
  	dotest(local_lock_2, SUCCESS, LOCKTYPE_LL);
  	pr_cont("
  ");
  
  	print_testname("local_lock inversion 3A");
  	dotest(local_lock_3A, SUCCESS, LOCKTYPE_LL);
  	pr_cont("
  ");
  
  	print_testname("local_lock inversion 3B");
  	dotest(local_lock_3B, FAILURE, LOCKTYPE_LL);
  	pr_cont("
  ");
  }
8946ccc25   Boqun Feng   locking/selftests...
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
  static void hardirq_deadlock_softirq_not_deadlock(void)
  {
  	/* mutex_A is hardirq-unsafe and softirq-unsafe */
  	/* mutex_A -> lock_C */
  	mutex_lock(&mutex_A);
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_C);
  	spin_unlock(&lock_C);
  	HARDIRQ_ENABLE();
  	mutex_unlock(&mutex_A);
  
  	/* lock_A is hardirq-safe */
  	HARDIRQ_ENTER();
  	spin_lock(&lock_A);
  	spin_unlock(&lock_A);
  	HARDIRQ_EXIT();
  
  	/* lock_A -> lock_B */
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_A);
  	spin_lock(&lock_B);
  	spin_unlock(&lock_B);
  	spin_unlock(&lock_A);
  	HARDIRQ_ENABLE();
  
  	/* lock_B -> lock_C */
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_B);
  	spin_lock(&lock_C);
  	spin_unlock(&lock_C);
  	spin_unlock(&lock_B);
  	HARDIRQ_ENABLE();
  
  	/* lock_D is softirq-safe */
  	SOFTIRQ_ENTER();
  	spin_lock(&lock_D);
  	spin_unlock(&lock_D);
  	SOFTIRQ_EXIT();
  
  	/* And lock_D is hardirq-unsafe */
  	SOFTIRQ_DISABLE();
  	spin_lock(&lock_D);
  	spin_unlock(&lock_D);
  	SOFTIRQ_ENABLE();
  
  	/*
  	 * mutex_A -> lock_C -> lock_D is softirq-unsafe -> softirq-safe, not
  	 * deadlock.
  	 *
  	 * lock_A -> lock_B -> lock_C -> lock_D is hardirq-safe ->
  	 * hardirq-unsafe, deadlock.
  	 */
  	HARDIRQ_DISABLE();
  	spin_lock(&lock_C);
  	spin_lock(&lock_D);
  	spin_unlock(&lock_D);
  	spin_unlock(&lock_C);
  	HARDIRQ_ENABLE();
  }
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
  void locking_selftest(void)
  {
  	/*
  	 * Got a locking failure before the selftest ran?
  	 */
  	if (!debug_locks) {
  		printk("----------------------------------
  ");
  		printk("| Locking API testsuite disabled |
  ");
  		printk("----------------------------------
  ");
  		return;
  	}
  
  	/*
e91818861   Boqun Feng   locking: More acc...
2808
2809
2810
2811
2812
  	 * treats read_lock() as recursive read locks for testing purpose
  	 */
  	force_read_lock_recursive = 1;
  
  	/*
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
  	 * Run the testsuite:
  	 */
  	printk("------------------------
  ");
  	printk("| Locking API testsuite:
  ");
  	printk("----------------------------------------------------------------------------
  ");
  	printk("                                 | spin |wlock |rlock |mutex | wsem | rsem |
  ");
  	printk("  --------------------------------------------------------------------------
  ");
  
  	init_shared_classes();
cdc84d794   Bart Van Assche   locking/lockdep: ...
2827
  	lockdep_set_selftest_task(current);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2828

6c9076ec9   Ingo Molnar   [PATCH] lockdep: ...
2829
  	DO_TESTCASE_6R("A-A deadlock", AA);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2830
2831
2832
2833
2834
2835
2836
2837
  	DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
  	DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
  	DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
  	DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
  	DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
  	DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
  	DO_TESTCASE_6("double unlock", double_unlock);
  	DO_TESTCASE_6("initialize held", init_held);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2838
2839
2840
2841
  
  	printk("  --------------------------------------------------------------------------
  ");
  	print_testname("recursive read-lock");
251394098   Michael Ellerman   locking/selftest:...
2842
  	pr_cont("             |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2843
  	dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
251394098   Michael Ellerman   locking/selftest:...
2844
  	pr_cont("             |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2845
  	dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
251394098   Michael Ellerman   locking/selftest:...
2846
2847
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2848
2849
  
  	print_testname("recursive read-lock #2");
251394098   Michael Ellerman   locking/selftest:...
2850
  	pr_cont("             |");
6c9076ec9   Ingo Molnar   [PATCH] lockdep: ...
2851
  	dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
251394098   Michael Ellerman   locking/selftest:...
2852
  	pr_cont("             |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2853
  	dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
251394098   Michael Ellerman   locking/selftest:...
2854
2855
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2856
2857
  
  	print_testname("mixed read-write-lock");
251394098   Michael Ellerman   locking/selftest:...
2858
  	pr_cont("             |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2859
  	dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
251394098   Michael Ellerman   locking/selftest:...
2860
  	pr_cont("             |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2861
  	dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
251394098   Michael Ellerman   locking/selftest:...
2862
2863
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2864
2865
  
  	print_testname("mixed write-read-lock");
251394098   Michael Ellerman   locking/selftest:...
2866
  	pr_cont("             |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2867
  	dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
251394098   Michael Ellerman   locking/selftest:...
2868
  	pr_cont("             |");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2869
  	dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
251394098   Michael Ellerman   locking/selftest:...
2870
2871
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2872

e91498589   Peter Zijlstra   locking/lockdep/s...
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
  	print_testname("mixed read-lock/lock-write ABBA");
  	pr_cont("             |");
  	dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
  	pr_cont("             |");
  	dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
  
  	print_testname("mixed read-lock/lock-read ABBA");
  	pr_cont("             |");
  	dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK);
  	pr_cont("             |");
  	dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM);
  
  	print_testname("mixed write-lock/lock-write ABBA");
  	pr_cont("             |");
  	dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK);
  	pr_cont("             |");
  	dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
d4f200e57   Boqun Feng   lockdep/selftest:...
2890
2891
2892
  	print_testname("chain cached mixed R-L/L-W ABBA");
  	pr_cont("             |");
  	dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
8ef7ca751   Boqun Feng   lockdep/selftest:...
2893
2894
2895
2896
  	DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
  	DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
  	DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
  	DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
  	printk("  --------------------------------------------------------------------------
  ");
  
  	/*
  	 * irq-context testcases:
  	 */
  	DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
  	DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
  	DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
  	DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
  	DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
  	DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
31e0d7477   Boqun Feng   lockdep/selftest:...
2909
2910
  	DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
  	DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
96a16f45a   Boqun Feng   lockdep/selftest:...
2911
  	DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2912

1de994452   Maarten Lankhorst   mutex: Add w/w te...
2913
  	ww_tests();
e91818861   Boqun Feng   locking: More acc...
2914
2915
2916
2917
  	force_read_lock_recursive = 0;
  	/*
  	 * queued_read_lock() specific test cases can be put here
  	 */
ad56450db   Boqun Feng   locking/selftest:...
2918
2919
  	if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
  		queued_read_lock_tests();
e91818861   Boqun Feng   locking: More acc...
2920

d5037d1d8   Daniel Vetter   locking/selftests...
2921
  	fs_reclaim_tests();
9271a40d2   Boqun Feng   lockdep/selftest:...
2922
2923
2924
  	/* Wait context test cases that are specific for RAW_LOCK_NESTING */
  	if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING))
  		wait_context_tests();
7e923e6a3   Peter Zijlstra   locking/selftests...
2925
  	local_lock_tests();
8946ccc25   Boqun Feng   locking/selftests...
2926
2927
2928
2929
  	print_testname("hardirq_unsafe_softirq_safe");
  	dotest(hardirq_deadlock_softirq_not_deadlock, FAILURE, LOCKTYPE_SPECIAL);
  	pr_cont("
  ");
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
  	if (unexpected_testcase_failures) {
  		printk("-----------------------------------------------------------------
  ");
  		debug_locks = 0;
  		printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |
  ",
  			unexpected_testcase_failures, testcase_total);
  		printk("-----------------------------------------------------------------
  ");
  	} else if (expected_testcase_failures && testcase_successes) {
  		printk("--------------------------------------------------------
  ");
  		printk("%3d out of %3d testcases failed, as expected. |
  ",
  			expected_testcase_failures, testcase_total);
  		printk("----------------------------------------------------
  ");
  		debug_locks = 1;
  	} else if (expected_testcase_failures && !testcase_successes) {
  		printk("--------------------------------------------------------
  ");
  		printk("All %3d testcases failed, as expected. |
  ",
  			expected_testcase_failures);
  		printk("----------------------------------------
  ");
  		debug_locks = 1;
  	} else {
  		printk("-------------------------------------------------------
  ");
  		printk("Good, all %3d testcases passed! |
  ",
  			testcase_successes);
  		printk("---------------------------------
  ");
  		debug_locks = 1;
  	}
cdc84d794   Bart Van Assche   locking/lockdep: ...
2967
  	lockdep_set_selftest_task(NULL);
cae2ed9aa   Ingo Molnar   [PATCH] lockdep: ...
2968
2969
  	debug_locks_silent = 0;
  }