4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
28 #include <linux/mutex.h>
29 #include <linux/sched.h>
30 #include <linux/delay.h>
31 #include <linux/module.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/spinlock.h>
35 #include <linux/kallsyms.h>
36 #include <linux/interrupt.h>
37 #include <linux/stacktrace.h>
38 #include <linux/debug_locks.h>
39 #include <linux/irqflags.h>
40 #include <linux/utsname.h>
41 #include <linux/hash.h>
42 #include <linux/ftrace.h>
44 #include <asm/sections.h>
46 #include "lockdep_internals.h"
48 #ifdef CONFIG_PROVE_LOCKING
49 int prove_locking = 1;
50 module_param(prove_locking, int, 0644);
52 #define prove_locking 0
55 #ifdef CONFIG_LOCK_STAT
57 module_param(lock_stat, int, 0644);
63 * lockdep_lock: protects the lockdep graph, the hashes and the
64 * class/list/hash allocators.
66 * This is one of the rare exceptions where it's justified
67 * to use a raw spinlock - we really dont want the spinlock
68 * code to recurse back into the lockdep code...
70 static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
72 static int graph_lock(void)
74 __raw_spin_lock(&lockdep_lock);
76 * Make sure that if another CPU detected a bug while
77 * walking the graph we dont change it (while the other
78 * CPU is busy printing out stuff with the graph lock
82 __raw_spin_unlock(&lockdep_lock);
85 /* prevent any recursions within lockdep from causing deadlocks */
86 current->lockdep_recursion++;
90 static inline int graph_unlock(void)
92 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
93 return DEBUG_LOCKS_WARN_ON(1);
95 current->lockdep_recursion--;
96 __raw_spin_unlock(&lockdep_lock);
101 * Turn lock debugging off and return with 0 if it was off already,
102 * and also release the graph lock:
104 static inline int debug_locks_off_graph_unlock(void)
106 int ret = debug_locks_off();
108 __raw_spin_unlock(&lockdep_lock);
113 static int lockdep_initialized;
115 unsigned long nr_list_entries;
116 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
119 * All data structures here are protected by the global debug_lock.
121 * Mutex key structs only get allocated, once during bootup, and never
122 * get freed - this significantly simplifies the debugging code.
124 unsigned long nr_lock_classes;
125 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
127 static inline struct lock_class *hlock_class(struct held_lock *hlock)
129 if (!hlock->class_idx) {
130 DEBUG_LOCKS_WARN_ON(1);
133 return lock_classes + hlock->class_idx - 1;
136 #ifdef CONFIG_LOCK_STAT
137 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
139 static int lock_contention_point(struct lock_class *class, unsigned long ip)
143 for (i = 0; i < ARRAY_SIZE(class->contention_point); i++) {
144 if (class->contention_point[i] == 0) {
145 class->contention_point[i] = ip;
148 if (class->contention_point[i] == ip)
155 static void lock_time_inc(struct lock_time *lt, s64 time)
160 if (time < lt->min || !lt->min)
167 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
169 dst->min += src->min;
170 dst->max += src->max;
171 dst->total += src->total;
175 struct lock_class_stats lock_stats(struct lock_class *class)
177 struct lock_class_stats stats;
180 memset(&stats, 0, sizeof(struct lock_class_stats));
181 for_each_possible_cpu(cpu) {
182 struct lock_class_stats *pcs =
183 &per_cpu(lock_stats, cpu)[class - lock_classes];
185 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
186 stats.contention_point[i] += pcs->contention_point[i];
188 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
189 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
191 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
192 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
194 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
195 stats.bounces[i] += pcs->bounces[i];
201 void clear_lock_stats(struct lock_class *class)
205 for_each_possible_cpu(cpu) {
206 struct lock_class_stats *cpu_stats =
207 &per_cpu(lock_stats, cpu)[class - lock_classes];
209 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
211 memset(class->contention_point, 0, sizeof(class->contention_point));
214 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
216 return &get_cpu_var(lock_stats)[class - lock_classes];
219 static void put_lock_stats(struct lock_class_stats *stats)
221 put_cpu_var(lock_stats);
224 static void lock_release_holdtime(struct held_lock *hlock)
226 struct lock_class_stats *stats;
232 holdtime = sched_clock() - hlock->holdtime_stamp;
234 stats = get_lock_stats(hlock_class(hlock));
236 lock_time_inc(&stats->read_holdtime, holdtime);
238 lock_time_inc(&stats->write_holdtime, holdtime);
239 put_lock_stats(stats);
242 static inline void lock_release_holdtime(struct held_lock *hlock)
248 * We keep a global list of all lock classes. The list only grows,
249 * never shrinks. The list is only accessed with the lockdep
250 * spinlock lock held.
252 LIST_HEAD(all_lock_classes);
255 * The lockdep classes are in a hash-table as well, for fast lookup:
257 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
258 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
259 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
260 #define classhashentry(key) (classhash_table + __classhashfn((key)))
262 static struct list_head classhash_table[CLASSHASH_SIZE];
265 * We put the lock dependency chains into a hash-table as well, to cache
268 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
269 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
270 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
271 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
273 static struct list_head chainhash_table[CHAINHASH_SIZE];
276 * The hash key of the lock dependency chains is a hash itself too:
277 * it's a hash of all locks taken up to that lock, including that lock.
278 * It's a 64-bit hash, because it's important for the keys to be
281 #define iterate_chain_key(key1, key2) \
282 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
283 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
286 void lockdep_off(void)
288 current->lockdep_recursion++;
291 EXPORT_SYMBOL(lockdep_off);
293 void lockdep_on(void)
295 current->lockdep_recursion--;
298 EXPORT_SYMBOL(lockdep_on);
301 * Debugging switches:
305 #define VERY_VERBOSE 0
308 # define HARDIRQ_VERBOSE 1
309 # define SOFTIRQ_VERBOSE 1
311 # define HARDIRQ_VERBOSE 0
312 # define SOFTIRQ_VERBOSE 0
315 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
317 * Quick filtering for interesting events:
319 static int class_filter(struct lock_class *class)
323 if (class->name_version == 1 &&
324 !strcmp(class->name, "lockname"))
326 if (class->name_version == 1 &&
327 !strcmp(class->name, "&struct->lockfield"))
330 /* Filter everything else. 1 would be to allow everything else */
335 static int verbose(struct lock_class *class)
338 return class_filter(class);
344 * Stack-trace: tightly packed array of stack backtrace
345 * addresses. Protected by the graph_lock.
347 unsigned long nr_stack_trace_entries;
348 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
350 static int save_trace(struct stack_trace *trace)
352 trace->nr_entries = 0;
353 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
354 trace->entries = stack_trace + nr_stack_trace_entries;
358 save_stack_trace(trace);
360 trace->max_entries = trace->nr_entries;
362 nr_stack_trace_entries += trace->nr_entries;
364 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
365 if (!debug_locks_off_graph_unlock())
368 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
369 printk("turning off the locking correctness validator.\n");
378 unsigned int nr_hardirq_chains;
379 unsigned int nr_softirq_chains;
380 unsigned int nr_process_chains;
381 unsigned int max_lockdep_depth;
382 unsigned int max_recursion_depth;
384 static unsigned int lockdep_dependency_gen_id;
386 static bool lockdep_dependency_visit(struct lock_class *source,
390 lockdep_dependency_gen_id++;
391 if (source->dep_gen_id == lockdep_dependency_gen_id)
393 source->dep_gen_id = lockdep_dependency_gen_id;
397 #ifdef CONFIG_DEBUG_LOCKDEP
399 * We cannot printk in early bootup code. Not even early_printk()
400 * might work. So we mark any initialization errors and printk
401 * about it later on, in lockdep_info().
403 static int lockdep_init_error;
404 static unsigned long lockdep_init_trace_data[20];
405 static struct stack_trace lockdep_init_trace = {
406 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
407 .entries = lockdep_init_trace_data,
411 * Various lockdep statistics:
413 atomic_t chain_lookup_hits;
414 atomic_t chain_lookup_misses;
415 atomic_t hardirqs_on_events;
416 atomic_t hardirqs_off_events;
417 atomic_t redundant_hardirqs_on;
418 atomic_t redundant_hardirqs_off;
419 atomic_t softirqs_on_events;
420 atomic_t softirqs_off_events;
421 atomic_t redundant_softirqs_on;
422 atomic_t redundant_softirqs_off;
423 atomic_t nr_unused_locks;
424 atomic_t nr_cyclic_checks;
425 atomic_t nr_cyclic_check_recursions;
426 atomic_t nr_find_usage_forwards_checks;
427 atomic_t nr_find_usage_forwards_recursions;
428 atomic_t nr_find_usage_backwards_checks;
429 atomic_t nr_find_usage_backwards_recursions;
430 # define debug_atomic_inc(ptr) atomic_inc(ptr)
431 # define debug_atomic_dec(ptr) atomic_dec(ptr)
432 # define debug_atomic_read(ptr) atomic_read(ptr)
434 # define debug_atomic_inc(ptr) do { } while (0)
435 # define debug_atomic_dec(ptr) do { } while (0)
436 # define debug_atomic_read(ptr) 0
443 static const char *usage_str[] =
445 [LOCK_USED] = "initial-use ",
446 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
447 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
448 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
449 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
450 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
451 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
452 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
453 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
456 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
458 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
462 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
464 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
466 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
469 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
472 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
475 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
478 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
480 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
482 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
486 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
488 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
490 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
495 static void print_lock_name(struct lock_class *class)
497 char str[KSYM_NAME_LEN], c1, c2, c3, c4;
500 get_usage_chars(class, &c1, &c2, &c3, &c4);
504 name = __get_key_name(class->key, str);
505 printk(" (%s", name);
507 printk(" (%s", name);
508 if (class->name_version > 1)
509 printk("#%d", class->name_version);
511 printk("/%d", class->subclass);
513 printk("){%c%c%c%c}", c1, c2, c3, c4);
516 static void print_lockdep_cache(struct lockdep_map *lock)
519 char str[KSYM_NAME_LEN];
523 name = __get_key_name(lock->key->subkeys, str);
528 static void print_lock(struct held_lock *hlock)
530 print_lock_name(hlock_class(hlock));
532 print_ip_sym(hlock->acquire_ip);
535 static void lockdep_print_held_locks(struct task_struct *curr)
537 int i, depth = curr->lockdep_depth;
540 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
543 printk("%d lock%s held by %s/%d:\n",
544 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
546 for (i = 0; i < depth; i++) {
548 print_lock(curr->held_locks + i);
552 static void print_lock_class_header(struct lock_class *class, int depth)
556 printk("%*s->", depth, "");
557 print_lock_name(class);
558 printk(" ops: %lu", class->ops);
561 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
562 if (class->usage_mask & (1 << bit)) {
565 len += printk("%*s %s", depth, "", usage_str[bit]);
566 len += printk(" at:\n");
567 print_stack_trace(class->usage_traces + bit, len);
570 printk("%*s }\n", depth, "");
572 printk("%*s ... key at: ",depth,"");
573 print_ip_sym((unsigned long)class->key);
577 * printk all lock dependencies starting at <entry>:
579 static void print_lock_dependencies(struct lock_class *class, int depth)
581 struct lock_list *entry;
583 if (lockdep_dependency_visit(class, depth))
586 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
589 print_lock_class_header(class, depth);
591 list_for_each_entry(entry, &class->locks_after, entry) {
592 if (DEBUG_LOCKS_WARN_ON(!entry->class))
595 print_lock_dependencies(entry->class, depth + 1);
597 printk("%*s ... acquired at:\n",depth,"");
598 print_stack_trace(&entry->trace, 2);
603 static void print_kernel_version(void)
605 printk("%s %.*s\n", init_utsname()->release,
606 (int)strcspn(init_utsname()->version, " "),
607 init_utsname()->version);
610 static int very_verbose(struct lock_class *class)
613 return class_filter(class);
619 * Is this the address of a static object:
621 static int static_obj(void *obj)
623 unsigned long start = (unsigned long) &_stext,
624 end = (unsigned long) &_end,
625 addr = (unsigned long) obj;
633 if ((addr >= start) && (addr < end))
640 for_each_possible_cpu(i) {
641 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
642 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
645 if ((addr >= start) && (addr < end))
653 return is_module_address(addr);
657 * To make lock name printouts unique, we calculate a unique
658 * class->name_version generation counter:
660 static int count_matching_names(struct lock_class *new_class)
662 struct lock_class *class;
665 if (!new_class->name)
668 list_for_each_entry(class, &all_lock_classes, lock_entry) {
669 if (new_class->key - new_class->subclass == class->key)
670 return class->name_version;
671 if (class->name && !strcmp(class->name, new_class->name))
672 count = max(count, class->name_version);
679 * Register a lock's class in the hash-table, if the class is not present
680 * yet. Otherwise we look it up. We cache the result in the lock object
681 * itself, so actual lookup of the hash should be once per lock object.
683 static inline struct lock_class *
684 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
686 struct lockdep_subclass_key *key;
687 struct list_head *hash_head;
688 struct lock_class *class;
690 #ifdef CONFIG_DEBUG_LOCKDEP
692 * If the architecture calls into lockdep before initializing
693 * the hashes then we'll warn about it later. (we cannot printk
696 if (unlikely(!lockdep_initialized)) {
698 lockdep_init_error = 1;
699 save_stack_trace(&lockdep_init_trace);
704 * Static locks do not have their class-keys yet - for them the key
705 * is the lock object itself:
707 if (unlikely(!lock->key))
708 lock->key = (void *)lock;
711 * NOTE: the class-key must be unique. For dynamic locks, a static
712 * lock_class_key variable is passed in through the mutex_init()
713 * (or spin_lock_init()) call - which acts as the key. For static
714 * locks we use the lock object itself as the key.
716 BUILD_BUG_ON(sizeof(struct lock_class_key) >
717 sizeof(struct lockdep_map));
719 key = lock->key->subkeys + subclass;
721 hash_head = classhashentry(key);
724 * We can walk the hash lockfree, because the hash only
725 * grows, and we are careful when adding entries to the end:
727 list_for_each_entry(class, hash_head, hash_entry) {
728 if (class->key == key) {
729 WARN_ON_ONCE(class->name != lock->name);
738 * Register a lock's class in the hash-table, if the class is not present
739 * yet. Otherwise we look it up. We cache the result in the lock object
740 * itself, so actual lookup of the hash should be once per lock object.
742 static inline struct lock_class *
743 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
745 struct lockdep_subclass_key *key;
746 struct list_head *hash_head;
747 struct lock_class *class;
750 class = look_up_lock_class(lock, subclass);
755 * Debug-check: all keys must be persistent!
757 if (!static_obj(lock->key)) {
759 printk("INFO: trying to register non-static key.\n");
760 printk("the code is fine but needs lockdep annotation.\n");
761 printk("turning off the locking correctness validator.\n");
767 key = lock->key->subkeys + subclass;
768 hash_head = classhashentry(key);
770 raw_local_irq_save(flags);
772 raw_local_irq_restore(flags);
776 * We have to do the hash-walk again, to avoid races
779 list_for_each_entry(class, hash_head, hash_entry)
780 if (class->key == key)
783 * Allocate a new key from the static array, and add it to
786 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
787 if (!debug_locks_off_graph_unlock()) {
788 raw_local_irq_restore(flags);
791 raw_local_irq_restore(flags);
793 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
794 printk("turning off the locking correctness validator.\n");
797 class = lock_classes + nr_lock_classes++;
798 debug_atomic_inc(&nr_unused_locks);
800 class->name = lock->name;
801 class->subclass = subclass;
802 INIT_LIST_HEAD(&class->lock_entry);
803 INIT_LIST_HEAD(&class->locks_before);
804 INIT_LIST_HEAD(&class->locks_after);
805 class->name_version = count_matching_names(class);
807 * We use RCU's safe list-add method to make
808 * parallel walking of the hash-list safe:
810 list_add_tail_rcu(&class->hash_entry, hash_head);
812 * Add it to the global list of classes:
814 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
816 if (verbose(class)) {
818 raw_local_irq_restore(flags);
820 printk("\nnew class %p: %s", class->key, class->name);
821 if (class->name_version > 1)
822 printk("#%d", class->name_version);
826 raw_local_irq_save(flags);
828 raw_local_irq_restore(flags);
834 raw_local_irq_restore(flags);
836 if (!subclass || force)
837 lock->class_cache = class;
839 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
845 #ifdef CONFIG_PROVE_LOCKING
847 * Allocate a lockdep entry. (assumes the graph_lock held, returns
848 * with NULL on failure)
850 static struct lock_list *alloc_list_entry(void)
852 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
853 if (!debug_locks_off_graph_unlock())
856 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
857 printk("turning off the locking correctness validator.\n");
860 return list_entries + nr_list_entries++;
864 * Add a new dependency to the head of the list:
866 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
867 struct list_head *head, unsigned long ip, int distance)
869 struct lock_list *entry;
871 * Lock not present yet - get a new dependency struct and
872 * add it to the list:
874 entry = alloc_list_entry();
878 if (!save_trace(&entry->trace))
882 entry->distance = distance;
884 * Since we never remove from the dependency list, the list can
885 * be walked lockless by other CPUs, it's only allocation
886 * that must be protected by the spinlock. But this also means
887 * we must make new entries visible only once writes to the
888 * entry become visible - hence the RCU op:
890 list_add_tail_rcu(&entry->entry, head);
896 * Recursive, forwards-direction lock-dependency checking, used for
897 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
900 * (to keep the stackframe of the recursive functions small we
901 * use these global variables, and we also mark various helper
902 * functions as noinline.)
904 static struct held_lock *check_source, *check_target;
907 * Print a dependency chain entry (this is only done when a deadlock
908 * has been detected):
911 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
913 if (debug_locks_silent)
915 printk("\n-> #%u", depth);
916 print_lock_name(target->class);
918 print_stack_trace(&target->trace, 6);
924 * When a circular dependency is detected, print the
928 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
930 struct task_struct *curr = current;
932 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
935 printk("\n=======================================================\n");
936 printk( "[ INFO: possible circular locking dependency detected ]\n");
937 print_kernel_version();
938 printk( "-------------------------------------------------------\n");
939 printk("%s/%d is trying to acquire lock:\n",
940 curr->comm, task_pid_nr(curr));
941 print_lock(check_source);
942 printk("\nbut task is already holding lock:\n");
943 print_lock(check_target);
944 printk("\nwhich lock already depends on the new lock.\n\n");
945 printk("\nthe existing dependency chain (in reverse order) is:\n");
947 print_circular_bug_entry(entry, depth);
952 static noinline int print_circular_bug_tail(void)
954 struct task_struct *curr = current;
955 struct lock_list this;
957 if (debug_locks_silent)
960 this.class = hlock_class(check_source);
961 if (!save_trace(&this.trace))
964 print_circular_bug_entry(&this, 0);
966 printk("\nother info that might help us debug this:\n\n");
967 lockdep_print_held_locks(curr);
969 printk("\nstack backtrace:\n");
975 #define RECURSION_LIMIT 40
977 static int noinline print_infinite_recursion_bug(void)
979 if (!debug_locks_off_graph_unlock())
987 unsigned long __lockdep_count_forward_deps(struct lock_class *class,
990 struct lock_list *entry;
991 unsigned long ret = 1;
993 if (lockdep_dependency_visit(class, depth))
997 * Recurse this class's dependency list:
999 list_for_each_entry(entry, &class->locks_after, entry)
1000 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1005 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1007 unsigned long ret, flags;
1009 local_irq_save(flags);
1010 __raw_spin_lock(&lockdep_lock);
1011 ret = __lockdep_count_forward_deps(class, 0);
1012 __raw_spin_unlock(&lockdep_lock);
1013 local_irq_restore(flags);
1018 unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1021 struct lock_list *entry;
1022 unsigned long ret = 1;
1024 if (lockdep_dependency_visit(class, depth))
1027 * Recurse this class's dependency list:
1029 list_for_each_entry(entry, &class->locks_before, entry)
1030 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1035 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1037 unsigned long ret, flags;
1039 local_irq_save(flags);
1040 __raw_spin_lock(&lockdep_lock);
1041 ret = __lockdep_count_backward_deps(class, 0);
1042 __raw_spin_unlock(&lockdep_lock);
1043 local_irq_restore(flags);
1049 * Prove that the dependency graph starting at <entry> can not
1050 * lead to <target>. Print an error and return 0 if it does.
1053 check_noncircular(struct lock_class *source, unsigned int depth)
1055 struct lock_list *entry;
1057 if (lockdep_dependency_visit(source, depth))
1060 debug_atomic_inc(&nr_cyclic_check_recursions);
1061 if (depth > max_recursion_depth)
1062 max_recursion_depth = depth;
1063 if (depth >= RECURSION_LIMIT)
1064 return print_infinite_recursion_bug();
1066 * Check this lock's dependency list:
1068 list_for_each_entry(entry, &source->locks_after, entry) {
1069 if (entry->class == hlock_class(check_target))
1070 return print_circular_bug_header(entry, depth+1);
1071 debug_atomic_inc(&nr_cyclic_checks);
1072 if (!check_noncircular(entry->class, depth+1))
1073 return print_circular_bug_entry(entry, depth+1);
1078 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1080 * Forwards and backwards subgraph searching, for the purposes of
1081 * proving that two subgraphs can be connected by a new dependency
1082 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1084 static enum lock_usage_bit find_usage_bit;
1085 static struct lock_class *forwards_match, *backwards_match;
1088 * Find a node in the forwards-direction dependency sub-graph starting
1089 * at <source> that matches <find_usage_bit>.
1091 * Return 2 if such a node exists in the subgraph, and put that node
1092 * into <forwards_match>.
1094 * Return 1 otherwise and keep <forwards_match> unchanged.
1095 * Return 0 on error.
1098 find_usage_forwards(struct lock_class *source, unsigned int depth)
1100 struct lock_list *entry;
1103 if (lockdep_dependency_visit(source, depth))
1106 if (depth > max_recursion_depth)
1107 max_recursion_depth = depth;
1108 if (depth >= RECURSION_LIMIT)
1109 return print_infinite_recursion_bug();
1111 debug_atomic_inc(&nr_find_usage_forwards_checks);
1112 if (source->usage_mask & (1 << find_usage_bit)) {
1113 forwards_match = source;
1118 * Check this lock's dependency list:
1120 list_for_each_entry(entry, &source->locks_after, entry) {
1121 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1122 ret = find_usage_forwards(entry->class, depth+1);
1123 if (ret == 2 || ret == 0)
1130 * Find a node in the backwards-direction dependency sub-graph starting
1131 * at <source> that matches <find_usage_bit>.
1133 * Return 2 if such a node exists in the subgraph, and put that node
1134 * into <backwards_match>.
1136 * Return 1 otherwise and keep <backwards_match> unchanged.
1137 * Return 0 on error.
1140 find_usage_backwards(struct lock_class *source, unsigned int depth)
1142 struct lock_list *entry;
1145 if (lockdep_dependency_visit(source, depth))
1148 if (!__raw_spin_is_locked(&lockdep_lock))
1149 return DEBUG_LOCKS_WARN_ON(1);
1151 if (depth > max_recursion_depth)
1152 max_recursion_depth = depth;
1153 if (depth >= RECURSION_LIMIT)
1154 return print_infinite_recursion_bug();
1156 debug_atomic_inc(&nr_find_usage_backwards_checks);
1157 if (source->usage_mask & (1 << find_usage_bit)) {
1158 backwards_match = source;
1162 if (!source && debug_locks_off_graph_unlock()) {
1168 * Check this lock's dependency list:
1170 list_for_each_entry(entry, &source->locks_before, entry) {
1171 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1172 ret = find_usage_backwards(entry->class, depth+1);
1173 if (ret == 2 || ret == 0)
1180 print_bad_irq_dependency(struct task_struct *curr,
1181 struct held_lock *prev,
1182 struct held_lock *next,
1183 enum lock_usage_bit bit1,
1184 enum lock_usage_bit bit2,
1185 const char *irqclass)
1187 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1190 printk("\n======================================================\n");
1191 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1192 irqclass, irqclass);
1193 print_kernel_version();
1194 printk( "------------------------------------------------------\n");
1195 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1196 curr->comm, task_pid_nr(curr),
1197 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1198 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1199 curr->hardirqs_enabled,
1200 curr->softirqs_enabled);
1203 printk("\nand this task is already holding:\n");
1205 printk("which would create a new lock dependency:\n");
1206 print_lock_name(hlock_class(prev));
1208 print_lock_name(hlock_class(next));
1211 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1213 print_lock_name(backwards_match);
1214 printk("\n... which became %s-irq-safe at:\n", irqclass);
1216 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1218 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1219 print_lock_name(forwards_match);
1220 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1223 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1225 printk("\nother info that might help us debug this:\n\n");
1226 lockdep_print_held_locks(curr);
1228 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1229 print_lock_dependencies(backwards_match, 0);
1231 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1232 print_lock_dependencies(forwards_match, 0);
1234 printk("\nstack backtrace:\n");
1241 check_usage(struct task_struct *curr, struct held_lock *prev,
1242 struct held_lock *next, enum lock_usage_bit bit_backwards,
1243 enum lock_usage_bit bit_forwards, const char *irqclass)
1247 find_usage_bit = bit_backwards;
1248 /* fills in <backwards_match> */
1249 ret = find_usage_backwards(hlock_class(prev), 0);
1250 if (!ret || ret == 1)
1253 find_usage_bit = bit_forwards;
1254 ret = find_usage_forwards(hlock_class(next), 0);
1255 if (!ret || ret == 1)
1258 return print_bad_irq_dependency(curr, prev, next,
1259 bit_backwards, bit_forwards, irqclass);
1263 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1264 struct held_lock *next)
1267 * Prove that the new dependency does not connect a hardirq-safe
1268 * lock with a hardirq-unsafe lock - to achieve this we search
1269 * the backwards-subgraph starting at <prev>, and the
1270 * forwards-subgraph starting at <next>:
1272 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
1273 LOCK_ENABLED_HARDIRQS, "hard"))
1277 * Prove that the new dependency does not connect a hardirq-safe-read
1278 * lock with a hardirq-unsafe lock - to achieve this we search
1279 * the backwards-subgraph starting at <prev>, and the
1280 * forwards-subgraph starting at <next>:
1282 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
1283 LOCK_ENABLED_HARDIRQS, "hard-read"))
1287 * Prove that the new dependency does not connect a softirq-safe
1288 * lock with a softirq-unsafe lock - to achieve this we search
1289 * the backwards-subgraph starting at <prev>, and the
1290 * forwards-subgraph starting at <next>:
1292 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
1293 LOCK_ENABLED_SOFTIRQS, "soft"))
1296 * Prove that the new dependency does not connect a softirq-safe-read
1297 * lock with a softirq-unsafe lock - to achieve this we search
1298 * the backwards-subgraph starting at <prev>, and the
1299 * forwards-subgraph starting at <next>:
1301 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1302 LOCK_ENABLED_SOFTIRQS, "soft"))
1308 static void inc_chains(void)
1310 if (current->hardirq_context)
1311 nr_hardirq_chains++;
1313 if (current->softirq_context)
1314 nr_softirq_chains++;
1316 nr_process_chains++;
1323 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1324 struct held_lock *next)
1329 static inline void inc_chains(void)
1331 nr_process_chains++;
1337 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1338 struct held_lock *next)
1340 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1343 printk("\n=============================================\n");
1344 printk( "[ INFO: possible recursive locking detected ]\n");
1345 print_kernel_version();
1346 printk( "---------------------------------------------\n");
1347 printk("%s/%d is trying to acquire lock:\n",
1348 curr->comm, task_pid_nr(curr));
1350 printk("\nbut task is already holding lock:\n");
1353 printk("\nother info that might help us debug this:\n");
1354 lockdep_print_held_locks(curr);
1356 printk("\nstack backtrace:\n");
1363 * Check whether we are holding such a class already.
1365 * (Note that this has to be done separately, because the graph cannot
1366 * detect such classes of deadlocks.)
1368 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1371 check_deadlock(struct task_struct *curr, struct held_lock *next,
1372 struct lockdep_map *next_instance, int read)
1374 struct held_lock *prev;
1375 struct held_lock *nest = NULL;
1378 for (i = 0; i < curr->lockdep_depth; i++) {
1379 prev = curr->held_locks + i;
1381 if (prev->instance == next->nest_lock)
1384 if (hlock_class(prev) != hlock_class(next))
1388 * Allow read-after-read recursion of the same
1389 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1391 if ((read == 2) && prev->read)
1395 * We're holding the nest_lock, which serializes this lock's
1396 * nesting behaviour.
1401 return print_deadlock_bug(curr, prev, next);
1407 * There was a chain-cache miss, and we are about to add a new dependency
1408 * to a previous lock. We recursively validate the following rules:
1410 * - would the adding of the <prev> -> <next> dependency create a
1411 * circular dependency in the graph? [== circular deadlock]
1413 * - does the new prev->next dependency connect any hardirq-safe lock
1414 * (in the full backwards-subgraph starting at <prev>) with any
1415 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1416 * <next>)? [== illegal lock inversion with hardirq contexts]
1418 * - does the new prev->next dependency connect any softirq-safe lock
1419 * (in the full backwards-subgraph starting at <prev>) with any
1420 * softirq-unsafe lock (in the full forwards-subgraph starting at
1421 * <next>)? [== illegal lock inversion with softirq contexts]
1423 * any of these scenarios could lead to a deadlock.
1425 * Then if all the validations pass, we add the forwards and backwards
1429 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1430 struct held_lock *next, int distance)
1432 struct lock_list *entry;
1436 * Prove that the new <prev> -> <next> dependency would not
1437 * create a circular dependency in the graph. (We do this by
1438 * forward-recursing into the graph starting at <next>, and
1439 * checking whether we can reach <prev>.)
1441 * We are using global variables to control the recursion, to
1442 * keep the stackframe size of the recursive functions low:
1444 check_source = next;
1445 check_target = prev;
1446 if (!(check_noncircular(hlock_class(next), 0)))
1447 return print_circular_bug_tail();
1449 if (!check_prev_add_irq(curr, prev, next))
1453 * For recursive read-locks we do all the dependency checks,
1454 * but we dont store read-triggered dependencies (only
1455 * write-triggered dependencies). This ensures that only the
1456 * write-side dependencies matter, and that if for example a
1457 * write-lock never takes any other locks, then the reads are
1458 * equivalent to a NOP.
1460 if (next->read == 2 || prev->read == 2)
1463 * Is the <prev> -> <next> dependency already present?
1465 * (this may occur even though this is a new chain: consider
1466 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1467 * chains - the second one will be new, but L1 already has
1468 * L2 added to its dependency list, due to the first chain.)
1470 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1471 if (entry->class == hlock_class(next)) {
1473 entry->distance = 1;
1479 * Ok, all validations passed, add the new lock
1480 * to the previous lock's dependency list:
1482 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1483 &hlock_class(prev)->locks_after,
1484 next->acquire_ip, distance);
1489 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1490 &hlock_class(next)->locks_before,
1491 next->acquire_ip, distance);
1496 * Debugging printouts:
1498 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1500 printk("\n new dependency: ");
1501 print_lock_name(hlock_class(prev));
1503 print_lock_name(hlock_class(next));
1506 return graph_lock();
1512 * Add the dependency to all directly-previous locks that are 'relevant'.
1513 * The ones that are relevant are (in increasing distance from curr):
1514 * all consecutive trylock entries and the final non-trylock entry - or
1515 * the end of this context's lock-chain - whichever comes first.
1518 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1520 int depth = curr->lockdep_depth;
1521 struct held_lock *hlock;
1526 * Depth must not be zero for a non-head lock:
1531 * At least two relevant locks must exist for this
1534 if (curr->held_locks[depth].irq_context !=
1535 curr->held_locks[depth-1].irq_context)
1539 int distance = curr->lockdep_depth - depth + 1;
1540 hlock = curr->held_locks + depth-1;
1542 * Only non-recursive-read entries get new dependencies
1545 if (hlock->read != 2) {
1546 if (!check_prev_add(curr, hlock, next, distance))
1549 * Stop after the first non-trylock entry,
1550 * as non-trylock entries have added their
1551 * own direct dependencies already, so this
1552 * lock is connected to them indirectly:
1554 if (!hlock->trylock)
1559 * End of lock-stack?
1564 * Stop the search if we cross into another context:
1566 if (curr->held_locks[depth].irq_context !=
1567 curr->held_locks[depth-1].irq_context)
1572 if (!debug_locks_off_graph_unlock())
1580 unsigned long nr_lock_chains;
1581 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1582 int nr_chain_hlocks;
1583 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1585 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1587 return lock_classes + chain_hlocks[chain->base + i];
1591 * Look up a dependency chain. If the key is not present yet then
1592 * add it and return 1 - in this case the new dependency chain is
1593 * validated. If the key is already hashed, return 0.
1594 * (On return with 1 graph_lock is held.)
1596 static inline int lookup_chain_cache(struct task_struct *curr,
1597 struct held_lock *hlock,
1600 struct lock_class *class = hlock_class(hlock);
1601 struct list_head *hash_head = chainhashentry(chain_key);
1602 struct lock_chain *chain;
1603 struct held_lock *hlock_curr, *hlock_next;
1606 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1609 * We can walk it lock-free, because entries only get added
1612 list_for_each_entry(chain, hash_head, entry) {
1613 if (chain->chain_key == chain_key) {
1615 debug_atomic_inc(&chain_lookup_hits);
1616 if (very_verbose(class))
1617 printk("\nhash chain already cached, key: "
1618 "%016Lx tail class: [%p] %s\n",
1619 (unsigned long long)chain_key,
1620 class->key, class->name);
1624 if (very_verbose(class))
1625 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1626 (unsigned long long)chain_key, class->key, class->name);
1628 * Allocate a new chain entry from the static array, and add
1634 * We have to walk the chain again locked - to avoid duplicates:
1636 list_for_each_entry(chain, hash_head, entry) {
1637 if (chain->chain_key == chain_key) {
1642 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1643 if (!debug_locks_off_graph_unlock())
1646 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1647 printk("turning off the locking correctness validator.\n");
1650 chain = lock_chains + nr_lock_chains++;
1651 chain->chain_key = chain_key;
1652 chain->irq_context = hlock->irq_context;
1653 /* Find the first held_lock of current chain */
1655 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1656 hlock_curr = curr->held_locks + i;
1657 if (hlock_curr->irq_context != hlock_next->irq_context)
1662 chain->depth = curr->lockdep_depth + 1 - i;
1663 cn = nr_chain_hlocks;
1664 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1665 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1670 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1672 for (j = 0; j < chain->depth - 1; j++, i++) {
1673 int lock_id = curr->held_locks[i].class_idx - 1;
1674 chain_hlocks[chain->base + j] = lock_id;
1676 chain_hlocks[chain->base + j] = class - lock_classes;
1678 list_add_tail_rcu(&chain->entry, hash_head);
1679 debug_atomic_inc(&chain_lookup_misses);
1685 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1686 struct held_lock *hlock, int chain_head, u64 chain_key)
1689 * Trylock needs to maintain the stack of held locks, but it
1690 * does not add new dependencies, because trylock can be done
1693 * We look up the chain_key and do the O(N^2) check and update of
1694 * the dependencies only if this is a new dependency chain.
1695 * (If lookup_chain_cache() returns with 1 it acquires
1696 * graph_lock for us)
1698 if (!hlock->trylock && (hlock->check == 2) &&
1699 lookup_chain_cache(curr, hlock, chain_key)) {
1701 * Check whether last held lock:
1703 * - is irq-safe, if this lock is irq-unsafe
1704 * - is softirq-safe, if this lock is hardirq-unsafe
1706 * And check whether the new lock's dependency graph
1707 * could lead back to the previous lock.
1709 * any of these scenarios could lead to a deadlock. If
1712 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1717 * Mark recursive read, as we jump over it when
1718 * building dependencies (just like we jump over
1724 * Add dependency only if this lock is not the head
1725 * of the chain, and if it's not a secondary read-lock:
1727 if (!chain_head && ret != 2)
1728 if (!check_prevs_add(curr, hlock))
1732 /* after lookup_chain_cache(): */
1733 if (unlikely(!debug_locks))
1739 static inline int validate_chain(struct task_struct *curr,
1740 struct lockdep_map *lock, struct held_lock *hlock,
1741 int chain_head, u64 chain_key)
1748 * We are building curr_chain_key incrementally, so double-check
1749 * it from scratch, to make sure that it's done correctly:
1751 static void check_chain_key(struct task_struct *curr)
1753 #ifdef CONFIG_DEBUG_LOCKDEP
1754 struct held_lock *hlock, *prev_hlock = NULL;
1758 for (i = 0; i < curr->lockdep_depth; i++) {
1759 hlock = curr->held_locks + i;
1760 if (chain_key != hlock->prev_chain_key) {
1762 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1763 curr->lockdep_depth, i,
1764 (unsigned long long)chain_key,
1765 (unsigned long long)hlock->prev_chain_key);
1768 id = hlock->class_idx - 1;
1769 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1772 if (prev_hlock && (prev_hlock->irq_context !=
1773 hlock->irq_context))
1775 chain_key = iterate_chain_key(chain_key, id);
1778 if (chain_key != curr->curr_chain_key) {
1780 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1781 curr->lockdep_depth, i,
1782 (unsigned long long)chain_key,
1783 (unsigned long long)curr->curr_chain_key);
1789 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1790 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1792 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1795 printk("\n=================================\n");
1796 printk( "[ INFO: inconsistent lock state ]\n");
1797 print_kernel_version();
1798 printk( "---------------------------------\n");
1800 printk("inconsistent {%s} -> {%s} usage.\n",
1801 usage_str[prev_bit], usage_str[new_bit]);
1803 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1804 curr->comm, task_pid_nr(curr),
1805 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1806 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1807 trace_hardirqs_enabled(curr),
1808 trace_softirqs_enabled(curr));
1811 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1812 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
1814 print_irqtrace_events(curr);
1815 printk("\nother info that might help us debug this:\n");
1816 lockdep_print_held_locks(curr);
1818 printk("\nstack backtrace:\n");
1825 * Print out an error if an invalid bit is set:
1828 valid_state(struct task_struct *curr, struct held_lock *this,
1829 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1831 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
1832 return print_usage_bug(curr, this, bad_bit, new_bit);
1836 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1837 enum lock_usage_bit new_bit);
1839 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1842 * print irq inversion bug:
1845 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1846 struct held_lock *this, int forwards,
1847 const char *irqclass)
1849 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1852 printk("\n=========================================================\n");
1853 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1854 print_kernel_version();
1855 printk( "---------------------------------------------------------\n");
1856 printk("%s/%d just changed the state of lock:\n",
1857 curr->comm, task_pid_nr(curr));
1860 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1862 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1863 print_lock_name(other);
1864 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1866 printk("\nother info that might help us debug this:\n");
1867 lockdep_print_held_locks(curr);
1869 printk("\nthe first lock's dependencies:\n");
1870 print_lock_dependencies(hlock_class(this), 0);
1872 printk("\nthe second lock's dependencies:\n");
1873 print_lock_dependencies(other, 0);
1875 printk("\nstack backtrace:\n");
1882 * Prove that in the forwards-direction subgraph starting at <this>
1883 * there is no lock matching <mask>:
1886 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1887 enum lock_usage_bit bit, const char *irqclass)
1891 find_usage_bit = bit;
1892 /* fills in <forwards_match> */
1893 ret = find_usage_forwards(hlock_class(this), 0);
1894 if (!ret || ret == 1)
1897 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1901 * Prove that in the backwards-direction subgraph starting at <this>
1902 * there is no lock matching <mask>:
1905 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1906 enum lock_usage_bit bit, const char *irqclass)
1910 find_usage_bit = bit;
1911 /* fills in <backwards_match> */
1912 ret = find_usage_backwards(hlock_class(this), 0);
1913 if (!ret || ret == 1)
1916 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1919 void print_irqtrace_events(struct task_struct *curr)
1921 printk("irq event stamp: %u\n", curr->irq_events);
1922 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1923 print_ip_sym(curr->hardirq_enable_ip);
1924 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1925 print_ip_sym(curr->hardirq_disable_ip);
1926 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1927 print_ip_sym(curr->softirq_enable_ip);
1928 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1929 print_ip_sym(curr->softirq_disable_ip);
1932 static int hardirq_verbose(struct lock_class *class)
1935 return class_filter(class);
1940 static int softirq_verbose(struct lock_class *class)
1943 return class_filter(class);
1948 #define STRICT_READ_CHECKS 1
1950 static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
1951 enum lock_usage_bit new_bit)
1956 case LOCK_USED_IN_HARDIRQ:
1957 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1959 if (!valid_state(curr, this, new_bit,
1960 LOCK_ENABLED_HARDIRQS_READ))
1963 * just marked it hardirq-safe, check that this lock
1964 * took no hardirq-unsafe lock in the past:
1966 if (!check_usage_forwards(curr, this,
1967 LOCK_ENABLED_HARDIRQS, "hard"))
1969 #if STRICT_READ_CHECKS
1971 * just marked it hardirq-safe, check that this lock
1972 * took no hardirq-unsafe-read lock in the past:
1974 if (!check_usage_forwards(curr, this,
1975 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1978 if (hardirq_verbose(hlock_class(this)))
1981 case LOCK_USED_IN_SOFTIRQ:
1982 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1984 if (!valid_state(curr, this, new_bit,
1985 LOCK_ENABLED_SOFTIRQS_READ))
1988 * just marked it softirq-safe, check that this lock
1989 * took no softirq-unsafe lock in the past:
1991 if (!check_usage_forwards(curr, this,
1992 LOCK_ENABLED_SOFTIRQS, "soft"))
1994 #if STRICT_READ_CHECKS
1996 * just marked it softirq-safe, check that this lock
1997 * took no softirq-unsafe-read lock in the past:
1999 if (!check_usage_forwards(curr, this,
2000 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
2003 if (softirq_verbose(hlock_class(this)))
2006 case LOCK_USED_IN_HARDIRQ_READ:
2007 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
2010 * just marked it hardirq-read-safe, check that this lock
2011 * took no hardirq-unsafe lock in the past:
2013 if (!check_usage_forwards(curr, this,
2014 LOCK_ENABLED_HARDIRQS, "hard"))
2016 if (hardirq_verbose(hlock_class(this)))
2019 case LOCK_USED_IN_SOFTIRQ_READ:
2020 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
2023 * just marked it softirq-read-safe, check that this lock
2024 * took no softirq-unsafe lock in the past:
2026 if (!check_usage_forwards(curr, this,
2027 LOCK_ENABLED_SOFTIRQS, "soft"))
2029 if (softirq_verbose(hlock_class(this)))
2032 case LOCK_ENABLED_HARDIRQS:
2033 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2035 if (!valid_state(curr, this, new_bit,
2036 LOCK_USED_IN_HARDIRQ_READ))
2039 * just marked it hardirq-unsafe, check that no hardirq-safe
2040 * lock in the system ever took it in the past:
2042 if (!check_usage_backwards(curr, this,
2043 LOCK_USED_IN_HARDIRQ, "hard"))
2045 #if STRICT_READ_CHECKS
2047 * just marked it hardirq-unsafe, check that no
2048 * hardirq-safe-read lock in the system ever took
2051 if (!check_usage_backwards(curr, this,
2052 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
2055 if (hardirq_verbose(hlock_class(this)))
2058 case LOCK_ENABLED_SOFTIRQS:
2059 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2061 if (!valid_state(curr, this, new_bit,
2062 LOCK_USED_IN_SOFTIRQ_READ))
2065 * just marked it softirq-unsafe, check that no softirq-safe
2066 * lock in the system ever took it in the past:
2068 if (!check_usage_backwards(curr, this,
2069 LOCK_USED_IN_SOFTIRQ, "soft"))
2071 #if STRICT_READ_CHECKS
2073 * just marked it softirq-unsafe, check that no
2074 * softirq-safe-read lock in the system ever took
2077 if (!check_usage_backwards(curr, this,
2078 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
2081 if (softirq_verbose(hlock_class(this)))
2084 case LOCK_ENABLED_HARDIRQS_READ:
2085 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2087 #if STRICT_READ_CHECKS
2089 * just marked it hardirq-read-unsafe, check that no
2090 * hardirq-safe lock in the system ever took it in the past:
2092 if (!check_usage_backwards(curr, this,
2093 LOCK_USED_IN_HARDIRQ, "hard"))
2096 if (hardirq_verbose(hlock_class(this)))
2099 case LOCK_ENABLED_SOFTIRQS_READ:
2100 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2102 #if STRICT_READ_CHECKS
2104 * just marked it softirq-read-unsafe, check that no
2105 * softirq-safe lock in the system ever took it in the past:
2107 if (!check_usage_backwards(curr, this,
2108 LOCK_USED_IN_SOFTIRQ, "soft"))
2111 if (softirq_verbose(hlock_class(this)))
2123 * Mark all held locks with a usage bit:
2126 mark_held_locks(struct task_struct *curr, int hardirq)
2128 enum lock_usage_bit usage_bit;
2129 struct held_lock *hlock;
2132 for (i = 0; i < curr->lockdep_depth; i++) {
2133 hlock = curr->held_locks + i;
2137 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
2139 usage_bit = LOCK_ENABLED_HARDIRQS;
2142 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
2144 usage_bit = LOCK_ENABLED_SOFTIRQS;
2146 if (!mark_lock(curr, hlock, usage_bit))
2154 * Debugging helper: via this flag we know that we are in
2155 * 'early bootup code', and will warn about any invalid irqs-on event:
2157 static int early_boot_irqs_enabled;
2159 void early_boot_irqs_off(void)
2161 early_boot_irqs_enabled = 0;
2164 void early_boot_irqs_on(void)
2166 early_boot_irqs_enabled = 1;
2170 * Hardirqs will be enabled:
2172 void trace_hardirqs_on_caller(unsigned long ip)
2174 struct task_struct *curr = current;
2176 time_hardirqs_on(CALLER_ADDR0, ip);
2178 if (unlikely(!debug_locks || current->lockdep_recursion))
2181 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2184 if (unlikely(curr->hardirqs_enabled)) {
2185 debug_atomic_inc(&redundant_hardirqs_on);
2188 /* we'll do an OFF -> ON transition: */
2189 curr->hardirqs_enabled = 1;
2191 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2193 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2196 * We are going to turn hardirqs on, so set the
2197 * usage bit for all held locks:
2199 if (!mark_held_locks(curr, 1))
2202 * If we have softirqs enabled, then set the usage
2203 * bit for all held locks. (disabled hardirqs prevented
2204 * this bit from being set before)
2206 if (curr->softirqs_enabled)
2207 if (!mark_held_locks(curr, 0))
2210 curr->hardirq_enable_ip = ip;
2211 curr->hardirq_enable_event = ++curr->irq_events;
2212 debug_atomic_inc(&hardirqs_on_events);
2214 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2216 void trace_hardirqs_on(void)
2218 trace_hardirqs_on_caller(CALLER_ADDR0);
2220 EXPORT_SYMBOL(trace_hardirqs_on);
2223 * Hardirqs were disabled:
2225 void trace_hardirqs_off_caller(unsigned long ip)
2227 struct task_struct *curr = current;
2229 time_hardirqs_off(CALLER_ADDR0, ip);
2231 if (unlikely(!debug_locks || current->lockdep_recursion))
2234 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2237 if (curr->hardirqs_enabled) {
2239 * We have done an ON -> OFF transition:
2241 curr->hardirqs_enabled = 0;
2242 curr->hardirq_disable_ip = ip;
2243 curr->hardirq_disable_event = ++curr->irq_events;
2244 debug_atomic_inc(&hardirqs_off_events);
2246 debug_atomic_inc(&redundant_hardirqs_off);
2248 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2250 void trace_hardirqs_off(void)
2252 trace_hardirqs_off_caller(CALLER_ADDR0);
2254 EXPORT_SYMBOL(trace_hardirqs_off);
2257 * Softirqs will be enabled:
2259 void trace_softirqs_on(unsigned long ip)
2261 struct task_struct *curr = current;
2263 if (unlikely(!debug_locks))
2266 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2269 if (curr->softirqs_enabled) {
2270 debug_atomic_inc(&redundant_softirqs_on);
2275 * We'll do an OFF -> ON transition:
2277 curr->softirqs_enabled = 1;
2278 curr->softirq_enable_ip = ip;
2279 curr->softirq_enable_event = ++curr->irq_events;
2280 debug_atomic_inc(&softirqs_on_events);
2282 * We are going to turn softirqs on, so set the
2283 * usage bit for all held locks, if hardirqs are
2286 if (curr->hardirqs_enabled)
2287 mark_held_locks(curr, 0);
2291 * Softirqs were disabled:
2293 void trace_softirqs_off(unsigned long ip)
2295 struct task_struct *curr = current;
2297 if (unlikely(!debug_locks))
2300 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2303 if (curr->softirqs_enabled) {
2305 * We have done an ON -> OFF transition:
2307 curr->softirqs_enabled = 0;
2308 curr->softirq_disable_ip = ip;
2309 curr->softirq_disable_event = ++curr->irq_events;
2310 debug_atomic_inc(&softirqs_off_events);
2311 DEBUG_LOCKS_WARN_ON(!softirq_count());
2313 debug_atomic_inc(&redundant_softirqs_off);
2316 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2319 * If non-trylock use in a hardirq or softirq context, then
2320 * mark the lock as used in these contexts:
2322 if (!hlock->trylock) {
2324 if (curr->hardirq_context)
2325 if (!mark_lock(curr, hlock,
2326 LOCK_USED_IN_HARDIRQ_READ))
2328 if (curr->softirq_context)
2329 if (!mark_lock(curr, hlock,
2330 LOCK_USED_IN_SOFTIRQ_READ))
2333 if (curr->hardirq_context)
2334 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2336 if (curr->softirq_context)
2337 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2341 if (!hlock->hardirqs_off) {
2343 if (!mark_lock(curr, hlock,
2344 LOCK_ENABLED_HARDIRQS_READ))
2346 if (curr->softirqs_enabled)
2347 if (!mark_lock(curr, hlock,
2348 LOCK_ENABLED_SOFTIRQS_READ))
2351 if (!mark_lock(curr, hlock,
2352 LOCK_ENABLED_HARDIRQS))
2354 if (curr->softirqs_enabled)
2355 if (!mark_lock(curr, hlock,
2356 LOCK_ENABLED_SOFTIRQS))
2364 static int separate_irq_context(struct task_struct *curr,
2365 struct held_lock *hlock)
2367 unsigned int depth = curr->lockdep_depth;
2370 * Keep track of points where we cross into an interrupt context:
2372 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2373 curr->softirq_context;
2375 struct held_lock *prev_hlock;
2377 prev_hlock = curr->held_locks + depth-1;
2379 * If we cross into another context, reset the
2380 * hash key (this also prevents the checking and the
2381 * adding of the dependency to 'prev'):
2383 if (prev_hlock->irq_context != hlock->irq_context)
2392 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2393 enum lock_usage_bit new_bit)
2399 static inline int mark_irqflags(struct task_struct *curr,
2400 struct held_lock *hlock)
2405 static inline int separate_irq_context(struct task_struct *curr,
2406 struct held_lock *hlock)
2414 * Mark a lock with a usage bit, and validate the state transition:
2416 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2417 enum lock_usage_bit new_bit)
2419 unsigned int new_mask = 1 << new_bit, ret = 1;
2422 * If already set then do not dirty the cacheline,
2423 * nor do any checks:
2425 if (likely(hlock_class(this)->usage_mask & new_mask))
2431 * Make sure we didnt race:
2433 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2438 hlock_class(this)->usage_mask |= new_mask;
2440 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2444 case LOCK_USED_IN_HARDIRQ:
2445 case LOCK_USED_IN_SOFTIRQ:
2446 case LOCK_USED_IN_HARDIRQ_READ:
2447 case LOCK_USED_IN_SOFTIRQ_READ:
2448 case LOCK_ENABLED_HARDIRQS:
2449 case LOCK_ENABLED_SOFTIRQS:
2450 case LOCK_ENABLED_HARDIRQS_READ:
2451 case LOCK_ENABLED_SOFTIRQS_READ:
2452 ret = mark_lock_irq(curr, this, new_bit);
2457 debug_atomic_dec(&nr_unused_locks);
2460 if (!debug_locks_off_graph_unlock())
2469 * We must printk outside of the graph_lock:
2472 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2474 print_irqtrace_events(curr);
2482 * Initialize a lock instance's lock-class mapping info:
2484 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2485 struct lock_class_key *key, int subclass)
2487 if (unlikely(!debug_locks))
2490 if (DEBUG_LOCKS_WARN_ON(!key))
2492 if (DEBUG_LOCKS_WARN_ON(!name))
2495 * Sanity check, the lock-class key must be persistent:
2497 if (!static_obj(key)) {
2498 printk("BUG: key %p not in .data!\n", key);
2499 DEBUG_LOCKS_WARN_ON(1);
2504 lock->class_cache = NULL;
2505 #ifdef CONFIG_LOCK_STAT
2506 lock->cpu = raw_smp_processor_id();
2509 register_lock_class(lock, subclass, 1);
2512 EXPORT_SYMBOL_GPL(lockdep_init_map);
2515 * This gets called for every mutex_lock*()/spin_lock*() operation.
2516 * We maintain the dependency maps and validate the locking attempt:
2518 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2519 int trylock, int read, int check, int hardirqs_off,
2520 struct lockdep_map *nest_lock, unsigned long ip)
2522 struct task_struct *curr = current;
2523 struct lock_class *class = NULL;
2524 struct held_lock *hlock;
2525 unsigned int depth, id;
2532 if (unlikely(!debug_locks))
2535 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2538 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2540 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2541 printk("turning off the locking correctness validator.\n");
2546 class = lock->class_cache;
2548 * Not cached yet or subclass?
2550 if (unlikely(!class)) {
2551 class = register_lock_class(lock, subclass, 0);
2555 debug_atomic_inc((atomic_t *)&class->ops);
2556 if (very_verbose(class)) {
2557 printk("\nacquire class [%p] %s", class->key, class->name);
2558 if (class->name_version > 1)
2559 printk("#%d", class->name_version);
2565 * Add the lock to the list of currently held locks.
2566 * (we dont increase the depth just yet, up until the
2567 * dependency checks are done)
2569 depth = curr->lockdep_depth;
2570 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2573 hlock = curr->held_locks + depth;
2574 if (DEBUG_LOCKS_WARN_ON(!class))
2576 hlock->class_idx = class - lock_classes + 1;
2577 hlock->acquire_ip = ip;
2578 hlock->instance = lock;
2579 hlock->nest_lock = nest_lock;
2580 hlock->trylock = trylock;
2582 hlock->check = check;
2583 hlock->hardirqs_off = !!hardirqs_off;
2584 #ifdef CONFIG_LOCK_STAT
2585 hlock->waittime_stamp = 0;
2586 hlock->holdtime_stamp = sched_clock();
2589 if (check == 2 && !mark_irqflags(curr, hlock))
2592 /* mark it as used: */
2593 if (!mark_lock(curr, hlock, LOCK_USED))
2597 * Calculate the chain hash: it's the combined hash of all the
2598 * lock keys along the dependency chain. We save the hash value
2599 * at every step so that we can get the current hash easily
2600 * after unlock. The chain hash is then used to cache dependency
2603 * The 'key ID' is what is the most compact key value to drive
2604 * the hash, not class->key.
2606 id = class - lock_classes;
2607 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2610 chain_key = curr->curr_chain_key;
2612 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2617 hlock->prev_chain_key = chain_key;
2618 if (separate_irq_context(curr, hlock)) {
2622 chain_key = iterate_chain_key(chain_key, id);
2624 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2627 curr->curr_chain_key = chain_key;
2628 curr->lockdep_depth++;
2629 check_chain_key(curr);
2630 #ifdef CONFIG_DEBUG_LOCKDEP
2631 if (unlikely(!debug_locks))
2634 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2636 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2637 printk("turning off the locking correctness validator.\n");
2641 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2642 max_lockdep_depth = curr->lockdep_depth;
2648 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2651 if (!debug_locks_off())
2653 if (debug_locks_silent)
2656 printk("\n=====================================\n");
2657 printk( "[ BUG: bad unlock balance detected! ]\n");
2658 printk( "-------------------------------------\n");
2659 printk("%s/%d is trying to release lock (",
2660 curr->comm, task_pid_nr(curr));
2661 print_lockdep_cache(lock);
2664 printk("but there are no more locks to release!\n");
2665 printk("\nother info that might help us debug this:\n");
2666 lockdep_print_held_locks(curr);
2668 printk("\nstack backtrace:\n");
2675 * Common debugging checks for both nested and non-nested unlock:
2677 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2680 if (unlikely(!debug_locks))
2682 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2685 if (curr->lockdep_depth <= 0)
2686 return print_unlock_inbalance_bug(curr, lock, ip);
2692 __lock_set_subclass(struct lockdep_map *lock,
2693 unsigned int subclass, unsigned long ip)
2695 struct task_struct *curr = current;
2696 struct held_lock *hlock, *prev_hlock;
2697 struct lock_class *class;
2701 depth = curr->lockdep_depth;
2702 if (DEBUG_LOCKS_WARN_ON(!depth))
2706 for (i = depth-1; i >= 0; i--) {
2707 hlock = curr->held_locks + i;
2709 * We must not cross into another context:
2711 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2713 if (hlock->instance == lock)
2717 return print_unlock_inbalance_bug(curr, lock, ip);
2720 class = register_lock_class(lock, subclass, 0);
2721 hlock->class_idx = class - lock_classes + 1;
2723 curr->lockdep_depth = i;
2724 curr->curr_chain_key = hlock->prev_chain_key;
2726 for (; i < depth; i++) {
2727 hlock = curr->held_locks + i;
2728 if (!__lock_acquire(hlock->instance,
2729 hlock_class(hlock)->subclass, hlock->trylock,
2730 hlock->read, hlock->check, hlock->hardirqs_off,
2731 hlock->nest_lock, hlock->acquire_ip))
2735 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2741 * Remove the lock to the list of currently held locks in a
2742 * potentially non-nested (out of order) manner. This is a
2743 * relatively rare operation, as all the unlock APIs default
2744 * to nested mode (which uses lock_release()):
2747 lock_release_non_nested(struct task_struct *curr,
2748 struct lockdep_map *lock, unsigned long ip)
2750 struct held_lock *hlock, *prev_hlock;
2755 * Check whether the lock exists in the current stack
2758 depth = curr->lockdep_depth;
2759 if (DEBUG_LOCKS_WARN_ON(!depth))
2763 for (i = depth-1; i >= 0; i--) {
2764 hlock = curr->held_locks + i;
2766 * We must not cross into another context:
2768 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2770 if (hlock->instance == lock)
2774 return print_unlock_inbalance_bug(curr, lock, ip);
2777 lock_release_holdtime(hlock);
2780 * We have the right lock to unlock, 'hlock' points to it.
2781 * Now we remove it from the stack, and add back the other
2782 * entries (if any), recalculating the hash along the way:
2784 curr->lockdep_depth = i;
2785 curr->curr_chain_key = hlock->prev_chain_key;
2787 for (i++; i < depth; i++) {
2788 hlock = curr->held_locks + i;
2789 if (!__lock_acquire(hlock->instance,
2790 hlock_class(hlock)->subclass, hlock->trylock,
2791 hlock->read, hlock->check, hlock->hardirqs_off,
2792 hlock->nest_lock, hlock->acquire_ip))
2796 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2802 * Remove the lock to the list of currently held locks - this gets
2803 * called on mutex_unlock()/spin_unlock*() (or on a failed
2804 * mutex_lock_interruptible()). This is done for unlocks that nest
2805 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2807 static int lock_release_nested(struct task_struct *curr,
2808 struct lockdep_map *lock, unsigned long ip)
2810 struct held_lock *hlock;
2814 * Pop off the top of the lock stack:
2816 depth = curr->lockdep_depth - 1;
2817 hlock = curr->held_locks + depth;
2820 * Is the unlock non-nested:
2822 if (hlock->instance != lock)
2823 return lock_release_non_nested(curr, lock, ip);
2824 curr->lockdep_depth--;
2826 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2829 curr->curr_chain_key = hlock->prev_chain_key;
2831 lock_release_holdtime(hlock);
2833 #ifdef CONFIG_DEBUG_LOCKDEP
2834 hlock->prev_chain_key = 0;
2835 hlock->class_idx = 0;
2836 hlock->acquire_ip = 0;
2837 hlock->irq_context = 0;
2843 * Remove the lock to the list of currently held locks - this gets
2844 * called on mutex_unlock()/spin_unlock*() (or on a failed
2845 * mutex_lock_interruptible()). This is done for unlocks that nest
2846 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2849 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2851 struct task_struct *curr = current;
2853 if (!check_unlock(curr, lock, ip))
2857 if (!lock_release_nested(curr, lock, ip))
2860 if (!lock_release_non_nested(curr, lock, ip))
2864 check_chain_key(curr);
2868 * Check whether we follow the irq-flags state precisely:
2870 static void check_flags(unsigned long flags)
2872 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2873 defined(CONFIG_TRACE_IRQFLAGS)
2877 if (irqs_disabled_flags(flags)) {
2878 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2879 printk("possible reason: unannotated irqs-off.\n");
2882 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2883 printk("possible reason: unannotated irqs-on.\n");
2888 * We dont accurately track softirq state in e.g.
2889 * hardirq contexts (such as on 4KSTACKS), so only
2890 * check if not in hardirq contexts:
2892 if (!hardirq_count()) {
2893 if (softirq_count())
2894 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2896 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2900 print_irqtrace_events(current);
2905 lock_set_subclass(struct lockdep_map *lock,
2906 unsigned int subclass, unsigned long ip)
2908 unsigned long flags;
2910 if (unlikely(current->lockdep_recursion))
2913 raw_local_irq_save(flags);
2914 current->lockdep_recursion = 1;
2916 if (__lock_set_subclass(lock, subclass, ip))
2917 check_chain_key(current);
2918 current->lockdep_recursion = 0;
2919 raw_local_irq_restore(flags);
2922 EXPORT_SYMBOL_GPL(lock_set_subclass);
2925 * We are not always called with irqs disabled - do that here,
2926 * and also avoid lockdep recursion:
2928 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2929 int trylock, int read, int check,
2930 struct lockdep_map *nest_lock, unsigned long ip)
2932 unsigned long flags;
2934 if (unlikely(current->lockdep_recursion))
2937 raw_local_irq_save(flags);
2940 current->lockdep_recursion = 1;
2941 __lock_acquire(lock, subclass, trylock, read, check,
2942 irqs_disabled_flags(flags), nest_lock, ip);
2943 current->lockdep_recursion = 0;
2944 raw_local_irq_restore(flags);
2947 EXPORT_SYMBOL_GPL(lock_acquire);
2949 void lock_release(struct lockdep_map *lock, int nested,
2952 unsigned long flags;
2954 if (unlikely(current->lockdep_recursion))
2957 raw_local_irq_save(flags);
2959 current->lockdep_recursion = 1;
2960 __lock_release(lock, nested, ip);
2961 current->lockdep_recursion = 0;
2962 raw_local_irq_restore(flags);
2965 EXPORT_SYMBOL_GPL(lock_release);
2967 #ifdef CONFIG_LOCK_STAT
2969 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2972 if (!debug_locks_off())
2974 if (debug_locks_silent)
2977 printk("\n=================================\n");
2978 printk( "[ BUG: bad contention detected! ]\n");
2979 printk( "---------------------------------\n");
2980 printk("%s/%d is trying to contend lock (",
2981 curr->comm, task_pid_nr(curr));
2982 print_lockdep_cache(lock);
2985 printk("but there are no locks held!\n");
2986 printk("\nother info that might help us debug this:\n");
2987 lockdep_print_held_locks(curr);
2989 printk("\nstack backtrace:\n");
2996 __lock_contended(struct lockdep_map *lock, unsigned long ip)
2998 struct task_struct *curr = current;
2999 struct held_lock *hlock, *prev_hlock;
3000 struct lock_class_stats *stats;
3004 depth = curr->lockdep_depth;
3005 if (DEBUG_LOCKS_WARN_ON(!depth))
3009 for (i = depth-1; i >= 0; i--) {
3010 hlock = curr->held_locks + i;
3012 * We must not cross into another context:
3014 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3016 if (hlock->instance == lock)
3020 print_lock_contention_bug(curr, lock, ip);
3024 hlock->waittime_stamp = sched_clock();
3026 point = lock_contention_point(hlock_class(hlock), ip);
3028 stats = get_lock_stats(hlock_class(hlock));
3029 if (point < ARRAY_SIZE(stats->contention_point))
3030 stats->contention_point[point]++;
3031 if (lock->cpu != smp_processor_id())
3032 stats->bounces[bounce_contended + !!hlock->read]++;
3033 put_lock_stats(stats);
3037 __lock_acquired(struct lockdep_map *lock)
3039 struct task_struct *curr = current;
3040 struct held_lock *hlock, *prev_hlock;
3041 struct lock_class_stats *stats;
3047 depth = curr->lockdep_depth;
3048 if (DEBUG_LOCKS_WARN_ON(!depth))
3052 for (i = depth-1; i >= 0; i--) {
3053 hlock = curr->held_locks + i;
3055 * We must not cross into another context:
3057 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3059 if (hlock->instance == lock)
3063 print_lock_contention_bug(curr, lock, _RET_IP_);
3067 cpu = smp_processor_id();
3068 if (hlock->waittime_stamp) {
3069 now = sched_clock();
3070 waittime = now - hlock->waittime_stamp;
3071 hlock->holdtime_stamp = now;
3074 stats = get_lock_stats(hlock_class(hlock));
3077 lock_time_inc(&stats->read_waittime, waittime);
3079 lock_time_inc(&stats->write_waittime, waittime);
3081 if (lock->cpu != cpu)
3082 stats->bounces[bounce_acquired + !!hlock->read]++;
3083 put_lock_stats(stats);
3088 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3090 unsigned long flags;
3092 if (unlikely(!lock_stat))
3095 if (unlikely(current->lockdep_recursion))
3098 raw_local_irq_save(flags);
3100 current->lockdep_recursion = 1;
3101 __lock_contended(lock, ip);
3102 current->lockdep_recursion = 0;
3103 raw_local_irq_restore(flags);
3105 EXPORT_SYMBOL_GPL(lock_contended);
3107 void lock_acquired(struct lockdep_map *lock)
3109 unsigned long flags;
3111 if (unlikely(!lock_stat))
3114 if (unlikely(current->lockdep_recursion))
3117 raw_local_irq_save(flags);
3119 current->lockdep_recursion = 1;
3120 __lock_acquired(lock);
3121 current->lockdep_recursion = 0;
3122 raw_local_irq_restore(flags);
3124 EXPORT_SYMBOL_GPL(lock_acquired);
3128 * Used by the testsuite, sanitize the validator state
3129 * after a simulated failure:
3132 void lockdep_reset(void)
3134 unsigned long flags;
3137 raw_local_irq_save(flags);
3138 current->curr_chain_key = 0;
3139 current->lockdep_depth = 0;
3140 current->lockdep_recursion = 0;
3141 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3142 nr_hardirq_chains = 0;
3143 nr_softirq_chains = 0;
3144 nr_process_chains = 0;
3146 for (i = 0; i < CHAINHASH_SIZE; i++)
3147 INIT_LIST_HEAD(chainhash_table + i);
3148 raw_local_irq_restore(flags);
3151 static void zap_class(struct lock_class *class)
3156 * Remove all dependencies this lock is
3159 for (i = 0; i < nr_list_entries; i++) {
3160 if (list_entries[i].class == class)
3161 list_del_rcu(&list_entries[i].entry);
3164 * Unhash the class and remove it from the all_lock_classes list:
3166 list_del_rcu(&class->hash_entry);
3167 list_del_rcu(&class->lock_entry);
3172 static inline int within(const void *addr, void *start, unsigned long size)
3174 return addr >= start && addr < start + size;
3177 void lockdep_free_key_range(void *start, unsigned long size)
3179 struct lock_class *class, *next;
3180 struct list_head *head;
3181 unsigned long flags;
3185 raw_local_irq_save(flags);
3186 locked = graph_lock();
3189 * Unhash all classes that were created by this module:
3191 for (i = 0; i < CLASSHASH_SIZE; i++) {
3192 head = classhash_table + i;
3193 if (list_empty(head))
3195 list_for_each_entry_safe(class, next, head, hash_entry) {
3196 if (within(class->key, start, size))
3198 else if (within(class->name, start, size))
3205 raw_local_irq_restore(flags);
3208 void lockdep_reset_lock(struct lockdep_map *lock)
3210 struct lock_class *class, *next;
3211 struct list_head *head;
3212 unsigned long flags;
3216 raw_local_irq_save(flags);
3219 * Remove all classes this lock might have:
3221 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3223 * If the class exists we look it up and zap it:
3225 class = look_up_lock_class(lock, j);
3230 * Debug check: in the end all mapped classes should
3233 locked = graph_lock();
3234 for (i = 0; i < CLASSHASH_SIZE; i++) {
3235 head = classhash_table + i;
3236 if (list_empty(head))
3238 list_for_each_entry_safe(class, next, head, hash_entry) {
3239 if (unlikely(class == lock->class_cache)) {
3240 if (debug_locks_off_graph_unlock())
3250 raw_local_irq_restore(flags);
3253 void lockdep_init(void)
3258 * Some architectures have their own start_kernel()
3259 * code which calls lockdep_init(), while we also
3260 * call lockdep_init() from the start_kernel() itself,
3261 * and we want to initialize the hashes only once:
3263 if (lockdep_initialized)
3266 for (i = 0; i < CLASSHASH_SIZE; i++)
3267 INIT_LIST_HEAD(classhash_table + i);
3269 for (i = 0; i < CHAINHASH_SIZE; i++)
3270 INIT_LIST_HEAD(chainhash_table + i);
3272 lockdep_initialized = 1;
3275 void __init lockdep_info(void)
3277 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3279 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3280 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3281 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3282 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3283 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3284 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3285 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3287 printk(" memory used by lock dependency info: %lu kB\n",
3288 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3289 sizeof(struct list_head) * CLASSHASH_SIZE +
3290 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3291 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3292 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3294 printk(" per task-struct memory footprint: %lu bytes\n",
3295 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3297 #ifdef CONFIG_DEBUG_LOCKDEP
3298 if (lockdep_init_error) {
3299 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3300 printk("Call stack leading to lockdep invocation was:\n");
3301 print_stack_trace(&lockdep_init_trace, 0);
3307 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3308 const void *mem_to, struct held_lock *hlock)
3310 if (!debug_locks_off())
3312 if (debug_locks_silent)
3315 printk("\n=========================\n");
3316 printk( "[ BUG: held lock freed! ]\n");
3317 printk( "-------------------------\n");
3318 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3319 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3321 lockdep_print_held_locks(curr);
3323 printk("\nstack backtrace:\n");
3327 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3328 const void* lock_from, unsigned long lock_len)
3330 return lock_from + lock_len <= mem_from ||
3331 mem_from + mem_len <= lock_from;
3335 * Called when kernel memory is freed (or unmapped), or if a lock
3336 * is destroyed or reinitialized - this code checks whether there is
3337 * any held lock in the memory range of <from> to <to>:
3339 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3341 struct task_struct *curr = current;
3342 struct held_lock *hlock;
3343 unsigned long flags;
3346 if (unlikely(!debug_locks))
3349 local_irq_save(flags);
3350 for (i = 0; i < curr->lockdep_depth; i++) {
3351 hlock = curr->held_locks + i;
3353 if (not_in_range(mem_from, mem_len, hlock->instance,
3354 sizeof(*hlock->instance)))
3357 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3360 local_irq_restore(flags);
3362 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3364 static void print_held_locks_bug(struct task_struct *curr)
3366 if (!debug_locks_off())
3368 if (debug_locks_silent)
3371 printk("\n=====================================\n");
3372 printk( "[ BUG: lock held at task exit time! ]\n");
3373 printk( "-------------------------------------\n");
3374 printk("%s/%d is exiting with locks still held!\n",
3375 curr->comm, task_pid_nr(curr));
3376 lockdep_print_held_locks(curr);
3378 printk("\nstack backtrace:\n");
3382 void debug_check_no_locks_held(struct task_struct *task)
3384 if (unlikely(task->lockdep_depth > 0))
3385 print_held_locks_bug(task);
3388 void debug_show_all_locks(void)
3390 struct task_struct *g, *p;
3394 if (unlikely(!debug_locks)) {
3395 printk("INFO: lockdep is turned off.\n");
3398 printk("\nShowing all locks held in the system:\n");
3401 * Here we try to get the tasklist_lock as hard as possible,
3402 * if not successful after 2 seconds we ignore it (but keep
3403 * trying). This is to enable a debug printout even if a
3404 * tasklist_lock-holding task deadlocks or crashes.
3407 if (!read_trylock(&tasklist_lock)) {
3409 printk("hm, tasklist_lock locked, retrying... ");
3412 printk(" #%d", 10-count);
3416 printk(" ignoring it.\n");
3420 printk(KERN_CONT " locked it.\n");
3423 do_each_thread(g, p) {
3425 * It's not reliable to print a task's held locks
3426 * if it's not sleeping (or if it's not the current
3429 if (p->state == TASK_RUNNING && p != current)
3431 if (p->lockdep_depth)
3432 lockdep_print_held_locks(p);
3434 if (read_trylock(&tasklist_lock))
3436 } while_each_thread(g, p);
3439 printk("=============================================\n\n");
3442 read_unlock(&tasklist_lock);
3445 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3448 * Careful: only use this function if you are sure that
3449 * the task cannot run in parallel!
3451 void __debug_show_held_locks(struct task_struct *task)
3453 if (unlikely(!debug_locks)) {
3454 printk("INFO: lockdep is turned off.\n");
3457 lockdep_print_held_locks(task);
3459 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3461 void debug_show_held_locks(struct task_struct *task)
3463 __debug_show_held_locks(task);
3466 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3468 void lockdep_sys_exit(void)
3470 struct task_struct *curr = current;
3472 if (unlikely(curr->lockdep_depth)) {
3473 if (!debug_locks_off())
3475 printk("\n================================================\n");
3476 printk( "[ BUG: lock held when returning to user space! ]\n");
3477 printk( "------------------------------------------------\n");
3478 printk("%s/%d is leaving the kernel with locks still held!\n",
3479 curr->comm, curr->pid);
3480 lockdep_print_held_locks(curr);