Back to home page

LXR

 
 

    


0001 /*
0002  * lib/locking-selftest.c
0003  *
0004  * Testsuite for various locking APIs: spinlocks, rwlocks,
0005  * mutexes and rw-semaphores.
0006  *
0007  * It is checking both false positives and false negatives.
0008  *
0009  * Started by Ingo Molnar:
0010  *
0011  *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
0012  */
0013 #include <linux/rwsem.h>
0014 #include <linux/mutex.h>
0015 #include <linux/ww_mutex.h>
0016 #include <linux/sched.h>
0017 #include <linux/delay.h>
0018 #include <linux/lockdep.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/kallsyms.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/debug_locks.h>
0023 #include <linux/irqflags.h>
0024 
0025 /*
0026  * Change this to 1 if you want to see the failure printouts:
0027  */
0028 static unsigned int debug_locks_verbose;
0029 
0030 static DEFINE_WW_CLASS(ww_lockdep);
0031 
0032 static int __init setup_debug_locks_verbose(char *str)
0033 {
0034     get_option(&str, &debug_locks_verbose);
0035 
0036     return 1;
0037 }
0038 
0039 __setup("debug_locks_verbose=", setup_debug_locks_verbose);
0040 
0041 #define FAILURE     0
0042 #define SUCCESS     1
0043 
0044 #define LOCKTYPE_SPIN   0x1
0045 #define LOCKTYPE_RWLOCK 0x2
0046 #define LOCKTYPE_MUTEX  0x4
0047 #define LOCKTYPE_RWSEM  0x8
0048 #define LOCKTYPE_WW 0x10
0049 
0050 static struct ww_acquire_ctx t, t2;
0051 static struct ww_mutex o, o2, o3;
0052 
0053 /*
0054  * Normal standalone locks, for the circular and irq-context
0055  * dependency tests:
0056  */
0057 static DEFINE_RAW_SPINLOCK(lock_A);
0058 static DEFINE_RAW_SPINLOCK(lock_B);
0059 static DEFINE_RAW_SPINLOCK(lock_C);
0060 static DEFINE_RAW_SPINLOCK(lock_D);
0061 
0062 static DEFINE_RWLOCK(rwlock_A);
0063 static DEFINE_RWLOCK(rwlock_B);
0064 static DEFINE_RWLOCK(rwlock_C);
0065 static DEFINE_RWLOCK(rwlock_D);
0066 
0067 static DEFINE_MUTEX(mutex_A);
0068 static DEFINE_MUTEX(mutex_B);
0069 static DEFINE_MUTEX(mutex_C);
0070 static DEFINE_MUTEX(mutex_D);
0071 
0072 static DECLARE_RWSEM(rwsem_A);
0073 static DECLARE_RWSEM(rwsem_B);
0074 static DECLARE_RWSEM(rwsem_C);
0075 static DECLARE_RWSEM(rwsem_D);
0076 
0077 /*
0078  * Locks that we initialize dynamically as well so that
0079  * e.g. X1 and X2 becomes two instances of the same class,
0080  * but X* and Y* are different classes. We do this so that
0081  * we do not trigger a real lockup:
0082  */
0083 static DEFINE_RAW_SPINLOCK(lock_X1);
0084 static DEFINE_RAW_SPINLOCK(lock_X2);
0085 static DEFINE_RAW_SPINLOCK(lock_Y1);
0086 static DEFINE_RAW_SPINLOCK(lock_Y2);
0087 static DEFINE_RAW_SPINLOCK(lock_Z1);
0088 static DEFINE_RAW_SPINLOCK(lock_Z2);
0089 
0090 static DEFINE_RWLOCK(rwlock_X1);
0091 static DEFINE_RWLOCK(rwlock_X2);
0092 static DEFINE_RWLOCK(rwlock_Y1);
0093 static DEFINE_RWLOCK(rwlock_Y2);
0094 static DEFINE_RWLOCK(rwlock_Z1);
0095 static DEFINE_RWLOCK(rwlock_Z2);
0096 
0097 static DEFINE_MUTEX(mutex_X1);
0098 static DEFINE_MUTEX(mutex_X2);
0099 static DEFINE_MUTEX(mutex_Y1);
0100 static DEFINE_MUTEX(mutex_Y2);
0101 static DEFINE_MUTEX(mutex_Z1);
0102 static DEFINE_MUTEX(mutex_Z2);
0103 
0104 static DECLARE_RWSEM(rwsem_X1);
0105 static DECLARE_RWSEM(rwsem_X2);
0106 static DECLARE_RWSEM(rwsem_Y1);
0107 static DECLARE_RWSEM(rwsem_Y2);
0108 static DECLARE_RWSEM(rwsem_Z1);
0109 static DECLARE_RWSEM(rwsem_Z2);
0110 
0111 /*
0112  * non-inlined runtime initializers, to let separate locks share
0113  * the same lock-class:
0114  */
0115 #define INIT_CLASS_FUNC(class)              \
0116 static noinline void                    \
0117 init_class_##class(raw_spinlock_t *lock, rwlock_t *rwlock, \
0118     struct mutex *mutex, struct rw_semaphore *rwsem)\
0119 {                           \
0120     raw_spin_lock_init(lock);           \
0121     rwlock_init(rwlock);                \
0122     mutex_init(mutex);              \
0123     init_rwsem(rwsem);              \
0124 }
0125 
0126 INIT_CLASS_FUNC(X)
0127 INIT_CLASS_FUNC(Y)
0128 INIT_CLASS_FUNC(Z)
0129 
0130 static void init_shared_classes(void)
0131 {
0132     init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);
0133     init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2);
0134 
0135     init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);
0136     init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2);
0137 
0138     init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);
0139     init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);
0140 }
0141 
0142 /*
0143  * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
0144  * The following functions use a lock from a simulated hardirq/softirq
0145  * context, causing the locks to be marked as hardirq-safe/softirq-safe:
0146  */
0147 
0148 #define HARDIRQ_DISABLE     local_irq_disable
0149 #define HARDIRQ_ENABLE      local_irq_enable
0150 
0151 #define HARDIRQ_ENTER()             \
0152     local_irq_disable();            \
0153     __irq_enter();              \
0154     WARN_ON(!in_irq());
0155 
0156 #define HARDIRQ_EXIT()              \
0157     __irq_exit();               \
0158     local_irq_enable();
0159 
0160 #define SOFTIRQ_DISABLE     local_bh_disable
0161 #define SOFTIRQ_ENABLE      local_bh_enable
0162 
0163 #define SOFTIRQ_ENTER()             \
0164         local_bh_disable();     \
0165         local_irq_disable();        \
0166         lockdep_softirq_enter();    \
0167         WARN_ON(!in_softirq());
0168 
0169 #define SOFTIRQ_EXIT()              \
0170         lockdep_softirq_exit();     \
0171         local_irq_enable();     \
0172         local_bh_enable();
0173 
0174 /*
0175  * Shortcuts for lock/unlock API variants, to keep
0176  * the testcases compact:
0177  */
0178 #define L(x)            raw_spin_lock(&lock_##x)
0179 #define U(x)            raw_spin_unlock(&lock_##x)
0180 #define LU(x)           L(x); U(x)
0181 #define SI(x)           raw_spin_lock_init(&lock_##x)
0182 
0183 #define WL(x)           write_lock(&rwlock_##x)
0184 #define WU(x)           write_unlock(&rwlock_##x)
0185 #define WLU(x)          WL(x); WU(x)
0186 
0187 #define RL(x)           read_lock(&rwlock_##x)
0188 #define RU(x)           read_unlock(&rwlock_##x)
0189 #define RLU(x)          RL(x); RU(x)
0190 #define RWI(x)          rwlock_init(&rwlock_##x)
0191 
0192 #define ML(x)           mutex_lock(&mutex_##x)
0193 #define MU(x)           mutex_unlock(&mutex_##x)
0194 #define MI(x)           mutex_init(&mutex_##x)
0195 
0196 #define WSL(x)          down_write(&rwsem_##x)
0197 #define WSU(x)          up_write(&rwsem_##x)
0198 
0199 #define RSL(x)          down_read(&rwsem_##x)
0200 #define RSU(x)          up_read(&rwsem_##x)
0201 #define RWSI(x)         init_rwsem(&rwsem_##x)
0202 
0203 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
0204 #define WWAI(x)         ww_acquire_init(x, &ww_lockdep)
0205 #else
0206 #define WWAI(x)         do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)
0207 #endif
0208 #define WWAD(x)         ww_acquire_done(x)
0209 #define WWAF(x)         ww_acquire_fini(x)
0210 
0211 #define WWL(x, c)       ww_mutex_lock(x, c)
0212 #define WWT(x)          ww_mutex_trylock(x)
0213 #define WWL1(x)         ww_mutex_lock(x, NULL)
0214 #define WWU(x)          ww_mutex_unlock(x)
0215 
0216 
0217 #define LOCK_UNLOCK_2(x,y)  LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x)
0218 
0219 /*
0220  * Generate different permutations of the same testcase, using
0221  * the same basic lock-dependency/state events:
0222  */
0223 
0224 #define GENERATE_TESTCASE(name)         \
0225                         \
0226 static void name(void) { E(); }
0227 
0228 #define GENERATE_PERMUTATIONS_2_EVENTS(name)    \
0229                         \
0230 static void name##_12(void) { E1(); E2(); } \
0231 static void name##_21(void) { E2(); E1(); }
0232 
0233 #define GENERATE_PERMUTATIONS_3_EVENTS(name)        \
0234                             \
0235 static void name##_123(void) { E1(); E2(); E3(); }  \
0236 static void name##_132(void) { E1(); E3(); E2(); }  \
0237 static void name##_213(void) { E2(); E1(); E3(); }  \
0238 static void name##_231(void) { E2(); E3(); E1(); }  \
0239 static void name##_312(void) { E3(); E1(); E2(); }  \
0240 static void name##_321(void) { E3(); E2(); E1(); }
0241 
0242 /*
0243  * AA deadlock:
0244  */
0245 
0246 #define E()                 \
0247                         \
0248     LOCK(X1);               \
0249     LOCK(X2); /* this one should fail */
0250 
0251 /*
0252  * 6 testcases:
0253  */
0254 #include "locking-selftest-spin.h"
0255 GENERATE_TESTCASE(AA_spin)
0256 #include "locking-selftest-wlock.h"
0257 GENERATE_TESTCASE(AA_wlock)
0258 #include "locking-selftest-rlock.h"
0259 GENERATE_TESTCASE(AA_rlock)
0260 #include "locking-selftest-mutex.h"
0261 GENERATE_TESTCASE(AA_mutex)
0262 #include "locking-selftest-wsem.h"
0263 GENERATE_TESTCASE(AA_wsem)
0264 #include "locking-selftest-rsem.h"
0265 GENERATE_TESTCASE(AA_rsem)
0266 
0267 #undef E
0268 
0269 /*
0270  * Special-case for read-locking, they are
0271  * allowed to recurse on the same lock class:
0272  */
0273 static void rlock_AA1(void)
0274 {
0275     RL(X1);
0276     RL(X1); // this one should NOT fail
0277 }
0278 
0279 static void rlock_AA1B(void)
0280 {
0281     RL(X1);
0282     RL(X2); // this one should NOT fail
0283 }
0284 
0285 static void rsem_AA1(void)
0286 {
0287     RSL(X1);
0288     RSL(X1); // this one should fail
0289 }
0290 
0291 static void rsem_AA1B(void)
0292 {
0293     RSL(X1);
0294     RSL(X2); // this one should fail
0295 }
0296 /*
0297  * The mixing of read and write locks is not allowed:
0298  */
0299 static void rlock_AA2(void)
0300 {
0301     RL(X1);
0302     WL(X2); // this one should fail
0303 }
0304 
0305 static void rsem_AA2(void)
0306 {
0307     RSL(X1);
0308     WSL(X2); // this one should fail
0309 }
0310 
0311 static void rlock_AA3(void)
0312 {
0313     WL(X1);
0314     RL(X2); // this one should fail
0315 }
0316 
0317 static void rsem_AA3(void)
0318 {
0319     WSL(X1);
0320     RSL(X2); // this one should fail
0321 }
0322 
0323 /*
0324  * ABBA deadlock:
0325  */
0326 
0327 #define E()                 \
0328                         \
0329     LOCK_UNLOCK_2(A, B);            \
0330     LOCK_UNLOCK_2(B, A); /* fail */
0331 
0332 /*
0333  * 6 testcases:
0334  */
0335 #include "locking-selftest-spin.h"
0336 GENERATE_TESTCASE(ABBA_spin)
0337 #include "locking-selftest-wlock.h"
0338 GENERATE_TESTCASE(ABBA_wlock)
0339 #include "locking-selftest-rlock.h"
0340 GENERATE_TESTCASE(ABBA_rlock)
0341 #include "locking-selftest-mutex.h"
0342 GENERATE_TESTCASE(ABBA_mutex)
0343 #include "locking-selftest-wsem.h"
0344 GENERATE_TESTCASE(ABBA_wsem)
0345 #include "locking-selftest-rsem.h"
0346 GENERATE_TESTCASE(ABBA_rsem)
0347 
0348 #undef E
0349 
0350 /*
0351  * AB BC CA deadlock:
0352  */
0353 
0354 #define E()                 \
0355                         \
0356     LOCK_UNLOCK_2(A, B);            \
0357     LOCK_UNLOCK_2(B, C);            \
0358     LOCK_UNLOCK_2(C, A); /* fail */
0359 
0360 /*
0361  * 6 testcases:
0362  */
0363 #include "locking-selftest-spin.h"
0364 GENERATE_TESTCASE(ABBCCA_spin)
0365 #include "locking-selftest-wlock.h"
0366 GENERATE_TESTCASE(ABBCCA_wlock)
0367 #include "locking-selftest-rlock.h"
0368 GENERATE_TESTCASE(ABBCCA_rlock)
0369 #include "locking-selftest-mutex.h"
0370 GENERATE_TESTCASE(ABBCCA_mutex)
0371 #include "locking-selftest-wsem.h"
0372 GENERATE_TESTCASE(ABBCCA_wsem)
0373 #include "locking-selftest-rsem.h"
0374 GENERATE_TESTCASE(ABBCCA_rsem)
0375 
0376 #undef E
0377 
0378 /*
0379  * AB CA BC deadlock:
0380  */
0381 
0382 #define E()                 \
0383                         \
0384     LOCK_UNLOCK_2(A, B);            \
0385     LOCK_UNLOCK_2(C, A);            \
0386     LOCK_UNLOCK_2(B, C); /* fail */
0387 
0388 /*
0389  * 6 testcases:
0390  */
0391 #include "locking-selftest-spin.h"
0392 GENERATE_TESTCASE(ABCABC_spin)
0393 #include "locking-selftest-wlock.h"
0394 GENERATE_TESTCASE(ABCABC_wlock)
0395 #include "locking-selftest-rlock.h"
0396 GENERATE_TESTCASE(ABCABC_rlock)
0397 #include "locking-selftest-mutex.h"
0398 GENERATE_TESTCASE(ABCABC_mutex)
0399 #include "locking-selftest-wsem.h"
0400 GENERATE_TESTCASE(ABCABC_wsem)
0401 #include "locking-selftest-rsem.h"
0402 GENERATE_TESTCASE(ABCABC_rsem)
0403 
0404 #undef E
0405 
0406 /*
0407  * AB BC CD DA deadlock:
0408  */
0409 
0410 #define E()                 \
0411                         \
0412     LOCK_UNLOCK_2(A, B);            \
0413     LOCK_UNLOCK_2(B, C);            \
0414     LOCK_UNLOCK_2(C, D);            \
0415     LOCK_UNLOCK_2(D, A); /* fail */
0416 
0417 /*
0418  * 6 testcases:
0419  */
0420 #include "locking-selftest-spin.h"
0421 GENERATE_TESTCASE(ABBCCDDA_spin)
0422 #include "locking-selftest-wlock.h"
0423 GENERATE_TESTCASE(ABBCCDDA_wlock)
0424 #include "locking-selftest-rlock.h"
0425 GENERATE_TESTCASE(ABBCCDDA_rlock)
0426 #include "locking-selftest-mutex.h"
0427 GENERATE_TESTCASE(ABBCCDDA_mutex)
0428 #include "locking-selftest-wsem.h"
0429 GENERATE_TESTCASE(ABBCCDDA_wsem)
0430 #include "locking-selftest-rsem.h"
0431 GENERATE_TESTCASE(ABBCCDDA_rsem)
0432 
0433 #undef E
0434 
0435 /*
0436  * AB CD BD DA deadlock:
0437  */
0438 #define E()                 \
0439                         \
0440     LOCK_UNLOCK_2(A, B);            \
0441     LOCK_UNLOCK_2(C, D);            \
0442     LOCK_UNLOCK_2(B, D);            \
0443     LOCK_UNLOCK_2(D, A); /* fail */
0444 
0445 /*
0446  * 6 testcases:
0447  */
0448 #include "locking-selftest-spin.h"
0449 GENERATE_TESTCASE(ABCDBDDA_spin)
0450 #include "locking-selftest-wlock.h"
0451 GENERATE_TESTCASE(ABCDBDDA_wlock)
0452 #include "locking-selftest-rlock.h"
0453 GENERATE_TESTCASE(ABCDBDDA_rlock)
0454 #include "locking-selftest-mutex.h"
0455 GENERATE_TESTCASE(ABCDBDDA_mutex)
0456 #include "locking-selftest-wsem.h"
0457 GENERATE_TESTCASE(ABCDBDDA_wsem)
0458 #include "locking-selftest-rsem.h"
0459 GENERATE_TESTCASE(ABCDBDDA_rsem)
0460 
0461 #undef E
0462 
0463 /*
0464  * AB CD BC DA deadlock:
0465  */
0466 #define E()                 \
0467                         \
0468     LOCK_UNLOCK_2(A, B);            \
0469     LOCK_UNLOCK_2(C, D);            \
0470     LOCK_UNLOCK_2(B, C);            \
0471     LOCK_UNLOCK_2(D, A); /* fail */
0472 
0473 /*
0474  * 6 testcases:
0475  */
0476 #include "locking-selftest-spin.h"
0477 GENERATE_TESTCASE(ABCDBCDA_spin)
0478 #include "locking-selftest-wlock.h"
0479 GENERATE_TESTCASE(ABCDBCDA_wlock)
0480 #include "locking-selftest-rlock.h"
0481 GENERATE_TESTCASE(ABCDBCDA_rlock)
0482 #include "locking-selftest-mutex.h"
0483 GENERATE_TESTCASE(ABCDBCDA_mutex)
0484 #include "locking-selftest-wsem.h"
0485 GENERATE_TESTCASE(ABCDBCDA_wsem)
0486 #include "locking-selftest-rsem.h"
0487 GENERATE_TESTCASE(ABCDBCDA_rsem)
0488 
0489 #undef E
0490 
0491 /*
0492  * Double unlock:
0493  */
0494 #define E()                 \
0495                         \
0496     LOCK(A);                \
0497     UNLOCK(A);              \
0498     UNLOCK(A); /* fail */
0499 
0500 /*
0501  * 6 testcases:
0502  */
0503 #include "locking-selftest-spin.h"
0504 GENERATE_TESTCASE(double_unlock_spin)
0505 #include "locking-selftest-wlock.h"
0506 GENERATE_TESTCASE(double_unlock_wlock)
0507 #include "locking-selftest-rlock.h"
0508 GENERATE_TESTCASE(double_unlock_rlock)
0509 #include "locking-selftest-mutex.h"
0510 GENERATE_TESTCASE(double_unlock_mutex)
0511 #include "locking-selftest-wsem.h"
0512 GENERATE_TESTCASE(double_unlock_wsem)
0513 #include "locking-selftest-rsem.h"
0514 GENERATE_TESTCASE(double_unlock_rsem)
0515 
0516 #undef E
0517 
0518 /*
0519  * Bad unlock ordering:
0520  */
0521 #define E()                 \
0522                         \
0523     LOCK(A);                \
0524     LOCK(B);                \
0525     UNLOCK(A); /* fail */           \
0526     UNLOCK(B);
0527 
0528 /*
0529  * 6 testcases:
0530  */
0531 #include "locking-selftest-spin.h"
0532 GENERATE_TESTCASE(bad_unlock_order_spin)
0533 #include "locking-selftest-wlock.h"
0534 GENERATE_TESTCASE(bad_unlock_order_wlock)
0535 #include "locking-selftest-rlock.h"
0536 GENERATE_TESTCASE(bad_unlock_order_rlock)
0537 #include "locking-selftest-mutex.h"
0538 GENERATE_TESTCASE(bad_unlock_order_mutex)
0539 #include "locking-selftest-wsem.h"
0540 GENERATE_TESTCASE(bad_unlock_order_wsem)
0541 #include "locking-selftest-rsem.h"
0542 GENERATE_TESTCASE(bad_unlock_order_rsem)
0543 
0544 #undef E
0545 
0546 /*
0547  * initializing a held lock:
0548  */
0549 #define E()                 \
0550                         \
0551     LOCK(A);                \
0552     INIT(A); /* fail */
0553 
0554 /*
0555  * 6 testcases:
0556  */
0557 #include "locking-selftest-spin.h"
0558 GENERATE_TESTCASE(init_held_spin)
0559 #include "locking-selftest-wlock.h"
0560 GENERATE_TESTCASE(init_held_wlock)
0561 #include "locking-selftest-rlock.h"
0562 GENERATE_TESTCASE(init_held_rlock)
0563 #include "locking-selftest-mutex.h"
0564 GENERATE_TESTCASE(init_held_mutex)
0565 #include "locking-selftest-wsem.h"
0566 GENERATE_TESTCASE(init_held_wsem)
0567 #include "locking-selftest-rsem.h"
0568 GENERATE_TESTCASE(init_held_rsem)
0569 
0570 #undef E
0571 
0572 /*
0573  * locking an irq-safe lock with irqs enabled:
0574  */
0575 #define E1()                \
0576                     \
0577     IRQ_ENTER();            \
0578     LOCK(A);            \
0579     UNLOCK(A);          \
0580     IRQ_EXIT();
0581 
0582 #define E2()                \
0583                     \
0584     LOCK(A);            \
0585     UNLOCK(A);
0586 
0587 /*
0588  * Generate 24 testcases:
0589  */
0590 #include "locking-selftest-spin-hardirq.h"
0591 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
0592 
0593 #include "locking-selftest-rlock-hardirq.h"
0594 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
0595 
0596 #include "locking-selftest-wlock-hardirq.h"
0597 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock)
0598 
0599 #include "locking-selftest-spin-softirq.h"
0600 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin)
0601 
0602 #include "locking-selftest-rlock-softirq.h"
0603 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
0604 
0605 #include "locking-selftest-wlock-softirq.h"
0606 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
0607 
0608 #undef E1
0609 #undef E2
0610 
0611 /*
0612  * Enabling hardirqs with a softirq-safe lock held:
0613  */
0614 #define E1()                \
0615                     \
0616     SOFTIRQ_ENTER();        \
0617     LOCK(A);            \
0618     UNLOCK(A);          \
0619     SOFTIRQ_EXIT();
0620 
0621 #define E2()                \
0622                     \
0623     HARDIRQ_DISABLE();      \
0624     LOCK(A);            \
0625     HARDIRQ_ENABLE();       \
0626     UNLOCK(A);
0627 
0628 /*
0629  * Generate 12 testcases:
0630  */
0631 #include "locking-selftest-spin.h"
0632 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin)
0633 
0634 #include "locking-selftest-wlock.h"
0635 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock)
0636 
0637 #include "locking-selftest-rlock.h"
0638 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
0639 
0640 #undef E1
0641 #undef E2
0642 
0643 /*
0644  * Enabling irqs with an irq-safe lock held:
0645  */
0646 #define E1()                \
0647                     \
0648     IRQ_ENTER();            \
0649     LOCK(A);            \
0650     UNLOCK(A);          \
0651     IRQ_EXIT();
0652 
0653 #define E2()                \
0654                     \
0655     IRQ_DISABLE();          \
0656     LOCK(A);            \
0657     IRQ_ENABLE();           \
0658     UNLOCK(A);
0659 
0660 /*
0661  * Generate 24 testcases:
0662  */
0663 #include "locking-selftest-spin-hardirq.h"
0664 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
0665 
0666 #include "locking-selftest-rlock-hardirq.h"
0667 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
0668 
0669 #include "locking-selftest-wlock-hardirq.h"
0670 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock)
0671 
0672 #include "locking-selftest-spin-softirq.h"
0673 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin)
0674 
0675 #include "locking-selftest-rlock-softirq.h"
0676 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
0677 
0678 #include "locking-selftest-wlock-softirq.h"
0679 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
0680 
0681 #undef E1
0682 #undef E2
0683 
0684 /*
0685  * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
0686  */
0687 #define E1()                \
0688                     \
0689     LOCK(A);            \
0690     LOCK(B);            \
0691     UNLOCK(B);          \
0692     UNLOCK(A);          \
0693 
0694 #define E2()                \
0695                     \
0696     LOCK(B);            \
0697     UNLOCK(B);
0698 
0699 #define E3()                \
0700                     \
0701     IRQ_ENTER();            \
0702     LOCK(A);            \
0703     UNLOCK(A);          \
0704     IRQ_EXIT();
0705 
0706 /*
0707  * Generate 36 testcases:
0708  */
0709 #include "locking-selftest-spin-hardirq.h"
0710 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
0711 
0712 #include "locking-selftest-rlock-hardirq.h"
0713 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
0714 
0715 #include "locking-selftest-wlock-hardirq.h"
0716 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock)
0717 
0718 #include "locking-selftest-spin-softirq.h"
0719 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin)
0720 
0721 #include "locking-selftest-rlock-softirq.h"
0722 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
0723 
0724 #include "locking-selftest-wlock-softirq.h"
0725 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
0726 
0727 #undef E1
0728 #undef E2
0729 #undef E3
0730 
0731 /*
0732  * If a lock turns into softirq-safe, but earlier it took
0733  * a softirq-unsafe lock:
0734  */
0735 
0736 #define E1()                \
0737     IRQ_DISABLE();          \
0738     LOCK(A);            \
0739     LOCK(B);            \
0740     UNLOCK(B);          \
0741     UNLOCK(A);          \
0742     IRQ_ENABLE();
0743 
0744 #define E2()                \
0745     LOCK(B);            \
0746     UNLOCK(B);
0747 
0748 #define E3()                \
0749     IRQ_ENTER();            \
0750     LOCK(A);            \
0751     UNLOCK(A);          \
0752     IRQ_EXIT();
0753 
0754 /*
0755  * Generate 36 testcases:
0756  */
0757 #include "locking-selftest-spin-hardirq.h"
0758 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
0759 
0760 #include "locking-selftest-rlock-hardirq.h"
0761 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
0762 
0763 #include "locking-selftest-wlock-hardirq.h"
0764 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock)
0765 
0766 #include "locking-selftest-spin-softirq.h"
0767 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin)
0768 
0769 #include "locking-selftest-rlock-softirq.h"
0770 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
0771 
0772 #include "locking-selftest-wlock-softirq.h"
0773 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
0774 
0775 #undef E1
0776 #undef E2
0777 #undef E3
0778 
0779 /*
0780  * read-lock / write-lock irq inversion.
0781  *
0782  * Deadlock scenario:
0783  *
0784  * CPU#1 is at #1, i.e. it has write-locked A, but has not
0785  * taken B yet.
0786  *
0787  * CPU#2 is at #2, i.e. it has locked B.
0788  *
0789  * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
0790  *
0791  * The deadlock occurs because CPU#1 will spin on B, and CPU#2
0792  * will spin on A.
0793  */
0794 
0795 #define E1()                \
0796                     \
0797     IRQ_DISABLE();          \
0798     WL(A);              \
0799     LOCK(B);            \
0800     UNLOCK(B);          \
0801     WU(A);              \
0802     IRQ_ENABLE();
0803 
0804 #define E2()                \
0805                     \
0806     LOCK(B);            \
0807     UNLOCK(B);
0808 
0809 #define E3()                \
0810                     \
0811     IRQ_ENTER();            \
0812     RL(A);              \
0813     RU(A);              \
0814     IRQ_EXIT();
0815 
0816 /*
0817  * Generate 36 testcases:
0818  */
0819 #include "locking-selftest-spin-hardirq.h"
0820 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin)
0821 
0822 #include "locking-selftest-rlock-hardirq.h"
0823 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock)
0824 
0825 #include "locking-selftest-wlock-hardirq.h"
0826 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock)
0827 
0828 #include "locking-selftest-spin-softirq.h"
0829 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin)
0830 
0831 #include "locking-selftest-rlock-softirq.h"
0832 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock)
0833 
0834 #include "locking-selftest-wlock-softirq.h"
0835 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
0836 
0837 #undef E1
0838 #undef E2
0839 #undef E3
0840 
0841 /*
0842  * read-lock / write-lock recursion that is actually safe.
0843  */
0844 
0845 #define E1()                \
0846                     \
0847     IRQ_DISABLE();          \
0848     WL(A);              \
0849     WU(A);              \
0850     IRQ_ENABLE();
0851 
0852 #define E2()                \
0853                     \
0854     RL(A);              \
0855     RU(A);              \
0856 
0857 #define E3()                \
0858                     \
0859     IRQ_ENTER();            \
0860     RL(A);              \
0861     L(B);               \
0862     U(B);               \
0863     RU(A);              \
0864     IRQ_EXIT();
0865 
0866 /*
0867  * Generate 12 testcases:
0868  */
0869 #include "locking-selftest-hardirq.h"
0870 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
0871 
0872 #include "locking-selftest-softirq.h"
0873 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
0874 
0875 #undef E1
0876 #undef E2
0877 #undef E3
0878 
0879 /*
0880  * read-lock / write-lock recursion that is unsafe.
0881  */
0882 
0883 #define E1()                \
0884                     \
0885     IRQ_DISABLE();          \
0886     L(B);               \
0887     WL(A);              \
0888     WU(A);              \
0889     U(B);               \
0890     IRQ_ENABLE();
0891 
0892 #define E2()                \
0893                     \
0894     RL(A);              \
0895     RU(A);              \
0896 
0897 #define E3()                \
0898                     \
0899     IRQ_ENTER();            \
0900     L(B);               \
0901     U(B);               \
0902     IRQ_EXIT();
0903 
0904 /*
0905  * Generate 12 testcases:
0906  */
0907 #include "locking-selftest-hardirq.h"
0908 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
0909 
0910 #include "locking-selftest-softirq.h"
0911 // GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
0912 
0913 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0914 # define I_SPINLOCK(x)  lockdep_reset_lock(&lock_##x.dep_map)
0915 # define I_RWLOCK(x)    lockdep_reset_lock(&rwlock_##x.dep_map)
0916 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map)
0917 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map)
0918 # define I_WW(x)    lockdep_reset_lock(&x.dep_map)
0919 #else
0920 # define I_SPINLOCK(x)
0921 # define I_RWLOCK(x)
0922 # define I_MUTEX(x)
0923 # define I_RWSEM(x)
0924 # define I_WW(x)
0925 #endif
0926 
0927 #define I1(x)                   \
0928     do {                    \
0929         I_SPINLOCK(x);          \
0930         I_RWLOCK(x);            \
0931         I_MUTEX(x);         \
0932         I_RWSEM(x);         \
0933     } while (0)
0934 
0935 #define I2(x)                   \
0936     do {                    \
0937         raw_spin_lock_init(&lock_##x);  \
0938         rwlock_init(&rwlock_##x);   \
0939         mutex_init(&mutex_##x);     \
0940         init_rwsem(&rwsem_##x);     \
0941     } while (0)
0942 
0943 static void reset_locks(void)
0944 {
0945     local_irq_disable();
0946     lockdep_free_key_range(&ww_lockdep.acquire_key, 1);
0947     lockdep_free_key_range(&ww_lockdep.mutex_key, 1);
0948 
0949     I1(A); I1(B); I1(C); I1(D);
0950     I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2);
0951     I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base);
0952     lockdep_reset();
0953     I2(A); I2(B); I2(C); I2(D);
0954     init_shared_classes();
0955 
0956     ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep);
0957     memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2));
0958     memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key));
0959     memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key));
0960     local_irq_enable();
0961 }
0962 
0963 #undef I
0964 
0965 static int testcase_total;
0966 static int testcase_successes;
0967 static int expected_testcase_failures;
0968 static int unexpected_testcase_failures;
0969 
0970 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
0971 {
0972     unsigned long saved_preempt_count = preempt_count();
0973 
0974     WARN_ON(irqs_disabled());
0975 
0976     testcase_fn();
0977     /*
0978      * Filter out expected failures:
0979      */
0980 #ifndef CONFIG_PROVE_LOCKING
0981     if (expected == FAILURE && debug_locks) {
0982         expected_testcase_failures++;
0983         pr_cont("failed|");
0984     }
0985     else
0986 #endif
0987     if (debug_locks != expected) {
0988         unexpected_testcase_failures++;
0989         pr_cont("FAILED|");
0990 
0991         dump_stack();
0992     } else {
0993         testcase_successes++;
0994         pr_cont("  ok  |");
0995     }
0996     testcase_total++;
0997 
0998     if (debug_locks_verbose)
0999         pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n",
1000             lockclass_mask, debug_locks, expected);
1001     /*
1002      * Some tests (e.g. double-unlock) might corrupt the preemption
1003      * count, so restore it:
1004      */
1005     preempt_count_set(saved_preempt_count);
1006 #ifdef CONFIG_TRACE_IRQFLAGS
1007     if (softirq_count())
1008         current->softirqs_enabled = 0;
1009     else
1010         current->softirqs_enabled = 1;
1011 #endif
1012 
1013     reset_locks();
1014 }
1015 
1016 static inline void print_testname(const char *testname)
1017 {
1018     printk("%33s:", testname);
1019 }
1020 
1021 #define DO_TESTCASE_1(desc, name, nr)               \
1022     print_testname(desc"/"#nr);             \
1023     dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);      \
1024     pr_cont("\n");
1025 
1026 #define DO_TESTCASE_1B(desc, name, nr)              \
1027     print_testname(desc"/"#nr);             \
1028     dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);      \
1029     pr_cont("\n");
1030 
1031 #define DO_TESTCASE_3(desc, name, nr)               \
1032     print_testname(desc"/"#nr);             \
1033     dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN);   \
1034     dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);    \
1035     dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
1036     pr_cont("\n");
1037 
1038 #define DO_TESTCASE_3RW(desc, name, nr)             \
1039     print_testname(desc"/"#nr);             \
1040     dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\
1041     dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);    \
1042     dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
1043     pr_cont("\n");
1044 
1045 #define DO_TESTCASE_6(desc, name)               \
1046     print_testname(desc);                   \
1047     dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);        \
1048     dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);     \
1049     dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK);     \
1050     dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);      \
1051     dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);       \
1052     dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);       \
1053     pr_cont("\n");
1054 
1055 #define DO_TESTCASE_6_SUCCESS(desc, name)           \
1056     print_testname(desc);                   \
1057     dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN);        \
1058     dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK);     \
1059     dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);     \
1060     dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX);      \
1061     dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM);       \
1062     dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM);       \
1063     pr_cont("\n");
1064 
1065 /*
1066  * 'read' variant: rlocks must not trigger.
1067  */
1068 #define DO_TESTCASE_6R(desc, name)              \
1069     print_testname(desc);                   \
1070     dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);        \
1071     dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK);     \
1072     dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK);     \
1073     dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX);      \
1074     dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM);       \
1075     dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM);       \
1076     pr_cont("\n");
1077 
1078 #define DO_TESTCASE_2I(desc, name, nr)              \
1079     DO_TESTCASE_1("hard-"desc, name##_hard, nr);        \
1080     DO_TESTCASE_1("soft-"desc, name##_soft, nr);
1081 
1082 #define DO_TESTCASE_2IB(desc, name, nr)             \
1083     DO_TESTCASE_1B("hard-"desc, name##_hard, nr);       \
1084     DO_TESTCASE_1B("soft-"desc, name##_soft, nr);
1085 
1086 #define DO_TESTCASE_6I(desc, name, nr)              \
1087     DO_TESTCASE_3("hard-"desc, name##_hard, nr);        \
1088     DO_TESTCASE_3("soft-"desc, name##_soft, nr);
1089 
1090 #define DO_TESTCASE_6IRW(desc, name, nr)            \
1091     DO_TESTCASE_3RW("hard-"desc, name##_hard, nr);      \
1092     DO_TESTCASE_3RW("soft-"desc, name##_soft, nr);
1093 
1094 #define DO_TESTCASE_2x3(desc, name)             \
1095     DO_TESTCASE_3(desc, name, 12);              \
1096     DO_TESTCASE_3(desc, name, 21);
1097 
1098 #define DO_TESTCASE_2x6(desc, name)             \
1099     DO_TESTCASE_6I(desc, name, 12);             \
1100     DO_TESTCASE_6I(desc, name, 21);
1101 
1102 #define DO_TESTCASE_6x2(desc, name)             \
1103     DO_TESTCASE_2I(desc, name, 123);            \
1104     DO_TESTCASE_2I(desc, name, 132);            \
1105     DO_TESTCASE_2I(desc, name, 213);            \
1106     DO_TESTCASE_2I(desc, name, 231);            \
1107     DO_TESTCASE_2I(desc, name, 312);            \
1108     DO_TESTCASE_2I(desc, name, 321);
1109 
1110 #define DO_TESTCASE_6x2B(desc, name)                \
1111     DO_TESTCASE_2IB(desc, name, 123);           \
1112     DO_TESTCASE_2IB(desc, name, 132);           \
1113     DO_TESTCASE_2IB(desc, name, 213);           \
1114     DO_TESTCASE_2IB(desc, name, 231);           \
1115     DO_TESTCASE_2IB(desc, name, 312);           \
1116     DO_TESTCASE_2IB(desc, name, 321);
1117 
1118 #define DO_TESTCASE_6x6(desc, name)             \
1119     DO_TESTCASE_6I(desc, name, 123);            \
1120     DO_TESTCASE_6I(desc, name, 132);            \
1121     DO_TESTCASE_6I(desc, name, 213);            \
1122     DO_TESTCASE_6I(desc, name, 231);            \
1123     DO_TESTCASE_6I(desc, name, 312);            \
1124     DO_TESTCASE_6I(desc, name, 321);
1125 
1126 #define DO_TESTCASE_6x6RW(desc, name)               \
1127     DO_TESTCASE_6IRW(desc, name, 123);          \
1128     DO_TESTCASE_6IRW(desc, name, 132);          \
1129     DO_TESTCASE_6IRW(desc, name, 213);          \
1130     DO_TESTCASE_6IRW(desc, name, 231);          \
1131     DO_TESTCASE_6IRW(desc, name, 312);          \
1132     DO_TESTCASE_6IRW(desc, name, 321);
1133 
1134 static void ww_test_fail_acquire(void)
1135 {
1136     int ret;
1137 
1138     WWAI(&t);
1139     t.stamp++;
1140 
1141     ret = WWL(&o, &t);
1142 
1143     if (WARN_ON(!o.ctx) ||
1144         WARN_ON(ret))
1145         return;
1146 
1147     /* No lockdep test, pure API */
1148     ret = WWL(&o, &t);
1149     WARN_ON(ret != -EALREADY);
1150 
1151     ret = WWT(&o);
1152     WARN_ON(ret);
1153 
1154     t2 = t;
1155     t2.stamp++;
1156     ret = WWL(&o, &t2);
1157     WARN_ON(ret != -EDEADLK);
1158     WWU(&o);
1159 
1160     if (WWT(&o))
1161         WWU(&o);
1162 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1163     else
1164         DEBUG_LOCKS_WARN_ON(1);
1165 #endif
1166 }
1167 
1168 static void ww_test_normal(void)
1169 {
1170     int ret;
1171 
1172     WWAI(&t);
1173 
1174     /*
1175      * None of the ww_mutex codepaths should be taken in the 'normal'
1176      * mutex calls. The easiest way to verify this is by using the
1177      * normal mutex calls, and making sure o.ctx is unmodified.
1178      */
1179 
1180     /* mutex_lock (and indirectly, mutex_lock_nested) */
1181     o.ctx = (void *)~0UL;
1182     mutex_lock(&o.base);
1183     mutex_unlock(&o.base);
1184     WARN_ON(o.ctx != (void *)~0UL);
1185 
1186     /* mutex_lock_interruptible (and *_nested) */
1187     o.ctx = (void *)~0UL;
1188     ret = mutex_lock_interruptible(&o.base);
1189     if (!ret)
1190         mutex_unlock(&o.base);
1191     else
1192         WARN_ON(1);
1193     WARN_ON(o.ctx != (void *)~0UL);
1194 
1195     /* mutex_lock_killable (and *_nested) */
1196     o.ctx = (void *)~0UL;
1197     ret = mutex_lock_killable(&o.base);
1198     if (!ret)
1199         mutex_unlock(&o.base);
1200     else
1201         WARN_ON(1);
1202     WARN_ON(o.ctx != (void *)~0UL);
1203 
1204     /* trylock, succeeding */
1205     o.ctx = (void *)~0UL;
1206     ret = mutex_trylock(&o.base);
1207     WARN_ON(!ret);
1208     if (ret)
1209         mutex_unlock(&o.base);
1210     else
1211         WARN_ON(1);
1212     WARN_ON(o.ctx != (void *)~0UL);
1213 
1214     /* trylock, failing */
1215     o.ctx = (void *)~0UL;
1216     mutex_lock(&o.base);
1217     ret = mutex_trylock(&o.base);
1218     WARN_ON(ret);
1219     mutex_unlock(&o.base);
1220     WARN_ON(o.ctx != (void *)~0UL);
1221 
1222     /* nest_lock */
1223     o.ctx = (void *)~0UL;
1224     mutex_lock_nest_lock(&o.base, &t);
1225     mutex_unlock(&o.base);
1226     WARN_ON(o.ctx != (void *)~0UL);
1227 }
1228 
1229 static void ww_test_two_contexts(void)
1230 {
1231     WWAI(&t);
1232     WWAI(&t2);
1233 }
1234 
1235 static void ww_test_diff_class(void)
1236 {
1237     WWAI(&t);
1238 #ifdef CONFIG_DEBUG_MUTEXES
1239     t.ww_class = NULL;
1240 #endif
1241     WWL(&o, &t);
1242 }
1243 
1244 static void ww_test_context_done_twice(void)
1245 {
1246     WWAI(&t);
1247     WWAD(&t);
1248     WWAD(&t);
1249     WWAF(&t);
1250 }
1251 
1252 static void ww_test_context_unlock_twice(void)
1253 {
1254     WWAI(&t);
1255     WWAD(&t);
1256     WWAF(&t);
1257     WWAF(&t);
1258 }
1259 
1260 static void ww_test_context_fini_early(void)
1261 {
1262     WWAI(&t);
1263     WWL(&o, &t);
1264     WWAD(&t);
1265     WWAF(&t);
1266 }
1267 
1268 static void ww_test_context_lock_after_done(void)
1269 {
1270     WWAI(&t);
1271     WWAD(&t);
1272     WWL(&o, &t);
1273 }
1274 
1275 static void ww_test_object_unlock_twice(void)
1276 {
1277     WWL1(&o);
1278     WWU(&o);
1279     WWU(&o);
1280 }
1281 
1282 static void ww_test_object_lock_unbalanced(void)
1283 {
1284     WWAI(&t);
1285     WWL(&o, &t);
1286     t.acquired = 0;
1287     WWU(&o);
1288     WWAF(&t);
1289 }
1290 
1291 static void ww_test_object_lock_stale_context(void)
1292 {
1293     WWAI(&t);
1294     o.ctx = &t2;
1295     WWL(&o, &t);
1296 }
1297 
1298 static void ww_test_edeadlk_normal(void)
1299 {
1300     int ret;
1301 
1302     mutex_lock(&o2.base);
1303     o2.ctx = &t2;
1304     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1305 
1306     WWAI(&t);
1307     t2 = t;
1308     t2.stamp--;
1309 
1310     ret = WWL(&o, &t);
1311     WARN_ON(ret);
1312 
1313     ret = WWL(&o2, &t);
1314     WARN_ON(ret != -EDEADLK);
1315 
1316     o2.ctx = NULL;
1317     mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1318     mutex_unlock(&o2.base);
1319     WWU(&o);
1320 
1321     WWL(&o2, &t);
1322 }
1323 
1324 static void ww_test_edeadlk_normal_slow(void)
1325 {
1326     int ret;
1327 
1328     mutex_lock(&o2.base);
1329     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1330     o2.ctx = &t2;
1331 
1332     WWAI(&t);
1333     t2 = t;
1334     t2.stamp--;
1335 
1336     ret = WWL(&o, &t);
1337     WARN_ON(ret);
1338 
1339     ret = WWL(&o2, &t);
1340     WARN_ON(ret != -EDEADLK);
1341 
1342     o2.ctx = NULL;
1343     mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1344     mutex_unlock(&o2.base);
1345     WWU(&o);
1346 
1347     ww_mutex_lock_slow(&o2, &t);
1348 }
1349 
1350 static void ww_test_edeadlk_no_unlock(void)
1351 {
1352     int ret;
1353 
1354     mutex_lock(&o2.base);
1355     o2.ctx = &t2;
1356     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1357 
1358     WWAI(&t);
1359     t2 = t;
1360     t2.stamp--;
1361 
1362     ret = WWL(&o, &t);
1363     WARN_ON(ret);
1364 
1365     ret = WWL(&o2, &t);
1366     WARN_ON(ret != -EDEADLK);
1367 
1368     o2.ctx = NULL;
1369     mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1370     mutex_unlock(&o2.base);
1371 
1372     WWL(&o2, &t);
1373 }
1374 
1375 static void ww_test_edeadlk_no_unlock_slow(void)
1376 {
1377     int ret;
1378 
1379     mutex_lock(&o2.base);
1380     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1381     o2.ctx = &t2;
1382 
1383     WWAI(&t);
1384     t2 = t;
1385     t2.stamp--;
1386 
1387     ret = WWL(&o, &t);
1388     WARN_ON(ret);
1389 
1390     ret = WWL(&o2, &t);
1391     WARN_ON(ret != -EDEADLK);
1392 
1393     o2.ctx = NULL;
1394     mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_);
1395     mutex_unlock(&o2.base);
1396 
1397     ww_mutex_lock_slow(&o2, &t);
1398 }
1399 
1400 static void ww_test_edeadlk_acquire_more(void)
1401 {
1402     int ret;
1403 
1404     mutex_lock(&o2.base);
1405     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1406     o2.ctx = &t2;
1407 
1408     WWAI(&t);
1409     t2 = t;
1410     t2.stamp--;
1411 
1412     ret = WWL(&o, &t);
1413     WARN_ON(ret);
1414 
1415     ret = WWL(&o2, &t);
1416     WARN_ON(ret != -EDEADLK);
1417 
1418     ret = WWL(&o3, &t);
1419 }
1420 
1421 static void ww_test_edeadlk_acquire_more_slow(void)
1422 {
1423     int ret;
1424 
1425     mutex_lock(&o2.base);
1426     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1427     o2.ctx = &t2;
1428 
1429     WWAI(&t);
1430     t2 = t;
1431     t2.stamp--;
1432 
1433     ret = WWL(&o, &t);
1434     WARN_ON(ret);
1435 
1436     ret = WWL(&o2, &t);
1437     WARN_ON(ret != -EDEADLK);
1438 
1439     ww_mutex_lock_slow(&o3, &t);
1440 }
1441 
1442 static void ww_test_edeadlk_acquire_more_edeadlk(void)
1443 {
1444     int ret;
1445 
1446     mutex_lock(&o2.base);
1447     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1448     o2.ctx = &t2;
1449 
1450     mutex_lock(&o3.base);
1451     mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1452     o3.ctx = &t2;
1453 
1454     WWAI(&t);
1455     t2 = t;
1456     t2.stamp--;
1457 
1458     ret = WWL(&o, &t);
1459     WARN_ON(ret);
1460 
1461     ret = WWL(&o2, &t);
1462     WARN_ON(ret != -EDEADLK);
1463 
1464     ret = WWL(&o3, &t);
1465     WARN_ON(ret != -EDEADLK);
1466 }
1467 
1468 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void)
1469 {
1470     int ret;
1471 
1472     mutex_lock(&o2.base);
1473     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1474     o2.ctx = &t2;
1475 
1476     mutex_lock(&o3.base);
1477     mutex_release(&o3.base.dep_map, 1, _THIS_IP_);
1478     o3.ctx = &t2;
1479 
1480     WWAI(&t);
1481     t2 = t;
1482     t2.stamp--;
1483 
1484     ret = WWL(&o, &t);
1485     WARN_ON(ret);
1486 
1487     ret = WWL(&o2, &t);
1488     WARN_ON(ret != -EDEADLK);
1489 
1490     ww_mutex_lock_slow(&o3, &t);
1491 }
1492 
1493 static void ww_test_edeadlk_acquire_wrong(void)
1494 {
1495     int ret;
1496 
1497     mutex_lock(&o2.base);
1498     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1499     o2.ctx = &t2;
1500 
1501     WWAI(&t);
1502     t2 = t;
1503     t2.stamp--;
1504 
1505     ret = WWL(&o, &t);
1506     WARN_ON(ret);
1507 
1508     ret = WWL(&o2, &t);
1509     WARN_ON(ret != -EDEADLK);
1510     if (!ret)
1511         WWU(&o2);
1512 
1513     WWU(&o);
1514 
1515     ret = WWL(&o3, &t);
1516 }
1517 
1518 static void ww_test_edeadlk_acquire_wrong_slow(void)
1519 {
1520     int ret;
1521 
1522     mutex_lock(&o2.base);
1523     mutex_release(&o2.base.dep_map, 1, _THIS_IP_);
1524     o2.ctx = &t2;
1525 
1526     WWAI(&t);
1527     t2 = t;
1528     t2.stamp--;
1529 
1530     ret = WWL(&o, &t);
1531     WARN_ON(ret);
1532 
1533     ret = WWL(&o2, &t);
1534     WARN_ON(ret != -EDEADLK);
1535     if (!ret)
1536         WWU(&o2);
1537 
1538     WWU(&o);
1539 
1540     ww_mutex_lock_slow(&o3, &t);
1541 }
1542 
1543 static void ww_test_spin_nest_unlocked(void)
1544 {
1545     raw_spin_lock_nest_lock(&lock_A, &o.base);
1546     U(A);
1547 }
1548 
1549 static void ww_test_unneeded_slow(void)
1550 {
1551     WWAI(&t);
1552 
1553     ww_mutex_lock_slow(&o, &t);
1554 }
1555 
1556 static void ww_test_context_block(void)
1557 {
1558     int ret;
1559 
1560     WWAI(&t);
1561 
1562     ret = WWL(&o, &t);
1563     WARN_ON(ret);
1564     WWL1(&o2);
1565 }
1566 
1567 static void ww_test_context_try(void)
1568 {
1569     int ret;
1570 
1571     WWAI(&t);
1572 
1573     ret = WWL(&o, &t);
1574     WARN_ON(ret);
1575 
1576     ret = WWT(&o2);
1577     WARN_ON(!ret);
1578     WWU(&o2);
1579     WWU(&o);
1580 }
1581 
1582 static void ww_test_context_context(void)
1583 {
1584     int ret;
1585 
1586     WWAI(&t);
1587 
1588     ret = WWL(&o, &t);
1589     WARN_ON(ret);
1590 
1591     ret = WWL(&o2, &t);
1592     WARN_ON(ret);
1593 
1594     WWU(&o2);
1595     WWU(&o);
1596 }
1597 
1598 static void ww_test_try_block(void)
1599 {
1600     bool ret;
1601 
1602     ret = WWT(&o);
1603     WARN_ON(!ret);
1604 
1605     WWL1(&o2);
1606     WWU(&o2);
1607     WWU(&o);
1608 }
1609 
1610 static void ww_test_try_try(void)
1611 {
1612     bool ret;
1613 
1614     ret = WWT(&o);
1615     WARN_ON(!ret);
1616     ret = WWT(&o2);
1617     WARN_ON(!ret);
1618     WWU(&o2);
1619     WWU(&o);
1620 }
1621 
1622 static void ww_test_try_context(void)
1623 {
1624     int ret;
1625 
1626     ret = WWT(&o);
1627     WARN_ON(!ret);
1628 
1629     WWAI(&t);
1630 
1631     ret = WWL(&o2, &t);
1632     WARN_ON(ret);
1633 }
1634 
1635 static void ww_test_block_block(void)
1636 {
1637     WWL1(&o);
1638     WWL1(&o2);
1639 }
1640 
1641 static void ww_test_block_try(void)
1642 {
1643     bool ret;
1644 
1645     WWL1(&o);
1646     ret = WWT(&o2);
1647     WARN_ON(!ret);
1648 }
1649 
1650 static void ww_test_block_context(void)
1651 {
1652     int ret;
1653 
1654     WWL1(&o);
1655     WWAI(&t);
1656 
1657     ret = WWL(&o2, &t);
1658     WARN_ON(ret);
1659 }
1660 
1661 static void ww_test_spin_block(void)
1662 {
1663     L(A);
1664     U(A);
1665 
1666     WWL1(&o);
1667     L(A);
1668     U(A);
1669     WWU(&o);
1670 
1671     L(A);
1672     WWL1(&o);
1673     WWU(&o);
1674     U(A);
1675 }
1676 
1677 static void ww_test_spin_try(void)
1678 {
1679     bool ret;
1680 
1681     L(A);
1682     U(A);
1683 
1684     ret = WWT(&o);
1685     WARN_ON(!ret);
1686     L(A);
1687     U(A);
1688     WWU(&o);
1689 
1690     L(A);
1691     ret = WWT(&o);
1692     WARN_ON(!ret);
1693     WWU(&o);
1694     U(A);
1695 }
1696 
1697 static void ww_test_spin_context(void)
1698 {
1699     int ret;
1700 
1701     L(A);
1702     U(A);
1703 
1704     WWAI(&t);
1705 
1706     ret = WWL(&o, &t);
1707     WARN_ON(ret);
1708     L(A);
1709     U(A);
1710     WWU(&o);
1711 
1712     L(A);
1713     ret = WWL(&o, &t);
1714     WARN_ON(ret);
1715     WWU(&o);
1716     U(A);
1717 }
1718 
1719 static void ww_tests(void)
1720 {
1721     printk("  --------------------------------------------------------------------------\n");
1722     printk("  | Wound/wait tests |\n");
1723     printk("  ---------------------\n");
1724 
1725     print_testname("ww api failures");
1726     dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW);
1727     dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW);
1728     dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW);
1729     pr_cont("\n");
1730 
1731     print_testname("ww contexts mixing");
1732     dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW);
1733     dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW);
1734     pr_cont("\n");
1735 
1736     print_testname("finishing ww context");
1737     dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW);
1738     dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW);
1739     dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW);
1740     dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW);
1741     pr_cont("\n");
1742 
1743     print_testname("locking mismatches");
1744     dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW);
1745     dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW);
1746     dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW);
1747     pr_cont("\n");
1748 
1749     print_testname("EDEADLK handling");
1750     dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW);
1751     dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW);
1752     dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW);
1753     dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW);
1754     dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW);
1755     dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW);
1756     dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW);
1757     dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW);
1758     dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW);
1759     dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW);
1760     pr_cont("\n");
1761 
1762     print_testname("spinlock nest unlocked");
1763     dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW);
1764     pr_cont("\n");
1765 
1766     printk("  -----------------------------------------------------\n");
1767     printk("                                 |block | try  |context|\n");
1768     printk("  -----------------------------------------------------\n");
1769 
1770     print_testname("context");
1771     dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW);
1772     dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW);
1773     dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW);
1774     pr_cont("\n");
1775 
1776     print_testname("try");
1777     dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW);
1778     dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW);
1779     dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW);
1780     pr_cont("\n");
1781 
1782     print_testname("block");
1783     dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW);
1784     dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW);
1785     dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW);
1786     pr_cont("\n");
1787 
1788     print_testname("spinlock");
1789     dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW);
1790     dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW);
1791     dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW);
1792     pr_cont("\n");
1793 }
1794 
1795 void locking_selftest(void)
1796 {
1797     /*
1798      * Got a locking failure before the selftest ran?
1799      */
1800     if (!debug_locks) {
1801         printk("----------------------------------\n");
1802         printk("| Locking API testsuite disabled |\n");
1803         printk("----------------------------------\n");
1804         return;
1805     }
1806 
1807     /*
1808      * Run the testsuite:
1809      */
1810     printk("------------------------\n");
1811     printk("| Locking API testsuite:\n");
1812     printk("----------------------------------------------------------------------------\n");
1813     printk("                                 | spin |wlock |rlock |mutex | wsem | rsem |\n");
1814     printk("  --------------------------------------------------------------------------\n");
1815 
1816     init_shared_classes();
1817     debug_locks_silent = !debug_locks_verbose;
1818 
1819     DO_TESTCASE_6R("A-A deadlock", AA);
1820     DO_TESTCASE_6R("A-B-B-A deadlock", ABBA);
1821     DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA);
1822     DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC);
1823     DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA);
1824     DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA);
1825     DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA);
1826     DO_TESTCASE_6("double unlock", double_unlock);
1827     DO_TESTCASE_6("initialize held", init_held);
1828     DO_TESTCASE_6_SUCCESS("bad unlock order", bad_unlock_order);
1829 
1830     printk("  --------------------------------------------------------------------------\n");
1831     print_testname("recursive read-lock");
1832     pr_cont("             |");
1833     dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK);
1834     pr_cont("             |");
1835     dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM);
1836     pr_cont("\n");
1837 
1838     print_testname("recursive read-lock #2");
1839     pr_cont("             |");
1840     dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK);
1841     pr_cont("             |");
1842     dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM);
1843     pr_cont("\n");
1844 
1845     print_testname("mixed read-write-lock");
1846     pr_cont("             |");
1847     dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK);
1848     pr_cont("             |");
1849     dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM);
1850     pr_cont("\n");
1851 
1852     print_testname("mixed write-read-lock");
1853     pr_cont("             |");
1854     dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK);
1855     pr_cont("             |");
1856     dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM);
1857     pr_cont("\n");
1858 
1859     printk("  --------------------------------------------------------------------------\n");
1860 
1861     /*
1862      * irq-context testcases:
1863      */
1864     DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1);
1865     DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A);
1866     DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B);
1867     DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3);
1868     DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
1869     DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
1870 
1871     DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
1872 //  DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
1873 
1874     ww_tests();
1875 
1876     if (unexpected_testcase_failures) {
1877         printk("-----------------------------------------------------------------\n");
1878         debug_locks = 0;
1879         printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
1880             unexpected_testcase_failures, testcase_total);
1881         printk("-----------------------------------------------------------------\n");
1882     } else if (expected_testcase_failures && testcase_successes) {
1883         printk("--------------------------------------------------------\n");
1884         printk("%3d out of %3d testcases failed, as expected. |\n",
1885             expected_testcase_failures, testcase_total);
1886         printk("----------------------------------------------------\n");
1887         debug_locks = 1;
1888     } else if (expected_testcase_failures && !testcase_successes) {
1889         printk("--------------------------------------------------------\n");
1890         printk("All %3d testcases failed, as expected. |\n",
1891             expected_testcase_failures);
1892         printk("----------------------------------------\n");
1893         debug_locks = 1;
1894     } else {
1895         printk("-------------------------------------------------------\n");
1896         printk("Good, all %3d testcases passed! |\n",
1897             testcase_successes);
1898         printk("---------------------------------\n");
1899         debug_locks = 1;
1900     }
1901     debug_locks_silent = 0;
1902 }