1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * lib/locking-selftest.c 4 * 5 * Testsuite for various locking APIs: spinlocks, rwlocks, 6 * mutexes and rw-semaphores. 7 * 8 * It is checking both false positives and false negatives. 9 * 10 * Started by Ingo Molnar: 11 * 12 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 13 */ 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> 28 29 #ifdef CONFIG_PREEMPT_RT 30 # define NON_RT(...) 31 #else 32 # define NON_RT(...) __VA_ARGS__ 33 #endif 34 35 /* 36 * Change this to 1 if you want to see the failure printouts: 37 */ 38 static unsigned int debug_locks_verbose; 39 unsigned int force_read_lock_recursive; 40 41 static DEFINE_WD_CLASS(ww_lockdep); 42 43 static int __init setup_debug_locks_verbose(char *str) 44 { 45 get_option(&str, &debug_locks_verbose); 46 47 return 1; 48 } 49 50 __setup("debug_locks_verbose=", setup_debug_locks_verbose); 51 52 #define FAILURE 0 53 #define SUCCESS 1 54 55 #define LOCKTYPE_SPIN 0x1 56 #define LOCKTYPE_RWLOCK 0x2 57 #define LOCKTYPE_MUTEX 0x4 58 #define LOCKTYPE_RWSEM 0x8 59 #define LOCKTYPE_WW 0x10 60 #define LOCKTYPE_RTMUTEX 0x20 61 #define LOCKTYPE_LL 0x40 62 #define LOCKTYPE_SPECIAL 0x80 63 64 static struct ww_acquire_ctx t, t2; 65 static struct ww_mutex o, o2, o3; 66 67 /* 68 * Normal standalone locks, for the circular and irq-context 69 * dependency tests: 70 */ 71 static DEFINE_SPINLOCK(lock_A); 72 static DEFINE_SPINLOCK(lock_B); 73 static DEFINE_SPINLOCK(lock_C); 74 static DEFINE_SPINLOCK(lock_D); 75 76 static DEFINE_RAW_SPINLOCK(raw_lock_A); 77 static DEFINE_RAW_SPINLOCK(raw_lock_B); 78 79 static DEFINE_RWLOCK(rwlock_A); 80 static DEFINE_RWLOCK(rwlock_B); 81 static DEFINE_RWLOCK(rwlock_C); 82 static DEFINE_RWLOCK(rwlock_D); 83 84 static DEFINE_MUTEX(mutex_A); 85 static DEFINE_MUTEX(mutex_B); 86 static DEFINE_MUTEX(mutex_C); 87 static DEFINE_MUTEX(mutex_D); 88 89 static DECLARE_RWSEM(rwsem_A); 90 static DECLARE_RWSEM(rwsem_B); 91 static DECLARE_RWSEM(rwsem_C); 92 static DECLARE_RWSEM(rwsem_D); 93 94 #ifdef CONFIG_RT_MUTEXES 95 96 static DEFINE_RT_MUTEX(rtmutex_A); 97 static DEFINE_RT_MUTEX(rtmutex_B); 98 static DEFINE_RT_MUTEX(rtmutex_C); 99 static DEFINE_RT_MUTEX(rtmutex_D); 100 101 #endif 102 103 /* 104 * Locks that we initialize dynamically as well so that 105 * e.g. X1 and X2 becomes two instances of the same class, 106 * but X* and Y* are different classes. We do this so that 107 * we do not trigger a real lockup: 108 */ 109 static DEFINE_SPINLOCK(lock_X1); 110 static DEFINE_SPINLOCK(lock_X2); 111 static DEFINE_SPINLOCK(lock_Y1); 112 static DEFINE_SPINLOCK(lock_Y2); 113 static DEFINE_SPINLOCK(lock_Z1); 114 static DEFINE_SPINLOCK(lock_Z2); 115 116 static DEFINE_RWLOCK(rwlock_X1); 117 static DEFINE_RWLOCK(rwlock_X2); 118 static DEFINE_RWLOCK(rwlock_Y1); 119 static DEFINE_RWLOCK(rwlock_Y2); 120 static DEFINE_RWLOCK(rwlock_Z1); 121 static DEFINE_RWLOCK(rwlock_Z2); 122 123 static DEFINE_MUTEX(mutex_X1); 124 static DEFINE_MUTEX(mutex_X2); 125 static DEFINE_MUTEX(mutex_Y1); 126 static DEFINE_MUTEX(mutex_Y2); 127 static DEFINE_MUTEX(mutex_Z1); 128 static DEFINE_MUTEX(mutex_Z2); 129 130 static DECLARE_RWSEM(rwsem_X1); 131 static DECLARE_RWSEM(rwsem_X2); 132 static DECLARE_RWSEM(rwsem_Y1); 133 static DECLARE_RWSEM(rwsem_Y2); 134 static DECLARE_RWSEM(rwsem_Z1); 135 static DECLARE_RWSEM(rwsem_Z2); 136 137 #ifdef CONFIG_RT_MUTEXES 138 139 static DEFINE_RT_MUTEX(rtmutex_X1); 140 static DEFINE_RT_MUTEX(rtmutex_X2); 141 static DEFINE_RT_MUTEX(rtmutex_Y1); 142 static DEFINE_RT_MUTEX(rtmutex_Y2); 143 static DEFINE_RT_MUTEX(rtmutex_Z1); 144 static DEFINE_RT_MUTEX(rtmutex_Z2); 145 146 #endif 147 148 static DEFINE_PER_CPU(local_lock_t, local_A); 149 150 /* 151 * non-inlined runtime initializers, to let separate locks share 152 * the same lock-class: 153 */ 154 #define INIT_CLASS_FUNC(class) \ 155 static noinline void \ 156 init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \ 157 struct mutex *mutex, struct rw_semaphore *rwsem)\ 158 { \ 159 spin_lock_init(lock); \ 160 rwlock_init(rwlock); \ 161 mutex_init(mutex); \ 162 init_rwsem(rwsem); \ 163 } 164 165 INIT_CLASS_FUNC(X) 166 INIT_CLASS_FUNC(Y) 167 INIT_CLASS_FUNC(Z) 168 169 static void init_shared_classes(void) 170 { 171 #ifdef CONFIG_RT_MUTEXES 172 static struct lock_class_key rt_X, rt_Y, rt_Z; 173 174 __rt_mutex_init(&rtmutex_X1, __func__, &rt_X); 175 __rt_mutex_init(&rtmutex_X2, __func__, &rt_X); 176 __rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y); 177 __rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y); 178 __rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z); 179 __rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z); 180 #endif 181 182 init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1); 183 init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2); 184 185 init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1); 186 init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2); 187 188 init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1); 189 init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2); 190 } 191 192 /* 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: 196 */ 197 198 #define HARDIRQ_DISABLE local_irq_disable 199 #define HARDIRQ_ENABLE local_irq_enable 200 201 #define HARDIRQ_ENTER() \ 202 local_irq_disable(); \ 203 __irq_enter(); \ 204 lockdep_hardirq_threaded(); \ 205 WARN_ON(!in_hardirq()); 206 207 #define HARDIRQ_EXIT() \ 208 __irq_exit(); \ 209 local_irq_enable(); 210 211 #define SOFTIRQ_DISABLE local_bh_disable 212 #define SOFTIRQ_ENABLE local_bh_enable 213 214 #define SOFTIRQ_ENTER() \ 215 local_bh_disable(); \ 216 local_irq_disable(); \ 217 lockdep_softirq_enter(); \ 218 WARN_ON(!in_softirq()); 219 220 #define SOFTIRQ_EXIT() \ 221 lockdep_softirq_exit(); \ 222 local_irq_enable(); \ 223 local_bh_enable(); 224 225 /* 226 * Shortcuts for lock/unlock API variants, to keep 227 * the testcases compact: 228 */ 229 #define L(x) spin_lock(&lock_##x) 230 #define U(x) spin_unlock(&lock_##x) 231 #define LU(x) L(x); U(x) 232 #define SI(x) spin_lock_init(&lock_##x) 233 234 #define WL(x) write_lock(&rwlock_##x) 235 #define WU(x) write_unlock(&rwlock_##x) 236 #define WLU(x) WL(x); WU(x) 237 238 #define RL(x) read_lock(&rwlock_##x) 239 #define RU(x) read_unlock(&rwlock_##x) 240 #define RLU(x) RL(x); RU(x) 241 #define RWI(x) rwlock_init(&rwlock_##x) 242 243 #define ML(x) mutex_lock(&mutex_##x) 244 #define MU(x) mutex_unlock(&mutex_##x) 245 #define MI(x) mutex_init(&mutex_##x) 246 247 #define RTL(x) rt_mutex_lock(&rtmutex_##x) 248 #define RTU(x) rt_mutex_unlock(&rtmutex_##x) 249 #define RTI(x) rt_mutex_init(&rtmutex_##x) 250 251 #define WSL(x) down_write(&rwsem_##x) 252 #define WSU(x) up_write(&rwsem_##x) 253 254 #define RSL(x) down_read(&rwsem_##x) 255 #define RSU(x) up_read(&rwsem_##x) 256 #define RWSI(x) init_rwsem(&rwsem_##x) 257 258 #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 259 #define WWAI(x) ww_acquire_init(x, &ww_lockdep) 260 #else 261 #define WWAI(x) do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0) 262 #endif 263 #define WWAD(x) ww_acquire_done(x) 264 #define WWAF(x) ww_acquire_fini(x) 265 266 #define WWL(x, c) ww_mutex_lock(x, c) 267 #define WWT(x) ww_mutex_trylock(x, NULL) 268 #define WWL1(x) ww_mutex_lock(x, NULL) 269 #define WWU(x) ww_mutex_unlock(x) 270 271 272 #define LOCK_UNLOCK_2(x,y) LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x) 273 274 /* 275 * Generate different permutations of the same testcase, using 276 * the same basic lock-dependency/state events: 277 */ 278 279 #define GENERATE_TESTCASE(name) \ 280 \ 281 static void name(void) { E(); } 282 283 #define GENERATE_PERMUTATIONS_2_EVENTS(name) \ 284 \ 285 static void name##_12(void) { E1(); E2(); } \ 286 static void name##_21(void) { E2(); E1(); } 287 288 #define GENERATE_PERMUTATIONS_3_EVENTS(name) \ 289 \ 290 static void name##_123(void) { E1(); E2(); E3(); } \ 291 static void name##_132(void) { E1(); E3(); E2(); } \ 292 static void name##_213(void) { E2(); E1(); E3(); } \ 293 static void name##_231(void) { E2(); E3(); E1(); } \ 294 static void name##_312(void) { E3(); E1(); E2(); } \ 295 static void name##_321(void) { E3(); E2(); E1(); } 296 297 /* 298 * AA deadlock: 299 */ 300 301 #define E() \ 302 \ 303 LOCK(X1); \ 304 LOCK(X2); /* this one should fail */ 305 306 /* 307 * 6 testcases: 308 */ 309 #include "locking-selftest-spin.h" 310 GENERATE_TESTCASE(AA_spin) 311 #include "locking-selftest-wlock.h" 312 GENERATE_TESTCASE(AA_wlock) 313 #include "locking-selftest-rlock.h" 314 GENERATE_TESTCASE(AA_rlock) 315 #include "locking-selftest-mutex.h" 316 GENERATE_TESTCASE(AA_mutex) 317 #include "locking-selftest-wsem.h" 318 GENERATE_TESTCASE(AA_wsem) 319 #include "locking-selftest-rsem.h" 320 GENERATE_TESTCASE(AA_rsem) 321 322 #ifdef CONFIG_RT_MUTEXES 323 #include "locking-selftest-rtmutex.h" 324 GENERATE_TESTCASE(AA_rtmutex); 325 #endif 326 327 #undef E 328 329 /* 330 * Special-case for read-locking, they are 331 * allowed to recurse on the same lock class: 332 */ 333 static void rlock_AA1(void) 334 { 335 RL(X1); 336 RL(X1); // this one should NOT fail 337 } 338 339 static void rlock_AA1B(void) 340 { 341 RL(X1); 342 RL(X2); // this one should NOT fail 343 } 344 345 static void rsem_AA1(void) 346 { 347 RSL(X1); 348 RSL(X1); // this one should fail 349 } 350 351 static void rsem_AA1B(void) 352 { 353 RSL(X1); 354 RSL(X2); // this one should fail 355 } 356 /* 357 * The mixing of read and write locks is not allowed: 358 */ 359 static void rlock_AA2(void) 360 { 361 RL(X1); 362 WL(X2); // this one should fail 363 } 364 365 static void rsem_AA2(void) 366 { 367 RSL(X1); 368 WSL(X2); // this one should fail 369 } 370 371 static void rlock_AA3(void) 372 { 373 WL(X1); 374 RL(X2); // this one should fail 375 } 376 377 static void rsem_AA3(void) 378 { 379 WSL(X1); 380 RSL(X2); // this one should fail 381 } 382 383 /* 384 * read_lock(A) 385 * spin_lock(B) 386 * spin_lock(B) 387 * write_lock(A) 388 */ 389 static void rlock_ABBA1(void) 390 { 391 RL(X1); 392 L(Y1); 393 U(Y1); 394 RU(X1); 395 396 L(Y1); 397 WL(X1); 398 WU(X1); 399 U(Y1); // should fail 400 } 401 402 static void rwsem_ABBA1(void) 403 { 404 RSL(X1); 405 ML(Y1); 406 MU(Y1); 407 RSU(X1); 408 409 ML(Y1); 410 WSL(X1); 411 WSU(X1); 412 MU(Y1); // should fail 413 } 414 415 /* 416 * read_lock(A) 417 * spin_lock(B) 418 * spin_lock(B) 419 * write_lock(A) 420 * 421 * This test case is aimed at poking whether the chain cache prevents us from 422 * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ 423 * read/write locks, the following case may happen 424 * 425 * { read_lock(A)->lock(B) dependency exists } 426 * 427 * P0: 428 * lock(B); 429 * read_lock(A); 430 * 431 * { Not a deadlock, B -> A is added in the chain cache } 432 * 433 * P1: 434 * lock(B); 435 * write_lock(A); 436 * 437 * { B->A found in chain cache, not reported as a deadlock } 438 * 439 */ 440 static void rlock_chaincache_ABBA1(void) 441 { 442 RL(X1); 443 L(Y1); 444 U(Y1); 445 RU(X1); 446 447 L(Y1); 448 RL(X1); 449 RU(X1); 450 U(Y1); 451 452 L(Y1); 453 WL(X1); 454 WU(X1); 455 U(Y1); // should fail 456 } 457 458 /* 459 * read_lock(A) 460 * spin_lock(B) 461 * spin_lock(B) 462 * read_lock(A) 463 */ 464 static void rlock_ABBA2(void) 465 { 466 RL(X1); 467 L(Y1); 468 U(Y1); 469 RU(X1); 470 471 L(Y1); 472 RL(X1); 473 RU(X1); 474 U(Y1); // should NOT fail 475 } 476 477 static void rwsem_ABBA2(void) 478 { 479 RSL(X1); 480 ML(Y1); 481 MU(Y1); 482 RSU(X1); 483 484 ML(Y1); 485 RSL(X1); 486 RSU(X1); 487 MU(Y1); // should fail 488 } 489 490 491 /* 492 * write_lock(A) 493 * spin_lock(B) 494 * spin_lock(B) 495 * write_lock(A) 496 */ 497 static void rlock_ABBA3(void) 498 { 499 WL(X1); 500 L(Y1); 501 U(Y1); 502 WU(X1); 503 504 L(Y1); 505 WL(X1); 506 WU(X1); 507 U(Y1); // should fail 508 } 509 510 static void rwsem_ABBA3(void) 511 { 512 WSL(X1); 513 ML(Y1); 514 MU(Y1); 515 WSU(X1); 516 517 ML(Y1); 518 WSL(X1); 519 WSU(X1); 520 MU(Y1); // should fail 521 } 522 523 /* 524 * ABBA deadlock: 525 */ 526 527 #define E() \ 528 \ 529 LOCK_UNLOCK_2(A, B); \ 530 LOCK_UNLOCK_2(B, A); /* fail */ 531 532 /* 533 * 6 testcases: 534 */ 535 #include "locking-selftest-spin.h" 536 GENERATE_TESTCASE(ABBA_spin) 537 #include "locking-selftest-wlock.h" 538 GENERATE_TESTCASE(ABBA_wlock) 539 #include "locking-selftest-rlock.h" 540 GENERATE_TESTCASE(ABBA_rlock) 541 #include "locking-selftest-mutex.h" 542 GENERATE_TESTCASE(ABBA_mutex) 543 #include "locking-selftest-wsem.h" 544 GENERATE_TESTCASE(ABBA_wsem) 545 #include "locking-selftest-rsem.h" 546 GENERATE_TESTCASE(ABBA_rsem) 547 548 #ifdef CONFIG_RT_MUTEXES 549 #include "locking-selftest-rtmutex.h" 550 GENERATE_TESTCASE(ABBA_rtmutex); 551 #endif 552 553 #undef E 554 555 /* 556 * AB BC CA deadlock: 557 */ 558 559 #define E() \ 560 \ 561 LOCK_UNLOCK_2(A, B); \ 562 LOCK_UNLOCK_2(B, C); \ 563 LOCK_UNLOCK_2(C, A); /* fail */ 564 565 /* 566 * 6 testcases: 567 */ 568 #include "locking-selftest-spin.h" 569 GENERATE_TESTCASE(ABBCCA_spin) 570 #include "locking-selftest-wlock.h" 571 GENERATE_TESTCASE(ABBCCA_wlock) 572 #include "locking-selftest-rlock.h" 573 GENERATE_TESTCASE(ABBCCA_rlock) 574 #include "locking-selftest-mutex.h" 575 GENERATE_TESTCASE(ABBCCA_mutex) 576 #include "locking-selftest-wsem.h" 577 GENERATE_TESTCASE(ABBCCA_wsem) 578 #include "locking-selftest-rsem.h" 579 GENERATE_TESTCASE(ABBCCA_rsem) 580 581 #ifdef CONFIG_RT_MUTEXES 582 #include "locking-selftest-rtmutex.h" 583 GENERATE_TESTCASE(ABBCCA_rtmutex); 584 #endif 585 586 #undef E 587 588 /* 589 * AB CA BC deadlock: 590 */ 591 592 #define E() \ 593 \ 594 LOCK_UNLOCK_2(A, B); \ 595 LOCK_UNLOCK_2(C, A); \ 596 LOCK_UNLOCK_2(B, C); /* fail */ 597 598 /* 599 * 6 testcases: 600 */ 601 #include "locking-selftest-spin.h" 602 GENERATE_TESTCASE(ABCABC_spin) 603 #include "locking-selftest-wlock.h" 604 GENERATE_TESTCASE(ABCABC_wlock) 605 #include "locking-selftest-rlock.h" 606 GENERATE_TESTCASE(ABCABC_rlock) 607 #include "locking-selftest-mutex.h" 608 GENERATE_TESTCASE(ABCABC_mutex) 609 #include "locking-selftest-wsem.h" 610 GENERATE_TESTCASE(ABCABC_wsem) 611 #include "locking-selftest-rsem.h" 612 GENERATE_TESTCASE(ABCABC_rsem) 613 614 #ifdef CONFIG_RT_MUTEXES 615 #include "locking-selftest-rtmutex.h" 616 GENERATE_TESTCASE(ABCABC_rtmutex); 617 #endif 618 619 #undef E 620 621 /* 622 * AB BC CD DA deadlock: 623 */ 624 625 #define E() \ 626 \ 627 LOCK_UNLOCK_2(A, B); \ 628 LOCK_UNLOCK_2(B, C); \ 629 LOCK_UNLOCK_2(C, D); \ 630 LOCK_UNLOCK_2(D, A); /* fail */ 631 632 /* 633 * 6 testcases: 634 */ 635 #include "locking-selftest-spin.h" 636 GENERATE_TESTCASE(ABBCCDDA_spin) 637 #include "locking-selftest-wlock.h" 638 GENERATE_TESTCASE(ABBCCDDA_wlock) 639 #include "locking-selftest-rlock.h" 640 GENERATE_TESTCASE(ABBCCDDA_rlock) 641 #include "locking-selftest-mutex.h" 642 GENERATE_TESTCASE(ABBCCDDA_mutex) 643 #include "locking-selftest-wsem.h" 644 GENERATE_TESTCASE(ABBCCDDA_wsem) 645 #include "locking-selftest-rsem.h" 646 GENERATE_TESTCASE(ABBCCDDA_rsem) 647 648 #ifdef CONFIG_RT_MUTEXES 649 #include "locking-selftest-rtmutex.h" 650 GENERATE_TESTCASE(ABBCCDDA_rtmutex); 651 #endif 652 653 #undef E 654 655 /* 656 * AB CD BD DA deadlock: 657 */ 658 #define E() \ 659 \ 660 LOCK_UNLOCK_2(A, B); \ 661 LOCK_UNLOCK_2(C, D); \ 662 LOCK_UNLOCK_2(B, D); \ 663 LOCK_UNLOCK_2(D, A); /* fail */ 664 665 /* 666 * 6 testcases: 667 */ 668 #include "locking-selftest-spin.h" 669 GENERATE_TESTCASE(ABCDBDDA_spin) 670 #include "locking-selftest-wlock.h" 671 GENERATE_TESTCASE(ABCDBDDA_wlock) 672 #include "locking-selftest-rlock.h" 673 GENERATE_TESTCASE(ABCDBDDA_rlock) 674 #include "locking-selftest-mutex.h" 675 GENERATE_TESTCASE(ABCDBDDA_mutex) 676 #include "locking-selftest-wsem.h" 677 GENERATE_TESTCASE(ABCDBDDA_wsem) 678 #include "locking-selftest-rsem.h" 679 GENERATE_TESTCASE(ABCDBDDA_rsem) 680 681 #ifdef CONFIG_RT_MUTEXES 682 #include "locking-selftest-rtmutex.h" 683 GENERATE_TESTCASE(ABCDBDDA_rtmutex); 684 #endif 685 686 #undef E 687 688 /* 689 * AB CD BC DA deadlock: 690 */ 691 #define E() \ 692 \ 693 LOCK_UNLOCK_2(A, B); \ 694 LOCK_UNLOCK_2(C, D); \ 695 LOCK_UNLOCK_2(B, C); \ 696 LOCK_UNLOCK_2(D, A); /* fail */ 697 698 /* 699 * 6 testcases: 700 */ 701 #include "locking-selftest-spin.h" 702 GENERATE_TESTCASE(ABCDBCDA_spin) 703 #include "locking-selftest-wlock.h" 704 GENERATE_TESTCASE(ABCDBCDA_wlock) 705 #include "locking-selftest-rlock.h" 706 GENERATE_TESTCASE(ABCDBCDA_rlock) 707 #include "locking-selftest-mutex.h" 708 GENERATE_TESTCASE(ABCDBCDA_mutex) 709 #include "locking-selftest-wsem.h" 710 GENERATE_TESTCASE(ABCDBCDA_wsem) 711 #include "locking-selftest-rsem.h" 712 GENERATE_TESTCASE(ABCDBCDA_rsem) 713 714 #ifdef CONFIG_RT_MUTEXES 715 #include "locking-selftest-rtmutex.h" 716 GENERATE_TESTCASE(ABCDBCDA_rtmutex); 717 #endif 718 719 #undef E 720 721 #ifdef CONFIG_PREEMPT_RT 722 # define RT_PREPARE_DBL_UNLOCK() { migrate_disable(); rcu_read_lock(); } 723 #else 724 # define RT_PREPARE_DBL_UNLOCK() 725 #endif 726 /* 727 * Double unlock: 728 */ 729 #define E() \ 730 \ 731 LOCK(A); \ 732 RT_PREPARE_DBL_UNLOCK(); \ 733 UNLOCK(A); \ 734 UNLOCK(A); /* fail */ 735 736 /* 737 * 6 testcases: 738 */ 739 #include "locking-selftest-spin.h" 740 GENERATE_TESTCASE(double_unlock_spin) 741 #include "locking-selftest-wlock.h" 742 GENERATE_TESTCASE(double_unlock_wlock) 743 #include "locking-selftest-rlock.h" 744 GENERATE_TESTCASE(double_unlock_rlock) 745 #include "locking-selftest-mutex.h" 746 GENERATE_TESTCASE(double_unlock_mutex) 747 #include "locking-selftest-wsem.h" 748 GENERATE_TESTCASE(double_unlock_wsem) 749 #include "locking-selftest-rsem.h" 750 GENERATE_TESTCASE(double_unlock_rsem) 751 752 #ifdef CONFIG_RT_MUTEXES 753 #include "locking-selftest-rtmutex.h" 754 GENERATE_TESTCASE(double_unlock_rtmutex); 755 #endif 756 757 #undef E 758 759 /* 760 * initializing a held lock: 761 */ 762 #define E() \ 763 \ 764 LOCK(A); \ 765 INIT(A); /* fail */ 766 767 /* 768 * 6 testcases: 769 */ 770 #include "locking-selftest-spin.h" 771 GENERATE_TESTCASE(init_held_spin) 772 #include "locking-selftest-wlock.h" 773 GENERATE_TESTCASE(init_held_wlock) 774 #include "locking-selftest-rlock.h" 775 GENERATE_TESTCASE(init_held_rlock) 776 #include "locking-selftest-mutex.h" 777 GENERATE_TESTCASE(init_held_mutex) 778 #include "locking-selftest-wsem.h" 779 GENERATE_TESTCASE(init_held_wsem) 780 #include "locking-selftest-rsem.h" 781 GENERATE_TESTCASE(init_held_rsem) 782 783 #ifdef CONFIG_RT_MUTEXES 784 #include "locking-selftest-rtmutex.h" 785 GENERATE_TESTCASE(init_held_rtmutex); 786 #endif 787 788 #undef E 789 790 /* 791 * locking an irq-safe lock with irqs enabled: 792 */ 793 #define E1() \ 794 \ 795 IRQ_ENTER(); \ 796 LOCK(A); \ 797 UNLOCK(A); \ 798 IRQ_EXIT(); 799 800 #define E2() \ 801 \ 802 LOCK(A); \ 803 UNLOCK(A); 804 805 /* 806 * Generate 24 testcases: 807 */ 808 #include "locking-selftest-spin-hardirq.h" 809 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin) 810 811 #include "locking-selftest-rlock-hardirq.h" 812 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock) 813 814 #include "locking-selftest-wlock-hardirq.h" 815 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_wlock) 816 817 #ifndef CONFIG_PREEMPT_RT 818 #include "locking-selftest-spin-softirq.h" 819 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_spin) 820 821 #include "locking-selftest-rlock-softirq.h" 822 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock) 823 824 #include "locking-selftest-wlock-softirq.h" 825 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock) 826 #endif 827 828 #undef E1 829 #undef E2 830 831 #ifndef CONFIG_PREEMPT_RT 832 /* 833 * Enabling hardirqs with a softirq-safe lock held: 834 */ 835 #define E1() \ 836 \ 837 SOFTIRQ_ENTER(); \ 838 LOCK(A); \ 839 UNLOCK(A); \ 840 SOFTIRQ_EXIT(); 841 842 #define E2() \ 843 \ 844 HARDIRQ_DISABLE(); \ 845 LOCK(A); \ 846 HARDIRQ_ENABLE(); \ 847 UNLOCK(A); 848 849 /* 850 * Generate 12 testcases: 851 */ 852 #include "locking-selftest-spin.h" 853 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_spin) 854 855 #include "locking-selftest-wlock.h" 856 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_wlock) 857 858 #include "locking-selftest-rlock.h" 859 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock) 860 861 #undef E1 862 #undef E2 863 864 #endif 865 866 /* 867 * Enabling irqs with an irq-safe lock held: 868 */ 869 #define E1() \ 870 \ 871 IRQ_ENTER(); \ 872 LOCK(A); \ 873 UNLOCK(A); \ 874 IRQ_EXIT(); 875 876 #define E2() \ 877 \ 878 IRQ_DISABLE(); \ 879 LOCK(A); \ 880 IRQ_ENABLE(); \ 881 UNLOCK(A); 882 883 /* 884 * Generate 24 testcases: 885 */ 886 #include "locking-selftest-spin-hardirq.h" 887 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin) 888 889 #include "locking-selftest-rlock-hardirq.h" 890 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock) 891 892 #include "locking-selftest-wlock-hardirq.h" 893 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_wlock) 894 895 #ifndef CONFIG_PREEMPT_RT 896 #include "locking-selftest-spin-softirq.h" 897 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_spin) 898 899 #include "locking-selftest-rlock-softirq.h" 900 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock) 901 902 #include "locking-selftest-wlock-softirq.h" 903 GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock) 904 #endif 905 906 #undef E1 907 #undef E2 908 909 /* 910 * Acquiring a irq-unsafe lock while holding an irq-safe-lock: 911 */ 912 #define E1() \ 913 \ 914 LOCK(A); \ 915 LOCK(B); \ 916 UNLOCK(B); \ 917 UNLOCK(A); \ 918 919 #define E2() \ 920 \ 921 LOCK(B); \ 922 UNLOCK(B); 923 924 #define E3() \ 925 \ 926 IRQ_ENTER(); \ 927 LOCK(A); \ 928 UNLOCK(A); \ 929 IRQ_EXIT(); 930 931 /* 932 * Generate 36 testcases: 933 */ 934 #include "locking-selftest-spin-hardirq.h" 935 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin) 936 937 #include "locking-selftest-rlock-hardirq.h" 938 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock) 939 940 #include "locking-selftest-wlock-hardirq.h" 941 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_wlock) 942 943 #ifndef CONFIG_PREEMPT_RT 944 #include "locking-selftest-spin-softirq.h" 945 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_spin) 946 947 #include "locking-selftest-rlock-softirq.h" 948 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock) 949 950 #include "locking-selftest-wlock-softirq.h" 951 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock) 952 #endif 953 954 #undef E1 955 #undef E2 956 #undef E3 957 958 /* 959 * If a lock turns into softirq-safe, but earlier it took 960 * a softirq-unsafe lock: 961 */ 962 963 #define E1() \ 964 IRQ_DISABLE(); \ 965 LOCK(A); \ 966 LOCK(B); \ 967 UNLOCK(B); \ 968 UNLOCK(A); \ 969 IRQ_ENABLE(); 970 971 #define E2() \ 972 LOCK(B); \ 973 UNLOCK(B); 974 975 #define E3() \ 976 IRQ_ENTER(); \ 977 LOCK(A); \ 978 UNLOCK(A); \ 979 IRQ_EXIT(); 980 981 /* 982 * Generate 36 testcases: 983 */ 984 #include "locking-selftest-spin-hardirq.h" 985 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin) 986 987 #include "locking-selftest-rlock-hardirq.h" 988 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock) 989 990 #include "locking-selftest-wlock-hardirq.h" 991 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_wlock) 992 993 #ifndef CONFIG_PREEMPT_RT 994 #include "locking-selftest-spin-softirq.h" 995 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_spin) 996 997 #include "locking-selftest-rlock-softirq.h" 998 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock) 999 1000 #include "locking-selftest-wlock-softirq.h" 1001 GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock) 1002 #endif 1003 1004 #undef E1 1005 #undef E2 1006 #undef E3 1007 1008 /* 1009 * read-lock / write-lock irq inversion. 1010 * 1011 * Deadlock scenario: 1012 * 1013 * CPU#1 is at #1, i.e. it has write-locked A, but has not 1014 * taken B yet. 1015 * 1016 * CPU#2 is at #2, i.e. it has locked B. 1017 * 1018 * Hardirq hits CPU#2 at point #2 and is trying to read-lock A. 1019 * 1020 * The deadlock occurs because CPU#1 will spin on B, and CPU#2 1021 * will spin on A. 1022 */ 1023 1024 #define E1() \ 1025 \ 1026 IRQ_DISABLE(); \ 1027 WL(A); \ 1028 LOCK(B); \ 1029 UNLOCK(B); \ 1030 WU(A); \ 1031 IRQ_ENABLE(); 1032 1033 #define E2() \ 1034 \ 1035 LOCK(B); \ 1036 UNLOCK(B); 1037 1038 #define E3() \ 1039 \ 1040 IRQ_ENTER(); \ 1041 RL(A); \ 1042 RU(A); \ 1043 IRQ_EXIT(); 1044 1045 /* 1046 * Generate 36 testcases: 1047 */ 1048 #include "locking-selftest-spin-hardirq.h" 1049 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_spin) 1050 1051 #include "locking-selftest-rlock-hardirq.h" 1052 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_rlock) 1053 1054 #include "locking-selftest-wlock-hardirq.h" 1055 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_hard_wlock) 1056 1057 #ifndef CONFIG_PREEMPT_RT 1058 #include "locking-selftest-spin-softirq.h" 1059 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_spin) 1060 1061 #include "locking-selftest-rlock-softirq.h" 1062 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_rlock) 1063 1064 #include "locking-selftest-wlock-softirq.h" 1065 GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock) 1066 #endif 1067 1068 #undef E1 1069 #undef E2 1070 #undef E3 1071 1072 /* 1073 * write-read / write-read / write-read deadlock even if read is recursive 1074 */ 1075 1076 #define E1() \ 1077 \ 1078 WL(X1); \ 1079 RL(Y1); \ 1080 RU(Y1); \ 1081 WU(X1); 1082 1083 #define E2() \ 1084 \ 1085 WL(Y1); \ 1086 RL(Z1); \ 1087 RU(Z1); \ 1088 WU(Y1); 1089 1090 #define E3() \ 1091 \ 1092 WL(Z1); \ 1093 RL(X1); \ 1094 RU(X1); \ 1095 WU(Z1); 1096 1097 #include "locking-selftest-rlock.h" 1098 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_W2R3_W3R1) 1099 1100 #undef E1 1101 #undef E2 1102 #undef E3 1103 1104 /* 1105 * write-write / read-read / write-read deadlock even if read is recursive 1106 */ 1107 1108 #define E1() \ 1109 \ 1110 WL(X1); \ 1111 WL(Y1); \ 1112 WU(Y1); \ 1113 WU(X1); 1114 1115 #define E2() \ 1116 \ 1117 RL(Y1); \ 1118 RL(Z1); \ 1119 RU(Z1); \ 1120 RU(Y1); 1121 1122 #define E3() \ 1123 \ 1124 WL(Z1); \ 1125 RL(X1); \ 1126 RU(X1); \ 1127 WU(Z1); 1128 1129 #include "locking-selftest-rlock.h" 1130 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_W3R1) 1131 1132 #undef E1 1133 #undef E2 1134 #undef E3 1135 1136 /* 1137 * write-write / read-read / read-write is not deadlock when read is recursive 1138 */ 1139 1140 #define E1() \ 1141 \ 1142 WL(X1); \ 1143 WL(Y1); \ 1144 WU(Y1); \ 1145 WU(X1); 1146 1147 #define E2() \ 1148 \ 1149 RL(Y1); \ 1150 RL(Z1); \ 1151 RU(Z1); \ 1152 RU(Y1); 1153 1154 #define E3() \ 1155 \ 1156 RL(Z1); \ 1157 WL(X1); \ 1158 WU(X1); \ 1159 RU(Z1); 1160 1161 #include "locking-selftest-rlock.h" 1162 GENERATE_PERMUTATIONS_3_EVENTS(W1R2_R2R3_W3W1) 1163 1164 #undef E1 1165 #undef E2 1166 #undef E3 1167 1168 /* 1169 * write-read / read-read / write-write is not deadlock when read is recursive 1170 */ 1171 1172 #define E1() \ 1173 \ 1174 WL(X1); \ 1175 RL(Y1); \ 1176 RU(Y1); \ 1177 WU(X1); 1178 1179 #define E2() \ 1180 \ 1181 RL(Y1); \ 1182 RL(Z1); \ 1183 RU(Z1); \ 1184 RU(Y1); 1185 1186 #define E3() \ 1187 \ 1188 WL(Z1); \ 1189 WL(X1); \ 1190 WU(X1); \ 1191 WU(Z1); 1192 1193 #include "locking-selftest-rlock.h" 1194 GENERATE_PERMUTATIONS_3_EVENTS(W1W2_R2R3_R3W1) 1195 1196 #undef E1 1197 #undef E2 1198 #undef E3 1199 /* 1200 * read-lock / write-lock recursion that is actually safe. 1201 */ 1202 1203 #define E1() \ 1204 \ 1205 IRQ_DISABLE(); \ 1206 WL(A); \ 1207 WU(A); \ 1208 IRQ_ENABLE(); 1209 1210 #define E2() \ 1211 \ 1212 RL(A); \ 1213 RU(A); \ 1214 1215 #define E3() \ 1216 \ 1217 IRQ_ENTER(); \ 1218 LOCK(A); \ 1219 L(B); \ 1220 U(B); \ 1221 UNLOCK(A); \ 1222 IRQ_EXIT(); 1223 1224 /* 1225 * Generate 24 testcases: 1226 */ 1227 #include "locking-selftest-hardirq.h" 1228 #include "locking-selftest-rlock.h" 1229 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_rlock) 1230 1231 #include "locking-selftest-wlock.h" 1232 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_hard_wlock) 1233 1234 #ifndef CONFIG_PREEMPT_RT 1235 #include "locking-selftest-softirq.h" 1236 #include "locking-selftest-rlock.h" 1237 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_rlock) 1238 1239 #include "locking-selftest-wlock.h" 1240 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft_wlock) 1241 #endif 1242 1243 #undef E1 1244 #undef E2 1245 #undef E3 1246 1247 /* 1248 * read-lock / write-lock recursion that is unsafe. 1249 */ 1250 1251 #define E1() \ 1252 \ 1253 IRQ_DISABLE(); \ 1254 L(B); \ 1255 LOCK(A); \ 1256 UNLOCK(A); \ 1257 U(B); \ 1258 IRQ_ENABLE(); 1259 1260 #define E2() \ 1261 \ 1262 RL(A); \ 1263 RU(A); \ 1264 1265 #define E3() \ 1266 \ 1267 IRQ_ENTER(); \ 1268 L(B); \ 1269 U(B); \ 1270 IRQ_EXIT(); 1271 1272 /* 1273 * Generate 24 testcases: 1274 */ 1275 #include "locking-selftest-hardirq.h" 1276 #include "locking-selftest-rlock.h" 1277 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_rlock) 1278 1279 #include "locking-selftest-wlock.h" 1280 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_hard_wlock) 1281 1282 #ifndef CONFIG_PREEMPT_RT 1283 #include "locking-selftest-softirq.h" 1284 #include "locking-selftest-rlock.h" 1285 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_rlock) 1286 1287 #include "locking-selftest-wlock.h" 1288 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion2_soft_wlock) 1289 #endif 1290 1291 #undef E1 1292 #undef E2 1293 #undef E3 1294 /* 1295 * read-lock / write-lock recursion that is unsafe. 1296 * 1297 * A is a ENABLED_*_READ lock 1298 * B is a USED_IN_*_READ lock 1299 * 1300 * read_lock(A); 1301 * write_lock(B); 1302 * <interrupt> 1303 * read_lock(B); 1304 * write_lock(A); // if this one is read_lock(), no deadlock 1305 */ 1306 1307 #define E1() \ 1308 \ 1309 IRQ_DISABLE(); \ 1310 WL(B); \ 1311 LOCK(A); \ 1312 UNLOCK(A); \ 1313 WU(B); \ 1314 IRQ_ENABLE(); 1315 1316 #define E2() \ 1317 \ 1318 RL(A); \ 1319 RU(A); \ 1320 1321 #define E3() \ 1322 \ 1323 IRQ_ENTER(); \ 1324 RL(B); \ 1325 RU(B); \ 1326 IRQ_EXIT(); 1327 1328 /* 1329 * Generate 24 testcases: 1330 */ 1331 #include "locking-selftest-hardirq.h" 1332 #include "locking-selftest-rlock.h" 1333 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_rlock) 1334 1335 #include "locking-selftest-wlock.h" 1336 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_hard_wlock) 1337 1338 #ifndef CONFIG_PREEMPT_RT 1339 #include "locking-selftest-softirq.h" 1340 #include "locking-selftest-rlock.h" 1341 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_rlock) 1342 1343 #include "locking-selftest-wlock.h" 1344 GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion3_soft_wlock) 1345 #endif 1346 1347 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1348 # define I_SPINLOCK(x) lockdep_reset_lock(&lock_##x.dep_map) 1349 # define I_RAW_SPINLOCK(x) lockdep_reset_lock(&raw_lock_##x.dep_map) 1350 # define I_RWLOCK(x) lockdep_reset_lock(&rwlock_##x.dep_map) 1351 # define I_MUTEX(x) lockdep_reset_lock(&mutex_##x.dep_map) 1352 # define I_RWSEM(x) lockdep_reset_lock(&rwsem_##x.dep_map) 1353 # define I_WW(x) lockdep_reset_lock(&x.dep_map) 1354 # define I_LOCAL_LOCK(x) lockdep_reset_lock(this_cpu_ptr(&local_##x.dep_map)) 1355 #ifdef CONFIG_RT_MUTEXES 1356 # define I_RTMUTEX(x) lockdep_reset_lock(&rtmutex_##x.dep_map) 1357 #endif 1358 #else 1359 # define I_SPINLOCK(x) 1360 # define I_RAW_SPINLOCK(x) 1361 # define I_RWLOCK(x) 1362 # define I_MUTEX(x) 1363 # define I_RWSEM(x) 1364 # define I_WW(x) 1365 # define I_LOCAL_LOCK(x) 1366 #endif 1367 1368 #ifndef I_RTMUTEX 1369 # define I_RTMUTEX(x) 1370 #endif 1371 1372 #ifdef CONFIG_RT_MUTEXES 1373 #define I2_RTMUTEX(x) rt_mutex_init(&rtmutex_##x) 1374 #else 1375 #define I2_RTMUTEX(x) 1376 #endif 1377 1378 #define I1(x) \ 1379 do { \ 1380 I_SPINLOCK(x); \ 1381 I_RWLOCK(x); \ 1382 I_MUTEX(x); \ 1383 I_RWSEM(x); \ 1384 I_RTMUTEX(x); \ 1385 } while (0) 1386 1387 #define I2(x) \ 1388 do { \ 1389 spin_lock_init(&lock_##x); \ 1390 rwlock_init(&rwlock_##x); \ 1391 mutex_init(&mutex_##x); \ 1392 init_rwsem(&rwsem_##x); \ 1393 I2_RTMUTEX(x); \ 1394 } while (0) 1395 1396 static void reset_locks(void) 1397 { 1398 local_irq_disable(); 1399 lockdep_free_key_range(&ww_lockdep.acquire_key, 1); 1400 lockdep_free_key_range(&ww_lockdep.mutex_key, 1); 1401 1402 I1(A); I1(B); I1(C); I1(D); 1403 I1(X1); I1(X2); I1(Y1); I1(Y2); I1(Z1); I1(Z2); 1404 I_WW(t); I_WW(t2); I_WW(o.base); I_WW(o2.base); I_WW(o3.base); 1405 I_RAW_SPINLOCK(A); I_RAW_SPINLOCK(B); 1406 I_LOCAL_LOCK(A); 1407 1408 lockdep_reset(); 1409 1410 I2(A); I2(B); I2(C); I2(D); 1411 init_shared_classes(); 1412 raw_spin_lock_init(&raw_lock_A); 1413 raw_spin_lock_init(&raw_lock_B); 1414 local_lock_init(this_cpu_ptr(&local_A)); 1415 1416 ww_mutex_init(&o, &ww_lockdep); ww_mutex_init(&o2, &ww_lockdep); ww_mutex_init(&o3, &ww_lockdep); 1417 memset(&t, 0, sizeof(t)); memset(&t2, 0, sizeof(t2)); 1418 memset(&ww_lockdep.acquire_key, 0, sizeof(ww_lockdep.acquire_key)); 1419 memset(&ww_lockdep.mutex_key, 0, sizeof(ww_lockdep.mutex_key)); 1420 local_irq_enable(); 1421 } 1422 1423 #undef I 1424 1425 static int testcase_total; 1426 static int testcase_successes; 1427 static int expected_testcase_failures; 1428 static int unexpected_testcase_failures; 1429 1430 static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask) 1431 { 1432 int saved_preempt_count = preempt_count(); 1433 #ifdef CONFIG_PREEMPT_RT 1434 int saved_mgd_count = current->migration_disabled; 1435 int saved_rcu_count = current->rcu_read_lock_nesting; 1436 int saved_sched_rt_mutex = current->sched_rt_mutex; 1437 #endif 1438 1439 WARN_ON(irqs_disabled()); 1440 1441 debug_locks_silent = !(debug_locks_verbose & lockclass_mask); 1442 1443 testcase_fn(); 1444 /* 1445 * Filter out expected failures: 1446 */ 1447 #ifndef CONFIG_PROVE_LOCKING 1448 if (expected == FAILURE && debug_locks) { 1449 expected_testcase_failures++; 1450 pr_cont("failed|"); 1451 } 1452 else 1453 #endif 1454 if (debug_locks != expected) { 1455 unexpected_testcase_failures++; 1456 pr_cont("FAILED|"); 1457 } else { 1458 testcase_successes++; 1459 pr_cont(" ok |"); 1460 } 1461 testcase_total++; 1462 1463 if (debug_locks_verbose & lockclass_mask) 1464 pr_cont(" lockclass mask: %x, debug_locks: %d, expected: %d\n", 1465 lockclass_mask, debug_locks, expected); 1466 /* 1467 * Some tests (e.g. double-unlock) might corrupt the preemption 1468 * count, so restore it: 1469 */ 1470 preempt_count_set(saved_preempt_count); 1471 1472 #ifdef CONFIG_PREEMPT_RT 1473 current->sched_rt_mutex = saved_sched_rt_mutex; 1474 1475 while (current->migration_disabled > saved_mgd_count) 1476 migrate_enable(); 1477 1478 while (current->rcu_read_lock_nesting > saved_rcu_count) 1479 rcu_read_unlock(); 1480 WARN_ON_ONCE(current->rcu_read_lock_nesting < saved_rcu_count); 1481 #endif 1482 1483 #ifdef CONFIG_TRACE_IRQFLAGS 1484 if (softirq_count()) 1485 current->softirqs_enabled = 0; 1486 else 1487 current->softirqs_enabled = 1; 1488 #endif 1489 1490 reset_locks(); 1491 } 1492 1493 #ifdef CONFIG_RT_MUTEXES 1494 #define dotest_rt(fn, e, m) dotest((fn), (e), (m)) 1495 #else 1496 #define dotest_rt(fn, e, m) 1497 #endif 1498 1499 static inline void print_testname(const char *testname) 1500 { 1501 printk("%33s:", testname); 1502 } 1503 1504 #define DO_TESTCASE_1(desc, name, nr) \ 1505 print_testname(desc"/"#nr); \ 1506 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ 1507 pr_cont("\n"); 1508 1509 #define DO_TESTCASE_1B(desc, name, nr) \ 1510 print_testname(desc"/"#nr); \ 1511 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \ 1512 pr_cont("\n"); 1513 1514 #define DO_TESTCASE_1RR(desc, name, nr) \ 1515 print_testname(desc"/"#nr); \ 1516 pr_cont(" |"); \ 1517 dotest(name##_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ 1518 pr_cont("\n"); 1519 1520 #define DO_TESTCASE_1RRB(desc, name, nr) \ 1521 print_testname(desc"/"#nr); \ 1522 pr_cont(" |"); \ 1523 dotest(name##_##nr, FAILURE, LOCKTYPE_RWLOCK); \ 1524 pr_cont("\n"); 1525 1526 1527 #define DO_TESTCASE_3(desc, name, nr) \ 1528 print_testname(desc"/"#nr); \ 1529 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN); \ 1530 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \ 1531 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ 1532 pr_cont("\n"); 1533 1534 #define DO_TESTCASE_3RW(desc, name, nr) \ 1535 print_testname(desc"/"#nr); \ 1536 dotest(name##_spin_##nr, FAILURE, LOCKTYPE_SPIN|LOCKTYPE_RWLOCK);\ 1537 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \ 1538 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ 1539 pr_cont("\n"); 1540 1541 #define DO_TESTCASE_2RW(desc, name, nr) \ 1542 print_testname(desc"/"#nr); \ 1543 pr_cont(" |"); \ 1544 dotest(name##_wlock_##nr, FAILURE, LOCKTYPE_RWLOCK); \ 1545 dotest(name##_rlock_##nr, SUCCESS, LOCKTYPE_RWLOCK); \ 1546 pr_cont("\n"); 1547 1548 #define DO_TESTCASE_2x2RW(desc, name, nr) \ 1549 DO_TESTCASE_2RW("hard-"desc, name##_hard, nr) \ 1550 NON_RT(DO_TESTCASE_2RW("soft-"desc, name##_soft, nr)) \ 1551 1552 #define DO_TESTCASE_6x2x2RW(desc, name) \ 1553 DO_TESTCASE_2x2RW(desc, name, 123); \ 1554 DO_TESTCASE_2x2RW(desc, name, 132); \ 1555 DO_TESTCASE_2x2RW(desc, name, 213); \ 1556 DO_TESTCASE_2x2RW(desc, name, 231); \ 1557 DO_TESTCASE_2x2RW(desc, name, 312); \ 1558 DO_TESTCASE_2x2RW(desc, name, 321); 1559 1560 #define DO_TESTCASE_6(desc, name) \ 1561 print_testname(desc); \ 1562 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \ 1563 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \ 1564 dotest(name##_rlock, FAILURE, LOCKTYPE_RWLOCK); \ 1565 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \ 1566 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \ 1567 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \ 1568 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \ 1569 pr_cont("\n"); 1570 1571 #define DO_TESTCASE_6_SUCCESS(desc, name) \ 1572 print_testname(desc); \ 1573 dotest(name##_spin, SUCCESS, LOCKTYPE_SPIN); \ 1574 dotest(name##_wlock, SUCCESS, LOCKTYPE_RWLOCK); \ 1575 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \ 1576 dotest(name##_mutex, SUCCESS, LOCKTYPE_MUTEX); \ 1577 dotest(name##_wsem, SUCCESS, LOCKTYPE_RWSEM); \ 1578 dotest(name##_rsem, SUCCESS, LOCKTYPE_RWSEM); \ 1579 dotest_rt(name##_rtmutex, SUCCESS, LOCKTYPE_RTMUTEX); \ 1580 pr_cont("\n"); 1581 1582 /* 1583 * 'read' variant: rlocks must not trigger. 1584 */ 1585 #define DO_TESTCASE_6R(desc, name) \ 1586 print_testname(desc); \ 1587 dotest(name##_spin, FAILURE, LOCKTYPE_SPIN); \ 1588 dotest(name##_wlock, FAILURE, LOCKTYPE_RWLOCK); \ 1589 dotest(name##_rlock, SUCCESS, LOCKTYPE_RWLOCK); \ 1590 dotest(name##_mutex, FAILURE, LOCKTYPE_MUTEX); \ 1591 dotest(name##_wsem, FAILURE, LOCKTYPE_RWSEM); \ 1592 dotest(name##_rsem, FAILURE, LOCKTYPE_RWSEM); \ 1593 dotest_rt(name##_rtmutex, FAILURE, LOCKTYPE_RTMUTEX); \ 1594 pr_cont("\n"); 1595 1596 #define DO_TESTCASE_2I(desc, name, nr) \ 1597 DO_TESTCASE_1("hard-"desc, name##_hard, nr); \ 1598 NON_RT(DO_TESTCASE_1("soft-"desc, name##_soft, nr)); 1599 1600 #define DO_TESTCASE_2IB(desc, name, nr) \ 1601 DO_TESTCASE_1B("hard-"desc, name##_hard, nr); \ 1602 NON_RT(DO_TESTCASE_1B("soft-"desc, name##_soft, nr)); 1603 1604 #define DO_TESTCASE_6I(desc, name, nr) \ 1605 DO_TESTCASE_3("hard-"desc, name##_hard, nr); \ 1606 NON_RT(DO_TESTCASE_3("soft-"desc, name##_soft, nr)); 1607 1608 #define DO_TESTCASE_6IRW(desc, name, nr) \ 1609 DO_TESTCASE_3RW("hard-"desc, name##_hard, nr); \ 1610 NON_RT(DO_TESTCASE_3RW("soft-"desc, name##_soft, nr)); 1611 1612 #define DO_TESTCASE_2x3(desc, name) \ 1613 DO_TESTCASE_3(desc, name, 12); \ 1614 DO_TESTCASE_3(desc, name, 21); 1615 1616 #define DO_TESTCASE_2x6(desc, name) \ 1617 DO_TESTCASE_6I(desc, name, 12); \ 1618 DO_TESTCASE_6I(desc, name, 21); 1619 1620 #define DO_TESTCASE_6x2(desc, name) \ 1621 DO_TESTCASE_2I(desc, name, 123); \ 1622 DO_TESTCASE_2I(desc, name, 132); \ 1623 DO_TESTCASE_2I(desc, name, 213); \ 1624 DO_TESTCASE_2I(desc, name, 231); \ 1625 DO_TESTCASE_2I(desc, name, 312); \ 1626 DO_TESTCASE_2I(desc, name, 321); 1627 1628 #define DO_TESTCASE_6x2B(desc, name) \ 1629 DO_TESTCASE_2IB(desc, name, 123); \ 1630 DO_TESTCASE_2IB(desc, name, 132); \ 1631 DO_TESTCASE_2IB(desc, name, 213); \ 1632 DO_TESTCASE_2IB(desc, name, 231); \ 1633 DO_TESTCASE_2IB(desc, name, 312); \ 1634 DO_TESTCASE_2IB(desc, name, 321); 1635 1636 #define DO_TESTCASE_6x1RR(desc, name) \ 1637 DO_TESTCASE_1RR(desc, name, 123); \ 1638 DO_TESTCASE_1RR(desc, name, 132); \ 1639 DO_TESTCASE_1RR(desc, name, 213); \ 1640 DO_TESTCASE_1RR(desc, name, 231); \ 1641 DO_TESTCASE_1RR(desc, name, 312); \ 1642 DO_TESTCASE_1RR(desc, name, 321); 1643 1644 #define DO_TESTCASE_6x1RRB(desc, name) \ 1645 DO_TESTCASE_1RRB(desc, name, 123); \ 1646 DO_TESTCASE_1RRB(desc, name, 132); \ 1647 DO_TESTCASE_1RRB(desc, name, 213); \ 1648 DO_TESTCASE_1RRB(desc, name, 231); \ 1649 DO_TESTCASE_1RRB(desc, name, 312); \ 1650 DO_TESTCASE_1RRB(desc, name, 321); 1651 1652 #define DO_TESTCASE_6x6(desc, name) \ 1653 DO_TESTCASE_6I(desc, name, 123); \ 1654 DO_TESTCASE_6I(desc, name, 132); \ 1655 DO_TESTCASE_6I(desc, name, 213); \ 1656 DO_TESTCASE_6I(desc, name, 231); \ 1657 DO_TESTCASE_6I(desc, name, 312); \ 1658 DO_TESTCASE_6I(desc, name, 321); 1659 1660 #define DO_TESTCASE_6x6RW(desc, name) \ 1661 DO_TESTCASE_6IRW(desc, name, 123); \ 1662 DO_TESTCASE_6IRW(desc, name, 132); \ 1663 DO_TESTCASE_6IRW(desc, name, 213); \ 1664 DO_TESTCASE_6IRW(desc, name, 231); \ 1665 DO_TESTCASE_6IRW(desc, name, 312); \ 1666 DO_TESTCASE_6IRW(desc, name, 321); 1667 1668 static void ww_test_fail_acquire(void) 1669 { 1670 int ret; 1671 1672 WWAI(&t); 1673 t.stamp++; 1674 1675 ret = WWL(&o, &t); 1676 1677 if (WARN_ON(!o.ctx) || 1678 WARN_ON(ret)) 1679 return; 1680 1681 /* No lockdep test, pure API */ 1682 ret = WWL(&o, &t); 1683 WARN_ON(ret != -EALREADY); 1684 1685 ret = WWT(&o); 1686 WARN_ON(ret); 1687 1688 t2 = t; 1689 t2.stamp++; 1690 ret = WWL(&o, &t2); 1691 WARN_ON(ret != -EDEADLK); 1692 WWU(&o); 1693 1694 if (WWT(&o)) 1695 WWU(&o); 1696 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1697 else 1698 DEBUG_LOCKS_WARN_ON(1); 1699 #endif 1700 } 1701 1702 #ifdef CONFIG_PREEMPT_RT 1703 #define ww_mutex_base_lock(b) rt_mutex_lock(b) 1704 #define ww_mutex_base_trylock(b) rt_mutex_trylock(b) 1705 #define ww_mutex_base_lock_nest_lock(b, b2) rt_mutex_lock_nest_lock(b, b2) 1706 #define ww_mutex_base_lock_interruptible(b) rt_mutex_lock_interruptible(b) 1707 #define ww_mutex_base_lock_killable(b) rt_mutex_lock_killable(b) 1708 #define ww_mutex_base_unlock(b) rt_mutex_unlock(b) 1709 #else 1710 #define ww_mutex_base_lock(b) mutex_lock(b) 1711 #define ww_mutex_base_trylock(b) mutex_trylock(b) 1712 #define ww_mutex_base_lock_nest_lock(b, b2) mutex_lock_nest_lock(b, b2) 1713 #define ww_mutex_base_lock_interruptible(b) mutex_lock_interruptible(b) 1714 #define ww_mutex_base_lock_killable(b) mutex_lock_killable(b) 1715 #define ww_mutex_base_unlock(b) mutex_unlock(b) 1716 #endif 1717 1718 static void ww_test_normal(void) 1719 { 1720 int ret; 1721 1722 /* 1723 * None of the ww_mutex codepaths should be taken in the 'normal' 1724 * mutex calls. The easiest way to verify this is by using the 1725 * normal mutex calls, and making sure o.ctx is unmodified. 1726 */ 1727 1728 /* mutex_lock (and indirectly, mutex_lock_nested) */ 1729 o.ctx = (void *)~0UL; 1730 ww_mutex_base_lock(&o.base); 1731 ww_mutex_base_unlock(&o.base); 1732 WARN_ON(o.ctx != (void *)~0UL); 1733 1734 /* mutex_lock_interruptible (and *_nested) */ 1735 o.ctx = (void *)~0UL; 1736 ret = ww_mutex_base_lock_interruptible(&o.base); 1737 if (!ret) 1738 ww_mutex_base_unlock(&o.base); 1739 else 1740 WARN_ON(1); 1741 WARN_ON(o.ctx != (void *)~0UL); 1742 1743 /* mutex_lock_killable (and *_nested) */ 1744 o.ctx = (void *)~0UL; 1745 ret = ww_mutex_base_lock_killable(&o.base); 1746 if (!ret) 1747 ww_mutex_base_unlock(&o.base); 1748 else 1749 WARN_ON(1); 1750 WARN_ON(o.ctx != (void *)~0UL); 1751 1752 /* trylock, succeeding */ 1753 o.ctx = (void *)~0UL; 1754 ret = ww_mutex_base_trylock(&o.base); 1755 WARN_ON(!ret); 1756 if (ret) 1757 ww_mutex_base_unlock(&o.base); 1758 else 1759 WARN_ON(1); 1760 WARN_ON(o.ctx != (void *)~0UL); 1761 1762 /* trylock, failing */ 1763 o.ctx = (void *)~0UL; 1764 ww_mutex_base_lock(&o.base); 1765 ret = ww_mutex_base_trylock(&o.base); 1766 WARN_ON(ret); 1767 ww_mutex_base_unlock(&o.base); 1768 WARN_ON(o.ctx != (void *)~0UL); 1769 1770 WWAI(&t); 1771 1772 /* nest_lock */ 1773 o.ctx = (void *)~0UL; 1774 ww_mutex_base_lock_nest_lock(&o.base, &t); 1775 ww_mutex_base_unlock(&o.base); 1776 WARN_ON(o.ctx != (void *)~0UL); 1777 } 1778 1779 static void ww_test_two_contexts(void) 1780 { 1781 WWAI(&t); 1782 WWAI(&t2); 1783 } 1784 1785 static void ww_test_diff_class(void) 1786 { 1787 WWAI(&t); 1788 #ifdef DEBUG_WW_MUTEXES 1789 t.ww_class = NULL; 1790 #endif 1791 WWL(&o, &t); 1792 } 1793 1794 static void ww_test_context_done_twice(void) 1795 { 1796 WWAI(&t); 1797 WWAD(&t); 1798 WWAD(&t); 1799 WWAF(&t); 1800 } 1801 1802 static void ww_test_context_unlock_twice(void) 1803 { 1804 WWAI(&t); 1805 WWAD(&t); 1806 WWAF(&t); 1807 WWAF(&t); 1808 } 1809 1810 static void ww_test_context_fini_early(void) 1811 { 1812 WWAI(&t); 1813 WWL(&o, &t); 1814 WWAD(&t); 1815 WWAF(&t); 1816 } 1817 1818 static void ww_test_context_lock_after_done(void) 1819 { 1820 WWAI(&t); 1821 WWAD(&t); 1822 WWL(&o, &t); 1823 } 1824 1825 static void ww_test_object_unlock_twice(void) 1826 { 1827 WWL1(&o); 1828 WWU(&o); 1829 WWU(&o); 1830 } 1831 1832 static void ww_test_object_lock_unbalanced(void) 1833 { 1834 WWAI(&t); 1835 WWL(&o, &t); 1836 t.acquired = 0; 1837 WWU(&o); 1838 WWAF(&t); 1839 } 1840 1841 static void ww_test_object_lock_stale_context(void) 1842 { 1843 WWAI(&t); 1844 o.ctx = &t2; 1845 WWL(&o, &t); 1846 } 1847 1848 static void ww_test_edeadlk_normal(void) 1849 { 1850 int ret; 1851 1852 ww_mutex_base_lock(&o2.base); 1853 o2.ctx = &t2; 1854 mutex_release(&o2.base.dep_map, _THIS_IP_); 1855 1856 WWAI(&t); 1857 t2 = t; 1858 t2.stamp--; 1859 1860 ret = WWL(&o, &t); 1861 WARN_ON(ret); 1862 1863 ret = WWL(&o2, &t); 1864 WARN_ON(ret != -EDEADLK); 1865 1866 o2.ctx = NULL; 1867 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); 1868 ww_mutex_base_unlock(&o2.base); 1869 WWU(&o); 1870 1871 WWL(&o2, &t); 1872 } 1873 1874 static void ww_test_edeadlk_normal_slow(void) 1875 { 1876 int ret; 1877 1878 ww_mutex_base_lock(&o2.base); 1879 mutex_release(&o2.base.dep_map, _THIS_IP_); 1880 o2.ctx = &t2; 1881 1882 WWAI(&t); 1883 t2 = t; 1884 t2.stamp--; 1885 1886 ret = WWL(&o, &t); 1887 WARN_ON(ret); 1888 1889 ret = WWL(&o2, &t); 1890 WARN_ON(ret != -EDEADLK); 1891 1892 o2.ctx = NULL; 1893 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); 1894 ww_mutex_base_unlock(&o2.base); 1895 WWU(&o); 1896 1897 ww_mutex_lock_slow(&o2, &t); 1898 } 1899 1900 static void ww_test_edeadlk_no_unlock(void) 1901 { 1902 int ret; 1903 1904 ww_mutex_base_lock(&o2.base); 1905 o2.ctx = &t2; 1906 mutex_release(&o2.base.dep_map, _THIS_IP_); 1907 1908 WWAI(&t); 1909 t2 = t; 1910 t2.stamp--; 1911 1912 ret = WWL(&o, &t); 1913 WARN_ON(ret); 1914 1915 ret = WWL(&o2, &t); 1916 WARN_ON(ret != -EDEADLK); 1917 1918 o2.ctx = NULL; 1919 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); 1920 ww_mutex_base_unlock(&o2.base); 1921 1922 WWL(&o2, &t); 1923 } 1924 1925 static void ww_test_edeadlk_no_unlock_slow(void) 1926 { 1927 int ret; 1928 1929 ww_mutex_base_lock(&o2.base); 1930 mutex_release(&o2.base.dep_map, _THIS_IP_); 1931 o2.ctx = &t2; 1932 1933 WWAI(&t); 1934 t2 = t; 1935 t2.stamp--; 1936 1937 ret = WWL(&o, &t); 1938 WARN_ON(ret); 1939 1940 ret = WWL(&o2, &t); 1941 WARN_ON(ret != -EDEADLK); 1942 1943 o2.ctx = NULL; 1944 mutex_acquire(&o2.base.dep_map, 0, 1, _THIS_IP_); 1945 ww_mutex_base_unlock(&o2.base); 1946 1947 ww_mutex_lock_slow(&o2, &t); 1948 } 1949 1950 static void ww_test_edeadlk_acquire_more(void) 1951 { 1952 int ret; 1953 1954 ww_mutex_base_lock(&o2.base); 1955 mutex_release(&o2.base.dep_map, _THIS_IP_); 1956 o2.ctx = &t2; 1957 1958 WWAI(&t); 1959 t2 = t; 1960 t2.stamp--; 1961 1962 ret = WWL(&o, &t); 1963 WARN_ON(ret); 1964 1965 ret = WWL(&o2, &t); 1966 WARN_ON(ret != -EDEADLK); 1967 1968 ret = WWL(&o3, &t); 1969 } 1970 1971 static void ww_test_edeadlk_acquire_more_slow(void) 1972 { 1973 int ret; 1974 1975 ww_mutex_base_lock(&o2.base); 1976 mutex_release(&o2.base.dep_map, _THIS_IP_); 1977 o2.ctx = &t2; 1978 1979 WWAI(&t); 1980 t2 = t; 1981 t2.stamp--; 1982 1983 ret = WWL(&o, &t); 1984 WARN_ON(ret); 1985 1986 ret = WWL(&o2, &t); 1987 WARN_ON(ret != -EDEADLK); 1988 1989 ww_mutex_lock_slow(&o3, &t); 1990 } 1991 1992 static void ww_test_edeadlk_acquire_more_edeadlk(void) 1993 { 1994 int ret; 1995 1996 ww_mutex_base_lock(&o2.base); 1997 mutex_release(&o2.base.dep_map, _THIS_IP_); 1998 o2.ctx = &t2; 1999 2000 ww_mutex_base_lock(&o3.base); 2001 mutex_release(&o3.base.dep_map, _THIS_IP_); 2002 o3.ctx = &t2; 2003 2004 WWAI(&t); 2005 t2 = t; 2006 t2.stamp--; 2007 2008 ret = WWL(&o, &t); 2009 WARN_ON(ret); 2010 2011 ret = WWL(&o2, &t); 2012 WARN_ON(ret != -EDEADLK); 2013 2014 ret = WWL(&o3, &t); 2015 WARN_ON(ret != -EDEADLK); 2016 } 2017 2018 static void ww_test_edeadlk_acquire_more_edeadlk_slow(void) 2019 { 2020 int ret; 2021 2022 ww_mutex_base_lock(&o2.base); 2023 mutex_release(&o2.base.dep_map, _THIS_IP_); 2024 o2.ctx = &t2; 2025 2026 ww_mutex_base_lock(&o3.base); 2027 mutex_release(&o3.base.dep_map, _THIS_IP_); 2028 o3.ctx = &t2; 2029 2030 WWAI(&t); 2031 t2 = t; 2032 t2.stamp--; 2033 2034 ret = WWL(&o, &t); 2035 WARN_ON(ret); 2036 2037 ret = WWL(&o2, &t); 2038 WARN_ON(ret != -EDEADLK); 2039 2040 ww_mutex_lock_slow(&o3, &t); 2041 } 2042 2043 static void ww_test_edeadlk_acquire_wrong(void) 2044 { 2045 int ret; 2046 2047 ww_mutex_base_lock(&o2.base); 2048 mutex_release(&o2.base.dep_map, _THIS_IP_); 2049 o2.ctx = &t2; 2050 2051 WWAI(&t); 2052 t2 = t; 2053 t2.stamp--; 2054 2055 ret = WWL(&o, &t); 2056 WARN_ON(ret); 2057 2058 ret = WWL(&o2, &t); 2059 WARN_ON(ret != -EDEADLK); 2060 if (!ret) 2061 WWU(&o2); 2062 2063 WWU(&o); 2064 2065 ret = WWL(&o3, &t); 2066 } 2067 2068 static void ww_test_edeadlk_acquire_wrong_slow(void) 2069 { 2070 int ret; 2071 2072 ww_mutex_base_lock(&o2.base); 2073 mutex_release(&o2.base.dep_map, _THIS_IP_); 2074 o2.ctx = &t2; 2075 2076 WWAI(&t); 2077 t2 = t; 2078 t2.stamp--; 2079 2080 ret = WWL(&o, &t); 2081 WARN_ON(ret); 2082 2083 ret = WWL(&o2, &t); 2084 WARN_ON(ret != -EDEADLK); 2085 if (!ret) 2086 WWU(&o2); 2087 2088 WWU(&o); 2089 2090 ww_mutex_lock_slow(&o3, &t); 2091 } 2092 2093 static void ww_test_spin_nest_unlocked(void) 2094 { 2095 spin_lock_nest_lock(&lock_A, &o.base); 2096 U(A); 2097 } 2098 2099 /* This is not a deadlock, because we have X1 to serialize Y1 and Y2 */ 2100 static void ww_test_spin_nest_lock(void) 2101 { 2102 spin_lock(&lock_X1); 2103 spin_lock_nest_lock(&lock_Y1, &lock_X1); 2104 spin_lock(&lock_A); 2105 spin_lock_nest_lock(&lock_Y2, &lock_X1); 2106 spin_unlock(&lock_A); 2107 spin_unlock(&lock_Y2); 2108 spin_unlock(&lock_Y1); 2109 spin_unlock(&lock_X1); 2110 } 2111 2112 static void ww_test_unneeded_slow(void) 2113 { 2114 WWAI(&t); 2115 2116 ww_mutex_lock_slow(&o, &t); 2117 } 2118 2119 static void ww_test_context_block(void) 2120 { 2121 int ret; 2122 2123 WWAI(&t); 2124 2125 ret = WWL(&o, &t); 2126 WARN_ON(ret); 2127 WWL1(&o2); 2128 } 2129 2130 static void ww_test_context_try(void) 2131 { 2132 int ret; 2133 2134 WWAI(&t); 2135 2136 ret = WWL(&o, &t); 2137 WARN_ON(ret); 2138 2139 ret = WWT(&o2); 2140 WARN_ON(!ret); 2141 WWU(&o2); 2142 WWU(&o); 2143 } 2144 2145 static void ww_test_context_context(void) 2146 { 2147 int ret; 2148 2149 WWAI(&t); 2150 2151 ret = WWL(&o, &t); 2152 WARN_ON(ret); 2153 2154 ret = WWL(&o2, &t); 2155 WARN_ON(ret); 2156 2157 WWU(&o2); 2158 WWU(&o); 2159 } 2160 2161 static void ww_test_try_block(void) 2162 { 2163 bool ret; 2164 2165 ret = WWT(&o); 2166 WARN_ON(!ret); 2167 2168 WWL1(&o2); 2169 WWU(&o2); 2170 WWU(&o); 2171 } 2172 2173 static void ww_test_try_try(void) 2174 { 2175 bool ret; 2176 2177 ret = WWT(&o); 2178 WARN_ON(!ret); 2179 ret = WWT(&o2); 2180 WARN_ON(!ret); 2181 WWU(&o2); 2182 WWU(&o); 2183 } 2184 2185 static void ww_test_try_context(void) 2186 { 2187 int ret; 2188 2189 ret = WWT(&o); 2190 WARN_ON(!ret); 2191 2192 WWAI(&t); 2193 2194 ret = WWL(&o2, &t); 2195 WARN_ON(ret); 2196 } 2197 2198 static void ww_test_block_block(void) 2199 { 2200 WWL1(&o); 2201 WWL1(&o2); 2202 } 2203 2204 static void ww_test_block_try(void) 2205 { 2206 bool ret; 2207 2208 WWL1(&o); 2209 ret = WWT(&o2); 2210 WARN_ON(!ret); 2211 } 2212 2213 static void ww_test_block_context(void) 2214 { 2215 int ret; 2216 2217 WWL1(&o); 2218 WWAI(&t); 2219 2220 ret = WWL(&o2, &t); 2221 WARN_ON(ret); 2222 } 2223 2224 static void ww_test_spin_block(void) 2225 { 2226 L(A); 2227 U(A); 2228 2229 WWL1(&o); 2230 L(A); 2231 U(A); 2232 WWU(&o); 2233 2234 L(A); 2235 WWL1(&o); 2236 WWU(&o); 2237 U(A); 2238 } 2239 2240 static void ww_test_spin_try(void) 2241 { 2242 bool ret; 2243 2244 L(A); 2245 U(A); 2246 2247 ret = WWT(&o); 2248 WARN_ON(!ret); 2249 L(A); 2250 U(A); 2251 WWU(&o); 2252 2253 L(A); 2254 ret = WWT(&o); 2255 WARN_ON(!ret); 2256 WWU(&o); 2257 U(A); 2258 } 2259 2260 static void ww_test_spin_context(void) 2261 { 2262 int ret; 2263 2264 L(A); 2265 U(A); 2266 2267 WWAI(&t); 2268 2269 ret = WWL(&o, &t); 2270 WARN_ON(ret); 2271 L(A); 2272 U(A); 2273 WWU(&o); 2274 2275 L(A); 2276 ret = WWL(&o, &t); 2277 WARN_ON(ret); 2278 WWU(&o); 2279 U(A); 2280 } 2281 2282 static void ww_tests(void) 2283 { 2284 printk(" --------------------------------------------------------------------------\n"); 2285 printk(" | Wound/wait tests |\n"); 2286 printk(" ---------------------\n"); 2287 2288 print_testname("ww api failures"); 2289 dotest(ww_test_fail_acquire, SUCCESS, LOCKTYPE_WW); 2290 dotest(ww_test_normal, SUCCESS, LOCKTYPE_WW); 2291 dotest(ww_test_unneeded_slow, FAILURE, LOCKTYPE_WW); 2292 pr_cont("\n"); 2293 2294 print_testname("ww contexts mixing"); 2295 dotest(ww_test_two_contexts, FAILURE, LOCKTYPE_WW); 2296 dotest(ww_test_diff_class, FAILURE, LOCKTYPE_WW); 2297 pr_cont("\n"); 2298 2299 print_testname("finishing ww context"); 2300 dotest(ww_test_context_done_twice, FAILURE, LOCKTYPE_WW); 2301 dotest(ww_test_context_unlock_twice, FAILURE, LOCKTYPE_WW); 2302 dotest(ww_test_context_fini_early, FAILURE, LOCKTYPE_WW); 2303 dotest(ww_test_context_lock_after_done, FAILURE, LOCKTYPE_WW); 2304 pr_cont("\n"); 2305 2306 print_testname("locking mismatches"); 2307 dotest(ww_test_object_unlock_twice, FAILURE, LOCKTYPE_WW); 2308 dotest(ww_test_object_lock_unbalanced, FAILURE, LOCKTYPE_WW); 2309 dotest(ww_test_object_lock_stale_context, FAILURE, LOCKTYPE_WW); 2310 pr_cont("\n"); 2311 2312 print_testname("EDEADLK handling"); 2313 dotest(ww_test_edeadlk_normal, SUCCESS, LOCKTYPE_WW); 2314 dotest(ww_test_edeadlk_normal_slow, SUCCESS, LOCKTYPE_WW); 2315 dotest(ww_test_edeadlk_no_unlock, FAILURE, LOCKTYPE_WW); 2316 dotest(ww_test_edeadlk_no_unlock_slow, FAILURE, LOCKTYPE_WW); 2317 dotest(ww_test_edeadlk_acquire_more, FAILURE, LOCKTYPE_WW); 2318 dotest(ww_test_edeadlk_acquire_more_slow, FAILURE, LOCKTYPE_WW); 2319 dotest(ww_test_edeadlk_acquire_more_edeadlk, FAILURE, LOCKTYPE_WW); 2320 dotest(ww_test_edeadlk_acquire_more_edeadlk_slow, FAILURE, LOCKTYPE_WW); 2321 dotest(ww_test_edeadlk_acquire_wrong, FAILURE, LOCKTYPE_WW); 2322 dotest(ww_test_edeadlk_acquire_wrong_slow, FAILURE, LOCKTYPE_WW); 2323 pr_cont("\n"); 2324 2325 print_testname("spinlock nest unlocked"); 2326 dotest(ww_test_spin_nest_unlocked, FAILURE, LOCKTYPE_WW); 2327 pr_cont("\n"); 2328 2329 print_testname("spinlock nest test"); 2330 dotest(ww_test_spin_nest_lock, SUCCESS, LOCKTYPE_WW); 2331 pr_cont("\n"); 2332 2333 printk(" -----------------------------------------------------\n"); 2334 printk(" |block | try |context|\n"); 2335 printk(" -----------------------------------------------------\n"); 2336 2337 print_testname("context"); 2338 dotest(ww_test_context_block, FAILURE, LOCKTYPE_WW); 2339 dotest(ww_test_context_try, SUCCESS, LOCKTYPE_WW); 2340 dotest(ww_test_context_context, SUCCESS, LOCKTYPE_WW); 2341 pr_cont("\n"); 2342 2343 print_testname("try"); 2344 dotest(ww_test_try_block, FAILURE, LOCKTYPE_WW); 2345 dotest(ww_test_try_try, SUCCESS, LOCKTYPE_WW); 2346 dotest(ww_test_try_context, FAILURE, LOCKTYPE_WW); 2347 pr_cont("\n"); 2348 2349 print_testname("block"); 2350 dotest(ww_test_block_block, FAILURE, LOCKTYPE_WW); 2351 dotest(ww_test_block_try, SUCCESS, LOCKTYPE_WW); 2352 dotest(ww_test_block_context, FAILURE, LOCKTYPE_WW); 2353 pr_cont("\n"); 2354 2355 print_testname("spinlock"); 2356 dotest(ww_test_spin_block, FAILURE, LOCKTYPE_WW); 2357 dotest(ww_test_spin_try, SUCCESS, LOCKTYPE_WW); 2358 dotest(ww_test_spin_context, FAILURE, LOCKTYPE_WW); 2359 pr_cont("\n"); 2360 } 2361 2362 2363 /* 2364 * <in hardirq handler> 2365 * read_lock(&A); 2366 * <hardirq disable> 2367 * spin_lock(&B); 2368 * spin_lock(&B); 2369 * read_lock(&A); 2370 * 2371 * is a deadlock. 2372 */ 2373 static void queued_read_lock_hardirq_RE_Er(void) 2374 { 2375 HARDIRQ_ENTER(); 2376 read_lock(&rwlock_A); 2377 LOCK(B); 2378 UNLOCK(B); 2379 read_unlock(&rwlock_A); 2380 HARDIRQ_EXIT(); 2381 2382 HARDIRQ_DISABLE(); 2383 LOCK(B); 2384 read_lock(&rwlock_A); 2385 read_unlock(&rwlock_A); 2386 UNLOCK(B); 2387 HARDIRQ_ENABLE(); 2388 } 2389 2390 /* 2391 * <in hardirq handler> 2392 * spin_lock(&B); 2393 * <hardirq disable> 2394 * read_lock(&A); 2395 * read_lock(&A); 2396 * spin_lock(&B); 2397 * 2398 * is not a deadlock. 2399 */ 2400 static void queued_read_lock_hardirq_ER_rE(void) 2401 { 2402 HARDIRQ_ENTER(); 2403 LOCK(B); 2404 read_lock(&rwlock_A); 2405 read_unlock(&rwlock_A); 2406 UNLOCK(B); 2407 HARDIRQ_EXIT(); 2408 2409 HARDIRQ_DISABLE(); 2410 read_lock(&rwlock_A); 2411 LOCK(B); 2412 UNLOCK(B); 2413 read_unlock(&rwlock_A); 2414 HARDIRQ_ENABLE(); 2415 } 2416 2417 /* 2418 * <hardirq disable> 2419 * spin_lock(&B); 2420 * read_lock(&A); 2421 * <in hardirq handler> 2422 * spin_lock(&B); 2423 * read_lock(&A); 2424 * 2425 * is a deadlock. Because the two read_lock()s are both non-recursive readers. 2426 */ 2427 static void queued_read_lock_hardirq_inversion(void) 2428 { 2429 2430 HARDIRQ_ENTER(); 2431 LOCK(B); 2432 UNLOCK(B); 2433 HARDIRQ_EXIT(); 2434 2435 HARDIRQ_DISABLE(); 2436 LOCK(B); 2437 read_lock(&rwlock_A); 2438 read_unlock(&rwlock_A); 2439 UNLOCK(B); 2440 HARDIRQ_ENABLE(); 2441 2442 read_lock(&rwlock_A); 2443 read_unlock(&rwlock_A); 2444 } 2445 2446 static void queued_read_lock_tests(void) 2447 { 2448 printk(" --------------------------------------------------------------------------\n"); 2449 printk(" | queued read lock tests |\n"); 2450 printk(" ---------------------------\n"); 2451 print_testname("hardirq read-lock/lock-read"); 2452 dotest(queued_read_lock_hardirq_RE_Er, FAILURE, LOCKTYPE_RWLOCK); 2453 pr_cont("\n"); 2454 2455 print_testname("hardirq lock-read/read-lock"); 2456 dotest(queued_read_lock_hardirq_ER_rE, SUCCESS, LOCKTYPE_RWLOCK); 2457 pr_cont("\n"); 2458 2459 print_testname("hardirq inversion"); 2460 dotest(queued_read_lock_hardirq_inversion, FAILURE, LOCKTYPE_RWLOCK); 2461 pr_cont("\n"); 2462 } 2463 2464 static void fs_reclaim_correct_nesting(void) 2465 { 2466 fs_reclaim_acquire(GFP_KERNEL); 2467 might_alloc(GFP_NOFS); 2468 fs_reclaim_release(GFP_KERNEL); 2469 } 2470 2471 static void fs_reclaim_wrong_nesting(void) 2472 { 2473 fs_reclaim_acquire(GFP_KERNEL); 2474 might_alloc(GFP_KERNEL); 2475 fs_reclaim_release(GFP_KERNEL); 2476 } 2477 2478 static void fs_reclaim_protected_nesting(void) 2479 { 2480 unsigned int flags; 2481 2482 fs_reclaim_acquire(GFP_KERNEL); 2483 flags = memalloc_nofs_save(); 2484 might_alloc(GFP_KERNEL); 2485 memalloc_nofs_restore(flags); 2486 fs_reclaim_release(GFP_KERNEL); 2487 } 2488 2489 static void fs_reclaim_tests(void) 2490 { 2491 printk(" --------------------\n"); 2492 printk(" | fs_reclaim tests |\n"); 2493 printk(" --------------------\n"); 2494 2495 print_testname("correct nesting"); 2496 dotest(fs_reclaim_correct_nesting, SUCCESS, 0); 2497 pr_cont("\n"); 2498 2499 print_testname("wrong nesting"); 2500 dotest(fs_reclaim_wrong_nesting, FAILURE, 0); 2501 pr_cont("\n"); 2502 2503 print_testname("protected nesting"); 2504 dotest(fs_reclaim_protected_nesting, SUCCESS, 0); 2505 pr_cont("\n"); 2506 } 2507 2508 /* Defines guard classes to create contexts */ 2509 DEFINE_LOCK_GUARD_0(HARDIRQ, HARDIRQ_ENTER(), HARDIRQ_EXIT()) 2510 DEFINE_LOCK_GUARD_0(NOTTHREADED_HARDIRQ, 2511 do { 2512 local_irq_disable(); 2513 __irq_enter(); 2514 WARN_ON(!in_hardirq()); 2515 } while(0), HARDIRQ_EXIT()) 2516 DEFINE_LOCK_GUARD_0(SOFTIRQ, SOFTIRQ_ENTER(), SOFTIRQ_EXIT()) 2517 2518 /* Define RCU guards, should go away when RCU has its own guard definitions */ 2519 DEFINE_LOCK_GUARD_0(RCU, rcu_read_lock(), rcu_read_unlock()) 2520 DEFINE_LOCK_GUARD_0(RCU_BH, rcu_read_lock_bh(), rcu_read_unlock_bh()) 2521 DEFINE_LOCK_GUARD_0(RCU_SCHED, rcu_read_lock_sched(), rcu_read_unlock_sched()) 2522 2523 2524 #define GENERATE_2_CONTEXT_TESTCASE(outer, outer_lock, inner, inner_lock) \ 2525 \ 2526 static void __maybe_unused inner##_in_##outer(void) \ 2527 { \ 2528 /* Relies the reversed clean-up ordering: inner first */ \ 2529 guard(outer)(outer_lock); \ 2530 guard(inner)(inner_lock); \ 2531 } 2532 2533 /* 2534 * wait contexts (considering PREEMPT_RT) 2535 * 2536 * o: inner is allowed in outer 2537 * x: inner is disallowed in outer 2538 * 2539 * \ inner | RCU | RAW_SPIN | SPIN | MUTEX 2540 * outer \ | | | | 2541 * ---------------+-------+----------+------+------- 2542 * HARDIRQ | o | o | o | x 2543 * ---------------+-------+----------+------+------- 2544 * NOTTHREADED_IRQ| o | o | x | x 2545 * ---------------+-------+----------+------+------- 2546 * SOFTIRQ | o | o | o | x 2547 * ---------------+-------+----------+------+------- 2548 * RCU | o | o | o | x 2549 * ---------------+-------+----------+------+------- 2550 * RCU_BH | o | o | o | x 2551 * ---------------+-------+----------+------+------- 2552 * RCU_SCHED | o | o | x | x 2553 * ---------------+-------+----------+------+------- 2554 * RAW_SPIN | o | o | x | x 2555 * ---------------+-------+----------+------+------- 2556 * SPIN | o | o | o | x 2557 * ---------------+-------+----------+------+------- 2558 * MUTEX | o | o | o | o 2559 * ---------------+-------+----------+------+------- 2560 */ 2561 2562 #define GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(inner, inner_lock) \ 2563 GENERATE_2_CONTEXT_TESTCASE(HARDIRQ, , inner, inner_lock) \ 2564 GENERATE_2_CONTEXT_TESTCASE(NOTTHREADED_HARDIRQ, , inner, inner_lock) \ 2565 GENERATE_2_CONTEXT_TESTCASE(SOFTIRQ, , inner, inner_lock) \ 2566 GENERATE_2_CONTEXT_TESTCASE(RCU, , inner, inner_lock) \ 2567 GENERATE_2_CONTEXT_TESTCASE(RCU_BH, , inner, inner_lock) \ 2568 GENERATE_2_CONTEXT_TESTCASE(RCU_SCHED, , inner, inner_lock) \ 2569 GENERATE_2_CONTEXT_TESTCASE(raw_spinlock, &raw_lock_A, inner, inner_lock) \ 2570 GENERATE_2_CONTEXT_TESTCASE(spinlock, &lock_A, inner, inner_lock) \ 2571 GENERATE_2_CONTEXT_TESTCASE(mutex, &mutex_A, inner, inner_lock) 2572 2573 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(RCU, ) 2574 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(raw_spinlock, &raw_lock_B) 2575 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(spinlock, &lock_B) 2576 GENERATE_2_CONTEXT_TESTCASE_FOR_ALL_OUTER(mutex, &mutex_B) 2577 2578 /* the outer context allows all kinds of preemption */ 2579 #define DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(outer) \ 2580 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \ 2581 dotest(raw_spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \ 2582 dotest(spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \ 2583 dotest(mutex_in_##outer, SUCCESS, LOCKTYPE_MUTEX); \ 2584 2585 /* 2586 * the outer context only allows the preemption introduced by spinlock_t (which 2587 * is a sleepable lock for PREEMPT_RT) 2588 */ 2589 #define DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(outer) \ 2590 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \ 2591 dotest(raw_spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \ 2592 dotest(spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \ 2593 dotest(mutex_in_##outer, FAILURE, LOCKTYPE_MUTEX); \ 2594 2595 /* the outer doesn't allows any kind of preemption */ 2596 #define DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(outer) \ 2597 dotest(RCU_in_##outer, SUCCESS, LOCKTYPE_RWLOCK); \ 2598 dotest(raw_spinlock_in_##outer, SUCCESS, LOCKTYPE_SPIN); \ 2599 dotest(spinlock_in_##outer, FAILURE, LOCKTYPE_SPIN); \ 2600 dotest(mutex_in_##outer, FAILURE, LOCKTYPE_MUTEX); \ 2601 2602 static void wait_context_tests(void) 2603 { 2604 printk(" --------------------------------------------------------------------------\n"); 2605 printk(" | wait context tests |\n"); 2606 printk(" --------------------------------------------------------------------------\n"); 2607 printk(" | rcu | raw | spin |mutex |\n"); 2608 printk(" --------------------------------------------------------------------------\n"); 2609 print_testname("in hardirq context"); 2610 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(HARDIRQ); 2611 pr_cont("\n"); 2612 2613 print_testname("in hardirq context (not threaded)"); 2614 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(NOTTHREADED_HARDIRQ); 2615 pr_cont("\n"); 2616 2617 print_testname("in softirq context"); 2618 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(SOFTIRQ); 2619 pr_cont("\n"); 2620 2621 print_testname("in RCU context"); 2622 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU); 2623 pr_cont("\n"); 2624 2625 print_testname("in RCU-bh context"); 2626 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(RCU_BH); 2627 pr_cont("\n"); 2628 2629 print_testname("in RCU-sched context"); 2630 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(RCU_SCHED); 2631 pr_cont("\n"); 2632 2633 print_testname("in RAW_SPINLOCK context"); 2634 DO_CONTEXT_TESTCASE_OUTER_NOT_PREEMPTIBLE(raw_spinlock); 2635 pr_cont("\n"); 2636 2637 print_testname("in SPINLOCK context"); 2638 DO_CONTEXT_TESTCASE_OUTER_LIMITED_PREEMPTIBLE(spinlock); 2639 pr_cont("\n"); 2640 2641 print_testname("in MUTEX context"); 2642 DO_CONTEXT_TESTCASE_OUTER_PREEMPTIBLE(mutex); 2643 pr_cont("\n"); 2644 } 2645 2646 static void local_lock_2(void) 2647 { 2648 local_lock(&local_A); /* IRQ-ON */ 2649 local_unlock(&local_A); 2650 2651 HARDIRQ_ENTER(); 2652 spin_lock(&lock_A); /* IN-IRQ */ 2653 spin_unlock(&lock_A); 2654 HARDIRQ_EXIT() 2655 2656 HARDIRQ_DISABLE(); 2657 spin_lock(&lock_A); 2658 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle, false */ 2659 local_unlock(&local_A); 2660 spin_unlock(&lock_A); 2661 HARDIRQ_ENABLE(); 2662 } 2663 2664 static void local_lock_3A(void) 2665 { 2666 local_lock(&local_A); /* IRQ-ON */ 2667 spin_lock(&lock_B); /* IRQ-ON */ 2668 spin_unlock(&lock_B); 2669 local_unlock(&local_A); 2670 2671 HARDIRQ_ENTER(); 2672 spin_lock(&lock_A); /* IN-IRQ */ 2673 spin_unlock(&lock_A); 2674 HARDIRQ_EXIT() 2675 2676 HARDIRQ_DISABLE(); 2677 spin_lock(&lock_A); 2678 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */ 2679 local_unlock(&local_A); 2680 spin_unlock(&lock_A); 2681 HARDIRQ_ENABLE(); 2682 } 2683 2684 static void local_lock_3B(void) 2685 { 2686 local_lock(&local_A); /* IRQ-ON */ 2687 spin_lock(&lock_B); /* IRQ-ON */ 2688 spin_unlock(&lock_B); 2689 local_unlock(&local_A); 2690 2691 HARDIRQ_ENTER(); 2692 spin_lock(&lock_A); /* IN-IRQ */ 2693 spin_unlock(&lock_A); 2694 HARDIRQ_EXIT() 2695 2696 HARDIRQ_DISABLE(); 2697 spin_lock(&lock_A); 2698 local_lock(&local_A); /* IN-IRQ <-> IRQ-ON cycle only if we count local_lock(), false */ 2699 local_unlock(&local_A); 2700 spin_unlock(&lock_A); 2701 HARDIRQ_ENABLE(); 2702 2703 HARDIRQ_DISABLE(); 2704 spin_lock(&lock_A); 2705 spin_lock(&lock_B); /* IN-IRQ <-> IRQ-ON cycle, true */ 2706 spin_unlock(&lock_B); 2707 spin_unlock(&lock_A); 2708 HARDIRQ_DISABLE(); 2709 2710 } 2711 2712 #ifdef CONFIG_DEBUG_LOCK_ALLOC 2713 static inline const char *rw_semaphore_lockdep_name(struct rw_semaphore *rwsem) 2714 { 2715 return rwsem->dep_map.name; 2716 } 2717 #else 2718 static inline const char *rw_semaphore_lockdep_name(struct rw_semaphore *rwsem) 2719 { 2720 return NULL; 2721 } 2722 #endif 2723 2724 static void test_lockdep_set_subclass_name(void) 2725 { 2726 const char *name_before = rw_semaphore_lockdep_name(&rwsem_X1); 2727 const char *name_after; 2728 2729 lockdep_set_subclass(&rwsem_X1, 1); 2730 name_after = rw_semaphore_lockdep_name(&rwsem_X1); 2731 DEBUG_LOCKS_WARN_ON(name_before != name_after); 2732 } 2733 2734 /* 2735 * lockdep_set_subclass() should reuse the existing lock class name instead 2736 * of creating a new one. 2737 */ 2738 static void lockdep_set_subclass_name_test(void) 2739 { 2740 printk(" --------------------------------------------------------------------------\n"); 2741 printk(" | lockdep_set_subclass() name test|\n"); 2742 printk(" -----------------------------------\n"); 2743 2744 print_testname("compare name before and after"); 2745 dotest(test_lockdep_set_subclass_name, SUCCESS, LOCKTYPE_RWSEM); 2746 pr_cont("\n"); 2747 } 2748 2749 static void local_lock_tests(void) 2750 { 2751 printk(" --------------------------------------------------------------------------\n"); 2752 printk(" | local_lock tests |\n"); 2753 printk(" ---------------------\n"); 2754 2755 print_testname("local_lock inversion 2"); 2756 dotest(local_lock_2, SUCCESS, LOCKTYPE_LL); 2757 pr_cont("\n"); 2758 2759 print_testname("local_lock inversion 3A"); 2760 dotest(local_lock_3A, SUCCESS, LOCKTYPE_LL); 2761 pr_cont("\n"); 2762 2763 print_testname("local_lock inversion 3B"); 2764 dotest(local_lock_3B, FAILURE, LOCKTYPE_LL); 2765 pr_cont("\n"); 2766 } 2767 2768 static void hardirq_deadlock_softirq_not_deadlock(void) 2769 { 2770 /* mutex_A is hardirq-unsafe and softirq-unsafe */ 2771 /* mutex_A -> lock_C */ 2772 mutex_lock(&mutex_A); 2773 HARDIRQ_DISABLE(); 2774 spin_lock(&lock_C); 2775 spin_unlock(&lock_C); 2776 HARDIRQ_ENABLE(); 2777 mutex_unlock(&mutex_A); 2778 2779 /* lock_A is hardirq-safe */ 2780 HARDIRQ_ENTER(); 2781 spin_lock(&lock_A); 2782 spin_unlock(&lock_A); 2783 HARDIRQ_EXIT(); 2784 2785 /* lock_A -> lock_B */ 2786 HARDIRQ_DISABLE(); 2787 spin_lock(&lock_A); 2788 spin_lock(&lock_B); 2789 spin_unlock(&lock_B); 2790 spin_unlock(&lock_A); 2791 HARDIRQ_ENABLE(); 2792 2793 /* lock_B -> lock_C */ 2794 HARDIRQ_DISABLE(); 2795 spin_lock(&lock_B); 2796 spin_lock(&lock_C); 2797 spin_unlock(&lock_C); 2798 spin_unlock(&lock_B); 2799 HARDIRQ_ENABLE(); 2800 2801 /* lock_D is softirq-safe */ 2802 SOFTIRQ_ENTER(); 2803 spin_lock(&lock_D); 2804 spin_unlock(&lock_D); 2805 SOFTIRQ_EXIT(); 2806 2807 /* And lock_D is hardirq-unsafe */ 2808 SOFTIRQ_DISABLE(); 2809 spin_lock(&lock_D); 2810 spin_unlock(&lock_D); 2811 SOFTIRQ_ENABLE(); 2812 2813 /* 2814 * mutex_A -> lock_C -> lock_D is softirq-unsafe -> softirq-safe, not 2815 * deadlock. 2816 * 2817 * lock_A -> lock_B -> lock_C -> lock_D is hardirq-safe -> 2818 * hardirq-unsafe, deadlock. 2819 */ 2820 HARDIRQ_DISABLE(); 2821 spin_lock(&lock_C); 2822 spin_lock(&lock_D); 2823 spin_unlock(&lock_D); 2824 spin_unlock(&lock_C); 2825 HARDIRQ_ENABLE(); 2826 } 2827 2828 void locking_selftest(void) 2829 { 2830 /* 2831 * Got a locking failure before the selftest ran? 2832 */ 2833 if (!debug_locks) { 2834 printk("----------------------------------\n"); 2835 printk("| Locking API testsuite disabled |\n"); 2836 printk("----------------------------------\n"); 2837 return; 2838 } 2839 2840 /* 2841 * treats read_lock() as recursive read locks for testing purpose 2842 */ 2843 force_read_lock_recursive = 1; 2844 2845 /* 2846 * Run the testsuite: 2847 */ 2848 printk("------------------------\n"); 2849 printk("| Locking API testsuite:\n"); 2850 printk("----------------------------------------------------------------------------\n"); 2851 printk(" | spin |wlock |rlock |mutex | wsem | rsem |rtmutex\n"); 2852 printk(" --------------------------------------------------------------------------\n"); 2853 2854 init_shared_classes(); 2855 lockdep_set_selftest_task(current); 2856 2857 DO_TESTCASE_6R("A-A deadlock", AA); 2858 DO_TESTCASE_6R("A-B-B-A deadlock", ABBA); 2859 DO_TESTCASE_6R("A-B-B-C-C-A deadlock", ABBCCA); 2860 DO_TESTCASE_6R("A-B-C-A-B-C deadlock", ABCABC); 2861 DO_TESTCASE_6R("A-B-B-C-C-D-D-A deadlock", ABBCCDDA); 2862 DO_TESTCASE_6R("A-B-C-D-B-D-D-A deadlock", ABCDBDDA); 2863 DO_TESTCASE_6R("A-B-C-D-B-C-D-A deadlock", ABCDBCDA); 2864 DO_TESTCASE_6("double unlock", double_unlock); 2865 DO_TESTCASE_6("initialize held", init_held); 2866 2867 printk(" --------------------------------------------------------------------------\n"); 2868 print_testname("recursive read-lock"); 2869 pr_cont(" |"); 2870 dotest(rlock_AA1, SUCCESS, LOCKTYPE_RWLOCK); 2871 pr_cont(" |"); 2872 dotest(rsem_AA1, FAILURE, LOCKTYPE_RWSEM); 2873 pr_cont("\n"); 2874 2875 print_testname("recursive read-lock #2"); 2876 pr_cont(" |"); 2877 dotest(rlock_AA1B, SUCCESS, LOCKTYPE_RWLOCK); 2878 pr_cont(" |"); 2879 dotest(rsem_AA1B, FAILURE, LOCKTYPE_RWSEM); 2880 pr_cont("\n"); 2881 2882 print_testname("mixed read-write-lock"); 2883 pr_cont(" |"); 2884 dotest(rlock_AA2, FAILURE, LOCKTYPE_RWLOCK); 2885 pr_cont(" |"); 2886 dotest(rsem_AA2, FAILURE, LOCKTYPE_RWSEM); 2887 pr_cont("\n"); 2888 2889 print_testname("mixed write-read-lock"); 2890 pr_cont(" |"); 2891 dotest(rlock_AA3, FAILURE, LOCKTYPE_RWLOCK); 2892 pr_cont(" |"); 2893 dotest(rsem_AA3, FAILURE, LOCKTYPE_RWSEM); 2894 pr_cont("\n"); 2895 2896 print_testname("mixed read-lock/lock-write ABBA"); 2897 pr_cont(" |"); 2898 dotest(rlock_ABBA1, FAILURE, LOCKTYPE_RWLOCK); 2899 pr_cont(" |"); 2900 dotest(rwsem_ABBA1, FAILURE, LOCKTYPE_RWSEM); 2901 2902 print_testname("mixed read-lock/lock-read ABBA"); 2903 pr_cont(" |"); 2904 dotest(rlock_ABBA2, SUCCESS, LOCKTYPE_RWLOCK); 2905 pr_cont(" |"); 2906 dotest(rwsem_ABBA2, FAILURE, LOCKTYPE_RWSEM); 2907 2908 print_testname("mixed write-lock/lock-write ABBA"); 2909 pr_cont(" |"); 2910 dotest(rlock_ABBA3, FAILURE, LOCKTYPE_RWLOCK); 2911 pr_cont(" |"); 2912 dotest(rwsem_ABBA3, FAILURE, LOCKTYPE_RWSEM); 2913 2914 print_testname("chain cached mixed R-L/L-W ABBA"); 2915 pr_cont(" |"); 2916 dotest(rlock_chaincache_ABBA1, FAILURE, LOCKTYPE_RWLOCK); 2917 2918 DO_TESTCASE_6x1RRB("rlock W1R2/W2R3/W3R1", W1R2_W2R3_W3R1); 2919 DO_TESTCASE_6x1RRB("rlock W1W2/R2R3/W3R1", W1W2_R2R3_W3R1); 2920 DO_TESTCASE_6x1RR("rlock W1W2/R2R3/R3W1", W1W2_R2R3_R3W1); 2921 DO_TESTCASE_6x1RR("rlock W1R2/R2R3/W3W1", W1R2_R2R3_W3W1); 2922 2923 printk(" --------------------------------------------------------------------------\n"); 2924 /* 2925 * irq-context testcases: 2926 */ 2927 DO_TESTCASE_2x6("irqs-on + irq-safe-A", irqsafe1); 2928 NON_RT(DO_TESTCASE_2x3("sirq-safe-A => hirqs-on", irqsafe2A)); 2929 DO_TESTCASE_2x6("safe-A + irqs-on", irqsafe2B); 2930 DO_TESTCASE_6x6("safe-A + unsafe-B #1", irqsafe3); 2931 DO_TESTCASE_6x6("safe-A + unsafe-B #2", irqsafe4); 2932 DO_TESTCASE_6x6RW("irq lock-inversion", irq_inversion); 2933 2934 DO_TESTCASE_6x2x2RW("irq read-recursion", irq_read_recursion); 2935 DO_TESTCASE_6x2x2RW("irq read-recursion #2", irq_read_recursion2); 2936 DO_TESTCASE_6x2x2RW("irq read-recursion #3", irq_read_recursion3); 2937 2938 ww_tests(); 2939 2940 force_read_lock_recursive = 0; 2941 /* 2942 * queued_read_lock() specific test cases can be put here 2943 */ 2944 if (IS_ENABLED(CONFIG_QUEUED_RWLOCKS)) 2945 queued_read_lock_tests(); 2946 2947 fs_reclaim_tests(); 2948 2949 /* Wait context test cases that are specific for RAW_LOCK_NESTING */ 2950 if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING)) 2951 wait_context_tests(); 2952 2953 local_lock_tests(); 2954 2955 print_testname("hardirq_unsafe_softirq_safe"); 2956 dotest(hardirq_deadlock_softirq_not_deadlock, FAILURE, LOCKTYPE_SPECIAL); 2957 pr_cont("\n"); 2958 2959 lockdep_set_subclass_name_test(); 2960 2961 if (unexpected_testcase_failures) { 2962 printk("-----------------------------------------------------------------\n"); 2963 debug_locks = 0; 2964 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n", 2965 unexpected_testcase_failures, testcase_total); 2966 printk("-----------------------------------------------------------------\n"); 2967 } else if (expected_testcase_failures && testcase_successes) { 2968 printk("--------------------------------------------------------\n"); 2969 printk("%3d out of %3d testcases failed, as expected. |\n", 2970 expected_testcase_failures, testcase_total); 2971 printk("----------------------------------------------------\n"); 2972 debug_locks = 1; 2973 } else if (expected_testcase_failures && !testcase_successes) { 2974 printk("--------------------------------------------------------\n"); 2975 printk("All %3d testcases failed, as expected. |\n", 2976 expected_testcase_failures); 2977 printk("----------------------------------------\n"); 2978 debug_locks = 1; 2979 } else { 2980 printk("-------------------------------------------------------\n"); 2981 printk("Good, all %3d testcases passed! |\n", 2982 testcase_successes); 2983 printk("---------------------------------\n"); 2984 debug_locks = 1; 2985 } 2986 lockdep_set_selftest_task(NULL); 2987 debug_locks_silent = 0; 2988 } 2989