1 /* 2 * kernel/lockdep.c 3 * 4 * Runtime locking correctness validator 5 * 6 * Started by Ingo Molnar: 7 * 8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra 10 * 11 * this code maps all the lock dependencies as they occur in a live kernel 12 * and will warn about the following classes of locking bugs: 13 * 14 * - lock inversion scenarios 15 * - circular lock dependencies 16 * - hardirq/softirq safe/unsafe locking bugs 17 * 18 * Bugs are reported even if the current locking scenario does not cause 19 * any deadlock at this point. 20 * 21 * I.e. if anytime in the past two locks were taken in a different order, 22 * even if it happened for another task, even if those were different 23 * locks (but of the same class as this lock), this code will detect it. 24 * 25 * Thanks to Arjan van de Ven for coming up with the initial idea of 26 * mapping lock dependencies runtime. 27 */ 28 #define DISABLE_BRANCH_PROFILING 29 #include <linux/mutex.h> 30 #include <linux/sched.h> 31 #include <linux/sched/clock.h> 32 #include <linux/sched/task.h> 33 #include <linux/sched/mm.h> 34 #include <linux/delay.h> 35 #include <linux/module.h> 36 #include <linux/proc_fs.h> 37 #include <linux/seq_file.h> 38 #include <linux/spinlock.h> 39 #include <linux/kallsyms.h> 40 #include <linux/interrupt.h> 41 #include <linux/stacktrace.h> 42 #include <linux/debug_locks.h> 43 #include <linux/irqflags.h> 44 #include <linux/utsname.h> 45 #include <linux/hash.h> 46 #include <linux/ftrace.h> 47 #include <linux/stringify.h> 48 #include <linux/bitops.h> 49 #include <linux/gfp.h> 50 #include <linux/random.h> 51 #include <linux/jhash.h> 52 53 #include <asm/sections.h> 54 55 #include "lockdep_internals.h" 56 57 #define CREATE_TRACE_POINTS 58 #include <trace/events/lock.h> 59 60 #ifdef CONFIG_PROVE_LOCKING 61 int prove_locking = 1; 62 module_param(prove_locking, int, 0644); 63 #else 64 #define prove_locking 0 65 #endif 66 67 #ifdef CONFIG_LOCK_STAT 68 int lock_stat = 1; 69 module_param(lock_stat, int, 0644); 70 #else 71 #define lock_stat 0 72 #endif 73 74 /* 75 * lockdep_lock: protects the lockdep graph, the hashes and the 76 * class/list/hash allocators. 77 * 78 * This is one of the rare exceptions where it's justified 79 * to use a raw spinlock - we really dont want the spinlock 80 * code to recurse back into the lockdep code... 81 */ 82 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 83 84 static int graph_lock(void) 85 { 86 arch_spin_lock(&lockdep_lock); 87 /* 88 * Make sure that if another CPU detected a bug while 89 * walking the graph we dont change it (while the other 90 * CPU is busy printing out stuff with the graph lock 91 * dropped already) 92 */ 93 if (!debug_locks) { 94 arch_spin_unlock(&lockdep_lock); 95 return 0; 96 } 97 /* prevent any recursions within lockdep from causing deadlocks */ 98 current->lockdep_recursion++; 99 return 1; 100 } 101 102 static inline int graph_unlock(void) 103 { 104 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) { 105 /* 106 * The lockdep graph lock isn't locked while we expect it to 107 * be, we're confused now, bye! 108 */ 109 return DEBUG_LOCKS_WARN_ON(1); 110 } 111 112 current->lockdep_recursion--; 113 arch_spin_unlock(&lockdep_lock); 114 return 0; 115 } 116 117 /* 118 * Turn lock debugging off and return with 0 if it was off already, 119 * and also release the graph lock: 120 */ 121 static inline int debug_locks_off_graph_unlock(void) 122 { 123 int ret = debug_locks_off(); 124 125 arch_spin_unlock(&lockdep_lock); 126 127 return ret; 128 } 129 130 unsigned long nr_list_entries; 131 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; 132 133 /* 134 * All data structures here are protected by the global debug_lock. 135 * 136 * Mutex key structs only get allocated, once during bootup, and never 137 * get freed - this significantly simplifies the debugging code. 138 */ 139 unsigned long nr_lock_classes; 140 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; 141 142 static inline struct lock_class *hlock_class(struct held_lock *hlock) 143 { 144 if (!hlock->class_idx) { 145 /* 146 * Someone passed in garbage, we give up. 147 */ 148 DEBUG_LOCKS_WARN_ON(1); 149 return NULL; 150 } 151 return lock_classes + hlock->class_idx - 1; 152 } 153 154 #ifdef CONFIG_LOCK_STAT 155 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats); 156 157 static inline u64 lockstat_clock(void) 158 { 159 return local_clock(); 160 } 161 162 static int lock_point(unsigned long points[], unsigned long ip) 163 { 164 int i; 165 166 for (i = 0; i < LOCKSTAT_POINTS; i++) { 167 if (points[i] == 0) { 168 points[i] = ip; 169 break; 170 } 171 if (points[i] == ip) 172 break; 173 } 174 175 return i; 176 } 177 178 static void lock_time_inc(struct lock_time *lt, u64 time) 179 { 180 if (time > lt->max) 181 lt->max = time; 182 183 if (time < lt->min || !lt->nr) 184 lt->min = time; 185 186 lt->total += time; 187 lt->nr++; 188 } 189 190 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) 191 { 192 if (!src->nr) 193 return; 194 195 if (src->max > dst->max) 196 dst->max = src->max; 197 198 if (src->min < dst->min || !dst->nr) 199 dst->min = src->min; 200 201 dst->total += src->total; 202 dst->nr += src->nr; 203 } 204 205 struct lock_class_stats lock_stats(struct lock_class *class) 206 { 207 struct lock_class_stats stats; 208 int cpu, i; 209 210 memset(&stats, 0, sizeof(struct lock_class_stats)); 211 for_each_possible_cpu(cpu) { 212 struct lock_class_stats *pcs = 213 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; 214 215 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) 216 stats.contention_point[i] += pcs->contention_point[i]; 217 218 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) 219 stats.contending_point[i] += pcs->contending_point[i]; 220 221 lock_time_add(&pcs->read_waittime, &stats.read_waittime); 222 lock_time_add(&pcs->write_waittime, &stats.write_waittime); 223 224 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); 225 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); 226 227 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) 228 stats.bounces[i] += pcs->bounces[i]; 229 } 230 231 return stats; 232 } 233 234 void clear_lock_stats(struct lock_class *class) 235 { 236 int cpu; 237 238 for_each_possible_cpu(cpu) { 239 struct lock_class_stats *cpu_stats = 240 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; 241 242 memset(cpu_stats, 0, sizeof(struct lock_class_stats)); 243 } 244 memset(class->contention_point, 0, sizeof(class->contention_point)); 245 memset(class->contending_point, 0, sizeof(class->contending_point)); 246 } 247 248 static struct lock_class_stats *get_lock_stats(struct lock_class *class) 249 { 250 return &get_cpu_var(cpu_lock_stats)[class - lock_classes]; 251 } 252 253 static void put_lock_stats(struct lock_class_stats *stats) 254 { 255 put_cpu_var(cpu_lock_stats); 256 } 257 258 static void lock_release_holdtime(struct held_lock *hlock) 259 { 260 struct lock_class_stats *stats; 261 u64 holdtime; 262 263 if (!lock_stat) 264 return; 265 266 holdtime = lockstat_clock() - hlock->holdtime_stamp; 267 268 stats = get_lock_stats(hlock_class(hlock)); 269 if (hlock->read) 270 lock_time_inc(&stats->read_holdtime, holdtime); 271 else 272 lock_time_inc(&stats->write_holdtime, holdtime); 273 put_lock_stats(stats); 274 } 275 #else 276 static inline void lock_release_holdtime(struct held_lock *hlock) 277 { 278 } 279 #endif 280 281 /* 282 * We keep a global list of all lock classes. The list only grows, 283 * never shrinks. The list is only accessed with the lockdep 284 * spinlock lock held. 285 */ 286 LIST_HEAD(all_lock_classes); 287 288 /* 289 * The lockdep classes are in a hash-table as well, for fast lookup: 290 */ 291 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) 292 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS) 293 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) 294 #define classhashentry(key) (classhash_table + __classhashfn((key))) 295 296 static struct hlist_head classhash_table[CLASSHASH_SIZE]; 297 298 /* 299 * We put the lock dependency chains into a hash-table as well, to cache 300 * their existence: 301 */ 302 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1) 303 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS) 304 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) 305 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) 306 307 static struct hlist_head chainhash_table[CHAINHASH_SIZE]; 308 309 /* 310 * The hash key of the lock dependency chains is a hash itself too: 311 * it's a hash of all locks taken up to that lock, including that lock. 312 * It's a 64-bit hash, because it's important for the keys to be 313 * unique. 314 */ 315 static inline u64 iterate_chain_key(u64 key, u32 idx) 316 { 317 u32 k0 = key, k1 = key >> 32; 318 319 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */ 320 321 return k0 | (u64)k1 << 32; 322 } 323 324 void lockdep_off(void) 325 { 326 current->lockdep_recursion++; 327 } 328 EXPORT_SYMBOL(lockdep_off); 329 330 void lockdep_on(void) 331 { 332 current->lockdep_recursion--; 333 } 334 EXPORT_SYMBOL(lockdep_on); 335 336 /* 337 * Debugging switches: 338 */ 339 340 #define VERBOSE 0 341 #define VERY_VERBOSE 0 342 343 #if VERBOSE 344 # define HARDIRQ_VERBOSE 1 345 # define SOFTIRQ_VERBOSE 1 346 #else 347 # define HARDIRQ_VERBOSE 0 348 # define SOFTIRQ_VERBOSE 0 349 #endif 350 351 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE 352 /* 353 * Quick filtering for interesting events: 354 */ 355 static int class_filter(struct lock_class *class) 356 { 357 #if 0 358 /* Example */ 359 if (class->name_version == 1 && 360 !strcmp(class->name, "lockname")) 361 return 1; 362 if (class->name_version == 1 && 363 !strcmp(class->name, "&struct->lockfield")) 364 return 1; 365 #endif 366 /* Filter everything else. 1 would be to allow everything else */ 367 return 0; 368 } 369 #endif 370 371 static int verbose(struct lock_class *class) 372 { 373 #if VERBOSE 374 return class_filter(class); 375 #endif 376 return 0; 377 } 378 379 /* 380 * Stack-trace: tightly packed array of stack backtrace 381 * addresses. Protected by the graph_lock. 382 */ 383 unsigned long nr_stack_trace_entries; 384 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; 385 386 static void print_lockdep_off(const char *bug_msg) 387 { 388 printk(KERN_DEBUG "%s\n", bug_msg); 389 printk(KERN_DEBUG "turning off the locking correctness validator.\n"); 390 #ifdef CONFIG_LOCK_STAT 391 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n"); 392 #endif 393 } 394 395 static int save_trace(struct stack_trace *trace) 396 { 397 trace->nr_entries = 0; 398 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries; 399 trace->entries = stack_trace + nr_stack_trace_entries; 400 401 trace->skip = 3; 402 403 save_stack_trace(trace); 404 405 /* 406 * Some daft arches put -1 at the end to indicate its a full trace. 407 * 408 * <rant> this is buggy anyway, since it takes a whole extra entry so a 409 * complete trace that maxes out the entries provided will be reported 410 * as incomplete, friggin useless </rant> 411 */ 412 if (trace->nr_entries != 0 && 413 trace->entries[trace->nr_entries-1] == ULONG_MAX) 414 trace->nr_entries--; 415 416 trace->max_entries = trace->nr_entries; 417 418 nr_stack_trace_entries += trace->nr_entries; 419 420 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) { 421 if (!debug_locks_off_graph_unlock()) 422 return 0; 423 424 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); 425 dump_stack(); 426 427 return 0; 428 } 429 430 return 1; 431 } 432 433 unsigned int nr_hardirq_chains; 434 unsigned int nr_softirq_chains; 435 unsigned int nr_process_chains; 436 unsigned int max_lockdep_depth; 437 438 #ifdef CONFIG_DEBUG_LOCKDEP 439 /* 440 * Various lockdep statistics: 441 */ 442 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats); 443 #endif 444 445 /* 446 * Locking printouts: 447 */ 448 449 #define __USAGE(__STATE) \ 450 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \ 451 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \ 452 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\ 453 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R", 454 455 static const char *usage_str[] = 456 { 457 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE) 458 #include "lockdep_states.h" 459 #undef LOCKDEP_STATE 460 [LOCK_USED] = "INITIAL USE", 461 }; 462 463 const char * __get_key_name(struct lockdep_subclass_key *key, char *str) 464 { 465 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); 466 } 467 468 static inline unsigned long lock_flag(enum lock_usage_bit bit) 469 { 470 return 1UL << bit; 471 } 472 473 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit) 474 { 475 char c = '.'; 476 477 if (class->usage_mask & lock_flag(bit + 2)) 478 c = '+'; 479 if (class->usage_mask & lock_flag(bit)) { 480 c = '-'; 481 if (class->usage_mask & lock_flag(bit + 2)) 482 c = '?'; 483 } 484 485 return c; 486 } 487 488 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]) 489 { 490 int i = 0; 491 492 #define LOCKDEP_STATE(__STATE) \ 493 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \ 494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ); 495 #include "lockdep_states.h" 496 #undef LOCKDEP_STATE 497 498 usage[i] = '\0'; 499 } 500 501 static void __print_lock_name(struct lock_class *class) 502 { 503 char str[KSYM_NAME_LEN]; 504 const char *name; 505 506 name = class->name; 507 if (!name) { 508 name = __get_key_name(class->key, str); 509 printk(KERN_CONT "%s", name); 510 } else { 511 printk(KERN_CONT "%s", name); 512 if (class->name_version > 1) 513 printk(KERN_CONT "#%d", class->name_version); 514 if (class->subclass) 515 printk(KERN_CONT "/%d", class->subclass); 516 } 517 } 518 519 static void print_lock_name(struct lock_class *class) 520 { 521 char usage[LOCK_USAGE_CHARS]; 522 523 get_usage_chars(class, usage); 524 525 printk(KERN_CONT " ("); 526 __print_lock_name(class); 527 printk(KERN_CONT "){%s}", usage); 528 } 529 530 static void print_lockdep_cache(struct lockdep_map *lock) 531 { 532 const char *name; 533 char str[KSYM_NAME_LEN]; 534 535 name = lock->name; 536 if (!name) 537 name = __get_key_name(lock->key->subkeys, str); 538 539 printk(KERN_CONT "%s", name); 540 } 541 542 static void print_lock(struct held_lock *hlock) 543 { 544 /* 545 * We can be called locklessly through debug_show_all_locks() so be 546 * extra careful, the hlock might have been released and cleared. 547 */ 548 unsigned int class_idx = hlock->class_idx; 549 550 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */ 551 barrier(); 552 553 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) { 554 printk(KERN_CONT "<RELEASED>\n"); 555 return; 556 } 557 558 print_lock_name(lock_classes + class_idx - 1); 559 printk(KERN_CONT ", at: [<%p>] %pS\n", 560 (void *)hlock->acquire_ip, (void *)hlock->acquire_ip); 561 } 562 563 static void lockdep_print_held_locks(struct task_struct *curr) 564 { 565 int i, depth = curr->lockdep_depth; 566 567 if (!depth) { 568 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr)); 569 return; 570 } 571 printk("%d lock%s held by %s/%d:\n", 572 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr)); 573 574 for (i = 0; i < depth; i++) { 575 printk(" #%d: ", i); 576 print_lock(curr->held_locks + i); 577 } 578 } 579 580 static void print_kernel_ident(void) 581 { 582 printk("%s %.*s %s\n", init_utsname()->release, 583 (int)strcspn(init_utsname()->version, " "), 584 init_utsname()->version, 585 print_tainted()); 586 } 587 588 static int very_verbose(struct lock_class *class) 589 { 590 #if VERY_VERBOSE 591 return class_filter(class); 592 #endif 593 return 0; 594 } 595 596 /* 597 * Is this the address of a static object: 598 */ 599 #ifdef __KERNEL__ 600 static int static_obj(void *obj) 601 { 602 unsigned long start = (unsigned long) &_stext, 603 end = (unsigned long) &_end, 604 addr = (unsigned long) obj; 605 606 /* 607 * static variable? 608 */ 609 if ((addr >= start) && (addr < end)) 610 return 1; 611 612 if (arch_is_kernel_data(addr)) 613 return 1; 614 615 /* 616 * in-kernel percpu var? 617 */ 618 if (is_kernel_percpu_address(addr)) 619 return 1; 620 621 /* 622 * module static or percpu var? 623 */ 624 return is_module_address(addr) || is_module_percpu_address(addr); 625 } 626 #endif 627 628 /* 629 * To make lock name printouts unique, we calculate a unique 630 * class->name_version generation counter: 631 */ 632 static int count_matching_names(struct lock_class *new_class) 633 { 634 struct lock_class *class; 635 int count = 0; 636 637 if (!new_class->name) 638 return 0; 639 640 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) { 641 if (new_class->key - new_class->subclass == class->key) 642 return class->name_version; 643 if (class->name && !strcmp(class->name, new_class->name)) 644 count = max(count, class->name_version); 645 } 646 647 return count + 1; 648 } 649 650 /* 651 * Register a lock's class in the hash-table, if the class is not present 652 * yet. Otherwise we look it up. We cache the result in the lock object 653 * itself, so actual lookup of the hash should be once per lock object. 654 */ 655 static inline struct lock_class * 656 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass) 657 { 658 struct lockdep_subclass_key *key; 659 struct hlist_head *hash_head; 660 struct lock_class *class; 661 bool is_static = false; 662 663 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) { 664 debug_locks_off(); 665 printk(KERN_ERR 666 "BUG: looking up invalid subclass: %u\n", subclass); 667 printk(KERN_ERR 668 "turning off the locking correctness validator.\n"); 669 dump_stack(); 670 return NULL; 671 } 672 673 /* 674 * Static locks do not have their class-keys yet - for them the key 675 * is the lock object itself. If the lock is in the per cpu area, 676 * the canonical address of the lock (per cpu offset removed) is 677 * used. 678 */ 679 if (unlikely(!lock->key)) { 680 unsigned long can_addr, addr = (unsigned long)lock; 681 682 if (__is_kernel_percpu_address(addr, &can_addr)) 683 lock->key = (void *)can_addr; 684 else if (__is_module_percpu_address(addr, &can_addr)) 685 lock->key = (void *)can_addr; 686 else if (static_obj(lock)) 687 lock->key = (void *)lock; 688 else 689 return ERR_PTR(-EINVAL); 690 is_static = true; 691 } 692 693 /* 694 * NOTE: the class-key must be unique. For dynamic locks, a static 695 * lock_class_key variable is passed in through the mutex_init() 696 * (or spin_lock_init()) call - which acts as the key. For static 697 * locks we use the lock object itself as the key. 698 */ 699 BUILD_BUG_ON(sizeof(struct lock_class_key) > 700 sizeof(struct lockdep_map)); 701 702 key = lock->key->subkeys + subclass; 703 704 hash_head = classhashentry(key); 705 706 /* 707 * We do an RCU walk of the hash, see lockdep_free_key_range(). 708 */ 709 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 710 return NULL; 711 712 hlist_for_each_entry_rcu(class, hash_head, hash_entry) { 713 if (class->key == key) { 714 /* 715 * Huh! same key, different name? Did someone trample 716 * on some memory? We're most confused. 717 */ 718 WARN_ON_ONCE(class->name != lock->name); 719 return class; 720 } 721 } 722 723 return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL); 724 } 725 726 /* 727 * Register a lock's class in the hash-table, if the class is not present 728 * yet. Otherwise we look it up. We cache the result in the lock object 729 * itself, so actual lookup of the hash should be once per lock object. 730 */ 731 static struct lock_class * 732 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) 733 { 734 struct lockdep_subclass_key *key; 735 struct hlist_head *hash_head; 736 struct lock_class *class; 737 738 DEBUG_LOCKS_WARN_ON(!irqs_disabled()); 739 740 class = look_up_lock_class(lock, subclass); 741 if (likely(!IS_ERR_OR_NULL(class))) 742 goto out_set_class_cache; 743 744 /* 745 * Debug-check: all keys must be persistent! 746 */ 747 if (IS_ERR(class)) { 748 debug_locks_off(); 749 printk("INFO: trying to register non-static key.\n"); 750 printk("the code is fine but needs lockdep annotation.\n"); 751 printk("turning off the locking correctness validator.\n"); 752 dump_stack(); 753 return NULL; 754 } 755 756 key = lock->key->subkeys + subclass; 757 hash_head = classhashentry(key); 758 759 if (!graph_lock()) { 760 return NULL; 761 } 762 /* 763 * We have to do the hash-walk again, to avoid races 764 * with another CPU: 765 */ 766 hlist_for_each_entry_rcu(class, hash_head, hash_entry) { 767 if (class->key == key) 768 goto out_unlock_set; 769 } 770 771 /* 772 * Allocate a new key from the static array, and add it to 773 * the hash: 774 */ 775 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) { 776 if (!debug_locks_off_graph_unlock()) { 777 return NULL; 778 } 779 780 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); 781 dump_stack(); 782 return NULL; 783 } 784 class = lock_classes + nr_lock_classes++; 785 debug_atomic_inc(nr_unused_locks); 786 class->key = key; 787 class->name = lock->name; 788 class->subclass = subclass; 789 INIT_LIST_HEAD(&class->lock_entry); 790 INIT_LIST_HEAD(&class->locks_before); 791 INIT_LIST_HEAD(&class->locks_after); 792 class->name_version = count_matching_names(class); 793 /* 794 * We use RCU's safe list-add method to make 795 * parallel walking of the hash-list safe: 796 */ 797 hlist_add_head_rcu(&class->hash_entry, hash_head); 798 /* 799 * Add it to the global list of classes: 800 */ 801 list_add_tail_rcu(&class->lock_entry, &all_lock_classes); 802 803 if (verbose(class)) { 804 graph_unlock(); 805 806 printk("\nnew class %p: %s", class->key, class->name); 807 if (class->name_version > 1) 808 printk(KERN_CONT "#%d", class->name_version); 809 printk(KERN_CONT "\n"); 810 dump_stack(); 811 812 if (!graph_lock()) { 813 return NULL; 814 } 815 } 816 out_unlock_set: 817 graph_unlock(); 818 819 out_set_class_cache: 820 if (!subclass || force) 821 lock->class_cache[0] = class; 822 else if (subclass < NR_LOCKDEP_CACHING_CLASSES) 823 lock->class_cache[subclass] = class; 824 825 /* 826 * Hash collision, did we smoke some? We found a class with a matching 827 * hash but the subclass -- which is hashed in -- didn't match. 828 */ 829 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) 830 return NULL; 831 832 return class; 833 } 834 835 #ifdef CONFIG_PROVE_LOCKING 836 /* 837 * Allocate a lockdep entry. (assumes the graph_lock held, returns 838 * with NULL on failure) 839 */ 840 static struct lock_list *alloc_list_entry(void) 841 { 842 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) { 843 if (!debug_locks_off_graph_unlock()) 844 return NULL; 845 846 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); 847 dump_stack(); 848 return NULL; 849 } 850 return list_entries + nr_list_entries++; 851 } 852 853 /* 854 * Add a new dependency to the head of the list: 855 */ 856 static int add_lock_to_list(struct lock_class *this, struct list_head *head, 857 unsigned long ip, int distance, 858 struct stack_trace *trace) 859 { 860 struct lock_list *entry; 861 /* 862 * Lock not present yet - get a new dependency struct and 863 * add it to the list: 864 */ 865 entry = alloc_list_entry(); 866 if (!entry) 867 return 0; 868 869 entry->class = this; 870 entry->distance = distance; 871 entry->trace = *trace; 872 /* 873 * Both allocation and removal are done under the graph lock; but 874 * iteration is under RCU-sched; see look_up_lock_class() and 875 * lockdep_free_key_range(). 876 */ 877 list_add_tail_rcu(&entry->entry, head); 878 879 return 1; 880 } 881 882 /* 883 * For good efficiency of modular, we use power of 2 884 */ 885 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL 886 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1) 887 888 /* 889 * The circular_queue and helpers is used to implement the 890 * breadth-first search(BFS)algorithem, by which we can build 891 * the shortest path from the next lock to be acquired to the 892 * previous held lock if there is a circular between them. 893 */ 894 struct circular_queue { 895 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE]; 896 unsigned int front, rear; 897 }; 898 899 static struct circular_queue lock_cq; 900 901 unsigned int max_bfs_queue_depth; 902 903 static unsigned int lockdep_dependency_gen_id; 904 905 static inline void __cq_init(struct circular_queue *cq) 906 { 907 cq->front = cq->rear = 0; 908 lockdep_dependency_gen_id++; 909 } 910 911 static inline int __cq_empty(struct circular_queue *cq) 912 { 913 return (cq->front == cq->rear); 914 } 915 916 static inline int __cq_full(struct circular_queue *cq) 917 { 918 return ((cq->rear + 1) & CQ_MASK) == cq->front; 919 } 920 921 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem) 922 { 923 if (__cq_full(cq)) 924 return -1; 925 926 cq->element[cq->rear] = elem; 927 cq->rear = (cq->rear + 1) & CQ_MASK; 928 return 0; 929 } 930 931 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem) 932 { 933 if (__cq_empty(cq)) 934 return -1; 935 936 *elem = cq->element[cq->front]; 937 cq->front = (cq->front + 1) & CQ_MASK; 938 return 0; 939 } 940 941 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq) 942 { 943 return (cq->rear - cq->front) & CQ_MASK; 944 } 945 946 static inline void mark_lock_accessed(struct lock_list *lock, 947 struct lock_list *parent) 948 { 949 unsigned long nr; 950 951 nr = lock - list_entries; 952 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */ 953 lock->parent = parent; 954 lock->class->dep_gen_id = lockdep_dependency_gen_id; 955 } 956 957 static inline unsigned long lock_accessed(struct lock_list *lock) 958 { 959 unsigned long nr; 960 961 nr = lock - list_entries; 962 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */ 963 return lock->class->dep_gen_id == lockdep_dependency_gen_id; 964 } 965 966 static inline struct lock_list *get_lock_parent(struct lock_list *child) 967 { 968 return child->parent; 969 } 970 971 static inline int get_lock_depth(struct lock_list *child) 972 { 973 int depth = 0; 974 struct lock_list *parent; 975 976 while ((parent = get_lock_parent(child))) { 977 child = parent; 978 depth++; 979 } 980 return depth; 981 } 982 983 static int __bfs(struct lock_list *source_entry, 984 void *data, 985 int (*match)(struct lock_list *entry, void *data), 986 struct lock_list **target_entry, 987 int forward) 988 { 989 struct lock_list *entry; 990 struct list_head *head; 991 struct circular_queue *cq = &lock_cq; 992 int ret = 1; 993 994 if (match(source_entry, data)) { 995 *target_entry = source_entry; 996 ret = 0; 997 goto exit; 998 } 999 1000 if (forward) 1001 head = &source_entry->class->locks_after; 1002 else 1003 head = &source_entry->class->locks_before; 1004 1005 if (list_empty(head)) 1006 goto exit; 1007 1008 __cq_init(cq); 1009 __cq_enqueue(cq, (unsigned long)source_entry); 1010 1011 while (!__cq_empty(cq)) { 1012 struct lock_list *lock; 1013 1014 __cq_dequeue(cq, (unsigned long *)&lock); 1015 1016 if (!lock->class) { 1017 ret = -2; 1018 goto exit; 1019 } 1020 1021 if (forward) 1022 head = &lock->class->locks_after; 1023 else 1024 head = &lock->class->locks_before; 1025 1026 DEBUG_LOCKS_WARN_ON(!irqs_disabled()); 1027 1028 list_for_each_entry_rcu(entry, head, entry) { 1029 if (!lock_accessed(entry)) { 1030 unsigned int cq_depth; 1031 mark_lock_accessed(entry, lock); 1032 if (match(entry, data)) { 1033 *target_entry = entry; 1034 ret = 0; 1035 goto exit; 1036 } 1037 1038 if (__cq_enqueue(cq, (unsigned long)entry)) { 1039 ret = -1; 1040 goto exit; 1041 } 1042 cq_depth = __cq_get_elem_count(cq); 1043 if (max_bfs_queue_depth < cq_depth) 1044 max_bfs_queue_depth = cq_depth; 1045 } 1046 } 1047 } 1048 exit: 1049 return ret; 1050 } 1051 1052 static inline int __bfs_forwards(struct lock_list *src_entry, 1053 void *data, 1054 int (*match)(struct lock_list *entry, void *data), 1055 struct lock_list **target_entry) 1056 { 1057 return __bfs(src_entry, data, match, target_entry, 1); 1058 1059 } 1060 1061 static inline int __bfs_backwards(struct lock_list *src_entry, 1062 void *data, 1063 int (*match)(struct lock_list *entry, void *data), 1064 struct lock_list **target_entry) 1065 { 1066 return __bfs(src_entry, data, match, target_entry, 0); 1067 1068 } 1069 1070 /* 1071 * Recursive, forwards-direction lock-dependency checking, used for 1072 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe 1073 * checking. 1074 */ 1075 1076 /* 1077 * Print a dependency chain entry (this is only done when a deadlock 1078 * has been detected): 1079 */ 1080 static noinline int 1081 print_circular_bug_entry(struct lock_list *target, int depth) 1082 { 1083 if (debug_locks_silent) 1084 return 0; 1085 printk("\n-> #%u", depth); 1086 print_lock_name(target->class); 1087 printk(KERN_CONT ":\n"); 1088 print_stack_trace(&target->trace, 6); 1089 1090 return 0; 1091 } 1092 1093 static void 1094 print_circular_lock_scenario(struct held_lock *src, 1095 struct held_lock *tgt, 1096 struct lock_list *prt) 1097 { 1098 struct lock_class *source = hlock_class(src); 1099 struct lock_class *target = hlock_class(tgt); 1100 struct lock_class *parent = prt->class; 1101 1102 /* 1103 * A direct locking problem where unsafe_class lock is taken 1104 * directly by safe_class lock, then all we need to show 1105 * is the deadlock scenario, as it is obvious that the 1106 * unsafe lock is taken under the safe lock. 1107 * 1108 * But if there is a chain instead, where the safe lock takes 1109 * an intermediate lock (middle_class) where this lock is 1110 * not the same as the safe lock, then the lock chain is 1111 * used to describe the problem. Otherwise we would need 1112 * to show a different CPU case for each link in the chain 1113 * from the safe_class lock to the unsafe_class lock. 1114 */ 1115 if (parent != source) { 1116 printk("Chain exists of:\n "); 1117 __print_lock_name(source); 1118 printk(KERN_CONT " --> "); 1119 __print_lock_name(parent); 1120 printk(KERN_CONT " --> "); 1121 __print_lock_name(target); 1122 printk(KERN_CONT "\n\n"); 1123 } 1124 1125 printk(" Possible unsafe locking scenario:\n\n"); 1126 printk(" CPU0 CPU1\n"); 1127 printk(" ---- ----\n"); 1128 printk(" lock("); 1129 __print_lock_name(target); 1130 printk(KERN_CONT ");\n"); 1131 printk(" lock("); 1132 __print_lock_name(parent); 1133 printk(KERN_CONT ");\n"); 1134 printk(" lock("); 1135 __print_lock_name(target); 1136 printk(KERN_CONT ");\n"); 1137 printk(" lock("); 1138 __print_lock_name(source); 1139 printk(KERN_CONT ");\n"); 1140 printk("\n *** DEADLOCK ***\n\n"); 1141 } 1142 1143 /* 1144 * When a circular dependency is detected, print the 1145 * header first: 1146 */ 1147 static noinline int 1148 print_circular_bug_header(struct lock_list *entry, unsigned int depth, 1149 struct held_lock *check_src, 1150 struct held_lock *check_tgt) 1151 { 1152 struct task_struct *curr = current; 1153 1154 if (debug_locks_silent) 1155 return 0; 1156 1157 pr_warn("\n"); 1158 pr_warn("======================================================\n"); 1159 pr_warn("WARNING: possible circular locking dependency detected\n"); 1160 print_kernel_ident(); 1161 pr_warn("------------------------------------------------------\n"); 1162 pr_warn("%s/%d is trying to acquire lock:\n", 1163 curr->comm, task_pid_nr(curr)); 1164 print_lock(check_src); 1165 1166 pr_warn("\nbut task is already holding lock:\n"); 1167 1168 print_lock(check_tgt); 1169 pr_warn("\nwhich lock already depends on the new lock.\n\n"); 1170 pr_warn("\nthe existing dependency chain (in reverse order) is:\n"); 1171 1172 print_circular_bug_entry(entry, depth); 1173 1174 return 0; 1175 } 1176 1177 static inline int class_equal(struct lock_list *entry, void *data) 1178 { 1179 return entry->class == data; 1180 } 1181 1182 static noinline int print_circular_bug(struct lock_list *this, 1183 struct lock_list *target, 1184 struct held_lock *check_src, 1185 struct held_lock *check_tgt, 1186 struct stack_trace *trace) 1187 { 1188 struct task_struct *curr = current; 1189 struct lock_list *parent; 1190 struct lock_list *first_parent; 1191 int depth; 1192 1193 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1194 return 0; 1195 1196 if (!save_trace(&this->trace)) 1197 return 0; 1198 1199 depth = get_lock_depth(target); 1200 1201 print_circular_bug_header(target, depth, check_src, check_tgt); 1202 1203 parent = get_lock_parent(target); 1204 first_parent = parent; 1205 1206 while (parent) { 1207 print_circular_bug_entry(parent, --depth); 1208 parent = get_lock_parent(parent); 1209 } 1210 1211 printk("\nother info that might help us debug this:\n\n"); 1212 print_circular_lock_scenario(check_src, check_tgt, 1213 first_parent); 1214 1215 lockdep_print_held_locks(curr); 1216 1217 printk("\nstack backtrace:\n"); 1218 dump_stack(); 1219 1220 return 0; 1221 } 1222 1223 static noinline int print_bfs_bug(int ret) 1224 { 1225 if (!debug_locks_off_graph_unlock()) 1226 return 0; 1227 1228 /* 1229 * Breadth-first-search failed, graph got corrupted? 1230 */ 1231 WARN(1, "lockdep bfs error:%d\n", ret); 1232 1233 return 0; 1234 } 1235 1236 static int noop_count(struct lock_list *entry, void *data) 1237 { 1238 (*(unsigned long *)data)++; 1239 return 0; 1240 } 1241 1242 static unsigned long __lockdep_count_forward_deps(struct lock_list *this) 1243 { 1244 unsigned long count = 0; 1245 struct lock_list *uninitialized_var(target_entry); 1246 1247 __bfs_forwards(this, (void *)&count, noop_count, &target_entry); 1248 1249 return count; 1250 } 1251 unsigned long lockdep_count_forward_deps(struct lock_class *class) 1252 { 1253 unsigned long ret, flags; 1254 struct lock_list this; 1255 1256 this.parent = NULL; 1257 this.class = class; 1258 1259 local_irq_save(flags); 1260 arch_spin_lock(&lockdep_lock); 1261 ret = __lockdep_count_forward_deps(&this); 1262 arch_spin_unlock(&lockdep_lock); 1263 local_irq_restore(flags); 1264 1265 return ret; 1266 } 1267 1268 static unsigned long __lockdep_count_backward_deps(struct lock_list *this) 1269 { 1270 unsigned long count = 0; 1271 struct lock_list *uninitialized_var(target_entry); 1272 1273 __bfs_backwards(this, (void *)&count, noop_count, &target_entry); 1274 1275 return count; 1276 } 1277 1278 unsigned long lockdep_count_backward_deps(struct lock_class *class) 1279 { 1280 unsigned long ret, flags; 1281 struct lock_list this; 1282 1283 this.parent = NULL; 1284 this.class = class; 1285 1286 local_irq_save(flags); 1287 arch_spin_lock(&lockdep_lock); 1288 ret = __lockdep_count_backward_deps(&this); 1289 arch_spin_unlock(&lockdep_lock); 1290 local_irq_restore(flags); 1291 1292 return ret; 1293 } 1294 1295 /* 1296 * Prove that the dependency graph starting at <entry> can not 1297 * lead to <target>. Print an error and return 0 if it does. 1298 */ 1299 static noinline int 1300 check_noncircular(struct lock_list *root, struct lock_class *target, 1301 struct lock_list **target_entry) 1302 { 1303 int result; 1304 1305 debug_atomic_inc(nr_cyclic_checks); 1306 1307 result = __bfs_forwards(root, target, class_equal, target_entry); 1308 1309 return result; 1310 } 1311 1312 static noinline int 1313 check_redundant(struct lock_list *root, struct lock_class *target, 1314 struct lock_list **target_entry) 1315 { 1316 int result; 1317 1318 debug_atomic_inc(nr_redundant_checks); 1319 1320 result = __bfs_forwards(root, target, class_equal, target_entry); 1321 1322 return result; 1323 } 1324 1325 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) 1326 /* 1327 * Forwards and backwards subgraph searching, for the purposes of 1328 * proving that two subgraphs can be connected by a new dependency 1329 * without creating any illegal irq-safe -> irq-unsafe lock dependency. 1330 */ 1331 1332 static inline int usage_match(struct lock_list *entry, void *bit) 1333 { 1334 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit); 1335 } 1336 1337 1338 1339 /* 1340 * Find a node in the forwards-direction dependency sub-graph starting 1341 * at @root->class that matches @bit. 1342 * 1343 * Return 0 if such a node exists in the subgraph, and put that node 1344 * into *@target_entry. 1345 * 1346 * Return 1 otherwise and keep *@target_entry unchanged. 1347 * Return <0 on error. 1348 */ 1349 static int 1350 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit, 1351 struct lock_list **target_entry) 1352 { 1353 int result; 1354 1355 debug_atomic_inc(nr_find_usage_forwards_checks); 1356 1357 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry); 1358 1359 return result; 1360 } 1361 1362 /* 1363 * Find a node in the backwards-direction dependency sub-graph starting 1364 * at @root->class that matches @bit. 1365 * 1366 * Return 0 if such a node exists in the subgraph, and put that node 1367 * into *@target_entry. 1368 * 1369 * Return 1 otherwise and keep *@target_entry unchanged. 1370 * Return <0 on error. 1371 */ 1372 static int 1373 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit, 1374 struct lock_list **target_entry) 1375 { 1376 int result; 1377 1378 debug_atomic_inc(nr_find_usage_backwards_checks); 1379 1380 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry); 1381 1382 return result; 1383 } 1384 1385 static void print_lock_class_header(struct lock_class *class, int depth) 1386 { 1387 int bit; 1388 1389 printk("%*s->", depth, ""); 1390 print_lock_name(class); 1391 printk(KERN_CONT " ops: %lu", class->ops); 1392 printk(KERN_CONT " {\n"); 1393 1394 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) { 1395 if (class->usage_mask & (1 << bit)) { 1396 int len = depth; 1397 1398 len += printk("%*s %s", depth, "", usage_str[bit]); 1399 len += printk(KERN_CONT " at:\n"); 1400 print_stack_trace(class->usage_traces + bit, len); 1401 } 1402 } 1403 printk("%*s }\n", depth, ""); 1404 1405 printk("%*s ... key at: [<%p>] %pS\n", 1406 depth, "", class->key, class->key); 1407 } 1408 1409 /* 1410 * printk the shortest lock dependencies from @start to @end in reverse order: 1411 */ 1412 static void __used 1413 print_shortest_lock_dependencies(struct lock_list *leaf, 1414 struct lock_list *root) 1415 { 1416 struct lock_list *entry = leaf; 1417 int depth; 1418 1419 /*compute depth from generated tree by BFS*/ 1420 depth = get_lock_depth(leaf); 1421 1422 do { 1423 print_lock_class_header(entry->class, depth); 1424 printk("%*s ... acquired at:\n", depth, ""); 1425 print_stack_trace(&entry->trace, 2); 1426 printk("\n"); 1427 1428 if (depth == 0 && (entry != root)) { 1429 printk("lockdep:%s bad path found in chain graph\n", __func__); 1430 break; 1431 } 1432 1433 entry = get_lock_parent(entry); 1434 depth--; 1435 } while (entry && (depth >= 0)); 1436 1437 return; 1438 } 1439 1440 static void 1441 print_irq_lock_scenario(struct lock_list *safe_entry, 1442 struct lock_list *unsafe_entry, 1443 struct lock_class *prev_class, 1444 struct lock_class *next_class) 1445 { 1446 struct lock_class *safe_class = safe_entry->class; 1447 struct lock_class *unsafe_class = unsafe_entry->class; 1448 struct lock_class *middle_class = prev_class; 1449 1450 if (middle_class == safe_class) 1451 middle_class = next_class; 1452 1453 /* 1454 * A direct locking problem where unsafe_class lock is taken 1455 * directly by safe_class lock, then all we need to show 1456 * is the deadlock scenario, as it is obvious that the 1457 * unsafe lock is taken under the safe lock. 1458 * 1459 * But if there is a chain instead, where the safe lock takes 1460 * an intermediate lock (middle_class) where this lock is 1461 * not the same as the safe lock, then the lock chain is 1462 * used to describe the problem. Otherwise we would need 1463 * to show a different CPU case for each link in the chain 1464 * from the safe_class lock to the unsafe_class lock. 1465 */ 1466 if (middle_class != unsafe_class) { 1467 printk("Chain exists of:\n "); 1468 __print_lock_name(safe_class); 1469 printk(KERN_CONT " --> "); 1470 __print_lock_name(middle_class); 1471 printk(KERN_CONT " --> "); 1472 __print_lock_name(unsafe_class); 1473 printk(KERN_CONT "\n\n"); 1474 } 1475 1476 printk(" Possible interrupt unsafe locking scenario:\n\n"); 1477 printk(" CPU0 CPU1\n"); 1478 printk(" ---- ----\n"); 1479 printk(" lock("); 1480 __print_lock_name(unsafe_class); 1481 printk(KERN_CONT ");\n"); 1482 printk(" local_irq_disable();\n"); 1483 printk(" lock("); 1484 __print_lock_name(safe_class); 1485 printk(KERN_CONT ");\n"); 1486 printk(" lock("); 1487 __print_lock_name(middle_class); 1488 printk(KERN_CONT ");\n"); 1489 printk(" <Interrupt>\n"); 1490 printk(" lock("); 1491 __print_lock_name(safe_class); 1492 printk(KERN_CONT ");\n"); 1493 printk("\n *** DEADLOCK ***\n\n"); 1494 } 1495 1496 static int 1497 print_bad_irq_dependency(struct task_struct *curr, 1498 struct lock_list *prev_root, 1499 struct lock_list *next_root, 1500 struct lock_list *backwards_entry, 1501 struct lock_list *forwards_entry, 1502 struct held_lock *prev, 1503 struct held_lock *next, 1504 enum lock_usage_bit bit1, 1505 enum lock_usage_bit bit2, 1506 const char *irqclass) 1507 { 1508 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1509 return 0; 1510 1511 pr_warn("\n"); 1512 pr_warn("=====================================================\n"); 1513 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", 1514 irqclass, irqclass); 1515 print_kernel_ident(); 1516 pr_warn("-----------------------------------------------------\n"); 1517 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n", 1518 curr->comm, task_pid_nr(curr), 1519 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT, 1520 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, 1521 curr->hardirqs_enabled, 1522 curr->softirqs_enabled); 1523 print_lock(next); 1524 1525 pr_warn("\nand this task is already holding:\n"); 1526 print_lock(prev); 1527 pr_warn("which would create a new lock dependency:\n"); 1528 print_lock_name(hlock_class(prev)); 1529 pr_cont(" ->"); 1530 print_lock_name(hlock_class(next)); 1531 pr_cont("\n"); 1532 1533 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n", 1534 irqclass); 1535 print_lock_name(backwards_entry->class); 1536 pr_warn("\n... which became %s-irq-safe at:\n", irqclass); 1537 1538 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1); 1539 1540 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass); 1541 print_lock_name(forwards_entry->class); 1542 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass); 1543 pr_warn("..."); 1544 1545 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1); 1546 1547 pr_warn("\nother info that might help us debug this:\n\n"); 1548 print_irq_lock_scenario(backwards_entry, forwards_entry, 1549 hlock_class(prev), hlock_class(next)); 1550 1551 lockdep_print_held_locks(curr); 1552 1553 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass); 1554 if (!save_trace(&prev_root->trace)) 1555 return 0; 1556 print_shortest_lock_dependencies(backwards_entry, prev_root); 1557 1558 pr_warn("\nthe dependencies between the lock to be acquired"); 1559 pr_warn(" and %s-irq-unsafe lock:\n", irqclass); 1560 if (!save_trace(&next_root->trace)) 1561 return 0; 1562 print_shortest_lock_dependencies(forwards_entry, next_root); 1563 1564 pr_warn("\nstack backtrace:\n"); 1565 dump_stack(); 1566 1567 return 0; 1568 } 1569 1570 static int 1571 check_usage(struct task_struct *curr, struct held_lock *prev, 1572 struct held_lock *next, enum lock_usage_bit bit_backwards, 1573 enum lock_usage_bit bit_forwards, const char *irqclass) 1574 { 1575 int ret; 1576 struct lock_list this, that; 1577 struct lock_list *uninitialized_var(target_entry); 1578 struct lock_list *uninitialized_var(target_entry1); 1579 1580 this.parent = NULL; 1581 1582 this.class = hlock_class(prev); 1583 ret = find_usage_backwards(&this, bit_backwards, &target_entry); 1584 if (ret < 0) 1585 return print_bfs_bug(ret); 1586 if (ret == 1) 1587 return ret; 1588 1589 that.parent = NULL; 1590 that.class = hlock_class(next); 1591 ret = find_usage_forwards(&that, bit_forwards, &target_entry1); 1592 if (ret < 0) 1593 return print_bfs_bug(ret); 1594 if (ret == 1) 1595 return ret; 1596 1597 return print_bad_irq_dependency(curr, &this, &that, 1598 target_entry, target_entry1, 1599 prev, next, 1600 bit_backwards, bit_forwards, irqclass); 1601 } 1602 1603 static const char *state_names[] = { 1604 #define LOCKDEP_STATE(__STATE) \ 1605 __stringify(__STATE), 1606 #include "lockdep_states.h" 1607 #undef LOCKDEP_STATE 1608 }; 1609 1610 static const char *state_rnames[] = { 1611 #define LOCKDEP_STATE(__STATE) \ 1612 __stringify(__STATE)"-READ", 1613 #include "lockdep_states.h" 1614 #undef LOCKDEP_STATE 1615 }; 1616 1617 static inline const char *state_name(enum lock_usage_bit bit) 1618 { 1619 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2]; 1620 } 1621 1622 static int exclusive_bit(int new_bit) 1623 { 1624 /* 1625 * USED_IN 1626 * USED_IN_READ 1627 * ENABLED 1628 * ENABLED_READ 1629 * 1630 * bit 0 - write/read 1631 * bit 1 - used_in/enabled 1632 * bit 2+ state 1633 */ 1634 1635 int state = new_bit & ~3; 1636 int dir = new_bit & 2; 1637 1638 /* 1639 * keep state, bit flip the direction and strip read. 1640 */ 1641 return state | (dir ^ 2); 1642 } 1643 1644 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev, 1645 struct held_lock *next, enum lock_usage_bit bit) 1646 { 1647 /* 1648 * Prove that the new dependency does not connect a hardirq-safe 1649 * lock with a hardirq-unsafe lock - to achieve this we search 1650 * the backwards-subgraph starting at <prev>, and the 1651 * forwards-subgraph starting at <next>: 1652 */ 1653 if (!check_usage(curr, prev, next, bit, 1654 exclusive_bit(bit), state_name(bit))) 1655 return 0; 1656 1657 bit++; /* _READ */ 1658 1659 /* 1660 * Prove that the new dependency does not connect a hardirq-safe-read 1661 * lock with a hardirq-unsafe lock - to achieve this we search 1662 * the backwards-subgraph starting at <prev>, and the 1663 * forwards-subgraph starting at <next>: 1664 */ 1665 if (!check_usage(curr, prev, next, bit, 1666 exclusive_bit(bit), state_name(bit))) 1667 return 0; 1668 1669 return 1; 1670 } 1671 1672 static int 1673 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, 1674 struct held_lock *next) 1675 { 1676 #define LOCKDEP_STATE(__STATE) \ 1677 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \ 1678 return 0; 1679 #include "lockdep_states.h" 1680 #undef LOCKDEP_STATE 1681 1682 return 1; 1683 } 1684 1685 static void inc_chains(void) 1686 { 1687 if (current->hardirq_context) 1688 nr_hardirq_chains++; 1689 else { 1690 if (current->softirq_context) 1691 nr_softirq_chains++; 1692 else 1693 nr_process_chains++; 1694 } 1695 } 1696 1697 #else 1698 1699 static inline int 1700 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, 1701 struct held_lock *next) 1702 { 1703 return 1; 1704 } 1705 1706 static inline void inc_chains(void) 1707 { 1708 nr_process_chains++; 1709 } 1710 1711 #endif 1712 1713 static void 1714 print_deadlock_scenario(struct held_lock *nxt, 1715 struct held_lock *prv) 1716 { 1717 struct lock_class *next = hlock_class(nxt); 1718 struct lock_class *prev = hlock_class(prv); 1719 1720 printk(" Possible unsafe locking scenario:\n\n"); 1721 printk(" CPU0\n"); 1722 printk(" ----\n"); 1723 printk(" lock("); 1724 __print_lock_name(prev); 1725 printk(KERN_CONT ");\n"); 1726 printk(" lock("); 1727 __print_lock_name(next); 1728 printk(KERN_CONT ");\n"); 1729 printk("\n *** DEADLOCK ***\n\n"); 1730 printk(" May be due to missing lock nesting notation\n\n"); 1731 } 1732 1733 static int 1734 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, 1735 struct held_lock *next) 1736 { 1737 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1738 return 0; 1739 1740 pr_warn("\n"); 1741 pr_warn("============================================\n"); 1742 pr_warn("WARNING: possible recursive locking detected\n"); 1743 print_kernel_ident(); 1744 pr_warn("--------------------------------------------\n"); 1745 pr_warn("%s/%d is trying to acquire lock:\n", 1746 curr->comm, task_pid_nr(curr)); 1747 print_lock(next); 1748 pr_warn("\nbut task is already holding lock:\n"); 1749 print_lock(prev); 1750 1751 pr_warn("\nother info that might help us debug this:\n"); 1752 print_deadlock_scenario(next, prev); 1753 lockdep_print_held_locks(curr); 1754 1755 pr_warn("\nstack backtrace:\n"); 1756 dump_stack(); 1757 1758 return 0; 1759 } 1760 1761 /* 1762 * Check whether we are holding such a class already. 1763 * 1764 * (Note that this has to be done separately, because the graph cannot 1765 * detect such classes of deadlocks.) 1766 * 1767 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read 1768 */ 1769 static int 1770 check_deadlock(struct task_struct *curr, struct held_lock *next, 1771 struct lockdep_map *next_instance, int read) 1772 { 1773 struct held_lock *prev; 1774 struct held_lock *nest = NULL; 1775 int i; 1776 1777 for (i = 0; i < curr->lockdep_depth; i++) { 1778 prev = curr->held_locks + i; 1779 1780 if (prev->instance == next->nest_lock) 1781 nest = prev; 1782 1783 if (hlock_class(prev) != hlock_class(next)) 1784 continue; 1785 1786 /* 1787 * Allow read-after-read recursion of the same 1788 * lock class (i.e. read_lock(lock)+read_lock(lock)): 1789 */ 1790 if ((read == 2) && prev->read) 1791 return 2; 1792 1793 /* 1794 * We're holding the nest_lock, which serializes this lock's 1795 * nesting behaviour. 1796 */ 1797 if (nest) 1798 return 2; 1799 1800 return print_deadlock_bug(curr, prev, next); 1801 } 1802 return 1; 1803 } 1804 1805 /* 1806 * There was a chain-cache miss, and we are about to add a new dependency 1807 * to a previous lock. We recursively validate the following rules: 1808 * 1809 * - would the adding of the <prev> -> <next> dependency create a 1810 * circular dependency in the graph? [== circular deadlock] 1811 * 1812 * - does the new prev->next dependency connect any hardirq-safe lock 1813 * (in the full backwards-subgraph starting at <prev>) with any 1814 * hardirq-unsafe lock (in the full forwards-subgraph starting at 1815 * <next>)? [== illegal lock inversion with hardirq contexts] 1816 * 1817 * - does the new prev->next dependency connect any softirq-safe lock 1818 * (in the full backwards-subgraph starting at <prev>) with any 1819 * softirq-unsafe lock (in the full forwards-subgraph starting at 1820 * <next>)? [== illegal lock inversion with softirq contexts] 1821 * 1822 * any of these scenarios could lead to a deadlock. 1823 * 1824 * Then if all the validations pass, we add the forwards and backwards 1825 * dependency. 1826 */ 1827 static int 1828 check_prev_add(struct task_struct *curr, struct held_lock *prev, 1829 struct held_lock *next, int distance, struct stack_trace *trace, 1830 int (*save)(struct stack_trace *trace)) 1831 { 1832 struct lock_list *uninitialized_var(target_entry); 1833 struct lock_list *entry; 1834 struct lock_list this; 1835 int ret; 1836 1837 /* 1838 * Prove that the new <prev> -> <next> dependency would not 1839 * create a circular dependency in the graph. (We do this by 1840 * forward-recursing into the graph starting at <next>, and 1841 * checking whether we can reach <prev>.) 1842 * 1843 * We are using global variables to control the recursion, to 1844 * keep the stackframe size of the recursive functions low: 1845 */ 1846 this.class = hlock_class(next); 1847 this.parent = NULL; 1848 ret = check_noncircular(&this, hlock_class(prev), &target_entry); 1849 if (unlikely(!ret)) { 1850 if (!trace->entries) { 1851 /* 1852 * If @save fails here, the printing might trigger 1853 * a WARN but because of the !nr_entries it should 1854 * not do bad things. 1855 */ 1856 save(trace); 1857 } 1858 return print_circular_bug(&this, target_entry, next, prev, trace); 1859 } 1860 else if (unlikely(ret < 0)) 1861 return print_bfs_bug(ret); 1862 1863 if (!check_prev_add_irq(curr, prev, next)) 1864 return 0; 1865 1866 /* 1867 * For recursive read-locks we do all the dependency checks, 1868 * but we dont store read-triggered dependencies (only 1869 * write-triggered dependencies). This ensures that only the 1870 * write-side dependencies matter, and that if for example a 1871 * write-lock never takes any other locks, then the reads are 1872 * equivalent to a NOP. 1873 */ 1874 if (next->read == 2 || prev->read == 2) 1875 return 1; 1876 /* 1877 * Is the <prev> -> <next> dependency already present? 1878 * 1879 * (this may occur even though this is a new chain: consider 1880 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 1881 * chains - the second one will be new, but L1 already has 1882 * L2 added to its dependency list, due to the first chain.) 1883 */ 1884 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { 1885 if (entry->class == hlock_class(next)) { 1886 if (distance == 1) 1887 entry->distance = 1; 1888 return 1; 1889 } 1890 } 1891 1892 /* 1893 * Is the <prev> -> <next> link redundant? 1894 */ 1895 this.class = hlock_class(prev); 1896 this.parent = NULL; 1897 ret = check_redundant(&this, hlock_class(next), &target_entry); 1898 if (!ret) { 1899 debug_atomic_inc(nr_redundant); 1900 return 2; 1901 } 1902 if (ret < 0) 1903 return print_bfs_bug(ret); 1904 1905 1906 if (!trace->entries && !save(trace)) 1907 return 0; 1908 1909 /* 1910 * Ok, all validations passed, add the new lock 1911 * to the previous lock's dependency list: 1912 */ 1913 ret = add_lock_to_list(hlock_class(next), 1914 &hlock_class(prev)->locks_after, 1915 next->acquire_ip, distance, trace); 1916 1917 if (!ret) 1918 return 0; 1919 1920 ret = add_lock_to_list(hlock_class(prev), 1921 &hlock_class(next)->locks_before, 1922 next->acquire_ip, distance, trace); 1923 if (!ret) 1924 return 0; 1925 1926 return 2; 1927 } 1928 1929 /* 1930 * Add the dependency to all directly-previous locks that are 'relevant'. 1931 * The ones that are relevant are (in increasing distance from curr): 1932 * all consecutive trylock entries and the final non-trylock entry - or 1933 * the end of this context's lock-chain - whichever comes first. 1934 */ 1935 static int 1936 check_prevs_add(struct task_struct *curr, struct held_lock *next) 1937 { 1938 int depth = curr->lockdep_depth; 1939 struct held_lock *hlock; 1940 struct stack_trace trace = { 1941 .nr_entries = 0, 1942 .max_entries = 0, 1943 .entries = NULL, 1944 .skip = 0, 1945 }; 1946 1947 /* 1948 * Debugging checks. 1949 * 1950 * Depth must not be zero for a non-head lock: 1951 */ 1952 if (!depth) 1953 goto out_bug; 1954 /* 1955 * At least two relevant locks must exist for this 1956 * to be a head: 1957 */ 1958 if (curr->held_locks[depth].irq_context != 1959 curr->held_locks[depth-1].irq_context) 1960 goto out_bug; 1961 1962 for (;;) { 1963 int distance = curr->lockdep_depth - depth + 1; 1964 hlock = curr->held_locks + depth - 1; 1965 1966 /* 1967 * Only non-recursive-read entries get new dependencies 1968 * added: 1969 */ 1970 if (hlock->read != 2 && hlock->check) { 1971 int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace); 1972 if (!ret) 1973 return 0; 1974 1975 /* 1976 * Stop after the first non-trylock entry, 1977 * as non-trylock entries have added their 1978 * own direct dependencies already, so this 1979 * lock is connected to them indirectly: 1980 */ 1981 if (!hlock->trylock) 1982 break; 1983 } 1984 1985 depth--; 1986 /* 1987 * End of lock-stack? 1988 */ 1989 if (!depth) 1990 break; 1991 /* 1992 * Stop the search if we cross into another context: 1993 */ 1994 if (curr->held_locks[depth].irq_context != 1995 curr->held_locks[depth-1].irq_context) 1996 break; 1997 } 1998 return 1; 1999 out_bug: 2000 if (!debug_locks_off_graph_unlock()) 2001 return 0; 2002 2003 /* 2004 * Clearly we all shouldn't be here, but since we made it we 2005 * can reliable say we messed up our state. See the above two 2006 * gotos for reasons why we could possibly end up here. 2007 */ 2008 WARN_ON(1); 2009 2010 return 0; 2011 } 2012 2013 unsigned long nr_lock_chains; 2014 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; 2015 int nr_chain_hlocks; 2016 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; 2017 2018 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i) 2019 { 2020 return lock_classes + chain_hlocks[chain->base + i]; 2021 } 2022 2023 /* 2024 * Returns the index of the first held_lock of the current chain 2025 */ 2026 static inline int get_first_held_lock(struct task_struct *curr, 2027 struct held_lock *hlock) 2028 { 2029 int i; 2030 struct held_lock *hlock_curr; 2031 2032 for (i = curr->lockdep_depth - 1; i >= 0; i--) { 2033 hlock_curr = curr->held_locks + i; 2034 if (hlock_curr->irq_context != hlock->irq_context) 2035 break; 2036 2037 } 2038 2039 return ++i; 2040 } 2041 2042 #ifdef CONFIG_DEBUG_LOCKDEP 2043 /* 2044 * Returns the next chain_key iteration 2045 */ 2046 static u64 print_chain_key_iteration(int class_idx, u64 chain_key) 2047 { 2048 u64 new_chain_key = iterate_chain_key(chain_key, class_idx); 2049 2050 printk(" class_idx:%d -> chain_key:%016Lx", 2051 class_idx, 2052 (unsigned long long)new_chain_key); 2053 return new_chain_key; 2054 } 2055 2056 static void 2057 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next) 2058 { 2059 struct held_lock *hlock; 2060 u64 chain_key = 0; 2061 int depth = curr->lockdep_depth; 2062 int i; 2063 2064 printk("depth: %u\n", depth + 1); 2065 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) { 2066 hlock = curr->held_locks + i; 2067 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key); 2068 2069 print_lock(hlock); 2070 } 2071 2072 print_chain_key_iteration(hlock_next->class_idx, chain_key); 2073 print_lock(hlock_next); 2074 } 2075 2076 static void print_chain_keys_chain(struct lock_chain *chain) 2077 { 2078 int i; 2079 u64 chain_key = 0; 2080 int class_id; 2081 2082 printk("depth: %u\n", chain->depth); 2083 for (i = 0; i < chain->depth; i++) { 2084 class_id = chain_hlocks[chain->base + i]; 2085 chain_key = print_chain_key_iteration(class_id + 1, chain_key); 2086 2087 print_lock_name(lock_classes + class_id); 2088 printk("\n"); 2089 } 2090 } 2091 2092 static void print_collision(struct task_struct *curr, 2093 struct held_lock *hlock_next, 2094 struct lock_chain *chain) 2095 { 2096 pr_warn("\n"); 2097 pr_warn("============================\n"); 2098 pr_warn("WARNING: chain_key collision\n"); 2099 print_kernel_ident(); 2100 pr_warn("----------------------------\n"); 2101 pr_warn("%s/%d: ", current->comm, task_pid_nr(current)); 2102 pr_warn("Hash chain already cached but the contents don't match!\n"); 2103 2104 pr_warn("Held locks:"); 2105 print_chain_keys_held_locks(curr, hlock_next); 2106 2107 pr_warn("Locks in cached chain:"); 2108 print_chain_keys_chain(chain); 2109 2110 pr_warn("\nstack backtrace:\n"); 2111 dump_stack(); 2112 } 2113 #endif 2114 2115 /* 2116 * Checks whether the chain and the current held locks are consistent 2117 * in depth and also in content. If they are not it most likely means 2118 * that there was a collision during the calculation of the chain_key. 2119 * Returns: 0 not passed, 1 passed 2120 */ 2121 static int check_no_collision(struct task_struct *curr, 2122 struct held_lock *hlock, 2123 struct lock_chain *chain) 2124 { 2125 #ifdef CONFIG_DEBUG_LOCKDEP 2126 int i, j, id; 2127 2128 i = get_first_held_lock(curr, hlock); 2129 2130 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { 2131 print_collision(curr, hlock, chain); 2132 return 0; 2133 } 2134 2135 for (j = 0; j < chain->depth - 1; j++, i++) { 2136 id = curr->held_locks[i].class_idx - 1; 2137 2138 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { 2139 print_collision(curr, hlock, chain); 2140 return 0; 2141 } 2142 } 2143 #endif 2144 return 1; 2145 } 2146 2147 /* 2148 * This is for building a chain between just two different classes, 2149 * instead of adding a new hlock upon current, which is done by 2150 * add_chain_cache(). 2151 * 2152 * This can be called in any context with two classes, while 2153 * add_chain_cache() must be done within the lock owener's context 2154 * since it uses hlock which might be racy in another context. 2155 */ 2156 static inline int add_chain_cache_classes(unsigned int prev, 2157 unsigned int next, 2158 unsigned int irq_context, 2159 u64 chain_key) 2160 { 2161 struct hlist_head *hash_head = chainhashentry(chain_key); 2162 struct lock_chain *chain; 2163 2164 /* 2165 * Allocate a new chain entry from the static array, and add 2166 * it to the hash: 2167 */ 2168 2169 /* 2170 * We might need to take the graph lock, ensure we've got IRQs 2171 * disabled to make this an IRQ-safe lock.. for recursion reasons 2172 * lockdep won't complain about its own locking errors. 2173 */ 2174 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2175 return 0; 2176 2177 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) { 2178 if (!debug_locks_off_graph_unlock()) 2179 return 0; 2180 2181 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); 2182 dump_stack(); 2183 return 0; 2184 } 2185 2186 chain = lock_chains + nr_lock_chains++; 2187 chain->chain_key = chain_key; 2188 chain->irq_context = irq_context; 2189 chain->depth = 2; 2190 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) { 2191 chain->base = nr_chain_hlocks; 2192 nr_chain_hlocks += chain->depth; 2193 chain_hlocks[chain->base] = prev - 1; 2194 chain_hlocks[chain->base + 1] = next -1; 2195 } 2196 #ifdef CONFIG_DEBUG_LOCKDEP 2197 /* 2198 * Important for check_no_collision(). 2199 */ 2200 else { 2201 if (!debug_locks_off_graph_unlock()) 2202 return 0; 2203 2204 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); 2205 dump_stack(); 2206 return 0; 2207 } 2208 #endif 2209 2210 hlist_add_head_rcu(&chain->entry, hash_head); 2211 debug_atomic_inc(chain_lookup_misses); 2212 inc_chains(); 2213 2214 return 1; 2215 } 2216 2217 /* 2218 * Adds a dependency chain into chain hashtable. And must be called with 2219 * graph_lock held. 2220 * 2221 * Return 0 if fail, and graph_lock is released. 2222 * Return 1 if succeed, with graph_lock held. 2223 */ 2224 static inline int add_chain_cache(struct task_struct *curr, 2225 struct held_lock *hlock, 2226 u64 chain_key) 2227 { 2228 struct lock_class *class = hlock_class(hlock); 2229 struct hlist_head *hash_head = chainhashentry(chain_key); 2230 struct lock_chain *chain; 2231 int i, j; 2232 2233 /* 2234 * Allocate a new chain entry from the static array, and add 2235 * it to the hash: 2236 */ 2237 2238 /* 2239 * We might need to take the graph lock, ensure we've got IRQs 2240 * disabled to make this an IRQ-safe lock.. for recursion reasons 2241 * lockdep won't complain about its own locking errors. 2242 */ 2243 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2244 return 0; 2245 2246 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) { 2247 if (!debug_locks_off_graph_unlock()) 2248 return 0; 2249 2250 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); 2251 dump_stack(); 2252 return 0; 2253 } 2254 chain = lock_chains + nr_lock_chains++; 2255 chain->chain_key = chain_key; 2256 chain->irq_context = hlock->irq_context; 2257 i = get_first_held_lock(curr, hlock); 2258 chain->depth = curr->lockdep_depth + 1 - i; 2259 2260 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks)); 2261 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks)); 2262 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes)); 2263 2264 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) { 2265 chain->base = nr_chain_hlocks; 2266 for (j = 0; j < chain->depth - 1; j++, i++) { 2267 int lock_id = curr->held_locks[i].class_idx - 1; 2268 chain_hlocks[chain->base + j] = lock_id; 2269 } 2270 chain_hlocks[chain->base + j] = class - lock_classes; 2271 } 2272 2273 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS) 2274 nr_chain_hlocks += chain->depth; 2275 2276 #ifdef CONFIG_DEBUG_LOCKDEP 2277 /* 2278 * Important for check_no_collision(). 2279 */ 2280 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) { 2281 if (!debug_locks_off_graph_unlock()) 2282 return 0; 2283 2284 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); 2285 dump_stack(); 2286 return 0; 2287 } 2288 #endif 2289 2290 hlist_add_head_rcu(&chain->entry, hash_head); 2291 debug_atomic_inc(chain_lookup_misses); 2292 inc_chains(); 2293 2294 return 1; 2295 } 2296 2297 /* 2298 * Look up a dependency chain. 2299 */ 2300 static inline struct lock_chain *lookup_chain_cache(u64 chain_key) 2301 { 2302 struct hlist_head *hash_head = chainhashentry(chain_key); 2303 struct lock_chain *chain; 2304 2305 /* 2306 * We can walk it lock-free, because entries only get added 2307 * to the hash: 2308 */ 2309 hlist_for_each_entry_rcu(chain, hash_head, entry) { 2310 if (chain->chain_key == chain_key) { 2311 debug_atomic_inc(chain_lookup_hits); 2312 return chain; 2313 } 2314 } 2315 return NULL; 2316 } 2317 2318 /* 2319 * If the key is not present yet in dependency chain cache then 2320 * add it and return 1 - in this case the new dependency chain is 2321 * validated. If the key is already hashed, return 0. 2322 * (On return with 1 graph_lock is held.) 2323 */ 2324 static inline int lookup_chain_cache_add(struct task_struct *curr, 2325 struct held_lock *hlock, 2326 u64 chain_key) 2327 { 2328 struct lock_class *class = hlock_class(hlock); 2329 struct lock_chain *chain = lookup_chain_cache(chain_key); 2330 2331 if (chain) { 2332 cache_hit: 2333 if (!check_no_collision(curr, hlock, chain)) 2334 return 0; 2335 2336 if (very_verbose(class)) { 2337 printk("\nhash chain already cached, key: " 2338 "%016Lx tail class: [%p] %s\n", 2339 (unsigned long long)chain_key, 2340 class->key, class->name); 2341 } 2342 2343 return 0; 2344 } 2345 2346 if (very_verbose(class)) { 2347 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n", 2348 (unsigned long long)chain_key, class->key, class->name); 2349 } 2350 2351 if (!graph_lock()) 2352 return 0; 2353 2354 /* 2355 * We have to walk the chain again locked - to avoid duplicates: 2356 */ 2357 chain = lookup_chain_cache(chain_key); 2358 if (chain) { 2359 graph_unlock(); 2360 goto cache_hit; 2361 } 2362 2363 if (!add_chain_cache(curr, hlock, chain_key)) 2364 return 0; 2365 2366 return 1; 2367 } 2368 2369 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock, 2370 struct held_lock *hlock, int chain_head, u64 chain_key) 2371 { 2372 /* 2373 * Trylock needs to maintain the stack of held locks, but it 2374 * does not add new dependencies, because trylock can be done 2375 * in any order. 2376 * 2377 * We look up the chain_key and do the O(N^2) check and update of 2378 * the dependencies only if this is a new dependency chain. 2379 * (If lookup_chain_cache_add() return with 1 it acquires 2380 * graph_lock for us) 2381 */ 2382 if (!hlock->trylock && hlock->check && 2383 lookup_chain_cache_add(curr, hlock, chain_key)) { 2384 /* 2385 * Check whether last held lock: 2386 * 2387 * - is irq-safe, if this lock is irq-unsafe 2388 * - is softirq-safe, if this lock is hardirq-unsafe 2389 * 2390 * And check whether the new lock's dependency graph 2391 * could lead back to the previous lock. 2392 * 2393 * any of these scenarios could lead to a deadlock. If 2394 * All validations 2395 */ 2396 int ret = check_deadlock(curr, hlock, lock, hlock->read); 2397 2398 if (!ret) 2399 return 0; 2400 /* 2401 * Mark recursive read, as we jump over it when 2402 * building dependencies (just like we jump over 2403 * trylock entries): 2404 */ 2405 if (ret == 2) 2406 hlock->read = 2; 2407 /* 2408 * Add dependency only if this lock is not the head 2409 * of the chain, and if it's not a secondary read-lock: 2410 */ 2411 if (!chain_head && ret != 2) { 2412 if (!check_prevs_add(curr, hlock)) 2413 return 0; 2414 } 2415 2416 graph_unlock(); 2417 } else { 2418 /* after lookup_chain_cache_add(): */ 2419 if (unlikely(!debug_locks)) 2420 return 0; 2421 } 2422 2423 return 1; 2424 } 2425 #else 2426 static inline int validate_chain(struct task_struct *curr, 2427 struct lockdep_map *lock, struct held_lock *hlock, 2428 int chain_head, u64 chain_key) 2429 { 2430 return 1; 2431 } 2432 #endif 2433 2434 /* 2435 * We are building curr_chain_key incrementally, so double-check 2436 * it from scratch, to make sure that it's done correctly: 2437 */ 2438 static void check_chain_key(struct task_struct *curr) 2439 { 2440 #ifdef CONFIG_DEBUG_LOCKDEP 2441 struct held_lock *hlock, *prev_hlock = NULL; 2442 unsigned int i; 2443 u64 chain_key = 0; 2444 2445 for (i = 0; i < curr->lockdep_depth; i++) { 2446 hlock = curr->held_locks + i; 2447 if (chain_key != hlock->prev_chain_key) { 2448 debug_locks_off(); 2449 /* 2450 * We got mighty confused, our chain keys don't match 2451 * with what we expect, someone trample on our task state? 2452 */ 2453 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", 2454 curr->lockdep_depth, i, 2455 (unsigned long long)chain_key, 2456 (unsigned long long)hlock->prev_chain_key); 2457 return; 2458 } 2459 /* 2460 * Whoops ran out of static storage again? 2461 */ 2462 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS)) 2463 return; 2464 2465 if (prev_hlock && (prev_hlock->irq_context != 2466 hlock->irq_context)) 2467 chain_key = 0; 2468 chain_key = iterate_chain_key(chain_key, hlock->class_idx); 2469 prev_hlock = hlock; 2470 } 2471 if (chain_key != curr->curr_chain_key) { 2472 debug_locks_off(); 2473 /* 2474 * More smoking hash instead of calculating it, damn see these 2475 * numbers float.. I bet that a pink elephant stepped on my memory. 2476 */ 2477 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", 2478 curr->lockdep_depth, i, 2479 (unsigned long long)chain_key, 2480 (unsigned long long)curr->curr_chain_key); 2481 } 2482 #endif 2483 } 2484 2485 static void 2486 print_usage_bug_scenario(struct held_lock *lock) 2487 { 2488 struct lock_class *class = hlock_class(lock); 2489 2490 printk(" Possible unsafe locking scenario:\n\n"); 2491 printk(" CPU0\n"); 2492 printk(" ----\n"); 2493 printk(" lock("); 2494 __print_lock_name(class); 2495 printk(KERN_CONT ");\n"); 2496 printk(" <Interrupt>\n"); 2497 printk(" lock("); 2498 __print_lock_name(class); 2499 printk(KERN_CONT ");\n"); 2500 printk("\n *** DEADLOCK ***\n\n"); 2501 } 2502 2503 static int 2504 print_usage_bug(struct task_struct *curr, struct held_lock *this, 2505 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit) 2506 { 2507 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 2508 return 0; 2509 2510 pr_warn("\n"); 2511 pr_warn("================================\n"); 2512 pr_warn("WARNING: inconsistent lock state\n"); 2513 print_kernel_ident(); 2514 pr_warn("--------------------------------\n"); 2515 2516 pr_warn("inconsistent {%s} -> {%s} usage.\n", 2517 usage_str[prev_bit], usage_str[new_bit]); 2518 2519 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n", 2520 curr->comm, task_pid_nr(curr), 2521 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT, 2522 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, 2523 trace_hardirqs_enabled(curr), 2524 trace_softirqs_enabled(curr)); 2525 print_lock(this); 2526 2527 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]); 2528 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1); 2529 2530 print_irqtrace_events(curr); 2531 pr_warn("\nother info that might help us debug this:\n"); 2532 print_usage_bug_scenario(this); 2533 2534 lockdep_print_held_locks(curr); 2535 2536 pr_warn("\nstack backtrace:\n"); 2537 dump_stack(); 2538 2539 return 0; 2540 } 2541 2542 /* 2543 * Print out an error if an invalid bit is set: 2544 */ 2545 static inline int 2546 valid_state(struct task_struct *curr, struct held_lock *this, 2547 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) 2548 { 2549 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) 2550 return print_usage_bug(curr, this, bad_bit, new_bit); 2551 return 1; 2552 } 2553 2554 static int mark_lock(struct task_struct *curr, struct held_lock *this, 2555 enum lock_usage_bit new_bit); 2556 2557 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) 2558 2559 /* 2560 * print irq inversion bug: 2561 */ 2562 static int 2563 print_irq_inversion_bug(struct task_struct *curr, 2564 struct lock_list *root, struct lock_list *other, 2565 struct held_lock *this, int forwards, 2566 const char *irqclass) 2567 { 2568 struct lock_list *entry = other; 2569 struct lock_list *middle = NULL; 2570 int depth; 2571 2572 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 2573 return 0; 2574 2575 pr_warn("\n"); 2576 pr_warn("========================================================\n"); 2577 pr_warn("WARNING: possible irq lock inversion dependency detected\n"); 2578 print_kernel_ident(); 2579 pr_warn("--------------------------------------------------------\n"); 2580 pr_warn("%s/%d just changed the state of lock:\n", 2581 curr->comm, task_pid_nr(curr)); 2582 print_lock(this); 2583 if (forwards) 2584 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass); 2585 else 2586 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); 2587 print_lock_name(other->class); 2588 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n"); 2589 2590 pr_warn("\nother info that might help us debug this:\n"); 2591 2592 /* Find a middle lock (if one exists) */ 2593 depth = get_lock_depth(other); 2594 do { 2595 if (depth == 0 && (entry != root)) { 2596 pr_warn("lockdep:%s bad path found in chain graph\n", __func__); 2597 break; 2598 } 2599 middle = entry; 2600 entry = get_lock_parent(entry); 2601 depth--; 2602 } while (entry && entry != root && (depth >= 0)); 2603 if (forwards) 2604 print_irq_lock_scenario(root, other, 2605 middle ? middle->class : root->class, other->class); 2606 else 2607 print_irq_lock_scenario(other, root, 2608 middle ? middle->class : other->class, root->class); 2609 2610 lockdep_print_held_locks(curr); 2611 2612 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); 2613 if (!save_trace(&root->trace)) 2614 return 0; 2615 print_shortest_lock_dependencies(other, root); 2616 2617 pr_warn("\nstack backtrace:\n"); 2618 dump_stack(); 2619 2620 return 0; 2621 } 2622 2623 /* 2624 * Prove that in the forwards-direction subgraph starting at <this> 2625 * there is no lock matching <mask>: 2626 */ 2627 static int 2628 check_usage_forwards(struct task_struct *curr, struct held_lock *this, 2629 enum lock_usage_bit bit, const char *irqclass) 2630 { 2631 int ret; 2632 struct lock_list root; 2633 struct lock_list *uninitialized_var(target_entry); 2634 2635 root.parent = NULL; 2636 root.class = hlock_class(this); 2637 ret = find_usage_forwards(&root, bit, &target_entry); 2638 if (ret < 0) 2639 return print_bfs_bug(ret); 2640 if (ret == 1) 2641 return ret; 2642 2643 return print_irq_inversion_bug(curr, &root, target_entry, 2644 this, 1, irqclass); 2645 } 2646 2647 /* 2648 * Prove that in the backwards-direction subgraph starting at <this> 2649 * there is no lock matching <mask>: 2650 */ 2651 static int 2652 check_usage_backwards(struct task_struct *curr, struct held_lock *this, 2653 enum lock_usage_bit bit, const char *irqclass) 2654 { 2655 int ret; 2656 struct lock_list root; 2657 struct lock_list *uninitialized_var(target_entry); 2658 2659 root.parent = NULL; 2660 root.class = hlock_class(this); 2661 ret = find_usage_backwards(&root, bit, &target_entry); 2662 if (ret < 0) 2663 return print_bfs_bug(ret); 2664 if (ret == 1) 2665 return ret; 2666 2667 return print_irq_inversion_bug(curr, &root, target_entry, 2668 this, 0, irqclass); 2669 } 2670 2671 void print_irqtrace_events(struct task_struct *curr) 2672 { 2673 printk("irq event stamp: %u\n", curr->irq_events); 2674 printk("hardirqs last enabled at (%u): [<%p>] %pS\n", 2675 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip, 2676 (void *)curr->hardirq_enable_ip); 2677 printk("hardirqs last disabled at (%u): [<%p>] %pS\n", 2678 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip, 2679 (void *)curr->hardirq_disable_ip); 2680 printk("softirqs last enabled at (%u): [<%p>] %pS\n", 2681 curr->softirq_enable_event, (void *)curr->softirq_enable_ip, 2682 (void *)curr->softirq_enable_ip); 2683 printk("softirqs last disabled at (%u): [<%p>] %pS\n", 2684 curr->softirq_disable_event, (void *)curr->softirq_disable_ip, 2685 (void *)curr->softirq_disable_ip); 2686 } 2687 2688 static int HARDIRQ_verbose(struct lock_class *class) 2689 { 2690 #if HARDIRQ_VERBOSE 2691 return class_filter(class); 2692 #endif 2693 return 0; 2694 } 2695 2696 static int SOFTIRQ_verbose(struct lock_class *class) 2697 { 2698 #if SOFTIRQ_VERBOSE 2699 return class_filter(class); 2700 #endif 2701 return 0; 2702 } 2703 2704 #define STRICT_READ_CHECKS 1 2705 2706 static int (*state_verbose_f[])(struct lock_class *class) = { 2707 #define LOCKDEP_STATE(__STATE) \ 2708 __STATE##_verbose, 2709 #include "lockdep_states.h" 2710 #undef LOCKDEP_STATE 2711 }; 2712 2713 static inline int state_verbose(enum lock_usage_bit bit, 2714 struct lock_class *class) 2715 { 2716 return state_verbose_f[bit >> 2](class); 2717 } 2718 2719 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, 2720 enum lock_usage_bit bit, const char *name); 2721 2722 static int 2723 mark_lock_irq(struct task_struct *curr, struct held_lock *this, 2724 enum lock_usage_bit new_bit) 2725 { 2726 int excl_bit = exclusive_bit(new_bit); 2727 int read = new_bit & 1; 2728 int dir = new_bit & 2; 2729 2730 /* 2731 * mark USED_IN has to look forwards -- to ensure no dependency 2732 * has ENABLED state, which would allow recursion deadlocks. 2733 * 2734 * mark ENABLED has to look backwards -- to ensure no dependee 2735 * has USED_IN state, which, again, would allow recursion deadlocks. 2736 */ 2737 check_usage_f usage = dir ? 2738 check_usage_backwards : check_usage_forwards; 2739 2740 /* 2741 * Validate that this particular lock does not have conflicting 2742 * usage states. 2743 */ 2744 if (!valid_state(curr, this, new_bit, excl_bit)) 2745 return 0; 2746 2747 /* 2748 * Validate that the lock dependencies don't have conflicting usage 2749 * states. 2750 */ 2751 if ((!read || !dir || STRICT_READ_CHECKS) && 2752 !usage(curr, this, excl_bit, state_name(new_bit & ~1))) 2753 return 0; 2754 2755 /* 2756 * Check for read in write conflicts 2757 */ 2758 if (!read) { 2759 if (!valid_state(curr, this, new_bit, excl_bit + 1)) 2760 return 0; 2761 2762 if (STRICT_READ_CHECKS && 2763 !usage(curr, this, excl_bit + 1, 2764 state_name(new_bit + 1))) 2765 return 0; 2766 } 2767 2768 if (state_verbose(new_bit, hlock_class(this))) 2769 return 2; 2770 2771 return 1; 2772 } 2773 2774 enum mark_type { 2775 #define LOCKDEP_STATE(__STATE) __STATE, 2776 #include "lockdep_states.h" 2777 #undef LOCKDEP_STATE 2778 }; 2779 2780 /* 2781 * Mark all held locks with a usage bit: 2782 */ 2783 static int 2784 mark_held_locks(struct task_struct *curr, enum mark_type mark) 2785 { 2786 enum lock_usage_bit usage_bit; 2787 struct held_lock *hlock; 2788 int i; 2789 2790 for (i = 0; i < curr->lockdep_depth; i++) { 2791 hlock = curr->held_locks + i; 2792 2793 usage_bit = 2 + (mark << 2); /* ENABLED */ 2794 if (hlock->read) 2795 usage_bit += 1; /* READ */ 2796 2797 BUG_ON(usage_bit >= LOCK_USAGE_STATES); 2798 2799 if (!hlock->check) 2800 continue; 2801 2802 if (!mark_lock(curr, hlock, usage_bit)) 2803 return 0; 2804 } 2805 2806 return 1; 2807 } 2808 2809 /* 2810 * Hardirqs will be enabled: 2811 */ 2812 static void __trace_hardirqs_on_caller(unsigned long ip) 2813 { 2814 struct task_struct *curr = current; 2815 2816 /* we'll do an OFF -> ON transition: */ 2817 curr->hardirqs_enabled = 1; 2818 2819 /* 2820 * We are going to turn hardirqs on, so set the 2821 * usage bit for all held locks: 2822 */ 2823 if (!mark_held_locks(curr, HARDIRQ)) 2824 return; 2825 /* 2826 * If we have softirqs enabled, then set the usage 2827 * bit for all held locks. (disabled hardirqs prevented 2828 * this bit from being set before) 2829 */ 2830 if (curr->softirqs_enabled) 2831 if (!mark_held_locks(curr, SOFTIRQ)) 2832 return; 2833 2834 curr->hardirq_enable_ip = ip; 2835 curr->hardirq_enable_event = ++curr->irq_events; 2836 debug_atomic_inc(hardirqs_on_events); 2837 } 2838 2839 __visible void trace_hardirqs_on_caller(unsigned long ip) 2840 { 2841 time_hardirqs_on(CALLER_ADDR0, ip); 2842 2843 if (unlikely(!debug_locks || current->lockdep_recursion)) 2844 return; 2845 2846 if (unlikely(current->hardirqs_enabled)) { 2847 /* 2848 * Neither irq nor preemption are disabled here 2849 * so this is racy by nature but losing one hit 2850 * in a stat is not a big deal. 2851 */ 2852 __debug_atomic_inc(redundant_hardirqs_on); 2853 return; 2854 } 2855 2856 /* 2857 * We're enabling irqs and according to our state above irqs weren't 2858 * already enabled, yet we find the hardware thinks they are in fact 2859 * enabled.. someone messed up their IRQ state tracing. 2860 */ 2861 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2862 return; 2863 2864 /* 2865 * See the fine text that goes along with this variable definition. 2866 */ 2867 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled))) 2868 return; 2869 2870 /* 2871 * Can't allow enabling interrupts while in an interrupt handler, 2872 * that's general bad form and such. Recursion, limited stack etc.. 2873 */ 2874 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context)) 2875 return; 2876 2877 current->lockdep_recursion = 1; 2878 __trace_hardirqs_on_caller(ip); 2879 current->lockdep_recursion = 0; 2880 } 2881 EXPORT_SYMBOL(trace_hardirqs_on_caller); 2882 2883 void trace_hardirqs_on(void) 2884 { 2885 trace_hardirqs_on_caller(CALLER_ADDR0); 2886 } 2887 EXPORT_SYMBOL(trace_hardirqs_on); 2888 2889 /* 2890 * Hardirqs were disabled: 2891 */ 2892 __visible void trace_hardirqs_off_caller(unsigned long ip) 2893 { 2894 struct task_struct *curr = current; 2895 2896 time_hardirqs_off(CALLER_ADDR0, ip); 2897 2898 if (unlikely(!debug_locks || current->lockdep_recursion)) 2899 return; 2900 2901 /* 2902 * So we're supposed to get called after you mask local IRQs, but for 2903 * some reason the hardware doesn't quite think you did a proper job. 2904 */ 2905 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2906 return; 2907 2908 if (curr->hardirqs_enabled) { 2909 /* 2910 * We have done an ON -> OFF transition: 2911 */ 2912 curr->hardirqs_enabled = 0; 2913 curr->hardirq_disable_ip = ip; 2914 curr->hardirq_disable_event = ++curr->irq_events; 2915 debug_atomic_inc(hardirqs_off_events); 2916 } else 2917 debug_atomic_inc(redundant_hardirqs_off); 2918 } 2919 EXPORT_SYMBOL(trace_hardirqs_off_caller); 2920 2921 void trace_hardirqs_off(void) 2922 { 2923 trace_hardirqs_off_caller(CALLER_ADDR0); 2924 } 2925 EXPORT_SYMBOL(trace_hardirqs_off); 2926 2927 /* 2928 * Softirqs will be enabled: 2929 */ 2930 void trace_softirqs_on(unsigned long ip) 2931 { 2932 struct task_struct *curr = current; 2933 2934 if (unlikely(!debug_locks || current->lockdep_recursion)) 2935 return; 2936 2937 /* 2938 * We fancy IRQs being disabled here, see softirq.c, avoids 2939 * funny state and nesting things. 2940 */ 2941 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2942 return; 2943 2944 if (curr->softirqs_enabled) { 2945 debug_atomic_inc(redundant_softirqs_on); 2946 return; 2947 } 2948 2949 current->lockdep_recursion = 1; 2950 /* 2951 * We'll do an OFF -> ON transition: 2952 */ 2953 curr->softirqs_enabled = 1; 2954 curr->softirq_enable_ip = ip; 2955 curr->softirq_enable_event = ++curr->irq_events; 2956 debug_atomic_inc(softirqs_on_events); 2957 /* 2958 * We are going to turn softirqs on, so set the 2959 * usage bit for all held locks, if hardirqs are 2960 * enabled too: 2961 */ 2962 if (curr->hardirqs_enabled) 2963 mark_held_locks(curr, SOFTIRQ); 2964 current->lockdep_recursion = 0; 2965 } 2966 2967 /* 2968 * Softirqs were disabled: 2969 */ 2970 void trace_softirqs_off(unsigned long ip) 2971 { 2972 struct task_struct *curr = current; 2973 2974 if (unlikely(!debug_locks || current->lockdep_recursion)) 2975 return; 2976 2977 /* 2978 * We fancy IRQs being disabled here, see softirq.c 2979 */ 2980 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2981 return; 2982 2983 if (curr->softirqs_enabled) { 2984 /* 2985 * We have done an ON -> OFF transition: 2986 */ 2987 curr->softirqs_enabled = 0; 2988 curr->softirq_disable_ip = ip; 2989 curr->softirq_disable_event = ++curr->irq_events; 2990 debug_atomic_inc(softirqs_off_events); 2991 /* 2992 * Whoops, we wanted softirqs off, so why aren't they? 2993 */ 2994 DEBUG_LOCKS_WARN_ON(!softirq_count()); 2995 } else 2996 debug_atomic_inc(redundant_softirqs_off); 2997 } 2998 2999 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock) 3000 { 3001 /* 3002 * If non-trylock use in a hardirq or softirq context, then 3003 * mark the lock as used in these contexts: 3004 */ 3005 if (!hlock->trylock) { 3006 if (hlock->read) { 3007 if (curr->hardirq_context) 3008 if (!mark_lock(curr, hlock, 3009 LOCK_USED_IN_HARDIRQ_READ)) 3010 return 0; 3011 if (curr->softirq_context) 3012 if (!mark_lock(curr, hlock, 3013 LOCK_USED_IN_SOFTIRQ_READ)) 3014 return 0; 3015 } else { 3016 if (curr->hardirq_context) 3017 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ)) 3018 return 0; 3019 if (curr->softirq_context) 3020 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ)) 3021 return 0; 3022 } 3023 } 3024 if (!hlock->hardirqs_off) { 3025 if (hlock->read) { 3026 if (!mark_lock(curr, hlock, 3027 LOCK_ENABLED_HARDIRQ_READ)) 3028 return 0; 3029 if (curr->softirqs_enabled) 3030 if (!mark_lock(curr, hlock, 3031 LOCK_ENABLED_SOFTIRQ_READ)) 3032 return 0; 3033 } else { 3034 if (!mark_lock(curr, hlock, 3035 LOCK_ENABLED_HARDIRQ)) 3036 return 0; 3037 if (curr->softirqs_enabled) 3038 if (!mark_lock(curr, hlock, 3039 LOCK_ENABLED_SOFTIRQ)) 3040 return 0; 3041 } 3042 } 3043 3044 return 1; 3045 } 3046 3047 static inline unsigned int task_irq_context(struct task_struct *task) 3048 { 3049 return 2 * !!task->hardirq_context + !!task->softirq_context; 3050 } 3051 3052 static int separate_irq_context(struct task_struct *curr, 3053 struct held_lock *hlock) 3054 { 3055 unsigned int depth = curr->lockdep_depth; 3056 3057 /* 3058 * Keep track of points where we cross into an interrupt context: 3059 */ 3060 if (depth) { 3061 struct held_lock *prev_hlock; 3062 3063 prev_hlock = curr->held_locks + depth-1; 3064 /* 3065 * If we cross into another context, reset the 3066 * hash key (this also prevents the checking and the 3067 * adding of the dependency to 'prev'): 3068 */ 3069 if (prev_hlock->irq_context != hlock->irq_context) 3070 return 1; 3071 } 3072 return 0; 3073 } 3074 3075 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */ 3076 3077 static inline 3078 int mark_lock_irq(struct task_struct *curr, struct held_lock *this, 3079 enum lock_usage_bit new_bit) 3080 { 3081 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */ 3082 return 1; 3083 } 3084 3085 static inline int mark_irqflags(struct task_struct *curr, 3086 struct held_lock *hlock) 3087 { 3088 return 1; 3089 } 3090 3091 static inline unsigned int task_irq_context(struct task_struct *task) 3092 { 3093 return 0; 3094 } 3095 3096 static inline int separate_irq_context(struct task_struct *curr, 3097 struct held_lock *hlock) 3098 { 3099 return 0; 3100 } 3101 3102 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */ 3103 3104 /* 3105 * Mark a lock with a usage bit, and validate the state transition: 3106 */ 3107 static int mark_lock(struct task_struct *curr, struct held_lock *this, 3108 enum lock_usage_bit new_bit) 3109 { 3110 unsigned int new_mask = 1 << new_bit, ret = 1; 3111 3112 /* 3113 * If already set then do not dirty the cacheline, 3114 * nor do any checks: 3115 */ 3116 if (likely(hlock_class(this)->usage_mask & new_mask)) 3117 return 1; 3118 3119 if (!graph_lock()) 3120 return 0; 3121 /* 3122 * Make sure we didn't race: 3123 */ 3124 if (unlikely(hlock_class(this)->usage_mask & new_mask)) { 3125 graph_unlock(); 3126 return 1; 3127 } 3128 3129 hlock_class(this)->usage_mask |= new_mask; 3130 3131 if (!save_trace(hlock_class(this)->usage_traces + new_bit)) 3132 return 0; 3133 3134 switch (new_bit) { 3135 #define LOCKDEP_STATE(__STATE) \ 3136 case LOCK_USED_IN_##__STATE: \ 3137 case LOCK_USED_IN_##__STATE##_READ: \ 3138 case LOCK_ENABLED_##__STATE: \ 3139 case LOCK_ENABLED_##__STATE##_READ: 3140 #include "lockdep_states.h" 3141 #undef LOCKDEP_STATE 3142 ret = mark_lock_irq(curr, this, new_bit); 3143 if (!ret) 3144 return 0; 3145 break; 3146 case LOCK_USED: 3147 debug_atomic_dec(nr_unused_locks); 3148 break; 3149 default: 3150 if (!debug_locks_off_graph_unlock()) 3151 return 0; 3152 WARN_ON(1); 3153 return 0; 3154 } 3155 3156 graph_unlock(); 3157 3158 /* 3159 * We must printk outside of the graph_lock: 3160 */ 3161 if (ret == 2) { 3162 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); 3163 print_lock(this); 3164 print_irqtrace_events(curr); 3165 dump_stack(); 3166 } 3167 3168 return ret; 3169 } 3170 3171 /* 3172 * Initialize a lock instance's lock-class mapping info: 3173 */ 3174 static void __lockdep_init_map(struct lockdep_map *lock, const char *name, 3175 struct lock_class_key *key, int subclass) 3176 { 3177 int i; 3178 3179 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++) 3180 lock->class_cache[i] = NULL; 3181 3182 #ifdef CONFIG_LOCK_STAT 3183 lock->cpu = raw_smp_processor_id(); 3184 #endif 3185 3186 /* 3187 * Can't be having no nameless bastards around this place! 3188 */ 3189 if (DEBUG_LOCKS_WARN_ON(!name)) { 3190 lock->name = "NULL"; 3191 return; 3192 } 3193 3194 lock->name = name; 3195 3196 /* 3197 * No key, no joy, we need to hash something. 3198 */ 3199 if (DEBUG_LOCKS_WARN_ON(!key)) 3200 return; 3201 /* 3202 * Sanity check, the lock-class key must be persistent: 3203 */ 3204 if (!static_obj(key)) { 3205 printk("BUG: key %p not in .data!\n", key); 3206 /* 3207 * What it says above ^^^^^, I suggest you read it. 3208 */ 3209 DEBUG_LOCKS_WARN_ON(1); 3210 return; 3211 } 3212 lock->key = key; 3213 3214 if (unlikely(!debug_locks)) 3215 return; 3216 3217 if (subclass) { 3218 unsigned long flags; 3219 3220 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion)) 3221 return; 3222 3223 raw_local_irq_save(flags); 3224 current->lockdep_recursion = 1; 3225 register_lock_class(lock, subclass, 1); 3226 current->lockdep_recursion = 0; 3227 raw_local_irq_restore(flags); 3228 } 3229 } 3230 3231 void lockdep_init_map(struct lockdep_map *lock, const char *name, 3232 struct lock_class_key *key, int subclass) 3233 { 3234 __lockdep_init_map(lock, name, key, subclass); 3235 } 3236 EXPORT_SYMBOL_GPL(lockdep_init_map); 3237 3238 struct lock_class_key __lockdep_no_validate__; 3239 EXPORT_SYMBOL_GPL(__lockdep_no_validate__); 3240 3241 static int 3242 print_lock_nested_lock_not_held(struct task_struct *curr, 3243 struct held_lock *hlock, 3244 unsigned long ip) 3245 { 3246 if (!debug_locks_off()) 3247 return 0; 3248 if (debug_locks_silent) 3249 return 0; 3250 3251 pr_warn("\n"); 3252 pr_warn("==================================\n"); 3253 pr_warn("WARNING: Nested lock was not taken\n"); 3254 print_kernel_ident(); 3255 pr_warn("----------------------------------\n"); 3256 3257 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); 3258 print_lock(hlock); 3259 3260 pr_warn("\nbut this task is not holding:\n"); 3261 pr_warn("%s\n", hlock->nest_lock->name); 3262 3263 pr_warn("\nstack backtrace:\n"); 3264 dump_stack(); 3265 3266 pr_warn("\nother info that might help us debug this:\n"); 3267 lockdep_print_held_locks(curr); 3268 3269 pr_warn("\nstack backtrace:\n"); 3270 dump_stack(); 3271 3272 return 0; 3273 } 3274 3275 static int __lock_is_held(struct lockdep_map *lock, int read); 3276 3277 /* 3278 * This gets called for every mutex_lock*()/spin_lock*() operation. 3279 * We maintain the dependency maps and validate the locking attempt: 3280 */ 3281 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, 3282 int trylock, int read, int check, int hardirqs_off, 3283 struct lockdep_map *nest_lock, unsigned long ip, 3284 int references, int pin_count) 3285 { 3286 struct task_struct *curr = current; 3287 struct lock_class *class = NULL; 3288 struct held_lock *hlock; 3289 unsigned int depth; 3290 int chain_head = 0; 3291 int class_idx; 3292 u64 chain_key; 3293 3294 if (unlikely(!debug_locks)) 3295 return 0; 3296 3297 /* 3298 * Lockdep should run with IRQs disabled, otherwise we could 3299 * get an interrupt which would want to take locks, which would 3300 * end up in lockdep and have you got a head-ache already? 3301 */ 3302 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 3303 return 0; 3304 3305 if (!prove_locking || lock->key == &__lockdep_no_validate__) 3306 check = 0; 3307 3308 if (subclass < NR_LOCKDEP_CACHING_CLASSES) 3309 class = lock->class_cache[subclass]; 3310 /* 3311 * Not cached? 3312 */ 3313 if (unlikely(!class)) { 3314 class = register_lock_class(lock, subclass, 0); 3315 if (!class) 3316 return 0; 3317 } 3318 atomic_inc((atomic_t *)&class->ops); 3319 if (very_verbose(class)) { 3320 printk("\nacquire class [%p] %s", class->key, class->name); 3321 if (class->name_version > 1) 3322 printk(KERN_CONT "#%d", class->name_version); 3323 printk(KERN_CONT "\n"); 3324 dump_stack(); 3325 } 3326 3327 /* 3328 * Add the lock to the list of currently held locks. 3329 * (we dont increase the depth just yet, up until the 3330 * dependency checks are done) 3331 */ 3332 depth = curr->lockdep_depth; 3333 /* 3334 * Ran out of static storage for our per-task lock stack again have we? 3335 */ 3336 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) 3337 return 0; 3338 3339 class_idx = class - lock_classes + 1; 3340 3341 if (depth) { 3342 hlock = curr->held_locks + depth - 1; 3343 if (hlock->class_idx == class_idx && nest_lock) { 3344 if (hlock->references) { 3345 /* 3346 * Check: unsigned int references:12, overflow. 3347 */ 3348 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1)) 3349 return 0; 3350 3351 hlock->references++; 3352 } else { 3353 hlock->references = 2; 3354 } 3355 3356 return 1; 3357 } 3358 } 3359 3360 hlock = curr->held_locks + depth; 3361 /* 3362 * Plain impossible, we just registered it and checked it weren't no 3363 * NULL like.. I bet this mushroom I ate was good! 3364 */ 3365 if (DEBUG_LOCKS_WARN_ON(!class)) 3366 return 0; 3367 hlock->class_idx = class_idx; 3368 hlock->acquire_ip = ip; 3369 hlock->instance = lock; 3370 hlock->nest_lock = nest_lock; 3371 hlock->irq_context = task_irq_context(curr); 3372 hlock->trylock = trylock; 3373 hlock->read = read; 3374 hlock->check = check; 3375 hlock->hardirqs_off = !!hardirqs_off; 3376 hlock->references = references; 3377 #ifdef CONFIG_LOCK_STAT 3378 hlock->waittime_stamp = 0; 3379 hlock->holdtime_stamp = lockstat_clock(); 3380 #endif 3381 hlock->pin_count = pin_count; 3382 3383 if (check && !mark_irqflags(curr, hlock)) 3384 return 0; 3385 3386 /* mark it as used: */ 3387 if (!mark_lock(curr, hlock, LOCK_USED)) 3388 return 0; 3389 3390 /* 3391 * Calculate the chain hash: it's the combined hash of all the 3392 * lock keys along the dependency chain. We save the hash value 3393 * at every step so that we can get the current hash easily 3394 * after unlock. The chain hash is then used to cache dependency 3395 * results. 3396 * 3397 * The 'key ID' is what is the most compact key value to drive 3398 * the hash, not class->key. 3399 */ 3400 /* 3401 * Whoops, we did it again.. ran straight out of our static allocation. 3402 */ 3403 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS)) 3404 return 0; 3405 3406 chain_key = curr->curr_chain_key; 3407 if (!depth) { 3408 /* 3409 * How can we have a chain hash when we ain't got no keys?! 3410 */ 3411 if (DEBUG_LOCKS_WARN_ON(chain_key != 0)) 3412 return 0; 3413 chain_head = 1; 3414 } 3415 3416 hlock->prev_chain_key = chain_key; 3417 if (separate_irq_context(curr, hlock)) { 3418 chain_key = 0; 3419 chain_head = 1; 3420 } 3421 chain_key = iterate_chain_key(chain_key, class_idx); 3422 3423 if (nest_lock && !__lock_is_held(nest_lock, -1)) 3424 return print_lock_nested_lock_not_held(curr, hlock, ip); 3425 3426 if (!validate_chain(curr, lock, hlock, chain_head, chain_key)) 3427 return 0; 3428 3429 curr->curr_chain_key = chain_key; 3430 curr->lockdep_depth++; 3431 check_chain_key(curr); 3432 #ifdef CONFIG_DEBUG_LOCKDEP 3433 if (unlikely(!debug_locks)) 3434 return 0; 3435 #endif 3436 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { 3437 debug_locks_off(); 3438 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); 3439 printk(KERN_DEBUG "depth: %i max: %lu!\n", 3440 curr->lockdep_depth, MAX_LOCK_DEPTH); 3441 3442 lockdep_print_held_locks(current); 3443 debug_show_all_locks(); 3444 dump_stack(); 3445 3446 return 0; 3447 } 3448 3449 if (unlikely(curr->lockdep_depth > max_lockdep_depth)) 3450 max_lockdep_depth = curr->lockdep_depth; 3451 3452 return 1; 3453 } 3454 3455 static int 3456 print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock, 3457 unsigned long ip) 3458 { 3459 if (!debug_locks_off()) 3460 return 0; 3461 if (debug_locks_silent) 3462 return 0; 3463 3464 pr_warn("\n"); 3465 pr_warn("=====================================\n"); 3466 pr_warn("WARNING: bad unlock balance detected!\n"); 3467 print_kernel_ident(); 3468 pr_warn("-------------------------------------\n"); 3469 pr_warn("%s/%d is trying to release lock (", 3470 curr->comm, task_pid_nr(curr)); 3471 print_lockdep_cache(lock); 3472 pr_cont(") at:\n"); 3473 print_ip_sym(ip); 3474 pr_warn("but there are no more locks to release!\n"); 3475 pr_warn("\nother info that might help us debug this:\n"); 3476 lockdep_print_held_locks(curr); 3477 3478 pr_warn("\nstack backtrace:\n"); 3479 dump_stack(); 3480 3481 return 0; 3482 } 3483 3484 static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock) 3485 { 3486 if (hlock->instance == lock) 3487 return 1; 3488 3489 if (hlock->references) { 3490 struct lock_class *class = lock->class_cache[0]; 3491 3492 if (!class) 3493 class = look_up_lock_class(lock, 0); 3494 3495 /* 3496 * If look_up_lock_class() failed to find a class, we're trying 3497 * to test if we hold a lock that has never yet been acquired. 3498 * Clearly if the lock hasn't been acquired _ever_, we're not 3499 * holding it either, so report failure. 3500 */ 3501 if (IS_ERR_OR_NULL(class)) 3502 return 0; 3503 3504 /* 3505 * References, but not a lock we're actually ref-counting? 3506 * State got messed up, follow the sites that change ->references 3507 * and try to make sense of it. 3508 */ 3509 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) 3510 return 0; 3511 3512 if (hlock->class_idx == class - lock_classes + 1) 3513 return 1; 3514 } 3515 3516 return 0; 3517 } 3518 3519 /* @depth must not be zero */ 3520 static struct held_lock *find_held_lock(struct task_struct *curr, 3521 struct lockdep_map *lock, 3522 unsigned int depth, int *idx) 3523 { 3524 struct held_lock *ret, *hlock, *prev_hlock; 3525 int i; 3526 3527 i = depth - 1; 3528 hlock = curr->held_locks + i; 3529 ret = hlock; 3530 if (match_held_lock(hlock, lock)) 3531 goto out; 3532 3533 ret = NULL; 3534 for (i--, prev_hlock = hlock--; 3535 i >= 0; 3536 i--, prev_hlock = hlock--) { 3537 /* 3538 * We must not cross into another context: 3539 */ 3540 if (prev_hlock->irq_context != hlock->irq_context) { 3541 ret = NULL; 3542 break; 3543 } 3544 if (match_held_lock(hlock, lock)) { 3545 ret = hlock; 3546 break; 3547 } 3548 } 3549 3550 out: 3551 *idx = i; 3552 return ret; 3553 } 3554 3555 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth, 3556 int idx) 3557 { 3558 struct held_lock *hlock; 3559 3560 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { 3561 if (!__lock_acquire(hlock->instance, 3562 hlock_class(hlock)->subclass, 3563 hlock->trylock, 3564 hlock->read, hlock->check, 3565 hlock->hardirqs_off, 3566 hlock->nest_lock, hlock->acquire_ip, 3567 hlock->references, hlock->pin_count)) 3568 return 1; 3569 } 3570 return 0; 3571 } 3572 3573 static int 3574 __lock_set_class(struct lockdep_map *lock, const char *name, 3575 struct lock_class_key *key, unsigned int subclass, 3576 unsigned long ip) 3577 { 3578 struct task_struct *curr = current; 3579 struct held_lock *hlock; 3580 struct lock_class *class; 3581 unsigned int depth; 3582 int i; 3583 3584 depth = curr->lockdep_depth; 3585 /* 3586 * This function is about (re)setting the class of a held lock, 3587 * yet we're not actually holding any locks. Naughty user! 3588 */ 3589 if (DEBUG_LOCKS_WARN_ON(!depth)) 3590 return 0; 3591 3592 hlock = find_held_lock(curr, lock, depth, &i); 3593 if (!hlock) 3594 return print_unlock_imbalance_bug(curr, lock, ip); 3595 3596 lockdep_init_map(lock, name, key, 0); 3597 class = register_lock_class(lock, subclass, 0); 3598 hlock->class_idx = class - lock_classes + 1; 3599 3600 curr->lockdep_depth = i; 3601 curr->curr_chain_key = hlock->prev_chain_key; 3602 3603 if (reacquire_held_locks(curr, depth, i)) 3604 return 0; 3605 3606 /* 3607 * I took it apart and put it back together again, except now I have 3608 * these 'spare' parts.. where shall I put them. 3609 */ 3610 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) 3611 return 0; 3612 return 1; 3613 } 3614 3615 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) 3616 { 3617 struct task_struct *curr = current; 3618 struct held_lock *hlock; 3619 unsigned int depth; 3620 int i; 3621 3622 depth = curr->lockdep_depth; 3623 /* 3624 * This function is about (re)setting the class of a held lock, 3625 * yet we're not actually holding any locks. Naughty user! 3626 */ 3627 if (DEBUG_LOCKS_WARN_ON(!depth)) 3628 return 0; 3629 3630 hlock = find_held_lock(curr, lock, depth, &i); 3631 if (!hlock) 3632 return print_unlock_imbalance_bug(curr, lock, ip); 3633 3634 curr->lockdep_depth = i; 3635 curr->curr_chain_key = hlock->prev_chain_key; 3636 3637 WARN(hlock->read, "downgrading a read lock"); 3638 hlock->read = 1; 3639 hlock->acquire_ip = ip; 3640 3641 if (reacquire_held_locks(curr, depth, i)) 3642 return 0; 3643 3644 /* 3645 * I took it apart and put it back together again, except now I have 3646 * these 'spare' parts.. where shall I put them. 3647 */ 3648 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) 3649 return 0; 3650 return 1; 3651 } 3652 3653 /* 3654 * Remove the lock to the list of currently held locks - this gets 3655 * called on mutex_unlock()/spin_unlock*() (or on a failed 3656 * mutex_lock_interruptible()). 3657 * 3658 * @nested is an hysterical artifact, needs a tree wide cleanup. 3659 */ 3660 static int 3661 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip) 3662 { 3663 struct task_struct *curr = current; 3664 struct held_lock *hlock; 3665 unsigned int depth; 3666 int i; 3667 3668 if (unlikely(!debug_locks)) 3669 return 0; 3670 3671 depth = curr->lockdep_depth; 3672 /* 3673 * So we're all set to release this lock.. wait what lock? We don't 3674 * own any locks, you've been drinking again? 3675 */ 3676 if (DEBUG_LOCKS_WARN_ON(depth <= 0)) 3677 return print_unlock_imbalance_bug(curr, lock, ip); 3678 3679 /* 3680 * Check whether the lock exists in the current stack 3681 * of held locks: 3682 */ 3683 hlock = find_held_lock(curr, lock, depth, &i); 3684 if (!hlock) 3685 return print_unlock_imbalance_bug(curr, lock, ip); 3686 3687 if (hlock->instance == lock) 3688 lock_release_holdtime(hlock); 3689 3690 WARN(hlock->pin_count, "releasing a pinned lock\n"); 3691 3692 if (hlock->references) { 3693 hlock->references--; 3694 if (hlock->references) { 3695 /* 3696 * We had, and after removing one, still have 3697 * references, the current lock stack is still 3698 * valid. We're done! 3699 */ 3700 return 1; 3701 } 3702 } 3703 3704 /* 3705 * We have the right lock to unlock, 'hlock' points to it. 3706 * Now we remove it from the stack, and add back the other 3707 * entries (if any), recalculating the hash along the way: 3708 */ 3709 3710 curr->lockdep_depth = i; 3711 curr->curr_chain_key = hlock->prev_chain_key; 3712 3713 if (reacquire_held_locks(curr, depth, i + 1)) 3714 return 0; 3715 3716 /* 3717 * We had N bottles of beer on the wall, we drank one, but now 3718 * there's not N-1 bottles of beer left on the wall... 3719 */ 3720 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1)) 3721 return 0; 3722 3723 return 1; 3724 } 3725 3726 static int __lock_is_held(struct lockdep_map *lock, int read) 3727 { 3728 struct task_struct *curr = current; 3729 int i; 3730 3731 for (i = 0; i < curr->lockdep_depth; i++) { 3732 struct held_lock *hlock = curr->held_locks + i; 3733 3734 if (match_held_lock(hlock, lock)) { 3735 if (read == -1 || hlock->read == read) 3736 return 1; 3737 3738 return 0; 3739 } 3740 } 3741 3742 return 0; 3743 } 3744 3745 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock) 3746 { 3747 struct pin_cookie cookie = NIL_COOKIE; 3748 struct task_struct *curr = current; 3749 int i; 3750 3751 if (unlikely(!debug_locks)) 3752 return cookie; 3753 3754 for (i = 0; i < curr->lockdep_depth; i++) { 3755 struct held_lock *hlock = curr->held_locks + i; 3756 3757 if (match_held_lock(hlock, lock)) { 3758 /* 3759 * Grab 16bits of randomness; this is sufficient to not 3760 * be guessable and still allows some pin nesting in 3761 * our u32 pin_count. 3762 */ 3763 cookie.val = 1 + (prandom_u32() >> 16); 3764 hlock->pin_count += cookie.val; 3765 return cookie; 3766 } 3767 } 3768 3769 WARN(1, "pinning an unheld lock\n"); 3770 return cookie; 3771 } 3772 3773 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 3774 { 3775 struct task_struct *curr = current; 3776 int i; 3777 3778 if (unlikely(!debug_locks)) 3779 return; 3780 3781 for (i = 0; i < curr->lockdep_depth; i++) { 3782 struct held_lock *hlock = curr->held_locks + i; 3783 3784 if (match_held_lock(hlock, lock)) { 3785 hlock->pin_count += cookie.val; 3786 return; 3787 } 3788 } 3789 3790 WARN(1, "pinning an unheld lock\n"); 3791 } 3792 3793 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 3794 { 3795 struct task_struct *curr = current; 3796 int i; 3797 3798 if (unlikely(!debug_locks)) 3799 return; 3800 3801 for (i = 0; i < curr->lockdep_depth; i++) { 3802 struct held_lock *hlock = curr->held_locks + i; 3803 3804 if (match_held_lock(hlock, lock)) { 3805 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n")) 3806 return; 3807 3808 hlock->pin_count -= cookie.val; 3809 3810 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n")) 3811 hlock->pin_count = 0; 3812 3813 return; 3814 } 3815 } 3816 3817 WARN(1, "unpinning an unheld lock\n"); 3818 } 3819 3820 /* 3821 * Check whether we follow the irq-flags state precisely: 3822 */ 3823 static void check_flags(unsigned long flags) 3824 { 3825 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \ 3826 defined(CONFIG_TRACE_IRQFLAGS) 3827 if (!debug_locks) 3828 return; 3829 3830 if (irqs_disabled_flags(flags)) { 3831 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) { 3832 printk("possible reason: unannotated irqs-off.\n"); 3833 } 3834 } else { 3835 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) { 3836 printk("possible reason: unannotated irqs-on.\n"); 3837 } 3838 } 3839 3840 /* 3841 * We dont accurately track softirq state in e.g. 3842 * hardirq contexts (such as on 4KSTACKS), so only 3843 * check if not in hardirq contexts: 3844 */ 3845 if (!hardirq_count()) { 3846 if (softirq_count()) { 3847 /* like the above, but with softirqs */ 3848 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); 3849 } else { 3850 /* lick the above, does it taste good? */ 3851 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); 3852 } 3853 } 3854 3855 if (!debug_locks) 3856 print_irqtrace_events(current); 3857 #endif 3858 } 3859 3860 void lock_set_class(struct lockdep_map *lock, const char *name, 3861 struct lock_class_key *key, unsigned int subclass, 3862 unsigned long ip) 3863 { 3864 unsigned long flags; 3865 3866 if (unlikely(current->lockdep_recursion)) 3867 return; 3868 3869 raw_local_irq_save(flags); 3870 current->lockdep_recursion = 1; 3871 check_flags(flags); 3872 if (__lock_set_class(lock, name, key, subclass, ip)) 3873 check_chain_key(current); 3874 current->lockdep_recursion = 0; 3875 raw_local_irq_restore(flags); 3876 } 3877 EXPORT_SYMBOL_GPL(lock_set_class); 3878 3879 void lock_downgrade(struct lockdep_map *lock, unsigned long ip) 3880 { 3881 unsigned long flags; 3882 3883 if (unlikely(current->lockdep_recursion)) 3884 return; 3885 3886 raw_local_irq_save(flags); 3887 current->lockdep_recursion = 1; 3888 check_flags(flags); 3889 if (__lock_downgrade(lock, ip)) 3890 check_chain_key(current); 3891 current->lockdep_recursion = 0; 3892 raw_local_irq_restore(flags); 3893 } 3894 EXPORT_SYMBOL_GPL(lock_downgrade); 3895 3896 /* 3897 * We are not always called with irqs disabled - do that here, 3898 * and also avoid lockdep recursion: 3899 */ 3900 void lock_acquire(struct lockdep_map *lock, unsigned int subclass, 3901 int trylock, int read, int check, 3902 struct lockdep_map *nest_lock, unsigned long ip) 3903 { 3904 unsigned long flags; 3905 3906 if (unlikely(current->lockdep_recursion)) 3907 return; 3908 3909 raw_local_irq_save(flags); 3910 check_flags(flags); 3911 3912 current->lockdep_recursion = 1; 3913 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip); 3914 __lock_acquire(lock, subclass, trylock, read, check, 3915 irqs_disabled_flags(flags), nest_lock, ip, 0, 0); 3916 current->lockdep_recursion = 0; 3917 raw_local_irq_restore(flags); 3918 } 3919 EXPORT_SYMBOL_GPL(lock_acquire); 3920 3921 void lock_release(struct lockdep_map *lock, int nested, 3922 unsigned long ip) 3923 { 3924 unsigned long flags; 3925 3926 if (unlikely(current->lockdep_recursion)) 3927 return; 3928 3929 raw_local_irq_save(flags); 3930 check_flags(flags); 3931 current->lockdep_recursion = 1; 3932 trace_lock_release(lock, ip); 3933 if (__lock_release(lock, nested, ip)) 3934 check_chain_key(current); 3935 current->lockdep_recursion = 0; 3936 raw_local_irq_restore(flags); 3937 } 3938 EXPORT_SYMBOL_GPL(lock_release); 3939 3940 int lock_is_held_type(struct lockdep_map *lock, int read) 3941 { 3942 unsigned long flags; 3943 int ret = 0; 3944 3945 if (unlikely(current->lockdep_recursion)) 3946 return 1; /* avoid false negative lockdep_assert_held() */ 3947 3948 raw_local_irq_save(flags); 3949 check_flags(flags); 3950 3951 current->lockdep_recursion = 1; 3952 ret = __lock_is_held(lock, read); 3953 current->lockdep_recursion = 0; 3954 raw_local_irq_restore(flags); 3955 3956 return ret; 3957 } 3958 EXPORT_SYMBOL_GPL(lock_is_held_type); 3959 3960 struct pin_cookie lock_pin_lock(struct lockdep_map *lock) 3961 { 3962 struct pin_cookie cookie = NIL_COOKIE; 3963 unsigned long flags; 3964 3965 if (unlikely(current->lockdep_recursion)) 3966 return cookie; 3967 3968 raw_local_irq_save(flags); 3969 check_flags(flags); 3970 3971 current->lockdep_recursion = 1; 3972 cookie = __lock_pin_lock(lock); 3973 current->lockdep_recursion = 0; 3974 raw_local_irq_restore(flags); 3975 3976 return cookie; 3977 } 3978 EXPORT_SYMBOL_GPL(lock_pin_lock); 3979 3980 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 3981 { 3982 unsigned long flags; 3983 3984 if (unlikely(current->lockdep_recursion)) 3985 return; 3986 3987 raw_local_irq_save(flags); 3988 check_flags(flags); 3989 3990 current->lockdep_recursion = 1; 3991 __lock_repin_lock(lock, cookie); 3992 current->lockdep_recursion = 0; 3993 raw_local_irq_restore(flags); 3994 } 3995 EXPORT_SYMBOL_GPL(lock_repin_lock); 3996 3997 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) 3998 { 3999 unsigned long flags; 4000 4001 if (unlikely(current->lockdep_recursion)) 4002 return; 4003 4004 raw_local_irq_save(flags); 4005 check_flags(flags); 4006 4007 current->lockdep_recursion = 1; 4008 __lock_unpin_lock(lock, cookie); 4009 current->lockdep_recursion = 0; 4010 raw_local_irq_restore(flags); 4011 } 4012 EXPORT_SYMBOL_GPL(lock_unpin_lock); 4013 4014 #ifdef CONFIG_LOCK_STAT 4015 static int 4016 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock, 4017 unsigned long ip) 4018 { 4019 if (!debug_locks_off()) 4020 return 0; 4021 if (debug_locks_silent) 4022 return 0; 4023 4024 pr_warn("\n"); 4025 pr_warn("=================================\n"); 4026 pr_warn("WARNING: bad contention detected!\n"); 4027 print_kernel_ident(); 4028 pr_warn("---------------------------------\n"); 4029 pr_warn("%s/%d is trying to contend lock (", 4030 curr->comm, task_pid_nr(curr)); 4031 print_lockdep_cache(lock); 4032 pr_cont(") at:\n"); 4033 print_ip_sym(ip); 4034 pr_warn("but there are no locks held!\n"); 4035 pr_warn("\nother info that might help us debug this:\n"); 4036 lockdep_print_held_locks(curr); 4037 4038 pr_warn("\nstack backtrace:\n"); 4039 dump_stack(); 4040 4041 return 0; 4042 } 4043 4044 static void 4045 __lock_contended(struct lockdep_map *lock, unsigned long ip) 4046 { 4047 struct task_struct *curr = current; 4048 struct held_lock *hlock; 4049 struct lock_class_stats *stats; 4050 unsigned int depth; 4051 int i, contention_point, contending_point; 4052 4053 depth = curr->lockdep_depth; 4054 /* 4055 * Whee, we contended on this lock, except it seems we're not 4056 * actually trying to acquire anything much at all.. 4057 */ 4058 if (DEBUG_LOCKS_WARN_ON(!depth)) 4059 return; 4060 4061 hlock = find_held_lock(curr, lock, depth, &i); 4062 if (!hlock) { 4063 print_lock_contention_bug(curr, lock, ip); 4064 return; 4065 } 4066 4067 if (hlock->instance != lock) 4068 return; 4069 4070 hlock->waittime_stamp = lockstat_clock(); 4071 4072 contention_point = lock_point(hlock_class(hlock)->contention_point, ip); 4073 contending_point = lock_point(hlock_class(hlock)->contending_point, 4074 lock->ip); 4075 4076 stats = get_lock_stats(hlock_class(hlock)); 4077 if (contention_point < LOCKSTAT_POINTS) 4078 stats->contention_point[contention_point]++; 4079 if (contending_point < LOCKSTAT_POINTS) 4080 stats->contending_point[contending_point]++; 4081 if (lock->cpu != smp_processor_id()) 4082 stats->bounces[bounce_contended + !!hlock->read]++; 4083 put_lock_stats(stats); 4084 } 4085 4086 static void 4087 __lock_acquired(struct lockdep_map *lock, unsigned long ip) 4088 { 4089 struct task_struct *curr = current; 4090 struct held_lock *hlock; 4091 struct lock_class_stats *stats; 4092 unsigned int depth; 4093 u64 now, waittime = 0; 4094 int i, cpu; 4095 4096 depth = curr->lockdep_depth; 4097 /* 4098 * Yay, we acquired ownership of this lock we didn't try to 4099 * acquire, how the heck did that happen? 4100 */ 4101 if (DEBUG_LOCKS_WARN_ON(!depth)) 4102 return; 4103 4104 hlock = find_held_lock(curr, lock, depth, &i); 4105 if (!hlock) { 4106 print_lock_contention_bug(curr, lock, _RET_IP_); 4107 return; 4108 } 4109 4110 if (hlock->instance != lock) 4111 return; 4112 4113 cpu = smp_processor_id(); 4114 if (hlock->waittime_stamp) { 4115 now = lockstat_clock(); 4116 waittime = now - hlock->waittime_stamp; 4117 hlock->holdtime_stamp = now; 4118 } 4119 4120 trace_lock_acquired(lock, ip); 4121 4122 stats = get_lock_stats(hlock_class(hlock)); 4123 if (waittime) { 4124 if (hlock->read) 4125 lock_time_inc(&stats->read_waittime, waittime); 4126 else 4127 lock_time_inc(&stats->write_waittime, waittime); 4128 } 4129 if (lock->cpu != cpu) 4130 stats->bounces[bounce_acquired + !!hlock->read]++; 4131 put_lock_stats(stats); 4132 4133 lock->cpu = cpu; 4134 lock->ip = ip; 4135 } 4136 4137 void lock_contended(struct lockdep_map *lock, unsigned long ip) 4138 { 4139 unsigned long flags; 4140 4141 if (unlikely(!lock_stat)) 4142 return; 4143 4144 if (unlikely(current->lockdep_recursion)) 4145 return; 4146 4147 raw_local_irq_save(flags); 4148 check_flags(flags); 4149 current->lockdep_recursion = 1; 4150 trace_lock_contended(lock, ip); 4151 __lock_contended(lock, ip); 4152 current->lockdep_recursion = 0; 4153 raw_local_irq_restore(flags); 4154 } 4155 EXPORT_SYMBOL_GPL(lock_contended); 4156 4157 void lock_acquired(struct lockdep_map *lock, unsigned long ip) 4158 { 4159 unsigned long flags; 4160 4161 if (unlikely(!lock_stat)) 4162 return; 4163 4164 if (unlikely(current->lockdep_recursion)) 4165 return; 4166 4167 raw_local_irq_save(flags); 4168 check_flags(flags); 4169 current->lockdep_recursion = 1; 4170 __lock_acquired(lock, ip); 4171 current->lockdep_recursion = 0; 4172 raw_local_irq_restore(flags); 4173 } 4174 EXPORT_SYMBOL_GPL(lock_acquired); 4175 #endif 4176 4177 /* 4178 * Used by the testsuite, sanitize the validator state 4179 * after a simulated failure: 4180 */ 4181 4182 void lockdep_reset(void) 4183 { 4184 unsigned long flags; 4185 int i; 4186 4187 raw_local_irq_save(flags); 4188 current->curr_chain_key = 0; 4189 current->lockdep_depth = 0; 4190 current->lockdep_recursion = 0; 4191 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); 4192 nr_hardirq_chains = 0; 4193 nr_softirq_chains = 0; 4194 nr_process_chains = 0; 4195 debug_locks = 1; 4196 for (i = 0; i < CHAINHASH_SIZE; i++) 4197 INIT_HLIST_HEAD(chainhash_table + i); 4198 raw_local_irq_restore(flags); 4199 } 4200 4201 static void zap_class(struct lock_class *class) 4202 { 4203 int i; 4204 4205 /* 4206 * Remove all dependencies this lock is 4207 * involved in: 4208 */ 4209 for (i = 0; i < nr_list_entries; i++) { 4210 if (list_entries[i].class == class) 4211 list_del_rcu(&list_entries[i].entry); 4212 } 4213 /* 4214 * Unhash the class and remove it from the all_lock_classes list: 4215 */ 4216 hlist_del_rcu(&class->hash_entry); 4217 list_del_rcu(&class->lock_entry); 4218 4219 RCU_INIT_POINTER(class->key, NULL); 4220 RCU_INIT_POINTER(class->name, NULL); 4221 } 4222 4223 static inline int within(const void *addr, void *start, unsigned long size) 4224 { 4225 return addr >= start && addr < start + size; 4226 } 4227 4228 /* 4229 * Used in module.c to remove lock classes from memory that is going to be 4230 * freed; and possibly re-used by other modules. 4231 * 4232 * We will have had one sync_sched() before getting here, so we're guaranteed 4233 * nobody will look up these exact classes -- they're properly dead but still 4234 * allocated. 4235 */ 4236 void lockdep_free_key_range(void *start, unsigned long size) 4237 { 4238 struct lock_class *class; 4239 struct hlist_head *head; 4240 unsigned long flags; 4241 int i; 4242 int locked; 4243 4244 raw_local_irq_save(flags); 4245 locked = graph_lock(); 4246 4247 /* 4248 * Unhash all classes that were created by this module: 4249 */ 4250 for (i = 0; i < CLASSHASH_SIZE; i++) { 4251 head = classhash_table + i; 4252 hlist_for_each_entry_rcu(class, head, hash_entry) { 4253 if (within(class->key, start, size)) 4254 zap_class(class); 4255 else if (within(class->name, start, size)) 4256 zap_class(class); 4257 } 4258 } 4259 4260 if (locked) 4261 graph_unlock(); 4262 raw_local_irq_restore(flags); 4263 4264 /* 4265 * Wait for any possible iterators from look_up_lock_class() to pass 4266 * before continuing to free the memory they refer to. 4267 * 4268 * sync_sched() is sufficient because the read-side is IRQ disable. 4269 */ 4270 synchronize_sched(); 4271 4272 /* 4273 * XXX at this point we could return the resources to the pool; 4274 * instead we leak them. We would need to change to bitmap allocators 4275 * instead of the linear allocators we have now. 4276 */ 4277 } 4278 4279 void lockdep_reset_lock(struct lockdep_map *lock) 4280 { 4281 struct lock_class *class; 4282 struct hlist_head *head; 4283 unsigned long flags; 4284 int i, j; 4285 int locked; 4286 4287 raw_local_irq_save(flags); 4288 4289 /* 4290 * Remove all classes this lock might have: 4291 */ 4292 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) { 4293 /* 4294 * If the class exists we look it up and zap it: 4295 */ 4296 class = look_up_lock_class(lock, j); 4297 if (!IS_ERR_OR_NULL(class)) 4298 zap_class(class); 4299 } 4300 /* 4301 * Debug check: in the end all mapped classes should 4302 * be gone. 4303 */ 4304 locked = graph_lock(); 4305 for (i = 0; i < CLASSHASH_SIZE; i++) { 4306 head = classhash_table + i; 4307 hlist_for_each_entry_rcu(class, head, hash_entry) { 4308 int match = 0; 4309 4310 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++) 4311 match |= class == lock->class_cache[j]; 4312 4313 if (unlikely(match)) { 4314 if (debug_locks_off_graph_unlock()) { 4315 /* 4316 * We all just reset everything, how did it match? 4317 */ 4318 WARN_ON(1); 4319 } 4320 goto out_restore; 4321 } 4322 } 4323 } 4324 if (locked) 4325 graph_unlock(); 4326 4327 out_restore: 4328 raw_local_irq_restore(flags); 4329 } 4330 4331 void __init lockdep_info(void) 4332 { 4333 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); 4334 4335 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); 4336 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); 4337 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); 4338 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); 4339 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES); 4340 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS); 4341 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE); 4342 4343 printk(" memory used by lock dependency info: %lu kB\n", 4344 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS + 4345 sizeof(struct list_head) * CLASSHASH_SIZE + 4346 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES + 4347 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS + 4348 sizeof(struct list_head) * CHAINHASH_SIZE 4349 #ifdef CONFIG_PROVE_LOCKING 4350 + sizeof(struct circular_queue) 4351 #endif 4352 ) / 1024 4353 ); 4354 4355 printk(" per task-struct memory footprint: %lu bytes\n", 4356 sizeof(struct held_lock) * MAX_LOCK_DEPTH); 4357 } 4358 4359 static void 4360 print_freed_lock_bug(struct task_struct *curr, const void *mem_from, 4361 const void *mem_to, struct held_lock *hlock) 4362 { 4363 if (!debug_locks_off()) 4364 return; 4365 if (debug_locks_silent) 4366 return; 4367 4368 pr_warn("\n"); 4369 pr_warn("=========================\n"); 4370 pr_warn("WARNING: held lock freed!\n"); 4371 print_kernel_ident(); 4372 pr_warn("-------------------------\n"); 4373 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n", 4374 curr->comm, task_pid_nr(curr), mem_from, mem_to-1); 4375 print_lock(hlock); 4376 lockdep_print_held_locks(curr); 4377 4378 pr_warn("\nstack backtrace:\n"); 4379 dump_stack(); 4380 } 4381 4382 static inline int not_in_range(const void* mem_from, unsigned long mem_len, 4383 const void* lock_from, unsigned long lock_len) 4384 { 4385 return lock_from + lock_len <= mem_from || 4386 mem_from + mem_len <= lock_from; 4387 } 4388 4389 /* 4390 * Called when kernel memory is freed (or unmapped), or if a lock 4391 * is destroyed or reinitialized - this code checks whether there is 4392 * any held lock in the memory range of <from> to <to>: 4393 */ 4394 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) 4395 { 4396 struct task_struct *curr = current; 4397 struct held_lock *hlock; 4398 unsigned long flags; 4399 int i; 4400 4401 if (unlikely(!debug_locks)) 4402 return; 4403 4404 local_irq_save(flags); 4405 for (i = 0; i < curr->lockdep_depth; i++) { 4406 hlock = curr->held_locks + i; 4407 4408 if (not_in_range(mem_from, mem_len, hlock->instance, 4409 sizeof(*hlock->instance))) 4410 continue; 4411 4412 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); 4413 break; 4414 } 4415 local_irq_restore(flags); 4416 } 4417 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); 4418 4419 static void print_held_locks_bug(void) 4420 { 4421 if (!debug_locks_off()) 4422 return; 4423 if (debug_locks_silent) 4424 return; 4425 4426 pr_warn("\n"); 4427 pr_warn("====================================\n"); 4428 pr_warn("WARNING: %s/%d still has locks held!\n", 4429 current->comm, task_pid_nr(current)); 4430 print_kernel_ident(); 4431 pr_warn("------------------------------------\n"); 4432 lockdep_print_held_locks(current); 4433 pr_warn("\nstack backtrace:\n"); 4434 dump_stack(); 4435 } 4436 4437 void debug_check_no_locks_held(void) 4438 { 4439 if (unlikely(current->lockdep_depth > 0)) 4440 print_held_locks_bug(); 4441 } 4442 EXPORT_SYMBOL_GPL(debug_check_no_locks_held); 4443 4444 #ifdef __KERNEL__ 4445 void debug_show_all_locks(void) 4446 { 4447 struct task_struct *g, *p; 4448 int count = 10; 4449 int unlock = 1; 4450 4451 if (unlikely(!debug_locks)) { 4452 pr_warn("INFO: lockdep is turned off.\n"); 4453 return; 4454 } 4455 pr_warn("\nShowing all locks held in the system:\n"); 4456 4457 /* 4458 * Here we try to get the tasklist_lock as hard as possible, 4459 * if not successful after 2 seconds we ignore it (but keep 4460 * trying). This is to enable a debug printout even if a 4461 * tasklist_lock-holding task deadlocks or crashes. 4462 */ 4463 retry: 4464 if (!read_trylock(&tasklist_lock)) { 4465 if (count == 10) 4466 pr_warn("hm, tasklist_lock locked, retrying... "); 4467 if (count) { 4468 count--; 4469 pr_cont(" #%d", 10-count); 4470 mdelay(200); 4471 goto retry; 4472 } 4473 pr_cont(" ignoring it.\n"); 4474 unlock = 0; 4475 } else { 4476 if (count != 10) 4477 pr_cont(" locked it.\n"); 4478 } 4479 4480 do_each_thread(g, p) { 4481 /* 4482 * It's not reliable to print a task's held locks 4483 * if it's not sleeping (or if it's not the current 4484 * task): 4485 */ 4486 if (p->state == TASK_RUNNING && p != current) 4487 continue; 4488 if (p->lockdep_depth) 4489 lockdep_print_held_locks(p); 4490 if (!unlock) 4491 if (read_trylock(&tasklist_lock)) 4492 unlock = 1; 4493 } while_each_thread(g, p); 4494 4495 pr_warn("\n"); 4496 pr_warn("=============================================\n\n"); 4497 4498 if (unlock) 4499 read_unlock(&tasklist_lock); 4500 } 4501 EXPORT_SYMBOL_GPL(debug_show_all_locks); 4502 #endif 4503 4504 /* 4505 * Careful: only use this function if you are sure that 4506 * the task cannot run in parallel! 4507 */ 4508 void debug_show_held_locks(struct task_struct *task) 4509 { 4510 if (unlikely(!debug_locks)) { 4511 printk("INFO: lockdep is turned off.\n"); 4512 return; 4513 } 4514 lockdep_print_held_locks(task); 4515 } 4516 EXPORT_SYMBOL_GPL(debug_show_held_locks); 4517 4518 asmlinkage __visible void lockdep_sys_exit(void) 4519 { 4520 struct task_struct *curr = current; 4521 4522 if (unlikely(curr->lockdep_depth)) { 4523 if (!debug_locks_off()) 4524 return; 4525 pr_warn("\n"); 4526 pr_warn("================================================\n"); 4527 pr_warn("WARNING: lock held when returning to user space!\n"); 4528 print_kernel_ident(); 4529 pr_warn("------------------------------------------------\n"); 4530 pr_warn("%s/%d is leaving the kernel with locks still held!\n", 4531 curr->comm, curr->pid); 4532 lockdep_print_held_locks(curr); 4533 } 4534 4535 /* 4536 * The lock history for each syscall should be independent. So wipe the 4537 * slate clean on return to userspace. 4538 */ 4539 lockdep_invariant_state(false); 4540 } 4541 4542 void lockdep_rcu_suspicious(const char *file, const int line, const char *s) 4543 { 4544 struct task_struct *curr = current; 4545 4546 /* Note: the following can be executed concurrently, so be careful. */ 4547 pr_warn("\n"); 4548 pr_warn("=============================\n"); 4549 pr_warn("WARNING: suspicious RCU usage\n"); 4550 print_kernel_ident(); 4551 pr_warn("-----------------------------\n"); 4552 pr_warn("%s:%d %s!\n", file, line, s); 4553 pr_warn("\nother info that might help us debug this:\n\n"); 4554 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n", 4555 !rcu_lockdep_current_cpu_online() 4556 ? "RCU used illegally from offline CPU!\n" 4557 : !rcu_is_watching() 4558 ? "RCU used illegally from idle CPU!\n" 4559 : "", 4560 rcu_scheduler_active, debug_locks); 4561 4562 /* 4563 * If a CPU is in the RCU-free window in idle (ie: in the section 4564 * between rcu_idle_enter() and rcu_idle_exit(), then RCU 4565 * considers that CPU to be in an "extended quiescent state", 4566 * which means that RCU will be completely ignoring that CPU. 4567 * Therefore, rcu_read_lock() and friends have absolutely no 4568 * effect on a CPU running in that state. In other words, even if 4569 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well 4570 * delete data structures out from under it. RCU really has no 4571 * choice here: we need to keep an RCU-free window in idle where 4572 * the CPU may possibly enter into low power mode. This way we can 4573 * notice an extended quiescent state to other CPUs that started a grace 4574 * period. Otherwise we would delay any grace period as long as we run 4575 * in the idle task. 4576 * 4577 * So complain bitterly if someone does call rcu_read_lock(), 4578 * rcu_read_lock_bh() and so on from extended quiescent states. 4579 */ 4580 if (!rcu_is_watching()) 4581 pr_warn("RCU used illegally from extended quiescent state!\n"); 4582 4583 lockdep_print_held_locks(curr); 4584 pr_warn("\nstack backtrace:\n"); 4585 dump_stack(); 4586 } 4587 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); 4588