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 <pzijlstr@redhat.com> 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/delay.h> 32#include <linux/module.h> 33#include <linux/proc_fs.h> 34#include <linux/seq_file.h> 35#include <linux/spinlock.h> 36#include <linux/kallsyms.h> 37#include <linux/interrupt.h> 38#include <linux/stacktrace.h> 39#include <linux/debug_locks.h> 40#include <linux/irqflags.h> 41#include <linux/utsname.h> 42#include <linux/hash.h> 43#include <linux/ftrace.h> 44#include <linux/stringify.h> 45#include <linux/bitops.h> 46#include <linux/gfp.h> 47 48#include <asm/sections.h> 49 50#include "lockdep_internals.h" 51 52#define CREATE_TRACE_POINTS 53#include <trace/events/lock.h> 54 55#ifdef CONFIG_PROVE_LOCKING 56int prove_locking = 1; 57module_param(prove_locking, int, 0644); 58#else 59#define prove_locking 0 60#endif 61 62#ifdef CONFIG_LOCK_STAT 63int lock_stat = 1; 64module_param(lock_stat, int, 0644); 65#else 66#define lock_stat 0 67#endif 68 69/* 70 * lockdep_lock: protects the lockdep graph, the hashes and the 71 * class/list/hash allocators. 72 * 73 * This is one of the rare exceptions where it's justified 74 * to use a raw spinlock - we really dont want the spinlock 75 * code to recurse back into the lockdep code... 76 */ 77static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 78 79static int graph_lock(void) 80{ 81 arch_spin_lock(&lockdep_lock); 82 /* 83 * Make sure that if another CPU detected a bug while 84 * walking the graph we dont change it (while the other 85 * CPU is busy printing out stuff with the graph lock 86 * dropped already) 87 */ 88 if (!debug_locks) { 89 arch_spin_unlock(&lockdep_lock); 90 return 0; 91 } 92 /* prevent any recursions within lockdep from causing deadlocks */ 93 current->lockdep_recursion++; 94 return 1; 95} 96 97static inline int graph_unlock(void) 98{ 99 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) 100 return DEBUG_LOCKS_WARN_ON(1); 101 102 current->lockdep_recursion--; 103 arch_spin_unlock(&lockdep_lock); 104 return 0; 105} 106 107/* 108 * Turn lock debugging off and return with 0 if it was off already, 109 * and also release the graph lock: 110 */ 111static inline int debug_locks_off_graph_unlock(void) 112{ 113 int ret = debug_locks_off(); 114 115 arch_spin_unlock(&lockdep_lock); 116 117 return ret; 118} 119 120static int lockdep_initialized; 121 122unsigned long nr_list_entries; 123static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; 124 125/* 126 * All data structures here are protected by the global debug_lock. 127 * 128 * Mutex key structs only get allocated, once during bootup, and never 129 * get freed - this significantly simplifies the debugging code. 130 */ 131unsigned long nr_lock_classes; 132static struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; 133 134static inline struct lock_class *hlock_class(struct held_lock *hlock) 135{ 136 if (!hlock->class_idx) { 137 DEBUG_LOCKS_WARN_ON(1); 138 return NULL; 139 } 140 return lock_classes + hlock->class_idx - 1; 141} 142 143#ifdef CONFIG_LOCK_STAT 144static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], 145 cpu_lock_stats); 146 147static inline u64 lockstat_clock(void) 148{ 149 return local_clock(); 150} 151 152static int lock_point(unsigned long points[], unsigned long ip) 153{ 154 int i; 155 156 for (i = 0; i < LOCKSTAT_POINTS; i++) { 157 if (points[i] == 0) { 158 points[i] = ip; 159 break; 160 } 161 if (points[i] == ip) 162 break; 163 } 164 165 return i; 166} 167 168static void lock_time_inc(struct lock_time *lt, u64 time) 169{ 170 if (time > lt->max) 171 lt->max = time; 172 173 if (time < lt->min || !lt->nr) 174 lt->min = time; 175 176 lt->total += time; 177 lt->nr++; 178} 179 180static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) 181{ 182 if (!src->nr) 183 return; 184 185 if (src->max > dst->max) 186 dst->max = src->max; 187 188 if (src->min < dst->min || !dst->nr) 189 dst->min = src->min; 190 191 dst->total += src->total; 192 dst->nr += src->nr; 193} 194 195struct lock_class_stats lock_stats(struct lock_class *class) 196{ 197 struct lock_class_stats stats; 198 int cpu, i; 199 200 memset(&stats, 0, sizeof(struct lock_class_stats)); 201 for_each_possible_cpu(cpu) { 202 struct lock_class_stats *pcs = 203 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; 204 205 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) 206 stats.contention_point[i] += pcs->contention_point[i]; 207 208 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) 209 stats.contending_point[i] += pcs->contending_point[i]; 210 211 lock_time_add(&pcs->read_waittime, &stats.read_waittime); 212 lock_time_add(&pcs->write_waittime, &stats.write_waittime); 213 214 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); 215 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); 216 217 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) 218 stats.bounces[i] += pcs->bounces[i]; 219 } 220 221 return stats; 222} 223 224void clear_lock_stats(struct lock_class *class) 225{ 226 int cpu; 227 228 for_each_possible_cpu(cpu) { 229 struct lock_class_stats *cpu_stats = 230 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; 231 232 memset(cpu_stats, 0, sizeof(struct lock_class_stats)); 233 } 234 memset(class->contention_point, 0, sizeof(class->contention_point)); 235 memset(class->contending_point, 0, sizeof(class->contending_point)); 236} 237 238static struct lock_class_stats *get_lock_stats(struct lock_class *class) 239{ 240 return &get_cpu_var(cpu_lock_stats)[class - lock_classes]; 241} 242 243static void put_lock_stats(struct lock_class_stats *stats) 244{ 245 put_cpu_var(cpu_lock_stats); 246} 247 248static void lock_release_holdtime(struct held_lock *hlock) 249{ 250 struct lock_class_stats *stats; 251 u64 holdtime; 252 253 if (!lock_stat) 254 return; 255 256 holdtime = lockstat_clock() - hlock->holdtime_stamp; 257 258 stats = get_lock_stats(hlock_class(hlock)); 259 if (hlock->read) 260 lock_time_inc(&stats->read_holdtime, holdtime); 261 else 262 lock_time_inc(&stats->write_holdtime, holdtime); 263 put_lock_stats(stats); 264} 265#else 266static inline void lock_release_holdtime(struct held_lock *hlock) 267{ 268} 269#endif 270 271/* 272 * We keep a global list of all lock classes. The list only grows, 273 * never shrinks. The list is only accessed with the lockdep 274 * spinlock lock held. 275 */ 276LIST_HEAD(all_lock_classes); 277 278/* 279 * The lockdep classes are in a hash-table as well, for fast lookup: 280 */ 281#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) 282#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS) 283#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) 284#define classhashentry(key) (classhash_table + __classhashfn((key))) 285 286static struct list_head classhash_table[CLASSHASH_SIZE]; 287 288/* 289 * We put the lock dependency chains into a hash-table as well, to cache 290 * their existence: 291 */ 292#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1) 293#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS) 294#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) 295#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) 296 297static struct list_head chainhash_table[CHAINHASH_SIZE]; 298 299/* 300 * The hash key of the lock dependency chains is a hash itself too: 301 * it's a hash of all locks taken up to that lock, including that lock. 302 * It's a 64-bit hash, because it's important for the keys to be 303 * unique. 304 */ 305#define iterate_chain_key(key1, key2) \ 306 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \ 307 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \ 308 (key2)) 309 310void lockdep_off(void) 311{ 312 current->lockdep_recursion++; 313} 314EXPORT_SYMBOL(lockdep_off); 315 316void lockdep_on(void) 317{ 318 current->lockdep_recursion--; 319} 320EXPORT_SYMBOL(lockdep_on); 321 322/* 323 * Debugging switches: 324 */ 325 326#define VERBOSE 0 327#define VERY_VERBOSE 0 328 329#if VERBOSE 330# define HARDIRQ_VERBOSE 1 331# define SOFTIRQ_VERBOSE 1 332# define RECLAIM_VERBOSE 1 333#else 334# define HARDIRQ_VERBOSE 0 335# define SOFTIRQ_VERBOSE 0 336# define RECLAIM_VERBOSE 0 337#endif 338 339#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE 340/* 341 * Quick filtering for interesting events: 342 */ 343static int class_filter(struct lock_class *class) 344{ 345 /* Filter everything else. 1 would be to allow everything else */ 346 return 0; 347} 348#endif 349 350static int verbose(struct lock_class *class) 351{ 352#if VERBOSE 353 return class_filter(class); 354#endif 355 return 0; 356} 357 358/* 359 * Stack-trace: tightly packed array of stack backtrace 360 * addresses. Protected by the graph_lock. 361 */ 362unsigned long nr_stack_trace_entries; 363static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; 364 365static int save_trace(struct stack_trace *trace) 366{ 367 trace->nr_entries = 0; 368 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries; 369 trace->entries = stack_trace + nr_stack_trace_entries; 370 371 trace->skip = 3; 372 373 save_stack_trace(trace); 374 375 /* 376 * Some daft arches put -1 at the end to indicate its a full trace. 377 * 378 * <rant> this is buggy anyway, since it takes a whole extra entry so a 379 * complete trace that maxes out the entries provided will be reported 380 * as incomplete, friggin useless </rant> 381 */ 382 if (trace->nr_entries != 0 && 383 trace->entries[trace->nr_entries-1] == ULONG_MAX) 384 trace->nr_entries--; 385 386 trace->max_entries = trace->nr_entries; 387 388 nr_stack_trace_entries += trace->nr_entries; 389 390 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) { 391 if (!debug_locks_off_graph_unlock()) 392 return 0; 393 394 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n"); 395 printk("turning off the locking correctness validator.\n"); 396 dump_stack(); 397 398 return 0; 399 } 400 401 return 1; 402} 403 404unsigned int nr_hardirq_chains; 405unsigned int nr_softirq_chains; 406unsigned int nr_process_chains; 407unsigned int max_lockdep_depth; 408 409#ifdef CONFIG_DEBUG_LOCKDEP 410/* 411 * We cannot printk in early bootup code. Not even early_printk() 412 * might work. So we mark any initialization errors and printk 413 * about it later on, in lockdep_info(). 414 */ 415static int lockdep_init_error; 416static unsigned long lockdep_init_trace_data[20]; 417static struct stack_trace lockdep_init_trace = { 418 .max_entries = ARRAY_SIZE(lockdep_init_trace_data), 419 .entries = lockdep_init_trace_data, 420}; 421 422/* 423 * Various lockdep statistics: 424 */ 425DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats); 426#endif 427 428/* 429 * Locking printouts: 430 */ 431 432#define __USAGE(__STATE) \ 433 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \ 434 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \ 435 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\ 436 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R", 437 438static const char *usage_str[] = 439{ 440#define LOCKDEP_STATE(__STATE) __USAGE(__STATE) 441#include "lockdep_states.h" 442#undef LOCKDEP_STATE 443 [LOCK_USED] = "INITIAL USE", 444}; 445 446const char * __get_key_name(struct lockdep_subclass_key *key, char *str) 447{ 448 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); 449} 450 451static inline unsigned long lock_flag(enum lock_usage_bit bit) 452{ 453 return 1UL << bit; 454} 455 456static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit) 457{ 458 char c = '.'; 459 460 if (class->usage_mask & lock_flag(bit + 2)) 461 c = '+'; 462 if (class->usage_mask & lock_flag(bit)) { 463 c = '-'; 464 if (class->usage_mask & lock_flag(bit + 2)) 465 c = '?'; 466 } 467 468 return c; 469} 470 471void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]) 472{ 473 int i = 0; 474 475#define LOCKDEP_STATE(__STATE) \ 476 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \ 477 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ); 478#include "lockdep_states.h" 479#undef LOCKDEP_STATE 480 481 usage[i] = '\0'; 482} 483 484static void print_lock_name(struct lock_class *class) 485{ 486 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS]; 487 const char *name; 488 489 get_usage_chars(class, usage); 490 491 name = class->name; 492 if (!name) { 493 name = __get_key_name(class->key, str); 494 printk(" (%s", name); 495 } else { 496 printk(" (%s", name); 497 if (class->name_version > 1) 498 printk("#%d", class->name_version); 499 if (class->subclass) 500 printk("/%d", class->subclass); 501 } 502 printk("){%s}", usage); 503} 504 505static void print_lockdep_cache(struct lockdep_map *lock) 506{ 507 const char *name; 508 char str[KSYM_NAME_LEN]; 509 510 name = lock->name; 511 if (!name) 512 name = __get_key_name(lock->key->subkeys, str); 513 514 printk("%s", name); 515} 516 517static void print_lock(struct held_lock *hlock) 518{ 519 print_lock_name(hlock_class(hlock)); 520 printk(", at: "); 521 print_ip_sym(hlock->acquire_ip); 522} 523 524static void lockdep_print_held_locks(struct task_struct *curr) 525{ 526 int i, depth = curr->lockdep_depth; 527 528 if (!depth) { 529 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr)); 530 return; 531 } 532 printk("%d lock%s held by %s/%d:\n", 533 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr)); 534 535 for (i = 0; i < depth; i++) { 536 printk(" #%d: ", i); 537 print_lock(curr->held_locks + i); 538 } 539} 540 541static void print_kernel_version(void) 542{ 543 printk("%s %.*s\n", init_utsname()->release, 544 (int)strcspn(init_utsname()->version, " "), 545 init_utsname()->version); 546} 547 548static int very_verbose(struct lock_class *class) 549{ 550#if VERY_VERBOSE 551 return class_filter(class); 552#endif 553 return 0; 554} 555 556/* 557 * Is this the address of a static object: 558 */ 559static int static_obj(void *obj) 560{ 561 unsigned long start = (unsigned long) &_stext, 562 end = (unsigned long) &_end, 563 addr = (unsigned long) obj; 564 565 /* 566 * static variable? 567 */ 568 if ((addr >= start) && (addr < end)) 569 return 1; 570 571 if (arch_is_kernel_data(addr)) 572 return 1; 573 574 /* 575 * in-kernel percpu var? 576 */ 577 if (is_kernel_percpu_address(addr)) 578 return 1; 579 580 /* 581 * module static or percpu var? 582 */ 583 return is_module_address(addr) || is_module_percpu_address(addr); 584} 585 586/* 587 * To make lock name printouts unique, we calculate a unique 588 * class->name_version generation counter: 589 */ 590static int count_matching_names(struct lock_class *new_class) 591{ 592 struct lock_class *class; 593 int count = 0; 594 595 if (!new_class->name) 596 return 0; 597 598 list_for_each_entry(class, &all_lock_classes, lock_entry) { 599 if (new_class->key - new_class->subclass == class->key) 600 return class->name_version; 601 if (class->name && !strcmp(class->name, new_class->name)) 602 count = max(count, class->name_version); 603 } 604 605 return count + 1; 606} 607 608/* 609 * Register a lock's class in the hash-table, if the class is not present 610 * yet. Otherwise we look it up. We cache the result in the lock object 611 * itself, so actual lookup of the hash should be once per lock object. 612 */ 613static inline struct lock_class * 614look_up_lock_class(struct lockdep_map *lock, unsigned int subclass) 615{ 616 struct lockdep_subclass_key *key; 617 struct list_head *hash_head; 618 struct lock_class *class; 619 620#ifdef CONFIG_DEBUG_LOCKDEP 621 /* 622 * If the architecture calls into lockdep before initializing 623 * the hashes then we'll warn about it later. (we cannot printk 624 * right now) 625 */ 626 if (unlikely(!lockdep_initialized)) { 627 lockdep_init(); 628 lockdep_init_error = 1; 629 save_stack_trace(&lockdep_init_trace); 630 } 631#endif 632 633 /* 634 * Static locks do not have their class-keys yet - for them the key 635 * is the lock object itself: 636 */ 637 if (unlikely(!lock->key)) 638 lock->key = (void *)lock; 639 640 /* 641 * NOTE: the class-key must be unique. For dynamic locks, a static 642 * lock_class_key variable is passed in through the mutex_init() 643 * (or spin_lock_init()) call - which acts as the key. For static 644 * locks we use the lock object itself as the key. 645 */ 646 BUILD_BUG_ON(sizeof(struct lock_class_key) > 647 sizeof(struct lockdep_map)); 648 649 key = lock->key->subkeys + subclass; 650 651 hash_head = classhashentry(key); 652 653 /* 654 * We can walk the hash lockfree, because the hash only 655 * grows, and we are careful when adding entries to the end: 656 */ 657 list_for_each_entry(class, hash_head, hash_entry) { 658 if (class->key == key) { 659 WARN_ON_ONCE(class->name != lock->name); 660 return class; 661 } 662 } 663 664 return NULL; 665} 666 667/* 668 * Register a lock's class in the hash-table, if the class is not present 669 * yet. Otherwise we look it up. We cache the result in the lock object 670 * itself, so actual lookup of the hash should be once per lock object. 671 */ 672static inline struct lock_class * 673register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) 674{ 675 struct lockdep_subclass_key *key; 676 struct list_head *hash_head; 677 struct lock_class *class; 678 unsigned long flags; 679 680 class = look_up_lock_class(lock, subclass); 681 if (likely(class)) 682 return class; 683 684 /* 685 * Debug-check: all keys must be persistent! 686 */ 687 if (!static_obj(lock->key)) { 688 debug_locks_off(); 689 printk("INFO: trying to register non-static key.\n"); 690 printk("the code is fine but needs lockdep annotation.\n"); 691 printk("turning off the locking correctness validator.\n"); 692 dump_stack(); 693 694 return NULL; 695 } 696 697 key = lock->key->subkeys + subclass; 698 hash_head = classhashentry(key); 699 700 raw_local_irq_save(flags); 701 if (!graph_lock()) { 702 raw_local_irq_restore(flags); 703 return NULL; 704 } 705 /* 706 * We have to do the hash-walk again, to avoid races 707 * with another CPU: 708 */ 709 list_for_each_entry(class, hash_head, hash_entry) 710 if (class->key == key) 711 goto out_unlock_set; 712 /* 713 * Allocate a new key from the static array, and add it to 714 * the hash: 715 */ 716 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) { 717 if (!debug_locks_off_graph_unlock()) { 718 raw_local_irq_restore(flags); 719 return NULL; 720 } 721 raw_local_irq_restore(flags); 722 723 printk("BUG: MAX_LOCKDEP_KEYS too low!\n"); 724 printk("turning off the locking correctness validator.\n"); 725 dump_stack(); 726 return NULL; 727 } 728 class = lock_classes + nr_lock_classes++; 729 debug_atomic_inc(nr_unused_locks); 730 class->key = key; 731 class->name = lock->name; 732 class->subclass = subclass; 733 INIT_LIST_HEAD(&class->lock_entry); 734 INIT_LIST_HEAD(&class->locks_before); 735 INIT_LIST_HEAD(&class->locks_after); 736 class->name_version = count_matching_names(class); 737 /* 738 * We use RCU's safe list-add method to make 739 * parallel walking of the hash-list safe: 740 */ 741 list_add_tail_rcu(&class->hash_entry, hash_head); 742 /* 743 * Add it to the global list of classes: 744 */ 745 list_add_tail_rcu(&class->lock_entry, &all_lock_classes); 746 747 if (verbose(class)) { 748 graph_unlock(); 749 raw_local_irq_restore(flags); 750 751 printk("\nnew class %p: %s", class->key, class->name); 752 if (class->name_version > 1) 753 printk("#%d", class->name_version); 754 printk("\n"); 755 dump_stack(); 756 757 raw_local_irq_save(flags); 758 if (!graph_lock()) { 759 raw_local_irq_restore(flags); 760 return NULL; 761 } 762 } 763out_unlock_set: 764 graph_unlock(); 765 raw_local_irq_restore(flags); 766 767 if (!subclass || force) 768 lock->class_cache = class; 769 770 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) 771 return NULL; 772 773 return class; 774} 775 776#ifdef CONFIG_PROVE_LOCKING 777/* 778 * Allocate a lockdep entry. (assumes the graph_lock held, returns 779 * with NULL on failure) 780 */ 781static struct lock_list *alloc_list_entry(void) 782{ 783 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) { 784 if (!debug_locks_off_graph_unlock()) 785 return NULL; 786 787 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n"); 788 printk("turning off the locking correctness validator.\n"); 789 dump_stack(); 790 return NULL; 791 } 792 return list_entries + nr_list_entries++; 793} 794 795/* 796 * Add a new dependency to the head of the list: 797 */ 798static int add_lock_to_list(struct lock_class *class, struct lock_class *this, 799 struct list_head *head, unsigned long ip, 800 int distance, struct stack_trace *trace) 801{ 802 struct lock_list *entry; 803 /* 804 * Lock not present yet - get a new dependency struct and 805 * add it to the list: 806 */ 807 entry = alloc_list_entry(); 808 if (!entry) 809 return 0; 810 811 entry->class = this; 812 entry->distance = distance; 813 entry->trace = *trace; 814 /* 815 * Since we never remove from the dependency list, the list can 816 * be walked lockless by other CPUs, it's only allocation 817 * that must be protected by the spinlock. But this also means 818 * we must make new entries visible only once writes to the 819 * entry become visible - hence the RCU op: 820 */ 821 list_add_tail_rcu(&entry->entry, head); 822 823 return 1; 824} 825 826/* 827 * For good efficiency of modular, we use power of 2 828 */ 829#define MAX_CIRCULAR_QUEUE_SIZE 4096UL 830#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1) 831 832/* 833 * The circular_queue and helpers is used to implement the 834 * breadth-first search(BFS)algorithem, by which we can build 835 * the shortest path from the next lock to be acquired to the 836 * previous held lock if there is a circular between them. 837 */ 838struct circular_queue { 839 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE]; 840 unsigned int front, rear; 841}; 842 843static struct circular_queue lock_cq; 844 845unsigned int max_bfs_queue_depth; 846 847static unsigned int lockdep_dependency_gen_id; 848 849static inline void __cq_init(struct circular_queue *cq) 850{ 851 cq->front = cq->rear = 0; 852 lockdep_dependency_gen_id++; 853} 854 855static inline int __cq_empty(struct circular_queue *cq) 856{ 857 return (cq->front == cq->rear); 858} 859 860static inline int __cq_full(struct circular_queue *cq) 861{ 862 return ((cq->rear + 1) & CQ_MASK) == cq->front; 863} 864 865static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem) 866{ 867 if (__cq_full(cq)) 868 return -1; 869 870 cq->element[cq->rear] = elem; 871 cq->rear = (cq->rear + 1) & CQ_MASK; 872 return 0; 873} 874 875static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem) 876{ 877 if (__cq_empty(cq)) 878 return -1; 879 880 *elem = cq->element[cq->front]; 881 cq->front = (cq->front + 1) & CQ_MASK; 882 return 0; 883} 884 885static inline unsigned int __cq_get_elem_count(struct circular_queue *cq) 886{ 887 return (cq->rear - cq->front) & CQ_MASK; 888} 889 890static inline void mark_lock_accessed(struct lock_list *lock, 891 struct lock_list *parent) 892{ 893 unsigned long nr; 894 895 nr = lock - list_entries; 896 WARN_ON(nr >= nr_list_entries); 897 lock->parent = parent; 898 lock->class->dep_gen_id = lockdep_dependency_gen_id; 899} 900 901static inline unsigned long lock_accessed(struct lock_list *lock) 902{ 903 unsigned long nr; 904 905 nr = lock - list_entries; 906 WARN_ON(nr >= nr_list_entries); 907 return lock->class->dep_gen_id == lockdep_dependency_gen_id; 908} 909 910static inline struct lock_list *get_lock_parent(struct lock_list *child) 911{ 912 return child->parent; 913} 914 915static inline int get_lock_depth(struct lock_list *child) 916{ 917 int depth = 0; 918 struct lock_list *parent; 919 920 while ((parent = get_lock_parent(child))) { 921 child = parent; 922 depth++; 923 } 924 return depth; 925} 926 927static int __bfs(struct lock_list *source_entry, 928 void *data, 929 int (*match)(struct lock_list *entry, void *data), 930 struct lock_list **target_entry, 931 int forward) 932{ 933 struct lock_list *entry; 934 struct list_head *head; 935 struct circular_queue *cq = &lock_cq; 936 int ret = 1; 937 938 if (match(source_entry, data)) { 939 *target_entry = source_entry; 940 ret = 0; 941 goto exit; 942 } 943 944 if (forward) 945 head = &source_entry->class->locks_after; 946 else 947 head = &source_entry->class->locks_before; 948 949 if (list_empty(head)) 950 goto exit; 951 952 __cq_init(cq); 953 __cq_enqueue(cq, (unsigned long)source_entry); 954 955 while (!__cq_empty(cq)) { 956 struct lock_list *lock; 957 958 __cq_dequeue(cq, (unsigned long *)&lock); 959 960 if (!lock->class) { 961 ret = -2; 962 goto exit; 963 } 964 965 if (forward) 966 head = &lock->class->locks_after; 967 else 968 head = &lock->class->locks_before; 969 970 list_for_each_entry(entry, head, entry) { 971 if (!lock_accessed(entry)) { 972 unsigned int cq_depth; 973 mark_lock_accessed(entry, lock); 974 if (match(entry, data)) { 975 *target_entry = entry; 976 ret = 0; 977 goto exit; 978 } 979 980 if (__cq_enqueue(cq, (unsigned long)entry)) { 981 ret = -1; 982 goto exit; 983 } 984 cq_depth = __cq_get_elem_count(cq); 985 if (max_bfs_queue_depth < cq_depth) 986 max_bfs_queue_depth = cq_depth; 987 } 988 } 989 } 990exit: 991 return ret; 992} 993 994static inline int __bfs_forwards(struct lock_list *src_entry, 995 void *data, 996 int (*match)(struct lock_list *entry, void *data), 997 struct lock_list **target_entry) 998{ 999 return __bfs(src_entry, data, match, target_entry, 1); 1000 1001} 1002 1003static inline int __bfs_backwards(struct lock_list *src_entry, 1004 void *data, 1005 int (*match)(struct lock_list *entry, void *data), 1006 struct lock_list **target_entry) 1007{ 1008 return __bfs(src_entry, data, match, target_entry, 0); 1009 1010} 1011 1012/* 1013 * Recursive, forwards-direction lock-dependency checking, used for 1014 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe 1015 * checking. 1016 */ 1017 1018/* 1019 * Print a dependency chain entry (this is only done when a deadlock 1020 * has been detected): 1021 */ 1022static noinline int 1023print_circular_bug_entry(struct lock_list *target, int depth) 1024{ 1025 if (debug_locks_silent) 1026 return 0; 1027 printk("\n-> #%u", depth); 1028 print_lock_name(target->class); 1029 printk(":\n"); 1030 print_stack_trace(&target->trace, 6); 1031 1032 return 0; 1033} 1034 1035/* 1036 * When a circular dependency is detected, print the 1037 * header first: 1038 */ 1039static noinline int 1040print_circular_bug_header(struct lock_list *entry, unsigned int depth, 1041 struct held_lock *check_src, 1042 struct held_lock *check_tgt) 1043{ 1044 struct task_struct *curr = current; 1045 1046 if (debug_locks_silent) 1047 return 0; 1048 1049 printk("\n=======================================================\n"); 1050 printk( "[ INFO: possible circular locking dependency detected ]\n"); 1051 print_kernel_version(); 1052 printk( "-------------------------------------------------------\n"); 1053 printk("%s/%d is trying to acquire lock:\n", 1054 curr->comm, task_pid_nr(curr)); 1055 print_lock(check_src); 1056 printk("\nbut task is already holding lock:\n"); 1057 print_lock(check_tgt); 1058 printk("\nwhich lock already depends on the new lock.\n\n"); 1059 printk("\nthe existing dependency chain (in reverse order) is:\n"); 1060 1061 print_circular_bug_entry(entry, depth); 1062 1063 return 0; 1064} 1065 1066static inline int class_equal(struct lock_list *entry, void *data) 1067{ 1068 return entry->class == data; 1069} 1070 1071static noinline int print_circular_bug(struct lock_list *this, 1072 struct lock_list *target, 1073 struct held_lock *check_src, 1074 struct held_lock *check_tgt) 1075{ 1076 struct task_struct *curr = current; 1077 struct lock_list *parent; 1078 int depth; 1079 1080 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1081 return 0; 1082 1083 if (!save_trace(&this->trace)) 1084 return 0; 1085 1086 depth = get_lock_depth(target); 1087 1088 print_circular_bug_header(target, depth, check_src, check_tgt); 1089 1090 parent = get_lock_parent(target); 1091 1092 while (parent) { 1093 print_circular_bug_entry(parent, --depth); 1094 parent = get_lock_parent(parent); 1095 } 1096 1097 printk("\nother info that might help us debug this:\n\n"); 1098 lockdep_print_held_locks(curr); 1099 1100 printk("\nstack backtrace:\n"); 1101 dump_stack(); 1102 1103 return 0; 1104} 1105 1106static noinline int print_bfs_bug(int ret) 1107{ 1108 if (!debug_locks_off_graph_unlock()) 1109 return 0; 1110 1111 WARN(1, "lockdep bfs error:%d\n", ret); 1112 1113 return 0; 1114} 1115 1116static int noop_count(struct lock_list *entry, void *data) 1117{ 1118 (*(unsigned long *)data)++; 1119 return 0; 1120} 1121 1122unsigned long __lockdep_count_forward_deps(struct lock_list *this) 1123{ 1124 unsigned long count = 0; 1125 struct lock_list *uninitialized_var(target_entry); 1126 1127 __bfs_forwards(this, (void *)&count, noop_count, &target_entry); 1128 1129 return count; 1130} 1131unsigned long lockdep_count_forward_deps(struct lock_class *class) 1132{ 1133 unsigned long ret, flags; 1134 struct lock_list this; 1135 1136 this.parent = NULL; 1137 this.class = class; 1138 1139 local_irq_save(flags); 1140 arch_spin_lock(&lockdep_lock); 1141 ret = __lockdep_count_forward_deps(&this); 1142 arch_spin_unlock(&lockdep_lock); 1143 local_irq_restore(flags); 1144 1145 return ret; 1146} 1147 1148unsigned long __lockdep_count_backward_deps(struct lock_list *this) 1149{ 1150 unsigned long count = 0; 1151 struct lock_list *uninitialized_var(target_entry); 1152 1153 __bfs_backwards(this, (void *)&count, noop_count, &target_entry); 1154 1155 return count; 1156} 1157 1158unsigned long lockdep_count_backward_deps(struct lock_class *class) 1159{ 1160 unsigned long ret, flags; 1161 struct lock_list this; 1162 1163 this.parent = NULL; 1164 this.class = class; 1165 1166 local_irq_save(flags); 1167 arch_spin_lock(&lockdep_lock); 1168 ret = __lockdep_count_backward_deps(&this); 1169 arch_spin_unlock(&lockdep_lock); 1170 local_irq_restore(flags); 1171 1172 return ret; 1173} 1174 1175/* 1176 * Prove that the dependency graph starting at <entry> can not 1177 * lead to <target>. Print an error and return 0 if it does. 1178 */ 1179static noinline int 1180check_noncircular(struct lock_list *root, struct lock_class *target, 1181 struct lock_list **target_entry) 1182{ 1183 int result; 1184 1185 debug_atomic_inc(nr_cyclic_checks); 1186 1187 result = __bfs_forwards(root, target, class_equal, target_entry); 1188 1189 return result; 1190} 1191 1192#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) 1193/* 1194 * Forwards and backwards subgraph searching, for the purposes of 1195 * proving that two subgraphs can be connected by a new dependency 1196 * without creating any illegal irq-safe -> irq-unsafe lock dependency. 1197 */ 1198 1199static inline int usage_match(struct lock_list *entry, void *bit) 1200{ 1201 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit); 1202} 1203 1204 1205 1206/* 1207 * Find a node in the forwards-direction dependency sub-graph starting 1208 * at @root->class that matches @bit. 1209 * 1210 * Return 0 if such a node exists in the subgraph, and put that node 1211 * into *@target_entry. 1212 * 1213 * Return 1 otherwise and keep *@target_entry unchanged. 1214 * Return <0 on error. 1215 */ 1216static int 1217find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit, 1218 struct lock_list **target_entry) 1219{ 1220 int result; 1221 1222 debug_atomic_inc(nr_find_usage_forwards_checks); 1223 1224 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry); 1225 1226 return result; 1227} 1228 1229/* 1230 * Find a node in the backwards-direction dependency sub-graph starting 1231 * at @root->class that matches @bit. 1232 * 1233 * Return 0 if such a node exists in the subgraph, and put that node 1234 * into *@target_entry. 1235 * 1236 * Return 1 otherwise and keep *@target_entry unchanged. 1237 * Return <0 on error. 1238 */ 1239static int 1240find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit, 1241 struct lock_list **target_entry) 1242{ 1243 int result; 1244 1245 debug_atomic_inc(nr_find_usage_backwards_checks); 1246 1247 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry); 1248 1249 return result; 1250} 1251 1252static void print_lock_class_header(struct lock_class *class, int depth) 1253{ 1254 int bit; 1255 1256 printk("%*s->", depth, ""); 1257 print_lock_name(class); 1258 printk(" ops: %lu", class->ops); 1259 printk(" {\n"); 1260 1261 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) { 1262 if (class->usage_mask & (1 << bit)) { 1263 int len = depth; 1264 1265 len += printk("%*s %s", depth, "", usage_str[bit]); 1266 len += printk(" at:\n"); 1267 print_stack_trace(class->usage_traces + bit, len); 1268 } 1269 } 1270 printk("%*s }\n", depth, ""); 1271 1272 printk("%*s ... key at: ",depth,""); 1273 print_ip_sym((unsigned long)class->key); 1274} 1275 1276/* 1277 * printk the shortest lock dependencies from @start to @end in reverse order: 1278 */ 1279static void __used 1280print_shortest_lock_dependencies(struct lock_list *leaf, 1281 struct lock_list *root) 1282{ 1283 struct lock_list *entry = leaf; 1284 int depth; 1285 1286 /*compute depth from generated tree by BFS*/ 1287 depth = get_lock_depth(leaf); 1288 1289 do { 1290 print_lock_class_header(entry->class, depth); 1291 printk("%*s ... acquired at:\n", depth, ""); 1292 print_stack_trace(&entry->trace, 2); 1293 printk("\n"); 1294 1295 if (depth == 0 && (entry != root)) { 1296 printk("lockdep:%s bad BFS generated tree\n", __func__); 1297 break; 1298 } 1299 1300 entry = get_lock_parent(entry); 1301 depth--; 1302 } while (entry && (depth >= 0)); 1303 1304 return; 1305} 1306 1307static int 1308print_bad_irq_dependency(struct task_struct *curr, 1309 struct lock_list *prev_root, 1310 struct lock_list *next_root, 1311 struct lock_list *backwards_entry, 1312 struct lock_list *forwards_entry, 1313 struct held_lock *prev, 1314 struct held_lock *next, 1315 enum lock_usage_bit bit1, 1316 enum lock_usage_bit bit2, 1317 const char *irqclass) 1318{ 1319 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1320 return 0; 1321 1322 printk("\n======================================================\n"); 1323 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n", 1324 irqclass, irqclass); 1325 print_kernel_version(); 1326 printk( "------------------------------------------------------\n"); 1327 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n", 1328 curr->comm, task_pid_nr(curr), 1329 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT, 1330 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, 1331 curr->hardirqs_enabled, 1332 curr->softirqs_enabled); 1333 print_lock(next); 1334 1335 printk("\nand this task is already holding:\n"); 1336 print_lock(prev); 1337 printk("which would create a new lock dependency:\n"); 1338 print_lock_name(hlock_class(prev)); 1339 printk(" ->"); 1340 print_lock_name(hlock_class(next)); 1341 printk("\n"); 1342 1343 printk("\nbut this new dependency connects a %s-irq-safe lock:\n", 1344 irqclass); 1345 print_lock_name(backwards_entry->class); 1346 printk("\n... which became %s-irq-safe at:\n", irqclass); 1347 1348 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1); 1349 1350 printk("\nto a %s-irq-unsafe lock:\n", irqclass); 1351 print_lock_name(forwards_entry->class); 1352 printk("\n... which became %s-irq-unsafe at:\n", irqclass); 1353 printk("..."); 1354 1355 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1); 1356 1357 printk("\nother info that might help us debug this:\n\n"); 1358 lockdep_print_held_locks(curr); 1359 1360 printk("\nthe dependencies between %s-irq-safe lock", irqclass); 1361 printk(" and the holding lock:\n"); 1362 if (!save_trace(&prev_root->trace)) 1363 return 0; 1364 print_shortest_lock_dependencies(backwards_entry, prev_root); 1365 1366 printk("\nthe dependencies between the lock to be acquired"); 1367 printk(" and %s-irq-unsafe lock:\n", irqclass); 1368 if (!save_trace(&next_root->trace)) 1369 return 0; 1370 print_shortest_lock_dependencies(forwards_entry, next_root); 1371 1372 printk("\nstack backtrace:\n"); 1373 dump_stack(); 1374 1375 return 0; 1376} 1377 1378static int 1379check_usage(struct task_struct *curr, struct held_lock *prev, 1380 struct held_lock *next, enum lock_usage_bit bit_backwards, 1381 enum lock_usage_bit bit_forwards, const char *irqclass) 1382{ 1383 int ret; 1384 struct lock_list this, that; 1385 struct lock_list *uninitialized_var(target_entry); 1386 struct lock_list *uninitialized_var(target_entry1); 1387 1388 this.parent = NULL; 1389 1390 this.class = hlock_class(prev); 1391 ret = find_usage_backwards(&this, bit_backwards, &target_entry); 1392 if (ret < 0) 1393 return print_bfs_bug(ret); 1394 if (ret == 1) 1395 return ret; 1396 1397 that.parent = NULL; 1398 that.class = hlock_class(next); 1399 ret = find_usage_forwards(&that, bit_forwards, &target_entry1); 1400 if (ret < 0) 1401 return print_bfs_bug(ret); 1402 if (ret == 1) 1403 return ret; 1404 1405 return print_bad_irq_dependency(curr, &this, &that, 1406 target_entry, target_entry1, 1407 prev, next, 1408 bit_backwards, bit_forwards, irqclass); 1409} 1410 1411static const char *state_names[] = { 1412#define LOCKDEP_STATE(__STATE) \ 1413 __stringify(__STATE), 1414#include "lockdep_states.h" 1415#undef LOCKDEP_STATE 1416}; 1417 1418static const char *state_rnames[] = { 1419#define LOCKDEP_STATE(__STATE) \ 1420 __stringify(__STATE)"-READ", 1421#include "lockdep_states.h" 1422#undef LOCKDEP_STATE 1423}; 1424 1425static inline const char *state_name(enum lock_usage_bit bit) 1426{ 1427 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2]; 1428} 1429 1430static int exclusive_bit(int new_bit) 1431{ 1432 /* 1433 * USED_IN 1434 * USED_IN_READ 1435 * ENABLED 1436 * ENABLED_READ 1437 * 1438 * bit 0 - write/read 1439 * bit 1 - used_in/enabled 1440 * bit 2+ state 1441 */ 1442 1443 int state = new_bit & ~3; 1444 int dir = new_bit & 2; 1445 1446 /* 1447 * keep state, bit flip the direction and strip read. 1448 */ 1449 return state | (dir ^ 2); 1450} 1451 1452static int check_irq_usage(struct task_struct *curr, struct held_lock *prev, 1453 struct held_lock *next, enum lock_usage_bit bit) 1454{ 1455 /* 1456 * Prove that the new dependency does not connect a hardirq-safe 1457 * lock with a hardirq-unsafe lock - to achieve this we search 1458 * the backwards-subgraph starting at <prev>, and the 1459 * forwards-subgraph starting at <next>: 1460 */ 1461 if (!check_usage(curr, prev, next, bit, 1462 exclusive_bit(bit), state_name(bit))) 1463 return 0; 1464 1465 bit++; /* _READ */ 1466 1467 /* 1468 * Prove that the new dependency does not connect a hardirq-safe-read 1469 * lock with a hardirq-unsafe lock - to achieve this we search 1470 * the backwards-subgraph starting at <prev>, and the 1471 * forwards-subgraph starting at <next>: 1472 */ 1473 if (!check_usage(curr, prev, next, bit, 1474 exclusive_bit(bit), state_name(bit))) 1475 return 0; 1476 1477 return 1; 1478} 1479 1480static int 1481check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, 1482 struct held_lock *next) 1483{ 1484#define LOCKDEP_STATE(__STATE) \ 1485 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \ 1486 return 0; 1487#include "lockdep_states.h" 1488#undef LOCKDEP_STATE 1489 1490 return 1; 1491} 1492 1493static void inc_chains(void) 1494{ 1495 if (current->hardirq_context) 1496 nr_hardirq_chains++; 1497 else { 1498 if (current->softirq_context) 1499 nr_softirq_chains++; 1500 else 1501 nr_process_chains++; 1502 } 1503} 1504 1505#else 1506 1507static inline int 1508check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, 1509 struct held_lock *next) 1510{ 1511 return 1; 1512} 1513 1514static inline void inc_chains(void) 1515{ 1516 nr_process_chains++; 1517} 1518 1519#endif 1520 1521static int 1522print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, 1523 struct held_lock *next) 1524{ 1525 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1526 return 0; 1527 1528 printk("\n=============================================\n"); 1529 printk( "[ INFO: possible recursive locking detected ]\n"); 1530 print_kernel_version(); 1531 printk( "---------------------------------------------\n"); 1532 printk("%s/%d is trying to acquire lock:\n", 1533 curr->comm, task_pid_nr(curr)); 1534 print_lock(next); 1535 printk("\nbut task is already holding lock:\n"); 1536 print_lock(prev); 1537 1538 printk("\nother info that might help us debug this:\n"); 1539 lockdep_print_held_locks(curr); 1540 1541 printk("\nstack backtrace:\n"); 1542 dump_stack(); 1543 1544 return 0; 1545} 1546 1547/* 1548 * Check whether we are holding such a class already. 1549 * 1550 * (Note that this has to be done separately, because the graph cannot 1551 * detect such classes of deadlocks.) 1552 * 1553 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read 1554 */ 1555static int 1556check_deadlock(struct task_struct *curr, struct held_lock *next, 1557 struct lockdep_map *next_instance, int read) 1558{ 1559 struct held_lock *prev; 1560 struct held_lock *nest = NULL; 1561 int i; 1562 1563 for (i = 0; i < curr->lockdep_depth; i++) { 1564 prev = curr->held_locks + i; 1565 1566 if (prev->instance == next->nest_lock) 1567 nest = prev; 1568 1569 if (hlock_class(prev) != hlock_class(next)) 1570 continue; 1571 1572 /* 1573 * Allow read-after-read recursion of the same 1574 * lock class (i.e. read_lock(lock)+read_lock(lock)): 1575 */ 1576 if ((read == 2) && prev->read) 1577 return 2; 1578 1579 /* 1580 * We're holding the nest_lock, which serializes this lock's 1581 * nesting behaviour. 1582 */ 1583 if (nest) 1584 return 2; 1585 1586 return print_deadlock_bug(curr, prev, next); 1587 } 1588 return 1; 1589} 1590 1591/* 1592 * There was a chain-cache miss, and we are about to add a new dependency 1593 * to a previous lock. We recursively validate the following rules: 1594 * 1595 * - would the adding of the <prev> -> <next> dependency create a 1596 * circular dependency in the graph? [== circular deadlock] 1597 * 1598 * - does the new prev->next dependency connect any hardirq-safe lock 1599 * (in the full backwards-subgraph starting at <prev>) with any 1600 * hardirq-unsafe lock (in the full forwards-subgraph starting at 1601 * <next>)? [== illegal lock inversion with hardirq contexts] 1602 * 1603 * - does the new prev->next dependency connect any softirq-safe lock 1604 * (in the full backwards-subgraph starting at <prev>) with any 1605 * softirq-unsafe lock (in the full forwards-subgraph starting at 1606 * <next>)? [== illegal lock inversion with softirq contexts] 1607 * 1608 * any of these scenarios could lead to a deadlock. 1609 * 1610 * Then if all the validations pass, we add the forwards and backwards 1611 * dependency. 1612 */ 1613static int 1614check_prev_add(struct task_struct *curr, struct held_lock *prev, 1615 struct held_lock *next, int distance, int trylock_loop) 1616{ 1617 struct lock_list *entry; 1618 int ret; 1619 struct lock_list this; 1620 struct lock_list *uninitialized_var(target_entry); 1621 /* 1622 * Static variable, serialized by the graph_lock(). 1623 * 1624 * We use this static variable to save the stack trace in case 1625 * we call into this function multiple times due to encountering 1626 * trylocks in the held lock stack. 1627 */ 1628 static struct stack_trace trace; 1629 1630 /* 1631 * Prove that the new <prev> -> <next> dependency would not 1632 * create a circular dependency in the graph. (We do this by 1633 * forward-recursing into the graph starting at <next>, and 1634 * checking whether we can reach <prev>.) 1635 * 1636 * We are using global variables to control the recursion, to 1637 * keep the stackframe size of the recursive functions low: 1638 */ 1639 this.class = hlock_class(next); 1640 this.parent = NULL; 1641 ret = check_noncircular(&this, hlock_class(prev), &target_entry); 1642 if (unlikely(!ret)) 1643 return print_circular_bug(&this, target_entry, next, prev); 1644 else if (unlikely(ret < 0)) 1645 return print_bfs_bug(ret); 1646 1647 if (!check_prev_add_irq(curr, prev, next)) 1648 return 0; 1649 1650 /* 1651 * For recursive read-locks we do all the dependency checks, 1652 * but we dont store read-triggered dependencies (only 1653 * write-triggered dependencies). This ensures that only the 1654 * write-side dependencies matter, and that if for example a 1655 * write-lock never takes any other locks, then the reads are 1656 * equivalent to a NOP. 1657 */ 1658 if (next->read == 2 || prev->read == 2) 1659 return 1; 1660 /* 1661 * Is the <prev> -> <next> dependency already present? 1662 * 1663 * (this may occur even though this is a new chain: consider 1664 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 1665 * chains - the second one will be new, but L1 already has 1666 * L2 added to its dependency list, due to the first chain.) 1667 */ 1668 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { 1669 if (entry->class == hlock_class(next)) { 1670 if (distance == 1) 1671 entry->distance = 1; 1672 return 2; 1673 } 1674 } 1675 1676 if (!trylock_loop && !save_trace(&trace)) 1677 return 0; 1678 1679 /* 1680 * Ok, all validations passed, add the new lock 1681 * to the previous lock's dependency list: 1682 */ 1683 ret = add_lock_to_list(hlock_class(prev), hlock_class(next), 1684 &hlock_class(prev)->locks_after, 1685 next->acquire_ip, distance, &trace); 1686 1687 if (!ret) 1688 return 0; 1689 1690 ret = add_lock_to_list(hlock_class(next), hlock_class(prev), 1691 &hlock_class(next)->locks_before, 1692 next->acquire_ip, distance, &trace); 1693 if (!ret) 1694 return 0; 1695 1696 /* 1697 * Debugging printouts: 1698 */ 1699 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) { 1700 graph_unlock(); 1701 printk("\n new dependency: "); 1702 print_lock_name(hlock_class(prev)); 1703 printk(" => "); 1704 print_lock_name(hlock_class(next)); 1705 printk("\n"); 1706 dump_stack(); 1707 return graph_lock(); 1708 } 1709 return 1; 1710} 1711 1712/* 1713 * Add the dependency to all directly-previous locks that are 'relevant'. 1714 * The ones that are relevant are (in increasing distance from curr): 1715 * all consecutive trylock entries and the final non-trylock entry - or 1716 * the end of this context's lock-chain - whichever comes first. 1717 */ 1718static int 1719check_prevs_add(struct task_struct *curr, struct held_lock *next) 1720{ 1721 int depth = curr->lockdep_depth; 1722 int trylock_loop = 0; 1723 struct held_lock *hlock; 1724 1725 /* 1726 * Debugging checks. 1727 * 1728 * Depth must not be zero for a non-head lock: 1729 */ 1730 if (!depth) 1731 goto out_bug; 1732 /* 1733 * At least two relevant locks must exist for this 1734 * to be a head: 1735 */ 1736 if (curr->held_locks[depth].irq_context != 1737 curr->held_locks[depth-1].irq_context) 1738 goto out_bug; 1739 1740 for (;;) { 1741 int distance = curr->lockdep_depth - depth + 1; 1742 hlock = curr->held_locks + depth-1; 1743 /* 1744 * Only non-recursive-read entries get new dependencies 1745 * added: 1746 */ 1747 if (hlock->read != 2) { 1748 if (!check_prev_add(curr, hlock, next, 1749 distance, trylock_loop)) 1750 return 0; 1751 /* 1752 * Stop after the first non-trylock entry, 1753 * as non-trylock entries have added their 1754 * own direct dependencies already, so this 1755 * lock is connected to them indirectly: 1756 */ 1757 if (!hlock->trylock) 1758 break; 1759 } 1760 depth--; 1761 /* 1762 * End of lock-stack? 1763 */ 1764 if (!depth) 1765 break; 1766 /* 1767 * Stop the search if we cross into another context: 1768 */ 1769 if (curr->held_locks[depth].irq_context != 1770 curr->held_locks[depth-1].irq_context) 1771 break; 1772 trylock_loop = 1; 1773 } 1774 return 1; 1775out_bug: 1776 if (!debug_locks_off_graph_unlock()) 1777 return 0; 1778 1779 WARN_ON(1); 1780 1781 return 0; 1782} 1783 1784unsigned long nr_lock_chains; 1785struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; 1786int nr_chain_hlocks; 1787static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; 1788 1789struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i) 1790{ 1791 return lock_classes + chain_hlocks[chain->base + i]; 1792} 1793 1794/* 1795 * Look up a dependency chain. If the key is not present yet then 1796 * add it and return 1 - in this case the new dependency chain is 1797 * validated. If the key is already hashed, return 0. 1798 * (On return with 1 graph_lock is held.) 1799 */ 1800static inline int lookup_chain_cache(struct task_struct *curr, 1801 struct held_lock *hlock, 1802 u64 chain_key) 1803{ 1804 struct lock_class *class = hlock_class(hlock); 1805 struct list_head *hash_head = chainhashentry(chain_key); 1806 struct lock_chain *chain; 1807 struct held_lock *hlock_curr, *hlock_next; 1808 int i, j, n, cn; 1809 1810 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 1811 return 0; 1812 /* 1813 * We can walk it lock-free, because entries only get added 1814 * to the hash: 1815 */ 1816 list_for_each_entry(chain, hash_head, entry) { 1817 if (chain->chain_key == chain_key) { 1818cache_hit: 1819 debug_atomic_inc(chain_lookup_hits); 1820 if (very_verbose(class)) 1821 printk("\nhash chain already cached, key: " 1822 "%016Lx tail class: [%p] %s\n", 1823 (unsigned long long)chain_key, 1824 class->key, class->name); 1825 return 0; 1826 } 1827 } 1828 if (very_verbose(class)) 1829 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n", 1830 (unsigned long long)chain_key, class->key, class->name); 1831 /* 1832 * Allocate a new chain entry from the static array, and add 1833 * it to the hash: 1834 */ 1835 if (!graph_lock()) 1836 return 0; 1837 /* 1838 * We have to walk the chain again locked - to avoid duplicates: 1839 */ 1840 list_for_each_entry(chain, hash_head, entry) { 1841 if (chain->chain_key == chain_key) { 1842 graph_unlock(); 1843 goto cache_hit; 1844 } 1845 } 1846 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) { 1847 if (!debug_locks_off_graph_unlock()) 1848 return 0; 1849 1850 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n"); 1851 printk("turning off the locking correctness validator.\n"); 1852 dump_stack(); 1853 return 0; 1854 } 1855 chain = lock_chains + nr_lock_chains++; 1856 chain->chain_key = chain_key; 1857 chain->irq_context = hlock->irq_context; 1858 /* Find the first held_lock of current chain */ 1859 hlock_next = hlock; 1860 for (i = curr->lockdep_depth - 1; i >= 0; i--) { 1861 hlock_curr = curr->held_locks + i; 1862 if (hlock_curr->irq_context != hlock_next->irq_context) 1863 break; 1864 hlock_next = hlock; 1865 } 1866 i++; 1867 chain->depth = curr->lockdep_depth + 1 - i; 1868 cn = nr_chain_hlocks; 1869 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) { 1870 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth); 1871 if (n == cn) 1872 break; 1873 cn = n; 1874 } 1875 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) { 1876 chain->base = cn; 1877 for (j = 0; j < chain->depth - 1; j++, i++) { 1878 int lock_id = curr->held_locks[i].class_idx - 1; 1879 chain_hlocks[chain->base + j] = lock_id; 1880 } 1881 chain_hlocks[chain->base + j] = class - lock_classes; 1882 } 1883 list_add_tail_rcu(&chain->entry, hash_head); 1884 debug_atomic_inc(chain_lookup_misses); 1885 inc_chains(); 1886 1887 return 1; 1888} 1889 1890static int validate_chain(struct task_struct *curr, struct lockdep_map *lock, 1891 struct held_lock *hlock, int chain_head, u64 chain_key) 1892{ 1893 /* 1894 * Trylock needs to maintain the stack of held locks, but it 1895 * does not add new dependencies, because trylock can be done 1896 * in any order. 1897 * 1898 * We look up the chain_key and do the O(N^2) check and update of 1899 * the dependencies only if this is a new dependency chain. 1900 * (If lookup_chain_cache() returns with 1 it acquires 1901 * graph_lock for us) 1902 */ 1903 if (!hlock->trylock && (hlock->check == 2) && 1904 lookup_chain_cache(curr, hlock, chain_key)) { 1905 /* 1906 * Check whether last held lock: 1907 * 1908 * - is irq-safe, if this lock is irq-unsafe 1909 * - is softirq-safe, if this lock is hardirq-unsafe 1910 * 1911 * And check whether the new lock's dependency graph 1912 * could lead back to the previous lock. 1913 * 1914 * any of these scenarios could lead to a deadlock. If 1915 * All validations 1916 */ 1917 int ret = check_deadlock(curr, hlock, lock, hlock->read); 1918 1919 if (!ret) 1920 return 0; 1921 /* 1922 * Mark recursive read, as we jump over it when 1923 * building dependencies (just like we jump over 1924 * trylock entries): 1925 */ 1926 if (ret == 2) 1927 hlock->read = 2; 1928 /* 1929 * Add dependency only if this lock is not the head 1930 * of the chain, and if it's not a secondary read-lock: 1931 */ 1932 if (!chain_head && ret != 2) 1933 if (!check_prevs_add(curr, hlock)) 1934 return 0; 1935 graph_unlock(); 1936 } else 1937 /* after lookup_chain_cache(): */ 1938 if (unlikely(!debug_locks)) 1939 return 0; 1940 1941 return 1; 1942} 1943#else 1944static inline int validate_chain(struct task_struct *curr, 1945 struct lockdep_map *lock, struct held_lock *hlock, 1946 int chain_head, u64 chain_key) 1947{ 1948 return 1; 1949} 1950#endif 1951 1952/* 1953 * We are building curr_chain_key incrementally, so double-check 1954 * it from scratch, to make sure that it's done correctly: 1955 */ 1956static void check_chain_key(struct task_struct *curr) 1957{ 1958#ifdef CONFIG_DEBUG_LOCKDEP 1959 struct held_lock *hlock, *prev_hlock = NULL; 1960 unsigned int i, id; 1961 u64 chain_key = 0; 1962 1963 for (i = 0; i < curr->lockdep_depth; i++) { 1964 hlock = curr->held_locks + i; 1965 if (chain_key != hlock->prev_chain_key) { 1966 debug_locks_off(); 1967 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", 1968 curr->lockdep_depth, i, 1969 (unsigned long long)chain_key, 1970 (unsigned long long)hlock->prev_chain_key); 1971 return; 1972 } 1973 id = hlock->class_idx - 1; 1974 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS)) 1975 return; 1976 1977 if (prev_hlock && (prev_hlock->irq_context != 1978 hlock->irq_context)) 1979 chain_key = 0; 1980 chain_key = iterate_chain_key(chain_key, id); 1981 prev_hlock = hlock; 1982 } 1983 if (chain_key != curr->curr_chain_key) { 1984 debug_locks_off(); 1985 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", 1986 curr->lockdep_depth, i, 1987 (unsigned long long)chain_key, 1988 (unsigned long long)curr->curr_chain_key); 1989 } 1990#endif 1991} 1992 1993static int 1994print_usage_bug(struct task_struct *curr, struct held_lock *this, 1995 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit) 1996{ 1997 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 1998 return 0; 1999 2000 printk("\n=================================\n"); 2001 printk( "[ INFO: inconsistent lock state ]\n"); 2002 print_kernel_version(); 2003 printk( "---------------------------------\n"); 2004 2005 printk("inconsistent {%s} -> {%s} usage.\n", 2006 usage_str[prev_bit], usage_str[new_bit]); 2007 2008 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n", 2009 curr->comm, task_pid_nr(curr), 2010 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT, 2011 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, 2012 trace_hardirqs_enabled(curr), 2013 trace_softirqs_enabled(curr)); 2014 print_lock(this); 2015 2016 printk("{%s} state was registered at:\n", usage_str[prev_bit]); 2017 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1); 2018 2019 print_irqtrace_events(curr); 2020 printk("\nother info that might help us debug this:\n"); 2021 lockdep_print_held_locks(curr); 2022 2023 printk("\nstack backtrace:\n"); 2024 dump_stack(); 2025 2026 return 0; 2027} 2028 2029/* 2030 * Print out an error if an invalid bit is set: 2031 */ 2032static inline int 2033valid_state(struct task_struct *curr, struct held_lock *this, 2034 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) 2035{ 2036 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) 2037 return print_usage_bug(curr, this, bad_bit, new_bit); 2038 return 1; 2039} 2040 2041static int mark_lock(struct task_struct *curr, struct held_lock *this, 2042 enum lock_usage_bit new_bit); 2043 2044#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) 2045 2046/* 2047 * print irq inversion bug: 2048 */ 2049static int 2050print_irq_inversion_bug(struct task_struct *curr, 2051 struct lock_list *root, struct lock_list *other, 2052 struct held_lock *this, int forwards, 2053 const char *irqclass) 2054{ 2055 if (!debug_locks_off_graph_unlock() || debug_locks_silent) 2056 return 0; 2057 2058 printk("\n=========================================================\n"); 2059 printk( "[ INFO: possible irq lock inversion dependency detected ]\n"); 2060 print_kernel_version(); 2061 printk( "---------------------------------------------------------\n"); 2062 printk("%s/%d just changed the state of lock:\n", 2063 curr->comm, task_pid_nr(curr)); 2064 print_lock(this); 2065 if (forwards) 2066 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass); 2067 else 2068 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); 2069 print_lock_name(other->class); 2070 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n"); 2071 2072 printk("\nother info that might help us debug this:\n"); 2073 lockdep_print_held_locks(curr); 2074 2075 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); 2076 if (!save_trace(&root->trace)) 2077 return 0; 2078 print_shortest_lock_dependencies(other, root); 2079 2080 printk("\nstack backtrace:\n"); 2081 dump_stack(); 2082 2083 return 0; 2084} 2085 2086/* 2087 * Prove that in the forwards-direction subgraph starting at <this> 2088 * there is no lock matching <mask>: 2089 */ 2090static int 2091check_usage_forwards(struct task_struct *curr, struct held_lock *this, 2092 enum lock_usage_bit bit, const char *irqclass) 2093{ 2094 int ret; 2095 struct lock_list root; 2096 struct lock_list *uninitialized_var(target_entry); 2097 2098 root.parent = NULL; 2099 root.class = hlock_class(this); 2100 ret = find_usage_forwards(&root, bit, &target_entry); 2101 if (ret < 0) 2102 return print_bfs_bug(ret); 2103 if (ret == 1) 2104 return ret; 2105 2106 return print_irq_inversion_bug(curr, &root, target_entry, 2107 this, 1, irqclass); 2108} 2109 2110/* 2111 * Prove that in the backwards-direction subgraph starting at <this> 2112 * there is no lock matching <mask>: 2113 */ 2114static int 2115check_usage_backwards(struct task_struct *curr, struct held_lock *this, 2116 enum lock_usage_bit bit, const char *irqclass) 2117{ 2118 int ret; 2119 struct lock_list root; 2120 struct lock_list *uninitialized_var(target_entry); 2121 2122 root.parent = NULL; 2123 root.class = hlock_class(this); 2124 ret = find_usage_backwards(&root, bit, &target_entry); 2125 if (ret < 0) 2126 return print_bfs_bug(ret); 2127 if (ret == 1) 2128 return ret; 2129 2130 return print_irq_inversion_bug(curr, &root, target_entry, 2131 this, 0, irqclass); 2132} 2133 2134void print_irqtrace_events(struct task_struct *curr) 2135{ 2136 printk("irq event stamp: %u\n", curr->irq_events); 2137 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event); 2138 print_ip_sym(curr->hardirq_enable_ip); 2139 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event); 2140 print_ip_sym(curr->hardirq_disable_ip); 2141 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event); 2142 print_ip_sym(curr->softirq_enable_ip); 2143 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event); 2144 print_ip_sym(curr->softirq_disable_ip); 2145} 2146 2147static int HARDIRQ_verbose(struct lock_class *class) 2148{ 2149#if HARDIRQ_VERBOSE 2150 return class_filter(class); 2151#endif 2152 return 0; 2153} 2154 2155static int SOFTIRQ_verbose(struct lock_class *class) 2156{ 2157#if SOFTIRQ_VERBOSE 2158 return class_filter(class); 2159#endif 2160 return 0; 2161} 2162 2163static int RECLAIM_FS_verbose(struct lock_class *class) 2164{ 2165#if RECLAIM_VERBOSE 2166 return class_filter(class); 2167#endif 2168 return 0; 2169} 2170 2171#define STRICT_READ_CHECKS 1 2172 2173static int (*state_verbose_f[])(struct lock_class *class) = { 2174#define LOCKDEP_STATE(__STATE) \ 2175 __STATE##_verbose, 2176#include "lockdep_states.h" 2177#undef LOCKDEP_STATE 2178}; 2179 2180static inline int state_verbose(enum lock_usage_bit bit, 2181 struct lock_class *class) 2182{ 2183 return state_verbose_f[bit >> 2](class); 2184} 2185 2186typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, 2187 enum lock_usage_bit bit, const char *name); 2188 2189static int 2190mark_lock_irq(struct task_struct *curr, struct held_lock *this, 2191 enum lock_usage_bit new_bit) 2192{ 2193 int excl_bit = exclusive_bit(new_bit); 2194 int read = new_bit & 1; 2195 int dir = new_bit & 2; 2196 2197 /* 2198 * mark USED_IN has to look forwards -- to ensure no dependency 2199 * has ENABLED state, which would allow recursion deadlocks. 2200 * 2201 * mark ENABLED has to look backwards -- to ensure no dependee 2202 * has USED_IN state, which, again, would allow recursion deadlocks. 2203 */ 2204 check_usage_f usage = dir ? 2205 check_usage_backwards : check_usage_forwards; 2206 2207 /* 2208 * Validate that this particular lock does not have conflicting 2209 * usage states. 2210 */ 2211 if (!valid_state(curr, this, new_bit, excl_bit)) 2212 return 0; 2213 2214 /* 2215 * Validate that the lock dependencies don't have conflicting usage 2216 * states. 2217 */ 2218 if ((!read || !dir || STRICT_READ_CHECKS) && 2219 !usage(curr, this, excl_bit, state_name(new_bit & ~1))) 2220 return 0; 2221 2222 /* 2223 * Check for read in write conflicts 2224 */ 2225 if (!read) { 2226 if (!valid_state(curr, this, new_bit, excl_bit + 1)) 2227 return 0; 2228 2229 if (STRICT_READ_CHECKS && 2230 !usage(curr, this, excl_bit + 1, 2231 state_name(new_bit + 1))) 2232 return 0; 2233 } 2234 2235 if (state_verbose(new_bit, hlock_class(this))) 2236 return 2; 2237 2238 return 1; 2239} 2240 2241enum mark_type { 2242#define LOCKDEP_STATE(__STATE) __STATE, 2243#include "lockdep_states.h" 2244#undef LOCKDEP_STATE 2245}; 2246 2247/* 2248 * Mark all held locks with a usage bit: 2249 */ 2250static int 2251mark_held_locks(struct task_struct *curr, enum mark_type mark) 2252{ 2253 enum lock_usage_bit usage_bit; 2254 struct held_lock *hlock; 2255 int i; 2256 2257 for (i = 0; i < curr->lockdep_depth; i++) { 2258 hlock = curr->held_locks + i; 2259 2260 usage_bit = 2 + (mark << 2); /* ENABLED */ 2261 if (hlock->read) 2262 usage_bit += 1; /* READ */ 2263 2264 BUG_ON(usage_bit >= LOCK_USAGE_STATES); 2265 2266 if (!mark_lock(curr, hlock, usage_bit)) 2267 return 0; 2268 } 2269 2270 return 1; 2271} 2272 2273/* 2274 * Debugging helper: via this flag we know that we are in 2275 * 'early bootup code', and will warn about any invalid irqs-on event: 2276 */ 2277static int early_boot_irqs_enabled; 2278 2279void early_boot_irqs_off(void) 2280{ 2281 early_boot_irqs_enabled = 0; 2282} 2283 2284void early_boot_irqs_on(void) 2285{ 2286 early_boot_irqs_enabled = 1; 2287} 2288 2289/* 2290 * Hardirqs will be enabled: 2291 */ 2292void trace_hardirqs_on_caller(unsigned long ip) 2293{ 2294 struct task_struct *curr = current; 2295 2296 time_hardirqs_on(CALLER_ADDR0, ip); 2297 2298 if (unlikely(!debug_locks || current->lockdep_recursion)) 2299 return; 2300 2301 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled))) 2302 return; 2303 2304 if (unlikely(curr->hardirqs_enabled)) { 2305 /* 2306 * Neither irq nor preemption are disabled here 2307 * so this is racy by nature but loosing one hit 2308 * in a stat is not a big deal. 2309 */ 2310 __debug_atomic_inc(redundant_hardirqs_on); 2311 return; 2312 } 2313 /* we'll do an OFF -> ON transition: */ 2314 curr->hardirqs_enabled = 1; 2315 2316 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2317 return; 2318 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context)) 2319 return; 2320 /* 2321 * We are going to turn hardirqs on, so set the 2322 * usage bit for all held locks: 2323 */ 2324 if (!mark_held_locks(curr, HARDIRQ)) 2325 return; 2326 /* 2327 * If we have softirqs enabled, then set the usage 2328 * bit for all held locks. (disabled hardirqs prevented 2329 * this bit from being set before) 2330 */ 2331 if (curr->softirqs_enabled) 2332 if (!mark_held_locks(curr, SOFTIRQ)) 2333 return; 2334 2335 curr->hardirq_enable_ip = ip; 2336 curr->hardirq_enable_event = ++curr->irq_events; 2337 debug_atomic_inc(hardirqs_on_events); 2338} 2339EXPORT_SYMBOL(trace_hardirqs_on_caller); 2340 2341void trace_hardirqs_on(void) 2342{ 2343 trace_hardirqs_on_caller(CALLER_ADDR0); 2344} 2345EXPORT_SYMBOL(trace_hardirqs_on); 2346 2347/* 2348 * Hardirqs were disabled: 2349 */ 2350void trace_hardirqs_off_caller(unsigned long ip) 2351{ 2352 struct task_struct *curr = current; 2353 2354 time_hardirqs_off(CALLER_ADDR0, ip); 2355 2356 if (unlikely(!debug_locks || current->lockdep_recursion)) 2357 return; 2358 2359 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2360 return; 2361 2362 if (curr->hardirqs_enabled) { 2363 /* 2364 * We have done an ON -> OFF transition: 2365 */ 2366 curr->hardirqs_enabled = 0; 2367 curr->hardirq_disable_ip = ip; 2368 curr->hardirq_disable_event = ++curr->irq_events; 2369 debug_atomic_inc(hardirqs_off_events); 2370 } else 2371 debug_atomic_inc(redundant_hardirqs_off); 2372} 2373EXPORT_SYMBOL(trace_hardirqs_off_caller); 2374 2375void trace_hardirqs_off(void) 2376{ 2377 trace_hardirqs_off_caller(CALLER_ADDR0); 2378} 2379EXPORT_SYMBOL(trace_hardirqs_off); 2380 2381/* 2382 * Softirqs will be enabled: 2383 */ 2384void trace_softirqs_on(unsigned long ip) 2385{ 2386 struct task_struct *curr = current; 2387 2388 if (unlikely(!debug_locks)) 2389 return; 2390 2391 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2392 return; 2393 2394 if (curr->softirqs_enabled) { 2395 debug_atomic_inc(redundant_softirqs_on); 2396 return; 2397 } 2398 2399 /* 2400 * We'll do an OFF -> ON transition: 2401 */ 2402 curr->softirqs_enabled = 1; 2403 curr->softirq_enable_ip = ip; 2404 curr->softirq_enable_event = ++curr->irq_events; 2405 debug_atomic_inc(softirqs_on_events); 2406 /* 2407 * We are going to turn softirqs on, so set the 2408 * usage bit for all held locks, if hardirqs are 2409 * enabled too: 2410 */ 2411 if (curr->hardirqs_enabled) 2412 mark_held_locks(curr, SOFTIRQ); 2413} 2414 2415/* 2416 * Softirqs were disabled: 2417 */ 2418void trace_softirqs_off(unsigned long ip) 2419{ 2420 struct task_struct *curr = current; 2421 2422 if (unlikely(!debug_locks)) 2423 return; 2424 2425 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2426 return; 2427 2428 if (curr->softirqs_enabled) { 2429 /* 2430 * We have done an ON -> OFF transition: 2431 */ 2432 curr->softirqs_enabled = 0; 2433 curr->softirq_disable_ip = ip; 2434 curr->softirq_disable_event = ++curr->irq_events; 2435 debug_atomic_inc(softirqs_off_events); 2436 DEBUG_LOCKS_WARN_ON(!softirq_count()); 2437 } else 2438 debug_atomic_inc(redundant_softirqs_off); 2439} 2440 2441static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags) 2442{ 2443 struct task_struct *curr = current; 2444 2445 if (unlikely(!debug_locks)) 2446 return; 2447 2448 /* no reclaim without waiting on it */ 2449 if (!(gfp_mask & __GFP_WAIT)) 2450 return; 2451 2452 /* this guy won't enter reclaim */ 2453 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC)) 2454 return; 2455 2456 /* We're only interested __GFP_FS allocations for now */ 2457 if (!(gfp_mask & __GFP_FS)) 2458 return; 2459 2460 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags))) 2461 return; 2462 2463 mark_held_locks(curr, RECLAIM_FS); 2464} 2465 2466static void check_flags(unsigned long flags); 2467 2468void lockdep_trace_alloc(gfp_t gfp_mask) 2469{ 2470 unsigned long flags; 2471 2472 if (unlikely(current->lockdep_recursion)) 2473 return; 2474 2475 raw_local_irq_save(flags); 2476 check_flags(flags); 2477 current->lockdep_recursion = 1; 2478 __lockdep_trace_alloc(gfp_mask, flags); 2479 current->lockdep_recursion = 0; 2480 raw_local_irq_restore(flags); 2481} 2482 2483static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock) 2484{ 2485 /* 2486 * If non-trylock use in a hardirq or softirq context, then 2487 * mark the lock as used in these contexts: 2488 */ 2489 if (!hlock->trylock) { 2490 if (hlock->read) { 2491 if (curr->hardirq_context) 2492 if (!mark_lock(curr, hlock, 2493 LOCK_USED_IN_HARDIRQ_READ)) 2494 return 0; 2495 if (curr->softirq_context) 2496 if (!mark_lock(curr, hlock, 2497 LOCK_USED_IN_SOFTIRQ_READ)) 2498 return 0; 2499 } else { 2500 if (curr->hardirq_context) 2501 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ)) 2502 return 0; 2503 if (curr->softirq_context) 2504 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ)) 2505 return 0; 2506 } 2507 } 2508 if (!hlock->hardirqs_off) { 2509 if (hlock->read) { 2510 if (!mark_lock(curr, hlock, 2511 LOCK_ENABLED_HARDIRQ_READ)) 2512 return 0; 2513 if (curr->softirqs_enabled) 2514 if (!mark_lock(curr, hlock, 2515 LOCK_ENABLED_SOFTIRQ_READ)) 2516 return 0; 2517 } else { 2518 if (!mark_lock(curr, hlock, 2519 LOCK_ENABLED_HARDIRQ)) 2520 return 0; 2521 if (curr->softirqs_enabled) 2522 if (!mark_lock(curr, hlock, 2523 LOCK_ENABLED_SOFTIRQ)) 2524 return 0; 2525 } 2526 } 2527 2528 /* 2529 * We reuse the irq context infrastructure more broadly as a general 2530 * context checking code. This tests GFP_FS recursion (a lock taken 2531 * during reclaim for a GFP_FS allocation is held over a GFP_FS 2532 * allocation). 2533 */ 2534 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) { 2535 if (hlock->read) { 2536 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ)) 2537 return 0; 2538 } else { 2539 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS)) 2540 return 0; 2541 } 2542 } 2543 2544 return 1; 2545} 2546 2547static int separate_irq_context(struct task_struct *curr, 2548 struct held_lock *hlock) 2549{ 2550 unsigned int depth = curr->lockdep_depth; 2551 2552 /* 2553 * Keep track of points where we cross into an interrupt context: 2554 */ 2555 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) + 2556 curr->softirq_context; 2557 if (depth) { 2558 struct held_lock *prev_hlock; 2559 2560 prev_hlock = curr->held_locks + depth-1; 2561 /* 2562 * If we cross into another context, reset the 2563 * hash key (this also prevents the checking and the 2564 * adding of the dependency to 'prev'): 2565 */ 2566 if (prev_hlock->irq_context != hlock->irq_context) 2567 return 1; 2568 } 2569 return 0; 2570} 2571 2572#else 2573 2574static inline 2575int mark_lock_irq(struct task_struct *curr, struct held_lock *this, 2576 enum lock_usage_bit new_bit) 2577{ 2578 WARN_ON(1); 2579 return 1; 2580} 2581 2582static inline int mark_irqflags(struct task_struct *curr, 2583 struct held_lock *hlock) 2584{ 2585 return 1; 2586} 2587 2588static inline int separate_irq_context(struct task_struct *curr, 2589 struct held_lock *hlock) 2590{ 2591 return 0; 2592} 2593 2594void lockdep_trace_alloc(gfp_t gfp_mask) 2595{ 2596} 2597 2598#endif 2599 2600/* 2601 * Mark a lock with a usage bit, and validate the state transition: 2602 */ 2603static int mark_lock(struct task_struct *curr, struct held_lock *this, 2604 enum lock_usage_bit new_bit) 2605{ 2606 unsigned int new_mask = 1 << new_bit, ret = 1; 2607 2608 /* 2609 * If already set then do not dirty the cacheline, 2610 * nor do any checks: 2611 */ 2612 if (likely(hlock_class(this)->usage_mask & new_mask)) 2613 return 1; 2614 2615 if (!graph_lock()) 2616 return 0; 2617 /* 2618 * Make sure we didnt race: 2619 */ 2620 if (unlikely(hlock_class(this)->usage_mask & new_mask)) { 2621 graph_unlock(); 2622 return 1; 2623 } 2624 2625 hlock_class(this)->usage_mask |= new_mask; 2626 2627 if (!save_trace(hlock_class(this)->usage_traces + new_bit)) 2628 return 0; 2629 2630 switch (new_bit) { 2631#define LOCKDEP_STATE(__STATE) \ 2632 case LOCK_USED_IN_##__STATE: \ 2633 case LOCK_USED_IN_##__STATE##_READ: \ 2634 case LOCK_ENABLED_##__STATE: \ 2635 case LOCK_ENABLED_##__STATE##_READ: 2636#include "lockdep_states.h" 2637#undef LOCKDEP_STATE 2638 ret = mark_lock_irq(curr, this, new_bit); 2639 if (!ret) 2640 return 0; 2641 break; 2642 case LOCK_USED: 2643 debug_atomic_dec(nr_unused_locks); 2644 break; 2645 default: 2646 if (!debug_locks_off_graph_unlock()) 2647 return 0; 2648 WARN_ON(1); 2649 return 0; 2650 } 2651 2652 graph_unlock(); 2653 2654 /* 2655 * We must printk outside of the graph_lock: 2656 */ 2657 if (ret == 2) { 2658 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); 2659 print_lock(this); 2660 print_irqtrace_events(curr); 2661 dump_stack(); 2662 } 2663 2664 return ret; 2665} 2666 2667/* 2668 * Initialize a lock instance's lock-class mapping info: 2669 */ 2670void lockdep_init_map(struct lockdep_map *lock, const char *name, 2671 struct lock_class_key *key, int subclass) 2672{ 2673 lock->class_cache = NULL; 2674#ifdef CONFIG_LOCK_STAT 2675 lock->cpu = raw_smp_processor_id(); 2676#endif 2677 2678 if (DEBUG_LOCKS_WARN_ON(!name)) { 2679 lock->name = "NULL"; 2680 return; 2681 } 2682 2683 lock->name = name; 2684 2685 if (DEBUG_LOCKS_WARN_ON(!key)) 2686 return; 2687 /* 2688 * Sanity check, the lock-class key must be persistent: 2689 */ 2690 if (!static_obj(key)) { 2691 printk("BUG: key %p not in .data!\n", key); 2692 DEBUG_LOCKS_WARN_ON(1); 2693 return; 2694 } 2695 lock->key = key; 2696 2697 if (unlikely(!debug_locks)) 2698 return; 2699 2700 if (subclass) 2701 register_lock_class(lock, subclass, 1); 2702} 2703EXPORT_SYMBOL_GPL(lockdep_init_map); 2704 2705struct lock_class_key __lockdep_no_validate__; 2706 2707/* 2708 * This gets called for every mutex_lock*()/spin_lock*() operation. 2709 * We maintain the dependency maps and validate the locking attempt: 2710 */ 2711static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, 2712 int trylock, int read, int check, int hardirqs_off, 2713 struct lockdep_map *nest_lock, unsigned long ip, 2714 int references) 2715{ 2716 struct task_struct *curr = current; 2717 struct lock_class *class = NULL; 2718 struct held_lock *hlock; 2719 unsigned int depth, id; 2720 int chain_head = 0; 2721 int class_idx; 2722 u64 chain_key; 2723 2724 if (!prove_locking) 2725 check = 1; 2726 2727 if (unlikely(!debug_locks)) 2728 return 0; 2729 2730 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2731 return 0; 2732 2733 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) { 2734 debug_locks_off(); 2735 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n"); 2736 printk("turning off the locking correctness validator.\n"); 2737 dump_stack(); 2738 return 0; 2739 } 2740 2741 if (lock->key == &__lockdep_no_validate__) 2742 check = 1; 2743 2744 if (!subclass) 2745 class = lock->class_cache; 2746 /* 2747 * Not cached yet or subclass? 2748 */ 2749 if (unlikely(!class)) { 2750 class = register_lock_class(lock, subclass, 0); 2751 if (!class) 2752 return 0; 2753 } 2754 atomic_inc((atomic_t *)&class->ops); 2755 if (very_verbose(class)) { 2756 printk("\nacquire class [%p] %s", class->key, class->name); 2757 if (class->name_version > 1) 2758 printk("#%d", class->name_version); 2759 printk("\n"); 2760 dump_stack(); 2761 } 2762 2763 /* 2764 * Add the lock to the list of currently held locks. 2765 * (we dont increase the depth just yet, up until the 2766 * dependency checks are done) 2767 */ 2768 depth = curr->lockdep_depth; 2769 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) 2770 return 0; 2771 2772 class_idx = class - lock_classes + 1; 2773 2774 if (depth) { 2775 hlock = curr->held_locks + depth - 1; 2776 if (hlock->class_idx == class_idx && nest_lock) { 2777 if (hlock->references) 2778 hlock->references++; 2779 else 2780 hlock->references = 2; 2781 2782 return 1; 2783 } 2784 } 2785 2786 hlock = curr->held_locks + depth; 2787 if (DEBUG_LOCKS_WARN_ON(!class)) 2788 return 0; 2789 hlock->class_idx = class_idx; 2790 hlock->acquire_ip = ip; 2791 hlock->instance = lock; 2792 hlock->nest_lock = nest_lock; 2793 hlock->trylock = trylock; 2794 hlock->read = read; 2795 hlock->check = check; 2796 hlock->hardirqs_off = !!hardirqs_off; 2797 hlock->references = references; 2798#ifdef CONFIG_LOCK_STAT 2799 hlock->waittime_stamp = 0; 2800 hlock->holdtime_stamp = lockstat_clock(); 2801#endif 2802 2803 if (check == 2 && !mark_irqflags(curr, hlock)) 2804 return 0; 2805 2806 /* mark it as used: */ 2807 if (!mark_lock(curr, hlock, LOCK_USED)) 2808 return 0; 2809 2810 /* 2811 * Calculate the chain hash: it's the combined hash of all the 2812 * lock keys along the dependency chain. We save the hash value 2813 * at every step so that we can get the current hash easily 2814 * after unlock. The chain hash is then used to cache dependency 2815 * results. 2816 * 2817 * The 'key ID' is what is the most compact key value to drive 2818 * the hash, not class->key. 2819 */ 2820 id = class - lock_classes; 2821 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS)) 2822 return 0; 2823 2824 chain_key = curr->curr_chain_key; 2825 if (!depth) { 2826 if (DEBUG_LOCKS_WARN_ON(chain_key != 0)) 2827 return 0; 2828 chain_head = 1; 2829 } 2830 2831 hlock->prev_chain_key = chain_key; 2832 if (separate_irq_context(curr, hlock)) { 2833 chain_key = 0; 2834 chain_head = 1; 2835 } 2836 chain_key = iterate_chain_key(chain_key, id); 2837 2838 if (!validate_chain(curr, lock, hlock, chain_head, chain_key)) 2839 return 0; 2840 2841 curr->curr_chain_key = chain_key; 2842 curr->lockdep_depth++; 2843 check_chain_key(curr); 2844#ifdef CONFIG_DEBUG_LOCKDEP 2845 if (unlikely(!debug_locks)) 2846 return 0; 2847#endif 2848 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { 2849 debug_locks_off(); 2850 printk("BUG: MAX_LOCK_DEPTH too low!\n"); 2851 printk("turning off the locking correctness validator.\n"); 2852 dump_stack(); 2853 return 0; 2854 } 2855 2856 if (unlikely(curr->lockdep_depth > max_lockdep_depth)) 2857 max_lockdep_depth = curr->lockdep_depth; 2858 2859 return 1; 2860} 2861 2862static int 2863print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock, 2864 unsigned long ip) 2865{ 2866 if (!debug_locks_off()) 2867 return 0; 2868 if (debug_locks_silent) 2869 return 0; 2870 2871 printk("\n=====================================\n"); 2872 printk( "[ BUG: bad unlock balance detected! ]\n"); 2873 printk( "-------------------------------------\n"); 2874 printk("%s/%d is trying to release lock (", 2875 curr->comm, task_pid_nr(curr)); 2876 print_lockdep_cache(lock); 2877 printk(") at:\n"); 2878 print_ip_sym(ip); 2879 printk("but there are no more locks to release!\n"); 2880 printk("\nother info that might help us debug this:\n"); 2881 lockdep_print_held_locks(curr); 2882 2883 printk("\nstack backtrace:\n"); 2884 dump_stack(); 2885 2886 return 0; 2887} 2888 2889/* 2890 * Common debugging checks for both nested and non-nested unlock: 2891 */ 2892static int check_unlock(struct task_struct *curr, struct lockdep_map *lock, 2893 unsigned long ip) 2894{ 2895 if (unlikely(!debug_locks)) 2896 return 0; 2897 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) 2898 return 0; 2899 2900 if (curr->lockdep_depth <= 0) 2901 return print_unlock_inbalance_bug(curr, lock, ip); 2902 2903 return 1; 2904} 2905 2906static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock) 2907{ 2908 if (hlock->instance == lock) 2909 return 1; 2910 2911 if (hlock->references) { 2912 struct lock_class *class = lock->class_cache; 2913 2914 if (!class) 2915 class = look_up_lock_class(lock, 0); 2916 2917 if (DEBUG_LOCKS_WARN_ON(!class)) 2918 return 0; 2919 2920 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) 2921 return 0; 2922 2923 if (hlock->class_idx == class - lock_classes + 1) 2924 return 1; 2925 } 2926 2927 return 0; 2928} 2929 2930static int 2931__lock_set_class(struct lockdep_map *lock, const char *name, 2932 struct lock_class_key *key, unsigned int subclass, 2933 unsigned long ip) 2934{ 2935 struct task_struct *curr = current; 2936 struct held_lock *hlock, *prev_hlock; 2937 struct lock_class *class; 2938 unsigned int depth; 2939 int i; 2940 2941 depth = curr->lockdep_depth; 2942 if (DEBUG_LOCKS_WARN_ON(!depth)) 2943 return 0; 2944 2945 prev_hlock = NULL; 2946 for (i = depth-1; i >= 0; i--) { 2947 hlock = curr->held_locks + i; 2948 /* 2949 * We must not cross into another context: 2950 */ 2951 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) 2952 break; 2953 if (match_held_lock(hlock, lock)) 2954 goto found_it; 2955 prev_hlock = hlock; 2956 } 2957 return print_unlock_inbalance_bug(curr, lock, ip); 2958 2959found_it: 2960 lockdep_init_map(lock, name, key, 0); 2961 class = register_lock_class(lock, subclass, 0); 2962 hlock->class_idx = class - lock_classes + 1; 2963 2964 curr->lockdep_depth = i; 2965 curr->curr_chain_key = hlock->prev_chain_key; 2966 2967 for (; i < depth; i++) { 2968 hlock = curr->held_locks + i; 2969 if (!__lock_acquire(hlock->instance, 2970 hlock_class(hlock)->subclass, hlock->trylock, 2971 hlock->read, hlock->check, hlock->hardirqs_off, 2972 hlock->nest_lock, hlock->acquire_ip, 2973 hlock->references)) 2974 return 0; 2975 } 2976 2977 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) 2978 return 0; 2979 return 1; 2980} 2981 2982/* 2983 * Remove the lock to the list of currently held locks in a 2984 * potentially non-nested (out of order) manner. This is a 2985 * relatively rare operation, as all the unlock APIs default 2986 * to nested mode (which uses lock_release()): 2987 */ 2988static int 2989lock_release_non_nested(struct task_struct *curr, 2990 struct lockdep_map *lock, unsigned long ip) 2991{ 2992 struct held_lock *hlock, *prev_hlock; 2993 unsigned int depth; 2994 int i; 2995 2996 /* 2997 * Check whether the lock exists in the current stack 2998 * of held locks: 2999 */ 3000 depth = curr->lockdep_depth; 3001 if (DEBUG_LOCKS_WARN_ON(!depth)) 3002 return 0; 3003 3004 prev_hlock = NULL; 3005 for (i = depth-1; i >= 0; i--) { 3006 hlock = curr->held_locks + i; 3007 /* 3008 * We must not cross into another context: 3009 */ 3010 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) 3011 break; 3012 if (match_held_lock(hlock, lock)) 3013 goto found_it; 3014 prev_hlock = hlock; 3015 } 3016 return print_unlock_inbalance_bug(curr, lock, ip); 3017 3018found_it: 3019 if (hlock->instance == lock) 3020 lock_release_holdtime(hlock); 3021 3022 if (hlock->references) { 3023 hlock->references--; 3024 if (hlock->references) { 3025 /* 3026 * We had, and after removing one, still have 3027 * references, the current lock stack is still 3028 * valid. We're done! 3029 */ 3030 return 1; 3031 } 3032 } 3033 3034 /* 3035 * We have the right lock to unlock, 'hlock' points to it. 3036 * Now we remove it from the stack, and add back the other 3037 * entries (if any), recalculating the hash along the way: 3038 */ 3039 3040 curr->lockdep_depth = i; 3041 curr->curr_chain_key = hlock->prev_chain_key; 3042 3043 for (i++; i < depth; i++) { 3044 hlock = curr->held_locks + i; 3045 if (!__lock_acquire(hlock->instance, 3046 hlock_class(hlock)->subclass, hlock->trylock, 3047 hlock->read, hlock->check, hlock->hardirqs_off, 3048 hlock->nest_lock, hlock->acquire_ip, 3049 hlock->references)) 3050 return 0; 3051 } 3052 3053 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1)) 3054 return 0; 3055 return 1; 3056} 3057 3058/* 3059 * Remove the lock to the list of currently held locks - this gets 3060 * called on mutex_unlock()/spin_unlock*() (or on a failed 3061 * mutex_lock_interruptible()). This is done for unlocks that nest 3062 * perfectly. (i.e. the current top of the lock-stack is unlocked) 3063 */ 3064static int lock_release_nested(struct task_struct *curr, 3065 struct lockdep_map *lock, unsigned long ip) 3066{ 3067 struct held_lock *hlock; 3068 unsigned int depth; 3069 3070 /* 3071 * Pop off the top of the lock stack: 3072 */ 3073 depth = curr->lockdep_depth - 1; 3074 hlock = curr->held_locks + depth; 3075 3076 /* 3077 * Is the unlock non-nested: 3078 */ 3079 if (hlock->instance != lock || hlock->references) 3080 return lock_release_non_nested(curr, lock, ip); 3081 curr->lockdep_depth--; 3082 3083 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0))) 3084 return 0; 3085 3086 curr->curr_chain_key = hlock->prev_chain_key; 3087 3088 lock_release_holdtime(hlock); 3089 3090#ifdef CONFIG_DEBUG_LOCKDEP 3091 hlock->prev_chain_key = 0; 3092 hlock->class_idx = 0; 3093 hlock->acquire_ip = 0; 3094 hlock->irq_context = 0; 3095#endif 3096 return 1; 3097} 3098 3099/* 3100 * Remove the lock to the list of currently held locks - this gets 3101 * called on mutex_unlock()/spin_unlock*() (or on a failed 3102 * mutex_lock_interruptible()). This is done for unlocks that nest 3103 * perfectly. (i.e. the current top of the lock-stack is unlocked) 3104 */ 3105static void 3106__lock_release(struct lockdep_map *lock, int nested, unsigned long ip) 3107{ 3108 struct task_struct *curr = current; 3109 3110 if (!check_unlock(curr, lock, ip)) 3111 return; 3112 3113 if (nested) { 3114 if (!lock_release_nested(curr, lock, ip)) 3115 return; 3116 } else { 3117 if (!lock_release_non_nested(curr, lock, ip)) 3118 return; 3119 } 3120 3121 check_chain_key(curr); 3122} 3123 3124static int __lock_is_held(struct lockdep_map *lock) 3125{ 3126 struct task_struct *curr = current; 3127 int i; 3128 3129 for (i = 0; i < curr->lockdep_depth; i++) { 3130 struct held_lock *hlock = curr->held_locks + i; 3131 3132 if (match_held_lock(hlock, lock)) 3133 return 1; 3134 } 3135 3136 return 0; 3137} 3138 3139/* 3140 * Check whether we follow the irq-flags state precisely: 3141 */ 3142static void check_flags(unsigned long flags) 3143{ 3144#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \ 3145 defined(CONFIG_TRACE_IRQFLAGS) 3146 if (!debug_locks) 3147 return; 3148 3149 if (irqs_disabled_flags(flags)) { 3150 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) { 3151 printk("possible reason: unannotated irqs-off.\n"); 3152 } 3153 } else { 3154 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) { 3155 printk("possible reason: unannotated irqs-on.\n"); 3156 } 3157 } 3158 3159 /* 3160 * We dont accurately track softirq state in e.g. 3161 * hardirq contexts (such as on 4KSTACKS), so only 3162 * check if not in hardirq contexts: 3163 */ 3164 if (!hardirq_count()) { 3165 if (softirq_count()) 3166 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); 3167 else 3168 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); 3169 } 3170 3171 if (!debug_locks) 3172 print_irqtrace_events(current); 3173#endif 3174} 3175 3176void lock_set_class(struct lockdep_map *lock, const char *name, 3177 struct lock_class_key *key, unsigned int subclass, 3178 unsigned long ip) 3179{ 3180 unsigned long flags; 3181 3182 if (unlikely(current->lockdep_recursion)) 3183 return; 3184 3185 raw_local_irq_save(flags); 3186 current->lockdep_recursion = 1; 3187 check_flags(flags); 3188 if (__lock_set_class(lock, name, key, subclass, ip)) 3189 check_chain_key(current); 3190 current->lockdep_recursion = 0; 3191 raw_local_irq_restore(flags); 3192} 3193EXPORT_SYMBOL_GPL(lock_set_class); 3194 3195/* 3196 * We are not always called with irqs disabled - do that here, 3197 * and also avoid lockdep recursion: 3198 */ 3199void lock_acquire(struct lockdep_map *lock, unsigned int subclass, 3200 int trylock, int read, int check, 3201 struct lockdep_map *nest_lock, unsigned long ip) 3202{ 3203 unsigned long flags; 3204 3205 if (unlikely(current->lockdep_recursion)) 3206 return; 3207 3208 raw_local_irq_save(flags); 3209 check_flags(flags); 3210 3211 current->lockdep_recursion = 1; 3212 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip); 3213 __lock_acquire(lock, subclass, trylock, read, check, 3214 irqs_disabled_flags(flags), nest_lock, ip, 0); 3215 current->lockdep_recursion = 0; 3216 raw_local_irq_restore(flags); 3217} 3218EXPORT_SYMBOL_GPL(lock_acquire); 3219 3220void lock_release(struct lockdep_map *lock, int nested, 3221 unsigned long ip) 3222{ 3223 unsigned long flags; 3224 3225 if (unlikely(current->lockdep_recursion)) 3226 return; 3227 3228 raw_local_irq_save(flags); 3229 check_flags(flags); 3230 current->lockdep_recursion = 1; 3231 trace_lock_release(lock, ip); 3232 __lock_release(lock, nested, ip); 3233 current->lockdep_recursion = 0; 3234 raw_local_irq_restore(flags); 3235} 3236EXPORT_SYMBOL_GPL(lock_release); 3237 3238int lock_is_held(struct lockdep_map *lock) 3239{ 3240 unsigned long flags; 3241 int ret = 0; 3242 3243 if (unlikely(current->lockdep_recursion)) 3244 return ret; 3245 3246 raw_local_irq_save(flags); 3247 check_flags(flags); 3248 3249 current->lockdep_recursion = 1; 3250 ret = __lock_is_held(lock); 3251 current->lockdep_recursion = 0; 3252 raw_local_irq_restore(flags); 3253 3254 return ret; 3255} 3256EXPORT_SYMBOL_GPL(lock_is_held); 3257 3258void lockdep_set_current_reclaim_state(gfp_t gfp_mask) 3259{ 3260 current->lockdep_reclaim_gfp = gfp_mask; 3261} 3262 3263void lockdep_clear_current_reclaim_state(void) 3264{ 3265 current->lockdep_reclaim_gfp = 0; 3266} 3267 3268#ifdef CONFIG_LOCK_STAT 3269static int 3270print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock, 3271 unsigned long ip) 3272{ 3273 if (!debug_locks_off()) 3274 return 0; 3275 if (debug_locks_silent) 3276 return 0; 3277 3278 printk("\n=================================\n"); 3279 printk( "[ BUG: bad contention detected! ]\n"); 3280 printk( "---------------------------------\n"); 3281 printk("%s/%d is trying to contend lock (", 3282 curr->comm, task_pid_nr(curr)); 3283 print_lockdep_cache(lock); 3284 printk(") at:\n"); 3285 print_ip_sym(ip); 3286 printk("but there are no locks held!\n"); 3287 printk("\nother info that might help us debug this:\n"); 3288 lockdep_print_held_locks(curr); 3289 3290 printk("\nstack backtrace:\n"); 3291 dump_stack(); 3292 3293 return 0; 3294} 3295 3296static void 3297__lock_contended(struct lockdep_map *lock, unsigned long ip) 3298{ 3299 struct task_struct *curr = current; 3300 struct held_lock *hlock, *prev_hlock; 3301 struct lock_class_stats *stats; 3302 unsigned int depth; 3303 int i, contention_point, contending_point; 3304 3305 depth = curr->lockdep_depth; 3306 if (DEBUG_LOCKS_WARN_ON(!depth)) 3307 return; 3308 3309 prev_hlock = NULL; 3310 for (i = depth-1; i >= 0; i--) { 3311 hlock = curr->held_locks + i; 3312 /* 3313 * We must not cross into another context: 3314 */ 3315 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) 3316 break; 3317 if (match_held_lock(hlock, lock)) 3318 goto found_it; 3319 prev_hlock = hlock; 3320 } 3321 print_lock_contention_bug(curr, lock, ip); 3322 return; 3323 3324found_it: 3325 if (hlock->instance != lock) 3326 return; 3327 3328 hlock->waittime_stamp = lockstat_clock(); 3329 3330 contention_point = lock_point(hlock_class(hlock)->contention_point, ip); 3331 contending_point = lock_point(hlock_class(hlock)->contending_point, 3332 lock->ip); 3333 3334 stats = get_lock_stats(hlock_class(hlock)); 3335 if (contention_point < LOCKSTAT_POINTS) 3336 stats->contention_point[contention_point]++; 3337 if (contending_point < LOCKSTAT_POINTS) 3338 stats->contending_point[contending_point]++; 3339 if (lock->cpu != smp_processor_id()) 3340 stats->bounces[bounce_contended + !!hlock->read]++; 3341 put_lock_stats(stats); 3342} 3343 3344static void 3345__lock_acquired(struct lockdep_map *lock, unsigned long ip) 3346{ 3347 struct task_struct *curr = current; 3348 struct held_lock *hlock, *prev_hlock; 3349 struct lock_class_stats *stats; 3350 unsigned int depth; 3351 u64 now, waittime = 0; 3352 int i, cpu; 3353 3354 depth = curr->lockdep_depth; 3355 if (DEBUG_LOCKS_WARN_ON(!depth)) 3356 return; 3357 3358 prev_hlock = NULL; 3359 for (i = depth-1; i >= 0; i--) { 3360 hlock = curr->held_locks + i; 3361 /* 3362 * We must not cross into another context: 3363 */ 3364 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) 3365 break; 3366 if (match_held_lock(hlock, lock)) 3367 goto found_it; 3368 prev_hlock = hlock; 3369 } 3370 print_lock_contention_bug(curr, lock, _RET_IP_); 3371 return; 3372 3373found_it: 3374 if (hlock->instance != lock) 3375 return; 3376 3377 cpu = smp_processor_id(); 3378 if (hlock->waittime_stamp) { 3379 now = lockstat_clock(); 3380 waittime = now - hlock->waittime_stamp; 3381 hlock->holdtime_stamp = now; 3382 } 3383 3384 trace_lock_acquired(lock, ip); 3385 3386 stats = get_lock_stats(hlock_class(hlock)); 3387 if (waittime) { 3388 if (hlock->read) 3389 lock_time_inc(&stats->read_waittime, waittime); 3390 else 3391 lock_time_inc(&stats->write_waittime, waittime); 3392 } 3393 if (lock->cpu != cpu) 3394 stats->bounces[bounce_acquired + !!hlock->read]++; 3395 put_lock_stats(stats); 3396 3397 lock->cpu = cpu; 3398 lock->ip = ip; 3399} 3400 3401void lock_contended(struct lockdep_map *lock, unsigned long ip) 3402{ 3403 unsigned long flags; 3404 3405 if (unlikely(!lock_stat)) 3406 return; 3407 3408 if (unlikely(current->lockdep_recursion)) 3409 return; 3410 3411 raw_local_irq_save(flags); 3412 check_flags(flags); 3413 current->lockdep_recursion = 1; 3414 trace_lock_contended(lock, ip); 3415 __lock_contended(lock, ip); 3416 current->lockdep_recursion = 0; 3417 raw_local_irq_restore(flags); 3418} 3419EXPORT_SYMBOL_GPL(lock_contended); 3420 3421void lock_acquired(struct lockdep_map *lock, unsigned long ip) 3422{ 3423 unsigned long flags; 3424 3425 if (unlikely(!lock_stat)) 3426 return; 3427 3428 if (unlikely(current->lockdep_recursion)) 3429 return; 3430 3431 raw_local_irq_save(flags); 3432 check_flags(flags); 3433 current->lockdep_recursion = 1; 3434 __lock_acquired(lock, ip); 3435 current->lockdep_recursion = 0; 3436 raw_local_irq_restore(flags); 3437} 3438EXPORT_SYMBOL_GPL(lock_acquired); 3439#endif 3440 3441/* 3442 * Used by the testsuite, sanitize the validator state 3443 * after a simulated failure: 3444 */ 3445 3446void lockdep_reset(void) 3447{ 3448 unsigned long flags; 3449 int i; 3450 3451 raw_local_irq_save(flags); 3452 current->curr_chain_key = 0; 3453 current->lockdep_depth = 0; 3454 current->lockdep_recursion = 0; 3455 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); 3456 nr_hardirq_chains = 0; 3457 nr_softirq_chains = 0; 3458 nr_process_chains = 0; 3459 debug_locks = 1; 3460 for (i = 0; i < CHAINHASH_SIZE; i++) 3461 INIT_LIST_HEAD(chainhash_table + i); 3462 raw_local_irq_restore(flags); 3463} 3464 3465static void zap_class(struct lock_class *class) 3466{ 3467 int i; 3468 3469 /* 3470 * Remove all dependencies this lock is 3471 * involved in: 3472 */ 3473 for (i = 0; i < nr_list_entries; i++) { 3474 if (list_entries[i].class == class) 3475 list_del_rcu(&list_entries[i].entry); 3476 } 3477 /* 3478 * Unhash the class and remove it from the all_lock_classes list: 3479 */ 3480 list_del_rcu(&class->hash_entry); 3481 list_del_rcu(&class->lock_entry); 3482 3483 class->key = NULL; 3484} 3485 3486static inline int within(const void *addr, void *start, unsigned long size) 3487{ 3488 return addr >= start && addr < start + size; 3489} 3490 3491void lockdep_free_key_range(void *start, unsigned long size) 3492{ 3493 struct lock_class *class, *next; 3494 struct list_head *head; 3495 unsigned long flags; 3496 int i; 3497 int locked; 3498 3499 raw_local_irq_save(flags); 3500 locked = graph_lock(); 3501 3502 /* 3503 * Unhash all classes that were created by this module: 3504 */ 3505 for (i = 0; i < CLASSHASH_SIZE; i++) { 3506 head = classhash_table + i; 3507 if (list_empty(head)) 3508 continue; 3509 list_for_each_entry_safe(class, next, head, hash_entry) { 3510 if (within(class->key, start, size)) 3511 zap_class(class); 3512 else if (within(class->name, start, size)) 3513 zap_class(class); 3514 } 3515 } 3516 3517 if (locked) 3518 graph_unlock(); 3519 raw_local_irq_restore(flags); 3520} 3521 3522void lockdep_reset_lock(struct lockdep_map *lock) 3523{ 3524 struct lock_class *class, *next; 3525 struct list_head *head; 3526 unsigned long flags; 3527 int i, j; 3528 int locked; 3529 3530 raw_local_irq_save(flags); 3531 3532 /* 3533 * Remove all classes this lock might have: 3534 */ 3535 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) { 3536 /* 3537 * If the class exists we look it up and zap it: 3538 */ 3539 class = look_up_lock_class(lock, j); 3540 if (class) 3541 zap_class(class); 3542 } 3543 /* 3544 * Debug check: in the end all mapped classes should 3545 * be gone. 3546 */ 3547 locked = graph_lock(); 3548 for (i = 0; i < CLASSHASH_SIZE; i++) { 3549 head = classhash_table + i; 3550 if (list_empty(head)) 3551 continue; 3552 list_for_each_entry_safe(class, next, head, hash_entry) { 3553 if (unlikely(class == lock->class_cache)) { 3554 if (debug_locks_off_graph_unlock()) 3555 WARN_ON(1); 3556 goto out_restore; 3557 } 3558 } 3559 } 3560 if (locked) 3561 graph_unlock(); 3562 3563out_restore: 3564 raw_local_irq_restore(flags); 3565} 3566 3567void lockdep_init(void) 3568{ 3569 int i; 3570 3571 /* 3572 * Some architectures have their own start_kernel() 3573 * code which calls lockdep_init(), while we also 3574 * call lockdep_init() from the start_kernel() itself, 3575 * and we want to initialize the hashes only once: 3576 */ 3577 if (lockdep_initialized) 3578 return; 3579 3580 for (i = 0; i < CLASSHASH_SIZE; i++) 3581 INIT_LIST_HEAD(classhash_table + i); 3582 3583 for (i = 0; i < CHAINHASH_SIZE; i++) 3584 INIT_LIST_HEAD(chainhash_table + i); 3585 3586 lockdep_initialized = 1; 3587} 3588 3589void __init lockdep_info(void) 3590{ 3591 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); 3592 3593 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); 3594 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); 3595 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); 3596 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); 3597 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES); 3598 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS); 3599 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE); 3600 3601 printk(" memory used by lock dependency info: %lu kB\n", 3602 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS + 3603 sizeof(struct list_head) * CLASSHASH_SIZE + 3604 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES + 3605 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS + 3606 sizeof(struct list_head) * CHAINHASH_SIZE 3607#ifdef CONFIG_PROVE_LOCKING 3608 + sizeof(struct circular_queue) 3609#endif 3610 ) / 1024 3611 ); 3612 3613 printk(" per task-struct memory footprint: %lu bytes\n", 3614 sizeof(struct held_lock) * MAX_LOCK_DEPTH); 3615 3616#ifdef CONFIG_DEBUG_LOCKDEP 3617 if (lockdep_init_error) { 3618 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n"); 3619 printk("Call stack leading to lockdep invocation was:\n"); 3620 print_stack_trace(&lockdep_init_trace, 0); 3621 } 3622#endif 3623} 3624 3625static void 3626print_freed_lock_bug(struct task_struct *curr, const void *mem_from, 3627 const void *mem_to, struct held_lock *hlock) 3628{ 3629 if (!debug_locks_off()) 3630 return; 3631 if (debug_locks_silent) 3632 return; 3633 3634 printk("\n=========================\n"); 3635 printk( "[ BUG: held lock freed! ]\n"); 3636 printk( "-------------------------\n"); 3637 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n", 3638 curr->comm, task_pid_nr(curr), mem_from, mem_to-1); 3639 print_lock(hlock); 3640 lockdep_print_held_locks(curr); 3641 3642 printk("\nstack backtrace:\n"); 3643 dump_stack(); 3644} 3645 3646static inline int not_in_range(const void* mem_from, unsigned long mem_len, 3647 const void* lock_from, unsigned long lock_len) 3648{ 3649 return lock_from + lock_len <= mem_from || 3650 mem_from + mem_len <= lock_from; 3651} 3652 3653/* 3654 * Called when kernel memory is freed (or unmapped), or if a lock 3655 * is destroyed or reinitialized - this code checks whether there is 3656 * any held lock in the memory range of <from> to <to>: 3657 */ 3658void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) 3659{ 3660 struct task_struct *curr = current; 3661 struct held_lock *hlock; 3662 unsigned long flags; 3663 int i; 3664 3665 if (unlikely(!debug_locks)) 3666 return; 3667 3668 local_irq_save(flags); 3669 for (i = 0; i < curr->lockdep_depth; i++) { 3670 hlock = curr->held_locks + i; 3671 3672 if (not_in_range(mem_from, mem_len, hlock->instance, 3673 sizeof(*hlock->instance))) 3674 continue; 3675 3676 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); 3677 break; 3678 } 3679 local_irq_restore(flags); 3680} 3681EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); 3682 3683static void print_held_locks_bug(struct task_struct *curr) 3684{ 3685 if (!debug_locks_off()) 3686 return; 3687 if (debug_locks_silent) 3688 return; 3689 3690 printk("\n=====================================\n"); 3691 printk( "[ BUG: lock held at task exit time! ]\n"); 3692 printk( "-------------------------------------\n"); 3693 printk("%s/%d is exiting with locks still held!\n", 3694 curr->comm, task_pid_nr(curr)); 3695 lockdep_print_held_locks(curr); 3696 3697 printk("\nstack backtrace:\n"); 3698 dump_stack(); 3699} 3700 3701void debug_check_no_locks_held(struct task_struct *task) 3702{ 3703 if (unlikely(task->lockdep_depth > 0)) 3704 print_held_locks_bug(task); 3705} 3706 3707void debug_show_all_locks(void) 3708{ 3709 struct task_struct *g, *p; 3710 int count = 10; 3711 int unlock = 1; 3712 3713 if (unlikely(!debug_locks)) { 3714 printk("INFO: lockdep is turned off.\n"); 3715 return; 3716 } 3717 printk("\nShowing all locks held in the system:\n"); 3718 3719 /* 3720 * Here we try to get the tasklist_lock as hard as possible, 3721 * if not successful after 2 seconds we ignore it (but keep 3722 * trying). This is to enable a debug printout even if a 3723 * tasklist_lock-holding task deadlocks or crashes. 3724 */ 3725retry: 3726 if (!read_trylock(&tasklist_lock)) { 3727 if (count == 10) 3728 printk("hm, tasklist_lock locked, retrying... "); 3729 if (count) { 3730 count--; 3731 printk(" #%d", 10-count); 3732 mdelay(200); 3733 goto retry; 3734 } 3735 printk(" ignoring it.\n"); 3736 unlock = 0; 3737 } else { 3738 if (count != 10) 3739 printk(KERN_CONT " locked it.\n"); 3740 } 3741 3742 do_each_thread(g, p) { 3743 /* 3744 * It's not reliable to print a task's held locks 3745 * if it's not sleeping (or if it's not the current 3746 * task): 3747 */ 3748 if (p->state == TASK_RUNNING && p != current) 3749 continue; 3750 if (p->lockdep_depth) 3751 lockdep_print_held_locks(p); 3752 if (!unlock) 3753 if (read_trylock(&tasklist_lock)) 3754 unlock = 1; 3755 } while_each_thread(g, p); 3756 3757 printk("\n"); 3758 printk("=============================================\n\n"); 3759 3760 if (unlock) 3761 read_unlock(&tasklist_lock); 3762} 3763EXPORT_SYMBOL_GPL(debug_show_all_locks); 3764 3765/* 3766 * Careful: only use this function if you are sure that 3767 * the task cannot run in parallel! 3768 */ 3769void __debug_show_held_locks(struct task_struct *task) 3770{ 3771 if (unlikely(!debug_locks)) { 3772 printk("INFO: lockdep is turned off.\n"); 3773 return; 3774 } 3775 lockdep_print_held_locks(task); 3776} 3777EXPORT_SYMBOL_GPL(__debug_show_held_locks); 3778 3779void debug_show_held_locks(struct task_struct *task) 3780{ 3781 __debug_show_held_locks(task); 3782} 3783EXPORT_SYMBOL_GPL(debug_show_held_locks); 3784 3785void lockdep_sys_exit(void) 3786{ 3787 struct task_struct *curr = current; 3788 3789 if (unlikely(curr->lockdep_depth)) { 3790 if (!debug_locks_off()) 3791 return; 3792 printk("\n================================================\n"); 3793 printk( "[ BUG: lock held when returning to user space! ]\n"); 3794 printk( "------------------------------------------------\n"); 3795 printk("%s/%d is leaving the kernel with locks still held!\n", 3796 curr->comm, curr->pid); 3797 lockdep_print_held_locks(curr); 3798 } 3799} 3800 3801void lockdep_rcu_dereference(const char *file, const int line) 3802{ 3803 struct task_struct *curr = current; 3804 3805#ifndef CONFIG_PROVE_RCU_REPEATEDLY 3806 if (!debug_locks_off()) 3807 return; 3808#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */ 3809 /* Note: the following can be executed concurrently, so be careful. */ 3810 printk("\n===================================================\n"); 3811 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n"); 3812 printk( "---------------------------------------------------\n"); 3813 printk("%s:%d invoked rcu_dereference_check() without protection!\n", 3814 file, line); 3815 printk("\nother info that might help us debug this:\n\n"); 3816 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks); 3817 lockdep_print_held_locks(curr); 3818 printk("\nstack backtrace:\n"); 3819 dump_stack(); 3820} 3821EXPORT_SYMBOL_GPL(lockdep_rcu_dereference); 3822