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