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