1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kernel/lockdep.c 4 * 5 * Runtime locking correctness validator 6 * 7 * Started by Ingo Molnar: 8 * 9 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 10 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra 11 * 12 * this code maps all the lock dependencies as they occur in a live kernel 13 * and will warn about the following classes of locking bugs: 14 * 15 * - lock inversion scenarios 16 * - circular lock dependencies 17 * - hardirq/softirq safe/unsafe locking bugs 18 * 19 * Bugs are reported even if the current locking scenario does not cause 20 * any deadlock at this point. 21 * 22 * I.e. if anytime in the past two locks were taken in a different order, 23 * even if it happened for another task, even if those were different 24 * locks (but of the same class as this lock), this code will detect it. 25 * 26 * Thanks to Arjan van de Ven for coming up with the initial idea of 27 * mapping lock dependencies runtime. 28 */ 29 #define DISABLE_BRANCH_PROFILING 30 #include <linux/mutex.h> 31 #include <linux/sched.h> 32 #include <linux/sched/clock.h> 33 #include <linux/sched/task.h> 34 #include <linux/sched/mm.h> 35 #include <linux/delay.h> 36 #include <linux/module.h> 37 #include <linux/proc_fs.h> 38 #include <linux/seq_file.h> 39 #include <linux/spinlock.h> 40 #include <linux/kallsyms.h> 41 #include <linux/interrupt.h> 42 #include <linux/stacktrace.h> 43 #include <linux/debug_locks.h> 44 #include <linux/irqflags.h> 45 #include <linux/utsname.h> 46 #include <linux/hash.h> 47 #include <linux/ftrace.h> 48 #include <linux/stringify.h> 49 #include <linux/bitmap.h> 50 #include <linux/bitops.h> 51 #include <linux/gfp.h> 52 #include <linux/random.h> 53 #include <linux/jhash.h> 54 #include <linux/nmi.h> 55 #include <linux/rcupdate.h> 56 #include <linux/kprobes.h> 57 58 #include <asm/sections.h> 59 60 #include "lockdep_internals.h" 61 62 #define CREATE_TRACE_POINTS 63 #include <trace/events/lock.h> 64 65 #ifdef CONFIG_PROVE_LOCKING 66 int prove_locking = 1; 67 module_param(prove_locking, int, 0644); 68 #else 69 #define prove_locking 0 70 #endif 71 72 #ifdef CONFIG_LOCK_STAT 73 int lock_stat = 1; 74 module_param(lock_stat, int, 0644); 75 #else 76 #define lock_stat 0 77 #endif 78 79 DEFINE_PER_CPU(unsigned int, lockdep_recursion); 80 EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion); 81 82 static __always_inline bool lockdep_enabled(void) 83 { 84 if (!debug_locks) 85 return false; 86 87 if (this_cpu_read(lockdep_recursion)) 88 return false; 89 90 if (current->lockdep_recursion) 91 return false; 92 93 return true; 94 } 95 96 /* 97 * lockdep_lock: protects the lockdep graph, the hashes and the 98 * class/list/hash allocators. 99 * 100 * This is one of the rare exceptions where it's justified 101 * to use a raw spinlock - we really dont want the spinlock 102 * code to recurse back into the lockdep code... 103 */ 104 static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 105 static struct task_struct *__owner; 106 107 static inline void lockdep_lock(void) 108 { 109 DEBUG_LOCKS_WARN_ON(!irqs_disabled()); 110 111 __this_cpu_inc(lockdep_recursion); 112 arch_spin_lock(&__lock); 113 __owner = current; 114 } 115 116 static inline void lockdep_unlock(void) 117 { 118 DEBUG_LOCKS_WARN_ON(!irqs_disabled()); 119 120 if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current)) 121 return; 122 123 __owner = NULL; 124 arch_spin_unlock(&__lock); 125 __this_cpu_dec(lockdep_recursion); 126 } 127 128 static inline bool lockdep_assert_locked(void) 129 { 130 return DEBUG_LOCKS_WARN_ON(__owner != current); 131 } 132 133 static struct task_struct *lockdep_selftest_task_struct; 134 135 136 static int graph_lock(void) 137 { 138 lockdep_lock(); 139 /* 140 * Make sure that if another CPU detected a bug while 141 * walking the graph we dont change it (while the other 142 * CPU is busy printing out stuff with the graph lock 143 * dropped already) 144 */ 145 if (!debug_locks) { 146 lockdep_unlock(); 147 return 0; 148 } 149 return 1; 150 } 151 152 static inline void graph_unlock(void) 153 { 154 lockdep_unlock(); 155 } 156 157 /* 158 * Turn lock debugging off and return with 0 if it was off already, 159 * and also release the graph lock: 160 */ 161 static inline int debug_locks_off_graph_unlock(void) 162 { 163 int ret = debug_locks_off(); 164 165 lockdep_unlock(); 166 167 return ret; 168 } 169 170 unsigned long nr_list_entries; 171 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; 172 static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES); 173 174 /* 175 * All data structures here are protected by the global debug_lock. 176 * 177 * nr_lock_classes is the number of elements of lock_classes[] that is 178 * in use. 179 */ 180 #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) 181 #define KEYHASH_SIZE (1UL << KEYHASH_BITS) 182 static struct hlist_head lock_keys_hash[KEYHASH_SIZE]; 183 unsigned long nr_lock_classes; 184 unsigned long nr_zapped_classes; 185 #ifndef CONFIG_DEBUG_LOCKDEP 186 static 187 #endif 188 struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; 189 static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS); 190 191 static inline struct lock_class *hlock_class(struct held_lock *hlock) 192 { 193 unsigned int class_idx = hlock->class_idx; 194 195 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */ 196 barrier(); 197 198 if (!test_bit(class_idx, lock_classes_in_use)) { 199 /* 200 * Someone passed in garbage, we give up. 201 */ 202 DEBUG_LOCKS_WARN_ON(1); 203 return NULL; 204 } 205 206 /* 207 * At this point, if the passed hlock->class_idx is still garbage, 208 * we just have to live with it 209 */ 210 return lock_classes + class_idx; 211 } 212 213 #ifdef CONFIG_LOCK_STAT 214 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats); 215 216 static inline u64 lockstat_clock(void) 217 { 218 return local_clock(); 219 } 220 221 static int lock_point(unsigned long points[], unsigned long ip) 222 { 223 int i; 224 225 for (i = 0; i < LOCKSTAT_POINTS; i++) { 226 if (points[i] == 0) { 227 points[i] = ip; 228 break; 229 } 230 if (points[i] == ip) 231 break; 232 } 233 234 return i; 235 } 236 237 static void lock_time_inc(struct lock_time *lt, u64 time) 238 { 239 if (time > lt->max) 240 lt->max = time; 241 242 if (time < lt->min || !lt->nr) 243 lt->min = time; 244 245 lt->total += time; 246 lt->nr++; 247 } 248 249 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) 250 { 251 if (!src->nr) 252 return; 253 254 if (src->max > dst->max) 255 dst->max = src->max; 256 257 if (src->min < dst->min || !dst->nr) 258 dst->min = src->min; 259 260 dst->total += src->total; 261 dst->nr += src->nr; 262 } 263 264 struct lock_class_stats lock_stats(struct lock_class *class) 265 { 266 struct lock_class_stats stats; 267 int cpu, i; 268 269 memset(&stats, 0, sizeof(struct lock_class_stats)); 270 for_each_possible_cpu(cpu) { 271 struct lock_class_stats *pcs = 272 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; 273 274 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) 275 stats.contention_point[i] += pcs->contention_point[i]; 276 277 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) 278 stats.contending_point[i] += pcs->contending_point[i]; 279 280 lock_time_add(&pcs->read_waittime, &stats.read_waittime); 281 lock_time_add(&pcs->write_waittime, &stats.write_waittime); 282 283 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); 284 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); 285 286 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) 287 stats.bounces[i] += pcs->bounces[i]; 288 } 289 290 return stats; 291 } 292 293 void clear_lock_stats(struct lock_class *class) 294 { 295 int cpu; 296 297 for_each_possible_cpu(cpu) { 298 struct lock_class_stats *cpu_stats = 299 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; 300 301 memset(cpu_stats, 0, sizeof(struct lock_class_stats)); 302 } 303 memset(class->contention_point, 0, sizeof(class->contention_point)); 304 memset(class->contending_point, 0, sizeof(class->contending_point)); 305 } 306 307 static struct lock_class_stats *get_lock_stats(struct lock_class *class) 308 { 309 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes]; 310 } 311 312 static void lock_release_holdtime(struct held_lock *hlock) 313 { 314 struct lock_class_stats *stats; 315 u64 holdtime; 316 317 if (!lock_stat) 318 return; 319 320 holdtime = lockstat_clock() - hlock->holdtime_stamp; 321 322 stats = get_lock_stats(hlock_class(hlock)); 323 if (hlock->read) 324 lock_time_inc(&stats->read_holdtime, holdtime); 325 else 326 lock_time_inc(&stats->write_holdtime, holdtime); 327 } 328 #else 329 static inline void lock_release_holdtime(struct held_lock *hlock) 330 { 331 } 332 #endif 333 334 /* 335 * We keep a global list of all lock classes. The list is only accessed with 336 * the lockdep spinlock lock held. free_lock_classes is a list with free 337 * elements. These elements are linked together by the lock_entry member in 338 * struct lock_class. 339 */ 340 LIST_HEAD(all_lock_classes); 341 static LIST_HEAD(free_lock_classes); 342 343 /** 344 * struct pending_free - information about data structures about to be freed 345 * @zapped: Head of a list with struct lock_class elements. 346 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements 347 * are about to be freed. 348 */ 349 struct pending_free { 350 struct list_head zapped; 351 DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS); 352 }; 353 354 /** 355 * struct delayed_free - data structures used for delayed freeing 356 * 357 * A data structure for delayed freeing of data structures that may be 358 * accessed by RCU readers at the time these were freed. 359 * 360 * @rcu_head: Used to schedule an RCU callback for freeing data structures. 361 * @index: Index of @pf to which freed data structures are added. 362 * @scheduled: Whether or not an RCU callback has been scheduled. 363 * @pf: Array with information about data structures about to be freed. 364 */ 365 static struct delayed_free { 366 struct rcu_head rcu_head; 367 int index; 368 int scheduled; 369 struct pending_free pf[2]; 370 } delayed_free; 371 372 /* 373 * The lockdep classes are in a hash-table as well, for fast lookup: 374 */ 375 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) 376 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS) 377 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) 378 #define classhashentry(key) (classhash_table + __classhashfn((key))) 379 380 static struct hlist_head classhash_table[CLASSHASH_SIZE]; 381 382 /* 383 * We put the lock dependency chains into a hash-table as well, to cache 384 * their existence: 385 */ 386 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1) 387 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS) 388 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) 389 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) 390 391 static struct hlist_head chainhash_table[CHAINHASH_SIZE]; 392 393 /* 394 * the id of held_lock 395 */ 396 static inline u16 hlock_id(struct held_lock *hlock) 397 { 398 BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16); 399 400 return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS)); 401 } 402 403 static inline unsigned int chain_hlock_class_idx(u16 hlock_id) 404 { 405 return hlock_id & (MAX_LOCKDEP_KEYS - 1); 406 } 407 408 /* 409 * The hash key of the lock dependency chains is a hash itself too: 410 * it's a hash of all locks taken up to that lock, including that lock. 411 * It's a 64-bit hash, because it's important for the keys to be 412 * unique. 413 */ 414 static inline u64 iterate_chain_key(u64 key, u32 idx) 415 { 416 u32 k0 = key, k1 = key >> 32; 417 418 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */ 419 420 return k0 | (u64)k1 << 32; 421 } 422 423 void lockdep_init_task(struct task_struct *task) 424 { 425 task->lockdep_depth = 0; /* no locks held yet */ 426 task->curr_chain_key = INITIAL_CHAIN_KEY; 427 task->lockdep_recursion = 0; 428 } 429 430 static __always_inline void lockdep_recursion_inc(void) 431 { 432 __this_cpu_inc(lockdep_recursion); 433 } 434 435 static __always_inline void lockdep_recursion_finish(void) 436 { 437 if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion))) 438 __this_cpu_write(lockdep_recursion, 0); 439 } 440 441 void lockdep_set_selftest_task(struct task_struct *task) 442 { 443 lockdep_selftest_task_struct = task; 444 } 445 446 /* 447 * Debugging switches: 448 */ 449 450 #define VERBOSE 0 451 #define VERY_VERBOSE 0 452 453 #if VERBOSE 454 # define HARDIRQ_VERBOSE 1 455 # define SOFTIRQ_VERBOSE 1 456 #else 457 # define HARDIRQ_VERBOSE 0 458 # define SOFTIRQ_VERBOSE 0 459 #endif 460 461 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE 462 /* 463 * Quick filtering for interesting events: 464 */ 465 static int class_filter(struct lock_class *class) 466 { 467 #if 0 468 /* Example */ 469 if (class->name_version == 1 && 470 !strcmp(class->name, "lockname")) 471 return 1; 472 if (class->name_version == 1 && 473 !strcmp(class->name, "&struct->lockfield")) 474 return 1; 475 #endif 476 /* Filter everything else. 1 would be to allow everything else */ 477 return 0; 478 } 479 #endif 480 481 static int verbose(struct lock_class *class) 482 { 483 #if VERBOSE 484 return class_filter(class); 485 #endif 486 return 0; 487 } 488 489 static void print_lockdep_off(const char *bug_msg) 490 { 491 printk(KERN_DEBUG "%s\n", bug_msg); 492 printk(KERN_DEBUG "turning off the locking correctness validator.\n"); 493 #ifdef CONFIG_LOCK_STAT 494 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n"); 495 #endif 496 } 497 498 unsigned long nr_stack_trace_entries; 499 500 #ifdef CONFIG_PROVE_LOCKING 501 /** 502 * struct lock_trace - single stack backtrace 503 * @hash_entry: Entry in a stack_trace_hash[] list. 504 * @hash: jhash() of @entries. 505 * @nr_entries: Number of entries in @entries. 506 * @entries: Actual stack backtrace. 507 */ 508 struct lock_trace { 509 struct hlist_node hash_entry; 510 u32 hash; 511 u32 nr_entries; 512 unsigned long entries[] __aligned(sizeof(unsigned long)); 513 }; 514 #define LOCK_TRACE_SIZE_IN_LONGS \ 515 (sizeof(struct lock_trace) / sizeof(unsigned long)) 516 /* 517 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock. 518 */ 519 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; 520 static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE]; 521 522 static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2) 523 { 524 return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries && 525 memcmp(t1->entries, t2->entries, 526 t1->nr_entries * sizeof(t1->entries[0])) == 0; 527 } 528 529 static struct lock_trace *save_trace(void) 530 { 531 struct lock_trace *trace, *t2; 532 struct hlist_head *hash_head; 533 u32 hash; 534 int max_entries; 535 536 BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE); 537 BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES); 538 539 trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries); 540 max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries - 541 LOCK_TRACE_SIZE_IN_LONGS; 542 543 if (max_entries <= 0) { 544 if (!debug_locks_off_graph_unlock()) 545 return NULL; 546 547 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); 548 dump_stack(); 549 550 return NULL; 551 } 552 trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3); 553 554 hash = jhash(trace->entries, trace->nr_entries * 555 sizeof(trace->entries[0]), 0); 556 trace->hash = hash; 557 hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1)); 558 hlist_for_each_entry(t2, hash_head, hash_entry) { 559 if (traces_identical(trace, t2)) 560 return t2; 561 } 562 nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries; 563 hlist_add_head(&trace->hash_entry, hash_head); 564 565 return trace; 566 } 567 568 /* Return the number of stack traces in the stack_trace[] array. */ 569 u64 lockdep_stack_trace_count(void) 570 { 571 struct lock_trace *trace; 572 u64 c = 0; 573 int i; 574 575 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) { 576 hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) { 577 c++; 578 } 579 } 580 581 return c; 582 } 583 584 /* Return the number of stack hash chains that have at least one stack trace. */ 585 u64 lockdep_stack_hash_count(void) 586 { 587 u64 c = 0; 588 int i; 589 590 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) 591 if (!hlist_empty(&stack_trace_hash[i])) 592 c++; 593 594 return c; 595 } 596 #endif 597 598 unsigned int nr_hardirq_chains; 599 unsigned int nr_softirq_chains; 600 unsigned int nr_process_chains; 601 unsigned int max_lockdep_depth; 602 603 #ifdef CONFIG_DEBUG_LOCKDEP 604 /* 605 * Various lockdep statistics: 606 */ 607 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats); 608 #endif 609 610 #ifdef CONFIG_PROVE_LOCKING 611 /* 612 * Locking printouts: 613 */ 614 615 #define __USAGE(__STATE) \ 616 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \ 617 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \ 618 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\ 619 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R", 620 621 static const char *usage_str[] = 622 { 623 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE) 624 #include "lockdep_states.h" 625 #undef LOCKDEP_STATE 626 [LOCK_USED] = "INITIAL USE", 627 [LOCK_USED_READ] = "INITIAL READ USE", 628 /* abused as string storage for verify_lock_unused() */ 629 [LOCK_USAGE_STATES] = "IN-NMI", 630 }; 631 #endif 632 633 const char *__get_key_name(const struct lockdep_subclass_key *key, char *str) 634 { 635 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); 636 } 637 638 static inline unsigned long lock_flag(enum lock_usage_bit bit) 639 { 640 return 1UL << bit; 641 } 642 643 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit) 644 { 645 /* 646 * The usage character defaults to '.' (i.e., irqs disabled and not in 647 * irq context), which is the safest usage category. 648 */ 649 char c = '.'; 650 651 /* 652 * The order of the following usage checks matters, which will 653 * result in the outcome character as follows: 654 * 655 * - '+': irq is enabled and not in irq context 656 * - '-': in irq context and irq is disabled 657 * - '?': in irq context and irq is enabled 658 */ 659 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) { 660 c = '+'; 661 if (class->usage_mask & lock_flag(bit)) 662 c = '?'; 663 } else if (class->usage_mask & lock_flag(bit)) 664 c = '-'; 665 666 return c; 667 } 668 669 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]) 670 { 671 int i = 0; 672 673 #define LOCKDEP_STATE(__STATE) \ 674 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \ 675 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ); 676 #include "lockdep_states.h" 677 #undef LOCKDEP_STATE 678 679 usage[i] = '\0'; 680 } 681 682 static void __print_lock_name(struct lock_class *class) 683 { 684 char str[KSYM_NAME_LEN]; 685 const char *name; 686 687 name = class->name; 688 if (!name) { 689 name = __get_key_name(class->key, str); 690 printk(KERN_CONT "%s", name); 691 } else { 692 printk(KERN_CONT "%s", name); 693 if (class->name_version > 1) 694 printk(KERN_CONT "#%d", class->name_version); 695 if (class->subclass) 696 printk(KERN_CONT "/%d", class->subclass); 697 } 698 } 699 700 static void print_lock_name(struct lock_class *class) 701 { 702 char usage[LOCK_USAGE_CHARS]; 703 704 get_usage_chars(class, usage); 705 706 printk(KERN_CONT " ("); 707 __print_lock_name(class); 708 printk(KERN_CONT "){%s}-{%d:%d}", usage, 709 class->wait_type_outer ?: class->wait_type_inner, 710 class->wait_type_inner); 711 } 712 713 static void print_lockdep_cache(struct lockdep_map *lock) 714 { 715 const char *name; 716 char str[KSYM_NAME_LEN]; 717 718 name = lock->name; 719 if (!name) 720 name = __get_key_name(lock->key->subkeys, str); 721 722 printk(KERN_CONT "%s", name); 723 } 724 725 static void print_lock(struct held_lock *hlock) 726 { 727 /* 728 * We can be called locklessly through debug_show_all_locks() so be 729 * extra careful, the hlock might have been released and cleared. 730 * 731 * If this indeed happens, lets pretend it does not hurt to continue 732 * to print the lock unless the hlock class_idx does not point to a 733 * registered class. The rationale here is: since we don't attempt 734 * to distinguish whether we are in this situation, if it just 735 * happened we can't count on class_idx to tell either. 736 */ 737 struct lock_class *lock = hlock_class(hlock); 738 739 if (!lock) { 740 printk(KERN_CONT "<RELEASED>\n"); 741 return; 742 } 743 744 printk(KERN_CONT "%px", hlock->instance); 745 print_lock_name(lock); 746 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip); 747 } 748 749 static void lockdep_print_held_locks(struct task_struct *p) 750 { 751 int i, depth = READ_ONCE(p->lockdep_depth); 752 753 if (!depth) 754 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p)); 755 else 756 printk("%d lock%s held by %s/%d:\n", depth, 757 depth > 1 ? "s" : "", p->comm, task_pid_nr(p)); 758 /* 759 * It's not reliable to print a task's held locks if it's not sleeping 760 * and it's not the current task. 761 */ 762 if (p->state == TASK_RUNNING && p != current) 763 return; 764 for (i = 0; i < depth; i++) { 765 printk(" #%d: ", i); 766 print_lock(p->held_locks + i); 767 } 768 } 769 770 static void print_kernel_ident(void) 771 { 772 printk("%s %.*s %s\n", init_utsname()->release, 773 (int)strcspn(init_utsname()->version, " "), 774 init_utsname()->version, 775 print_tainted()); 776 } 777 778 static int very_verbose(struct lock_class *class) 779 { 780 #if VERY_VERBOSE 781 return class_filter(class); 782 #endif 783 return 0; 784 } 785 786 /* 787 * Is this the address of a static object: 788 */ 789 #ifdef __KERNEL__ 790 static int static_obj(const void *obj) 791 { 792 unsigned long start = (unsigned long) &_stext, 793 end = (unsigned long) &_end, 794 addr = (unsigned long) obj; 795 796 if (arch_is_kernel_initmem_freed(addr)) 797 return 0; 798 799 /* 800 * static variable? 801 */ 802 if ((addr >= start) && (addr < end)) 803 return 1; 804 805 if (arch_is_kernel_data(addr)) 806 return 1; 807 808 /* 809 * in-kernel percpu var? 810 */ 811 if (is_kernel_percpu_address(addr)) 812 return 1; 813 814 /* 815 * module static or percpu var? 816 */ 817 return is_module_address(addr) || is_module_percpu_address(addr); 818 } 819 #endif 820 821 /* 822 * To make lock name printouts unique, we calculate a unique 823 * class->name_version generation counter. The caller must hold the graph 824 * lock. 825 */ 826 static int count_matching_names(struct lock_class *new_class) 827 { 828 struct lock_class *class; 829 int count = 0; 830 831 if (!new_class->name) 832 return 0; 833 834 list_for_each_entry(class, &all_lock_classes, lock_entry) { 835 if (new_class->key - new_class->subclass == class->key) 836 return class->name_version; 837 if (class->name && !strcmp(class->name, new_class->name)) 838 count = max(count, class->name_version); 839 } 840 841 return count + 1; 842 } 843 844 /* used from NMI context -- must be lockless */ 845 static __always_inline struct lock_class * 846 look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass) 847 { 848 struct lockdep_subclass_key *key; 849 struct hlist_head *hash_head; 850 struct lock_class *class; 851 852 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) { 853 debug_locks_off(); 854 printk(KERN_ERR 855 "BUG: looking up invalid subclass: %u\n", subclass); 856 printk(KERN_ERR 857 "turning off the locking correctness validator.\n"); 858 dump_stack(); 859 return NULL; 860 } 861 862 /* 863 * If it is not initialised then it has never been locked, 864 * so it won't be present in the hash table. 865 */ 866 if (unlikely(!lock->key)) 867 return NULL; 868 869 /* 870 * NOTE: the class-key must be unique. For dynamic locks, a static 871 * lock_class_key variable is passed in through the mutex_init() 872 * (or spin_lock_init()) call - which acts as the key. For static 873 * locks we use the lock object itself as the key. 874 */ 875 BUILD_BUG_ON(sizeof(struct lock_class_key) > 876 sizeof(struct lockdep_map)); 877 878 key = lock->key->subkeys + subclass; 879 880 hash_head = classhashentry(key); 881 882 /* 883 * We do an RCU walk of the hash, see lockdep_free_key_range(). 884 */ 885 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 886 return NULL; 887 888 hlist_for_each_entry_rcu(class, hash_head, hash_entry) { 889 if (class->key == key) { 890 /* 891 * Huh! same key, different name? Did someone trample 892 * on some memory? We're most confused. 893 */ 894 WARN_ON_ONCE(class->name != lock->name && 895 lock->key != &__lockdep_no_validate__); 896 return class; 897 } 898 } 899 900 return NULL; 901 } 902 903 /* 904 * Static locks do not have their class-keys yet - for them the key is 905 * the lock object itself. If the lock is in the per cpu area, the 906 * canonical address of the lock (per cpu offset removed) is used. 907 */ 908 static bool assign_lock_key(struct lockdep_map *lock) 909 { 910 unsigned long can_addr, addr = (unsigned long)lock; 911 912 #ifdef __KERNEL__ 913 /* 914 * lockdep_free_key_range() assumes that struct lock_class_key 915 * objects do not overlap. Since we use the address of lock 916 * objects as class key for static objects, check whether the 917 * size of lock_class_key objects does not exceed the size of 918 * the smallest lock object. 919 */ 920 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t)); 921 #endif 922 923 if (__is_kernel_percpu_address(addr, &can_addr)) 924 lock->key = (void *)can_addr; 925 else if (__is_module_percpu_address(addr, &can_addr)) 926 lock->key = (void *)can_addr; 927 else if (static_obj(lock)) 928 lock->key = (void *)lock; 929 else { 930 /* Debug-check: all keys must be persistent! */ 931 debug_locks_off(); 932 pr_err("INFO: trying to register non-static key.\n"); 933 pr_err("The code is fine but needs lockdep annotation, or maybe\n"); 934 pr_err("you didn't initialize this object before use?\n"); 935 pr_err("turning off the locking correctness validator.\n"); 936 dump_stack(); 937 return false; 938 } 939 940 return true; 941 } 942 943 #ifdef CONFIG_DEBUG_LOCKDEP 944 945 /* Check whether element @e occurs in list @h */ 946 static bool in_list(struct list_head *e, struct list_head *h) 947 { 948 struct list_head *f; 949 950 list_for_each(f, h) { 951 if (e == f) 952 return true; 953 } 954 955 return false; 956 } 957 958 /* 959 * Check whether entry @e occurs in any of the locks_after or locks_before 960 * lists. 961 */ 962 static bool in_any_class_list(struct list_head *e) 963 { 964 struct lock_class *class; 965 int i; 966 967 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { 968 class = &lock_classes[i]; 969 if (in_list(e, &class->locks_after) || 970 in_list(e, &class->locks_before)) 971 return true; 972 } 973 return false; 974 } 975 976 static bool class_lock_list_valid(struct lock_class *c, struct list_head *h) 977 { 978 struct lock_list *e; 979 980 list_for_each_entry(e, h, entry) { 981 if (e->links_to != c) { 982 printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s", 983 c->name ? : "(?)", 984 (unsigned long)(e - list_entries), 985 e->links_to && e->links_to->name ? 986 e->links_to->name : "(?)", 987 e->class && e->class->name ? e->class->name : 988 "(?)"); 989 return false; 990 } 991 } 992 return true; 993 } 994 995 #ifdef CONFIG_PROVE_LOCKING 996 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; 997 #endif 998 999 static bool check_lock_chain_key(struct lock_chain *chain) 1000 { 1001 #ifdef CONFIG_PROVE_LOCKING 1002 u64 chain_key = INITIAL_CHAIN_KEY; 1003 int i; 1004 1005 for (i = chain->base; i < chain->base + chain->depth; i++) 1006 chain_key = iterate_chain_key(chain_key, chain_hlocks[i]); 1007 /* 1008 * The 'unsigned long long' casts avoid that a compiler warning 1009 * is reported when building tools/lib/lockdep. 1010 */ 1011 if (chain->chain_key != chain_key) { 1012 printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n", 1013 (unsigned long long)(chain - lock_chains), 1014 (unsigned long long)chain->chain_key, 1015 (unsigned long long)chain_key); 1016 return false; 1017 } 1018 #endif 1019 return true; 1020 } 1021 1022 static bool in_any_zapped_class_list(struct lock_class *class) 1023 { 1024 struct pending_free *pf; 1025 int i; 1026 1027 for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) { 1028 if (in_list(&class->lock_entry, &pf->zapped)) 1029 return true; 1030 } 1031 1032 return false; 1033 } 1034 1035 static bool __check_data_structures(void) 1036 { 1037 struct lock_class *class; 1038 struct lock_chain *chain; 1039 struct hlist_head *head; 1040 struct lock_list *e; 1041 int i; 1042 1043 /* Check whether all classes occur in a lock list. */ 1044 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { 1045 class = &lock_classes[i]; 1046 if (!in_list(&class->lock_entry, &all_lock_classes) && 1047 !in_list(&class->lock_entry, &free_lock_classes) && 1048 !in_any_zapped_class_list(class)) { 1049 printk(KERN_INFO "class %px/%s is not in any class list\n", 1050 class, class->name ? : "(?)"); 1051 return false; 1052 } 1053 } 1054 1055 /* Check whether all classes have valid lock lists. */ 1056 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { 1057 class = &lock_classes[i]; 1058 if (!class_lock_list_valid(class, &class->locks_before)) 1059 return false; 1060 if (!class_lock_list_valid(class, &class->locks_after)) 1061 return false; 1062 } 1063 1064 /* Check the chain_key of all lock chains. */ 1065 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) { 1066 head = chainhash_table + i; 1067 hlist_for_each_entry_rcu(chain, head, entry) { 1068 if (!check_lock_chain_key(chain)) 1069 return false; 1070 } 1071 } 1072 1073 /* 1074 * Check whether all list entries that are in use occur in a class 1075 * lock list. 1076 */ 1077 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { 1078 e = list_entries + i; 1079 if (!in_any_class_list(&e->entry)) { 1080 printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n", 1081 (unsigned int)(e - list_entries), 1082 e->class->name ? : "(?)", 1083 e->links_to->name ? : "(?)"); 1084 return false; 1085 } 1086 } 1087 1088 /* 1089 * Check whether all list entries that are not in use do not occur in 1090 * a class lock list. 1091 */ 1092 for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { 1093 e = list_entries + i; 1094 if (in_any_class_list(&e->entry)) { 1095 printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n", 1096 (unsigned int)(e - list_entries), 1097 e->class && e->class->name ? e->class->name : 1098 "(?)", 1099 e->links_to && e->links_to->name ? 1100 e->links_to->name : "(?)"); 1101 return false; 1102 } 1103 } 1104 1105 return true; 1106 } 1107 1108 int check_consistency = 0; 1109 module_param(check_consistency, int, 0644); 1110 1111 static void check_data_structures(void) 1112 { 1113 static bool once = false; 1114 1115 if (check_consistency && !once) { 1116 if (!__check_data_structures()) { 1117 once = true; 1118 WARN_ON(once); 1119 } 1120 } 1121 } 1122 1123 #else /* CONFIG_DEBUG_LOCKDEP */ 1124 1125 static inline void check_data_structures(void) { } 1126 1127 #endif /* CONFIG_DEBUG_LOCKDEP */ 1128 1129 static void init_chain_block_buckets(void); 1130 1131 /* 1132 * Initialize the lock_classes[] array elements, the free_lock_classes list 1133 * and also the delayed_free structure. 1134 */ 1135 static void init_data_structures_once(void) 1136 { 1137 static bool __read_mostly ds_initialized, rcu_head_initialized; 1138 int i; 1139 1140 if (likely(rcu_head_initialized)) 1141 return; 1142 1143 if (system_state >= SYSTEM_SCHEDULING) { 1144 init_rcu_head(&delayed_free.rcu_head); 1145 rcu_head_initialized = true; 1146 } 1147 1148 if (ds_initialized) 1149 return; 1150 1151 ds_initialized = true; 1152 1153 INIT_LIST_HEAD(&delayed_free.pf[0].zapped); 1154 INIT_LIST_HEAD(&delayed_free.pf[1].zapped); 1155 1156 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { 1157 list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes); 1158 INIT_LIST_HEAD(&lock_classes[i].locks_after); 1159 INIT_LIST_HEAD(&lock_classes[i].locks_before); 1160 } 1161 init_chain_block_buckets(); 1162 } 1163 1164 static inline struct hlist_head *keyhashentry(const struct lock_class_key *key) 1165 { 1166 unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS); 1167 1168 return lock_keys_hash + hash; 1169 } 1170 1171 /* Register a dynamically allocated key. */ 1172 void lockdep_register_key(struct lock_class_key *key) 1173 { 1174 struct hlist_head *hash_head; 1175 struct lock_class_key *k; 1176 unsigned long flags; 1177 1178 if (WARN_ON_ONCE(static_obj(key))) 1179 return; 1180 hash_head = keyhashentry(key); 1181 1182 raw_local_irq_save(flags); 1183 if (!graph_lock()) 1184 goto restore_irqs; 1185 hlist_for_each_entry_rcu(k, hash_head, hash_entry) { 1186 if (WARN_ON_ONCE(k == key)) 1187 goto out_unlock; 1188 } 1189 hlist_add_head_rcu(&key->hash_entry, hash_head); 1190 out_unlock: 1191 graph_unlock(); 1192 restore_irqs: 1193 raw_local_irq_restore(flags); 1194 } 1195 EXPORT_SYMBOL_GPL(lockdep_register_key); 1196 1197 /* Check whether a key has been registered as a dynamic key. */ 1198 static bool is_dynamic_key(const struct lock_class_key *key) 1199 { 1200 struct hlist_head *hash_head; 1201 struct lock_class_key *k; 1202 bool found = false; 1203 1204 if (WARN_ON_ONCE(static_obj(key))) 1205 return false; 1206 1207 /* 1208 * If lock debugging is disabled lock_keys_hash[] may contain 1209 * pointers to memory that has already been freed. Avoid triggering 1210 * a use-after-free in that case by returning early. 1211 */ 1212 if (!debug_locks) 1213 return true; 1214 1215 hash_head = keyhashentry(key); 1216 1217 rcu_read_lock(); 1218 hlist_for_each_entry_rcu(k, hash_head, hash_entry) { 1219 if (k == key) { 1220 found = true; 1221 break; 1222 } 1223 } 1224 rcu_read_unlock(); 1225 1226 return found; 1227 } 1228 1229 /* 1230 * Register a lock's class in the hash-table, if the class is not present 1231 * yet. Otherwise we look it up. We cache the result in the lock object 1232 * itself, so actual lookup of the hash should be once per lock object. 1233 */ 1234 static struct lock_class * 1235 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) 1236 { 1237 struct lockdep_subclass_key *key; 1238 struct hlist_head *hash_head; 1239 struct lock_class *class; 1240 1241 DEBUG_LOCKS_WARN_ON(!irqs_disabled()); 1242 1243 class = look_up_lock_class(lock, subclass); 1244 if (likely(class)) 1245 goto out_set_class_cache; 1246 1247 if (!lock->key) { 1248 if (!assign_lock_key(lock)) 1249 return NULL; 1250 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) { 1251 return NULL; 1252 } 1253 1254 key = lock->key->subkeys + subclass; 1255 hash_head = classhashentry(key); 1256 1257 if (!graph_lock()) { 1258 return NULL; 1259 } 1260 /* 1261 * We have to do the hash-walk again, to avoid races 1262 * with another CPU: 1263 */ 1264 hlist_for_each_entry_rcu(class, hash_head, hash_entry) { 1265 if (class->key == key) 1266 goto out_unlock_set; 1267 } 1268 1269 init_data_structures_once(); 1270 1271 /* Allocate a new lock class and add it to the hash. */ 1272 class = list_first_entry_or_null(&free_lock_classes, typeof(*class), 1273 lock_entry); 1274 if (!class) { 1275 if (!debug_locks_off_graph_unlock()) { 1276 return NULL; 1277 } 1278 1279 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); 1280 dump_stack(); 1281 return NULL; 1282 } 1283 nr_lock_classes++; 1284 __set_bit(class - lock_classes, lock_classes_in_use); 1285 debug_atomic_inc(nr_unused_locks); 1286 class->key = key; 1287 class->name = lock->name; 1288 class->subclass = subclass; 1289 WARN_ON_ONCE(!list_empty(&class->locks_before)); 1290 WARN_ON_ONCE(!list_empty(&class->locks_after)); 1291 class->name_version = count_matching_names(class); 1292 class->wait_type_inner = lock->wait_type_inner; 1293 class->wait_type_outer = lock->wait_type_outer; 1294 class->lock_type = lock->lock_type; 1295 /* 1296 * We use RCU's safe list-add method to make 1297 * parallel walking of the hash-list safe: 1298 */ 1299 hlist_add_head_rcu(&class->hash_entry, hash_head); 1300 /* 1301 * Remove the class from the free list and add it to the global list 1302 * of classes. 1303 */ 1304 list_move_tail(&class->lock_entry, &all_lock_classes); 1305 1306 if (verbose(class)) { 1307 graph_unlock(); 1308 1309 printk("\nnew class %px: %s", class->key, class->name); 1310 if (class->name_version > 1) 1311 printk(KERN_CONT "#%d", class->name_version); 1312 printk(KERN_CONT "\n"); 1313 dump_stack(); 1314 1315 if (!graph_lock()) { 1316 return NULL; 1317 } 1318 } 1319 out_unlock_set: 1320 graph_unlock(); 1321 1322 out_set_class_cache: 1323 if (!subclass || force) 1324 lock->class_cache[0] = class; 1325 else if (subclass < NR_LOCKDEP_CACHING_CLASSES) 1326 lock->class_cache[subclass] = class; 1327 1328 /* 1329 * Hash collision, did we smoke some? We found a class with a matching 1330 * hash but the subclass -- which is hashed in -- didn't match. 1331 */ 1332 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) 1333 return NULL; 1334 1335 return class; 1336 } 1337 1338 #ifdef CONFIG_PROVE_LOCKING 1339 /* 1340 * Allocate a lockdep entry. (assumes the graph_lock held, returns 1341 * with NULL on failure) 1342 */ 1343 static struct lock_list *alloc_list_entry(void) 1344 { 1345 int idx = find_first_zero_bit(list_entries_in_use, 1346 ARRAY_SIZE(list_entries)); 1347 1348 if (idx >= ARRAY_SIZE(list_entries)) { 1349 if (!debug_locks_off_graph_unlock()) 1350 return NULL; 1351 1352 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); 1353 dump_stack(); 1354 return NULL; 1355 } 1356 nr_list_entries++; 1357 __set_bit(idx, list_entries_in_use); 1358 return list_entries + idx; 1359 } 1360 1361 /* 1362 * Add a new dependency to the head of the list: 1363 */ 1364 static int add_lock_to_list(struct lock_class *this, 1365 struct lock_class *links_to, struct list_head *head, 1366 unsigned long ip, u16 distance, u8 dep, 1367 const struct lock_trace *trace) 1368 { 1369 struct lock_list *entry; 1370 /* 1371 * Lock not present yet - get a new dependency struct and 1372 * add it to the list: 1373 */ 1374 entry = alloc_list_entry(); 1375 if (!entry) 1376 return 0; 1377 1378 entry->class = this; 1379 entry->links_to = links_to; 1380 entry->dep = dep; 1381 entry->distance = distance; 1382 entry->trace = trace; 1383 /* 1384 * Both allocation and removal are done under the graph lock; but 1385 * iteration is under RCU-sched; see look_up_lock_class() and 1386 * lockdep_free_key_range(). 1387 */ 1388 list_add_tail_rcu(&entry->entry, head); 1389 1390 return 1; 1391 } 1392 1393 /* 1394 * For good efficiency of modular, we use power of 2 1395 */ 1396 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL 1397 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1) 1398 1399 /* 1400 * The circular_queue and helpers are used to implement graph 1401 * breadth-first search (BFS) algorithm, by which we can determine 1402 * whether there is a path from a lock to another. In deadlock checks, 1403 * a path from the next lock to be acquired to a previous held lock 1404 * indicates that adding the <prev> -> <next> lock dependency will 1405 * produce a circle in the graph. Breadth-first search instead of 1406 * depth-first search is used in order to find the shortest (circular) 1407 * path. 1408 */ 1409 struct circular_queue { 1410 struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE]; 1411 unsigned int front, rear; 1412 }; 1413 1414 static struct circular_queue lock_cq; 1415 1416 unsigned int max_bfs_queue_depth; 1417 1418 static unsigned int lockdep_dependency_gen_id; 1419 1420 static inline void __cq_init(struct circular_queue *cq) 1421 { 1422 cq->front = cq->rear = 0; 1423 lockdep_dependency_gen_id++; 1424 } 1425 1426 static inline int __cq_empty(struct circular_queue *cq) 1427 { 1428 return (cq->front == cq->rear); 1429 } 1430 1431 static inline int __cq_full(struct circular_queue *cq) 1432 { 1433 return ((cq->rear + 1) & CQ_MASK) == cq->front; 1434 } 1435 1436 static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem) 1437 { 1438 if (__cq_full(cq)) 1439 return -1; 1440 1441 cq->element[cq->rear] = elem; 1442 cq->rear = (cq->rear + 1) & CQ_MASK; 1443 return 0; 1444 } 1445 1446 /* 1447 * Dequeue an element from the circular_queue, return a lock_list if 1448 * the queue is not empty, or NULL if otherwise. 1449 */ 1450 static inline struct lock_list * __cq_dequeue(struct circular_queue *cq) 1451 { 1452 struct lock_list * lock; 1453 1454 if (__cq_empty(cq)) 1455 return NULL; 1456 1457 lock = cq->element[cq->front]; 1458 cq->front = (cq->front + 1) & CQ_MASK; 1459 1460 return lock; 1461 } 1462 1463 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq) 1464 { 1465 return (cq->rear - cq->front) & CQ_MASK; 1466 } 1467 1468 static inline void mark_lock_accessed(struct lock_list *lock) 1469 { 1470 lock->class->dep_gen_id = lockdep_dependency_gen_id; 1471 } 1472 1473 static inline void visit_lock_entry(struct lock_list *lock, 1474 struct lock_list *parent) 1475 { 1476 lock->parent = parent; 1477 } 1478 1479 static inline unsigned long lock_accessed(struct lock_list *lock) 1480 { 1481 return lock->class->dep_gen_id == lockdep_dependency_gen_id; 1482 } 1483 1484 static inline struct lock_list *get_lock_parent(struct lock_list *child) 1485 { 1486 return child->parent; 1487 } 1488 1489 static inline int get_lock_depth(struct lock_list *child) 1490 { 1491 int depth = 0; 1492 struct lock_list *parent; 1493 1494 while ((parent = get_lock_parent(child))) { 1495 child = parent; 1496 depth++; 1497 } 1498 return depth; 1499 } 1500 1501 /* 1502 * Return the forward or backward dependency list. 1503 * 1504 * @lock: the lock_list to get its class's dependency list 1505 * @offset: the offset to struct lock_class to determine whether it is 1506 * locks_after or locks_before 1507 */ 1508 static inline struct list_head *get_dep_list(struct lock_list *lock, int offset) 1509 { 1510 void *lock_class = lock->class; 1511 1512 return lock_class + offset; 1513 } 1514 /* 1515 * Return values of a bfs search: 1516 * 1517 * BFS_E* indicates an error 1518 * BFS_R* indicates a result (match or not) 1519 * 1520 * BFS_EINVALIDNODE: Find a invalid node in the graph. 1521 * 1522 * BFS_EQUEUEFULL: The queue is full while doing the bfs. 1523 * 1524 * BFS_RMATCH: Find the matched node in the graph, and put that node into 1525 * *@target_entry. 1526 * 1527 * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry 1528 * _unchanged_. 1529 */ 1530 enum bfs_result { 1531 BFS_EINVALIDNODE = -2, 1532 BFS_EQUEUEFULL = -1, 1533 BFS_RMATCH = 0, 1534 BFS_RNOMATCH = 1, 1535 }; 1536 1537 /* 1538 * bfs_result < 0 means error 1539 */ 1540 static inline bool bfs_error(enum bfs_result res) 1541 { 1542 return res < 0; 1543 } 1544 1545 /* 1546 * DEP_*_BIT in lock_list::dep 1547 * 1548 * For dependency @prev -> @next: 1549 * 1550 * SR: @prev is shared reader (->read != 0) and @next is recursive reader 1551 * (->read == 2) 1552 * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader 1553 * SN: @prev is shared reader and @next is non-recursive locker (->read != 2) 1554 * EN: @prev is exclusive locker and @next is non-recursive locker 1555 * 1556 * Note that we define the value of DEP_*_BITs so that: 1557 * bit0 is prev->read == 0 1558 * bit1 is next->read != 2 1559 */ 1560 #define DEP_SR_BIT (0 + (0 << 1)) /* 0 */ 1561 #define DEP_ER_BIT (1 + (0 << 1)) /* 1 */ 1562 #define DEP_SN_BIT (0 + (1 << 1)) /* 2 */ 1563 #define DEP_EN_BIT (1 + (1 << 1)) /* 3 */ 1564 1565 #define DEP_SR_MASK (1U << (DEP_SR_BIT)) 1566 #define DEP_ER_MASK (1U << (DEP_ER_BIT)) 1567 #define DEP_SN_MASK (1U << (DEP_SN_BIT)) 1568 #define DEP_EN_MASK (1U << (DEP_EN_BIT)) 1569 1570 static inline unsigned int 1571 __calc_dep_bit(struct held_lock *prev, struct held_lock *next) 1572 { 1573 return (prev->read == 0) + ((next->read != 2) << 1); 1574 } 1575 1576 static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next) 1577 { 1578 return 1U << __calc_dep_bit(prev, next); 1579 } 1580 1581 /* 1582 * calculate the dep_bit for backwards edges. We care about whether @prev is 1583 * shared and whether @next is recursive. 1584 */ 1585 static inline unsigned int 1586 __calc_dep_bitb(struct held_lock *prev, struct held_lock *next) 1587 { 1588 return (next->read != 2) + ((prev->read == 0) << 1); 1589 } 1590 1591 static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next) 1592 { 1593 return 1U << __calc_dep_bitb(prev, next); 1594 } 1595 1596 /* 1597 * Initialize a lock_list entry @lock belonging to @class as the root for a BFS 1598 * search. 1599 */ 1600 static inline void __bfs_init_root(struct lock_list *lock, 1601 struct lock_class *class) 1602 { 1603 lock->class = class; 1604 lock->parent = NULL; 1605 lock->only_xr = 0; 1606 } 1607 1608 /* 1609 * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the 1610 * root for a BFS search. 1611 * 1612 * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure 1613 * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)-> 1614 * and -(S*)->. 1615 */ 1616 static inline void bfs_init_root(struct lock_list *lock, 1617 struct held_lock *hlock) 1618 { 1619 __bfs_init_root(lock, hlock_class(hlock)); 1620 lock->only_xr = (hlock->read == 2); 1621 } 1622 1623 /* 1624 * Similar to bfs_init_root() but initialize the root for backwards BFS. 1625 * 1626 * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure 1627 * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not 1628 * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->). 1629 */ 1630 static inline void bfs_init_rootb(struct lock_list *lock, 1631 struct held_lock *hlock) 1632 { 1633 __bfs_init_root(lock, hlock_class(hlock)); 1634 lock->only_xr = (hlock->read != 0); 1635 } 1636 1637 static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset) 1638 { 1639 if (!lock || !lock->parent) 1640 return NULL; 1641 1642 return list_next_or_null_rcu(get_dep_list(lock->parent, offset), 1643 &lock->entry, struct lock_list, entry); 1644 } 1645 1646 /* 1647 * Breadth-First Search to find a strong path in the dependency graph. 1648 * 1649 * @source_entry: the source of the path we are searching for. 1650 * @data: data used for the second parameter of @match function 1651 * @match: match function for the search 1652 * @target_entry: pointer to the target of a matched path 1653 * @offset: the offset to struct lock_class to determine whether it is 1654 * locks_after or locks_before 1655 * 1656 * We may have multiple edges (considering different kinds of dependencies, 1657 * e.g. ER and SN) between two nodes in the dependency graph. But 1658 * only the strong dependency path in the graph is relevant to deadlocks. A 1659 * strong dependency path is a dependency path that doesn't have two adjacent 1660 * dependencies as -(*R)-> -(S*)->, please see: 1661 * 1662 * Documentation/locking/lockdep-design.rst 1663 * 1664 * for more explanation of the definition of strong dependency paths 1665 * 1666 * In __bfs(), we only traverse in the strong dependency path: 1667 * 1668 * In lock_list::only_xr, we record whether the previous dependency only 1669 * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we 1670 * filter out any -(S*)-> in the current dependency and after that, the 1671 * ->only_xr is set according to whether we only have -(*R)-> left. 1672 */ 1673 static enum bfs_result __bfs(struct lock_list *source_entry, 1674 void *data, 1675 bool (*match)(struct lock_list *entry, void *data), 1676 bool (*skip)(struct lock_list *entry, void *data), 1677 struct lock_list **target_entry, 1678 int offset) 1679 { 1680 struct circular_queue *cq = &lock_cq; 1681 struct lock_list *lock = NULL; 1682 struct lock_list *entry; 1683 struct list_head *head; 1684 unsigned int cq_depth; 1685 bool first; 1686 1687 lockdep_assert_locked(); 1688 1689 __cq_init(cq); 1690 __cq_enqueue(cq, source_entry); 1691 1692 while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) { 1693 if (!lock->class) 1694 return BFS_EINVALIDNODE; 1695 1696 /* 1697 * Step 1: check whether we already finish on this one. 1698 * 1699 * If we have visited all the dependencies from this @lock to 1700 * others (iow, if we have visited all lock_list entries in 1701 * @lock->class->locks_{after,before}) we skip, otherwise go 1702 * and visit all the dependencies in the list and mark this 1703 * list accessed. 1704 */ 1705 if (lock_accessed(lock)) 1706 continue; 1707 else 1708 mark_lock_accessed(lock); 1709 1710 /* 1711 * Step 2: check whether prev dependency and this form a strong 1712 * dependency path. 1713 */ 1714 if (lock->parent) { /* Parent exists, check prev dependency */ 1715 u8 dep = lock->dep; 1716 bool prev_only_xr = lock->parent->only_xr; 1717 1718 /* 1719 * Mask out all -(S*)-> if we only have *R in previous 1720 * step, because -(*R)-> -(S*)-> don't make up a strong 1721 * dependency. 1722 */ 1723 if (prev_only_xr) 1724 dep &= ~(DEP_SR_MASK | DEP_SN_MASK); 1725 1726 /* If nothing left, we skip */ 1727 if (!dep) 1728 continue; 1729 1730 /* If there are only -(*R)-> left, set that for the next step */ 1731 lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK)); 1732 } 1733 1734 /* 1735 * Step 3: we haven't visited this and there is a strong 1736 * dependency path to this, so check with @match. 1737 * If @skip is provide and returns true, we skip this 1738 * lock (and any path this lock is in). 1739 */ 1740 if (skip && skip(lock, data)) 1741 continue; 1742 1743 if (match(lock, data)) { 1744 *target_entry = lock; 1745 return BFS_RMATCH; 1746 } 1747 1748 /* 1749 * Step 4: if not match, expand the path by adding the 1750 * forward or backwards dependencis in the search 1751 * 1752 */ 1753 first = true; 1754 head = get_dep_list(lock, offset); 1755 list_for_each_entry_rcu(entry, head, entry) { 1756 visit_lock_entry(entry, lock); 1757 1758 /* 1759 * Note we only enqueue the first of the list into the 1760 * queue, because we can always find a sibling 1761 * dependency from one (see __bfs_next()), as a result 1762 * the space of queue is saved. 1763 */ 1764 if (!first) 1765 continue; 1766 1767 first = false; 1768 1769 if (__cq_enqueue(cq, entry)) 1770 return BFS_EQUEUEFULL; 1771 1772 cq_depth = __cq_get_elem_count(cq); 1773 if (max_bfs_queue_depth < cq_depth) 1774 max_bfs_queue_depth = cq_depth; 1775 } 1776 } 1777 1778 return BFS_RNOMATCH; 1779 } 1780 1781 static inline enum bfs_result 1782 __bfs_forwards(struct lock_list *src_entry, 1783 void *data, 1784 bool (*match)(struct lock_list *entry, void *data), 1785 bool (*skip)(struct lock_list *entry, void *data), 1786 struct lock_list **target_entry) 1787 { 1788 return __bfs(src_entry, data, match, skip, target_entry, 1789 offsetof(struct lock_class, locks_after)); 1790 1791 } 1792 1793 static inline enum bfs_result 1794 __bfs_backwards(struct lock_list *src_entry, 1795 void *data, 1796 bool (*match)(struct lock_list *entry, void *data), 1797 bool (*skip)(struct lock_list *entry, void *data), 1798 struct lock_list **target_entry) 1799 { 1800 return __bfs(src_entry, data, match, skip, target_entry, 1801 offsetof(struct lock_class, locks_before)); 1802 1803 } 1804 1805 static void print_lock_trace(const struct lock_trace *trace, 1806 unsigned int spaces) 1807 { 1808 stack_trace_print(trace->entries, trace->nr_entries, spaces); 1809 } 1810 1811 /* 1812 * Print a dependency chain entry (this is only done when a deadlock 1813 * has been detected): 1814 */ 1815 static noinline void 1816 print_circular_bug_entry(struct lock_list *target, int depth) 1817 { 1818 if (debug_locks_silent) 1819 return; 1820 printk("\n-> #%u", depth); 1821 print_lock_name(target->class); 1822 printk(KERN_CONT ":\n"); 1823 print_lock_trace(target->trace, 6); 1824 } 1825 1826 static void 1827 print_circular_lock_scenario(struct held_lock *src, 1828 struct held_lock *tgt, 1829 struct lock_list *prt) 1830 { 1831 struct lock_class *source = hlock_class(src); 1832 struct lock_class *target = hlock_class(tgt); 1833 struct lock_class *parent = prt->class; 1834 1835 /* 1836 * A direct locking problem where unsafe_class lock is taken 1837 * directly by safe_class lock, then all we need to show 1838 * is the deadlock scenario, as it is obvious that the 1839 * unsafe lock is taken under the safe lock. 1840 * 1841 * But if there is a chain instead, where the safe lock takes 1842 * an intermediate lock (middle_class) where this lock is 1843 * not the same as the safe lock, then the lock chain is 1844 * used to describe the problem. Otherwise we would need 1845 * to show a different CPU case for each link in the chain 1846 * from the safe_class lock to the unsafe_class lock. 1847 */ 1848 if (parent != source) { 1849 printk("Chain exists of:\n "); 1850 __print_lock_name(source); 1851 printk(KERN_CONT " --> "); 1852 __print_lock_name(parent); 1853 printk(KERN_CONT " --> "); 1854 __print_lock_name(target); 1855 printk(KERN_CONT "\n\n"); 1856 } 1857 1858 printk(" Possible unsafe locking scenario:\n\n"); 1859 printk(" CPU0 CPU1\n"); 1860 printk(" ---- ----\n"); 1861 printk(" lock("); 1862 __print_lock_name(target); 1863 printk(KERN_CONT ");\n"); 1864 printk(" lock("); 1865 __print_lock_name(parent); 1866 printk(KERN_CONT ");\n"); 1867 printk(" lock("); 1868 __print_lock_name(target); 1869 printk(KERN_CONT ");\n"); 1870 printk(" lock("); 1871 __print_lock_name(source); 1872 printk(KERN_CONT ");\n"); 1873 printk("\n *** DEADLOCK ***\n\n"); 1874 } 1875 1876 /* 1877 * When a circular dependency is detected, print the 1878 * header first: 1879 */ 1880 static noinline void 1881 print_circular_bug_header(struct lock_list *entry, unsigned int depth, 1882 struct held_lock *check_src, 1883 struct held_lock *check_tgt) 1884 { 1885 struct task_struct *curr = current; 1886 1887 if (debug_locks_silent) 1888 return; 1889 1890 pr_warn("\n"); 1891 pr_warn("======================================================\n"); 1892 pr_warn("WARNING: possible circular locking dependency detected\n"); 1893 print_kernel_ident(); 1894 pr_warn("------------------------------------------------------\n"); 1895 pr_warn("%s/%d is trying to acquire lock:\n", 1896 curr->comm, task_pid_nr(curr)); 1897 print_lock(check_src); 1898 1899 pr_warn("\nbut task is already holding lock:\n"); 1900 1901 print_lock(check_tgt); 1902 pr_warn("\nwhich lock already depends on the new lock.\n\n"); 1903 pr_warn("\nthe existing dependency chain (in reverse order) is:\n"); 1904 1905 print_circular_bug_entry(entry, depth); 1906 } 1907 1908 /* 1909 * We are about to add A -> B into the dependency graph, and in __bfs() a 1910 * strong dependency path A -> .. -> B is found: hlock_class equals 1911 * entry->class. 1912 * 1913 * If A -> .. -> B can replace A -> B in any __bfs() search (means the former 1914 * is _stronger_ than or equal to the latter), we consider A -> B as redundant. 1915 * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A 1916 * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the 1917 * dependency graph, as any strong path ..-> A -> B ->.. we can get with 1918 * having dependency A -> B, we could already get a equivalent path ..-> A -> 1919 * .. -> B -> .. with A -> .. -> B. Therefore A -> B is reduntant. 1920 * 1921 * We need to make sure both the start and the end of A -> .. -> B is not 1922 * weaker than A -> B. For the start part, please see the comment in 1923 * check_redundant(). For the end part, we need: 1924 * 1925 * Either 1926 * 1927 * a) A -> B is -(*R)-> (everything is not weaker than that) 1928 * 1929 * or 1930 * 1931 * b) A -> .. -> B is -(*N)-> (nothing is stronger than this) 1932 * 1933 */ 1934 static inline bool hlock_equal(struct lock_list *entry, void *data) 1935 { 1936 struct held_lock *hlock = (struct held_lock *)data; 1937 1938 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ 1939 (hlock->read == 2 || /* A -> B is -(*R)-> */ 1940 !entry->only_xr); /* A -> .. -> B is -(*N)-> */ 1941 } 1942 1943 /* 1944 * We are about to add B -> A into the dependency graph, and in __bfs() a 1945 * strong dependency path A -> .. -> B is found: hlock_class equals 1946 * entry->class. 1947 * 1948 * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong 1949 * dependency cycle, that means: 1950 * 1951 * Either 1952 * 1953 * a) B -> A is -(E*)-> 1954 * 1955 * or 1956 * 1957 * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B) 1958 * 1959 * as then we don't have -(*R)-> -(S*)-> in the cycle. 1960 */ 1961 static inline bool hlock_conflict(struct lock_list *entry, void *data) 1962 { 1963 struct held_lock *hlock = (struct held_lock *)data; 1964 1965 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ 1966 (hlock->read == 0 || /* B -> A is -(E*)-> */ 1967 !entry->only_xr); /* A -> .. -> B is -(*N)-> */ 1968 } 1969 1970 static noinline void print_circular_bug(struct lock_list *this, 1971 struct lock_list *target, 1972 struct held_lock *check_src, 1973 struct held_lock *check_tgt) 1974 { 1975 struct task_struct *curr = current; 1976 struct lock_list *parent; 1977 struct lock_list *first_parent; 1978 int depth; 1979 1980 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1981 return; 1982 1983 this->trace = save_trace(); 1984 if (!this->trace) 1985 return; 1986 1987 depth = get_lock_depth(target); 1988 1989 print_circular_bug_header(target, depth, check_src, check_tgt); 1990 1991 parent = get_lock_parent(target); 1992 first_parent = parent; 1993 1994 while (parent) { 1995 print_circular_bug_entry(parent, --depth); 1996 parent = get_lock_parent(parent); 1997 } 1998 1999 printk("\nother info that might help us debug this:\n\n"); 2000 print_circular_lock_scenario(check_src, check_tgt, 2001 first_parent); 2002 2003 lockdep_print_held_locks(curr); 2004 2005 printk("\nstack backtrace:\n"); 2006 dump_stack(); 2007 } 2008 2009 static noinline void print_bfs_bug(int ret) 2010 { 2011 if (!debug_locks_off_graph_unlock()) 2012 return; 2013 2014 /* 2015 * Breadth-first-search failed, graph got corrupted? 2016 */ 2017 WARN(1, "lockdep bfs error:%d\n", ret); 2018 } 2019 2020 static bool noop_count(struct lock_list *entry, void *data) 2021 { 2022 (*(unsigned long *)data)++; 2023 return false; 2024 } 2025 2026 static unsigned long __lockdep_count_forward_deps(struct lock_list *this) 2027 { 2028 unsigned long count = 0; 2029 struct lock_list *target_entry; 2030 2031 __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry); 2032 2033 return count; 2034 } 2035 unsigned long lockdep_count_forward_deps(struct lock_class *class) 2036 { 2037 unsigned long ret, flags; 2038 struct lock_list this; 2039 2040 __bfs_init_root(&this, class); 2041 2042 raw_local_irq_save(flags); 2043 lockdep_lock(); 2044 ret = __lockdep_count_forward_deps(&this); 2045 lockdep_unlock(); 2046 raw_local_irq_restore(flags); 2047 2048 return ret; 2049 } 2050 2051 static unsigned long __lockdep_count_backward_deps(struct lock_list *this) 2052 { 2053 unsigned long count = 0; 2054 struct lock_list *target_entry; 2055 2056 __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry); 2057 2058 return count; 2059 } 2060 2061 unsigned long lockdep_count_backward_deps(struct lock_class *class) 2062 { 2063 unsigned long ret, flags; 2064 struct lock_list this; 2065 2066 __bfs_init_root(&this, class); 2067 2068 raw_local_irq_save(flags); 2069 lockdep_lock(); 2070 ret = __lockdep_count_backward_deps(&this); 2071 lockdep_unlock(); 2072 raw_local_irq_restore(flags); 2073 2074 return ret; 2075 } 2076 2077 /* 2078 * Check that the dependency graph starting at <src> can lead to 2079 * <target> or not. 2080 */ 2081 static noinline enum bfs_result 2082 check_path(struct held_lock *target, struct lock_list *src_entry, 2083 bool (*match)(struct lock_list *entry, void *data), 2084 bool (*skip)(struct lock_list *entry, void *data), 2085 struct lock_list **target_entry) 2086 { 2087 enum bfs_result ret; 2088 2089 ret = __bfs_forwards(src_entry, target, match, skip, target_entry); 2090 2091 if (unlikely(bfs_error(ret))) 2092 print_bfs_bug(ret); 2093 2094 return ret; 2095 } 2096 2097 /* 2098 * Prove that the dependency graph starting at <src> can not 2099 * lead to <target>. If it can, there is a circle when adding 2100 * <target> -> <src> dependency. 2101 * 2102 * Print an error and return BFS_RMATCH if it does. 2103 */ 2104 static noinline enum bfs_result 2105 check_noncircular(struct held_lock *src, struct held_lock *target, 2106 struct lock_trace **const trace) 2107 { 2108 enum bfs_result ret; 2109 struct lock_list *target_entry; 2110 struct lock_list src_entry; 2111 2112 bfs_init_root(&src_entry, src); 2113 2114 debug_atomic_inc(nr_cyclic_checks); 2115 2116 ret = check_path(target, &src_entry, hlock_conflict, NULL, &target_entry); 2117 2118 if (unlikely(ret == BFS_RMATCH)) { 2119 if (!*trace) { 2120 /* 2121 * If save_trace fails here, the printing might 2122 * trigger a WARN but because of the !nr_entries it 2123 * should not do bad things. 2124 */ 2125 *trace = save_trace(); 2126 } 2127 2128 print_circular_bug(&src_entry, target_entry, src, target); 2129 } 2130 2131 return ret; 2132 } 2133 2134 #ifdef CONFIG_TRACE_IRQFLAGS 2135 2136 /* 2137 * Forwards and backwards subgraph searching, for the purposes of 2138 * proving that two subgraphs can be connected by a new dependency 2139 * without creating any illegal irq-safe -> irq-unsafe lock dependency. 2140 * 2141 * A irq safe->unsafe deadlock happens with the following conditions: 2142 * 2143 * 1) We have a strong dependency path A -> ... -> B 2144 * 2145 * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore 2146 * irq can create a new dependency B -> A (consider the case that a holder 2147 * of B gets interrupted by an irq whose handler will try to acquire A). 2148 * 2149 * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a 2150 * strong circle: 2151 * 2152 * For the usage bits of B: 2153 * a) if A -> B is -(*N)->, then B -> A could be any type, so any 2154 * ENABLED_IRQ usage suffices. 2155 * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only 2156 * ENABLED_IRQ_*_READ usage suffices. 2157 * 2158 * For the usage bits of A: 2159 * c) if A -> B is -(E*)->, then B -> A could be any type, so any 2160 * USED_IN_IRQ usage suffices. 2161 * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only 2162 * USED_IN_IRQ_*_READ usage suffices. 2163 */ 2164 2165 /* 2166 * There is a strong dependency path in the dependency graph: A -> B, and now 2167 * we need to decide which usage bit of A should be accumulated to detect 2168 * safe->unsafe bugs. 2169 * 2170 * Note that usage_accumulate() is used in backwards search, so ->only_xr 2171 * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true). 2172 * 2173 * As above, if only_xr is false, which means A -> B has -(E*)-> dependency 2174 * path, any usage of A should be considered. Otherwise, we should only 2175 * consider _READ usage. 2176 */ 2177 static inline bool usage_accumulate(struct lock_list *entry, void *mask) 2178 { 2179 if (!entry->only_xr) 2180 *(unsigned long *)mask |= entry->class->usage_mask; 2181 else /* Mask out _READ usage bits */ 2182 *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ); 2183 2184 return false; 2185 } 2186 2187 /* 2188 * There is a strong dependency path in the dependency graph: A -> B, and now 2189 * we need to decide which usage bit of B conflicts with the usage bits of A, 2190 * i.e. which usage bit of B may introduce safe->unsafe deadlocks. 2191 * 2192 * As above, if only_xr is false, which means A -> B has -(*N)-> dependency 2193 * path, any usage of B should be considered. Otherwise, we should only 2194 * consider _READ usage. 2195 */ 2196 static inline bool usage_match(struct lock_list *entry, void *mask) 2197 { 2198 if (!entry->only_xr) 2199 return !!(entry->class->usage_mask & *(unsigned long *)mask); 2200 else /* Mask out _READ usage bits */ 2201 return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask); 2202 } 2203 2204 static inline bool usage_skip(struct lock_list *entry, void *mask) 2205 { 2206 /* 2207 * Skip local_lock() for irq inversion detection. 2208 * 2209 * For !RT, local_lock() is not a real lock, so it won't carry any 2210 * dependency. 2211 * 2212 * For RT, an irq inversion happens when we have lock A and B, and on 2213 * some CPU we can have: 2214 * 2215 * lock(A); 2216 * <interrupted> 2217 * lock(B); 2218 * 2219 * where lock(B) cannot sleep, and we have a dependency B -> ... -> A. 2220 * 2221 * Now we prove local_lock() cannot exist in that dependency. First we 2222 * have the observation for any lock chain L1 -> ... -> Ln, for any 2223 * 1 <= i <= n, Li.inner_wait_type <= L1.inner_wait_type, otherwise 2224 * wait context check will complain. And since B is not a sleep lock, 2225 * therefore B.inner_wait_type >= 2, and since the inner_wait_type of 2226 * local_lock() is 3, which is greater than 2, therefore there is no 2227 * way the local_lock() exists in the dependency B -> ... -> A. 2228 * 2229 * As a result, we will skip local_lock(), when we search for irq 2230 * inversion bugs. 2231 */ 2232 if (entry->class->lock_type == LD_LOCK_PERCPU) { 2233 if (DEBUG_LOCKS_WARN_ON(entry->class->wait_type_inner < LD_WAIT_CONFIG)) 2234 return false; 2235 2236 return true; 2237 } 2238 2239 return false; 2240 } 2241 2242 /* 2243 * Find a node in the forwards-direction dependency sub-graph starting 2244 * at @root->class that matches @bit. 2245 * 2246 * Return BFS_MATCH if such a node exists in the subgraph, and put that node 2247 * into *@target_entry. 2248 */ 2249 static enum bfs_result 2250 find_usage_forwards(struct lock_list *root, unsigned long usage_mask, 2251 struct lock_list **target_entry) 2252 { 2253 enum bfs_result result; 2254 2255 debug_atomic_inc(nr_find_usage_forwards_checks); 2256 2257 result = __bfs_forwards(root, &usage_mask, usage_match, usage_skip, target_entry); 2258 2259 return result; 2260 } 2261 2262 /* 2263 * Find a node in the backwards-direction dependency sub-graph starting 2264 * at @root->class that matches @bit. 2265 */ 2266 static enum bfs_result 2267 find_usage_backwards(struct lock_list *root, unsigned long usage_mask, 2268 struct lock_list **target_entry) 2269 { 2270 enum bfs_result result; 2271 2272 debug_atomic_inc(nr_find_usage_backwards_checks); 2273 2274 result = __bfs_backwards(root, &usage_mask, usage_match, usage_skip, target_entry); 2275 2276 return result; 2277 } 2278 2279 static void print_lock_class_header(struct lock_class *class, int depth) 2280 { 2281 int bit; 2282 2283 printk("%*s->", depth, ""); 2284 print_lock_name(class); 2285 #ifdef CONFIG_DEBUG_LOCKDEP 2286 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class)); 2287 #endif 2288 printk(KERN_CONT " {\n"); 2289 2290 for (bit = 0; bit < LOCK_TRACE_STATES; bit++) { 2291 if (class->usage_mask & (1 << bit)) { 2292 int len = depth; 2293 2294 len += printk("%*s %s", depth, "", usage_str[bit]); 2295 len += printk(KERN_CONT " at:\n"); 2296 print_lock_trace(class->usage_traces[bit], len); 2297 } 2298 } 2299 printk("%*s }\n", depth, ""); 2300 2301 printk("%*s ... key at: [<%px>] %pS\n", 2302 depth, "", class->key, class->key); 2303 } 2304 2305 /* 2306 * printk the shortest lock dependencies from @start to @end in reverse order: 2307 */ 2308 static void __used 2309 print_shortest_lock_dependencies(struct lock_list *leaf, 2310 struct lock_list *root) 2311 { 2312 struct lock_list *entry = leaf; 2313 int depth; 2314 2315 /*compute depth from generated tree by BFS*/ 2316 depth = get_lock_depth(leaf); 2317 2318 do { 2319 print_lock_class_header(entry->class, depth); 2320 printk("%*s ... acquired at:\n", depth, ""); 2321 print_lock_trace(entry->trace, 2); 2322 printk("\n"); 2323 2324 if (depth == 0 && (entry != root)) { 2325 printk("lockdep:%s bad path found in chain graph\n", __func__); 2326 break; 2327 } 2328 2329 entry = get_lock_parent(entry); 2330 depth--; 2331 } while (entry && (depth >= 0)); 2332 } 2333 2334 static void 2335 print_irq_lock_scenario(struct lock_list *safe_entry, 2336 struct lock_list *unsafe_entry, 2337 struct lock_class *prev_class, 2338 struct lock_class *next_class) 2339 { 2340 struct lock_class *safe_class = safe_entry->class; 2341 struct lock_class *unsafe_class = unsafe_entry->class; 2342 struct lock_class *middle_class = prev_class; 2343 2344 if (middle_class == safe_class) 2345 middle_class = next_class; 2346 2347 /* 2348 * A direct locking problem where unsafe_class lock is taken 2349 * directly by safe_class lock, then all we need to show 2350 * is the deadlock scenario, as it is obvious that the 2351 * unsafe lock is taken under the safe lock. 2352 * 2353 * But if there is a chain instead, where the safe lock takes 2354 * an intermediate lock (middle_class) where this lock is 2355 * not the same as the safe lock, then the lock chain is 2356 * used to describe the problem. Otherwise we would need 2357 * to show a different CPU case for each link in the chain 2358 * from the safe_class lock to the unsafe_class lock. 2359 */ 2360 if (middle_class != unsafe_class) { 2361 printk("Chain exists of:\n "); 2362 __print_lock_name(safe_class); 2363 printk(KERN_CONT " --> "); 2364 __print_lock_name(middle_class); 2365 printk(KERN_CONT " --> "); 2366 __print_lock_name(unsafe_class); 2367 printk(KERN_CONT "\n\n"); 2368 } 2369 2370 printk(" Possible interrupt unsafe locking scenario:\n\n"); 2371 printk(" CPU0 CPU1\n"); 2372 printk(" ---- ----\n"); 2373 printk(" lock("); 2374 __print_lock_name(unsafe_class); 2375 printk(KERN_CONT ");\n"); 2376 printk(" local_irq_disable();\n"); 2377 printk(" lock("); 2378 __print_lock_name(safe_class); 2379 printk(KERN_CONT ");\n"); 2380 printk(" lock("); 2381 __print_lock_name(middle_class); 2382 printk(KERN_CONT ");\n"); 2383 printk(" <Interrupt>\n"); 2384 printk(" lock("); 2385 __print_lock_name(safe_class); 2386 printk(KERN_CONT ");\n"); 2387 printk("\n *** DEADLOCK ***\n\n"); 2388 } 2389 2390 static void 2391 print_bad_irq_dependency(struct task_struct *curr, 2392 struct lock_list *prev_root, 2393 struct lock_list *next_root, 2394 struct lock_list *backwards_entry, 2395 struct lock_list *forwards_entry, 2396 struct held_lock *prev, 2397 struct held_lock *next, 2398 enum lock_usage_bit bit1, 2399 enum lock_usage_bit bit2, 2400 const char *irqclass) 2401 { 2402 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 2403 return; 2404 2405 pr_warn("\n"); 2406 pr_warn("=====================================================\n"); 2407 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", 2408 irqclass, irqclass); 2409 print_kernel_ident(); 2410 pr_warn("-----------------------------------------------------\n"); 2411 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n", 2412 curr->comm, task_pid_nr(curr), 2413 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT, 2414 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, 2415 lockdep_hardirqs_enabled(), 2416 curr->softirqs_enabled); 2417 print_lock(next); 2418 2419 pr_warn("\nand this task is already holding:\n"); 2420 print_lock(prev); 2421 pr_warn("which would create a new lock dependency:\n"); 2422 print_lock_name(hlock_class(prev)); 2423 pr_cont(" ->"); 2424 print_lock_name(hlock_class(next)); 2425 pr_cont("\n"); 2426 2427 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n", 2428 irqclass); 2429 print_lock_name(backwards_entry->class); 2430 pr_warn("\n... which became %s-irq-safe at:\n", irqclass); 2431 2432 print_lock_trace(backwards_entry->class->usage_traces[bit1], 1); 2433 2434 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass); 2435 print_lock_name(forwards_entry->class); 2436 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass); 2437 pr_warn("..."); 2438 2439 print_lock_trace(forwards_entry->class->usage_traces[bit2], 1); 2440 2441 pr_warn("\nother info that might help us debug this:\n\n"); 2442 print_irq_lock_scenario(backwards_entry, forwards_entry, 2443 hlock_class(prev), hlock_class(next)); 2444 2445 lockdep_print_held_locks(curr); 2446 2447 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass); 2448 prev_root->trace = save_trace(); 2449 if (!prev_root->trace) 2450 return; 2451 print_shortest_lock_dependencies(backwards_entry, prev_root); 2452 2453 pr_warn("\nthe dependencies between the lock to be acquired"); 2454 pr_warn(" and %s-irq-unsafe lock:\n", irqclass); 2455 next_root->trace = save_trace(); 2456 if (!next_root->trace) 2457 return; 2458 print_shortest_lock_dependencies(forwards_entry, next_root); 2459 2460 pr_warn("\nstack backtrace:\n"); 2461 dump_stack(); 2462 } 2463 2464 static const char *state_names[] = { 2465 #define LOCKDEP_STATE(__STATE) \ 2466 __stringify(__STATE), 2467 #include "lockdep_states.h" 2468 #undef LOCKDEP_STATE 2469 }; 2470 2471 static const char *state_rnames[] = { 2472 #define LOCKDEP_STATE(__STATE) \ 2473 __stringify(__STATE)"-READ", 2474 #include "lockdep_states.h" 2475 #undef LOCKDEP_STATE 2476 }; 2477 2478 static inline const char *state_name(enum lock_usage_bit bit) 2479 { 2480 if (bit & LOCK_USAGE_READ_MASK) 2481 return state_rnames[bit >> LOCK_USAGE_DIR_MASK]; 2482 else 2483 return state_names[bit >> LOCK_USAGE_DIR_MASK]; 2484 } 2485 2486 /* 2487 * The bit number is encoded like: 2488 * 2489 * bit0: 0 exclusive, 1 read lock 2490 * bit1: 0 used in irq, 1 irq enabled 2491 * bit2-n: state 2492 */ 2493 static int exclusive_bit(int new_bit) 2494 { 2495 int state = new_bit & LOCK_USAGE_STATE_MASK; 2496 int dir = new_bit & LOCK_USAGE_DIR_MASK; 2497 2498 /* 2499 * keep state, bit flip the direction and strip read. 2500 */ 2501 return state | (dir ^ LOCK_USAGE_DIR_MASK); 2502 } 2503 2504 /* 2505 * Observe that when given a bitmask where each bitnr is encoded as above, a 2506 * right shift of the mask transforms the individual bitnrs as -1 and 2507 * conversely, a left shift transforms into +1 for the individual bitnrs. 2508 * 2509 * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can 2510 * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0) 2511 * instead by subtracting the bit number by 2, or shifting the mask right by 2. 2512 * 2513 * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2. 2514 * 2515 * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is 2516 * all bits set) and recompose with bitnr1 flipped. 2517 */ 2518 static unsigned long invert_dir_mask(unsigned long mask) 2519 { 2520 unsigned long excl = 0; 2521 2522 /* Invert dir */ 2523 excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK; 2524 excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK; 2525 2526 return excl; 2527 } 2528 2529 /* 2530 * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ 2531 * usage may cause deadlock too, for example: 2532 * 2533 * P1 P2 2534 * <irq disabled> 2535 * write_lock(l1); <irq enabled> 2536 * read_lock(l2); 2537 * write_lock(l2); 2538 * <in irq> 2539 * read_lock(l1); 2540 * 2541 * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2 2542 * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible 2543 * deadlock. 2544 * 2545 * In fact, all of the following cases may cause deadlocks: 2546 * 2547 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_* 2548 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_* 2549 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ 2550 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ 2551 * 2552 * As a result, to calculate the "exclusive mask", first we invert the 2553 * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with 2554 * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all 2555 * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*). 2556 */ 2557 static unsigned long exclusive_mask(unsigned long mask) 2558 { 2559 unsigned long excl = invert_dir_mask(mask); 2560 2561 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK; 2562 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK; 2563 2564 return excl; 2565 } 2566 2567 /* 2568 * Retrieve the _possible_ original mask to which @mask is 2569 * exclusive. Ie: this is the opposite of exclusive_mask(). 2570 * Note that 2 possible original bits can match an exclusive 2571 * bit: one has LOCK_USAGE_READ_MASK set, the other has it 2572 * cleared. So both are returned for each exclusive bit. 2573 */ 2574 static unsigned long original_mask(unsigned long mask) 2575 { 2576 unsigned long excl = invert_dir_mask(mask); 2577 2578 /* Include read in existing usages */ 2579 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK; 2580 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK; 2581 2582 return excl; 2583 } 2584 2585 /* 2586 * Find the first pair of bit match between an original 2587 * usage mask and an exclusive usage mask. 2588 */ 2589 static int find_exclusive_match(unsigned long mask, 2590 unsigned long excl_mask, 2591 enum lock_usage_bit *bitp, 2592 enum lock_usage_bit *excl_bitp) 2593 { 2594 int bit, excl, excl_read; 2595 2596 for_each_set_bit(bit, &mask, LOCK_USED) { 2597 /* 2598 * exclusive_bit() strips the read bit, however, 2599 * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need 2600 * to search excl | LOCK_USAGE_READ_MASK as well. 2601 */ 2602 excl = exclusive_bit(bit); 2603 excl_read = excl | LOCK_USAGE_READ_MASK; 2604 if (excl_mask & lock_flag(excl)) { 2605 *bitp = bit; 2606 *excl_bitp = excl; 2607 return 0; 2608 } else if (excl_mask & lock_flag(excl_read)) { 2609 *bitp = bit; 2610 *excl_bitp = excl_read; 2611 return 0; 2612 } 2613 } 2614 return -1; 2615 } 2616 2617 /* 2618 * Prove that the new dependency does not connect a hardirq-safe(-read) 2619 * lock with a hardirq-unsafe lock - to achieve this we search 2620 * the backwards-subgraph starting at <prev>, and the 2621 * forwards-subgraph starting at <next>: 2622 */ 2623 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev, 2624 struct held_lock *next) 2625 { 2626 unsigned long usage_mask = 0, forward_mask, backward_mask; 2627 enum lock_usage_bit forward_bit = 0, backward_bit = 0; 2628 struct lock_list *target_entry1; 2629 struct lock_list *target_entry; 2630 struct lock_list this, that; 2631 enum bfs_result ret; 2632 2633 /* 2634 * Step 1: gather all hard/soft IRQs usages backward in an 2635 * accumulated usage mask. 2636 */ 2637 bfs_init_rootb(&this, prev); 2638 2639 ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, usage_skip, NULL); 2640 if (bfs_error(ret)) { 2641 print_bfs_bug(ret); 2642 return 0; 2643 } 2644 2645 usage_mask &= LOCKF_USED_IN_IRQ_ALL; 2646 if (!usage_mask) 2647 return 1; 2648 2649 /* 2650 * Step 2: find exclusive uses forward that match the previous 2651 * backward accumulated mask. 2652 */ 2653 forward_mask = exclusive_mask(usage_mask); 2654 2655 bfs_init_root(&that, next); 2656 2657 ret = find_usage_forwards(&that, forward_mask, &target_entry1); 2658 if (bfs_error(ret)) { 2659 print_bfs_bug(ret); 2660 return 0; 2661 } 2662 if (ret == BFS_RNOMATCH) 2663 return 1; 2664 2665 /* 2666 * Step 3: we found a bad match! Now retrieve a lock from the backward 2667 * list whose usage mask matches the exclusive usage mask from the 2668 * lock found on the forward list. 2669 */ 2670 backward_mask = original_mask(target_entry1->class->usage_mask); 2671 2672 ret = find_usage_backwards(&this, backward_mask, &target_entry); 2673 if (bfs_error(ret)) { 2674 print_bfs_bug(ret); 2675 return 0; 2676 } 2677 if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH)) 2678 return 1; 2679 2680 /* 2681 * Step 4: narrow down to a pair of incompatible usage bits 2682 * and report it. 2683 */ 2684 ret = find_exclusive_match(target_entry->class->usage_mask, 2685 target_entry1->class->usage_mask, 2686 &backward_bit, &forward_bit); 2687 if (DEBUG_LOCKS_WARN_ON(ret == -1)) 2688 return 1; 2689 2690 print_bad_irq_dependency(curr, &this, &that, 2691 target_entry, target_entry1, 2692 prev, next, 2693 backward_bit, forward_bit, 2694 state_name(backward_bit)); 2695 2696 return 0; 2697 } 2698 2699 #else 2700 2701 static inline int check_irq_usage(struct task_struct *curr, 2702 struct held_lock *prev, struct held_lock *next) 2703 { 2704 return 1; 2705 } 2706 2707 static inline bool usage_skip(struct lock_list *entry, void *mask) 2708 { 2709 return false; 2710 } 2711 2712 #endif /* CONFIG_TRACE_IRQFLAGS */ 2713 2714 #ifdef CONFIG_LOCKDEP_SMALL 2715 /* 2716 * Check that the dependency graph starting at <src> can lead to 2717 * <target> or not. If it can, <src> -> <target> dependency is already 2718 * in the graph. 2719 * 2720 * Return BFS_RMATCH if it does, or BFS_RMATCH if it does not, return BFS_E* if 2721 * any error appears in the bfs search. 2722 */ 2723 static noinline enum bfs_result 2724 check_redundant(struct held_lock *src, struct held_lock *target) 2725 { 2726 enum bfs_result ret; 2727 struct lock_list *target_entry; 2728 struct lock_list src_entry; 2729 2730 bfs_init_root(&src_entry, src); 2731 /* 2732 * Special setup for check_redundant(). 2733 * 2734 * To report redundant, we need to find a strong dependency path that 2735 * is equal to or stronger than <src> -> <target>. So if <src> is E, 2736 * we need to let __bfs() only search for a path starting at a -(E*)->, 2737 * we achieve this by setting the initial node's ->only_xr to true in 2738 * that case. And if <prev> is S, we set initial ->only_xr to false 2739 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant. 2740 */ 2741 src_entry.only_xr = src->read == 0; 2742 2743 debug_atomic_inc(nr_redundant_checks); 2744 2745 /* 2746 * Note: we skip local_lock() for redundant check, because as the 2747 * comment in usage_skip(), A -> local_lock() -> B and A -> B are not 2748 * the same. 2749 */ 2750 ret = check_path(target, &src_entry, hlock_equal, usage_skip, &target_entry); 2751 2752 if (ret == BFS_RMATCH) 2753 debug_atomic_inc(nr_redundant); 2754 2755 return ret; 2756 } 2757 2758 #else 2759 2760 static inline enum bfs_result 2761 check_redundant(struct held_lock *src, struct held_lock *target) 2762 { 2763 return BFS_RNOMATCH; 2764 } 2765 2766 #endif 2767 2768 static void inc_chains(int irq_context) 2769 { 2770 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT) 2771 nr_hardirq_chains++; 2772 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT) 2773 nr_softirq_chains++; 2774 else 2775 nr_process_chains++; 2776 } 2777 2778 static void dec_chains(int irq_context) 2779 { 2780 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT) 2781 nr_hardirq_chains--; 2782 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT) 2783 nr_softirq_chains--; 2784 else 2785 nr_process_chains--; 2786 } 2787 2788 static void 2789 print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv) 2790 { 2791 struct lock_class *next = hlock_class(nxt); 2792 struct lock_class *prev = hlock_class(prv); 2793 2794 printk(" Possible unsafe locking scenario:\n\n"); 2795 printk(" CPU0\n"); 2796 printk(" ----\n"); 2797 printk(" lock("); 2798 __print_lock_name(prev); 2799 printk(KERN_CONT ");\n"); 2800 printk(" lock("); 2801 __print_lock_name(next); 2802 printk(KERN_CONT ");\n"); 2803 printk("\n *** DEADLOCK ***\n\n"); 2804 printk(" May be due to missing lock nesting notation\n\n"); 2805 } 2806 2807 static void 2808 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, 2809 struct held_lock *next) 2810 { 2811 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 2812 return; 2813 2814 pr_warn("\n"); 2815 pr_warn("============================================\n"); 2816 pr_warn("WARNING: possible recursive locking detected\n"); 2817 print_kernel_ident(); 2818 pr_warn("--------------------------------------------\n"); 2819 pr_warn("%s/%d is trying to acquire lock:\n", 2820 curr->comm, task_pid_nr(curr)); 2821 print_lock(next); 2822 pr_warn("\nbut task is already holding lock:\n"); 2823 print_lock(prev); 2824 2825 pr_warn("\nother info that might help us debug this:\n"); 2826 print_deadlock_scenario(next, prev); 2827 lockdep_print_held_locks(curr); 2828 2829 pr_warn("\nstack backtrace:\n"); 2830 dump_stack(); 2831 } 2832 2833 /* 2834 * Check whether we are holding such a class already. 2835 * 2836 * (Note that this has to be done separately, because the graph cannot 2837 * detect such classes of deadlocks.) 2838 * 2839 * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same 2840 * lock class is held but nest_lock is also held, i.e. we rely on the 2841 * nest_lock to avoid the deadlock. 2842 */ 2843 static int 2844 check_deadlock(struct task_struct *curr, struct held_lock *next) 2845 { 2846 struct held_lock *prev; 2847 struct held_lock *nest = NULL; 2848 int i; 2849 2850 for (i = 0; i < curr->lockdep_depth; i++) { 2851 prev = curr->held_locks + i; 2852 2853 if (prev->instance == next->nest_lock) 2854 nest = prev; 2855 2856 if (hlock_class(prev) != hlock_class(next)) 2857 continue; 2858 2859 /* 2860 * Allow read-after-read recursion of the same 2861 * lock class (i.e. read_lock(lock)+read_lock(lock)): 2862 */ 2863 if ((next->read == 2) && prev->read) 2864 continue; 2865 2866 /* 2867 * We're holding the nest_lock, which serializes this lock's 2868 * nesting behaviour. 2869 */ 2870 if (nest) 2871 return 2; 2872 2873 print_deadlock_bug(curr, prev, next); 2874 return 0; 2875 } 2876 return 1; 2877 } 2878 2879 /* 2880 * There was a chain-cache miss, and we are about to add a new dependency 2881 * to a previous lock. We validate the following rules: 2882 * 2883 * - would the adding of the <prev> -> <next> dependency create a 2884 * circular dependency in the graph? [== circular deadlock] 2885 * 2886 * - does the new prev->next dependency connect any hardirq-safe lock 2887 * (in the full backwards-subgraph starting at <prev>) with any 2888 * hardirq-unsafe lock (in the full forwards-subgraph starting at 2889 * <next>)? [== illegal lock inversion with hardirq contexts] 2890 * 2891 * - does the new prev->next dependency connect any softirq-safe lock 2892 * (in the full backwards-subgraph starting at <prev>) with any 2893 * softirq-unsafe lock (in the full forwards-subgraph starting at 2894 * <next>)? [== illegal lock inversion with softirq contexts] 2895 * 2896 * any of these scenarios could lead to a deadlock. 2897 * 2898 * Then if all the validations pass, we add the forwards and backwards 2899 * dependency. 2900 */ 2901 static int 2902 check_prev_add(struct task_struct *curr, struct held_lock *prev, 2903 struct held_lock *next, u16 distance, 2904 struct lock_trace **const trace) 2905 { 2906 struct lock_list *entry; 2907 enum bfs_result ret; 2908 2909 if (!hlock_class(prev)->key || !hlock_class(next)->key) { 2910 /* 2911 * The warning statements below may trigger a use-after-free 2912 * of the class name. It is better to trigger a use-after free 2913 * and to have the class name most of the time instead of not 2914 * having the class name available. 2915 */ 2916 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key, 2917 "Detected use-after-free of lock class %px/%s\n", 2918 hlock_class(prev), 2919 hlock_class(prev)->name); 2920 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key, 2921 "Detected use-after-free of lock class %px/%s\n", 2922 hlock_class(next), 2923 hlock_class(next)->name); 2924 return 2; 2925 } 2926 2927 /* 2928 * Prove that the new <prev> -> <next> dependency would not 2929 * create a circular dependency in the graph. (We do this by 2930 * a breadth-first search into the graph starting at <next>, 2931 * and check whether we can reach <prev>.) 2932 * 2933 * The search is limited by the size of the circular queue (i.e., 2934 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes 2935 * in the graph whose neighbours are to be checked. 2936 */ 2937 ret = check_noncircular(next, prev, trace); 2938 if (unlikely(bfs_error(ret) || ret == BFS_RMATCH)) 2939 return 0; 2940 2941 if (!check_irq_usage(curr, prev, next)) 2942 return 0; 2943 2944 /* 2945 * Is the <prev> -> <next> dependency already present? 2946 * 2947 * (this may occur even though this is a new chain: consider 2948 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 2949 * chains - the second one will be new, but L1 already has 2950 * L2 added to its dependency list, due to the first chain.) 2951 */ 2952 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { 2953 if (entry->class == hlock_class(next)) { 2954 if (distance == 1) 2955 entry->distance = 1; 2956 entry->dep |= calc_dep(prev, next); 2957 2958 /* 2959 * Also, update the reverse dependency in @next's 2960 * ->locks_before list. 2961 * 2962 * Here we reuse @entry as the cursor, which is fine 2963 * because we won't go to the next iteration of the 2964 * outer loop: 2965 * 2966 * For normal cases, we return in the inner loop. 2967 * 2968 * If we fail to return, we have inconsistency, i.e. 2969 * <prev>::locks_after contains <next> while 2970 * <next>::locks_before doesn't contain <prev>. In 2971 * that case, we return after the inner and indicate 2972 * something is wrong. 2973 */ 2974 list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) { 2975 if (entry->class == hlock_class(prev)) { 2976 if (distance == 1) 2977 entry->distance = 1; 2978 entry->dep |= calc_depb(prev, next); 2979 return 1; 2980 } 2981 } 2982 2983 /* <prev> is not found in <next>::locks_before */ 2984 return 0; 2985 } 2986 } 2987 2988 /* 2989 * Is the <prev> -> <next> link redundant? 2990 */ 2991 ret = check_redundant(prev, next); 2992 if (bfs_error(ret)) 2993 return 0; 2994 else if (ret == BFS_RMATCH) 2995 return 2; 2996 2997 if (!*trace) { 2998 *trace = save_trace(); 2999 if (!*trace) 3000 return 0; 3001 } 3002 3003 /* 3004 * Ok, all validations passed, add the new lock 3005 * to the previous lock's dependency list: 3006 */ 3007 ret = add_lock_to_list(hlock_class(next), hlock_class(prev), 3008 &hlock_class(prev)->locks_after, 3009 next->acquire_ip, distance, 3010 calc_dep(prev, next), 3011 *trace); 3012 3013 if (!ret) 3014 return 0; 3015 3016 ret = add_lock_to_list(hlock_class(prev), hlock_class(next), 3017 &hlock_class(next)->locks_before, 3018 next->acquire_ip, distance, 3019 calc_depb(prev, next), 3020 *trace); 3021 if (!ret) 3022 return 0; 3023 3024 return 2; 3025 } 3026 3027 /* 3028 * Add the dependency to all directly-previous locks that are 'relevant'. 3029 * The ones that are relevant are (in increasing distance from curr): 3030 * all consecutive trylock entries and the final non-trylock entry - or 3031 * the end of this context's lock-chain - whichever comes first. 3032 */ 3033 static int 3034 check_prevs_add(struct task_struct *curr, struct held_lock *next) 3035 { 3036 struct lock_trace *trace = NULL; 3037 int depth = curr->lockdep_depth; 3038 struct held_lock *hlock; 3039 3040 /* 3041 * Debugging checks. 3042 * 3043 * Depth must not be zero for a non-head lock: 3044 */ 3045 if (!depth) 3046 goto out_bug; 3047 /* 3048 * At least two relevant locks must exist for this 3049 * to be a head: 3050 */ 3051 if (curr->held_locks[depth].irq_context != 3052 curr->held_locks[depth-1].irq_context) 3053 goto out_bug; 3054 3055 for (;;) { 3056 u16 distance = curr->lockdep_depth - depth + 1; 3057 hlock = curr->held_locks + depth - 1; 3058 3059 if (hlock->check) { 3060 int ret = check_prev_add(curr, hlock, next, distance, &trace); 3061 if (!ret) 3062 return 0; 3063 3064 /* 3065 * Stop after the first non-trylock entry, 3066 * as non-trylock entries have added their 3067 * own direct dependencies already, so this 3068 * lock is connected to them indirectly: 3069 */ 3070 if (!hlock->trylock) 3071 break; 3072 } 3073 3074 depth--; 3075 /* 3076 * End of lock-stack? 3077 */ 3078 if (!depth) 3079 break; 3080 /* 3081 * Stop the search if we cross into another context: 3082 */ 3083 if (curr->held_locks[depth].irq_context != 3084 curr->held_locks[depth-1].irq_context) 3085 break; 3086 } 3087 return 1; 3088 out_bug: 3089 if (!debug_locks_off_graph_unlock()) 3090 return 0; 3091 3092 /* 3093 * Clearly we all shouldn't be here, but since we made it we 3094 * can reliable say we messed up our state. See the above two 3095 * gotos for reasons why we could possibly end up here. 3096 */ 3097 WARN_ON(1); 3098 3099 return 0; 3100 } 3101 3102 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; 3103 static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS); 3104 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; 3105 unsigned long nr_zapped_lock_chains; 3106 unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */ 3107 unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */ 3108 unsigned int nr_large_chain_blocks; /* size > MAX_CHAIN_BUCKETS */ 3109 3110 /* 3111 * The first 2 chain_hlocks entries in the chain block in the bucket 3112 * list contains the following meta data: 3113 * 3114 * entry[0]: 3115 * Bit 15 - always set to 1 (it is not a class index) 3116 * Bits 0-14 - upper 15 bits of the next block index 3117 * entry[1] - lower 16 bits of next block index 3118 * 3119 * A next block index of all 1 bits means it is the end of the list. 3120 * 3121 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain 3122 * the chain block size: 3123 * 3124 * entry[2] - upper 16 bits of the chain block size 3125 * entry[3] - lower 16 bits of the chain block size 3126 */ 3127 #define MAX_CHAIN_BUCKETS 16 3128 #define CHAIN_BLK_FLAG (1U << 15) 3129 #define CHAIN_BLK_LIST_END 0xFFFFU 3130 3131 static int chain_block_buckets[MAX_CHAIN_BUCKETS]; 3132 3133 static inline int size_to_bucket(int size) 3134 { 3135 if (size > MAX_CHAIN_BUCKETS) 3136 return 0; 3137 3138 return size - 1; 3139 } 3140 3141 /* 3142 * Iterate all the chain blocks in a bucket. 3143 */ 3144 #define for_each_chain_block(bucket, prev, curr) \ 3145 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \ 3146 (curr) >= 0; \ 3147 (prev) = (curr), (curr) = chain_block_next(curr)) 3148 3149 /* 3150 * next block or -1 3151 */ 3152 static inline int chain_block_next(int offset) 3153 { 3154 int next = chain_hlocks[offset]; 3155 3156 WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG)); 3157 3158 if (next == CHAIN_BLK_LIST_END) 3159 return -1; 3160 3161 next &= ~CHAIN_BLK_FLAG; 3162 next <<= 16; 3163 next |= chain_hlocks[offset + 1]; 3164 3165 return next; 3166 } 3167 3168 /* 3169 * bucket-0 only 3170 */ 3171 static inline int chain_block_size(int offset) 3172 { 3173 return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3]; 3174 } 3175 3176 static inline void init_chain_block(int offset, int next, int bucket, int size) 3177 { 3178 chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG; 3179 chain_hlocks[offset + 1] = (u16)next; 3180 3181 if (size && !bucket) { 3182 chain_hlocks[offset + 2] = size >> 16; 3183 chain_hlocks[offset + 3] = (u16)size; 3184 } 3185 } 3186 3187 static inline void add_chain_block(int offset, int size) 3188 { 3189 int bucket = size_to_bucket(size); 3190 int next = chain_block_buckets[bucket]; 3191 int prev, curr; 3192 3193 if (unlikely(size < 2)) { 3194 /* 3195 * We can't store single entries on the freelist. Leak them. 3196 * 3197 * One possible way out would be to uniquely mark them, other 3198 * than with CHAIN_BLK_FLAG, such that we can recover them when 3199 * the block before it is re-added. 3200 */ 3201 if (size) 3202 nr_lost_chain_hlocks++; 3203 return; 3204 } 3205 3206 nr_free_chain_hlocks += size; 3207 if (!bucket) { 3208 nr_large_chain_blocks++; 3209 3210 /* 3211 * Variable sized, sort large to small. 3212 */ 3213 for_each_chain_block(0, prev, curr) { 3214 if (size >= chain_block_size(curr)) 3215 break; 3216 } 3217 init_chain_block(offset, curr, 0, size); 3218 if (prev < 0) 3219 chain_block_buckets[0] = offset; 3220 else 3221 init_chain_block(prev, offset, 0, 0); 3222 return; 3223 } 3224 /* 3225 * Fixed size, add to head. 3226 */ 3227 init_chain_block(offset, next, bucket, size); 3228 chain_block_buckets[bucket] = offset; 3229 } 3230 3231 /* 3232 * Only the first block in the list can be deleted. 3233 * 3234 * For the variable size bucket[0], the first block (the largest one) is 3235 * returned, broken up and put back into the pool. So if a chain block of 3236 * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be 3237 * queued up after the primordial chain block and never be used until the 3238 * hlock entries in the primordial chain block is almost used up. That 3239 * causes fragmentation and reduce allocation efficiency. That can be 3240 * monitored by looking at the "large chain blocks" number in lockdep_stats. 3241 */ 3242 static inline void del_chain_block(int bucket, int size, int next) 3243 { 3244 nr_free_chain_hlocks -= size; 3245 chain_block_buckets[bucket] = next; 3246 3247 if (!bucket) 3248 nr_large_chain_blocks--; 3249 } 3250 3251 static void init_chain_block_buckets(void) 3252 { 3253 int i; 3254 3255 for (i = 0; i < MAX_CHAIN_BUCKETS; i++) 3256 chain_block_buckets[i] = -1; 3257 3258 add_chain_block(0, ARRAY_SIZE(chain_hlocks)); 3259 } 3260 3261 /* 3262 * Return offset of a chain block of the right size or -1 if not found. 3263 * 3264 * Fairly simple worst-fit allocator with the addition of a number of size 3265 * specific free lists. 3266 */ 3267 static int alloc_chain_hlocks(int req) 3268 { 3269 int bucket, curr, size; 3270 3271 /* 3272 * We rely on the MSB to act as an escape bit to denote freelist 3273 * pointers. Make sure this bit isn't set in 'normal' class_idx usage. 3274 */ 3275 BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG); 3276 3277 init_data_structures_once(); 3278 3279 if (nr_free_chain_hlocks < req) 3280 return -1; 3281 3282 /* 3283 * We require a minimum of 2 (u16) entries to encode a freelist 3284 * 'pointer'. 3285 */ 3286 req = max(req, 2); 3287 bucket = size_to_bucket(req); 3288 curr = chain_block_buckets[bucket]; 3289 3290 if (bucket) { 3291 if (curr >= 0) { 3292 del_chain_block(bucket, req, chain_block_next(curr)); 3293 return curr; 3294 } 3295 /* Try bucket 0 */ 3296 curr = chain_block_buckets[0]; 3297 } 3298 3299 /* 3300 * The variable sized freelist is sorted by size; the first entry is 3301 * the largest. Use it if it fits. 3302 */ 3303 if (curr >= 0) { 3304 size = chain_block_size(curr); 3305 if (likely(size >= req)) { 3306 del_chain_block(0, size, chain_block_next(curr)); 3307 add_chain_block(curr + req, size - req); 3308 return curr; 3309 } 3310 } 3311 3312 /* 3313 * Last resort, split a block in a larger sized bucket. 3314 */ 3315 for (size = MAX_CHAIN_BUCKETS; size > req; size--) { 3316 bucket = size_to_bucket(size); 3317 curr = chain_block_buckets[bucket]; 3318 if (curr < 0) 3319 continue; 3320 3321 del_chain_block(bucket, size, chain_block_next(curr)); 3322 add_chain_block(curr + req, size - req); 3323 return curr; 3324 } 3325 3326 return -1; 3327 } 3328 3329 static inline void free_chain_hlocks(int base, int size) 3330 { 3331 add_chain_block(base, max(size, 2)); 3332 } 3333 3334 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i) 3335 { 3336 u16 chain_hlock = chain_hlocks[chain->base + i]; 3337 unsigned int class_idx = chain_hlock_class_idx(chain_hlock); 3338 3339 return lock_classes + class_idx - 1; 3340 } 3341 3342 /* 3343 * Returns the index of the first held_lock of the current chain 3344 */ 3345 static inline int get_first_held_lock(struct task_struct *curr, 3346 struct held_lock *hlock) 3347 { 3348 int i; 3349 struct held_lock *hlock_curr; 3350 3351 for (i = curr->lockdep_depth - 1; i >= 0; i--) { 3352 hlock_curr = curr->held_locks + i; 3353 if (hlock_curr->irq_context != hlock->irq_context) 3354 break; 3355 3356 } 3357 3358 return ++i; 3359 } 3360 3361 #ifdef CONFIG_DEBUG_LOCKDEP 3362 /* 3363 * Returns the next chain_key iteration 3364 */ 3365 static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key) 3366 { 3367 u64 new_chain_key = iterate_chain_key(chain_key, hlock_id); 3368 3369 printk(" hlock_id:%d -> chain_key:%016Lx", 3370 (unsigned int)hlock_id, 3371 (unsigned long long)new_chain_key); 3372 return new_chain_key; 3373 } 3374 3375 static void 3376 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next) 3377 { 3378 struct held_lock *hlock; 3379 u64 chain_key = INITIAL_CHAIN_KEY; 3380 int depth = curr->lockdep_depth; 3381 int i = get_first_held_lock(curr, hlock_next); 3382 3383 printk("depth: %u (irq_context %u)\n", depth - i + 1, 3384 hlock_next->irq_context); 3385 for (; i < depth; i++) { 3386 hlock = curr->held_locks + i; 3387 chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key); 3388 3389 print_lock(hlock); 3390 } 3391 3392 print_chain_key_iteration(hlock_id(hlock_next), chain_key); 3393 print_lock(hlock_next); 3394 } 3395 3396 static void print_chain_keys_chain(struct lock_chain *chain) 3397 { 3398 int i; 3399 u64 chain_key = INITIAL_CHAIN_KEY; 3400 u16 hlock_id; 3401 3402 printk("depth: %u\n", chain->depth); 3403 for (i = 0; i < chain->depth; i++) { 3404 hlock_id = chain_hlocks[chain->base + i]; 3405 chain_key = print_chain_key_iteration(hlock_id, chain_key); 3406 3407 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id) - 1); 3408 printk("\n"); 3409 } 3410 } 3411 3412 static void print_collision(struct task_struct *curr, 3413 struct held_lock *hlock_next, 3414 struct lock_chain *chain) 3415 { 3416 pr_warn("\n"); 3417 pr_warn("============================\n"); 3418 pr_warn("WARNING: chain_key collision\n"); 3419 print_kernel_ident(); 3420 pr_warn("----------------------------\n"); 3421 pr_warn("%s/%d: ", current->comm, task_pid_nr(current)); 3422 pr_warn("Hash chain already cached but the contents don't match!\n"); 3423 3424 pr_warn("Held locks:"); 3425 print_chain_keys_held_locks(curr, hlock_next); 3426 3427 pr_warn("Locks in cached chain:"); 3428 print_chain_keys_chain(chain); 3429 3430 pr_warn("\nstack backtrace:\n"); 3431 dump_stack(); 3432 } 3433 #endif 3434 3435 /* 3436 * Checks whether the chain and the current held locks are consistent 3437 * in depth and also in content. If they are not it most likely means 3438 * that there was a collision during the calculation of the chain_key. 3439 * Returns: 0 not passed, 1 passed 3440 */ 3441 static int check_no_collision(struct task_struct *curr, 3442 struct held_lock *hlock, 3443 struct lock_chain *chain) 3444 { 3445 #ifdef CONFIG_DEBUG_LOCKDEP 3446 int i, j, id; 3447 3448 i = get_first_held_lock(curr, hlock); 3449 3450 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { 3451 print_collision(curr, hlock, chain); 3452 return 0; 3453 } 3454 3455 for (j = 0; j < chain->depth - 1; j++, i++) { 3456 id = hlock_id(&curr->held_locks[i]); 3457 3458 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { 3459 print_collision(curr, hlock, chain); 3460 return 0; 3461 } 3462 } 3463 #endif 3464 return 1; 3465 } 3466 3467 /* 3468 * Given an index that is >= -1, return the index of the next lock chain. 3469 * Return -2 if there is no next lock chain. 3470 */ 3471 long lockdep_next_lockchain(long i) 3472 { 3473 i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1); 3474 return i < ARRAY_SIZE(lock_chains) ? i : -2; 3475 } 3476 3477 unsigned long lock_chain_count(void) 3478 { 3479 return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains)); 3480 } 3481 3482 /* Must be called with the graph lock held. */ 3483 static struct lock_chain *alloc_lock_chain(void) 3484 { 3485 int idx = find_first_zero_bit(lock_chains_in_use, 3486 ARRAY_SIZE(lock_chains)); 3487 3488 if (unlikely(idx >= ARRAY_SIZE(lock_chains))) 3489 return NULL; 3490 __set_bit(idx, lock_chains_in_use); 3491 return lock_chains + idx; 3492 } 3493 3494 /* 3495 * Adds a dependency chain into chain hashtable. And must be called with 3496 * graph_lock held. 3497 * 3498 * Return 0 if fail, and graph_lock is released. 3499 * Return 1 if succeed, with graph_lock held. 3500 */ 3501 static inline int add_chain_cache(struct task_struct *curr, 3502 struct held_lock *hlock, 3503 u64 chain_key) 3504 { 3505 struct hlist_head *hash_head = chainhashentry(chain_key); 3506 struct lock_chain *chain; 3507 int i, j; 3508 3509 /* 3510 * The caller must hold the graph lock, ensure we've got IRQs 3511 * disabled to make this an IRQ-safe lock.. for recursion reasons 3512 * lockdep won't complain about its own locking errors. 3513 */ 3514 if (lockdep_assert_locked()) 3515 return 0; 3516 3517 chain = alloc_lock_chain(); 3518 if (!chain) { 3519 if (!debug_locks_off_graph_unlock()) 3520 return 0; 3521 3522 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); 3523 dump_stack(); 3524 return 0; 3525 } 3526 chain->chain_key = chain_key; 3527 chain->irq_context = hlock->irq_context; 3528 i = get_first_held_lock(curr, hlock); 3529 chain->depth = curr->lockdep_depth + 1 - i; 3530 3531 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks)); 3532 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks)); 3533 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes)); 3534 3535 j = alloc_chain_hlocks(chain->depth); 3536 if (j < 0) { 3537 if (!debug_locks_off_graph_unlock()) 3538 return 0; 3539 3540 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); 3541 dump_stack(); 3542 return 0; 3543 } 3544 3545 chain->base = j; 3546 for (j = 0; j < chain->depth - 1; j++, i++) { 3547 int lock_id = hlock_id(curr->held_locks + i); 3548 3549 chain_hlocks[chain->base + j] = lock_id; 3550 } 3551 chain_hlocks[chain->base + j] = hlock_id(hlock); 3552 hlist_add_head_rcu(&chain->entry, hash_head); 3553 debug_atomic_inc(chain_lookup_misses); 3554 inc_chains(chain->irq_context); 3555 3556 return 1; 3557 } 3558 3559 /* 3560 * Look up a dependency chain. Must be called with either the graph lock or 3561 * the RCU read lock held. 3562 */ 3563 static inline struct lock_chain *lookup_chain_cache(u64 chain_key) 3564 { 3565 struct hlist_head *hash_head = chainhashentry(chain_key); 3566 struct lock_chain *chain; 3567 3568 hlist_for_each_entry_rcu(chain, hash_head, entry) { 3569 if (READ_ONCE(chain->chain_key) == chain_key) { 3570 debug_atomic_inc(chain_lookup_hits); 3571 return chain; 3572 } 3573 } 3574 return NULL; 3575 } 3576 3577 /* 3578 * If the key is not present yet in dependency chain cache then 3579 * add it and return 1 - in this case the new dependency chain is 3580 * validated. If the key is already hashed, return 0. 3581 * (On return with 1 graph_lock is held.) 3582 */ 3583 static inline int lookup_chain_cache_add(struct task_struct *curr, 3584 struct held_lock *hlock, 3585 u64 chain_key) 3586 { 3587 struct lock_class *class = hlock_class(hlock); 3588 struct lock_chain *chain = lookup_chain_cache(chain_key); 3589 3590 if (chain) { 3591 cache_hit: 3592 if (!check_no_collision(curr, hlock, chain)) 3593 return 0; 3594 3595 if (very_verbose(class)) { 3596 printk("\nhash chain already cached, key: " 3597 "%016Lx tail class: [%px] %s\n", 3598 (unsigned long long)chain_key, 3599 class->key, class->name); 3600 } 3601 3602 return 0; 3603 } 3604 3605 if (very_verbose(class)) { 3606 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n", 3607 (unsigned long long)chain_key, class->key, class->name); 3608 } 3609 3610 if (!graph_lock()) 3611 return 0; 3612 3613 /* 3614 * We have to walk the chain again locked - to avoid duplicates: 3615 */ 3616 chain = lookup_chain_cache(chain_key); 3617 if (chain) { 3618 graph_unlock(); 3619 goto cache_hit; 3620 } 3621 3622 if (!add_chain_cache(curr, hlock, chain_key)) 3623 return 0; 3624 3625 return 1; 3626 } 3627 3628 static int validate_chain(struct task_struct *curr, 3629 struct held_lock *hlock, 3630 int chain_head, u64 chain_key) 3631 { 3632 /* 3633 * Trylock needs to maintain the stack of held locks, but it 3634 * does not add new dependencies, because trylock can be done 3635 * in any order. 3636 * 3637 * We look up the chain_key and do the O(N^2) check and update of 3638 * the dependencies only if this is a new dependency chain. 3639 * (If lookup_chain_cache_add() return with 1 it acquires 3640 * graph_lock for us) 3641 */ 3642 if (!hlock->trylock && hlock->check && 3643 lookup_chain_cache_add(curr, hlock, chain_key)) { 3644 /* 3645 * Check whether last held lock: 3646 * 3647 * - is irq-safe, if this lock is irq-unsafe 3648 * - is softirq-safe, if this lock is hardirq-unsafe 3649 * 3650 * And check whether the new lock's dependency graph 3651 * could lead back to the previous lock: 3652 * 3653 * - within the current held-lock stack 3654 * - across our accumulated lock dependency records 3655 * 3656 * any of these scenarios could lead to a deadlock. 3657 */ 3658 /* 3659 * The simple case: does the current hold the same lock 3660 * already? 3661 */ 3662 int ret = check_deadlock(curr, hlock); 3663 3664 if (!ret) 3665 return 0; 3666 /* 3667 * Add dependency only if this lock is not the head 3668 * of the chain, and if the new lock introduces no more 3669 * lock dependency (because we already hold a lock with the 3670 * same lock class) nor deadlock (because the nest_lock 3671 * serializes nesting locks), see the comments for 3672 * check_deadlock(). 3673 */ 3674 if (!chain_head && ret != 2) { 3675 if (!check_prevs_add(curr, hlock)) 3676 return 0; 3677 } 3678 3679 graph_unlock(); 3680 } else { 3681 /* after lookup_chain_cache_add(): */ 3682 if (unlikely(!debug_locks)) 3683 return 0; 3684 } 3685 3686 return 1; 3687 } 3688 #else 3689 static inline int validate_chain(struct task_struct *curr, 3690 struct held_lock *hlock, 3691 int chain_head, u64 chain_key) 3692 { 3693 return 1; 3694 } 3695 3696 static void init_chain_block_buckets(void) { } 3697 #endif /* CONFIG_PROVE_LOCKING */ 3698 3699 /* 3700 * We are building curr_chain_key incrementally, so double-check 3701 * it from scratch, to make sure that it's done correctly: 3702 */ 3703 static void check_chain_key(struct task_struct *curr) 3704 { 3705 #ifdef CONFIG_DEBUG_LOCKDEP 3706 struct held_lock *hlock, *prev_hlock = NULL; 3707 unsigned int i; 3708 u64 chain_key = INITIAL_CHAIN_KEY; 3709 3710 for (i = 0; i < curr->lockdep_depth; i++) { 3711 hlock = curr->held_locks + i; 3712 if (chain_key != hlock->prev_chain_key) { 3713 debug_locks_off(); 3714 /* 3715 * We got mighty confused, our chain keys don't match 3716 * with what we expect, someone trample on our task state? 3717 */ 3718 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", 3719 curr->lockdep_depth, i, 3720 (unsigned long long)chain_key, 3721 (unsigned long long)hlock->prev_chain_key); 3722 return; 3723 } 3724 3725 /* 3726 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is 3727 * it registered lock class index? 3728 */ 3729 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use))) 3730 return; 3731 3732 if (prev_hlock && (prev_hlock->irq_context != 3733 hlock->irq_context)) 3734 chain_key = INITIAL_CHAIN_KEY; 3735 chain_key = iterate_chain_key(chain_key, hlock_id(hlock)); 3736 prev_hlock = hlock; 3737 } 3738 if (chain_key != curr->curr_chain_key) { 3739 debug_locks_off(); 3740 /* 3741 * More smoking hash instead of calculating it, damn see these 3742 * numbers float.. I bet that a pink elephant stepped on my memory. 3743 */ 3744 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", 3745 curr->lockdep_depth, i, 3746 (unsigned long long)chain_key, 3747 (unsigned long long)curr->curr_chain_key); 3748 } 3749 #endif 3750 } 3751 3752 #ifdef CONFIG_PROVE_LOCKING 3753 static int mark_lock(struct task_struct *curr, struct held_lock *this, 3754 enum lock_usage_bit new_bit); 3755 3756 static void print_usage_bug_scenario(struct held_lock *lock) 3757 { 3758 struct lock_class *class = hlock_class(lock); 3759 3760 printk(" Possible unsafe locking scenario:\n\n"); 3761 printk(" CPU0\n"); 3762 printk(" ----\n"); 3763 printk(" lock("); 3764 __print_lock_name(class); 3765 printk(KERN_CONT ");\n"); 3766 printk(" <Interrupt>\n"); 3767 printk(" lock("); 3768 __print_lock_name(class); 3769 printk(KERN_CONT ");\n"); 3770 printk("\n *** DEADLOCK ***\n\n"); 3771 } 3772 3773 static void 3774 print_usage_bug(struct task_struct *curr, struct held_lock *this, 3775 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit) 3776 { 3777 if (!debug_locks_off() || debug_locks_silent) 3778 return; 3779 3780 pr_warn("\n"); 3781 pr_warn("================================\n"); 3782 pr_warn("WARNING: inconsistent lock state\n"); 3783 print_kernel_ident(); 3784 pr_warn("--------------------------------\n"); 3785 3786 pr_warn("inconsistent {%s} -> {%s} usage.\n", 3787 usage_str[prev_bit], usage_str[new_bit]); 3788 3789 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n", 3790 curr->comm, task_pid_nr(curr), 3791 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT, 3792 lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, 3793 lockdep_hardirqs_enabled(), 3794 lockdep_softirqs_enabled(curr)); 3795 print_lock(this); 3796 3797 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]); 3798 print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1); 3799 3800 print_irqtrace_events(curr); 3801 pr_warn("\nother info that might help us debug this:\n"); 3802 print_usage_bug_scenario(this); 3803 3804 lockdep_print_held_locks(curr); 3805 3806 pr_warn("\nstack backtrace:\n"); 3807 dump_stack(); 3808 } 3809 3810 /* 3811 * Print out an error if an invalid bit is set: 3812 */ 3813 static inline int 3814 valid_state(struct task_struct *curr, struct held_lock *this, 3815 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) 3816 { 3817 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) { 3818 graph_unlock(); 3819 print_usage_bug(curr, this, bad_bit, new_bit); 3820 return 0; 3821 } 3822 return 1; 3823 } 3824 3825 3826 /* 3827 * print irq inversion bug: 3828 */ 3829 static void 3830 print_irq_inversion_bug(struct task_struct *curr, 3831 struct lock_list *root, struct lock_list *other, 3832 struct held_lock *this, int forwards, 3833 const char *irqclass) 3834 { 3835 struct lock_list *entry = other; 3836 struct lock_list *middle = NULL; 3837 int depth; 3838 3839 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 3840 return; 3841 3842 pr_warn("\n"); 3843 pr_warn("========================================================\n"); 3844 pr_warn("WARNING: possible irq lock inversion dependency detected\n"); 3845 print_kernel_ident(); 3846 pr_warn("--------------------------------------------------------\n"); 3847 pr_warn("%s/%d just changed the state of lock:\n", 3848 curr->comm, task_pid_nr(curr)); 3849 print_lock(this); 3850 if (forwards) 3851 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass); 3852 else 3853 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); 3854 print_lock_name(other->class); 3855 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n"); 3856 3857 pr_warn("\nother info that might help us debug this:\n"); 3858 3859 /* Find a middle lock (if one exists) */ 3860 depth = get_lock_depth(other); 3861 do { 3862 if (depth == 0 && (entry != root)) { 3863 pr_warn("lockdep:%s bad path found in chain graph\n", __func__); 3864 break; 3865 } 3866 middle = entry; 3867 entry = get_lock_parent(entry); 3868 depth--; 3869 } while (entry && entry != root && (depth >= 0)); 3870 if (forwards) 3871 print_irq_lock_scenario(root, other, 3872 middle ? middle->class : root->class, other->class); 3873 else 3874 print_irq_lock_scenario(other, root, 3875 middle ? middle->class : other->class, root->class); 3876 3877 lockdep_print_held_locks(curr); 3878 3879 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); 3880 root->trace = save_trace(); 3881 if (!root->trace) 3882 return; 3883 print_shortest_lock_dependencies(other, root); 3884 3885 pr_warn("\nstack backtrace:\n"); 3886 dump_stack(); 3887 } 3888 3889 /* 3890 * Prove that in the forwards-direction subgraph starting at <this> 3891 * there is no lock matching <mask>: 3892 */ 3893 static int 3894 check_usage_forwards(struct task_struct *curr, struct held_lock *this, 3895 enum lock_usage_bit bit) 3896 { 3897 enum bfs_result ret; 3898 struct lock_list root; 3899 struct lock_list *target_entry; 3900 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK; 3901 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit); 3902 3903 bfs_init_root(&root, this); 3904 ret = find_usage_forwards(&root, usage_mask, &target_entry); 3905 if (bfs_error(ret)) { 3906 print_bfs_bug(ret); 3907 return 0; 3908 } 3909 if (ret == BFS_RNOMATCH) 3910 return 1; 3911 3912 /* Check whether write or read usage is the match */ 3913 if (target_entry->class->usage_mask & lock_flag(bit)) { 3914 print_irq_inversion_bug(curr, &root, target_entry, 3915 this, 1, state_name(bit)); 3916 } else { 3917 print_irq_inversion_bug(curr, &root, target_entry, 3918 this, 1, state_name(read_bit)); 3919 } 3920 3921 return 0; 3922 } 3923 3924 /* 3925 * Prove that in the backwards-direction subgraph starting at <this> 3926 * there is no lock matching <mask>: 3927 */ 3928 static int 3929 check_usage_backwards(struct task_struct *curr, struct held_lock *this, 3930 enum lock_usage_bit bit) 3931 { 3932 enum bfs_result ret; 3933 struct lock_list root; 3934 struct lock_list *target_entry; 3935 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK; 3936 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit); 3937 3938 bfs_init_rootb(&root, this); 3939 ret = find_usage_backwards(&root, usage_mask, &target_entry); 3940 if (bfs_error(ret)) { 3941 print_bfs_bug(ret); 3942 return 0; 3943 } 3944 if (ret == BFS_RNOMATCH) 3945 return 1; 3946 3947 /* Check whether write or read usage is the match */ 3948 if (target_entry->class->usage_mask & lock_flag(bit)) { 3949 print_irq_inversion_bug(curr, &root, target_entry, 3950 this, 0, state_name(bit)); 3951 } else { 3952 print_irq_inversion_bug(curr, &root, target_entry, 3953 this, 0, state_name(read_bit)); 3954 } 3955 3956 return 0; 3957 } 3958 3959 void print_irqtrace_events(struct task_struct *curr) 3960 { 3961 const struct irqtrace_events *trace = &curr->irqtrace; 3962 3963 printk("irq event stamp: %u\n", trace->irq_events); 3964 printk("hardirqs last enabled at (%u): [<%px>] %pS\n", 3965 trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip, 3966 (void *)trace->hardirq_enable_ip); 3967 printk("hardirqs last disabled at (%u): [<%px>] %pS\n", 3968 trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip, 3969 (void *)trace->hardirq_disable_ip); 3970 printk("softirqs last enabled at (%u): [<%px>] %pS\n", 3971 trace->softirq_enable_event, (void *)trace->softirq_enable_ip, 3972 (void *)trace->softirq_enable_ip); 3973 printk("softirqs last disabled at (%u): [<%px>] %pS\n", 3974 trace->softirq_disable_event, (void *)trace->softirq_disable_ip, 3975 (void *)trace->softirq_disable_ip); 3976 } 3977 3978 static int HARDIRQ_verbose(struct lock_class *class) 3979 { 3980 #if HARDIRQ_VERBOSE 3981 return class_filter(class); 3982 #endif 3983 return 0; 3984 } 3985 3986 static int SOFTIRQ_verbose(struct lock_class *class) 3987 { 3988 #if SOFTIRQ_VERBOSE 3989 return class_filter(class); 3990 #endif 3991 return 0; 3992 } 3993 3994 static int (*state_verbose_f[])(struct lock_class *class) = { 3995 #define LOCKDEP_STATE(__STATE) \ 3996 __STATE##_verbose, 3997 #include "lockdep_states.h" 3998 #undef LOCKDEP_STATE 3999 }; 4000 4001 static inline int state_verbose(enum lock_usage_bit bit, 4002 struct lock_class *class) 4003 { 4004 return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class); 4005 } 4006 4007 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, 4008 enum lock_usage_bit bit, const char *name); 4009 4010 static int 4011 mark_lock_irq(struct task_struct *curr, struct held_lock *this, 4012 enum lock_usage_bit new_bit) 4013 { 4014 int excl_bit = exclusive_bit(new_bit); 4015 int read = new_bit & LOCK_USAGE_READ_MASK; 4016 int dir = new_bit & LOCK_USAGE_DIR_MASK; 4017 4018 /* 4019 * Validate that this particular lock does not have conflicting 4020 * usage states. 4021 */ 4022 if (!valid_state(curr, this, new_bit, excl_bit)) 4023 return 0; 4024 4025 /* 4026 * Check for read in write conflicts 4027 */ 4028 if (!read && !valid_state(curr, this, new_bit, 4029 excl_bit + LOCK_USAGE_READ_MASK)) 4030 return 0; 4031 4032 4033 /* 4034 * Validate that the lock dependencies don't have conflicting usage 4035 * states. 4036 */ 4037 if (dir) { 4038 /* 4039 * mark ENABLED has to look backwards -- to ensure no dependee 4040 * has USED_IN state, which, again, would allow recursion deadlocks. 4041 */ 4042 if (!check_usage_backwards(curr, this, excl_bit)) 4043 return 0; 4044 } else { 4045 /* 4046 * mark USED_IN has to look forwards -- to ensure no dependency 4047 * has ENABLED state, which would allow recursion deadlocks. 4048 */ 4049 if (!check_usage_forwards(curr, this, excl_bit)) 4050 return 0; 4051 } 4052 4053 if (state_verbose(new_bit, hlock_class(this))) 4054 return 2; 4055 4056 return 1; 4057 } 4058 4059 /* 4060 * Mark all held locks with a usage bit: 4061 */ 4062 static int 4063 mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit) 4064 { 4065 struct held_lock *hlock; 4066 int i; 4067 4068 for (i = 0; i < curr->lockdep_depth; i++) { 4069 enum lock_usage_bit hlock_bit = base_bit; 4070 hlock = curr->held_locks + i; 4071 4072 if (hlock->read) 4073 hlock_bit += LOCK_USAGE_READ_MASK; 4074 4075 BUG_ON(hlock_bit >= LOCK_USAGE_STATES); 4076 4077 if (!hlock->check) 4078 continue; 4079 4080 if (!mark_lock(curr, hlock, hlock_bit)) 4081 return 0; 4082 } 4083 4084 return 1; 4085 } 4086 4087 /* 4088 * Hardirqs will be enabled: 4089 */ 4090 static void __trace_hardirqs_on_caller(void) 4091 { 4092 struct task_struct *curr = current; 4093 4094 /* 4095 * We are going to turn hardirqs on, so set the 4096 * usage bit for all held locks: 4097 */ 4098 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ)) 4099 return; 4100 /* 4101 * If we have softirqs enabled, then set the usage 4102 * bit for all held locks. (disabled hardirqs prevented 4103 * this bit from being set before) 4104 */ 4105 if (curr->softirqs_enabled) 4106 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ); 4107 } 4108 4109 /** 4110 * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts 4111 * @ip: Caller address 4112 * 4113 * Invoked before a possible transition to RCU idle from exit to user or 4114 * guest mode. This ensures that all RCU operations are done before RCU 4115 * stops watching. After the RCU transition lockdep_hardirqs_on() has to be 4116 * invoked to set the final state. 4117 */ 4118 void lockdep_hardirqs_on_prepare(unsigned long ip) 4119 { 4120 if (unlikely(!debug_locks)) 4121 return; 4122 4123 /* 4124 * NMIs do not (and cannot) track lock dependencies, nothing to do. 4125 */ 4126 if (unlikely(in_nmi())) 4127 return; 4128 4129 if (unlikely(this_cpu_read(lockdep_recursion))) 4130 return; 4131 4132 if (unlikely(lockdep_hardirqs_enabled())) { 4133 /* 4134 * Neither irq nor preemption are disabled here 4135 * so this is racy by nature but losing one hit 4136 * in a stat is not a big deal. 4137 */ 4138 __debug_atomic_inc(redundant_hardirqs_on); 4139 return; 4140 } 4141 4142 /* 4143 * We're enabling irqs and according to our state above irqs weren't 4144 * already enabled, yet we find the hardware thinks they are in fact 4145 * enabled.. someone messed up their IRQ state tracing. 4146 */ 4147 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 4148 return; 4149 4150 /* 4151 * See the fine text that goes along with this variable definition. 4152 */ 4153 if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled)) 4154 return; 4155 4156 /* 4157 * Can't allow enabling interrupts while in an interrupt handler, 4158 * that's general bad form and such. Recursion, limited stack etc.. 4159 */ 4160 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context())) 4161 return; 4162 4163 current->hardirq_chain_key = current->curr_chain_key; 4164 4165 lockdep_recursion_inc(); 4166 __trace_hardirqs_on_caller(); 4167 lockdep_recursion_finish(); 4168 } 4169 EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare); 4170 4171 void noinstr lockdep_hardirqs_on(unsigned long ip) 4172 { 4173 struct irqtrace_events *trace = ¤t->irqtrace; 4174 4175 if (unlikely(!debug_locks)) 4176 return; 4177 4178 /* 4179 * NMIs can happen in the middle of local_irq_{en,dis}able() where the 4180 * tracking state and hardware state are out of sync. 4181 * 4182 * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from, 4183 * and not rely on hardware state like normal interrupts. 4184 */ 4185 if (unlikely(in_nmi())) { 4186 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI)) 4187 return; 4188 4189 /* 4190 * Skip: 4191 * - recursion check, because NMI can hit lockdep; 4192 * - hardware state check, because above; 4193 * - chain_key check, see lockdep_hardirqs_on_prepare(). 4194 */ 4195 goto skip_checks; 4196 } 4197 4198 if (unlikely(this_cpu_read(lockdep_recursion))) 4199 return; 4200 4201 if (lockdep_hardirqs_enabled()) { 4202 /* 4203 * Neither irq nor preemption are disabled here 4204 * so this is racy by nature but losing one hit 4205 * in a stat is not a big deal. 4206 */ 4207 __debug_atomic_inc(redundant_hardirqs_on); 4208 return; 4209 } 4210 4211 /* 4212 * We're enabling irqs and according to our state above irqs weren't 4213 * already enabled, yet we find the hardware thinks they are in fact 4214 * enabled.. someone messed up their IRQ state tracing. 4215 */ 4216 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 4217 return; 4218 4219 /* 4220 * Ensure the lock stack remained unchanged between 4221 * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on(). 4222 */ 4223 DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key != 4224 current->curr_chain_key); 4225 4226 skip_checks: 4227 /* we'll do an OFF -> ON transition: */ 4228 __this_cpu_write(hardirqs_enabled, 1); 4229 trace->hardirq_enable_ip = ip; 4230 trace->hardirq_enable_event = ++trace->irq_events; 4231 debug_atomic_inc(hardirqs_on_events); 4232 } 4233 EXPORT_SYMBOL_GPL(lockdep_hardirqs_on); 4234 4235 /* 4236 * Hardirqs were disabled: 4237 */ 4238 void noinstr lockdep_hardirqs_off(unsigned long ip) 4239 { 4240 if (unlikely(!debug_locks)) 4241 return; 4242 4243 /* 4244 * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep; 4245 * they will restore the software state. This ensures the software 4246 * state is consistent inside NMIs as well. 4247 */ 4248 if (in_nmi()) { 4249 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI)) 4250 return; 4251 } else if (__this_cpu_read(lockdep_recursion)) 4252 return; 4253 4254 /* 4255 * So we're supposed to get called after you mask local IRQs, but for 4256 * some reason the hardware doesn't quite think you did a proper job. 4257 */ 4258 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 4259 return; 4260 4261 if (lockdep_hardirqs_enabled()) { 4262 struct irqtrace_events *trace = ¤t->irqtrace; 4263 4264 /* 4265 * We have done an ON -> OFF transition: 4266 */ 4267 __this_cpu_write(hardirqs_enabled, 0); 4268 trace->hardirq_disable_ip = ip; 4269 trace->hardirq_disable_event = ++trace->irq_events; 4270 debug_atomic_inc(hardirqs_off_events); 4271 } else { 4272 debug_atomic_inc(redundant_hardirqs_off); 4273 } 4274 } 4275 EXPORT_SYMBOL_GPL(lockdep_hardirqs_off); 4276 4277 /* 4278 * Softirqs will be enabled: 4279 */ 4280 void lockdep_softirqs_on(unsigned long ip) 4281 { 4282 struct irqtrace_events *trace = ¤t->irqtrace; 4283 4284 if (unlikely(!lockdep_enabled())) 4285 return; 4286 4287 /* 4288 * We fancy IRQs being disabled here, see softirq.c, avoids 4289 * funny state and nesting things. 4290 */ 4291 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 4292 return; 4293 4294 if (current->softirqs_enabled) { 4295 debug_atomic_inc(redundant_softirqs_on); 4296 return; 4297 } 4298 4299 lockdep_recursion_inc(); 4300 /* 4301 * We'll do an OFF -> ON transition: 4302 */ 4303 current->softirqs_enabled = 1; 4304 trace->softirq_enable_ip = ip; 4305 trace->softirq_enable_event = ++trace->irq_events; 4306 debug_atomic_inc(softirqs_on_events); 4307 /* 4308 * We are going to turn softirqs on, so set the 4309 * usage bit for all held locks, if hardirqs are 4310 * enabled too: 4311 */ 4312 if (lockdep_hardirqs_enabled()) 4313 mark_held_locks(current, LOCK_ENABLED_SOFTIRQ); 4314 lockdep_recursion_finish(); 4315 } 4316 4317 /* 4318 * Softirqs were disabled: 4319 */ 4320 void lockdep_softirqs_off(unsigned long ip) 4321 { 4322 if (unlikely(!lockdep_enabled())) 4323 return; 4324 4325 /* 4326 * We fancy IRQs being disabled here, see softirq.c 4327 */ 4328 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 4329 return; 4330 4331 if (current->softirqs_enabled) { 4332 struct irqtrace_events *trace = ¤t->irqtrace; 4333 4334 /* 4335 * We have done an ON -> OFF transition: 4336 */ 4337 current->softirqs_enabled = 0; 4338 trace->softirq_disable_ip = ip; 4339 trace->softirq_disable_event = ++trace->irq_events; 4340 debug_atomic_inc(softirqs_off_events); 4341 /* 4342 * Whoops, we wanted softirqs off, so why aren't they? 4343 */ 4344 DEBUG_LOCKS_WARN_ON(!softirq_count()); 4345 } else 4346 debug_atomic_inc(redundant_softirqs_off); 4347 } 4348 4349 static int 4350 mark_usage(struct task_struct *curr, struct held_lock *hlock, int check) 4351 { 4352 if (!check) 4353 goto lock_used; 4354 4355 /* 4356 * If non-trylock use in a hardirq or softirq context, then 4357 * mark the lock as used in these contexts: 4358 */ 4359 if (!hlock->trylock) { 4360 if (hlock->read) { 4361 if (lockdep_hardirq_context()) 4362 if (!mark_lock(curr, hlock, 4363 LOCK_USED_IN_HARDIRQ_READ)) 4364 return 0; 4365 if (curr->softirq_context) 4366 if (!mark_lock(curr, hlock, 4367 LOCK_USED_IN_SOFTIRQ_READ)) 4368 return 0; 4369 } else { 4370 if (lockdep_hardirq_context()) 4371 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ)) 4372 return 0; 4373 if (curr->softirq_context) 4374 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ)) 4375 return 0; 4376 } 4377 } 4378 if (!hlock->hardirqs_off) { 4379 if (hlock->read) { 4380 if (!mark_lock(curr, hlock, 4381 LOCK_ENABLED_HARDIRQ_READ)) 4382 return 0; 4383 if (curr->softirqs_enabled) 4384 if (!mark_lock(curr, hlock, 4385 LOCK_ENABLED_SOFTIRQ_READ)) 4386 return 0; 4387 } else { 4388 if (!mark_lock(curr, hlock, 4389 LOCK_ENABLED_HARDIRQ)) 4390 return 0; 4391 if (curr->softirqs_enabled) 4392 if (!mark_lock(curr, hlock, 4393 LOCK_ENABLED_SOFTIRQ)) 4394 return 0; 4395 } 4396 } 4397 4398 lock_used: 4399 /* mark it as used: */ 4400 if (!mark_lock(curr, hlock, LOCK_USED)) 4401 return 0; 4402 4403 return 1; 4404 } 4405 4406 static inline unsigned int task_irq_context(struct task_struct *task) 4407 { 4408 return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() + 4409 LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context; 4410 } 4411 4412 static int separate_irq_context(struct task_struct *curr, 4413 struct held_lock *hlock) 4414 { 4415 unsigned int depth = curr->lockdep_depth; 4416 4417 /* 4418 * Keep track of points where we cross into an interrupt context: 4419 */ 4420 if (depth) { 4421 struct held_lock *prev_hlock; 4422 4423 prev_hlock = curr->held_locks + depth-1; 4424 /* 4425 * If we cross into another context, reset the 4426 * hash key (this also prevents the checking and the 4427 * adding of the dependency to 'prev'): 4428 */ 4429 if (prev_hlock->irq_context != hlock->irq_context) 4430 return 1; 4431 } 4432 return 0; 4433 } 4434 4435 /* 4436 * Mark a lock with a usage bit, and validate the state transition: 4437 */ 4438 static int mark_lock(struct task_struct *curr, struct held_lock *this, 4439 enum lock_usage_bit new_bit) 4440 { 4441 unsigned int new_mask, ret = 1; 4442 4443 if (new_bit >= LOCK_USAGE_STATES) { 4444 DEBUG_LOCKS_WARN_ON(1); 4445 return 0; 4446 } 4447 4448 if (new_bit == LOCK_USED && this->read) 4449 new_bit = LOCK_USED_READ; 4450 4451 new_mask = 1 << new_bit; 4452 4453 /* 4454 * If already set then do not dirty the cacheline, 4455 * nor do any checks: 4456 */ 4457 if (likely(hlock_class(this)->usage_mask & new_mask)) 4458 return 1; 4459 4460 if (!graph_lock()) 4461 return 0; 4462 /* 4463 * Make sure we didn't race: 4464 */ 4465 if (unlikely(hlock_class(this)->usage_mask & new_mask)) 4466 goto unlock; 4467 4468 if (!hlock_class(this)->usage_mask) 4469 debug_atomic_dec(nr_unused_locks); 4470 4471 hlock_class(this)->usage_mask |= new_mask; 4472 4473 if (new_bit < LOCK_TRACE_STATES) { 4474 if (!(hlock_class(this)->usage_traces[new_bit] = save_trace())) 4475 return 0; 4476 } 4477 4478 if (new_bit < LOCK_USED) { 4479 ret = mark_lock_irq(curr, this, new_bit); 4480 if (!ret) 4481 return 0; 4482 } 4483 4484 unlock: 4485 graph_unlock(); 4486 4487 /* 4488 * We must printk outside of the graph_lock: 4489 */ 4490 if (ret == 2) { 4491 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); 4492 print_lock(this); 4493 print_irqtrace_events(curr); 4494 dump_stack(); 4495 } 4496 4497 return ret; 4498 } 4499 4500 static inline short task_wait_context(struct task_struct *curr) 4501 { 4502 /* 4503 * Set appropriate wait type for the context; for IRQs we have to take 4504 * into account force_irqthread as that is implied by PREEMPT_RT. 4505 */ 4506 if (lockdep_hardirq_context()) { 4507 /* 4508 * Check if force_irqthreads will run us threaded. 4509 */ 4510 if (curr->hardirq_threaded || curr->irq_config) 4511 return LD_WAIT_CONFIG; 4512 4513 return LD_WAIT_SPIN; 4514 } else if (curr->softirq_context) { 4515 /* 4516 * Softirqs are always threaded. 4517 */ 4518 return LD_WAIT_CONFIG; 4519 } 4520 4521 return LD_WAIT_MAX; 4522 } 4523 4524 static int 4525 print_lock_invalid_wait_context(struct task_struct *curr, 4526 struct held_lock *hlock) 4527 { 4528 short curr_inner; 4529 4530 if (!debug_locks_off()) 4531 return 0; 4532 if (debug_locks_silent) 4533 return 0; 4534 4535 pr_warn("\n"); 4536 pr_warn("=============================\n"); 4537 pr_warn("[ BUG: Invalid wait context ]\n"); 4538 print_kernel_ident(); 4539 pr_warn("-----------------------------\n"); 4540 4541 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); 4542 print_lock(hlock); 4543 4544 pr_warn("other info that might help us debug this:\n"); 4545 4546 curr_inner = task_wait_context(curr); 4547 pr_warn("context-{%d:%d}\n", curr_inner, curr_inner); 4548 4549 lockdep_print_held_locks(curr); 4550 4551 pr_warn("stack backtrace:\n"); 4552 dump_stack(); 4553 4554 return 0; 4555 } 4556 4557 /* 4558 * Verify the wait_type context. 4559 * 4560 * This check validates we takes locks in the right wait-type order; that is it 4561 * ensures that we do not take mutexes inside spinlocks and do not attempt to 4562 * acquire spinlocks inside raw_spinlocks and the sort. 4563 * 4564 * The entire thing is slightly more complex because of RCU, RCU is a lock that 4565 * can be taken from (pretty much) any context but also has constraints. 4566 * However when taken in a stricter environment the RCU lock does not loosen 4567 * the constraints. 4568 * 4569 * Therefore we must look for the strictest environment in the lock stack and 4570 * compare that to the lock we're trying to acquire. 4571 */ 4572 static int check_wait_context(struct task_struct *curr, struct held_lock *next) 4573 { 4574 u8 next_inner = hlock_class(next)->wait_type_inner; 4575 u8 next_outer = hlock_class(next)->wait_type_outer; 4576 u8 curr_inner; 4577 int depth; 4578 4579 if (!curr->lockdep_depth || !next_inner || next->trylock) 4580 return 0; 4581 4582 if (!next_outer) 4583 next_outer = next_inner; 4584 4585 /* 4586 * Find start of current irq_context.. 4587 */ 4588 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) { 4589 struct held_lock *prev = curr->held_locks + depth; 4590 if (prev->irq_context != next->irq_context) 4591 break; 4592 } 4593 depth++; 4594 4595 curr_inner = task_wait_context(curr); 4596 4597 for (; depth < curr->lockdep_depth; depth++) { 4598 struct held_lock *prev = curr->held_locks + depth; 4599 u8 prev_inner = hlock_class(prev)->wait_type_inner; 4600 4601 if (prev_inner) { 4602 /* 4603 * We can have a bigger inner than a previous one 4604 * when outer is smaller than inner, as with RCU. 4605 * 4606 * Also due to trylocks. 4607 */ 4608 curr_inner = min(curr_inner, prev_inner); 4609 } 4610 } 4611 4612 if (next_outer > curr_inner) 4613 return print_lock_invalid_wait_context(curr, next); 4614 4615 return 0; 4616 } 4617 4618 #else /* CONFIG_PROVE_LOCKING */ 4619 4620 static inline int 4621 mark_usage(struct task_struct *curr, struct held_lock *hlock, int check) 4622 { 4623 return 1; 4624 } 4625 4626 static inline unsigned int task_irq_context(struct task_struct *task) 4627 { 4628 return 0; 4629 } 4630 4631 static inline int separate_irq_context(struct task_struct *curr, 4632 struct held_lock *hlock) 4633 { 4634 return 0; 4635 } 4636 4637 static inline int check_wait_context(struct task_struct *curr, 4638 struct held_lock *next) 4639 { 4640 return 0; 4641 } 4642 4643 #endif /* CONFIG_PROVE_LOCKING */ 4644 4645 /* 4646 * Initialize a lock instance's lock-class mapping info: 4647 */ 4648 void lockdep_init_map_type(struct lockdep_map *lock, const char *name, 4649 struct lock_class_key *key, int subclass, 4650 u8 inner, u8 outer, u8 lock_type) 4651 { 4652 int i; 4653 4654 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++) 4655 lock->class_cache[i] = NULL; 4656 4657 #ifdef CONFIG_LOCK_STAT 4658 lock->cpu = raw_smp_processor_id(); 4659 #endif 4660 4661 /* 4662 * Can't be having no nameless bastards around this place! 4663 */ 4664 if (DEBUG_LOCKS_WARN_ON(!name)) { 4665 lock->name = "NULL"; 4666 return; 4667 } 4668 4669 lock->name = name; 4670 4671 lock->wait_type_outer = outer; 4672 lock->wait_type_inner = inner; 4673 lock->lock_type = lock_type; 4674 4675 /* 4676 * No key, no joy, we need to hash something. 4677 */ 4678 if (DEBUG_LOCKS_WARN_ON(!key)) 4679 return; 4680 /* 4681 * Sanity check, the lock-class key must either have been allocated 4682 * statically or must have been registered as a dynamic key. 4683 */ 4684 if (!static_obj(key) && !is_dynamic_key(key)) { 4685 if (debug_locks) 4686 printk(KERN_ERR "BUG: key %px has not been registered!\n", key); 4687 DEBUG_LOCKS_WARN_ON(1); 4688 return; 4689 } 4690 lock->key = key; 4691 4692 if (unlikely(!debug_locks)) 4693 return; 4694 4695 if (subclass) { 4696 unsigned long flags; 4697 4698 if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled())) 4699 return; 4700 4701 raw_local_irq_save(flags); 4702 lockdep_recursion_inc(); 4703 register_lock_class(lock, subclass, 1); 4704 lockdep_recursion_finish(); 4705 raw_local_irq_restore(flags); 4706 } 4707 } 4708 EXPORT_SYMBOL_GPL(lockdep_init_map_type); 4709 4710 struct lock_class_key __lockdep_no_validate__; 4711 EXPORT_SYMBOL_GPL(__lockdep_no_validate__); 4712 4713 static void 4714 print_lock_nested_lock_not_held(struct task_struct *curr, 4715 struct held_lock *hlock, 4716 unsigned long ip) 4717 { 4718 if (!debug_locks_off()) 4719 return; 4720 if (debug_locks_silent) 4721 return; 4722 4723 pr_warn("\n"); 4724 pr_warn("==================================\n"); 4725 pr_warn("WARNING: Nested lock was not taken\n"); 4726 print_kernel_ident(); 4727 pr_warn("----------------------------------\n"); 4728 4729 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); 4730 print_lock(hlock); 4731 4732 pr_warn("\nbut this task is not holding:\n"); 4733 pr_warn("%s\n", hlock->nest_lock->name); 4734 4735 pr_warn("\nstack backtrace:\n"); 4736 dump_stack(); 4737 4738 pr_warn("\nother info that might help us debug this:\n"); 4739 lockdep_print_held_locks(curr); 4740 4741 pr_warn("\nstack backtrace:\n"); 4742 dump_stack(); 4743 } 4744 4745 static int __lock_is_held(const struct lockdep_map *lock, int read); 4746 4747 /* 4748 * This gets called for every mutex_lock*()/spin_lock*() operation. 4749 * We maintain the dependency maps and validate the locking attempt: 4750 * 4751 * The callers must make sure that IRQs are disabled before calling it, 4752 * otherwise we could get an interrupt which would want to take locks, 4753 * which would end up in lockdep again. 4754 */ 4755 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, 4756 int trylock, int read, int check, int hardirqs_off, 4757 struct lockdep_map *nest_lock, unsigned long ip, 4758 int references, int pin_count) 4759 { 4760 struct task_struct *curr = current; 4761 struct lock_class *class = NULL; 4762 struct held_lock *hlock; 4763 unsigned int depth; 4764 int chain_head = 0; 4765 int class_idx; 4766 u64 chain_key; 4767 4768 if (unlikely(!debug_locks)) 4769 return 0; 4770 4771 if (!prove_locking || lock->key == &__lockdep_no_validate__) 4772 check = 0; 4773 4774 if (subclass < NR_LOCKDEP_CACHING_CLASSES) 4775 class = lock->class_cache[subclass]; 4776 /* 4777 * Not cached? 4778 */ 4779 if (unlikely(!class)) { 4780 class = register_lock_class(lock, subclass, 0); 4781 if (!class) 4782 return 0; 4783 } 4784 4785 debug_class_ops_inc(class); 4786 4787 if (very_verbose(class)) { 4788 printk("\nacquire class [%px] %s", class->key, class->name); 4789 if (class->name_version > 1) 4790 printk(KERN_CONT "#%d", class->name_version); 4791 printk(KERN_CONT "\n"); 4792 dump_stack(); 4793 } 4794 4795 /* 4796 * Add the lock to the list of currently held locks. 4797 * (we dont increase the depth just yet, up until the 4798 * dependency checks are done) 4799 */ 4800 depth = curr->lockdep_depth; 4801 /* 4802 * Ran out of static storage for our per-task lock stack again have we? 4803 */ 4804 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) 4805 return 0; 4806 4807 class_idx = class - lock_classes; 4808 4809 if (depth) { /* we're holding locks */ 4810 hlock = curr->held_locks + depth - 1; 4811 if (hlock->class_idx == class_idx && nest_lock) { 4812 if (!references) 4813 references++; 4814 4815 if (!hlock->references) 4816 hlock->references++; 4817 4818 hlock->references += references; 4819 4820 /* Overflow */ 4821 if (DEBUG_LOCKS_WARN_ON(hlock->references < references)) 4822 return 0; 4823 4824 return 2; 4825 } 4826 } 4827 4828 hlock = curr->held_locks + depth; 4829 /* 4830 * Plain impossible, we just registered it and checked it weren't no 4831 * NULL like.. I bet this mushroom I ate was good! 4832 */ 4833 if (DEBUG_LOCKS_WARN_ON(!class)) 4834 return 0; 4835 hlock->class_idx = class_idx; 4836 hlock->acquire_ip = ip; 4837 hlock->instance = lock; 4838 hlock->nest_lock = nest_lock; 4839 hlock->irq_context = task_irq_context(curr); 4840 hlock->trylock = trylock; 4841 hlock->read = read; 4842 hlock->check = check; 4843 hlock->hardirqs_off = !!hardirqs_off; 4844 hlock->references = references; 4845 #ifdef CONFIG_LOCK_STAT 4846 hlock->waittime_stamp = 0; 4847 hlock->holdtime_stamp = lockstat_clock(); 4848 #endif 4849 hlock->pin_count = pin_count; 4850 4851 if (check_wait_context(curr, hlock)) 4852 return 0; 4853 4854 /* Initialize the lock usage bit */ 4855 if (!mark_usage(curr, hlock, check)) 4856 return 0; 4857 4858 /* 4859 * Calculate the chain hash: it's the combined hash of all the 4860 * lock keys along the dependency chain. We save the hash value 4861 * at every step so that we can get the current hash easily 4862 * after unlock. The chain hash is then used to cache dependency 4863 * results. 4864 * 4865 * The 'key ID' is what is the most compact key value to drive 4866 * the hash, not class->key. 4867 */ 4868 /* 4869 * Whoops, we did it again.. class_idx is invalid. 4870 */ 4871 if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use))) 4872 return 0; 4873 4874 chain_key = curr->curr_chain_key; 4875 if (!depth) { 4876 /* 4877 * How can we have a chain hash when we ain't got no keys?! 4878 */ 4879 if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY)) 4880 return 0; 4881 chain_head = 1; 4882 } 4883 4884 hlock->prev_chain_key = chain_key; 4885 if (separate_irq_context(curr, hlock)) { 4886 chain_key = INITIAL_CHAIN_KEY; 4887 chain_head = 1; 4888 } 4889 chain_key = iterate_chain_key(chain_key, hlock_id(hlock)); 4890 4891 if (nest_lock && !__lock_is_held(nest_lock, -1)) { 4892 print_lock_nested_lock_not_held(curr, hlock, ip); 4893 return 0; 4894 } 4895 4896 if (!debug_locks_silent) { 4897 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key); 4898 WARN_ON_ONCE(!hlock_class(hlock)->key); 4899 } 4900 4901 if (!validate_chain(curr, hlock, chain_head, chain_key)) 4902 return 0; 4903 4904 curr->curr_chain_key = chain_key; 4905 curr->lockdep_depth++; 4906 check_chain_key(curr); 4907 #ifdef CONFIG_DEBUG_LOCKDEP 4908 if (unlikely(!debug_locks)) 4909 return 0; 4910 #endif 4911 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { 4912 debug_locks_off(); 4913 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); 4914 printk(KERN_DEBUG "depth: %i max: %lu!\n", 4915 curr->lockdep_depth, MAX_LOCK_DEPTH); 4916 4917 lockdep_print_held_locks(current); 4918 debug_show_all_locks(); 4919 dump_stack(); 4920 4921 return 0; 4922 } 4923 4924 if (unlikely(curr->lockdep_depth > max_lockdep_depth)) 4925 max_lockdep_depth = curr->lockdep_depth; 4926 4927 return 1; 4928 } 4929 4930 static void print_unlock_imbalance_bug(struct task_struct *curr, 4931 struct lockdep_map *lock, 4932 unsigned long ip) 4933 { 4934 if (!debug_locks_off()) 4935 return; 4936 if (debug_locks_silent) 4937 return; 4938 4939 pr_warn("\n"); 4940 pr_warn("=====================================\n"); 4941 pr_warn("WARNING: bad unlock balance detected!\n"); 4942 print_kernel_ident(); 4943 pr_warn("-------------------------------------\n"); 4944 pr_warn("%s/%d is trying to release lock (", 4945 curr->comm, task_pid_nr(curr)); 4946 print_lockdep_cache(lock); 4947 pr_cont(") at:\n"); 4948 print_ip_sym(KERN_WARNING, ip); 4949 pr_warn("but there are no more locks to release!\n"); 4950 pr_warn("\nother info that might help us debug this:\n"); 4951 lockdep_print_held_locks(curr); 4952 4953 pr_warn("\nstack backtrace:\n"); 4954 dump_stack(); 4955 } 4956 4957 static noinstr int match_held_lock(const struct held_lock *hlock, 4958 const struct lockdep_map *lock) 4959 { 4960 if (hlock->instance == lock) 4961 return 1; 4962 4963 if (hlock->references) { 4964 const struct lock_class *class = lock->class_cache[0]; 4965 4966 if (!class) 4967 class = look_up_lock_class(lock, 0); 4968 4969 /* 4970 * If look_up_lock_class() failed to find a class, we're trying 4971 * to test if we hold a lock that has never yet been acquired. 4972 * Clearly if the lock hasn't been acquired _ever_, we're not 4973 * holding it either, so report failure. 4974 */ 4975 if (!class) 4976 return 0; 4977 4978 /* 4979 * References, but not a lock we're actually ref-counting? 4980 * State got messed up, follow the sites that change ->references 4981 * and try to make sense of it. 4982 */ 4983 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) 4984 return 0; 4985 4986 if (hlock->class_idx == class - lock_classes) 4987 return 1; 4988 } 4989 4990 return 0; 4991 } 4992 4993 /* @depth must not be zero */ 4994 static struct held_lock *find_held_lock(struct task_struct *curr, 4995 struct lockdep_map *lock, 4996 unsigned int depth, int *idx) 4997 { 4998 struct held_lock *ret, *hlock, *prev_hlock; 4999 int i; 5000 5001 i = depth - 1; 5002 hlock = curr->held_locks + i; 5003 ret = hlock; 5004 if (match_held_lock(hlock, lock)) 5005 goto out; 5006 5007 ret = NULL; 5008 for (i--, prev_hlock = hlock--; 5009 i >= 0; 5010 i--, prev_hlock = hlock--) { 5011 /* 5012 * We must not cross into another context: 5013 */ 5014 if (prev_hlock->irq_context != hlock->irq_context) { 5015 ret = NULL; 5016 break; 5017 } 5018 if (match_held_lock(hlock, lock)) { 5019 ret = hlock; 5020 break; 5021 } 5022 } 5023 5024 out: 5025 *idx = i; 5026 return ret; 5027 } 5028 5029 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth, 5030 int idx, unsigned int *merged) 5031 { 5032 struct held_lock *hlock; 5033 int first_idx = idx; 5034 5035 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 5036 return 0; 5037 5038 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { 5039 switch (__lock_acquire(hlock->instance, 5040 hlock_class(hlock)->subclass, 5041 hlock->trylock, 5042 hlock->read, hlock->check, 5043 hlock->hardirqs_off, 5044 hlock->nest_lock, hlock->acquire_ip, 5045 hlock->references, hlock->pin_count)) { 5046 case 0: 5047 return 1; 5048 case 1: 5049 break; 5050 case 2: 5051 *merged += (idx == first_idx); 5052 break; 5053 default: 5054 WARN_ON(1); 5055 return 0; 5056 } 5057 } 5058 return 0; 5059 } 5060 5061 static int 5062 __lock_set_class(struct lockdep_map *lock, const char *name, 5063 struct lock_class_key *key, unsigned int subclass, 5064 unsigned long ip) 5065 { 5066 struct task_struct *curr = current; 5067 unsigned int depth, merged = 0; 5068 struct held_lock *hlock; 5069 struct lock_class *class; 5070 int i; 5071 5072 if (unlikely(!debug_locks)) 5073 return 0; 5074 5075 depth = curr->lockdep_depth; 5076 /* 5077 * This function is about (re)setting the class of a held lock, 5078 * yet we're not actually holding any locks. Naughty user! 5079 */ 5080 if (DEBUG_LOCKS_WARN_ON(!depth)) 5081 return 0; 5082 5083 hlock = find_held_lock(curr, lock, depth, &i); 5084 if (!hlock) { 5085 print_unlock_imbalance_bug(curr, lock, ip); 5086 return 0; 5087 } 5088 5089 lockdep_init_map_waits(lock, name, key, 0, 5090 lock->wait_type_inner, 5091 lock->wait_type_outer); 5092 class = register_lock_class(lock, subclass, 0); 5093 hlock->class_idx = class - lock_classes; 5094 5095 curr->lockdep_depth = i; 5096 curr->curr_chain_key = hlock->prev_chain_key; 5097 5098 if (reacquire_held_locks(curr, depth, i, &merged)) 5099 return 0; 5100 5101 /* 5102 * I took it apart and put it back together again, except now I have 5103 * these 'spare' parts.. where shall I put them. 5104 */ 5105 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged)) 5106 return 0; 5107 return 1; 5108 } 5109 5110 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) 5111 { 5112 struct task_struct *curr = current; 5113 unsigned int depth, merged = 0; 5114 struct held_lock *hlock; 5115 int i; 5116 5117 if (unlikely(!debug_locks)) 5118 return 0; 5119 5120 depth = curr->lockdep_depth; 5121 /* 5122 * This function is about (re)setting the class of a held lock, 5123 * yet we're not actually holding any locks. Naughty user! 5124 */ 5125 if (DEBUG_LOCKS_WARN_ON(!depth)) 5126 return 0; 5127 5128 hlock = find_held_lock(curr, lock, depth, &i); 5129 if (!hlock) { 5130 print_unlock_imbalance_bug(curr, lock, ip); 5131 return 0; 5132 } 5133 5134 curr->lockdep_depth = i; 5135 curr->curr_chain_key = hlock->prev_chain_key; 5136 5137 WARN(hlock->read, "downgrading a read lock"); 5138 hlock->read = 1; 5139 hlock->acquire_ip = ip; 5140 5141 if (reacquire_held_locks(curr, depth, i, &merged)) 5142 return 0; 5143 5144 /* Merging can't happen with unchanged classes.. */ 5145 if (DEBUG_LOCKS_WARN_ON(merged)) 5146 return 0; 5147 5148 /* 5149 * I took it apart and put it back together again, except now I have 5150 * these 'spare' parts.. where shall I put them. 5151 */ 5152 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) 5153 return 0; 5154 5155 return 1; 5156 } 5157 5158 /* 5159 * Remove the lock from the list of currently held locks - this gets 5160 * called on mutex_unlock()/spin_unlock*() (or on a failed 5161 * mutex_lock_interruptible()). 5162 */ 5163 static int 5164 __lock_release(struct lockdep_map *lock, unsigned long ip) 5165 { 5166 struct task_struct *curr = current; 5167 unsigned int depth, merged = 1; 5168 struct held_lock *hlock; 5169 int i; 5170 5171 if (unlikely(!debug_locks)) 5172 return 0; 5173 5174 depth = curr->lockdep_depth; 5175 /* 5176 * So we're all set to release this lock.. wait what lock? We don't 5177 * own any locks, you've been drinking again? 5178 */ 5179 if (depth <= 0) { 5180 print_unlock_imbalance_bug(curr, lock, ip); 5181 return 0; 5182 } 5183 5184 /* 5185 * Check whether the lock exists in the current stack 5186 * of held locks: 5187 */ 5188 hlock = find_held_lock(curr, lock, depth, &i); 5189 if (!hlock) { 5190 print_unlock_imbalance_bug(curr, lock, ip); 5191 return 0; 5192 } 5193 5194 if (hlock->instance == lock) 5195 lock_release_holdtime(hlock); 5196 5197 WARN(hlock->pin_count, "releasing a pinned lock\n"); 5198 5199 if (hlock->references) { 5200 hlock->references--; 5201 if (hlock->references) { 5202 /* 5203 * We had, and after removing one, still have 5204 * references, the current lock stack is still 5205 * valid. We're done! 5206 */ 5207 return 1; 5208 } 5209 } 5210 5211 /* 5212 * We have the right lock to unlock, 'hlock' points to it. 5213 * Now we remove it from the stack, and add back the other 5214 * entries (if any), recalculating the hash along the way: 5215 */ 5216 5217 curr->lockdep_depth = i; 5218 curr->curr_chain_key = hlock->prev_chain_key; 5219 5220 /* 5221 * The most likely case is when the unlock is on the innermost 5222 * lock. In this case, we are done! 5223 */ 5224 if (i == depth-1) 5225 return 1; 5226 5227 if (reacquire_held_locks(curr, depth, i + 1, &merged)) 5228 return 0; 5229 5230 /* 5231 * We had N bottles of beer on the wall, we drank one, but now 5232 * there's not N-1 bottles of beer left on the wall... 5233 * Pouring two of the bottles together is acceptable. 5234 */ 5235 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged); 5236 5237 /* 5238 * Since reacquire_held_locks() would have called check_chain_key() 5239 * indirectly via __lock_acquire(), we don't need to do it again 5240 * on return. 5241 */ 5242 return 0; 5243 } 5244 5245 static __always_inline 5246 int __lock_is_held(const struct lockdep_map *lock, int read) 5247 { 5248 struct task_struct *curr = current; 5249 int i; 5250 5251 for (i = 0; i < curr->lockdep_depth; i++) { 5252 struct held_lock *hlock = curr->held_locks + i; 5253 5254 if (match_held_lock(hlock, lock)) { 5255 if (read == -1 || hlock->read == read) 5256 return 1; 5257 5258 return 0; 5259 } 5260 } 5261 5262 return 0; 5263 } 5264 5265 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock) 5266 { 5267 struct pin_cookie cookie = NIL_COOKIE; 5268 struct task_struct *curr = current; 5269 int i; 5270 5271 if (unlikely(!debug_locks)) 5272 return cookie; 5273 5274 for (i = 0; i < curr->lockdep_depth; i++) { 5275 struct held_lock *hlock = curr->held_locks + i; 5276 5277 if (match_held_lock(hlock, lock)) { 5278 /* 5279 * Grab 16bits of randomness; this is sufficient to not 5280 * be guessable and still allows some pin nesting in 5281 * our u32 pin_count. 5282 */ 5283 cookie.val = 1 + (prandom_u32() >> 16); 5284 hlock->pin_count += cookie.val; 5285 return cookie; 5286 } 5287 } 5288 5289 WARN(1, "pinning an unheld lock\n"); 5290 return cookie; 5291 } 5292 5293 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 5294 { 5295 struct task_struct *curr = current; 5296 int i; 5297 5298 if (unlikely(!debug_locks)) 5299 return; 5300 5301 for (i = 0; i < curr->lockdep_depth; i++) { 5302 struct held_lock *hlock = curr->held_locks + i; 5303 5304 if (match_held_lock(hlock, lock)) { 5305 hlock->pin_count += cookie.val; 5306 return; 5307 } 5308 } 5309 5310 WARN(1, "pinning an unheld lock\n"); 5311 } 5312 5313 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 5314 { 5315 struct task_struct *curr = current; 5316 int i; 5317 5318 if (unlikely(!debug_locks)) 5319 return; 5320 5321 for (i = 0; i < curr->lockdep_depth; i++) { 5322 struct held_lock *hlock = curr->held_locks + i; 5323 5324 if (match_held_lock(hlock, lock)) { 5325 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n")) 5326 return; 5327 5328 hlock->pin_count -= cookie.val; 5329 5330 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n")) 5331 hlock->pin_count = 0; 5332 5333 return; 5334 } 5335 } 5336 5337 WARN(1, "unpinning an unheld lock\n"); 5338 } 5339 5340 /* 5341 * Check whether we follow the irq-flags state precisely: 5342 */ 5343 static noinstr void check_flags(unsigned long flags) 5344 { 5345 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) 5346 if (!debug_locks) 5347 return; 5348 5349 /* Get the warning out.. */ 5350 instrumentation_begin(); 5351 5352 if (irqs_disabled_flags(flags)) { 5353 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) { 5354 printk("possible reason: unannotated irqs-off.\n"); 5355 } 5356 } else { 5357 if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) { 5358 printk("possible reason: unannotated irqs-on.\n"); 5359 } 5360 } 5361 5362 /* 5363 * We dont accurately track softirq state in e.g. 5364 * hardirq contexts (such as on 4KSTACKS), so only 5365 * check if not in hardirq contexts: 5366 */ 5367 if (!hardirq_count()) { 5368 if (softirq_count()) { 5369 /* like the above, but with softirqs */ 5370 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); 5371 } else { 5372 /* lick the above, does it taste good? */ 5373 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); 5374 } 5375 } 5376 5377 if (!debug_locks) 5378 print_irqtrace_events(current); 5379 5380 instrumentation_end(); 5381 #endif 5382 } 5383 5384 void lock_set_class(struct lockdep_map *lock, const char *name, 5385 struct lock_class_key *key, unsigned int subclass, 5386 unsigned long ip) 5387 { 5388 unsigned long flags; 5389 5390 if (unlikely(!lockdep_enabled())) 5391 return; 5392 5393 raw_local_irq_save(flags); 5394 lockdep_recursion_inc(); 5395 check_flags(flags); 5396 if (__lock_set_class(lock, name, key, subclass, ip)) 5397 check_chain_key(current); 5398 lockdep_recursion_finish(); 5399 raw_local_irq_restore(flags); 5400 } 5401 EXPORT_SYMBOL_GPL(lock_set_class); 5402 5403 void lock_downgrade(struct lockdep_map *lock, unsigned long ip) 5404 { 5405 unsigned long flags; 5406 5407 if (unlikely(!lockdep_enabled())) 5408 return; 5409 5410 raw_local_irq_save(flags); 5411 lockdep_recursion_inc(); 5412 check_flags(flags); 5413 if (__lock_downgrade(lock, ip)) 5414 check_chain_key(current); 5415 lockdep_recursion_finish(); 5416 raw_local_irq_restore(flags); 5417 } 5418 EXPORT_SYMBOL_GPL(lock_downgrade); 5419 5420 /* NMI context !!! */ 5421 static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass) 5422 { 5423 #ifdef CONFIG_PROVE_LOCKING 5424 struct lock_class *class = look_up_lock_class(lock, subclass); 5425 unsigned long mask = LOCKF_USED; 5426 5427 /* if it doesn't have a class (yet), it certainly hasn't been used yet */ 5428 if (!class) 5429 return; 5430 5431 /* 5432 * READ locks only conflict with USED, such that if we only ever use 5433 * READ locks, there is no deadlock possible -- RCU. 5434 */ 5435 if (!hlock->read) 5436 mask |= LOCKF_USED_READ; 5437 5438 if (!(class->usage_mask & mask)) 5439 return; 5440 5441 hlock->class_idx = class - lock_classes; 5442 5443 print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES); 5444 #endif 5445 } 5446 5447 static bool lockdep_nmi(void) 5448 { 5449 if (raw_cpu_read(lockdep_recursion)) 5450 return false; 5451 5452 if (!in_nmi()) 5453 return false; 5454 5455 return true; 5456 } 5457 5458 /* 5459 * read_lock() is recursive if: 5460 * 1. We force lockdep think this way in selftests or 5461 * 2. The implementation is not queued read/write lock or 5462 * 3. The locker is at an in_interrupt() context. 5463 */ 5464 bool read_lock_is_recursive(void) 5465 { 5466 return force_read_lock_recursive || 5467 !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) || 5468 in_interrupt(); 5469 } 5470 EXPORT_SYMBOL_GPL(read_lock_is_recursive); 5471 5472 /* 5473 * We are not always called with irqs disabled - do that here, 5474 * and also avoid lockdep recursion: 5475 */ 5476 void lock_acquire(struct lockdep_map *lock, unsigned int subclass, 5477 int trylock, int read, int check, 5478 struct lockdep_map *nest_lock, unsigned long ip) 5479 { 5480 unsigned long flags; 5481 5482 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip); 5483 5484 if (!debug_locks) 5485 return; 5486 5487 if (unlikely(!lockdep_enabled())) { 5488 /* XXX allow trylock from NMI ?!? */ 5489 if (lockdep_nmi() && !trylock) { 5490 struct held_lock hlock; 5491 5492 hlock.acquire_ip = ip; 5493 hlock.instance = lock; 5494 hlock.nest_lock = nest_lock; 5495 hlock.irq_context = 2; // XXX 5496 hlock.trylock = trylock; 5497 hlock.read = read; 5498 hlock.check = check; 5499 hlock.hardirqs_off = true; 5500 hlock.references = 0; 5501 5502 verify_lock_unused(lock, &hlock, subclass); 5503 } 5504 return; 5505 } 5506 5507 raw_local_irq_save(flags); 5508 check_flags(flags); 5509 5510 lockdep_recursion_inc(); 5511 __lock_acquire(lock, subclass, trylock, read, check, 5512 irqs_disabled_flags(flags), nest_lock, ip, 0, 0); 5513 lockdep_recursion_finish(); 5514 raw_local_irq_restore(flags); 5515 } 5516 EXPORT_SYMBOL_GPL(lock_acquire); 5517 5518 void lock_release(struct lockdep_map *lock, unsigned long ip) 5519 { 5520 unsigned long flags; 5521 5522 trace_lock_release(lock, ip); 5523 5524 if (unlikely(!lockdep_enabled())) 5525 return; 5526 5527 raw_local_irq_save(flags); 5528 check_flags(flags); 5529 5530 lockdep_recursion_inc(); 5531 if (__lock_release(lock, ip)) 5532 check_chain_key(current); 5533 lockdep_recursion_finish(); 5534 raw_local_irq_restore(flags); 5535 } 5536 EXPORT_SYMBOL_GPL(lock_release); 5537 5538 noinstr int lock_is_held_type(const struct lockdep_map *lock, int read) 5539 { 5540 unsigned long flags; 5541 int ret = 0; 5542 5543 if (unlikely(!lockdep_enabled())) 5544 return 1; /* avoid false negative lockdep_assert_held() */ 5545 5546 raw_local_irq_save(flags); 5547 check_flags(flags); 5548 5549 lockdep_recursion_inc(); 5550 ret = __lock_is_held(lock, read); 5551 lockdep_recursion_finish(); 5552 raw_local_irq_restore(flags); 5553 5554 return ret; 5555 } 5556 EXPORT_SYMBOL_GPL(lock_is_held_type); 5557 NOKPROBE_SYMBOL(lock_is_held_type); 5558 5559 struct pin_cookie lock_pin_lock(struct lockdep_map *lock) 5560 { 5561 struct pin_cookie cookie = NIL_COOKIE; 5562 unsigned long flags; 5563 5564 if (unlikely(!lockdep_enabled())) 5565 return cookie; 5566 5567 raw_local_irq_save(flags); 5568 check_flags(flags); 5569 5570 lockdep_recursion_inc(); 5571 cookie = __lock_pin_lock(lock); 5572 lockdep_recursion_finish(); 5573 raw_local_irq_restore(flags); 5574 5575 return cookie; 5576 } 5577 EXPORT_SYMBOL_GPL(lock_pin_lock); 5578 5579 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 5580 { 5581 unsigned long flags; 5582 5583 if (unlikely(!lockdep_enabled())) 5584 return; 5585 5586 raw_local_irq_save(flags); 5587 check_flags(flags); 5588 5589 lockdep_recursion_inc(); 5590 __lock_repin_lock(lock, cookie); 5591 lockdep_recursion_finish(); 5592 raw_local_irq_restore(flags); 5593 } 5594 EXPORT_SYMBOL_GPL(lock_repin_lock); 5595 5596 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 5597 { 5598 unsigned long flags; 5599 5600 if (unlikely(!lockdep_enabled())) 5601 return; 5602 5603 raw_local_irq_save(flags); 5604 check_flags(flags); 5605 5606 lockdep_recursion_inc(); 5607 __lock_unpin_lock(lock, cookie); 5608 lockdep_recursion_finish(); 5609 raw_local_irq_restore(flags); 5610 } 5611 EXPORT_SYMBOL_GPL(lock_unpin_lock); 5612 5613 #ifdef CONFIG_LOCK_STAT 5614 static void print_lock_contention_bug(struct task_struct *curr, 5615 struct lockdep_map *lock, 5616 unsigned long ip) 5617 { 5618 if (!debug_locks_off()) 5619 return; 5620 if (debug_locks_silent) 5621 return; 5622 5623 pr_warn("\n"); 5624 pr_warn("=================================\n"); 5625 pr_warn("WARNING: bad contention detected!\n"); 5626 print_kernel_ident(); 5627 pr_warn("---------------------------------\n"); 5628 pr_warn("%s/%d is trying to contend lock (", 5629 curr->comm, task_pid_nr(curr)); 5630 print_lockdep_cache(lock); 5631 pr_cont(") at:\n"); 5632 print_ip_sym(KERN_WARNING, ip); 5633 pr_warn("but there are no locks held!\n"); 5634 pr_warn("\nother info that might help us debug this:\n"); 5635 lockdep_print_held_locks(curr); 5636 5637 pr_warn("\nstack backtrace:\n"); 5638 dump_stack(); 5639 } 5640 5641 static void 5642 __lock_contended(struct lockdep_map *lock, unsigned long ip) 5643 { 5644 struct task_struct *curr = current; 5645 struct held_lock *hlock; 5646 struct lock_class_stats *stats; 5647 unsigned int depth; 5648 int i, contention_point, contending_point; 5649 5650 depth = curr->lockdep_depth; 5651 /* 5652 * Whee, we contended on this lock, except it seems we're not 5653 * actually trying to acquire anything much at all.. 5654 */ 5655 if (DEBUG_LOCKS_WARN_ON(!depth)) 5656 return; 5657 5658 hlock = find_held_lock(curr, lock, depth, &i); 5659 if (!hlock) { 5660 print_lock_contention_bug(curr, lock, ip); 5661 return; 5662 } 5663 5664 if (hlock->instance != lock) 5665 return; 5666 5667 hlock->waittime_stamp = lockstat_clock(); 5668 5669 contention_point = lock_point(hlock_class(hlock)->contention_point, ip); 5670 contending_point = lock_point(hlock_class(hlock)->contending_point, 5671 lock->ip); 5672 5673 stats = get_lock_stats(hlock_class(hlock)); 5674 if (contention_point < LOCKSTAT_POINTS) 5675 stats->contention_point[contention_point]++; 5676 if (contending_point < LOCKSTAT_POINTS) 5677 stats->contending_point[contending_point]++; 5678 if (lock->cpu != smp_processor_id()) 5679 stats->bounces[bounce_contended + !!hlock->read]++; 5680 } 5681 5682 static void 5683 __lock_acquired(struct lockdep_map *lock, unsigned long ip) 5684 { 5685 struct task_struct *curr = current; 5686 struct held_lock *hlock; 5687 struct lock_class_stats *stats; 5688 unsigned int depth; 5689 u64 now, waittime = 0; 5690 int i, cpu; 5691 5692 depth = curr->lockdep_depth; 5693 /* 5694 * Yay, we acquired ownership of this lock we didn't try to 5695 * acquire, how the heck did that happen? 5696 */ 5697 if (DEBUG_LOCKS_WARN_ON(!depth)) 5698 return; 5699 5700 hlock = find_held_lock(curr, lock, depth, &i); 5701 if (!hlock) { 5702 print_lock_contention_bug(curr, lock, _RET_IP_); 5703 return; 5704 } 5705 5706 if (hlock->instance != lock) 5707 return; 5708 5709 cpu = smp_processor_id(); 5710 if (hlock->waittime_stamp) { 5711 now = lockstat_clock(); 5712 waittime = now - hlock->waittime_stamp; 5713 hlock->holdtime_stamp = now; 5714 } 5715 5716 stats = get_lock_stats(hlock_class(hlock)); 5717 if (waittime) { 5718 if (hlock->read) 5719 lock_time_inc(&stats->read_waittime, waittime); 5720 else 5721 lock_time_inc(&stats->write_waittime, waittime); 5722 } 5723 if (lock->cpu != cpu) 5724 stats->bounces[bounce_acquired + !!hlock->read]++; 5725 5726 lock->cpu = cpu; 5727 lock->ip = ip; 5728 } 5729 5730 void lock_contended(struct lockdep_map *lock, unsigned long ip) 5731 { 5732 unsigned long flags; 5733 5734 trace_lock_acquired(lock, ip); 5735 5736 if (unlikely(!lock_stat || !lockdep_enabled())) 5737 return; 5738 5739 raw_local_irq_save(flags); 5740 check_flags(flags); 5741 lockdep_recursion_inc(); 5742 __lock_contended(lock, ip); 5743 lockdep_recursion_finish(); 5744 raw_local_irq_restore(flags); 5745 } 5746 EXPORT_SYMBOL_GPL(lock_contended); 5747 5748 void lock_acquired(struct lockdep_map *lock, unsigned long ip) 5749 { 5750 unsigned long flags; 5751 5752 trace_lock_contended(lock, ip); 5753 5754 if (unlikely(!lock_stat || !lockdep_enabled())) 5755 return; 5756 5757 raw_local_irq_save(flags); 5758 check_flags(flags); 5759 lockdep_recursion_inc(); 5760 __lock_acquired(lock, ip); 5761 lockdep_recursion_finish(); 5762 raw_local_irq_restore(flags); 5763 } 5764 EXPORT_SYMBOL_GPL(lock_acquired); 5765 #endif 5766 5767 /* 5768 * Used by the testsuite, sanitize the validator state 5769 * after a simulated failure: 5770 */ 5771 5772 void lockdep_reset(void) 5773 { 5774 unsigned long flags; 5775 int i; 5776 5777 raw_local_irq_save(flags); 5778 lockdep_init_task(current); 5779 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); 5780 nr_hardirq_chains = 0; 5781 nr_softirq_chains = 0; 5782 nr_process_chains = 0; 5783 debug_locks = 1; 5784 for (i = 0; i < CHAINHASH_SIZE; i++) 5785 INIT_HLIST_HEAD(chainhash_table + i); 5786 raw_local_irq_restore(flags); 5787 } 5788 5789 /* Remove a class from a lock chain. Must be called with the graph lock held. */ 5790 static void remove_class_from_lock_chain(struct pending_free *pf, 5791 struct lock_chain *chain, 5792 struct lock_class *class) 5793 { 5794 #ifdef CONFIG_PROVE_LOCKING 5795 int i; 5796 5797 for (i = chain->base; i < chain->base + chain->depth; i++) { 5798 if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes) 5799 continue; 5800 /* 5801 * Each lock class occurs at most once in a lock chain so once 5802 * we found a match we can break out of this loop. 5803 */ 5804 goto free_lock_chain; 5805 } 5806 /* Since the chain has not been modified, return. */ 5807 return; 5808 5809 free_lock_chain: 5810 free_chain_hlocks(chain->base, chain->depth); 5811 /* Overwrite the chain key for concurrent RCU readers. */ 5812 WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY); 5813 dec_chains(chain->irq_context); 5814 5815 /* 5816 * Note: calling hlist_del_rcu() from inside a 5817 * hlist_for_each_entry_rcu() loop is safe. 5818 */ 5819 hlist_del_rcu(&chain->entry); 5820 __set_bit(chain - lock_chains, pf->lock_chains_being_freed); 5821 nr_zapped_lock_chains++; 5822 #endif 5823 } 5824 5825 /* Must be called with the graph lock held. */ 5826 static void remove_class_from_lock_chains(struct pending_free *pf, 5827 struct lock_class *class) 5828 { 5829 struct lock_chain *chain; 5830 struct hlist_head *head; 5831 int i; 5832 5833 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) { 5834 head = chainhash_table + i; 5835 hlist_for_each_entry_rcu(chain, head, entry) { 5836 remove_class_from_lock_chain(pf, chain, class); 5837 } 5838 } 5839 } 5840 5841 /* 5842 * Remove all references to a lock class. The caller must hold the graph lock. 5843 */ 5844 static void zap_class(struct pending_free *pf, struct lock_class *class) 5845 { 5846 struct lock_list *entry; 5847 int i; 5848 5849 WARN_ON_ONCE(!class->key); 5850 5851 /* 5852 * Remove all dependencies this lock is 5853 * involved in: 5854 */ 5855 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { 5856 entry = list_entries + i; 5857 if (entry->class != class && entry->links_to != class) 5858 continue; 5859 __clear_bit(i, list_entries_in_use); 5860 nr_list_entries--; 5861 list_del_rcu(&entry->entry); 5862 } 5863 if (list_empty(&class->locks_after) && 5864 list_empty(&class->locks_before)) { 5865 list_move_tail(&class->lock_entry, &pf->zapped); 5866 hlist_del_rcu(&class->hash_entry); 5867 WRITE_ONCE(class->key, NULL); 5868 WRITE_ONCE(class->name, NULL); 5869 nr_lock_classes--; 5870 __clear_bit(class - lock_classes, lock_classes_in_use); 5871 } else { 5872 WARN_ONCE(true, "%s() failed for class %s\n", __func__, 5873 class->name); 5874 } 5875 5876 remove_class_from_lock_chains(pf, class); 5877 nr_zapped_classes++; 5878 } 5879 5880 static void reinit_class(struct lock_class *class) 5881 { 5882 void *const p = class; 5883 const unsigned int offset = offsetof(struct lock_class, key); 5884 5885 WARN_ON_ONCE(!class->lock_entry.next); 5886 WARN_ON_ONCE(!list_empty(&class->locks_after)); 5887 WARN_ON_ONCE(!list_empty(&class->locks_before)); 5888 memset(p + offset, 0, sizeof(*class) - offset); 5889 WARN_ON_ONCE(!class->lock_entry.next); 5890 WARN_ON_ONCE(!list_empty(&class->locks_after)); 5891 WARN_ON_ONCE(!list_empty(&class->locks_before)); 5892 } 5893 5894 static inline int within(const void *addr, void *start, unsigned long size) 5895 { 5896 return addr >= start && addr < start + size; 5897 } 5898 5899 static bool inside_selftest(void) 5900 { 5901 return current == lockdep_selftest_task_struct; 5902 } 5903 5904 /* The caller must hold the graph lock. */ 5905 static struct pending_free *get_pending_free(void) 5906 { 5907 return delayed_free.pf + delayed_free.index; 5908 } 5909 5910 static void free_zapped_rcu(struct rcu_head *cb); 5911 5912 /* 5913 * Schedule an RCU callback if no RCU callback is pending. Must be called with 5914 * the graph lock held. 5915 */ 5916 static void call_rcu_zapped(struct pending_free *pf) 5917 { 5918 WARN_ON_ONCE(inside_selftest()); 5919 5920 if (list_empty(&pf->zapped)) 5921 return; 5922 5923 if (delayed_free.scheduled) 5924 return; 5925 5926 delayed_free.scheduled = true; 5927 5928 WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf); 5929 delayed_free.index ^= 1; 5930 5931 call_rcu(&delayed_free.rcu_head, free_zapped_rcu); 5932 } 5933 5934 /* The caller must hold the graph lock. May be called from RCU context. */ 5935 static void __free_zapped_classes(struct pending_free *pf) 5936 { 5937 struct lock_class *class; 5938 5939 check_data_structures(); 5940 5941 list_for_each_entry(class, &pf->zapped, lock_entry) 5942 reinit_class(class); 5943 5944 list_splice_init(&pf->zapped, &free_lock_classes); 5945 5946 #ifdef CONFIG_PROVE_LOCKING 5947 bitmap_andnot(lock_chains_in_use, lock_chains_in_use, 5948 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains)); 5949 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains)); 5950 #endif 5951 } 5952 5953 static void free_zapped_rcu(struct rcu_head *ch) 5954 { 5955 struct pending_free *pf; 5956 unsigned long flags; 5957 5958 if (WARN_ON_ONCE(ch != &delayed_free.rcu_head)) 5959 return; 5960 5961 raw_local_irq_save(flags); 5962 lockdep_lock(); 5963 5964 /* closed head */ 5965 pf = delayed_free.pf + (delayed_free.index ^ 1); 5966 __free_zapped_classes(pf); 5967 delayed_free.scheduled = false; 5968 5969 /* 5970 * If there's anything on the open list, close and start a new callback. 5971 */ 5972 call_rcu_zapped(delayed_free.pf + delayed_free.index); 5973 5974 lockdep_unlock(); 5975 raw_local_irq_restore(flags); 5976 } 5977 5978 /* 5979 * Remove all lock classes from the class hash table and from the 5980 * all_lock_classes list whose key or name is in the address range [start, 5981 * start + size). Move these lock classes to the zapped_classes list. Must 5982 * be called with the graph lock held. 5983 */ 5984 static void __lockdep_free_key_range(struct pending_free *pf, void *start, 5985 unsigned long size) 5986 { 5987 struct lock_class *class; 5988 struct hlist_head *head; 5989 int i; 5990 5991 /* Unhash all classes that were created by a module. */ 5992 for (i = 0; i < CLASSHASH_SIZE; i++) { 5993 head = classhash_table + i; 5994 hlist_for_each_entry_rcu(class, head, hash_entry) { 5995 if (!within(class->key, start, size) && 5996 !within(class->name, start, size)) 5997 continue; 5998 zap_class(pf, class); 5999 } 6000 } 6001 } 6002 6003 /* 6004 * Used in module.c to remove lock classes from memory that is going to be 6005 * freed; and possibly re-used by other modules. 6006 * 6007 * We will have had one synchronize_rcu() before getting here, so we're 6008 * guaranteed nobody will look up these exact classes -- they're properly dead 6009 * but still allocated. 6010 */ 6011 static void lockdep_free_key_range_reg(void *start, unsigned long size) 6012 { 6013 struct pending_free *pf; 6014 unsigned long flags; 6015 6016 init_data_structures_once(); 6017 6018 raw_local_irq_save(flags); 6019 lockdep_lock(); 6020 pf = get_pending_free(); 6021 __lockdep_free_key_range(pf, start, size); 6022 call_rcu_zapped(pf); 6023 lockdep_unlock(); 6024 raw_local_irq_restore(flags); 6025 6026 /* 6027 * Wait for any possible iterators from look_up_lock_class() to pass 6028 * before continuing to free the memory they refer to. 6029 */ 6030 synchronize_rcu(); 6031 } 6032 6033 /* 6034 * Free all lockdep keys in the range [start, start+size). Does not sleep. 6035 * Ignores debug_locks. Must only be used by the lockdep selftests. 6036 */ 6037 static void lockdep_free_key_range_imm(void *start, unsigned long size) 6038 { 6039 struct pending_free *pf = delayed_free.pf; 6040 unsigned long flags; 6041 6042 init_data_structures_once(); 6043 6044 raw_local_irq_save(flags); 6045 lockdep_lock(); 6046 __lockdep_free_key_range(pf, start, size); 6047 __free_zapped_classes(pf); 6048 lockdep_unlock(); 6049 raw_local_irq_restore(flags); 6050 } 6051 6052 void lockdep_free_key_range(void *start, unsigned long size) 6053 { 6054 init_data_structures_once(); 6055 6056 if (inside_selftest()) 6057 lockdep_free_key_range_imm(start, size); 6058 else 6059 lockdep_free_key_range_reg(start, size); 6060 } 6061 6062 /* 6063 * Check whether any element of the @lock->class_cache[] array refers to a 6064 * registered lock class. The caller must hold either the graph lock or the 6065 * RCU read lock. 6066 */ 6067 static bool lock_class_cache_is_registered(struct lockdep_map *lock) 6068 { 6069 struct lock_class *class; 6070 struct hlist_head *head; 6071 int i, j; 6072 6073 for (i = 0; i < CLASSHASH_SIZE; i++) { 6074 head = classhash_table + i; 6075 hlist_for_each_entry_rcu(class, head, hash_entry) { 6076 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++) 6077 if (lock->class_cache[j] == class) 6078 return true; 6079 } 6080 } 6081 return false; 6082 } 6083 6084 /* The caller must hold the graph lock. Does not sleep. */ 6085 static void __lockdep_reset_lock(struct pending_free *pf, 6086 struct lockdep_map *lock) 6087 { 6088 struct lock_class *class; 6089 int j; 6090 6091 /* 6092 * Remove all classes this lock might have: 6093 */ 6094 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) { 6095 /* 6096 * If the class exists we look it up and zap it: 6097 */ 6098 class = look_up_lock_class(lock, j); 6099 if (class) 6100 zap_class(pf, class); 6101 } 6102 /* 6103 * Debug check: in the end all mapped classes should 6104 * be gone. 6105 */ 6106 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock))) 6107 debug_locks_off(); 6108 } 6109 6110 /* 6111 * Remove all information lockdep has about a lock if debug_locks == 1. Free 6112 * released data structures from RCU context. 6113 */ 6114 static void lockdep_reset_lock_reg(struct lockdep_map *lock) 6115 { 6116 struct pending_free *pf; 6117 unsigned long flags; 6118 int locked; 6119 6120 raw_local_irq_save(flags); 6121 locked = graph_lock(); 6122 if (!locked) 6123 goto out_irq; 6124 6125 pf = get_pending_free(); 6126 __lockdep_reset_lock(pf, lock); 6127 call_rcu_zapped(pf); 6128 6129 graph_unlock(); 6130 out_irq: 6131 raw_local_irq_restore(flags); 6132 } 6133 6134 /* 6135 * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the 6136 * lockdep selftests. 6137 */ 6138 static void lockdep_reset_lock_imm(struct lockdep_map *lock) 6139 { 6140 struct pending_free *pf = delayed_free.pf; 6141 unsigned long flags; 6142 6143 raw_local_irq_save(flags); 6144 lockdep_lock(); 6145 __lockdep_reset_lock(pf, lock); 6146 __free_zapped_classes(pf); 6147 lockdep_unlock(); 6148 raw_local_irq_restore(flags); 6149 } 6150 6151 void lockdep_reset_lock(struct lockdep_map *lock) 6152 { 6153 init_data_structures_once(); 6154 6155 if (inside_selftest()) 6156 lockdep_reset_lock_imm(lock); 6157 else 6158 lockdep_reset_lock_reg(lock); 6159 } 6160 6161 /* Unregister a dynamically allocated key. */ 6162 void lockdep_unregister_key(struct lock_class_key *key) 6163 { 6164 struct hlist_head *hash_head = keyhashentry(key); 6165 struct lock_class_key *k; 6166 struct pending_free *pf; 6167 unsigned long flags; 6168 bool found = false; 6169 6170 might_sleep(); 6171 6172 if (WARN_ON_ONCE(static_obj(key))) 6173 return; 6174 6175 raw_local_irq_save(flags); 6176 if (!graph_lock()) 6177 goto out_irq; 6178 6179 pf = get_pending_free(); 6180 hlist_for_each_entry_rcu(k, hash_head, hash_entry) { 6181 if (k == key) { 6182 hlist_del_rcu(&k->hash_entry); 6183 found = true; 6184 break; 6185 } 6186 } 6187 WARN_ON_ONCE(!found); 6188 __lockdep_free_key_range(pf, key, 1); 6189 call_rcu_zapped(pf); 6190 graph_unlock(); 6191 out_irq: 6192 raw_local_irq_restore(flags); 6193 6194 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */ 6195 synchronize_rcu(); 6196 } 6197 EXPORT_SYMBOL_GPL(lockdep_unregister_key); 6198 6199 void __init lockdep_init(void) 6200 { 6201 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); 6202 6203 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); 6204 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); 6205 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); 6206 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); 6207 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES); 6208 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS); 6209 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE); 6210 6211 printk(" memory used by lock dependency info: %zu kB\n", 6212 (sizeof(lock_classes) + 6213 sizeof(lock_classes_in_use) + 6214 sizeof(classhash_table) + 6215 sizeof(list_entries) + 6216 sizeof(list_entries_in_use) + 6217 sizeof(chainhash_table) + 6218 sizeof(delayed_free) 6219 #ifdef CONFIG_PROVE_LOCKING 6220 + sizeof(lock_cq) 6221 + sizeof(lock_chains) 6222 + sizeof(lock_chains_in_use) 6223 + sizeof(chain_hlocks) 6224 #endif 6225 ) / 1024 6226 ); 6227 6228 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) 6229 printk(" memory used for stack traces: %zu kB\n", 6230 (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024 6231 ); 6232 #endif 6233 6234 printk(" per task-struct memory footprint: %zu bytes\n", 6235 sizeof(((struct task_struct *)NULL)->held_locks)); 6236 } 6237 6238 static void 6239 print_freed_lock_bug(struct task_struct *curr, const void *mem_from, 6240 const void *mem_to, struct held_lock *hlock) 6241 { 6242 if (!debug_locks_off()) 6243 return; 6244 if (debug_locks_silent) 6245 return; 6246 6247 pr_warn("\n"); 6248 pr_warn("=========================\n"); 6249 pr_warn("WARNING: held lock freed!\n"); 6250 print_kernel_ident(); 6251 pr_warn("-------------------------\n"); 6252 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n", 6253 curr->comm, task_pid_nr(curr), mem_from, mem_to-1); 6254 print_lock(hlock); 6255 lockdep_print_held_locks(curr); 6256 6257 pr_warn("\nstack backtrace:\n"); 6258 dump_stack(); 6259 } 6260 6261 static inline int not_in_range(const void* mem_from, unsigned long mem_len, 6262 const void* lock_from, unsigned long lock_len) 6263 { 6264 return lock_from + lock_len <= mem_from || 6265 mem_from + mem_len <= lock_from; 6266 } 6267 6268 /* 6269 * Called when kernel memory is freed (or unmapped), or if a lock 6270 * is destroyed or reinitialized - this code checks whether there is 6271 * any held lock in the memory range of <from> to <to>: 6272 */ 6273 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) 6274 { 6275 struct task_struct *curr = current; 6276 struct held_lock *hlock; 6277 unsigned long flags; 6278 int i; 6279 6280 if (unlikely(!debug_locks)) 6281 return; 6282 6283 raw_local_irq_save(flags); 6284 for (i = 0; i < curr->lockdep_depth; i++) { 6285 hlock = curr->held_locks + i; 6286 6287 if (not_in_range(mem_from, mem_len, hlock->instance, 6288 sizeof(*hlock->instance))) 6289 continue; 6290 6291 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); 6292 break; 6293 } 6294 raw_local_irq_restore(flags); 6295 } 6296 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); 6297 6298 static void print_held_locks_bug(void) 6299 { 6300 if (!debug_locks_off()) 6301 return; 6302 if (debug_locks_silent) 6303 return; 6304 6305 pr_warn("\n"); 6306 pr_warn("====================================\n"); 6307 pr_warn("WARNING: %s/%d still has locks held!\n", 6308 current->comm, task_pid_nr(current)); 6309 print_kernel_ident(); 6310 pr_warn("------------------------------------\n"); 6311 lockdep_print_held_locks(current); 6312 pr_warn("\nstack backtrace:\n"); 6313 dump_stack(); 6314 } 6315 6316 void debug_check_no_locks_held(void) 6317 { 6318 if (unlikely(current->lockdep_depth > 0)) 6319 print_held_locks_bug(); 6320 } 6321 EXPORT_SYMBOL_GPL(debug_check_no_locks_held); 6322 6323 #ifdef __KERNEL__ 6324 void debug_show_all_locks(void) 6325 { 6326 struct task_struct *g, *p; 6327 6328 if (unlikely(!debug_locks)) { 6329 pr_warn("INFO: lockdep is turned off.\n"); 6330 return; 6331 } 6332 pr_warn("\nShowing all locks held in the system:\n"); 6333 6334 rcu_read_lock(); 6335 for_each_process_thread(g, p) { 6336 if (!p->lockdep_depth) 6337 continue; 6338 lockdep_print_held_locks(p); 6339 touch_nmi_watchdog(); 6340 touch_all_softlockup_watchdogs(); 6341 } 6342 rcu_read_unlock(); 6343 6344 pr_warn("\n"); 6345 pr_warn("=============================================\n\n"); 6346 } 6347 EXPORT_SYMBOL_GPL(debug_show_all_locks); 6348 #endif 6349 6350 /* 6351 * Careful: only use this function if you are sure that 6352 * the task cannot run in parallel! 6353 */ 6354 void debug_show_held_locks(struct task_struct *task) 6355 { 6356 if (unlikely(!debug_locks)) { 6357 printk("INFO: lockdep is turned off.\n"); 6358 return; 6359 } 6360 lockdep_print_held_locks(task); 6361 } 6362 EXPORT_SYMBOL_GPL(debug_show_held_locks); 6363 6364 asmlinkage __visible void lockdep_sys_exit(void) 6365 { 6366 struct task_struct *curr = current; 6367 6368 if (unlikely(curr->lockdep_depth)) { 6369 if (!debug_locks_off()) 6370 return; 6371 pr_warn("\n"); 6372 pr_warn("================================================\n"); 6373 pr_warn("WARNING: lock held when returning to user space!\n"); 6374 print_kernel_ident(); 6375 pr_warn("------------------------------------------------\n"); 6376 pr_warn("%s/%d is leaving the kernel with locks still held!\n", 6377 curr->comm, curr->pid); 6378 lockdep_print_held_locks(curr); 6379 } 6380 6381 /* 6382 * The lock history for each syscall should be independent. So wipe the 6383 * slate clean on return to userspace. 6384 */ 6385 lockdep_invariant_state(false); 6386 } 6387 6388 void lockdep_rcu_suspicious(const char *file, const int line, const char *s) 6389 { 6390 struct task_struct *curr = current; 6391 6392 /* Note: the following can be executed concurrently, so be careful. */ 6393 pr_warn("\n"); 6394 pr_warn("=============================\n"); 6395 pr_warn("WARNING: suspicious RCU usage\n"); 6396 print_kernel_ident(); 6397 pr_warn("-----------------------------\n"); 6398 pr_warn("%s:%d %s!\n", file, line, s); 6399 pr_warn("\nother info that might help us debug this:\n\n"); 6400 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n", 6401 !rcu_lockdep_current_cpu_online() 6402 ? "RCU used illegally from offline CPU!\n" 6403 : "", 6404 rcu_scheduler_active, debug_locks); 6405 6406 /* 6407 * If a CPU is in the RCU-free window in idle (ie: in the section 6408 * between rcu_idle_enter() and rcu_idle_exit(), then RCU 6409 * considers that CPU to be in an "extended quiescent state", 6410 * which means that RCU will be completely ignoring that CPU. 6411 * Therefore, rcu_read_lock() and friends have absolutely no 6412 * effect on a CPU running in that state. In other words, even if 6413 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well 6414 * delete data structures out from under it. RCU really has no 6415 * choice here: we need to keep an RCU-free window in idle where 6416 * the CPU may possibly enter into low power mode. This way we can 6417 * notice an extended quiescent state to other CPUs that started a grace 6418 * period. Otherwise we would delay any grace period as long as we run 6419 * in the idle task. 6420 * 6421 * So complain bitterly if someone does call rcu_read_lock(), 6422 * rcu_read_lock_bh() and so on from extended quiescent states. 6423 */ 6424 if (!rcu_is_watching()) 6425 pr_warn("RCU used illegally from extended quiescent state!\n"); 6426 6427 lockdep_print_held_locks(curr); 6428 pr_warn("\nstack backtrace:\n"); 6429 dump_stack(); 6430 } 6431 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); 6432