lib: radix-tree: delete duplicated words
[linux-2.6-microblaze.git] / lib / locking-selftest.c
index 14f44f5..a899b3f 100644 (file)
@@ -28,6 +28,7 @@
  * Change this to 1 if you want to see the failure printouts:
  */
 static unsigned int debug_locks_verbose;
+unsigned int force_read_lock_recursive;
 
 static DEFINE_WD_CLASS(ww_lockdep);
 
@@ -395,6 +396,49 @@ static void rwsem_ABBA1(void)
        MU(Y1); // should fail
 }
 
+/*
+ * read_lock(A)
+ * spin_lock(B)
+ *             spin_lock(B)
+ *             write_lock(A)
+ *
+ * This test case is aimed at poking whether the chain cache prevents us from
+ * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
+ * read/write locks, the following case may happen
+ *
+ *     { read_lock(A)->lock(B) dependency exists }
+ *
+ *     P0:
+ *     lock(B);
+ *     read_lock(A);
+ *
+ *     { Not a deadlock, B -> A is added in the chain cache }
+ *
+ *     P1:
+ *     lock(B);
+ *     write_lock(A);
+ *
+ *     { B->A found in chain cache, not reported as a deadlock }
+ *
+ */
+static void rlock_chaincache_ABBA1(void)
+{
+       RL(X1);
+       L(Y1);
+       U(Y1);
+       RU(X1);
+
+       L(Y1);
+       RL(X1);
+       RU(X1);
+       U(Y1);
+
+       L(Y1);
+       WL(X1);
+       WU(X1);
+       U(Y1); // should fail
+}
+
 /*
  * read_lock(A)
  * spin_lock(B)
@@ -990,6 +1034,133 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
 #undef E2
 #undef E3
 
+/*
+ * write-read / write-read / write-read deadlock even if read is recursive
+ */
+
+#define E1()                           \
+                                       \
+       WL(X1);                         \
+       RL(Y1);                         \
+       RU(Y1);                         \
+       WU(X1);
+
+#define E2()                           \
+                                       \
+       WL(Y1);                         \
+       RL(Z1);                         \
+       RU(Z1);                         \
+       WU(Y1);
+
+#define E3()                           \
+                                       \
+       WL(Z1);                         \
+       RL(X1);                         \
+       RU(X1);                         \
+       WU(Z1);
+
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1)
+
+#undef E1
+#undef E2
+#undef E3
+
+/*
+ * write-write / read-read / write-read deadlock even if read is recursive
+ */
+
+#define E1()                           \
+                                       \
+       WL(X1);                         \
+       WL(Y1);                         \
+       WU(Y1);                         \
+       WU(X1);
+
+#define E2()                           \
+                                       \
+       RL(Y1);                         \
+       RL(Z1);                         \
+       RU(Z1);                         \
+       RU(Y1);
+
+#define E3()                           \
+                                       \
+       WL(Z1);                         \
+       RL(X1);                         \
+       RU(X1);                         \
+       WU(Z1);
+
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1)
+
+#undef E1
+#undef E2
+#undef E3
+
+/*
+ * write-write / read-read / read-write is not deadlock when read is recursive
+ */
+
+#define E1()                           \
+                                       \
+       WL(X1);                         \
+       WL(Y1);                         \
+       WU(Y1);                         \
+       WU(X1);
+
+#define E2()                           \
+                                       \
+       RL(Y1);                         \
+       RL(Z1);                         \
+       RU(Z1);                         \
+       RU(Y1);
+
+#define E3()                           \
+                                       \
+       RL(Z1);                         \
+       WL(X1);                         \
+       WU(X1);                         \
+       RU(Z1);
+
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1)
+
+#undef E1
+#undef E2
+#undef E3
+
+/*
+ * write-read / read-read / write-write is not deadlock when read is recursive
+ */
+
+#define E1()                           \
+                                       \
+       WL(X1);                         \
+       RL(Y1);                         \
+       RU(Y1);                         \
+       WU(X1);
+
+#define E2()                           \
+                                       \
+       RL(Y1);                         \
+       RL(Z1);                         \
+       RU(Z1);                         \
+       RU(Y1);
+
+#define E3()                           \
+                                       \
+       WL(Z1);                         \
+       WL(X1);                         \
+       WU(X1);                         \
+       WU(Z1);
+
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1)
+
+#undef E1
+#undef E2
+#undef E3
 /*
  * read-lock / write-lock recursion that is actually safe.
  */
@@ -1009,20 +1180,28 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
 #define E3()                           \
                                        \
        IRQ_ENTER();                    \
-       RL(A);                          \
+       LOCK(A);                        \
        L(B);                           \
        U(B);                           \
-       RU(A);                          \
+       UNLOCK(A);                      \
        IRQ_EXIT();
 
 /*
- * Generate 12 testcases:
+ * Generate 24 testcases:
  */
 #include "locking-selftest-hardirq.h"
-GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard)
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock)
+
+#include "locking-selftest-wlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock)
 
 #include "locking-selftest-softirq.h"
-GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock)
+
+#include "locking-selftest-wlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock)
 
 #undef E1
 #undef E2
@@ -1036,8 +1215,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
                                        \
        IRQ_DISABLE();                  \
        L(B);                           \
-       WL(A);                          \
-       WU(A);                          \
+       LOCK(A);                        \
+       UNLOCK(A);                      \
        U(B);                           \
        IRQ_ENABLE();
 
@@ -1054,13 +1233,75 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
        IRQ_EXIT();
 
 /*
- * Generate 12 testcases:
+ * Generate 24 testcases:
  */
 #include "locking-selftest-hardirq.h"
-// GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard)
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock)
+
+#include "locking-selftest-wlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock)
 
 #include "locking-selftest-softirq.h"
-// GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft)
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock)
+
+#include "locking-selftest-wlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock)
+
+#undef E1
+#undef E2
+#undef E3
+/*
+ * read-lock / write-lock recursion that is unsafe.
+ *
+ * A is a ENABLED_*_READ lock
+ * B is a USED_IN_*_READ lock
+ *
+ * read_lock(A);
+ *                     write_lock(B);
+ * <interrupt>
+ * read_lock(B);
+ *                     write_lock(A); // if this one is read_lock(), no deadlock
+ */
+
+#define E1()                           \
+                                       \
+       IRQ_DISABLE();                  \
+       WL(B);                          \
+       LOCK(A);                        \
+       UNLOCK(A);                      \
+       WU(B);                          \
+       IRQ_ENABLE();
+
+#define E2()                           \
+                                       \
+       RL(A);                          \
+       RU(A);                          \
+
+#define E3()                           \
+                                       \
+       IRQ_ENTER();                    \
+       RL(B);                          \
+       RU(B);                          \
+       IRQ_EXIT();
+
+/*
+ * Generate 24 testcases:
+ */
+#include "locking-selftest-hardirq.h"
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock)
+
+#include "locking-selftest-wlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock)
+
+#include "locking-selftest-softirq.h"
+#include "locking-selftest-rlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock)
+
+#include "locking-selftest-wlock.h"
+GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock)
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map)
@@ -1199,6 +1440,19 @@ static inline void print_testname(const char *testname)
        dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);          \
        pr_cont("\n");
 
+#define DO_TESTCASE_1RR(desc, name, nr)                                \
+       print_testname(desc"/"#nr);                             \
+       pr_cont("             |");                              \
+       dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK);          \
+       pr_cont("\n");
+
+#define DO_TESTCASE_1RRB(desc, name, nr)                       \
+       print_testname(desc"/"#nr);                             \
+       pr_cont("             |");                              \
+       dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK);          \
+       pr_cont("\n");
+
+
 #define DO_TESTCASE_3(desc, name, nr)                          \
        print_testname(desc"/"#nr);                             \
        dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN);       \
@@ -1213,6 +1467,25 @@ static inline void print_testname(const char *testname)
        dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
        pr_cont("\n");
 
+#define DO_TESTCASE_2RW(desc, name, nr)                                \
+       print_testname(desc"/"#nr);                             \
+       pr_cont("      |");                                     \
+       dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK);    \
+       dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK);    \
+       pr_cont("\n");
+
+#define DO_TESTCASE_2x2RW(desc, name, nr)                      \
+       DO_TESTCASE_2RW("hard-"desc, name##_hard, nr)           \
+       DO_TESTCASE_2RW("soft-"desc, name##_soft, nr)           \
+
+#define DO_TESTCASE_6x2x2RW(desc, name)                                \
+       DO_TESTCASE_2x2RW(desc, name, 123);                     \
+       DO_TESTCASE_2x2RW(desc, name, 132);                     \
+       DO_TESTCASE_2x2RW(desc, name, 213);                     \
+       DO_TESTCASE_2x2RW(desc, name, 231);                     \
+       DO_TESTCASE_2x2RW(desc, name, 312);                     \
+       DO_TESTCASE_2x2RW(desc, name, 321);
+
 #define DO_TESTCASE_6(desc, name)                              \
        print_testname(desc);                                   \
        dotest(name##_spin, FAILURE, LOCKTYPE_SPIN);            \
@@ -1289,6 +1562,22 @@ static inline void print_testname(const char *testname)
        DO_TESTCASE_2IB(desc, name, 312);                       \
        DO_TESTCASE_2IB(desc, name, 321);
 
+#define DO_TESTCASE_6x1RR(desc, name)                          \
+       DO_TESTCASE_1RR(desc, name, 123);                       \
+       DO_TESTCASE_1RR(desc, name, 132);                       \
+       DO_TESTCASE_1RR(desc, name, 213);                       \
+       DO_TESTCASE_1RR(desc, name, 231);                       \
+       DO_TESTCASE_1RR(desc, name, 312);                       \
+       DO_TESTCASE_1RR(desc, name, 321);
+
+#define DO_TESTCASE_6x1RRB(desc, name)                         \
+       DO_TESTCASE_1RRB(desc, name, 123);                      \
+       DO_TESTCASE_1RRB(desc, name, 132);                      \
+       DO_TESTCASE_1RRB(desc, name, 213);                      \
+       DO_TESTCASE_1RRB(desc, name, 231);                      \
+       DO_TESTCASE_1RRB(desc, name, 312);                      \
+       DO_TESTCASE_1RRB(desc, name, 321);
+
 #define DO_TESTCASE_6x6(desc, name)                            \
        DO_TESTCASE_6I(desc, name, 123);                        \
        DO_TESTCASE_6I(desc, name, 132);                        \
@@ -1966,6 +2255,108 @@ static void ww_tests(void)
        pr_cont("\n");
 }
 
+
+/*
+ * <in hardirq handler>
+ * read_lock(&A);
+ *                     <hardirq disable>
+ *                     spin_lock(&B);
+ * spin_lock(&B);
+ *                     read_lock(&A);
+ *
+ * is a deadlock.
+ */
+static void queued_read_lock_hardirq_RE_Er(void)
+{
+       HARDIRQ_ENTER();
+       read_lock(&rwlock_A);
+       LOCK(B);
+       UNLOCK(B);
+       read_unlock(&rwlock_A);
+       HARDIRQ_EXIT();
+
+       HARDIRQ_DISABLE();
+       LOCK(B);
+       read_lock(&rwlock_A);
+       read_unlock(&rwlock_A);
+       UNLOCK(B);
+       HARDIRQ_ENABLE();
+}
+
+/*
+ * <in hardirq handler>
+ * spin_lock(&B);
+ *                     <hardirq disable>
+ *                     read_lock(&A);
+ * read_lock(&A);
+ *                     spin_lock(&B);
+ *
+ * is not a deadlock.
+ */
+static void queued_read_lock_hardirq_ER_rE(void)
+{
+       HARDIRQ_ENTER();
+       LOCK(B);
+       read_lock(&rwlock_A);
+       read_unlock(&rwlock_A);
+       UNLOCK(B);
+       HARDIRQ_EXIT();
+
+       HARDIRQ_DISABLE();
+       read_lock(&rwlock_A);
+       LOCK(B);
+       UNLOCK(B);
+       read_unlock(&rwlock_A);
+       HARDIRQ_ENABLE();
+}
+
+/*
+ * <hardirq disable>
+ * spin_lock(&B);
+ *                     read_lock(&A);
+ *                     <in hardirq handler>
+ *                     spin_lock(&B);
+ * read_lock(&A);
+ *
+ * is a deadlock. Because the two read_lock()s are both non-recursive readers.
+ */
+static void queued_read_lock_hardirq_inversion(void)
+{
+
+       HARDIRQ_ENTER();
+       LOCK(B);
+       UNLOCK(B);
+       HARDIRQ_EXIT();
+
+       HARDIRQ_DISABLE();
+       LOCK(B);
+       read_lock(&rwlock_A);
+       read_unlock(&rwlock_A);
+       UNLOCK(B);
+       HARDIRQ_ENABLE();
+
+       read_lock(&rwlock_A);
+       read_unlock(&rwlock_A);
+}
+
+static void queued_read_lock_tests(void)
+{
+       printk("  --------------------------------------------------------------------------\n");
+       printk("  | queued read lock tests |\n");
+       printk("  ---------------------------\n");
+       print_testname("hardirq read-lock/lock-read");
+       dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK);
+       pr_cont("\n");
+
+       print_testname("hardirq lock-read/read-lock");
+       dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK);
+       pr_cont("\n");
+
+       print_testname("hardirq inversion");
+       dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK);
+       pr_cont("\n");
+}
+
 void locking_selftest(void)
 {
        /*
@@ -1978,6 +2369,11 @@ void locking_selftest(void)
                return;
        }
 
+       /*
+        * treats read_lock() as recursive read locks for testing purpose
+        */
+       force_read_lock_recursive = 1;
+
        /*
         * Run the testsuite:
         */
@@ -2033,14 +2429,6 @@ void locking_selftest(void)
        print_testname("mixed read-lock/lock-write ABBA");
        pr_cont("             |");
        dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
-#ifdef CONFIG_PROVE_LOCKING
-       /*
-        * Lockdep does indeed fail here, but there's nothing we can do about
-        * that now.  Don't kill lockdep for it.
-        */
-       unexpected_testcase_failures--;
-#endif
-
        pr_cont("             |");
        dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM);
 
@@ -2056,6 +2444,15 @@ void locking_selftest(void)
        pr_cont("             |");
        dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM);
 
+       print_testname("chain cached mixed R-L/L-W ABBA");
+       pr_cont("             |");
+       dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK);
+
+       DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1);
+       DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1);
+       DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1);
+       DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1);
+
        printk("  --------------------------------------------------------------------------\n");
 
        /*
@@ -2068,11 +2465,19 @@ void locking_selftest(void)
        DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4);
        DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion);
 
-       DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
-//     DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
+       DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion);
+       DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2);
+       DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3);
 
        ww_tests();
 
+       force_read_lock_recursive = 0;
+       /*
+        * queued_read_lock() specific test cases can be put here
+        */
+       if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS))
+               queued_read_lock_tests();
+
        if (unexpected_testcase_failures) {
                printk("-----------------------------------------------------------------\n");
                debug_locks = 0;