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>
43 #include <asm/sections.h>
45 #include "lockdep_internals.h"
47 #ifdef CONFIG_PROVE_LOCKING
48 int prove_locking
= 1;
49 module_param(prove_locking
, int, 0644);
51 #define prove_locking 0
54 #ifdef CONFIG_LOCK_STAT
56 module_param(lock_stat
, int, 0644);
62 * lockdep_lock: protects the lockdep graph, the hashes and the
63 * class/list/hash allocators.
65 * This is one of the rare exceptions where it's justified
66 * to use a raw spinlock - we really dont want the spinlock
67 * code to recurse back into the lockdep code...
69 static raw_spinlock_t lockdep_lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
71 static int graph_lock(void)
73 __raw_spin_lock(&lockdep_lock
);
75 * Make sure that if another CPU detected a bug while
76 * walking the graph we dont change it (while the other
77 * CPU is busy printing out stuff with the graph lock
81 __raw_spin_unlock(&lockdep_lock
);
87 static inline int graph_unlock(void)
89 if (debug_locks
&& !__raw_spin_is_locked(&lockdep_lock
))
90 return DEBUG_LOCKS_WARN_ON(1);
92 __raw_spin_unlock(&lockdep_lock
);
97 * Turn lock debugging off and return with 0 if it was off already,
98 * and also release the graph lock:
100 static inline int debug_locks_off_graph_unlock(void)
102 int ret
= debug_locks_off();
104 __raw_spin_unlock(&lockdep_lock
);
109 static int lockdep_initialized
;
111 unsigned long nr_list_entries
;
112 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
115 * All data structures here are protected by the global debug_lock.
117 * Mutex key structs only get allocated, once during bootup, and never
118 * get freed - this significantly simplifies the debugging code.
120 unsigned long nr_lock_classes
;
121 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
123 #ifdef CONFIG_LOCK_STAT
124 static DEFINE_PER_CPU(struct lock_class_stats
[MAX_LOCKDEP_KEYS
], lock_stats
);
126 static int lock_contention_point(struct lock_class
*class, unsigned long ip
)
130 for (i
= 0; i
< ARRAY_SIZE(class->contention_point
); i
++) {
131 if (class->contention_point
[i
] == 0) {
132 class->contention_point
[i
] = ip
;
135 if (class->contention_point
[i
] == ip
)
142 static void lock_time_inc(struct lock_time
*lt
, s64 time
)
147 if (time
< lt
->min
|| !lt
->min
)
154 static inline void lock_time_add(struct lock_time
*src
, struct lock_time
*dst
)
156 dst
->min
+= src
->min
;
157 dst
->max
+= src
->max
;
158 dst
->total
+= src
->total
;
162 struct lock_class_stats
lock_stats(struct lock_class
*class)
164 struct lock_class_stats stats
;
167 memset(&stats
, 0, sizeof(struct lock_class_stats
));
168 for_each_possible_cpu(cpu
) {
169 struct lock_class_stats
*pcs
=
170 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
172 for (i
= 0; i
< ARRAY_SIZE(stats
.contention_point
); i
++)
173 stats
.contention_point
[i
] += pcs
->contention_point
[i
];
175 lock_time_add(&pcs
->read_waittime
, &stats
.read_waittime
);
176 lock_time_add(&pcs
->write_waittime
, &stats
.write_waittime
);
178 lock_time_add(&pcs
->read_holdtime
, &stats
.read_holdtime
);
179 lock_time_add(&pcs
->write_holdtime
, &stats
.write_holdtime
);
185 void clear_lock_stats(struct lock_class
*class)
189 for_each_possible_cpu(cpu
) {
190 struct lock_class_stats
*cpu_stats
=
191 &per_cpu(lock_stats
, cpu
)[class - lock_classes
];
193 memset(cpu_stats
, 0, sizeof(struct lock_class_stats
));
195 memset(class->contention_point
, 0, sizeof(class->contention_point
));
198 static struct lock_class_stats
*get_lock_stats(struct lock_class
*class)
200 return &get_cpu_var(lock_stats
)[class - lock_classes
];
203 static void put_lock_stats(struct lock_class_stats
*stats
)
205 put_cpu_var(lock_stats
);
208 static void lock_release_holdtime(struct held_lock
*hlock
)
210 struct lock_class_stats
*stats
;
216 holdtime
= sched_clock() - hlock
->holdtime_stamp
;
218 stats
= get_lock_stats(hlock
->class);
220 lock_time_inc(&stats
->read_holdtime
, holdtime
);
222 lock_time_inc(&stats
->write_holdtime
, holdtime
);
223 put_lock_stats(stats
);
226 static inline void lock_release_holdtime(struct held_lock
*hlock
)
232 * We keep a global list of all lock classes. The list only grows,
233 * never shrinks. The list is only accessed with the lockdep
234 * spinlock lock held.
236 LIST_HEAD(all_lock_classes
);
239 * The lockdep classes are in a hash-table as well, for fast lookup:
241 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
242 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
243 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
244 #define classhashentry(key) (classhash_table + __classhashfn((key)))
246 static struct list_head classhash_table
[CLASSHASH_SIZE
];
249 * We put the lock dependency chains into a hash-table as well, to cache
252 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
253 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
254 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
255 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
257 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
260 * The hash key of the lock dependency chains is a hash itself too:
261 * it's a hash of all locks taken up to that lock, including that lock.
262 * It's a 64-bit hash, because it's important for the keys to be
265 #define iterate_chain_key(key1, key2) \
266 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
267 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
270 void lockdep_off(void)
272 current
->lockdep_recursion
++;
275 EXPORT_SYMBOL(lockdep_off
);
277 void lockdep_on(void)
279 current
->lockdep_recursion
--;
282 EXPORT_SYMBOL(lockdep_on
);
285 * Debugging switches:
289 #define VERY_VERBOSE 0
292 # define HARDIRQ_VERBOSE 1
293 # define SOFTIRQ_VERBOSE 1
295 # define HARDIRQ_VERBOSE 0
296 # define SOFTIRQ_VERBOSE 0
299 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
301 * Quick filtering for interesting events:
303 static int class_filter(struct lock_class
*class)
307 if (class->name_version
== 1 &&
308 !strcmp(class->name
, "lockname"))
310 if (class->name_version
== 1 &&
311 !strcmp(class->name
, "&struct->lockfield"))
314 /* Filter everything else. 1 would be to allow everything else */
319 static int verbose(struct lock_class
*class)
322 return class_filter(class);
328 * Stack-trace: tightly packed array of stack backtrace
329 * addresses. Protected by the graph_lock.
331 unsigned long nr_stack_trace_entries
;
332 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
334 static int save_trace(struct stack_trace
*trace
)
336 trace
->nr_entries
= 0;
337 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
338 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
342 save_stack_trace(trace
);
344 trace
->max_entries
= trace
->nr_entries
;
346 nr_stack_trace_entries
+= trace
->nr_entries
;
348 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
349 if (!debug_locks_off_graph_unlock())
352 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
353 printk("turning off the locking correctness validator.\n");
362 unsigned int nr_hardirq_chains
;
363 unsigned int nr_softirq_chains
;
364 unsigned int nr_process_chains
;
365 unsigned int max_lockdep_depth
;
366 unsigned int max_recursion_depth
;
368 #ifdef CONFIG_DEBUG_LOCKDEP
370 * We cannot printk in early bootup code. Not even early_printk()
371 * might work. So we mark any initialization errors and printk
372 * about it later on, in lockdep_info().
374 static int lockdep_init_error
;
377 * Various lockdep statistics:
379 atomic_t chain_lookup_hits
;
380 atomic_t chain_lookup_misses
;
381 atomic_t hardirqs_on_events
;
382 atomic_t hardirqs_off_events
;
383 atomic_t redundant_hardirqs_on
;
384 atomic_t redundant_hardirqs_off
;
385 atomic_t softirqs_on_events
;
386 atomic_t softirqs_off_events
;
387 atomic_t redundant_softirqs_on
;
388 atomic_t redundant_softirqs_off
;
389 atomic_t nr_unused_locks
;
390 atomic_t nr_cyclic_checks
;
391 atomic_t nr_cyclic_check_recursions
;
392 atomic_t nr_find_usage_forwards_checks
;
393 atomic_t nr_find_usage_forwards_recursions
;
394 atomic_t nr_find_usage_backwards_checks
;
395 atomic_t nr_find_usage_backwards_recursions
;
396 # define debug_atomic_inc(ptr) atomic_inc(ptr)
397 # define debug_atomic_dec(ptr) atomic_dec(ptr)
398 # define debug_atomic_read(ptr) atomic_read(ptr)
400 # define debug_atomic_inc(ptr) do { } while (0)
401 # define debug_atomic_dec(ptr) do { } while (0)
402 # define debug_atomic_read(ptr) 0
409 static const char *usage_str
[] =
411 [LOCK_USED
] = "initial-use ",
412 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
413 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
414 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
415 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
416 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
417 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
418 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
419 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
422 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
424 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
428 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
430 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
432 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
435 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
438 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
441 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
444 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
446 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
448 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
452 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
454 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
456 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
461 static void print_lock_name(struct lock_class
*class)
463 char str
[KSYM_NAME_LEN
], c1
, c2
, c3
, c4
;
466 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
470 name
= __get_key_name(class->key
, str
);
471 printk(" (%s", name
);
473 printk(" (%s", name
);
474 if (class->name_version
> 1)
475 printk("#%d", class->name_version
);
477 printk("/%d", class->subclass
);
479 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
482 static void print_lockdep_cache(struct lockdep_map
*lock
)
485 char str
[KSYM_NAME_LEN
];
489 name
= __get_key_name(lock
->key
->subkeys
, str
);
494 static void print_lock(struct held_lock
*hlock
)
496 print_lock_name(hlock
->class);
498 print_ip_sym(hlock
->acquire_ip
);
501 static void lockdep_print_held_locks(struct task_struct
*curr
)
503 int i
, depth
= curr
->lockdep_depth
;
506 printk("no locks held by %s/%d.\n", curr
->comm
, curr
->pid
);
509 printk("%d lock%s held by %s/%d:\n",
510 depth
, depth
> 1 ? "s" : "", curr
->comm
, curr
->pid
);
512 for (i
= 0; i
< depth
; i
++) {
514 print_lock(curr
->held_locks
+ i
);
518 static void print_lock_class_header(struct lock_class
*class, int depth
)
522 printk("%*s->", depth
, "");
523 print_lock_name(class);
524 printk(" ops: %lu", class->ops
);
527 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
528 if (class->usage_mask
& (1 << bit
)) {
531 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
532 len
+= printk(" at:\n");
533 print_stack_trace(class->usage_traces
+ bit
, len
);
536 printk("%*s }\n", depth
, "");
538 printk("%*s ... key at: ",depth
,"");
539 print_ip_sym((unsigned long)class->key
);
543 * printk all lock dependencies starting at <entry>:
545 static void print_lock_dependencies(struct lock_class
*class, int depth
)
547 struct lock_list
*entry
;
549 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
552 print_lock_class_header(class, depth
);
554 list_for_each_entry(entry
, &class->locks_after
, entry
) {
555 if (DEBUG_LOCKS_WARN_ON(!entry
->class))
558 print_lock_dependencies(entry
->class, depth
+ 1);
560 printk("%*s ... acquired at:\n",depth
,"");
561 print_stack_trace(&entry
->trace
, 2);
566 static void print_kernel_version(void)
568 printk("%s %.*s\n", init_utsname()->release
,
569 (int)strcspn(init_utsname()->version
, " "),
570 init_utsname()->version
);
573 static int very_verbose(struct lock_class
*class)
576 return class_filter(class);
582 * Is this the address of a static object:
584 static int static_obj(void *obj
)
586 unsigned long start
= (unsigned long) &_stext
,
587 end
= (unsigned long) &_end
,
588 addr
= (unsigned long) obj
;
596 if ((addr
>= start
) && (addr
< end
))
603 for_each_possible_cpu(i
) {
604 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
605 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
608 if ((addr
>= start
) && (addr
< end
))
616 return is_module_address(addr
);
620 * To make lock name printouts unique, we calculate a unique
621 * class->name_version generation counter:
623 static int count_matching_names(struct lock_class
*new_class
)
625 struct lock_class
*class;
628 if (!new_class
->name
)
631 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
632 if (new_class
->key
- new_class
->subclass
== class->key
)
633 return class->name_version
;
634 if (class->name
&& !strcmp(class->name
, new_class
->name
))
635 count
= max(count
, class->name_version
);
642 * Register a lock's class in the hash-table, if the class is not present
643 * yet. Otherwise we look it up. We cache the result in the lock object
644 * itself, so actual lookup of the hash should be once per lock object.
646 static inline struct lock_class
*
647 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
649 struct lockdep_subclass_key
*key
;
650 struct list_head
*hash_head
;
651 struct lock_class
*class;
653 #ifdef CONFIG_DEBUG_LOCKDEP
655 * If the architecture calls into lockdep before initializing
656 * the hashes then we'll warn about it later. (we cannot printk
659 if (unlikely(!lockdep_initialized
)) {
661 lockdep_init_error
= 1;
666 * Static locks do not have their class-keys yet - for them the key
667 * is the lock object itself:
669 if (unlikely(!lock
->key
))
670 lock
->key
= (void *)lock
;
673 * NOTE: the class-key must be unique. For dynamic locks, a static
674 * lock_class_key variable is passed in through the mutex_init()
675 * (or spin_lock_init()) call - which acts as the key. For static
676 * locks we use the lock object itself as the key.
678 BUILD_BUG_ON(sizeof(struct lock_class_key
) >
679 sizeof(struct lockdep_map
));
681 key
= lock
->key
->subkeys
+ subclass
;
683 hash_head
= classhashentry(key
);
686 * We can walk the hash lockfree, because the hash only
687 * grows, and we are careful when adding entries to the end:
689 list_for_each_entry(class, hash_head
, hash_entry
) {
690 if (class->key
== key
) {
691 WARN_ON_ONCE(class->name
!= lock
->name
);
700 * Register a lock's class in the hash-table, if the class is not present
701 * yet. Otherwise we look it up. We cache the result in the lock object
702 * itself, so actual lookup of the hash should be once per lock object.
704 static inline struct lock_class
*
705 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
707 struct lockdep_subclass_key
*key
;
708 struct list_head
*hash_head
;
709 struct lock_class
*class;
712 class = look_up_lock_class(lock
, subclass
);
717 * Debug-check: all keys must be persistent!
719 if (!static_obj(lock
->key
)) {
721 printk("INFO: trying to register non-static key.\n");
722 printk("the code is fine but needs lockdep annotation.\n");
723 printk("turning off the locking correctness validator.\n");
729 key
= lock
->key
->subkeys
+ subclass
;
730 hash_head
= classhashentry(key
);
732 raw_local_irq_save(flags
);
734 raw_local_irq_restore(flags
);
738 * We have to do the hash-walk again, to avoid races
741 list_for_each_entry(class, hash_head
, hash_entry
)
742 if (class->key
== key
)
745 * Allocate a new key from the static array, and add it to
748 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
749 if (!debug_locks_off_graph_unlock()) {
750 raw_local_irq_restore(flags
);
753 raw_local_irq_restore(flags
);
755 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
756 printk("turning off the locking correctness validator.\n");
759 class = lock_classes
+ nr_lock_classes
++;
760 debug_atomic_inc(&nr_unused_locks
);
762 class->name
= lock
->name
;
763 class->subclass
= subclass
;
764 INIT_LIST_HEAD(&class->lock_entry
);
765 INIT_LIST_HEAD(&class->locks_before
);
766 INIT_LIST_HEAD(&class->locks_after
);
767 class->name_version
= count_matching_names(class);
769 * We use RCU's safe list-add method to make
770 * parallel walking of the hash-list safe:
772 list_add_tail_rcu(&class->hash_entry
, hash_head
);
774 if (verbose(class)) {
776 raw_local_irq_restore(flags
);
778 printk("\nnew class %p: %s", class->key
, class->name
);
779 if (class->name_version
> 1)
780 printk("#%d", class->name_version
);
784 raw_local_irq_save(flags
);
786 raw_local_irq_restore(flags
);
792 raw_local_irq_restore(flags
);
794 if (!subclass
|| force
)
795 lock
->class_cache
= class;
797 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
803 #ifdef CONFIG_PROVE_LOCKING
805 * Allocate a lockdep entry. (assumes the graph_lock held, returns
806 * with NULL on failure)
808 static struct lock_list
*alloc_list_entry(void)
810 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
811 if (!debug_locks_off_graph_unlock())
814 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
815 printk("turning off the locking correctness validator.\n");
818 return list_entries
+ nr_list_entries
++;
822 * Add a new dependency to the head of the list:
824 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
825 struct list_head
*head
, unsigned long ip
, int distance
)
827 struct lock_list
*entry
;
829 * Lock not present yet - get a new dependency struct and
830 * add it to the list:
832 entry
= alloc_list_entry();
837 entry
->distance
= distance
;
838 if (!save_trace(&entry
->trace
))
842 * Since we never remove from the dependency list, the list can
843 * be walked lockless by other CPUs, it's only allocation
844 * that must be protected by the spinlock. But this also means
845 * we must make new entries visible only once writes to the
846 * entry become visible - hence the RCU op:
848 list_add_tail_rcu(&entry
->entry
, head
);
854 * Recursive, forwards-direction lock-dependency checking, used for
855 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
858 * (to keep the stackframe of the recursive functions small we
859 * use these global variables, and we also mark various helper
860 * functions as noinline.)
862 static struct held_lock
*check_source
, *check_target
;
865 * Print a dependency chain entry (this is only done when a deadlock
866 * has been detected):
869 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
871 if (debug_locks_silent
)
873 printk("\n-> #%u", depth
);
874 print_lock_name(target
->class);
876 print_stack_trace(&target
->trace
, 6);
882 * When a circular dependency is detected, print the
886 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
888 struct task_struct
*curr
= current
;
890 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
893 printk("\n=======================================================\n");
894 printk( "[ INFO: possible circular locking dependency detected ]\n");
895 print_kernel_version();
896 printk( "-------------------------------------------------------\n");
897 printk("%s/%d is trying to acquire lock:\n",
898 curr
->comm
, curr
->pid
);
899 print_lock(check_source
);
900 printk("\nbut task is already holding lock:\n");
901 print_lock(check_target
);
902 printk("\nwhich lock already depends on the new lock.\n\n");
903 printk("\nthe existing dependency chain (in reverse order) is:\n");
905 print_circular_bug_entry(entry
, depth
);
910 static noinline
int print_circular_bug_tail(void)
912 struct task_struct
*curr
= current
;
913 struct lock_list
this;
915 if (debug_locks_silent
)
918 this.class = check_source
->class;
919 if (!save_trace(&this.trace
))
922 print_circular_bug_entry(&this, 0);
924 printk("\nother info that might help us debug this:\n\n");
925 lockdep_print_held_locks(curr
);
927 printk("\nstack backtrace:\n");
933 #define RECURSION_LIMIT 40
935 static int noinline
print_infinite_recursion_bug(void)
937 if (!debug_locks_off_graph_unlock())
946 * Prove that the dependency graph starting at <entry> can not
947 * lead to <target>. Print an error and return 0 if it does.
950 check_noncircular(struct lock_class
*source
, unsigned int depth
)
952 struct lock_list
*entry
;
954 debug_atomic_inc(&nr_cyclic_check_recursions
);
955 if (depth
> max_recursion_depth
)
956 max_recursion_depth
= depth
;
957 if (depth
>= RECURSION_LIMIT
)
958 return print_infinite_recursion_bug();
960 * Check this lock's dependency list:
962 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
963 if (entry
->class == check_target
->class)
964 return print_circular_bug_header(entry
, depth
+1);
965 debug_atomic_inc(&nr_cyclic_checks
);
966 if (!check_noncircular(entry
->class, depth
+1))
967 return print_circular_bug_entry(entry
, depth
+1);
972 #ifdef CONFIG_TRACE_IRQFLAGS
974 * Forwards and backwards subgraph searching, for the purposes of
975 * proving that two subgraphs can be connected by a new dependency
976 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
978 static enum lock_usage_bit find_usage_bit
;
979 static struct lock_class
*forwards_match
, *backwards_match
;
982 * Find a node in the forwards-direction dependency sub-graph starting
983 * at <source> that matches <find_usage_bit>.
985 * Return 2 if such a node exists in the subgraph, and put that node
986 * into <forwards_match>.
988 * Return 1 otherwise and keep <forwards_match> unchanged.
992 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
994 struct lock_list
*entry
;
997 if (depth
> max_recursion_depth
)
998 max_recursion_depth
= depth
;
999 if (depth
>= RECURSION_LIMIT
)
1000 return print_infinite_recursion_bug();
1002 debug_atomic_inc(&nr_find_usage_forwards_checks
);
1003 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1004 forwards_match
= source
;
1009 * Check this lock's dependency list:
1011 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
1012 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
1013 ret
= find_usage_forwards(entry
->class, depth
+1);
1014 if (ret
== 2 || ret
== 0)
1021 * Find a node in the backwards-direction dependency sub-graph starting
1022 * at <source> that matches <find_usage_bit>.
1024 * Return 2 if such a node exists in the subgraph, and put that node
1025 * into <backwards_match>.
1027 * Return 1 otherwise and keep <backwards_match> unchanged.
1028 * Return 0 on error.
1031 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
1033 struct lock_list
*entry
;
1036 if (!__raw_spin_is_locked(&lockdep_lock
))
1037 return DEBUG_LOCKS_WARN_ON(1);
1039 if (depth
> max_recursion_depth
)
1040 max_recursion_depth
= depth
;
1041 if (depth
>= RECURSION_LIMIT
)
1042 return print_infinite_recursion_bug();
1044 debug_atomic_inc(&nr_find_usage_backwards_checks
);
1045 if (source
->usage_mask
& (1 << find_usage_bit
)) {
1046 backwards_match
= source
;
1051 * Check this lock's dependency list:
1053 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
1054 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
1055 ret
= find_usage_backwards(entry
->class, depth
+1);
1056 if (ret
== 2 || ret
== 0)
1063 print_bad_irq_dependency(struct task_struct
*curr
,
1064 struct held_lock
*prev
,
1065 struct held_lock
*next
,
1066 enum lock_usage_bit bit1
,
1067 enum lock_usage_bit bit2
,
1068 const char *irqclass
)
1070 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1073 printk("\n======================================================\n");
1074 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1075 irqclass
, irqclass
);
1076 print_kernel_version();
1077 printk( "------------------------------------------------------\n");
1078 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1079 curr
->comm
, curr
->pid
,
1080 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
1081 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
1082 curr
->hardirqs_enabled
,
1083 curr
->softirqs_enabled
);
1086 printk("\nand this task is already holding:\n");
1088 printk("which would create a new lock dependency:\n");
1089 print_lock_name(prev
->class);
1091 print_lock_name(next
->class);
1094 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1096 print_lock_name(backwards_match
);
1097 printk("\n... which became %s-irq-safe at:\n", irqclass
);
1099 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
1101 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
1102 print_lock_name(forwards_match
);
1103 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
1106 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
1108 printk("\nother info that might help us debug this:\n\n");
1109 lockdep_print_held_locks(curr
);
1111 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
1112 print_lock_dependencies(backwards_match
, 0);
1114 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
1115 print_lock_dependencies(forwards_match
, 0);
1117 printk("\nstack backtrace:\n");
1124 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
1125 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
1126 enum lock_usage_bit bit_forwards
, const char *irqclass
)
1130 find_usage_bit
= bit_backwards
;
1131 /* fills in <backwards_match> */
1132 ret
= find_usage_backwards(prev
->class, 0);
1133 if (!ret
|| ret
== 1)
1136 find_usage_bit
= bit_forwards
;
1137 ret
= find_usage_forwards(next
->class, 0);
1138 if (!ret
|| ret
== 1)
1141 return print_bad_irq_dependency(curr
, prev
, next
,
1142 bit_backwards
, bit_forwards
, irqclass
);
1146 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1147 struct held_lock
*next
)
1150 * Prove that the new dependency does not connect a hardirq-safe
1151 * lock with a hardirq-unsafe lock - to achieve this we search
1152 * the backwards-subgraph starting at <prev>, and the
1153 * forwards-subgraph starting at <next>:
1155 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
1156 LOCK_ENABLED_HARDIRQS
, "hard"))
1160 * Prove that the new dependency does not connect a hardirq-safe-read
1161 * lock with a hardirq-unsafe lock - to achieve this we search
1162 * the backwards-subgraph starting at <prev>, and the
1163 * forwards-subgraph starting at <next>:
1165 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
1166 LOCK_ENABLED_HARDIRQS
, "hard-read"))
1170 * Prove that the new dependency does not connect a softirq-safe
1171 * lock with a softirq-unsafe lock - to achieve this we search
1172 * the backwards-subgraph starting at <prev>, and the
1173 * forwards-subgraph starting at <next>:
1175 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
1176 LOCK_ENABLED_SOFTIRQS
, "soft"))
1179 * Prove that the new dependency does not connect a softirq-safe-read
1180 * lock with a softirq-unsafe lock - to achieve this we search
1181 * the backwards-subgraph starting at <prev>, and the
1182 * forwards-subgraph starting at <next>:
1184 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
1185 LOCK_ENABLED_SOFTIRQS
, "soft"))
1191 static void inc_chains(void)
1193 if (current
->hardirq_context
)
1194 nr_hardirq_chains
++;
1196 if (current
->softirq_context
)
1197 nr_softirq_chains
++;
1199 nr_process_chains
++;
1206 check_prev_add_irq(struct task_struct
*curr
, struct held_lock
*prev
,
1207 struct held_lock
*next
)
1212 static inline void inc_chains(void)
1214 nr_process_chains
++;
1220 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
1221 struct held_lock
*next
)
1223 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1226 printk("\n=============================================\n");
1227 printk( "[ INFO: possible recursive locking detected ]\n");
1228 print_kernel_version();
1229 printk( "---------------------------------------------\n");
1230 printk("%s/%d is trying to acquire lock:\n",
1231 curr
->comm
, curr
->pid
);
1233 printk("\nbut task is already holding lock:\n");
1236 printk("\nother info that might help us debug this:\n");
1237 lockdep_print_held_locks(curr
);
1239 printk("\nstack backtrace:\n");
1246 * Check whether we are holding such a class already.
1248 * (Note that this has to be done separately, because the graph cannot
1249 * detect such classes of deadlocks.)
1251 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1254 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
1255 struct lockdep_map
*next_instance
, int read
)
1257 struct held_lock
*prev
;
1260 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1261 prev
= curr
->held_locks
+ i
;
1262 if (prev
->class != next
->class)
1265 * Allow read-after-read recursion of the same
1266 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1268 if ((read
== 2) && prev
->read
)
1270 return print_deadlock_bug(curr
, prev
, next
);
1276 * There was a chain-cache miss, and we are about to add a new dependency
1277 * to a previous lock. We recursively validate the following rules:
1279 * - would the adding of the <prev> -> <next> dependency create a
1280 * circular dependency in the graph? [== circular deadlock]
1282 * - does the new prev->next dependency connect any hardirq-safe lock
1283 * (in the full backwards-subgraph starting at <prev>) with any
1284 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1285 * <next>)? [== illegal lock inversion with hardirq contexts]
1287 * - does the new prev->next dependency connect any softirq-safe lock
1288 * (in the full backwards-subgraph starting at <prev>) with any
1289 * softirq-unsafe lock (in the full forwards-subgraph starting at
1290 * <next>)? [== illegal lock inversion with softirq contexts]
1292 * any of these scenarios could lead to a deadlock.
1294 * Then if all the validations pass, we add the forwards and backwards
1298 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
1299 struct held_lock
*next
, int distance
)
1301 struct lock_list
*entry
;
1305 * Prove that the new <prev> -> <next> dependency would not
1306 * create a circular dependency in the graph. (We do this by
1307 * forward-recursing into the graph starting at <next>, and
1308 * checking whether we can reach <prev>.)
1310 * We are using global variables to control the recursion, to
1311 * keep the stackframe size of the recursive functions low:
1313 check_source
= next
;
1314 check_target
= prev
;
1315 if (!(check_noncircular(next
->class, 0)))
1316 return print_circular_bug_tail();
1318 if (!check_prev_add_irq(curr
, prev
, next
))
1322 * For recursive read-locks we do all the dependency checks,
1323 * but we dont store read-triggered dependencies (only
1324 * write-triggered dependencies). This ensures that only the
1325 * write-side dependencies matter, and that if for example a
1326 * write-lock never takes any other locks, then the reads are
1327 * equivalent to a NOP.
1329 if (next
->read
== 2 || prev
->read
== 2)
1332 * Is the <prev> -> <next> dependency already present?
1334 * (this may occur even though this is a new chain: consider
1335 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1336 * chains - the second one will be new, but L1 already has
1337 * L2 added to its dependency list, due to the first chain.)
1339 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
1340 if (entry
->class == next
->class) {
1342 entry
->distance
= 1;
1348 * Ok, all validations passed, add the new lock
1349 * to the previous lock's dependency list:
1351 ret
= add_lock_to_list(prev
->class, next
->class,
1352 &prev
->class->locks_after
, next
->acquire_ip
, distance
);
1357 ret
= add_lock_to_list(next
->class, prev
->class,
1358 &next
->class->locks_before
, next
->acquire_ip
, distance
);
1363 * Debugging printouts:
1365 if (verbose(prev
->class) || verbose(next
->class)) {
1367 printk("\n new dependency: ");
1368 print_lock_name(prev
->class);
1370 print_lock_name(next
->class);
1373 return graph_lock();
1379 * Add the dependency to all directly-previous locks that are 'relevant'.
1380 * The ones that are relevant are (in increasing distance from curr):
1381 * all consecutive trylock entries and the final non-trylock entry - or
1382 * the end of this context's lock-chain - whichever comes first.
1385 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1387 int depth
= curr
->lockdep_depth
;
1388 struct held_lock
*hlock
;
1393 * Depth must not be zero for a non-head lock:
1398 * At least two relevant locks must exist for this
1401 if (curr
->held_locks
[depth
].irq_context
!=
1402 curr
->held_locks
[depth
-1].irq_context
)
1406 int distance
= curr
->lockdep_depth
- depth
+ 1;
1407 hlock
= curr
->held_locks
+ depth
-1;
1409 * Only non-recursive-read entries get new dependencies
1412 if (hlock
->read
!= 2) {
1413 if (!check_prev_add(curr
, hlock
, next
, distance
))
1416 * Stop after the first non-trylock entry,
1417 * as non-trylock entries have added their
1418 * own direct dependencies already, so this
1419 * lock is connected to them indirectly:
1421 if (!hlock
->trylock
)
1426 * End of lock-stack?
1431 * Stop the search if we cross into another context:
1433 if (curr
->held_locks
[depth
].irq_context
!=
1434 curr
->held_locks
[depth
-1].irq_context
)
1439 if (!debug_locks_off_graph_unlock())
1447 unsigned long nr_lock_chains
;
1448 static struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
1451 * Look up a dependency chain. If the key is not present yet then
1452 * add it and return 1 - in this case the new dependency chain is
1453 * validated. If the key is already hashed, return 0.
1454 * (On return with 1 graph_lock is held.)
1456 static inline int lookup_chain_cache(u64 chain_key
, struct lock_class
*class)
1458 struct list_head
*hash_head
= chainhashentry(chain_key
);
1459 struct lock_chain
*chain
;
1461 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1464 * We can walk it lock-free, because entries only get added
1467 list_for_each_entry(chain
, hash_head
, entry
) {
1468 if (chain
->chain_key
== chain_key
) {
1470 debug_atomic_inc(&chain_lookup_hits
);
1471 if (very_verbose(class))
1472 printk("\nhash chain already cached, key: "
1473 "%016Lx tail class: [%p] %s\n",
1474 (unsigned long long)chain_key
,
1475 class->key
, class->name
);
1479 if (very_verbose(class))
1480 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1481 (unsigned long long)chain_key
, class->key
, class->name
);
1483 * Allocate a new chain entry from the static array, and add
1489 * We have to walk the chain again locked - to avoid duplicates:
1491 list_for_each_entry(chain
, hash_head
, entry
) {
1492 if (chain
->chain_key
== chain_key
) {
1497 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1498 if (!debug_locks_off_graph_unlock())
1501 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1502 printk("turning off the locking correctness validator.\n");
1505 chain
= lock_chains
+ nr_lock_chains
++;
1506 chain
->chain_key
= chain_key
;
1507 list_add_tail_rcu(&chain
->entry
, hash_head
);
1508 debug_atomic_inc(&chain_lookup_misses
);
1514 static int validate_chain(struct task_struct
*curr
, struct lockdep_map
*lock
,
1515 struct held_lock
*hlock
, int chain_head
)
1518 * Trylock needs to maintain the stack of held locks, but it
1519 * does not add new dependencies, because trylock can be done
1522 * We look up the chain_key and do the O(N^2) check and update of
1523 * the dependencies only if this is a new dependency chain.
1524 * (If lookup_chain_cache() returns with 1 it acquires
1525 * graph_lock for us)
1527 if (!hlock
->trylock
&& (hlock
->check
== 2) &&
1528 lookup_chain_cache(curr
->curr_chain_key
, hlock
->class)) {
1530 * Check whether last held lock:
1532 * - is irq-safe, if this lock is irq-unsafe
1533 * - is softirq-safe, if this lock is hardirq-unsafe
1535 * And check whether the new lock's dependency graph
1536 * could lead back to the previous lock.
1538 * any of these scenarios could lead to a deadlock. If
1541 int ret
= check_deadlock(curr
, hlock
, lock
, hlock
->read
);
1546 * Mark recursive read, as we jump over it when
1547 * building dependencies (just like we jump over
1553 * Add dependency only if this lock is not the head
1554 * of the chain, and if it's not a secondary read-lock:
1556 if (!chain_head
&& ret
!= 2)
1557 if (!check_prevs_add(curr
, hlock
))
1561 /* after lookup_chain_cache(): */
1562 if (unlikely(!debug_locks
))
1568 static inline int validate_chain(struct task_struct
*curr
,
1569 struct lockdep_map
*lock
, struct held_lock
*hlock
,
1577 * We are building curr_chain_key incrementally, so double-check
1578 * it from scratch, to make sure that it's done correctly:
1580 static void check_chain_key(struct task_struct
*curr
)
1582 #ifdef CONFIG_DEBUG_LOCKDEP
1583 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1587 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1588 hlock
= curr
->held_locks
+ i
;
1589 if (chain_key
!= hlock
->prev_chain_key
) {
1591 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1592 curr
->lockdep_depth
, i
,
1593 (unsigned long long)chain_key
,
1594 (unsigned long long)hlock
->prev_chain_key
);
1598 id
= hlock
->class - lock_classes
;
1599 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1602 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1603 hlock
->irq_context
))
1605 chain_key
= iterate_chain_key(chain_key
, id
);
1608 if (chain_key
!= curr
->curr_chain_key
) {
1610 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1611 curr
->lockdep_depth
, i
,
1612 (unsigned long long)chain_key
,
1613 (unsigned long long)curr
->curr_chain_key
);
1620 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1621 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1623 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1626 printk("\n=================================\n");
1627 printk( "[ INFO: inconsistent lock state ]\n");
1628 print_kernel_version();
1629 printk( "---------------------------------\n");
1631 printk("inconsistent {%s} -> {%s} usage.\n",
1632 usage_str
[prev_bit
], usage_str
[new_bit
]);
1634 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1635 curr
->comm
, curr
->pid
,
1636 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1637 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1638 trace_hardirqs_enabled(curr
),
1639 trace_softirqs_enabled(curr
));
1642 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1643 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1645 print_irqtrace_events(curr
);
1646 printk("\nother info that might help us debug this:\n");
1647 lockdep_print_held_locks(curr
);
1649 printk("\nstack backtrace:\n");
1656 * Print out an error if an invalid bit is set:
1659 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1660 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1662 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1663 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1667 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1668 enum lock_usage_bit new_bit
);
1670 #ifdef CONFIG_TRACE_IRQFLAGS
1673 * print irq inversion bug:
1676 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1677 struct held_lock
*this, int forwards
,
1678 const char *irqclass
)
1680 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1683 printk("\n=========================================================\n");
1684 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1685 print_kernel_version();
1686 printk( "---------------------------------------------------------\n");
1687 printk("%s/%d just changed the state of lock:\n",
1688 curr
->comm
, curr
->pid
);
1691 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1693 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1694 print_lock_name(other
);
1695 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1697 printk("\nother info that might help us debug this:\n");
1698 lockdep_print_held_locks(curr
);
1700 printk("\nthe first lock's dependencies:\n");
1701 print_lock_dependencies(this->class, 0);
1703 printk("\nthe second lock's dependencies:\n");
1704 print_lock_dependencies(other
, 0);
1706 printk("\nstack backtrace:\n");
1713 * Prove that in the forwards-direction subgraph starting at <this>
1714 * there is no lock matching <mask>:
1717 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1718 enum lock_usage_bit bit
, const char *irqclass
)
1722 find_usage_bit
= bit
;
1723 /* fills in <forwards_match> */
1724 ret
= find_usage_forwards(this->class, 0);
1725 if (!ret
|| ret
== 1)
1728 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1732 * Prove that in the backwards-direction subgraph starting at <this>
1733 * there is no lock matching <mask>:
1736 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1737 enum lock_usage_bit bit
, const char *irqclass
)
1741 find_usage_bit
= bit
;
1742 /* fills in <backwards_match> */
1743 ret
= find_usage_backwards(this->class, 0);
1744 if (!ret
|| ret
== 1)
1747 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1750 void print_irqtrace_events(struct task_struct
*curr
)
1752 printk("irq event stamp: %u\n", curr
->irq_events
);
1753 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1754 print_ip_sym(curr
->hardirq_enable_ip
);
1755 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1756 print_ip_sym(curr
->hardirq_disable_ip
);
1757 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1758 print_ip_sym(curr
->softirq_enable_ip
);
1759 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1760 print_ip_sym(curr
->softirq_disable_ip
);
1763 static int hardirq_verbose(struct lock_class
*class)
1766 return class_filter(class);
1771 static int softirq_verbose(struct lock_class
*class)
1774 return class_filter(class);
1779 #define STRICT_READ_CHECKS 1
1781 static int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
1782 enum lock_usage_bit new_bit
)
1787 case LOCK_USED_IN_HARDIRQ
:
1788 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1790 if (!valid_state(curr
, this, new_bit
,
1791 LOCK_ENABLED_HARDIRQS_READ
))
1794 * just marked it hardirq-safe, check that this lock
1795 * took no hardirq-unsafe lock in the past:
1797 if (!check_usage_forwards(curr
, this,
1798 LOCK_ENABLED_HARDIRQS
, "hard"))
1800 #if STRICT_READ_CHECKS
1802 * just marked it hardirq-safe, check that this lock
1803 * took no hardirq-unsafe-read lock in the past:
1805 if (!check_usage_forwards(curr
, this,
1806 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1809 if (hardirq_verbose(this->class))
1812 case LOCK_USED_IN_SOFTIRQ
:
1813 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1815 if (!valid_state(curr
, this, new_bit
,
1816 LOCK_ENABLED_SOFTIRQS_READ
))
1819 * just marked it softirq-safe, check that this lock
1820 * took no softirq-unsafe lock in the past:
1822 if (!check_usage_forwards(curr
, this,
1823 LOCK_ENABLED_SOFTIRQS
, "soft"))
1825 #if STRICT_READ_CHECKS
1827 * just marked it softirq-safe, check that this lock
1828 * took no softirq-unsafe-read lock in the past:
1830 if (!check_usage_forwards(curr
, this,
1831 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1834 if (softirq_verbose(this->class))
1837 case LOCK_USED_IN_HARDIRQ_READ
:
1838 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1841 * just marked it hardirq-read-safe, check that this lock
1842 * took no hardirq-unsafe lock in the past:
1844 if (!check_usage_forwards(curr
, this,
1845 LOCK_ENABLED_HARDIRQS
, "hard"))
1847 if (hardirq_verbose(this->class))
1850 case LOCK_USED_IN_SOFTIRQ_READ
:
1851 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1854 * just marked it softirq-read-safe, check that this lock
1855 * took no softirq-unsafe lock in the past:
1857 if (!check_usage_forwards(curr
, this,
1858 LOCK_ENABLED_SOFTIRQS
, "soft"))
1860 if (softirq_verbose(this->class))
1863 case LOCK_ENABLED_HARDIRQS
:
1864 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1866 if (!valid_state(curr
, this, new_bit
,
1867 LOCK_USED_IN_HARDIRQ_READ
))
1870 * just marked it hardirq-unsafe, check that no hardirq-safe
1871 * lock in the system ever took it in the past:
1873 if (!check_usage_backwards(curr
, this,
1874 LOCK_USED_IN_HARDIRQ
, "hard"))
1876 #if STRICT_READ_CHECKS
1878 * just marked it hardirq-unsafe, check that no
1879 * hardirq-safe-read lock in the system ever took
1882 if (!check_usage_backwards(curr
, this,
1883 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1886 if (hardirq_verbose(this->class))
1889 case LOCK_ENABLED_SOFTIRQS
:
1890 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1892 if (!valid_state(curr
, this, new_bit
,
1893 LOCK_USED_IN_SOFTIRQ_READ
))
1896 * just marked it softirq-unsafe, check that no softirq-safe
1897 * lock in the system ever took it in the past:
1899 if (!check_usage_backwards(curr
, this,
1900 LOCK_USED_IN_SOFTIRQ
, "soft"))
1902 #if STRICT_READ_CHECKS
1904 * just marked it softirq-unsafe, check that no
1905 * softirq-safe-read lock in the system ever took
1908 if (!check_usage_backwards(curr
, this,
1909 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1912 if (softirq_verbose(this->class))
1915 case LOCK_ENABLED_HARDIRQS_READ
:
1916 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1918 #if STRICT_READ_CHECKS
1920 * just marked it hardirq-read-unsafe, check that no
1921 * hardirq-safe lock in the system ever took it in the past:
1923 if (!check_usage_backwards(curr
, this,
1924 LOCK_USED_IN_HARDIRQ
, "hard"))
1927 if (hardirq_verbose(this->class))
1930 case LOCK_ENABLED_SOFTIRQS_READ
:
1931 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1933 #if STRICT_READ_CHECKS
1935 * just marked it softirq-read-unsafe, check that no
1936 * softirq-safe lock in the system ever took it in the past:
1938 if (!check_usage_backwards(curr
, this,
1939 LOCK_USED_IN_SOFTIRQ
, "soft"))
1942 if (softirq_verbose(this->class))
1954 * Mark all held locks with a usage bit:
1957 mark_held_locks(struct task_struct
*curr
, int hardirq
)
1959 enum lock_usage_bit usage_bit
;
1960 struct held_lock
*hlock
;
1963 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1964 hlock
= curr
->held_locks
+ i
;
1968 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
1970 usage_bit
= LOCK_ENABLED_HARDIRQS
;
1973 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
1975 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
1977 if (!mark_lock(curr
, hlock
, usage_bit
))
1985 * Debugging helper: via this flag we know that we are in
1986 * 'early bootup code', and will warn about any invalid irqs-on event:
1988 static int early_boot_irqs_enabled
;
1990 void early_boot_irqs_off(void)
1992 early_boot_irqs_enabled
= 0;
1995 void early_boot_irqs_on(void)
1997 early_boot_irqs_enabled
= 1;
2001 * Hardirqs will be enabled:
2003 void trace_hardirqs_on(void)
2005 struct task_struct
*curr
= current
;
2008 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2011 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
2014 if (unlikely(curr
->hardirqs_enabled
)) {
2015 debug_atomic_inc(&redundant_hardirqs_on
);
2018 /* we'll do an OFF -> ON transition: */
2019 curr
->hardirqs_enabled
= 1;
2020 ip
= (unsigned long) __builtin_return_address(0);
2022 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2024 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
2027 * We are going to turn hardirqs on, so set the
2028 * usage bit for all held locks:
2030 if (!mark_held_locks(curr
, 1))
2033 * If we have softirqs enabled, then set the usage
2034 * bit for all held locks. (disabled hardirqs prevented
2035 * this bit from being set before)
2037 if (curr
->softirqs_enabled
)
2038 if (!mark_held_locks(curr
, 0))
2041 curr
->hardirq_enable_ip
= ip
;
2042 curr
->hardirq_enable_event
= ++curr
->irq_events
;
2043 debug_atomic_inc(&hardirqs_on_events
);
2046 EXPORT_SYMBOL(trace_hardirqs_on
);
2049 * Hardirqs were disabled:
2051 void trace_hardirqs_off(void)
2053 struct task_struct
*curr
= current
;
2055 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
2058 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2061 if (curr
->hardirqs_enabled
) {
2063 * We have done an ON -> OFF transition:
2065 curr
->hardirqs_enabled
= 0;
2066 curr
->hardirq_disable_ip
= _RET_IP_
;
2067 curr
->hardirq_disable_event
= ++curr
->irq_events
;
2068 debug_atomic_inc(&hardirqs_off_events
);
2070 debug_atomic_inc(&redundant_hardirqs_off
);
2073 EXPORT_SYMBOL(trace_hardirqs_off
);
2076 * Softirqs will be enabled:
2078 void trace_softirqs_on(unsigned long ip
)
2080 struct task_struct
*curr
= current
;
2082 if (unlikely(!debug_locks
))
2085 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2088 if (curr
->softirqs_enabled
) {
2089 debug_atomic_inc(&redundant_softirqs_on
);
2094 * We'll do an OFF -> ON transition:
2096 curr
->softirqs_enabled
= 1;
2097 curr
->softirq_enable_ip
= ip
;
2098 curr
->softirq_enable_event
= ++curr
->irq_events
;
2099 debug_atomic_inc(&softirqs_on_events
);
2101 * We are going to turn softirqs on, so set the
2102 * usage bit for all held locks, if hardirqs are
2105 if (curr
->hardirqs_enabled
)
2106 mark_held_locks(curr
, 0);
2110 * Softirqs were disabled:
2112 void trace_softirqs_off(unsigned long ip
)
2114 struct task_struct
*curr
= current
;
2116 if (unlikely(!debug_locks
))
2119 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2122 if (curr
->softirqs_enabled
) {
2124 * We have done an ON -> OFF transition:
2126 curr
->softirqs_enabled
= 0;
2127 curr
->softirq_disable_ip
= ip
;
2128 curr
->softirq_disable_event
= ++curr
->irq_events
;
2129 debug_atomic_inc(&softirqs_off_events
);
2130 DEBUG_LOCKS_WARN_ON(!softirq_count());
2132 debug_atomic_inc(&redundant_softirqs_off
);
2135 static int mark_irqflags(struct task_struct
*curr
, struct held_lock
*hlock
)
2138 * If non-trylock use in a hardirq or softirq context, then
2139 * mark the lock as used in these contexts:
2141 if (!hlock
->trylock
) {
2143 if (curr
->hardirq_context
)
2144 if (!mark_lock(curr
, hlock
,
2145 LOCK_USED_IN_HARDIRQ_READ
))
2147 if (curr
->softirq_context
)
2148 if (!mark_lock(curr
, hlock
,
2149 LOCK_USED_IN_SOFTIRQ_READ
))
2152 if (curr
->hardirq_context
)
2153 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
))
2155 if (curr
->softirq_context
)
2156 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
))
2160 if (!hlock
->hardirqs_off
) {
2162 if (!mark_lock(curr
, hlock
,
2163 LOCK_ENABLED_HARDIRQS_READ
))
2165 if (curr
->softirqs_enabled
)
2166 if (!mark_lock(curr
, hlock
,
2167 LOCK_ENABLED_SOFTIRQS_READ
))
2170 if (!mark_lock(curr
, hlock
,
2171 LOCK_ENABLED_HARDIRQS
))
2173 if (curr
->softirqs_enabled
)
2174 if (!mark_lock(curr
, hlock
,
2175 LOCK_ENABLED_SOFTIRQS
))
2183 static int separate_irq_context(struct task_struct
*curr
,
2184 struct held_lock
*hlock
)
2186 unsigned int depth
= curr
->lockdep_depth
;
2189 * Keep track of points where we cross into an interrupt context:
2191 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2192 curr
->softirq_context
;
2194 struct held_lock
*prev_hlock
;
2196 prev_hlock
= curr
->held_locks
+ depth
-1;
2198 * If we cross into another context, reset the
2199 * hash key (this also prevents the checking and the
2200 * adding of the dependency to 'prev'):
2202 if (prev_hlock
->irq_context
!= hlock
->irq_context
)
2211 int mark_lock_irq(struct task_struct
*curr
, struct held_lock
*this,
2212 enum lock_usage_bit new_bit
)
2218 static inline int mark_irqflags(struct task_struct
*curr
,
2219 struct held_lock
*hlock
)
2224 static inline int separate_irq_context(struct task_struct
*curr
,
2225 struct held_lock
*hlock
)
2233 * Mark a lock with a usage bit, and validate the state transition:
2235 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
2236 enum lock_usage_bit new_bit
)
2238 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
2241 * If already set then do not dirty the cacheline,
2242 * nor do any checks:
2244 if (likely(this->class->usage_mask
& new_mask
))
2250 * Make sure we didnt race:
2252 if (unlikely(this->class->usage_mask
& new_mask
)) {
2257 this->class->usage_mask
|= new_mask
;
2259 if (!save_trace(this->class->usage_traces
+ new_bit
))
2263 case LOCK_USED_IN_HARDIRQ
:
2264 case LOCK_USED_IN_SOFTIRQ
:
2265 case LOCK_USED_IN_HARDIRQ_READ
:
2266 case LOCK_USED_IN_SOFTIRQ_READ
:
2267 case LOCK_ENABLED_HARDIRQS
:
2268 case LOCK_ENABLED_SOFTIRQS
:
2269 case LOCK_ENABLED_HARDIRQS_READ
:
2270 case LOCK_ENABLED_SOFTIRQS_READ
:
2271 ret
= mark_lock_irq(curr
, this, new_bit
);
2277 * Add it to the global list of classes:
2279 list_add_tail_rcu(&this->class->lock_entry
, &all_lock_classes
);
2280 debug_atomic_dec(&nr_unused_locks
);
2283 if (!debug_locks_off_graph_unlock())
2292 * We must printk outside of the graph_lock:
2295 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
2297 print_irqtrace_events(curr
);
2305 * Initialize a lock instance's lock-class mapping info:
2307 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
2308 struct lock_class_key
*key
, int subclass
)
2310 if (unlikely(!debug_locks
))
2313 if (DEBUG_LOCKS_WARN_ON(!key
))
2315 if (DEBUG_LOCKS_WARN_ON(!name
))
2318 * Sanity check, the lock-class key must be persistent:
2320 if (!static_obj(key
)) {
2321 printk("BUG: key %p not in .data!\n", key
);
2322 DEBUG_LOCKS_WARN_ON(1);
2327 lock
->class_cache
= NULL
;
2329 register_lock_class(lock
, subclass
, 1);
2332 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2335 * This gets called for every mutex_lock*()/spin_lock*() operation.
2336 * We maintain the dependency maps and validate the locking attempt:
2338 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2339 int trylock
, int read
, int check
, int hardirqs_off
,
2342 struct task_struct
*curr
= current
;
2343 struct lock_class
*class = NULL
;
2344 struct held_lock
*hlock
;
2345 unsigned int depth
, id
;
2352 if (unlikely(!debug_locks
))
2355 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2358 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2360 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2361 printk("turning off the locking correctness validator.\n");
2366 class = lock
->class_cache
;
2368 * Not cached yet or subclass?
2370 if (unlikely(!class)) {
2371 class = register_lock_class(lock
, subclass
, 0);
2375 debug_atomic_inc((atomic_t
*)&class->ops
);
2376 if (very_verbose(class)) {
2377 printk("\nacquire class [%p] %s", class->key
, class->name
);
2378 if (class->name_version
> 1)
2379 printk("#%d", class->name_version
);
2385 * Add the lock to the list of currently held locks.
2386 * (we dont increase the depth just yet, up until the
2387 * dependency checks are done)
2389 depth
= curr
->lockdep_depth
;
2390 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2393 hlock
= curr
->held_locks
+ depth
;
2395 hlock
->class = class;
2396 hlock
->acquire_ip
= ip
;
2397 hlock
->instance
= lock
;
2398 hlock
->trylock
= trylock
;
2400 hlock
->check
= check
;
2401 hlock
->hardirqs_off
= hardirqs_off
;
2402 #ifdef CONFIG_LOCK_STAT
2403 hlock
->waittime_stamp
= 0;
2404 hlock
->holdtime_stamp
= sched_clock();
2407 if (check
== 2 && !mark_irqflags(curr
, hlock
))
2410 /* mark it as used: */
2411 if (!mark_lock(curr
, hlock
, LOCK_USED
))
2415 * Calculate the chain hash: it's the combined has of all the
2416 * lock keys along the dependency chain. We save the hash value
2417 * at every step so that we can get the current hash easily
2418 * after unlock. The chain hash is then used to cache dependency
2421 * The 'key ID' is what is the most compact key value to drive
2422 * the hash, not class->key.
2424 id
= class - lock_classes
;
2425 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2428 chain_key
= curr
->curr_chain_key
;
2430 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2435 hlock
->prev_chain_key
= chain_key
;
2436 if (separate_irq_context(curr
, hlock
)) {
2440 chain_key
= iterate_chain_key(chain_key
, id
);
2441 curr
->curr_chain_key
= chain_key
;
2443 if (!validate_chain(curr
, lock
, hlock
, chain_head
))
2446 curr
->lockdep_depth
++;
2447 check_chain_key(curr
);
2448 #ifdef CONFIG_DEBUG_LOCKDEP
2449 if (unlikely(!debug_locks
))
2452 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2454 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2455 printk("turning off the locking correctness validator.\n");
2459 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2460 max_lockdep_depth
= curr
->lockdep_depth
;
2466 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2469 if (!debug_locks_off())
2471 if (debug_locks_silent
)
2474 printk("\n=====================================\n");
2475 printk( "[ BUG: bad unlock balance detected! ]\n");
2476 printk( "-------------------------------------\n");
2477 printk("%s/%d is trying to release lock (",
2478 curr
->comm
, curr
->pid
);
2479 print_lockdep_cache(lock
);
2482 printk("but there are no more locks to release!\n");
2483 printk("\nother info that might help us debug this:\n");
2484 lockdep_print_held_locks(curr
);
2486 printk("\nstack backtrace:\n");
2493 * Common debugging checks for both nested and non-nested unlock:
2495 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2498 if (unlikely(!debug_locks
))
2500 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2503 if (curr
->lockdep_depth
<= 0)
2504 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2510 * Remove the lock to the list of currently held locks in a
2511 * potentially non-nested (out of order) manner. This is a
2512 * relatively rare operation, as all the unlock APIs default
2513 * to nested mode (which uses lock_release()):
2516 lock_release_non_nested(struct task_struct
*curr
,
2517 struct lockdep_map
*lock
, unsigned long ip
)
2519 struct held_lock
*hlock
, *prev_hlock
;
2524 * Check whether the lock exists in the current stack
2527 depth
= curr
->lockdep_depth
;
2528 if (DEBUG_LOCKS_WARN_ON(!depth
))
2532 for (i
= depth
-1; i
>= 0; i
--) {
2533 hlock
= curr
->held_locks
+ i
;
2535 * We must not cross into another context:
2537 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2539 if (hlock
->instance
== lock
)
2543 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2546 lock_release_holdtime(hlock
);
2549 * We have the right lock to unlock, 'hlock' points to it.
2550 * Now we remove it from the stack, and add back the other
2551 * entries (if any), recalculating the hash along the way:
2553 curr
->lockdep_depth
= i
;
2554 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2556 for (i
++; i
< depth
; i
++) {
2557 hlock
= curr
->held_locks
+ i
;
2558 if (!__lock_acquire(hlock
->instance
,
2559 hlock
->class->subclass
, hlock
->trylock
,
2560 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2565 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2571 * Remove the lock to the list of currently held locks - this gets
2572 * called on mutex_unlock()/spin_unlock*() (or on a failed
2573 * mutex_lock_interruptible()). This is done for unlocks that nest
2574 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2576 static int lock_release_nested(struct task_struct
*curr
,
2577 struct lockdep_map
*lock
, unsigned long ip
)
2579 struct held_lock
*hlock
;
2583 * Pop off the top of the lock stack:
2585 depth
= curr
->lockdep_depth
- 1;
2586 hlock
= curr
->held_locks
+ depth
;
2589 * Is the unlock non-nested:
2591 if (hlock
->instance
!= lock
)
2592 return lock_release_non_nested(curr
, lock
, ip
);
2593 curr
->lockdep_depth
--;
2595 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2598 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2600 lock_release_holdtime(hlock
);
2602 #ifdef CONFIG_DEBUG_LOCKDEP
2603 hlock
->prev_chain_key
= 0;
2604 hlock
->class = NULL
;
2605 hlock
->acquire_ip
= 0;
2606 hlock
->irq_context
= 0;
2612 * Remove the lock to the list of currently held locks - this gets
2613 * called on mutex_unlock()/spin_unlock*() (or on a failed
2614 * mutex_lock_interruptible()). This is done for unlocks that nest
2615 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2618 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2620 struct task_struct
*curr
= current
;
2622 if (!check_unlock(curr
, lock
, ip
))
2626 if (!lock_release_nested(curr
, lock
, ip
))
2629 if (!lock_release_non_nested(curr
, lock
, ip
))
2633 check_chain_key(curr
);
2637 * Check whether we follow the irq-flags state precisely:
2639 static void check_flags(unsigned long flags
)
2641 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2645 if (irqs_disabled_flags(flags
))
2646 DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
);
2648 DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
);
2651 * We dont accurately track softirq state in e.g.
2652 * hardirq contexts (such as on 4KSTACKS), so only
2653 * check if not in hardirq contexts:
2655 if (!hardirq_count()) {
2656 if (softirq_count())
2657 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2659 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2663 print_irqtrace_events(current
);
2668 * We are not always called with irqs disabled - do that here,
2669 * and also avoid lockdep recursion:
2671 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2672 int trylock
, int read
, int check
, unsigned long ip
)
2674 unsigned long flags
;
2676 if (unlikely(!lock_stat
&& !prove_locking
))
2679 if (unlikely(current
->lockdep_recursion
))
2682 raw_local_irq_save(flags
);
2685 current
->lockdep_recursion
= 1;
2686 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2687 irqs_disabled_flags(flags
), ip
);
2688 current
->lockdep_recursion
= 0;
2689 raw_local_irq_restore(flags
);
2692 EXPORT_SYMBOL_GPL(lock_acquire
);
2694 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2696 unsigned long flags
;
2698 if (unlikely(!lock_stat
&& !prove_locking
))
2701 if (unlikely(current
->lockdep_recursion
))
2704 raw_local_irq_save(flags
);
2706 current
->lockdep_recursion
= 1;
2707 __lock_release(lock
, nested
, ip
);
2708 current
->lockdep_recursion
= 0;
2709 raw_local_irq_restore(flags
);
2712 EXPORT_SYMBOL_GPL(lock_release
);
2714 #ifdef CONFIG_LOCK_STAT
2716 print_lock_contention_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2719 if (!debug_locks_off())
2721 if (debug_locks_silent
)
2724 printk("\n=================================\n");
2725 printk( "[ BUG: bad contention detected! ]\n");
2726 printk( "---------------------------------\n");
2727 printk("%s/%d is trying to contend lock (",
2728 curr
->comm
, curr
->pid
);
2729 print_lockdep_cache(lock
);
2732 printk("but there are no locks held!\n");
2733 printk("\nother info that might help us debug this:\n");
2734 lockdep_print_held_locks(curr
);
2736 printk("\nstack backtrace:\n");
2743 __lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
2745 struct task_struct
*curr
= current
;
2746 struct held_lock
*hlock
, *prev_hlock
;
2747 struct lock_class_stats
*stats
;
2751 depth
= curr
->lockdep_depth
;
2752 if (DEBUG_LOCKS_WARN_ON(!depth
))
2756 for (i
= depth
-1; i
>= 0; i
--) {
2757 hlock
= curr
->held_locks
+ i
;
2759 * We must not cross into another context:
2761 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2763 if (hlock
->instance
== lock
)
2767 print_lock_contention_bug(curr
, lock
, ip
);
2771 hlock
->waittime_stamp
= sched_clock();
2773 point
= lock_contention_point(hlock
->class, ip
);
2775 stats
= get_lock_stats(hlock
->class);
2776 if (point
< ARRAY_SIZE(stats
->contention_point
))
2777 stats
->contention_point
[i
]++;
2778 put_lock_stats(stats
);
2782 __lock_acquired(struct lockdep_map
*lock
)
2784 struct task_struct
*curr
= current
;
2785 struct held_lock
*hlock
, *prev_hlock
;
2786 struct lock_class_stats
*stats
;
2792 depth
= curr
->lockdep_depth
;
2793 if (DEBUG_LOCKS_WARN_ON(!depth
))
2797 for (i
= depth
-1; i
>= 0; i
--) {
2798 hlock
= curr
->held_locks
+ i
;
2800 * We must not cross into another context:
2802 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2804 if (hlock
->instance
== lock
)
2808 print_lock_contention_bug(curr
, lock
, _RET_IP_
);
2812 if (!hlock
->waittime_stamp
)
2815 now
= sched_clock();
2816 waittime
= now
- hlock
->waittime_stamp
;
2817 hlock
->holdtime_stamp
= now
;
2819 stats
= get_lock_stats(hlock
->class);
2821 lock_time_inc(&stats
->read_waittime
, waittime
);
2823 lock_time_inc(&stats
->write_waittime
, waittime
);
2824 put_lock_stats(stats
);
2827 void lock_contended(struct lockdep_map
*lock
, unsigned long ip
)
2829 unsigned long flags
;
2831 if (unlikely(!lock_stat
))
2834 if (unlikely(current
->lockdep_recursion
))
2837 raw_local_irq_save(flags
);
2839 current
->lockdep_recursion
= 1;
2840 __lock_contended(lock
, ip
);
2841 current
->lockdep_recursion
= 0;
2842 raw_local_irq_restore(flags
);
2844 EXPORT_SYMBOL_GPL(lock_contended
);
2846 void lock_acquired(struct lockdep_map
*lock
)
2848 unsigned long flags
;
2850 if (unlikely(!lock_stat
))
2853 if (unlikely(current
->lockdep_recursion
))
2856 raw_local_irq_save(flags
);
2858 current
->lockdep_recursion
= 1;
2859 __lock_acquired(lock
);
2860 current
->lockdep_recursion
= 0;
2861 raw_local_irq_restore(flags
);
2863 EXPORT_SYMBOL_GPL(lock_acquired
);
2867 * Used by the testsuite, sanitize the validator state
2868 * after a simulated failure:
2871 void lockdep_reset(void)
2873 unsigned long flags
;
2876 raw_local_irq_save(flags
);
2877 current
->curr_chain_key
= 0;
2878 current
->lockdep_depth
= 0;
2879 current
->lockdep_recursion
= 0;
2880 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2881 nr_hardirq_chains
= 0;
2882 nr_softirq_chains
= 0;
2883 nr_process_chains
= 0;
2885 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2886 INIT_LIST_HEAD(chainhash_table
+ i
);
2887 raw_local_irq_restore(flags
);
2890 static void zap_class(struct lock_class
*class)
2895 * Remove all dependencies this lock is
2898 for (i
= 0; i
< nr_list_entries
; i
++) {
2899 if (list_entries
[i
].class == class)
2900 list_del_rcu(&list_entries
[i
].entry
);
2903 * Unhash the class and remove it from the all_lock_classes list:
2905 list_del_rcu(&class->hash_entry
);
2906 list_del_rcu(&class->lock_entry
);
2910 static inline int within(void *addr
, void *start
, unsigned long size
)
2912 return addr
>= start
&& addr
< start
+ size
;
2915 void lockdep_free_key_range(void *start
, unsigned long size
)
2917 struct lock_class
*class, *next
;
2918 struct list_head
*head
;
2919 unsigned long flags
;
2922 raw_local_irq_save(flags
);
2926 * Unhash all classes that were created by this module:
2928 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2929 head
= classhash_table
+ i
;
2930 if (list_empty(head
))
2932 list_for_each_entry_safe(class, next
, head
, hash_entry
)
2933 if (within(class->key
, start
, size
))
2938 raw_local_irq_restore(flags
);
2941 void lockdep_reset_lock(struct lockdep_map
*lock
)
2943 struct lock_class
*class, *next
;
2944 struct list_head
*head
;
2945 unsigned long flags
;
2948 raw_local_irq_save(flags
);
2951 * Remove all classes this lock might have:
2953 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2955 * If the class exists we look it up and zap it:
2957 class = look_up_lock_class(lock
, j
);
2962 * Debug check: in the end all mapped classes should
2966 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2967 head
= classhash_table
+ i
;
2968 if (list_empty(head
))
2970 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2971 if (unlikely(class == lock
->class_cache
)) {
2972 if (debug_locks_off_graph_unlock())
2981 raw_local_irq_restore(flags
);
2984 void lockdep_init(void)
2989 * Some architectures have their own start_kernel()
2990 * code which calls lockdep_init(), while we also
2991 * call lockdep_init() from the start_kernel() itself,
2992 * and we want to initialize the hashes only once:
2994 if (lockdep_initialized
)
2997 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
2998 INIT_LIST_HEAD(classhash_table
+ i
);
3000 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
3001 INIT_LIST_HEAD(chainhash_table
+ i
);
3003 lockdep_initialized
= 1;
3006 void __init
lockdep_info(void)
3008 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3010 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
3011 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
3012 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
3013 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
3014 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
3015 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
3016 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
3018 printk(" memory used by lock dependency info: %lu kB\n",
3019 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
3020 sizeof(struct list_head
) * CLASSHASH_SIZE
+
3021 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
3022 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
3023 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
3025 printk(" per task-struct memory footprint: %lu bytes\n",
3026 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
3028 #ifdef CONFIG_DEBUG_LOCKDEP
3029 if (lockdep_init_error
)
3030 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
3034 static inline int in_range(const void *start
, const void *addr
, const void *end
)
3036 return addr
>= start
&& addr
<= end
;
3040 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
3041 const void *mem_to
, struct held_lock
*hlock
)
3043 if (!debug_locks_off())
3045 if (debug_locks_silent
)
3048 printk("\n=========================\n");
3049 printk( "[ BUG: held lock freed! ]\n");
3050 printk( "-------------------------\n");
3051 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3052 curr
->comm
, curr
->pid
, mem_from
, mem_to
-1);
3054 lockdep_print_held_locks(curr
);
3056 printk("\nstack backtrace:\n");
3061 * Called when kernel memory is freed (or unmapped), or if a lock
3062 * is destroyed or reinitialized - this code checks whether there is
3063 * any held lock in the memory range of <from> to <to>:
3065 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
3067 const void *mem_to
= mem_from
+ mem_len
, *lock_from
, *lock_to
;
3068 struct task_struct
*curr
= current
;
3069 struct held_lock
*hlock
;
3070 unsigned long flags
;
3073 if (unlikely(!debug_locks
))
3076 local_irq_save(flags
);
3077 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
3078 hlock
= curr
->held_locks
+ i
;
3080 lock_from
= (void *)hlock
->instance
;
3081 lock_to
= (void *)(hlock
->instance
+ 1);
3083 if (!in_range(mem_from
, lock_from
, mem_to
) &&
3084 !in_range(mem_from
, lock_to
, mem_to
))
3087 print_freed_lock_bug(curr
, mem_from
, mem_to
, hlock
);
3090 local_irq_restore(flags
);
3092 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
3094 static void print_held_locks_bug(struct task_struct
*curr
)
3096 if (!debug_locks_off())
3098 if (debug_locks_silent
)
3101 printk("\n=====================================\n");
3102 printk( "[ BUG: lock held at task exit time! ]\n");
3103 printk( "-------------------------------------\n");
3104 printk("%s/%d is exiting with locks still held!\n",
3105 curr
->comm
, curr
->pid
);
3106 lockdep_print_held_locks(curr
);
3108 printk("\nstack backtrace:\n");
3112 void debug_check_no_locks_held(struct task_struct
*task
)
3114 if (unlikely(task
->lockdep_depth
> 0))
3115 print_held_locks_bug(task
);
3118 void debug_show_all_locks(void)
3120 struct task_struct
*g
, *p
;
3124 if (unlikely(!debug_locks
)) {
3125 printk("INFO: lockdep is turned off.\n");
3128 printk("\nShowing all locks held in the system:\n");
3131 * Here we try to get the tasklist_lock as hard as possible,
3132 * if not successful after 2 seconds we ignore it (but keep
3133 * trying). This is to enable a debug printout even if a
3134 * tasklist_lock-holding task deadlocks or crashes.
3137 if (!read_trylock(&tasklist_lock
)) {
3139 printk("hm, tasklist_lock locked, retrying... ");
3142 printk(" #%d", 10-count
);
3146 printk(" ignoring it.\n");
3150 printk(" locked it.\n");
3152 do_each_thread(g
, p
) {
3153 if (p
->lockdep_depth
)
3154 lockdep_print_held_locks(p
);
3156 if (read_trylock(&tasklist_lock
))
3158 } while_each_thread(g
, p
);
3161 printk("=============================================\n\n");
3164 read_unlock(&tasklist_lock
);
3167 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
3169 void debug_show_held_locks(struct task_struct
*task
)
3171 if (unlikely(!debug_locks
)) {
3172 printk("INFO: lockdep is turned off.\n");
3175 lockdep_print_held_locks(task
);
3178 EXPORT_SYMBOL_GPL(debug_show_held_locks
);