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