Lines Matching +full:a +full:- +full:h

1 // SPDX-License-Identifier: GPL-2.0
3 * lib/locking-selftest.c
6 * mutexes and rw-semaphores.
14 #include <linux/rwsem.h>
15 #include <linux/mutex.h>
16 #include <linux/ww_mutex.h>
17 #include <linux/sched.h>
18 #include <linux/sched/mm.h>
19 #include <linux/delay.h>
20 #include <linux/lockdep.h>
21 #include <linux/spinlock.h>
22 #include <linux/kallsyms.h>
23 #include <linux/interrupt.h>
24 #include <linux/debug_locks.h>
25 #include <linux/irqflags.h>
26 #include <linux/rtmutex.h>
27 #include <linux/local_lock.h>
68 * Normal standalone locks, for the circular and irq-context
107 * we do not trigger a real lockup:
151 * non-inlined runtime initializers, to let separate locks share
152 * the same lock-class:
193 * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests.
194 * The following functions use a lock from a simulated hardirq/softirq
195 * context, causing the locks to be marked as hardirq-safe/softirq-safe:
261 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } whi…
276 * the same basic lock-dependency/state events:
309 #include "locking-selftest-spin.h"
311 #include "locking-selftest-wlock.h"
313 #include "locking-selftest-rlock.h"
315 #include "locking-selftest-mutex.h"
317 #include "locking-selftest-wsem.h"
319 #include "locking-selftest-rsem.h"
323 #include "locking-selftest-rtmutex.h"
330 * Special-case for read-locking, they are
384 * read_lock(A)
387 * write_lock(A)
416 * read_lock(A)
419 * write_lock(A)
422 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ
425 * { read_lock(A)->lock(B) dependency exists }
429 * read_lock(A);
431 * { Not a deadlock, B -> A is added in the chain cache }
435 * write_lock(A);
437 * { B->A found in chain cache, not reported as a deadlock }
459 * read_lock(A)
462 * read_lock(A)
492 * write_lock(A)
495 * write_lock(A)
529 LOCK_UNLOCK_2(A, B); \
530 LOCK_UNLOCK_2(B, A); /* fail */
535 #include "locking-selftest-spin.h"
537 #include "locking-selftest-wlock.h"
539 #include "locking-selftest-rlock.h"
541 #include "locking-selftest-mutex.h"
543 #include "locking-selftest-wsem.h"
545 #include "locking-selftest-rsem.h"
549 #include "locking-selftest-rtmutex.h"
561 LOCK_UNLOCK_2(A, B); \
563 LOCK_UNLOCK_2(C, A); /* fail */
568 #include "locking-selftest-spin.h"
570 #include "locking-selftest-wlock.h"
572 #include "locking-selftest-rlock.h"
574 #include "locking-selftest-mutex.h"
576 #include "locking-selftest-wsem.h"
578 #include "locking-selftest-rsem.h"
582 #include "locking-selftest-rtmutex.h"
594 LOCK_UNLOCK_2(A, B); \
595 LOCK_UNLOCK_2(C, A); \
601 #include "locking-selftest-spin.h"
603 #include "locking-selftest-wlock.h"
605 #include "locking-selftest-rlock.h"
607 #include "locking-selftest-mutex.h"
609 #include "locking-selftest-wsem.h"
611 #include "locking-selftest-rsem.h"
615 #include "locking-selftest-rtmutex.h"
627 LOCK_UNLOCK_2(A, B); \
630 LOCK_UNLOCK_2(D, A); /* fail */
635 #include "locking-selftest-spin.h"
637 #include "locking-selftest-wlock.h"
639 #include "locking-selftest-rlock.h"
641 #include "locking-selftest-mutex.h"
643 #include "locking-selftest-wsem.h"
645 #include "locking-selftest-rsem.h"
649 #include "locking-selftest-rtmutex.h"
660 LOCK_UNLOCK_2(A, B); \
663 LOCK_UNLOCK_2(D, A); /* fail */
668 #include "locking-selftest-spin.h"
670 #include "locking-selftest-wlock.h"
672 #include "locking-selftest-rlock.h"
674 #include "locking-selftest-mutex.h"
676 #include "locking-selftest-wsem.h"
678 #include "locking-selftest-rsem.h"
682 #include "locking-selftest-rtmutex.h"
693 LOCK_UNLOCK_2(A, B); \
696 LOCK_UNLOCK_2(D, A); /* fail */
701 #include "locking-selftest-spin.h"
703 #include "locking-selftest-wlock.h"
705 #include "locking-selftest-rlock.h"
707 #include "locking-selftest-mutex.h"
709 #include "locking-selftest-wsem.h"
711 #include "locking-selftest-rsem.h"
715 #include "locking-selftest-rtmutex.h"
731 LOCK(A); \
733 UNLOCK(A); \
734 UNLOCK(A); /* fail */
739 #include "locking-selftest-spin.h"
741 #include "locking-selftest-wlock.h"
743 #include "locking-selftest-rlock.h"
745 #include "locking-selftest-mutex.h"
747 #include "locking-selftest-wsem.h"
749 #include "locking-selftest-rsem.h"
753 #include "locking-selftest-rtmutex.h"
760 * initializing a held lock:
764 LOCK(A); \
765 INIT(A); /* fail */
770 #include "locking-selftest-spin.h"
772 #include "locking-selftest-wlock.h"
774 #include "locking-selftest-rlock.h"
776 #include "locking-selftest-mutex.h"
778 #include "locking-selftest-wsem.h"
780 #include "locking-selftest-rsem.h"
784 #include "locking-selftest-rtmutex.h"
791 * locking an irq-safe lock with irqs enabled:
796 LOCK(A); \
797 UNLOCK(A); \
802 LOCK(A); \
803 UNLOCK(A);
808 #include "locking-selftest-spin-hardirq.h"
811 #include "locking-selftest-rlock-hardirq.h"
814 #include "locking-selftest-wlock-hardirq.h" in GENERATE_PERMUTATIONS_2_EVENTS()
818 #include "locking-selftest-spin-softirq.h" in GENERATE_PERMUTATIONS_2_EVENTS()
821 #include "locking-selftest-rlock-softirq.h" in GENERATE_PERMUTATIONS_2_EVENTS()
824 #include "locking-selftest-wlock-softirq.h" in GENERATE_PERMUTATIONS_2_EVENTS()
833 * Enabling hardirqs with a softirq-safe lock held: in GENERATE_PERMUTATIONS_2_EVENTS()
838 LOCK(A); \
839 UNLOCK(A); \
845 LOCK(A); \
847 UNLOCK(A);
852 #include "locking-selftest-spin.h"
855 #include "locking-selftest-wlock.h"
858 #include "locking-selftest-rlock.h"
867 * Enabling irqs with an irq-safe lock held:
872 LOCK(A); \
873 UNLOCK(A); \
879 LOCK(A); \
881 UNLOCK(A);
886 #include "locking-selftest-spin-hardirq.h"
889 #include "locking-selftest-rlock-hardirq.h"
892 #include "locking-selftest-wlock-hardirq.h"
896 #include "locking-selftest-spin-softirq.h"
899 #include "locking-selftest-rlock-softirq.h"
902 #include "locking-selftest-wlock-softirq.h"
910 * Acquiring a irq-unsafe lock while holding an irq-safe-lock:
914 LOCK(A); \
917 UNLOCK(A); \
927 LOCK(A); \
928 UNLOCK(A); \
934 #include "locking-selftest-spin-hardirq.h"
937 #include "locking-selftest-rlock-hardirq.h"
940 #include "locking-selftest-wlock-hardirq.h"
944 #include "locking-selftest-spin-softirq.h"
947 #include "locking-selftest-rlock-softirq.h"
950 #include "locking-selftest-wlock-softirq.h"
959 * If a lock turns into softirq-safe, but earlier it took
960 * a softirq-unsafe lock:
965 LOCK(A); \
968 UNLOCK(A); \
977 LOCK(A); \
978 UNLOCK(A); \
984 #include "locking-selftest-spin-hardirq.h"
987 #include "locking-selftest-rlock-hardirq.h"
990 #include "locking-selftest-wlock-hardirq.h"
994 #include "locking-selftest-spin-softirq.h"
997 #include "locking-selftest-rlock-softirq.h"
1000 #include "locking-selftest-wlock-softirq.h"
1009 * read-lock / write-lock irq inversion.
1013 * CPU#1 is at #1, i.e. it has write-locked A, but has not
1018 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A.
1021 * will spin on A.
1027 WL(A); \
1030 WU(A); \
1041 RL(A); \
1042 RU(A); \
1048 #include "locking-selftest-spin-hardirq.h"
1051 #include "locking-selftest-rlock-hardirq.h"
1054 #include "locking-selftest-wlock-hardirq.h"
1058 #include "locking-selftest-spin-softirq.h"
1061 #include "locking-selftest-rlock-softirq.h"
1064 #include "locking-selftest-wlock-softirq.h"
1073 * write-read / write-read / write-read deadlock even if read is recursive
1097 #include "locking-selftest-rlock.h"
1105 * write-write / read-read / write-read deadlock even if read is recursive
1129 #include "locking-selftest-rlock.h"
1137 * write-write / read-read / read-write is not deadlock when read is recursive
1161 #include "locking-selftest-rlock.h"
1169 * write-read / read-read / write-write is not deadlock when read is recursive
1193 #include "locking-selftest-rlock.h"
1200 * read-lock / write-lock recursion that is actually safe.
1206 WL(A); \
1207 WU(A); \
1212 RL(A); \
1213 RU(A); \
1218 LOCK(A); \
1221 UNLOCK(A); \
1227 #include "locking-selftest-hardirq.h"
1228 #include "locking-selftest-rlock.h"
1231 #include "locking-selftest-wlock.h"
1235 #include "locking-selftest-softirq.h"
1236 #include "locking-selftest-rlock.h"
1239 #include "locking-selftest-wlock.h"
1248 * read-lock / write-lock recursion that is unsafe.
1255 LOCK(A); \
1256 UNLOCK(A); \
1262 RL(A); \
1263 RU(A); \
1275 #include "locking-selftest-hardirq.h"
1276 #include "locking-selftest-rlock.h"
1279 #include "locking-selftest-wlock.h"
1283 #include "locking-selftest-softirq.h"
1284 #include "locking-selftest-rlock.h"
1287 #include "locking-selftest-wlock.h"
1295 * read-lock / write-lock recursion that is unsafe.
1297 * A is a ENABLED_*_READ lock
1298 * B is a USED_IN_*_READ lock
1300 * read_lock(A);
1304 * write_lock(A); // if this one is read_lock(), no deadlock
1311 LOCK(A); \
1312 UNLOCK(A); \
1318 RL(A); \
1319 RU(A); \
1331 #include "locking-selftest-hardirq.h"
1332 #include "locking-selftest-rlock.h"
1335 #include "locking-selftest-wlock.h"
1339 #include "locking-selftest-softirq.h"
1340 #include "locking-selftest-rlock.h"
1343 #include "locking-selftest-wlock.h"
1402 I1(A); I1(B); I1(C); I1(D);
1405 I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B);
1406 I_LOCAL_LOCK(A);
1410 I2(A); I2(B); I2(C); I2(D);
1435 int saved_mgd_count = current->migration_disabled; in dotest()
1437 int saved_rcu_count = current->rcu_read_lock_nesting; in dotest()
1468 * Some tests (e.g. double-unlock) might corrupt the preemption in dotest()
1475 while (current->migration_disabled > saved_mgd_count) in dotest()
1479 while (current->rcu_read_lock_nesting > saved_rcu_count) in dotest()
1481 WARN_ON_ONCE(current->rcu_read_lock_nesting < saved_rcu_count); in dotest()
1486 current->softirqs_enabled = 0; in dotest()
1488 current->softirqs_enabled = 1; in dotest()
1550 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \
1551 NON_RT(DO_TESTCASE_2RW("soft-"desc, name##_soft, nr)) \
1598 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \
1599 NON_RT(DO_TESTCASE_1("soft-"desc, name##_soft, nr));
1602 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \
1603 NON_RT(DO_TESTCASE_1B("soft-"desc, name##_soft, nr));
1606 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \
1607 NON_RT(DO_TESTCASE_3("soft-"desc, name##_soft, nr));
1610 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \
1611 NON_RT(DO_TESTCASE_3RW("soft-"desc, name##_soft, nr));
1684 WARN_ON(ret != -EALREADY); in ww_test_fail_acquire()
1692 WARN_ON(ret != -EDEADLK); in ww_test_fail_acquire()
1859 t2.stamp--; in ww_test_edeadlk_normal()
1865 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_normal()
1885 t2.stamp--; in ww_test_edeadlk_normal_slow()
1891 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_normal_slow()
1911 t2.stamp--; in ww_test_edeadlk_no_unlock()
1917 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_no_unlock()
1936 t2.stamp--; in ww_test_edeadlk_no_unlock_slow()
1942 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_no_unlock_slow()
1961 t2.stamp--; in ww_test_edeadlk_acquire_more()
1967 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_acquire_more()
1982 t2.stamp--; in ww_test_edeadlk_acquire_more_slow()
1988 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_acquire_more_slow()
2007 t2.stamp--; in ww_test_edeadlk_acquire_more_edeadlk()
2013 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_acquire_more_edeadlk()
2016 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_acquire_more_edeadlk()
2033 t2.stamp--; in ww_test_edeadlk_acquire_more_edeadlk_slow()
2039 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_acquire_more_edeadlk_slow()
2054 t2.stamp--; in ww_test_edeadlk_acquire_wrong()
2060 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_acquire_wrong()
2079 t2.stamp--; in ww_test_edeadlk_acquire_wrong_slow()
2085 WARN_ON(ret != -EDEADLK); in ww_test_edeadlk_acquire_wrong_slow()
2097 U(A); in ww_test_spin_nest_unlocked()
2100 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */
2227 L(A); in ww_test_spin_block()
2228 U(A); in ww_test_spin_block()
2231 L(A); in ww_test_spin_block()
2232 U(A); in ww_test_spin_block()
2235 L(A); in ww_test_spin_block()
2238 U(A); in ww_test_spin_block()
2245 L(A); in ww_test_spin_try()
2246 U(A); in ww_test_spin_try()
2250 L(A); in ww_test_spin_try()
2251 U(A); in ww_test_spin_try()
2254 L(A); in ww_test_spin_try()
2258 U(A); in ww_test_spin_try()
2265 L(A); in ww_test_spin_context()
2266 U(A); in ww_test_spin_context()
2272 L(A); in ww_test_spin_context()
2273 U(A); in ww_test_spin_context()
2276 L(A); in ww_test_spin_context()
2280 U(A); in ww_test_spin_context()
2285 printk(" --------------------------------------------------------------------------\n"); in ww_tests()
2287 printk(" ---------------------\n"); in ww_tests()
2334 printk(" -----------------------------------------------------\n"); in ww_tests()
2336 printk(" -----------------------------------------------------\n"); in ww_tests()
2366 * read_lock(&A);
2370 * read_lock(&A);
2372 * is a deadlock.
2395 * read_lock(&A);
2396 * read_lock(&A);
2399 * is not a deadlock.
2421 * read_lock(&A);
2424 * read_lock(&A);
2426 * is a deadlock. Because the two read_lock()s are both non-recursive readers.
2449 printk(" --------------------------------------------------------------------------\n"); in queued_read_lock_tests()
2451 printk(" ---------------------------\n"); in queued_read_lock_tests()
2452 print_testname("hardirq read-lock/lock-read"); in queued_read_lock_tests()
2456 print_testname("hardirq lock-read/read-lock"); in queued_read_lock_tests()
2492 printk(" --------------------\n"); in fs_reclaim_tests()
2494 printk(" --------------------\n"); in fs_reclaim_tests()
2529 /* Relies the reversed clean-up ordering: inner first */ \
2542 * ---------------+-------+----------+------+-------
2544 * ---------------+-------+----------+------+-------
2546 * ---------------+-------+----------+------+-------
2548 * ---------------+-------+----------+------+-------
2550 * ---------------+-------+----------+------+-------
2552 * ---------------+-------+----------+------+-------
2554 * ---------------+-------+----------+------+-------
2556 * ---------------+-------+----------+------+-------
2558 * ---------------+-------+----------+------+-------
2560 * ---------------+-------+----------+------+-------
2588 * is a sleepable lock for PREEMPT_RT)
2605 printk(" --------------------------------------------------------------------------\n");
2607 printk(" --------------------------------------------------------------------------\n");
2609 printk(" --------------------------------------------------------------------------\n");
2626 print_testname("in RCU-bh context");
2630 print_testname("in RCU-sched context");
2649 local_lock(&local_A); /* IRQ-ON */ in local_lock_2()
2653 spin_lock(&lock_A); /* IN-IRQ */ in local_lock_2()
2659 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle, false */ in local_lock_2()
2667 local_lock(&local_A); /* IRQ-ON */ in local_lock_3A()
2668 spin_lock(&lock_B); /* IRQ-ON */ in local_lock_3A()
2673 spin_lock(&lock_A); /* IN-IRQ */ in local_lock_3A()
2679 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */ in local_lock_3A()
2687 local_lock(&local_A); /* IRQ-ON */ in local_lock_3B()
2688 spin_lock(&lock_B); /* IRQ-ON */ in local_lock_3B()
2693 spin_lock(&lock_A); /* IN-IRQ */ in local_lock_3B()
2699 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */ in local_lock_3B()
2706 spin_lock(&lock_B); /* IN-IRQ <-> IRQ-ON cycle, true */ in local_lock_3B()
2716 return rwsem->dep_map.name; in rw_semaphore_lockdep_name()
2737 * of creating a new one.
2741 printk(" --------------------------------------------------------------------------\n"); in lockdep_set_subclass_name_test()
2743 printk(" -----------------------------------\n"); in lockdep_set_subclass_name_test()
2752 printk(" --------------------------------------------------------------------------\n"); in local_lock_tests()
2754 printk(" ---------------------\n"); in local_lock_tests()
2760 print_testname("local_lock inversion 3A"); in local_lock_tests()
2771 /* mutex_A is hardirq-unsafe and softirq-unsafe */ in hardirq_deadlock_softirq_not_deadlock()
2772 /* mutex_A -> lock_C */ in hardirq_deadlock_softirq_not_deadlock()
2780 /* lock_A is hardirq-safe */ in hardirq_deadlock_softirq_not_deadlock()
2786 /* lock_A -> lock_B */ in hardirq_deadlock_softirq_not_deadlock()
2794 /* lock_B -> lock_C */ in hardirq_deadlock_softirq_not_deadlock()
2802 /* lock_D is softirq-safe */ in hardirq_deadlock_softirq_not_deadlock()
2808 /* And lock_D is hardirq-unsafe */ in hardirq_deadlock_softirq_not_deadlock()
2815 * mutex_A -> lock_C -> lock_D is softirq-unsafe -> softirq-safe, not in hardirq_deadlock_softirq_not_deadlock()
2818 * lock_A -> lock_B -> lock_C -> lock_D is hardirq-safe -> in hardirq_deadlock_softirq_not_deadlock()
2819 * hardirq-unsafe, deadlock. in hardirq_deadlock_softirq_not_deadlock()
2832 * Got a locking failure before the selftest ran? in locking_selftest()
2835 printk("----------------------------------\n"); in locking_selftest()
2837 printk("----------------------------------\n"); in locking_selftest()
2849 printk("------------------------\n"); in locking_selftest()
2851 printk("----------------------------------------------------------------------------\n"); in locking_selftest()
2853 printk(" --------------------------------------------------------------------------\n"); in locking_selftest()
2858 DO_TESTCASE_6R("A-A deadlock", AA); in locking_selftest()
2859 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA); in locking_selftest()
2860 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA); in locking_selftest()
2861 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC); in locking_selftest()
2862 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA); in locking_selftest()
2863 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA); in locking_selftest()
2864 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA); in locking_selftest()
2868 printk(" --------------------------------------------------------------------------\n"); in locking_selftest()
2869 print_testname("recursive read-lock"); in locking_selftest()
2876 print_testname("recursive read-lock #2"); in locking_selftest()
2883 print_testname("mixed read-write-lock"); in locking_selftest()
2890 print_testname("mixed write-read-lock"); in locking_selftest()
2897 print_testname("mixed read-lock/lock-write ABBA"); in locking_selftest()
2903 print_testname("mixed read-lock/lock-read ABBA"); in locking_selftest()
2909 print_testname("mixed write-lock/lock-write ABBA"); in locking_selftest()
2915 print_testname("chain cached mixed R-L/L-W ABBA"); in locking_selftest()
2924 printk(" --------------------------------------------------------------------------\n"); in locking_selftest()
2926 * irq-context testcases: in locking_selftest()
2928 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1); in locking_selftest()
2929 NON_RT(DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A)); in locking_selftest()
2930 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B); in locking_selftest()
2931 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3); in locking_selftest()
2932 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4); in locking_selftest()
2933 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion); in locking_selftest()
2935 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion); in locking_selftest()
2936 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2); in locking_selftest()
2937 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3); in locking_selftest()
2963 printk("-----------------------------------------------------------------\n"); in locking_selftest()
2965 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n", in locking_selftest()
2967 printk("-----------------------------------------------------------------\n"); in locking_selftest()
2969 printk("--------------------------------------------------------\n"); in locking_selftest()
2972 printk("----------------------------------------------------\n"); in locking_selftest()
2975 printk("--------------------------------------------------------\n"); in locking_selftest()
2978 printk("----------------------------------------\n"); in locking_selftest()
2981 printk("-------------------------------------------------------\n"); in locking_selftest()
2984 printk("---------------------------------\n"); in locking_selftest()